0001
0002
0003
0004 #include <linux/err.h>
0005 #include <netinet/tcp.h>
0006 #include <test_progs.h>
0007 #include "network_helpers.h"
0008 #include "bpf_dctcp.skel.h"
0009 #include "bpf_cubic.skel.h"
0010 #include "bpf_tcp_nogpl.skel.h"
0011 #include "bpf_dctcp_release.skel.h"
0012 #include "tcp_ca_write_sk_pacing.skel.h"
0013 #include "tcp_ca_incompl_cong_ops.skel.h"
0014 #include "tcp_ca_unsupp_cong_op.skel.h"
0015
0016 #ifndef ENOTSUPP
0017 #define ENOTSUPP 524
0018 #endif
0019
0020 static const unsigned int total_bytes = 10 * 1024 * 1024;
0021 static int expected_stg = 0xeB9F;
0022 static int stop, duration;
0023
0024 static int settcpca(int fd, const char *tcp_ca)
0025 {
0026 int err;
0027
0028 err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
0029 if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n",
0030 errno))
0031 return -1;
0032
0033 return 0;
0034 }
0035
0036 static void *server(void *arg)
0037 {
0038 int lfd = (int)(long)arg, err = 0, fd;
0039 ssize_t nr_sent = 0, bytes = 0;
0040 char batch[1500];
0041
0042 fd = accept(lfd, NULL, NULL);
0043 while (fd == -1) {
0044 if (errno == EINTR)
0045 continue;
0046 err = -errno;
0047 goto done;
0048 }
0049
0050 if (settimeo(fd, 0)) {
0051 err = -errno;
0052 goto done;
0053 }
0054
0055 while (bytes < total_bytes && !READ_ONCE(stop)) {
0056 nr_sent = send(fd, &batch,
0057 MIN(total_bytes - bytes, sizeof(batch)), 0);
0058 if (nr_sent == -1 && errno == EINTR)
0059 continue;
0060 if (nr_sent == -1) {
0061 err = -errno;
0062 break;
0063 }
0064 bytes += nr_sent;
0065 }
0066
0067 CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
0068 bytes, total_bytes, nr_sent, errno);
0069
0070 done:
0071 if (fd >= 0)
0072 close(fd);
0073 if (err) {
0074 WRITE_ONCE(stop, 1);
0075 return ERR_PTR(err);
0076 }
0077 return NULL;
0078 }
0079
0080 static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
0081 {
0082 struct sockaddr_in6 sa6 = {};
0083 ssize_t nr_recv = 0, bytes = 0;
0084 int lfd = -1, fd = -1;
0085 pthread_t srv_thread;
0086 socklen_t addrlen = sizeof(sa6);
0087 void *thread_ret;
0088 char batch[1500];
0089 int err;
0090
0091 WRITE_ONCE(stop, 0);
0092
0093 lfd = socket(AF_INET6, SOCK_STREAM, 0);
0094 if (CHECK(lfd == -1, "socket", "errno:%d\n", errno))
0095 return;
0096 fd = socket(AF_INET6, SOCK_STREAM, 0);
0097 if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) {
0098 close(lfd);
0099 return;
0100 }
0101
0102 if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
0103 settimeo(lfd, 0) || settimeo(fd, 0))
0104 goto done;
0105
0106
0107 sa6.sin6_family = AF_INET6;
0108 sa6.sin6_addr = in6addr_loopback;
0109 err = bind(lfd, (struct sockaddr *)&sa6, addrlen);
0110 if (CHECK(err == -1, "bind", "errno:%d\n", errno))
0111 goto done;
0112 err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen);
0113 if (CHECK(err == -1, "getsockname", "errno:%d\n", errno))
0114 goto done;
0115 err = listen(lfd, 1);
0116 if (CHECK(err == -1, "listen", "errno:%d\n", errno))
0117 goto done;
0118
0119 if (sk_stg_map) {
0120 err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
0121 &expected_stg, BPF_NOEXIST);
0122 if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
0123 "err:%d errno:%d\n", err, errno))
0124 goto done;
0125 }
0126
0127
0128 err = connect(fd, (struct sockaddr *)&sa6, addrlen);
0129 if (CHECK(err == -1, "connect", "errno:%d\n", errno))
0130 goto done;
0131
0132 if (sk_stg_map) {
0133 int tmp_stg;
0134
0135 err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
0136 &tmp_stg);
0137 if (CHECK(!err || errno != ENOENT,
0138 "bpf_map_lookup_elem(sk_stg_map)",
0139 "err:%d errno:%d\n", err, errno))
0140 goto done;
0141 }
0142
0143 err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
0144 if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno))
0145 goto done;
0146
0147
0148 while (bytes < total_bytes && !READ_ONCE(stop)) {
0149 nr_recv = recv(fd, &batch,
0150 MIN(total_bytes - bytes, sizeof(batch)), 0);
0151 if (nr_recv == -1 && errno == EINTR)
0152 continue;
0153 if (nr_recv == -1)
0154 break;
0155 bytes += nr_recv;
0156 }
0157
0158 CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
0159 bytes, total_bytes, nr_recv, errno);
0160
0161 WRITE_ONCE(stop, 1);
0162 pthread_join(srv_thread, &thread_ret);
0163 CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
0164 PTR_ERR(thread_ret));
0165 done:
0166 close(lfd);
0167 close(fd);
0168 }
0169
0170 static void test_cubic(void)
0171 {
0172 struct bpf_cubic *cubic_skel;
0173 struct bpf_link *link;
0174
0175 cubic_skel = bpf_cubic__open_and_load();
0176 if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
0177 return;
0178
0179 link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
0180 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
0181 bpf_cubic__destroy(cubic_skel);
0182 return;
0183 }
0184
0185 do_test("bpf_cubic", NULL);
0186
0187 bpf_link__destroy(link);
0188 bpf_cubic__destroy(cubic_skel);
0189 }
0190
0191 static void test_dctcp(void)
0192 {
0193 struct bpf_dctcp *dctcp_skel;
0194 struct bpf_link *link;
0195
0196 dctcp_skel = bpf_dctcp__open_and_load();
0197 if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n"))
0198 return;
0199
0200 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
0201 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
0202 bpf_dctcp__destroy(dctcp_skel);
0203 return;
0204 }
0205
0206 do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
0207 CHECK(dctcp_skel->bss->stg_result != expected_stg,
0208 "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
0209 dctcp_skel->bss->stg_result, expected_stg);
0210
0211 bpf_link__destroy(link);
0212 bpf_dctcp__destroy(dctcp_skel);
0213 }
0214
0215 static char *err_str;
0216 static bool found;
0217
0218 static int libbpf_debug_print(enum libbpf_print_level level,
0219 const char *format, va_list args)
0220 {
0221 const char *prog_name, *log_buf;
0222
0223 if (level != LIBBPF_WARN ||
0224 !strstr(format, "-- BEGIN PROG LOAD LOG --")) {
0225 vprintf(format, args);
0226 return 0;
0227 }
0228
0229 prog_name = va_arg(args, char *);
0230 log_buf = va_arg(args, char *);
0231 if (!log_buf)
0232 goto out;
0233 if (err_str && strstr(log_buf, err_str) != NULL)
0234 found = true;
0235 out:
0236 printf(format, prog_name, log_buf);
0237 return 0;
0238 }
0239
0240 static void test_invalid_license(void)
0241 {
0242 libbpf_print_fn_t old_print_fn;
0243 struct bpf_tcp_nogpl *skel;
0244
0245 err_str = "struct ops programs must have a GPL compatible license";
0246 found = false;
0247 old_print_fn = libbpf_set_print(libbpf_debug_print);
0248
0249 skel = bpf_tcp_nogpl__open_and_load();
0250 ASSERT_NULL(skel, "bpf_tcp_nogpl");
0251 ASSERT_EQ(found, true, "expected_err_msg");
0252
0253 bpf_tcp_nogpl__destroy(skel);
0254 libbpf_set_print(old_print_fn);
0255 }
0256
0257 static void test_dctcp_fallback(void)
0258 {
0259 int err, lfd = -1, cli_fd = -1, srv_fd = -1;
0260 struct network_helper_opts opts = {
0261 .cc = "cubic",
0262 };
0263 struct bpf_dctcp *dctcp_skel;
0264 struct bpf_link *link = NULL;
0265 char srv_cc[16];
0266 socklen_t cc_len = sizeof(srv_cc);
0267
0268 dctcp_skel = bpf_dctcp__open();
0269 if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
0270 return;
0271 strcpy(dctcp_skel->rodata->fallback, "cubic");
0272 if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load"))
0273 goto done;
0274
0275 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
0276 if (!ASSERT_OK_PTR(link, "dctcp link"))
0277 goto done;
0278
0279 lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
0280 if (!ASSERT_GE(lfd, 0, "lfd") ||
0281 !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
0282 goto done;
0283
0284 cli_fd = connect_to_fd_opts(lfd, &opts);
0285 if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
0286 goto done;
0287
0288 srv_fd = accept(lfd, NULL, 0);
0289 if (!ASSERT_GE(srv_fd, 0, "srv_fd"))
0290 goto done;
0291 ASSERT_STREQ(dctcp_skel->bss->cc_res, "cubic", "cc_res");
0292 ASSERT_EQ(dctcp_skel->bss->tcp_cdg_res, -ENOTSUPP, "tcp_cdg_res");
0293
0294 err = getsockopt(srv_fd, SOL_TCP, TCP_CONGESTION, srv_cc, &cc_len);
0295 if (!ASSERT_OK(err, "getsockopt(srv_fd, TCP_CONGESTION)"))
0296 goto done;
0297 ASSERT_STREQ(srv_cc, "cubic", "srv_fd cc");
0298
0299 done:
0300 bpf_link__destroy(link);
0301 bpf_dctcp__destroy(dctcp_skel);
0302 if (lfd != -1)
0303 close(lfd);
0304 if (srv_fd != -1)
0305 close(srv_fd);
0306 if (cli_fd != -1)
0307 close(cli_fd);
0308 }
0309
0310 static void test_rel_setsockopt(void)
0311 {
0312 struct bpf_dctcp_release *rel_skel;
0313 libbpf_print_fn_t old_print_fn;
0314
0315 err_str = "unknown func bpf_setsockopt";
0316 found = false;
0317
0318 old_print_fn = libbpf_set_print(libbpf_debug_print);
0319 rel_skel = bpf_dctcp_release__open_and_load();
0320 libbpf_set_print(old_print_fn);
0321
0322 ASSERT_ERR_PTR(rel_skel, "rel_skel");
0323 ASSERT_TRUE(found, "expected_err_msg");
0324
0325 bpf_dctcp_release__destroy(rel_skel);
0326 }
0327
0328 static void test_write_sk_pacing(void)
0329 {
0330 struct tcp_ca_write_sk_pacing *skel;
0331 struct bpf_link *link;
0332
0333 skel = tcp_ca_write_sk_pacing__open_and_load();
0334 if (!ASSERT_OK_PTR(skel, "open_and_load"))
0335 return;
0336
0337 link = bpf_map__attach_struct_ops(skel->maps.write_sk_pacing);
0338 ASSERT_OK_PTR(link, "attach_struct_ops");
0339
0340 bpf_link__destroy(link);
0341 tcp_ca_write_sk_pacing__destroy(skel);
0342 }
0343
0344 static void test_incompl_cong_ops(void)
0345 {
0346 struct tcp_ca_incompl_cong_ops *skel;
0347 struct bpf_link *link;
0348
0349 skel = tcp_ca_incompl_cong_ops__open_and_load();
0350 if (!ASSERT_OK_PTR(skel, "open_and_load"))
0351 return;
0352
0353
0354
0355
0356 link = bpf_map__attach_struct_ops(skel->maps.incompl_cong_ops);
0357 ASSERT_ERR_PTR(link, "attach_struct_ops");
0358
0359 bpf_link__destroy(link);
0360 tcp_ca_incompl_cong_ops__destroy(skel);
0361 }
0362
0363 static void test_unsupp_cong_op(void)
0364 {
0365 libbpf_print_fn_t old_print_fn;
0366 struct tcp_ca_unsupp_cong_op *skel;
0367
0368 err_str = "attach to unsupported member get_info";
0369 found = false;
0370 old_print_fn = libbpf_set_print(libbpf_debug_print);
0371
0372 skel = tcp_ca_unsupp_cong_op__open_and_load();
0373 ASSERT_NULL(skel, "open_and_load");
0374 ASSERT_EQ(found, true, "expected_err_msg");
0375
0376 tcp_ca_unsupp_cong_op__destroy(skel);
0377 libbpf_set_print(old_print_fn);
0378 }
0379
0380 void test_bpf_tcp_ca(void)
0381 {
0382 if (test__start_subtest("dctcp"))
0383 test_dctcp();
0384 if (test__start_subtest("cubic"))
0385 test_cubic();
0386 if (test__start_subtest("invalid_license"))
0387 test_invalid_license();
0388 if (test__start_subtest("dctcp_fallback"))
0389 test_dctcp_fallback();
0390 if (test__start_subtest("rel_setsockopt"))
0391 test_rel_setsockopt();
0392 if (test__start_subtest("write_sk_pacing"))
0393 test_write_sk_pacing();
0394 if (test__start_subtest("incompl_cong_ops"))
0395 test_incompl_cong_ops();
0396 if (test__start_subtest("unsupp_cong_op"))
0397 test_unsupp_cong_op();
0398 }