bpf.c 15 KB

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