xdp_monitor_user.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc. */
  3. static const char *__doc__=
  4. "XDP monitor tool, based on tracepoints\n";
  5. static const char *__doc_err_only__=
  6. " NOTICE: Only tracking XDP redirect errors\n"
  7. " Enable redirect success stats via '-s/--stats'\n"
  8. " (which comes with a per packet processing overhead)\n";
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <unistd.h>
  17. #include <locale.h>
  18. #include <getopt.h>
  19. #include <net/if.h>
  20. #include <time.h>
  21. #include <signal.h>
  22. #include <bpf/bpf.h>
  23. #include <bpf/libbpf.h>
  24. #include "bpf_util.h"
  25. #include "xdp_sample_user.h"
  26. #include "xdp_monitor.skel.h"
  27. static int mask = SAMPLE_REDIRECT_ERR_CNT | SAMPLE_CPUMAP_ENQUEUE_CNT |
  28. SAMPLE_CPUMAP_KTHREAD_CNT | SAMPLE_EXCEPTION_CNT |
  29. SAMPLE_DEVMAP_XMIT_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
  30. DEFINE_SAMPLE_INIT(xdp_monitor);
  31. static const struct option long_options[] = {
  32. { "help", no_argument, NULL, 'h' },
  33. { "stats", no_argument, NULL, 's' },
  34. { "interval", required_argument, NULL, 'i' },
  35. { "verbose", no_argument, NULL, 'v' },
  36. {}
  37. };
  38. int main(int argc, char **argv)
  39. {
  40. unsigned long interval = 2;
  41. int ret = EXIT_FAIL_OPTION;
  42. struct xdp_monitor *skel;
  43. bool errors_only = true;
  44. int longindex = 0, opt;
  45. bool error = true;
  46. /* Parse commands line args */
  47. while ((opt = getopt_long(argc, argv, "si:vh",
  48. long_options, &longindex)) != -1) {
  49. switch (opt) {
  50. case 's':
  51. errors_only = false;
  52. mask |= SAMPLE_REDIRECT_CNT;
  53. break;
  54. case 'i':
  55. interval = strtoul(optarg, NULL, 0);
  56. break;
  57. case 'v':
  58. sample_switch_mode();
  59. break;
  60. case 'h':
  61. error = false;
  62. default:
  63. sample_usage(argv, long_options, __doc__, mask, error);
  64. return ret;
  65. }
  66. }
  67. skel = xdp_monitor__open();
  68. if (!skel) {
  69. fprintf(stderr, "Failed to xdp_monitor__open: %s\n",
  70. strerror(errno));
  71. ret = EXIT_FAIL_BPF;
  72. goto end;
  73. }
  74. ret = sample_init_pre_load(skel);
  75. if (ret < 0) {
  76. fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
  77. ret = EXIT_FAIL_BPF;
  78. goto end_destroy;
  79. }
  80. ret = xdp_monitor__load(skel);
  81. if (ret < 0) {
  82. fprintf(stderr, "Failed to xdp_monitor__load: %s\n", strerror(errno));
  83. ret = EXIT_FAIL_BPF;
  84. goto end_destroy;
  85. }
  86. ret = sample_init(skel, mask);
  87. if (ret < 0) {
  88. fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
  89. ret = EXIT_FAIL_BPF;
  90. goto end_destroy;
  91. }
  92. if (errors_only)
  93. printf("%s", __doc_err_only__);
  94. ret = sample_run(interval, NULL, NULL);
  95. if (ret < 0) {
  96. fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
  97. ret = EXIT_FAIL;
  98. goto end_destroy;
  99. }
  100. ret = EXIT_OK;
  101. end_destroy:
  102. xdp_monitor__destroy(skel);
  103. end:
  104. sample_exit(ret);
  105. }