]> www.infradead.org Git - users/hch/configfs.git/commitdiff
selftests/bpf: Drop type from network_helper_opts
authorGeliang Tang <tanggeliang@kylinos.cn>
Fri, 21 Jun 2024 02:15:58 +0000 (10:15 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 21 Jun 2024 03:42:44 +0000 (20:42 -0700)
The opts.{type, noconnect} 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 "type" parameter for connect_to_fd_opts(), then
opts->type and getsockopt(SO_TYPE) can be replaced by "type" parameter in
it.

In connect_to_fd(), use getsockopt(SO_TYPE) to get "type" value and pass
it to connect_to_fd_opts().

In bpf_tcp_ca.c and cgroup_v1v2.c, "SOCK_STREAM" types are passed to
connect_to_fd_opts(), and in ip_check_defrag.c, different types "SOCK_RAW"
and "SOCK_DGRAM" are passed to it.

With these changes, the strcut member "type" of network_helper_opts can be
dropped now.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/cfd20b5ad4085c1d1af5e79df3b09013a407199f.1718932493.git.tanggeliang@kylinos.cn
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c
tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c

index e20caef06aae9ebae9f64231144dfd253e87665f..c0646d5a4283463dd82ce0ddea8cfa7361c52480 100644 (file)
@@ -303,36 +303,16 @@ error_close:
        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;
+       socklen_t addrlen;
+       int fd;
 
        if (!opts)
                opts = &default_opts;
 
-       optlen = sizeof(type);
-
-       if (opts->type) {
-               type = opts->type;
-       } else {
-               if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
-                       log_err("getsockopt(SOL_TYPE)");
-                       return -1;
-               }
-       }
-
-       if (opts->proto) {
-               protocol = opts->proto;
-       } else {
-               if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
-                       log_err("getsockopt(SOL_PROTOCOL)");
-                       return -1;
-               }
-       }
-
        addrlen = sizeof(addr);
        if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) {
                log_err("Failed to get server addr");
@@ -340,7 +320,7 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
        }
 
        addr_in = (struct sockaddr_in *)&addr;
-       fd = socket(addr_in->sin_family, type, protocol);
+       fd = socket(addr_in->sin_family, type, opts->proto);
        if (fd < 0) {
                log_err("Failed to create client socket");
                return -1;
@@ -369,8 +349,23 @@ 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;
+       }
+
+       optlen = sizeof(protocol);
+       if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
+               log_err("getsockopt(SOL_PROTOCOL)");
+               return -1;
+       }
+       opts.proto = protocol;
 
-       return connect_to_fd_opts(server_fd, &opts);
+       return connect_to_fd_opts(server_fd, type, &opts);
 }
 
 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
index 11eea8e2e4f1e23d1fef674ae02af0d8ea63551c..c92bed35dfe2cbc609a93749a461f935097e2e0c 100644 (file)
@@ -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,7 +60,7 @@ 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, const struct network_helper_opts *opts);
+int connect_to_fd_opts(int server_fd, int type, 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);
index 67358adf5db3a5492552ef95e4291bcd5843baf1..164f237b24ddf35983e1ed854a4c3e9c4e4baacf 100644 (file)
@@ -49,7 +49,7 @@ static bool start_test(char *addr_str,
                goto err;
 
        /* connect to server */
-       *cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
+       *cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts);
        if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
                goto err;
 
index addf720428f7d5ca5ca91345552f72e02caac45a..9709c8db727512fba4a4614c849b6195717dbbbb 100644 (file)
@@ -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, &opts);
+       fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &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, &opts);
+       client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts);
        if (!ASSERT_GE(client_fd, 0, "client_fd")) {
                close(server_fd);
                return;
index 284764e7179f621ca98e0c77327bc2cbec5da04e..1607a05bf2c2a44924cbc409429be048730eb418 100644 (file)
@@ -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, SOCK_DGRAM, &rx_opts);
        close_netns(nstoken);
        if (!ASSERT_GE(client_rx_fd, 0, "connect_to_fd_opts"))
                goto out;