From patchwork Fri Jul 5 02:58:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724385 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DDE81C693; Fri, 5 Jul 2024 02:58:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148331; cv=none; b=MJtceDrQxAIDna02ANxIC33FflzSqonX4ceBJjh5XzBheFS+Aq/r6K6Y03mptObxp3nalzYIKbAKLI0A3na0yws8wyW6wzJlAzMkSXJUKEUq9uESrPwYukmTOWOOORzIwNCiLoniQQ4FeCuOsRmfEZyx+wi9cByJot3tFSoCE5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148331; c=relaxed/simple; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kGUTo69HOz0eAprp87Db4P9lsrmsuz9kBuEGtMC297QX3duMZWhOzeLopDH/XCFIAic2v4GcGb3kQgNbr12DCLETYP/8mJU2+DuEDuSg2FGp8rPjcYQermhUC929yjUo+XI6QTG3zcoVzCesCxGjxQXhpOotgtUFK7CAt11w8dM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=us8zZ8TL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="us8zZ8TL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDE90C4AF0D; Fri, 5 Jul 2024 02:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148330; bh=bAX1OLxg4DkksZvjRAzn/L1GhmwINGXrUi0DgLiNpt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=us8zZ8TL6HY58PbFV2b4kKvqeofS9BDLqTUALIpmnkmDJ8IAbUjUDIg0xjQlbayZI xGTQ2Fm6BIOHs6Zk/lznk7jhMBhOFkW7fJzUHOiSfBGzHDhes8+v1k9spoRSHubUOL r6f/C44JWHVetoAeMVaPyqLS2kpbU9HL1V8gOc9LZejxeKEcXMAglNhk0wX1PxhLHm B1YOCChqfCk57nc4eBKWkn5U/7EafGlfWJZ1WSisl4zmo244f7gfWADbw1aAORLwgn 6nblum7YtWyDC+Pj2F2WrLX77DnZIayK5e4y7ADOtPKmKIhtRR7/ElzTuVmrVmdlh5 dQk4ShznAb6og== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 1/9] selftests/bpf: Add backlog for network_helper_opts Date: Fri, 5 Jul 2024 10:58:23 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Some callers expect __start_server() helper to pass their own "backlog" value to listen() instead of the default of 1. So this patch adds struct member "backlog" for network_helper_opts to allow callers to set "backlog" value via start_server_str() helper. listen(fd, 0 /* backlog */) can be used to enforce syncookie. Meaning backlog 0 is a legit value. Using 0 as a default and changing it to 1 here is fine. It makes the test program easier to write for the common case. Enforcing syncookie mode by using backlog 0 is a niche use case but it should at least have a way for the caller to do that. Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 2 +- tools/testing/selftests/bpf/network_helpers.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 44c2c8fa542a..e0cba4178e41 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -106,7 +106,7 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl } if (type == SOCK_STREAM) { - if (listen(fd, 1) < 0) { + if (listen(fd, opts->backlog ? MAX(opts->backlog, 0) : 1) < 0) { log_err("Failed to listed on socket"); goto error_close; } diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 9ea36524b9db..4f26bfc2dbf5 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -25,6 +25,10 @@ struct network_helper_opts { int timeout_ms; bool must_fail; int proto; + /* The backlog argument for listen(), defines the maximum length to which + * the queue of pending connections for sockfd may grow. + */ + int backlog; int (*post_socket_cb)(int fd, void *opts); void *cb_opts; }; From patchwork Fri Jul 5 02:58:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724386 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AC8F1643D; Fri, 5 Jul 2024 02:58:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148335; cv=none; b=IABOEQDrIYgoKErNwWBJFhuIYCXzzLmJdZNMkorc/6qKXSuZdWXB+TsMC2EduYn6LhBjNdGgs2SVnmZqTJTBgT2s7/6sKIRsbS7nlRYiMLILkuJ+boP0jOBYCq04cJ6rjKT9h6+z7kNm2yhuAqhDtZf33lYxWxOTDHJyfT5c/qI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148335; c=relaxed/simple; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jcuiHI9H7yxS3FEIj6V2wieWqch77VY6wd/mqct87QwF42QflAVl08OWWeMA+l8WbMifYgH1FKs+JEZNKwE6cPLAISQUPU3B8PziKbSy03q3DraOOge6VILeE+LxhkAdCgGQHjGapdqFwn3zD9m8f5FH1Qf179MGoYRcNi3HzEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ipgo53tQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ipgo53tQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4581BC3277B; Fri, 5 Jul 2024 02:58:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148335; bh=00pdNC+8+bJCChvMSP7B2ks/Ry/OoNnOXIemGC5RruM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ipgo53tQlW4NqqT9JKFtucStZVJhFykS0i3lubCnGuNUjNTjtfZOjNsDI/e6VDQdG TmAun2WWVObAeDWA53PR4RsDnePyz8ADYS/WfwjXH9FZ5v7RyY4D09tr4BJ+5Bqybx wm+UX4wKB+3Jyr/J3TinPcBU8d1joV/qaZN+CP9q8wTEYnRm1tWSjHrYMlE8zUIeBN bKfJ0B5/Z+b12vH/PTiRdqZFkvqwsW1D4+UbH8sbrpfZHfhECr+bnrvBN/JjwuI04U +k+hcHFDfikBEhpqiJzjXNY4fAZ1l6NqtIKwRHDeV63AG/ihtFhr5BVmKqUU26kMF8 6G0TKB+xbiyuw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 2/9] selftests/bpf: Use start_server_str in sockmap_ktls Date: Fri, 5 Jul 2024 10:58:24 +0800 Message-ID: <65e49479affc051c12a45b37be219af293095fe7.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server_str() instead of local defined function tcp_server(). This can avoid duplicate code. Technically, this is not a one-for-one replacement, as start_server_str() also does bind(). But the difference does not seem to matter. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 2d0796314862..32be112967a5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -6,25 +6,11 @@ #include #include "test_progs.h" +#include "network_helpers.h" #define MAX_TEST_NAME 80 #define TCP_ULP 31 -static int tcp_server(int family) -{ - int err, s; - - s = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - return -1; - - err = listen(s, SOMAXCONN); - if (!ASSERT_OK(err, "listen")) - return -1; - - return s; -} - static int disconnect(int fd) { struct sockaddr unspec = { AF_UNSPEC }; @@ -35,12 +21,15 @@ static int disconnect(int fd) /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) { + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + }; struct sockaddr_storage addr = {0}; socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; - srv = tcp_server(family); - if (srv == -1) + srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); + if (!ASSERT_GE(srv, 0, "start_server_str")) return; err = getsockname(srv, (struct sockaddr *)&addr, &len); From patchwork Fri Jul 5 02:58:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724387 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBF638003B; Fri, 5 Jul 2024 02:58:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148340; cv=none; b=ZoOAVYtuwFGEhacv2glZeRKftTgf7fcwHQ6iKsSnyDbAolKMNHqLwZGk76p/YMnrtxAGewQdjBK19eBVjbHRefYcqDoOlgpbDrjJJd25q465zjBQozhGqRfKN0gnj7/4lZQKYCqSyfqZC2EYJv+kvgk9wIVT64JxutxAFib/wLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148340; c=relaxed/simple; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S7wLnzIxaG0nIH1i1LC9ok9ezID986lAwjh7rAhlzD3bvxoBSUKRZurxm914m6nJ+dvOkspG14xx7XHHqiVxuTwl2gbRQe2w1iR6I597B7ArYOnXA6aU/A0kU9NKfz6OCmCsH+LJhiBPwFmXwnQqthxhEcimhVFuvNAZRIT5zHI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I541pEWT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="I541pEWT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1793C32781; Fri, 5 Jul 2024 02:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148339; bh=Qq9hn27IvadosGSaFEY1IhNuXDt86aHiR/dUXu2AMTk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I541pEWTKzQjEJYp7yVnV+6IAi5JrpjY6vbyBQDQNRlx3FTiyJPyjd+M5sKb6aKl9 QzLnB+rPV3orChnmujuCKPbtfK9NnMhgmz3vJPsw/N8ZRTBvKmIEC78CUmT10s8UHh XjqTdhNiHExDd5hERmq+1gWs80jAaU+xquOy6o1OOEql5IE5k6p1a4au54F7eRf/n/ zgF3yuQhG0v67hsmhnr8PdZJPWmvF9bfKHPzORgue1VJs2HSTu5Nn2DrbugmVhmzM4 aUvumvxLHv6nmv14xhtCuB17U/HcBjrajZDM6Ckk6HxLqo2m8re0+aKsCxHs5+4ZzT 8/M+RyyTNu2tA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 3/9] selftests/bpf: Use connect_to_fd_opts in sockmap_ktls Date: Fri, 5 Jul 2024 10:58:25 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use public network helper connect_to_fd_opts() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index 32be112967a5..a794aa688ab5 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -24,26 +24,16 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) struct network_helper_opts opts = { .backlog = SOMAXCONN, }; - struct sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; srv = start_server_str(family, SOCK_STREAM, NULL, 0, &opts); if (!ASSERT_GE(srv, 0, "start_server_str")) return; - err = getsockname(srv, (struct sockaddr *)&addr, &len); - if (!ASSERT_OK(err, "getsockopt")) - goto close_srv; - - cli = socket(family, SOCK_STREAM, 0); - if (!ASSERT_GE(cli, 0, "socket")) + cli = connect_to_fd_opts(srv, SOCK_STREAM, NULL); + if (!ASSERT_GE(cli, 0, "connect_to_fd_opts")) goto close_srv; - err = connect(cli, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto close_cli; - err = bpf_map_update_elem(map, &zero, &cli, 0); if (!ASSERT_OK(err, "bpf_map_update_elem")) goto close_cli; From patchwork Fri Jul 5 02:58:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724388 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEE491643D; Fri, 5 Jul 2024 02:59:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148344; cv=none; b=RD2t9pMavPCYzld4J5ySqAF6Mprkb7gSA7PR7a3HiHkM9fYcFhd1XK8OI0731Gp7GFCI68NNfP5dUqc1ZrlbBmbL3wEx4tC6OLIG4nfRJcqZUrZyjMxJEcXjme0Noee5BQfCExei/0J6sUwykAoQ42aDtVp35007SPRaJH6Cx/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148344; c=relaxed/simple; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BP7DDIm9U5/6kzbgBtqXYHdEivnh6R5Z2kk57em34v80FxyT8Zk+ZSYc47WWGo5sy0QxBbOKdre8Meb4oi31sVf3U54nSykRlsaWml1RKma1dR+10Vd1VIOZaZjAVKwuV54oDH7Uz8VzbnPxaNRzRRaeD9KAnuhXC/oUgzgMhl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IQqbbg+s; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IQqbbg+s" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46C5FC4AF0B; Fri, 5 Jul 2024 02:59:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148344; bh=rRP0ZiPRY0W17MVOWxZwWV5aU8LBd+K/G2ydWK7Wq+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IQqbbg+sk0QyTpBtSmxKzH0Rmp8QEorxtlRf5inn+QutNg9Q5QKGQRP53UVmZ52LK nx8cWcax9WQCdhOikz5w/KONP3wWqQ/vdBWCq+ALwtQTt3aGq302DdPRQi/0Sn78N8 qGIZXpt9xTSukWt325qVTaBtQpZQFkYey1YhufRhwfD/Gh9X3IxK51sbQB3J5OLYkX Q2Jc1VftlbO3YsFqLoReZuy2m7Y8X9aELSeKrIx6Bs6KYvNQBS0JKnyiRf552Cc8z6 PoHZW2RrjgUi08rAraFnD6oQqTiSo1xSozv1fMpg/7Zb262wFCtl03KWiNkprZkDaO AAXf+8SgoyFtA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 4/9] selftests/bpf: Use make_sockaddr in sockmap_ktls Date: Fri, 5 Jul 2024 10:58:26 +0800 Message-ID: <7125742ccf22a673a9ea66fa11eed0be24301516.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper make_sockaddr() exported in network_helpers.h instead of open-coding in sockmap_ktls.c. This can avoid duplicate code. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c index a794aa688ab5..3c223601e8b6 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -59,23 +59,11 @@ static void test_sockmap_ktls_update_fails_when_sock_has_ulp(int family, int map { struct sockaddr_storage addr = {}; socklen_t len = sizeof(addr); - struct sockaddr_in6 *v6; - struct sockaddr_in *v4; int err, s, zero = 0; - switch (family) { - case AF_INET: - v4 = (struct sockaddr_in *)&addr; - v4->sin_family = AF_INET; - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *)&addr; - v6->sin6_family = AF_INET6; - break; - default: - PRINT_FAIL("unsupported socket family %d", family); + err = make_sockaddr(family, NULL, 0, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return; - } s = socket(family, SOCK_STREAM, 0); if (!ASSERT_GE(s, 0, "socket")) From patchwork Fri Jul 5 02:58:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724389 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26A4A132139; Fri, 5 Jul 2024 02:59:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148350; cv=none; b=OQGQ2DVzNUOCPbakpOlEK3gsbSNgz+44hBPt9RlvSfW5yweyfSxH9qbvwrAFSEnMI/s4WUxJRWUkmqUxfCD3AGfaiBiv/Eekc4Dpcoy6GXN5dolNYlJHFrlrACxpd1eBLR2rPrJ4683ZNKVUPJfoPOOd/20XncSlotsvg3Sf3ZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148350; c=relaxed/simple; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UZ7TSi6LIlBaN+4UJnA+vivYMgWXl+uU2gIBRSi5HuLplVey3EUc5MWUalU5xSOBSSeppAc1cg9gVETy8lgK9vAEpKhGgNogNcwqEkPng8O2hOrjT54pK2ZN2JQb+aZjVjwdrlezw70wehgx6TkhL3vZTFDkGKNxIAKCBg3MYxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NlLZixcQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NlLZixcQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE136C32781; Fri, 5 Jul 2024 02:59:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148348; bh=JAWAcKR7zBXRC50szWd1XFpZsq7hHNsF3ZdFSJ3411U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NlLZixcQhR6BN7yQaXc142HrOusxN2+SP9SGUJOWBkoITjBuOQHWyDL0ZolQ381SR 6vxh3PS5ze06baC7N6SJK/8qeb3aJc7mVNZ2tZw1r239Wq51dcSgDEEsj/UYRQa9ZN TZ5O0CT6llFyRhXouIILvwwKHZPddvt1/Os2wt0bdo08327cmYQHfE4lfhX40dV7Wy uY0OBDKRyPYSDOS783VNGWEAtlkfUNVXkbM/j0ob82AcAIKE82x65q/6++3w3YGk3t xE/ALICJDwlYEBdzc8iK7pgNy0gETedPa3d/uyAlvzzTKJYFSHfQBy8ijaC+lrw4zM 6FP+Ztw+KI/zA== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 5/9] selftests/bpf: Close fd in error path in drop_on_reuseport Date: Fri, 5 Jul 2024 10:58:27 +0800 Message-ID: <5104fcc71dd7a7e18716bf468dcbe82d8571af94.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Server 1 fd should be closed in the error path when update_lookup_map() fails. This patch fixes it by goto "close_srv1" instead of "detach" lable in that case. Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 597d0467a926..de2466547efe 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -994,7 +994,7 @@ static void drop_on_reuseport(const struct test *t) err = update_lookup_map(t->sock_map, SERVER_A, server1); if (err) - goto detach; + goto close_srv1; /* second server on destination address we should never reach */ server2 = make_server(t->sotype, t->connect_to.ip, t->connect_to.port, From patchwork Fri Jul 5 02:58:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724390 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A37B5136E3E; Fri, 5 Jul 2024 02:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148353; cv=none; b=mrcdKkIRcrSvyJU/LaozT5aib6mrN0DhTy9Pce61dtRHaWBt6ahSaPXhE9KEz0vOl1KM29D9QMeWbmIVHNSVV61OG2+MiEmXHEnuSz84ZkCBJ1AaBYQWb5ct7zZIu2ZjEVBVURiJN9hUkyIVP5c5YkxIWDp7v1wrSgBqOEKAUwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148353; c=relaxed/simple; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LRWFCDI6F+S0dplG8Q/mgf9ThhPF92baMQY8KEJo+j3+3tqrLRbTLfBCc7W1SFsRKzcG0ZnPqxLff+8gwnoqHdnbaXW/3kdBR9aNEtjxWvwUGrc8Rh4ZJJQPkR6Qc8m3B0I9ydabE2Of+LBDUiU1aPVhKyWDZAiivHzzt5nUynY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jY8unmBp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jY8unmBp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42B09C3277B; Fri, 5 Jul 2024 02:59:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148353; bh=3p/deRrkgPGTdGxSapLPunN1pNLrCJ3NVhjsTWR8QBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jY8unmBpwohzTvg7kjNRtbVXHK4+l+IDiuFCSnJZSv5O/Hk1LRgRZp/nQ0sEJv4tt QR91YxbEdf5UZFZ3xzVZtF8HqVO3DcmPh0YcT2ankDmeGO8N6vdWTxFYWqb6GoP1Vy 4jHCK48K3BWAU8cMlxGPTmiyx93wbrUc5d3AlyHI/fatzQuMPcjv2ie80hsSNto5SQ fRDQz+BhpiUKJf7Jbu848cPYYWyKwqzFSctY5+WX6viYuuWG0Y1a4STViCt/IIM5iu T8i3Lh+fsjk7cD81ubaYv5fJTxaRLm3BGv727HCnhZIGxXb1fwjxPpY6aYsvbZ02MX h9g1ZuZxT3D+w== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 6/9] selftests/bpf: Use start_server_str in sk_lookup Date: Fri, 5 Jul 2024 10:58:28 +0800 Message-ID: <243103c6240fcc1303638ef5f6aad3bef9dc4078.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper start_server_addr() instead of local defined function make_server() in prog_tests/sk_lookup.c to avoid duplicate code. Add a helper setsockopts() to set SOL_CUSTOM sockopt looply, set it to setsockopt pointer of struct network_helper_opts, and pass it to start_server_addr(). Acked-by: Eduard Zingerman Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index de2466547efe..d03ea3e64a2e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -77,6 +77,12 @@ struct test { bool reuseport_has_conns; /* Add a connected socket to reuseport group */ }; +struct cb_opts { + int family; + int sotype; + bool reuseport; +}; + static __u32 duration; /* for CHECK macro */ static bool is_ipv6(const char *ip) @@ -142,19 +148,14 @@ static int make_socket(int sotype, const char *ip, int port, return fd; } -static int make_server(int sotype, const char *ip, int port, - struct bpf_program *reuseport_prog) +static int setsockopts(int fd, void *opts) { - struct sockaddr_storage addr = {0}; + struct cb_opts *co = (struct cb_opts *)opts; const int one = 1; - int err, fd = -1; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int err = 0; /* Enabled for UDPv6 sockets for IPv4-mapped IPv6 to work. */ - if (sotype == SOCK_DGRAM) { + if (co->sotype == SOCK_DGRAM) { err = setsockopt(fd, SOL_IP, IP_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IP_RECVORIGDSTADDR)", "failed\n")) { @@ -163,7 +164,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_DGRAM && addr.ss_family == AF_INET6) { + if (co->sotype == SOCK_DGRAM && co->family == AF_INET6) { err = setsockopt(fd, SOL_IPV6, IPV6_RECVORIGDSTADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(IPV6_RECVORIGDSTADDR)", "failed\n")) { @@ -172,7 +173,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (sotype == SOCK_STREAM) { + if (co->sotype == SOCK_STREAM) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEADDR)", "failed\n")) { @@ -181,7 +182,7 @@ static int make_server(int sotype, const char *ip, int port, } } - if (reuseport_prog) { + if (co->reuseport) { err = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); if (CHECK(err, "setsockopt(SO_REUSEPORT)", "failed\n")) { @@ -190,19 +191,28 @@ static int make_server(int sotype, const char *ip, int port, } } - err = bind(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "bind", "failed\n")) { - log_err("failed to bind listen socket"); - goto fail; - } +fail: + return err; +} - if (sotype == SOCK_STREAM) { - err = listen(fd, SOMAXCONN); - if (CHECK(err, "make_server", "listen")) { - log_err("failed to listen on port %d", port); - goto fail; - } - } +static int make_server(int sotype, const char *ip, int port, + struct bpf_program *reuseport_prog) +{ + struct cb_opts cb_opts = { + .family = is_ipv6(ip) ? AF_INET6 : AF_INET, + .sotype = sotype, + .reuseport = reuseport_prog, + }; + struct network_helper_opts opts = { + .backlog = SOMAXCONN, + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (!ASSERT_GE(fd, 0, "start_server_str")) + return -1; /* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) { From patchwork Fri Jul 5 02:58:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724391 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B44C136E3E; Fri, 5 Jul 2024 02:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148358; cv=none; b=prto/nOREZhfMRqsqv8K53ISEsiCVlimLjjE2rOG5fVLg2roU+EhGSAAcCGRpe4yCdwfpA5oDNttMyDiYPkObfwv1R0CN5ViEOcyi48zYFZRZZonGDzmVsgFljBwsvT0oYDuXWwbfUqX/b9aZ1tmWEMoe/fAmovsg/kP6U5bQ1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148358; c=relaxed/simple; bh=yIL0fQNsMx5ijF7GWUBR64Zkm01hjeVcW0TnosV2/ds=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y7LEyZLOzpscpU4ED0/kCsa6QLSQRPPHzhLwo1v0In5NyxfuGwI16z2wbaZgz7UD8nC8GCIcPWM+RezcoZIrRUW2so2tUoS1MPHzfAGNX86SNWnlVmdavW2yAk/D6szP982OKefkH0ntkGCEO4W0XeGkwXvKdQSkm7AjSqgYWmg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WBjG8NV0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WBjG8NV0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE542C32781; Fri, 5 Jul 2024 02:59:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148357; bh=yIL0fQNsMx5ijF7GWUBR64Zkm01hjeVcW0TnosV2/ds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WBjG8NV0YCkK/cOXME/VLAzFqweb8xLBwJyQSVPw2NLaFJu1qU+HmF/rtlfEDq01D g/TvhlGaeZ1IAwWZ9mhKVErbZ6huPWlBazACUJN7wi5hIanwFCNq1IX6Rp+PO9GYBo ak2tDbaodNCoKsdUsadVjUMcUT309/PbWWo1fSpNXVrtz5y+Tvwjf4bNjSTSWAvE7M rzwosECe2uUO3zowv4yGpw0s5feCATdJQdCbLMyu9Pd9pWrxsUO4WuLmWNtHWibn0u L5mkFTHPp/xd7VN3lxdsm0cN9CWkFAjfT+lEtydOfBOOgzGqYzCYVuYIb8UvuftZfh MNr6u4jS71nyw== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 7/9] selftests/bpf: Use connect_to_fd_opts in sk_lookup Date: Fri, 5 Jul 2024 10:58:29 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch uses public helper connect_to_fd_opts() exported in network_helpers.h instead of using make_socket() and connect() in prog_tests/sk_lookup.c. This can simplify the code. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index d03ea3e64a2e..ef4a3db34c5f 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -874,7 +874,9 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int client_fd, server_fd, err; struct bpf_link *lookup_link; ssize_t n; @@ -888,12 +890,11 @@ static void drop_on_lookup(const struct test *t) if (server_fd < 0) goto detach; - client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client_fd = connect_to_fd_opts(server_fd, t->sotype, &opts); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -988,7 +989,9 @@ static void test_drop_on_lookup(struct test_sk_lookup *skel) static void drop_on_reuseport(const struct test *t) { - struct sockaddr_storage dst = { 0 }; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; int client, server1, server2, err; struct bpf_link *lookup_link; ssize_t n; @@ -1012,12 +1015,11 @@ static void drop_on_reuseport(const struct test *t) if (server2 < 0) goto close_srv1; - client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + client = connect_to_fd_opts(server2, t->sotype, &opts); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1158,13 +1160,16 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, int sotype) { int err, client_fd, connected_fd, server_fd; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; struct bpf_link *lookup_link; server_fd = make_server(sotype, EXT_IP4, EXT_PORT, NULL); if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = connect_to_fd_opts(server_fd, sotype, &opts); if (connected_fd < 0) goto out_close_server; @@ -1178,7 +1183,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, goto out_close_connected; /* Try to redirect TCP SYN / UDP packet to a connected socket */ - client_fd = make_client(sotype, EXT_IP4, EXT_PORT); + client_fd = connect_to_fd_opts(server_fd, sotype, &opts); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Fri Jul 5 02:58:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724392 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B1D7225AE; Fri, 5 Jul 2024 02:59:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148362; cv=none; b=G9UZDPvU9/B85xJvnyNiwsDCtd/3kwTkNFHKS8+7i2/aUDoKxeI3IiRHon5OdQ8Vjhsmqmf3zVWoNja/5WnE6J3cyb3rlFlgllsYZneVn78Zu48da/hxnEviQL/kRjAdzXssP7+yQgZlRZb/g5Vfe5IFfAIyadBu8uLCJPw9xS8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148362; c=relaxed/simple; bh=B7hg/lyFr/T3CIqXns6zB9hcYkz6130EeWwLxus1WNs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i8RmneGVuXbGXAOB7xAtKBFspjjrfTOGWA6IuDPlpGliltR3kznbVp9/rzAlkFf+4m+hY7e+8hz/WVVGsQ1t1/YtXBpnXVbVAlQa848zYu6gAgMsXSrkvSyD7jDInT/k3kzPPNnKxeQqXTjsY2NP0J7R0T9HCGZCXg/jL+v9YD0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jcnGBD+9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jcnGBD+9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45670C3277B; Fri, 5 Jul 2024 02:59:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148362; bh=B7hg/lyFr/T3CIqXns6zB9hcYkz6130EeWwLxus1WNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jcnGBD+9XGnefj7PhzpcE54oyqbkT9fRGwsooMSNI3t0s2NTnUQa4u9V6sQhMXGUK /3DbqMlGuiNq32/pNJmzl/e6SV85nCfOQZSKkDvtI8vJo4x4FntasqHSwhUdM4ag2q 0bfmyoBxydmjHqVmTPqEeXNvQnguu79WbaZ5X1AFjkrP9GBn+a+gZBoIPMiCAvbaYA aRAeaUhnSL/mLDORkUZJv21dZaf2JNStPdnqZSA2PqkKLaDCicxUYzaaL59QqA8r3w LIPTyaYwYbt7fk/Xl0Legavdki2u1Jz5kqLzR9nNuBsisrAnonTf4PT5imRcSWi8KU 6SrjXPr4IKfBg== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 8/9] selftests/bpf: Use connect_to_addr in sk_lookup Date: Fri, 5 Jul 2024 10:58:30 +0800 Message-ID: <52c22148c66184baf5ba09d60c06d49a8a33d743.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use public network helpers make_sockaddr() and connect_to_addr() instead of using the local defined function make_socket() and connect(). This make_socket() can be dropped latter. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index ef4a3db34c5f..a436ed8b34e0 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -231,23 +231,19 @@ static int make_server(int sotype, const char *ip, int port, static int make_client(int sotype, const char *ip, int port) { + int family = is_ipv6(ip) ? AF_INET6 : AF_INET; + struct network_helper_opts opts = { + .timeout_ms = IO_TIMEOUT_SEC, + }; struct sockaddr_storage addr = {0}; - int err, fd; + socklen_t len; + int err; - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (!ASSERT_OK(err, "make_sockaddr")) return -1; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { - log_err("failed to connect client socket"); - goto fail; - } - - return fd; -fail: - close(fd); - return -1; + return connect_to_addr(sotype, &addr, len, &opts); } static __u64 socket_cookie(int fd) From patchwork Fri Jul 5 02:58:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13724393 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2013A224D5; Fri, 5 Jul 2024 02:59:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148367; cv=none; b=i6xvqnnUzH1J+iBR4/zyYdChI3l+KmN2v94YrHSiOt3NrwiktGcteeMOxLegvibiu6SMGvCZ+khYrpS0ltgg5XfDx/Fb7VbVPJqFtOJNGGy1o8Tk3T8JjsOmG+BX61SYZ4gHyahAj4Tx7JF0ih8oubNxm6tdpbV1AcYpXfKCJXg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720148367; c=relaxed/simple; bh=kA/m27sKB231+X3zvUUlslhNKjZRSzqGNK+eksVxZH4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lsWQK87m+hpJAOt/7YNZco2QeYOT5pHIITk5vdxfKYB9b7NjgthZIiktwCtTYMey1gONqO9KgWhMvYI/JPdftg0LyowIeNmVYsszPFFFni+awQVzaLPH/w+GmdbH6kXvH+u1YoLV7nbYgEHVmEnFX7eU/jLqRTOmCXnYEuD2IA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bGgH1JCN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bGgH1JCN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2421C4AF0B; Fri, 5 Jul 2024 02:59:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720148366; bh=kA/m27sKB231+X3zvUUlslhNKjZRSzqGNK+eksVxZH4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bGgH1JCN+i3FRlaE7KTwuGUdusseaNNEkSa/28gnxaOaW/OpKQD6ERtTxt1h0jWgP Ku8MKVH6PpIlTpyFitPgAbYbvbTS2d52K5BD91GFzx6htolvDcziasrsJyimRjvKi2 t8SWE8JLPmft+e0n9ycIeNrGEcA3GFFI54GlkLEuRXdJUP4+28G79NisE/m/A5gdMc P7UOIJY+84KUORfne8JVMMuC4J1W6vCpscefZyPlkmQyQi72j1YlQsoLPxYk5sdwX0 5gq7wK8gnaXFdgL6QP/TB3oAmWNkAWix0F3L+g5wghxc8L5LsnF/rd33EiNhG78x3p VZs7Epgewcu/Q== From: Geliang Tang To: Andrii Nakryiko , Eduard Zingerman , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v8 9/9] selftests/bpf: Drop make_socket in sk_lookup Date: Fri, 5 Jul 2024 10:58:31 +0800 Message-ID: <8eb2034f81a493fc3688759f59104b6e7fd6a91e.1720147953.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang Use local helper make_client() instead of using make_socket() and connect(). Then make_socket() and inetaddr_len() can be dropped now. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 45 +------------------ 1 file changed, 2 insertions(+), 43 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index a436ed8b34e0..c18bdcd1099e 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,46 +108,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; } -static socklen_t inetaddr_len(const struct sockaddr_storage *addr) -{ - return (addr->ss_family == AF_INET ? sizeof(struct sockaddr_in) : - addr->ss_family == AF_INET6 ? sizeof(struct sockaddr_in6) : 0); -} - -static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr) -{ - struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; - int err, family, fd; - - family = is_ipv6(ip) ? AF_INET6 : AF_INET; - err = make_sockaddr(family, ip, port, addr, NULL); - if (CHECK(err, "make_address", "failed\n")) - return -1; - - fd = socket(addr->ss_family, sotype, 0); - if (CHECK(fd < 0, "socket", "failed\n")) { - log_err("failed to make socket"); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_SNDTIMEO)", "failed\n")) { - log_err("failed to set SNDTIMEO"); - close(fd); - return -1; - } - - err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); - if (CHECK(err, "setsockopt(SO_RCVTIMEO)", "failed\n")) { - log_err("failed to set RCVTIMEO"); - close(fd); - return -1; - } - - return fd; -} - static int setsockopts(int fd, void *opts) { struct cb_opts *co = (struct cb_opts *)opts; @@ -1228,7 +1188,6 @@ struct test_multi_prog { static void run_multi_prog_lookup(const struct test_multi_prog *t) { - struct sockaddr_storage dst = {}; int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; @@ -1261,11 +1220,11 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (err) goto out_close_server; - client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); + client_fd = make_client(SOCK_STREAM, EXT_IP4, EXT_PORT); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = 0; if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;