xref: /linux/tools/lib/bpf/bpf.c (revision 84f7a49e76ec8e0a1e18f3758e89800f8cf8cfc6)
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 # elif defined(__loongarch__)
63 #  define __NR_bpf 280
64 # else
65 #  error __NR_bpf not defined. libbpf does not support your arch.
66 # endif
67 #endif
68 
69 static inline __u64 ptr_to_u64(const void *ptr)
70 {
71 	return (__u64) (unsigned long) ptr;
72 }
73 
74 static inline int sys_bpf_ext(enum bpf_cmd cmd, union bpf_attr *attr,
75 			      unsigned int size,
76 			      struct bpf_common_attr *attr_common,
77 			      unsigned int size_common)
78 {
79 	cmd = attr_common ? (cmd | BPF_COMMON_ATTRS) : (cmd & ~BPF_COMMON_ATTRS);
80 	return syscall(__NR_bpf, cmd, attr, size, attr_common, size_common);
81 }
82 
83 static inline int sys_bpf_ext_fd(enum bpf_cmd cmd, union bpf_attr *attr,
84 				 unsigned int size,
85 				 struct bpf_common_attr *attr_common,
86 				 unsigned int size_common)
87 {
88 	int fd;
89 
90 	fd = sys_bpf_ext(cmd, attr, size, attr_common, size_common);
91 	return ensure_good_fd(fd);
92 }
93 
94 int probe_sys_bpf_ext(void)
95 {
96 	const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
97 	union bpf_attr attr;
98 	int fd;
99 
100 	memset(&attr, 0, attr_sz);
101 	fd = syscall(__NR_bpf, BPF_PROG_LOAD | BPF_COMMON_ATTRS, &attr, attr_sz, NULL,
102 		     sizeof(struct bpf_common_attr));
103 	if (fd >= 0) {
104 		close(fd);
105 		return -EINVAL;
106 	}
107 	return errno == EFAULT ? 1 : 0;
108 }
109 
110 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
111 			  unsigned int size)
112 {
113 	return syscall(__NR_bpf, cmd, attr, size);
114 }
115 
116 static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,
117 			     unsigned int size)
118 {
119 	int fd;
120 
121 	fd = sys_bpf(cmd, attr, size);
122 	return ensure_good_fd(fd);
123 }
124 
125 int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
126 {
127 	int fd;
128 
129 	do {
130 		fd = sys_bpf_fd(BPF_PROG_LOAD, attr, size);
131 	} while (fd < 0 && errno == EAGAIN && --attempts > 0);
132 
133 	return fd;
134 }
135 
136 /* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
137  * memcg-based memory accounting for BPF maps and progs. This was done in [0].
138  * We use the support for bpf_ktime_get_coarse_ns() helper, which was added in
139  * the same 5.11 Linux release ([1]), to detect memcg-based accounting for BPF.
140  *
141  *   [0] https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
142  *   [1] d05512618056 ("bpf: Add bpf_ktime_get_coarse_ns helper")
143  */
144 int probe_memcg_account(int token_fd)
145 {
146 	const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
147 	struct bpf_insn insns[] = {
148 		BPF_EMIT_CALL(BPF_FUNC_ktime_get_coarse_ns),
149 		BPF_EXIT_INSN(),
150 	};
151 	size_t insn_cnt = ARRAY_SIZE(insns);
152 	union bpf_attr attr;
153 	int prog_fd;
154 
155 	/* attempt loading freplace trying to use custom BTF */
156 	memset(&attr, 0, attr_sz);
157 	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
158 	attr.insns = ptr_to_u64(insns);
159 	attr.insn_cnt = insn_cnt;
160 	attr.license = ptr_to_u64("GPL");
161 	attr.prog_token_fd = token_fd;
162 	if (token_fd)
163 		attr.prog_flags |= BPF_F_TOKEN_FD;
164 
165 	prog_fd = sys_bpf_fd(BPF_PROG_LOAD, &attr, attr_sz);
166 	if (prog_fd >= 0) {
167 		close(prog_fd);
168 		return 1;
169 	}
170 	return 0;
171 }
172 
173 static bool memlock_bumped;
174 static rlim_t memlock_rlim = RLIM_INFINITY;
175 
176 int libbpf_set_memlock_rlim(size_t memlock_bytes)
177 {
178 	if (memlock_bumped)
179 		return libbpf_err(-EBUSY);
180 
181 	memlock_rlim = memlock_bytes;
182 	return 0;
183 }
184 
185 int bump_rlimit_memlock(void)
186 {
187 	struct rlimit rlim;
188 
189 	/* if kernel supports memcg-based accounting, skip bumping RLIMIT_MEMLOCK */
190 	if (memlock_bumped || feat_supported(NULL, FEAT_MEMCG_ACCOUNT))
191 		return 0;
192 
193 	memlock_bumped = true;
194 
195 	/* zero memlock_rlim disables auto-bumping RLIMIT_MEMLOCK */
196 	if (memlock_rlim == 0)
197 		return 0;
198 
199 	rlim.rlim_cur = rlim.rlim_max = memlock_rlim;
200 	if (setrlimit(RLIMIT_MEMLOCK, &rlim))
201 		return -errno;
202 
203 	return 0;
204 }
205 
206 int bpf_map_create(enum bpf_map_type map_type,
207 		   const char *map_name,
208 		   __u32 key_size,
209 		   __u32 value_size,
210 		   __u32 max_entries,
211 		   const struct bpf_map_create_opts *opts)
212 {
213 	const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size);
214 	const size_t attr_common_sz = sizeof(struct bpf_common_attr);
215 	struct bpf_common_attr attr_common;
216 	struct bpf_log_opts *log_opts;
217 	union bpf_attr attr;
218 	int fd;
219 
220 	bump_rlimit_memlock();
221 
222 	memset(&attr, 0, attr_sz);
223 
224 	if (!OPTS_VALID(opts, bpf_map_create_opts))
225 		return libbpf_err(-EINVAL);
226 
227 	attr.map_type = map_type;
228 	if (map_name && feat_supported(NULL, FEAT_PROG_NAME))
229 		libbpf_strlcpy(attr.map_name, map_name, sizeof(attr.map_name));
230 	attr.key_size = key_size;
231 	attr.value_size = value_size;
232 	attr.max_entries = max_entries;
233 
234 	attr.btf_fd = OPTS_GET(opts, btf_fd, 0);
235 	attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
236 	attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
237 	attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
238 	attr.value_type_btf_obj_fd = OPTS_GET(opts, value_type_btf_obj_fd, 0);
239 
240 	attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
241 	attr.map_flags = OPTS_GET(opts, map_flags, 0);
242 	attr.map_extra = OPTS_GET(opts, map_extra, 0);
243 	attr.numa_node = OPTS_GET(opts, numa_node, 0);
244 	attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
245 
246 	attr.map_token_fd = OPTS_GET(opts, token_fd, 0);
247 	attr.excl_prog_hash = ptr_to_u64(OPTS_GET(opts, excl_prog_hash, NULL));
248 	attr.excl_prog_hash_size = OPTS_GET(opts, excl_prog_hash_size, 0);
249 
250 	log_opts = OPTS_GET(opts, log_opts, NULL);
251 	if (!OPTS_VALID(log_opts, bpf_log_opts))
252 		return libbpf_err(-EINVAL);
253 
254 	if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
255 		memset(&attr_common, 0, attr_common_sz);
256 		attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));
257 		attr_common.log_size = OPTS_GET(log_opts, size, 0);
258 		attr_common.log_level = OPTS_GET(log_opts, level, 0);
259 		fd = sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &attr_common, attr_common_sz);
260 		OPTS_SET(log_opts, true_size, attr_common.log_true_size);
261 	} else {
262 		fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
263 		OPTS_SET(log_opts, true_size, 0);
264 	}
265 	return libbpf_err_errno(fd);
266 }
267 
268 static void *
269 alloc_zero_tailing_info(const void *orecord, __u32 cnt,
270 			__u32 actual_rec_size, __u32 expected_rec_size)
271 {
272 	__u64 info_len = (__u64)actual_rec_size * cnt;
273 	void *info, *nrecord;
274 	int i;
275 
276 	info = malloc(info_len);
277 	if (!info)
278 		return NULL;
279 
280 	/* zero out bytes kernel does not understand */
281 	nrecord = info;
282 	for (i = 0; i < cnt; i++) {
283 		memcpy(nrecord, orecord, expected_rec_size);
284 		memset(nrecord + expected_rec_size, 0,
285 		       actual_rec_size - expected_rec_size);
286 		orecord += actual_rec_size;
287 		nrecord += actual_rec_size;
288 	}
289 
290 	return info;
291 }
292 
293 int bpf_prog_load(enum bpf_prog_type prog_type,
294 		  const char *prog_name, const char *license,
295 		  const struct bpf_insn *insns, size_t insn_cnt,
296 		  struct bpf_prog_load_opts *opts)
297 {
298 	const size_t attr_sz = offsetofend(union bpf_attr, keyring_id);
299 	void *finfo = NULL, *linfo = NULL;
300 	const char *func_info, *line_info;
301 	__u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
302 	__u32 func_info_rec_size, line_info_rec_size;
303 	int fd, attempts;
304 	union bpf_attr attr;
305 	char *log_buf;
306 
307 	bump_rlimit_memlock();
308 
309 	if (!OPTS_VALID(opts, bpf_prog_load_opts))
310 		return libbpf_err(-EINVAL);
311 
312 	attempts = OPTS_GET(opts, attempts, 0);
313 	if (attempts < 0)
314 		return libbpf_err(-EINVAL);
315 	if (attempts == 0)
316 		attempts = PROG_LOAD_ATTEMPTS;
317 
318 	memset(&attr, 0, attr_sz);
319 
320 	attr.prog_type = prog_type;
321 	attr.expected_attach_type = OPTS_GET(opts, expected_attach_type, 0);
322 
323 	attr.prog_btf_fd = OPTS_GET(opts, prog_btf_fd, 0);
324 	attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
325 	attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
326 	attr.kern_version = OPTS_GET(opts, kern_version, 0);
327 	attr.prog_token_fd = OPTS_GET(opts, token_fd, 0);
328 
329 	if (prog_name && feat_supported(NULL, FEAT_PROG_NAME))
330 		libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
331 	attr.license = ptr_to_u64(license);
332 
333 	if (insn_cnt > UINT_MAX)
334 		return libbpf_err(-E2BIG);
335 
336 	attr.insns = ptr_to_u64(insns);
337 	attr.insn_cnt = (__u32)insn_cnt;
338 
339 	attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
340 	attach_btf_obj_fd = OPTS_GET(opts, attach_btf_obj_fd, 0);
341 
342 	if (attach_prog_fd && attach_btf_obj_fd)
343 		return libbpf_err(-EINVAL);
344 
345 	attr.attach_btf_id = OPTS_GET(opts, attach_btf_id, 0);
346 	if (attach_prog_fd)
347 		attr.attach_prog_fd = attach_prog_fd;
348 	else
349 		attr.attach_btf_obj_fd = attach_btf_obj_fd;
350 
351 	log_buf = OPTS_GET(opts, log_buf, NULL);
352 	log_size = OPTS_GET(opts, log_size, 0);
353 	log_level = OPTS_GET(opts, log_level, 0);
354 
355 	if (!!log_buf != !!log_size)
356 		return libbpf_err(-EINVAL);
357 
358 	func_info_rec_size = OPTS_GET(opts, func_info_rec_size, 0);
359 	func_info = OPTS_GET(opts, func_info, NULL);
360 	attr.func_info_rec_size = func_info_rec_size;
361 	attr.func_info = ptr_to_u64(func_info);
362 	attr.func_info_cnt = OPTS_GET(opts, func_info_cnt, 0);
363 
364 	line_info_rec_size = OPTS_GET(opts, line_info_rec_size, 0);
365 	line_info = OPTS_GET(opts, line_info, NULL);
366 	attr.line_info_rec_size = line_info_rec_size;
367 	attr.line_info = ptr_to_u64(line_info);
368 	attr.line_info_cnt = OPTS_GET(opts, line_info_cnt, 0);
369 
370 	attr.fd_array = ptr_to_u64(OPTS_GET(opts, fd_array, NULL));
371 	attr.fd_array_cnt = OPTS_GET(opts, fd_array_cnt, 0);
372 
373 	if (log_level) {
374 		attr.log_buf = ptr_to_u64(log_buf);
375 		attr.log_size = log_size;
376 		attr.log_level = log_level;
377 	}
378 
379 	fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
380 	OPTS_SET(opts, log_true_size, attr.log_true_size);
381 	if (fd >= 0)
382 		return fd;
383 
384 	/* After bpf_prog_load, the kernel may modify certain attributes
385 	 * to give user space a hint how to deal with loading failure.
386 	 * Check to see whether we can make some changes and load again.
387 	 */
388 	while (errno == E2BIG && (!finfo || !linfo)) {
389 		if (!finfo && attr.func_info_cnt &&
390 		    attr.func_info_rec_size < func_info_rec_size) {
391 			/* try with corrected func info records */
392 			finfo = alloc_zero_tailing_info(func_info,
393 							attr.func_info_cnt,
394 							func_info_rec_size,
395 							attr.func_info_rec_size);
396 			if (!finfo) {
397 				errno = E2BIG;
398 				goto done;
399 			}
400 
401 			attr.func_info = ptr_to_u64(finfo);
402 			attr.func_info_rec_size = func_info_rec_size;
403 		} else if (!linfo && attr.line_info_cnt &&
404 			   attr.line_info_rec_size < line_info_rec_size) {
405 			linfo = alloc_zero_tailing_info(line_info,
406 							attr.line_info_cnt,
407 							line_info_rec_size,
408 							attr.line_info_rec_size);
409 			if (!linfo) {
410 				errno = E2BIG;
411 				goto done;
412 			}
413 
414 			attr.line_info = ptr_to_u64(linfo);
415 			attr.line_info_rec_size = line_info_rec_size;
416 		} else {
417 			break;
418 		}
419 
420 		fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
421 		OPTS_SET(opts, log_true_size, attr.log_true_size);
422 		if (fd >= 0)
423 			goto done;
424 	}
425 
426 	if (log_level == 0 && log_buf) {
427 		/* log_level == 0 with non-NULL log_buf requires retrying on error
428 		 * with log_level == 1 and log_buf/log_buf_size set, to get details of
429 		 * failure
430 		 */
431 		attr.log_buf = ptr_to_u64(log_buf);
432 		attr.log_size = log_size;
433 		attr.log_level = 1;
434 
435 		fd = sys_bpf_prog_load(&attr, attr_sz, attempts);
436 		OPTS_SET(opts, log_true_size, attr.log_true_size);
437 	}
438 done:
439 	/* free() doesn't affect errno, so we don't need to restore it */
440 	free(finfo);
441 	free(linfo);
442 	return libbpf_err_errno(fd);
443 }
444 
445 int bpf_map_update_elem(int fd, const void *key, const void *value,
446 			__u64 flags)
447 {
448 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
449 	union bpf_attr attr;
450 	int ret;
451 
452 	memset(&attr, 0, attr_sz);
453 	attr.map_fd = fd;
454 	attr.key = ptr_to_u64(key);
455 	attr.value = ptr_to_u64(value);
456 	attr.flags = flags;
457 
458 	ret = sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, attr_sz);
459 	return libbpf_err_errno(ret);
460 }
461 
462 int bpf_map_lookup_elem(int fd, const void *key, void *value)
463 {
464 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
465 	union bpf_attr attr;
466 	int ret;
467 
468 	memset(&attr, 0, attr_sz);
469 	attr.map_fd = fd;
470 	attr.key = ptr_to_u64(key);
471 	attr.value = ptr_to_u64(value);
472 
473 	ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
474 	return libbpf_err_errno(ret);
475 }
476 
477 int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
478 {
479 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
480 	union bpf_attr attr;
481 	int ret;
482 
483 	memset(&attr, 0, attr_sz);
484 	attr.map_fd = fd;
485 	attr.key = ptr_to_u64(key);
486 	attr.value = ptr_to_u64(value);
487 	attr.flags = flags;
488 
489 	ret = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, attr_sz);
490 	return libbpf_err_errno(ret);
491 }
492 
493 int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
494 {
495 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
496 	union bpf_attr attr;
497 	int ret;
498 
499 	memset(&attr, 0, attr_sz);
500 	attr.map_fd = fd;
501 	attr.key = ptr_to_u64(key);
502 	attr.value = ptr_to_u64(value);
503 
504 	ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
505 	return libbpf_err_errno(ret);
506 }
507 
508 int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key, void *value, __u64 flags)
509 {
510 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
511 	union bpf_attr attr;
512 	int ret;
513 
514 	memset(&attr, 0, attr_sz);
515 	attr.map_fd = fd;
516 	attr.key = ptr_to_u64(key);
517 	attr.value = ptr_to_u64(value);
518 	attr.flags = flags;
519 
520 	ret = sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, attr_sz);
521 	return libbpf_err_errno(ret);
522 }
523 
524 int bpf_map_delete_elem(int fd, const void *key)
525 {
526 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
527 	union bpf_attr attr;
528 	int ret;
529 
530 	memset(&attr, 0, attr_sz);
531 	attr.map_fd = fd;
532 	attr.key = ptr_to_u64(key);
533 
534 	ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
535 	return libbpf_err_errno(ret);
536 }
537 
538 int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags)
539 {
540 	const size_t attr_sz = offsetofend(union bpf_attr, flags);
541 	union bpf_attr attr;
542 	int ret;
543 
544 	memset(&attr, 0, attr_sz);
545 	attr.map_fd = fd;
546 	attr.key = ptr_to_u64(key);
547 	attr.flags = flags;
548 
549 	ret = sys_bpf(BPF_MAP_DELETE_ELEM, &attr, attr_sz);
550 	return libbpf_err_errno(ret);
551 }
552 
553 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
554 {
555 	const size_t attr_sz = offsetofend(union bpf_attr, next_key);
556 	union bpf_attr attr;
557 	int ret;
558 
559 	memset(&attr, 0, attr_sz);
560 	attr.map_fd = fd;
561 	attr.key = ptr_to_u64(key);
562 	attr.next_key = ptr_to_u64(next_key);
563 
564 	ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, attr_sz);
565 	return libbpf_err_errno(ret);
566 }
567 
568 int bpf_map_freeze(int fd)
569 {
570 	const size_t attr_sz = offsetofend(union bpf_attr, map_fd);
571 	union bpf_attr attr;
572 	int ret;
573 
574 	memset(&attr, 0, attr_sz);
575 	attr.map_fd = fd;
576 
577 	ret = sys_bpf(BPF_MAP_FREEZE, &attr, attr_sz);
578 	return libbpf_err_errno(ret);
579 }
580 
581 static int bpf_map_batch_common(int cmd, int fd, void  *in_batch,
582 				void *out_batch, void *keys, void *values,
583 				__u32 *count,
584 				const struct bpf_map_batch_opts *opts)
585 {
586 	const size_t attr_sz = offsetofend(union bpf_attr, batch);
587 	union bpf_attr attr;
588 	int ret;
589 
590 	if (!OPTS_VALID(opts, bpf_map_batch_opts))
591 		return libbpf_err(-EINVAL);
592 
593 	memset(&attr, 0, attr_sz);
594 	attr.batch.map_fd = fd;
595 	attr.batch.in_batch = ptr_to_u64(in_batch);
596 	attr.batch.out_batch = ptr_to_u64(out_batch);
597 	attr.batch.keys = ptr_to_u64(keys);
598 	attr.batch.values = ptr_to_u64(values);
599 	attr.batch.count = *count;
600 	attr.batch.elem_flags  = OPTS_GET(opts, elem_flags, 0);
601 	attr.batch.flags = OPTS_GET(opts, flags, 0);
602 
603 	ret = sys_bpf(cmd, &attr, attr_sz);
604 	*count = attr.batch.count;
605 
606 	return libbpf_err_errno(ret);
607 }
608 
609 int bpf_map_delete_batch(int fd, const void *keys, __u32 *count,
610 			 const struct bpf_map_batch_opts *opts)
611 {
612 	return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL,
613 				    NULL, (void *)keys, NULL, count, opts);
614 }
615 
616 int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys,
617 			 void *values, __u32 *count,
618 			 const struct bpf_map_batch_opts *opts)
619 {
620 	return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch,
621 				    out_batch, keys, values, count, opts);
622 }
623 
624 int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch,
625 				    void *keys, void *values, __u32 *count,
626 				    const struct bpf_map_batch_opts *opts)
627 {
628 	return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH,
629 				    fd, in_batch, out_batch, keys, values,
630 				    count, opts);
631 }
632 
633 int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *count,
634 			 const struct bpf_map_batch_opts *opts)
635 {
636 	return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL,
637 				    (void *)keys, (void *)values, count, opts);
638 }
639 
640 int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts)
641 {
642 	const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
643 	union bpf_attr attr;
644 	int ret;
645 
646 	if (!OPTS_VALID(opts, bpf_obj_pin_opts))
647 		return libbpf_err(-EINVAL);
648 
649 	memset(&attr, 0, attr_sz);
650 	attr.path_fd = OPTS_GET(opts, path_fd, 0);
651 	attr.pathname = ptr_to_u64((void *)pathname);
652 	attr.file_flags = OPTS_GET(opts, file_flags, 0);
653 	attr.bpf_fd = fd;
654 
655 	ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
656 	return libbpf_err_errno(ret);
657 }
658 
659 int bpf_obj_pin(int fd, const char *pathname)
660 {
661 	return bpf_obj_pin_opts(fd, pathname, NULL);
662 }
663 
664 int bpf_obj_get(const char *pathname)
665 {
666 	return bpf_obj_get_opts(pathname, NULL);
667 }
668 
669 int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
670 {
671 	const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
672 	union bpf_attr attr;
673 	int fd;
674 
675 	if (!OPTS_VALID(opts, bpf_obj_get_opts))
676 		return libbpf_err(-EINVAL);
677 
678 	memset(&attr, 0, attr_sz);
679 	attr.path_fd = OPTS_GET(opts, path_fd, 0);
680 	attr.pathname = ptr_to_u64((void *)pathname);
681 	attr.file_flags = OPTS_GET(opts, file_flags, 0);
682 
683 	fd = sys_bpf_fd(BPF_OBJ_GET, &attr, attr_sz);
684 	return libbpf_err_errno(fd);
685 }
686 
687 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
688 		    unsigned int flags)
689 {
690 	DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
691 		.flags = flags,
692 	);
693 
694 	return bpf_prog_attach_opts(prog_fd, target_fd, type, &opts);
695 }
696 
697 int bpf_prog_attach_opts(int prog_fd, int target, enum bpf_attach_type type,
698 			 const struct bpf_prog_attach_opts *opts)
699 {
700 	const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
701 	__u32 relative_id, flags;
702 	int ret, relative_fd;
703 	union bpf_attr attr;
704 
705 	if (!OPTS_VALID(opts, bpf_prog_attach_opts))
706 		return libbpf_err(-EINVAL);
707 
708 	relative_id = OPTS_GET(opts, relative_id, 0);
709 	relative_fd = OPTS_GET(opts, relative_fd, 0);
710 	flags = OPTS_GET(opts, flags, 0);
711 
712 	/* validate we don't have unexpected combinations of non-zero fields */
713 	if (relative_fd && relative_id)
714 		return libbpf_err(-EINVAL);
715 
716 	memset(&attr, 0, attr_sz);
717 	attr.target_fd		= target;
718 	attr.attach_bpf_fd	= prog_fd;
719 	attr.attach_type	= type;
720 	attr.replace_bpf_fd	= OPTS_GET(opts, replace_fd, 0);
721 	attr.expected_revision	= OPTS_GET(opts, expected_revision, 0);
722 
723 	if (relative_id) {
724 		attr.attach_flags = flags | BPF_F_ID;
725 		attr.relative_id  = relative_id;
726 	} else {
727 		attr.attach_flags = flags;
728 		attr.relative_fd  = relative_fd;
729 	}
730 
731 	ret = sys_bpf(BPF_PROG_ATTACH, &attr, attr_sz);
732 	return libbpf_err_errno(ret);
733 }
734 
735 int bpf_prog_detach_opts(int prog_fd, int target, enum bpf_attach_type type,
736 			 const struct bpf_prog_detach_opts *opts)
737 {
738 	const size_t attr_sz = offsetofend(union bpf_attr, expected_revision);
739 	__u32 relative_id, flags;
740 	int ret, relative_fd;
741 	union bpf_attr attr;
742 
743 	if (!OPTS_VALID(opts, bpf_prog_detach_opts))
744 		return libbpf_err(-EINVAL);
745 
746 	relative_id = OPTS_GET(opts, relative_id, 0);
747 	relative_fd = OPTS_GET(opts, relative_fd, 0);
748 	flags = OPTS_GET(opts, flags, 0);
749 
750 	/* validate we don't have unexpected combinations of non-zero fields */
751 	if (relative_fd && relative_id)
752 		return libbpf_err(-EINVAL);
753 
754 	memset(&attr, 0, attr_sz);
755 	attr.target_fd		= target;
756 	attr.attach_bpf_fd	= prog_fd;
757 	attr.attach_type	= type;
758 	attr.expected_revision	= OPTS_GET(opts, expected_revision, 0);
759 
760 	if (relative_id) {
761 		attr.attach_flags = flags | BPF_F_ID;
762 		attr.relative_id  = relative_id;
763 	} else {
764 		attr.attach_flags = flags;
765 		attr.relative_fd  = relative_fd;
766 	}
767 
768 	ret = sys_bpf(BPF_PROG_DETACH, &attr, attr_sz);
769 	return libbpf_err_errno(ret);
770 }
771 
772 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
773 {
774 	return bpf_prog_detach_opts(0, target_fd, type, NULL);
775 }
776 
777 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
778 {
779 	return bpf_prog_detach_opts(prog_fd, target_fd, type, NULL);
780 }
781 
782 int bpf_link_create(int prog_fd, int target_fd,
783 		    enum bpf_attach_type attach_type,
784 		    const struct bpf_link_create_opts *opts)
785 {
786 	const size_t attr_sz = offsetofend(union bpf_attr, link_create);
787 	__u32 target_btf_id, iter_info_len, relative_id;
788 	int fd, err, relative_fd;
789 	union bpf_attr attr;
790 
791 	if (!OPTS_VALID(opts, bpf_link_create_opts))
792 		return libbpf_err(-EINVAL);
793 
794 	iter_info_len = OPTS_GET(opts, iter_info_len, 0);
795 	target_btf_id = OPTS_GET(opts, target_btf_id, 0);
796 
797 	/* validate we don't have unexpected combinations of non-zero fields */
798 	if (iter_info_len || target_btf_id) {
799 		if (iter_info_len && target_btf_id)
800 			return libbpf_err(-EINVAL);
801 		if (!OPTS_ZEROED(opts, target_btf_id))
802 			return libbpf_err(-EINVAL);
803 	}
804 
805 	memset(&attr, 0, attr_sz);
806 	attr.link_create.prog_fd = prog_fd;
807 	attr.link_create.target_fd = target_fd;
808 	attr.link_create.attach_type = attach_type;
809 	attr.link_create.flags = OPTS_GET(opts, flags, 0);
810 
811 	if (target_btf_id) {
812 		attr.link_create.target_btf_id = target_btf_id;
813 		goto proceed;
814 	}
815 
816 	switch (attach_type) {
817 	case BPF_TRACE_ITER:
818 		attr.link_create.iter_info = ptr_to_u64(OPTS_GET(opts, iter_info, (void *)0));
819 		attr.link_create.iter_info_len = iter_info_len;
820 		break;
821 	case BPF_PERF_EVENT:
822 		attr.link_create.perf_event.bpf_cookie = OPTS_GET(opts, perf_event.bpf_cookie, 0);
823 		if (!OPTS_ZEROED(opts, perf_event))
824 			return libbpf_err(-EINVAL);
825 		break;
826 	case BPF_TRACE_KPROBE_MULTI:
827 	case BPF_TRACE_KPROBE_SESSION:
828 		attr.link_create.kprobe_multi.flags = OPTS_GET(opts, kprobe_multi.flags, 0);
829 		attr.link_create.kprobe_multi.cnt = OPTS_GET(opts, kprobe_multi.cnt, 0);
830 		attr.link_create.kprobe_multi.syms = ptr_to_u64(OPTS_GET(opts, kprobe_multi.syms, 0));
831 		attr.link_create.kprobe_multi.addrs = ptr_to_u64(OPTS_GET(opts, kprobe_multi.addrs, 0));
832 		attr.link_create.kprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, kprobe_multi.cookies, 0));
833 		if (!OPTS_ZEROED(opts, kprobe_multi))
834 			return libbpf_err(-EINVAL);
835 		break;
836 	case BPF_TRACE_UPROBE_MULTI:
837 	case BPF_TRACE_UPROBE_SESSION:
838 		attr.link_create.uprobe_multi.flags = OPTS_GET(opts, uprobe_multi.flags, 0);
839 		attr.link_create.uprobe_multi.cnt = OPTS_GET(opts, uprobe_multi.cnt, 0);
840 		attr.link_create.uprobe_multi.path = ptr_to_u64(OPTS_GET(opts, uprobe_multi.path, 0));
841 		attr.link_create.uprobe_multi.offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.offsets, 0));
842 		attr.link_create.uprobe_multi.ref_ctr_offsets = ptr_to_u64(OPTS_GET(opts, uprobe_multi.ref_ctr_offsets, 0));
843 		attr.link_create.uprobe_multi.cookies = ptr_to_u64(OPTS_GET(opts, uprobe_multi.cookies, 0));
844 		attr.link_create.uprobe_multi.pid = OPTS_GET(opts, uprobe_multi.pid, 0);
845 		if (!OPTS_ZEROED(opts, uprobe_multi))
846 			return libbpf_err(-EINVAL);
847 		break;
848 	case BPF_TRACE_RAW_TP:
849 	case BPF_TRACE_FENTRY:
850 	case BPF_TRACE_FEXIT:
851 	case BPF_MODIFY_RETURN:
852 	case BPF_TRACE_FSESSION:
853 	case BPF_LSM_MAC:
854 		attr.link_create.tracing.cookie = OPTS_GET(opts, tracing.cookie, 0);
855 		if (!OPTS_ZEROED(opts, tracing))
856 			return libbpf_err(-EINVAL);
857 		break;
858 	case BPF_NETFILTER:
859 		attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0);
860 		attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0);
861 		attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0);
862 		attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0);
863 		if (!OPTS_ZEROED(opts, netfilter))
864 			return libbpf_err(-EINVAL);
865 		break;
866 	case BPF_TCX_INGRESS:
867 	case BPF_TCX_EGRESS:
868 		relative_fd = OPTS_GET(opts, tcx.relative_fd, 0);
869 		relative_id = OPTS_GET(opts, tcx.relative_id, 0);
870 		if (relative_fd && relative_id)
871 			return libbpf_err(-EINVAL);
872 		if (relative_id) {
873 			attr.link_create.tcx.relative_id = relative_id;
874 			attr.link_create.flags |= BPF_F_ID;
875 		} else {
876 			attr.link_create.tcx.relative_fd = relative_fd;
877 		}
878 		attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0);
879 		if (!OPTS_ZEROED(opts, tcx))
880 			return libbpf_err(-EINVAL);
881 		break;
882 	case BPF_NETKIT_PRIMARY:
883 	case BPF_NETKIT_PEER:
884 		relative_fd = OPTS_GET(opts, netkit.relative_fd, 0);
885 		relative_id = OPTS_GET(opts, netkit.relative_id, 0);
886 		if (relative_fd && relative_id)
887 			return libbpf_err(-EINVAL);
888 		if (relative_id) {
889 			attr.link_create.netkit.relative_id = relative_id;
890 			attr.link_create.flags |= BPF_F_ID;
891 		} else {
892 			attr.link_create.netkit.relative_fd = relative_fd;
893 		}
894 		attr.link_create.netkit.expected_revision = OPTS_GET(opts, netkit.expected_revision, 0);
895 		if (!OPTS_ZEROED(opts, netkit))
896 			return libbpf_err(-EINVAL);
897 		break;
898 	case BPF_CGROUP_INET_INGRESS:
899 	case BPF_CGROUP_INET_EGRESS:
900 	case BPF_CGROUP_INET_SOCK_CREATE:
901 	case BPF_CGROUP_INET_SOCK_RELEASE:
902 	case BPF_CGROUP_INET4_BIND:
903 	case BPF_CGROUP_INET6_BIND:
904 	case BPF_CGROUP_INET4_POST_BIND:
905 	case BPF_CGROUP_INET6_POST_BIND:
906 	case BPF_CGROUP_INET4_CONNECT:
907 	case BPF_CGROUP_INET6_CONNECT:
908 	case BPF_CGROUP_UNIX_CONNECT:
909 	case BPF_CGROUP_INET4_GETPEERNAME:
910 	case BPF_CGROUP_INET6_GETPEERNAME:
911 	case BPF_CGROUP_UNIX_GETPEERNAME:
912 	case BPF_CGROUP_INET4_GETSOCKNAME:
913 	case BPF_CGROUP_INET6_GETSOCKNAME:
914 	case BPF_CGROUP_UNIX_GETSOCKNAME:
915 	case BPF_CGROUP_UDP4_SENDMSG:
916 	case BPF_CGROUP_UDP6_SENDMSG:
917 	case BPF_CGROUP_UNIX_SENDMSG:
918 	case BPF_CGROUP_UDP4_RECVMSG:
919 	case BPF_CGROUP_UDP6_RECVMSG:
920 	case BPF_CGROUP_UNIX_RECVMSG:
921 	case BPF_CGROUP_SOCK_OPS:
922 	case BPF_CGROUP_DEVICE:
923 	case BPF_CGROUP_SYSCTL:
924 	case BPF_CGROUP_GETSOCKOPT:
925 	case BPF_CGROUP_SETSOCKOPT:
926 	case BPF_LSM_CGROUP:
927 		relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0);
928 		relative_id = OPTS_GET(opts, cgroup.relative_id, 0);
929 		if (relative_fd && relative_id)
930 			return libbpf_err(-EINVAL);
931 		if (relative_id) {
932 			attr.link_create.cgroup.relative_id = relative_id;
933 			attr.link_create.flags |= BPF_F_ID;
934 		} else {
935 			attr.link_create.cgroup.relative_fd = relative_fd;
936 		}
937 		attr.link_create.cgroup.expected_revision =
938 			OPTS_GET(opts, cgroup.expected_revision, 0);
939 		if (!OPTS_ZEROED(opts, cgroup))
940 			return libbpf_err(-EINVAL);
941 		break;
942 	default:
943 		if (!OPTS_ZEROED(opts, flags))
944 			return libbpf_err(-EINVAL);
945 		break;
946 	}
947 proceed:
948 	fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
949 	if (fd >= 0)
950 		return fd;
951 	/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry
952 	 * and other similar programs
953 	 */
954 	err = -errno;
955 	if (err != -EINVAL)
956 		return libbpf_err(err);
957 
958 	/* if user used features not supported by
959 	 * BPF_RAW_TRACEPOINT_OPEN command, then just give up immediately
960 	 */
961 	if (attr.link_create.target_fd || attr.link_create.target_btf_id)
962 		return libbpf_err(err);
963 	if (!OPTS_ZEROED(opts, sz))
964 		return libbpf_err(err);
965 
966 	/* otherwise, for few select kinds of programs that can be
967 	 * attached using BPF_RAW_TRACEPOINT_OPEN command, try that as
968 	 * a fallback for older kernels
969 	 */
970 	switch (attach_type) {
971 	case BPF_TRACE_RAW_TP:
972 	case BPF_LSM_MAC:
973 	case BPF_TRACE_FENTRY:
974 	case BPF_TRACE_FEXIT:
975 	case BPF_MODIFY_RETURN:
976 		return bpf_raw_tracepoint_open(NULL, prog_fd);
977 	default:
978 		return libbpf_err(err);
979 	}
980 }
981 
982 int bpf_link_detach(int link_fd)
983 {
984 	const size_t attr_sz = offsetofend(union bpf_attr, link_detach);
985 	union bpf_attr attr;
986 	int ret;
987 
988 	memset(&attr, 0, attr_sz);
989 	attr.link_detach.link_fd = link_fd;
990 
991 	ret = sys_bpf(BPF_LINK_DETACH, &attr, attr_sz);
992 	return libbpf_err_errno(ret);
993 }
994 
995 int bpf_link_update(int link_fd, int new_prog_fd,
996 		    const struct bpf_link_update_opts *opts)
997 {
998 	const size_t attr_sz = offsetofend(union bpf_attr, link_update);
999 	union bpf_attr attr;
1000 	int ret;
1001 
1002 	if (!OPTS_VALID(opts, bpf_link_update_opts))
1003 		return libbpf_err(-EINVAL);
1004 
1005 	if (OPTS_GET(opts, old_prog_fd, 0) && OPTS_GET(opts, old_map_fd, 0))
1006 		return libbpf_err(-EINVAL);
1007 
1008 	memset(&attr, 0, attr_sz);
1009 	attr.link_update.link_fd = link_fd;
1010 	attr.link_update.new_prog_fd = new_prog_fd;
1011 	attr.link_update.flags = OPTS_GET(opts, flags, 0);
1012 	if (OPTS_GET(opts, old_prog_fd, 0))
1013 		attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);
1014 	else if (OPTS_GET(opts, old_map_fd, 0))
1015 		attr.link_update.old_map_fd = OPTS_GET(opts, old_map_fd, 0);
1016 
1017 	ret = sys_bpf(BPF_LINK_UPDATE, &attr, attr_sz);
1018 	return libbpf_err_errno(ret);
1019 }
1020 
1021 int bpf_iter_create(int link_fd)
1022 {
1023 	const size_t attr_sz = offsetofend(union bpf_attr, iter_create);
1024 	union bpf_attr attr;
1025 	int fd;
1026 
1027 	memset(&attr, 0, attr_sz);
1028 	attr.iter_create.link_fd = link_fd;
1029 
1030 	fd = sys_bpf_fd(BPF_ITER_CREATE, &attr, attr_sz);
1031 	return libbpf_err_errno(fd);
1032 }
1033 
1034 int bpf_prog_query_opts(int target, enum bpf_attach_type type,
1035 			struct bpf_prog_query_opts *opts)
1036 {
1037 	const size_t attr_sz = offsetofend(union bpf_attr, query);
1038 	union bpf_attr attr;
1039 	int ret;
1040 
1041 	if (!OPTS_VALID(opts, bpf_prog_query_opts))
1042 		return libbpf_err(-EINVAL);
1043 
1044 	memset(&attr, 0, attr_sz);
1045 	attr.query.target_fd		= target;
1046 	attr.query.attach_type		= type;
1047 	attr.query.query_flags		= OPTS_GET(opts, query_flags, 0);
1048 	attr.query.count		= OPTS_GET(opts, count, 0);
1049 	attr.query.prog_ids		= ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
1050 	attr.query.link_ids		= ptr_to_u64(OPTS_GET(opts, link_ids, NULL));
1051 	attr.query.prog_attach_flags	= ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
1052 	attr.query.link_attach_flags	= ptr_to_u64(OPTS_GET(opts, link_attach_flags, NULL));
1053 
1054 	ret = sys_bpf(BPF_PROG_QUERY, &attr, attr_sz);
1055 
1056 	OPTS_SET(opts, attach_flags, attr.query.attach_flags);
1057 	OPTS_SET(opts, revision, attr.query.revision);
1058 	OPTS_SET(opts, count, attr.query.count);
1059 
1060 	return libbpf_err_errno(ret);
1061 }
1062 
1063 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
1064 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
1065 {
1066 	LIBBPF_OPTS(bpf_prog_query_opts, opts);
1067 	int ret;
1068 
1069 	opts.query_flags = query_flags;
1070 	opts.prog_ids = prog_ids;
1071 	opts.prog_cnt = *prog_cnt;
1072 
1073 	ret = bpf_prog_query_opts(target_fd, type, &opts);
1074 
1075 	if (attach_flags)
1076 		*attach_flags = opts.attach_flags;
1077 	*prog_cnt = opts.prog_cnt;
1078 
1079 	return libbpf_err_errno(ret);
1080 }
1081 
1082 int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
1083 {
1084 	const size_t attr_sz = offsetofend(union bpf_attr, test);
1085 	union bpf_attr attr;
1086 	int ret;
1087 
1088 	if (!OPTS_VALID(opts, bpf_test_run_opts))
1089 		return libbpf_err(-EINVAL);
1090 
1091 	memset(&attr, 0, attr_sz);
1092 	attr.test.prog_fd = prog_fd;
1093 	attr.test.batch_size = OPTS_GET(opts, batch_size, 0);
1094 	attr.test.cpu = OPTS_GET(opts, cpu, 0);
1095 	attr.test.flags = OPTS_GET(opts, flags, 0);
1096 	attr.test.repeat = OPTS_GET(opts, repeat, 0);
1097 	attr.test.duration = OPTS_GET(opts, duration, 0);
1098 	attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
1099 	attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
1100 	attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
1101 	attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
1102 	attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
1103 	attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
1104 	attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
1105 	attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
1106 
1107 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, attr_sz);
1108 
1109 	OPTS_SET(opts, data_size_out, attr.test.data_size_out);
1110 	OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
1111 	OPTS_SET(opts, duration, attr.test.duration);
1112 	OPTS_SET(opts, retval, attr.test.retval);
1113 
1114 	return libbpf_err_errno(ret);
1115 }
1116 
1117 static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
1118 {
1119 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1120 	union bpf_attr attr;
1121 	int err;
1122 
1123 	memset(&attr, 0, attr_sz);
1124 	attr.start_id = start_id;
1125 
1126 	err = sys_bpf(cmd, &attr, attr_sz);
1127 	if (!err)
1128 		*next_id = attr.next_id;
1129 
1130 	return libbpf_err_errno(err);
1131 }
1132 
1133 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
1134 {
1135 	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
1136 }
1137 
1138 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
1139 {
1140 	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
1141 }
1142 
1143 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
1144 {
1145 	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
1146 }
1147 
1148 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
1149 {
1150 	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
1151 }
1152 
1153 int bpf_prog_get_fd_by_id_opts(__u32 id,
1154 			       const struct bpf_get_fd_by_id_opts *opts)
1155 {
1156 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1157 	union bpf_attr attr;
1158 	int fd;
1159 
1160 	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1161 		return libbpf_err(-EINVAL);
1162 
1163 	memset(&attr, 0, attr_sz);
1164 	attr.prog_id = id;
1165 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
1166 
1167 	fd = sys_bpf_fd(BPF_PROG_GET_FD_BY_ID, &attr, attr_sz);
1168 	return libbpf_err_errno(fd);
1169 }
1170 
1171 int bpf_prog_get_fd_by_id(__u32 id)
1172 {
1173 	return bpf_prog_get_fd_by_id_opts(id, NULL);
1174 }
1175 
1176 int bpf_map_get_fd_by_id_opts(__u32 id,
1177 			      const struct bpf_get_fd_by_id_opts *opts)
1178 {
1179 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1180 	union bpf_attr attr;
1181 	int fd;
1182 
1183 	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1184 		return libbpf_err(-EINVAL);
1185 
1186 	memset(&attr, 0, attr_sz);
1187 	attr.map_id = id;
1188 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
1189 
1190 	fd = sys_bpf_fd(BPF_MAP_GET_FD_BY_ID, &attr, attr_sz);
1191 	return libbpf_err_errno(fd);
1192 }
1193 
1194 int bpf_map_get_fd_by_id(__u32 id)
1195 {
1196 	return bpf_map_get_fd_by_id_opts(id, NULL);
1197 }
1198 
1199 int bpf_btf_get_fd_by_id_opts(__u32 id,
1200 			      const struct bpf_get_fd_by_id_opts *opts)
1201 {
1202 	const size_t attr_sz = offsetofend(union bpf_attr, fd_by_id_token_fd);
1203 	union bpf_attr attr;
1204 	int fd;
1205 
1206 	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1207 		return libbpf_err(-EINVAL);
1208 
1209 	memset(&attr, 0, attr_sz);
1210 	attr.btf_id = id;
1211 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
1212 	attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0);
1213 
1214 	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
1215 	return libbpf_err_errno(fd);
1216 }
1217 
1218 int bpf_btf_get_fd_by_id(__u32 id)
1219 {
1220 	return bpf_btf_get_fd_by_id_opts(id, NULL);
1221 }
1222 
1223 int bpf_link_get_fd_by_id_opts(__u32 id,
1224 			       const struct bpf_get_fd_by_id_opts *opts)
1225 {
1226 	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
1227 	union bpf_attr attr;
1228 	int fd;
1229 
1230 	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
1231 		return libbpf_err(-EINVAL);
1232 
1233 	memset(&attr, 0, attr_sz);
1234 	attr.link_id = id;
1235 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
1236 
1237 	fd = sys_bpf_fd(BPF_LINK_GET_FD_BY_ID, &attr, attr_sz);
1238 	return libbpf_err_errno(fd);
1239 }
1240 
1241 int bpf_link_get_fd_by_id(__u32 id)
1242 {
1243 	return bpf_link_get_fd_by_id_opts(id, NULL);
1244 }
1245 
1246 int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len)
1247 {
1248 	const size_t attr_sz = offsetofend(union bpf_attr, info);
1249 	union bpf_attr attr;
1250 	int err;
1251 
1252 	memset(&attr, 0, attr_sz);
1253 	attr.info.bpf_fd = bpf_fd;
1254 	attr.info.info_len = *info_len;
1255 	attr.info.info = ptr_to_u64(info);
1256 
1257 	err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, attr_sz);
1258 	if (!err)
1259 		*info_len = attr.info.info_len;
1260 	return libbpf_err_errno(err);
1261 }
1262 
1263 int bpf_prog_get_info_by_fd(int prog_fd, struct bpf_prog_info *info, __u32 *info_len)
1264 {
1265 	return bpf_obj_get_info_by_fd(prog_fd, info, info_len);
1266 }
1267 
1268 int bpf_map_get_info_by_fd(int map_fd, struct bpf_map_info *info, __u32 *info_len)
1269 {
1270 	return bpf_obj_get_info_by_fd(map_fd, info, info_len);
1271 }
1272 
1273 int bpf_btf_get_info_by_fd(int btf_fd, struct bpf_btf_info *info, __u32 *info_len)
1274 {
1275 	return bpf_obj_get_info_by_fd(btf_fd, info, info_len);
1276 }
1277 
1278 int bpf_link_get_info_by_fd(int link_fd, struct bpf_link_info *info, __u32 *info_len)
1279 {
1280 	return bpf_obj_get_info_by_fd(link_fd, info, info_len);
1281 }
1282 
1283 int bpf_raw_tracepoint_open_opts(int prog_fd, struct bpf_raw_tp_opts *opts)
1284 {
1285 	const size_t attr_sz = offsetofend(union bpf_attr, raw_tracepoint);
1286 	union bpf_attr attr;
1287 	int fd;
1288 
1289 	if (!OPTS_VALID(opts, bpf_raw_tp_opts))
1290 		return libbpf_err(-EINVAL);
1291 
1292 	memset(&attr, 0, attr_sz);
1293 	attr.raw_tracepoint.prog_fd = prog_fd;
1294 	attr.raw_tracepoint.name = ptr_to_u64(OPTS_GET(opts, tp_name, NULL));
1295 	attr.raw_tracepoint.cookie = OPTS_GET(opts, cookie, 0);
1296 
1297 	fd = sys_bpf_fd(BPF_RAW_TRACEPOINT_OPEN, &attr, attr_sz);
1298 	return libbpf_err_errno(fd);
1299 }
1300 
1301 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
1302 {
1303 	LIBBPF_OPTS(bpf_raw_tp_opts, opts, .tp_name = name);
1304 
1305 	return bpf_raw_tracepoint_open_opts(prog_fd, &opts);
1306 }
1307 
1308 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
1309 {
1310 	const size_t attr_sz = offsetofend(union bpf_attr, btf_token_fd);
1311 	union bpf_attr attr;
1312 	char *log_buf;
1313 	size_t log_size;
1314 	__u32 log_level;
1315 	int fd;
1316 
1317 	bump_rlimit_memlock();
1318 
1319 	memset(&attr, 0, attr_sz);
1320 
1321 	if (!OPTS_VALID(opts, bpf_btf_load_opts))
1322 		return libbpf_err(-EINVAL);
1323 
1324 	log_buf = OPTS_GET(opts, log_buf, NULL);
1325 	log_size = OPTS_GET(opts, log_size, 0);
1326 	log_level = OPTS_GET(opts, log_level, 0);
1327 
1328 	if (log_size > UINT_MAX)
1329 		return libbpf_err(-EINVAL);
1330 	if (log_size && !log_buf)
1331 		return libbpf_err(-EINVAL);
1332 
1333 	attr.btf = ptr_to_u64(btf_data);
1334 	attr.btf_size = btf_size;
1335 
1336 	attr.btf_flags = OPTS_GET(opts, btf_flags, 0);
1337 	attr.btf_token_fd = OPTS_GET(opts, token_fd, 0);
1338 
1339 	/* log_level == 0 and log_buf != NULL means "try loading without
1340 	 * log_buf, but retry with log_buf and log_level=1 on error", which is
1341 	 * consistent across low-level and high-level BTF and program loading
1342 	 * APIs within libbpf and provides a sensible behavior in practice
1343 	 */
1344 	if (log_level) {
1345 		attr.btf_log_buf = ptr_to_u64(log_buf);
1346 		attr.btf_log_size = (__u32)log_size;
1347 		attr.btf_log_level = log_level;
1348 	}
1349 
1350 	fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1351 	if (fd < 0 && log_buf && log_level == 0) {
1352 		attr.btf_log_buf = ptr_to_u64(log_buf);
1353 		attr.btf_log_size = (__u32)log_size;
1354 		attr.btf_log_level = 1;
1355 		fd = sys_bpf_fd(BPF_BTF_LOAD, &attr, attr_sz);
1356 	}
1357 
1358 	OPTS_SET(opts, log_true_size, attr.btf_log_true_size);
1359 	return libbpf_err_errno(fd);
1360 }
1361 
1362 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
1363 		      __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
1364 		      __u64 *probe_addr)
1365 {
1366 	const size_t attr_sz = offsetofend(union bpf_attr, task_fd_query);
1367 	union bpf_attr attr;
1368 	int err;
1369 
1370 	memset(&attr, 0, attr_sz);
1371 	attr.task_fd_query.pid = pid;
1372 	attr.task_fd_query.fd = fd;
1373 	attr.task_fd_query.flags = flags;
1374 	attr.task_fd_query.buf = ptr_to_u64(buf);
1375 	attr.task_fd_query.buf_len = *buf_len;
1376 
1377 	err = sys_bpf(BPF_TASK_FD_QUERY, &attr, attr_sz);
1378 
1379 	*buf_len = attr.task_fd_query.buf_len;
1380 	*prog_id = attr.task_fd_query.prog_id;
1381 	*fd_type = attr.task_fd_query.fd_type;
1382 	*probe_offset = attr.task_fd_query.probe_offset;
1383 	*probe_addr = attr.task_fd_query.probe_addr;
1384 
1385 	return libbpf_err_errno(err);
1386 }
1387 
1388 int bpf_enable_stats(enum bpf_stats_type type)
1389 {
1390 	const size_t attr_sz = offsetofend(union bpf_attr, enable_stats);
1391 	union bpf_attr attr;
1392 	int fd;
1393 
1394 	memset(&attr, 0, attr_sz);
1395 	attr.enable_stats.type = type;
1396 
1397 	fd = sys_bpf_fd(BPF_ENABLE_STATS, &attr, attr_sz);
1398 	return libbpf_err_errno(fd);
1399 }
1400 
1401 int bpf_prog_bind_map(int prog_fd, int map_fd,
1402 		      const struct bpf_prog_bind_opts *opts)
1403 {
1404 	const size_t attr_sz = offsetofend(union bpf_attr, prog_bind_map);
1405 	union bpf_attr attr;
1406 	int ret;
1407 
1408 	if (!OPTS_VALID(opts, bpf_prog_bind_opts))
1409 		return libbpf_err(-EINVAL);
1410 
1411 	memset(&attr, 0, attr_sz);
1412 	attr.prog_bind_map.prog_fd = prog_fd;
1413 	attr.prog_bind_map.map_fd = map_fd;
1414 	attr.prog_bind_map.flags = OPTS_GET(opts, flags, 0);
1415 
1416 	ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
1417 	return libbpf_err_errno(ret);
1418 }
1419 
1420 int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts)
1421 {
1422 	const size_t attr_sz = offsetofend(union bpf_attr, token_create);
1423 	union bpf_attr attr;
1424 	int fd;
1425 
1426 	if (!OPTS_VALID(opts, bpf_token_create_opts))
1427 		return libbpf_err(-EINVAL);
1428 
1429 	memset(&attr, 0, attr_sz);
1430 	attr.token_create.bpffs_fd = bpffs_fd;
1431 	attr.token_create.flags = OPTS_GET(opts, flags, 0);
1432 
1433 	fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz);
1434 	return libbpf_err_errno(fd);
1435 }
1436 
1437 int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
1438 			 struct bpf_prog_stream_read_opts *opts)
1439 {
1440 	const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read);
1441 	union bpf_attr attr;
1442 	int err;
1443 
1444 	if (!OPTS_VALID(opts, bpf_prog_stream_read_opts))
1445 		return libbpf_err(-EINVAL);
1446 
1447 	memset(&attr, 0, attr_sz);
1448 	attr.prog_stream_read.stream_buf = ptr_to_u64(buf);
1449 	attr.prog_stream_read.stream_buf_len = buf_len;
1450 	attr.prog_stream_read.stream_id = stream_id;
1451 	attr.prog_stream_read.prog_fd = prog_fd;
1452 
1453 	err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz);
1454 	return libbpf_err_errno(err);
1455 }
1456 
1457 int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd,
1458 			      struct bpf_prog_assoc_struct_ops_opts *opts)
1459 {
1460 	const size_t attr_sz = offsetofend(union bpf_attr, prog_assoc_struct_ops);
1461 	union bpf_attr attr;
1462 	int err;
1463 
1464 	if (!OPTS_VALID(opts, bpf_prog_assoc_struct_ops_opts))
1465 		return libbpf_err(-EINVAL);
1466 
1467 	memset(&attr, 0, attr_sz);
1468 	attr.prog_assoc_struct_ops.map_fd = map_fd;
1469 	attr.prog_assoc_struct_ops.prog_fd = prog_fd;
1470 	attr.prog_assoc_struct_ops.flags = OPTS_GET(opts, flags, 0);
1471 
1472 	err = sys_bpf(BPF_PROG_ASSOC_STRUCT_OPS, &attr, attr_sz);
1473 	return libbpf_err_errno(err);
1474 }
1475