Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: selftests: Add connect_to_fd_opts to network_helpers

The next test requires to setsockopt(TCP_CONGESTION) before
connect(), so a new arg is needed for the connect_to_fd() to specify
the cc's name.

This patch adds a new "struct network_helper_opts" for the future
option needs. It starts with the "cc" and "timeout_ms" option.
A new helper connect_to_fd_opts() is added to take the new
"const struct network_helper_opts *opts" as an arg.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210824173019.3977910-1-kafai@fb.com

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
3d778983 700dcf0f

+27 -2
+21 -2
tools/testing/selftests/bpf/network_helpers.c
··· 218 218 return 0; 219 219 } 220 220 221 - int connect_to_fd(int server_fd, int timeout_ms) 221 + static const struct network_helper_opts default_opts; 222 + 223 + int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) 222 224 { 223 225 struct sockaddr_storage addr; 224 226 struct sockaddr_in *addr_in; 225 227 socklen_t addrlen, optlen; 226 228 int fd, type; 229 + 230 + if (!opts) 231 + opts = &default_opts; 227 232 228 233 optlen = sizeof(type); 229 234 if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { ··· 249 244 return -1; 250 245 } 251 246 252 - if (settimeo(fd, timeout_ms)) 247 + if (settimeo(fd, opts->timeout_ms)) 248 + goto error_close; 249 + 250 + if (opts->cc && opts->cc[0] && 251 + setsockopt(fd, SOL_TCP, TCP_CONGESTION, opts->cc, 252 + strlen(opts->cc) + 1)) 253 253 goto error_close; 254 254 255 255 if (connect_fd_to_addr(fd, &addr, addrlen)) ··· 265 255 error_close: 266 256 save_errno_close(fd); 267 257 return -1; 258 + } 259 + 260 + int connect_to_fd(int server_fd, int timeout_ms) 261 + { 262 + struct network_helper_opts opts = { 263 + .timeout_ms = timeout_ms, 264 + }; 265 + 266 + return connect_to_fd_opts(server_fd, &opts); 268 267 } 269 268 270 269 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms)
+6
tools/testing/selftests/bpf/network_helpers.h
··· 17 17 #define VIP_NUM 5 18 18 #define MAGIC_BYTES 123 19 19 20 + struct network_helper_opts { 21 + const char *cc; 22 + int timeout_ms; 23 + }; 24 + 20 25 /* ipv4 test vector */ 21 26 struct ipv4_packet { 22 27 struct ethhdr eth; ··· 46 41 unsigned int nr_listens); 47 42 void free_fds(int *fds, unsigned int nr_close_fds); 48 43 int connect_to_fd(int server_fd, int timeout_ms); 44 + int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); 49 45 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); 50 46 int fastopen_connect(int server_fd, const char *data, unsigned int data_len, 51 47 int timeout_ms);