]> Git Repo - J-linux.git/blob - tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c
Merge tag 'ata-5.17-rc1-part2' of git://git.kernel.org/pub/scm/linux/kernel/git/dlemo...
[J-linux.git] / tools / testing / selftests / bpf / prog_tests / bpf_tcp_ca.c
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 "bpf_dctcp_release.skel.h"
12
13 #define min(a, b) ((a) < (b) ? (a) : (b))
14
15 #ifndef ENOTSUPP
16 #define ENOTSUPP 524
17 #endif
18
19 static const unsigned int total_bytes = 10 * 1024 * 1024;
20 static int expected_stg = 0xeB9F;
21 static int stop, duration;
22
23 static int settcpca(int fd, const char *tcp_ca)
24 {
25         int err;
26
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",
29                   errno))
30                 return -1;
31
32         return 0;
33 }
34
35 static void *server(void *arg)
36 {
37         int lfd = (int)(long)arg, err = 0, fd;
38         ssize_t nr_sent = 0, bytes = 0;
39         char batch[1500];
40
41         fd = accept(lfd, NULL, NULL);
42         while (fd == -1) {
43                 if (errno == EINTR)
44                         continue;
45                 err = -errno;
46                 goto done;
47         }
48
49         if (settimeo(fd, 0)) {
50                 err = -errno;
51                 goto done;
52         }
53
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)
58                         continue;
59                 if (nr_sent == -1) {
60                         err = -errno;
61                         break;
62                 }
63                 bytes += nr_sent;
64         }
65
66         CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
67               bytes, total_bytes, nr_sent, errno);
68
69 done:
70         if (fd >= 0)
71                 close(fd);
72         if (err) {
73                 WRITE_ONCE(stop, 1);
74                 return ERR_PTR(err);
75         }
76         return NULL;
77 }
78
79 static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
80 {
81         struct sockaddr_in6 sa6 = {};
82         ssize_t nr_recv = 0, bytes = 0;
83         int lfd = -1, fd = -1;
84         pthread_t srv_thread;
85         socklen_t addrlen = sizeof(sa6);
86         void *thread_ret;
87         char batch[1500];
88         int err;
89
90         WRITE_ONCE(stop, 0);
91
92         lfd = socket(AF_INET6, SOCK_STREAM, 0);
93         if (CHECK(lfd == -1, "socket", "errno:%d\n", errno))
94                 return;
95         fd = socket(AF_INET6, SOCK_STREAM, 0);
96         if (CHECK(fd == -1, "socket", "errno:%d\n", errno)) {
97                 close(lfd);
98                 return;
99         }
100
101         if (settcpca(lfd, tcp_ca) || settcpca(fd, tcp_ca) ||
102             settimeo(lfd, 0) || settimeo(fd, 0))
103                 goto done;
104
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))
110                 goto done;
111         err = getsockname(lfd, (struct sockaddr *)&sa6, &addrlen);
112         if (CHECK(err == -1, "getsockname", "errno:%d\n", errno))
113                 goto done;
114         err = listen(lfd, 1);
115         if (CHECK(err == -1, "listen", "errno:%d\n", errno))
116                 goto done;
117
118         if (sk_stg_map) {
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))
123                         goto done;
124         }
125
126         /* connect to server */
127         err = connect(fd, (struct sockaddr *)&sa6, addrlen);
128         if (CHECK(err == -1, "connect", "errno:%d\n", errno))
129                 goto done;
130
131         if (sk_stg_map) {
132                 int tmp_stg;
133
134                 err = bpf_map_lookup_elem(bpf_map__fd(sk_stg_map), &fd,
135                                           &tmp_stg);
136                 if (CHECK(!err || errno != ENOENT,
137                           "bpf_map_lookup_elem(sk_stg_map)",
138                           "err:%d errno:%d\n", err, errno))
139                         goto done;
140         }
141
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))
144                 goto done;
145
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)
151                         continue;
152                 if (nr_recv == -1)
153                         break;
154                 bytes += nr_recv;
155         }
156
157         CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
158               bytes, total_bytes, nr_recv, errno);
159
160         WRITE_ONCE(stop, 1);
161         pthread_join(srv_thread, &thread_ret);
162         CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
163               PTR_ERR(thread_ret));
164 done:
165         close(lfd);
166         close(fd);
167 }
168
169 static void test_cubic(void)
170 {
171         struct bpf_cubic *cubic_skel;
172         struct bpf_link *link;
173
174         cubic_skel = bpf_cubic__open_and_load();
175         if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
176                 return;
177
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);
181                 return;
182         }
183
184         do_test("bpf_cubic", NULL);
185
186         bpf_link__destroy(link);
187         bpf_cubic__destroy(cubic_skel);
188 }
189
190 static void test_dctcp(void)
191 {
192         struct bpf_dctcp *dctcp_skel;
193         struct bpf_link *link;
194
195         dctcp_skel = bpf_dctcp__open_and_load();
196         if (CHECK(!dctcp_skel, "bpf_dctcp__open_and_load", "failed\n"))
197                 return;
198
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);
202                 return;
203         }
204
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);
209
210         bpf_link__destroy(link);
211         bpf_dctcp__destroy(dctcp_skel);
212 }
213
214 static char *err_str;
215 static bool found;
216
217 static int libbpf_debug_print(enum libbpf_print_level level,
218                               const char *format, va_list args)
219 {
220         const char *prog_name, *log_buf;
221
222         if (level != LIBBPF_WARN ||
223             !strstr(format, "-- BEGIN PROG LOAD LOG --")) {
224                 vprintf(format, args);
225                 return 0;
226         }
227
228         prog_name = va_arg(args, char *);
229         log_buf = va_arg(args, char *);
230         if (!log_buf)
231                 goto out;
232         if (err_str && strstr(log_buf, err_str) != NULL)
233                 found = true;
234 out:
235         printf(format, prog_name, log_buf);
236         return 0;
237 }
238
239 static void test_invalid_license(void)
240 {
241         libbpf_print_fn_t old_print_fn;
242         struct bpf_tcp_nogpl *skel;
243
244         err_str = "struct ops programs must have a GPL compatible license";
245         found = false;
246         old_print_fn = libbpf_set_print(libbpf_debug_print);
247
248         skel = bpf_tcp_nogpl__open_and_load();
249         ASSERT_NULL(skel, "bpf_tcp_nogpl");
250         ASSERT_EQ(found, true, "expected_err_msg");
251
252         bpf_tcp_nogpl__destroy(skel);
253         libbpf_set_print(old_print_fn);
254 }
255
256 static void test_dctcp_fallback(void)
257 {
258         int err, lfd = -1, cli_fd = -1, srv_fd = -1;
259         struct network_helper_opts opts = {
260                 .cc = "cubic",
261         };
262         struct bpf_dctcp *dctcp_skel;
263         struct bpf_link *link = NULL;
264         char srv_cc[16];
265         socklen_t cc_len = sizeof(srv_cc);
266
267         dctcp_skel = bpf_dctcp__open();
268         if (!ASSERT_OK_PTR(dctcp_skel, "dctcp_skel"))
269                 return;
270         strcpy(dctcp_skel->rodata->fallback, "cubic");
271         if (!ASSERT_OK(bpf_dctcp__load(dctcp_skel), "bpf_dctcp__load"))
272                 goto done;
273
274         link = bpf_map__attach_struct_ops(dctcp_skel->maps.dctcp);
275         if (!ASSERT_OK_PTR(link, "dctcp link"))
276                 goto done;
277
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"))
281                 goto done;
282
283         cli_fd = connect_to_fd_opts(lfd, &opts);
284         if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
285                 goto done;
286
287         srv_fd = accept(lfd, NULL, 0);
288         if (!ASSERT_GE(srv_fd, 0, "srv_fd"))
289                 goto done;
290         ASSERT_STREQ(dctcp_skel->bss->cc_res, "cubic", "cc_res");
291         ASSERT_EQ(dctcp_skel->bss->tcp_cdg_res, -ENOTSUPP, "tcp_cdg_res");
292
293         err = getsockopt(srv_fd, SOL_TCP, TCP_CONGESTION, srv_cc, &cc_len);
294         if (!ASSERT_OK(err, "getsockopt(srv_fd, TCP_CONGESTION)"))
295                 goto done;
296         ASSERT_STREQ(srv_cc, "cubic", "srv_fd cc");
297
298 done:
299         bpf_link__destroy(link);
300         bpf_dctcp__destroy(dctcp_skel);
301         if (lfd != -1)
302                 close(lfd);
303         if (srv_fd != -1)
304                 close(srv_fd);
305         if (cli_fd != -1)
306                 close(cli_fd);
307 }
308
309 static void test_rel_setsockopt(void)
310 {
311         struct bpf_dctcp_release *rel_skel;
312         libbpf_print_fn_t old_print_fn;
313
314         err_str = "unknown func bpf_setsockopt";
315         found = false;
316
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);
320
321         ASSERT_ERR_PTR(rel_skel, "rel_skel");
322         ASSERT_TRUE(found, "expected_err_msg");
323
324         bpf_dctcp_release__destroy(rel_skel);
325 }
326
327 void test_bpf_tcp_ca(void)
328 {
329         if (test__start_subtest("dctcp"))
330                 test_dctcp();
331         if (test__start_subtest("cubic"))
332                 test_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();
339 }
This page took 0.049982 seconds and 4 git commands to generate.