Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Test functionality of BPF filters with SO_REUSEPORT.  This program creates
0004  * an SO_REUSEPORT receiver group containing one socket per CPU core. It then
0005  * creates a BPF program that will select a socket from this group based
0006  * on the core id that receives the packet.  The sending code artificially
0007  * moves itself to run on different core ids and sends one message from
0008  * each core.  Since these packets are delivered over loopback, they should
0009  * arrive on the same core that sent them.  The receiving code then ensures
0010  * that the packet was received on the socket for the corresponding core id.
0011  * This entire process is done for several different core id permutations
0012  * and for each IPv4/IPv6 and TCP/UDP combination.
0013  */
0014 
0015 #define _GNU_SOURCE
0016 
0017 #include <arpa/inet.h>
0018 #include <errno.h>
0019 #include <error.h>
0020 #include <linux/filter.h>
0021 #include <linux/in.h>
0022 #include <linux/unistd.h>
0023 #include <sched.h>
0024 #include <stdio.h>
0025 #include <stdlib.h>
0026 #include <string.h>
0027 #include <sys/epoll.h>
0028 #include <sys/types.h>
0029 #include <sys/socket.h>
0030 #include <unistd.h>
0031 
0032 static const int PORT = 8888;
0033 
0034 static void build_rcv_group(int *rcv_fd, size_t len, int family, int proto)
0035 {
0036     struct sockaddr_storage addr;
0037     struct sockaddr_in  *addr4;
0038     struct sockaddr_in6 *addr6;
0039     size_t i;
0040     int opt;
0041 
0042     switch (family) {
0043     case AF_INET:
0044         addr4 = (struct sockaddr_in *)&addr;
0045         addr4->sin_family = AF_INET;
0046         addr4->sin_addr.s_addr = htonl(INADDR_ANY);
0047         addr4->sin_port = htons(PORT);
0048         break;
0049     case AF_INET6:
0050         addr6 = (struct sockaddr_in6 *)&addr;
0051         addr6->sin6_family = AF_INET6;
0052         addr6->sin6_addr = in6addr_any;
0053         addr6->sin6_port = htons(PORT);
0054         break;
0055     default:
0056         error(1, 0, "Unsupported family %d", family);
0057     }
0058 
0059     for (i = 0; i < len; ++i) {
0060         rcv_fd[i] = socket(family, proto, 0);
0061         if (rcv_fd[i] < 0)
0062             error(1, errno, "failed to create receive socket");
0063 
0064         opt = 1;
0065         if (setsockopt(rcv_fd[i], SOL_SOCKET, SO_REUSEPORT, &opt,
0066                    sizeof(opt)))
0067             error(1, errno, "failed to set SO_REUSEPORT");
0068 
0069         if (bind(rcv_fd[i], (struct sockaddr *)&addr, sizeof(addr)))
0070             error(1, errno, "failed to bind receive socket");
0071 
0072         if (proto == SOCK_STREAM && listen(rcv_fd[i], len * 10))
0073             error(1, errno, "failed to listen on receive port");
0074     }
0075 }
0076 
0077 static void attach_bpf(int fd)
0078 {
0079     struct sock_filter code[] = {
0080         /* A = raw_smp_processor_id() */
0081         { BPF_LD  | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF + SKF_AD_CPU },
0082         /* return A */
0083         { BPF_RET | BPF_A, 0, 0, 0 },
0084     };
0085     struct sock_fprog p = {
0086         .len = 2,
0087         .filter = code,
0088     };
0089 
0090     if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)))
0091         error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF");
0092 }
0093 
0094 static void send_from_cpu(int cpu_id, int family, int proto)
0095 {
0096     struct sockaddr_storage saddr, daddr;
0097     struct sockaddr_in  *saddr4, *daddr4;
0098     struct sockaddr_in6 *saddr6, *daddr6;
0099     cpu_set_t cpu_set;
0100     int fd;
0101 
0102     switch (family) {
0103     case AF_INET:
0104         saddr4 = (struct sockaddr_in *)&saddr;
0105         saddr4->sin_family = AF_INET;
0106         saddr4->sin_addr.s_addr = htonl(INADDR_ANY);
0107         saddr4->sin_port = 0;
0108 
0109         daddr4 = (struct sockaddr_in *)&daddr;
0110         daddr4->sin_family = AF_INET;
0111         daddr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
0112         daddr4->sin_port = htons(PORT);
0113         break;
0114     case AF_INET6:
0115         saddr6 = (struct sockaddr_in6 *)&saddr;
0116         saddr6->sin6_family = AF_INET6;
0117         saddr6->sin6_addr = in6addr_any;
0118         saddr6->sin6_port = 0;
0119 
0120         daddr6 = (struct sockaddr_in6 *)&daddr;
0121         daddr6->sin6_family = AF_INET6;
0122         daddr6->sin6_addr = in6addr_loopback;
0123         daddr6->sin6_port = htons(PORT);
0124         break;
0125     default:
0126         error(1, 0, "Unsupported family %d", family);
0127     }
0128 
0129     memset(&cpu_set, 0, sizeof(cpu_set));
0130     CPU_SET(cpu_id, &cpu_set);
0131     if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
0132         error(1, errno, "failed to pin to cpu");
0133 
0134     fd = socket(family, proto, 0);
0135     if (fd < 0)
0136         error(1, errno, "failed to create send socket");
0137 
0138     if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
0139         error(1, errno, "failed to bind send socket");
0140 
0141     if (connect(fd, (struct sockaddr *)&daddr, sizeof(daddr)))
0142         error(1, errno, "failed to connect send socket");
0143 
0144     if (send(fd, "a", 1, 0) < 0)
0145         error(1, errno, "failed to send message");
0146 
0147     close(fd);
0148 }
0149 
0150 static
0151 void receive_on_cpu(int *rcv_fd, int len, int epfd, int cpu_id, int proto)
0152 {
0153     struct epoll_event ev;
0154     int i, fd;
0155     char buf[8];
0156 
0157     i = epoll_wait(epfd, &ev, 1, -1);
0158     if (i < 0)
0159         error(1, errno, "epoll_wait failed");
0160 
0161     if (proto == SOCK_STREAM) {
0162         fd = accept(ev.data.fd, NULL, NULL);
0163         if (fd < 0)
0164             error(1, errno, "failed to accept");
0165         i = recv(fd, buf, sizeof(buf), 0);
0166         close(fd);
0167     } else {
0168         i = recv(ev.data.fd, buf, sizeof(buf), 0);
0169     }
0170 
0171     if (i < 0)
0172         error(1, errno, "failed to recv");
0173 
0174     for (i = 0; i < len; ++i)
0175         if (ev.data.fd == rcv_fd[i])
0176             break;
0177     if (i == len)
0178         error(1, 0, "failed to find socket");
0179     fprintf(stderr, "send cpu %d, receive socket %d\n", cpu_id, i);
0180     if (cpu_id != i)
0181         error(1, 0, "cpu id/receive socket mismatch");
0182 }
0183 
0184 static void test(int *rcv_fd, int len, int family, int proto)
0185 {
0186     struct epoll_event ev;
0187     int epfd, cpu;
0188 
0189     build_rcv_group(rcv_fd, len, family, proto);
0190     attach_bpf(rcv_fd[0]);
0191 
0192     epfd = epoll_create(1);
0193     if (epfd < 0)
0194         error(1, errno, "failed to create epoll");
0195     for (cpu = 0; cpu < len; ++cpu) {
0196         ev.events = EPOLLIN;
0197         ev.data.fd = rcv_fd[cpu];
0198         if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fd[cpu], &ev))
0199             error(1, errno, "failed to register sock epoll");
0200     }
0201 
0202     /* Forward iterate */
0203     for (cpu = 0; cpu < len; ++cpu) {
0204         send_from_cpu(cpu, family, proto);
0205         receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
0206     }
0207 
0208     /* Reverse iterate */
0209     for (cpu = len - 1; cpu >= 0; --cpu) {
0210         send_from_cpu(cpu, family, proto);
0211         receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
0212     }
0213 
0214     /* Even cores */
0215     for (cpu = 0; cpu < len; cpu += 2) {
0216         send_from_cpu(cpu, family, proto);
0217         receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
0218     }
0219 
0220     /* Odd cores */
0221     for (cpu = 1; cpu < len; cpu += 2) {
0222         send_from_cpu(cpu, family, proto);
0223         receive_on_cpu(rcv_fd, len, epfd, cpu, proto);
0224     }
0225 
0226     close(epfd);
0227     for (cpu = 0; cpu < len; ++cpu)
0228         close(rcv_fd[cpu]);
0229 }
0230 
0231 int main(void)
0232 {
0233     int *rcv_fd, cpus;
0234 
0235     cpus = sysconf(_SC_NPROCESSORS_ONLN);
0236     if (cpus <= 0)
0237         error(1, errno, "failed counting cpus");
0238 
0239     rcv_fd = calloc(cpus, sizeof(int));
0240     if (!rcv_fd)
0241         error(1, 0, "failed to allocate array");
0242 
0243     fprintf(stderr, "---- IPv4 UDP ----\n");
0244     test(rcv_fd, cpus, AF_INET, SOCK_DGRAM);
0245 
0246     fprintf(stderr, "---- IPv6 UDP ----\n");
0247     test(rcv_fd, cpus, AF_INET6, SOCK_DGRAM);
0248 
0249     fprintf(stderr, "---- IPv4 TCP ----\n");
0250     test(rcv_fd, cpus, AF_INET, SOCK_STREAM);
0251 
0252     fprintf(stderr, "---- IPv6 TCP ----\n");
0253     test(rcv_fd, cpus, AF_INET6, SOCK_STREAM);
0254 
0255     free(rcv_fd);
0256 
0257     fprintf(stderr, "SUCCESS\n");
0258     return 0;
0259 }