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 "bpf_dctcp_release.skel.h"
13 #define min(a, b) ((a) < (b) ? (a) : (b))
19 static const unsigned int total_bytes = 10 * 1024 * 1024;
20 static int expected_stg = 0xeB9F;
21 static int stop, duration;
23 static int settcpca(int fd, const char *tcp_ca)
27 err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
28 if (CHECK(err == -1, "setsockopt(fd, TCP_CONGESTION)", "errno:%d\n",
35 static void *server(void *arg)
37 int lfd = (int)(long)arg, err = 0, fd;
38 ssize_t nr_sent = 0, bytes = 0;
41 fd = accept(lfd, NULL, NULL);
49 if (settimeo(fd, 0)) {
54 while (bytes < total_bytes && !READ_ONCE(stop)) {
55 nr_sent = send(fd, &batch,
56 min(total_bytes - bytes, sizeof(batch)), 0);
57 if (nr_sent == -1 && errno == EINTR)
66 CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
67 bytes, total_bytes, nr_sent, errno);
79 static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
81 struct sockaddr_in6 sa6 = {};
82 ssize_t nr_recv = 0, bytes = 0;
83 int lfd = -1, fd = -1;
85 socklen_t addrlen = sizeof(sa6);
92 lfd = socket(AF_INET6, SOCK_STREAM, 0);
93 if (CHECK(lfd == -1, "socket", "errno:%d\n", errno))
95 fd = socket(AF_INET6, SOCK_STREAM, 0);
96 if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) {
101 if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
102 settimeo(lfd, 0) || settimeo(fd, 0))
105 /* bind, listen and start server thread to accept */
106 sa6.sin6_family = AF_INET6;
107 sa6.sin6_addr = in6addr_loopback;
108 err = bind(lfd, (struct sockaddr *)&sa6, addrlen);
109 if (CHECK(err == -1, "bind", "errno:%d\n", errno))
111 err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen);
112 if (CHECK(err == -1, "getsockname", "errno:%d\n", errno))
114 err = listen(lfd, 1);
115 if (CHECK(err == -1, "listen", "errno:%d\n", errno))
119 err = bpf_map_update_elem(bpf_map__fd(sk_stg_map), &fd,
120 &expected_stg, BPF_NOEXIST);
121 if (CHECK(err, "bpf_map_update_elem(sk_stg_map)",
122 "err:%d errno:%d\n", err, errno))
126 /* connect to server */
127 err = connect(fd, (struct sockaddr *)&sa6, addrlen);
128 if (CHECK(err == -1, "connect", "errno:%d\n", errno))
134 err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
136 if (CHECK(!err || errno != ENOENT,
137 "bpf_map_lookup_elem(sk_stg_map)",
138 "err:%d errno:%d\n", err, errno))
142 err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
143 if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno))
146 /* recv total_bytes */
147 while (bytes < total_bytes && !READ_ONCE(stop)) {
148 nr_recv = recv(fd, &batch,
149 min(total_bytes - bytes, sizeof(batch)), 0);
150 if (nr_recv == -1 && errno == EINTR)
157 CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
158 bytes, total_bytes, nr_recv, errno);
161 pthread_join(srv_thread, &thread_ret);
162 CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
163 PTR_ERR(thread_ret));
169 static void test_cubic(void)
171 struct bpf_cubic *cubic_skel;
172 struct bpf_link *link;
174 cubic_skel = bpf_cubic__open_and_load();
175 if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
178 link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
179 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
180 bpf_cubic__destroy(cubic_skel);
184 do_test("bpf_cubic", NULL);
186 bpf_link__destroy(link);
187 bpf_cubic__destroy(cubic_skel);
190 static void test_dctcp(void)
192 struct bpf_dctcp *dctcp_skel;
193 struct bpf_link *link;
195 dctcp_skel = bpf_dctcp__open_and_load();
196 if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n"))
199 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
200 if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
201 bpf_dctcp__destroy(dctcp_skel);
205 do_test("bpf_dctcp", dctcp_skel->maps.sk_stg_map);
206 CHECK(dctcp_skel->bss->stg_result != expected_stg,
207 "Unexpected stg_result", "stg_result (%x) != expected_stg (%x)\n",
208 dctcp_skel->bss->stg_result, expected_stg);
210 bpf_link__destroy(link);
211 bpf_dctcp__destroy(dctcp_skel);
214 static char *err_str;
217 static int libbpf_debug_print(enum libbpf_print_level level,
218 const char *format, va_list args)
220 const char *prog_name, *log_buf;
222 if (level != LIBBPF_WARN ||
223 !strstr(format, "-- BEGIN PROG LOAD LOG --")) {
224 vprintf(format, args);
228 prog_name = va_arg(args, char *);
229 log_buf = va_arg(args, char *);
232 if (err_str && strstr(log_buf, err_str) != NULL)
235 printf(format, prog_name, log_buf);
239 static void test_invalid_license(void)
241 libbpf_print_fn_t old_print_fn;
242 struct bpf_tcp_nogpl *skel;
244 err_str = "struct ops programs must have a GPL compatible license";
246 old_print_fn = libbpf_set_print(libbpf_debug_print);
248 skel = bpf_tcp_nogpl__open_and_load();
249 ASSERT_NULL(skel, "bpf_tcp_nogpl");
250 ASSERT_EQ(found, true, "expected_err_msg");
252 bpf_tcp_nogpl__destroy(skel);
253 libbpf_set_print(old_print_fn);
256 static void test_dctcp_fallback(void)
258 int err, lfd = -1, cli_fd = -1, srv_fd = -1;
259 struct network_helper_opts opts = {
262 struct bpf_dctcp *dctcp_skel;
263 struct bpf_link *link = NULL;
265 socklen_t cc_len = sizeof(srv_cc);
267 dctcp_skel = bpf_dctcp__open();
268 if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
270 strcpy(dctcp_skel->rodata->fallback, "cubic");
271 if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load"))
274 link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
275 if (!ASSERT_OK_PTR(link, "dctcp link"))
278 lfd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
279 if (!ASSERT_GE(lfd, 0, "lfd") ||
280 !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
283 cli_fd = connect_to_fd_opts(lfd, &opts);
284 if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
287 srv_fd = accept(lfd, NULL, 0);
288 if (!ASSERT_GE(srv_fd, 0, "srv_fd"))
290 ASSERT_STREQ(dctcp_skel->bss->cc_res, "cubic", "cc_res");
291 ASSERT_EQ(dctcp_skel->bss->tcp_cdg_res, -ENOTSUPP, "tcp_cdg_res");
293 err = getsockopt(srv_fd, SOL_TCP, TCP_CONGESTION, srv_cc, &cc_len);
294 if (!ASSERT_OK(err, "getsockopt(srv_fd, TCP_CONGESTION)"))
296 ASSERT_STREQ(srv_cc, "cubic", "srv_fd cc");
299 bpf_link__destroy(link);
300 bpf_dctcp__destroy(dctcp_skel);
309 static void test_rel_setsockopt(void)
311 struct bpf_dctcp_release *rel_skel;
312 libbpf_print_fn_t old_print_fn;
314 err_str = "unknown func bpf_setsockopt";
317 old_print_fn = libbpf_set_print(libbpf_debug_print);
318 rel_skel = bpf_dctcp_release__open_and_load();
319 libbpf_set_print(old_print_fn);
321 ASSERT_ERR_PTR(rel_skel, "rel_skel");
322 ASSERT_TRUE(found, "expected_err_msg");
324 bpf_dctcp_release__destroy(rel_skel);
327 void test_bpf_tcp_ca(void)
329 if (test__start_subtest("dctcp"))
331 if (test__start_subtest("cubic"))
333 if (test__start_subtest("invalid_license"))
334 test_invalid_license();
335 if (test__start_subtest("dctcp_fallback"))
336 test_dctcp_fallback();
337 if (test__start_subtest("rel_setsockopt"))
338 test_rel_setsockopt();