From: Geliang Tang <tanggeliang@xxxxxxxxxx> __start_server() sets SO_REUSPORT through setsockopt() when the parameter 'reuseport' is set. This patch makes it more flexible by accepting a function pointer named setsockopt. Then the original start_reuseport_server() can be implemented by passing in a newly defined setsockopt_reuse() function pointer to __start_server(). Signed-off-by: Geliang Tang <tanggeliang@xxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 91b014784dd9..2a389e8c2503 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -79,9 +79,9 @@ int settimeo(int fd, int timeout_ms) #define save_errno_close(fd) ({ int __save = errno; close(fd); errno = __save; }) static int __start_server(int type, int protocol, const struct sockaddr *addr, - socklen_t addrlen, int timeout_ms, bool reuseport) + socklen_t addrlen, int timeout_ms, + int *(*setsockopt)(int fd, int val), int val) { - int on = 1; int fd; fd = socket(addr->sa_family, type, protocol); @@ -93,9 +93,8 @@ static int __start_server(int type, int protocol, const struct sockaddr *addr, if (settimeo(fd, timeout_ms)) goto error_close; - if (reuseport && - setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on))) { - log_err("Failed to set SO_REUSEPORT"); + if (setsockopt && setsockopt(fd, val)) { + log_err("Failed to set sockopt"); goto error_close; } @@ -128,7 +127,7 @@ static int start_server_proto(int family, int type, int protocol, return -1; return __start_server(type, protocol, (struct sockaddr *)&addr, - addrlen, timeout_ms, false); + addrlen, timeout_ms, NULL, 0); } int start_server(int family, int type, const char *addr_str, __u16 port, @@ -144,6 +143,11 @@ int start_mptcp_server(int family, const char *addr_str, __u16 port, port, timeout_ms); } +static int setsockopt_reuse(int fd, int on) +{ + return setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)); +} + int *start_reuseport_server(int family, int type, const char *addr_str, __u16 port, int timeout_ms, unsigned int nr_listens) { @@ -163,7 +167,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, return NULL; fds[0] = __start_server(type, 0, (struct sockaddr *)&addr, addrlen, - timeout_ms, true); + timeout_ms, (void *)setsockopt_reuse, 1); if (fds[0] == -1) goto close_fds; nr_fds = 1; @@ -173,7 +177,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, for (; nr_fds < nr_listens; nr_fds++) { fds[nr_fds] = __start_server(type, 0, (struct sockaddr *)&addr, - addrlen, timeout_ms, true); + addrlen, timeout_ms, (void *)setsockopt_reuse, 1); if (fds[nr_fds] == -1) goto close_fds; } @@ -187,7 +191,7 @@ int *start_reuseport_server(int family, int type, const char *addr_str, int start_server_addr(const struct sockaddr *addr, socklen_t addrlen, int type) { - return __start_server(type, 0, addr, addrlen, 0, 0); + return __start_server(type, 0, addr, addrlen, 0, NULL, 0); } void free_fds(int *fds, unsigned int nr_close_fds) -- 2.40.1