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