bpf.c 17 KB

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