xdp_monitor_kern.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* SPDX-License-Identifier: GPL-2.0
  2. * Copyright(c) 2017-2018 Jesper Dangaard Brouer, Red Hat Inc.
  3. *
  4. * XDP monitor tool, based on tracepoints
  5. */
  6. #include <uapi/linux/bpf.h>
  7. #include "bpf_helpers.h"
  8. struct bpf_map_def SEC("maps") redirect_err_cnt = {
  9. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  10. .key_size = sizeof(u32),
  11. .value_size = sizeof(u64),
  12. .max_entries = 2,
  13. /* TODO: have entries for all possible errno's */
  14. };
  15. #define XDP_UNKNOWN XDP_REDIRECT + 1
  16. struct bpf_map_def SEC("maps") exception_cnt = {
  17. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  18. .key_size = sizeof(u32),
  19. .value_size = sizeof(u64),
  20. .max_entries = XDP_UNKNOWN + 1,
  21. };
  22. /* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_redirect/format
  23. * Code in: kernel/include/trace/events/xdp.h
  24. */
  25. struct xdp_redirect_ctx {
  26. u64 __pad; // First 8 bytes are not accessible by bpf code
  27. int prog_id; // offset:8; size:4; signed:1;
  28. u32 act; // offset:12 size:4; signed:0;
  29. int ifindex; // offset:16 size:4; signed:1;
  30. int err; // offset:20 size:4; signed:1;
  31. int to_ifindex; // offset:24 size:4; signed:1;
  32. u32 map_id; // offset:28 size:4; signed:0;
  33. int map_index; // offset:32 size:4; signed:1;
  34. }; // offset:36
  35. enum {
  36. XDP_REDIRECT_SUCCESS = 0,
  37. XDP_REDIRECT_ERROR = 1
  38. };
  39. static __always_inline
  40. int xdp_redirect_collect_stat(struct xdp_redirect_ctx *ctx)
  41. {
  42. u32 key = XDP_REDIRECT_ERROR;
  43. int err = ctx->err;
  44. u64 *cnt;
  45. if (!err)
  46. key = XDP_REDIRECT_SUCCESS;
  47. cnt = bpf_map_lookup_elem(&redirect_err_cnt, &key);
  48. if (!cnt)
  49. return 1;
  50. *cnt += 1;
  51. return 0; /* Indicate event was filtered (no further processing)*/
  52. /*
  53. * Returning 1 here would allow e.g. a perf-record tracepoint
  54. * to see and record these events, but it doesn't work well
  55. * in-practice as stopping perf-record also unload this
  56. * bpf_prog. Plus, there is additional overhead of doing so.
  57. */
  58. }
  59. SEC("tracepoint/xdp/xdp_redirect_err")
  60. int trace_xdp_redirect_err(struct xdp_redirect_ctx *ctx)
  61. {
  62. return xdp_redirect_collect_stat(ctx);
  63. }
  64. SEC("tracepoint/xdp/xdp_redirect_map_err")
  65. int trace_xdp_redirect_map_err(struct xdp_redirect_ctx *ctx)
  66. {
  67. return xdp_redirect_collect_stat(ctx);
  68. }
  69. /* Likely unloaded when prog starts */
  70. SEC("tracepoint/xdp/xdp_redirect")
  71. int trace_xdp_redirect(struct xdp_redirect_ctx *ctx)
  72. {
  73. return xdp_redirect_collect_stat(ctx);
  74. }
  75. /* Likely unloaded when prog starts */
  76. SEC("tracepoint/xdp/xdp_redirect_map")
  77. int trace_xdp_redirect_map(struct xdp_redirect_ctx *ctx)
  78. {
  79. return xdp_redirect_collect_stat(ctx);
  80. }
  81. /* Tracepoint format: /sys/kernel/debug/tracing/events/xdp/xdp_exception/format
  82. * Code in: kernel/include/trace/events/xdp.h
  83. */
  84. struct xdp_exception_ctx {
  85. u64 __pad; // First 8 bytes are not accessible by bpf code
  86. int prog_id; // offset:8; size:4; signed:1;
  87. u32 act; // offset:12; size:4; signed:0;
  88. int ifindex; // offset:16; size:4; signed:1;
  89. };
  90. SEC("tracepoint/xdp/xdp_exception")
  91. int trace_xdp_exception(struct xdp_exception_ctx *ctx)
  92. {
  93. u64 *cnt;
  94. u32 key;
  95. key = ctx->act;
  96. if (key > XDP_REDIRECT)
  97. key = XDP_UNKNOWN;
  98. cnt = bpf_map_lookup_elem(&exception_cnt, &key);
  99. if (!cnt)
  100. return 1;
  101. *cnt += 1;
  102. return 0;
  103. }
  104. /* Common stats data record shared with _user.c */
  105. struct datarec {
  106. u64 processed;
  107. u64 dropped;
  108. u64 info;
  109. u64 err;
  110. };
  111. #define MAX_CPUS 64
  112. struct bpf_map_def SEC("maps") cpumap_enqueue_cnt = {
  113. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  114. .key_size = sizeof(u32),
  115. .value_size = sizeof(struct datarec),
  116. .max_entries = MAX_CPUS,
  117. };
  118. struct bpf_map_def SEC("maps") cpumap_kthread_cnt = {
  119. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  120. .key_size = sizeof(u32),
  121. .value_size = sizeof(struct datarec),
  122. .max_entries = 1,
  123. };
  124. /* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_enqueue/format
  125. * Code in: kernel/include/trace/events/xdp.h
  126. */
  127. struct cpumap_enqueue_ctx {
  128. u64 __pad; // First 8 bytes are not accessible by bpf code
  129. int map_id; // offset:8; size:4; signed:1;
  130. u32 act; // offset:12; size:4; signed:0;
  131. int cpu; // offset:16; size:4; signed:1;
  132. unsigned int drops; // offset:20; size:4; signed:0;
  133. unsigned int processed; // offset:24; size:4; signed:0;
  134. int to_cpu; // offset:28; size:4; signed:1;
  135. };
  136. SEC("tracepoint/xdp/xdp_cpumap_enqueue")
  137. int trace_xdp_cpumap_enqueue(struct cpumap_enqueue_ctx *ctx)
  138. {
  139. u32 to_cpu = ctx->to_cpu;
  140. struct datarec *rec;
  141. if (to_cpu >= MAX_CPUS)
  142. return 1;
  143. rec = bpf_map_lookup_elem(&cpumap_enqueue_cnt, &to_cpu);
  144. if (!rec)
  145. return 0;
  146. rec->processed += ctx->processed;
  147. rec->dropped += ctx->drops;
  148. /* Record bulk events, then userspace can calc average bulk size */
  149. if (ctx->processed > 0)
  150. rec->info += 1;
  151. return 0;
  152. }
  153. /* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_cpumap_kthread/format
  154. * Code in: kernel/include/trace/events/xdp.h
  155. */
  156. struct cpumap_kthread_ctx {
  157. u64 __pad; // First 8 bytes are not accessible by bpf code
  158. int map_id; // offset:8; size:4; signed:1;
  159. u32 act; // offset:12; size:4; signed:0;
  160. int cpu; // offset:16; size:4; signed:1;
  161. unsigned int drops; // offset:20; size:4; signed:0;
  162. unsigned int processed; // offset:24; size:4; signed:0;
  163. int sched; // offset:28; size:4; signed:1;
  164. };
  165. SEC("tracepoint/xdp/xdp_cpumap_kthread")
  166. int trace_xdp_cpumap_kthread(struct cpumap_kthread_ctx *ctx)
  167. {
  168. struct datarec *rec;
  169. u32 key = 0;
  170. rec = bpf_map_lookup_elem(&cpumap_kthread_cnt, &key);
  171. if (!rec)
  172. return 0;
  173. rec->processed += ctx->processed;
  174. rec->dropped += ctx->drops;
  175. /* Count times kthread yielded CPU via schedule call */
  176. if (ctx->sched)
  177. rec->info++;
  178. return 0;
  179. }
  180. struct bpf_map_def SEC("maps") devmap_xmit_cnt = {
  181. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  182. .key_size = sizeof(u32),
  183. .value_size = sizeof(struct datarec),
  184. .max_entries = 1,
  185. };
  186. /* Tracepoint: /sys/kernel/debug/tracing/events/xdp/xdp_devmap_xmit/format
  187. * Code in: kernel/include/trace/events/xdp.h
  188. */
  189. struct devmap_xmit_ctx {
  190. u64 __pad; // First 8 bytes are not accessible by bpf code
  191. int map_id; // offset:8; size:4; signed:1;
  192. u32 act; // offset:12; size:4; signed:0;
  193. u32 map_index; // offset:16; size:4; signed:0;
  194. int drops; // offset:20; size:4; signed:1;
  195. int sent; // offset:24; size:4; signed:1;
  196. int from_ifindex; // offset:28; size:4; signed:1;
  197. int to_ifindex; // offset:32; size:4; signed:1;
  198. int err; // offset:36; size:4; signed:1;
  199. };
  200. SEC("tracepoint/xdp/xdp_devmap_xmit")
  201. int trace_xdp_devmap_xmit(struct devmap_xmit_ctx *ctx)
  202. {
  203. struct datarec *rec;
  204. u32 key = 0;
  205. rec = bpf_map_lookup_elem(&devmap_xmit_cnt, &key);
  206. if (!rec)
  207. return 0;
  208. rec->processed += ctx->sent;
  209. rec->dropped += ctx->drops;
  210. /* Record bulk events, then userspace can calc average bulk size */
  211. rec->info += 1;
  212. /* Record error cases, where no frame were sent */
  213. if (ctx->err)
  214. rec->err++;
  215. /* Catch API error of drv ndo_xdp_xmit sent more than count */
  216. if (ctx->drops < 0)
  217. rec->err++;
  218. return 1;
  219. }