multimap.bpf.c 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <linux/bpf.h>
  2. #include <bpf/bpf_helpers.h>
  3. const int XDP_ACTION_MAX = (XDP_TX + 1);
  4. struct datarec {
  5. __u64 rx_packets;
  6. };
  7. // stats keyed by XDP Action
  8. struct bpf_map_def SEC("maps") xdp_stats_map = {
  9. .type = BPF_MAP_TYPE_ARRAY,
  10. .key_size = sizeof(__u32),
  11. .value_size = sizeof(struct datarec),
  12. .max_entries = XDP_ACTION_MAX,
  13. };
  14. // tracks number of times called
  15. struct bpf_map_def SEC("maps") prog_stats_map = {
  16. .type = BPF_MAP_TYPE_ARRAY,
  17. .key_size = sizeof(__u32),
  18. .value_size = sizeof(__u64),
  19. .max_entries = 1,
  20. };
  21. SEC("xdp/stats")
  22. int xdp_stats(struct xdp_md *ctx)
  23. {
  24. __u64 *stats;
  25. struct datarec *rec;
  26. __u32 key = XDP_PASS;
  27. __u32 k1 = 0;
  28. stats = bpf_map_lookup_elem(&prog_stats_map, &k1);
  29. if (!stats)
  30. return XDP_ABORTED;
  31. __sync_fetch_and_add(stats, 1);
  32. rec = bpf_map_lookup_elem(&xdp_stats_map, &key);
  33. if (!rec)
  34. return XDP_ABORTED;
  35. __sync_fetch_and_add(&rec->rx_packets, 1);
  36. return XDP_PASS;
  37. }
  38. char _license[] SEC("license") = "GPL";