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

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'use network helpers, part 9'

Geliang Tang says:

====================
v3:
- patch 2:
- clear errno before connect_to_fd_opts.
- print err logs in run_test.
- set err to -1 when fd >= 0.
- patch 3:
- drop "int err".

v2:
- update patch 2 as Martin suggested.

This is the 9th part of series "use network helpers" all BPF selftests
wide.

Patches 1-2 update network helpers interfaces suggested by Martin.
Patch 3 adds a new helper connect_to_addr_str() as Martin suggested
instead of adding connect_fd_to_addr_str().
Patch 4 uses this newly added helper in make_client().
Patch 5 uses make_client() in sk_lookup and drop make_socket().
====================

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

authored by

Martin KaFai Lau and committed by
Andrii Nakryiko
22912472 1722389b

+46 -53
+33 -43
tools/testing/selftests/bpf/network_helpers.c
··· 277 277 return -1; 278 278 } 279 279 280 - static int connect_fd_to_addr(int fd, 281 - const struct sockaddr_storage *addr, 282 - socklen_t addrlen, const bool must_fail) 283 - { 284 - int ret; 285 - 286 - errno = 0; 287 - ret = connect(fd, (const struct sockaddr *)addr, addrlen); 288 - if (must_fail) { 289 - if (!ret) { 290 - log_err("Unexpected success to connect to server"); 291 - return -1; 292 - } 293 - if (errno != EPERM) { 294 - log_err("Unexpected error from connect to server"); 295 - return -1; 296 - } 297 - } else { 298 - if (ret) { 299 - log_err("Failed to connect to server"); 300 - return -1; 301 - } 302 - } 303 - 304 - return 0; 305 - } 306 - 307 280 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addrlen, 308 281 const struct network_helper_opts *opts) 309 282 { ··· 291 318 return -1; 292 319 } 293 320 294 - if (connect_fd_to_addr(fd, addr, addrlen, opts->must_fail)) 295 - goto error_close; 321 + if (connect(fd, (const struct sockaddr *)addr, addrlen)) { 322 + log_err("Failed to connect to server"); 323 + save_errno_close(fd); 324 + return -1; 325 + } 296 326 297 327 return fd; 298 - 299 - error_close: 300 - save_errno_close(fd); 301 - return -1; 302 328 } 303 329 304 - int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts) 330 + int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port, 331 + const struct network_helper_opts *opts) 305 332 { 306 333 struct sockaddr_storage addr; 307 334 socklen_t addrlen; 308 335 309 336 if (!opts) 310 337 opts = &default_opts; 338 + 339 + if (make_sockaddr(family, addr_str, port, &addr, &addrlen)) 340 + return -1; 341 + 342 + return connect_to_addr(type, &addr, addrlen, opts); 343 + } 344 + 345 + int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) 346 + { 347 + struct sockaddr_storage addr; 348 + socklen_t addrlen, optlen; 349 + int type; 350 + 351 + if (!opts) 352 + opts = &default_opts; 353 + 354 + optlen = sizeof(type); 355 + if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { 356 + log_err("getsockopt(SOL_TYPE)"); 357 + return -1; 358 + } 311 359 312 360 addrlen = sizeof(addr); 313 361 if (getsockname(server_fd, (struct sockaddr *)&addr, &addrlen)) { ··· 344 350 struct network_helper_opts opts = { 345 351 .timeout_ms = timeout_ms, 346 352 }; 347 - int type, protocol; 348 353 socklen_t optlen; 349 - 350 - optlen = sizeof(type); 351 - if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { 352 - log_err("getsockopt(SOL_TYPE)"); 353 - return -1; 354 - } 354 + int protocol; 355 355 356 356 optlen = sizeof(protocol); 357 357 if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) { ··· 354 366 } 355 367 opts.proto = protocol; 356 368 357 - return connect_to_fd_opts(server_fd, type, &opts); 369 + return connect_to_fd_opts(server_fd, &opts); 358 370 } 359 371 360 372 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms) ··· 370 382 return -1; 371 383 } 372 384 373 - if (connect_fd_to_addr(client_fd, &addr, len, false)) 385 + if (connect(client_fd, (const struct sockaddr *)&addr, len)) { 386 + log_err("Failed to connect to server"); 374 387 return -1; 388 + } 375 389 376 390 return 0; 377 391 }
+3 -2
tools/testing/selftests/bpf/network_helpers.h
··· 23 23 24 24 struct network_helper_opts { 25 25 int timeout_ms; 26 - bool must_fail; 27 26 int proto; 28 27 /* +ve: Passed to listen() as-is. 29 28 * 0: Default when the test does not set ··· 69 70 const struct network_helper_opts *opts); 70 71 int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t len, 71 72 const struct network_helper_opts *opts); 73 + int connect_to_addr_str(int family, int type, const char *addr_str, __u16 port, 74 + const struct network_helper_opts *opts); 72 75 int connect_to_fd(int server_fd, int timeout_ms); 73 - int connect_to_fd_opts(int server_fd, int type, const struct network_helper_opts *opts); 76 + int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts); 74 77 int connect_fd_to_fd(int client_fd, int server_fd, int timeout_ms); 75 78 int fastopen_connect(int server_fd, const char *data, unsigned int data_len, 76 79 int timeout_ms);
+1 -1
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c
··· 49 49 goto err; 50 50 51 51 /* connect to server */ 52 - *cli_fd = connect_to_fd_opts(*srv_fd, SOCK_STREAM, cli_opts); 52 + *cli_fd = connect_to_fd_opts(*srv_fd, cli_opts); 53 53 if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts")) 54 54 goto err; 55 55
+9 -7
tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c
··· 9 9 10 10 static int run_test(int cgroup_fd, int server_fd, bool classid) 11 11 { 12 - struct network_helper_opts opts = { 13 - .must_fail = true, 14 - }; 15 12 struct connect4_dropper *skel; 16 13 int fd, err = 0; 17 14 ··· 29 32 goto out; 30 33 } 31 34 32 - fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); 33 - if (fd < 0) 35 + errno = 0; 36 + fd = connect_to_fd_opts(server_fd, NULL); 37 + if (fd >= 0) { 38 + log_err("Unexpected success to connect to server"); 34 39 err = -1; 35 - else 36 40 close(fd); 41 + } else if (errno != EPERM) { 42 + log_err("Unexpected errno from connect to server"); 43 + err = -1; 44 + } 37 45 out: 38 46 connect4_dropper__destroy(skel); 39 47 return err; ··· 54 52 server_fd = start_server(AF_INET, SOCK_STREAM, NULL, port, 0); 55 53 if (!ASSERT_GE(server_fd, 0, "server_fd")) 56 54 return; 57 - client_fd = connect_to_fd_opts(server_fd, SOCK_STREAM, &opts); 55 + client_fd = connect_to_fd_opts(server_fd, &opts); 58 56 if (!ASSERT_GE(client_fd, 0, "client_fd")) { 59 57 close(server_fd); 60 58 return;