bpf.c 13 KB

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