xdp_sample_user.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #ifndef XDP_SAMPLE_USER_H
  3. #define XDP_SAMPLE_USER_H
  4. #include <bpf/libbpf.h>
  5. #include <linux/compiler.h>
  6. #include "xdp_sample_shared.h"
  7. enum stats_mask {
  8. _SAMPLE_REDIRECT_MAP = 1U << 0,
  9. SAMPLE_RX_CNT = 1U << 1,
  10. SAMPLE_REDIRECT_ERR_CNT = 1U << 2,
  11. SAMPLE_CPUMAP_ENQUEUE_CNT = 1U << 3,
  12. SAMPLE_CPUMAP_KTHREAD_CNT = 1U << 4,
  13. SAMPLE_EXCEPTION_CNT = 1U << 5,
  14. SAMPLE_DEVMAP_XMIT_CNT = 1U << 6,
  15. SAMPLE_REDIRECT_CNT = 1U << 7,
  16. SAMPLE_REDIRECT_MAP_CNT = SAMPLE_REDIRECT_CNT | _SAMPLE_REDIRECT_MAP,
  17. SAMPLE_REDIRECT_ERR_MAP_CNT = SAMPLE_REDIRECT_ERR_CNT | _SAMPLE_REDIRECT_MAP,
  18. SAMPLE_DEVMAP_XMIT_CNT_MULTI = 1U << 8,
  19. SAMPLE_SKIP_HEADING = 1U << 9,
  20. };
  21. /* Exit return codes */
  22. #define EXIT_OK 0
  23. #define EXIT_FAIL 1
  24. #define EXIT_FAIL_OPTION 2
  25. #define EXIT_FAIL_XDP 3
  26. #define EXIT_FAIL_BPF 4
  27. #define EXIT_FAIL_MEM 5
  28. int sample_setup_maps(struct bpf_map **maps);
  29. int __sample_init(int mask);
  30. void sample_exit(int status);
  31. int sample_run(int interval, void (*post_cb)(void *), void *ctx);
  32. void sample_switch_mode(void);
  33. int sample_install_xdp(struct bpf_program *xdp_prog, int ifindex, bool generic,
  34. bool force);
  35. void sample_usage(char *argv[], const struct option *long_options,
  36. const char *doc, int mask, bool error);
  37. const char *get_driver_name(int ifindex);
  38. int get_mac_addr(int ifindex, void *mac_addr);
  39. #pragma GCC diagnostic push
  40. #ifndef __clang__
  41. #pragma GCC diagnostic ignored "-Wstringop-truncation"
  42. #endif
  43. __attribute__((unused))
  44. static inline char *safe_strncpy(char *dst, const char *src, size_t size)
  45. {
  46. if (!size)
  47. return dst;
  48. strncpy(dst, src, size - 1);
  49. dst[size - 1] = '\0';
  50. return dst;
  51. }
  52. #pragma GCC diagnostic pop
  53. #define __attach_tp(name) \
  54. ({ \
  55. if (bpf_program__type(skel->progs.name) != BPF_PROG_TYPE_TRACING)\
  56. return -EINVAL; \
  57. skel->links.name = bpf_program__attach(skel->progs.name); \
  58. if (!skel->links.name) \
  59. return -errno; \
  60. })
  61. #define sample_init_pre_load(skel) \
  62. ({ \
  63. skel->rodata->nr_cpus = libbpf_num_possible_cpus(); \
  64. sample_setup_maps((struct bpf_map *[]){ \
  65. skel->maps.rx_cnt, skel->maps.redir_err_cnt, \
  66. skel->maps.cpumap_enqueue_cnt, \
  67. skel->maps.cpumap_kthread_cnt, \
  68. skel->maps.exception_cnt, skel->maps.devmap_xmit_cnt, \
  69. skel->maps.devmap_xmit_cnt_multi }); \
  70. })
  71. #define DEFINE_SAMPLE_INIT(name) \
  72. static int sample_init(struct name *skel, int mask) \
  73. { \
  74. int ret; \
  75. ret = __sample_init(mask); \
  76. if (ret < 0) \
  77. return ret; \
  78. if (mask & SAMPLE_REDIRECT_MAP_CNT) \
  79. __attach_tp(tp_xdp_redirect_map); \
  80. if (mask & SAMPLE_REDIRECT_CNT) \
  81. __attach_tp(tp_xdp_redirect); \
  82. if (mask & SAMPLE_REDIRECT_ERR_MAP_CNT) \
  83. __attach_tp(tp_xdp_redirect_map_err); \
  84. if (mask & SAMPLE_REDIRECT_ERR_CNT) \
  85. __attach_tp(tp_xdp_redirect_err); \
  86. if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT) \
  87. __attach_tp(tp_xdp_cpumap_enqueue); \
  88. if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) \
  89. __attach_tp(tp_xdp_cpumap_kthread); \
  90. if (mask & SAMPLE_EXCEPTION_CNT) \
  91. __attach_tp(tp_xdp_exception); \
  92. if (mask & SAMPLE_DEVMAP_XMIT_CNT) \
  93. __attach_tp(tp_xdp_devmap_xmit); \
  94. if (mask & SAMPLE_DEVMAP_XMIT_CNT_MULTI) \
  95. __attach_tp(tp_xdp_devmap_xmit_multi); \
  96. return 0; \
  97. }
  98. #endif