1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Facebook */
3 
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <linux/bpf.h>
12 #include <linux/err.h>
13 #include <linux/types.h>
14 #include <linux/if_ether.h>
15 #include <sys/types.h>
16 #include <sys/epoll.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <bpf/bpf.h>
20 #include <bpf/libbpf.h>
21 #include "bpf_rlimit.h"
22 #include "bpf_util.h"
23 
24 #include "test_progs.h"
25 #include "test_select_reuseport_common.h"
26 
27 #define MAX_TEST_NAME 80
28 #define MIN_TCPHDR_LEN 20
29 #define UDPHDR_LEN 8
30 
31 #define TCP_SYNCOOKIE_SYSCTL "/proc/sys/net/ipv4/tcp_syncookies"
32 #define TCP_FO_SYSCTL "/proc/sys/net/ipv4/tcp_fastopen"
33 #define REUSEPORT_ARRAY_SIZE 32
34 
35 static int result_map, tmp_index_ovr_map, linum_map, data_check_map;
36 static __u32 expected_results[NR_RESULTS];
37 static int sk_fds[REUSEPORT_ARRAY_SIZE];
38 static int reuseport_array = -1, outer_map = -1;
39 static int select_by_skb_data_prog;
40 static int saved_tcp_syncookie = -1;
41 static struct bpf_object *obj;
42 static int saved_tcp_fo = -1;
43 static __u32 index_zero;
44 static int epfd;
45 
46 static union sa46 {
47 	struct sockaddr_in6 v6;
48 	struct sockaddr_in v4;
49 	sa_family_t family;
50 } srv_sa;
51 
52 #define RET_IF(condition, tag, format...) ({				\
53 	if (CHECK_FAIL(condition)) {					\
54 		printf(tag " " format);					\
55 		return;							\
56 	}								\
57 })
58 
59 #define RET_ERR(condition, tag, format...) ({				\
60 	if (CHECK_FAIL(condition)) {					\
61 		printf(tag " " format);					\
62 		return -1;						\
63 	}								\
64 })
65 
66 static int create_maps(void)
67 {
68 	struct bpf_create_map_attr attr = {};
69 
70 	/* Creating reuseport_array */
71 	attr.name = "reuseport_array";
72 	attr.map_type = BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
73 	attr.key_size = sizeof(__u32);
74 	attr.value_size = sizeof(__u32);
75 	attr.max_entries = REUSEPORT_ARRAY_SIZE;
76 
77 	reuseport_array = bpf_create_map_xattr(&attr);
78 	RET_ERR(reuseport_array == -1, "creating reuseport_array",
79 		"reuseport_array:%d errno:%d\n", reuseport_array, errno);
80 
81 	/* Creating outer_map */
82 	attr.name = "outer_map";
83 	attr.map_type = BPF_MAP_TYPE_ARRAY_OF_MAPS;
84 	attr.key_size = sizeof(__u32);
85 	attr.value_size = sizeof(__u32);
86 	attr.max_entries = 1;
87 	attr.inner_map_fd = reuseport_array;
88 	outer_map = bpf_create_map_xattr(&attr);
89 	RET_ERR(outer_map == -1, "creating outer_map",
90 		"outer_map:%d errno:%d\n", outer_map, errno);
91 
92 	return 0;
93 }
94 
95 static int prepare_bpf_obj(void)
96 {
97 	struct bpf_program *prog;
98 	struct bpf_map *map;
99 	int err;
100 
101 	obj = bpf_object__open("test_select_reuseport_kern.o");
102 	RET_ERR(IS_ERR_OR_NULL(obj), "open test_select_reuseport_kern.o",
103 		"obj:%p PTR_ERR(obj):%ld\n", obj, PTR_ERR(obj));
104 
105 	map = bpf_object__find_map_by_name(obj, "outer_map");
106 	RET_ERR(!map, "find outer_map", "!map\n");
107 	err = bpf_map__reuse_fd(map, outer_map);
108 	RET_ERR(err, "reuse outer_map", "err:%d\n", err);
109 
110 	err = bpf_object__load(obj);
111 	RET_ERR(err, "load bpf_object", "err:%d\n", err);
112 
113 	prog = bpf_program__next(NULL, obj);
114 	RET_ERR(!prog, "get first bpf_program", "!prog\n");
115 	select_by_skb_data_prog = bpf_program__fd(prog);
116 	RET_ERR(select_by_skb_data_prog == -1, "get prog fd",
117 		"select_by_skb_data_prog:%d\n", select_by_skb_data_prog);
118 
119 	map = bpf_object__find_map_by_name(obj, "result_map");
120 	RET_ERR(!map, "find result_map", "!map\n");
121 	result_map = bpf_map__fd(map);
122 	RET_ERR(result_map == -1, "get result_map fd",
123 		"result_map:%d\n", result_map);
124 
125 	map = bpf_object__find_map_by_name(obj, "tmp_index_ovr_map");
126 	RET_ERR(!map, "find tmp_index_ovr_map\n", "!map");
127 	tmp_index_ovr_map = bpf_map__fd(map);
128 	RET_ERR(tmp_index_ovr_map == -1, "get tmp_index_ovr_map fd",
129 		"tmp_index_ovr_map:%d\n", tmp_index_ovr_map);
130 
131 	map = bpf_object__find_map_by_name(obj, "linum_map");
132 	RET_ERR(!map, "find linum_map", "!map\n");
133 	linum_map = bpf_map__fd(map);
134 	RET_ERR(linum_map == -1, "get linum_map fd",
135 		"linum_map:%d\n", linum_map);
136 
137 	map = bpf_object__find_map_by_name(obj, "data_check_map");
138 	RET_ERR(!map, "find data_check_map", "!map\n");
139 	data_check_map = bpf_map__fd(map);
140 	RET_ERR(data_check_map == -1, "get data_check_map fd",
141 		"data_check_map:%d\n", data_check_map);
142 
143 	return 0;
144 }
145 
146 static void sa46_init_loopback(union sa46 *sa, sa_family_t family)
147 {
148 	memset(sa, 0, sizeof(*sa));
149 	sa->family = family;
150 	if (sa->family == AF_INET6)
151 		sa->v6.sin6_addr = in6addr_loopback;
152 	else
153 		sa->v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
154 }
155 
156 static void sa46_init_inany(union sa46 *sa, sa_family_t family)
157 {
158 	memset(sa, 0, sizeof(*sa));
159 	sa->family = family;
160 	if (sa->family == AF_INET6)
161 		sa->v6.sin6_addr = in6addr_any;
162 	else
163 		sa->v4.sin_addr.s_addr = INADDR_ANY;
164 }
165 
166 static int read_int_sysctl(const char *sysctl)
167 {
168 	char buf[16];
169 	int fd, ret;
170 
171 	fd = open(sysctl, 0);
172 	RET_ERR(fd == -1, "open(sysctl)",
173 		"sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
174 
175 	ret = read(fd, buf, sizeof(buf));
176 	RET_ERR(ret <= 0, "read(sysctl)",
177 		"sysctl:%s ret:%d errno:%d\n", sysctl, ret, errno);
178 
179 	close(fd);
180 	return atoi(buf);
181 }
182 
183 static int write_int_sysctl(const char *sysctl, int v)
184 {
185 	int fd, ret, size;
186 	char buf[16];
187 
188 	fd = open(sysctl, O_RDWR);
189 	RET_ERR(fd == -1, "open(sysctl)",
190 		"sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
191 
192 	size = snprintf(buf, sizeof(buf), "%d", v);
193 	ret = write(fd, buf, size);
194 	RET_ERR(ret != size, "write(sysctl)",
195 		"sysctl:%s ret:%d size:%d errno:%d\n",
196 		sysctl, ret, size, errno);
197 
198 	close(fd);
199 	return 0;
200 }
201 
202 static void restore_sysctls(void)
203 {
204 	if (saved_tcp_fo != -1)
205 		write_int_sysctl(TCP_FO_SYSCTL, saved_tcp_fo);
206 	if (saved_tcp_syncookie != -1)
207 		write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, saved_tcp_syncookie);
208 }
209 
210 static int enable_fastopen(void)
211 {
212 	int fo;
213 
214 	fo = read_int_sysctl(TCP_FO_SYSCTL);
215 	if (fo < 0)
216 		return -1;
217 
218 	return write_int_sysctl(TCP_FO_SYSCTL, fo | 7);
219 }
220 
221 static int enable_syncookie(void)
222 {
223 	return write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 2);
224 }
225 
226 static int disable_syncookie(void)
227 {
228 	return write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 0);
229 }
230 
231 static long get_linum(void)
232 {
233 	__u32 linum;
234 	int err;
235 
236 	err = bpf_map_lookup_elem(linum_map, &index_zero, &linum);
237 	RET_ERR(err == -1, "lookup_elem(linum_map)", "err:%d errno:%d\n",
238 		err, errno);
239 
240 	return linum;
241 }
242 
243 static void check_data(int type, sa_family_t family, const struct cmd *cmd,
244 		       int cli_fd)
245 {
246 	struct data_check expected = {}, result;
247 	union sa46 cli_sa;
248 	socklen_t addrlen;
249 	int err;
250 
251 	addrlen = sizeof(cli_sa);
252 	err = getsockname(cli_fd, (struct sockaddr *)&cli_sa,
253 			  &addrlen);
254 	RET_IF(err == -1, "getsockname(cli_fd)", "err:%d errno:%d\n",
255 	       err, errno);
256 
257 	err = bpf_map_lookup_elem(data_check_map, &index_zero, &result);
258 	RET_IF(err == -1, "lookup_elem(data_check_map)", "err:%d errno:%d\n",
259 	       err, errno);
260 
261 	if (type == SOCK_STREAM) {
262 		expected.len = MIN_TCPHDR_LEN;
263 		expected.ip_protocol = IPPROTO_TCP;
264 	} else {
265 		expected.len = UDPHDR_LEN;
266 		expected.ip_protocol = IPPROTO_UDP;
267 	}
268 
269 	if (family == AF_INET6) {
270 		expected.eth_protocol = htons(ETH_P_IPV6);
271 		expected.bind_inany = !srv_sa.v6.sin6_addr.s6_addr32[3] &&
272 			!srv_sa.v6.sin6_addr.s6_addr32[2] &&
273 			!srv_sa.v6.sin6_addr.s6_addr32[1] &&
274 			!srv_sa.v6.sin6_addr.s6_addr32[0];
275 
276 		memcpy(&expected.skb_addrs[0], cli_sa.v6.sin6_addr.s6_addr32,
277 		       sizeof(cli_sa.v6.sin6_addr));
278 		memcpy(&expected.skb_addrs[4], &in6addr_loopback,
279 		       sizeof(in6addr_loopback));
280 		expected.skb_ports[0] = cli_sa.v6.sin6_port;
281 		expected.skb_ports[1] = srv_sa.v6.sin6_port;
282 	} else {
283 		expected.eth_protocol = htons(ETH_P_IP);
284 		expected.bind_inany = !srv_sa.v4.sin_addr.s_addr;
285 
286 		expected.skb_addrs[0] = cli_sa.v4.sin_addr.s_addr;
287 		expected.skb_addrs[1] = htonl(INADDR_LOOPBACK);
288 		expected.skb_ports[0] = cli_sa.v4.sin_port;
289 		expected.skb_ports[1] = srv_sa.v4.sin_port;
290 	}
291 
292 	if (memcmp(&result, &expected, offsetof(struct data_check,
293 						equal_check_end))) {
294 		printf("unexpected data_check\n");
295 		printf("  result: (0x%x, %u, %u)\n",
296 		       result.eth_protocol, result.ip_protocol,
297 		       result.bind_inany);
298 		printf("expected: (0x%x, %u, %u)\n",
299 		       expected.eth_protocol, expected.ip_protocol,
300 		       expected.bind_inany);
301 		RET_IF(1, "data_check result != expected",
302 		       "bpf_prog_linum:%ld\n", get_linum());
303 	}
304 
305 	RET_IF(!result.hash, "data_check result.hash empty",
306 	       "result.hash:%u", result.hash);
307 
308 	expected.len += cmd ? sizeof(*cmd) : 0;
309 	if (type == SOCK_STREAM)
310 		RET_IF(expected.len > result.len, "expected.len > result.len",
311 		       "expected.len:%u result.len:%u bpf_prog_linum:%ld\n",
312 		       expected.len, result.len, get_linum());
313 	else
314 		RET_IF(expected.len != result.len, "expected.len != result.len",
315 		       "expected.len:%u result.len:%u bpf_prog_linum:%ld\n",
316 		       expected.len, result.len, get_linum());
317 }
318 
319 static const char *result_to_str(enum result res)
320 {
321 	switch (res) {
322 	case DROP_ERR_INNER_MAP:
323 		return "DROP_ERR_INNER_MAP";
324 	case DROP_ERR_SKB_DATA:
325 		return "DROP_ERR_SKB_DATA";
326 	case DROP_ERR_SK_SELECT_REUSEPORT:
327 		return "DROP_ERR_SK_SELECT_REUSEPORT";
328 	case DROP_MISC:
329 		return "DROP_MISC";
330 	case PASS:
331 		return "PASS";
332 	case PASS_ERR_SK_SELECT_REUSEPORT:
333 		return "PASS_ERR_SK_SELECT_REUSEPORT";
334 	default:
335 		return "UNKNOWN";
336 	}
337 }
338 
339 static void check_results(void)
340 {
341 	__u32 results[NR_RESULTS];
342 	__u32 i, broken = 0;
343 	int err;
344 
345 	for (i = 0; i < NR_RESULTS; i++) {
346 		err = bpf_map_lookup_elem(result_map, &i, &results[i]);
347 		RET_IF(err == -1, "lookup_elem(result_map)",
348 		       "i:%u err:%d errno:%d\n", i, err, errno);
349 	}
350 
351 	for (i = 0; i < NR_RESULTS; i++) {
352 		if (results[i] != expected_results[i]) {
353 			broken = i;
354 			break;
355 		}
356 	}
357 
358 	if (i == NR_RESULTS)
359 		return;
360 
361 	printf("unexpected result\n");
362 	printf(" result: [");
363 	printf("%u", results[0]);
364 	for (i = 1; i < NR_RESULTS; i++)
365 		printf(", %u", results[i]);
366 	printf("]\n");
367 
368 	printf("expected: [");
369 	printf("%u", expected_results[0]);
370 	for (i = 1; i < NR_RESULTS; i++)
371 		printf(", %u", expected_results[i]);
372 	printf("]\n");
373 
374 	printf("mismatch on %s (bpf_prog_linum:%ld)\n", result_to_str(broken),
375 	       get_linum());
376 
377 	CHECK_FAIL(true);
378 }
379 
380 static int send_data(int type, sa_family_t family, void *data, size_t len,
381 		     enum result expected)
382 {
383 	union sa46 cli_sa;
384 	int fd, err;
385 
386 	fd = socket(family, type, 0);
387 	RET_ERR(fd == -1, "socket()", "fd:%d errno:%d\n", fd, errno);
388 
389 	sa46_init_loopback(&cli_sa, family);
390 	err = bind(fd, (struct sockaddr *)&cli_sa, sizeof(cli_sa));
391 	RET_ERR(fd == -1, "bind(cli_sa)", "err:%d errno:%d\n", err, errno);
392 
393 	err = sendto(fd, data, len, MSG_FASTOPEN, (struct sockaddr *)&srv_sa,
394 		     sizeof(srv_sa));
395 	RET_ERR(err != len && expected >= PASS,
396 		"sendto()", "family:%u err:%d errno:%d expected:%d\n",
397 		family, err, errno, expected);
398 
399 	return fd;
400 }
401 
402 static void do_test(int type, sa_family_t family, struct cmd *cmd,
403 		    enum result expected)
404 {
405 	int nev, srv_fd, cli_fd;
406 	struct epoll_event ev;
407 	struct cmd rcv_cmd;
408 	ssize_t nread;
409 
410 	cli_fd = send_data(type, family, cmd, cmd ? sizeof(*cmd) : 0,
411 			   expected);
412 	if (cli_fd < 0)
413 		return;
414 	nev = epoll_wait(epfd, &ev, 1, expected >= PASS ? 5 : 0);
415 	RET_IF((nev <= 0 && expected >= PASS) ||
416 	       (nev > 0 && expected < PASS),
417 	       "nev <> expected",
418 	       "nev:%d expected:%d type:%d family:%d data:(%d, %d)\n",
419 	       nev, expected, type, family,
420 	       cmd ? cmd->reuseport_index : -1,
421 	       cmd ? cmd->pass_on_failure : -1);
422 	check_results();
423 	check_data(type, family, cmd, cli_fd);
424 
425 	if (expected < PASS)
426 		return;
427 
428 	RET_IF(expected != PASS_ERR_SK_SELECT_REUSEPORT &&
429 	       cmd->reuseport_index != ev.data.u32,
430 	       "check cmd->reuseport_index",
431 	       "cmd:(%u, %u) ev.data.u32:%u\n",
432 	       cmd->pass_on_failure, cmd->reuseport_index, ev.data.u32);
433 
434 	srv_fd = sk_fds[ev.data.u32];
435 	if (type == SOCK_STREAM) {
436 		int new_fd = accept(srv_fd, NULL, 0);
437 
438 		RET_IF(new_fd == -1, "accept(srv_fd)",
439 		       "ev.data.u32:%u new_fd:%d errno:%d\n",
440 		       ev.data.u32, new_fd, errno);
441 
442 		nread = recv(new_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT);
443 		RET_IF(nread != sizeof(rcv_cmd),
444 		       "recv(new_fd)",
445 		       "ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n",
446 		       ev.data.u32, nread, sizeof(rcv_cmd), errno);
447 
448 		close(new_fd);
449 	} else {
450 		nread = recv(srv_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT);
451 		RET_IF(nread != sizeof(rcv_cmd),
452 		       "recv(sk_fds)",
453 		       "ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n",
454 		       ev.data.u32, nread, sizeof(rcv_cmd), errno);
455 	}
456 
457 	close(cli_fd);
458 }
459 
460 static void test_err_inner_map(int type, sa_family_t family)
461 {
462 	struct cmd cmd = {
463 		.reuseport_index = 0,
464 		.pass_on_failure = 0,
465 	};
466 
467 	expected_results[DROP_ERR_INNER_MAP]++;
468 	do_test(type, family, &cmd, DROP_ERR_INNER_MAP);
469 }
470 
471 static void test_err_skb_data(int type, sa_family_t family)
472 {
473 	expected_results[DROP_ERR_SKB_DATA]++;
474 	do_test(type, family, NULL, DROP_ERR_SKB_DATA);
475 }
476 
477 static void test_err_sk_select_port(int type, sa_family_t family)
478 {
479 	struct cmd cmd = {
480 		.reuseport_index = REUSEPORT_ARRAY_SIZE,
481 		.pass_on_failure = 0,
482 	};
483 
484 	expected_results[DROP_ERR_SK_SELECT_REUSEPORT]++;
485 	do_test(type, family, &cmd, DROP_ERR_SK_SELECT_REUSEPORT);
486 }
487 
488 static void test_pass(int type, sa_family_t family)
489 {
490 	struct cmd cmd;
491 	int i;
492 
493 	cmd.pass_on_failure = 0;
494 	for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) {
495 		expected_results[PASS]++;
496 		cmd.reuseport_index = i;
497 		do_test(type, family, &cmd, PASS);
498 	}
499 }
500 
501 static void test_syncookie(int type, sa_family_t family)
502 {
503 	int err, tmp_index = 1;
504 	struct cmd cmd = {
505 		.reuseport_index = 0,
506 		.pass_on_failure = 0,
507 	};
508 
509 	if (type != SOCK_STREAM)
510 		return;
511 
512 	/*
513 	 * +1 for TCP-SYN and
514 	 * +1 for the TCP-ACK (ack the syncookie)
515 	 */
516 	expected_results[PASS] += 2;
517 	enable_syncookie();
518 	/*
519 	 * Simulate TCP-SYN and TCP-ACK are handled by two different sk:
520 	 * TCP-SYN: select sk_fds[tmp_index = 1] tmp_index is from the
521 	 *          tmp_index_ovr_map
522 	 * TCP-ACK: select sk_fds[reuseport_index = 0] reuseport_index
523 	 *          is from the cmd.reuseport_index
524 	 */
525 	err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero,
526 				  &tmp_index, BPF_ANY);
527 	RET_IF(err == -1, "update_elem(tmp_index_ovr_map, 0, 1)",
528 	       "err:%d errno:%d\n", err, errno);
529 	do_test(type, family, &cmd, PASS);
530 	err = bpf_map_lookup_elem(tmp_index_ovr_map, &index_zero,
531 				  &tmp_index);
532 	RET_IF(err == -1 || tmp_index != -1,
533 	       "lookup_elem(tmp_index_ovr_map)",
534 	       "err:%d errno:%d tmp_index:%d\n",
535 	       err, errno, tmp_index);
536 	disable_syncookie();
537 }
538 
539 static void test_pass_on_err(int type, sa_family_t family)
540 {
541 	struct cmd cmd = {
542 		.reuseport_index = REUSEPORT_ARRAY_SIZE,
543 		.pass_on_failure = 1,
544 	};
545 
546 	expected_results[PASS_ERR_SK_SELECT_REUSEPORT] += 1;
547 	do_test(type, family, &cmd, PASS_ERR_SK_SELECT_REUSEPORT);
548 }
549 
550 static void test_detach_bpf(int type, sa_family_t family)
551 {
552 #ifdef SO_DETACH_REUSEPORT_BPF
553 	__u32 nr_run_before = 0, nr_run_after = 0, tmp, i;
554 	struct epoll_event ev;
555 	int cli_fd, err, nev;
556 	struct cmd cmd = {};
557 	int optvalue = 0;
558 
559 	err = setsockopt(sk_fds[0], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF,
560 			 &optvalue, sizeof(optvalue));
561 	RET_IF(err == -1, "setsockopt(SO_DETACH_REUSEPORT_BPF)",
562 	       "err:%d errno:%d\n", err, errno);
563 
564 	err = setsockopt(sk_fds[1], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF,
565 			 &optvalue, sizeof(optvalue));
566 	RET_IF(err == 0 || errno != ENOENT,
567 	       "setsockopt(SO_DETACH_REUSEPORT_BPF)",
568 	       "err:%d errno:%d\n", err, errno);
569 
570 	for (i = 0; i < NR_RESULTS; i++) {
571 		err = bpf_map_lookup_elem(result_map, &i, &tmp);
572 		RET_IF(err == -1, "lookup_elem(result_map)",
573 		       "i:%u err:%d errno:%d\n", i, err, errno);
574 		nr_run_before += tmp;
575 	}
576 
577 	cli_fd = send_data(type, family, &cmd, sizeof(cmd), PASS);
578 	if (cli_fd < 0)
579 		return;
580 	nev = epoll_wait(epfd, &ev, 1, 5);
581 	RET_IF(nev <= 0, "nev <= 0",
582 	       "nev:%d expected:1 type:%d family:%d data:(0, 0)\n",
583 	       nev,  type, family);
584 
585 	for (i = 0; i < NR_RESULTS; i++) {
586 		err = bpf_map_lookup_elem(result_map, &i, &tmp);
587 		RET_IF(err == -1, "lookup_elem(result_map)",
588 		       "i:%u err:%d errno:%d\n", i, err, errno);
589 		nr_run_after += tmp;
590 	}
591 
592 	RET_IF(nr_run_before != nr_run_after,
593 	       "nr_run_before != nr_run_after",
594 	       "nr_run_before:%u nr_run_after:%u\n",
595 	       nr_run_before, nr_run_after);
596 
597 	close(cli_fd);
598 #else
599 	test__skip();
600 #endif
601 }
602 
603 static void prepare_sk_fds(int type, sa_family_t family, bool inany)
604 {
605 	const int first = REUSEPORT_ARRAY_SIZE - 1;
606 	int i, err, optval = 1;
607 	struct epoll_event ev;
608 	socklen_t addrlen;
609 
610 	if (inany)
611 		sa46_init_inany(&srv_sa, family);
612 	else
613 		sa46_init_loopback(&srv_sa, family);
614 	addrlen = sizeof(srv_sa);
615 
616 	/*
617 	 * The sk_fds[] is filled from the back such that the order
618 	 * is exactly opposite to the (struct sock_reuseport *)reuse->socks[].
619 	 */
620 	for (i = first; i >= 0; i--) {
621 		sk_fds[i] = socket(family, type, 0);
622 		RET_IF(sk_fds[i] == -1, "socket()", "sk_fds[%d]:%d errno:%d\n",
623 		       i, sk_fds[i], errno);
624 		err = setsockopt(sk_fds[i], SOL_SOCKET, SO_REUSEPORT,
625 				 &optval, sizeof(optval));
626 		RET_IF(err == -1, "setsockopt(SO_REUSEPORT)",
627 		       "sk_fds[%d] err:%d errno:%d\n",
628 		       i, err, errno);
629 
630 		if (i == first) {
631 			err = setsockopt(sk_fds[i], SOL_SOCKET,
632 					 SO_ATTACH_REUSEPORT_EBPF,
633 					 &select_by_skb_data_prog,
634 					 sizeof(select_by_skb_data_prog));
635 			RET_IF(err == -1, "setsockopt(SO_ATTACH_REUEPORT_EBPF)",
636 			       "err:%d errno:%d\n", err, errno);
637 		}
638 
639 		err = bind(sk_fds[i], (struct sockaddr *)&srv_sa, addrlen);
640 		RET_IF(err == -1, "bind()", "sk_fds[%d] err:%d errno:%d\n",
641 		       i, err, errno);
642 
643 		if (type == SOCK_STREAM) {
644 			err = listen(sk_fds[i], 10);
645 			RET_IF(err == -1, "listen()",
646 			       "sk_fds[%d] err:%d errno:%d\n",
647 			       i, err, errno);
648 		}
649 
650 		err = bpf_map_update_elem(reuseport_array, &i, &sk_fds[i],
651 					  BPF_NOEXIST);
652 		RET_IF(err == -1, "update_elem(reuseport_array)",
653 		       "sk_fds[%d] err:%d errno:%d\n", i, err, errno);
654 
655 		if (i == first) {
656 			socklen_t addrlen = sizeof(srv_sa);
657 
658 			err = getsockname(sk_fds[i], (struct sockaddr *)&srv_sa,
659 					  &addrlen);
660 			RET_IF(err == -1, "getsockname()",
661 			       "sk_fds[%d] err:%d errno:%d\n", i, err, errno);
662 		}
663 	}
664 
665 	epfd = epoll_create(1);
666 	RET_IF(epfd == -1, "epoll_create(1)",
667 	       "epfd:%d errno:%d\n", epfd, errno);
668 
669 	ev.events = EPOLLIN;
670 	for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) {
671 		ev.data.u32 = i;
672 		err = epoll_ctl(epfd, EPOLL_CTL_ADD, sk_fds[i], &ev);
673 		RET_IF(err, "epoll_ctl(EPOLL_CTL_ADD)", "sk_fds[%d]\n", i);
674 	}
675 }
676 
677 static void setup_per_test(int type, sa_family_t family, bool inany,
678 			   bool no_inner_map)
679 {
680 	int ovr = -1, err;
681 
682 	prepare_sk_fds(type, family, inany);
683 	err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero, &ovr,
684 				  BPF_ANY);
685 	RET_IF(err == -1, "update_elem(tmp_index_ovr_map, 0, -1)",
686 	       "err:%d errno:%d\n", err, errno);
687 
688 	/* Install reuseport_array to outer_map? */
689 	if (no_inner_map)
690 		return;
691 
692 	err = bpf_map_update_elem(outer_map, &index_zero, &reuseport_array,
693 				  BPF_ANY);
694 	RET_IF(err == -1, "update_elem(outer_map, 0, reuseport_array)",
695 	       "err:%d errno:%d\n", err, errno);
696 }
697 
698 static void cleanup_per_test(bool no_inner_map)
699 {
700 	int i, err, zero = 0;
701 
702 	memset(expected_results, 0, sizeof(expected_results));
703 
704 	for (i = 0; i < NR_RESULTS; i++) {
705 		err = bpf_map_update_elem(result_map, &i, &zero, BPF_ANY);
706 		RET_IF(err, "reset elem in result_map",
707 		       "i:%u err:%d errno:%d\n", i, err, errno);
708 	}
709 
710 	err = bpf_map_update_elem(linum_map, &zero, &zero, BPF_ANY);
711 	RET_IF(err, "reset line number in linum_map", "err:%d errno:%d\n",
712 	       err, errno);
713 
714 	for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++)
715 		close(sk_fds[i]);
716 	close(epfd);
717 
718 	/* Delete reuseport_array from outer_map? */
719 	if (no_inner_map)
720 		return;
721 
722 	err = bpf_map_delete_elem(outer_map, &index_zero);
723 	RET_IF(err == -1, "delete_elem(outer_map)",
724 	       "err:%d errno:%d\n", err, errno);
725 }
726 
727 static void cleanup(void)
728 {
729 	if (outer_map != -1)
730 		close(outer_map);
731 	if (reuseport_array != -1)
732 		close(reuseport_array);
733 	if (obj)
734 		bpf_object__close(obj);
735 }
736 
737 static const char *family_str(sa_family_t family)
738 {
739 	switch (family) {
740 	case AF_INET:
741 		return "IPv4";
742 	case AF_INET6:
743 		return "IPv6";
744 	default:
745 		return "unknown";
746 	}
747 }
748 
749 static const char *sotype_str(int sotype)
750 {
751 	switch (sotype) {
752 	case SOCK_STREAM:
753 		return "TCP";
754 	case SOCK_DGRAM:
755 		return "UDP";
756 	default:
757 		return "unknown";
758 	}
759 }
760 
761 #define TEST_INIT(fn, ...) { fn, #fn, __VA_ARGS__ }
762 
763 static void test_config(int sotype, sa_family_t family, bool inany)
764 {
765 	const struct test {
766 		void (*fn)(int sotype, sa_family_t family);
767 		const char *name;
768 		bool no_inner_map;
769 	} tests[] = {
770 		TEST_INIT(test_err_inner_map, true /* no_inner_map */),
771 		TEST_INIT(test_err_skb_data),
772 		TEST_INIT(test_err_sk_select_port),
773 		TEST_INIT(test_pass),
774 		TEST_INIT(test_syncookie),
775 		TEST_INIT(test_pass_on_err),
776 		TEST_INIT(test_detach_bpf),
777 	};
778 	char s[MAX_TEST_NAME];
779 	const struct test *t;
780 
781 	for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
782 		snprintf(s, sizeof(s), "%s/%s %s %s",
783 			 family_str(family), sotype_str(sotype),
784 			 inany ? "INANY" : "LOOPBACK", t->name);
785 
786 		if (!test__start_subtest(s))
787 			continue;
788 
789 		setup_per_test(sotype, family, inany, t->no_inner_map);
790 		t->fn(sotype, family);
791 		cleanup_per_test(t->no_inner_map);
792 	}
793 }
794 
795 #define BIND_INANY true
796 
797 static void test_all(void)
798 {
799 	const struct config {
800 		int sotype;
801 		sa_family_t family;
802 		bool inany;
803 	} configs[] = {
804 		{ SOCK_STREAM, AF_INET },
805 		{ SOCK_STREAM, AF_INET, BIND_INANY },
806 		{ SOCK_STREAM, AF_INET6 },
807 		{ SOCK_STREAM, AF_INET6, BIND_INANY },
808 		{ SOCK_DGRAM, AF_INET },
809 		{ SOCK_DGRAM, AF_INET6 },
810 	};
811 	const struct config *c;
812 
813 	for (c = configs; c < configs + ARRAY_SIZE(configs); c++)
814 		test_config(c->sotype, c->family, c->inany);
815 }
816 
817 void test_select_reuseport(void)
818 {
819 	if (create_maps())
820 		goto out;
821 	if (prepare_bpf_obj())
822 		goto out;
823 
824 	saved_tcp_fo = read_int_sysctl(TCP_FO_SYSCTL);
825 	saved_tcp_syncookie = read_int_sysctl(TCP_SYNCOOKIE_SYSCTL);
826 	if (saved_tcp_syncookie < 0 || saved_tcp_syncookie < 0)
827 		goto out;
828 
829 	if (enable_fastopen())
830 		goto out;
831 	if (disable_syncookie())
832 		goto out;
833 
834 	test_all();
835 out:
836 	cleanup();
837 	restore_sysctls();
838 }
839