bpf: selftests: Support custom type and proto for client sockets
authorDaniel Xu <dxu@dxuuu.xyz>
Fri, 21 Jul 2023 20:22:48 +0000 (14:22 -0600)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 28 Jul 2023 23:52:08 +0000 (16:52 -0700)
Extend connect_to_fd_opts() to take optional type and protocol
parameters for the client socket. These parameters are useful when
opening a raw socket to send IP fragments.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Link: https://lore.kernel.org/r/9067db539efdfd608aa86a2b143c521337c111fc.1689970773.git.dxu@dxuuu.xyz
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/network_helpers.c
tools/testing/selftests/bpf/network_helpers.h

index 4dcd63a..da72a3a 100644 (file)
@@ -270,14 +270,23 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
                opts = &default_opts;
 
        optlen = sizeof(type);
-       if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) {
-               log_err("getsockopt(SOL_TYPE)");
-               return -1;
+
+       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 (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) {
-               log_err("getsockopt(SOL_PROTOCOL)");
-               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);
index 87894dc..5eccc67 100644 (file)
@@ -22,6 +22,8 @@ struct network_helper_opts {
        int timeout_ms;
        bool must_fail;
        bool noconnect;
+       int type;
+       int proto;
 };
 
 /* ipv4 test vector */