bpf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /*
  3. * common eBPF ELF operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  6. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  7. * Copyright (C) 2015 Huawei Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License (not later!)
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, see <http://www.gnu.org/licenses>
  21. */
  22. #include <stdlib.h>
  23. #include <memory.h>
  24. #include <unistd.h>
  25. #include <asm/unistd.h>
  26. #include <linux/bpf.h>
  27. #include "bpf.h"
  28. #include "libbpf.h"
  29. #include <errno.h>
  30. /*
  31. * When building perf, unistd.h is overridden. __NR_bpf is
  32. * required to be defined explicitly.
  33. */
  34. #ifndef __NR_bpf
  35. # if defined(__i386__)
  36. # define __NR_bpf 357
  37. # elif defined(__x86_64__)
  38. # define __NR_bpf 321
  39. # elif defined(__aarch64__)
  40. # define __NR_bpf 280
  41. # elif defined(__sparc__)
  42. # define __NR_bpf 349
  43. # elif defined(__s390__)
  44. # define __NR_bpf 351
  45. # else
  46. # error __NR_bpf not defined. libbpf does not support your arch.
  47. # endif
  48. #endif
  49. #ifndef min
  50. #define min(x, y) ((x) < (y) ? (x) : (y))
  51. #endif
  52. static inline __u64 ptr_to_u64(const void *ptr)
  53. {
  54. return (__u64) (unsigned long) ptr;
  55. }
  56. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  57. unsigned int size)
  58. {
  59. return syscall(__NR_bpf, cmd, attr, size);
  60. }
  61. int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
  62. {
  63. __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
  64. union bpf_attr attr;
  65. memset(&attr, '\0', sizeof(attr));
  66. attr.map_type = create_attr->map_type;
  67. attr.key_size = create_attr->key_size;
  68. attr.value_size = create_attr->value_size;
  69. attr.max_entries = create_attr->max_entries;
  70. attr.map_flags = create_attr->map_flags;
  71. memcpy(attr.map_name, create_attr->name,
  72. min(name_len, BPF_OBJ_NAME_LEN - 1));
  73. attr.numa_node = create_attr->numa_node;
  74. attr.btf_fd = create_attr->btf_fd;
  75. attr.btf_key_type_id = create_attr->btf_key_type_id;
  76. attr.btf_value_type_id = create_attr->btf_value_type_id;
  77. attr.map_ifindex = create_attr->map_ifindex;
  78. attr.inner_map_fd = create_attr->inner_map_fd;
  79. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  80. }
  81. int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
  82. int key_size, int value_size, int max_entries,
  83. __u32 map_flags, int node)
  84. {
  85. struct bpf_create_map_attr map_attr = {};
  86. map_attr.name = name;
  87. map_attr.map_type = map_type;
  88. map_attr.map_flags = map_flags;
  89. map_attr.key_size = key_size;
  90. map_attr.value_size = value_size;
  91. map_attr.max_entries = max_entries;
  92. if (node >= 0) {
  93. map_attr.numa_node = node;
  94. map_attr.map_flags |= BPF_F_NUMA_NODE;
  95. }
  96. return bpf_create_map_xattr(&map_attr);
  97. }
  98. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  99. int value_size, int max_entries, __u32 map_flags)
  100. {
  101. struct bpf_create_map_attr map_attr = {};
  102. map_attr.map_type = map_type;
  103. map_attr.map_flags = map_flags;
  104. map_attr.key_size = key_size;
  105. map_attr.value_size = value_size;
  106. map_attr.max_entries = max_entries;
  107. return bpf_create_map_xattr(&map_attr);
  108. }
  109. int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
  110. int key_size, int value_size, int max_entries,
  111. __u32 map_flags)
  112. {
  113. struct bpf_create_map_attr map_attr = {};
  114. map_attr.name = name;
  115. map_attr.map_type = map_type;
  116. map_attr.map_flags = map_flags;
  117. map_attr.key_size = key_size;
  118. map_attr.value_size = value_size;
  119. map_attr.max_entries = max_entries;
  120. return bpf_create_map_xattr(&map_attr);
  121. }
  122. int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
  123. int key_size, int inner_map_fd, int max_entries,
  124. __u32 map_flags, int node)
  125. {
  126. __u32 name_len = name ? strlen(name) : 0;
  127. union bpf_attr attr;
  128. memset(&attr, '\0', sizeof(attr));
  129. attr.map_type = map_type;
  130. attr.key_size = key_size;
  131. attr.value_size = 4;
  132. attr.inner_map_fd = inner_map_fd;
  133. attr.max_entries = max_entries;
  134. attr.map_flags = map_flags;
  135. memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
  136. if (node >= 0) {
  137. attr.map_flags |= BPF_F_NUMA_NODE;
  138. attr.numa_node = node;
  139. }
  140. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  141. }
  142. int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
  143. int key_size, int inner_map_fd, int max_entries,
  144. __u32 map_flags)
  145. {
  146. return bpf_create_map_in_map_node(map_type, name, key_size,
  147. inner_map_fd, max_entries, map_flags,
  148. -1);
  149. }
  150. int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
  151. char *log_buf, size_t log_buf_sz)
  152. {
  153. union bpf_attr attr;
  154. __u32 name_len;
  155. int fd;
  156. if (!load_attr)
  157. return -EINVAL;
  158. name_len = load_attr->name ? strlen(load_attr->name) : 0;
  159. bzero(&attr, sizeof(attr));
  160. attr.prog_type = load_attr->prog_type;
  161. attr.expected_attach_type = load_attr->expected_attach_type;
  162. attr.insn_cnt = (__u32)load_attr->insns_cnt;
  163. attr.insns = ptr_to_u64(load_attr->insns);
  164. attr.license = ptr_to_u64(load_attr->license);
  165. attr.log_buf = ptr_to_u64(NULL);
  166. attr.log_size = 0;
  167. attr.log_level = 0;
  168. attr.kern_version = load_attr->kern_version;
  169. attr.prog_ifindex = load_attr->prog_ifindex;
  170. memcpy(attr.prog_name, load_attr->name,
  171. min(name_len, BPF_OBJ_NAME_LEN - 1));
  172. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  173. if (fd >= 0 || !log_buf || !log_buf_sz)
  174. return fd;
  175. /* Try again with log */
  176. attr.log_buf = ptr_to_u64(log_buf);
  177. attr.log_size = log_buf_sz;
  178. attr.log_level = 1;
  179. log_buf[0] = 0;
  180. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  181. }
  182. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  183. size_t insns_cnt, const char *license,
  184. __u32 kern_version, char *log_buf,
  185. size_t log_buf_sz)
  186. {
  187. struct bpf_load_program_attr load_attr;
  188. memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
  189. load_attr.prog_type = type;
  190. load_attr.expected_attach_type = 0;
  191. load_attr.name = NULL;
  192. load_attr.insns = insns;
  193. load_attr.insns_cnt = insns_cnt;
  194. load_attr.license = license;
  195. load_attr.kern_version = kern_version;
  196. return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
  197. }
  198. int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  199. size_t insns_cnt, int strict_alignment,
  200. const char *license, __u32 kern_version,
  201. char *log_buf, size_t log_buf_sz, int log_level)
  202. {
  203. union bpf_attr attr;
  204. bzero(&attr, sizeof(attr));
  205. attr.prog_type = type;
  206. attr.insn_cnt = (__u32)insns_cnt;
  207. attr.insns = ptr_to_u64(insns);
  208. attr.license = ptr_to_u64(license);
  209. attr.log_buf = ptr_to_u64(log_buf);
  210. attr.log_size = log_buf_sz;
  211. attr.log_level = log_level;
  212. log_buf[0] = 0;
  213. attr.kern_version = kern_version;
  214. attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
  215. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  216. }
  217. int bpf_map_update_elem(int fd, const void *key, const void *value,
  218. __u64 flags)
  219. {
  220. union bpf_attr attr;
  221. bzero(&attr, sizeof(attr));
  222. attr.map_fd = fd;
  223. attr.key = ptr_to_u64(key);
  224. attr.value = ptr_to_u64(value);
  225. attr.flags = flags;
  226. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  227. }
  228. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  229. {
  230. union bpf_attr attr;
  231. bzero(&attr, sizeof(attr));
  232. attr.map_fd = fd;
  233. attr.key = ptr_to_u64(key);
  234. attr.value = ptr_to_u64(value);
  235. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  236. }
  237. int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
  238. {
  239. union bpf_attr attr;
  240. bzero(&attr, sizeof(attr));
  241. attr.map_fd = fd;
  242. attr.key = ptr_to_u64(key);
  243. attr.value = ptr_to_u64(value);
  244. return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
  245. }
  246. int bpf_map_delete_elem(int fd, const void *key)
  247. {
  248. union bpf_attr attr;
  249. bzero(&attr, sizeof(attr));
  250. attr.map_fd = fd;
  251. attr.key = ptr_to_u64(key);
  252. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  253. }
  254. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  255. {
  256. union bpf_attr attr;
  257. bzero(&attr, sizeof(attr));
  258. attr.map_fd = fd;
  259. attr.key = ptr_to_u64(key);
  260. attr.next_key = ptr_to_u64(next_key);
  261. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  262. }
  263. int bpf_obj_pin(int fd, const char *pathname)
  264. {
  265. union bpf_attr attr;
  266. bzero(&attr, sizeof(attr));
  267. attr.pathname = ptr_to_u64((void *)pathname);
  268. attr.bpf_fd = fd;
  269. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  270. }
  271. int bpf_obj_get(const char *pathname)
  272. {
  273. union bpf_attr attr;
  274. bzero(&attr, sizeof(attr));
  275. attr.pathname = ptr_to_u64((void *)pathname);
  276. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  277. }
  278. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  279. unsigned int flags)
  280. {
  281. union bpf_attr attr;
  282. bzero(&attr, sizeof(attr));
  283. attr.target_fd = target_fd;
  284. attr.attach_bpf_fd = prog_fd;
  285. attr.attach_type = type;
  286. attr.attach_flags = flags;
  287. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  288. }
  289. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  290. {
  291. union bpf_attr attr;
  292. bzero(&attr, sizeof(attr));
  293. attr.target_fd = target_fd;
  294. attr.attach_type = type;
  295. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  296. }
  297. int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
  298. {
  299. union bpf_attr attr;
  300. bzero(&attr, sizeof(attr));
  301. attr.target_fd = target_fd;
  302. attr.attach_bpf_fd = prog_fd;
  303. attr.attach_type = type;
  304. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  305. }
  306. int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
  307. __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
  308. {
  309. union bpf_attr attr;
  310. int ret;
  311. bzero(&attr, sizeof(attr));
  312. attr.query.target_fd = target_fd;
  313. attr.query.attach_type = type;
  314. attr.query.query_flags = query_flags;
  315. attr.query.prog_cnt = *prog_cnt;
  316. attr.query.prog_ids = ptr_to_u64(prog_ids);
  317. ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
  318. if (attach_flags)
  319. *attach_flags = attr.query.attach_flags;
  320. *prog_cnt = attr.query.prog_cnt;
  321. return ret;
  322. }
  323. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  324. void *data_out, __u32 *size_out, __u32 *retval,
  325. __u32 *duration)
  326. {
  327. union bpf_attr attr;
  328. int ret;
  329. bzero(&attr, sizeof(attr));
  330. attr.test.prog_fd = prog_fd;
  331. attr.test.data_in = ptr_to_u64(data);
  332. attr.test.data_out = ptr_to_u64(data_out);
  333. attr.test.data_size_in = size;
  334. attr.test.repeat = repeat;
  335. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  336. if (size_out)
  337. *size_out = attr.test.data_size_out;
  338. if (retval)
  339. *retval = attr.test.retval;
  340. if (duration)
  341. *duration = attr.test.duration;
  342. return ret;
  343. }
  344. int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
  345. {
  346. union bpf_attr attr;
  347. int err;
  348. bzero(&attr, sizeof(attr));
  349. attr.start_id = start_id;
  350. err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
  351. if (!err)
  352. *next_id = attr.next_id;
  353. return err;
  354. }
  355. int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
  356. {
  357. union bpf_attr attr;
  358. int err;
  359. bzero(&attr, sizeof(attr));
  360. attr.start_id = start_id;
  361. err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
  362. if (!err)
  363. *next_id = attr.next_id;
  364. return err;
  365. }
  366. int bpf_prog_get_fd_by_id(__u32 id)
  367. {
  368. union bpf_attr attr;
  369. bzero(&attr, sizeof(attr));
  370. attr.prog_id = id;
  371. return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
  372. }
  373. int bpf_map_get_fd_by_id(__u32 id)
  374. {
  375. union bpf_attr attr;
  376. bzero(&attr, sizeof(attr));
  377. attr.map_id = id;
  378. return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
  379. }
  380. int bpf_btf_get_fd_by_id(__u32 id)
  381. {
  382. union bpf_attr attr;
  383. bzero(&attr, sizeof(attr));
  384. attr.btf_id = id;
  385. return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
  386. }
  387. int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
  388. {
  389. union bpf_attr attr;
  390. int err;
  391. bzero(&attr, sizeof(attr));
  392. attr.info.bpf_fd = prog_fd;
  393. attr.info.info_len = *info_len;
  394. attr.info.info = ptr_to_u64(info);
  395. err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
  396. if (!err)
  397. *info_len = attr.info.info_len;
  398. return err;
  399. }
  400. int bpf_raw_tracepoint_open(const char *name, int prog_fd)
  401. {
  402. union bpf_attr attr;
  403. bzero(&attr, sizeof(attr));
  404. attr.raw_tracepoint.name = ptr_to_u64(name);
  405. attr.raw_tracepoint.prog_fd = prog_fd;
  406. return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
  407. }
  408. int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
  409. bool do_log)
  410. {
  411. union bpf_attr attr = {};
  412. int fd;
  413. attr.btf = ptr_to_u64(btf);
  414. attr.btf_size = btf_size;
  415. retry:
  416. if (do_log && log_buf && log_buf_size) {
  417. attr.btf_log_level = 1;
  418. attr.btf_log_size = log_buf_size;
  419. attr.btf_log_buf = ptr_to_u64(log_buf);
  420. }
  421. fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
  422. if (fd == -1 && !do_log && log_buf && log_buf_size) {
  423. do_log = true;
  424. goto retry;
  425. }
  426. return fd;
  427. }
  428. int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
  429. __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
  430. __u64 *probe_addr)
  431. {
  432. union bpf_attr attr = {};
  433. int err;
  434. attr.task_fd_query.pid = pid;
  435. attr.task_fd_query.fd = fd;
  436. attr.task_fd_query.flags = flags;
  437. attr.task_fd_query.buf = ptr_to_u64(buf);
  438. attr.task_fd_query.buf_len = *buf_len;
  439. err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
  440. *buf_len = attr.task_fd_query.buf_len;
  441. *prog_id = attr.task_fd_query.prog_id;
  442. *fd_type = attr.task_fd_query.fd_type;
  443. *probe_offset = attr.task_fd_query.probe_offset;
  444. *probe_addr = attr.task_fd_query.probe_addr;
  445. return err;
  446. }