bpf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * common eBPF ELF operations.
  3. *
  4. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  5. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  6. * Copyright (C) 2015 Huawei Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License (not later!)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, see <http://www.gnu.org/licenses>
  20. */
  21. #include <stdlib.h>
  22. #include <memory.h>
  23. #include <unistd.h>
  24. #include <asm/unistd.h>
  25. #include <linux/bpf.h>
  26. #include "bpf.h"
  27. #include "libbpf.h"
  28. #include "nlattr.h"
  29. #include <linux/rtnetlink.h>
  30. #include <linux/if_link.h>
  31. #include <sys/socket.h>
  32. #include <errno.h>
  33. #ifndef SOL_NETLINK
  34. #define SOL_NETLINK 270
  35. #endif
  36. /*
  37. * When building perf, unistd.h is overridden. __NR_bpf is
  38. * required to be defined explicitly.
  39. */
  40. #ifndef __NR_bpf
  41. # if defined(__i386__)
  42. # define __NR_bpf 357
  43. # elif defined(__x86_64__)
  44. # define __NR_bpf 321
  45. # elif defined(__aarch64__)
  46. # define __NR_bpf 280
  47. # elif defined(__sparc__)
  48. # define __NR_bpf 349
  49. # elif defined(__s390__)
  50. # define __NR_bpf 351
  51. # else
  52. # error __NR_bpf not defined. libbpf does not support your arch.
  53. # endif
  54. #endif
  55. #ifndef min
  56. #define min(x, y) ((x) < (y) ? (x) : (y))
  57. #endif
  58. static inline __u64 ptr_to_u64(const void *ptr)
  59. {
  60. return (__u64) (unsigned long) ptr;
  61. }
  62. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  63. unsigned int size)
  64. {
  65. return syscall(__NR_bpf, cmd, attr, size);
  66. }
  67. int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
  68. int key_size, int value_size, int max_entries,
  69. __u32 map_flags, int node)
  70. {
  71. __u32 name_len = name ? strlen(name) : 0;
  72. union bpf_attr attr;
  73. memset(&attr, '\0', sizeof(attr));
  74. attr.map_type = map_type;
  75. attr.key_size = key_size;
  76. attr.value_size = value_size;
  77. attr.max_entries = max_entries;
  78. attr.map_flags = map_flags;
  79. memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
  80. if (node >= 0) {
  81. attr.map_flags |= BPF_F_NUMA_NODE;
  82. attr.numa_node = node;
  83. }
  84. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  85. }
  86. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  87. int value_size, int max_entries, __u32 map_flags)
  88. {
  89. return bpf_create_map_node(map_type, NULL, key_size, value_size,
  90. max_entries, map_flags, -1);
  91. }
  92. int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
  93. int key_size, int value_size, int max_entries,
  94. __u32 map_flags)
  95. {
  96. return bpf_create_map_node(map_type, name, key_size, value_size,
  97. max_entries, map_flags, -1);
  98. }
  99. int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
  100. int key_size, int inner_map_fd, int max_entries,
  101. __u32 map_flags, int node)
  102. {
  103. __u32 name_len = name ? strlen(name) : 0;
  104. union bpf_attr attr;
  105. memset(&attr, '\0', sizeof(attr));
  106. attr.map_type = map_type;
  107. attr.key_size = key_size;
  108. attr.value_size = 4;
  109. attr.inner_map_fd = inner_map_fd;
  110. attr.max_entries = max_entries;
  111. attr.map_flags = map_flags;
  112. memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
  113. if (node >= 0) {
  114. attr.map_flags |= BPF_F_NUMA_NODE;
  115. attr.numa_node = node;
  116. }
  117. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  118. }
  119. int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
  120. int key_size, int inner_map_fd, int max_entries,
  121. __u32 map_flags)
  122. {
  123. return bpf_create_map_in_map_node(map_type, name, key_size,
  124. inner_map_fd, max_entries, map_flags,
  125. -1);
  126. }
  127. int bpf_load_program_name(enum bpf_prog_type type, const char *name,
  128. const struct bpf_insn *insns,
  129. size_t insns_cnt, const char *license,
  130. __u32 kern_version, char *log_buf,
  131. size_t log_buf_sz)
  132. {
  133. int fd;
  134. union bpf_attr attr;
  135. __u32 name_len = name ? strlen(name) : 0;
  136. bzero(&attr, sizeof(attr));
  137. attr.prog_type = type;
  138. attr.insn_cnt = (__u32)insns_cnt;
  139. attr.insns = ptr_to_u64(insns);
  140. attr.license = ptr_to_u64(license);
  141. attr.log_buf = ptr_to_u64(NULL);
  142. attr.log_size = 0;
  143. attr.log_level = 0;
  144. attr.kern_version = kern_version;
  145. memcpy(attr.prog_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
  146. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  147. if (fd >= 0 || !log_buf || !log_buf_sz)
  148. return fd;
  149. /* Try again with log */
  150. attr.log_buf = ptr_to_u64(log_buf);
  151. attr.log_size = log_buf_sz;
  152. attr.log_level = 1;
  153. log_buf[0] = 0;
  154. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  155. }
  156. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  157. size_t insns_cnt, const char *license,
  158. __u32 kern_version, char *log_buf,
  159. size_t log_buf_sz)
  160. {
  161. return bpf_load_program_name(type, NULL, insns, insns_cnt, license,
  162. kern_version, log_buf, log_buf_sz);
  163. }
  164. int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  165. size_t insns_cnt, int strict_alignment,
  166. const char *license, __u32 kern_version,
  167. char *log_buf, size_t log_buf_sz, int log_level)
  168. {
  169. union bpf_attr attr;
  170. bzero(&attr, sizeof(attr));
  171. attr.prog_type = type;
  172. attr.insn_cnt = (__u32)insns_cnt;
  173. attr.insns = ptr_to_u64(insns);
  174. attr.license = ptr_to_u64(license);
  175. attr.log_buf = ptr_to_u64(log_buf);
  176. attr.log_size = log_buf_sz;
  177. attr.log_level = log_level;
  178. log_buf[0] = 0;
  179. attr.kern_version = kern_version;
  180. attr.prog_flags = strict_alignment ? BPF_F_STRICT_ALIGNMENT : 0;
  181. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  182. }
  183. int bpf_map_update_elem(int fd, const void *key, const void *value,
  184. __u64 flags)
  185. {
  186. union bpf_attr attr;
  187. bzero(&attr, sizeof(attr));
  188. attr.map_fd = fd;
  189. attr.key = ptr_to_u64(key);
  190. attr.value = ptr_to_u64(value);
  191. attr.flags = flags;
  192. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  193. }
  194. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  195. {
  196. union bpf_attr attr;
  197. bzero(&attr, sizeof(attr));
  198. attr.map_fd = fd;
  199. attr.key = ptr_to_u64(key);
  200. attr.value = ptr_to_u64(value);
  201. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  202. }
  203. int bpf_map_delete_elem(int fd, const void *key)
  204. {
  205. union bpf_attr attr;
  206. bzero(&attr, sizeof(attr));
  207. attr.map_fd = fd;
  208. attr.key = ptr_to_u64(key);
  209. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  210. }
  211. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  212. {
  213. union bpf_attr attr;
  214. bzero(&attr, sizeof(attr));
  215. attr.map_fd = fd;
  216. attr.key = ptr_to_u64(key);
  217. attr.next_key = ptr_to_u64(next_key);
  218. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  219. }
  220. int bpf_obj_pin(int fd, const char *pathname)
  221. {
  222. union bpf_attr attr;
  223. bzero(&attr, sizeof(attr));
  224. attr.pathname = ptr_to_u64((void *)pathname);
  225. attr.bpf_fd = fd;
  226. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  227. }
  228. int bpf_obj_get(const char *pathname)
  229. {
  230. union bpf_attr attr;
  231. bzero(&attr, sizeof(attr));
  232. attr.pathname = ptr_to_u64((void *)pathname);
  233. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  234. }
  235. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  236. unsigned int flags)
  237. {
  238. union bpf_attr attr;
  239. bzero(&attr, sizeof(attr));
  240. attr.target_fd = target_fd;
  241. attr.attach_bpf_fd = prog_fd;
  242. attr.attach_type = type;
  243. attr.attach_flags = flags;
  244. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  245. }
  246. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  247. {
  248. union bpf_attr attr;
  249. bzero(&attr, sizeof(attr));
  250. attr.target_fd = target_fd;
  251. attr.attach_type = type;
  252. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  253. }
  254. int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
  255. {
  256. union bpf_attr attr;
  257. bzero(&attr, sizeof(attr));
  258. attr.target_fd = target_fd;
  259. attr.attach_bpf_fd = prog_fd;
  260. attr.attach_type = type;
  261. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  262. }
  263. int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
  264. __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
  265. {
  266. union bpf_attr attr;
  267. int ret;
  268. bzero(&attr, sizeof(attr));
  269. attr.query.target_fd = target_fd;
  270. attr.query.attach_type = type;
  271. attr.query.query_flags = query_flags;
  272. attr.query.prog_cnt = *prog_cnt;
  273. attr.query.prog_ids = ptr_to_u64(prog_ids);
  274. ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
  275. if (attach_flags)
  276. *attach_flags = attr.query.attach_flags;
  277. *prog_cnt = attr.query.prog_cnt;
  278. return ret;
  279. }
  280. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  281. void *data_out, __u32 *size_out, __u32 *retval,
  282. __u32 *duration)
  283. {
  284. union bpf_attr attr;
  285. int ret;
  286. bzero(&attr, sizeof(attr));
  287. attr.test.prog_fd = prog_fd;
  288. attr.test.data_in = ptr_to_u64(data);
  289. attr.test.data_out = ptr_to_u64(data_out);
  290. attr.test.data_size_in = size;
  291. attr.test.repeat = repeat;
  292. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  293. if (size_out)
  294. *size_out = attr.test.data_size_out;
  295. if (retval)
  296. *retval = attr.test.retval;
  297. if (duration)
  298. *duration = attr.test.duration;
  299. return ret;
  300. }
  301. int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
  302. {
  303. union bpf_attr attr;
  304. int err;
  305. bzero(&attr, sizeof(attr));
  306. attr.start_id = start_id;
  307. err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
  308. if (!err)
  309. *next_id = attr.next_id;
  310. return err;
  311. }
  312. int bpf_map_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_MAP_GET_NEXT_ID, &attr, sizeof(attr));
  319. if (!err)
  320. *next_id = attr.next_id;
  321. return err;
  322. }
  323. int bpf_prog_get_fd_by_id(__u32 id)
  324. {
  325. union bpf_attr attr;
  326. bzero(&attr, sizeof(attr));
  327. attr.prog_id = id;
  328. return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
  329. }
  330. int bpf_map_get_fd_by_id(__u32 id)
  331. {
  332. union bpf_attr attr;
  333. bzero(&attr, sizeof(attr));
  334. attr.map_id = id;
  335. return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
  336. }
  337. int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
  338. {
  339. union bpf_attr attr;
  340. int err;
  341. bzero(&attr, sizeof(attr));
  342. attr.info.bpf_fd = prog_fd;
  343. attr.info.info_len = *info_len;
  344. attr.info.info = ptr_to_u64(info);
  345. err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
  346. if (!err)
  347. *info_len = attr.info.info_len;
  348. return err;
  349. }
  350. int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
  351. {
  352. struct sockaddr_nl sa;
  353. int sock, seq = 0, len, ret = -1;
  354. char buf[4096];
  355. struct nlattr *nla, *nla_xdp;
  356. struct {
  357. struct nlmsghdr nh;
  358. struct ifinfomsg ifinfo;
  359. char attrbuf[64];
  360. } req;
  361. struct nlmsghdr *nh;
  362. struct nlmsgerr *err;
  363. socklen_t addrlen;
  364. int one = 1;
  365. memset(&sa, 0, sizeof(sa));
  366. sa.nl_family = AF_NETLINK;
  367. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  368. if (sock < 0) {
  369. return -errno;
  370. }
  371. if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
  372. &one, sizeof(one)) < 0) {
  373. fprintf(stderr, "Netlink error reporting not supported\n");
  374. }
  375. if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
  376. ret = -errno;
  377. goto cleanup;
  378. }
  379. addrlen = sizeof(sa);
  380. if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
  381. ret = -errno;
  382. goto cleanup;
  383. }
  384. if (addrlen != sizeof(sa)) {
  385. ret = -LIBBPF_ERRNO__INTERNAL;
  386. goto cleanup;
  387. }
  388. memset(&req, 0, sizeof(req));
  389. req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  390. req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
  391. req.nh.nlmsg_type = RTM_SETLINK;
  392. req.nh.nlmsg_pid = 0;
  393. req.nh.nlmsg_seq = ++seq;
  394. req.ifinfo.ifi_family = AF_UNSPEC;
  395. req.ifinfo.ifi_index = ifindex;
  396. /* started nested attribute for XDP */
  397. nla = (struct nlattr *)(((char *)&req)
  398. + NLMSG_ALIGN(req.nh.nlmsg_len));
  399. nla->nla_type = NLA_F_NESTED | IFLA_XDP;
  400. nla->nla_len = NLA_HDRLEN;
  401. /* add XDP fd */
  402. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  403. nla_xdp->nla_type = IFLA_XDP_FD;
  404. nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
  405. memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
  406. nla->nla_len += nla_xdp->nla_len;
  407. /* if user passed in any flags, add those too */
  408. if (flags) {
  409. nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
  410. nla_xdp->nla_type = IFLA_XDP_FLAGS;
  411. nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
  412. memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
  413. nla->nla_len += nla_xdp->nla_len;
  414. }
  415. req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
  416. if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
  417. ret = -errno;
  418. goto cleanup;
  419. }
  420. len = recv(sock, buf, sizeof(buf), 0);
  421. if (len < 0) {
  422. ret = -errno;
  423. goto cleanup;
  424. }
  425. for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
  426. nh = NLMSG_NEXT(nh, len)) {
  427. if (nh->nlmsg_pid != sa.nl_pid) {
  428. ret = -LIBBPF_ERRNO__WRNGPID;
  429. goto cleanup;
  430. }
  431. if (nh->nlmsg_seq != seq) {
  432. ret = -LIBBPF_ERRNO__INVSEQ;
  433. goto cleanup;
  434. }
  435. switch (nh->nlmsg_type) {
  436. case NLMSG_ERROR:
  437. err = (struct nlmsgerr *)NLMSG_DATA(nh);
  438. if (!err->error)
  439. continue;
  440. ret = err->error;
  441. nla_dump_errormsg(nh);
  442. goto cleanup;
  443. case NLMSG_DONE:
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. ret = 0;
  450. cleanup:
  451. close(sock);
  452. return ret;
  453. }