xdpsock_kern.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define KBUILD_MODNAME "foo"
  3. #include <uapi/linux/bpf.h>
  4. #include "bpf_helpers.h"
  5. #include "xdpsock.h"
  6. struct bpf_map_def SEC("maps") qidconf_map = {
  7. .type = BPF_MAP_TYPE_ARRAY,
  8. .key_size = sizeof(int),
  9. .value_size = sizeof(int),
  10. .max_entries = 1,
  11. };
  12. struct bpf_map_def SEC("maps") xsks_map = {
  13. .type = BPF_MAP_TYPE_XSKMAP,
  14. .key_size = sizeof(int),
  15. .value_size = sizeof(int),
  16. .max_entries = 4,
  17. };
  18. struct bpf_map_def SEC("maps") rr_map = {
  19. .type = BPF_MAP_TYPE_PERCPU_ARRAY,
  20. .key_size = sizeof(int),
  21. .value_size = sizeof(unsigned int),
  22. .max_entries = 1,
  23. };
  24. SEC("xdp_sock")
  25. int xdp_sock_prog(struct xdp_md *ctx)
  26. {
  27. int *qidconf, key = 0, idx;
  28. unsigned int *rr;
  29. qidconf = bpf_map_lookup_elem(&qidconf_map, &key);
  30. if (!qidconf)
  31. return XDP_ABORTED;
  32. if (*qidconf != ctx->rx_queue_index)
  33. return XDP_PASS;
  34. #if RR_LB /* NB! RR_LB is configured in xdpsock.h */
  35. rr = bpf_map_lookup_elem(&rr_map, &key);
  36. if (!rr)
  37. return XDP_ABORTED;
  38. *rr = (*rr + 1) & (MAX_SOCKS - 1);
  39. idx = *rr;
  40. #else
  41. idx = 0;
  42. #endif
  43. return bpf_redirect_map(&xsks_map, idx, 0);
  44. }
  45. char _license[] SEC("license") = "GPL";