bpf.c 14 KB

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