bpf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * common eBPF ELF operations.
  3. *
  4. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  5. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  6. * Copyright (C) 2015 Huawei Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License (not later!)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, see <http://www.gnu.org/licenses>
  20. */
  21. #include <stdlib.h>
  22. #include <memory.h>
  23. #include <unistd.h>
  24. #include <asm/unistd.h>
  25. #include <linux/bpf.h>
  26. #include "bpf.h"
  27. /*
  28. * When building perf, unistd.h is overridden. __NR_bpf is
  29. * required to be defined explicitly.
  30. */
  31. #ifndef __NR_bpf
  32. # if defined(__i386__)
  33. # define __NR_bpf 357
  34. # elif defined(__x86_64__)
  35. # define __NR_bpf 321
  36. # elif defined(__aarch64__)
  37. # define __NR_bpf 280
  38. # else
  39. # error __NR_bpf not defined. libbpf does not support your arch.
  40. # endif
  41. #endif
  42. static inline __u64 ptr_to_u64(const void *ptr)
  43. {
  44. return (__u64) (unsigned long) ptr;
  45. }
  46. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  47. unsigned int size)
  48. {
  49. return syscall(__NR_bpf, cmd, attr, size);
  50. }
  51. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  52. int value_size, int max_entries, __u32 map_flags)
  53. {
  54. union bpf_attr attr;
  55. memset(&attr, '\0', sizeof(attr));
  56. attr.map_type = map_type;
  57. attr.key_size = key_size;
  58. attr.value_size = value_size;
  59. attr.max_entries = max_entries;
  60. attr.map_flags = map_flags;
  61. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  62. }
  63. int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
  64. int inner_map_fd, int max_entries, __u32 map_flags)
  65. {
  66. union bpf_attr attr;
  67. memset(&attr, '\0', sizeof(attr));
  68. attr.map_type = map_type;
  69. attr.key_size = key_size;
  70. attr.value_size = 4;
  71. attr.inner_map_fd = inner_map_fd;
  72. attr.max_entries = max_entries;
  73. attr.map_flags = map_flags;
  74. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  75. }
  76. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  77. size_t insns_cnt, const char *license,
  78. __u32 kern_version, char *log_buf, size_t log_buf_sz)
  79. {
  80. int fd;
  81. union bpf_attr attr;
  82. bzero(&attr, sizeof(attr));
  83. attr.prog_type = type;
  84. attr.insn_cnt = (__u32)insns_cnt;
  85. attr.insns = ptr_to_u64(insns);
  86. attr.license = ptr_to_u64(license);
  87. attr.log_buf = ptr_to_u64(NULL);
  88. attr.log_size = 0;
  89. attr.log_level = 0;
  90. attr.kern_version = kern_version;
  91. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  92. if (fd >= 0 || !log_buf || !log_buf_sz)
  93. return fd;
  94. /* Try again with log */
  95. attr.log_buf = ptr_to_u64(log_buf);
  96. attr.log_size = log_buf_sz;
  97. attr.log_level = 1;
  98. log_buf[0] = 0;
  99. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  100. }
  101. int bpf_map_update_elem(int fd, const void *key, const void *value,
  102. __u64 flags)
  103. {
  104. union bpf_attr attr;
  105. bzero(&attr, sizeof(attr));
  106. attr.map_fd = fd;
  107. attr.key = ptr_to_u64(key);
  108. attr.value = ptr_to_u64(value);
  109. attr.flags = flags;
  110. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  111. }
  112. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  113. {
  114. union bpf_attr attr;
  115. bzero(&attr, sizeof(attr));
  116. attr.map_fd = fd;
  117. attr.key = ptr_to_u64(key);
  118. attr.value = ptr_to_u64(value);
  119. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  120. }
  121. int bpf_map_delete_elem(int fd, const void *key)
  122. {
  123. union bpf_attr attr;
  124. bzero(&attr, sizeof(attr));
  125. attr.map_fd = fd;
  126. attr.key = ptr_to_u64(key);
  127. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  128. }
  129. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  130. {
  131. union bpf_attr attr;
  132. bzero(&attr, sizeof(attr));
  133. attr.map_fd = fd;
  134. attr.key = ptr_to_u64(key);
  135. attr.next_key = ptr_to_u64(next_key);
  136. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  137. }
  138. int bpf_obj_pin(int fd, const char *pathname)
  139. {
  140. union bpf_attr attr;
  141. bzero(&attr, sizeof(attr));
  142. attr.pathname = ptr_to_u64((void *)pathname);
  143. attr.bpf_fd = fd;
  144. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  145. }
  146. int bpf_obj_get(const char *pathname)
  147. {
  148. union bpf_attr attr;
  149. bzero(&attr, sizeof(attr));
  150. attr.pathname = ptr_to_u64((void *)pathname);
  151. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  152. }
  153. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  154. unsigned int flags)
  155. {
  156. union bpf_attr attr;
  157. bzero(&attr, sizeof(attr));
  158. attr.target_fd = target_fd;
  159. attr.attach_bpf_fd = prog_fd;
  160. attr.attach_type = type;
  161. attr.attach_flags = flags;
  162. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  163. }
  164. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  165. {
  166. union bpf_attr attr;
  167. bzero(&attr, sizeof(attr));
  168. attr.target_fd = target_fd;
  169. attr.attach_type = type;
  170. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  171. }