From patchwork Fri Jun 21 10:35: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: 13707246 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 0C214171662; Fri, 21 Jun 2024 10:35:48 +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=1718966149; cv=none; b=HWmDL6mowZE7LkJsE0R9Tu5KsMT3Tcgsv9golUkoj1dGoNzUCDAacSOj4KRp73+7qHBbEJz827bjJPJ9YpGTNX4Rp5BeLs093mn9pVCgRYADm0N/a7NoYAtxsOSD1vyTVUlXYFxssM/56WIywBtLD3kgngh6APvPIweOuhQN4+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966149; c=relaxed/simple; bh=SKyyCdL528nLERGcSxTQAVRM3f1L6kcDJj8MadPAgFU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PwGzxvfgaQaZzRh/rOAaoO6eUlbkymRmZm2xRmbNBJ3qiyvNhc3mFt39FcioMcM9XtkwxrFjLfEuJJLBNSB23/40VLsUQUiMYTj8wikIi9hW2UcWyUjtTliA4xv/r5vI7f699FWxxw7j3xMG0oxudwT9SGEvdNKvqYhzB+IxSqU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SLv1s1hI; 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="SLv1s1hI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 115A4C2BBFC; Fri, 21 Jun 2024 10:35:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966148; bh=SKyyCdL528nLERGcSxTQAVRM3f1L6kcDJj8MadPAgFU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SLv1s1hIHomfbvFsRi1MCeQnZPIkmMag7BGXhS6rlkN4dFVOm4jGjLmtmM1rEOy/2 G/8v6JI9jQ+zLhc5QAWac+5BEDl9xgwwubv9jOhSOMUFBoccoGLn14YU+7NxLEK6sN +mc8o1I+MQPqGBQlqVOz3iEWRgpboT2IA4SZHeQYcgfmarlljtr1q097a4oUDZDxYW KQuNwszGuSipHSvnY/pGQ+fzyO6rGv0M2qS8ar8RIaGs2abnVEDlPTjXXF7cZCRKSG rYWJaMHMLSCiZMJSxYiwWhiHYuRUpRZVwneR7dad7qnlHxwgJmE7AYYjanN5DuKG5y 55+Ll0MlhfEAQ== 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 1/6] selftests/bpf: Use start_server in sockmap_ktls Date: Fri, 21 Jun 2024 18:35:24 +0800 Message-ID: <37b9e2f3c61b4bb73326f6c4e63d562433b59255.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Include network_helpers.h in prog_tests/sockmap_ktls.c, use public network helper start_server() instead of the local defined function tcp_server(). This can avoid duplicate code. Signed-off-by: Geliang Tang Acked-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/sockmap_ktls.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 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..9cc881b38669 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 }; @@ -39,7 +25,7 @@ static void test_sockmap_ktls_disconnect_after_delete(int family, int map) socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; - srv = tcp_server(family); + srv = start_server(family, SOCK_STREAM, NULL, 0, 0); if (srv == -1) return; From patchwork Fri Jun 21 10:35: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: 13707247 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 1B40E171676; Fri, 21 Jun 2024 10:35:53 +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=1718966154; cv=none; b=q1LjnG7vVDP7ec3xYC/7xmmE0JrOYudw4SQQA0X8CpOv87hvPynNBMCG35kIiGF3j15T0HDjbBaIaUebMFIxELTTNqdaXMcwc85eHSTuOqMYrAMEJwetdAzDqAnMOeUN6ghcpC9StpfcKr9qLBlHPl0CGT6U6tAic7weWGxwCEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966154; c=relaxed/simple; bh=7CMKoJFRAWlWFOBTWpSXqbxq7VKgG6yr7QvPcXK6Enk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PLi4aFrtQ1ceIjEStj8jlzc1j+M90pEBXEBMfrzvNMsCLviHL6vYL2xUT/CD5xIvwVt9cQyPTMIfKLqIluCdE0uk4+c7CppDsYk0I+DbBQVIX9UjnxeKbLT6XHt3cHNY8/+FKGL2aPLTItN8gsqX+0PT4vNFrE53LewWEoFmz0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T4aLu2ZV; 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="T4aLu2ZV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B95CC4AF0A; Fri, 21 Jun 2024 10:35:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966153; bh=7CMKoJFRAWlWFOBTWpSXqbxq7VKgG6yr7QvPcXK6Enk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4aLu2ZVqsdbqYYhFcgNUrrF3a6I+w/d6vtFjYTvBs7SSd5s1kE0XV4AMHBiCAmqW MR+f1uHGpro5cViBopAy+nf81Ivjja7SX5s05CDi1lkj4cnR9j9YRqTt7CzoKx0NWX 8y3lo1QO7LNz3rGZfIm0+C1GY3IwGujglQCdv6bAOx5q0rUp8d8x+LJnUzXt9st+y/ Rg/4X/sEND55D59BS1LPL8EgCuuiWDy2lOylpAjdFUMaxmi2iv8RJFYId51EYfCKgM n1AwNHMRmvAl5uedBN4zKUogux2Jf8WfhJLepqeKhW/r9CQ91ADp8oQJXpaaE9bJYQ uC1OjsBmkUmIw== 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 2/6] selftests/bpf: Use connect_to_fd in sockmap_ktls Date: Fri, 21 Jun 2024 18:35:25 +0800 Message-ID: <86e6562a9bad3c44ce80c1c38ff27940f177651f.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Use public network helper connect_to_fd() instead of open-coding it in prog_tests/sockmap_ktls.c. This can avoid duplicate code. Signed-off-by: Geliang Tang Acked-by: Eduard Zingerman --- .../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 9cc881b38669..68867028c432 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_ktls.c @@ -21,26 +21,16 @@ 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 sockaddr_storage addr = {0}; - socklen_t len = sizeof(addr); int err, cli, srv, zero = 0; srv = start_server(family, SOCK_STREAM, NULL, 0, 0); if (srv == -1) 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(srv, 0); + if (!ASSERT_GE(cli, 0, "connect_to_fd")) 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 Jun 21 10:35: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: 13707248 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 C73B981723; Fri, 21 Jun 2024 10:35:58 +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=1718966158; cv=none; b=bONWbcKbUVbU6v6mrMoJuSmk3IWOO0IbktljFLwTvlHQ5ir8ZWhxS0PVedTcFpAICzOl8hdlgVnx3skfkiIz4HtrtY2oASYEORDTKnQWHCWPNBeVfF3oBhtcmc+tCFhn4ckkcdxeiogfSlTPnNgy9nhhfPdEyA5ikI46KDiCnws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966158; c=relaxed/simple; bh=87jb8EGZgPF/paszI1bqGdv16ecuTJaKpY9O20F+rtc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HxChF3e/WXt/qO9JMulZqdx8vJjwC5VbL4Bb+IY4qSy+NzZkbYVHfYGtzoszA4SYxwrXxc3UHxf9XncqWb22uQPwj1iooiX/k7aNL2xlW5Rhw2zEhinqIphWK/qENoBMlGhgVzHvRdckV58IiG+HpkowmV0CafDwciVd2nPGluU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J8xjwDSL; 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="J8xjwDSL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 47768C4AF0A; Fri, 21 Jun 2024 10:35:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966158; bh=87jb8EGZgPF/paszI1bqGdv16ecuTJaKpY9O20F+rtc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J8xjwDSLzO+/ybf/nTjIz/cQP3SFr4yLRkwlG441tCAzWLkYnJPY22JUPFsuylT/a a8tJ/OJBcMekQmTomzw4vuyGGVaViy8uTBhJt1flz1/gTjd4qiZMQpMdduoMkPqu4e xuCNncinnbBudFDUedH7xOFKWfNmf4mZXIyPQ0JYzp2yutRIFOpbO2saD5yEvcUYVH +hQqi7MbNecmdISZc6BmwRVSeYZQNrealwYi8OZhd7jbHSmLV/JEGcuplxzCBZUYFU wyAVKnHGPYThWctVnm0y+QKUETOJNvF6DUc8l7b/4QfsEXuuq2b6o6nKugCoa49P+f cQCSuGx+kjpRg== 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 3/6] selftests/bpf: Use start_server_str in sk_lookup Date: Fri, 21 Jun 2024 18:35:26 +0800 Message-ID: <519199c91c2b6adf52c5cc35ddb81c7be45c85df.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper start_server_addr() in 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(). Signed-off-by: Geliang Tang Acked-by: Eduard Zingerman --- .../selftests/bpf/prog_tests/sk_lookup.c | 57 +++++++++++-------- 1 file changed, 33 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 597d0467a926..ba73b56f4484 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,27 @@ 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 = { + .post_socket_cb = setsockopts, + .cb_opts = &cb_opts, + }; + int err, fd = -1; + + fd = start_server_str(cb_opts.family, sotype, ip, port, &opts); + if (CHECK(fd < 0, "start_server_addr", "failed\n")) + return -1; /* Late attach reuseport prog so we can have one init path */ if (reuseport_prog) { From patchwork Fri Jun 21 10:35: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: 13707249 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 758F017166A; Fri, 21 Jun 2024 10:36:03 +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=1718966163; cv=none; b=FkDKyYJfmYWqIVvS0pjsY2jytOtJYJQTyqLuFa/g+4t1o4A8IVPuVP2jR7KZjxvOrVLqWXO22RPztgCVqScnjY5QiBkKFdv2aO8xt1Ex6II4RWtrG+h+G3QGmY/lgj0QgCV47oJUp3l/mdTY7NziEKjUw+ORK2P8PZtQUEVVXeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966163; c=relaxed/simple; bh=obi93RcFYsnTiQvAxwes3xclDEbY46BXn9dP7uPO/qk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pGvjldXI60Mfdf4Grge1Uk/7Z1fvd0gHsOf4Q0v6JQZv2OcfMuMB0nfyo94U5yvAwuJQZuy9utP3C3rWYE+X1+MdD382JbCfZPRYJ5J34XZszc95mCe+1AQ+X9QZxeUipK4W4qBxewxlF08Gm7wLzDcE0HUUo5haxcb/jJJAKnk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=O0YJ+4CY; 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="O0YJ+4CY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF4FDC2BBFC; Fri, 21 Jun 2024 10:35:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966163; bh=obi93RcFYsnTiQvAxwes3xclDEbY46BXn9dP7uPO/qk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O0YJ+4CYvgubvKXRVHJv1GycVNbidpmzcDXiJIyA2UVyKXSAbJ4gF1gACC13kHRW1 v/xuoOc10lBYbQyO0qTJGGieHZ6mdxfAj6lrvzri9+AqMDw2i/vwjG453EKaLoKN5K rko2C0GlXGZOMRYUup+5+j5XEoG9jkqQ6XgnmYaShbdsOZ+ssBPStSVJGbX9Vnc9zv 3g/HYJ31/BuYwMkTx8sROtxyjeyQHagbOGV96Ce/Hi8vGACnMwzqXFEJ9SN3NMn++8 RVW3TaaovJ5+JBAzLVZLfENQvx0jFRWcq9a0LGAx42jy8ncpC2YpEZD/zLedhCYzT+ J5u4x0LnE6T8g== 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 4/6] selftests/bpf: Drop inetaddr_len in sk_lookup Date: Fri, 21 Jun 2024 18:35:27 +0800 Message-ID: <13aec50e1faa04dc497e9394fd4ff5384532c7fc.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch adds a new parameter "len" for make_socket() to get the length of the address by make_sockaddr(), then local helper inetaddr_len() can be dropped. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index ba73b56f4484..2fc2b928e168 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -108,20 +108,14 @@ 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 sockaddr_storage *addr, socklen_t *len) { 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); + err = make_sockaddr(family, ip, port, addr, len); if (CHECK(err, "make_address", "failed\n")) return -1; @@ -231,13 +225,14 @@ static int make_server(int sotype, const char *ip, int port, static int make_client(int sotype, const char *ip, int port) { struct sockaddr_storage addr = {0}; + socklen_t len; int err, fd; - fd = make_socket(sotype, ip, port, &addr); + fd = make_socket(sotype, ip, port, &addr, &len); if (fd < 0) return -1; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); + err = connect(fd, (void *)&addr, len); if (CHECK(err, "make_client", "connect")) { log_err("failed to connect client socket"); goto fail; @@ -876,6 +871,7 @@ static void drop_on_lookup(const struct test *t) struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; + socklen_t len; ssize_t n; lookup_link = attach_lookup_prog(t->lookup_prog); @@ -888,11 +884,11 @@ static void drop_on_lookup(const struct test *t) goto detach; client_fd = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + t->connect_to.port, &dst, &len); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = connect(client_fd, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -990,6 +986,7 @@ static void drop_on_reuseport(const struct test *t) struct sockaddr_storage dst = { 0 }; int client, server1, server2, err; struct bpf_link *lookup_link; + socklen_t len; ssize_t n; lookup_link = attach_lookup_prog(t->lookup_prog); @@ -1012,11 +1009,11 @@ static void drop_on_reuseport(const struct test *t) goto close_srv1; client = make_socket(t->sotype, t->connect_to.ip, - t->connect_to.port, &dst); + t->connect_to.port, &dst, &len); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); + err = connect(client, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1230,6 +1227,7 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) int map_fd, server_fd, client_fd; struct bpf_link *link1, *link2; int prog_idx, done, err; + socklen_t len; map_fd = bpf_map__fd(t->run_map); @@ -1259,11 +1257,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_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst, &len); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); + err = connect(client_fd, (void *)&dst, len); if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client; From patchwork Fri Jun 21 10:35: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: 13707250 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 3E01274416; Fri, 21 Jun 2024 10:36:07 +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=1718966168; cv=none; b=e3NGugfB0Rscf+4Am6SwHpLt29P9XZSDSjXliD6S357BVz+LgxBwyU1M5QRGIiWUAHj8MmmfVFpsSuVqV6VPMU1dW1927jwQObE85rgYvTU6Xmy18IZMjgVUK8eYd7rhhNr1V+Yu4mgtCeMLvzndtKUYiIXUNHZ417K35p/MxcI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966168; c=relaxed/simple; bh=WzGOMLewYd5PMcP0rKoHd/PUDB+GjRgLRtoznjoAL1k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W8LSlLV4m8Dk7QU9zQcjeiOFVl1HQVR6nnhJZLqxdvcKF5lX7nsEyW/k0HeIKGr6AQFtj1n8FrIM6/rnZuqU84EDYWNbOsRVc5Lt3Y5P26IWXnRyZTDhps39B7R2Y6fyyYqbXIDKSDN3aBmELR0QIW9afNE2Kanf+D1fLjllsB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iUkeVpJy; 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="iUkeVpJy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AAD6BC32781; Fri, 21 Jun 2024 10:36:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966167; bh=WzGOMLewYd5PMcP0rKoHd/PUDB+GjRgLRtoznjoAL1k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUkeVpJykFr+2VvDbNKR7HEktVUtwtw4TwbMaDkbVIHDHMKbJfytJznwONcJ9Va7w ttCpTN92kTkO1znFTqhXMssUDy7tjJDEPoNJTnKRvdkQN6L3C4fRIxJ+/focowzOD+ J+835Sf8e6APtzjuO4zYKKzFDQVP4hocxDKd7VkcAA1jM7LR/AgjQnebT/0PjNs/CZ xwpAWhJetBaKuvdPHa4npSwTkGGy9avWz+guCdzyhe2Ej0atRdX7UNvu9nfegtzOF8 UnFx5sn2MByDIYnC7844+Js29Ew/zQpmhJHnIbdkMPfYudqDMxeXIQhkodxw+ywAQR 2TQeUpCN9JBLA== 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 5/6] selftests/bpf: Use connect_to_fd in sk_lookup Date: Fri, 21 Jun 2024 18:35:28 +0800 Message-ID: <588b96d36b36f88095802d28a6b236842e30cb96.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang This patch uses public helper connect_to_fd() 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 | 33 +++++-------------- 1 file changed, 9 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 2fc2b928e168..0f2e8a4c1524 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -868,10 +868,8 @@ static void test_redirect_lookup(struct test_sk_lookup *skel) static void drop_on_lookup(const struct test *t) { - struct sockaddr_storage dst = {}; int client_fd, server_fd, err; struct bpf_link *lookup_link; - socklen_t len; ssize_t n; lookup_link = attach_lookup_prog(t->lookup_prog); @@ -883,12 +881,10 @@ 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, &len); + client_fd = connect_to_fd(server_fd, 0); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -983,10 +979,8 @@ 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 }; int client, server1, server2, err; struct bpf_link *lookup_link; - socklen_t len; ssize_t n; lookup_link = attach_lookup_prog(t->lookup_prog); @@ -1008,12 +1002,10 @@ 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, &len); + client = connect_to_fd(server2, 0); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, len); if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1160,7 +1152,7 @@ static void run_sk_assign_connected(struct test_sk_lookup *skel, if (server_fd < 0) return; - connected_fd = make_client(sotype, EXT_IP4, EXT_PORT); + connected_fd = connect_to_fd(server_fd, 0); if (connected_fd < 0) goto out_close_server; @@ -1174,7 +1166,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(server_fd, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { @@ -1223,11 +1215,9 @@ 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; - socklen_t len; map_fd = bpf_map__fd(t->run_map); @@ -1257,17 +1247,13 @@ 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, &len); - if (client_fd < 0) - goto out_close_server; - - err = connect(client_fd, (void *)&dst, len); - if (CHECK(err && !t->expect_errno, "connect", + client_fd = connect_to_fd(server_fd, 0); + if (CHECK(client_fd < 0 && !t->expect_errno, "connect", "unexpected error %d\n", errno)) - goto out_close_client; - if (CHECK(err && t->expect_errno && errno != t->expect_errno, + goto out_close_server; + if (CHECK(client_fd < 0 && t->expect_errno && errno != t->expect_errno, "connect", "unexpected error %d\n", errno)) - goto out_close_client; + goto out_close_server; done = 0; prog_idx = PROG1; @@ -1281,7 +1267,6 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) CHECK(err, "bpf_map_lookup_elem", "failed\n"); CHECK(!done, "bpf_map_lookup_elem", "PROG2 !done\n"); -out_close_client: close(client_fd); out_close_server: close(server_fd); From patchwork Fri Jun 21 10:35: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: 13707251 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 F26F316D33A; Fri, 21 Jun 2024 10:36:12 +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=1718966173; cv=none; b=Elf0h3P1kjTJckHXh0fkLLHL9QmbRq7Kk2veLQ001OJEo0inA2Mxg9ahsTS7ekTdpJuDmvvlylGiDsyj3pAlG1md19zEGoqaMIN8oAS/lK6nuH/Pv82yRSSfd5r5dVL0y8aDrr43px/4tKljjOLtMFW0sev78uFYgzCDXFp1+0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966173; c=relaxed/simple; bh=1y7UwnSWe3/0q0L9cNLNBb4Omt7+3nhYnY3r1wJsGOw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vEunLFzweynNs85+RYN+I1MjzPW50/9+r9FKmdMZ8JW50Zwvp/RbPFoW9JE/Znq+bf819EIwNoRinibNs6M0WeMQLCk1tyPaMYe8X89BNNODgjc1RDfoB4MFztYRxhhx394a9IyrKKB0hZ6qW6+qoubcuA6PNuIL3Mqdt5Okm5o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SHP1sJ6A; 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="SHP1sJ6A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65E06C2BBFC; Fri, 21 Jun 2024 10:36:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718966172; bh=1y7UwnSWe3/0q0L9cNLNBb4Omt7+3nhYnY3r1wJsGOw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SHP1sJ6ATXFnwqLlo3Kjho02rcHp0QdtDqaAoMM7IUexYP7hjUOfE3+qNKAwStq/m v5mRgt5Aq27CwU1cDuVFFRzuEiqaq/1mUQk4VFwMOcHPFcVx8eDglXSBHkxUaYK+0b 2GwKuw6K5UKjxz/uDhzzKfmJaGMTkjrbz4hh15ed4mAjbD+XsmC4i9X6gCm4skcfNv PMR3znjbM6YImYBBq72Vhoci1jstBZoH5evxXgEzQg1VPicRCXiZqu/nNDMtxOSICA cciTP8ydlecjUpwDd+ZjRcXR9ynqPnjtutX5lu/PredUljZZtLBDfUWmBeQAly+KM7 al1tSjsD5bulA== 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 6/6] selftests/bpf: Drop make_socket in sk_lookup Date: Fri, 21 Jun 2024 18:35:29 +0800 Message-ID: <489e1543f8c4ac8feb9e90d1384af66ecb59ba82.1718965270.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Geliang Tang Use public network helpers make_sockaddr() and connect_to_addr() instead of using the local defined function make_socket() and connect(). Then make_socket() can be dropped. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 50 +++---------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 0f2e8a4c1524..e4c983a00511 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -47,8 +47,6 @@ #define INT_IP6 "fd00::2" #define INT_PORT 8008 -#define IO_TIMEOUT_SEC 3 - enum server { SERVER_A = 0, SERVER_B = 1, @@ -108,40 +106,6 @@ static int attach_reuseport(int sock_fd, struct bpf_program *reuseport_prog) return 0; } -static int make_socket(int sotype, const char *ip, int port, - struct sockaddr_storage *addr, socklen_t *len) -{ - 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, len); - 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; @@ -224,24 +188,22 @@ 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 sockaddr_storage addr = {0}; socklen_t len; int err, fd; - fd = make_socket(sotype, ip, port, &addr, &len); - if (fd < 0) + err = make_sockaddr(family, ip, port, &addr, &len); + if (err) return -1; - err = connect(fd, (void *)&addr, len); - if (CHECK(err, "make_client", "connect")) { + fd = connect_to_addr(sotype, &addr, len, NULL); + if (CHECK(fd < 0, "connect_to_addr", "connect")) { log_err("failed to connect client socket"); - goto fail; + return -1; } return fd; -fail: - close(fd); - return -1; } static __u64 socket_cookie(int fd)