Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2013 Google Inc.
0004  * Author: Willem de Bruijn (willemb@google.com)
0005  *
0006  * A basic test of packet socket fanout behavior.
0007  *
0008  * Control:
0009  * - create fanout fails as expected with illegal flag combinations
0010  * - join   fanout fails as expected with diverging types or flags
0011  *
0012  * Datapath:
0013  *   Open a pair of packet sockets and a pair of INET sockets, send a known
0014  *   number of packets across the two INET sockets and count the number of
0015  *   packets enqueued onto the two packet sockets.
0016  *
0017  *   The test currently runs for
0018  *   - PACKET_FANOUT_HASH
0019  *   - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
0020  *   - PACKET_FANOUT_LB
0021  *   - PACKET_FANOUT_CPU
0022  *   - PACKET_FANOUT_ROLLOVER
0023  *   - PACKET_FANOUT_CBPF
0024  *   - PACKET_FANOUT_EBPF
0025  *
0026  * Todo:
0027  * - functionality: PACKET_FANOUT_FLAG_DEFRAG
0028  */
0029 
0030 #define _GNU_SOURCE     /* for sched_setaffinity */
0031 
0032 #include <arpa/inet.h>
0033 #include <errno.h>
0034 #include <fcntl.h>
0035 #include <linux/unistd.h>   /* for __NR_bpf */
0036 #include <linux/filter.h>
0037 #include <linux/bpf.h>
0038 #include <linux/if_packet.h>
0039 #include <net/if.h>
0040 #include <net/ethernet.h>
0041 #include <netinet/ip.h>
0042 #include <netinet/udp.h>
0043 #include <poll.h>
0044 #include <sched.h>
0045 #include <stdint.h>
0046 #include <stdio.h>
0047 #include <stdlib.h>
0048 #include <string.h>
0049 #include <sys/mman.h>
0050 #include <sys/socket.h>
0051 #include <sys/stat.h>
0052 #include <sys/types.h>
0053 #include <unistd.h>
0054 
0055 #include "psock_lib.h"
0056 #include "../kselftest.h"
0057 
0058 #define RING_NUM_FRAMES         20
0059 
0060 static uint32_t cfg_max_num_members;
0061 
0062 /* Open a socket in a given fanout mode.
0063  * @return -1 if mode is bad, a valid socket otherwise */
0064 static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
0065 {
0066     struct sockaddr_ll addr = {0};
0067     struct fanout_args args;
0068     int fd, val, err;
0069 
0070     fd = socket(PF_PACKET, SOCK_RAW, 0);
0071     if (fd < 0) {
0072         perror("socket packet");
0073         exit(1);
0074     }
0075 
0076     pair_udp_setfilter(fd);
0077 
0078     addr.sll_family = AF_PACKET;
0079     addr.sll_protocol = htons(ETH_P_IP);
0080     addr.sll_ifindex = if_nametoindex("lo");
0081     if (addr.sll_ifindex == 0) {
0082         perror("if_nametoindex");
0083         exit(1);
0084     }
0085     if (bind(fd, (void *) &addr, sizeof(addr))) {
0086         perror("bind packet");
0087         exit(1);
0088     }
0089 
0090     if (cfg_max_num_members) {
0091         args.id = group_id;
0092         args.type_flags = typeflags;
0093         args.max_num_members = cfg_max_num_members;
0094         err = setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args,
0095                  sizeof(args));
0096     } else {
0097         val = (((int) typeflags) << 16) | group_id;
0098         err = setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val,
0099                  sizeof(val));
0100     }
0101     if (err) {
0102         if (close(fd)) {
0103             perror("close packet");
0104             exit(1);
0105         }
0106         return -1;
0107     }
0108 
0109     return fd;
0110 }
0111 
0112 static void sock_fanout_set_cbpf(int fd)
0113 {
0114     struct sock_filter bpf_filter[] = {
0115         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 80),       /* ldb [80] */
0116         BPF_STMT(BPF_RET | BPF_A, 0),             /* ret A */
0117     };
0118     struct sock_fprog bpf_prog;
0119 
0120     bpf_prog.filter = bpf_filter;
0121     bpf_prog.len = ARRAY_SIZE(bpf_filter);
0122 
0123     if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
0124                sizeof(bpf_prog))) {
0125         perror("fanout data cbpf");
0126         exit(1);
0127     }
0128 }
0129 
0130 static void sock_fanout_getopts(int fd, uint16_t *typeflags, uint16_t *group_id)
0131 {
0132     int sockopt;
0133     socklen_t sockopt_len = sizeof(sockopt);
0134 
0135     if (getsockopt(fd, SOL_PACKET, PACKET_FANOUT,
0136                &sockopt, &sockopt_len)) {
0137         perror("failed to getsockopt");
0138         exit(1);
0139     }
0140     *typeflags = sockopt >> 16;
0141     *group_id = sockopt & 0xfffff;
0142 }
0143 
0144 static void sock_fanout_set_ebpf(int fd)
0145 {
0146     static char log_buf[65536];
0147 
0148     const int len_off = __builtin_offsetof(struct __sk_buff, len);
0149     struct bpf_insn prog[] = {
0150         { BPF_ALU64 | BPF_MOV | BPF_X,   6, 1, 0, 0 },
0151         { BPF_LDX   | BPF_W   | BPF_MEM, 0, 6, len_off, 0 },
0152         { BPF_JMP   | BPF_JGE | BPF_K,   0, 0, 1, DATA_LEN },
0153         { BPF_JMP   | BPF_JA  | BPF_K,   0, 0, 4, 0 },
0154         { BPF_LD    | BPF_B   | BPF_ABS, 0, 0, 0, 0x50 },
0155         { BPF_JMP   | BPF_JEQ | BPF_K,   0, 0, 2, DATA_CHAR },
0156         { BPF_JMP   | BPF_JEQ | BPF_K,   0, 0, 1, DATA_CHAR_1 },
0157         { BPF_ALU   | BPF_MOV | BPF_K,   0, 0, 0, 0 },
0158         { BPF_JMP   | BPF_EXIT,          0, 0, 0, 0 }
0159     };
0160     union bpf_attr attr;
0161     int pfd;
0162 
0163     memset(&attr, 0, sizeof(attr));
0164     attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
0165     attr.insns = (unsigned long) prog;
0166     attr.insn_cnt = ARRAY_SIZE(prog);
0167     attr.license = (unsigned long) "GPL";
0168     attr.log_buf = (unsigned long) log_buf,
0169     attr.log_size = sizeof(log_buf),
0170     attr.log_level = 1,
0171 
0172     pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
0173     if (pfd < 0) {
0174         perror("bpf");
0175         fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
0176         exit(1);
0177     }
0178 
0179     if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
0180         perror("fanout data ebpf");
0181         exit(1);
0182     }
0183 
0184     if (close(pfd)) {
0185         perror("close ebpf");
0186         exit(1);
0187     }
0188 }
0189 
0190 static char *sock_fanout_open_ring(int fd)
0191 {
0192     struct tpacket_req req = {
0193         .tp_block_size = getpagesize(),
0194         .tp_frame_size = getpagesize(),
0195         .tp_block_nr   = RING_NUM_FRAMES,
0196         .tp_frame_nr   = RING_NUM_FRAMES,
0197     };
0198     char *ring;
0199     int val = TPACKET_V2;
0200 
0201     if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
0202                sizeof(val))) {
0203         perror("packetsock ring setsockopt version");
0204         exit(1);
0205     }
0206     if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
0207                sizeof(req))) {
0208         perror("packetsock ring setsockopt");
0209         exit(1);
0210     }
0211 
0212     ring = mmap(0, req.tp_block_size * req.tp_block_nr,
0213             PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
0214     if (ring == MAP_FAILED) {
0215         perror("packetsock ring mmap");
0216         exit(1);
0217     }
0218 
0219     return ring;
0220 }
0221 
0222 static int sock_fanout_read_ring(int fd, void *ring)
0223 {
0224     struct tpacket2_hdr *header = ring;
0225     int count = 0;
0226 
0227     while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
0228         count++;
0229         header = ring + (count * getpagesize());
0230     }
0231 
0232     return count;
0233 }
0234 
0235 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
0236 {
0237     int ret[2];
0238 
0239     ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
0240     ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
0241 
0242     fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
0243             ret[0], ret[1], expect[0], expect[1]);
0244 
0245     if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
0246         (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
0247         fprintf(stderr, "warning: incorrect queue lengths\n");
0248         return 1;
0249     }
0250 
0251     return 0;
0252 }
0253 
0254 /* Test illegal mode + flag combination */
0255 static void test_control_single(void)
0256 {
0257     fprintf(stderr, "test: control single socket\n");
0258 
0259     if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
0260                    PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
0261         fprintf(stderr, "ERROR: opened socket with dual rollover\n");
0262         exit(1);
0263     }
0264 }
0265 
0266 /* Test illegal group with different modes or flags */
0267 static void test_control_group(void)
0268 {
0269     int fds[2];
0270 
0271     fprintf(stderr, "test: control multiple sockets\n");
0272 
0273     fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
0274     if (fds[0] == -1) {
0275         fprintf(stderr, "ERROR: failed to open HASH socket\n");
0276         exit(1);
0277     }
0278     if (sock_fanout_open(PACKET_FANOUT_HASH |
0279                    PACKET_FANOUT_FLAG_DEFRAG, 0) != -1) {
0280         fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
0281         exit(1);
0282     }
0283     if (sock_fanout_open(PACKET_FANOUT_HASH |
0284                    PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
0285         fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
0286         exit(1);
0287     }
0288     if (sock_fanout_open(PACKET_FANOUT_CPU, 0) != -1) {
0289         fprintf(stderr, "ERROR: joined group with wrong mode\n");
0290         exit(1);
0291     }
0292     fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
0293     if (fds[1] == -1) {
0294         fprintf(stderr, "ERROR: failed to join group\n");
0295         exit(1);
0296     }
0297     if (close(fds[1]) || close(fds[0])) {
0298         fprintf(stderr, "ERROR: closing sockets\n");
0299         exit(1);
0300     }
0301 }
0302 
0303 /* Test illegal max_num_members values */
0304 static void test_control_group_max_num_members(void)
0305 {
0306     int fds[3];
0307 
0308     fprintf(stderr, "test: control multiple sockets, max_num_members\n");
0309 
0310     /* expected failure on greater than PACKET_FANOUT_MAX */
0311     cfg_max_num_members = (1 << 16) + 1;
0312     if (sock_fanout_open(PACKET_FANOUT_HASH, 0) != -1) {
0313         fprintf(stderr, "ERROR: max_num_members > PACKET_FANOUT_MAX\n");
0314         exit(1);
0315     }
0316 
0317     cfg_max_num_members = 256;
0318     fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
0319     if (fds[0] == -1) {
0320         fprintf(stderr, "ERROR: failed open\n");
0321         exit(1);
0322     }
0323 
0324     /* expected failure on joining group with different max_num_members */
0325     cfg_max_num_members = 257;
0326     if (sock_fanout_open(PACKET_FANOUT_HASH, 0) != -1) {
0327         fprintf(stderr, "ERROR: set different max_num_members\n");
0328         exit(1);
0329     }
0330 
0331     /* success on joining group with same max_num_members */
0332     cfg_max_num_members = 256;
0333     fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
0334     if (fds[1] == -1) {
0335         fprintf(stderr, "ERROR: failed to join group\n");
0336         exit(1);
0337     }
0338 
0339     /* success on joining group with max_num_members unspecified */
0340     cfg_max_num_members = 0;
0341     fds[2] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
0342     if (fds[2] == -1) {
0343         fprintf(stderr, "ERROR: failed to join group\n");
0344         exit(1);
0345     }
0346 
0347     if (close(fds[2]) || close(fds[1]) || close(fds[0])) {
0348         fprintf(stderr, "ERROR: closing sockets\n");
0349         exit(1);
0350     }
0351 }
0352 
0353 /* Test creating a unique fanout group ids */
0354 static void test_unique_fanout_group_ids(void)
0355 {
0356     int fds[3];
0357     uint16_t typeflags, first_group_id, second_group_id;
0358 
0359     fprintf(stderr, "test: unique ids\n");
0360 
0361     fds[0] = sock_fanout_open(PACKET_FANOUT_HASH |
0362                   PACKET_FANOUT_FLAG_UNIQUEID, 0);
0363     if (fds[0] == -1) {
0364         fprintf(stderr, "ERROR: failed to create a unique id group.\n");
0365         exit(1);
0366     }
0367 
0368     sock_fanout_getopts(fds[0], &typeflags, &first_group_id);
0369     if (typeflags != PACKET_FANOUT_HASH) {
0370         fprintf(stderr, "ERROR: unexpected typeflags %x\n", typeflags);
0371         exit(1);
0372     }
0373 
0374     if (sock_fanout_open(PACKET_FANOUT_CPU, first_group_id) != -1) {
0375         fprintf(stderr, "ERROR: joined group with wrong type.\n");
0376         exit(1);
0377     }
0378 
0379     fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, first_group_id);
0380     if (fds[1] == -1) {
0381         fprintf(stderr,
0382             "ERROR: failed to join previously created group.\n");
0383         exit(1);
0384     }
0385 
0386     fds[2] = sock_fanout_open(PACKET_FANOUT_HASH |
0387                   PACKET_FANOUT_FLAG_UNIQUEID, 0);
0388     if (fds[2] == -1) {
0389         fprintf(stderr,
0390             "ERROR: failed to create a second unique id group.\n");
0391         exit(1);
0392     }
0393 
0394     sock_fanout_getopts(fds[2], &typeflags, &second_group_id);
0395     if (sock_fanout_open(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_UNIQUEID,
0396                  second_group_id) != -1) {
0397         fprintf(stderr,
0398             "ERROR: specified a group id when requesting unique id\n");
0399         exit(1);
0400     }
0401 
0402     if (close(fds[0]) || close(fds[1]) || close(fds[2])) {
0403         fprintf(stderr, "ERROR: closing sockets\n");
0404         exit(1);
0405     }
0406 }
0407 
0408 static int test_datapath(uint16_t typeflags, int port_off,
0409              const int expect1[], const int expect2[])
0410 {
0411     const int expect0[] = { 0, 0 };
0412     char *rings[2];
0413     uint8_t type = typeflags & 0xFF;
0414     int fds[2], fds_udp[2][2], ret;
0415 
0416     fprintf(stderr, "\ntest: datapath 0x%hx ports %hu,%hu\n",
0417         typeflags, (uint16_t)PORT_BASE,
0418         (uint16_t)(PORT_BASE + port_off));
0419 
0420     fds[0] = sock_fanout_open(typeflags, 0);
0421     fds[1] = sock_fanout_open(typeflags, 0);
0422     if (fds[0] == -1 || fds[1] == -1) {
0423         fprintf(stderr, "ERROR: failed open\n");
0424         exit(1);
0425     }
0426     if (type == PACKET_FANOUT_CBPF)
0427         sock_fanout_set_cbpf(fds[0]);
0428     else if (type == PACKET_FANOUT_EBPF)
0429         sock_fanout_set_ebpf(fds[0]);
0430 
0431     rings[0] = sock_fanout_open_ring(fds[0]);
0432     rings[1] = sock_fanout_open_ring(fds[1]);
0433     pair_udp_open(fds_udp[0], PORT_BASE);
0434     pair_udp_open(fds_udp[1], PORT_BASE + port_off);
0435     sock_fanout_read(fds, rings, expect0);
0436 
0437     /* Send data, but not enough to overflow a queue */
0438     pair_udp_send(fds_udp[0], 15);
0439     pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
0440     ret = sock_fanout_read(fds, rings, expect1);
0441 
0442     /* Send more data, overflow the queue */
0443     pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
0444     /* TODO: ensure consistent order between expect1 and expect2 */
0445     ret |= sock_fanout_read(fds, rings, expect2);
0446 
0447     if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
0448         munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
0449         fprintf(stderr, "close rings\n");
0450         exit(1);
0451     }
0452     if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
0453         close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
0454         close(fds[1]) || close(fds[0])) {
0455         fprintf(stderr, "close datapath\n");
0456         exit(1);
0457     }
0458 
0459     return ret;
0460 }
0461 
0462 static int set_cpuaffinity(int cpuid)
0463 {
0464     cpu_set_t mask;
0465 
0466     CPU_ZERO(&mask);
0467     CPU_SET(cpuid, &mask);
0468     if (sched_setaffinity(0, sizeof(mask), &mask)) {
0469         if (errno != EINVAL) {
0470             fprintf(stderr, "setaffinity %d\n", cpuid);
0471             exit(1);
0472         }
0473         return 1;
0474     }
0475 
0476     return 0;
0477 }
0478 
0479 int main(int argc, char **argv)
0480 {
0481     const int expect_hash[2][2] = { { 15, 5 },  { 20, 5 } };
0482     const int expect_hash_rb[2][2]  = { { 15, 5 },  { 20, 15 } };
0483     const int expect_lb[2][2]   = { { 10, 10 }, { 18, 17 } };
0484     const int expect_rb[2][2]   = { { 15, 5 },  { 20, 15 } };
0485     const int expect_cpu0[2][2] = { { 20, 0 },  { 20, 0 } };
0486     const int expect_cpu1[2][2] = { { 0, 20 },  { 0, 20 } };
0487     const int expect_bpf[2][2]  = { { 15, 5 },  { 15, 20 } };
0488     const int expect_uniqueid[2][2] = { { 20, 20},  { 20, 20 } };
0489     int port_off = 2, tries = 20, ret;
0490 
0491     test_control_single();
0492     test_control_group();
0493     test_control_group_max_num_members();
0494     test_unique_fanout_group_ids();
0495 
0496     /* PACKET_FANOUT_MAX */
0497     cfg_max_num_members = 1 << 16;
0498     /* find a set of ports that do not collide onto the same socket */
0499     ret = test_datapath(PACKET_FANOUT_HASH, port_off,
0500                 expect_hash[0], expect_hash[1]);
0501     while (ret) {
0502         fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
0503         ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
0504                     expect_hash[0], expect_hash[1]);
0505         if (!--tries) {
0506             fprintf(stderr, "too many collisions\n");
0507             return 1;
0508         }
0509     }
0510 
0511     ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
0512                  port_off, expect_hash_rb[0], expect_hash_rb[1]);
0513     ret |= test_datapath(PACKET_FANOUT_LB,
0514                  port_off, expect_lb[0], expect_lb[1]);
0515     ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
0516                  port_off, expect_rb[0], expect_rb[1]);
0517 
0518     ret |= test_datapath(PACKET_FANOUT_CBPF,
0519                  port_off, expect_bpf[0], expect_bpf[1]);
0520     ret |= test_datapath(PACKET_FANOUT_EBPF,
0521                  port_off, expect_bpf[0], expect_bpf[1]);
0522 
0523     set_cpuaffinity(0);
0524     ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
0525                  expect_cpu0[0], expect_cpu0[1]);
0526     if (!set_cpuaffinity(1))
0527         /* TODO: test that choice alternates with previous */
0528         ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
0529                      expect_cpu1[0], expect_cpu1[1]);
0530 
0531     ret |= test_datapath(PACKET_FANOUT_FLAG_UNIQUEID, port_off,
0532                  expect_uniqueid[0], expect_uniqueid[1]);
0533 
0534     if (ret)
0535         return 1;
0536 
0537     printf("OK. All tests passed\n");
0538     return 0;
0539 }