Loading...
1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3/*
4 * common eBPF ELF operations.
5 *
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.
9 *
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!)
14 *
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.
19 *
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>
22 */
23
24#include <stdlib.h>
25#include <string.h>
26#include <memory.h>
27#include <unistd.h>
28#include <asm/unistd.h>
29#include <errno.h>
30#include <linux/bpf.h>
31#include <linux/filter.h>
32#include <linux/kernel.h>
33#include <limits.h>
34#include <sys/resource.h>
35#include "bpf.h"
36#include "libbpf.h"
37#include "libbpf_internal.h"
38
39/*
40 * When building perf, unistd.h is overridden. __NR_bpf is
41 * required to be defined explicitly.
42 */
43#ifndef __NR_bpf
44# if defined(__i386__)
45# define __NR_bpf 357
46# elif defined(__x86_64__)
47# define __NR_bpf 321
48# elif defined(__aarch64__)
49# define __NR_bpf 280
50# elif defined(__sparc__)
51# define __NR_bpf 349
52# elif defined(__s390__)
53# define __NR_bpf 351
54# elif defined(__arc__)
55# define __NR_bpf 280
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
62# else
63# error __NR_bpf not defined. libbpf does not support your arch.
64# endif
65#endif
66
67static inline __u64 ptr_to_u64(const void *ptr)
68{
69 return (__u64) (unsigned long) ptr;
70}
71
72static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
73 unsigned int size)
74{
75 return syscall(__NR_bpf, cmd, attr, size);
76}
77
78static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,
79 unsigned int size)
80{
81 int fd;
82
83 fd = sys_bpf(cmd, attr, size);
84 return ensure_good_fd(fd);
85}
86
87int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
88{
89 int fd;
90
91 do {
92 fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size);
93 } while (fd < 0 && errno == EAGAIN && --attempts > 0);
94
95 return fd;
96}
97
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.
102 *
103 * [0] https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
104 * [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper")
105 */
106int probe_memcg_account(void)
107{
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),
111 BPF_EXIT_INSN(),
112 };
113 size_t insn_cnt = ARRAY_SIZE(insns);
114 union bpf_attr attr;
115 int prog_fd;
116
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");
123
124 prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
125 if (prog_fd >= 0) {
126 close(prog_fd);
127 return 1;
128 }
129 return 0;
130}
131
132static bool memlock_bumped;
133static rlim_t memlock_rlim = RLIM_INFINITY;
134
135int libbpf_set_memlock_rlim(size_t memlock_bytes)
136{
137 if (memlock_bumped)
138 return libbpf_err(-EBUSY);
139
140 memlock_rlim = memlock_bytes;
141 return 0;
142}
143
144int bump_rlimit_memlock(void)
145{
146 struct rlimit rlim;
147
148 /* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */
149 if (memlock_bumped || kernel_supports(NULL, FEAT_MEMCG_ACCOUNT))
150 return 0;
151
152 memlock_bumped = true;
153
154 /* zero memlock_rlim_max disables auto-bumping RLIMIT_MEMLOCK */
155 if (memlock_rlim == 0)
156 return 0;
157
158 rlim.rlim_cur = rlim.rlim_max = memlock_rlim;
159 if (setrlimit(RLIMIT_MEMLOCK, &rlim))
160 return -errno;
161
162 return 0;
163}
164
165int bpf_map_create(enum bpf_map_type map_type,
166 const char *map_name,
167 __u32 key_size,
168 __u32 value_size,
169 __u32 max_entries,
170 const struct bpf_map_create_opts *opts)
171{
172 const size_t attr_sz = offsetofend(union bpf_attr, map_extra);
173 union bpf_attr attr;
174 int fd;
175
176 bump_rlimit_memlock();
177
178 memset(&attr, 0, attr_sz);
179
180 if (!OPTS_VALID(opts, bpf_map_create_opts))
181 return libbpf_err(-EINVAL);
182
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;
189
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);
194
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);
200
201 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
202 return libbpf_err_errno(fd);
203}
204
205static void *
206alloc_zero_tailing_info(const void *orecord, __u32 cnt,
207 __u32 actual_rec_size, __u32 expected_rec_size)
208{
209 __u64 info_len = (__u64)actual_rec_size * cnt;
210 void *info, *nrecord;
211 int i;
212
213 info = malloc(info_len);
214 if (!info)
215 return NULL;
216
217 /* zero out bytes kernel does not understand */
218 nrecord = info;
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;
225 }
226
227 return info;
228}
229
230int 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 const struct bpf_prog_load_opts *opts)
234{
235 const size_t attr_sz = offsetofend(union bpf_attr, fd_array);
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;
240 int fd, attempts;
241 union bpf_attr attr;
242 char *log_buf;
243
244 bump_rlimit_memlock();
245
246 if (!OPTS_VALID(opts, bpf_prog_load_opts))
247 return libbpf_err(-EINVAL);
248
249 attempts = OPTS_GET(opts, attempts, 0);
250 if (attempts < 0)
251 return libbpf_err(-EINVAL);
252 if (attempts == 0)
253 attempts = PROG_LOAD_ATTEMPTS;
254
255 memset(&attr, 0, attr_sz);
256
257 attr.prog_type = prog_type;
258 attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
259
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);
264
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);
268
269 if (insn_cnt > UINT_MAX)
270 return libbpf_err(-E2BIG);
271
272 attr.insns = ptr_to_u64(insns);
273 attr.insn_cnt = (__u32)insn_cnt;
274
275 attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
276 attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0);
277
278 if (attach_prog_fd && attach_btf_obj_fd)
279 return libbpf_err(-EINVAL);
280
281 attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0);
282 if (attach_prog_fd)
283 attr.attach_prog_fd = attach_prog_fd;
284 else
285 attr.attach_btf_obj_fd = attach_btf_obj_fd;
286
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);
290
291 if (!!log_buf != !!log_size)
292 return libbpf_err(-EINVAL);
293 if (log_level > (4 | 2 | 1))
294 return libbpf_err(-EINVAL);
295 if (log_level && !log_buf)
296 return libbpf_err(-EINVAL);
297
298 func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0);
299 func_info = OPTS_GET(opts, func_info, NULL);
300 attr.func_info_rec_size = func_info_rec_size;
301 attr.func_info = ptr_to_u64(func_info);
302 attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0);
303
304 line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0);
305 line_info = OPTS_GET(opts, line_info, NULL);
306 attr.line_info_rec_size = line_info_rec_size;
307 attr.line_info = ptr_to_u64(line_info);
308 attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0);
309
310 attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
311
312 if (log_level) {
313 attr.log_buf = ptr_to_u64(log_buf);
314 attr.log_size = log_size;
315 attr.log_level = log_level;
316 }
317
318 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
319 if (fd >= 0)
320 return fd;
321
322 /* After bpf_prog_load, the kernel may modify certain attributes
323 * to give user space a hint how to deal with loading failure.
324 * Check to see whether we can make some changes and load again.
325 */
326 while (errno == E2BIG && (!finfo || !linfo)) {
327 if (!finfo && attr.func_info_cnt &&
328 attr.func_info_rec_size < func_info_rec_size) {
329 /* try with corrected func info records */
330 finfo = alloc_zero_tailing_info(func_info,
331 attr.func_info_cnt,
332 func_info_rec_size,
333 attr.func_info_rec_size);
334 if (!finfo) {
335 errno = E2BIG;
336 goto done;
337 }
338
339 attr.func_info = ptr_to_u64(finfo);
340 attr.func_info_rec_size = func_info_rec_size;
341 } else if (!linfo && attr.line_info_cnt &&
342 attr.line_info_rec_size < line_info_rec_size) {
343 linfo = alloc_zero_tailing_info(line_info,
344 attr.line_info_cnt,
345 line_info_rec_size,
346 attr.line_info_rec_size);
347 if (!linfo) {
348 errno = E2BIG;
349 goto done;
350 }
351
352 attr.line_info = ptr_to_u64(linfo);
353 attr.line_info_rec_size = line_info_rec_size;
354 } else {
355 break;
356 }
357
358 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
359 if (fd >= 0)
360 goto done;
361 }
362
363 if (log_level == 0 && log_buf) {
364 /* log_level == 0 with non-NULL log_buf requires retrying on error
365 * with log_level == 1 and log_buf/log_buf_size set, to get details of
366 * failure
367 */
368 attr.log_buf = ptr_to_u64(log_buf);
369 attr.log_size = log_size;
370 attr.log_level = 1;
371
372 fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
373 }
374done:
375 /* free() doesn't affect errno, so we don't need to restore it */
376 free(finfo);
377 free(linfo);
378 return libbpf_err_errno(fd);
379}
380
381int bpf_map_update_elem(int fd, const void *key, const void *value,
382 __u64 flags)
383{
384 const size_t attr_sz = offsetofend(union bpf_attr, flags);
385 union bpf_attr attr;
386 int ret;
387
388 memset(&attr, 0, attr_sz);
389 attr.map_fd = fd;
390 attr.key = ptr_to_u64(key);
391 attr.value = ptr_to_u64(value);
392 attr.flags = flags;
393
394 ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
395 return libbpf_err_errno(ret);
396}
397
398int bpf_map_lookup_elem(int fd, const void *key, void *value)
399{
400 const size_t attr_sz = offsetofend(union bpf_attr, flags);
401 union bpf_attr attr;
402 int ret;
403
404 memset(&attr, 0, attr_sz);
405 attr.map_fd = fd;
406 attr.key = ptr_to_u64(key);
407 attr.value = ptr_to_u64(value);
408
409 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
410 return libbpf_err_errno(ret);
411}
412
413int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
414{
415 const size_t attr_sz = offsetofend(union bpf_attr, flags);
416 union bpf_attr attr;
417 int ret;
418
419 memset(&attr, 0, attr_sz);
420 attr.map_fd = fd;
421 attr.key = ptr_to_u64(key);
422 attr.value = ptr_to_u64(value);
423 attr.flags = flags;
424
425 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
426 return libbpf_err_errno(ret);
427}
428
429int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
430{
431 const size_t attr_sz = offsetofend(union bpf_attr, flags);
432 union bpf_attr attr;
433 int ret;
434
435 memset(&attr, 0, attr_sz);
436 attr.map_fd = fd;
437 attr.key = ptr_to_u64(key);
438 attr.value = ptr_to_u64(value);
439
440 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
441 return libbpf_err_errno(ret);
442}
443
444int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
445{
446 const size_t attr_sz = offsetofend(union bpf_attr, flags);
447 union bpf_attr attr;
448 int ret;
449
450 memset(&attr, 0, attr_sz);
451 attr.map_fd = fd;
452 attr.key = ptr_to_u64(key);
453 attr.value = ptr_to_u64(value);
454 attr.flags = flags;
455
456 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
457 return libbpf_err_errno(ret);
458}
459
460int bpf_map_delete_elem(int fd, const void *key)
461{
462 const size_t attr_sz = offsetofend(union bpf_attr, flags);
463 union bpf_attr attr;
464 int ret;
465
466 memset(&attr, 0, attr_sz);
467 attr.map_fd = fd;
468 attr.key = ptr_to_u64(key);
469
470 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
471 return libbpf_err_errno(ret);
472}
473
474int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
475{
476 const size_t attr_sz = offsetofend(union bpf_attr, flags);
477 union bpf_attr attr;
478 int ret;
479
480 memset(&attr, 0, attr_sz);
481 attr.map_fd = fd;
482 attr.key = ptr_to_u64(key);
483 attr.flags = flags;
484
485 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
486 return libbpf_err_errno(ret);
487}
488
489int bpf_map_get_next_key(int fd, const void *key, void *next_key)
490{
491 const size_t attr_sz = offsetofend(union bpf_attr, next_key);
492 union bpf_attr attr;
493 int ret;
494
495 memset(&attr, 0, attr_sz);
496 attr.map_fd = fd;
497 attr.key = ptr_to_u64(key);
498 attr.next_key = ptr_to_u64(next_key);
499
500 ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
501 return libbpf_err_errno(ret);
502}
503
504int bpf_map_freeze(int fd)
505{
506 const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
507 union bpf_attr attr;
508 int ret;
509
510 memset(&attr, 0, attr_sz);
511 attr.map_fd = fd;
512
513 ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
514 return libbpf_err_errno(ret);
515}
516
517static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
518 void *out_batch, void *keys, void *values,
519 __u32 *count,
520 const struct bpf_map_batch_opts *opts)
521{
522 const size_t attr_sz = offsetofend(union bpf_attr, batch);
523 union bpf_attr attr;
524 int ret;
525
526 if (!OPTS_VALID(opts, bpf_map_batch_opts))
527 return libbpf_err(-EINVAL);
528
529 memset(&attr, 0, attr_sz);
530 attr.batch.map_fd = fd;
531 attr.batch.in_batch = ptr_to_u64(in_batch);
532 attr.batch.out_batch = ptr_to_u64(out_batch);
533 attr.batch.keys = ptr_to_u64(keys);
534 attr.batch.values = ptr_to_u64(values);
535 attr.batch.count = *count;
536 attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
537 attr.batch.flags = OPTS_GET(opts, flags, 0);
538
539 ret = sys_bpf(cmd, &attr, attr_sz);
540 *count = attr.batch.count;
541
542 return libbpf_err_errno(ret);
543}
544
545int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
546 const struct bpf_map_batch_opts *opts)
547{
548 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
549 NULL, (void *)keys, NULL, count, opts);
550}
551
552int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
553 void *values, __u32 *count,
554 const struct bpf_map_batch_opts *opts)
555{
556 return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
557 out_batch, keys, values, count, opts);
558}
559
560int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
561 void *keys, void *values, __u32 *count,
562 const struct bpf_map_batch_opts *opts)
563{
564 return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
565 fd, in_batch, out_batch, keys, values,
566 count, opts);
567}
568
569int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
570 const struct bpf_map_batch_opts *opts)
571{
572 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
573 (void *)keys, (void *)values, count, opts);
574}
575
576int bpf_obj_pin(int fd, const char *pathname)
577{
578 const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
579 union bpf_attr attr;
580 int ret;
581
582 memset(&attr, 0, attr_sz);
583 attr.pathname = ptr_to_u64((void *)pathname);
584 attr.bpf_fd = fd;
585
586 ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
587 return libbpf_err_errno(ret);
588}
589
590int bpf_obj_get(const char *pathname)
591{
592 return bpf_obj_get_opts(pathname, NULL);
593}
594
595int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
596{
597 const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
598 union bpf_attr attr;
599 int fd;
600
601 if (!OPTS_VALID(opts, bpf_obj_get_opts))
602 return libbpf_err(-EINVAL);
603
604 memset(&attr, 0, attr_sz);
605 attr.pathname = ptr_to_u64((void *)pathname);
606 attr.file_flags = OPTS_GET(opts, file_flags, 0);
607
608 fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
609 return libbpf_err_errno(fd);
610}
611
612int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
613 unsigned int flags)
614{
615 DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
616 .flags = flags,
617 );
618
619 return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts);
620}
621
622int bpf_prog_attach_opts(int prog_fd, int target_fd,
623 enum bpf_attach_type type,
624 const struct bpf_prog_attach_opts *opts)
625{
626 const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
627 union bpf_attr attr;
628 int ret;
629
630 if (!OPTS_VALID(opts, bpf_prog_attach_opts))
631 return libbpf_err(-EINVAL);
632
633 memset(&attr, 0, attr_sz);
634 attr.target_fd = target_fd;
635 attr.attach_bpf_fd = prog_fd;
636 attr.attach_type = type;
637 attr.attach_flags = OPTS_GET(opts, flags, 0);
638 attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
639
640 ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
641 return libbpf_err_errno(ret);
642}
643
644int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
645{
646 const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
647 union bpf_attr attr;
648 int ret;
649
650 memset(&attr, 0, attr_sz);
651 attr.target_fd = target_fd;
652 attr.attach_type = type;
653
654 ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
655 return libbpf_err_errno(ret);
656}
657
658int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
659{
660 const size_t attr_sz = offsetofend(union bpf_attr, replace_bpf_fd);
661 union bpf_attr attr;
662 int ret;
663
664 memset(&attr, 0, attr_sz);
665 attr.target_fd = target_fd;
666 attr.attach_bpf_fd = prog_fd;
667 attr.attach_type = type;
668
669 ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
670 return libbpf_err_errno(ret);
671}
672
673int bpf_link_create(int prog_fd, int target_fd,
674 enum bpf_attach_type attach_type,
675 const struct bpf_link_create_opts *opts)
676{
677 const size_t attr_sz = offsetofend(union bpf_attr, link_create);
678 __u32 target_btf_id, iter_info_len;
679 union bpf_attr attr;
680 int fd, err;
681
682 if (!OPTS_VALID(opts, bpf_link_create_opts))
683 return libbpf_err(-EINVAL);
684
685 iter_info_len = OPTS_GET(opts, iter_info_len, 0);
686 target_btf_id = OPTS_GET(opts, target_btf_id, 0);
687
688 /* validate we don't have unexpected combinations of non-zero fields */
689 if (iter_info_len || target_btf_id) {
690 if (iter_info_len && target_btf_id)
691 return libbpf_err(-EINVAL);
692 if (!OPTS_ZEROED(opts, target_btf_id))
693 return libbpf_err(-EINVAL);
694 }
695
696 memset(&attr, 0, attr_sz);
697 attr.link_create.prog_fd = prog_fd;
698 attr.link_create.target_fd = target_fd;
699 attr.link_create.attach_type = attach_type;
700 attr.link_create.flags = OPTS_GET(opts, flags, 0);
701
702 if (target_btf_id) {
703 attr.link_create.target_btf_id = target_btf_id;
704 goto proceed;
705 }
706
707 switch (attach_type) {
708 case BPF_TRACE_ITER:
709 attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
710 attr.link_create.iter_info_len = iter_info_len;
711 break;
712 case BPF_PERF_EVENT:
713 attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0);
714 if (!OPTS_ZEROED(opts, perf_event))
715 return libbpf_err(-EINVAL);
716 break;
717 case BPF_TRACE_KPROBE_MULTI:
718 attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0);
719 attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0);
720 attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0));
721 attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0));
722 attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0));
723 if (!OPTS_ZEROED(opts, kprobe_multi))
724 return libbpf_err(-EINVAL);
725 break;
726 case BPF_TRACE_FENTRY:
727 case BPF_TRACE_FEXIT:
728 case BPF_MODIFY_RETURN:
729 case BPF_LSM_MAC:
730 attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
731 if (!OPTS_ZEROED(opts, tracing))
732 return libbpf_err(-EINVAL);
733 break;
734 default:
735 if (!OPTS_ZEROED(opts, flags))
736 return libbpf_err(-EINVAL);
737 break;
738 }
739proceed:
740 fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
741 if (fd >= 0)
742 return fd;
743 /* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
744 * and other similar programs
745 */
746 err = -errno;
747 if (err != -EINVAL)
748 return libbpf_err(err);
749
750 /* if user used features not supported by
751 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
752 */
753 if (attr.link_create.target_fd || attr.link_create.target_btf_id)
754 return libbpf_err(err);
755 if (!OPTS_ZEROED(opts, sz))
756 return libbpf_err(err);
757
758 /* otherwise, for few select kinds of programs that can be
759 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
760 * a fallback for older kernels
761 */
762 switch (attach_type) {
763 case BPF_TRACE_RAW_TP:
764 case BPF_LSM_MAC:
765 case BPF_TRACE_FENTRY:
766 case BPF_TRACE_FEXIT:
767 case BPF_MODIFY_RETURN:
768 return bpf_raw_tracepoint_open(NULL, prog_fd);
769 default:
770 return libbpf_err(err);
771 }
772}
773
774int bpf_link_detach(int link_fd)
775{
776 const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
777 union bpf_attr attr;
778 int ret;
779
780 memset(&attr, 0, attr_sz);
781 attr.link_detach.link_fd = link_fd;
782
783 ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
784 return libbpf_err_errno(ret);
785}
786
787int bpf_link_update(int link_fd, int new_prog_fd,
788 const struct bpf_link_update_opts *opts)
789{
790 const size_t attr_sz = offsetofend(union bpf_attr, link_update);
791 union bpf_attr attr;
792 int ret;
793
794 if (!OPTS_VALID(opts, bpf_link_update_opts))
795 return libbpf_err(-EINVAL);
796
797 memset(&attr, 0, attr_sz);
798 attr.link_update.link_fd = link_fd;
799 attr.link_update.new_prog_fd = new_prog_fd;
800 attr.link_update.flags = OPTS_GET(opts, flags, 0);
801 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
802
803 ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
804 return libbpf_err_errno(ret);
805}
806
807int bpf_iter_create(int link_fd)
808{
809 const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
810 union bpf_attr attr;
811 int fd;
812
813 memset(&attr, 0, attr_sz);
814 attr.iter_create.link_fd = link_fd;
815
816 fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
817 return libbpf_err_errno(fd);
818}
819
820int bpf_prog_query_opts(int target_fd,
821 enum bpf_attach_type type,
822 struct bpf_prog_query_opts *opts)
823{
824 const size_t attr_sz = offsetofend(union bpf_attr, query);
825 union bpf_attr attr;
826 int ret;
827
828 if (!OPTS_VALID(opts, bpf_prog_query_opts))
829 return libbpf_err(-EINVAL);
830
831 memset(&attr, 0, attr_sz);
832
833 attr.query.target_fd = target_fd;
834 attr.query.attach_type = type;
835 attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
836 attr.query.prog_cnt = OPTS_GET(opts, prog_cnt, 0);
837 attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
838 attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
839
840 ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
841
842 OPTS_SET(opts, attach_flags, attr.query.attach_flags);
843 OPTS_SET(opts, prog_cnt, attr.query.prog_cnt);
844
845 return libbpf_err_errno(ret);
846}
847
848int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
849 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
850{
851 LIBBPF_OPTS(bpf_prog_query_opts, opts);
852 int ret;
853
854 opts.query_flags = query_flags;
855 opts.prog_ids = prog_ids;
856 opts.prog_cnt = *prog_cnt;
857
858 ret = bpf_prog_query_opts(target_fd, type, &opts);
859
860 if (attach_flags)
861 *attach_flags = opts.attach_flags;
862 *prog_cnt = opts.prog_cnt;
863
864 return libbpf_err_errno(ret);
865}
866
867int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
868{
869 const size_t attr_sz = offsetofend(union bpf_attr, test);
870 union bpf_attr attr;
871 int ret;
872
873 if (!OPTS_VALID(opts, bpf_test_run_opts))
874 return libbpf_err(-EINVAL);
875
876 memset(&attr, 0, attr_sz);
877 attr.test.prog_fd = prog_fd;
878 attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
879 attr.test.cpu = OPTS_GET(opts, cpu, 0);
880 attr.test.flags = OPTS_GET(opts, flags, 0);
881 attr.test.repeat = OPTS_GET(opts, repeat, 0);
882 attr.test.duration = OPTS_GET(opts, duration, 0);
883 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
884 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
885 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
886 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
887 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
888 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
889 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
890 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
891
892 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
893
894 OPTS_SET(opts, data_size_out, attr.test.data_size_out);
895 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
896 OPTS_SET(opts, duration, attr.test.duration);
897 OPTS_SET(opts, retval, attr.test.retval);
898
899 return libbpf_err_errno(ret);
900}
901
902static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
903{
904 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
905 union bpf_attr attr;
906 int err;
907
908 memset(&attr, 0, attr_sz);
909 attr.start_id = start_id;
910
911 err = sys_bpf(cmd, &attr, attr_sz);
912 if (!err)
913 *next_id = attr.next_id;
914
915 return libbpf_err_errno(err);
916}
917
918int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
919{
920 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
921}
922
923int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
924{
925 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
926}
927
928int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
929{
930 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
931}
932
933int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
934{
935 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
936}
937
938int bpf_prog_get_fd_by_id_opts(__u32 id,
939 const struct bpf_get_fd_by_id_opts *opts)
940{
941 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
942 union bpf_attr attr;
943 int fd;
944
945 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
946 return libbpf_err(-EINVAL);
947
948 memset(&attr, 0, attr_sz);
949 attr.prog_id = id;
950 attr.open_flags = OPTS_GET(opts, open_flags, 0);
951
952 fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
953 return libbpf_err_errno(fd);
954}
955
956int bpf_prog_get_fd_by_id(__u32 id)
957{
958 return bpf_prog_get_fd_by_id_opts(id, NULL);
959}
960
961int bpf_map_get_fd_by_id_opts(__u32 id,
962 const struct bpf_get_fd_by_id_opts *opts)
963{
964 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
965 union bpf_attr attr;
966 int fd;
967
968 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
969 return libbpf_err(-EINVAL);
970
971 memset(&attr, 0, attr_sz);
972 attr.map_id = id;
973 attr.open_flags = OPTS_GET(opts, open_flags, 0);
974
975 fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
976 return libbpf_err_errno(fd);
977}
978
979int bpf_map_get_fd_by_id(__u32 id)
980{
981 return bpf_map_get_fd_by_id_opts(id, NULL);
982}
983
984int bpf_btf_get_fd_by_id_opts(__u32 id,
985 const struct bpf_get_fd_by_id_opts *opts)
986{
987 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
988 union bpf_attr attr;
989 int fd;
990
991 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
992 return libbpf_err(-EINVAL);
993
994 memset(&attr, 0, attr_sz);
995 attr.btf_id = id;
996 attr.open_flags = OPTS_GET(opts, open_flags, 0);
997
998 fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
999 return libbpf_err_errno(fd);
1000}
1001
1002int bpf_btf_get_fd_by_id(__u32 id)
1003{
1004 return bpf_btf_get_fd_by_id_opts(id, NULL);
1005}
1006
1007int bpf_link_get_fd_by_id_opts(__u32 id,
1008 const struct bpf_get_fd_by_id_opts *opts)
1009{
1010 const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1011 union bpf_attr attr;
1012 int fd;
1013
1014 if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1015 return libbpf_err(-EINVAL);
1016
1017 memset(&attr, 0, attr_sz);
1018 attr.link_id = id;
1019 attr.open_flags = OPTS_GET(opts, open_flags, 0);
1020
1021 fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
1022 return libbpf_err_errno(fd);
1023}
1024
1025int bpf_link_get_fd_by_id(__u32 id)
1026{
1027 return bpf_link_get_fd_by_id_opts(id, NULL);
1028}
1029
1030int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
1031{
1032 const size_t attr_sz = offsetofend(union bpf_attr, info);
1033 union bpf_attr attr;
1034 int err;
1035
1036 memset(&attr, 0, attr_sz);
1037 attr.info.bpf_fd = bpf_fd;
1038 attr.info.info_len = *info_len;
1039 attr.info.info = ptr_to_u64(info);
1040
1041 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
1042 if (!err)
1043 *info_len = attr.info.info_len;
1044 return libbpf_err_errno(err);
1045}
1046
1047int bpf_raw_tracepoint_open(const char *name, int prog_fd)
1048{
1049 const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
1050 union bpf_attr attr;
1051 int fd;
1052
1053 memset(&attr, 0, attr_sz);
1054 attr.raw_tracepoint.name = ptr_to_u64(name);
1055 attr.raw_tracepoint.prog_fd = prog_fd;
1056
1057 fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
1058 return libbpf_err_errno(fd);
1059}
1060
1061int bpf_btf_load(const void *btf_data, size_t btf_size, const struct bpf_btf_load_opts *opts)
1062{
1063 const size_t attr_sz = offsetofend(union bpf_attr, btf_log_level);
1064 union bpf_attr attr;
1065 char *log_buf;
1066 size_t log_size;
1067 __u32 log_level;
1068 int fd;
1069
1070 bump_rlimit_memlock();
1071
1072 memset(&attr, 0, attr_sz);
1073
1074 if (!OPTS_VALID(opts, bpf_btf_load_opts))
1075 return libbpf_err(-EINVAL);
1076
1077 log_buf = OPTS_GET(opts, log_buf, NULL);
1078 log_size = OPTS_GET(opts, log_size, 0);
1079 log_level = OPTS_GET(opts, log_level, 0);
1080
1081 if (log_size > UINT_MAX)
1082 return libbpf_err(-EINVAL);
1083 if (log_size && !log_buf)
1084 return libbpf_err(-EINVAL);
1085
1086 attr.btf = ptr_to_u64(btf_data);
1087 attr.btf_size = btf_size;
1088 /* log_level == 0 and log_buf != NULL means "try loading without
1089 * log_buf, but retry with log_buf and log_level=1 on error", which is
1090 * consistent across low-level and high-level BTF and program loading
1091 * APIs within libbpf and provides a sensible behavior in practice
1092 */
1093 if (log_level) {
1094 attr.btf_log_buf = ptr_to_u64(log_buf);
1095 attr.btf_log_size = (__u32)log_size;
1096 attr.btf_log_level = log_level;
1097 }
1098
1099 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1100 if (fd < 0 && log_buf && log_level == 0) {
1101 attr.btf_log_buf = ptr_to_u64(log_buf);
1102 attr.btf_log_size = (__u32)log_size;
1103 attr.btf_log_level = 1;
1104 fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1105 }
1106 return libbpf_err_errno(fd);
1107}
1108
1109int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1110 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1111 __u64 *probe_addr)
1112{
1113 const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
1114 union bpf_attr attr;
1115 int err;
1116
1117 memset(&attr, 0, attr_sz);
1118 attr.task_fd_query.pid = pid;
1119 attr.task_fd_query.fd = fd;
1120 attr.task_fd_query.flags = flags;
1121 attr.task_fd_query.buf = ptr_to_u64(buf);
1122 attr.task_fd_query.buf_len = *buf_len;
1123
1124 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
1125
1126 *buf_len = attr.task_fd_query.buf_len;
1127 *prog_id = attr.task_fd_query.prog_id;
1128 *fd_type = attr.task_fd_query.fd_type;
1129 *probe_offset = attr.task_fd_query.probe_offset;
1130 *probe_addr = attr.task_fd_query.probe_addr;
1131
1132 return libbpf_err_errno(err);
1133}
1134
1135int bpf_enable_stats(enum bpf_stats_type type)
1136{
1137 const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
1138 union bpf_attr attr;
1139 int fd;
1140
1141 memset(&attr, 0, attr_sz);
1142 attr.enable_stats.type = type;
1143
1144 fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
1145 return libbpf_err_errno(fd);
1146}
1147
1148int bpf_prog_bind_map(int prog_fd, int map_fd,
1149 const struct bpf_prog_bind_opts *opts)
1150{
1151 const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
1152 union bpf_attr attr;
1153 int ret;
1154
1155 if (!OPTS_VALID(opts, bpf_prog_bind_opts))
1156 return libbpf_err(-EINVAL);
1157
1158 memset(&attr, 0, attr_sz);
1159 attr.prog_bind_map.prog_fd = prog_fd;
1160 attr.prog_bind_map.map_fd = map_fd;
1161 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1162
1163 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
1164 return libbpf_err_errno(ret);
1165}
1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3/*
4 * common eBPF ELF operations.
5 *
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.
9 *
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!)
14 *
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.
19 *
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>
22 */
23
24#include <stdlib.h>
25#include <string.h>
26#include <memory.h>
27#include <unistd.h>
28#include <asm/unistd.h>
29#include <errno.h>
30#include <linux/bpf.h>
31#include "bpf.h"
32#include "libbpf.h"
33#include "libbpf_internal.h"
34
35/*
36 * When building perf, unistd.h is overridden. __NR_bpf is
37 * required to be defined explicitly.
38 */
39#ifndef __NR_bpf
40# if defined(__i386__)
41# define __NR_bpf 357
42# elif defined(__x86_64__)
43# define __NR_bpf 321
44# elif defined(__aarch64__)
45# define __NR_bpf 280
46# elif defined(__sparc__)
47# define __NR_bpf 349
48# elif defined(__s390__)
49# define __NR_bpf 351
50# elif defined(__arc__)
51# define __NR_bpf 280
52# else
53# error __NR_bpf not defined. libbpf does not support your arch.
54# endif
55#endif
56
57static inline __u64 ptr_to_u64(const void *ptr)
58{
59 return (__u64) (unsigned long) ptr;
60}
61
62static 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
68static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
69{
70 int retries = 5;
71 int fd;
72
73 do {
74 fd = sys_bpf(BPF_PROG_LOAD, attr, size);
75 } while (fd < 0 && errno == EAGAIN && retries-- > 0);
76
77 return fd;
78}
79
80int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
81{
82 union bpf_attr attr;
83 int fd;
84
85 memset(&attr, '\0', sizeof(attr));
86
87 attr.map_type = create_attr->map_type;
88 attr.key_size = create_attr->key_size;
89 attr.value_size = create_attr->value_size;
90 attr.max_entries = create_attr->max_entries;
91 attr.map_flags = create_attr->map_flags;
92 if (create_attr->name)
93 memcpy(attr.map_name, create_attr->name,
94 min(strlen(create_attr->name), BPF_OBJ_NAME_LEN - 1));
95 attr.numa_node = create_attr->numa_node;
96 attr.btf_fd = create_attr->btf_fd;
97 attr.btf_key_type_id = create_attr->btf_key_type_id;
98 attr.btf_value_type_id = create_attr->btf_value_type_id;
99 attr.map_ifindex = create_attr->map_ifindex;
100 if (attr.map_type == BPF_MAP_TYPE_STRUCT_OPS)
101 attr.btf_vmlinux_value_type_id =
102 create_attr->btf_vmlinux_value_type_id;
103 else
104 attr.inner_map_fd = create_attr->inner_map_fd;
105
106 fd = sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
107 return libbpf_err_errno(fd);
108}
109
110int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
111 int key_size, int value_size, int max_entries,
112 __u32 map_flags, int node)
113{
114 struct bpf_create_map_attr map_attr = {};
115
116 map_attr.name = name;
117 map_attr.map_type = map_type;
118 map_attr.map_flags = map_flags;
119 map_attr.key_size = key_size;
120 map_attr.value_size = value_size;
121 map_attr.max_entries = max_entries;
122 if (node >= 0) {
123 map_attr.numa_node = node;
124 map_attr.map_flags |= BPF_F_NUMA_NODE;
125 }
126
127 return bpf_create_map_xattr(&map_attr);
128}
129
130int bpf_create_map(enum bpf_map_type map_type, int key_size,
131 int value_size, int max_entries, __u32 map_flags)
132{
133 struct bpf_create_map_attr map_attr = {};
134
135 map_attr.map_type = map_type;
136 map_attr.map_flags = map_flags;
137 map_attr.key_size = key_size;
138 map_attr.value_size = value_size;
139 map_attr.max_entries = max_entries;
140
141 return bpf_create_map_xattr(&map_attr);
142}
143
144int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
145 int key_size, int value_size, int max_entries,
146 __u32 map_flags)
147{
148 struct bpf_create_map_attr map_attr = {};
149
150 map_attr.name = name;
151 map_attr.map_type = map_type;
152 map_attr.map_flags = map_flags;
153 map_attr.key_size = key_size;
154 map_attr.value_size = value_size;
155 map_attr.max_entries = max_entries;
156
157 return bpf_create_map_xattr(&map_attr);
158}
159
160int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
161 int key_size, int inner_map_fd, int max_entries,
162 __u32 map_flags, int node)
163{
164 union bpf_attr attr;
165 int fd;
166
167 memset(&attr, '\0', sizeof(attr));
168
169 attr.map_type = map_type;
170 attr.key_size = key_size;
171 attr.value_size = 4;
172 attr.inner_map_fd = inner_map_fd;
173 attr.max_entries = max_entries;
174 attr.map_flags = map_flags;
175 if (name)
176 memcpy(attr.map_name, name,
177 min(strlen(name), BPF_OBJ_NAME_LEN - 1));
178
179 if (node >= 0) {
180 attr.map_flags |= BPF_F_NUMA_NODE;
181 attr.numa_node = node;
182 }
183
184 fd = sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
185 return libbpf_err_errno(fd);
186}
187
188int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
189 int key_size, int inner_map_fd, int max_entries,
190 __u32 map_flags)
191{
192 return bpf_create_map_in_map_node(map_type, name, key_size,
193 inner_map_fd, max_entries, map_flags,
194 -1);
195}
196
197static void *
198alloc_zero_tailing_info(const void *orecord, __u32 cnt,
199 __u32 actual_rec_size, __u32 expected_rec_size)
200{
201 __u64 info_len = (__u64)actual_rec_size * cnt;
202 void *info, *nrecord;
203 int i;
204
205 info = malloc(info_len);
206 if (!info)
207 return NULL;
208
209 /* zero out bytes kernel does not understand */
210 nrecord = info;
211 for (i = 0; i < cnt; i++) {
212 memcpy(nrecord, orecord, expected_rec_size);
213 memset(nrecord + expected_rec_size, 0,
214 actual_rec_size - expected_rec_size);
215 orecord += actual_rec_size;
216 nrecord += actual_rec_size;
217 }
218
219 return info;
220}
221
222int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr)
223{
224 void *finfo = NULL, *linfo = NULL;
225 union bpf_attr attr;
226 int fd;
227
228 if (!load_attr->log_buf != !load_attr->log_buf_sz)
229 return libbpf_err(-EINVAL);
230
231 if (load_attr->log_level > (4 | 2 | 1) || (load_attr->log_level && !load_attr->log_buf))
232 return libbpf_err(-EINVAL);
233
234 memset(&attr, 0, sizeof(attr));
235 attr.prog_type = load_attr->prog_type;
236 attr.expected_attach_type = load_attr->expected_attach_type;
237
238 if (load_attr->attach_prog_fd)
239 attr.attach_prog_fd = load_attr->attach_prog_fd;
240 else
241 attr.attach_btf_obj_fd = load_attr->attach_btf_obj_fd;
242 attr.attach_btf_id = load_attr->attach_btf_id;
243
244 attr.prog_ifindex = load_attr->prog_ifindex;
245 attr.kern_version = load_attr->kern_version;
246
247 attr.insn_cnt = (__u32)load_attr->insn_cnt;
248 attr.insns = ptr_to_u64(load_attr->insns);
249 attr.license = ptr_to_u64(load_attr->license);
250
251 attr.log_level = load_attr->log_level;
252 if (attr.log_level) {
253 attr.log_buf = ptr_to_u64(load_attr->log_buf);
254 attr.log_size = load_attr->log_buf_sz;
255 }
256
257 attr.prog_btf_fd = load_attr->prog_btf_fd;
258 attr.prog_flags = load_attr->prog_flags;
259
260 attr.func_info_rec_size = load_attr->func_info_rec_size;
261 attr.func_info_cnt = load_attr->func_info_cnt;
262 attr.func_info = ptr_to_u64(load_attr->func_info);
263
264 attr.line_info_rec_size = load_attr->line_info_rec_size;
265 attr.line_info_cnt = load_attr->line_info_cnt;
266 attr.line_info = ptr_to_u64(load_attr->line_info);
267
268 if (load_attr->name)
269 memcpy(attr.prog_name, load_attr->name,
270 min(strlen(load_attr->name), (size_t)BPF_OBJ_NAME_LEN - 1));
271
272 fd = sys_bpf_prog_load(&attr, sizeof(attr));
273 if (fd >= 0)
274 return fd;
275
276 /* After bpf_prog_load, the kernel may modify certain attributes
277 * to give user space a hint how to deal with loading failure.
278 * Check to see whether we can make some changes and load again.
279 */
280 while (errno == E2BIG && (!finfo || !linfo)) {
281 if (!finfo && attr.func_info_cnt &&
282 attr.func_info_rec_size < load_attr->func_info_rec_size) {
283 /* try with corrected func info records */
284 finfo = alloc_zero_tailing_info(load_attr->func_info,
285 load_attr->func_info_cnt,
286 load_attr->func_info_rec_size,
287 attr.func_info_rec_size);
288 if (!finfo) {
289 errno = E2BIG;
290 goto done;
291 }
292
293 attr.func_info = ptr_to_u64(finfo);
294 attr.func_info_rec_size = load_attr->func_info_rec_size;
295 } else if (!linfo && attr.line_info_cnt &&
296 attr.line_info_rec_size <
297 load_attr->line_info_rec_size) {
298 linfo = alloc_zero_tailing_info(load_attr->line_info,
299 load_attr->line_info_cnt,
300 load_attr->line_info_rec_size,
301 attr.line_info_rec_size);
302 if (!linfo) {
303 errno = E2BIG;
304 goto done;
305 }
306
307 attr.line_info = ptr_to_u64(linfo);
308 attr.line_info_rec_size = load_attr->line_info_rec_size;
309 } else {
310 break;
311 }
312
313 fd = sys_bpf_prog_load(&attr, sizeof(attr));
314 if (fd >= 0)
315 goto done;
316 }
317
318 if (load_attr->log_level || !load_attr->log_buf)
319 goto done;
320
321 /* Try again with log */
322 attr.log_buf = ptr_to_u64(load_attr->log_buf);
323 attr.log_size = load_attr->log_buf_sz;
324 attr.log_level = 1;
325 load_attr->log_buf[0] = 0;
326
327 fd = sys_bpf_prog_load(&attr, sizeof(attr));
328done:
329 /* free() doesn't affect errno, so we don't need to restore it */
330 free(finfo);
331 free(linfo);
332 return libbpf_err_errno(fd);
333}
334
335int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
336 char *log_buf, size_t log_buf_sz)
337{
338 struct bpf_prog_load_params p = {};
339
340 if (!load_attr || !log_buf != !log_buf_sz)
341 return libbpf_err(-EINVAL);
342
343 p.prog_type = load_attr->prog_type;
344 p.expected_attach_type = load_attr->expected_attach_type;
345 switch (p.prog_type) {
346 case BPF_PROG_TYPE_STRUCT_OPS:
347 case BPF_PROG_TYPE_LSM:
348 p.attach_btf_id = load_attr->attach_btf_id;
349 break;
350 case BPF_PROG_TYPE_TRACING:
351 case BPF_PROG_TYPE_EXT:
352 p.attach_btf_id = load_attr->attach_btf_id;
353 p.attach_prog_fd = load_attr->attach_prog_fd;
354 break;
355 default:
356 p.prog_ifindex = load_attr->prog_ifindex;
357 p.kern_version = load_attr->kern_version;
358 }
359 p.insn_cnt = load_attr->insns_cnt;
360 p.insns = load_attr->insns;
361 p.license = load_attr->license;
362 p.log_level = load_attr->log_level;
363 p.log_buf = log_buf;
364 p.log_buf_sz = log_buf_sz;
365 p.prog_btf_fd = load_attr->prog_btf_fd;
366 p.func_info_rec_size = load_attr->func_info_rec_size;
367 p.func_info_cnt = load_attr->func_info_cnt;
368 p.func_info = load_attr->func_info;
369 p.line_info_rec_size = load_attr->line_info_rec_size;
370 p.line_info_cnt = load_attr->line_info_cnt;
371 p.line_info = load_attr->line_info;
372 p.name = load_attr->name;
373 p.prog_flags = load_attr->prog_flags;
374
375 return libbpf__bpf_prog_load(&p);
376}
377
378int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
379 size_t insns_cnt, const char *license,
380 __u32 kern_version, char *log_buf,
381 size_t log_buf_sz)
382{
383 struct bpf_load_program_attr load_attr;
384
385 memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
386 load_attr.prog_type = type;
387 load_attr.expected_attach_type = 0;
388 load_attr.name = NULL;
389 load_attr.insns = insns;
390 load_attr.insns_cnt = insns_cnt;
391 load_attr.license = license;
392 load_attr.kern_version = kern_version;
393
394 return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
395}
396
397int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
398 size_t insns_cnt, __u32 prog_flags, const char *license,
399 __u32 kern_version, char *log_buf, size_t log_buf_sz,
400 int log_level)
401{
402 union bpf_attr attr;
403 int fd;
404
405 memset(&attr, 0, sizeof(attr));
406 attr.prog_type = type;
407 attr.insn_cnt = (__u32)insns_cnt;
408 attr.insns = ptr_to_u64(insns);
409 attr.license = ptr_to_u64(license);
410 attr.log_buf = ptr_to_u64(log_buf);
411 attr.log_size = log_buf_sz;
412 attr.log_level = log_level;
413 log_buf[0] = 0;
414 attr.kern_version = kern_version;
415 attr.prog_flags = prog_flags;
416
417 fd = sys_bpf_prog_load(&attr, sizeof(attr));
418 return libbpf_err_errno(fd);
419}
420
421int bpf_map_update_elem(int fd, const void *key, const void *value,
422 __u64 flags)
423{
424 union bpf_attr attr;
425 int ret;
426
427 memset(&attr, 0, sizeof(attr));
428 attr.map_fd = fd;
429 attr.key = ptr_to_u64(key);
430 attr.value = ptr_to_u64(value);
431 attr.flags = flags;
432
433 ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
434 return libbpf_err_errno(ret);
435}
436
437int bpf_map_lookup_elem(int fd, const void *key, void *value)
438{
439 union bpf_attr attr;
440 int ret;
441
442 memset(&attr, 0, sizeof(attr));
443 attr.map_fd = fd;
444 attr.key = ptr_to_u64(key);
445 attr.value = ptr_to_u64(value);
446
447 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
448 return libbpf_err_errno(ret);
449}
450
451int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
452{
453 union bpf_attr attr;
454 int ret;
455
456 memset(&attr, 0, sizeof(attr));
457 attr.map_fd = fd;
458 attr.key = ptr_to_u64(key);
459 attr.value = ptr_to_u64(value);
460 attr.flags = flags;
461
462 ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
463 return libbpf_err_errno(ret);
464}
465
466int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
467{
468 union bpf_attr attr;
469 int ret;
470
471 memset(&attr, 0, sizeof(attr));
472 attr.map_fd = fd;
473 attr.key = ptr_to_u64(key);
474 attr.value = ptr_to_u64(value);
475
476 ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
477 return libbpf_err_errno(ret);
478}
479
480int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
481{
482 union bpf_attr attr;
483
484 memset(&attr, 0, sizeof(attr));
485 attr.map_fd = fd;
486 attr.key = ptr_to_u64(key);
487 attr.value = ptr_to_u64(value);
488 attr.flags = flags;
489
490 return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
491}
492
493int bpf_map_delete_elem(int fd, const void *key)
494{
495 union bpf_attr attr;
496 int ret;
497
498 memset(&attr, 0, sizeof(attr));
499 attr.map_fd = fd;
500 attr.key = ptr_to_u64(key);
501
502 ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
503 return libbpf_err_errno(ret);
504}
505
506int bpf_map_get_next_key(int fd, const void *key, void *next_key)
507{
508 union bpf_attr attr;
509 int ret;
510
511 memset(&attr, 0, sizeof(attr));
512 attr.map_fd = fd;
513 attr.key = ptr_to_u64(key);
514 attr.next_key = ptr_to_u64(next_key);
515
516 ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
517 return libbpf_err_errno(ret);
518}
519
520int bpf_map_freeze(int fd)
521{
522 union bpf_attr attr;
523 int ret;
524
525 memset(&attr, 0, sizeof(attr));
526 attr.map_fd = fd;
527
528 ret = sys_bpf(BPF_MAP_FREEZE, &attr, sizeof(attr));
529 return libbpf_err_errno(ret);
530}
531
532static int bpf_map_batch_common(int cmd, int fd, void *in_batch,
533 void *out_batch, void *keys, void *values,
534 __u32 *count,
535 const struct bpf_map_batch_opts *opts)
536{
537 union bpf_attr attr;
538 int ret;
539
540 if (!OPTS_VALID(opts, bpf_map_batch_opts))
541 return libbpf_err(-EINVAL);
542
543 memset(&attr, 0, sizeof(attr));
544 attr.batch.map_fd = fd;
545 attr.batch.in_batch = ptr_to_u64(in_batch);
546 attr.batch.out_batch = ptr_to_u64(out_batch);
547 attr.batch.keys = ptr_to_u64(keys);
548 attr.batch.values = ptr_to_u64(values);
549 attr.batch.count = *count;
550 attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0);
551 attr.batch.flags = OPTS_GET(opts, flags, 0);
552
553 ret = sys_bpf(cmd, &attr, sizeof(attr));
554 *count = attr.batch.count;
555
556 return libbpf_err_errno(ret);
557}
558
559int bpf_map_delete_batch(int fd, void *keys, __u32 *count,
560 const struct bpf_map_batch_opts *opts)
561{
562 return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
563 NULL, keys, NULL, count, opts);
564}
565
566int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
567 void *values, __u32 *count,
568 const struct bpf_map_batch_opts *opts)
569{
570 return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
571 out_batch, keys, values, count, opts);
572}
573
574int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
575 void *keys, void *values, __u32 *count,
576 const struct bpf_map_batch_opts *opts)
577{
578 return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
579 fd, in_batch, out_batch, keys, values,
580 count, opts);
581}
582
583int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count,
584 const struct bpf_map_batch_opts *opts)
585{
586 return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
587 keys, values, count, opts);
588}
589
590int bpf_obj_pin(int fd, const char *pathname)
591{
592 union bpf_attr attr;
593 int ret;
594
595 memset(&attr, 0, sizeof(attr));
596 attr.pathname = ptr_to_u64((void *)pathname);
597 attr.bpf_fd = fd;
598
599 ret = sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
600 return libbpf_err_errno(ret);
601}
602
603int bpf_obj_get(const char *pathname)
604{
605 union bpf_attr attr;
606 int fd;
607
608 memset(&attr, 0, sizeof(attr));
609 attr.pathname = ptr_to_u64((void *)pathname);
610
611 fd = sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
612 return libbpf_err_errno(fd);
613}
614
615int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
616 unsigned int flags)
617{
618 DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
619 .flags = flags,
620 );
621
622 return bpf_prog_attach_xattr(prog_fd, target_fd, type, &opts);
623}
624
625int bpf_prog_attach_xattr(int prog_fd, int target_fd,
626 enum bpf_attach_type type,
627 const struct bpf_prog_attach_opts *opts)
628{
629 union bpf_attr attr;
630 int ret;
631
632 if (!OPTS_VALID(opts, bpf_prog_attach_opts))
633 return libbpf_err(-EINVAL);
634
635 memset(&attr, 0, sizeof(attr));
636 attr.target_fd = target_fd;
637 attr.attach_bpf_fd = prog_fd;
638 attr.attach_type = type;
639 attr.attach_flags = OPTS_GET(opts, flags, 0);
640 attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
641
642 ret = sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
643 return libbpf_err_errno(ret);
644}
645
646int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
647{
648 union bpf_attr attr;
649 int ret;
650
651 memset(&attr, 0, sizeof(attr));
652 attr.target_fd = target_fd;
653 attr.attach_type = type;
654
655 ret = sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
656 return libbpf_err_errno(ret);
657}
658
659int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
660{
661 union bpf_attr attr;
662 int ret;
663
664 memset(&attr, 0, sizeof(attr));
665 attr.target_fd = target_fd;
666 attr.attach_bpf_fd = prog_fd;
667 attr.attach_type = type;
668
669 ret = sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
670 return libbpf_err_errno(ret);
671}
672
673int bpf_link_create(int prog_fd, int target_fd,
674 enum bpf_attach_type attach_type,
675 const struct bpf_link_create_opts *opts)
676{
677 __u32 target_btf_id, iter_info_len;
678 union bpf_attr attr;
679 int fd;
680
681 if (!OPTS_VALID(opts, bpf_link_create_opts))
682 return libbpf_err(-EINVAL);
683
684 iter_info_len = OPTS_GET(opts, iter_info_len, 0);
685 target_btf_id = OPTS_GET(opts, target_btf_id, 0);
686
687 if (iter_info_len && target_btf_id)
688 return libbpf_err(-EINVAL);
689
690 memset(&attr, 0, sizeof(attr));
691 attr.link_create.prog_fd = prog_fd;
692 attr.link_create.target_fd = target_fd;
693 attr.link_create.attach_type = attach_type;
694 attr.link_create.flags = OPTS_GET(opts, flags, 0);
695
696 if (iter_info_len) {
697 attr.link_create.iter_info =
698 ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
699 attr.link_create.iter_info_len = iter_info_len;
700 } else if (target_btf_id) {
701 attr.link_create.target_btf_id = target_btf_id;
702 }
703
704 fd = sys_bpf(BPF_LINK_CREATE, &attr, sizeof(attr));
705 return libbpf_err_errno(fd);
706}
707
708int bpf_link_detach(int link_fd)
709{
710 union bpf_attr attr;
711 int ret;
712
713 memset(&attr, 0, sizeof(attr));
714 attr.link_detach.link_fd = link_fd;
715
716 ret = sys_bpf(BPF_LINK_DETACH, &attr, sizeof(attr));
717 return libbpf_err_errno(ret);
718}
719
720int bpf_link_update(int link_fd, int new_prog_fd,
721 const struct bpf_link_update_opts *opts)
722{
723 union bpf_attr attr;
724 int ret;
725
726 if (!OPTS_VALID(opts, bpf_link_update_opts))
727 return libbpf_err(-EINVAL);
728
729 memset(&attr, 0, sizeof(attr));
730 attr.link_update.link_fd = link_fd;
731 attr.link_update.new_prog_fd = new_prog_fd;
732 attr.link_update.flags = OPTS_GET(opts, flags, 0);
733 attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
734
735 ret = sys_bpf(BPF_LINK_UPDATE, &attr, sizeof(attr));
736 return libbpf_err_errno(ret);
737}
738
739int bpf_iter_create(int link_fd)
740{
741 union bpf_attr attr;
742 int fd;
743
744 memset(&attr, 0, sizeof(attr));
745 attr.iter_create.link_fd = link_fd;
746
747 fd = sys_bpf(BPF_ITER_CREATE, &attr, sizeof(attr));
748 return libbpf_err_errno(fd);
749}
750
751int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
752 __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
753{
754 union bpf_attr attr;
755 int ret;
756
757 memset(&attr, 0, sizeof(attr));
758 attr.query.target_fd = target_fd;
759 attr.query.attach_type = type;
760 attr.query.query_flags = query_flags;
761 attr.query.prog_cnt = *prog_cnt;
762 attr.query.prog_ids = ptr_to_u64(prog_ids);
763
764 ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
765
766 if (attach_flags)
767 *attach_flags = attr.query.attach_flags;
768 *prog_cnt = attr.query.prog_cnt;
769
770 return libbpf_err_errno(ret);
771}
772
773int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
774 void *data_out, __u32 *size_out, __u32 *retval,
775 __u32 *duration)
776{
777 union bpf_attr attr;
778 int ret;
779
780 memset(&attr, 0, sizeof(attr));
781 attr.test.prog_fd = prog_fd;
782 attr.test.data_in = ptr_to_u64(data);
783 attr.test.data_out = ptr_to_u64(data_out);
784 attr.test.data_size_in = size;
785 attr.test.repeat = repeat;
786
787 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
788
789 if (size_out)
790 *size_out = attr.test.data_size_out;
791 if (retval)
792 *retval = attr.test.retval;
793 if (duration)
794 *duration = attr.test.duration;
795
796 return libbpf_err_errno(ret);
797}
798
799int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
800{
801 union bpf_attr attr;
802 int ret;
803
804 if (!test_attr->data_out && test_attr->data_size_out > 0)
805 return libbpf_err(-EINVAL);
806
807 memset(&attr, 0, sizeof(attr));
808 attr.test.prog_fd = test_attr->prog_fd;
809 attr.test.data_in = ptr_to_u64(test_attr->data_in);
810 attr.test.data_out = ptr_to_u64(test_attr->data_out);
811 attr.test.data_size_in = test_attr->data_size_in;
812 attr.test.data_size_out = test_attr->data_size_out;
813 attr.test.ctx_in = ptr_to_u64(test_attr->ctx_in);
814 attr.test.ctx_out = ptr_to_u64(test_attr->ctx_out);
815 attr.test.ctx_size_in = test_attr->ctx_size_in;
816 attr.test.ctx_size_out = test_attr->ctx_size_out;
817 attr.test.repeat = test_attr->repeat;
818
819 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
820
821 test_attr->data_size_out = attr.test.data_size_out;
822 test_attr->ctx_size_out = attr.test.ctx_size_out;
823 test_attr->retval = attr.test.retval;
824 test_attr->duration = attr.test.duration;
825
826 return libbpf_err_errno(ret);
827}
828
829int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
830{
831 union bpf_attr attr;
832 int ret;
833
834 if (!OPTS_VALID(opts, bpf_test_run_opts))
835 return libbpf_err(-EINVAL);
836
837 memset(&attr, 0, sizeof(attr));
838 attr.test.prog_fd = prog_fd;
839 attr.test.cpu = OPTS_GET(opts, cpu, 0);
840 attr.test.flags = OPTS_GET(opts, flags, 0);
841 attr.test.repeat = OPTS_GET(opts, repeat, 0);
842 attr.test.duration = OPTS_GET(opts, duration, 0);
843 attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
844 attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
845 attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
846 attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
847 attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
848 attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
849 attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
850 attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
851
852 ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
853
854 OPTS_SET(opts, data_size_out, attr.test.data_size_out);
855 OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
856 OPTS_SET(opts, duration, attr.test.duration);
857 OPTS_SET(opts, retval, attr.test.retval);
858
859 return libbpf_err_errno(ret);
860}
861
862static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
863{
864 union bpf_attr attr;
865 int err;
866
867 memset(&attr, 0, sizeof(attr));
868 attr.start_id = start_id;
869
870 err = sys_bpf(cmd, &attr, sizeof(attr));
871 if (!err)
872 *next_id = attr.next_id;
873
874 return libbpf_err_errno(err);
875}
876
877int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
878{
879 return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
880}
881
882int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
883{
884 return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
885}
886
887int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
888{
889 return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
890}
891
892int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
893{
894 return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
895}
896
897int bpf_prog_get_fd_by_id(__u32 id)
898{
899 union bpf_attr attr;
900 int fd;
901
902 memset(&attr, 0, sizeof(attr));
903 attr.prog_id = id;
904
905 fd = sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
906 return libbpf_err_errno(fd);
907}
908
909int bpf_map_get_fd_by_id(__u32 id)
910{
911 union bpf_attr attr;
912 int fd;
913
914 memset(&attr, 0, sizeof(attr));
915 attr.map_id = id;
916
917 fd = sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
918 return libbpf_err_errno(fd);
919}
920
921int bpf_btf_get_fd_by_id(__u32 id)
922{
923 union bpf_attr attr;
924 int fd;
925
926 memset(&attr, 0, sizeof(attr));
927 attr.btf_id = id;
928
929 fd = sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
930 return libbpf_err_errno(fd);
931}
932
933int bpf_link_get_fd_by_id(__u32 id)
934{
935 union bpf_attr attr;
936 int fd;
937
938 memset(&attr, 0, sizeof(attr));
939 attr.link_id = id;
940
941 fd = sys_bpf(BPF_LINK_GET_FD_BY_ID, &attr, sizeof(attr));
942 return libbpf_err_errno(fd);
943}
944
945int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
946{
947 union bpf_attr attr;
948 int err;
949
950 memset(&attr, 0, sizeof(attr));
951 attr.info.bpf_fd = bpf_fd;
952 attr.info.info_len = *info_len;
953 attr.info.info = ptr_to_u64(info);
954
955 err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
956
957 if (!err)
958 *info_len = attr.info.info_len;
959
960 return libbpf_err_errno(err);
961}
962
963int bpf_raw_tracepoint_open(const char *name, int prog_fd)
964{
965 union bpf_attr attr;
966 int fd;
967
968 memset(&attr, 0, sizeof(attr));
969 attr.raw_tracepoint.name = ptr_to_u64(name);
970 attr.raw_tracepoint.prog_fd = prog_fd;
971
972 fd = sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
973 return libbpf_err_errno(fd);
974}
975
976int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
977 bool do_log)
978{
979 union bpf_attr attr = {};
980 int fd;
981
982 attr.btf = ptr_to_u64(btf);
983 attr.btf_size = btf_size;
984
985retry:
986 if (do_log && log_buf && log_buf_size) {
987 attr.btf_log_level = 1;
988 attr.btf_log_size = log_buf_size;
989 attr.btf_log_buf = ptr_to_u64(log_buf);
990 }
991
992 fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
993
994 if (fd < 0 && !do_log && log_buf && log_buf_size) {
995 do_log = true;
996 goto retry;
997 }
998
999 return libbpf_err_errno(fd);
1000}
1001
1002int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1003 __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1004 __u64 *probe_addr)
1005{
1006 union bpf_attr attr = {};
1007 int err;
1008
1009 attr.task_fd_query.pid = pid;
1010 attr.task_fd_query.fd = fd;
1011 attr.task_fd_query.flags = flags;
1012 attr.task_fd_query.buf = ptr_to_u64(buf);
1013 attr.task_fd_query.buf_len = *buf_len;
1014
1015 err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
1016
1017 *buf_len = attr.task_fd_query.buf_len;
1018 *prog_id = attr.task_fd_query.prog_id;
1019 *fd_type = attr.task_fd_query.fd_type;
1020 *probe_offset = attr.task_fd_query.probe_offset;
1021 *probe_addr = attr.task_fd_query.probe_addr;
1022
1023 return libbpf_err_errno(err);
1024}
1025
1026int bpf_enable_stats(enum bpf_stats_type type)
1027{
1028 union bpf_attr attr;
1029 int fd;
1030
1031 memset(&attr, 0, sizeof(attr));
1032 attr.enable_stats.type = type;
1033
1034 fd = sys_bpf(BPF_ENABLE_STATS, &attr, sizeof(attr));
1035 return libbpf_err_errno(fd);
1036}
1037
1038int bpf_prog_bind_map(int prog_fd, int map_fd,
1039 const struct bpf_prog_bind_opts *opts)
1040{
1041 union bpf_attr attr;
1042 int ret;
1043
1044 if (!OPTS_VALID(opts, bpf_prog_bind_opts))
1045 return libbpf_err(-EINVAL);
1046
1047 memset(&attr, 0, sizeof(attr));
1048 attr.prog_bind_map.prog_fd = prog_fd;
1049 attr.prog_bind_map.map_fd = map_fd;
1050 attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1051
1052 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, sizeof(attr));
1053 return libbpf_err_errno(ret);
1054}