1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 4 #include <linux/err.h> 5 #include <netinet/tcp.h> 6 #include <test_progs.h> 7 #include "network_helpers.h" 8 #include "bpf_dctcp.skel.h" 9 #include "bpf_cubic.skel.h" 10 #include "bpf_tcp_nogpl.skel.h" 11 #include "tcp_ca_update.skel.h" 12 #include "bpf_dctcp_release.skel.h" 13 #include "tcp_ca_write_sk_pacing.skel.h" 14 #include "tcp_ca_incompl_cong_ops.skel.h" 15 #include "tcp_ca_unsupp_cong_op.skel.h" 16 17 #ifndef ENOTSUPP 18 #define ENOTSUPP 524 19 #endif 20 21 static const unsigned int total_bytes = 10 * 1024 * 1024; 22 static int expected_stg = 0xeB9F; 23 static int stop, duration; 24 25 static int settcpca(int fd, const char *tcp_ca) 26 { 27 int err; 28 29 err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca)); 30 if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n", 31 errno)) 32 return -1; 33 34 return 0; 35 } 36 37 static void *server(void *arg) 38 { 39 int lfd = (int)(long)arg, err = 0, fd; 40 ssize_t nr_sent = 0, bytes = 0; 41 char batch[1500]; 42 43 fd = accept(lfd, NULL, NULL); 44 while (fd == -1) { 45 if (errno == EINTR) 46 continue; 47 err = -errno; 48 goto done; 49 } 50 51 if (settimeo(fd, 0)) { 52 err = -errno; 53 goto done; 54 } 55 56 while (bytes < total_bytes && !READ_ONCE(stop)) { 57 nr_sent = send(fd, &batch, 58 MIN(total_bytes - bytes, sizeof(batch)), 0); 59 if (nr_sent == -1 && errno == EINTR) 60 continue; 61 if (nr_sent == -1) { 62 err = -errno; 63 break; 64 } 65 bytes += nr_sent; 66 } 67 68 CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n", 69 bytes, total_bytes, nr_sent, errno); 70 71 done: 72 if (fd >= 0) 73 close(fd); 74 if (err) { 75 WRITE_ONCE(stop, 1); 76 return ERR_PTR(err); 77 } 78 return NULL; 79 } 80 81 static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) 82 { 83 struct sockaddr_in6 sa6 = {}; 84 ssize_t nr_recv = 0, bytes = 0; 85 int lfd = -1, fd = -1; 86 pthread_t srv_thread; 87 socklen_t addrlen = sizeof(sa6); 88 void *thread_ret; 89 char batch[1500]; 90 int err; 91 92 WRITE_ONCE(stop, 0); 93 94 lfd = socket(AF_INET6, SOCK_STREAM, 0); 95 if (CHECK(lfd == -1, "socket", "errno:%d\n", errno)) 96 return; 97 fd = socket(AF_INET6, SOCK_STREAM, 0); 98 if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) { 99 close(lfd); 100 return; 101 } 102 103 if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) || 104 settimeo(lfd, 0) || settimeo(fd, 0)) 105 goto done; 106 107 /* bind, listen and start server thread to accept */ 108 sa6.sin6_family = AF_INET6; 109 sa6.sin6_addr = in6addr_loopback; 110 err = bind(lfd, (struct sockaddr *)&sa6, addrlen); 111 if (CHECK(err == -1, "bind", "errno:%d\n", errno)) 112 goto done; 113 err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen); 114 if (CHECK(err == -1, "getsockname", "errno:%d\n", errno)) 115 goto done; 116 err = listen(lfd, 1); 117 if (CHECK(err == -1, "listen", "errno:%d\n", errno)) 118 goto done; 119 120 if (sk_stg_map) { 121 err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd, 122 &expected_stg, BPF_NOEXIST); 123 if (CHECK(err, "bpf_map_update_elem(sk_stg_map)", 124 "err:%d errno:%d\n", err, errno)) 125 goto done; 126 } 127 128 /* connect to server */ 129 err = connect(fd, (struct sockaddr *)&sa6, addrlen); 130 if (CHECK(err == -1, "connect", "errno:%d\n", errno)) 131 goto done; 132 133 if (sk_stg_map) { 134 int tmp_stg; 135 136 err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd, 137 &tmp_stg); 138 if (CHECK(!err || errno != ENOENT, 139 "bpf_map_lookup_elem(sk_stg_map)", 140 "err:%d errno:%d\n", err, errno)) 141 goto done; 142 } 143 144 err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd); 145 if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno)) 146 goto done; 147 148 /* recv total_bytes */ 149 while (bytes < total_bytes && !READ_ONCE(stop)) { 150 nr_recv = recv(fd, &batch, 151 MIN(total_bytes - bytes, sizeof(batch)), 0); 152 if (nr_recv == -1 && errno == EINTR) 153 continue; 154 if (nr_recv == -1) 155 break; 156 bytes += nr_recv; 157 } 158 159 CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n", 160 bytes, total_bytes, nr_recv, errno); 161 162 WRITE_ONCE(stop, 1); 163 pthread_join(srv_thread, &thread_ret); 164 CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld", 165 PTR_ERR(thread_ret)); 166 done: 167 close(lfd); 168 close(fd); 169 } 170 171 static void test_cubic(void) 172 { 173 struct bpf_cubic *cubic_skel; 174 struct bpf_link *link; 175 176 cubic_skel = bpf_cubic__open_and_load(); 177 if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n")) 178 return; 179 180 link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic); 181 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) { 182 bpf_cubic__destroy(cubic_skel); 183 return; 184 } 185 186 do_test("bpf_cubic", NULL); 187 188 bpf_link__destroy(link); 189 bpf_cubic__destroy(cubic_skel); 190 } 191 192 static void test_dctcp(void) 193 { 194 struct bpf_dctcp *dctcp_skel; 195 struct bpf_link *link; 196 197 dctcp_skel = bpf_dctcp__open_and_load(); 198 if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n")) 199 return; 200 201 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp); 202 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) { 203 bpf_dctcp__destroy(dctcp_skel); 204 return; 205 } 206 207 do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map); 208 CHECK(dctcp_skel->bss->stg_result != expected_stg, 209 "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n", 210 dctcp_skel->bss->stg_result, expected_stg); 211 212 bpf_link__destroy(link); 213 bpf_dctcp__destroy(dctcp_skel); 214 } 215 216 static char *err_str; 217 static bool found; 218 219 static int libbpf_debug_print(enum libbpf_print_level level, 220 const char *format, va_list args) 221 { 222 const char *prog_name, *log_buf; 223 224 if (level != LIBBPF_WARN || 225 !strstr(format, "-- BEGIN PROG LOAD LOG --")) { 226 vprintf(format, args); 227 return 0; 228 } 229 230 prog_name = va_arg(args, char *); 231 log_buf = va_arg(args, char *); 232 if (!log_buf) 233 goto out; 234 if (err_str && strstr(log_buf, err_str) != NULL) 235 found = true; 236 out: 237 printf(format, prog_name, log_buf); 238 return 0; 239 } 240 241 static void test_invalid_license(void) 242 { 243 libbpf_print_fn_t old_print_fn; 244 struct bpf_tcp_nogpl *skel; 245 246 err_str = "struct ops programs must have a GPL compatible license"; 247 found = false; 248 old_print_fn = libbpf_set_print(libbpf_debug_print); 249 250 skel = bpf_tcp_nogpl__open_and_load(); 251 ASSERT_NULL(skel, "bpf_tcp_nogpl"); 252 ASSERT_EQ(found, true, "expected_err_msg"); 253 254 bpf_tcp_nogpl__destroy(skel); 255 libbpf_set_print(old_print_fn); 256 } 257 258 static void test_dctcp_fallback(void) 259 { 260 int err, lfd = -1, cli_fd = -1, srv_fd = -1; 261 struct network_helper_opts opts = { 262 .cc = "cubic", 263 }; 264 struct bpf_dctcp *dctcp_skel; 265 struct bpf_link *link = NULL; 266 char srv_cc[16]; 267 socklen_t cc_len = sizeof(srv_cc); 268 269 dctcp_skel = bpf_dctcp__open(); 270 if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel")) 271 return; 272 strcpy(dctcp_skel->rodata->fallback, "cubic"); 273 if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load")) 274 goto done; 275 276 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp); 277 if (!ASSERT_OK_PTR(link, "dctcp link")) 278 goto done; 279 280 lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0); 281 if (!ASSERT_GE(lfd, 0, "lfd") || 282 !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp")) 283 goto done; 284 285 cli_fd = connect_to_fd_opts(lfd, &opts); 286 if (!ASSERT_GE(cli_fd, 0, "cli_fd")) 287 goto done; 288 289 srv_fd = accept(lfd, NULL, 0); 290 if (!ASSERT_GE(srv_fd, 0, "srv_fd")) 291 goto done; 292 ASSERT_STREQ(dctcp_skel->bss->cc_res, "cubic", "cc_res"); 293 ASSERT_EQ(dctcp_skel->bss->tcp_cdg_res, -ENOTSUPP, "tcp_cdg_res"); 294 /* All setsockopt(TCP_CONGESTION) in the recurred 295 * bpf_dctcp->init() should fail with -EBUSY. 296 */ 297 ASSERT_EQ(dctcp_skel->bss->ebusy_cnt, 3, "ebusy_cnt"); 298 299 err = getsockopt(srv_fd, SOL_TCP, TCP_CONGESTION, srv_cc, &cc_len); 300 if (!ASSERT_OK(err, "getsockopt(srv_fd, TCP_CONGESTION)")) 301 goto done; 302 ASSERT_STREQ(srv_cc, "cubic", "srv_fd cc"); 303 304 done: 305 bpf_link__destroy(link); 306 bpf_dctcp__destroy(dctcp_skel); 307 if (lfd != -1) 308 close(lfd); 309 if (srv_fd != -1) 310 close(srv_fd); 311 if (cli_fd != -1) 312 close(cli_fd); 313 } 314 315 static void test_rel_setsockopt(void) 316 { 317 struct bpf_dctcp_release *rel_skel; 318 libbpf_print_fn_t old_print_fn; 319 320 err_str = "unknown func bpf_setsockopt"; 321 found = false; 322 323 old_print_fn = libbpf_set_print(libbpf_debug_print); 324 rel_skel = bpf_dctcp_release__open_and_load(); 325 libbpf_set_print(old_print_fn); 326 327 ASSERT_ERR_PTR(rel_skel, "rel_skel"); 328 ASSERT_TRUE(found, "expected_err_msg"); 329 330 bpf_dctcp_release__destroy(rel_skel); 331 } 332 333 static void test_write_sk_pacing(void) 334 { 335 struct tcp_ca_write_sk_pacing *skel; 336 struct bpf_link *link; 337 338 skel = tcp_ca_write_sk_pacing__open_and_load(); 339 if (!ASSERT_OK_PTR(skel, "open_and_load")) 340 return; 341 342 link = bpf_map__attach_struct_ops(skel->maps.write_sk_pacing); 343 ASSERT_OK_PTR(link, "attach_struct_ops"); 344 345 bpf_link__destroy(link); 346 tcp_ca_write_sk_pacing__destroy(skel); 347 } 348 349 static void test_incompl_cong_ops(void) 350 { 351 struct tcp_ca_incompl_cong_ops *skel; 352 struct bpf_link *link; 353 354 skel = tcp_ca_incompl_cong_ops__open_and_load(); 355 if (!ASSERT_OK_PTR(skel, "open_and_load")) 356 return; 357 358 /* That cong_avoid() and cong_control() are missing is only reported at 359 * this point: 360 */ 361 link = bpf_map__attach_struct_ops(skel->maps.incompl_cong_ops); 362 ASSERT_ERR_PTR(link, "attach_struct_ops"); 363 364 bpf_link__destroy(link); 365 tcp_ca_incompl_cong_ops__destroy(skel); 366 } 367 368 static void test_unsupp_cong_op(void) 369 { 370 libbpf_print_fn_t old_print_fn; 371 struct tcp_ca_unsupp_cong_op *skel; 372 373 err_str = "attach to unsupported member get_info"; 374 found = false; 375 old_print_fn = libbpf_set_print(libbpf_debug_print); 376 377 skel = tcp_ca_unsupp_cong_op__open_and_load(); 378 ASSERT_NULL(skel, "open_and_load"); 379 ASSERT_EQ(found, true, "expected_err_msg"); 380 381 tcp_ca_unsupp_cong_op__destroy(skel); 382 libbpf_set_print(old_print_fn); 383 } 384 385 static void test_update_ca(void) 386 { 387 struct tcp_ca_update *skel; 388 struct bpf_link *link; 389 int saved_ca1_cnt; 390 int err; 391 392 skel = tcp_ca_update__open_and_load(); 393 if (!ASSERT_OK_PTR(skel, "open")) 394 return; 395 396 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 397 ASSERT_OK_PTR(link, "attach_struct_ops"); 398 399 do_test("tcp_ca_update", NULL); 400 saved_ca1_cnt = skel->bss->ca1_cnt; 401 ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt"); 402 403 err = bpf_link__update_map(link, skel->maps.ca_update_2); 404 ASSERT_OK(err, "update_map"); 405 406 do_test("tcp_ca_update", NULL); 407 ASSERT_EQ(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt"); 408 ASSERT_GT(skel->bss->ca2_cnt, 0, "ca2_ca2_cnt"); 409 410 bpf_link__destroy(link); 411 tcp_ca_update__destroy(skel); 412 } 413 414 static void test_update_wrong(void) 415 { 416 struct tcp_ca_update *skel; 417 struct bpf_link *link; 418 int saved_ca1_cnt; 419 int err; 420 421 skel = tcp_ca_update__open_and_load(); 422 if (!ASSERT_OK_PTR(skel, "open")) 423 return; 424 425 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 426 ASSERT_OK_PTR(link, "attach_struct_ops"); 427 428 do_test("tcp_ca_update", NULL); 429 saved_ca1_cnt = skel->bss->ca1_cnt; 430 ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt"); 431 432 err = bpf_link__update_map(link, skel->maps.ca_wrong); 433 ASSERT_ERR(err, "update_map"); 434 435 do_test("tcp_ca_update", NULL); 436 ASSERT_GT(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt"); 437 438 bpf_link__destroy(link); 439 tcp_ca_update__destroy(skel); 440 } 441 442 static void test_mixed_links(void) 443 { 444 struct tcp_ca_update *skel; 445 struct bpf_link *link, *link_nl; 446 int err; 447 448 skel = tcp_ca_update__open_and_load(); 449 if (!ASSERT_OK_PTR(skel, "open")) 450 return; 451 452 link_nl = bpf_map__attach_struct_ops(skel->maps.ca_no_link); 453 ASSERT_OK_PTR(link_nl, "attach_struct_ops_nl"); 454 455 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 456 ASSERT_OK_PTR(link, "attach_struct_ops"); 457 458 do_test("tcp_ca_update", NULL); 459 ASSERT_GT(skel->bss->ca1_cnt, 0, "ca1_ca1_cnt"); 460 461 err = bpf_link__update_map(link, skel->maps.ca_no_link); 462 ASSERT_ERR(err, "update_map"); 463 464 bpf_link__destroy(link); 465 bpf_link__destroy(link_nl); 466 tcp_ca_update__destroy(skel); 467 } 468 469 static void test_multi_links(void) 470 { 471 struct tcp_ca_update *skel; 472 struct bpf_link *link; 473 474 skel = tcp_ca_update__open_and_load(); 475 if (!ASSERT_OK_PTR(skel, "open")) 476 return; 477 478 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 479 ASSERT_OK_PTR(link, "attach_struct_ops_1st"); 480 bpf_link__destroy(link); 481 482 /* A map should be able to be used to create links multiple 483 * times. 484 */ 485 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 486 ASSERT_OK_PTR(link, "attach_struct_ops_2nd"); 487 bpf_link__destroy(link); 488 489 tcp_ca_update__destroy(skel); 490 } 491 492 static void test_link_replace(void) 493 { 494 DECLARE_LIBBPF_OPTS(bpf_link_update_opts, opts); 495 struct tcp_ca_update *skel; 496 struct bpf_link *link; 497 int err; 498 499 skel = tcp_ca_update__open_and_load(); 500 if (!ASSERT_OK_PTR(skel, "open")) 501 return; 502 503 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1); 504 ASSERT_OK_PTR(link, "attach_struct_ops_1st"); 505 bpf_link__destroy(link); 506 507 link = bpf_map__attach_struct_ops(skel->maps.ca_update_2); 508 ASSERT_OK_PTR(link, "attach_struct_ops_2nd"); 509 510 /* BPF_F_REPLACE with a wrong old map Fd. It should fail! 511 * 512 * With BPF_F_REPLACE, the link should be updated only if the 513 * old map fd given here matches the map backing the link. 514 */ 515 opts.old_map_fd = bpf_map__fd(skel->maps.ca_update_1); 516 opts.flags = BPF_F_REPLACE; 517 err = bpf_link_update(bpf_link__fd(link), 518 bpf_map__fd(skel->maps.ca_update_1), 519 &opts); 520 ASSERT_ERR(err, "bpf_link_update_fail"); 521 522 /* BPF_F_REPLACE with a correct old map Fd. It should success! */ 523 opts.old_map_fd = bpf_map__fd(skel->maps.ca_update_2); 524 err = bpf_link_update(bpf_link__fd(link), 525 bpf_map__fd(skel->maps.ca_update_1), 526 &opts); 527 ASSERT_OK(err, "bpf_link_update_success"); 528 529 bpf_link__destroy(link); 530 531 tcp_ca_update__destroy(skel); 532 } 533 534 void test_bpf_tcp_ca(void) 535 { 536 if (test__start_subtest("dctcp")) 537 test_dctcp(); 538 if (test__start_subtest("cubic")) 539 test_cubic(); 540 if (test__start_subtest("invalid_license")) 541 test_invalid_license(); 542 if (test__start_subtest("dctcp_fallback")) 543 test_dctcp_fallback(); 544 if (test__start_subtest("rel_setsockopt")) 545 test_rel_setsockopt(); 546 if (test__start_subtest("write_sk_pacing")) 547 test_write_sk_pacing(); 548 if (test__start_subtest("incompl_cong_ops")) 549 test_incompl_cong_ops(); 550 if (test__start_subtest("unsupp_cong_op")) 551 test_unsupp_cong_op(); 552 if (test__start_subtest("update_ca")) 553 test_update_ca(); 554 if (test__start_subtest("update_wrong")) 555 test_update_wrong(); 556 if (test__start_subtest("mixed_links")) 557 test_mixed_links(); 558 if (test__start_subtest("multi_links")) 559 test_multi_links(); 560 if (test__start_subtest("link_replace")) 561 test_link_replace(); 562 } 563