From nobody Mon Aug 08 21:10:45 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4M1pl95hSBz4Y98F; Mon, 8 Aug 2022 21:10:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4M1pl95Fvvz3tvT; Mon, 8 Aug 2022 21:10:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1659993045; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=J8i1tWvn3aRG05o3wDDV3gRhKETQARst/4JKS8jwrUE=; b=EThJAofHoXgZ9cK38YYOthHVjzaa3BkH2wtRQfzfPbAg2YzoP95omfZzrR5dh3Zt9+AzEe EdXvMihzqLJfmLfWY3k7+MWzjF6DY9GZ90bv9x/FNYcadPb9i/YsDuI2P5b0g+BWsrAm+b YwBlHz2Z0q6+/5hmp3RAZ9WWNTbjfkrcJFox4rzVKtBev3mTMOYGdaaNHUzJ9s4vJvhsZu 8mlkA8rP3N9RSESUa56md419F8CZxK6yfyeyeKtn2CG1e1j5vyXDRRLl2pEFFEoJoUnUJ2 wGfP8Y0iP364qfFv96gaCBVKBVK5U4T8zAknJFZhFwvoohSiW9R5DFxIDSyIGQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4M1pl941YGz15Yw; Mon, 8 Aug 2022 21:10:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 278LAjbv069427; Mon, 8 Aug 2022 21:10:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 278LAj6H069426; Mon, 8 Aug 2022 21:10:45 GMT (envelope-from git) Date: Mon, 8 Aug 2022 21:10:45 GMT Message-Id: <202208082110.278LAj6H069426@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: e87ff1ea2252 - main - tests/socket: add accept_filter(9) test List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e87ff1ea225246e1d983c4c2c1ecfd0119fd138c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1659993045; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=J8i1tWvn3aRG05o3wDDV3gRhKETQARst/4JKS8jwrUE=; b=N+RkGPg5gfuxTl6oUyXJy8Mj3mQ+9fL2ZgtmliwJbJEvfyed6e21wd5KvBck4oMb+1YT0/ dYiws4WjIpzJSxXnMYwrRjy7dJCxn/SqsofRgDXHCDceA3DZJg3NsfkVCfiNE8WbG+aXB1 Mq4E0q34sh2l5sOclQfJoBZci4G0ticPL/GNSDfVRVAIklqxudYrI1/Vy1TCznSBXAaBgU IK3bjcAcyd1d7k49pBFaEr3FScyitt18JRsLkvAgUOODQTIrjozL+iiJuvElTznO275d8W 0eZpjlRcV8TVRX+kZBJR2Z0P9Zur2AGdtL7wxPQNy/vIRzaLf83dRi988xVfxw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1659993045; a=rsa-sha256; cv=none; b=ZD8Bzetm1l34d/iS2azBTOxdAP3QExY5WSOvY/fYPYvgVjHtw3D9Ab/tIvIaU9gLd9zI4X MfPX800K+FbL6rDc12U6D7RAopAsW9Pafn2U4DN2ThwIm01219IUeKKCcI9dU66cB8UAs/ DAuQoNCHryJHbhJ6LQaobSqjK5xWsVn1jaqx1Xbyw38Xy+/Du1tYAeSgbfKwxKIa0K2XDr bgap1mBbgJM9jeEFu7+xi9kzy5kw/GgJZ+JYfRGMorymKHtNnn6WMjgiC/84pJ8OfK/T0j bZfY+G/ldj0fwfYC7s2eMV7az7B1nR75FfTIgqLHt7JYe+f8bWHythg1B2hwBA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=e87ff1ea225246e1d983c4c2c1ecfd0119fd138c commit e87ff1ea225246e1d983c4c2c1ecfd0119fd138c Author: Gleb Smirnoff AuthorDate: 2022-08-08 21:10:18 +0000 Commit: Gleb Smirnoff CommitDate: 2022-08-08 21:10:18 +0000 tests/socket: add accept_filter(9) test Test basic functionality of accf_data(9) and accf_http(9) --- tests/sys/kern/Makefile | 1 + tests/sys/kern/socket_accf.c | 160 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index 3891d78f7629..00baa5c99fae 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -29,6 +29,7 @@ ATF_TESTS_C+= reaper ATF_TESTS_C+= sched_affinity ATF_TESTS_C+= sigaltstack ATF_TESTS_C+= sigwait +ATF_TESTS_C+= socket_accf ATF_TESTS_C+= socket_msg_trunc TEST_METADATA.sigwait+= is_exclusive="true" .if ${MACHINE_ARCH} != "i386" && ${MACHINE_ARCH:Mpowerpc*} == "" diff --git a/tests/sys/kern/socket_accf.c b/tests/sys/kern/socket_accf.c new file mode 100644 index 000000000000..384062810c8d --- /dev/null +++ b/tests/sys/kern/socket_accf.c @@ -0,0 +1,160 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2022 Gleb Smirnoff + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include + +static int +listensock(struct sockaddr_in *sin) +{ + int l; + + ATF_REQUIRE((l = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(fcntl(l, F_SETFL, O_NONBLOCK) != -1); + ATF_REQUIRE(setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, + sizeof(int)) == 0); + *sin = (struct sockaddr_in){ + .sin_len = sizeof(sin), + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(INADDR_LOOPBACK), + }; + ATF_REQUIRE(bind(l, (struct sockaddr *)sin, sizeof(*sin)) == 0); + ATF_REQUIRE(getsockname(l, (struct sockaddr *)sin, + &(socklen_t){ sizeof(*sin) }) == 0); + ATF_REQUIRE(listen(l, -1) == 0); + + return (l); +} + +static int +clientsock(struct sockaddr_in *sin) +{ + int s; + + ATF_REQUIRE((s = socket(PF_INET, SOCK_STREAM, 0)) > 0); + ATF_REQUIRE(connect(s, (struct sockaddr *)sin, sizeof(*sin)) == 0); + + return (s); +} + +static void +accfon(int l, struct accept_filter_arg *af) +{ + + if (setsockopt(l, SOL_SOCKET, SO_ACCEPTFILTER, af, sizeof(*af)) != 0) { + if (errno == ENOENT) + atf_tc_skip("Accept filter %s not loaded in kernel", + af->af_name); + else + atf_tc_fail("setsockopt(SO_ACCEPTFILTER): %s", + strerror(errno)); + } +} + +/* + * XXX: return from send(2) on a localhost connection doesn't guarantee that + * netisr has fully processed and delivered the data to the remote local + * socket. Sleep a fraction of second to "guarantee" that it did. + */ +static ssize_t +usend(int s, const void *msg, size_t len) +{ + ssize_t rv; + + rv = send(s, msg, len, 0); + usleep(100000); + return (rv); +} + +ATF_TC_WITHOUT_HEAD(data); +ATF_TC_BODY(data, tc) +{ + struct accept_filter_arg afa = { + .af_name = "dataready" + }; + struct sockaddr_in sin; + int l, s, a; + + l = listensock(&sin); + accfon(l, &afa); + s = clientsock(&sin); + ATF_REQUIRE(accept(l, NULL, 0) == -1); + ATF_REQUIRE(errno == EAGAIN); + ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo")); + ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); +} + +ATF_TC_WITHOUT_HEAD(http); +ATF_TC_BODY(http, tc) +{ + struct accept_filter_arg afa = { + .af_name = "httpready" + }; + struct sockaddr_in sin; + int l, s, a; + + l = listensock(&sin); + accfon(l, &afa); + s = clientsock(&sin); + + /* 1) No data. */ + ATF_REQUIRE(accept(l, NULL, 0) == -1); + ATF_REQUIRE(errno == EAGAIN); + + /* 2) Data, that doesn't look like HTTP. */ + ATF_REQUIRE(usend(s, "foo", sizeof("foo")) == sizeof("foo")); + ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); + + close(s); + close(a); + +#define CHUNK1 "GET / " +#define CHUNK2 "HTTP/1.0\r\n\n" +#define LEN(c) (sizeof(c) - 1) + + /* 3) Partial HTTP. */ + s = clientsock(&sin); + ATF_REQUIRE(usend(s, CHUNK1, LEN(CHUNK1)) == LEN(CHUNK1)); + ATF_REQUIRE(accept(l, NULL, 0) == -1); + ATF_REQUIRE(errno == EAGAIN); + + /* 4) Complete HTTP. */ + ATF_REQUIRE(usend(s, CHUNK2, LEN(CHUNK2)) == LEN(CHUNK2)); + ATF_REQUIRE((a = accept(l, NULL, 0)) > 0); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, data); + ATF_TP_ADD_TC(tp, http); + + return (atf_no_error()); +}