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