Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

soreuseport: BPF selection functional test for TCP

Unfortunately the existing test relied on packet payload in order to
map incoming packets to sockets. In order to get this to work with TCP,
TCP_FASTOPEN needed to be used.

Since the fast open path is slightly different than the standard TCP path,
I created a second test which sends to reuseport group members based
on receiving cpu core id. This will probably serve as a better
real-world example use as well.

Signed-off-by: Craig Gallek <kraig@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Craig Gallek and committed by
David S. Miller
4b2a6aed c125e80b

+370 -8
+1
tools/testing/selftests/net/.gitignore
··· 2 2 psock_fanout 3 3 psock_tpacket 4 4 reuseport_bpf 5 + reuseport_bpf_cpu
+1 -1
tools/testing/selftests/net/Makefile
··· 4 4 5 5 CFLAGS += -I../../../../usr/include/ 6 6 7 - NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf 7 + NET_PROGS = socket psock_fanout psock_tpacket reuseport_bpf reuseport_bpf_cpu 8 8 9 9 all: $(NET_PROGS) 10 10 %: %.c
+110 -7
tools/testing/selftests/net/reuseport_bpf.c
··· 9 9 10 10 #include <errno.h> 11 11 #include <error.h> 12 + #include <fcntl.h> 12 13 #include <linux/bpf.h> 13 14 #include <linux/filter.h> 14 15 #include <linux/unistd.h> 15 16 #include <netinet/in.h> 17 + #include <netinet/tcp.h> 16 18 #include <stdio.h> 17 19 #include <stdlib.h> 18 20 #include <string.h> ··· 171 169 if (bind(fd[i], addr, sockaddr_size())) 172 170 error(1, errno, "failed to bind recv socket %d", i); 173 171 174 - if (p.protocol == SOCK_STREAM) 172 + if (p.protocol == SOCK_STREAM) { 173 + opt = 4; 174 + if (setsockopt(fd[i], SOL_TCP, TCP_FASTOPEN, &opt, 175 + sizeof(opt))) 176 + error(1, errno, 177 + "failed to set TCP_FASTOPEN on %d", i); 175 178 if (listen(fd[i], p.recv_socks * 10)) 176 179 error(1, errno, "failed to listen on socket"); 180 + } 177 181 } 178 182 free(addr); 179 183 } ··· 197 189 198 190 if (bind(fd, saddr, sockaddr_size())) 199 191 error(1, errno, "failed to bind send socket"); 200 - if (connect(fd, daddr, sockaddr_size())) 201 - error(1, errno, "failed to connect"); 202 192 203 - if (send(fd, buf, len, 0) < 0) 193 + if (sendto(fd, buf, len, MSG_FASTOPEN, daddr, sockaddr_size()) < 0) 204 194 error(1, errno, "failed to send message"); 205 195 206 196 close(fd); ··· 266 260 } 267 261 } 268 262 269 - static void test_reuseport_ebpf(const struct test_params p) 263 + static void test_reuseport_ebpf(struct test_params p) 270 264 { 271 265 int i, fd[p.recv_socks]; 272 266 ··· 274 268 build_recv_group(p, fd, p.recv_socks, attach_ebpf); 275 269 test_recv_order(p, fd, p.recv_socks); 276 270 271 + p.send_port_min += p.recv_socks * 2; 277 272 fprintf(stderr, "Reprograming, testing mod %zd...\n", p.recv_socks / 2); 278 273 attach_ebpf(fd[0], p.recv_socks / 2); 279 274 test_recv_order(p, fd, p.recv_socks / 2); ··· 283 276 close(fd[i]); 284 277 } 285 278 286 - static void test_reuseport_cbpf(const struct test_params p) 279 + static void test_reuseport_cbpf(struct test_params p) 287 280 { 288 281 int i, fd[p.recv_socks]; 289 282 ··· 291 284 build_recv_group(p, fd, p.recv_socks, attach_cbpf); 292 285 test_recv_order(p, fd, p.recv_socks); 293 286 287 + p.send_port_min += p.recv_socks * 2; 294 288 fprintf(stderr, "Reprograming, testing mod %zd...\n", p.recv_socks / 2); 295 289 attach_cbpf(fd[0], p.recv_socks / 2); 296 290 test_recv_order(p, fd, p.recv_socks / 2); ··· 385 377 386 378 static void test_filter_without_bind(void) 387 379 { 388 - int fd1, fd2; 380 + int fd1, fd2, opt = 1; 389 381 390 382 fprintf(stderr, "Testing filter add without bind...\n"); 391 383 fd1 = socket(AF_INET, SOCK_DGRAM, 0); ··· 394 386 fd2 = socket(AF_INET, SOCK_DGRAM, 0); 395 387 if (fd2 < 0) 396 388 error(1, errno, "failed to create socket 2"); 389 + if (setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt))) 390 + error(1, errno, "failed to set SO_REUSEPORT on socket 1"); 391 + if (setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt))) 392 + error(1, errno, "failed to set SO_REUSEPORT on socket 2"); 397 393 398 394 attach_ebpf(fd1, 10); 399 395 attach_cbpf(fd2, 10); ··· 406 394 close(fd2); 407 395 } 408 396 397 + void enable_fastopen(void) 398 + { 399 + int fd = open("/proc/sys/net/ipv4/tcp_fastopen", 0); 400 + int rw_mask = 3; /* bit 1: client side; bit-2 server side */ 401 + int val, size; 402 + char buf[16]; 403 + 404 + if (fd < 0) 405 + error(1, errno, "Unable to open tcp_fastopen sysctl"); 406 + if (read(fd, buf, sizeof(buf)) <= 0) 407 + error(1, errno, "Unable to read tcp_fastopen sysctl"); 408 + val = atoi(buf); 409 + close(fd); 410 + 411 + if ((val & rw_mask) != rw_mask) { 412 + fd = open("/proc/sys/net/ipv4/tcp_fastopen", O_RDWR); 413 + if (fd < 0) 414 + error(1, errno, 415 + "Unable to open tcp_fastopen sysctl for writing"); 416 + val |= rw_mask; 417 + size = snprintf(buf, 16, "%d", val); 418 + if (write(fd, buf, size) <= 0) 419 + error(1, errno, "Unable to write tcp_fastopen sysctl"); 420 + close(fd); 421 + } 422 + } 409 423 410 424 int main(void) 411 425 { ··· 544 506 .recv_port = 8007, 545 507 .send_port_min = 9100}); 546 508 509 + /* TCP fastopen is required for the TCP tests */ 510 + enable_fastopen(); 511 + fprintf(stderr, "---- IPv4 TCP ----\n"); 512 + test_reuseport_ebpf((struct test_params) { 513 + .recv_family = AF_INET, 514 + .send_family = AF_INET, 515 + .protocol = SOCK_STREAM, 516 + .recv_socks = 10, 517 + .recv_port = 8008, 518 + .send_port_min = 9120}); 519 + test_reuseport_cbpf((struct test_params) { 520 + .recv_family = AF_INET, 521 + .send_family = AF_INET, 522 + .protocol = SOCK_STREAM, 523 + .recv_socks = 10, 524 + .recv_port = 8009, 525 + .send_port_min = 9160}); 526 + test_extra_filter((struct test_params) { 527 + .recv_family = AF_INET, 528 + .protocol = SOCK_STREAM, 529 + .recv_port = 8010}); 530 + test_filter_no_reuseport((struct test_params) { 531 + .recv_family = AF_INET, 532 + .protocol = SOCK_STREAM, 533 + .recv_port = 8011}); 534 + 535 + fprintf(stderr, "---- IPv6 TCP ----\n"); 536 + test_reuseport_ebpf((struct test_params) { 537 + .recv_family = AF_INET6, 538 + .send_family = AF_INET6, 539 + .protocol = SOCK_STREAM, 540 + .recv_socks = 10, 541 + .recv_port = 8012, 542 + .send_port_min = 9200}); 543 + test_reuseport_cbpf((struct test_params) { 544 + .recv_family = AF_INET6, 545 + .send_family = AF_INET6, 546 + .protocol = SOCK_STREAM, 547 + .recv_socks = 10, 548 + .recv_port = 8013, 549 + .send_port_min = 9240}); 550 + test_extra_filter((struct test_params) { 551 + .recv_family = AF_INET6, 552 + .protocol = SOCK_STREAM, 553 + .recv_port = 8014}); 554 + test_filter_no_reuseport((struct test_params) { 555 + .recv_family = AF_INET6, 556 + .protocol = SOCK_STREAM, 557 + .recv_port = 8015}); 558 + 559 + fprintf(stderr, "---- IPv6 TCP w/ mapped IPv4 ----\n"); 560 + test_reuseport_ebpf((struct test_params) { 561 + .recv_family = AF_INET6, 562 + .send_family = AF_INET, 563 + .protocol = SOCK_STREAM, 564 + .recv_socks = 10, 565 + .recv_port = 8016, 566 + .send_port_min = 9320}); 567 + test_reuseport_cbpf((struct test_params) { 568 + .recv_family = AF_INET6, 569 + .send_family = AF_INET, 570 + .protocol = SOCK_STREAM, 571 + .recv_socks = 10, 572 + .recv_port = 8017, 573 + .send_port_min = 9360}); 547 574 548 575 test_filter_without_bind(); 549 576
+258
tools/testing/selftests/net/reuseport_bpf_cpu.c
··· 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 + 14 + #define _GNU_SOURCE 15 + 16 + #include <arpa/inet.h> 17 + #include <errno.h> 18 + #include <error.h> 19 + #include <linux/filter.h> 20 + #include <linux/in.h> 21 + #include <linux/unistd.h> 22 + #include <sched.h> 23 + #include <stdio.h> 24 + #include <stdlib.h> 25 + #include <string.h> 26 + #include <sys/epoll.h> 27 + #include <sys/types.h> 28 + #include <sys/socket.h> 29 + #include <unistd.h> 30 + 31 + static const int PORT = 8888; 32 + 33 + static void build_rcv_group(int *rcv_fd, size_t len, int family, int proto) 34 + { 35 + struct sockaddr_storage addr; 36 + struct sockaddr_in *addr4; 37 + struct sockaddr_in6 *addr6; 38 + size_t i; 39 + int opt; 40 + 41 + switch (family) { 42 + case AF_INET: 43 + addr4 = (struct sockaddr_in *)&addr; 44 + addr4->sin_family = AF_INET; 45 + addr4->sin_addr.s_addr = htonl(INADDR_ANY); 46 + addr4->sin_port = htons(PORT); 47 + break; 48 + case AF_INET6: 49 + addr6 = (struct sockaddr_in6 *)&addr; 50 + addr6->sin6_family = AF_INET6; 51 + addr6->sin6_addr = in6addr_any; 52 + addr6->sin6_port = htons(PORT); 53 + break; 54 + default: 55 + error(1, 0, "Unsupported family %d", family); 56 + } 57 + 58 + for (i = 0; i < len; ++i) { 59 + rcv_fd[i] = socket(family, proto, 0); 60 + if (rcv_fd[i] < 0) 61 + error(1, errno, "failed to create receive socket"); 62 + 63 + opt = 1; 64 + if (setsockopt(rcv_fd[i], SOL_SOCKET, SO_REUSEPORT, &opt, 65 + sizeof(opt))) 66 + error(1, errno, "failed to set SO_REUSEPORT"); 67 + 68 + if (bind(rcv_fd[i], (struct sockaddr *)&addr, sizeof(addr))) 69 + error(1, errno, "failed to bind receive socket"); 70 + 71 + if (proto == SOCK_STREAM && listen(rcv_fd[i], len * 10)) 72 + error(1, errno, "failed to listen on receive port"); 73 + } 74 + } 75 + 76 + static void attach_bpf(int fd) 77 + { 78 + struct sock_filter code[] = { 79 + /* A = raw_smp_processor_id() */ 80 + { BPF_LD | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF + SKF_AD_CPU }, 81 + /* return A */ 82 + { BPF_RET | BPF_A, 0, 0, 0 }, 83 + }; 84 + struct sock_fprog p = { 85 + .len = 2, 86 + .filter = code, 87 + }; 88 + 89 + if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p))) 90 + error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF"); 91 + } 92 + 93 + static void send_from_cpu(int cpu_id, int family, int proto) 94 + { 95 + struct sockaddr_storage saddr, daddr; 96 + struct sockaddr_in *saddr4, *daddr4; 97 + struct sockaddr_in6 *saddr6, *daddr6; 98 + cpu_set_t cpu_set; 99 + int fd; 100 + 101 + switch (family) { 102 + case AF_INET: 103 + saddr4 = (struct sockaddr_in *)&saddr; 104 + saddr4->sin_family = AF_INET; 105 + saddr4->sin_addr.s_addr = htonl(INADDR_ANY); 106 + saddr4->sin_port = 0; 107 + 108 + daddr4 = (struct sockaddr_in *)&daddr; 109 + daddr4->sin_family = AF_INET; 110 + daddr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 111 + daddr4->sin_port = htons(PORT); 112 + break; 113 + case AF_INET6: 114 + saddr6 = (struct sockaddr_in6 *)&saddr; 115 + saddr6->sin6_family = AF_INET6; 116 + saddr6->sin6_addr = in6addr_any; 117 + saddr6->sin6_port = 0; 118 + 119 + daddr6 = (struct sockaddr_in6 *)&daddr; 120 + daddr6->sin6_family = AF_INET6; 121 + daddr6->sin6_addr = in6addr_loopback; 122 + daddr6->sin6_port = htons(PORT); 123 + break; 124 + default: 125 + error(1, 0, "Unsupported family %d", family); 126 + } 127 + 128 + memset(&cpu_set, 0, sizeof(cpu_set)); 129 + CPU_SET(cpu_id, &cpu_set); 130 + if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) 131 + error(1, errno, "failed to pin to cpu"); 132 + 133 + fd = socket(family, proto, 0); 134 + if (fd < 0) 135 + error(1, errno, "failed to create send socket"); 136 + 137 + if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr))) 138 + error(1, errno, "failed to bind send socket"); 139 + 140 + if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr))) 141 + error(1, errno, "failed to connect send socket"); 142 + 143 + if (send(fd, "a", 1, 0) < 0) 144 + error(1, errno, "failed to send message"); 145 + 146 + close(fd); 147 + } 148 + 149 + static 150 + void receive_on_cpu(int *rcv_fd, int len, int epfd, int cpu_id, int proto) 151 + { 152 + struct epoll_event ev; 153 + int i, fd; 154 + char buf[8]; 155 + 156 + i = epoll_wait(epfd, &ev, 1, -1); 157 + if (i < 0) 158 + error(1, errno, "epoll_wait failed"); 159 + 160 + if (proto == SOCK_STREAM) { 161 + fd = accept(ev.data.fd, NULL, NULL); 162 + if (fd < 0) 163 + error(1, errno, "failed to accept"); 164 + i = recv(fd, buf, sizeof(buf), 0); 165 + close(fd); 166 + } else { 167 + i = recv(ev.data.fd, buf, sizeof(buf), 0); 168 + } 169 + 170 + if (i < 0) 171 + error(1, errno, "failed to recv"); 172 + 173 + for (i = 0; i < len; ++i) 174 + if (ev.data.fd == rcv_fd[i]) 175 + break; 176 + if (i == len) 177 + error(1, 0, "failed to find socket"); 178 + fprintf(stderr, "send cpu %d, receive socket %d\n", cpu_id, i); 179 + if (cpu_id != i) 180 + error(1, 0, "cpu id/receive socket mismatch"); 181 + } 182 + 183 + static void test(int *rcv_fd, int len, int family, int proto) 184 + { 185 + struct epoll_event ev; 186 + int epfd, cpu; 187 + 188 + build_rcv_group(rcv_fd, len, family, proto); 189 + attach_bpf(rcv_fd[0]); 190 + 191 + epfd = epoll_create(1); 192 + if (epfd < 0) 193 + error(1, errno, "failed to create epoll"); 194 + for (cpu = 0; cpu < len; ++cpu) { 195 + ev.events = EPOLLIN; 196 + ev.data.fd = rcv_fd[cpu]; 197 + if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fd[cpu], &ev)) 198 + error(1, errno, "failed to register sock epoll"); 199 + } 200 + 201 + /* Forward iterate */ 202 + for (cpu = 0; cpu < len; ++cpu) { 203 + send_from_cpu(cpu, family, proto); 204 + receive_on_cpu(rcv_fd, len, epfd, cpu, proto); 205 + } 206 + 207 + /* Reverse iterate */ 208 + for (cpu = len - 1; cpu >= 0; --cpu) { 209 + send_from_cpu(cpu, family, proto); 210 + receive_on_cpu(rcv_fd, len, epfd, cpu, proto); 211 + } 212 + 213 + /* Even cores */ 214 + for (cpu = 0; cpu < len; cpu += 2) { 215 + send_from_cpu(cpu, family, proto); 216 + receive_on_cpu(rcv_fd, len, epfd, cpu, proto); 217 + } 218 + 219 + /* Odd cores */ 220 + for (cpu = 1; cpu < len; cpu += 2) { 221 + send_from_cpu(cpu, family, proto); 222 + receive_on_cpu(rcv_fd, len, epfd, cpu, proto); 223 + } 224 + 225 + close(epfd); 226 + for (cpu = 0; cpu < len; ++cpu) 227 + close(rcv_fd[cpu]); 228 + } 229 + 230 + int main(void) 231 + { 232 + int *rcv_fd, cpus; 233 + 234 + cpus = sysconf(_SC_NPROCESSORS_ONLN); 235 + if (cpus <= 0) 236 + error(1, errno, "failed counting cpus"); 237 + 238 + rcv_fd = calloc(cpus, sizeof(int)); 239 + if (!rcv_fd) 240 + error(1, 0, "failed to allocate array"); 241 + 242 + fprintf(stderr, "---- IPv4 UDP ----\n"); 243 + test(rcv_fd, cpus, AF_INET, SOCK_DGRAM); 244 + 245 + fprintf(stderr, "---- IPv6 UDP ----\n"); 246 + test(rcv_fd, cpus, AF_INET6, SOCK_DGRAM); 247 + 248 + fprintf(stderr, "---- IPv4 TCP ----\n"); 249 + test(rcv_fd, cpus, AF_INET, SOCK_STREAM); 250 + 251 + fprintf(stderr, "---- IPv6 TCP ----\n"); 252 + test(rcv_fd, cpus, AF_INET6, SOCK_STREAM); 253 + 254 + free(rcv_fd); 255 + 256 + fprintf(stderr, "SUCCESS\n"); 257 + return 0; 258 + }