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