xdp_rxq_info_user.c 14 KB

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