From patchwork Thu Jul 18 06:22: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: 813304 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 C280E26ACF; Thu, 18 Jul 2024 06:23:16 +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=1721283796; cv=none; b=tkwCNCCZxW3Om2ovqdV3ogrFpDkxfkwpl3AM+VpPSjuYLNWlhXHoUoEjvnGbWrK7tJ9g1IUkYEYn/KgkqI/2rl9laiiP4B3Ub/gHzf8UjCju4NzRe1HyTAZA9IGukQo+WSSvmjF6++mDLHrUbqElu7qz2QlfwcaXHJ3OvPq7xGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721283796; c=relaxed/simple; bh=jQxuy3jS5q4uYp2ek18QbkEdKwp0RfC9loCP6oGS0uQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XPflSNOYLbgC9tol67D2brEuRGqbksoUpxqxxokcNlnb3WjJTjrNf4LM+2oA1D+Re0oemI/7HmGnkcpEOOP2hzlnIpCUJky0yKLZMoLVOV2BbyiBPshdt9VwNWU4MAvPm/agac6wIJ01XB3pipkfhrr44VbC1d9Odv2E6TxV34s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VJEWNc0d; 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="VJEWNc0d" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC4A1C4AF0C; Thu, 18 Jul 2024 06:23:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721283796; bh=jQxuy3jS5q4uYp2ek18QbkEdKwp0RfC9loCP6oGS0uQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VJEWNc0dSQjnvHDoONZeYwK7gfFWwYArizOj44jc10sqPlU92xqpOt/fdJoH3WiRL ki6VMiTQ50ulCPfpbB8wjV909CtBBJJhgPRXNNM1AynydcOnSpQQuLReco0mCriYnE h4z1Xr5EqL94M9tj1ReRPmV+ih2priF/C0pHKAkpaLK+uNRee/P6EDIjmXllp1JOa4 bd8KvYBWA/YHd4LQwd6luS2Q4LJwmXRqP9oNXjuCFjYd3Gm1LeKUVpaLEV4Kc+kPML a9YIdl2zX+FlFI2+5VEFxSVqMSp2763BV0qMIvQywUCBMQ6S5d/9GmnpkvMvJdT8rI /PGIZgrzoX0cg== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Martin KaFai Lau Subject: [PATCH bpf-next v3 1/5] selftests/bpf: Drop type of connect_to_fd_opts Date: Thu, 18 Jul 2024 14:22:29 +0800 Message-ID: <50d8ce7ab7ab0c0f4d211fc7cc4ebe3d3f63424c.1721282219.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 The "type" parameter of connect_to_fd_opts() is redundant of "server_fd". Since the "type" can be obtained inside by invoking getsockopt(SO_TYPE), without passing it in as a parameter. This patch drops the "type" parameter of connect_to_fd_opts() and updates its callers. Suggested-by: Martin KaFai Lau Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 21 ++++++++++--------- tools/testing/selftests/bpf/network_helpers.h | 2 +- .../selftests/bpf/prog_tests/bpf_tcp_ca.c | 2 +- .../selftests/bpf/prog_tests/cgroup_v1v2.c | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index e0cba4178e41..15e0e0bb7553 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -328,14 +328,21 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; } -int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts) +int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) { struct sockaddr_storage addr; - socklen_t addrlen; + socklen_t addrlen, optlen; + int type; if (!opts) opts = &default_opts; + optlen = sizeof(type); + if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { + log_err("getsockopt(SOL_TYPE)"); + return -1; + } + addrlen = sizeof(addr); if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) { log_err("Failed to get server addr"); @@ -350,14 +357,8 @@ int connect_to_fd(int server_fd, int timeout_ms) struct network_helper_opts opts = { .timeout_ms = timeout_ms, }; - int type, protocol; socklen_t optlen; - - optlen = sizeof(type); - if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { - log_err("getsockopt(SOL_TYPE)"); - return -1; - } + int protocol; optlen = sizeof(protocol); if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) { @@ -366,7 +367,7 @@ int connect_to_fd(int server_fd, int timeout_ms) } opts.proto = protocol; - return connect_to_fd_opts(server_fd, type, &opts); + return connect_to_fd_opts(server_fd, &opts); } int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index aac5b94d6379..5b548c0c60de 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -71,7 +71,7 @@ int client_socket(int family, int type, int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, const struct network_helper_opts *opts); int connect_to_fd(int server_fd, int timeout_ms); -int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); +int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int timeout_ms); diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c index 63422f4f3896..1d494b4453f4 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c @@ -49,7 +49,7 @@ static bool start_test(char *addr_str, goto err; /* connect to server */ - *cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts); + *cli_fd = connect_to_fd_opts(*srv_fd, cli_opts); if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts")) goto err; diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9709c8db7275..addf720428f7 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -32,7 +32,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) goto out; } - fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); + fd = connect_to_fd_opts(server_fd, &opts); if (fd < 0) err = -1; else @@ -52,7 +52,7 @@ void test_cgroup_v1v2(void) server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0); if (!ASSERT_GE(server_fd, 0, "server_fd")) return; - client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); + client_fd = connect_to_fd_opts(server_fd, &opts); if (!ASSERT_GE(client_fd, 0, "client_fd")) { close(server_fd); return; From patchwork Thu Jul 18 06:22: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: 813569 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 6D3EC5FB8A; Thu, 18 Jul 2024 06:23:23 +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=1721283803; cv=none; b=uHGw5/JFIsoc8x3LS+kAGuEF72CHSWd051Wb6ON1qsYTUV2uGrabgbM8UTAJqugYrpIvytupZQsAsMPOdyFCoyJnA8scrUUzWPgE9CtjT/qnZa8A3w60pvqHYc8FSyi3BACVqjtijkGMSl9++qeHTp4Ic331GSzjL6r0ByTSzOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721283803; c=relaxed/simple; bh=kMZGhbYFsv5b+GY+m+qbhK9Wk0HNDAyX5YWMOzLEd20=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oyymVSvLuK7RpYrbEZwOOAXBfSJnU+a8Xll8Y6BSPxNVlWGiTM/LPPrvDLORcUKzJALFk8CzeJl0r0A7SMQUPp2qIMJxoU2ukNQFNzMZ8G0uGh8I7CSnAQ1b0rrxgxeLXypoug0GcxYsHxc7Ueit0o2GD2olx02AXTYZhwQPtXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d+Z2cPUL; 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="d+Z2cPUL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE99BC4AF0C; Thu, 18 Jul 2024 06:23:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721283803; bh=kMZGhbYFsv5b+GY+m+qbhK9Wk0HNDAyX5YWMOzLEd20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d+Z2cPULvw75OxlJlqg0Oq0VPXvQ1bT+9ks0Ml5twugTpMFmAK5uc0cdcZSaKR6G5 QpD35HUmmmHEfXTsKh7IT+W9ORs50fpwOTRt1/UZpBT6o4lWBjJUpDuiD082knzRmQ bNJtsKqH0tgFkAJqsSCR2Whh984gY70Zk/OOPzFeuYJB4frspFLcOnD7RI3MWv3M9j jGGpziu2o15kdkXbiwnMKqdVb2L1AD06eCpk9SIj5NsCxPGbWM/QAsGsMu4YkMib3y yy0OfwQR506w3iN0SaXobzHTmKvr0rZKulLAe0LPRQnqH3pjx0uQE2qW1qbth7lN+/ wVvtfMKj2i+0Q== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Martin KaFai Lau Subject: [PATCH bpf-next v3 2/5] selftests/bpf: Drop must_fail from network_helper_opts Date: Thu, 18 Jul 2024 14:22:30 +0800 Message-ID: <3faf336019a9a48e2e8951f4cdebf19e3ac6e441.1721282219.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 The struct member "must_fail" of network_helper_opts() is only used in cgroup_v1v2 tests, it makes sense to drop it from network_helper_opts. Return value (fd) of connect_to_fd_opts() and the expect errno (EPERM) can be checked in cgroup_v1v2.c directly, no need to check them in connect_fd_to_addr() in network_helpers.c. This also makes connect_fd_to_addr() function useless. It can be replaced by connect(). Suggested-by: Martin KaFai Lau Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 31 ++----------------- tools/testing/selftests/bpf/network_helpers.h | 1 - .../selftests/bpf/prog_tests/cgroup_v1v2.c | 14 +++++---- 3 files changed, 10 insertions(+), 36 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 15e0e0bb7553..48c27c810db7 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -277,33 +277,6 @@ int client_socket(int family, int type, return -1; } -static int connect_fd_to_addr(int fd, - const struct sockaddr_storage *addr, - socklen_t addrlen, const bool must_fail) -{ - int ret; - - errno = 0; - ret = connect(fd, (const struct sockaddr *)addr, addrlen); - if (must_fail) { - if (!ret) { - log_err("Unexpected success to connect to server"); - return -1; - } - if (errno != EPERM) { - log_err("Unexpected error from connect to server"); - return -1; - } - } else { - if (ret) { - log_err("Failed to connect to server"); - return -1; - } - } - - return 0; -} - int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen, const struct network_helper_opts *opts) { @@ -318,7 +291,7 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; } - if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) + if (connect(fd, (const struct sockaddr *)addr, addrlen)) goto error_close; return fd; @@ -383,7 +356,7 @@ int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) return -1; } - if (connect_fd_to_addr(client_fd, &addr, len, false)) + if (connect(client_fd, (const struct sockaddr *)&addr, len)) return -1; return 0; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 5b548c0c60de..f39eeb5a4594 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -23,7 +23,6 @@ typedef __u16 __sum16; struct network_helper_opts { int timeout_ms; - bool must_fail; int proto; /* +ve: Passed to listen() as-is. * 0: Default when the test does not set diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index addf720428f7..e91099c637b0 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -9,9 +9,6 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) { - struct network_helper_opts opts = { - .must_fail = true, - }; struct connect4_dropper *skel; int fd, err = 0; @@ -32,11 +29,16 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) goto out; } - fd = connect_to_fd_opts(server_fd, &opts); - if (fd < 0) + errno = 0; + fd = connect_to_fd_opts(server_fd, NULL); + if (fd >= 0) { + log_err("Unexpected success to connect to server"); err = -1; - else close(fd); + } else if (errno != EPERM) { + log_err("Unexpected error from connect to server"); + err = -1; + } out: connect4_dropper__destroy(skel); return err; From patchwork Thu Jul 18 06:22: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: 813303 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 1428B6BB4B; Thu, 18 Jul 2024 06:23:30 +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=1721283810; cv=none; b=D6mQ4HRnrdCx/T3159w8vks2qAzGwYzsTv2h7+y29e79V7D5j0KcHj/66ja3svjrXujORx9+cWhyGer6tbWitCred+h2Rco98WQuBjNvh3eLZk6U1JKNu2Q6jp0T3N+/ZPs+/e+XiX1ICE4utCRHe7jhxKQxOIzKC+WwJvvMuEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721283810; c=relaxed/simple; bh=5fMCJ0BD5aW4CguVh1SsSPVVZwwEweUrXtzIxCkROzY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cFsZDso0anLTj5UmJ8gNN6RRt7qNEkDULSOZyOGtwSBqdh2v8zowqxqWRGhrGzN8QTwnEMuVGfmc6knSmqZF4gchK8bbiDNx87jIkKvejh7/yHoXRy4VMhN0SuhP2hkr8e5fQmNKMhIQ/A7GwQiYn/XXIqoguHdy192iAulo6oA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ihaiFb8F; 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="ihaiFb8F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3EC3C4AF0D; Thu, 18 Jul 2024 06:23:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721283809; bh=5fMCJ0BD5aW4CguVh1SsSPVVZwwEweUrXtzIxCkROzY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ihaiFb8FP2K7vg+BeTqZSySGU/994m/7rxD+mYaIhGVSqt6YcRVMgsmLwWTM5gjqP JK6QJYhDf2IuI4JFJdkXny1djAOL8REAY0KGdcakKWnkmp4IWC1vQBYXrMNBPgFJy7 xkTs2KSnYYCWocWqCnpRtJ4apwJvOhsUZYeTR6zxiDdvsR/EyfSQ1ablPQANtCsn6g Pc0+430/CIwnF+icG/s5dT7/brAqfMB4bPU18xaApeMz4TNgxYsdNFCdIg4XoYbbNK eGHCd3RmkNPsCMjXplAyw2uN+UTIpL2gQJKW5CfU5BLwGnouevWP5cpwTFlfcNJYnv Iv3YJLe4iBOSQ== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Martin KaFai Lau Subject: [PATCH bpf-next v3 3/5] selftests/bpf: Add connect_to_addr_str helper Date: Thu, 18 Jul 2024 14:22:31 +0800 Message-ID: <647e82170831558dbde132a7a3d86df660dba2c4.1721282219.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 Similar to connect_to_addr() helper for connecting to a server with the given sockaddr_storage type address, this patch adds a new helper named connect_to_addr_str() for connecting to a server with the given string type address "addr_str", together with its "family" and "port" as other parameters of connect_to_addr_str(). In connect_to_addr_str(), the parameters "family", "addr_str" and "port" are used to create a sockaddr_storage type address "addr" by invoking make_sockaddr(). Then pass this "addr" together with "addrlen", "type" and "opts" to connect_to_addr(). Suggested-by: Martin KaFai Lau Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 15 +++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 48c27c810db7..a71af637bcbc 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -301,6 +301,21 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add return -1; } +int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port, + const struct network_helper_opts *opts) +{ + struct sockaddr_storage addr; + socklen_t addrlen; + + if (!opts) + opts = &default_opts; + + if (make_sockaddr(family, addr_str, port, &addr, &addrlen)) + return -1; + + return connect_to_addr(type, &addr, addrlen, opts); +} + int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) { struct sockaddr_storage addr; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index f39eeb5a4594..cce56955371f 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -69,6 +69,8 @@ int client_socket(int family, int type, const struct network_helper_opts *opts); int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, const struct network_helper_opts *opts); +int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port, + const struct network_helper_opts *opts); int connect_to_fd(int server_fd, int timeout_ms); int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); From patchwork Thu Jul 18 06:22:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 813568 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 96C0573477; Thu, 18 Jul 2024 06:23:36 +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=1721283816; cv=none; b=hKjlny0fRGTs6bauM02zO2jqtxnj9YQ9b/SPtko6e4bGoYdJ+5auKw0hyV+KQVLw7qmxlYZMWMeGNmg2jKb2WaBlmMmNG4/GXMYNDWeIw56j7RxyVe7dPgYb16/VO+u4X6gjfkO3OZTCS8k8ZIMnm1QyoJP09XcqJuSTB/lfFwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721283816; c=relaxed/simple; bh=1XDJn0TtQHL6OsmkJ5DEznCn6PXG39XsRnPu7oafgvw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n6ArE+uGMBozWFS/TKWi8R9k4xgU6T/7nrVNLw4hs7BL8qKoCMz8e2b43dNPonIn5DfMszTesbfabY3+1XsPZ527WiEWfInvbhEG/03F/hMv0hKjCeY1FijRr7D73raphzk/nufP9v/AK4wv0M9NWRndQGoJ+D5ZSI56csKBeRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uvNzvg+A; 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="uvNzvg+A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89695C4AF18; Thu, 18 Jul 2024 06:23:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721283816; bh=1XDJn0TtQHL6OsmkJ5DEznCn6PXG39XsRnPu7oafgvw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uvNzvg+A++u/1SUvDELhIgj3SELlcAGFGT255hPzueWB2UuYWZeTyuT7Z8hxmsW1Z gNRJGixWR2tKl7sHG2NY4aWn/2NtSvLFoCh1JuSyUT1AX2EuqZfOk1vX4wKHNyrIIW m3mJZcIErhdKVPE35KpP0Qvy7IkzdQAwTS7ZRbgnULd1xND9MDhpFoeUT9wdMlUQ5M T+PEVWwc2EFtebqCX9SxxafLTWMPWcYrBwThG9pL++2GYCVQxPIAYa9VrGVh2EtYF+ VsvougAwJW6QZhC+wP08ZZ2NnCkF0KQzVOR4sg6f6FE1Jus90LiyOMuM/uLwUfxtdf QJgZnlNtnESFg== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 4/5] selftests/bpf: Use connect_to_addr_str in sk_lookup Date: Thu, 18 Jul 2024 14:22:32 +0800 Message-ID: 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 the new helper connect_to_addr_str() in make_client() instead of using local defined function make_socket() + connect(). This local function can be dropped latter. A new parameter "expect_errno" is added for make_client() too to allow different "expect_errno" is passed to make_client(). It is used to check with "errno" after invoking connect_to_addr_str(). Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 26 +++++++------------ 1 file changed, 10 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 ae87c00867ba..beea7866b37f 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -229,25 +229,19 @@ static int make_server(int sotype, const char *ip, int port, return -1; } -static int make_client(int sotype, const char *ip, int port) +static int make_client(int sotype, const char *ip, int port, int expect_errno) { - struct sockaddr_storage addr = {0}; - int err, fd; - - fd = make_socket(sotype, ip, port, &addr); - if (fd < 0) - return -1; + int fd; - err = connect(fd, (void *)&addr, inetaddr_len(&addr)); - if (CHECK(err, "make_client", "connect")) { + fd = connect_to_addr_str(is_ipv6(ip) ? AF_INET6 : AF_INET, + sotype, ip, port, NULL); + if (CHECK(fd < 0 && (!expect_errno || errno != expect_errno), + "make_client", "connect_to_addr_str")) { log_err("failed to connect client socket"); - goto fail; + return -1; } return fd; -fail: - close(fd); - return -1; } static __u64 socket_cookie(int fd) @@ -646,7 +640,7 @@ static void run_lookup_prog(const struct test *t) goto close; } - client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port); + client_fd = make_client(t->sotype, t->connect_to.ip, t->connect_to.port, 0); if (client_fd < 0) goto close; @@ -1152,7 +1146,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 = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (connected_fd < 0) goto out_close_server; @@ -1166,7 +1160,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 = make_client(sotype, EXT_IP4, EXT_PORT, 0); if (client_fd < 0) goto out_unlink_prog; if (sotype == SOCK_DGRAM) { From patchwork Thu Jul 18 06:22:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 813302 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 4F10A42078; Thu, 18 Jul 2024 06:23:42 +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=1721283823; cv=none; b=a1HcQb6d9dQjm1ZTGgheZnNTN/ldbEKv062Zp48tUfo/jJ62c7L/mJ48oXMubKuAslYTGVwhU4bHCjMQzA7iV16Qb5atqzKqECKsNpyvRTkESLzdahra5AWoLCEGI9nOirMvfAsH95FLwto4LvpZEv7AeicXzxesXVyr0rtOWYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721283823; c=relaxed/simple; bh=HwKICgEpbwXi5sOe44e/mtrg0kyb+kSvDeOFE71Puno=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XIjtoh1zXx9mGJdQaljyL1gTntzi9tqqRLhetp9kp0CmZ2+1vnYf4C1ikwMNBpw01XCiPrCnviU7zWBoCwneHDsKamPPBtABgzhr+KGyP+nrjAtYEYWO21zhGpwdasiayeD94ca7Dixc8Au500TpZn3z0gcqf4AiHqr1bh94Xl8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GjUu/BVV; 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="GjUu/BVV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1EF0CC116B1; Thu, 18 Jul 2024 06:23:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721283822; bh=HwKICgEpbwXi5sOe44e/mtrg0kyb+kSvDeOFE71Puno=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GjUu/BVVaSI+ANJhZPBDrfwFV5CrBl6D6xdMiH6tiaDTrKm3/5z+yBLOYAyAiv5TD h6MuzSOx59Z5f+ckMUfkX8TvjAgmmKIfGPYarA2y4F0/dAdlhNMuV/R5wpBP8dLJrB YQrQl/nLAbd8IwguHx7ibrBGB0KhdSGRabhAg+FlJhZhcfYdD6kkvlW87S0TxlGshF ulxdoxMu/g6VWlfP7KDcQTddjMjMPv/k6T2HGcoRavQsdWHPnrxwYk7ruZJOeCaN/G m0L0sJ5yilNoi56y5EXvImudvz1Y8ifvCvS0xmT6iCPD7qTb5DLt3m+I3VnxkRb6MC FazW1CgaVx6GA== 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 , Hao Luo , Jiri Olsa , Shuah Khan Cc: Geliang Tang , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v3 5/5] selftests/bpf: Drop make_socket in sk_lookup Date: Thu, 18 Jul 2024 14:22:33 +0800 Message-ID: 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 local helper make_client() in drop_on_lookup(), drop_on_reuseport() and run_multi_prog_lookup() instead of using make_socket() + connect(). Then make_socket() and inetaddr_len() can be dropped. Signed-off-by: Geliang Tang --- .../selftests/bpf/prog_tests/sk_lookup.c | 58 ++----------------- 1 file changed, 6 insertions(+), 52 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index beea7866b37f..3d1c315841b7 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; @@ -856,8 +816,7 @@ 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; + int client_fd, server_fd, err = 0; struct bpf_link *lookup_link; ssize_t n; @@ -870,12 +829,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 = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client_fd < 0) goto close_srv; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); if (t->sotype == SOCK_DGRAM) { err = send_byte(client_fd); if (err) @@ -970,7 +928,6 @@ 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; ssize_t n; @@ -994,12 +951,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 = make_client(t->sotype, t->connect_to.ip, + t->connect_to.port, ECONNREFUSED); if (client < 0) goto close_srv2; - err = connect(client, (void *)&dst, inetaddr_len(&dst)); if (t->sotype == SOCK_DGRAM) { err = send_byte(client); if (err) @@ -1209,7 +1165,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; @@ -1242,11 +1197,10 @@ 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, t->expect_errno); if (client_fd < 0) goto out_close_server; - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); if (CHECK(err && !t->expect_errno, "connect", "unexpected error %d\n", errno)) goto out_close_client;