reuseport_bpf.c 17 KB

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