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