1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
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>
28 #include <asm/unistd.h>
30 #include <linux/bpf.h>
31 #include <linux/filter.h>
32 #include <linux/kernel.h>
34 #include <sys/resource.h>
37 #include "libbpf_internal.h"
40 * When building perf, unistd.h is overridden. __NR_bpf is
41 * required to be defined explicitly.
44 # if defined(__i386__)
46 # elif defined(__x86_64__)
48 # elif defined(__aarch64__)
50 # elif defined(__sparc__)
52 # elif defined(__s390__)
54 # elif defined(__arc__)
56 # elif defined(__mips__) && defined(_ABIO32)
57 # define __NR_bpf 4355
58 # elif defined(__mips__) && defined(_ABIN32)
59 # define __NR_bpf 6319
60 # elif defined(__mips__) && defined(_ABI64)
61 # define __NR_bpf 5315
63 # error __NR_bpf not defined. libbpf does not support your arch.
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_fd(enum bpf_cmd cmd, union bpf_attr *attr,
83 fd = sys_bpf(cmd, attr, size);
84 return ensure_good_fd(fd);
87 int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
92 fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size);
93 } while (fd < 0 && errno == EAGAIN && --attempts > 0);
98 /* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
99 * memcg-based memory accounting for BPF maps and progs. This was done in [0].
100 * We use the support for bpf_ktime_get_coarse_ns() helper, which was added in
101 * the same 5.11 Linux release ([1]), to detect memcg-based accounting for BPF.
103 * [0] https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
104 * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper")
106 int probe_memcg_account(void)
108 const size_t attr_sz = offsetofend(union bpf_attr, attach_btf_obj_fd);
109 struct bpf_insn insns[] = {
110 BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns),
113 size_t insn_cnt = ARRAY_SIZE(insns);
117 /* attempt loading freplace trying to use custom BTF */
118 memset(&attr, 0, attr_sz);
119 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
120 attr.insns = ptr_to_u64(insns);
121 attr.insn_cnt = insn_cnt;
122 attr.license = ptr_to_u64("GPL");
124 prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
132 static bool memlock_bumped;
133 static rlim_t memlock_rlim = RLIM_INFINITY;
135 int libbpf_set_memlock_rlim(size_t memlock_bytes)
138 return libbpf_err(-EBUSY);
140 memlock_rlim = memlock_bytes;
144 int bump_rlimit_memlock(void)
148 /* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */
149 if (memlock_bumped || kernel_supports(NULL, FEAT_MEMCG_ACCOUNT))
152 memlock_bumped = true;
154 /* zero memlock_rlim_max disables auto-bumping RLIMIT_MEMLOCK */
155 if (memlock_rlim == 0)
158 rlim.rlim_cur = rlim.rlim_max = memlock_rlim;
159 if (setrlimit(RLIMIT_MEMLOCK, &rlim))
165 int bpf_map_create(enum bpf_map_type map_type,
166 const char *map_name,
170 const struct bpf_map_create_opts *opts)
172 const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
176 bump_rlimit_memlock();
178 memset(&attr, 0, attr_sz);
180 if (!OPTS_VALID(opts, bpf_map_create_opts))
181 return libbpf_err(-EINVAL);
183 attr.map_type = map_type;
184 if (map_name && kernel_supports(NULL, FEAT_PROG_NAME))
185 libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
186 attr.key_size = key_size;
187 attr.value_size = value_size;
188 attr.max_entries = max_entries;
190 attr.btf_fd = OPTS_GET(opts, btf_fd, 0);
191 attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
192 attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
193 attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
195 attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
196 attr.map_flags = OPTS_GET(opts, map_flags, 0);
197 attr.map_extra = OPTS_GET(opts, map_extra, 0);
198 attr.numa_node = OPTS_GET(opts, numa_node, 0);
199 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
201 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
202 return libbpf_err_errno(fd);
206 alloc_zero_tailing_info(const void *orecord, __u32 cnt,
207 __u32 actual_rec_size, __u32 expected_rec_size)
209 __u64 info_len = (__u64)actual_rec_size * cnt;
210 void *info, *nrecord;
213 info = malloc(info_len);
217 /* zero out bytes kernel does not understand */
219 for (i = 0; i < cnt; i++) {
220 memcpy(nrecord, orecord, expected_rec_size);
221 memset(nrecord + expected_rec_size, 0,
222 actual_rec_size - expected_rec_size);
223 orecord += actual_rec_size;
224 nrecord += actual_rec_size;
230 int bpf_prog_load(enum bpf_prog_type prog_type,
231 const char *prog_name, const char *license,
232 const struct bpf_insn *insns, size_t insn_cnt,
233 struct bpf_prog_load_opts *opts)
235 const size_t attr_sz = offsetofend(union bpf_attr, log_true_size);
236 void *finfo = NULL, *linfo = NULL;
237 const char *func_info, *line_info;
238 __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
239 __u32 func_info_rec_size, line_info_rec_size;
244 bump_rlimit_memlock();
246 if (!OPTS_VALID(opts, bpf_prog_load_opts))
247 return libbpf_err(-EINVAL);
249 attempts = OPTS_GET(opts, attempts, 0);
251 return libbpf_err(-EINVAL);
253 attempts = PROG_LOAD_ATTEMPTS;
255 memset(&attr, 0, attr_sz);
257 attr.prog_type = prog_type;
258 attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
260 attr.prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
261 attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
262 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
263 attr.kern_version = OPTS_GET(opts, kern_version, 0);
265 if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
266 libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
267 attr.license = ptr_to_u64(license);
269 if (insn_cnt > UINT_MAX)
270 return libbpf_err(-E2BIG);
272 attr.insns = ptr_to_u64(insns);
273 attr.insn_cnt = (__u32)insn_cnt;
275 attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
276 attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0);
278 if (attach_prog_fd && attach_btf_obj_fd)
279 return libbpf_err(-EINVAL);
281 attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0);
283 attr.attach_prog_fd = attach_prog_fd;
285 attr.attach_btf_obj_fd = attach_btf_obj_fd;
287 log_buf = OPTS_GET(opts, log_buf, NULL);
288 log_size = OPTS_GET(opts, log_size, 0);
289 log_level = OPTS_GET(opts, log_level, 0);
291 if (!!log_buf != !!log_size)
292 return libbpf_err(-EINVAL);
294 func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0);
295 func_info = OPTS_GET(opts, func_info, NULL);
296 attr.func_info_rec_size = func_info_rec_size;
297 attr.func_info = ptr_to_u64(func_info);
298 attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0);
300 line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0);
301 line_info = OPTS_GET(opts, line_info, NULL);
302 attr.line_info_rec_size = line_info_rec_size;
303 attr.line_info = ptr_to_u64(line_info);
304 attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0);
306 attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
309 attr.log_buf = ptr_to_u64(log_buf);
310 attr.log_size = log_size;
311 attr.log_level = log_level;
314 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
315 OPTS_SET(opts, log_true_size, attr.log_true_size);
319 /* After bpf_prog_load, the kernel may modify certain attributes
320 * to give user space a hint how to deal with loading failure.
321 * Check to see whether we can make some changes and load again.
323 while (errno == E2BIG && (!finfo || !linfo)) {
324 if (!finfo && attr.func_info_cnt &&
325 attr.func_info_rec_size < func_info_rec_size) {
326 /* try with corrected func info records */
327 finfo = alloc_zero_tailing_info(func_info,
330 attr.func_info_rec_size);
336 attr.func_info = ptr_to_u64(finfo);
337 attr.func_info_rec_size = func_info_rec_size;
338 } else if (!linfo && attr.line_info_cnt &&
339 attr.line_info_rec_size < line_info_rec_size) {
340 linfo = alloc_zero_tailing_info(line_info,
343 attr.line_info_rec_size);
349 attr.line_info = ptr_to_u64(linfo);
350 attr.line_info_rec_size = line_info_rec_size;
355 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
356 OPTS_SET(opts, log_true_size, attr.log_true_size);
361 if (log_level == 0 && log_buf) {
362 /* log_level == 0 with non-NULL log_buf requires retrying on error
363 * with log_level == 1 and log_buf/log_buf_size set, to get details of
366 attr.log_buf = ptr_to_u64(log_buf);
367 attr.log_size = log_size;
370 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
371 OPTS_SET(opts, log_true_size, attr.log_true_size);
374 /* free() doesn't affect errno, so we don't need to restore it */
377 return libbpf_err_errno(fd);
380 int bpf_map_update_elem(int fd, const void *key, const void *value,
383 const size_t attr_sz = offsetofend(union bpf_attr, flags);
387 memset(&attr, 0, attr_sz);
389 attr.key = ptr_to_u64(key);
390 attr.value = ptr_to_u64(value);
393 ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
394 return libbpf_err_errno(ret);
397 int bpf_map_lookup_elem(int fd, const void *key, void *value)
399 const size_t attr_sz = offsetofend(union bpf_attr, flags);
403 memset(&attr, 0, attr_sz);
405 attr.key = ptr_to_u64(key);
406 attr.value = ptr_to_u64(value);
408 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
409 return libbpf_err_errno(ret);
412 int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
414 const size_t attr_sz = offsetofend(union bpf_attr, flags);
418 memset(&attr, 0, attr_sz);
420 attr.key = ptr_to_u64(key);
421 attr.value = ptr_to_u64(value);
424 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
425 return libbpf_err_errno(ret);
428 int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
430 const size_t attr_sz = offsetofend(union bpf_attr, flags);
434 memset(&attr, 0, attr_sz);
436 attr.key = ptr_to_u64(key);
437 attr.value = ptr_to_u64(value);
439 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
440 return libbpf_err_errno(ret);
443 int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
445 const size_t attr_sz = offsetofend(union bpf_attr, flags);
449 memset(&attr, 0, attr_sz);
451 attr.key = ptr_to_u64(key);
452 attr.value = ptr_to_u64(value);
455 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
456 return libbpf_err_errno(ret);
459 int bpf_map_delete_elem(int fd, const void *key)
461 const size_t attr_sz = offsetofend(union bpf_attr, flags);
465 memset(&attr, 0, attr_sz);
467 attr.key = ptr_to_u64(key);
469 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
470 return libbpf_err_errno(ret);
473 int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
475 const size_t attr_sz = offsetofend(union bpf_attr, flags);
479 memset(&attr, 0, attr_sz);
481 attr.key = ptr_to_u64(key);
484 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
485 return libbpf_err_errno(ret);
488 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
490 const size_t attr_sz = offsetofend(union bpf_attr, next_key);
494 memset(&attr, 0, attr_sz);
496 attr.key = ptr_to_u64(key);
497 attr.next_key = ptr_to_u64(next_key);
499 ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
500 return libbpf_err_errno(ret);
503 int bpf_map_freeze(int fd)
505 const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
509 memset(&attr, 0, attr_sz);
512 ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
513 return libbpf_err_errno(ret);
516 static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
517 void *out_batch, void *keys, void *values,
519 const struct bpf_map_batch_opts *opts)
521 const size_t attr_sz = offsetofend(union bpf_attr, batch);
525 if (!OPTS_VALID(opts, bpf_map_batch_opts))
526 return libbpf_err(-EINVAL);
528 memset(&attr, 0, attr_sz);
529 attr.batch.map_fd = fd;
530 attr.batch.in_batch = ptr_to_u64(in_batch);
531 attr.batch.out_batch = ptr_to_u64(out_batch);
532 attr.batch.keys = ptr_to_u64(keys);
533 attr.batch.values = ptr_to_u64(values);
534 attr.batch.count = *count;
535 attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
536 attr.batch.flags = OPTS_GET(opts, flags, 0);
538 ret = sys_bpf(cmd, &attr, attr_sz);
539 *count = attr.batch.count;
541 return libbpf_err_errno(ret);
544 int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
545 const struct bpf_map_batch_opts *opts)
547 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
548 NULL, (void *)keys, NULL, count, opts);
551 int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
552 void *values, __u32 *count,
553 const struct bpf_map_batch_opts *opts)
555 return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
556 out_batch, keys, values, count, opts);
559 int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
560 void *keys, void *values, __u32 *count,
561 const struct bpf_map_batch_opts *opts)
563 return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
564 fd, in_batch, out_batch, keys, values,
568 int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
569 const struct bpf_map_batch_opts *opts)
571 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
572 (void *)keys, (void *)values, count, opts);
575 int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts)
577 const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
581 if (!OPTS_VALID(opts, bpf_obj_pin_opts))
582 return libbpf_err(-EINVAL);
584 memset(&attr, 0, attr_sz);
585 attr.path_fd = OPTS_GET(opts, path_fd, 0);
586 attr.pathname = ptr_to_u64((void *)pathname);
587 attr.file_flags = OPTS_GET(opts, file_flags, 0);
590 ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
591 return libbpf_err_errno(ret);
594 int bpf_obj_pin(int fd, const char *pathname)
596 return bpf_obj_pin_opts(fd, pathname, NULL);
599 int bpf_obj_get(const char *pathname)
601 return bpf_obj_get_opts(pathname, NULL);
604 int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
606 const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
610 if (!OPTS_VALID(opts, bpf_obj_get_opts))
611 return libbpf_err(-EINVAL);
613 memset(&attr, 0, attr_sz);
614 attr.path_fd = OPTS_GET(opts, path_fd, 0);
615 attr.pathname = ptr_to_u64((void *)pathname);
616 attr.file_flags = OPTS_GET(opts, file_flags, 0);
618 fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
619 return libbpf_err_errno(fd);
622 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
625 DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
629 return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts);
632 int bpf_prog_attach_opts(int prog_fd, int target, enum bpf_attach_type type,
633 const struct bpf_prog_attach_opts *opts)
635 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
636 __u32 relative_id, flags;
637 int ret, relative_fd;
640 if (!OPTS_VALID(opts, bpf_prog_attach_opts))
641 return libbpf_err(-EINVAL);
643 relative_id = OPTS_GET(opts, relative_id, 0);
644 relative_fd = OPTS_GET(opts, relative_fd, 0);
645 flags = OPTS_GET(opts, flags, 0);
647 /* validate we don't have unexpected combinations of non-zero fields */
648 if (relative_fd && relative_id)
649 return libbpf_err(-EINVAL);
651 memset(&attr, 0, attr_sz);
652 attr.target_fd = target;
653 attr.attach_bpf_fd = prog_fd;
654 attr.attach_type = type;
655 attr.replace_bpf_fd = OPTS_GET(opts, replace_fd, 0);
656 attr.expected_revision = OPTS_GET(opts, expected_revision, 0);
659 attr.attach_flags = flags | BPF_F_ID;
660 attr.relative_id = relative_id;
662 attr.attach_flags = flags;
663 attr.relative_fd = relative_fd;
666 ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
667 return libbpf_err_errno(ret);
670 int bpf_prog_detach_opts(int prog_fd, int target, enum bpf_attach_type type,
671 const struct bpf_prog_detach_opts *opts)
673 const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
674 __u32 relative_id, flags;
675 int ret, relative_fd;
678 if (!OPTS_VALID(opts, bpf_prog_detach_opts))
679 return libbpf_err(-EINVAL);
681 relative_id = OPTS_GET(opts, relative_id, 0);
682 relative_fd = OPTS_GET(opts, relative_fd, 0);
683 flags = OPTS_GET(opts, flags, 0);
685 /* validate we don't have unexpected combinations of non-zero fields */
686 if (relative_fd && relative_id)
687 return libbpf_err(-EINVAL);
689 memset(&attr, 0, attr_sz);
690 attr.target_fd = target;
691 attr.attach_bpf_fd = prog_fd;
692 attr.attach_type = type;
693 attr.expected_revision = OPTS_GET(opts, expected_revision, 0);
696 attr.attach_flags = flags | BPF_F_ID;
697 attr.relative_id = relative_id;
699 attr.attach_flags = flags;
700 attr.relative_fd = relative_fd;
703 ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
704 return libbpf_err_errno(ret);
707 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
709 return bpf_prog_detach_opts(0, target_fd, type, NULL);
712 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
714 return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
717 int bpf_link_create(int prog_fd, int target_fd,
718 enum bpf_attach_type attach_type,
719 const struct bpf_link_create_opts *opts)
721 const size_t attr_sz = offsetofend(union bpf_attr, link_create);
722 __u32 target_btf_id, iter_info_len, relative_id;
723 int fd, err, relative_fd;
726 if (!OPTS_VALID(opts, bpf_link_create_opts))
727 return libbpf_err(-EINVAL);
729 iter_info_len = OPTS_GET(opts, iter_info_len, 0);
730 target_btf_id = OPTS_GET(opts, target_btf_id, 0);
732 /* validate we don't have unexpected combinations of non-zero fields */
733 if (iter_info_len || target_btf_id) {
734 if (iter_info_len && target_btf_id)
735 return libbpf_err(-EINVAL);
736 if (!OPTS_ZEROED(opts, target_btf_id))
737 return libbpf_err(-EINVAL);
740 memset(&attr, 0, attr_sz);
741 attr.link_create.prog_fd = prog_fd;
742 attr.link_create.target_fd = target_fd;
743 attr.link_create.attach_type = attach_type;
744 attr.link_create.flags = OPTS_GET(opts, flags, 0);
747 attr.link_create.target_btf_id = target_btf_id;
751 switch (attach_type) {
753 attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
754 attr.link_create.iter_info_len = iter_info_len;
757 attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0);
758 if (!OPTS_ZEROED(opts, perf_event))
759 return libbpf_err(-EINVAL);
761 case BPF_TRACE_KPROBE_MULTI:
762 attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0);
763 attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0);
764 attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0));
765 attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0));
766 attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0));
767 if (!OPTS_ZEROED(opts, kprobe_multi))
768 return libbpf_err(-EINVAL);
770 case BPF_TRACE_UPROBE_MULTI:
771 attr.link_create.uprobe_multi.flags = OPTS_GET(opts, uprobe_multi.flags, 0);
772 attr.link_create.uprobe_multi.cnt = OPTS_GET(opts, uprobe_multi.cnt, 0);
773 attr.link_create.uprobe_multi.path = ptr_to_u64(OPTS_GET(opts, uprobe_multi.path, 0));
774 attr.link_create.uprobe_multi.offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.offsets, 0));
775 attr.link_create.uprobe_multi.ref_ctr_offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.ref_ctr_offsets, 0));
776 attr.link_create.uprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, uprobe_multi.cookies, 0));
777 attr.link_create.uprobe_multi.pid = OPTS_GET(opts, uprobe_multi.pid, 0);
778 if (!OPTS_ZEROED(opts, uprobe_multi))
779 return libbpf_err(-EINVAL);
781 case BPF_TRACE_FENTRY:
782 case BPF_TRACE_FEXIT:
783 case BPF_MODIFY_RETURN:
785 attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
786 if (!OPTS_ZEROED(opts, tracing))
787 return libbpf_err(-EINVAL);
790 attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0);
791 attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0);
792 attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0);
793 attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0);
794 if (!OPTS_ZEROED(opts, netfilter))
795 return libbpf_err(-EINVAL);
797 case BPF_TCX_INGRESS:
799 relative_fd = OPTS_GET(opts, tcx.relative_fd, 0);
800 relative_id = OPTS_GET(opts, tcx.relative_id, 0);
801 if (relative_fd && relative_id)
802 return libbpf_err(-EINVAL);
804 attr.link_create.tcx.relative_id = relative_id;
805 attr.link_create.flags |= BPF_F_ID;
807 attr.link_create.tcx.relative_fd = relative_fd;
809 attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0);
810 if (!OPTS_ZEROED(opts, tcx))
811 return libbpf_err(-EINVAL);
814 if (!OPTS_ZEROED(opts, flags))
815 return libbpf_err(-EINVAL);
819 fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
822 /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
823 * and other similar programs
827 return libbpf_err(err);
829 /* if user used features not supported by
830 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
832 if (attr.link_create.target_fd || attr.link_create.target_btf_id)
833 return libbpf_err(err);
834 if (!OPTS_ZEROED(opts, sz))
835 return libbpf_err(err);
837 /* otherwise, for few select kinds of programs that can be
838 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
839 * a fallback for older kernels
841 switch (attach_type) {
842 case BPF_TRACE_RAW_TP:
844 case BPF_TRACE_FENTRY:
845 case BPF_TRACE_FEXIT:
846 case BPF_MODIFY_RETURN:
847 return bpf_raw_tracepoint_open(NULL, prog_fd);
849 return libbpf_err(err);
853 int bpf_link_detach(int link_fd)
855 const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
859 memset(&attr, 0, attr_sz);
860 attr.link_detach.link_fd = link_fd;
862 ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
863 return libbpf_err_errno(ret);
866 int bpf_link_update(int link_fd, int new_prog_fd,
867 const struct bpf_link_update_opts *opts)
869 const size_t attr_sz = offsetofend(union bpf_attr, link_update);
873 if (!OPTS_VALID(opts, bpf_link_update_opts))
874 return libbpf_err(-EINVAL);
876 if (OPTS_GET(opts, old_prog_fd, 0) && OPTS_GET(opts, old_map_fd, 0))
877 return libbpf_err(-EINVAL);
879 memset(&attr, 0, attr_sz);
880 attr.link_update.link_fd = link_fd;
881 attr.link_update.new_prog_fd = new_prog_fd;
882 attr.link_update.flags = OPTS_GET(opts, flags, 0);
883 if (OPTS_GET(opts, old_prog_fd, 0))
884 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
885 else if (OPTS_GET(opts, old_map_fd, 0))
886 attr.link_update.old_map_fd = OPTS_GET(opts, old_map_fd, 0);
888 ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
889 return libbpf_err_errno(ret);
892 int bpf_iter_create(int link_fd)
894 const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
898 memset(&attr, 0, attr_sz);
899 attr.iter_create.link_fd = link_fd;
901 fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
902 return libbpf_err_errno(fd);
905 int bpf_prog_query_opts(int target, enum bpf_attach_type type,
906 struct bpf_prog_query_opts *opts)
908 const size_t attr_sz = offsetofend(union bpf_attr, query);
912 if (!OPTS_VALID(opts, bpf_prog_query_opts))
913 return libbpf_err(-EINVAL);
915 memset(&attr, 0, attr_sz);
916 attr.query.target_fd = target;
917 attr.query.attach_type = type;
918 attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
919 attr.query.count = OPTS_GET(opts, count, 0);
920 attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
921 attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL));
922 attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
923 attr.query.link_attach_flags = ptr_to_u64(OPTS_GET(opts, link_attach_flags, NULL));
925 ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
927 OPTS_SET(opts, attach_flags, attr.query.attach_flags);
928 OPTS_SET(opts, revision, attr.query.revision);
929 OPTS_SET(opts, count, attr.query.count);
931 return libbpf_err_errno(ret);
934 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
935 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
937 LIBBPF_OPTS(bpf_prog_query_opts, opts);
940 opts.query_flags = query_flags;
941 opts.prog_ids = prog_ids;
942 opts.prog_cnt = *prog_cnt;
944 ret = bpf_prog_query_opts(target_fd, type, &opts);
947 *attach_flags = opts.attach_flags;
948 *prog_cnt = opts.prog_cnt;
950 return libbpf_err_errno(ret);
953 int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
955 const size_t attr_sz = offsetofend(union bpf_attr, test);
959 if (!OPTS_VALID(opts, bpf_test_run_opts))
960 return libbpf_err(-EINVAL);
962 memset(&attr, 0, attr_sz);
963 attr.test.prog_fd = prog_fd;
964 attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
965 attr.test.cpu = OPTS_GET(opts, cpu, 0);
966 attr.test.flags = OPTS_GET(opts, flags, 0);
967 attr.test.repeat = OPTS_GET(opts, repeat, 0);
968 attr.test.duration = OPTS_GET(opts, duration, 0);
969 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
970 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
971 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
972 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
973 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
974 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
975 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
976 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
978 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
980 OPTS_SET(opts, data_size_out, attr.test.data_size_out);
981 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
982 OPTS_SET(opts, duration, attr.test.duration);
983 OPTS_SET(opts, retval, attr.test.retval);
985 return libbpf_err_errno(ret);
988 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
990 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
994 memset(&attr, 0, attr_sz);
995 attr.start_id = start_id;
997 err = sys_bpf(cmd, &attr, attr_sz);
999 *next_id = attr.next_id;
1001 return libbpf_err_errno(err);
1004 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
1006 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
1009 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
1011 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
1014 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
1016 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
1019 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
1021 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
1024 int bpf_prog_get_fd_by_id_opts(__u32 id,
1025 const struct bpf_get_fd_by_id_opts *opts)
1027 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1028 union bpf_attr attr;
1031 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1032 return libbpf_err(-EINVAL);
1034 memset(&attr, 0, attr_sz);
1036 attr.open_flags = OPTS_GET(opts, open_flags, 0);
1038 fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
1039 return libbpf_err_errno(fd);
1042 int bpf_prog_get_fd_by_id(__u32 id)
1044 return bpf_prog_get_fd_by_id_opts(id, NULL);
1047 int bpf_map_get_fd_by_id_opts(__u32 id,
1048 const struct bpf_get_fd_by_id_opts *opts)
1050 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1051 union bpf_attr attr;
1054 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1055 return libbpf_err(-EINVAL);
1057 memset(&attr, 0, attr_sz);
1059 attr.open_flags = OPTS_GET(opts, open_flags, 0);
1061 fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
1062 return libbpf_err_errno(fd);
1065 int bpf_map_get_fd_by_id(__u32 id)
1067 return bpf_map_get_fd_by_id_opts(id, NULL);
1070 int bpf_btf_get_fd_by_id_opts(__u32 id,
1071 const struct bpf_get_fd_by_id_opts *opts)
1073 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1074 union bpf_attr attr;
1077 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1078 return libbpf_err(-EINVAL);
1080 memset(&attr, 0, attr_sz);
1082 attr.open_flags = OPTS_GET(opts, open_flags, 0);
1084 fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
1085 return libbpf_err_errno(fd);
1088 int bpf_btf_get_fd_by_id(__u32 id)
1090 return bpf_btf_get_fd_by_id_opts(id, NULL);
1093 int bpf_link_get_fd_by_id_opts(__u32 id,
1094 const struct bpf_get_fd_by_id_opts *opts)
1096 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1097 union bpf_attr attr;
1100 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1101 return libbpf_err(-EINVAL);
1103 memset(&attr, 0, attr_sz);
1105 attr.open_flags = OPTS_GET(opts, open_flags, 0);
1107 fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
1108 return libbpf_err_errno(fd);
1111 int bpf_link_get_fd_by_id(__u32 id)
1113 return bpf_link_get_fd_by_id_opts(id, NULL);
1116 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
1118 const size_t attr_sz = offsetofend(union bpf_attr, info);
1119 union bpf_attr attr;
1122 memset(&attr, 0, attr_sz);
1123 attr.info.bpf_fd = bpf_fd;
1124 attr.info.info_len = *info_len;
1125 attr.info.info = ptr_to_u64(info);
1127 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
1129 *info_len = attr.info.info_len;
1130 return libbpf_err_errno(err);
1133 int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len)
1135 return bpf_obj_get_info_by_fd(prog_fd, info, info_len);
1138 int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len)
1140 return bpf_obj_get_info_by_fd(map_fd, info, info_len);
1143 int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len)
1145 return bpf_obj_get_info_by_fd(btf_fd, info, info_len);
1148 int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len)
1150 return bpf_obj_get_info_by_fd(link_fd, info, info_len);
1153 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
1155 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
1156 union bpf_attr attr;
1159 memset(&attr, 0, attr_sz);
1160 attr.raw_tracepoint.name = ptr_to_u64(name);
1161 attr.raw_tracepoint.prog_fd = prog_fd;
1163 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
1164 return libbpf_err_errno(fd);
1167 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
1169 const size_t attr_sz = offsetofend(union bpf_attr, btf_log_true_size);
1170 union bpf_attr attr;
1176 bump_rlimit_memlock();
1178 memset(&attr, 0, attr_sz);
1180 if (!OPTS_VALID(opts, bpf_btf_load_opts))
1181 return libbpf_err(-EINVAL);
1183 log_buf = OPTS_GET(opts, log_buf, NULL);
1184 log_size = OPTS_GET(opts, log_size, 0);
1185 log_level = OPTS_GET(opts, log_level, 0);
1187 if (log_size > UINT_MAX)
1188 return libbpf_err(-EINVAL);
1189 if (log_size && !log_buf)
1190 return libbpf_err(-EINVAL);
1192 attr.btf = ptr_to_u64(btf_data);
1193 attr.btf_size = btf_size;
1194 /* log_level == 0 and log_buf != NULL means "try loading without
1195 * log_buf, but retry with log_buf and log_level=1 on error", which is
1196 * consistent across low-level and high-level BTF and program loading
1197 * APIs within libbpf and provides a sensible behavior in practice
1200 attr.btf_log_buf = ptr_to_u64(log_buf);
1201 attr.btf_log_size = (__u32)log_size;
1202 attr.btf_log_level = log_level;
1205 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1206 if (fd < 0 && log_buf && log_level == 0) {
1207 attr.btf_log_buf = ptr_to_u64(log_buf);
1208 attr.btf_log_size = (__u32)log_size;
1209 attr.btf_log_level = 1;
1210 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1213 OPTS_SET(opts, log_true_size, attr.btf_log_true_size);
1214 return libbpf_err_errno(fd);
1217 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1218 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1221 const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
1222 union bpf_attr attr;
1225 memset(&attr, 0, attr_sz);
1226 attr.task_fd_query.pid = pid;
1227 attr.task_fd_query.fd = fd;
1228 attr.task_fd_query.flags = flags;
1229 attr.task_fd_query.buf = ptr_to_u64(buf);
1230 attr.task_fd_query.buf_len = *buf_len;
1232 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
1234 *buf_len = attr.task_fd_query.buf_len;
1235 *prog_id = attr.task_fd_query.prog_id;
1236 *fd_type = attr.task_fd_query.fd_type;
1237 *probe_offset = attr.task_fd_query.probe_offset;
1238 *probe_addr = attr.task_fd_query.probe_addr;
1240 return libbpf_err_errno(err);
1243 int bpf_enable_stats(enum bpf_stats_type type)
1245 const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
1246 union bpf_attr attr;
1249 memset(&attr, 0, attr_sz);
1250 attr.enable_stats.type = type;
1252 fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
1253 return libbpf_err_errno(fd);
1256 int bpf_prog_bind_map(int prog_fd, int map_fd,
1257 const struct bpf_prog_bind_opts *opts)
1259 const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
1260 union bpf_attr attr;
1263 if (!OPTS_VALID(opts, bpf_prog_bind_opts))
1264 return libbpf_err(-EINVAL);
1266 memset(&attr, 0, attr_sz);
1267 attr.prog_bind_map.prog_fd = prog_fd;
1268 attr.prog_bind_map.map_fd = map_fd;
1269 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1271 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
1272 return libbpf_err_errno(ret);