diff mbox series

[bpf-next,1/5] selftests/bpf: Drop type from network_helper_opts

Message ID b80ca04f4f1e65e4b796331c48283ea282fe7ee0.1718070939.git.tanggeliang@kylinos.cn (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series use network helpers, part 7 | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: dxu@dxuuu.xyz
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 74 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 2 this patch: 2
netdev/source_inline success Was 0 now: 0

Commit Message

Geliang Tang June 11, 2024, 1:59 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

The opts.{type, noconnect, must_fail} is at least a bit non intuitive or
unnecessary. The only use case now is in test_bpf_ip_check_defrag_ok which
ends up bypassing most (or at least some) of the connect_to_fd_opts()
logic. It's much better that test should have its own connect_to_fd_opts()
instead.

This patch adds a new helper named __connect_to_fd_opts() to do this. It
accepts a new "type" parameter, then opts->type can be replaced by "type"
parameter in this helper. In test_bpf_ip_check_defrag_ok, different types
are passed to it. And the strcut member "type" of network_helper_opts can
be dropped now.

Then connect_to_fd_opts can implement as a wrapper of this new helper.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c      | 14 +++++++++-----
 tools/testing/selftests/bpf/network_helpers.h      |  3 ++-
 .../selftests/bpf/prog_tests/ip_check_defrag.c     |  5 ++---
 3 files changed, 13 insertions(+), 9 deletions(-)

Comments

Eduard Zingerman June 11, 2024, 9:20 p.m. UTC | #1
On Tue, 2024-06-11 at 09:59 +0800, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> The opts.{type, noconnect, must_fail} is at least a bit non intuitive or
> unnecessary. The only use case now is in test_bpf_ip_check_defrag_ok which
> ends up bypassing most (or at least some) of the connect_to_fd_opts()
> logic. It's much better that test should have its own connect_to_fd_opts()
> instead.
> 
> This patch adds a new helper named __connect_to_fd_opts() to do this. It
> accepts a new "type" parameter, then opts->type can be replaced by "type"
> parameter in this helper. In test_bpf_ip_check_defrag_ok, different types
> are passed to it. And the strcut member "type" of network_helper_opts can
> be dropped now.
> 
> Then connect_to_fd_opts can implement as a wrapper of this new helper.
> 
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---

Patches #1,2,3 trade options specified as struct fields for options
specified as function parameters. Tbh, this seems to be an opinionated
stylistic change, what is the need for it?

If anything, I think that this is less readable:

> +	client_rx_fd = __connect_to_fd_opts(srv_fd, 0, &rx_opts);

compared to this:

>  	struct network_helper_opts tx_ops = {
>  		.timeout_ms = 1000,
> -		.type = SOCK_RAW,
>  		.proto = IPPROTO_RAW,
>  		.noconnect = true,
>  	};
...
> -	client_rx_fd = connect_to_fd_opts(srv_fd, &rx_opts);

(given that by patch #3 three parameters are added to
 __connect_to_fd_opts() *and* it also accepts options).

[...]
Geliang Tang June 12, 2024, 3:39 a.m. UTC | #2
On Tue, 2024-06-11 at 14:20 -0700, Eduard Zingerman wrote:
> On Tue, 2024-06-11 at 09:59 +0800, Geliang Tang wrote:
> > From: Geliang Tang <tanggeliang@kylinos.cn>
> > 
> > The opts.{type, noconnect, must_fail} is at least a bit non
> > intuitive or
> > unnecessary. The only use case now is in
> > test_bpf_ip_check_defrag_ok which
> > ends up bypassing most (or at least some) of the
> > connect_to_fd_opts()
> > logic. It's much better that test should have its own
> > connect_to_fd_opts()
> > instead.
> > 
> > This patch adds a new helper named __connect_to_fd_opts() to do
> > this. It
> > accepts a new "type" parameter, then opts->type can be replaced by
> > "type"
> > parameter in this helper. In test_bpf_ip_check_defrag_ok, different
> > types
> > are passed to it. And the strcut member "type" of
> > network_helper_opts can
> > be dropped now.
> > 
> > Then connect_to_fd_opts can implement as a wrapper of this new
> > helper.
> > 
> > Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> > ---
> 
> Patches #1,2,3 trade options specified as struct fields for options
> specified as function parameters. Tbh, this seems to be an
> opinionated
> stylistic change, what is the need for it?

Thanks for your review.

Patches 1-3 address Martin's comment for "Drop type parameter of
start_server_addr" [1].

Since opts.{type, noconnect} are only used in ip_check_defrag.c and
opts.must_fail is only used in cgroup_v1v2.c, they are not generic
enough to be added into network_helper_opts. So this set removes them
from network_helper_opts and use them as function parameters.

Thanks,
-Geliang

[1]
https://patchwork.kernel.org/project/netdevbpf/patch/65dd42dd91d678740e9c05e32852f5e01ba2b7bc.1716369375.git.tanggeliang@kylinos.cn/

> 
> If anything, I think that this is less readable:
> 
> > +	client_rx_fd = __connect_to_fd_opts(srv_fd, 0, &rx_opts);
> 
> compared to this:
> 
> >  	struct network_helper_opts tx_ops = {
> >  		.timeout_ms = 1000,
> > -		.type = SOCK_RAW,
> >  		.proto = IPPROTO_RAW,
> >  		.noconnect = true,
> >  	};
> ...
> > -	client_rx_fd = connect_to_fd_opts(srv_fd, &rx_opts);
> 
> (given that by patch #3 three parameters are added to
>  __connect_to_fd_opts() *and* it also accepts options).
> 
> [...]
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index e20caef06aae..902060a70e3b 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -303,21 +303,20 @@  int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 	return -1;
 }
 
-int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
+int __connect_to_fd_opts(int server_fd, int type,
+			 const struct network_helper_opts *opts)
 {
 	struct sockaddr_storage addr;
 	struct sockaddr_in *addr_in;
 	socklen_t addrlen, optlen;
-	int fd, type, protocol;
+	int fd, protocol;
 
 	if (!opts)
 		opts = &default_opts;
 
 	optlen = sizeof(type);
 
-	if (opts->type) {
-		type = opts->type;
-	} else {
+	if (!type) {
 		if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
 			log_err("getsockopt(SOL_TYPE)");
 			return -1;
@@ -364,6 +363,11 @@  int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
 	return -1;
 }
 
+int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
+{
+	return __connect_to_fd_opts(server_fd, 0, opts);
+}
+
 int connect_to_fd(int server_fd, int timeout_ms)
 {
 	struct network_helper_opts opts = {
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 11eea8e2e4f1..9a7cbea87967 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -25,7 +25,6 @@  struct network_helper_opts {
 	int timeout_ms;
 	bool must_fail;
 	bool noconnect;
-	int type;
 	int proto;
 	int (*post_socket_cb)(int fd, void *opts);
 	void *cb_opts;
@@ -61,6 +60,8 @@  void free_fds(int *fds, unsigned int nr_close_fds);
 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,
diff --git a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c
index 284764e7179f..30349c866c77 100644
--- a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c
+++ b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c
@@ -164,7 +164,6 @@  void test_bpf_ip_check_defrag_ok(bool ipv6)
 	};
 	struct network_helper_opts tx_ops = {
 		.timeout_ms = 1000,
-		.type = SOCK_RAW,
 		.proto = IPPROTO_RAW,
 		.noconnect = true,
 	};
@@ -201,7 +200,7 @@  void test_bpf_ip_check_defrag_ok(bool ipv6)
 	nstoken = open_netns(NS0);
 	if (!ASSERT_OK_PTR(nstoken, "setns ns0"))
 		goto out;
-	client_tx_fd = connect_to_fd_opts(srv_fd, &tx_ops);
+	client_tx_fd = __connect_to_fd_opts(srv_fd, SOCK_RAW, &tx_ops);
 	close_netns(nstoken);
 	if (!ASSERT_GE(client_tx_fd, 0, "connect_to_fd_opts"))
 		goto out;
@@ -210,7 +209,7 @@  void test_bpf_ip_check_defrag_ok(bool ipv6)
 	nstoken = open_netns(NS0);
 	if (!ASSERT_OK_PTR(nstoken, "setns ns0"))
 		goto out;
-	client_rx_fd = connect_to_fd_opts(srv_fd, &rx_opts);
+	client_rx_fd = __connect_to_fd_opts(srv_fd, 0, &rx_opts);
 	close_netns(nstoken);
 	if (!ASSERT_GE(client_rx_fd, 0, "connect_to_fd_opts"))
 		goto out;