reuseport_bpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Test functionality of BPF filters for SO_REUSEPORT. The tests below will use
  3. * a BPF program (both classic and extended) to read the first word from an
  4. * incoming packet (expected to be in network byte-order), calculate a modulus
  5. * of that number, and then dispatch the packet to the Nth socket using the
  6. * result. These tests are run for each supported address family and protocol.
  7. * Additionally, a few edge cases in the implementation are tested.
  8. */
  9. #include <errno.h>
  10. #include <error.h>
  11. #include <fcntl.h>
  12. #include <linux/bpf.h>
  13. #include <linux/filter.h>
  14. #include <linux/unistd.h>
  15. #include <netinet/in.h>
  16. #include <netinet/tcp.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/epoll.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <sys/resource.h>
  24. #include <unistd.h>
  25. #include "kselftest.h"
  26. struct test_params {
  27. int recv_family;
  28. int send_family;
  29. int protocol;
  30. size_t recv_socks;
  31. uint16_t recv_port;
  32. uint16_t send_port_min;
  33. };
  34. static size_t sockaddr_size(void)
  35. {
  36. return sizeof(struct sockaddr_storage);
  37. }
  38. static struct sockaddr *new_any_sockaddr(int family, uint16_t port)
  39. {
  40. struct sockaddr_storage *addr;
  41. struct sockaddr_in *addr4;
  42. struct sockaddr_in6 *addr6;
  43. addr = malloc(sizeof(struct sockaddr_storage));
  44. memset(addr, 0, sizeof(struct sockaddr_storage));
  45. switch (family) {
  46. case AF_INET:
  47. addr4 = (struct sockaddr_in *)addr;
  48. addr4->sin_family = AF_INET;
  49. addr4->sin_addr.s_addr = htonl(INADDR_ANY);
  50. addr4->sin_port = htons(port);
  51. break;
  52. case AF_INET6:
  53. addr6 = (struct sockaddr_in6 *)addr;
  54. addr6->sin6_family = AF_INET6;
  55. addr6->sin6_addr = in6addr_any;
  56. addr6->sin6_port = htons(port);
  57. break;
  58. default:
  59. error(1, 0, "Unsupported family %d", family);
  60. }
  61. return (struct sockaddr *)addr;
  62. }
  63. static struct sockaddr *new_loopback_sockaddr(int family, uint16_t port)
  64. {
  65. struct sockaddr *addr = new_any_sockaddr(family, port);
  66. struct sockaddr_in *addr4;
  67. struct sockaddr_in6 *addr6;
  68. switch (family) {
  69. case AF_INET:
  70. addr4 = (struct sockaddr_in *)addr;
  71. addr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  72. break;
  73. case AF_INET6:
  74. addr6 = (struct sockaddr_in6 *)addr;
  75. addr6->sin6_addr = in6addr_loopback;
  76. break;
  77. default:
  78. error(1, 0, "Unsupported family %d", family);
  79. }
  80. return addr;
  81. }
  82. static void attach_ebpf(int fd, uint16_t mod)
  83. {
  84. static char bpf_log_buf[65536];
  85. static const char bpf_license[] = "GPL";
  86. int bpf_fd;
  87. const struct bpf_insn prog[] = {
  88. /* BPF_MOV64_REG(BPF_REG_6, BPF_REG_1) */
  89. { BPF_ALU64 | BPF_MOV | BPF_X, BPF_REG_6, BPF_REG_1, 0, 0 },
  90. /* BPF_LD_ABS(BPF_W, 0) R0 = (uint32_t)skb[0] */
  91. { BPF_LD | BPF_ABS | BPF_W, 0, 0, 0, 0 },
  92. /* BPF_ALU64_IMM(BPF_MOD, BPF_REG_0, mod) */
  93. { BPF_ALU64 | BPF_MOD | BPF_K, BPF_REG_0, 0, 0, mod },
  94. /* BPF_EXIT_INSN() */
  95. { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
  96. };
  97. union bpf_attr attr;
  98. memset(&attr, 0, sizeof(attr));
  99. attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  100. attr.insn_cnt = ARRAY_SIZE(prog);
  101. attr.insns = (unsigned long) &prog;
  102. attr.license = (unsigned long) &bpf_license;
  103. attr.log_buf = (unsigned long) &bpf_log_buf;
  104. attr.log_size = sizeof(bpf_log_buf);
  105. attr.log_level = 1;
  106. attr.kern_version = 0;
  107. bpf_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  108. if (bpf_fd < 0)
  109. error(1, errno, "ebpf error. log:\n%s\n", bpf_log_buf);
  110. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd,
  111. sizeof(bpf_fd)))
  112. error(1, errno, "failed to set SO_ATTACH_REUSEPORT_EBPF");
  113. close(bpf_fd);
  114. }
  115. static void attach_cbpf(int fd, uint16_t mod)
  116. {
  117. struct sock_filter code[] = {
  118. /* A = (uint32_t)skb[0] */
  119. { BPF_LD | BPF_W | BPF_ABS, 0, 0, 0 },
  120. /* A = A % mod */
  121. { BPF_ALU | BPF_MOD, 0, 0, mod },
  122. /* return A */
  123. { BPF_RET | BPF_A, 0, 0, 0 },
  124. };
  125. struct sock_fprog p = {
  126. .len = ARRAY_SIZE(code),
  127. .filter = code,
  128. };
  129. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)))
  130. error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF");
  131. }
  132. static void build_recv_group(const struct test_params p, int fd[], uint16_t mod,
  133. void (*attach_bpf)(int, uint16_t))
  134. {
  135. struct sockaddr * const addr =
  136. new_any_sockaddr(p.recv_family, p.recv_port);
  137. int i, opt;
  138. for (i = 0; i < p.recv_socks; ++i) {
  139. fd[i] = socket(p.recv_family, p.protocol, 0);
  140. if (fd[i] < 0)
  141. error(1, errno, "failed to create recv %d", i);
  142. opt = 1;
  143. if (setsockopt(fd[i], SOL_SOCKET, SO_REUSEPORT, &opt,
  144. sizeof(opt)))
  145. error(1, errno, "failed to set SO_REUSEPORT on %d", i);
  146. if (i == 0)
  147. attach_bpf(fd[i], mod);
  148. if (bind(fd[i], addr, sockaddr_size()))
  149. error(1, errno, "failed to bind recv socket %d", i);
  150. if (p.protocol == SOCK_STREAM) {
  151. opt = 4;
  152. if (setsockopt(fd[i], SOL_TCP, TCP_FASTOPEN, &opt,
  153. sizeof(opt)))
  154. error(1, errno,
  155. "failed to set TCP_FASTOPEN on %d", i);
  156. if (listen(fd[i], p.recv_socks * 10))
  157. error(1, errno, "failed to listen on socket");
  158. }
  159. }
  160. free(addr);
  161. }
  162. static void send_from(struct test_params p, uint16_t sport, char *buf,
  163. size_t len)
  164. {
  165. struct sockaddr * const saddr = new_any_sockaddr(p.send_family, sport);
  166. struct sockaddr * const daddr =
  167. new_loopback_sockaddr(p.send_family, p.recv_port);
  168. const int fd = socket(p.send_family, p.protocol, 0), one = 1;
  169. if (fd < 0)
  170. error(1, errno, "failed to create send socket");
  171. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
  172. error(1, errno, "failed to set reuseaddr");
  173. if (bind(fd, saddr, sockaddr_size()))
  174. error(1, errno, "failed to bind send socket");
  175. if (sendto(fd, buf, len, MSG_FASTOPEN, daddr, sockaddr_size()) < 0)
  176. error(1, errno, "failed to send message");
  177. close(fd);
  178. free(saddr);
  179. free(daddr);
  180. }
  181. static void test_recv_order(const struct test_params p, int fd[], int mod)
  182. {
  183. char recv_buf[8], send_buf[8];
  184. struct msghdr msg;
  185. struct iovec recv_io = { recv_buf, 8 };
  186. struct epoll_event ev;
  187. int epfd, conn, i, sport, expected;
  188. uint32_t data, ndata;
  189. epfd = epoll_create(1);
  190. if (epfd < 0)
  191. error(1, errno, "failed to create epoll");
  192. for (i = 0; i < p.recv_socks; ++i) {
  193. ev.events = EPOLLIN;
  194. ev.data.fd = fd[i];
  195. if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd[i], &ev))
  196. error(1, errno, "failed to register sock %d epoll", i);
  197. }
  198. memset(&msg, 0, sizeof(msg));
  199. msg.msg_iov = &recv_io;
  200. msg.msg_iovlen = 1;
  201. for (data = 0; data < p.recv_socks * 2; ++data) {
  202. sport = p.send_port_min + data;
  203. ndata = htonl(data);
  204. memcpy(send_buf, &ndata, sizeof(ndata));
  205. send_from(p, sport, send_buf, sizeof(ndata));
  206. i = epoll_wait(epfd, &ev, 1, -1);
  207. if (i < 0)
  208. error(1, errno, "epoll wait failed");
  209. if (p.protocol == SOCK_STREAM) {
  210. conn = accept(ev.data.fd, NULL, NULL);
  211. if (conn < 0)
  212. error(1, errno, "error accepting");
  213. i = recvmsg(conn, &msg, 0);
  214. close(conn);
  215. } else {
  216. i = recvmsg(ev.data.fd, &msg, 0);
  217. }
  218. if (i < 0)
  219. error(1, errno, "recvmsg error");
  220. if (i != sizeof(ndata))
  221. error(1, 0, "expected size %zd got %d",
  222. sizeof(ndata), i);
  223. for (i = 0; i < p.recv_socks; ++i)
  224. if (ev.data.fd == fd[i])
  225. break;
  226. memcpy(&ndata, recv_buf, sizeof(ndata));
  227. fprintf(stderr, "Socket %d: %d\n", i, ntohl(ndata));
  228. expected = (sport % mod);
  229. if (i != expected)
  230. error(1, 0, "expected socket %d", expected);
  231. }
  232. }
  233. static void test_reuseport_ebpf(struct test_params p)
  234. {
  235. int i, fd[p.recv_socks];
  236. fprintf(stderr, "Testing EBPF mod %zd...\n", p.recv_socks);
  237. build_recv_group(p, fd, p.recv_socks, attach_ebpf);
  238. test_recv_order(p, fd, p.recv_socks);
  239. p.send_port_min += p.recv_socks * 2;
  240. fprintf(stderr, "Reprograming, testing mod %zd...\n", p.recv_socks / 2);
  241. attach_ebpf(fd[0], p.recv_socks / 2);
  242. test_recv_order(p, fd, p.recv_socks / 2);
  243. for (i = 0; i < p.recv_socks; ++i)
  244. close(fd[i]);
  245. }
  246. static void test_reuseport_cbpf(struct test_params p)
  247. {
  248. int i, fd[p.recv_socks];
  249. fprintf(stderr, "Testing CBPF mod %zd...\n", p.recv_socks);
  250. build_recv_group(p, fd, p.recv_socks, attach_cbpf);
  251. test_recv_order(p, fd, p.recv_socks);
  252. p.send_port_min += p.recv_socks * 2;
  253. fprintf(stderr, "Reprograming, testing mod %zd...\n", p.recv_socks / 2);
  254. attach_cbpf(fd[0], p.recv_socks / 2);
  255. test_recv_order(p, fd, p.recv_socks / 2);
  256. for (i = 0; i < p.recv_socks; ++i)
  257. close(fd[i]);
  258. }
  259. static void test_extra_filter(const struct test_params p)
  260. {
  261. struct sockaddr * const addr =
  262. new_any_sockaddr(p.recv_family, p.recv_port);
  263. int fd1, fd2, opt;
  264. fprintf(stderr, "Testing too many filters...\n");
  265. fd1 = socket(p.recv_family, p.protocol, 0);
  266. if (fd1 < 0)
  267. error(1, errno, "failed to create socket 1");
  268. fd2 = socket(p.recv_family, p.protocol, 0);
  269. if (fd2 < 0)
  270. error(1, errno, "failed to create socket 2");
  271. opt = 1;
  272. if (setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
  273. error(1, errno, "failed to set SO_REUSEPORT on socket 1");
  274. if (setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
  275. error(1, errno, "failed to set SO_REUSEPORT on socket 2");
  276. attach_ebpf(fd1, 10);
  277. attach_ebpf(fd2, 10);
  278. if (bind(fd1, addr, sockaddr_size()))
  279. error(1, errno, "failed to bind recv socket 1");
  280. if (!bind(fd2, addr, sockaddr_size()) || errno != EADDRINUSE)
  281. error(1, errno, "bind socket 2 should fail with EADDRINUSE");
  282. free(addr);
  283. }
  284. static void test_filter_no_reuseport(const struct test_params p)
  285. {
  286. struct sockaddr * const addr =
  287. new_any_sockaddr(p.recv_family, p.recv_port);
  288. const char bpf_license[] = "GPL";
  289. struct bpf_insn ecode[] = {
  290. { BPF_ALU64 | BPF_MOV | BPF_K, BPF_REG_0, 0, 0, 10 },
  291. { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
  292. };
  293. struct sock_filter ccode[] = {{ BPF_RET | BPF_A, 0, 0, 0 }};
  294. union bpf_attr eprog;
  295. struct sock_fprog cprog;
  296. int fd, bpf_fd;
  297. fprintf(stderr, "Testing filters on non-SO_REUSEPORT socket...\n");
  298. memset(&eprog, 0, sizeof(eprog));
  299. eprog.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  300. eprog.insn_cnt = ARRAY_SIZE(ecode);
  301. eprog.insns = (unsigned long) &ecode;
  302. eprog.license = (unsigned long) &bpf_license;
  303. eprog.kern_version = 0;
  304. memset(&cprog, 0, sizeof(cprog));
  305. cprog.len = ARRAY_SIZE(ccode);
  306. cprog.filter = ccode;
  307. bpf_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &eprog, sizeof(eprog));
  308. if (bpf_fd < 0)
  309. error(1, errno, "ebpf error");
  310. fd = socket(p.recv_family, p.protocol, 0);
  311. if (fd < 0)
  312. error(1, errno, "failed to create socket 1");
  313. if (bind(fd, addr, sockaddr_size()))
  314. error(1, errno, "failed to bind recv socket 1");
  315. errno = 0;
  316. if (!setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd,
  317. sizeof(bpf_fd)) || errno != EINVAL)
  318. error(1, errno, "setsockopt should have returned EINVAL");
  319. errno = 0;
  320. if (!setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &cprog,
  321. sizeof(cprog)) || errno != EINVAL)
  322. error(1, errno, "setsockopt should have returned EINVAL");
  323. free(addr);
  324. }
  325. static void test_filter_without_bind(void)
  326. {
  327. int fd1, fd2, opt = 1;
  328. fprintf(stderr, "Testing filter add without bind...\n");
  329. fd1 = socket(AF_INET, SOCK_DGRAM, 0);
  330. if (fd1 < 0)
  331. error(1, errno, "failed to create socket 1");
  332. fd2 = socket(AF_INET, SOCK_DGRAM, 0);
  333. if (fd2 < 0)
  334. error(1, errno, "failed to create socket 2");
  335. if (setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
  336. error(1, errno, "failed to set SO_REUSEPORT on socket 1");
  337. if (setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
  338. error(1, errno, "failed to set SO_REUSEPORT on socket 2");
  339. attach_ebpf(fd1, 10);
  340. attach_cbpf(fd2, 10);
  341. close(fd1);
  342. close(fd2);
  343. }
  344. void enable_fastopen(void)
  345. {
  346. int fd = open("/proc/sys/net/ipv4/tcp_fastopen", 0);
  347. int rw_mask = 3; /* bit 1: client side; bit-2 server side */
  348. int val, size;
  349. char buf[16];
  350. if (fd < 0)
  351. error(1, errno, "Unable to open tcp_fastopen sysctl");
  352. if (read(fd, buf, sizeof(buf)) <= 0)
  353. error(1, errno, "Unable to read tcp_fastopen sysctl");
  354. val = atoi(buf);
  355. close(fd);
  356. if ((val & rw_mask) != rw_mask) {
  357. fd = open("/proc/sys/net/ipv4/tcp_fastopen", O_RDWR);
  358. if (fd < 0)
  359. error(1, errno,
  360. "Unable to open tcp_fastopen sysctl for writing");
  361. val |= rw_mask;
  362. size = snprintf(buf, 16, "%d", val);
  363. if (write(fd, buf, size) <= 0)
  364. error(1, errno, "Unable to write tcp_fastopen sysctl");
  365. close(fd);
  366. }
  367. }
  368. static struct rlimit rlim_old;
  369. static __attribute__((constructor)) void main_ctor(void)
  370. {
  371. getrlimit(RLIMIT_MEMLOCK, &rlim_old);
  372. if (rlim_old.rlim_cur != RLIM_INFINITY) {
  373. struct rlimit rlim_new;
  374. rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
  375. rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
  376. setrlimit(RLIMIT_MEMLOCK, &rlim_new);
  377. }
  378. }
  379. static __attribute__((destructor)) void main_dtor(void)
  380. {
  381. setrlimit(RLIMIT_MEMLOCK, &rlim_old);
  382. }
  383. int main(void)
  384. {
  385. fprintf(stderr, "---- IPv4 UDP ----\n");
  386. /* NOTE: UDP socket lookups traverse a different code path when there
  387. * are > 10 sockets in a group. Run the bpf test through both paths.
  388. */
  389. test_reuseport_ebpf((struct test_params) {
  390. .recv_family = AF_INET,
  391. .send_family = AF_INET,
  392. .protocol = SOCK_DGRAM,
  393. .recv_socks = 10,
  394. .recv_port = 8000,
  395. .send_port_min = 9000});
  396. test_reuseport_ebpf((struct test_params) {
  397. .recv_family = AF_INET,
  398. .send_family = AF_INET,
  399. .protocol = SOCK_DGRAM,
  400. .recv_socks = 20,
  401. .recv_port = 8000,
  402. .send_port_min = 9000});
  403. test_reuseport_cbpf((struct test_params) {
  404. .recv_family = AF_INET,
  405. .send_family = AF_INET,
  406. .protocol = SOCK_DGRAM,
  407. .recv_socks = 10,
  408. .recv_port = 8001,
  409. .send_port_min = 9020});
  410. test_reuseport_cbpf((struct test_params) {
  411. .recv_family = AF_INET,
  412. .send_family = AF_INET,
  413. .protocol = SOCK_DGRAM,
  414. .recv_socks = 20,
  415. .recv_port = 8001,
  416. .send_port_min = 9020});
  417. test_extra_filter((struct test_params) {
  418. .recv_family = AF_INET,
  419. .protocol = SOCK_DGRAM,
  420. .recv_port = 8002});
  421. test_filter_no_reuseport((struct test_params) {
  422. .recv_family = AF_INET,
  423. .protocol = SOCK_DGRAM,
  424. .recv_port = 8008});
  425. fprintf(stderr, "---- IPv6 UDP ----\n");
  426. test_reuseport_ebpf((struct test_params) {
  427. .recv_family = AF_INET6,
  428. .send_family = AF_INET6,
  429. .protocol = SOCK_DGRAM,
  430. .recv_socks = 10,
  431. .recv_port = 8003,
  432. .send_port_min = 9040});
  433. test_reuseport_ebpf((struct test_params) {
  434. .recv_family = AF_INET6,
  435. .send_family = AF_INET6,
  436. .protocol = SOCK_DGRAM,
  437. .recv_socks = 20,
  438. .recv_port = 8003,
  439. .send_port_min = 9040});
  440. test_reuseport_cbpf((struct test_params) {
  441. .recv_family = AF_INET6,
  442. .send_family = AF_INET6,
  443. .protocol = SOCK_DGRAM,
  444. .recv_socks = 10,
  445. .recv_port = 8004,
  446. .send_port_min = 9060});
  447. test_reuseport_cbpf((struct test_params) {
  448. .recv_family = AF_INET6,
  449. .send_family = AF_INET6,
  450. .protocol = SOCK_DGRAM,
  451. .recv_socks = 20,
  452. .recv_port = 8004,
  453. .send_port_min = 9060});
  454. test_extra_filter((struct test_params) {
  455. .recv_family = AF_INET6,
  456. .protocol = SOCK_DGRAM,
  457. .recv_port = 8005});
  458. test_filter_no_reuseport((struct test_params) {
  459. .recv_family = AF_INET6,
  460. .protocol = SOCK_DGRAM,
  461. .recv_port = 8009});
  462. fprintf(stderr, "---- IPv6 UDP w/ mapped IPv4 ----\n");
  463. test_reuseport_ebpf((struct test_params) {
  464. .recv_family = AF_INET6,
  465. .send_family = AF_INET,
  466. .protocol = SOCK_DGRAM,
  467. .recv_socks = 20,
  468. .recv_port = 8006,
  469. .send_port_min = 9080});
  470. test_reuseport_ebpf((struct test_params) {
  471. .recv_family = AF_INET6,
  472. .send_family = AF_INET,
  473. .protocol = SOCK_DGRAM,
  474. .recv_socks = 10,
  475. .recv_port = 8006,
  476. .send_port_min = 9080});
  477. test_reuseport_cbpf((struct test_params) {
  478. .recv_family = AF_INET6,
  479. .send_family = AF_INET,
  480. .protocol = SOCK_DGRAM,
  481. .recv_socks = 10,
  482. .recv_port = 8007,
  483. .send_port_min = 9100});
  484. test_reuseport_cbpf((struct test_params) {
  485. .recv_family = AF_INET6,
  486. .send_family = AF_INET,
  487. .protocol = SOCK_DGRAM,
  488. .recv_socks = 20,
  489. .recv_port = 8007,
  490. .send_port_min = 9100});
  491. /* TCP fastopen is required for the TCP tests */
  492. enable_fastopen();
  493. fprintf(stderr, "---- IPv4 TCP ----\n");
  494. test_reuseport_ebpf((struct test_params) {
  495. .recv_family = AF_INET,
  496. .send_family = AF_INET,
  497. .protocol = SOCK_STREAM,
  498. .recv_socks = 10,
  499. .recv_port = 8008,
  500. .send_port_min = 9120});
  501. test_reuseport_cbpf((struct test_params) {
  502. .recv_family = AF_INET,
  503. .send_family = AF_INET,
  504. .protocol = SOCK_STREAM,
  505. .recv_socks = 10,
  506. .recv_port = 8009,
  507. .send_port_min = 9160});
  508. test_extra_filter((struct test_params) {
  509. .recv_family = AF_INET,
  510. .protocol = SOCK_STREAM,
  511. .recv_port = 8010});
  512. test_filter_no_reuseport((struct test_params) {
  513. .recv_family = AF_INET,
  514. .protocol = SOCK_STREAM,
  515. .recv_port = 8011});
  516. fprintf(stderr, "---- IPv6 TCP ----\n");
  517. test_reuseport_ebpf((struct test_params) {
  518. .recv_family = AF_INET6,
  519. .send_family = AF_INET6,
  520. .protocol = SOCK_STREAM,
  521. .recv_socks = 10,
  522. .recv_port = 8012,
  523. .send_port_min = 9200});
  524. test_reuseport_cbpf((struct test_params) {
  525. .recv_family = AF_INET6,
  526. .send_family = AF_INET6,
  527. .protocol = SOCK_STREAM,
  528. .recv_socks = 10,
  529. .recv_port = 8013,
  530. .send_port_min = 9240});
  531. test_extra_filter((struct test_params) {
  532. .recv_family = AF_INET6,
  533. .protocol = SOCK_STREAM,
  534. .recv_port = 8014});
  535. test_filter_no_reuseport((struct test_params) {
  536. .recv_family = AF_INET6,
  537. .protocol = SOCK_STREAM,
  538. .recv_port = 8015});
  539. fprintf(stderr, "---- IPv6 TCP w/ mapped IPv4 ----\n");
  540. test_reuseport_ebpf((struct test_params) {
  541. .recv_family = AF_INET6,
  542. .send_family = AF_INET,
  543. .protocol = SOCK_STREAM,
  544. .recv_socks = 10,
  545. .recv_port = 8016,
  546. .send_port_min = 9320});
  547. test_reuseport_cbpf((struct test_params) {
  548. .recv_family = AF_INET6,
  549. .send_family = AF_INET,
  550. .protocol = SOCK_STREAM,
  551. .recv_socks = 10,
  552. .recv_port = 8017,
  553. .send_port_min = 9360});
  554. test_filter_without_bind();
  555. fprintf(stderr, "SUCCESS\n");
  556. return 0;
  557. }