xdp_rxq_info_user.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* SPDX-License-Identifier: GPL-2.0
  2. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  3. */
  4. static const char *__doc__ = " XDP RX-queue info extract example\n\n"
  5. "Monitor how many packets per sec (pps) are received\n"
  6. "per NIC RX queue index and which CPU processed the packet\n"
  7. ;
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <locale.h>
  16. #include <sys/resource.h>
  17. #include <getopt.h>
  18. #include <net/if.h>
  19. #include <time.h>
  20. #include <arpa/inet.h>
  21. #include <linux/if_link.h>
  22. #include "bpf/bpf.h"
  23. #include "bpf/libbpf.h"
  24. #include "bpf_util.h"
  25. static int ifindex = -1;
  26. static char ifname_buf[IF_NAMESIZE];
  27. static char *ifname;
  28. static __u32 xdp_flags;
  29. static struct bpf_map *stats_global_map;
  30. static struct bpf_map *rx_queue_index_map;
  31. /* Exit return codes */
  32. #define EXIT_OK 0
  33. #define EXIT_FAIL 1
  34. #define EXIT_FAIL_OPTION 2
  35. #define EXIT_FAIL_XDP 3
  36. #define EXIT_FAIL_BPF 4
  37. #define EXIT_FAIL_MEM 5
  38. static const struct option long_options[] = {
  39. {"help", no_argument, NULL, 'h' },
  40. {"dev", required_argument, NULL, 'd' },
  41. {"skb-mode", no_argument, NULL, 'S' },
  42. {"sec", required_argument, NULL, 's' },
  43. {"no-separators", no_argument, NULL, 'z' },
  44. {"action", required_argument, NULL, 'a' },
  45. {"readmem", no_argument, NULL, 'r' },
  46. {"swapmac", no_argument, NULL, 'm' },
  47. {0, 0, NULL, 0 }
  48. };
  49. static void int_exit(int sig)
  50. {
  51. fprintf(stderr,
  52. "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
  53. ifindex, ifname);
  54. if (ifindex > -1)
  55. bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
  56. exit(EXIT_OK);
  57. }
  58. struct config {
  59. __u32 action;
  60. int ifindex;
  61. __u32 options;
  62. };
  63. enum cfg_options_flags {
  64. NO_TOUCH = 0x0U,
  65. READ_MEM = 0x1U,
  66. SWAP_MAC = 0x2U,
  67. };
  68. #define XDP_ACTION_MAX (XDP_TX + 1)
  69. #define XDP_ACTION_MAX_STRLEN 11
  70. static const char *xdp_action_names[XDP_ACTION_MAX] = {
  71. [XDP_ABORTED] = "XDP_ABORTED",
  72. [XDP_DROP] = "XDP_DROP",
  73. [XDP_PASS] = "XDP_PASS",
  74. [XDP_TX] = "XDP_TX",
  75. };
  76. static const char *action2str(int action)
  77. {
  78. if (action < XDP_ACTION_MAX)
  79. return xdp_action_names[action];
  80. return NULL;
  81. }
  82. static int parse_xdp_action(char *action_str)
  83. {
  84. size_t maxlen;
  85. __u64 action = -1;
  86. int i;
  87. for (i = 0; i < XDP_ACTION_MAX; i++) {
  88. maxlen = XDP_ACTION_MAX_STRLEN;
  89. if (strncmp(xdp_action_names[i], action_str, maxlen) == 0) {
  90. action = i;
  91. break;
  92. }
  93. }
  94. return action;
  95. }
  96. static void list_xdp_actions(void)
  97. {
  98. int i;
  99. printf("Available XDP --action <options>\n");
  100. for (i = 0; i < XDP_ACTION_MAX; i++)
  101. printf("\t%s\n", xdp_action_names[i]);
  102. printf("\n");
  103. }
  104. static char* options2str(enum cfg_options_flags flag)
  105. {
  106. if (flag == NO_TOUCH)
  107. return "no_touch";
  108. if (flag & SWAP_MAC)
  109. return "swapmac";
  110. if (flag & READ_MEM)
  111. return "read";
  112. fprintf(stderr, "ERR: Unknown config option flags");
  113. exit(EXIT_FAIL);
  114. }
  115. static void usage(char *argv[])
  116. {
  117. int i;
  118. printf("\nDOCUMENTATION:\n%s\n", __doc__);
  119. printf(" Usage: %s (options-see-below)\n", argv[0]);
  120. printf(" Listing options:\n");
  121. for (i = 0; long_options[i].name != 0; i++) {
  122. printf(" --%-12s", long_options[i].name);
  123. if (long_options[i].flag != NULL)
  124. printf(" flag (internal value:%d)",
  125. *long_options[i].flag);
  126. else
  127. printf(" short-option: -%c",
  128. long_options[i].val);
  129. printf("\n");
  130. }
  131. printf("\n");
  132. list_xdp_actions();
  133. }
  134. #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
  135. static __u64 gettime(void)
  136. {
  137. struct timespec t;
  138. int res;
  139. res = clock_gettime(CLOCK_MONOTONIC, &t);
  140. if (res < 0) {
  141. fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
  142. exit(EXIT_FAIL);
  143. }
  144. return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
  145. }
  146. /* Common stats data record shared with _kern.c */
  147. struct datarec {
  148. __u64 processed;
  149. __u64 issue;
  150. };
  151. struct record {
  152. __u64 timestamp;
  153. struct datarec total;
  154. struct datarec *cpu;
  155. };
  156. struct stats_record {
  157. struct record stats;
  158. struct record *rxq;
  159. };
  160. static struct datarec *alloc_record_per_cpu(void)
  161. {
  162. unsigned int nr_cpus = bpf_num_possible_cpus();
  163. struct datarec *array;
  164. size_t size;
  165. size = sizeof(struct datarec) * nr_cpus;
  166. array = malloc(size);
  167. memset(array, 0, size);
  168. if (!array) {
  169. fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
  170. exit(EXIT_FAIL_MEM);
  171. }
  172. return array;
  173. }
  174. static struct record *alloc_record_per_rxq(void)
  175. {
  176. unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
  177. struct record *array;
  178. size_t size;
  179. size = sizeof(struct record) * nr_rxqs;
  180. array = malloc(size);
  181. memset(array, 0, size);
  182. if (!array) {
  183. fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
  184. exit(EXIT_FAIL_MEM);
  185. }
  186. return array;
  187. }
  188. static struct stats_record *alloc_stats_record(void)
  189. {
  190. unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
  191. struct stats_record *rec;
  192. int i;
  193. rec = malloc(sizeof(*rec));
  194. memset(rec, 0, sizeof(*rec));
  195. if (!rec) {
  196. fprintf(stderr, "Mem alloc error\n");
  197. exit(EXIT_FAIL_MEM);
  198. }
  199. rec->rxq = alloc_record_per_rxq();
  200. for (i = 0; i < nr_rxqs; i++)
  201. rec->rxq[i].cpu = alloc_record_per_cpu();
  202. rec->stats.cpu = alloc_record_per_cpu();
  203. return rec;
  204. }
  205. static void free_stats_record(struct stats_record *r)
  206. {
  207. unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
  208. int i;
  209. for (i = 0; i < nr_rxqs; i++)
  210. free(r->rxq[i].cpu);
  211. free(r->rxq);
  212. free(r->stats.cpu);
  213. free(r);
  214. }
  215. static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
  216. {
  217. /* For percpu maps, userspace gets a value per possible CPU */
  218. unsigned int nr_cpus = bpf_num_possible_cpus();
  219. struct datarec values[nr_cpus];
  220. __u64 sum_processed = 0;
  221. __u64 sum_issue = 0;
  222. int i;
  223. if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
  224. fprintf(stderr,
  225. "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
  226. return false;
  227. }
  228. /* Get time as close as possible to reading map contents */
  229. rec->timestamp = gettime();
  230. /* Record and sum values from each CPU */
  231. for (i = 0; i < nr_cpus; i++) {
  232. rec->cpu[i].processed = values[i].processed;
  233. sum_processed += values[i].processed;
  234. rec->cpu[i].issue = values[i].issue;
  235. sum_issue += values[i].issue;
  236. }
  237. rec->total.processed = sum_processed;
  238. rec->total.issue = sum_issue;
  239. return true;
  240. }
  241. static void stats_collect(struct stats_record *rec)
  242. {
  243. int fd, i, max_rxqs;
  244. fd = bpf_map__fd(stats_global_map);
  245. map_collect_percpu(fd, 0, &rec->stats);
  246. fd = bpf_map__fd(rx_queue_index_map);
  247. max_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
  248. for (i = 0; i < max_rxqs; i++)
  249. map_collect_percpu(fd, i, &rec->rxq[i]);
  250. }
  251. static double calc_period(struct record *r, struct record *p)
  252. {
  253. double period_ = 0;
  254. __u64 period = 0;
  255. period = r->timestamp - p->timestamp;
  256. if (period > 0)
  257. period_ = ((double) period / NANOSEC_PER_SEC);
  258. return period_;
  259. }
  260. static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
  261. {
  262. __u64 packets = 0;
  263. __u64 pps = 0;
  264. if (period_ > 0) {
  265. packets = r->processed - p->processed;
  266. pps = packets / period_;
  267. }
  268. return pps;
  269. }
  270. static __u64 calc_errs_pps(struct datarec *r,
  271. struct datarec *p, double period_)
  272. {
  273. __u64 packets = 0;
  274. __u64 pps = 0;
  275. if (period_ > 0) {
  276. packets = r->issue - p->issue;
  277. pps = packets / period_;
  278. }
  279. return pps;
  280. }
  281. static void stats_print(struct stats_record *stats_rec,
  282. struct stats_record *stats_prev,
  283. int action, __u32 cfg_opt)
  284. {
  285. unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
  286. unsigned int nr_cpus = bpf_num_possible_cpus();
  287. double pps = 0, err = 0;
  288. struct record *rec, *prev;
  289. double t;
  290. int rxq;
  291. int i;
  292. /* Header */
  293. printf("\nRunning XDP on dev:%s (ifindex:%d) action:%s options:%s\n",
  294. ifname, ifindex, action2str(action), options2str(cfg_opt));
  295. /* stats_global_map */
  296. {
  297. char *fmt_rx = "%-15s %-7d %'-11.0f %'-10.0f %s\n";
  298. char *fm2_rx = "%-15s %-7s %'-11.0f\n";
  299. char *errstr = "";
  300. printf("%-15s %-7s %-11s %-11s\n",
  301. "XDP stats", "CPU", "pps", "issue-pps");
  302. rec = &stats_rec->stats;
  303. prev = &stats_prev->stats;
  304. t = calc_period(rec, prev);
  305. for (i = 0; i < nr_cpus; i++) {
  306. struct datarec *r = &rec->cpu[i];
  307. struct datarec *p = &prev->cpu[i];
  308. pps = calc_pps (r, p, t);
  309. err = calc_errs_pps(r, p, t);
  310. if (err > 0)
  311. errstr = "invalid-ifindex";
  312. if (pps > 0)
  313. printf(fmt_rx, "XDP-RX CPU",
  314. i, pps, err, errstr);
  315. }
  316. pps = calc_pps (&rec->total, &prev->total, t);
  317. err = calc_errs_pps(&rec->total, &prev->total, t);
  318. printf(fm2_rx, "XDP-RX CPU", "total", pps, err);
  319. }
  320. /* rx_queue_index_map */
  321. printf("\n%-15s %-7s %-11s %-11s\n",
  322. "RXQ stats", "RXQ:CPU", "pps", "issue-pps");
  323. for (rxq = 0; rxq < nr_rxqs; rxq++) {
  324. char *fmt_rx = "%-15s %3d:%-3d %'-11.0f %'-10.0f %s\n";
  325. char *fm2_rx = "%-15s %3d:%-3s %'-11.0f\n";
  326. char *errstr = "";
  327. int rxq_ = rxq;
  328. /* Last RXQ in map catch overflows */
  329. if (rxq_ == nr_rxqs - 1)
  330. rxq_ = -1;
  331. rec = &stats_rec->rxq[rxq];
  332. prev = &stats_prev->rxq[rxq];
  333. t = calc_period(rec, prev);
  334. for (i = 0; i < nr_cpus; i++) {
  335. struct datarec *r = &rec->cpu[i];
  336. struct datarec *p = &prev->cpu[i];
  337. pps = calc_pps (r, p, t);
  338. err = calc_errs_pps(r, p, t);
  339. if (err > 0) {
  340. if (rxq_ == -1)
  341. errstr = "map-overflow-RXQ";
  342. else
  343. errstr = "err";
  344. }
  345. if (pps > 0)
  346. printf(fmt_rx, "rx_queue_index",
  347. rxq_, i, pps, err, errstr);
  348. }
  349. pps = calc_pps (&rec->total, &prev->total, t);
  350. err = calc_errs_pps(&rec->total, &prev->total, t);
  351. if (pps || err)
  352. printf(fm2_rx, "rx_queue_index", rxq_, "sum", pps, err);
  353. }
  354. }
  355. /* Pointer swap trick */
  356. static inline void swap(struct stats_record **a, struct stats_record **b)
  357. {
  358. struct stats_record *tmp;
  359. tmp = *a;
  360. *a = *b;
  361. *b = tmp;
  362. }
  363. static void stats_poll(int interval, int action, __u32 cfg_opt)
  364. {
  365. struct stats_record *record, *prev;
  366. record = alloc_stats_record();
  367. prev = alloc_stats_record();
  368. stats_collect(record);
  369. while (1) {
  370. swap(&prev, &record);
  371. stats_collect(record);
  372. stats_print(record, prev, action, cfg_opt);
  373. sleep(interval);
  374. }
  375. free_stats_record(record);
  376. free_stats_record(prev);
  377. }
  378. int main(int argc, char **argv)
  379. {
  380. __u32 cfg_options= NO_TOUCH ; /* Default: Don't touch packet memory */
  381. struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY};
  382. struct bpf_prog_load_attr prog_load_attr = {
  383. .prog_type = BPF_PROG_TYPE_XDP,
  384. };
  385. int prog_fd, map_fd, opt, err;
  386. bool use_separators = true;
  387. struct config cfg = { 0 };
  388. struct bpf_object *obj;
  389. struct bpf_map *map;
  390. char filename[256];
  391. int longindex = 0;
  392. int interval = 2;
  393. __u32 key = 0;
  394. char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 };
  395. int action = XDP_PASS; /* Default action */
  396. char *action_str = NULL;
  397. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  398. prog_load_attr.file = filename;
  399. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  400. perror("setrlimit(RLIMIT_MEMLOCK)");
  401. return 1;
  402. }
  403. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  404. return EXIT_FAIL;
  405. map = bpf_object__find_map_by_name(obj, "config_map");
  406. stats_global_map = bpf_object__find_map_by_name(obj, "stats_global_map");
  407. rx_queue_index_map = bpf_object__find_map_by_name(obj, "rx_queue_index_map");
  408. if (!map || !stats_global_map || !rx_queue_index_map) {
  409. printf("finding a map in obj file failed\n");
  410. return EXIT_FAIL;
  411. }
  412. map_fd = bpf_map__fd(map);
  413. if (!prog_fd) {
  414. fprintf(stderr, "ERR: load_bpf_file: %s\n", strerror(errno));
  415. return EXIT_FAIL;
  416. }
  417. /* Parse commands line args */
  418. while ((opt = getopt_long(argc, argv, "hSd:",
  419. long_options, &longindex)) != -1) {
  420. switch (opt) {
  421. case 'd':
  422. if (strlen(optarg) >= IF_NAMESIZE) {
  423. fprintf(stderr, "ERR: --dev name too long\n");
  424. goto error;
  425. }
  426. ifname = (char *)&ifname_buf;
  427. strncpy(ifname, optarg, IF_NAMESIZE);
  428. ifindex = if_nametoindex(ifname);
  429. if (ifindex == 0) {
  430. fprintf(stderr,
  431. "ERR: --dev name unknown err(%d):%s\n",
  432. errno, strerror(errno));
  433. goto error;
  434. }
  435. break;
  436. case 's':
  437. interval = atoi(optarg);
  438. break;
  439. case 'S':
  440. xdp_flags |= XDP_FLAGS_SKB_MODE;
  441. break;
  442. case 'z':
  443. use_separators = false;
  444. break;
  445. case 'a':
  446. action_str = (char *)&action_str_buf;
  447. strncpy(action_str, optarg, XDP_ACTION_MAX_STRLEN);
  448. break;
  449. case 'r':
  450. cfg_options |= READ_MEM;
  451. break;
  452. case 'm':
  453. cfg_options |= SWAP_MAC;
  454. break;
  455. case 'h':
  456. error:
  457. default:
  458. usage(argv);
  459. return EXIT_FAIL_OPTION;
  460. }
  461. }
  462. /* Required option */
  463. if (ifindex == -1) {
  464. fprintf(stderr, "ERR: required option --dev missing\n");
  465. usage(argv);
  466. return EXIT_FAIL_OPTION;
  467. }
  468. cfg.ifindex = ifindex;
  469. /* Parse action string */
  470. if (action_str) {
  471. action = parse_xdp_action(action_str);
  472. if (action < 0) {
  473. fprintf(stderr, "ERR: Invalid XDP --action: %s\n",
  474. action_str);
  475. list_xdp_actions();
  476. return EXIT_FAIL_OPTION;
  477. }
  478. }
  479. cfg.action = action;
  480. /* XDP_TX requires changing MAC-addrs, else HW may drop */
  481. if (action == XDP_TX)
  482. cfg_options |= SWAP_MAC;
  483. cfg.options = cfg_options;
  484. /* Trick to pretty printf with thousands separators use %' */
  485. if (use_separators)
  486. setlocale(LC_NUMERIC, "en_US");
  487. /* User-side setup ifindex in config_map */
  488. err = bpf_map_update_elem(map_fd, &key, &cfg, 0);
  489. if (err) {
  490. fprintf(stderr, "Store config failed (err:%d)\n", err);
  491. exit(EXIT_FAIL_BPF);
  492. }
  493. /* Remove XDP program when program is interrupted or killed */
  494. signal(SIGINT, int_exit);
  495. signal(SIGTERM, int_exit);
  496. if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
  497. fprintf(stderr, "link set xdp fd failed\n");
  498. return EXIT_FAIL_XDP;
  499. }
  500. stats_poll(interval, action, cfg_options);
  501. return EXIT_OK;
  502. }