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