Path: blob/master/tools/testing/selftests/drivers/net/hw/toeplitz.c
49174 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 606263#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/* configuration options (cmdline arguments) */75static uint16_t cfg_dport = 8000;76static int cfg_family = AF_INET6;77static char *cfg_ifname = "eth0";78static int cfg_num_queues;79static int cfg_num_rps_cpus;80static bool cfg_sink;81static int cfg_type = SOCK_STREAM;82static int cfg_timeout_msec = 1000;83static bool cfg_verbose;8485/* global vars */86static int num_cpus;87static int ring_block_nr;88static int ring_block_sz;8990/* stats */91static int frames_received;92static int frames_nohash;93static int frames_error;9495#define log_verbose(args...) do { if (cfg_verbose) fprintf(stderr, args); } while (0)9697/* tpacket ring */98struct ring_state {99int fd;100char *mmap;101int idx;102int cpu;103};104105static unsigned int rx_irq_cpus[RSS_MAX_CPUS]; /* map from rxq to cpu */106static int rps_silo_to_cpu[RPS_MAX_CPUS];107static unsigned char toeplitz_key[TOEPLITZ_KEY_MAX_LEN];108static unsigned int rss_indir_tbl[RSS_MAX_INDIR];109static unsigned int rss_indir_tbl_size;110static struct ring_state rings[RSS_MAX_CPUS];111112static inline uint32_t toeplitz(const unsigned char *four_tuple,113const unsigned char *key)114{115int i, bit, ret = 0;116uint32_t key32;117118key32 = ntohl(*((uint32_t *)key));119key += 4;120121for (i = 0; i < FOUR_TUPLE_MAX_LEN; i++) {122for (bit = 7; bit >= 0; bit--) {123if (four_tuple[i] & (1 << bit))124ret ^= key32;125126key32 <<= 1;127key32 |= !!(key[0] & (1 << bit));128}129key++;130}131132return ret;133}134135/* Compare computed cpu with arrival cpu from packet_fanout_cpu */136static void verify_rss(uint32_t rx_hash, int cpu)137{138int queue;139140if (rss_indir_tbl_size)141queue = rss_indir_tbl[rx_hash % rss_indir_tbl_size];142else143queue = rx_hash % cfg_num_queues;144145log_verbose(" rxq %d (cpu %d)", queue, rx_irq_cpus[queue]);146if (rx_irq_cpus[queue] != cpu) {147log_verbose(". error: rss cpu mismatch (%d)", cpu);148frames_error++;149}150}151152static void verify_rps(uint64_t rx_hash, int cpu)153{154int silo = (rx_hash * cfg_num_rps_cpus) >> 32;155156log_verbose(" silo %d (cpu %d)", silo, rps_silo_to_cpu[silo]);157if (rps_silo_to_cpu[silo] != cpu) {158log_verbose(". error: rps cpu mismatch (%d)", cpu);159frames_error++;160}161}162163static void log_rxhash(int cpu, uint32_t rx_hash,164const char *addrs, int addr_len)165{166char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];167uint16_t *ports;168169if (!inet_ntop(cfg_family, addrs, saddr, sizeof(saddr)) ||170!inet_ntop(cfg_family, addrs + addr_len, daddr, sizeof(daddr)))171error(1, 0, "address parse error");172173ports = (void *)addrs + (addr_len * 2);174log_verbose("cpu %d: rx_hash 0x%08x [saddr %s daddr %s sport %02hu dport %02hu]",175cpu, rx_hash, saddr, daddr,176ntohs(ports[0]), ntohs(ports[1]));177}178179/* Compare computed rxhash with rxhash received from tpacket_v3 */180static void verify_rxhash(const char *pkt, uint32_t rx_hash, int cpu)181{182unsigned char four_tuple[FOUR_TUPLE_MAX_LEN] = {0};183uint32_t rx_hash_sw;184const char *addrs;185int addr_len;186187if (cfg_family == AF_INET) {188addr_len = sizeof(struct in_addr);189addrs = pkt + offsetof(struct iphdr, saddr);190} else {191addr_len = sizeof(struct in6_addr);192addrs = pkt + offsetof(struct ip6_hdr, ip6_src);193}194195memcpy(four_tuple, addrs, (addr_len * 2) + (sizeof(uint16_t) * 2));196rx_hash_sw = toeplitz(four_tuple, toeplitz_key);197198if (cfg_verbose)199log_rxhash(cpu, rx_hash, addrs, addr_len);200201if (rx_hash != rx_hash_sw) {202log_verbose(" != expected 0x%x\n", rx_hash_sw);203frames_error++;204return;205}206207log_verbose(" OK");208if (cfg_num_queues)209verify_rss(rx_hash, cpu);210else if (cfg_num_rps_cpus)211verify_rps(rx_hash, cpu);212log_verbose("\n");213}214215static char *recv_frame(const struct ring_state *ring, char *frame)216{217struct tpacket3_hdr *hdr = (void *)frame;218219if (hdr->hv1.tp_rxhash)220verify_rxhash(frame + hdr->tp_net, hdr->hv1.tp_rxhash,221ring->cpu);222else223frames_nohash++;224225return frame + hdr->tp_next_offset;226}227228/* A single TPACKET_V3 block can hold multiple frames */229static bool recv_block(struct ring_state *ring)230{231struct tpacket_block_desc *block;232char *frame;233int i;234235block = (void *)(ring->mmap + ring->idx * ring_block_sz);236if (!(block->hdr.bh1.block_status & TP_STATUS_USER))237return false;238239frame = (char *)block;240frame += block->hdr.bh1.offset_to_first_pkt;241242for (i = 0; i < block->hdr.bh1.num_pkts; i++) {243frame = recv_frame(ring, frame);244frames_received++;245}246247block->hdr.bh1.block_status = TP_STATUS_KERNEL;248ring->idx = (ring->idx + 1) % ring_block_nr;249250return true;251}252253/* simple test: sleep once unconditionally and then process all rings */254static void process_rings(void)255{256int i;257258usleep(1000 * cfg_timeout_msec);259260for (i = 0; i < num_cpus; i++)261do {} while (recv_block(&rings[i]));262263fprintf(stderr, "count: pass=%u nohash=%u fail=%u\n",264frames_received - frames_nohash - frames_error,265frames_nohash, frames_error);266}267268static char *setup_ring(int fd)269{270struct tpacket_req3 req3 = {0};271void *ring;272273req3.tp_retire_blk_tov = cfg_timeout_msec / 8;274req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;275276req3.tp_frame_size = 2048;277req3.tp_frame_nr = 1 << 10;278req3.tp_block_nr = 16;279280req3.tp_block_size = req3.tp_frame_size * req3.tp_frame_nr;281req3.tp_block_size /= req3.tp_block_nr;282283if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &req3, sizeof(req3)))284error(1, errno, "setsockopt PACKET_RX_RING");285286ring_block_sz = req3.tp_block_size;287ring_block_nr = req3.tp_block_nr;288289ring = mmap(0, req3.tp_block_size * req3.tp_block_nr,290PROT_READ | PROT_WRITE,291MAP_SHARED | MAP_LOCKED | MAP_POPULATE, fd, 0);292if (ring == MAP_FAILED)293error(1, 0, "mmap failed");294295return ring;296}297298static void __set_filter(int fd, int off_proto, uint8_t proto, int off_dport)299{300struct sock_filter filter[] = {301BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),302BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),303BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_proto),304BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, proto, 0, 2),305BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),306BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_dport, 1, 0),307BPF_STMT(BPF_RET + BPF_K, 0),308BPF_STMT(BPF_RET + BPF_K, 0xFFFF),309};310struct sock_fprog prog = {};311312prog.filter = filter;313prog.len = ARRAY_SIZE(filter);314if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))315error(1, errno, "setsockopt filter");316}317318/* filter on transport protocol and destination port */319static void set_filter(int fd)320{321const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */322uint8_t proto;323324proto = cfg_type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;325if (cfg_family == AF_INET)326__set_filter(fd, offsetof(struct iphdr, protocol), proto,327sizeof(struct iphdr) + off_dport);328else329__set_filter(fd, offsetof(struct ip6_hdr, ip6_nxt), proto,330sizeof(struct ip6_hdr) + off_dport);331}332333/* drop everything: used temporarily during setup */334static void set_filter_null(int fd)335{336struct sock_filter filter[] = {337BPF_STMT(BPF_RET + BPF_K, 0),338};339struct sock_fprog prog = {};340341prog.filter = filter;342prog.len = ARRAY_SIZE(filter);343if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))344error(1, errno, "setsockopt filter");345}346347static int create_ring(char **ring)348{349struct fanout_args args = {350.id = 1,351.type_flags = PACKET_FANOUT_CPU,352.max_num_members = RSS_MAX_CPUS353};354struct sockaddr_ll ll = { 0 };355int fd, val;356357fd = socket(PF_PACKET, SOCK_DGRAM, 0);358if (fd == -1)359error(1, errno, "socket creation failed");360361val = TPACKET_V3;362if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val)))363error(1, errno, "setsockopt PACKET_VERSION");364*ring = setup_ring(fd);365366/* block packets until all rings are added to the fanout group:367* else packets can arrive during setup and get misclassified368*/369set_filter_null(fd);370371ll.sll_family = AF_PACKET;372ll.sll_ifindex = if_nametoindex(cfg_ifname);373ll.sll_protocol = cfg_family == AF_INET ? htons(ETH_P_IP) :374htons(ETH_P_IPV6);375if (bind(fd, (void *)&ll, sizeof(ll)))376error(1, errno, "bind");377378/* must come after bind: verifies all programs in group match */379if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args, sizeof(args))) {380/* on failure, retry using old API if that is sufficient:381* it has a hard limit of 256 sockets, so only try if382* (a) only testing rxhash, not RSS or (b) <= 256 cpus.383* in this API, the third argument is left implicit.384*/385if (cfg_num_queues || num_cpus > 256 ||386setsockopt(fd, SOL_PACKET, PACKET_FANOUT,387&args, sizeof(uint32_t)))388error(1, errno, "setsockopt PACKET_FANOUT cpu");389}390391return fd;392}393394/* setup inet(6) socket to blackhole the test traffic, if arg '-s' */395static int setup_sink(void)396{397int fd, val;398399fd = socket(cfg_family, cfg_type, 0);400if (fd == -1)401error(1, errno, "socket %d.%d", cfg_family, cfg_type);402403val = 1 << 20;404if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val)))405error(1, errno, "setsockopt rcvbuf");406407return fd;408}409410static void setup_rings(void)411{412int i;413414for (i = 0; i < num_cpus; i++) {415rings[i].cpu = i;416rings[i].fd = create_ring(&rings[i].mmap);417}418419/* accept packets once all rings in the fanout group are up */420for (i = 0; i < num_cpus; i++)421set_filter(rings[i].fd);422}423424static void cleanup_rings(void)425{426int i;427428for (i = 0; i < num_cpus; i++) {429if (munmap(rings[i].mmap, ring_block_nr * ring_block_sz))430error(1, errno, "munmap");431if (close(rings[i].fd))432error(1, errno, "close");433}434}435436static void parse_cpulist(const char *arg)437{438do {439rx_irq_cpus[cfg_num_queues++] = strtol(arg, NULL, 10);440441arg = strchr(arg, ',');442if (!arg)443break;444arg++; // skip ','445} while (1);446}447448static void show_cpulist(void)449{450int i;451452for (i = 0; i < cfg_num_queues; i++)453fprintf(stderr, "rxq %d: cpu %d\n", i, rx_irq_cpus[i]);454}455456static void show_silos(void)457{458int i;459460for (i = 0; i < cfg_num_rps_cpus; i++)461fprintf(stderr, "silo %d: cpu %d\n", i, rps_silo_to_cpu[i]);462}463464static void parse_toeplitz_key(const char *str, int slen, unsigned char *key)465{466int i, ret, off;467468if (slen < TOEPLITZ_STR_MIN_LEN ||469slen > TOEPLITZ_STR_MAX_LEN + 1)470error(1, 0, "invalid toeplitz key");471472for (i = 0, off = 0; off < slen; i++, off += 3) {473ret = sscanf(str + off, "%hhx", &key[i]);474if (ret != 1)475error(1, 0, "key parse error at %d off %d len %d",476i, off, slen);477}478}479480static void parse_rps_bitmap(const char *arg)481{482unsigned long bitmap;483int i;484485bitmap = strtoul(arg, NULL, 0);486487if (bitmap & ~((1UL << RPS_MAX_CPUS) - 1))488error(1, 0, "rps bitmap 0x%lx out of bounds, max cpu %lu",489bitmap, RPS_MAX_CPUS - 1);490491for (i = 0; i < RPS_MAX_CPUS; i++)492if (bitmap & 1UL << i)493rps_silo_to_cpu[cfg_num_rps_cpus++] = i;494}495496static void read_rss_dev_info_ynl(void)497{498struct ethtool_rss_get_req *req;499struct ethtool_rss_get_rsp *rsp;500struct ynl_sock *ys;501502ys = ynl_sock_create(&ynl_ethtool_family, NULL);503if (!ys)504error(1, errno, "ynl_sock_create failed");505506req = ethtool_rss_get_req_alloc();507if (!req)508error(1, errno, "ethtool_rss_get_req_alloc failed");509510ethtool_rss_get_req_set_header_dev_name(req, cfg_ifname);511512rsp = ethtool_rss_get(ys, req);513if (!rsp)514error(1, ys->err.code, "YNL: %s", ys->err.msg);515516if (!rsp->_len.hkey)517error(1, 0, "RSS key not available for %s", cfg_ifname);518519if (rsp->_len.hkey < TOEPLITZ_KEY_MIN_LEN ||520rsp->_len.hkey > TOEPLITZ_KEY_MAX_LEN)521error(1, 0, "RSS key length %u out of bounds [%u, %u]",522rsp->_len.hkey, TOEPLITZ_KEY_MIN_LEN,523TOEPLITZ_KEY_MAX_LEN);524525memcpy(toeplitz_key, rsp->hkey, rsp->_len.hkey);526527if (rsp->_count.indir > RSS_MAX_INDIR)528error(1, 0, "RSS indirection table too large (%u > %u)",529rsp->_count.indir, RSS_MAX_INDIR);530531/* If indir table not available we'll fallback to simple modulo math */532if (rsp->_count.indir) {533memcpy(rss_indir_tbl, rsp->indir,534rsp->_count.indir * sizeof(rss_indir_tbl[0]));535rss_indir_tbl_size = rsp->_count.indir;536537log_verbose("RSS indirection table size: %u\n",538rss_indir_tbl_size);539}540541ethtool_rss_get_rsp_free(rsp);542ethtool_rss_get_req_free(req);543ynl_sock_destroy(ys);544}545546static void parse_opts(int argc, char **argv)547{548static struct option long_options[] = {549{"dport", required_argument, 0, 'd'},550{"cpus", required_argument, 0, 'C'},551{"key", required_argument, 0, 'k'},552{"iface", required_argument, 0, 'i'},553{"ipv4", no_argument, 0, '4'},554{"ipv6", no_argument, 0, '6'},555{"sink", no_argument, 0, 's'},556{"tcp", no_argument, 0, 't'},557{"timeout", required_argument, 0, 'T'},558{"udp", no_argument, 0, 'u'},559{"verbose", no_argument, 0, 'v'},560{"rps", required_argument, 0, 'r'},561{0, 0, 0, 0}562};563bool have_toeplitz = false;564int index, c;565566while ((c = getopt_long(argc, argv, "46C:d:i:k:r:stT:uv", long_options, &index)) != -1) {567switch (c) {568case '4':569cfg_family = AF_INET;570break;571case '6':572cfg_family = AF_INET6;573break;574case 'C':575parse_cpulist(optarg);576break;577case 'd':578cfg_dport = strtol(optarg, NULL, 0);579break;580case 'i':581cfg_ifname = optarg;582break;583case 'k':584parse_toeplitz_key(optarg, strlen(optarg),585toeplitz_key);586have_toeplitz = true;587break;588case 'r':589parse_rps_bitmap(optarg);590break;591case 's':592cfg_sink = true;593break;594case 't':595cfg_type = SOCK_STREAM;596break;597case 'T':598cfg_timeout_msec = strtol(optarg, NULL, 0);599break;600case 'u':601cfg_type = SOCK_DGRAM;602break;603case 'v':604cfg_verbose = true;605break;606607default:608error(1, 0, "unknown option %c", optopt);609break;610}611}612613if (!have_toeplitz)614read_rss_dev_info_ynl();615616num_cpus = get_nprocs();617if (num_cpus > RSS_MAX_CPUS)618error(1, 0, "increase RSS_MAX_CPUS");619620if (cfg_num_queues && cfg_num_rps_cpus)621error(1, 0,622"Can't supply both RSS cpus ('-C') and RPS map ('-r')");623if (cfg_verbose) {624show_cpulist();625show_silos();626}627}628629int main(int argc, char **argv)630{631const int min_tests = 10;632int fd_sink = -1;633634parse_opts(argc, argv);635636if (cfg_sink)637fd_sink = setup_sink();638639setup_rings();640641/* Signal to test framework that we're ready to receive */642ksft_ready();643644process_rings();645cleanup_rings();646647if (cfg_sink && close(fd_sink))648error(1, errno, "close sink");649650if (frames_received - frames_nohash < min_tests)651error(1, 0, "too few frames for verification");652653return frames_error;654}655656657