From 5dfa2a30b52d7c89f04ac47afea0f5c36a89cda1 Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Mon, 27 May 2024 14:40:54 +0800 Subject: [PATCH] selftests/bpf: Use connect_to_addr in sockmap_basic Signed-off-by: Geliang Tang --- tools/testing/selftests/bpf/network_helpers.c | 8 +++ .../selftests/bpf/prog_tests/sockmap_basic.c | 49 ++++++++++++------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 30655fe92428e..b84ad74ccf00a 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -283,9 +283,17 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t add if (settimeo(fd, opts->timeout_ms)) goto error_close; + if (opts->post_socket_cb && + opts->post_socket_cb(fd, opts->cb_opts)) + goto error_close; + if (connect_fd_to_addr(fd, addr, addrlen, false)) goto error_close; + if (opts->post_connect_cb && + opts->post_connect_cb(fd, opts->cb_opts)) + goto error_close; + return fd; error_close: diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index 1337153eb0ad7..9b29f12f5b792 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -5,6 +5,7 @@ #include #include "test_progs.h" +#include "network_helpers.h" #include "test_skmsg_load_helpers.skel.h" #include "test_sockmap_update.skel.h" #include "test_sockmap_invalid_update.skel.h" @@ -21,32 +22,46 @@ #define TCP_REPAIR_ON 1 #define TCP_REPAIR_OFF_NO_WP -1 /* Turn off without window probes */ +static int repair_socket_cb(int fd, void *opts) +{ + int err, repair; + + repair = TCP_REPAIR_ON; + err = setsockopt(fd, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair)); + if (!ASSERT_OK(err, "setsockopt(TCP_REPAIR)")) + return err; + + return 0; +} + +static int repair_connect_cb(int fd, void *opts) +{ + int err, repair; + + repair = TCP_REPAIR_OFF_NO_WP; + err = setsockopt(fd, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair)); + if (!ASSERT_OK(err, "setsockopt(TCP_REPAIR)")) + return err; + + return 0; +} + static int connected_socket_v4(void) { + struct network_helper_opts opts = { + .post_socket_cb = repair_socket_cb, + .post_connect_cb = repair_connect_cb, + }; struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(80), .sin_addr = { inet_addr("127.0.0.1") }, }; socklen_t len = sizeof(addr); - int s, repair, err; - - s = socket(AF_INET, SOCK_STREAM, 0); - if (!ASSERT_GE(s, 0, "socket")) - goto error; - - repair = TCP_REPAIR_ON; - err = setsockopt(s, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair)); - if (!ASSERT_OK(err, "setsockopt(TCP_REPAIR)")) - goto error; - - err = connect(s, (struct sockaddr *)&addr, len); - if (!ASSERT_OK(err, "connect")) - goto error; + int s; - repair = TCP_REPAIR_OFF_NO_WP; - err = setsockopt(s, SOL_TCP, TCP_REPAIR, &repair, sizeof(repair)); - if (!ASSERT_OK(err, "setsockopt(TCP_REPAIR)")) + s = connect_to_addr(SOCK_STREAM, (struct sockaddr_storage *)&addr, len, &opts); + if (!ASSERT_GE(s, 0, "connect_to_addr")) goto error; return s;