Path: blob/master/tools/testing/selftests/drivers/net/hw/toeplitz.c
122962 views
// SPDX-License-Identifier: GPL-2.01/* Toeplitz test2*3* 1. Read packets and their rx_hash using PF_PACKET/TPACKET_V34* 2. Compute the rx_hash in software based on the packet contents5* 3. Compare the two6*7* Optionally, either '-C $rx_irq_cpu_list' or '-r $rps_bitmap' may be given.8*9* If '-C $rx_irq_cpu_list' is given, also10*11* 4. Identify the cpu on which the packet arrived with PACKET_FANOUT_CPU12* 5. Compute the rxqueue that RSS would select based on this rx_hash13* 6. Using the $rx_irq_cpu_list map, identify the arriving cpu based on rxq irq14* 7. Compare the cpus from 4 and 615*16* Else if '-r $rps_bitmap' is given, also17*18* 4. Identify the cpu on which the packet arrived with PACKET_FANOUT_CPU19* 5. Compute the cpu that RPS should select based on rx_hash and $rps_bitmap20* 6. Compare the cpus from 4 and 521*/2223#define _GNU_SOURCE2425#include <arpa/inet.h>26#include <errno.h>27#include <error.h>28#include <fcntl.h>29#include <getopt.h>30#include <linux/filter.h>31#include <linux/if_ether.h>32#include <linux/if_packet.h>33#include <net/if.h>34#include <netdb.h>35#include <netinet/ip.h>36#include <netinet/ip6.h>37#include <netinet/tcp.h>38#include <netinet/udp.h>39#include <poll.h>40#include <stdbool.h>41#include <stddef.h>42#include <stdint.h>43#include <stdio.h>44#include <stdlib.h>45#include <string.h>46#include <sys/mman.h>47#include <sys/socket.h>48#include <sys/stat.h>49#include <sys/sysinfo.h>50#include <sys/time.h>51#include <sys/types.h>52#include <unistd.h>5354#include <ynl.h>55#include "ethtool-user.h"5657#include "kselftest.h"58#include "../../../net/lib/ksft.h"5960#define TOEPLITZ_KEY_MIN_LEN 4061#define TOEPLITZ_KEY_MAX_LEN 2566263#define TOEPLITZ_STR_LEN(K) (((K) * 3) - 1) /* hex encoded: AA:BB:CC:...:ZZ */64#define TOEPLITZ_STR_MIN_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MIN_LEN)65#define TOEPLITZ_STR_MAX_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MAX_LEN)6667#define FOUR_TUPLE_MAX_LEN ((sizeof(struct in6_addr) * 2) + (sizeof(uint16_t) * 2))6869#define RSS_MAX_CPUS (1 << 16) /* real constraint is PACKET_FANOUT_MAX */70#define RSS_MAX_INDIR (1 << 16)7172#define RPS_MAX_CPUS 16UL /* must be a power of 2 */7374#define MIN_PKT_SAMPLES 40 /* minimum number of packets to receive */7576/* configuration options (cmdline arguments) */77static uint16_t cfg_dport = 8000;78static int cfg_family = AF_INET6;79static char *cfg_ifname = "eth0";80static int cfg_num_queues;81static int cfg_num_rps_cpus;82static bool cfg_sink;83static int cfg_type = SOCK_STREAM;84static int cfg_timeout_msec = 1000;85static bool cfg_verbose;8687/* global vars */88static int num_cpus;89static int ring_block_nr;90static int ring_block_sz;9192/* stats */93static int frames_received;94static int frames_nohash;95static int frames_error;9697#define log_verbose(args...) do { if (cfg_verbose) fprintf(stderr, args); } while (0)9899/* tpacket ring */100struct ring_state {101int fd;102char *mmap;103int idx;104int cpu;105};106107static unsigned int rx_irq_cpus[RSS_MAX_CPUS]; /* map from rxq to cpu */108static int rps_silo_to_cpu[RPS_MAX_CPUS];109static unsigned char toeplitz_key[TOEPLITZ_KEY_MAX_LEN];110static unsigned int rss_indir_tbl[RSS_MAX_INDIR];111static unsigned int rss_indir_tbl_size;112static struct ring_state rings[RSS_MAX_CPUS];113114static inline uint32_t toeplitz(const unsigned char *four_tuple,115const unsigned char *key)116{117int i, bit, ret = 0;118uint32_t key32;119120key32 = ntohl(*((uint32_t *)key));121key += 4;122123for (i = 0; i < FOUR_TUPLE_MAX_LEN; i++) {124for (bit = 7; bit >= 0; bit--) {125if (four_tuple[i] & (1 << bit))126ret ^= key32;127128key32 <<= 1;129key32 |= !!(key[0] & (1 << bit));130}131key++;132}133134return ret;135}136137/* Compare computed cpu with arrival cpu from packet_fanout_cpu */138static void verify_rss(uint32_t rx_hash, int cpu)139{140int queue;141142if (rss_indir_tbl_size)143queue = rss_indir_tbl[rx_hash % rss_indir_tbl_size];144else145queue = rx_hash % cfg_num_queues;146147log_verbose(" rxq %d (cpu %d)", queue, rx_irq_cpus[queue]);148if (rx_irq_cpus[queue] != cpu) {149log_verbose(". error: rss cpu mismatch (%d)", cpu);150frames_error++;151}152}153154static void verify_rps(uint64_t rx_hash, int cpu)155{156int silo = (rx_hash * cfg_num_rps_cpus) >> 32;157158log_verbose(" silo %d (cpu %d)", silo, rps_silo_to_cpu[silo]);159if (rps_silo_to_cpu[silo] != cpu) {160log_verbose(". error: rps cpu mismatch (%d)", cpu);161frames_error++;162}163}164165static void log_rxhash(int cpu, uint32_t rx_hash,166const char *addrs, int addr_len)167{168char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];169uint16_t *ports;170171if (!inet_ntop(cfg_family, addrs, saddr, sizeof(saddr)) ||172!inet_ntop(cfg_family, addrs + addr_len, daddr, sizeof(daddr)))173error(1, 0, "address parse error");174175ports = (void *)addrs + (addr_len * 2);176log_verbose("cpu %d: rx_hash 0x%08x [saddr %s daddr %s sport %02hu dport %02hu]",177cpu, rx_hash, saddr, daddr,178ntohs(ports[0]), ntohs(ports[1]));179}180181/* Compare computed rxhash with rxhash received from tpacket_v3 */182static void verify_rxhash(const char *pkt, uint32_t rx_hash, int cpu)183{184unsigned char four_tuple[FOUR_TUPLE_MAX_LEN] = {0};185uint32_t rx_hash_sw;186const char *addrs;187int addr_len;188189if (cfg_family == AF_INET) {190addr_len = sizeof(struct in_addr);191addrs = pkt + offsetof(struct iphdr, saddr);192} else {193addr_len = sizeof(struct in6_addr);194addrs = pkt + offsetof(struct ip6_hdr, ip6_src);195}196197memcpy(four_tuple, addrs, (addr_len * 2) + (sizeof(uint16_t) * 2));198rx_hash_sw = toeplitz(four_tuple, toeplitz_key);199200if (cfg_verbose)201log_rxhash(cpu, rx_hash, addrs, addr_len);202203if (rx_hash != rx_hash_sw) {204log_verbose(" != expected 0x%x\n", rx_hash_sw);205frames_error++;206return;207}208209log_verbose(" OK");210if (cfg_num_queues)211verify_rss(rx_hash, cpu);212else if (cfg_num_rps_cpus)213verify_rps(rx_hash, cpu);214log_verbose("\n");215}216217static char *recv_frame(const struct ring_state *ring, char *frame)218{219struct tpacket3_hdr *hdr = (void *)frame;220221if (hdr->hv1.tp_rxhash)222verify_rxhash(frame + hdr->tp_net, hdr->hv1.tp_rxhash,223ring->cpu);224else225frames_nohash++;226227return frame + hdr->tp_next_offset;228}229230/* A single TPACKET_V3 block can hold multiple frames */231static bool recv_block(struct ring_state *ring)232{233struct tpacket_block_desc *block;234char *frame;235int i;236237block = (void *)(ring->mmap + ring->idx * ring_block_sz);238if (!(block->hdr.bh1.block_status & TP_STATUS_USER))239return false;240241frame = (char *)block;242frame += block->hdr.bh1.offset_to_first_pkt;243244for (i = 0; i < block->hdr.bh1.num_pkts; i++) {245frame = recv_frame(ring, frame);246frames_received++;247}248249block->hdr.bh1.block_status = TP_STATUS_KERNEL;250ring->idx = (ring->idx + 1) % ring_block_nr;251252return true;253}254255/* simple test: process all rings until MIN_PKT_SAMPLES packets are received,256* or the test times out.257*/258static void process_rings(void)259{260struct timeval start, now;261bool pkts_found = true;262long elapsed_usec;263int i;264265gettimeofday(&start, NULL);266267do {268if (!pkts_found)269usleep(100);270271pkts_found = false;272for (i = 0; i < num_cpus; i++)273pkts_found |= recv_block(&rings[i]);274275gettimeofday(&now, NULL);276elapsed_usec = (now.tv_sec - start.tv_sec) * 1000000 +277(now.tv_usec - start.tv_usec);278} while (frames_received - frames_nohash < MIN_PKT_SAMPLES &&279elapsed_usec < cfg_timeout_msec * 1000);280281fprintf(stderr, "count: pass=%u nohash=%u fail=%u\n",282frames_received - frames_nohash - frames_error,283frames_nohash, frames_error);284}285286static char *setup_ring(int fd)287{288struct tpacket_req3 req3 = {0};289void *ring;290291req3.tp_retire_blk_tov = cfg_timeout_msec / 8;292req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;293294req3.tp_frame_size = 2048;295req3.tp_frame_nr = 1 << 10;296req3.tp_block_nr = 16;297298req3.tp_block_size = req3.tp_frame_size * req3.tp_frame_nr;299req3.tp_block_size /= req3.tp_block_nr;300301if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &req3, sizeof(req3)))302error(1, errno, "setsockopt PACKET_RX_RING");303304ring_block_sz = req3.tp_block_size;305ring_block_nr = req3.tp_block_nr;306307ring = mmap(0, req3.tp_block_size * req3.tp_block_nr,308PROT_READ | PROT_WRITE,309MAP_SHARED | MAP_LOCKED | MAP_POPULATE, fd, 0);310if (ring == MAP_FAILED)311error(1, 0, "mmap failed");312313return ring;314}315316static void __set_filter(int fd, int off_proto, uint8_t proto, int off_dport)317{318struct sock_filter filter[] = {319BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),320BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),321BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_proto),322BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, proto, 0, 2),323BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),324BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_dport, 1, 0),325BPF_STMT(BPF_RET + BPF_K, 0),326BPF_STMT(BPF_RET + BPF_K, 0xFFFF),327};328struct sock_fprog prog = {};329330prog.filter = filter;331prog.len = ARRAY_SIZE(filter);332if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))333error(1, errno, "setsockopt filter");334}335336/* filter on transport protocol and destination port */337static void set_filter(int fd)338{339const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */340uint8_t proto;341342proto = cfg_type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;343if (cfg_family == AF_INET)344__set_filter(fd, offsetof(struct iphdr, protocol), proto,345sizeof(struct iphdr) + off_dport);346else347__set_filter(fd, offsetof(struct ip6_hdr, ip6_nxt), proto,348sizeof(struct ip6_hdr) + off_dport);349}350351/* drop everything: used temporarily during setup */352static void set_filter_null(int fd)353{354struct sock_filter filter[] = {355BPF_STMT(BPF_RET + BPF_K, 0),356};357struct sock_fprog prog = {};358359prog.filter = filter;360prog.len = ARRAY_SIZE(filter);361if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))362error(1, errno, "setsockopt filter");363}364365static int create_ring(char **ring)366{367struct fanout_args args = {368.id = 1,369.type_flags = PACKET_FANOUT_CPU,370.max_num_members = RSS_MAX_CPUS371};372struct sockaddr_ll ll = { 0 };373int fd, val;374375fd = socket(PF_PACKET, SOCK_DGRAM, 0);376if (fd == -1)377error(1, errno, "socket creation failed");378379val = TPACKET_V3;380if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val)))381error(1, errno, "setsockopt PACKET_VERSION");382*ring = setup_ring(fd);383384/* block packets until all rings are added to the fanout group:385* else packets can arrive during setup and get misclassified386*/387set_filter_null(fd);388389ll.sll_family = AF_PACKET;390ll.sll_ifindex = if_nametoindex(cfg_ifname);391ll.sll_protocol = cfg_family == AF_INET ? htons(ETH_P_IP) :392htons(ETH_P_IPV6);393if (bind(fd, (void *)&ll, sizeof(ll)))394error(1, errno, "bind");395396/* must come after bind: verifies all programs in group match */397if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args, sizeof(args))) {398/* on failure, retry using old API if that is sufficient:399* it has a hard limit of 256 sockets, so only try if400* (a) only testing rxhash, not RSS or (b) <= 256 cpus.401* in this API, the third argument is left implicit.402*/403if (cfg_num_queues || num_cpus > 256 ||404setsockopt(fd, SOL_PACKET, PACKET_FANOUT,405&args, sizeof(uint32_t)))406error(1, errno, "setsockopt PACKET_FANOUT cpu");407}408409return fd;410}411412/* setup inet(6) socket to blackhole the test traffic, if arg '-s' */413static int setup_sink(void)414{415int fd, val;416417fd = socket(cfg_family, cfg_type, 0);418if (fd == -1)419error(1, errno, "socket %d.%d", cfg_family, cfg_type);420421val = 1 << 20;422if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val)))423error(1, errno, "setsockopt rcvbuf");424425return fd;426}427428static void setup_rings(void)429{430int i;431432for (i = 0; i < num_cpus; i++) {433rings[i].cpu = i;434rings[i].fd = create_ring(&rings[i].mmap);435}436437/* accept packets once all rings in the fanout group are up */438for (i = 0; i < num_cpus; i++)439set_filter(rings[i].fd);440}441442static void cleanup_rings(void)443{444int i;445446for (i = 0; i < num_cpus; i++) {447if (munmap(rings[i].mmap, ring_block_nr * ring_block_sz))448error(1, errno, "munmap");449if (close(rings[i].fd))450error(1, errno, "close");451}452}453454static void parse_cpulist(const char *arg)455{456do {457rx_irq_cpus[cfg_num_queues++] = strtol(arg, NULL, 10);458459arg = strchr(arg, ',');460if (!arg)461break;462arg++; // skip ','463} while (1);464}465466static void show_cpulist(void)467{468int i;469470for (i = 0; i < cfg_num_queues; i++)471fprintf(stderr, "rxq %d: cpu %d\n", i, rx_irq_cpus[i]);472}473474static void show_silos(void)475{476int i;477478for (i = 0; i < cfg_num_rps_cpus; i++)479fprintf(stderr, "silo %d: cpu %d\n", i, rps_silo_to_cpu[i]);480}481482static void parse_toeplitz_key(const char *str, int slen, unsigned char *key)483{484int i, ret, off;485486if (slen < TOEPLITZ_STR_MIN_LEN ||487slen > TOEPLITZ_STR_MAX_LEN + 1)488error(1, 0, "invalid toeplitz key");489490for (i = 0, off = 0; off < slen; i++, off += 3) {491ret = sscanf(str + off, "%hhx", &key[i]);492if (ret != 1)493error(1, 0, "key parse error at %d off %d len %d",494i, off, slen);495}496}497498static void parse_rps_bitmap(const char *arg)499{500unsigned long bitmap;501int i;502503bitmap = strtoul(arg, NULL, 0);504505if (bitmap & ~((1UL << RPS_MAX_CPUS) - 1))506error(1, 0, "rps bitmap 0x%lx out of bounds, max cpu %lu",507bitmap, RPS_MAX_CPUS - 1);508509for (i = 0; i < RPS_MAX_CPUS; i++)510if (bitmap & 1UL << i)511rps_silo_to_cpu[cfg_num_rps_cpus++] = i;512}513514static void read_rss_dev_info_ynl(void)515{516struct ethtool_rss_get_req *req;517struct ethtool_rss_get_rsp *rsp;518struct ynl_sock *ys;519520ys = ynl_sock_create(&ynl_ethtool_family, NULL);521if (!ys)522error(1, errno, "ynl_sock_create failed");523524req = ethtool_rss_get_req_alloc();525if (!req)526error(1, errno, "ethtool_rss_get_req_alloc failed");527528ethtool_rss_get_req_set_header_dev_name(req, cfg_ifname);529530rsp = ethtool_rss_get(ys, req);531if (!rsp)532error(1, ys->err.code, "YNL: %s", ys->err.msg);533534if (!rsp->_len.hkey)535error(1, 0, "RSS key not available for %s", cfg_ifname);536537if (rsp->_len.hkey < TOEPLITZ_KEY_MIN_LEN ||538rsp->_len.hkey > TOEPLITZ_KEY_MAX_LEN)539error(1, 0, "RSS key length %u out of bounds [%u, %u]",540rsp->_len.hkey, TOEPLITZ_KEY_MIN_LEN,541TOEPLITZ_KEY_MAX_LEN);542543memcpy(toeplitz_key, rsp->hkey, rsp->_len.hkey);544545if (rsp->_count.indir > RSS_MAX_INDIR)546error(1, 0, "RSS indirection table too large (%u > %u)",547rsp->_count.indir, RSS_MAX_INDIR);548549/* If indir table not available we'll fallback to simple modulo math */550if (rsp->_count.indir) {551memcpy(rss_indir_tbl, rsp->indir,552rsp->_count.indir * sizeof(rss_indir_tbl[0]));553rss_indir_tbl_size = rsp->_count.indir;554555log_verbose("RSS indirection table size: %u\n",556rss_indir_tbl_size);557}558559ethtool_rss_get_rsp_free(rsp);560ethtool_rss_get_req_free(req);561ynl_sock_destroy(ys);562}563564static void parse_opts(int argc, char **argv)565{566static struct option long_options[] = {567{"dport", required_argument, 0, 'd'},568{"cpus", required_argument, 0, 'C'},569{"key", required_argument, 0, 'k'},570{"iface", required_argument, 0, 'i'},571{"ipv4", no_argument, 0, '4'},572{"ipv6", no_argument, 0, '6'},573{"sink", no_argument, 0, 's'},574{"tcp", no_argument, 0, 't'},575{"timeout", required_argument, 0, 'T'},576{"udp", no_argument, 0, 'u'},577{"verbose", no_argument, 0, 'v'},578{"rps", required_argument, 0, 'r'},579{0, 0, 0, 0}580};581bool have_toeplitz = false;582int index, c;583584while ((c = getopt_long(argc, argv, "46C:d:i:k:r:stT:uv", long_options, &index)) != -1) {585switch (c) {586case '4':587cfg_family = AF_INET;588break;589case '6':590cfg_family = AF_INET6;591break;592case 'C':593parse_cpulist(optarg);594break;595case 'd':596cfg_dport = strtol(optarg, NULL, 0);597break;598case 'i':599cfg_ifname = optarg;600break;601case 'k':602parse_toeplitz_key(optarg, strlen(optarg),603toeplitz_key);604have_toeplitz = true;605break;606case 'r':607parse_rps_bitmap(optarg);608break;609case 's':610cfg_sink = true;611break;612case 't':613cfg_type = SOCK_STREAM;614break;615case 'T':616cfg_timeout_msec = strtol(optarg, NULL, 0);617break;618case 'u':619cfg_type = SOCK_DGRAM;620break;621case 'v':622cfg_verbose = true;623break;624625default:626error(1, 0, "unknown option %c", optopt);627break;628}629}630631if (!have_toeplitz)632read_rss_dev_info_ynl();633634num_cpus = get_nprocs();635if (num_cpus > RSS_MAX_CPUS)636error(1, 0, "increase RSS_MAX_CPUS");637638if (cfg_num_queues && cfg_num_rps_cpus)639error(1, 0,640"Can't supply both RSS cpus ('-C') and RPS map ('-r')");641if (cfg_verbose) {642show_cpulist();643show_silos();644}645}646647int main(int argc, char **argv)648{649const int min_tests = 10;650int fd_sink = -1;651652parse_opts(argc, argv);653654if (cfg_sink)655fd_sink = setup_sink();656657setup_rings();658659/* Signal to test framework that we're ready to receive */660ksft_ready();661662process_rings();663cleanup_rings();664665if (cfg_sink && close(fd_sink))666error(1, errno, "close sink");667668if (frames_received - frames_nohash < min_tests)669error(1, 0, "too few frames for verification");670671return frames_error;672}673674675