bpf.c 7.8 KB

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