reuseport_bpf_cpu.c 6.5 KB

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