reuseport_bpf_cpu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test functionality of BPF filters with SO_REUSEPORT. This program creates
  4. * an SO_REUSEPORT receiver group containing one socket per CPU core. It then
  5. * creates a BPF program that will select a socket from this group based
  6. * on the core id that receives the packet. The sending code artificially
  7. * moves itself to run on different core ids and sends one message from
  8. * each core. Since these packets are delivered over loopback, they should
  9. * arrive on the same core that sent them. The receiving code then ensures
  10. * that the packet was received on the socket for the corresponding core id.
  11. * This entire process is done for several different core id permutations
  12. * and for each IPv4/IPv6 and TCP/UDP combination.
  13. */
  14. #define _GNU_SOURCE
  15. #include <arpa/inet.h>
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <linux/filter.h>
  19. #include <linux/in.h>
  20. #include <linux/unistd.h>
  21. #include <sched.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/epoll.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <unistd.h>
  29. static const int PORT = 8888;
  30. static void build_rcv_group(int *rcv_fd, size_t len, int family, int proto)
  31. {
  32. struct sockaddr_storage addr;
  33. struct sockaddr_in *addr4;
  34. struct sockaddr_in6 *addr6;
  35. size_t i;
  36. int opt;
  37. switch (family) {
  38. case AF_INET:
  39. addr4 = (struct sockaddr_in *)&addr;
  40. addr4->sin_family = AF_INET;
  41. addr4->sin_addr.s_addr = htonl(INADDR_ANY);
  42. addr4->sin_port = htons(PORT);
  43. break;
  44. case AF_INET6:
  45. addr6 = (struct sockaddr_in6 *)&addr;
  46. addr6->sin6_family = AF_INET6;
  47. addr6->sin6_addr = in6addr_any;
  48. addr6->sin6_port = htons(PORT);
  49. break;
  50. default:
  51. error(1, 0, "Unsupported family %d", family);
  52. }
  53. for (i = 0; i < len; ++i) {
  54. rcv_fd[i] = socket(family, proto, 0);
  55. if (rcv_fd[i] < 0)
  56. error(1, errno, "failed to create receive socket");
  57. opt = 1;
  58. if (setsockopt(rcv_fd[i], SOL_SOCKET, SO_REUSEPORT, &opt,
  59. sizeof(opt)))
  60. error(1, errno, "failed to set SO_REUSEPORT");
  61. if (bind(rcv_fd[i], (struct sockaddr *)&addr, sizeof(addr)))
  62. error(1, errno, "failed to bind receive socket");
  63. if (proto == SOCK_STREAM && listen(rcv_fd[i], len * 10))
  64. error(1, errno, "failed to listen on receive port");
  65. }
  66. }
  67. static void attach_bpf(int fd)
  68. {
  69. struct sock_filter code[] = {
  70. /* A = raw_smp_processor_id() */
  71. { BPF_LD | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF + SKF_AD_CPU },
  72. /* return A */
  73. { BPF_RET | BPF_A, 0, 0, 0 },
  74. };
  75. struct sock_fprog p = {
  76. .len = 2,
  77. .filter = code,
  78. };
  79. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)))
  80. error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF");
  81. }
  82. static void send_from_cpu(int cpu_id, int family, int proto)
  83. {
  84. struct sockaddr_storage saddr, daddr;
  85. struct sockaddr_in *saddr4, *daddr4;
  86. struct sockaddr_in6 *saddr6, *daddr6;
  87. cpu_set_t cpu_set;
  88. int fd;
  89. switch (family) {
  90. case AF_INET:
  91. saddr4 = (struct sockaddr_in *)&saddr;
  92. saddr4->sin_family = AF_INET;
  93. saddr4->sin_addr.s_addr = htonl(INADDR_ANY);
  94. saddr4->sin_port = 0;
  95. daddr4 = (struct sockaddr_in *)&daddr;
  96. daddr4->sin_family = AF_INET;
  97. daddr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  98. daddr4->sin_port = htons(PORT);
  99. break;
  100. case AF_INET6:
  101. saddr6 = (struct sockaddr_in6 *)&saddr;
  102. saddr6->sin6_family = AF_INET6;
  103. saddr6->sin6_addr = in6addr_any;
  104. saddr6->sin6_port = 0;
  105. daddr6 = (struct sockaddr_in6 *)&daddr;
  106. daddr6->sin6_family = AF_INET6;
  107. daddr6->sin6_addr = in6addr_loopback;
  108. daddr6->sin6_port = htons(PORT);
  109. break;
  110. default:
  111. error(1, 0, "Unsupported family %d", family);
  112. }
  113. memset(&cpu_set, 0, sizeof(cpu_set));
  114. CPU_SET(cpu_id, &cpu_set);
  115. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
  116. error(1, errno, "failed to pin to cpu");
  117. fd = socket(family, proto, 0);
  118. if (fd < 0)
  119. error(1, errno, "failed to create send socket");
  120. if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
  121. error(1, errno, "failed to bind send socket");
  122. if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr)))
  123. error(1, errno, "failed to connect send socket");
  124. if (send(fd, "a", 1, 0) < 0)
  125. error(1, errno, "failed to send message");
  126. close(fd);
  127. }
  128. static
  129. void receive_on_cpu(int *rcv_fd, int len, int epfd, int cpu_id, int proto)
  130. {
  131. struct epoll_event ev;
  132. int i, fd;
  133. char buf[8];
  134. i = epoll_wait(epfd, &ev, 1, -1);
  135. if (i < 0)
  136. error(1, errno, "epoll_wait failed");
  137. if (proto == SOCK_STREAM) {
  138. fd = accept(ev.data.fd, NULL, NULL);
  139. if (fd < 0)
  140. error(1, errno, "failed to accept");
  141. i = recv(fd, buf, sizeof(buf), 0);
  142. close(fd);
  143. } else {
  144. i = recv(ev.data.fd, buf, sizeof(buf), 0);
  145. }
  146. if (i < 0)
  147. error(1, errno, "failed to recv");
  148. for (i = 0; i < len; ++i)
  149. if (ev.data.fd == rcv_fd[i])
  150. break;
  151. if (i == len)
  152. error(1, 0, "failed to find socket");
  153. fprintf(stderr, "send cpu %d, receive socket %d\n", cpu_id, i);
  154. if (cpu_id != i)
  155. error(1, 0, "cpu id/receive socket mismatch");
  156. }
  157. static void test(int *rcv_fd, int len, int family, int proto)
  158. {
  159. struct epoll_event ev;
  160. int epfd, cpu;
  161. build_rcv_group(rcv_fd, len, family, proto);
  162. attach_bpf(rcv_fd[0]);
  163. epfd = epoll_create(1);
  164. if (epfd < 0)
  165. error(1, errno, "failed to create epoll");
  166. for (cpu = 0; cpu < len; ++cpu) {
  167. ev.events = EPOLLIN;
  168. ev.data.fd = rcv_fd[cpu];
  169. if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fd[cpu], &ev))
  170. error(1, errno, "failed to register sock epoll");
  171. }
  172. /* Forward iterate */
  173. for (cpu = 0; cpu < len; ++cpu) {
  174. send_from_cpu(cpu, family, proto);
  175. receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
  176. }
  177. /* Reverse iterate */
  178. for (cpu = len - 1; cpu >= 0; --cpu) {
  179. send_from_cpu(cpu, family, proto);
  180. receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
  181. }
  182. /* Even cores */
  183. for (cpu = 0; cpu < len; cpu += 2) {
  184. send_from_cpu(cpu, family, proto);
  185. receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
  186. }
  187. /* Odd cores */
  188. for (cpu = 1; cpu < len; cpu += 2) {
  189. send_from_cpu(cpu, family, proto);
  190. receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
  191. }
  192. close(epfd);
  193. for (cpu = 0; cpu < len; ++cpu)
  194. close(rcv_fd[cpu]);
  195. }
  196. int main(void)
  197. {
  198. int *rcv_fd, cpus;
  199. cpus = sysconf(_SC_NPROCESSORS_ONLN);
  200. if (cpus <= 0)
  201. error(1, errno, "failed counting cpus");
  202. rcv_fd = calloc(cpus, sizeof(int));
  203. if (!rcv_fd)
  204. error(1, 0, "failed to allocate array");
  205. fprintf(stderr, "---- IPv4 UDP ----\n");
  206. test(rcv_fd, cpus, AF_INET, SOCK_DGRAM);
  207. fprintf(stderr, "---- IPv6 UDP ----\n");
  208. test(rcv_fd, cpus, AF_INET6, SOCK_DGRAM);
  209. fprintf(stderr, "---- IPv4 TCP ----\n");
  210. test(rcv_fd, cpus, AF_INET, SOCK_STREAM);
  211. fprintf(stderr, "---- IPv6 TCP ----\n");
  212. test(rcv_fd, cpus, AF_INET6, SOCK_STREAM);
  213. free(rcv_fd);
  214. fprintf(stderr, "SUCCESS\n");
  215. return 0;
  216. }