bpf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. # elif defined(__sparc__)
  39. # define __NR_bpf 349
  40. # else
  41. # error __NR_bpf not defined. libbpf does not support your arch.
  42. # endif
  43. #endif
  44. static inline __u64 ptr_to_u64(const void *ptr)
  45. {
  46. return (__u64) (unsigned long) ptr;
  47. }
  48. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  49. unsigned int size)
  50. {
  51. return syscall(__NR_bpf, cmd, attr, size);
  52. }
  53. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  54. int value_size, int max_entries, __u32 map_flags)
  55. {
  56. union bpf_attr attr;
  57. memset(&attr, '\0', sizeof(attr));
  58. attr.map_type = map_type;
  59. attr.key_size = key_size;
  60. attr.value_size = value_size;
  61. attr.max_entries = max_entries;
  62. attr.map_flags = map_flags;
  63. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  64. }
  65. int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
  66. int inner_map_fd, int max_entries, __u32 map_flags)
  67. {
  68. union bpf_attr attr;
  69. memset(&attr, '\0', sizeof(attr));
  70. attr.map_type = map_type;
  71. attr.key_size = key_size;
  72. attr.value_size = 4;
  73. attr.inner_map_fd = inner_map_fd;
  74. attr.max_entries = max_entries;
  75. attr.map_flags = map_flags;
  76. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  77. }
  78. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  79. size_t insns_cnt, const char *license,
  80. __u32 kern_version, char *log_buf, size_t log_buf_sz)
  81. {
  82. int fd;
  83. union bpf_attr attr;
  84. bzero(&attr, sizeof(attr));
  85. attr.prog_type = type;
  86. attr.insn_cnt = (__u32)insns_cnt;
  87. attr.insns = ptr_to_u64(insns);
  88. attr.license = ptr_to_u64(license);
  89. attr.log_buf = ptr_to_u64(NULL);
  90. attr.log_size = 0;
  91. attr.log_level = 0;
  92. attr.kern_version = kern_version;
  93. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  94. if (fd >= 0 || !log_buf || !log_buf_sz)
  95. return fd;
  96. /* Try again with log */
  97. attr.log_buf = ptr_to_u64(log_buf);
  98. attr.log_size = log_buf_sz;
  99. attr.log_level = 1;
  100. log_buf[0] = 0;
  101. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  102. }
  103. int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  104. size_t insns_cnt, int strict_alignment,
  105. const char *license, __u32 kern_version,
  106. char *log_buf, size_t log_buf_sz)
  107. {
  108. union bpf_attr attr;
  109. bzero(&attr, sizeof(attr));
  110. attr.prog_type = type;
  111. attr.insn_cnt = (__u32)insns_cnt;
  112. attr.insns = ptr_to_u64(insns);
  113. attr.license = ptr_to_u64(license);
  114. attr.log_buf = ptr_to_u64(log_buf);
  115. attr.log_size = log_buf_sz;
  116. attr.log_level = 2;
  117. log_buf[0] = 0;
  118. attr.kern_version = kern_version;
  119. attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
  120. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  121. }
  122. int bpf_map_update_elem(int fd, const void *key, const void *value,
  123. __u64 flags)
  124. {
  125. union bpf_attr attr;
  126. bzero(&attr, sizeof(attr));
  127. attr.map_fd = fd;
  128. attr.key = ptr_to_u64(key);
  129. attr.value = ptr_to_u64(value);
  130. attr.flags = flags;
  131. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  132. }
  133. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  134. {
  135. union bpf_attr attr;
  136. bzero(&attr, sizeof(attr));
  137. attr.map_fd = fd;
  138. attr.key = ptr_to_u64(key);
  139. attr.value = ptr_to_u64(value);
  140. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  141. }
  142. int bpf_map_delete_elem(int fd, const void *key)
  143. {
  144. union bpf_attr attr;
  145. bzero(&attr, sizeof(attr));
  146. attr.map_fd = fd;
  147. attr.key = ptr_to_u64(key);
  148. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  149. }
  150. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  151. {
  152. union bpf_attr attr;
  153. bzero(&attr, sizeof(attr));
  154. attr.map_fd = fd;
  155. attr.key = ptr_to_u64(key);
  156. attr.next_key = ptr_to_u64(next_key);
  157. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  158. }
  159. int bpf_obj_pin(int fd, const char *pathname)
  160. {
  161. union bpf_attr attr;
  162. bzero(&attr, sizeof(attr));
  163. attr.pathname = ptr_to_u64((void *)pathname);
  164. attr.bpf_fd = fd;
  165. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  166. }
  167. int bpf_obj_get(const char *pathname)
  168. {
  169. union bpf_attr attr;
  170. bzero(&attr, sizeof(attr));
  171. attr.pathname = ptr_to_u64((void *)pathname);
  172. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  173. }
  174. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  175. unsigned int flags)
  176. {
  177. union bpf_attr attr;
  178. bzero(&attr, sizeof(attr));
  179. attr.target_fd = target_fd;
  180. attr.attach_bpf_fd = prog_fd;
  181. attr.attach_type = type;
  182. attr.attach_flags = flags;
  183. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  184. }
  185. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  186. {
  187. union bpf_attr attr;
  188. bzero(&attr, sizeof(attr));
  189. attr.target_fd = target_fd;
  190. attr.attach_type = type;
  191. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  192. }
  193. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  194. void *data_out, __u32 *size_out, __u32 *retval,
  195. __u32 *duration)
  196. {
  197. union bpf_attr attr;
  198. int ret;
  199. bzero(&attr, sizeof(attr));
  200. attr.test.prog_fd = prog_fd;
  201. attr.test.data_in = ptr_to_u64(data);
  202. attr.test.data_out = ptr_to_u64(data_out);
  203. attr.test.data_size_in = size;
  204. attr.test.repeat = repeat;
  205. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  206. if (size_out)
  207. *size_out = attr.test.data_size_out;
  208. if (retval)
  209. *retval = attr.test.retval;
  210. if (duration)
  211. *duration = attr.test.duration;
  212. return ret;
  213. }