Skip to contentSkip to conversation

use network helpers, part 5

PATCH In review Geliang Tang 3 participants 10 messages May 25, 2024 – May 29, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover use network helpers, part 5

From: Geliang Tang <[email protected]>

This patchset uses post_socket_cb and post_connect_cb callbacks of struct network_helper_opts to refactor do_test() in bpf_tcp_ca.c to move dctcp test dedicated code out of do_test() into test_dctcp().

v5:
- address Martin's comments in v4 (thanks)
- add patch 4, use start_server_str in test_dctcp_fallback too
- ASSERT_* is already used in settcpca, use this helper in cc_cb (patch 3) and stg_post_socket_cb (patch 6)
- add ASSERT_* in stg_post_socket_cb in patch 6

v4:
- address Martin's comments in v3 (thanks).
- drop 2 patches, keep "type" as the individual arg to start_server_addr, connect_to_addr and start_server_str.

v3:
- Add 4 new patches, 1-3 are cleanups. 4 adds a new helper.
- address Martin's comments in v2.

v2:
- rebased on commit "selftests/bpf: Add test for the use of new args in cong_control"

Geliang Tang (7):
selftests/bpf: Drop struct post_socket_opts selftests/bpf: Add start_server_str helper selftests/bpf: Use post_socket_cb in connect_to_fd_opts selftests/bpf: Use post_socket_cb in start_server_str selftests/bpf: Use start_server_str in do_test in bpf_tcp_ca selftests/bpf: Use connect_to_fd_opts in do_test in bpf_tcp_ca selftests/bpf: Add post_connect_cb callback

 tools/testing/selftests/bpf/network_helpers.c |  39 +++--
 tools/testing/selftests/bpf/network_helpers.h |   9 +-
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 153 +++++++++++++-----
 .../bpf/prog_tests/sockopt_inherit.c          |   2 +-
 .../bpf/test_tcp_check_syncookie_user.c       |   4 +-
 5 files changed, 145 insertions(+), 62 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/7 selftests/bpf: Drop struct post_socket_opts

From: Geliang Tang <[email protected]>

It's not possible to have one generic/common "struct post_socket_opts" for all tests. It's better to have the individual test define its own callback opts struct.

So this patch drops struct post_socket_opts, and changes the second parameter of post_socket_cb as "void *" type.

---
 tools/testing/selftests/bpf/network_helpers.c               | 2 +-
 tools/testing/selftests/bpf/network_helpers.h               | 4 +---
 tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c    | 2 +-
 tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c | 4 ++--
 4 files changed, 5 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +1 -1
index 35250e6cde7f..4d776b78929c 100644
@@ -133,7 +133,7 @@ int start_server(int family, int type, const char *addr_str, __u16 port,
 	return __start_server(type, (struct sockaddr *)&addr, addrlen, &opts);
 }
 
-static int reuseport_cb(int fd, const struct post_socket_opts *opts)
+static int reuseport_cb(int fd, void *opts)
 {
 	int on = 1;
 
tools/testing/selftests/bpf/network_helpers.h +1 -3
index 883c7ea9d8d5..40011e0f584b 100644
@@ -21,8 +21,6 @@ typedef __u16 __sum16;
 #define VIP_NUM 5
 #define MAGIC_BYTES 123
 
-struct post_socket_opts {};
-
 struct network_helper_opts {
 	const char *cc;
 	int timeout_ms;
@@ -30,7 +28,7 @@ struct network_helper_opts {
 	bool noconnect;
 	int type;
 	int proto;
-	int (*post_socket_cb)(int fd, const struct post_socket_opts *opts);
+	int (*post_socket_cb)(int fd, void *opts);
 };
 
 /* ipv4 test vector */
tools/testing/selftests/bpf/prog_tests/sockopt_inherit.c +1 -1
index 1d3a20f01b60..7cd8be2780ca 100644
@@ -70,7 +70,7 @@ static void *server_thread(void *arg)
 	return (void *)(long)err;
 }
 
-static int custom_cb(int fd, const struct post_socket_opts *opts)
+static int custom_cb(int fd, void *opts)
 {
 	char buf;
 	int err;
tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c +2 -2
index 7b5fc98838cd..aebc58c24dc5 100644
@@ -139,14 +139,14 @@ static int run_test(int server_fd, int results_fd, bool xdp)
 	return ret;
 }
 
-static int v6only_true(int fd, const struct post_socket_opts *opts)
+static int v6only_true(int fd, void *opts)
 {
 	int mode = true;
 
 	return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &mode, sizeof(mode));
 }
 
-static int v6only_false(int fd, const struct post_socket_opts *opts)
+static int v6only_false(int fd, void *opts)
 {
 	int mode = false;
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v5 2/7 selftests/bpf: Add start_server_str helper

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

It's a tech debt that start_server() does not take the "opts" argument. It's pretty handy to have start_server() as a helper that takes string address.

So this patch creates a new helper start_server_str(). Then start_server() can be a wrapper of it.

---
 tools/testing/selftests/bpf/network_helpers.c | 22 ++++++++++++++-----
 tools/testing/selftests/bpf/network_helpers.h |  2 ++
 2 files changed, 18 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +16 -6
index 4d776b78929c..0e8266f439e4 100644
@@ -118,19 +118,29 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl
 	return -1;
 }
 
-int start_server(int family, int type, const char *addr_str, __u16 port,
-		 int timeout_ms)
+int start_server_str(int family, int type, const char *addr_str, __u16 port,
+		     const struct network_helper_opts *opts)
 {
-	struct network_helper_opts opts = {
-		.timeout_ms	= timeout_ms,
-	};
 	struct sockaddr_storage addr;
 	socklen_t addrlen;
 
+	if (!opts)
+		opts = &default_opts;
+
 	if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
 		return -1;
 
-	return __start_server(type, (struct sockaddr *)&addr, addrlen, &opts);
+	return __start_server(type, (struct sockaddr *)&addr, addrlen, opts);
+}
+
+int start_server(int family, int type, const char *addr_str, __u16 port,
+		 int timeout_ms)
+{
+	struct network_helper_opts opts = {
+		.timeout_ms	= timeout_ms,
+	};
+
+	return start_server_str(family, type, addr_str, port, &opts);
 }
 
 static int reuseport_cb(int fd, void *opts)
tools/testing/selftests/bpf/network_helpers.h +2 -0
index 40011e0f584b..4e3e6afe7d3a 100644
@@ -48,6 +48,8 @@ struct ipv6_packet {
 extern struct ipv6_packet pkt_v6;
 
 int settimeo(int fd, int timeout_ms);
+int start_server_str(int family, int type, const char *addr_str, __u16 port,
+		     const struct network_helper_opts *opts);
 int start_server(int family, int type, const char *addr, __u16 port,
 		 int timeout_ms);
 int *start_reuseport_server(int family, int type, const char *addr_str,
Signature · 1 line
2.43.0
#3 lore raw

PATCH v5 3/7 selftests/bpf: Use post_socket_cb in connect_to_fd_opts

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Since the post_socket_cb() callback is added in struct network_helper_opts, it's make sense to use it not only in __start_server(), but also in connect_to_fd_opts(). Then it can be used to set TCP_CONGESTION sockopt.

Add a "void *" type member cb_opts into struct network_helper_opts, and add a new struct named cb_opts in prog_tests/bpf_tcp_ca.c, then cc can be moved into struct cb_opts from network_helper_opts. Define a new callback cc_cb() to set TCP_CONGESTION sockopt, and set it to post_socket_cb pointer of opts. Define a new cb_opts cubic, set it to cb_opts of opts. Pass this opts to connect_to_fd_opts() in test_dctcp_fallback().

---
 tools/testing/selftests/bpf/network_helpers.c   |  5 ++---
 tools/testing/selftests/bpf/network_helpers.h   |  2 +-
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c       | 17 ++++++++++++++++-
 3 files changed, 19 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +2 -3
index 0e8266f439e4..8502917b6c7b 100644
@@ -348,9 +348,8 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
 	if (settimeo(fd, opts->timeout_ms))
 		goto error_close;
 
-	if (opts->cc && opts->cc[0] &&
-	    setsockopt(fd, SOL_TCP, TCP_CONGESTION, opts->cc,
-		       strlen(opts->cc) + 1))
+	if (opts->post_socket_cb &&
+	    opts->post_socket_cb(fd, opts->cb_opts))
 		goto error_close;
 
 	if (!opts->noconnect)
tools/testing/selftests/bpf/network_helpers.h +1 -1
index 4e3e6afe7d3a..11eea8e2e4f1 100644
@@ -22,13 +22,13 @@ typedef __u16 __sum16;
 #define MAGIC_BYTES 123
 
 struct network_helper_opts {
-	const char *cc;
 	int timeout_ms;
 	bool must_fail;
 	bool noconnect;
 	int type;
 	int proto;
 	int (*post_socket_cb)(int fd, void *opts);
+	void *cb_opts;
 };
 
 /* ipv4 test vector */
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +16 -1
index 0aca02532794..6f034059888a 100644
@@ -23,6 +23,10 @@
 static const unsigned int total_bytes = 10 * 1024 * 1024;
 static int expected_stg = 0xeB9F;
 
+struct cb_opts {
+	const char *cc;
+};
+
 static int settcpca(int fd, const char *tcp_ca)
 {
 	int err;
@@ -81,6 +85,13 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
 	close(fd);
 }
 
+static int cc_cb(int fd, void *opts)
+{
+	struct cb_opts *cb_opts = (struct cb_opts *)opts;
+
+	return settcpca(fd, cb_opts->cc);
+}
+
 static void test_cubic(void)
 {
 	struct bpf_cubic *cubic_skel;
@@ -172,10 +183,13 @@ static void test_dctcp_fallback(void)
 {
 	int err, lfd = -1, cli_fd = -1, srv_fd = -1;
 	struct network_helper_opts opts = {
-		.cc = "cubic",
+		.post_socket_cb	= cc_cb,
 	};
 	struct bpf_dctcp *dctcp_skel;
 	struct bpf_link *link = NULL;
+	struct cb_opts cubic = {
+		.cc = "cubic",
+	};
 	char srv_cc[16];
 	socklen_t cc_len = sizeof(srv_cc);
 
@@ -195,6 +209,7 @@ static void test_dctcp_fallback(void)
 	    !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
 		goto done;
 
+	opts.cb_opts = &cubic;
 	cli_fd = connect_to_fd_opts(lfd, &opts);
 	if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
 		goto done;
Signature · 1 line
2.43.0
#4 lore raw

PATCH v5 4/7 selftests/bpf: Use post_socket_cb in start_server_str

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch uses start_server_str() helper in test_dctcp_fallback() in bpf_tcp_ca.c, instead of using start_server() and settcpca(). For support opts in start_server_str() helper, opts->cb_opts needs to be passed to post_socket_cb() in __start_server().

---
 tools/testing/selftests/bpf/network_helpers.c       | 3 ++-
 tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c | 9 ++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +2 -1
index 8502917b6c7b..e20caef06aae 100644
@@ -94,7 +94,8 @@ static int __start_server(int type, const struct sockaddr *addr, socklen_t addrl
 	if (settimeo(fd, opts->timeout_ms))
 		goto error_close;
 
-	if (opts->post_socket_cb && opts->post_socket_cb(fd, NULL)) {
+	if (opts->post_socket_cb &&
+	    opts->post_socket_cb(fd, opts->cb_opts)) {
 		log_err("Failed to call post_socket_cb");
 		goto error_close;
 	}
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +6 -3
index 6f034059888a..7a523c7a0c6a 100644
@@ -187,6 +187,9 @@ static void test_dctcp_fallback(void)
 	};
 	struct bpf_dctcp *dctcp_skel;
 	struct bpf_link *link = NULL;
+	struct cb_opts dctcp = {
+		.cc = "bpf_dctcp",
+	};
 	struct cb_opts cubic = {
 		.cc = "cubic",
 	};
@@ -204,9 +207,9 @@ static void test_dctcp_fallback(void)
 	if (!ASSERT_OK_PTR(link, "dctcp link"))
 		goto done;
 
-	lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
-	if (!ASSERT_GE(lfd, 0, "lfd") ||
-	    !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
+	opts.cb_opts = &dctcp;
+	lfd = start_server_str(AF_INET6, SOCK_STREAM, "::1", 0, &opts);
+	if (!ASSERT_GE(lfd, 0, "lfd"))
 		goto done;
 
 	opts.cb_opts = &cubic;
Signature · 1 line
2.43.0
#5 lore raw

PATCH v5 5/7 selftests/bpf: Use start_server_str in do_test in bpf_tcp_ca

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch uses new helper start_server_str() in do_test() in bpf_tcp_ca.c to accept a struct network_helper_opts argument instead of using start_server() and settcpca(). Then change the type of the first paramenter of do_test() into a struct network_helper_opts one.

Define its own cb_opts and opts for each test, set its own cc name into cb_opts.cc, and cc_cb() into post_socket_cb callback, then pass it to do_test().

---
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 66 +++++++++++++++----
 1 file changed, 55 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +55 -11
index 7a523c7a0c6a..ebc7d4616880 100644
@@ -38,12 +38,14 @@ static int settcpca(int fd, const char *tcp_ca)
 	return 0;
 }
 
-static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
+static void do_test(const struct network_helper_opts *opts,
+		    const struct bpf_map *sk_stg_map)
 {
+	struct cb_opts *cb_opts = (struct cb_opts *)opts->cb_opts;
 	int lfd = -1, fd = -1;
 	int err;
 
-	lfd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
+	lfd = start_server_str(AF_INET6, SOCK_STREAM, NULL, 0, opts);
 	if (!ASSERT_NEQ(lfd, -1, "socket"))
 		return;
 
@@ -53,7 +55,7 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
 		return;
 	}
 
-	if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca))
+	if (settcpca(fd, cb_opts->cc))
 		goto done;
 
 	if (sk_stg_map) {
@@ -94,6 +96,13 @@ static int cc_cb(int fd, void *opts)
 
 static void test_cubic(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "bpf_cubic",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct bpf_cubic *cubic_skel;
 	struct bpf_link *link;
 
@@ -107,7 +116,7 @@ static void test_cubic(void)
 		return;
 	}
 
-	do_test("bpf_cubic", NULL);
+	do_test(&opts, NULL);
 
 	ASSERT_EQ(cubic_skel->bss->bpf_cubic_acked_called, 1, "pkts_acked called");
 
@@ -117,6 +126,13 @@ static void test_cubic(void)
 
 static void test_dctcp(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "bpf_dctcp",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct bpf_dctcp *dctcp_skel;
 	struct bpf_link *link;
 
@@ -130,7 +146,7 @@ static void test_dctcp(void)
 		return;
 	}
 
-	do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
+	do_test(&opts, dctcp_skel->maps.sk_stg_map);
 	ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");
 
 	bpf_link__destroy(link);
@@ -315,6 +331,13 @@ static void test_unsupp_cong_op(void)
 
 static void test_update_ca(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "tcp_ca_update",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct tcp_ca_update *skel;
 	struct bpf_link *link;
 	int saved_ca1_cnt;
@@ -327,14 +350,14 @@ static void test_update_ca(void)
 	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
 	ASSERT_OK_PTR(link, "attach_struct_ops");
 
-	do_test("tcp_ca_update", NULL);
+	do_test(&opts, NULL);
 	saved_ca1_cnt = skel->bss->ca1_cnt;
 	ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");
 
 	err = bpf_link__update_map(link, skel->maps.ca_update_2);
 	ASSERT_OK(err, "update_map");
 
-	do_test("tcp_ca_update", NULL);
+	do_test(&opts, NULL);
 	ASSERT_EQ(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");
 	ASSERT_GT(skel->bss->ca2_cnt, 0, "ca2_ca2_cnt");
 
@@ -344,6 +367,13 @@ static void test_update_ca(void)
 
 static void test_update_wrong(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "tcp_ca_update",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct tcp_ca_update *skel;
 	struct bpf_link *link;
 	int saved_ca1_cnt;
@@ -356,14 +386,14 @@ static void test_update_wrong(void)
 	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
 	ASSERT_OK_PTR(link, "attach_struct_ops");
 
-	do_test("tcp_ca_update", NULL);
+	do_test(&opts, NULL);
 	saved_ca1_cnt = skel->bss->ca1_cnt;
 	ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");
 
 	err = bpf_link__update_map(link, skel->maps.ca_wrong);
 	ASSERT_ERR(err, "update_map");
 
-	do_test("tcp_ca_update", NULL);
+	do_test(&opts, NULL);
 	ASSERT_GT(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");
 
 	bpf_link__destroy(link);
@@ -372,6 +402,13 @@ static void test_update_wrong(void)
 
 static void test_mixed_links(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "tcp_ca_update",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct tcp_ca_update *skel;
 	struct bpf_link *link, *link_nl;
 	int err;
@@ -386,7 +423,7 @@ static void test_mixed_links(void)
 	link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
 	ASSERT_OK_PTR(link, "attach_struct_ops");
 
-	do_test("tcp_ca_update", NULL);
+	do_test(&opts, NULL);
 	ASSERT_GT(skel->bss->ca1_cnt, 0, "ca1_ca1_cnt");
 
 	err = bpf_link__update_map(link, skel->maps.ca_no_link);
@@ -473,6 +510,13 @@ static void test_tcp_ca_kfunc(void)
 
 static void test_cc_cubic(void)
 {
+	struct cb_opts cb_opts = {
+		.cc = "bpf_cc_cubic",
+	};
+	struct network_helper_opts opts = {
+		.post_socket_cb	= cc_cb,
+		.cb_opts	= &cb_opts,
+	};
 	struct bpf_cc_cubic *cc_cubic_skel;
 	struct bpf_link *link;
 
@@ -486,7 +530,7 @@ static void test_cc_cubic(void)
 		return;
 	}
 
-	do_test("bpf_cc_cubic", NULL);
+	do_test(&opts, NULL);
 
 	bpf_link__destroy(link);
 	bpf_cc_cubic__destroy(cc_cubic_skel);
Signature · 1 line
2.43.0
#6 lore raw

PATCH v5 6/7 selftests/bpf: Use connect_to_fd_opts in do_test in bpf_tcp_ca

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch uses connect_to_fd_opts() instead of using connect_fd_to_fd() and settcpca() in do_test() in prog_tests/bpf_tcp_ca.c to accept a struct network_helper_opts argument.

Then define a dctcp dedicated post_socket_cb callback stg_post_socket_cb(), invoking both cc_cb() and bpf_map_update_elem() in it, and set it in test_dctcp(). For passing map_fd into stg_post_socket_cb() callback, a new member map_fd is added in struct cb_opts.

---
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 42 ++++++++++---------
 1 file changed, 22 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +22 -20
index ebc7d4616880..9a7c3dc39008 100644
@@ -25,6 +25,7 @@ static int expected_stg = 0xeB9F;
 
 struct cb_opts {
 	const char *cc;
+	int map_fd;
 };
 
 static int settcpca(int fd, const char *tcp_ca)
@@ -41,7 +42,6 @@ static int settcpca(int fd, const char *tcp_ca)
 static void do_test(const struct network_helper_opts *opts,
 		    const struct bpf_map *sk_stg_map)
 {
-	struct cb_opts *cb_opts = (struct cb_opts *)opts->cb_opts;
 	int lfd = -1, fd = -1;
 	int err;
 
@@ -49,25 +49,9 @@ static void do_test(const struct network_helper_opts *opts,
 	if (!ASSERT_NEQ(lfd, -1, "socket"))
 		return;
 
-	fd = socket(AF_INET6, SOCK_STREAM, 0);
-	if (!ASSERT_NEQ(fd, -1, "socket")) {
-		close(lfd);
-		return;
-	}
-
-	if (settcpca(fd, cb_opts->cc))
-		goto done;
-
-	if (sk_stg_map) {
-		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
-					  &expected_stg, BPF_NOEXIST);
-		if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
-			goto done;
-	}
-
 	/* connect to server */
-	err = connect_fd_to_fd(fd, lfd, 0);
-	if (!ASSERT_NEQ(err, -1, "connect"))
+	fd = connect_to_fd_opts(lfd, opts);
+	if (!ASSERT_NEQ(fd, -1, "connect_to_fd_opts"))
 		goto done;
 
 	if (sk_stg_map) {
@@ -124,13 +108,30 @@ static void test_cubic(void)
 	bpf_cubic__destroy(cubic_skel);
 }
 
+static int stg_post_socket_cb(int fd, void *opts)
+{
+	struct cb_opts *cb_opts = (struct cb_opts *)opts;
+	int err;
+
+	err = settcpca(fd, cb_opts->cc);
+	if (err)
+		return err;
+
+	err = bpf_map_update_elem(cb_opts->map_fd, &fd,
+				  &expected_stg, BPF_NOEXIST);
+	if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
+		return err;
+
+	return 0;
+}
+
 static void test_dctcp(void)
 {
 	struct cb_opts cb_opts = {
 		.cc = "bpf_dctcp",
 	};
 	struct network_helper_opts opts = {
-		.post_socket_cb	= cc_cb,
+		.post_socket_cb	= stg_post_socket_cb,
 		.cb_opts	= &cb_opts,
 	};
 	struct bpf_dctcp *dctcp_skel;
@@ -146,6 +147,7 @@ static void test_dctcp(void)
 		return;
 	}
 
+	cb_opts.map_fd = bpf_map__fd(dctcp_skel->maps.sk_stg_map);
 	do_test(&opts, dctcp_skel->maps.sk_stg_map);
 	ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");
 
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH bpf-next v5 6/7] selftests/bpf: Use connect_to_fd_opts in do_test in bpf_tcp_ca

Quoted 69 lines · Geliang Tang

From: Geliang Tang <[email protected]>

This patch uses connect_to_fd_opts() instead of using connect_fd_to_fd() and settcpca() in do_test() in prog_tests/bpf_tcp_ca.c to accept a struct network_helper_opts argument.

Then define a dctcp dedicated post_socket_cb callback stg_post_socket_cb(), invoking both cc_cb() and bpf_map_update_elem() in it, and set it in test_dctcp(). For passing map_fd into stg_post_socket_cb() callback, a new member map_fd is added in struct cb_opts.

---
  .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 42 ++++++++++---------
  1 file changed, 22 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +4 -19
index ebc7d4616880..9a7c3dc39008 100644
@@ -25,6 +25,7 @@ static int expected_stg = 0xeB9F;
 
  struct cb_opts {
  	const char *cc;
+	int map_fd;
  };
 
  static int settcpca(int fd, const char *tcp_ca)
@@ -41,7 +42,6 @@ static int settcpca(int fd, const char *tcp_ca)
  static void do_test(const struct network_helper_opts *opts,
  		    const struct bpf_map *sk_stg_map)
  {
-	struct cb_opts *cb_opts = (struct cb_opts *)opts->cb_opts;
  	int lfd = -1, fd = -1;
  	int err;
 
@@ -49,25 +49,9 @@ static void do_test(const struct network_helper_opts *opts,
  	if (!ASSERT_NEQ(lfd, -1, "socket"))
  		return;
 
-	fd = socket(AF_INET6, SOCK_STREAM, 0);
-	if (!ASSERT_NEQ(fd, -1, "socket")) {
-		close(lfd);
-		return;
-	}
-
-	if (settcpca(fd, cb_opts->cc))
-		goto done;
-
-	if (sk_stg_map) {
-		err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
-					  &expected_stg, BPF_NOEXIST);
-		if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
-			goto done;
-	}
-
  	/* connect to server */
-	err = connect_fd_to_fd(fd, lfd, 0);
-	if (!ASSERT_NEQ(err, -1, "connect"))
+	fd = connect_to_fd_opts(lfd, opts);
+	if (!ASSERT_NEQ(fd, -1, "connect_to_fd_opts"))
  		goto done;
 
  	if (sk_stg_map) {
@@ -124,13 +108,30 @@ static void test_cubic(void)
  	bpf_cubic__destroy(cubic_skel);
  }
 
+static int stg_post_socket_cb(int fd, void *opts)

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c static void test_cubic(void) · line 121

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +10 -0
+{
+	struct cb_opts *cb_opts = (struct cb_opts *)opts;
+	int err;
+
+	err = settcpca(fd, cb_opts->cc);
+	if (err)
+		return err;
+
+	err = bpf_map_update_elem(cb_opts->map_fd, &fd,
+				  &expected_stg, BPF_NOEXIST);

Looking at it again, it is not very correct. At least this map update is unnecessary for the start_server_str() in do_test(). do_test() uses the same opts for start_server_str() and connect_to_fd_opts().

Also, the post_connect_cb in patch 8 is unnecessary for network_helpers.c. The connect_to_fd_opts helper is going to return the fd anyway. It is unnecessary to have the helper to do the post_connect_cb(fd). The caller (do_test here) can directly do that instead. Lets not add unnecessary post_connect_cb to the network_helpers.c now until there is a clear case that the caller cannot do it.

I would just add another "const struct network_helper_opts *cli_opts" to the do_test() to do what patch 7 and patch 8 need to do. Separate it from the server "opts".

Patach 1 to 5 is applied. Thanks.

Quoted 25 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +8 -1
+	if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
+		return err;
+
+	return 0;
+}
+
  static void test_dctcp(void)
  {
  	struct cb_opts cb_opts = {
  		.cc = "bpf_dctcp",
  	};
  	struct network_helper_opts opts = {
-		.post_socket_cb	= cc_cb,
+		.post_socket_cb	= stg_post_socket_cb,
  		.cb_opts	= &cb_opts,
  	};
  	struct bpf_dctcp *dctcp_skel;
@@ -146,6 +147,7 @@ static void test_dctcp(void)
  		return;
  	}
 
+	cb_opts.map_fd = bpf_map__fd(dctcp_skel->maps.sk_stg_map);
  	do_test(&opts, dctcp_skel->maps.sk_stg_map);
  	ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");
 
#8 lore raw

PATCH v5 7/7 selftests/bpf: Add post_connect_cb callback

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

For getting rid of the second parameter of do_test(), this patch adds a new callback post_connect_cb in struct network_helper_opts, it will be invoked after connect_fd_to_addr() in connect_to_fd_opts().

Then define a dctcp dedicated post_connect_cb callback, invoking bpf_map_lookup_elem() in it, named stg_post_connect_cb() and set it in test_dctcp().

---
 tools/testing/selftests/bpf/network_helpers.c |  7 ++-
 tools/testing/selftests/bpf/network_helpers.h |  1 +
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     | 45 ++++++++++---------
 3 files changed, 31 insertions(+), 22 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +6 -1
index e20caef06aae..75589597c17e 100644
@@ -353,10 +353,15 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
 	    opts->post_socket_cb(fd, opts->cb_opts))
 		goto error_close;
 
-	if (!opts->noconnect)
+	if (!opts->noconnect) {
 		if (connect_fd_to_addr(fd, &addr, addrlen, opts->must_fail))
 			goto error_close;
 
+		if (opts->post_connect_cb &&
+		    opts->post_connect_cb(fd, opts->cb_opts))
+			goto error_close;
+	}
+
 	return fd;
 
 error_close:
tools/testing/selftests/bpf/network_helpers.h +1 -0
index 11eea8e2e4f1..b387e8142f3c 100644
@@ -28,6 +28,7 @@ struct network_helper_opts {
 	int type;
 	int proto;
 	int (*post_socket_cb)(int fd, void *opts);
+	int (*post_connect_cb)(int fd, void *opts);
 	void *cb_opts;
 };
 
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +24 -21

119 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw
In reply to Geliang Tang
This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 6 lines · On Sat, 25 May 2024 20:08:14 +0800 you wrote:

From: Geliang Tang <[email protected]>

This patchset uses post_socket_cb and post_connect_cb callbacks of struct network_helper_opts to refactor do_test() in bpf_tcp_ca.c to move dctcp test dedicated code out of do_test() into test_dctcp().

Quoted 8 lines

v5:
- address Martin's comments in v4 (thanks)
- add patch 4, use start_server_str in test_dctcp_fallback too
- ASSERT_* is already used in settcpca, use this helper in cc_cb (patch 3) and stg_post_socket_cb (patch 6)
- add ASSERT_* in stg_post_socket_cb in patch 6

[...]

Here is the summary with links:
- [bpf-next,v5,1/7] selftests/bpf: Drop struct post_socket_opts

    https://git.kernel.org/bpf/bpf-next/c/ed31adf6874d
  - [bpf-next,v5,2/7] selftests/bpf: Add start_server_str helper
    https://git.kernel.org/bpf/bpf-next/c/6f802cb8988e
  - [bpf-next,v5,3/7] selftests/bpf: Use post_socket_cb in connect_to_fd_opts
    https://git.kernel.org/bpf/bpf-next/c/e078255abd53
  - [bpf-next,v5,4/7] selftests/bpf: Use post_socket_cb in start_server_str
    https://git.kernel.org/bpf/bpf-next/c/79b330c57deb
  - [bpf-next,v5,5/7] selftests/bpf: Use start_server_str in do_test in bpf_tcp_ca
    https://git.kernel.org/bpf/bpf-next/c/ed61271af523

- [bpf-next,v5,6/7] selftests/bpf: Use connect_to_fd_opts in do_test in bpf_tcp_ca (no matching commit)
- [bpf-next,v5,7/7] selftests/bpf: Add post_connect_cb callback
(no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html