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