1 /* 2 * Test functionality of BPF filters with SO_REUSEPORT. Same test as 3 * in reuseport_bpf_cpu, only as one socket per NUMA node. 4 */ 5 6 #define _GNU_SOURCE 7 8 #include <arpa/inet.h> 9 #include <errno.h> 10 #include <error.h> 11 #include <linux/filter.h> 12 #include <linux/bpf.h> 13 #include <linux/in.h> 14 #include <linux/unistd.h> 15 #include <sched.h> 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <sys/epoll.h> 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <unistd.h> 23 #include <numa.h> 24 25 static const int PORT = 8888; 26 27 static void build_rcv_group(int *rcv_fd, size_t len, int family, int proto) 28 { 29 struct sockaddr_storage addr; 30 struct sockaddr_in *addr4; 31 struct sockaddr_in6 *addr6; 32 size_t i; 33 int opt; 34 35 switch (family) { 36 case AF_INET: 37 addr4 = (struct sockaddr_in *)&addr; 38 addr4->sin_family = AF_INET; 39 addr4->sin_addr.s_addr = htonl(INADDR_ANY); 40 addr4->sin_port = htons(PORT); 41 break; 42 case AF_INET6: 43 addr6 = (struct sockaddr_in6 *)&addr; 44 addr6->sin6_family = AF_INET6; 45 addr6->sin6_addr = in6addr_any; 46 addr6->sin6_port = htons(PORT); 47 break; 48 default: 49 error(1, 0, "Unsupported family %d", family); 50 } 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 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 62 if (bind(rcv_fd[i], (struct sockaddr *)&addr, sizeof(addr))) 63 error(1, errno, "failed to bind receive socket"); 64 65 if (proto == SOCK_STREAM && listen(rcv_fd[i], len * 10)) 66 error(1, errno, "failed to listen on receive port"); 67 } 68 } 69 70 static void attach_bpf(int fd) 71 { 72 static char bpf_log_buf[65536]; 73 static const char bpf_license[] = ""; 74 75 int bpf_fd; 76 const struct bpf_insn prog[] = { 77 /* R0 = bpf_get_numa_node_id() */ 78 { BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_numa_node_id }, 79 /* return R0 */ 80 { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 } 81 }; 82 union bpf_attr attr; 83 84 memset(&attr, 0, sizeof(attr)); 85 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; 86 attr.insn_cnt = sizeof(prog) / sizeof(prog[0]); 87 attr.insns = (unsigned long) &prog; 88 attr.license = (unsigned long) &bpf_license; 89 attr.log_buf = (unsigned long) &bpf_log_buf; 90 attr.log_size = sizeof(bpf_log_buf); 91 attr.log_level = 1; 92 93 bpf_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); 94 if (bpf_fd < 0) 95 error(1, errno, "ebpf error. log:\n%s\n", bpf_log_buf); 96 97 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd, 98 sizeof(bpf_fd))) 99 error(1, errno, "failed to set SO_ATTACH_REUSEPORT_EBPF"); 100 101 close(bpf_fd); 102 } 103 104 static void send_from_node(int node_id, int family, int proto) 105 { 106 struct sockaddr_storage saddr, daddr; 107 struct sockaddr_in *saddr4, *daddr4; 108 struct sockaddr_in6 *saddr6, *daddr6; 109 int fd; 110 111 switch (family) { 112 case AF_INET: 113 saddr4 = (struct sockaddr_in *)&saddr; 114 saddr4->sin_family = AF_INET; 115 saddr4->sin_addr.s_addr = htonl(INADDR_ANY); 116 saddr4->sin_port = 0; 117 118 daddr4 = (struct sockaddr_in *)&daddr; 119 daddr4->sin_family = AF_INET; 120 daddr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 121 daddr4->sin_port = htons(PORT); 122 break; 123 case AF_INET6: 124 saddr6 = (struct sockaddr_in6 *)&saddr; 125 saddr6->sin6_family = AF_INET6; 126 saddr6->sin6_addr = in6addr_any; 127 saddr6->sin6_port = 0; 128 129 daddr6 = (struct sockaddr_in6 *)&daddr; 130 daddr6->sin6_family = AF_INET6; 131 daddr6->sin6_addr = in6addr_loopback; 132 daddr6->sin6_port = htons(PORT); 133 break; 134 default: 135 error(1, 0, "Unsupported family %d", family); 136 } 137 138 if (numa_run_on_node(node_id) < 0) 139 error(1, errno, "failed to pin to node"); 140 141 fd = socket(family, proto, 0); 142 if (fd < 0) 143 error(1, errno, "failed to create send socket"); 144 145 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr))) 146 error(1, errno, "failed to bind send socket"); 147 148 if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr))) 149 error(1, errno, "failed to connect send socket"); 150 151 if (send(fd, "a", 1, 0) < 0) 152 error(1, errno, "failed to send message"); 153 154 close(fd); 155 } 156 157 static 158 void receive_on_node(int *rcv_fd, int len, int epfd, int node_id, int proto) 159 { 160 struct epoll_event ev; 161 int i, fd; 162 char buf[8]; 163 164 i = epoll_wait(epfd, &ev, 1, -1); 165 if (i < 0) 166 error(1, errno, "epoll_wait failed"); 167 168 if (proto == SOCK_STREAM) { 169 fd = accept(ev.data.fd, NULL, NULL); 170 if (fd < 0) 171 error(1, errno, "failed to accept"); 172 i = recv(fd, buf, sizeof(buf), 0); 173 close(fd); 174 } else { 175 i = recv(ev.data.fd, buf, sizeof(buf), 0); 176 } 177 178 if (i < 0) 179 error(1, errno, "failed to recv"); 180 181 for (i = 0; i < len; ++i) 182 if (ev.data.fd == rcv_fd[i]) 183 break; 184 if (i == len) 185 error(1, 0, "failed to find socket"); 186 fprintf(stderr, "send node %d, receive socket %d\n", node_id, i); 187 if (node_id != i) 188 error(1, 0, "node id/receive socket mismatch"); 189 } 190 191 static void test(int *rcv_fd, int len, int family, int proto) 192 { 193 struct epoll_event ev; 194 int epfd, node; 195 196 build_rcv_group(rcv_fd, len, family, proto); 197 attach_bpf(rcv_fd[0]); 198 199 epfd = epoll_create(1); 200 if (epfd < 0) 201 error(1, errno, "failed to create epoll"); 202 for (node = 0; node < len; ++node) { 203 ev.events = EPOLLIN; 204 ev.data.fd = rcv_fd[node]; 205 if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fd[node], &ev)) 206 error(1, errno, "failed to register sock epoll"); 207 } 208 209 /* Forward iterate */ 210 for (node = 0; node < len; ++node) { 211 send_from_node(node, family, proto); 212 receive_on_node(rcv_fd, len, epfd, node, proto); 213 } 214 215 /* Reverse iterate */ 216 for (node = len - 1; node >= 0; --node) { 217 send_from_node(node, family, proto); 218 receive_on_node(rcv_fd, len, epfd, node, proto); 219 } 220 221 close(epfd); 222 for (node = 0; node < len; ++node) 223 close(rcv_fd[node]); 224 } 225 226 int main(void) 227 { 228 int *rcv_fd, nodes; 229 230 if (numa_available() < 0) 231 error(1, errno, "no numa api support"); 232 233 nodes = numa_max_node() + 1; 234 235 rcv_fd = calloc(nodes, sizeof(int)); 236 if (!rcv_fd) 237 error(1, 0, "failed to allocate array"); 238 239 fprintf(stderr, "---- IPv4 UDP ----\n"); 240 test(rcv_fd, nodes, AF_INET, SOCK_DGRAM); 241 242 fprintf(stderr, "---- IPv6 UDP ----\n"); 243 test(rcv_fd, nodes, AF_INET6, SOCK_DGRAM); 244 245 fprintf(stderr, "---- IPv4 TCP ----\n"); 246 test(rcv_fd, nodes, AF_INET, SOCK_STREAM); 247 248 fprintf(stderr, "---- IPv6 TCP ----\n"); 249 test(rcv_fd, nodes, AF_INET6, SOCK_STREAM); 250 251 free(rcv_fd); 252 253 fprintf(stderr, "SUCCESS\n"); 254 return 0; 255 } 256