bpf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. # elif defined(__s390__)
  41. # define __NR_bpf 351
  42. # else
  43. # error __NR_bpf not defined. libbpf does not support your arch.
  44. # endif
  45. #endif
  46. static inline __u64 ptr_to_u64(const void *ptr)
  47. {
  48. return (__u64) (unsigned long) ptr;
  49. }
  50. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  51. unsigned int size)
  52. {
  53. return syscall(__NR_bpf, cmd, attr, size);
  54. }
  55. int bpf_create_map_node(enum bpf_map_type map_type, int key_size,
  56. int value_size, int max_entries, __u32 map_flags,
  57. int node)
  58. {
  59. union bpf_attr attr;
  60. memset(&attr, '\0', sizeof(attr));
  61. attr.map_type = map_type;
  62. attr.key_size = key_size;
  63. attr.value_size = value_size;
  64. attr.max_entries = max_entries;
  65. attr.map_flags = map_flags;
  66. if (node >= 0) {
  67. attr.map_flags |= BPF_F_NUMA_NODE;
  68. attr.numa_node = node;
  69. }
  70. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  71. }
  72. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  73. int value_size, int max_entries, __u32 map_flags)
  74. {
  75. return bpf_create_map_node(map_type, key_size, value_size,
  76. max_entries, map_flags, -1);
  77. }
  78. int bpf_create_map_in_map_node(enum bpf_map_type map_type, int key_size,
  79. int inner_map_fd, int max_entries,
  80. __u32 map_flags, int node)
  81. {
  82. union bpf_attr attr;
  83. memset(&attr, '\0', sizeof(attr));
  84. attr.map_type = map_type;
  85. attr.key_size = key_size;
  86. attr.value_size = 4;
  87. attr.inner_map_fd = inner_map_fd;
  88. attr.max_entries = max_entries;
  89. attr.map_flags = map_flags;
  90. if (node >= 0) {
  91. attr.map_flags |= BPF_F_NUMA_NODE;
  92. attr.numa_node = node;
  93. }
  94. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  95. }
  96. int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
  97. int inner_map_fd, int max_entries, __u32 map_flags)
  98. {
  99. return bpf_create_map_in_map_node(map_type, key_size, inner_map_fd,
  100. max_entries, map_flags, -1);
  101. }
  102. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  103. size_t insns_cnt, const char *license,
  104. __u32 kern_version, char *log_buf, size_t log_buf_sz)
  105. {
  106. int fd;
  107. union bpf_attr attr;
  108. bzero(&attr, sizeof(attr));
  109. attr.prog_type = type;
  110. attr.insn_cnt = (__u32)insns_cnt;
  111. attr.insns = ptr_to_u64(insns);
  112. attr.license = ptr_to_u64(license);
  113. attr.log_buf = ptr_to_u64(NULL);
  114. attr.log_size = 0;
  115. attr.log_level = 0;
  116. attr.kern_version = kern_version;
  117. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  118. if (fd >= 0 || !log_buf || !log_buf_sz)
  119. return fd;
  120. /* Try again with log */
  121. attr.log_buf = ptr_to_u64(log_buf);
  122. attr.log_size = log_buf_sz;
  123. attr.log_level = 1;
  124. log_buf[0] = 0;
  125. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  126. }
  127. int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  128. size_t insns_cnt, int strict_alignment,
  129. const char *license, __u32 kern_version,
  130. char *log_buf, size_t log_buf_sz, int log_level)
  131. {
  132. union bpf_attr attr;
  133. bzero(&attr, sizeof(attr));
  134. attr.prog_type = type;
  135. attr.insn_cnt = (__u32)insns_cnt;
  136. attr.insns = ptr_to_u64(insns);
  137. attr.license = ptr_to_u64(license);
  138. attr.log_buf = ptr_to_u64(log_buf);
  139. attr.log_size = log_buf_sz;
  140. attr.log_level = log_level;
  141. log_buf[0] = 0;
  142. attr.kern_version = kern_version;
  143. attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
  144. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  145. }
  146. int bpf_map_update_elem(int fd, const void *key, const void *value,
  147. __u64 flags)
  148. {
  149. union bpf_attr attr;
  150. bzero(&attr, sizeof(attr));
  151. attr.map_fd = fd;
  152. attr.key = ptr_to_u64(key);
  153. attr.value = ptr_to_u64(value);
  154. attr.flags = flags;
  155. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  156. }
  157. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  158. {
  159. union bpf_attr attr;
  160. bzero(&attr, sizeof(attr));
  161. attr.map_fd = fd;
  162. attr.key = ptr_to_u64(key);
  163. attr.value = ptr_to_u64(value);
  164. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  165. }
  166. int bpf_map_delete_elem(int fd, const void *key)
  167. {
  168. union bpf_attr attr;
  169. bzero(&attr, sizeof(attr));
  170. attr.map_fd = fd;
  171. attr.key = ptr_to_u64(key);
  172. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  173. }
  174. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  175. {
  176. union bpf_attr attr;
  177. bzero(&attr, sizeof(attr));
  178. attr.map_fd = fd;
  179. attr.key = ptr_to_u64(key);
  180. attr.next_key = ptr_to_u64(next_key);
  181. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  182. }
  183. int bpf_obj_pin(int fd, const char *pathname)
  184. {
  185. union bpf_attr attr;
  186. bzero(&attr, sizeof(attr));
  187. attr.pathname = ptr_to_u64((void *)pathname);
  188. attr.bpf_fd = fd;
  189. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  190. }
  191. int bpf_obj_get(const char *pathname)
  192. {
  193. union bpf_attr attr;
  194. bzero(&attr, sizeof(attr));
  195. attr.pathname = ptr_to_u64((void *)pathname);
  196. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  197. }
  198. int __bpf_prog_attach(int prog_fd1, int prog_fd2, int target_fd,
  199. enum bpf_attach_type type,
  200. unsigned int flags)
  201. {
  202. union bpf_attr attr;
  203. bzero(&attr, sizeof(attr));
  204. attr.target_fd = target_fd;
  205. attr.attach_bpf_fd = prog_fd1;
  206. attr.attach_bpf_fd2 = prog_fd2;
  207. attr.attach_type = type;
  208. attr.attach_flags = flags;
  209. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  210. }
  211. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  212. unsigned int flags)
  213. {
  214. return __bpf_prog_attach(prog_fd, 0, target_fd, type, flags);
  215. }
  216. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  217. {
  218. union bpf_attr attr;
  219. bzero(&attr, sizeof(attr));
  220. attr.target_fd = target_fd;
  221. attr.attach_type = type;
  222. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  223. }
  224. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  225. void *data_out, __u32 *size_out, __u32 *retval,
  226. __u32 *duration)
  227. {
  228. union bpf_attr attr;
  229. int ret;
  230. bzero(&attr, sizeof(attr));
  231. attr.test.prog_fd = prog_fd;
  232. attr.test.data_in = ptr_to_u64(data);
  233. attr.test.data_out = ptr_to_u64(data_out);
  234. attr.test.data_size_in = size;
  235. attr.test.repeat = repeat;
  236. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  237. if (size_out)
  238. *size_out = attr.test.data_size_out;
  239. if (retval)
  240. *retval = attr.test.retval;
  241. if (duration)
  242. *duration = attr.test.duration;
  243. return ret;
  244. }
  245. int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
  246. {
  247. union bpf_attr attr;
  248. int err;
  249. bzero(&attr, sizeof(attr));
  250. attr.start_id = start_id;
  251. err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
  252. if (!err)
  253. *next_id = attr.next_id;
  254. return err;
  255. }
  256. int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
  257. {
  258. union bpf_attr attr;
  259. int err;
  260. bzero(&attr, sizeof(attr));
  261. attr.start_id = start_id;
  262. err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
  263. if (!err)
  264. *next_id = attr.next_id;
  265. return err;
  266. }
  267. int bpf_prog_get_fd_by_id(__u32 id)
  268. {
  269. union bpf_attr attr;
  270. bzero(&attr, sizeof(attr));
  271. attr.prog_id = id;
  272. return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
  273. }
  274. int bpf_map_get_fd_by_id(__u32 id)
  275. {
  276. union bpf_attr attr;
  277. bzero(&attr, sizeof(attr));
  278. attr.map_id = id;
  279. return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
  280. }
  281. int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
  282. {
  283. union bpf_attr attr;
  284. int err;
  285. bzero(&attr, sizeof(attr));
  286. attr.info.bpf_fd = prog_fd;
  287. attr.info.info_len = *info_len;
  288. attr.info.info = ptr_to_u64(info);
  289. err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
  290. if (!err)
  291. *info_len = attr.info.info_len;
  292. return err;
  293. }