diff mbox series

[bpf-next,v3,3/8] selftests/bpf: Drop type parameter of connect_to_addr

Message ID 3a0ac6ce2cfd09df244dff1766af57861bc7598c.1716369375.git.tanggeliang@kylinos.cn
State New
Headers show
Series use network helpers, part 5 | expand

Commit Message

Geliang Tang May 22, 2024, 9:23 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

Since "type" is a struct member of network_helper_opts, it makes sense
to drop the "type" parameter of connect_to_addr() helper, using opts->type
instead in this helper.

If no network_helper_opts is set, use SOCK_STREAM as default socket type.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c |  4 ++--
 tools/testing/selftests/bpf/network_helpers.h |  2 +-
 .../selftests/bpf/prog_tests/cls_redirect.c   |  2 +-
 .../selftests/bpf/prog_tests/sk_assign.c      |  5 +++-
 .../selftests/bpf/prog_tests/sock_addr.c      | 23 ++++++++++++-------
 5 files changed, 23 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 129ac90e4528..49e807ffcccb 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -268,7 +268,7 @@  static int connect_fd_to_addr(int fd,
 	return 0;
 }
 
-int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
+int connect_to_addr(const struct sockaddr_storage *addr, socklen_t addrlen,
 		    const struct network_helper_opts *opts)
 {
 	int fd;
@@ -276,7 +276,7 @@  int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add
 	if (!opts)
 		opts = &default_opts;
 
-	fd = socket(addr->ss_family, type, opts->proto);
+	fd = socket(addr->ss_family, opts->type ? : SOCK_STREAM, opts->proto);
 	if (fd < 0) {
 		log_err("Failed to create client socket");
 		return -1;
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 9de84a8d00fd..c925c771bd98 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -56,7 +56,7 @@  int *start_reuseport_server(int family, int type, const char *addr_str,
 int start_server_addr(const struct sockaddr_storage *addr, socklen_t len,
 		      const struct network_helper_opts *opts);
 void free_fds(int *fds, unsigned int nr_close_fds);
-int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len,
+int connect_to_addr(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, const struct network_helper_opts *opts);
diff --git a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c
index 53b81651a6fa..1f1b9f0c2ffc 100644
--- a/tools/testing/selftests/bpf/prog_tests/cls_redirect.c
+++ b/tools/testing/selftests/bpf/prog_tests/cls_redirect.c
@@ -76,7 +76,7 @@  static bool set_up_conn(const struct sockaddr *addr, socklen_t len, int type,
 	if (CHECK_FAIL(getsockname(*server, sa, &slen)))
 		goto close_server;
 
-	*conn = connect_to_addr(type, (struct sockaddr_storage *)sa, slen, NULL);
+	*conn = connect_to_addr((struct sockaddr_storage *)sa, slen, &opts);
 	if (*conn < 0)
 		goto close_server;
 
diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
index 517d1186e386..91f65b407f3a 100644
--- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
+++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
@@ -109,12 +109,15 @@  rcv_msg(int srv_client, int type)
 static int
 run_test(int server_fd, const struct sockaddr *addr, socklen_t len, int type)
 {
+	struct network_helper_opts opts = {
+		.type = type,
+	};
 	int client = -1, srv_client = -1;
 	char buf[] = "testing";
 	in_port_t port;
 	int ret = 1;
 
-	client = connect_to_addr(type, (struct sockaddr_storage *)addr, len, NULL);
+	client = connect_to_addr((struct sockaddr_storage *)addr, len, &opts);
 	if (client == -1) {
 		perror("Cannot connect to server");
 		goto out;
diff --git a/tools/testing/selftests/bpf/prog_tests/sock_addr.c b/tools/testing/selftests/bpf/prog_tests/sock_addr.c
index b880c564a204..244686d582cb 100644
--- a/tools/testing/selftests/bpf/prog_tests/sock_addr.c
+++ b/tools/testing/selftests/bpf/prog_tests/sock_addr.c
@@ -224,12 +224,12 @@  static int kernel_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
 	return sock_addr_op("kernel_getpeername", addr, addrlen, true);
 }
 
-int kernel_connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen,
+int kernel_connect_to_addr(const struct sockaddr_storage *addr, socklen_t addrlen,
 			   const struct network_helper_opts *opts)
 {
 	int err;
 
-	if (!ASSERT_OK(kernel_init_sock(addr->ss_family, type, 0),
+	if (!ASSERT_OK(kernel_init_sock(addr->ss_family, opts->type, 0),
 		       "kernel_init_sock"))
 		goto err;
 
@@ -278,7 +278,7 @@  int kernel_start_server(int family, int type, const char *addr_str, __u16 port,
 }
 
 struct sock_ops {
-	int (*connect_to_addr)(int type, const struct sockaddr_storage *addr,
+	int (*connect_to_addr)(const struct sockaddr_storage *addr,
 			       socklen_t addrlen,
 			       const struct network_helper_opts *opts);
 	int (*start_server)(int family, int type, const char *addr_str,
@@ -2284,6 +2284,9 @@  static int test_bind(struct sock_addr_test *test)
 {
 	struct sockaddr_storage expected_addr;
 	socklen_t expected_addr_len = sizeof(struct sockaddr_storage);
+	struct network_helper_opts opts = {
+		.type = test->socket_type,
+	};
 	int serv = -1, client = -1, err;
 
 	serv = test->ops->start_server(test->socket_family, test->socket_type,
@@ -2306,7 +2309,7 @@  static int test_bind(struct sock_addr_test *test)
 		goto cleanup;
 
 	/* Try to connect to server just in case */
-	client = connect_to_addr(test->socket_type, &expected_addr, expected_addr_len, NULL);
+	client = connect_to_addr(&expected_addr, expected_addr_len, &opts);
 	if (!ASSERT_GE(client, 0, "connect_to_addr"))
 		goto cleanup;
 
@@ -2327,6 +2330,9 @@  static int test_connect(struct sock_addr_test *test)
 	socklen_t addr_len = sizeof(struct sockaddr_storage),
 		  expected_addr_len = sizeof(struct sockaddr_storage),
 		  expected_src_addr_len = sizeof(struct sockaddr_storage);
+	struct network_helper_opts opts = {
+		.type = test->socket_type,
+	};
 	int serv = -1, client = -1, err;
 
 	serv = start_server(test->socket_family, test->socket_type,
@@ -2339,8 +2345,7 @@  static int test_connect(struct sock_addr_test *test)
 	if (!ASSERT_EQ(err, 0, "make_sockaddr"))
 		goto cleanup;
 
-	client = test->ops->connect_to_addr(test->socket_type, &addr, addr_len,
-					    NULL);
+	client = test->ops->connect_to_addr(&addr, addr_len, &opts);
 	if (client < 0) {
 		err = errno;
 		goto err;
@@ -2519,6 +2524,9 @@  static int test_getpeername(struct sock_addr_test *test)
 	struct sockaddr_storage addr, expected_addr;
 	socklen_t addr_len = sizeof(struct sockaddr_storage),
 		  expected_addr_len = sizeof(struct sockaddr_storage);
+	struct network_helper_opts opts = {
+		.type = test->socket_type,
+	};
 	int serv = -1, client = -1, err;
 
 	serv = start_server(test->socket_family, test->socket_type,
@@ -2531,8 +2539,7 @@  static int test_getpeername(struct sock_addr_test *test)
 	if (!ASSERT_EQ(err, 0, "make_sockaddr"))
 		goto cleanup;
 
-	client = test->ops->connect_to_addr(test->socket_type, &addr, addr_len,
-					    NULL);
+	client = test->ops->connect_to_addr(&addr, addr_len, &opts);
 	if (!ASSERT_GE(client, 0, "connect_to_addr"))
 		goto cleanup;