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