xref: /openbmc/linux/tools/lib/bpf/bpf.c (revision c9933d494c54f72290831191c09bb8488bfd5905)
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, err;
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 	if (fd >= 0)
874 		return fd;
875 	/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
876 	 * and other similar programs
877 	 */
878 	err = -errno;
879 	if (err != -EINVAL)
880 		return libbpf_err(err);
881 
882 	/* if user used features not supported by
883 	 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
884 	 */
885 	if (attr.link_create.target_fd || attr.link_create.target_btf_id)
886 		return libbpf_err(err);
887 	if (!OPTS_ZEROED(opts, sz))
888 		return libbpf_err(err);
889 
890 	/* otherwise, for few select kinds of programs that can be
891 	 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
892 	 * a fallback for older kernels
893 	 */
894 	switch (attach_type) {
895 	case BPF_TRACE_RAW_TP:
896 	case BPF_LSM_MAC:
897 	case BPF_TRACE_FENTRY:
898 	case BPF_TRACE_FEXIT:
899 	case BPF_MODIFY_RETURN:
900 		return bpf_raw_tracepoint_open(NULL, prog_fd);
901 	default:
902 		return libbpf_err(err);
903 	}
904 }
905 
906 int bpf_link_detach(int link_fd)
907 {
908 	union bpf_attr attr;
909 	int ret;
910 
911 	memset(&attr, 0, sizeof(attr));
912 	attr.link_detach.link_fd = link_fd;
913 
914 	ret = sys_bpf(BPF_LINK_DETACH, &attr, sizeof(attr));
915 	return libbpf_err_errno(ret);
916 }
917 
918 int bpf_link_update(int link_fd, int new_prog_fd,
919 		    const struct bpf_link_update_opts *opts)
920 {
921 	union bpf_attr attr;
922 	int ret;
923 
924 	if (!OPTS_VALID(opts, bpf_link_update_opts))
925 		return libbpf_err(-EINVAL);
926 
927 	memset(&attr, 0, sizeof(attr));
928 	attr.link_update.link_fd = link_fd;
929 	attr.link_update.new_prog_fd = new_prog_fd;
930 	attr.link_update.flags = OPTS_GET(opts, flags, 0);
931 	attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
932 
933 	ret = sys_bpf(BPF_LINK_UPDATE, &attr, sizeof(attr));
934 	return libbpf_err_errno(ret);
935 }
936 
937 int bpf_iter_create(int link_fd)
938 {
939 	union bpf_attr attr;
940 	int fd;
941 
942 	memset(&attr, 0, sizeof(attr));
943 	attr.iter_create.link_fd = link_fd;
944 
945 	fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, sizeof(attr));
946 	return libbpf_err_errno(fd);
947 }
948 
949 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
950 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
951 {
952 	union bpf_attr attr;
953 	int ret;
954 
955 	memset(&attr, 0, sizeof(attr));
956 	attr.query.target_fd	= target_fd;
957 	attr.query.attach_type	= type;
958 	attr.query.query_flags	= query_flags;
959 	attr.query.prog_cnt	= *prog_cnt;
960 	attr.query.prog_ids	= ptr_to_u64(prog_ids);
961 
962 	ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
963 
964 	if (attach_flags)
965 		*attach_flags = attr.query.attach_flags;
966 	*prog_cnt = attr.query.prog_cnt;
967 
968 	return libbpf_err_errno(ret);
969 }
970 
971 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
972 		      void *data_out, __u32 *size_out, __u32 *retval,
973 		      __u32 *duration)
974 {
975 	union bpf_attr attr;
976 	int ret;
977 
978 	memset(&attr, 0, sizeof(attr));
979 	attr.test.prog_fd = prog_fd;
980 	attr.test.data_in = ptr_to_u64(data);
981 	attr.test.data_out = ptr_to_u64(data_out);
982 	attr.test.data_size_in = size;
983 	attr.test.repeat = repeat;
984 
985 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
986 
987 	if (size_out)
988 		*size_out = attr.test.data_size_out;
989 	if (retval)
990 		*retval = attr.test.retval;
991 	if (duration)
992 		*duration = attr.test.duration;
993 
994 	return libbpf_err_errno(ret);
995 }
996 
997 int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
998 {
999 	union bpf_attr attr;
1000 	int ret;
1001 
1002 	if (!test_attr->data_out && test_attr->data_size_out > 0)
1003 		return libbpf_err(-EINVAL);
1004 
1005 	memset(&attr, 0, sizeof(attr));
1006 	attr.test.prog_fd = test_attr->prog_fd;
1007 	attr.test.data_in = ptr_to_u64(test_attr->data_in);
1008 	attr.test.data_out = ptr_to_u64(test_attr->data_out);
1009 	attr.test.data_size_in = test_attr->data_size_in;
1010 	attr.test.data_size_out = test_attr->data_size_out;
1011 	attr.test.ctx_in = ptr_to_u64(test_attr->ctx_in);
1012 	attr.test.ctx_out = ptr_to_u64(test_attr->ctx_out);
1013 	attr.test.ctx_size_in = test_attr->ctx_size_in;
1014 	attr.test.ctx_size_out = test_attr->ctx_size_out;
1015 	attr.test.repeat = test_attr->repeat;
1016 
1017 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
1018 
1019 	test_attr->data_size_out = attr.test.data_size_out;
1020 	test_attr->ctx_size_out = attr.test.ctx_size_out;
1021 	test_attr->retval = attr.test.retval;
1022 	test_attr->duration = attr.test.duration;
1023 
1024 	return libbpf_err_errno(ret);
1025 }
1026 
1027 int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
1028 {
1029 	union bpf_attr attr;
1030 	int ret;
1031 
1032 	if (!OPTS_VALID(opts, bpf_test_run_opts))
1033 		return libbpf_err(-EINVAL);
1034 
1035 	memset(&attr, 0, sizeof(attr));
1036 	attr.test.prog_fd = prog_fd;
1037 	attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
1038 	attr.test.cpu = OPTS_GET(opts, cpu, 0);
1039 	attr.test.flags = OPTS_GET(opts, flags, 0);
1040 	attr.test.repeat = OPTS_GET(opts, repeat, 0);
1041 	attr.test.duration = OPTS_GET(opts, duration, 0);
1042 	attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
1043 	attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
1044 	attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
1045 	attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
1046 	attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
1047 	attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
1048 	attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
1049 	attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
1050 
1051 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
1052 
1053 	OPTS_SET(opts, data_size_out, attr.test.data_size_out);
1054 	OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
1055 	OPTS_SET(opts, duration, attr.test.duration);
1056 	OPTS_SET(opts, retval, attr.test.retval);
1057 
1058 	return libbpf_err_errno(ret);
1059 }
1060 
1061 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
1062 {
1063 	union bpf_attr attr;
1064 	int err;
1065 
1066 	memset(&attr, 0, sizeof(attr));
1067 	attr.start_id = start_id;
1068 
1069 	err = sys_bpf(cmd, &attr, sizeof(attr));
1070 	if (!err)
1071 		*next_id = attr.next_id;
1072 
1073 	return libbpf_err_errno(err);
1074 }
1075 
1076 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
1077 {
1078 	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
1079 }
1080 
1081 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
1082 {
1083 	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
1084 }
1085 
1086 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
1087 {
1088 	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
1089 }
1090 
1091 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
1092 {
1093 	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
1094 }
1095 
1096 int bpf_prog_get_fd_by_id(__u32 id)
1097 {
1098 	union bpf_attr attr;
1099 	int fd;
1100 
1101 	memset(&attr, 0, sizeof(attr));
1102 	attr.prog_id = id;
1103 
1104 	fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
1105 	return libbpf_err_errno(fd);
1106 }
1107 
1108 int bpf_map_get_fd_by_id(__u32 id)
1109 {
1110 	union bpf_attr attr;
1111 	int fd;
1112 
1113 	memset(&attr, 0, sizeof(attr));
1114 	attr.map_id = id;
1115 
1116 	fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
1117 	return libbpf_err_errno(fd);
1118 }
1119 
1120 int bpf_btf_get_fd_by_id(__u32 id)
1121 {
1122 	union bpf_attr attr;
1123 	int fd;
1124 
1125 	memset(&attr, 0, sizeof(attr));
1126 	attr.btf_id = id;
1127 
1128 	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
1129 	return libbpf_err_errno(fd);
1130 }
1131 
1132 int bpf_link_get_fd_by_id(__u32 id)
1133 {
1134 	union bpf_attr attr;
1135 	int fd;
1136 
1137 	memset(&attr, 0, sizeof(attr));
1138 	attr.link_id = id;
1139 
1140 	fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, sizeof(attr));
1141 	return libbpf_err_errno(fd);
1142 }
1143 
1144 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
1145 {
1146 	union bpf_attr attr;
1147 	int err;
1148 
1149 	memset(&attr, 0, sizeof(attr));
1150 	attr.info.bpf_fd = bpf_fd;
1151 	attr.info.info_len = *info_len;
1152 	attr.info.info = ptr_to_u64(info);
1153 
1154 	err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
1155 
1156 	if (!err)
1157 		*info_len = attr.info.info_len;
1158 
1159 	return libbpf_err_errno(err);
1160 }
1161 
1162 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
1163 {
1164 	union bpf_attr attr;
1165 	int fd;
1166 
1167 	memset(&attr, 0, sizeof(attr));
1168 	attr.raw_tracepoint.name = ptr_to_u64(name);
1169 	attr.raw_tracepoint.prog_fd = prog_fd;
1170 
1171 	fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
1172 	return libbpf_err_errno(fd);
1173 }
1174 
1175 int bpf_btf_load(const void *btf_data, size_t btf_size, const struct bpf_btf_load_opts *opts)
1176 {
1177 	const size_t attr_sz = offsetofend(union bpf_attr, btf_log_level);
1178 	union bpf_attr attr;
1179 	char *log_buf;
1180 	size_t log_size;
1181 	__u32 log_level;
1182 	int fd;
1183 
1184 	bump_rlimit_memlock();
1185 
1186 	memset(&attr, 0, attr_sz);
1187 
1188 	if (!OPTS_VALID(opts, bpf_btf_load_opts))
1189 		return libbpf_err(-EINVAL);
1190 
1191 	log_buf = OPTS_GET(opts, log_buf, NULL);
1192 	log_size = OPTS_GET(opts, log_size, 0);
1193 	log_level = OPTS_GET(opts, log_level, 0);
1194 
1195 	if (log_size > UINT_MAX)
1196 		return libbpf_err(-EINVAL);
1197 	if (log_size && !log_buf)
1198 		return libbpf_err(-EINVAL);
1199 
1200 	attr.btf = ptr_to_u64(btf_data);
1201 	attr.btf_size = btf_size;
1202 	/* log_level == 0 and log_buf != NULL means "try loading without
1203 	 * log_buf, but retry with log_buf and log_level=1 on error", which is
1204 	 * consistent across low-level and high-level BTF and program loading
1205 	 * APIs within libbpf and provides a sensible behavior in practice
1206 	 */
1207 	if (log_level) {
1208 		attr.btf_log_buf = ptr_to_u64(log_buf);
1209 		attr.btf_log_size = (__u32)log_size;
1210 		attr.btf_log_level = log_level;
1211 	}
1212 
1213 	fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1214 	if (fd < 0 && log_buf && log_level == 0) {
1215 		attr.btf_log_buf = ptr_to_u64(log_buf);
1216 		attr.btf_log_size = (__u32)log_size;
1217 		attr.btf_log_level = 1;
1218 		fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1219 	}
1220 	return libbpf_err_errno(fd);
1221 }
1222 
1223 int bpf_load_btf(const void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log)
1224 {
1225 	LIBBPF_OPTS(bpf_btf_load_opts, opts);
1226 	int fd;
1227 
1228 retry:
1229 	if (do_log && log_buf && log_buf_size) {
1230 		opts.log_buf = log_buf;
1231 		opts.log_size = log_buf_size;
1232 		opts.log_level = 1;
1233 	}
1234 
1235 	fd = bpf_btf_load(btf, btf_size, &opts);
1236 	if (fd < 0 && !do_log && log_buf && log_buf_size) {
1237 		do_log = true;
1238 		goto retry;
1239 	}
1240 
1241 	return libbpf_err_errno(fd);
1242 }
1243 
1244 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1245 		      __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1246 		      __u64 *probe_addr)
1247 {
1248 	union bpf_attr attr = {};
1249 	int err;
1250 
1251 	attr.task_fd_query.pid = pid;
1252 	attr.task_fd_query.fd = fd;
1253 	attr.task_fd_query.flags = flags;
1254 	attr.task_fd_query.buf = ptr_to_u64(buf);
1255 	attr.task_fd_query.buf_len = *buf_len;
1256 
1257 	err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
1258 
1259 	*buf_len = attr.task_fd_query.buf_len;
1260 	*prog_id = attr.task_fd_query.prog_id;
1261 	*fd_type = attr.task_fd_query.fd_type;
1262 	*probe_offset = attr.task_fd_query.probe_offset;
1263 	*probe_addr = attr.task_fd_query.probe_addr;
1264 
1265 	return libbpf_err_errno(err);
1266 }
1267 
1268 int bpf_enable_stats(enum bpf_stats_type type)
1269 {
1270 	union bpf_attr attr;
1271 	int fd;
1272 
1273 	memset(&attr, 0, sizeof(attr));
1274 	attr.enable_stats.type = type;
1275 
1276 	fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, sizeof(attr));
1277 	return libbpf_err_errno(fd);
1278 }
1279 
1280 int bpf_prog_bind_map(int prog_fd, int map_fd,
1281 		      const struct bpf_prog_bind_opts *opts)
1282 {
1283 	union bpf_attr attr;
1284 	int ret;
1285 
1286 	if (!OPTS_VALID(opts, bpf_prog_bind_opts))
1287 		return libbpf_err(-EINVAL);
1288 
1289 	memset(&attr, 0, sizeof(attr));
1290 	attr.prog_bind_map.prog_fd = prog_fd;
1291 	attr.prog_bind_map.map_fd = map_fd;
1292 	attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1293 
1294 	ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, sizeof(attr));
1295 	return libbpf_err_errno(ret);
1296 }
1297