xdp_rxq_info_user.c 12 KB

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