libbpf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* eBPF mini library */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <linux/unistd.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <linux/netlink.h>
  8. #include <linux/bpf.h>
  9. #include <errno.h>
  10. #include <net/ethernet.h>
  11. #include <net/if.h>
  12. #include <linux/if_packet.h>
  13. #include <arpa/inet.h>
  14. #include "libbpf.h"
  15. static __u64 ptr_to_u64(void *ptr)
  16. {
  17. return (__u64) (unsigned long) ptr;
  18. }
  19. int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
  20. int max_entries)
  21. {
  22. union bpf_attr attr = {
  23. .map_type = map_type,
  24. .key_size = key_size,
  25. .value_size = value_size,
  26. .max_entries = max_entries
  27. };
  28. return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
  29. }
  30. int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags)
  31. {
  32. union bpf_attr attr = {
  33. .map_fd = fd,
  34. .key = ptr_to_u64(key),
  35. .value = ptr_to_u64(value),
  36. .flags = flags,
  37. };
  38. return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  39. }
  40. int bpf_lookup_elem(int fd, void *key, void *value)
  41. {
  42. union bpf_attr attr = {
  43. .map_fd = fd,
  44. .key = ptr_to_u64(key),
  45. .value = ptr_to_u64(value),
  46. };
  47. return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  48. }
  49. int bpf_delete_elem(int fd, void *key)
  50. {
  51. union bpf_attr attr = {
  52. .map_fd = fd,
  53. .key = ptr_to_u64(key),
  54. };
  55. return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  56. }
  57. int bpf_get_next_key(int fd, void *key, void *next_key)
  58. {
  59. union bpf_attr attr = {
  60. .map_fd = fd,
  61. .key = ptr_to_u64(key),
  62. .next_key = ptr_to_u64(next_key),
  63. };
  64. return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  65. }
  66. #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
  67. char bpf_log_buf[LOG_BUF_SIZE];
  68. int bpf_prog_load(enum bpf_prog_type prog_type,
  69. const struct bpf_insn *insns, int prog_len,
  70. const char *license, int kern_version)
  71. {
  72. union bpf_attr attr = {
  73. .prog_type = prog_type,
  74. .insns = ptr_to_u64((void *) insns),
  75. .insn_cnt = prog_len / sizeof(struct bpf_insn),
  76. .license = ptr_to_u64((void *) license),
  77. .log_buf = ptr_to_u64(bpf_log_buf),
  78. .log_size = LOG_BUF_SIZE,
  79. .log_level = 1,
  80. };
  81. /* assign one field outside of struct init to make sure any
  82. * padding is zero initialized
  83. */
  84. attr.kern_version = kern_version;
  85. bpf_log_buf[0] = 0;
  86. return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  87. }
  88. int open_raw_sock(const char *name)
  89. {
  90. struct sockaddr_ll sll;
  91. int sock;
  92. sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL));
  93. if (sock < 0) {
  94. printf("cannot create raw socket\n");
  95. return -1;
  96. }
  97. memset(&sll, 0, sizeof(sll));
  98. sll.sll_family = AF_PACKET;
  99. sll.sll_ifindex = if_nametoindex(name);
  100. sll.sll_protocol = htons(ETH_P_ALL);
  101. if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
  102. printf("bind to %s: %s\n", name, strerror(errno));
  103. close(sock);
  104. return -1;
  105. }
  106. return sock;
  107. }
  108. int perf_event_open(struct perf_event_attr *attr, int pid, int cpu,
  109. int group_fd, unsigned long flags)
  110. {
  111. return syscall(__NR_perf_event_open, attr, pid, cpu,
  112. group_fd, flags);
  113. }