bpf_sys.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef __BPF_SYS__
  2. #define __BPF_SYS__
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <sys/syscall.h>
  6. #include <linux/bpf.h>
  7. static inline __u64 bpf_ptr_to_u64(const void *ptr)
  8. {
  9. return (__u64)(unsigned long) ptr;
  10. }
  11. static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
  12. {
  13. #ifdef __NR_bpf
  14. return syscall(__NR_bpf, cmd, attr, size);
  15. #else
  16. fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
  17. errno = ENOSYS;
  18. return -1;
  19. #endif
  20. }
  21. static inline int bpf_map_lookup(int fd, const void *key, void *value)
  22. {
  23. union bpf_attr attr = {};
  24. attr.map_fd = fd;
  25. attr.key = bpf_ptr_to_u64(key);
  26. attr.value = bpf_ptr_to_u64(value);
  27. return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  28. }
  29. static inline int bpf_map_update(int fd, const void *key, const void *value,
  30. uint64_t flags)
  31. {
  32. union bpf_attr attr = {};
  33. attr.map_fd = fd;
  34. attr.key = bpf_ptr_to_u64(key);
  35. attr.value = bpf_ptr_to_u64(value);
  36. attr.flags = flags;
  37. return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  38. }
  39. static inline int bpf_map_delete(int fd, const void *key)
  40. {
  41. union bpf_attr attr = {};
  42. attr.map_fd = fd;
  43. attr.key = bpf_ptr_to_u64(key);
  44. return bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  45. }
  46. static inline int bpf_map_next_key(int fd, const void *key, void *next_key)
  47. {
  48. union bpf_attr attr = {};
  49. attr.map_fd = fd;
  50. attr.key = bpf_ptr_to_u64(key);
  51. attr.next_key = bpf_ptr_to_u64(next_key);
  52. return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  53. }
  54. static inline int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
  55. uint32_t size_value, uint32_t max_elem,
  56. uint32_t flags)
  57. {
  58. union bpf_attr attr = {};
  59. attr.map_type = type;
  60. attr.key_size = size_key;
  61. attr.value_size = size_value;
  62. attr.max_entries = max_elem;
  63. attr.map_flags = flags;
  64. return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  65. }
  66. static inline int bpf_prog_load(enum bpf_prog_type type,
  67. const struct bpf_insn *insns, size_t size_insns,
  68. const char *license, char *log, size_t size_log)
  69. {
  70. union bpf_attr attr = {};
  71. attr.prog_type = type;
  72. attr.insns = bpf_ptr_to_u64(insns);
  73. attr.insn_cnt = size_insns / sizeof(struct bpf_insn);
  74. attr.license = bpf_ptr_to_u64(license);
  75. if (size_log > 0) {
  76. attr.log_buf = bpf_ptr_to_u64(log);
  77. attr.log_size = size_log;
  78. attr.log_level = 1;
  79. log[0] = 0;
  80. }
  81. return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  82. }
  83. #endif /* __BPF_SYS__ */