bpf.c 9.2 KB

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