1 // SPDX-License-Identifier: LGPL-2.1
4 * common eBPF ELF operations.
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation;
13 * version 2.1 of the License (not later!)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, see <http://www.gnu.org/licenses>
27 #include <asm/unistd.h>
28 #include <linux/bpf.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/if_link.h>
34 #include <sys/socket.h>
38 #define SOL_NETLINK 270
42 * When building perf, unistd.h is overridden. __NR_bpf is
43 * required to be defined explicitly.
46 # if defined(__i386__)
48 # elif defined(__x86_64__)
50 # elif defined(__aarch64__)
52 # elif defined(__sparc__)
54 # elif defined(__s390__)
56 # elif defined(__arc__)
59 # error __NR_bpf not defined. libbpf does not support your arch.
64 #define min(x, y) ((x) < (y) ? (x) : (y))
67 static inline __u64 ptr_to_u64(const void *ptr)
69 return (__u64) (unsigned long) ptr;
72 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
75 return syscall(__NR_bpf, cmd, attr, size);
78 static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
83 fd = sys_bpf(BPF_PROG_LOAD, attr, size);
84 } while (fd < 0 && errno == EAGAIN);
89 int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
91 __u32 name_len = create_attr->name ? strlen(create_attr->name) : 0;
95 memset(&attr, '\0', sizeof(attr));
97 attr.map_type = create_attr->map_type;
98 attr.key_size = create_attr->key_size;
99 attr.value_size = create_attr->value_size;
100 attr.max_entries = create_attr->max_entries;
101 attr.map_flags = create_attr->map_flags;
102 memcpy(attr.map_name, create_attr->name,
103 min(name_len, BPF_OBJ_NAME_LEN - 1));
104 attr.numa_node = create_attr->numa_node;
105 attr.btf_fd = create_attr->btf_fd;
106 attr.btf_key_type_id = create_attr->btf_key_type_id;
107 attr.btf_value_type_id = create_attr->btf_value_type_id;
108 attr.map_ifindex = create_attr->map_ifindex;
109 attr.inner_map_fd = create_attr->inner_map_fd;
111 ret = sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
112 if (ret < 0 && errno == EINVAL && create_attr->name) {
113 /* Retry the same syscall, but without the name.
114 * Pre v4.14 kernels don't support map names.
116 memset(attr.map_name, 0, sizeof(attr.map_name));
117 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
122 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
123 int key_size, int value_size, int max_entries,
124 __u32 map_flags, int node)
126 struct bpf_create_map_attr map_attr = {};
128 map_attr.name = name;
129 map_attr.map_type = map_type;
130 map_attr.map_flags = map_flags;
131 map_attr.key_size = key_size;
132 map_attr.value_size = value_size;
133 map_attr.max_entries = max_entries;
135 map_attr.numa_node = node;
136 map_attr.map_flags |= BPF_F_NUMA_NODE;
139 return bpf_create_map_xattr(&map_attr);
142 int bpf_create_map(enum bpf_map_type map_type, int key_size,
143 int value_size, int max_entries, __u32 map_flags)
145 struct bpf_create_map_attr map_attr = {};
147 map_attr.map_type = map_type;
148 map_attr.map_flags = map_flags;
149 map_attr.key_size = key_size;
150 map_attr.value_size = value_size;
151 map_attr.max_entries = max_entries;
153 return bpf_create_map_xattr(&map_attr);
156 int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
157 int key_size, int value_size, int max_entries,
160 struct bpf_create_map_attr map_attr = {};
162 map_attr.name = name;
163 map_attr.map_type = map_type;
164 map_attr.map_flags = map_flags;
165 map_attr.key_size = key_size;
166 map_attr.value_size = value_size;
167 map_attr.max_entries = max_entries;
169 return bpf_create_map_xattr(&map_attr);
172 int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
173 int key_size, int inner_map_fd, int max_entries,
174 __u32 map_flags, int node)
176 __u32 name_len = name ? strlen(name) : 0;
179 memset(&attr, '\0', sizeof(attr));
181 attr.map_type = map_type;
182 attr.key_size = key_size;
184 attr.inner_map_fd = inner_map_fd;
185 attr.max_entries = max_entries;
186 attr.map_flags = map_flags;
187 memcpy(attr.map_name, name, min(name_len, BPF_OBJ_NAME_LEN - 1));
190 attr.map_flags |= BPF_F_NUMA_NODE;
191 attr.numa_node = node;
194 return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
197 int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
198 int key_size, int inner_map_fd, int max_entries,
201 return bpf_create_map_in_map_node(map_type, name, key_size,
202 inner_map_fd, max_entries, map_flags,
206 int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
207 char *log_buf, size_t log_buf_sz)
216 name_len = load_attr->name ? strlen(load_attr->name) : 0;
218 bzero(&attr, sizeof(attr));
219 attr.prog_type = load_attr->prog_type;
220 attr.expected_attach_type = load_attr->expected_attach_type;
221 attr.insn_cnt = (__u32)load_attr->insns_cnt;
222 attr.insns = ptr_to_u64(load_attr->insns);
223 attr.license = ptr_to_u64(load_attr->license);
224 attr.log_buf = ptr_to_u64(NULL);
227 attr.kern_version = load_attr->kern_version;
228 attr.prog_ifindex = load_attr->prog_ifindex;
229 memcpy(attr.prog_name, load_attr->name,
230 min(name_len, BPF_OBJ_NAME_LEN - 1));
232 fd = sys_bpf_prog_load(&attr, sizeof(attr));
233 if (fd >= 0 || !log_buf || !log_buf_sz)
236 /* Try again with log */
237 attr.log_buf = ptr_to_u64(log_buf);
238 attr.log_size = log_buf_sz;
241 return sys_bpf_prog_load(&attr, sizeof(attr));
244 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
245 size_t insns_cnt, const char *license,
246 __u32 kern_version, char *log_buf,
249 struct bpf_load_program_attr load_attr;
251 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
252 load_attr.prog_type = type;
253 load_attr.expected_attach_type = 0;
254 load_attr.name = NULL;
255 load_attr.insns = insns;
256 load_attr.insns_cnt = insns_cnt;
257 load_attr.license = license;
258 load_attr.kern_version = kern_version;
260 return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
263 int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
264 size_t insns_cnt, __u32 prog_flags, const char *license,
265 __u32 kern_version, char *log_buf, size_t log_buf_sz,
270 bzero(&attr, sizeof(attr));
271 attr.prog_type = type;
272 attr.insn_cnt = (__u32)insns_cnt;
273 attr.insns = ptr_to_u64(insns);
274 attr.license = ptr_to_u64(license);
275 attr.log_buf = ptr_to_u64(log_buf);
276 attr.log_size = log_buf_sz;
277 attr.log_level = log_level;
279 attr.kern_version = kern_version;
280 attr.prog_flags = prog_flags;
282 return sys_bpf_prog_load(&attr, sizeof(attr));
285 int bpf_map_update_elem(int fd, const void *key, const void *value,
290 bzero(&attr, sizeof(attr));
292 attr.key = ptr_to_u64(key);
293 attr.value = ptr_to_u64(value);
296 return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
299 int bpf_map_lookup_elem(int fd, const void *key, void *value)
303 bzero(&attr, sizeof(attr));
305 attr.key = ptr_to_u64(key);
306 attr.value = ptr_to_u64(value);
308 return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
311 int bpf_map_delete_elem(int fd, const void *key)
315 bzero(&attr, sizeof(attr));
317 attr.key = ptr_to_u64(key);
319 return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
322 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
326 bzero(&attr, sizeof(attr));
328 attr.key = ptr_to_u64(key);
329 attr.next_key = ptr_to_u64(next_key);
331 return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
334 int bpf_obj_pin(int fd, const char *pathname)
338 bzero(&attr, sizeof(attr));
339 attr.pathname = ptr_to_u64((void *)pathname);
342 return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
345 int bpf_obj_get(const char *pathname)
349 bzero(&attr, sizeof(attr));
350 attr.pathname = ptr_to_u64((void *)pathname);
352 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
355 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
360 bzero(&attr, sizeof(attr));
361 attr.target_fd = target_fd;
362 attr.attach_bpf_fd = prog_fd;
363 attr.attach_type = type;
364 attr.attach_flags = flags;
366 return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
369 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
373 bzero(&attr, sizeof(attr));
374 attr.target_fd = target_fd;
375 attr.attach_type = type;
377 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
380 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
384 bzero(&attr, sizeof(attr));
385 attr.target_fd = target_fd;
386 attr.attach_bpf_fd = prog_fd;
387 attr.attach_type = type;
389 return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
392 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
393 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
398 bzero(&attr, sizeof(attr));
399 attr.query.target_fd = target_fd;
400 attr.query.attach_type = type;
401 attr.query.query_flags = query_flags;
402 attr.query.prog_cnt = *prog_cnt;
403 attr.query.prog_ids = ptr_to_u64(prog_ids);
405 ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
407 *attach_flags = attr.query.attach_flags;
408 *prog_cnt = attr.query.prog_cnt;
412 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
413 void *data_out, __u32 *size_out, __u32 *retval,
419 bzero(&attr, sizeof(attr));
420 attr.test.prog_fd = prog_fd;
421 attr.test.data_in = ptr_to_u64(data);
422 attr.test.data_out = ptr_to_u64(data_out);
423 attr.test.data_size_in = size;
424 attr.test.repeat = repeat;
426 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
428 *size_out = attr.test.data_size_out;
430 *retval = attr.test.retval;
432 *duration = attr.test.duration;
436 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
441 bzero(&attr, sizeof(attr));
442 attr.start_id = start_id;
444 err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
446 *next_id = attr.next_id;
451 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
456 bzero(&attr, sizeof(attr));
457 attr.start_id = start_id;
459 err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
461 *next_id = attr.next_id;
466 int bpf_prog_get_fd_by_id(__u32 id)
470 bzero(&attr, sizeof(attr));
473 return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
476 int bpf_map_get_fd_by_id(__u32 id)
480 bzero(&attr, sizeof(attr));
483 return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
486 int bpf_btf_get_fd_by_id(__u32 id)
490 bzero(&attr, sizeof(attr));
493 return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
496 int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
501 bzero(&attr, sizeof(attr));
502 attr.info.bpf_fd = prog_fd;
503 attr.info.info_len = *info_len;
504 attr.info.info = ptr_to_u64(info);
506 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
508 *info_len = attr.info.info_len;
513 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
517 bzero(&attr, sizeof(attr));
518 attr.raw_tracepoint.name = ptr_to_u64(name);
519 attr.raw_tracepoint.prog_fd = prog_fd;
521 return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
524 int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags)
526 struct sockaddr_nl sa;
527 int sock, seq = 0, len, ret = -1;
529 struct nlattr *nla, *nla_xdp;
532 struct ifinfomsg ifinfo;
536 struct nlmsgerr *err;
540 memset(&sa, 0, sizeof(sa));
541 sa.nl_family = AF_NETLINK;
543 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
548 if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
549 &one, sizeof(one)) < 0) {
550 fprintf(stderr, "Netlink error reporting not supported\n");
553 if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
558 addrlen = sizeof(sa);
559 if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
564 if (addrlen != sizeof(sa)) {
565 ret = -LIBBPF_ERRNO__INTERNAL;
569 memset(&req, 0, sizeof(req));
570 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
571 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
572 req.nh.nlmsg_type = RTM_SETLINK;
573 req.nh.nlmsg_pid = 0;
574 req.nh.nlmsg_seq = ++seq;
575 req.ifinfo.ifi_family = AF_UNSPEC;
576 req.ifinfo.ifi_index = ifindex;
578 /* started nested attribute for XDP */
579 nla = (struct nlattr *)(((char *)&req)
580 + NLMSG_ALIGN(req.nh.nlmsg_len));
581 nla->nla_type = NLA_F_NESTED | IFLA_XDP;
582 nla->nla_len = NLA_HDRLEN;
585 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
586 nla_xdp->nla_type = IFLA_XDP_FD;
587 nla_xdp->nla_len = NLA_HDRLEN + sizeof(int);
588 memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd));
589 nla->nla_len += nla_xdp->nla_len;
591 /* if user passed in any flags, add those too */
593 nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len);
594 nla_xdp->nla_type = IFLA_XDP_FLAGS;
595 nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags);
596 memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags));
597 nla->nla_len += nla_xdp->nla_len;
600 req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len);
602 if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) {
607 len = recv(sock, buf, sizeof(buf), 0);
613 for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len);
614 nh = NLMSG_NEXT(nh, len)) {
615 if (nh->nlmsg_pid != sa.nl_pid) {
616 ret = -LIBBPF_ERRNO__WRNGPID;
619 if (nh->nlmsg_seq != seq) {
620 ret = -LIBBPF_ERRNO__INVSEQ;
623 switch (nh->nlmsg_type) {
625 err = (struct nlmsgerr *)NLMSG_DATA(nh);
629 nla_dump_errormsg(nh);
645 int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
648 union bpf_attr attr = {};
651 attr.btf = ptr_to_u64(btf);
652 attr.btf_size = btf_size;
655 if (do_log && log_buf && log_buf_size) {
656 attr.btf_log_level = 1;
657 attr.btf_log_size = log_buf_size;
658 attr.btf_log_buf = ptr_to_u64(log_buf);
661 fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
662 if (fd == -1 && !do_log && log_buf && log_buf_size) {
670 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
671 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
674 union bpf_attr attr = {};
677 attr.task_fd_query.pid = pid;
678 attr.task_fd_query.fd = fd;
679 attr.task_fd_query.flags = flags;
680 attr.task_fd_query.buf = ptr_to_u64(buf);
681 attr.task_fd_query.buf_len = *buf_len;
683 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
684 *buf_len = attr.task_fd_query.buf_len;
685 *prog_id = attr.task_fd_query.prog_id;
686 *fd_type = attr.task_fd_query.fd_type;
687 *probe_offset = attr.task_fd_query.probe_offset;
688 *probe_addr = attr.task_fd_query.probe_addr;