1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2013 Google Inc.
4 * Author: Willem de Bruijn (willemb@google.com)
6 * A basic test of packet socket fanout behavior.
9 * - create fanout fails as expected with illegal flag combinations
10 * - join fanout fails as expected with diverging types or flags
13 * Open a pair of packet sockets and a pair of INET sockets, send a known
14 * number of packets across the two INET sockets and count the number of
15 * packets enqueued onto the two packet sockets.
17 * The test currently runs for
18 * - PACKET_FANOUT_HASH
19 * - PACKET_FANOUT_HASH with PACKET_FANOUT_FLAG_ROLLOVER
22 * - PACKET_FANOUT_ROLLOVER
23 * - PACKET_FANOUT_CBPF
24 * - PACKET_FANOUT_EBPF
27 * - functionality: PACKET_FANOUT_FLAG_DEFRAG
30 #define _GNU_SOURCE /* for sched_setaffinity */
32 #include <arpa/inet.h>
35 #include <linux/unistd.h> /* for __NR_bpf */
36 #include <linux/filter.h>
37 #include <linux/bpf.h>
38 #include <linux/if_packet.h>
40 #include <net/ethernet.h>
41 #include <netinet/ip.h>
42 #include <netinet/udp.h>
50 #include <sys/socket.h>
52 #include <sys/types.h>
55 #include "psock_lib.h"
57 #define RING_NUM_FRAMES 20
59 /* Open a socket in a given fanout mode.
60 * @return -1 if mode is bad, a valid socket otherwise */
61 static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
63 struct sockaddr_ll addr = {0};
66 fd = socket(PF_PACKET, SOCK_RAW, 0);
68 perror("socket packet");
72 pair_udp_setfilter(fd);
74 addr.sll_family = AF_PACKET;
75 addr.sll_protocol = htons(ETH_P_IP);
76 addr.sll_ifindex = if_nametoindex("lo");
77 if (addr.sll_ifindex == 0) {
78 perror("if_nametoindex");
81 if (bind(fd, (void *) &addr, sizeof(addr))) {
82 perror("bind packet");
86 val = (((int) typeflags) << 16) | group_id;
87 if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val))) {
89 perror("close packet");
98 static void sock_fanout_set_cbpf(int fd)
100 struct sock_filter bpf_filter[] = {
101 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 80), /* ldb [80] */
102 BPF_STMT(BPF_RET+BPF_A, 0), /* ret A */
104 struct sock_fprog bpf_prog;
106 bpf_prog.filter = bpf_filter;
107 bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
109 if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
111 perror("fanout data cbpf");
116 static void sock_fanout_getopts(int fd, uint16_t *typeflags, uint16_t *group_id)
119 socklen_t sockopt_len = sizeof(sockopt);
121 if (getsockopt(fd, SOL_PACKET, PACKET_FANOUT,
122 &sockopt, &sockopt_len)) {
123 perror("failed to getsockopt");
126 *typeflags = sockopt >> 16;
127 *group_id = sockopt & 0xfffff;
130 static void sock_fanout_set_ebpf(int fd)
132 static char log_buf[65536];
134 const int len_off = __builtin_offsetof(struct __sk_buff, len);
135 struct bpf_insn prog[] = {
136 { BPF_ALU64 | BPF_MOV | BPF_X, 6, 1, 0, 0 },
137 { BPF_LDX | BPF_W | BPF_MEM, 0, 6, len_off, 0 },
138 { BPF_JMP | BPF_JGE | BPF_K, 0, 0, 1, DATA_LEN },
139 { BPF_JMP | BPF_JA | BPF_K, 0, 0, 4, 0 },
140 { BPF_LD | BPF_B | BPF_ABS, 0, 0, 0, 0x50 },
141 { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 2, DATA_CHAR },
142 { BPF_JMP | BPF_JEQ | BPF_K, 0, 0, 1, DATA_CHAR_1 },
143 { BPF_ALU | BPF_MOV | BPF_K, 0, 0, 0, 0 },
144 { BPF_JMP | BPF_EXIT, 0, 0, 0, 0 }
149 memset(&attr, 0, sizeof(attr));
150 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
151 attr.insns = (unsigned long) prog;
152 attr.insn_cnt = sizeof(prog) / sizeof(prog[0]);
153 attr.license = (unsigned long) "GPL";
154 attr.log_buf = (unsigned long) log_buf,
155 attr.log_size = sizeof(log_buf),
158 pfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
161 fprintf(stderr, "bpf verifier:\n%s\n", log_buf);
165 if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &pfd, sizeof(pfd))) {
166 perror("fanout data ebpf");
171 perror("close ebpf");
176 static char *sock_fanout_open_ring(int fd)
178 struct tpacket_req req = {
179 .tp_block_size = getpagesize(),
180 .tp_frame_size = getpagesize(),
181 .tp_block_nr = RING_NUM_FRAMES,
182 .tp_frame_nr = RING_NUM_FRAMES,
185 int val = TPACKET_V2;
187 if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, (void *) &val,
189 perror("packetsock ring setsockopt version");
192 if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req,
194 perror("packetsock ring setsockopt");
198 ring = mmap(0, req.tp_block_size * req.tp_block_nr,
199 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
200 if (ring == MAP_FAILED) {
201 perror("packetsock ring mmap");
208 static int sock_fanout_read_ring(int fd, void *ring)
210 struct tpacket2_hdr *header = ring;
213 while (count < RING_NUM_FRAMES && header->tp_status & TP_STATUS_USER) {
215 header = ring + (count * getpagesize());
221 static int sock_fanout_read(int fds[], char *rings[], const int expect[])
225 ret[0] = sock_fanout_read_ring(fds[0], rings[0]);
226 ret[1] = sock_fanout_read_ring(fds[1], rings[1]);
228 fprintf(stderr, "info: count=%d,%d, expect=%d,%d\n",
229 ret[0], ret[1], expect[0], expect[1]);
231 if ((!(ret[0] == expect[0] && ret[1] == expect[1])) &&
232 (!(ret[0] == expect[1] && ret[1] == expect[0]))) {
233 fprintf(stderr, "warning: incorrect queue lengths\n");
240 /* Test illegal mode + flag combination */
241 static void test_control_single(void)
243 fprintf(stderr, "test: control single socket\n");
245 if (sock_fanout_open(PACKET_FANOUT_ROLLOVER |
246 PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
247 fprintf(stderr, "ERROR: opened socket with dual rollover\n");
252 /* Test illegal group with different modes or flags */
253 static void test_control_group(void)
257 fprintf(stderr, "test: control multiple sockets\n");
259 fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
261 fprintf(stderr, "ERROR: failed to open HASH socket\n");
264 if (sock_fanout_open(PACKET_FANOUT_HASH |
265 PACKET_FANOUT_FLAG_DEFRAG, 0) != -1) {
266 fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
269 if (sock_fanout_open(PACKET_FANOUT_HASH |
270 PACKET_FANOUT_FLAG_ROLLOVER, 0) != -1) {
271 fprintf(stderr, "ERROR: joined group with wrong flag ro\n");
274 if (sock_fanout_open(PACKET_FANOUT_CPU, 0) != -1) {
275 fprintf(stderr, "ERROR: joined group with wrong mode\n");
278 fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
280 fprintf(stderr, "ERROR: failed to join group\n");
283 if (close(fds[1]) || close(fds[0])) {
284 fprintf(stderr, "ERROR: closing sockets\n");
289 /* Test creating a unique fanout group ids */
290 static void test_unique_fanout_group_ids(void)
293 uint16_t typeflags, first_group_id, second_group_id;
295 fprintf(stderr, "test: unique ids\n");
297 fds[0] = sock_fanout_open(PACKET_FANOUT_HASH |
298 PACKET_FANOUT_FLAG_UNIQUEID, 0);
300 fprintf(stderr, "ERROR: failed to create a unique id group.\n");
304 sock_fanout_getopts(fds[0], &typeflags, &first_group_id);
305 if (typeflags != PACKET_FANOUT_HASH) {
306 fprintf(stderr, "ERROR: unexpected typeflags %x\n", typeflags);
310 if (sock_fanout_open(PACKET_FANOUT_CPU, first_group_id) != -1) {
311 fprintf(stderr, "ERROR: joined group with wrong type.\n");
315 fds[1] = sock_fanout_open(PACKET_FANOUT_HASH, first_group_id);
318 "ERROR: failed to join previously created group.\n");
322 fds[2] = sock_fanout_open(PACKET_FANOUT_HASH |
323 PACKET_FANOUT_FLAG_UNIQUEID, 0);
326 "ERROR: failed to create a second unique id group.\n");
330 sock_fanout_getopts(fds[2], &typeflags, &second_group_id);
331 if (sock_fanout_open(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_UNIQUEID,
332 second_group_id) != -1) {
334 "ERROR: specified a group id when requesting unique id\n");
338 if (close(fds[0]) || close(fds[1]) || close(fds[2])) {
339 fprintf(stderr, "ERROR: closing sockets\n");
344 static int test_datapath(uint16_t typeflags, int port_off,
345 const int expect1[], const int expect2[])
347 const int expect0[] = { 0, 0 };
349 uint8_t type = typeflags & 0xFF;
350 int fds[2], fds_udp[2][2], ret;
352 fprintf(stderr, "\ntest: datapath 0x%hx ports %hu,%hu\n",
353 typeflags, (uint16_t)PORT_BASE,
354 (uint16_t)(PORT_BASE + port_off));
356 fds[0] = sock_fanout_open(typeflags, 0);
357 fds[1] = sock_fanout_open(typeflags, 0);
358 if (fds[0] == -1 || fds[1] == -1) {
359 fprintf(stderr, "ERROR: failed open\n");
362 if (type == PACKET_FANOUT_CBPF)
363 sock_fanout_set_cbpf(fds[0]);
364 else if (type == PACKET_FANOUT_EBPF)
365 sock_fanout_set_ebpf(fds[0]);
367 rings[0] = sock_fanout_open_ring(fds[0]);
368 rings[1] = sock_fanout_open_ring(fds[1]);
369 pair_udp_open(fds_udp[0], PORT_BASE);
370 pair_udp_open(fds_udp[1], PORT_BASE + port_off);
371 sock_fanout_read(fds, rings, expect0);
373 /* Send data, but not enough to overflow a queue */
374 pair_udp_send(fds_udp[0], 15);
375 pair_udp_send_char(fds_udp[1], 5, DATA_CHAR_1);
376 ret = sock_fanout_read(fds, rings, expect1);
378 /* Send more data, overflow the queue */
379 pair_udp_send_char(fds_udp[0], 15, DATA_CHAR_1);
380 /* TODO: ensure consistent order between expect1 and expect2 */
381 ret |= sock_fanout_read(fds, rings, expect2);
383 if (munmap(rings[1], RING_NUM_FRAMES * getpagesize()) ||
384 munmap(rings[0], RING_NUM_FRAMES * getpagesize())) {
385 fprintf(stderr, "close rings\n");
388 if (close(fds_udp[1][1]) || close(fds_udp[1][0]) ||
389 close(fds_udp[0][1]) || close(fds_udp[0][0]) ||
390 close(fds[1]) || close(fds[0])) {
391 fprintf(stderr, "close datapath\n");
398 static int set_cpuaffinity(int cpuid)
403 CPU_SET(cpuid, &mask);
404 if (sched_setaffinity(0, sizeof(mask), &mask)) {
405 if (errno != EINVAL) {
406 fprintf(stderr, "setaffinity %d\n", cpuid);
415 int main(int argc, char **argv)
417 const int expect_hash[2][2] = { { 15, 5 }, { 20, 5 } };
418 const int expect_hash_rb[2][2] = { { 15, 5 }, { 20, 15 } };
419 const int expect_lb[2][2] = { { 10, 10 }, { 18, 17 } };
420 const int expect_rb[2][2] = { { 15, 5 }, { 20, 15 } };
421 const int expect_cpu0[2][2] = { { 20, 0 }, { 20, 0 } };
422 const int expect_cpu1[2][2] = { { 0, 20 }, { 0, 20 } };
423 const int expect_bpf[2][2] = { { 15, 5 }, { 15, 20 } };
424 const int expect_uniqueid[2][2] = { { 20, 20}, { 20, 20 } };
425 int port_off = 2, tries = 20, ret;
427 test_control_single();
428 test_control_group();
429 test_unique_fanout_group_ids();
431 /* find a set of ports that do not collide onto the same socket */
432 ret = test_datapath(PACKET_FANOUT_HASH, port_off,
433 expect_hash[0], expect_hash[1]);
435 fprintf(stderr, "info: trying alternate ports (%d)\n", tries);
436 ret = test_datapath(PACKET_FANOUT_HASH, ++port_off,
437 expect_hash[0], expect_hash[1]);
439 fprintf(stderr, "too many collisions\n");
444 ret |= test_datapath(PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_ROLLOVER,
445 port_off, expect_hash_rb[0], expect_hash_rb[1]);
446 ret |= test_datapath(PACKET_FANOUT_LB,
447 port_off, expect_lb[0], expect_lb[1]);
448 ret |= test_datapath(PACKET_FANOUT_ROLLOVER,
449 port_off, expect_rb[0], expect_rb[1]);
451 ret |= test_datapath(PACKET_FANOUT_CBPF,
452 port_off, expect_bpf[0], expect_bpf[1]);
453 ret |= test_datapath(PACKET_FANOUT_EBPF,
454 port_off, expect_bpf[0], expect_bpf[1]);
457 ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
458 expect_cpu0[0], expect_cpu0[1]);
459 if (!set_cpuaffinity(1))
460 /* TODO: test that choice alternates with previous */
461 ret |= test_datapath(PACKET_FANOUT_CPU, port_off,
462 expect_cpu1[0], expect_cpu1[1]);
464 ret |= test_datapath(PACKET_FANOUT_FLAG_UNIQUEID, port_off,
465 expect_uniqueid[0], expect_uniqueid[1]);
470 printf("OK. All tests passed\n");