1 // SPDX-License-Identifier: GPL-2.0
4 * 1. Read packets and their rx_hash using PF_PACKET/TPACKET_V3
5 * 2. Compute the rx_hash in software based on the packet contents
8 * Optionally, either '-C $rx_irq_cpu_list' or '-r $rps_bitmap' may be given.
10 * If '-C $rx_irq_cpu_list' is given, also
12 * 4. Identify the cpu on which the packet arrived with PACKET_FANOUT_CPU
13 * 5. Compute the rxqueue that RSS would select based on this rx_hash
14 * 6. Using the $rx_irq_cpu_list map, identify the arriving cpu based on rxq irq
15 * 7. Compare the cpus from 4 and 6
17 * Else if '-r $rps_bitmap' is given, also
19 * 4. Identify the cpu on which the packet arrived with PACKET_FANOUT_CPU
20 * 5. Compute the cpu that RPS should select based on rx_hash and $rps_bitmap
21 * 6. Compare the cpus from 4 and 5
26 #include <arpa/inet.h>
31 #include <linux/filter.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_packet.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip6.h>
38 #include <netinet/tcp.h>
39 #include <netinet/udp.h>
48 #include <sys/socket.h>
50 #include <sys/sysinfo.h>
52 #include <sys/types.h>
55 #define TOEPLITZ_KEY_MIN_LEN 40
56 #define TOEPLITZ_KEY_MAX_LEN 60
58 #define TOEPLITZ_STR_LEN(K) (((K) * 3) - 1) /* hex encoded: AA:BB:CC:...:ZZ */
59 #define TOEPLITZ_STR_MIN_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MIN_LEN)
60 #define TOEPLITZ_STR_MAX_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MAX_LEN)
62 #define FOUR_TUPLE_MAX_LEN ((sizeof(struct in6_addr) * 2) + (sizeof(uint16_t) * 2))
64 #define RSS_MAX_CPUS (1 << 16) /* real constraint is PACKET_FANOUT_MAX */
66 #define RPS_MAX_CPUS 16UL /* must be a power of 2 */
68 /* configuration options (cmdline arguments) */
69 static uint16_t cfg_dport = 8000;
70 static int cfg_family = AF_INET6;
71 static char *cfg_ifname = "eth0";
72 static int cfg_num_queues;
73 static int cfg_num_rps_cpus;
75 static int cfg_type = SOCK_STREAM;
76 static int cfg_timeout_msec = 1000;
77 static bool cfg_verbose;
81 static int ring_block_nr;
82 static int ring_block_sz;
85 static int frames_received;
86 static int frames_nohash;
87 static int frames_error;
89 #define log_verbose(args...) do { if (cfg_verbose) fprintf(stderr, args); } while (0)
99 static unsigned int rx_irq_cpus[RSS_MAX_CPUS]; /* map from rxq to cpu */
100 static int rps_silo_to_cpu[RPS_MAX_CPUS];
101 static unsigned char toeplitz_key[TOEPLITZ_KEY_MAX_LEN];
102 static struct ring_state rings[RSS_MAX_CPUS];
104 static inline uint32_t toeplitz(const unsigned char *four_tuple,
105 const unsigned char *key)
110 key32 = ntohl(*((uint32_t *)key));
113 for (i = 0; i < FOUR_TUPLE_MAX_LEN; i++) {
114 for (bit = 7; bit >= 0; bit--) {
115 if (four_tuple[i] & (1 << bit))
119 key32 |= !!(key[0] & (1 << bit));
127 /* Compare computed cpu with arrival cpu from packet_fanout_cpu */
128 static void verify_rss(uint32_t rx_hash, int cpu)
130 int queue = rx_hash % cfg_num_queues;
132 log_verbose(" rxq %d (cpu %d)", queue, rx_irq_cpus[queue]);
133 if (rx_irq_cpus[queue] != cpu) {
134 log_verbose(". error: rss cpu mismatch (%d)", cpu);
139 static void verify_rps(uint64_t rx_hash, int cpu)
141 int silo = (rx_hash * cfg_num_rps_cpus) >> 32;
143 log_verbose(" silo %d (cpu %d)", silo, rps_silo_to_cpu[silo]);
144 if (rps_silo_to_cpu[silo] != cpu) {
145 log_verbose(". error: rps cpu mismatch (%d)", cpu);
150 static void log_rxhash(int cpu, uint32_t rx_hash,
151 const char *addrs, int addr_len)
153 char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];
156 if (!inet_ntop(cfg_family, addrs, saddr, sizeof(saddr)) ||
157 !inet_ntop(cfg_family, addrs + addr_len, daddr, sizeof(daddr)))
158 error(1, 0, "address parse error");
160 ports = (void *)addrs + (addr_len * 2);
161 log_verbose("cpu %d: rx_hash 0x%08x [saddr %s daddr %s sport %02hu dport %02hu]",
162 cpu, rx_hash, saddr, daddr,
163 ntohs(ports[0]), ntohs(ports[1]));
166 /* Compare computed rxhash with rxhash received from tpacket_v3 */
167 static void verify_rxhash(const char *pkt, uint32_t rx_hash, int cpu)
169 unsigned char four_tuple[FOUR_TUPLE_MAX_LEN] = {0};
174 if (cfg_family == AF_INET) {
175 addr_len = sizeof(struct in_addr);
176 addrs = pkt + offsetof(struct iphdr, saddr);
178 addr_len = sizeof(struct in6_addr);
179 addrs = pkt + offsetof(struct ip6_hdr, ip6_src);
182 memcpy(four_tuple, addrs, (addr_len * 2) + (sizeof(uint16_t) * 2));
183 rx_hash_sw = toeplitz(four_tuple, toeplitz_key);
186 log_rxhash(cpu, rx_hash, addrs, addr_len);
188 if (rx_hash != rx_hash_sw) {
189 log_verbose(" != expected 0x%x\n", rx_hash_sw);
196 verify_rss(rx_hash, cpu);
197 else if (cfg_num_rps_cpus)
198 verify_rps(rx_hash, cpu);
202 static char *recv_frame(const struct ring_state *ring, char *frame)
204 struct tpacket3_hdr *hdr = (void *)frame;
206 if (hdr->hv1.tp_rxhash)
207 verify_rxhash(frame + hdr->tp_net, hdr->hv1.tp_rxhash,
212 return frame + hdr->tp_next_offset;
215 /* A single TPACKET_V3 block can hold multiple frames */
216 static void recv_block(struct ring_state *ring)
218 struct tpacket_block_desc *block;
222 block = (void *)(ring->mmap + ring->idx * ring_block_sz);
223 if (!(block->hdr.bh1.block_status & TP_STATUS_USER))
226 frame = (char *)block;
227 frame += block->hdr.bh1.offset_to_first_pkt;
229 for (i = 0; i < block->hdr.bh1.num_pkts; i++) {
230 frame = recv_frame(ring, frame);
234 block->hdr.bh1.block_status = TP_STATUS_KERNEL;
235 ring->idx = (ring->idx + 1) % ring_block_nr;
238 /* simple test: sleep once unconditionally and then process all rings */
239 static void process_rings(void)
243 usleep(1000 * cfg_timeout_msec);
245 for (i = 0; i < num_cpus; i++)
246 recv_block(&rings[i]);
248 fprintf(stderr, "count: pass=%u nohash=%u fail=%u\n",
249 frames_received - frames_nohash - frames_error,
250 frames_nohash, frames_error);
253 static char *setup_ring(int fd)
255 struct tpacket_req3 req3 = {0};
258 req3.tp_retire_blk_tov = cfg_timeout_msec;
259 req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
261 req3.tp_frame_size = 2048;
262 req3.tp_frame_nr = 1 << 10;
263 req3.tp_block_nr = 2;
265 req3.tp_block_size = req3.tp_frame_size * req3.tp_frame_nr;
266 req3.tp_block_size /= req3.tp_block_nr;
268 if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &req3, sizeof(req3)))
269 error(1, errno, "setsockopt PACKET_RX_RING");
271 ring_block_sz = req3.tp_block_size;
272 ring_block_nr = req3.tp_block_nr;
274 ring = mmap(0, req3.tp_block_size * req3.tp_block_nr,
275 PROT_READ | PROT_WRITE,
276 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, fd, 0);
277 if (ring == MAP_FAILED)
278 error(1, 0, "mmap failed");
283 static void __set_filter(int fd, int off_proto, uint8_t proto, int off_dport)
285 struct sock_filter filter[] = {
286 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),
287 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),
288 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_proto),
289 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, proto, 0, 2),
290 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),
291 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_dport, 1, 0),
292 BPF_STMT(BPF_RET + BPF_K, 0),
293 BPF_STMT(BPF_RET + BPF_K, 0xFFFF),
295 struct sock_fprog prog = {};
297 prog.filter = filter;
298 prog.len = sizeof(filter) / sizeof(struct sock_filter);
299 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
300 error(1, errno, "setsockopt filter");
303 /* filter on transport protocol and destination port */
304 static void set_filter(int fd)
306 const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */
309 proto = cfg_type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;
310 if (cfg_family == AF_INET)
311 __set_filter(fd, offsetof(struct iphdr, protocol), proto,
312 sizeof(struct iphdr) + off_dport);
314 __set_filter(fd, offsetof(struct ip6_hdr, ip6_nxt), proto,
315 sizeof(struct ip6_hdr) + off_dport);
318 /* drop everything: used temporarily during setup */
319 static void set_filter_null(int fd)
321 struct sock_filter filter[] = {
322 BPF_STMT(BPF_RET + BPF_K, 0),
324 struct sock_fprog prog = {};
326 prog.filter = filter;
327 prog.len = sizeof(filter) / sizeof(struct sock_filter);
328 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
329 error(1, errno, "setsockopt filter");
332 static int create_ring(char **ring)
334 struct fanout_args args = {
336 .type_flags = PACKET_FANOUT_CPU,
337 .max_num_members = RSS_MAX_CPUS
339 struct sockaddr_ll ll = { 0 };
342 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
344 error(1, errno, "socket creation failed");
347 if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val)))
348 error(1, errno, "setsockopt PACKET_VERSION");
349 *ring = setup_ring(fd);
351 /* block packets until all rings are added to the fanout group:
352 * else packets can arrive during setup and get misclassified
356 ll.sll_family = AF_PACKET;
357 ll.sll_ifindex = if_nametoindex(cfg_ifname);
358 ll.sll_protocol = cfg_family == AF_INET ? htons(ETH_P_IP) :
360 if (bind(fd, (void *)&ll, sizeof(ll)))
361 error(1, errno, "bind");
363 /* must come after bind: verifies all programs in group match */
364 if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args, sizeof(args))) {
365 /* on failure, retry using old API if that is sufficient:
366 * it has a hard limit of 256 sockets, so only try if
367 * (a) only testing rxhash, not RSS or (b) <= 256 cpus.
368 * in this API, the third argument is left implicit.
370 if (cfg_num_queues || num_cpus > 256 ||
371 setsockopt(fd, SOL_PACKET, PACKET_FANOUT,
372 &args, sizeof(uint32_t)))
373 error(1, errno, "setsockopt PACKET_FANOUT cpu");
379 /* setup inet(6) socket to blackhole the test traffic, if arg '-s' */
380 static int setup_sink(void)
384 fd = socket(cfg_family, cfg_type, 0);
386 error(1, errno, "socket %d.%d", cfg_family, cfg_type);
389 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val)))
390 error(1, errno, "setsockopt rcvbuf");
395 static void setup_rings(void)
399 for (i = 0; i < num_cpus; i++) {
401 rings[i].fd = create_ring(&rings[i].mmap);
404 /* accept packets once all rings in the fanout group are up */
405 for (i = 0; i < num_cpus; i++)
406 set_filter(rings[i].fd);
409 static void cleanup_rings(void)
413 for (i = 0; i < num_cpus; i++) {
414 if (munmap(rings[i].mmap, ring_block_nr * ring_block_sz))
415 error(1, errno, "munmap");
416 if (close(rings[i].fd))
417 error(1, errno, "close");
421 static void parse_cpulist(const char *arg)
424 rx_irq_cpus[cfg_num_queues++] = strtol(arg, NULL, 10);
426 arg = strchr(arg, ',');
433 static void show_cpulist(void)
437 for (i = 0; i < cfg_num_queues; i++)
438 fprintf(stderr, "rxq %d: cpu %d\n", i, rx_irq_cpus[i]);
441 static void show_silos(void)
445 for (i = 0; i < cfg_num_rps_cpus; i++)
446 fprintf(stderr, "silo %d: cpu %d\n", i, rps_silo_to_cpu[i]);
449 static void parse_toeplitz_key(const char *str, int slen, unsigned char *key)
453 if (slen < TOEPLITZ_STR_MIN_LEN ||
454 slen > TOEPLITZ_STR_MAX_LEN + 1)
455 error(1, 0, "invalid toeplitz key");
457 for (i = 0, off = 0; off < slen; i++, off += 3) {
458 ret = sscanf(str + off, "%hhx", &key[i]);
460 error(1, 0, "key parse error at %d off %d len %d",
465 static void parse_rps_bitmap(const char *arg)
467 unsigned long bitmap;
470 bitmap = strtoul(arg, NULL, 0);
472 if (bitmap & ~(RPS_MAX_CPUS - 1))
473 error(1, 0, "rps bitmap 0x%lx out of bounds 0..%lu",
474 bitmap, RPS_MAX_CPUS - 1);
476 for (i = 0; i < RPS_MAX_CPUS; i++)
477 if (bitmap & 1UL << i)
478 rps_silo_to_cpu[cfg_num_rps_cpus++] = i;
481 static void parse_opts(int argc, char **argv)
483 static struct option long_options[] = {
484 {"dport", required_argument, 0, 'd'},
485 {"cpus", required_argument, 0, 'C'},
486 {"key", required_argument, 0, 'k'},
487 {"iface", required_argument, 0, 'i'},
488 {"ipv4", no_argument, 0, '4'},
489 {"ipv6", no_argument, 0, '6'},
490 {"sink", no_argument, 0, 's'},
491 {"tcp", no_argument, 0, 't'},
492 {"timeout", required_argument, 0, 'T'},
493 {"udp", no_argument, 0, 'u'},
494 {"verbose", no_argument, 0, 'v'},
495 {"rps", required_argument, 0, 'r'},
498 bool have_toeplitz = false;
501 while ((c = getopt_long(argc, argv, "46C:d:i:k:r:stT:uv", long_options, &index)) != -1) {
504 cfg_family = AF_INET;
507 cfg_family = AF_INET6;
510 parse_cpulist(optarg);
513 cfg_dport = strtol(optarg, NULL, 0);
519 parse_toeplitz_key(optarg, strlen(optarg),
521 have_toeplitz = true;
524 parse_rps_bitmap(optarg);
530 cfg_type = SOCK_STREAM;
533 cfg_timeout_msec = strtol(optarg, NULL, 0);
536 cfg_type = SOCK_DGRAM;
543 error(1, 0, "unknown option %c", optopt);
549 error(1, 0, "Must supply rss key ('-k')");
551 num_cpus = get_nprocs();
552 if (num_cpus > RSS_MAX_CPUS)
553 error(1, 0, "increase RSS_MAX_CPUS");
555 if (cfg_num_queues && cfg_num_rps_cpus)
557 "Can't supply both RSS cpus ('-C') and RPS map ('-r')");
564 int main(int argc, char **argv)
566 const int min_tests = 10;
569 parse_opts(argc, argv);
572 fd_sink = setup_sink();
578 if (cfg_sink && close(fd_sink))
579 error(1, errno, "close sink");
581 if (frames_received - frames_nohash < min_tests)
582 error(1, 0, "too few frames for verification");