bpf.c 16 KB

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