diff mbox series

[bpf-next,v4,6/9] selftests/bpf: Invoke attach_reuseport out of make_server

Message ID f846835a758f2e531026083253eeb747205bd0dd.1719203293.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use network helpers, part 8 | expand

Commit Message

Geliang Tang June 24, 2024, 4:33 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

In order to facilitate subsequent commits to drop make_server(), this patch
invokes attach_reuseport() out of make_server(), right after invoking
make_server() if the passed "reuseport_prog" argument is not NULL.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 .../selftests/bpf/prog_tests/sk_lookup.c      | 21 +++++++++----------
 1 file changed, 10 insertions(+), 11 deletions(-)

Comments

Eduard Zingerman June 25, 2024, 12:59 a.m. UTC | #1
On Mon, 2024-06-24 at 12:33 +0800, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
> 
> In order to facilitate subsequent commits to drop make_server(), this patch
> invokes attach_reuseport() out of make_server(), right after invoking
> make_server() if the passed "reuseport_prog" argument is not NULL.
> 
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
>  .../selftests/bpf/prog_tests/sk_lookup.c      | 21 +++++++++----------
>  1 file changed, 10 insertions(+), 11 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> index de2466547efe..d87dfcf5db07 100644

Tbh, I don't like this refactoring for sk_lookup, it does not seem to
make the code clearer or shorter (e.g. 1414 LOC vs 1409).
If anything, it looks like reuseport_prog, callbacks setup and
start_server_str could be hidden insider make_server().

> --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
> @@ -204,15 +204,6 @@ static int make_server(int sotype, const char *ip, int port,
>  		}
>  	}
>  
> -	/* Late attach reuseport prog so we can have one init path */
> -	if (reuseport_prog) {
> -		err = attach_reuseport(fd, reuseport_prog);
> -		if (CHECK(err, "attach_reuseport", "failed\n")) {
> -			log_err("failed to attach reuseport prog");
> -			goto fail;
> -		}
> -	}
> -
>  	return fd;
>  fail:
>  	close(fd);
> @@ -610,7 +601,8 @@ static void run_lookup_prog(const struct test *t)
>  		server_fds[i] = make_server(t->sotype, t->listen_at.ip,
>  					    t->listen_at.port,
>  					    t->reuseport_prog);
> -		if (server_fds[i] < 0)
> +		if (server_fds[i] < 0 ||
> +		    attach_reuseport(server_fds[i], t->reuseport_prog))
>  			goto close;
>  
>  		err = update_lookup_map(t->sock_map, i, server_fds[i]);
> @@ -636,7 +628,8 @@ static void run_lookup_prog(const struct test *t)
>  		reuse_conn_fd = make_server(t->sotype, t->listen_at.ip,
>  					    t->listen_at.port,
>  					    t->reuseport_prog);
> -		if (reuse_conn_fd < 0)
> +		if (reuse_conn_fd < 0 ||
> +		    attach_reuseport(reuse_conn_fd, t->reuseport_prog))
>  			goto close;
>  
>  		/* Connect the extra socket to itself */
> @@ -878,6 +871,9 @@ static void drop_on_lookup(const struct test *t)
>  	if (server_fd < 0)
>  		goto detach;
>  
> +	if (attach_reuseport(server_fd, t->reuseport_prog))
> +		goto close_srv;
> +
>  	client_fd = make_socket(t->sotype, t->connect_to.ip,
>  				t->connect_to.port, &dst);
>  	if (client_fd < 0)
> @@ -992,6 +988,9 @@ static void drop_on_reuseport(const struct test *t)
>  	if (server1 < 0)
>  		goto detach;
>  
> +	if (attach_reuseport(server1, t->reuseport_prog))
> +		goto close_srv1;
> +
>  	err = update_lookup_map(t->sock_map, SERVER_A, server1);
>  	if (err)
>  		goto close_srv1;
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
index de2466547efe..d87dfcf5db07 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c
@@ -204,15 +204,6 @@  static int make_server(int sotype, const char *ip, int port,
 		}
 	}
 
-	/* Late attach reuseport prog so we can have one init path */
-	if (reuseport_prog) {
-		err = attach_reuseport(fd, reuseport_prog);
-		if (CHECK(err, "attach_reuseport", "failed\n")) {
-			log_err("failed to attach reuseport prog");
-			goto fail;
-		}
-	}
-
 	return fd;
 fail:
 	close(fd);
@@ -610,7 +601,8 @@  static void run_lookup_prog(const struct test *t)
 		server_fds[i] = make_server(t->sotype, t->listen_at.ip,
 					    t->listen_at.port,
 					    t->reuseport_prog);
-		if (server_fds[i] < 0)
+		if (server_fds[i] < 0 ||
+		    attach_reuseport(server_fds[i], t->reuseport_prog))
 			goto close;
 
 		err = update_lookup_map(t->sock_map, i, server_fds[i]);
@@ -636,7 +628,8 @@  static void run_lookup_prog(const struct test *t)
 		reuse_conn_fd = make_server(t->sotype, t->listen_at.ip,
 					    t->listen_at.port,
 					    t->reuseport_prog);
-		if (reuse_conn_fd < 0)
+		if (reuse_conn_fd < 0 ||
+		    attach_reuseport(reuse_conn_fd, t->reuseport_prog))
 			goto close;
 
 		/* Connect the extra socket to itself */
@@ -878,6 +871,9 @@  static void drop_on_lookup(const struct test *t)
 	if (server_fd < 0)
 		goto detach;
 
+	if (attach_reuseport(server_fd, t->reuseport_prog))
+		goto close_srv;
+
 	client_fd = make_socket(t->sotype, t->connect_to.ip,
 				t->connect_to.port, &dst);
 	if (client_fd < 0)
@@ -992,6 +988,9 @@  static void drop_on_reuseport(const struct test *t)
 	if (server1 < 0)
 		goto detach;
 
+	if (attach_reuseport(server1, t->reuseport_prog))
+		goto close_srv1;
+
 	err = update_lookup_map(t->sock_map, SERVER_A, server1);
 	if (err)
 		goto close_srv1;