bpf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // SPDX-License-Identifier: LGPL-2.1
  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 "nlattr.h"
  30. #include <linux/rtnetlink.h>
  31. #include <linux/if_link.h>
  32. #include <sys/socket.h>
  33. #include <errno.h>
  34. #ifndef SOL_NETLINK
  35. #define SOL_NETLINK 270
  36. #endif
  37. /*
  38. * When building perf, unistd.h is overridden. __NR_bpf is
  39. * required to be defined explicitly.
  40. */
  41. #ifndef __NR_bpf
  42. # if defined(__i386__)
  43. # define __NR_bpf 357
  44. # elif defined(__x86_64__)
  45. # define __NR_bpf 321
  46. # elif defined(__aarch64__)
  47. # define __NR_bpf 280
  48. # elif defined(__sparc__)
  49. # define __NR_bpf 349
  50. # elif defined(__s390__)
  51. # define __NR_bpf 351
  52. # else
  53. # error __NR_bpf not defined. libbpf does not support your arch.
  54. # endif
  55. #endif
  56. #ifndef min
  57. #define min(x, y) ((x) < (y) ? (x) : (y))
  58. #endif
  59. static inline __u64 ptr_to_u64(const void *ptr)
  60. {
  61. return (__u64) (unsigned long) ptr;
  62. }
  63. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  64. unsigned int size)
  65. {
  66. return syscall(__NR_bpf, cmd, attr, size);
  67. }
  68. int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
  69. {
  70. __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
  71. union bpf_attr attr;
  72. memset(&attr, '\0', sizeof(attr));
  73. attr.map_type = create_attr->map_type;
  74. attr.key_size = create_attr->key_size;
  75. attr.value_size = create_attr->value_size;
  76. attr.max_entries = create_attr->max_entries;
  77. attr.map_flags = create_attr->map_flags;
  78. memcpy(attr.map_name, create_attr->name,
  79. min(name_len, BPF_OBJ_NAME_LEN - 1));
  80. attr.numa_node = create_attr->numa_node;
  81. attr.btf_fd = create_attr->btf_fd;
  82. attr.btf_key_type_id = create_attr->btf_key_type_id;
  83. attr.btf_value_type_id = create_attr->btf_value_type_id;
  84. attr.map_ifindex = create_attr->map_ifindex;
  85. attr.inner_map_fd = create_attr->inner_map_fd;
  86. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  87. }
  88. int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
  89. int key_size, int value_size, int max_entries,
  90. __u32 map_flags, int node)
  91. {
  92. struct bpf_create_map_attr map_attr = {};
  93. map_attr.name = name;
  94. map_attr.map_type = map_type;
  95. map_attr.map_flags = map_flags;
  96. map_attr.key_size = key_size;
  97. map_attr.value_size = value_size;
  98. map_attr.max_entries = max_entries;
  99. if (node >= 0) {
  100. map_attr.numa_node = node;
  101. map_attr.map_flags |= BPF_F_NUMA_NODE;
  102. }
  103. return bpf_create_map_xattr(&map_attr);
  104. }
  105. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  106. int value_size, int max_entries, __u32 map_flags)
  107. {
  108. struct bpf_create_map_attr map_attr = {};
  109. map_attr.map_type = map_type;
  110. map_attr.map_flags = map_flags;
  111. map_attr.key_size = key_size;
  112. map_attr.value_size = value_size;
  113. map_attr.max_entries = max_entries;
  114. return bpf_create_map_xattr(&map_attr);
  115. }
  116. int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
  117. int key_size, int value_size, int max_entries,
  118. __u32 map_flags)
  119. {
  120. struct bpf_create_map_attr map_attr = {};
  121. map_attr.name = name;
  122. map_attr.map_type = map_type;
  123. map_attr.map_flags = map_flags;
  124. map_attr.key_size = key_size;
  125. map_attr.value_size = value_size;
  126. map_attr.max_entries = max_entries;
  127. return bpf_create_map_xattr(&map_attr);
  128. }
  129. int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
  130. int key_size, int inner_map_fd, int max_entries,
  131. __u32 map_flags, int node)
  132. {
  133. __u32 name_len = name ? strlen(name) : 0;
  134. union bpf_attr attr;
  135. memset(&attr, '\0', sizeof(attr));
  136. attr.map_type = map_type;
  137. attr.key_size = key_size;
  138. attr.value_size = 4;
  139. attr.inner_map_fd = inner_map_fd;
  140. attr.max_entries = max_entries;
  141. attr.map_flags = map_flags;
  142. memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
  143. if (node >= 0) {
  144. attr.map_flags |= BPF_F_NUMA_NODE;
  145. attr.numa_node = node;
  146. }
  147. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  148. }
  149. int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
  150. int key_size, int inner_map_fd, int max_entries,
  151. __u32 map_flags)
  152. {
  153. return bpf_create_map_in_map_node(map_type, name, key_size,
  154. inner_map_fd, max_entries, map_flags,
  155. -1);
  156. }
  157. int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
  158. char *log_buf, size_t log_buf_sz)
  159. {
  160. union bpf_attr attr;
  161. __u32 name_len;
  162. int fd;
  163. if (!load_attr)
  164. return -EINVAL;
  165. name_len = load_attr->name ? strlen(load_attr->name) : 0;
  166. bzero(&attr, sizeof(attr));
  167. attr.prog_type = load_attr->prog_type;
  168. attr.expected_attach_type = load_attr->expected_attach_type;
  169. attr.insn_cnt = (__u32)load_attr->insns_cnt;
  170. attr.insns = ptr_to_u64(load_attr->insns);
  171. attr.license = ptr_to_u64(load_attr->license);
  172. attr.log_buf = ptr_to_u64(NULL);
  173. attr.log_size = 0;
  174. attr.log_level = 0;
  175. attr.kern_version = load_attr->kern_version;
  176. attr.prog_ifindex = load_attr->prog_ifindex;
  177. memcpy(attr.prog_name, load_attr->name,
  178. min(name_len, BPF_OBJ_NAME_LEN - 1));
  179. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  180. if (fd >= 0 || !log_buf || !log_buf_sz)
  181. return fd;
  182. /* Try again with log */
  183. attr.log_buf = ptr_to_u64(log_buf);
  184. attr.log_size = log_buf_sz;
  185. attr.log_level = 1;
  186. log_buf[0] = 0;
  187. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  188. }
  189. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  190. size_t insns_cnt, const char *license,
  191. __u32 kern_version, char *log_buf,
  192. size_t log_buf_sz)
  193. {
  194. struct bpf_load_program_attr load_attr;
  195. memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
  196. load_attr.prog_type = type;
  197. load_attr.expected_attach_type = 0;
  198. load_attr.name = NULL;
  199. load_attr.insns = insns;
  200. load_attr.insns_cnt = insns_cnt;
  201. load_attr.license = license;
  202. load_attr.kern_version = kern_version;
  203. return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
  204. }
  205. int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  206. size_t insns_cnt, int strict_alignment,
  207. const char *license, __u32 kern_version,
  208. char *log_buf, size_t log_buf_sz, int log_level)
  209. {
  210. union bpf_attr attr;
  211. bzero(&attr, sizeof(attr));
  212. attr.prog_type = type;
  213. attr.insn_cnt = (__u32)insns_cnt;
  214. attr.insns = ptr_to_u64(insns);
  215. attr.license = ptr_to_u64(license);
  216. attr.log_buf = ptr_to_u64(log_buf);
  217. attr.log_size = log_buf_sz;
  218. attr.log_level = log_level;
  219. log_buf[0] = 0;
  220. attr.kern_version = kern_version;
  221. attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
  222. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  223. }
  224. int bpf_map_update_elem(int fd, const void *key, const void *value,
  225. __u64 flags)
  226. {
  227. union bpf_attr attr;
  228. bzero(&attr, sizeof(attr));
  229. attr.map_fd = fd;
  230. attr.key = ptr_to_u64(key);
  231. attr.value = ptr_to_u64(value);
  232. attr.flags = flags;
  233. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  234. }
  235. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  236. {
  237. union bpf_attr attr;
  238. bzero(&attr, sizeof(attr));
  239. attr.map_fd = fd;
  240. attr.key = ptr_to_u64(key);
  241. attr.value = ptr_to_u64(value);
  242. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  243. }
  244. int bpf_map_delete_elem(int fd, const void *key)
  245. {
  246. union bpf_attr attr;
  247. bzero(&attr, sizeof(attr));
  248. attr.map_fd = fd;
  249. attr.key = ptr_to_u64(key);
  250. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  251. }
  252. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  253. {
  254. union bpf_attr attr;
  255. bzero(&attr, sizeof(attr));
  256. attr.map_fd = fd;
  257. attr.key = ptr_to_u64(key);
  258. attr.next_key = ptr_to_u64(next_key);
  259. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  260. }
  261. int bpf_obj_pin(int fd, const char *pathname)
  262. {
  263. union bpf_attr attr;
  264. bzero(&attr, sizeof(attr));
  265. attr.pathname = ptr_to_u64((void *)pathname);
  266. attr.bpf_fd = fd;
  267. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  268. }
  269. int bpf_obj_get(const char *pathname)
  270. {
  271. union bpf_attr attr;
  272. bzero(&attr, sizeof(attr));
  273. attr.pathname = ptr_to_u64((void *)pathname);
  274. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  275. }
  276. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  277. unsigned int flags)
  278. {
  279. union bpf_attr attr;
  280. bzero(&attr, sizeof(attr));
  281. attr.target_fd = target_fd;
  282. attr.attach_bpf_fd = prog_fd;
  283. attr.attach_type = type;
  284. attr.attach_flags = flags;
  285. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  286. }
  287. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  288. {
  289. union bpf_attr attr;
  290. bzero(&attr, sizeof(attr));
  291. attr.target_fd = target_fd;
  292. attr.attach_type = type;
  293. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  294. }
  295. int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
  296. {
  297. union bpf_attr attr;
  298. bzero(&attr, sizeof(attr));
  299. attr.target_fd = target_fd;
  300. attr.attach_bpf_fd = prog_fd;
  301. attr.attach_type = type;
  302. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  303. }
  304. int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
  305. __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
  306. {
  307. union bpf_attr attr;
  308. int ret;
  309. bzero(&attr, sizeof(attr));
  310. attr.query.target_fd = target_fd;
  311. attr.query.attach_type = type;
  312. attr.query.query_flags = query_flags;
  313. attr.query.prog_cnt = *prog_cnt;
  314. attr.query.prog_ids = ptr_to_u64(prog_ids);
  315. ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
  316. if (attach_flags)
  317. *attach_flags = attr.query.attach_flags;
  318. *prog_cnt = attr.query.prog_cnt;
  319. return ret;
  320. }
  321. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  322. void *data_out, __u32 *size_out, __u32 *retval,
  323. __u32 *duration)
  324. {
  325. union bpf_attr attr;
  326. int ret;
  327. bzero(&attr, sizeof(attr));
  328. attr.test.prog_fd = prog_fd;
  329. attr.test.data_in = ptr_to_u64(data);
  330. attr.test.data_out = ptr_to_u64(data_out);
  331. attr.test.data_size_in = size;
  332. attr.test.repeat = repeat;
  333. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  334. if (size_out)
  335. *size_out = attr.test.data_size_out;
  336. if (retval)
  337. *retval = attr.test.retval;
  338. if (duration)
  339. *duration = attr.test.duration;
  340. return ret;
  341. }
  342. int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
  343. {
  344. union bpf_attr attr;
  345. int err;
  346. bzero(&attr, sizeof(attr));
  347. attr.start_id = start_id;
  348. err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
  349. if (!err)
  350. *next_id = attr.next_id;
  351. return err;
  352. }
  353. int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
  354. {
  355. union bpf_attr attr;
  356. int err;
  357. bzero(&attr, sizeof(attr));
  358. attr.start_id = start_id;
  359. err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
  360. if (!err)
  361. *next_id = attr.next_id;
  362. return err;
  363. }
  364. int bpf_prog_get_fd_by_id(__u32 id)
  365. {
  366. union bpf_attr attr;
  367. bzero(&attr, sizeof(attr));
  368. attr.prog_id = id;
  369. return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
  370. }
  371. int bpf_map_get_fd_by_id(__u32 id)
  372. {
  373. union bpf_attr attr;
  374. bzero(&attr, sizeof(attr));
  375. attr.map_id = id;
  376. return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
  377. }
  378. int bpf_btf_get_fd_by_id(__u32 id)
  379. {
  380. union bpf_attr attr;
  381. bzero(&attr, sizeof(attr));
  382. attr.btf_id = id;
  383. return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
  384. }
  385. int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
  386. {
  387. union bpf_attr attr;
  388. int err;
  389. bzero(&attr, sizeof(attr));
  390. attr.info.bpf_fd = prog_fd;
  391. attr.info.info_len = *info_len;
  392. attr.info.info = ptr_to_u64(info);
  393. err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
  394. if (!err)
  395. *info_len = attr.info.info_len;
  396. return err;
  397. }
  398. int bpf_raw_tracepoint_open(const char *name, int prog_fd)
  399. {
  400. union bpf_attr attr;
  401. bzero(&attr, sizeof(attr));
  402. attr.raw_tracepoint.name = ptr_to_u64(name);
  403. attr.raw_tracepoint.prog_fd = prog_fd;
  404. return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
  405. }
  406. int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
  407. {
  408. struct sockaddr_nl sa;
  409. int sock, seq = 0, len, ret = -1;
  410. char buf[4096];
  411. struct nlattr *nla, *nla_xdp;
  412. struct {
  413. struct nlmsghdr nh;
  414. struct ifinfomsg ifinfo;
  415. char attrbuf[64];
  416. } req;
  417. struct nlmsghdr *nh;
  418. struct nlmsgerr *err;
  419. socklen_t addrlen;
  420. int one = 1;
  421. memset(&sa, 0, sizeof(sa));
  422. sa.nl_family = AF_NETLINK;
  423. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  424. if (sock < 0) {
  425. return -errno;
  426. }
  427. if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
  428. &one, sizeof(one)) < 0) {
  429. fprintf(stderr, "Netlink error reporting not supported\n");
  430. }
  431. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  432. ret = -errno;
  433. goto cleanup;
  434. }
  435. addrlen = sizeof(sa);
  436. if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
  437. ret = -errno;
  438. goto cleanup;
  439. }
  440. if (addrlen != sizeof(sa)) {
  441. ret = -LIBBPF_ERRNO__INTERNAL;
  442. goto cleanup;
  443. }
  444. memset(&req, 0, sizeof(req));
  445. req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  446. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  447. req.nh.nlmsg_type = RTM_SETLINK;
  448. req.nh.nlmsg_pid = 0;
  449. req.nh.nlmsg_seq = ++seq;
  450. req.ifinfo.ifi_family = AF_UNSPEC;
  451. req.ifinfo.ifi_index = ifindex;
  452. /* started nested attribute for XDP */
  453. nla = (struct nlattr *)(((char *)&req)
  454. + NLMSG_ALIGN(req.nh.nlmsg_len));
  455. nla->nla_type = NLA_F_NESTED | IFLA_XDP;
  456. nla->nla_len = NLA_HDRLEN;
  457. /* add XDP fd */
  458. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  459. nla_xdp->nla_type = IFLA_XDP_FD;
  460. nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
  461. memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
  462. nla->nla_len += nla_xdp->nla_len;
  463. /* if user passed in any flags, add those too */
  464. if (flags) {
  465. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  466. nla_xdp->nla_type = IFLA_XDP_FLAGS;
  467. nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
  468. memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
  469. nla->nla_len += nla_xdp->nla_len;
  470. }
  471. req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
  472. if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
  473. ret = -errno;
  474. goto cleanup;
  475. }
  476. len = recv(sock, buf, sizeof(buf), 0);
  477. if (len < 0) {
  478. ret = -errno;
  479. goto cleanup;
  480. }
  481. for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
  482. nh = NLMSG_NEXT(nh, len)) {
  483. if (nh->nlmsg_pid != sa.nl_pid) {
  484. ret = -LIBBPF_ERRNO__WRNGPID;
  485. goto cleanup;
  486. }
  487. if (nh->nlmsg_seq != seq) {
  488. ret = -LIBBPF_ERRNO__INVSEQ;
  489. goto cleanup;
  490. }
  491. switch (nh->nlmsg_type) {
  492. case NLMSG_ERROR:
  493. err = (struct nlmsgerr *)NLMSG_DATA(nh);
  494. if (!err->error)
  495. continue;
  496. ret = err->error;
  497. nla_dump_errormsg(nh);
  498. goto cleanup;
  499. case NLMSG_DONE:
  500. break;
  501. default:
  502. break;
  503. }
  504. }
  505. ret = 0;
  506. cleanup:
  507. close(sock);
  508. return ret;
  509. }
  510. int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
  511. bool do_log)
  512. {
  513. union bpf_attr attr = {};
  514. int fd;
  515. attr.btf = ptr_to_u64(btf);
  516. attr.btf_size = btf_size;
  517. retry:
  518. if (do_log && log_buf && log_buf_size) {
  519. attr.btf_log_level = 1;
  520. attr.btf_log_size = log_buf_size;
  521. attr.btf_log_buf = ptr_to_u64(log_buf);
  522. }
  523. fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
  524. if (fd == -1 && !do_log && log_buf && log_buf_size) {
  525. do_log = true;
  526. goto retry;
  527. }
  528. return fd;
  529. }
  530. int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
  531. __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
  532. __u64 *probe_addr)
  533. {
  534. union bpf_attr attr = {};
  535. int err;
  536. attr.task_fd_query.pid = pid;
  537. attr.task_fd_query.fd = fd;
  538. attr.task_fd_query.flags = flags;
  539. attr.task_fd_query.buf = ptr_to_u64(buf);
  540. attr.task_fd_query.buf_len = *buf_len;
  541. err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
  542. *buf_len = attr.task_fd_query.buf_len;
  543. *prog_id = attr.task_fd_query.prog_id;
  544. *fd_type = attr.task_fd_query.fd_type;
  545. *probe_offset = attr.task_fd_query.probe_offset;
  546. *probe_addr = attr.task_fd_query.probe_addr;
  547. return err;
  548. }