PageRenderTime 72ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/testing/selftests/net/toeplitz.c

https://github.com/tiwai/sound
C | 587 lines | 444 code | 100 blank | 43 comment | 70 complexity | d0cf62382a3b60d69bc9825b83aa4482 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Toeplitz test
  3. *
  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
  6. * 3. Compare the two
  7. *
  8. * Optionally, either '-C $rx_irq_cpu_list' or '-r $rps_bitmap' may be given.
  9. *
  10. * If '-C $rx_irq_cpu_list' is given, also
  11. *
  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
  16. *
  17. * Else if '-r $rps_bitmap' is given, also
  18. *
  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
  22. */
  23. #define _GNU_SOURCE
  24. #include <arpa/inet.h>
  25. #include <errno.h>
  26. #include <error.h>
  27. #include <fcntl.h>
  28. #include <getopt.h>
  29. #include <linux/filter.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/if_packet.h>
  32. #include <net/if.h>
  33. #include <netdb.h>
  34. #include <netinet/ip.h>
  35. #include <netinet/ip6.h>
  36. #include <netinet/tcp.h>
  37. #include <netinet/udp.h>
  38. #include <poll.h>
  39. #include <stdbool.h>
  40. #include <stddef.h>
  41. #include <stdint.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <sys/mman.h>
  46. #include <sys/socket.h>
  47. #include <sys/stat.h>
  48. #include <sys/sysinfo.h>
  49. #include <sys/time.h>
  50. #include <sys/types.h>
  51. #include <unistd.h>
  52. #include "../kselftest.h"
  53. #define TOEPLITZ_KEY_MIN_LEN 40
  54. #define TOEPLITZ_KEY_MAX_LEN 60
  55. #define TOEPLITZ_STR_LEN(K) (((K) * 3) - 1) /* hex encoded: AA:BB:CC:...:ZZ */
  56. #define TOEPLITZ_STR_MIN_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MIN_LEN)
  57. #define TOEPLITZ_STR_MAX_LEN TOEPLITZ_STR_LEN(TOEPLITZ_KEY_MAX_LEN)
  58. #define FOUR_TUPLE_MAX_LEN ((sizeof(struct in6_addr) * 2) + (sizeof(uint16_t) * 2))
  59. #define RSS_MAX_CPUS (1 << 16) /* real constraint is PACKET_FANOUT_MAX */
  60. #define RPS_MAX_CPUS 16UL /* must be a power of 2 */
  61. /* configuration options (cmdline arguments) */
  62. static uint16_t cfg_dport = 8000;
  63. static int cfg_family = AF_INET6;
  64. static char *cfg_ifname = "eth0";
  65. static int cfg_num_queues;
  66. static int cfg_num_rps_cpus;
  67. static bool cfg_sink;
  68. static int cfg_type = SOCK_STREAM;
  69. static int cfg_timeout_msec = 1000;
  70. static bool cfg_verbose;
  71. /* global vars */
  72. static int num_cpus;
  73. static int ring_block_nr;
  74. static int ring_block_sz;
  75. /* stats */
  76. static int frames_received;
  77. static int frames_nohash;
  78. static int frames_error;
  79. #define log_verbose(args...) do { if (cfg_verbose) fprintf(stderr, args); } while (0)
  80. /* tpacket ring */
  81. struct ring_state {
  82. int fd;
  83. char *mmap;
  84. int idx;
  85. int cpu;
  86. };
  87. static unsigned int rx_irq_cpus[RSS_MAX_CPUS]; /* map from rxq to cpu */
  88. static int rps_silo_to_cpu[RPS_MAX_CPUS];
  89. static unsigned char toeplitz_key[TOEPLITZ_KEY_MAX_LEN];
  90. static struct ring_state rings[RSS_MAX_CPUS];
  91. static inline uint32_t toeplitz(const unsigned char *four_tuple,
  92. const unsigned char *key)
  93. {
  94. int i, bit, ret = 0;
  95. uint32_t key32;
  96. key32 = ntohl(*((uint32_t *)key));
  97. key += 4;
  98. for (i = 0; i < FOUR_TUPLE_MAX_LEN; i++) {
  99. for (bit = 7; bit >= 0; bit--) {
  100. if (four_tuple[i] & (1 << bit))
  101. ret ^= key32;
  102. key32 <<= 1;
  103. key32 |= !!(key[0] & (1 << bit));
  104. }
  105. key++;
  106. }
  107. return ret;
  108. }
  109. /* Compare computed cpu with arrival cpu from packet_fanout_cpu */
  110. static void verify_rss(uint32_t rx_hash, int cpu)
  111. {
  112. int queue = rx_hash % cfg_num_queues;
  113. log_verbose(" rxq %d (cpu %d)", queue, rx_irq_cpus[queue]);
  114. if (rx_irq_cpus[queue] != cpu) {
  115. log_verbose(". error: rss cpu mismatch (%d)", cpu);
  116. frames_error++;
  117. }
  118. }
  119. static void verify_rps(uint64_t rx_hash, int cpu)
  120. {
  121. int silo = (rx_hash * cfg_num_rps_cpus) >> 32;
  122. log_verbose(" silo %d (cpu %d)", silo, rps_silo_to_cpu[silo]);
  123. if (rps_silo_to_cpu[silo] != cpu) {
  124. log_verbose(". error: rps cpu mismatch (%d)", cpu);
  125. frames_error++;
  126. }
  127. }
  128. static void log_rxhash(int cpu, uint32_t rx_hash,
  129. const char *addrs, int addr_len)
  130. {
  131. char saddr[INET6_ADDRSTRLEN], daddr[INET6_ADDRSTRLEN];
  132. uint16_t *ports;
  133. if (!inet_ntop(cfg_family, addrs, saddr, sizeof(saddr)) ||
  134. !inet_ntop(cfg_family, addrs + addr_len, daddr, sizeof(daddr)))
  135. error(1, 0, "address parse error");
  136. ports = (void *)addrs + (addr_len * 2);
  137. log_verbose("cpu %d: rx_hash 0x%08x [saddr %s daddr %s sport %02hu dport %02hu]",
  138. cpu, rx_hash, saddr, daddr,
  139. ntohs(ports[0]), ntohs(ports[1]));
  140. }
  141. /* Compare computed rxhash with rxhash received from tpacket_v3 */
  142. static void verify_rxhash(const char *pkt, uint32_t rx_hash, int cpu)
  143. {
  144. unsigned char four_tuple[FOUR_TUPLE_MAX_LEN] = {0};
  145. uint32_t rx_hash_sw;
  146. const char *addrs;
  147. int addr_len;
  148. if (cfg_family == AF_INET) {
  149. addr_len = sizeof(struct in_addr);
  150. addrs = pkt + offsetof(struct iphdr, saddr);
  151. } else {
  152. addr_len = sizeof(struct in6_addr);
  153. addrs = pkt + offsetof(struct ip6_hdr, ip6_src);
  154. }
  155. memcpy(four_tuple, addrs, (addr_len * 2) + (sizeof(uint16_t) * 2));
  156. rx_hash_sw = toeplitz(four_tuple, toeplitz_key);
  157. if (cfg_verbose)
  158. log_rxhash(cpu, rx_hash, addrs, addr_len);
  159. if (rx_hash != rx_hash_sw) {
  160. log_verbose(" != expected 0x%x\n", rx_hash_sw);
  161. frames_error++;
  162. return;
  163. }
  164. log_verbose(" OK");
  165. if (cfg_num_queues)
  166. verify_rss(rx_hash, cpu);
  167. else if (cfg_num_rps_cpus)
  168. verify_rps(rx_hash, cpu);
  169. log_verbose("\n");
  170. }
  171. static char *recv_frame(const struct ring_state *ring, char *frame)
  172. {
  173. struct tpacket3_hdr *hdr = (void *)frame;
  174. if (hdr->hv1.tp_rxhash)
  175. verify_rxhash(frame + hdr->tp_net, hdr->hv1.tp_rxhash,
  176. ring->cpu);
  177. else
  178. frames_nohash++;
  179. return frame + hdr->tp_next_offset;
  180. }
  181. /* A single TPACKET_V3 block can hold multiple frames */
  182. static void recv_block(struct ring_state *ring)
  183. {
  184. struct tpacket_block_desc *block;
  185. char *frame;
  186. int i;
  187. block = (void *)(ring->mmap + ring->idx * ring_block_sz);
  188. if (!(block->hdr.bh1.block_status & TP_STATUS_USER))
  189. return;
  190. frame = (char *)block;
  191. frame += block->hdr.bh1.offset_to_first_pkt;
  192. for (i = 0; i < block->hdr.bh1.num_pkts; i++) {
  193. frame = recv_frame(ring, frame);
  194. frames_received++;
  195. }
  196. block->hdr.bh1.block_status = TP_STATUS_KERNEL;
  197. ring->idx = (ring->idx + 1) % ring_block_nr;
  198. }
  199. /* simple test: sleep once unconditionally and then process all rings */
  200. static void process_rings(void)
  201. {
  202. int i;
  203. usleep(1000 * cfg_timeout_msec);
  204. for (i = 0; i < num_cpus; i++)
  205. recv_block(&rings[i]);
  206. fprintf(stderr, "count: pass=%u nohash=%u fail=%u\n",
  207. frames_received - frames_nohash - frames_error,
  208. frames_nohash, frames_error);
  209. }
  210. static char *setup_ring(int fd)
  211. {
  212. struct tpacket_req3 req3 = {0};
  213. void *ring;
  214. req3.tp_retire_blk_tov = cfg_timeout_msec;
  215. req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
  216. req3.tp_frame_size = 2048;
  217. req3.tp_frame_nr = 1 << 10;
  218. req3.tp_block_nr = 2;
  219. req3.tp_block_size = req3.tp_frame_size * req3.tp_frame_nr;
  220. req3.tp_block_size /= req3.tp_block_nr;
  221. if (setsockopt(fd, SOL_PACKET, PACKET_RX_RING, &req3, sizeof(req3)))
  222. error(1, errno, "setsockopt PACKET_RX_RING");
  223. ring_block_sz = req3.tp_block_size;
  224. ring_block_nr = req3.tp_block_nr;
  225. ring = mmap(0, req3.tp_block_size * req3.tp_block_nr,
  226. PROT_READ | PROT_WRITE,
  227. MAP_SHARED | MAP_LOCKED | MAP_POPULATE, fd, 0);
  228. if (ring == MAP_FAILED)
  229. error(1, 0, "mmap failed");
  230. return ring;
  231. }
  232. static void __set_filter(int fd, int off_proto, uint8_t proto, int off_dport)
  233. {
  234. struct sock_filter filter[] = {
  235. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),
  236. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),
  237. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_proto),
  238. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, proto, 0, 2),
  239. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),
  240. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_dport, 1, 0),
  241. BPF_STMT(BPF_RET + BPF_K, 0),
  242. BPF_STMT(BPF_RET + BPF_K, 0xFFFF),
  243. };
  244. struct sock_fprog prog = {};
  245. prog.filter = filter;
  246. prog.len = ARRAY_SIZE(filter);
  247. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
  248. error(1, errno, "setsockopt filter");
  249. }
  250. /* filter on transport protocol and destination port */
  251. static void set_filter(int fd)
  252. {
  253. const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */
  254. uint8_t proto;
  255. proto = cfg_type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP;
  256. if (cfg_family == AF_INET)
  257. __set_filter(fd, offsetof(struct iphdr, protocol), proto,
  258. sizeof(struct iphdr) + off_dport);
  259. else
  260. __set_filter(fd, offsetof(struct ip6_hdr, ip6_nxt), proto,
  261. sizeof(struct ip6_hdr) + off_dport);
  262. }
  263. /* drop everything: used temporarily during setup */
  264. static void set_filter_null(int fd)
  265. {
  266. struct sock_filter filter[] = {
  267. BPF_STMT(BPF_RET + BPF_K, 0),
  268. };
  269. struct sock_fprog prog = {};
  270. prog.filter = filter;
  271. prog.len = ARRAY_SIZE(filter);
  272. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
  273. error(1, errno, "setsockopt filter");
  274. }
  275. static int create_ring(char **ring)
  276. {
  277. struct fanout_args args = {
  278. .id = 1,
  279. .type_flags = PACKET_FANOUT_CPU,
  280. .max_num_members = RSS_MAX_CPUS
  281. };
  282. struct sockaddr_ll ll = { 0 };
  283. int fd, val;
  284. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  285. if (fd == -1)
  286. error(1, errno, "socket creation failed");
  287. val = TPACKET_V3;
  288. if (setsockopt(fd, SOL_PACKET, PACKET_VERSION, &val, sizeof(val)))
  289. error(1, errno, "setsockopt PACKET_VERSION");
  290. *ring = setup_ring(fd);
  291. /* block packets until all rings are added to the fanout group:
  292. * else packets can arrive during setup and get misclassified
  293. */
  294. set_filter_null(fd);
  295. ll.sll_family = AF_PACKET;
  296. ll.sll_ifindex = if_nametoindex(cfg_ifname);
  297. ll.sll_protocol = cfg_family == AF_INET ? htons(ETH_P_IP) :
  298. htons(ETH_P_IPV6);
  299. if (bind(fd, (void *)&ll, sizeof(ll)))
  300. error(1, errno, "bind");
  301. /* must come after bind: verifies all programs in group match */
  302. if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT, &args, sizeof(args))) {
  303. /* on failure, retry using old API if that is sufficient:
  304. * it has a hard limit of 256 sockets, so only try if
  305. * (a) only testing rxhash, not RSS or (b) <= 256 cpus.
  306. * in this API, the third argument is left implicit.
  307. */
  308. if (cfg_num_queues || num_cpus > 256 ||
  309. setsockopt(fd, SOL_PACKET, PACKET_FANOUT,
  310. &args, sizeof(uint32_t)))
  311. error(1, errno, "setsockopt PACKET_FANOUT cpu");
  312. }
  313. return fd;
  314. }
  315. /* setup inet(6) socket to blackhole the test traffic, if arg '-s' */
  316. static int setup_sink(void)
  317. {
  318. int fd, val;
  319. fd = socket(cfg_family, cfg_type, 0);
  320. if (fd == -1)
  321. error(1, errno, "socket %d.%d", cfg_family, cfg_type);
  322. val = 1 << 20;
  323. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val)))
  324. error(1, errno, "setsockopt rcvbuf");
  325. return fd;
  326. }
  327. static void setup_rings(void)
  328. {
  329. int i;
  330. for (i = 0; i < num_cpus; i++) {
  331. rings[i].cpu = i;
  332. rings[i].fd = create_ring(&rings[i].mmap);
  333. }
  334. /* accept packets once all rings in the fanout group are up */
  335. for (i = 0; i < num_cpus; i++)
  336. set_filter(rings[i].fd);
  337. }
  338. static void cleanup_rings(void)
  339. {
  340. int i;
  341. for (i = 0; i < num_cpus; i++) {
  342. if (munmap(rings[i].mmap, ring_block_nr * ring_block_sz))
  343. error(1, errno, "munmap");
  344. if (close(rings[i].fd))
  345. error(1, errno, "close");
  346. }
  347. }
  348. static void parse_cpulist(const char *arg)
  349. {
  350. do {
  351. rx_irq_cpus[cfg_num_queues++] = strtol(arg, NULL, 10);
  352. arg = strchr(arg, ',');
  353. if (!arg)
  354. break;
  355. arg++; // skip ','
  356. } while (1);
  357. }
  358. static void show_cpulist(void)
  359. {
  360. int i;
  361. for (i = 0; i < cfg_num_queues; i++)
  362. fprintf(stderr, "rxq %d: cpu %d\n", i, rx_irq_cpus[i]);
  363. }
  364. static void show_silos(void)
  365. {
  366. int i;
  367. for (i = 0; i < cfg_num_rps_cpus; i++)
  368. fprintf(stderr, "silo %d: cpu %d\n", i, rps_silo_to_cpu[i]);
  369. }
  370. static void parse_toeplitz_key(const char *str, int slen, unsigned char *key)
  371. {
  372. int i, ret, off;
  373. if (slen < TOEPLITZ_STR_MIN_LEN ||
  374. slen > TOEPLITZ_STR_MAX_LEN + 1)
  375. error(1, 0, "invalid toeplitz key");
  376. for (i = 0, off = 0; off < slen; i++, off += 3) {
  377. ret = sscanf(str + off, "%hhx", &key[i]);
  378. if (ret != 1)
  379. error(1, 0, "key parse error at %d off %d len %d",
  380. i, off, slen);
  381. }
  382. }
  383. static void parse_rps_bitmap(const char *arg)
  384. {
  385. unsigned long bitmap;
  386. int i;
  387. bitmap = strtoul(arg, NULL, 0);
  388. if (bitmap & ~(RPS_MAX_CPUS - 1))
  389. error(1, 0, "rps bitmap 0x%lx out of bounds 0..%lu",
  390. bitmap, RPS_MAX_CPUS - 1);
  391. for (i = 0; i < RPS_MAX_CPUS; i++)
  392. if (bitmap & 1UL << i)
  393. rps_silo_to_cpu[cfg_num_rps_cpus++] = i;
  394. }
  395. static void parse_opts(int argc, char **argv)
  396. {
  397. static struct option long_options[] = {
  398. {"dport", required_argument, 0, 'd'},
  399. {"cpus", required_argument, 0, 'C'},
  400. {"key", required_argument, 0, 'k'},
  401. {"iface", required_argument, 0, 'i'},
  402. {"ipv4", no_argument, 0, '4'},
  403. {"ipv6", no_argument, 0, '6'},
  404. {"sink", no_argument, 0, 's'},
  405. {"tcp", no_argument, 0, 't'},
  406. {"timeout", required_argument, 0, 'T'},
  407. {"udp", no_argument, 0, 'u'},
  408. {"verbose", no_argument, 0, 'v'},
  409. {"rps", required_argument, 0, 'r'},
  410. {0, 0, 0, 0}
  411. };
  412. bool have_toeplitz = false;
  413. int index, c;
  414. while ((c = getopt_long(argc, argv, "46C:d:i:k:r:stT:uv", long_options, &index)) != -1) {
  415. switch (c) {
  416. case '4':
  417. cfg_family = AF_INET;
  418. break;
  419. case '6':
  420. cfg_family = AF_INET6;
  421. break;
  422. case 'C':
  423. parse_cpulist(optarg);
  424. break;
  425. case 'd':
  426. cfg_dport = strtol(optarg, NULL, 0);
  427. break;
  428. case 'i':
  429. cfg_ifname = optarg;
  430. break;
  431. case 'k':
  432. parse_toeplitz_key(optarg, strlen(optarg),
  433. toeplitz_key);
  434. have_toeplitz = true;
  435. break;
  436. case 'r':
  437. parse_rps_bitmap(optarg);
  438. break;
  439. case 's':
  440. cfg_sink = true;
  441. break;
  442. case 't':
  443. cfg_type = SOCK_STREAM;
  444. break;
  445. case 'T':
  446. cfg_timeout_msec = strtol(optarg, NULL, 0);
  447. break;
  448. case 'u':
  449. cfg_type = SOCK_DGRAM;
  450. break;
  451. case 'v':
  452. cfg_verbose = true;
  453. break;
  454. default:
  455. error(1, 0, "unknown option %c", optopt);
  456. break;
  457. }
  458. }
  459. if (!have_toeplitz)
  460. error(1, 0, "Must supply rss key ('-k')");
  461. num_cpus = get_nprocs();
  462. if (num_cpus > RSS_MAX_CPUS)
  463. error(1, 0, "increase RSS_MAX_CPUS");
  464. if (cfg_num_queues && cfg_num_rps_cpus)
  465. error(1, 0,
  466. "Can't supply both RSS cpus ('-C') and RPS map ('-r')");
  467. if (cfg_verbose) {
  468. show_cpulist();
  469. show_silos();
  470. }
  471. }
  472. int main(int argc, char **argv)
  473. {
  474. const int min_tests = 10;
  475. int fd_sink = -1;
  476. parse_opts(argc, argv);
  477. if (cfg_sink)
  478. fd_sink = setup_sink();
  479. setup_rings();
  480. process_rings();
  481. cleanup_rings();
  482. if (cfg_sink && close(fd_sink))
  483. error(1, errno, "close sink");
  484. if (frames_received - frames_nohash < min_tests)
  485. error(1, 0, "too few frames for verification");
  486. return frames_error;
  487. }