1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
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"
19 static const unsigned int total_bytes = 10 * 1024 * 1024;
20 static int expected_stg = 0xeB9F;
27 static int settcpca(int fd, const char *tcp_ca)
31 err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
32 if (!ASSERT_NEQ(err, -1, "setsockopt"))
38 static bool start_test(char *addr_str,
39 const struct network_helper_opts *srv_opts,
40 const struct network_helper_opts *cli_opts,
41 int *srv_fd, int *cli_fd)
43 *srv_fd = start_server_str(AF_INET6, SOCK_STREAM, addr_str, 0, srv_opts);
44 if (!ASSERT_NEQ(*srv_fd, -1, "start_server_str"))
47 /* connect to server */
48 *cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
49 if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
66 static void do_test(const struct network_helper_opts *opts)
68 int lfd = -1, fd = -1;
70 if (!start_test(NULL, opts, opts, &lfd, &fd))
73 ASSERT_OK(send_recv_data(lfd, fd, total_bytes), "send_recv_data");
82 static int cc_cb(int fd, void *opts)
84 struct cb_opts *cb_opts = (struct cb_opts *)opts;
86 return settcpca(fd, cb_opts->cc);
89 static void test_cubic(void)
91 struct cb_opts cb_opts = {
94 struct network_helper_opts opts = {
95 .post_socket_cb = cc_cb,
98 struct bpf_cubic *cubic_skel;
99 struct bpf_link *link;
101 cubic_skel = bpf_cubic__open_and_load();
102 if (!ASSERT_OK_PTR(cubic_skel, "bpf_cubic__open_and_load"))
105 link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
106 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
107 bpf_cubic__destroy(cubic_skel);
113 ASSERT_EQ(cubic_skel->bss->bpf_cubic_acked_called, 1, "pkts_acked called");
115 bpf_link__destroy(link);
116 bpf_cubic__destroy(cubic_skel);
119 static int stg_post_socket_cb(int fd, void *opts)
121 struct cb_opts *cb_opts = (struct cb_opts *)opts;
124 err = settcpca(fd, cb_opts->cc);
128 err = bpf_map_update_elem(cb_opts->map_fd, &fd,
129 &expected_stg, BPF_NOEXIST);
130 if (!ASSERT_OK(err, "bpf_map_update_elem(sk_stg_map)"))
136 static void test_dctcp(void)
138 struct cb_opts cb_opts = {
141 struct network_helper_opts opts = {
142 .post_socket_cb = cc_cb,
145 struct network_helper_opts cli_opts = {
146 .post_socket_cb = stg_post_socket_cb,
149 int lfd = -1, fd = -1, tmp_stg, err;
150 struct bpf_dctcp *dctcp_skel;
151 struct bpf_link *link;
153 dctcp_skel = bpf_dctcp__open_and_load();
154 if (!ASSERT_OK_PTR(dctcp_skel, "bpf_dctcp__open_and_load"))
157 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
158 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
159 bpf_dctcp__destroy(dctcp_skel);
163 cb_opts.map_fd = bpf_map__fd(dctcp_skel->maps.sk_stg_map);
164 if (!start_test(NULL, &opts, &cli_opts, &lfd, &fd))
167 err = bpf_map_lookup_elem(cb_opts.map_fd, &fd, &tmp_stg);
168 if (!ASSERT_ERR(err, "bpf_map_lookup_elem(sk_stg_map)") ||
169 !ASSERT_EQ(errno, ENOENT, "bpf_map_lookup_elem(sk_stg_map)"))
172 ASSERT_OK(send_recv_data(lfd, fd, total_bytes), "send_recv_data");
173 ASSERT_EQ(dctcp_skel->bss->stg_result, expected_stg, "stg_result");
176 bpf_link__destroy(link);
177 bpf_dctcp__destroy(dctcp_skel);
184 static void test_dctcp_autoattach_map(void)
186 struct cb_opts cb_opts = {
189 struct network_helper_opts opts = {
190 .post_socket_cb = cc_cb,
193 struct bpf_dctcp *dctcp_skel;
194 struct bpf_link *link;
196 dctcp_skel = bpf_dctcp__open_and_load();
197 if (!ASSERT_OK_PTR(dctcp_skel, "bpf_dctcp__open_and_load"))
200 bpf_map__set_autoattach(dctcp_skel->maps.dctcp, true);
201 bpf_map__set_autoattach(dctcp_skel->maps.dctcp_nouse, false);
203 if (!ASSERT_OK(bpf_dctcp__attach(dctcp_skel), "bpf_dctcp__attach"))
206 /* struct_ops is auto-attached */
207 link = dctcp_skel->links.dctcp;
208 if (!ASSERT_OK_PTR(link, "link"))
214 bpf_dctcp__destroy(dctcp_skel);
217 static char *err_str;
220 static int libbpf_debug_print(enum libbpf_print_level level,
221 const char *format, va_list args)
223 const char *prog_name, *log_buf;
225 if (level != LIBBPF_WARN ||
226 !strstr(format, "-- BEGIN PROG LOAD LOG --")) {
227 vprintf(format, args);
231 prog_name = va_arg(args, char *);
232 log_buf = va_arg(args, char *);
235 if (err_str && strstr(log_buf, err_str) != NULL)
238 printf(format, prog_name, log_buf);
242 static void test_invalid_license(void)
244 libbpf_print_fn_t old_print_fn;
245 struct bpf_tcp_nogpl *skel;
247 err_str = "struct ops programs must have a GPL compatible license";
249 old_print_fn = libbpf_set_print(libbpf_debug_print);
251 skel = bpf_tcp_nogpl__open_and_load();
252 ASSERT_NULL(skel, "bpf_tcp_nogpl");
253 ASSERT_EQ(found, true, "expected_err_msg");
255 bpf_tcp_nogpl__destroy(skel);
256 libbpf_set_print(old_print_fn);
259 static void test_dctcp_fallback(void)
261 int err, lfd = -1, cli_fd = -1, srv_fd = -1;
262 struct bpf_dctcp *dctcp_skel;
263 struct bpf_link *link = NULL;
264 struct cb_opts dctcp = {
267 struct network_helper_opts srv_opts = {
268 .post_socket_cb = cc_cb,
271 struct cb_opts cubic = {
274 struct network_helper_opts cli_opts = {
275 .post_socket_cb = cc_cb,
279 socklen_t cc_len = sizeof(srv_cc);
281 dctcp_skel = bpf_dctcp__open();
282 if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
284 strcpy(dctcp_skel->rodata->fallback_cc, "cubic");
285 if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load"))
288 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
289 if (!ASSERT_OK_PTR(link, "dctcp link"))
292 if (!start_test("::1", &srv_opts, &cli_opts, &lfd, &cli_fd))
295 srv_fd = accept(lfd, NULL, 0);
296 if (!ASSERT_GE(srv_fd, 0, "srv_fd"))
298 ASSERT_STREQ(dctcp_skel->bss->cc_res, "cubic", "cc_res");
299 ASSERT_EQ(dctcp_skel->bss->tcp_cdg_res, -ENOTSUPP, "tcp_cdg_res");
300 /* All setsockopt(TCP_CONGESTION) in the recurred
301 * bpf_dctcp->init() should fail with -EBUSY.
303 ASSERT_EQ(dctcp_skel->bss->ebusy_cnt, 3, "ebusy_cnt");
305 err = getsockopt(srv_fd, SOL_TCP, TCP_CONGESTION, srv_cc, &cc_len);
306 if (!ASSERT_OK(err, "getsockopt(srv_fd, TCP_CONGESTION)"))
308 ASSERT_STREQ(srv_cc, "cubic", "srv_fd cc");
311 bpf_link__destroy(link);
312 bpf_dctcp__destroy(dctcp_skel);
321 static void test_rel_setsockopt(void)
323 struct bpf_dctcp_release *rel_skel;
324 libbpf_print_fn_t old_print_fn;
326 err_str = "program of this type cannot use helper bpf_setsockopt";
329 old_print_fn = libbpf_set_print(libbpf_debug_print);
330 rel_skel = bpf_dctcp_release__open_and_load();
331 libbpf_set_print(old_print_fn);
333 ASSERT_ERR_PTR(rel_skel, "rel_skel");
334 ASSERT_TRUE(found, "expected_err_msg");
336 bpf_dctcp_release__destroy(rel_skel);
339 static void test_write_sk_pacing(void)
341 struct tcp_ca_write_sk_pacing *skel;
342 struct bpf_link *link;
344 skel = tcp_ca_write_sk_pacing__open_and_load();
345 if (!ASSERT_OK_PTR(skel, "open_and_load"))
348 link = bpf_map__attach_struct_ops(skel->maps.write_sk_pacing);
349 ASSERT_OK_PTR(link, "attach_struct_ops");
351 bpf_link__destroy(link);
352 tcp_ca_write_sk_pacing__destroy(skel);
355 static void test_incompl_cong_ops(void)
357 struct tcp_ca_incompl_cong_ops *skel;
358 struct bpf_link *link;
360 skel = tcp_ca_incompl_cong_ops__open_and_load();
361 if (!ASSERT_OK_PTR(skel, "open_and_load"))
364 /* That cong_avoid() and cong_control() are missing is only reported at
367 link = bpf_map__attach_struct_ops(skel->maps.incompl_cong_ops);
368 ASSERT_ERR_PTR(link, "attach_struct_ops");
370 bpf_link__destroy(link);
371 tcp_ca_incompl_cong_ops__destroy(skel);
374 static void test_unsupp_cong_op(void)
376 libbpf_print_fn_t old_print_fn;
377 struct tcp_ca_unsupp_cong_op *skel;
379 err_str = "attach to unsupported member get_info";
381 old_print_fn = libbpf_set_print(libbpf_debug_print);
383 skel = tcp_ca_unsupp_cong_op__open_and_load();
384 ASSERT_NULL(skel, "open_and_load");
385 ASSERT_EQ(found, true, "expected_err_msg");
387 tcp_ca_unsupp_cong_op__destroy(skel);
388 libbpf_set_print(old_print_fn);
391 static void test_update_ca(void)
393 struct cb_opts cb_opts = {
394 .cc = "tcp_ca_update",
396 struct network_helper_opts opts = {
397 .post_socket_cb = cc_cb,
400 struct tcp_ca_update *skel;
401 struct bpf_link *link;
405 skel = tcp_ca_update__open_and_load();
406 if (!ASSERT_OK_PTR(skel, "open"))
409 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
410 if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
414 saved_ca1_cnt = skel->bss->ca1_cnt;
415 ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");
417 err = bpf_link__update_map(link, skel->maps.ca_update_2);
418 ASSERT_OK(err, "update_map");
421 ASSERT_EQ(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");
422 ASSERT_GT(skel->bss->ca2_cnt, 0, "ca2_ca2_cnt");
424 bpf_link__destroy(link);
426 tcp_ca_update__destroy(skel);
429 static void test_update_wrong(void)
431 struct cb_opts cb_opts = {
432 .cc = "tcp_ca_update",
434 struct network_helper_opts opts = {
435 .post_socket_cb = cc_cb,
438 struct tcp_ca_update *skel;
439 struct bpf_link *link;
443 skel = tcp_ca_update__open_and_load();
444 if (!ASSERT_OK_PTR(skel, "open"))
447 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
448 if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
452 saved_ca1_cnt = skel->bss->ca1_cnt;
453 ASSERT_GT(saved_ca1_cnt, 0, "ca1_ca1_cnt");
455 err = bpf_link__update_map(link, skel->maps.ca_wrong);
456 ASSERT_ERR(err, "update_map");
459 ASSERT_GT(skel->bss->ca1_cnt, saved_ca1_cnt, "ca2_ca1_cnt");
461 bpf_link__destroy(link);
463 tcp_ca_update__destroy(skel);
466 static void test_mixed_links(void)
468 struct cb_opts cb_opts = {
469 .cc = "tcp_ca_update",
471 struct network_helper_opts opts = {
472 .post_socket_cb = cc_cb,
475 struct tcp_ca_update *skel;
476 struct bpf_link *link, *link_nl;
479 skel = tcp_ca_update__open_and_load();
480 if (!ASSERT_OK_PTR(skel, "open"))
483 link_nl = bpf_map__attach_struct_ops(skel->maps.ca_no_link);
484 if (!ASSERT_OK_PTR(link_nl, "attach_struct_ops_nl"))
487 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
488 ASSERT_OK_PTR(link, "attach_struct_ops");
491 ASSERT_GT(skel->bss->ca1_cnt, 0, "ca1_ca1_cnt");
493 err = bpf_link__update_map(link, skel->maps.ca_no_link);
494 ASSERT_ERR(err, "update_map");
496 bpf_link__destroy(link);
497 bpf_link__destroy(link_nl);
499 tcp_ca_update__destroy(skel);
502 static void test_multi_links(void)
504 struct tcp_ca_update *skel;
505 struct bpf_link *link;
507 skel = tcp_ca_update__open_and_load();
508 if (!ASSERT_OK_PTR(skel, "open"))
511 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
512 ASSERT_OK_PTR(link, "attach_struct_ops_1st");
513 bpf_link__destroy(link);
515 /* A map should be able to be used to create links multiple
518 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
519 ASSERT_OK_PTR(link, "attach_struct_ops_2nd");
520 bpf_link__destroy(link);
522 tcp_ca_update__destroy(skel);
525 static void test_link_replace(void)
527 DECLARE_LIBBPF_OPTS(bpf_link_update_opts, opts);
528 struct tcp_ca_update *skel;
529 struct bpf_link *link;
532 skel = tcp_ca_update__open_and_load();
533 if (!ASSERT_OK_PTR(skel, "open"))
536 link = bpf_map__attach_struct_ops(skel->maps.ca_update_1);
537 ASSERT_OK_PTR(link, "attach_struct_ops_1st");
538 bpf_link__destroy(link);
540 link = bpf_map__attach_struct_ops(skel->maps.ca_update_2);
541 if (!ASSERT_OK_PTR(link, "attach_struct_ops_2nd"))
544 /* BPF_F_REPLACE with a wrong old map Fd. It should fail!
546 * With BPF_F_REPLACE, the link should be updated only if the
547 * old map fd given here matches the map backing the link.
549 opts.old_map_fd = bpf_map__fd(skel->maps.ca_update_1);
550 opts.flags = BPF_F_REPLACE;
551 err = bpf_link_update(bpf_link__fd(link),
552 bpf_map__fd(skel->maps.ca_update_1),
554 ASSERT_ERR(err, "bpf_link_update_fail");
556 /* BPF_F_REPLACE with a correct old map Fd. It should success! */
557 opts.old_map_fd = bpf_map__fd(skel->maps.ca_update_2);
558 err = bpf_link_update(bpf_link__fd(link),
559 bpf_map__fd(skel->maps.ca_update_1),
561 ASSERT_OK(err, "bpf_link_update_success");
563 bpf_link__destroy(link);
566 tcp_ca_update__destroy(skel);
569 static void test_tcp_ca_kfunc(void)
571 struct tcp_ca_kfunc *skel;
573 skel = tcp_ca_kfunc__open_and_load();
574 ASSERT_OK_PTR(skel, "tcp_ca_kfunc__open_and_load");
575 tcp_ca_kfunc__destroy(skel);
578 static void test_cc_cubic(void)
580 struct cb_opts cb_opts = {
581 .cc = "bpf_cc_cubic",
583 struct network_helper_opts opts = {
584 .post_socket_cb = cc_cb,
587 struct bpf_cc_cubic *cc_cubic_skel;
588 struct bpf_link *link;
590 cc_cubic_skel = bpf_cc_cubic__open_and_load();
591 if (!ASSERT_OK_PTR(cc_cubic_skel, "bpf_cc_cubic__open_and_load"))
594 link = bpf_map__attach_struct_ops(cc_cubic_skel->maps.cc_cubic);
595 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
596 bpf_cc_cubic__destroy(cc_cubic_skel);
602 bpf_link__destroy(link);
603 bpf_cc_cubic__destroy(cc_cubic_skel);
606 void test_bpf_tcp_ca(void)
608 if (test__start_subtest("dctcp"))
610 if (test__start_subtest("cubic"))
612 if (test__start_subtest("invalid_license"))
613 test_invalid_license();
614 if (test__start_subtest("dctcp_fallback"))
615 test_dctcp_fallback();
616 if (test__start_subtest("rel_setsockopt"))
617 test_rel_setsockopt();
618 if (test__start_subtest("write_sk_pacing"))
619 test_write_sk_pacing();
620 if (test__start_subtest("incompl_cong_ops"))
621 test_incompl_cong_ops();
622 if (test__start_subtest("unsupp_cong_op"))
623 test_unsupp_cong_op();
624 if (test__start_subtest("update_ca"))
626 if (test__start_subtest("update_wrong"))
628 if (test__start_subtest("mixed_links"))
630 if (test__start_subtest("multi_links"))
632 if (test__start_subtest("link_replace"))
634 if (test__start_subtest("tcp_ca_kfunc"))
636 if (test__start_subtest("cc_cubic"))
638 if (test__start_subtest("dctcp_autoattach_map"))
639 test_dctcp_autoattach_map();