xref: /linux/tools/lib/bpf/bpf.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
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 <linux/bpf.h>
30 #include "bpf.h"
31 #include "libbpf.h"
32 #include <errno.h>
33 
34 /*
35  * When building perf, unistd.h is overridden. __NR_bpf is
36  * required to be defined explicitly.
37  */
38 #ifndef __NR_bpf
39 # if defined(__i386__)
40 #  define __NR_bpf 357
41 # elif defined(__x86_64__)
42 #  define __NR_bpf 321
43 # elif defined(__aarch64__)
44 #  define __NR_bpf 280
45 # elif defined(__sparc__)
46 #  define __NR_bpf 349
47 # elif defined(__s390__)
48 #  define __NR_bpf 351
49 # elif defined(__arc__)
50 #  define __NR_bpf 280
51 # else
52 #  error __NR_bpf not defined. libbpf does not support your arch.
53 # endif
54 #endif
55 
56 #ifndef min
57 #define min(x, y) ((x) < (y) ? (x) : (y))
58 #endif
59 
60 static inline __u64 ptr_to_u64(const void *ptr)
61 {
62 	return (__u64) (unsigned long) ptr;
63 }
64 
65 static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
66 			  unsigned int size)
67 {
68 	return syscall(__NR_bpf, cmd, attr, size);
69 }
70 
71 static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size)
72 {
73 	int fd;
74 
75 	do {
76 		fd = sys_bpf(BPF_PROG_LOAD, attr, size);
77 	} while (fd < 0 && errno == EAGAIN);
78 
79 	return fd;
80 }
81 
82 int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
83 {
84 	union bpf_attr attr;
85 
86 	memset(&attr, '\0', sizeof(attr));
87 
88 	attr.map_type = create_attr->map_type;
89 	attr.key_size = create_attr->key_size;
90 	attr.value_size = create_attr->value_size;
91 	attr.max_entries = create_attr->max_entries;
92 	attr.map_flags = create_attr->map_flags;
93 	if (create_attr->name)
94 		memcpy(attr.map_name, create_attr->name,
95 		       min(strlen(create_attr->name), BPF_OBJ_NAME_LEN - 1));
96 	attr.numa_node = create_attr->numa_node;
97 	attr.btf_fd = create_attr->btf_fd;
98 	attr.btf_key_type_id = create_attr->btf_key_type_id;
99 	attr.btf_value_type_id = create_attr->btf_value_type_id;
100 	attr.map_ifindex = create_attr->map_ifindex;
101 	attr.inner_map_fd = create_attr->inner_map_fd;
102 
103 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
104 }
105 
106 int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
107 			int key_size, int value_size, int max_entries,
108 			__u32 map_flags, int node)
109 {
110 	struct bpf_create_map_attr map_attr = {};
111 
112 	map_attr.name = name;
113 	map_attr.map_type = map_type;
114 	map_attr.map_flags = map_flags;
115 	map_attr.key_size = key_size;
116 	map_attr.value_size = value_size;
117 	map_attr.max_entries = max_entries;
118 	if (node >= 0) {
119 		map_attr.numa_node = node;
120 		map_attr.map_flags |= BPF_F_NUMA_NODE;
121 	}
122 
123 	return bpf_create_map_xattr(&map_attr);
124 }
125 
126 int bpf_create_map(enum bpf_map_type map_type, int key_size,
127 		   int value_size, int max_entries, __u32 map_flags)
128 {
129 	struct bpf_create_map_attr map_attr = {};
130 
131 	map_attr.map_type = map_type;
132 	map_attr.map_flags = map_flags;
133 	map_attr.key_size = key_size;
134 	map_attr.value_size = value_size;
135 	map_attr.max_entries = max_entries;
136 
137 	return bpf_create_map_xattr(&map_attr);
138 }
139 
140 int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
141 			int key_size, int value_size, int max_entries,
142 			__u32 map_flags)
143 {
144 	struct bpf_create_map_attr map_attr = {};
145 
146 	map_attr.name = name;
147 	map_attr.map_type = map_type;
148 	map_attr.map_flags = map_flags;
149 	map_attr.key_size = key_size;
150 	map_attr.value_size = value_size;
151 	map_attr.max_entries = max_entries;
152 
153 	return bpf_create_map_xattr(&map_attr);
154 }
155 
156 int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
157 			       int key_size, int inner_map_fd, int max_entries,
158 			       __u32 map_flags, int node)
159 {
160 	union bpf_attr attr;
161 
162 	memset(&attr, '\0', sizeof(attr));
163 
164 	attr.map_type = map_type;
165 	attr.key_size = key_size;
166 	attr.value_size = 4;
167 	attr.inner_map_fd = inner_map_fd;
168 	attr.max_entries = max_entries;
169 	attr.map_flags = map_flags;
170 	if (name)
171 		memcpy(attr.map_name, name,
172 		       min(strlen(name), BPF_OBJ_NAME_LEN - 1));
173 
174 	if (node >= 0) {
175 		attr.map_flags |= BPF_F_NUMA_NODE;
176 		attr.numa_node = node;
177 	}
178 
179 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
180 }
181 
182 int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
183 			  int key_size, int inner_map_fd, int max_entries,
184 			  __u32 map_flags)
185 {
186 	return bpf_create_map_in_map_node(map_type, name, key_size,
187 					  inner_map_fd, max_entries, map_flags,
188 					  -1);
189 }
190 
191 static void *
192 alloc_zero_tailing_info(const void *orecord, __u32 cnt,
193 			__u32 actual_rec_size, __u32 expected_rec_size)
194 {
195 	__u64 info_len = actual_rec_size * cnt;
196 	void *info, *nrecord;
197 	int i;
198 
199 	info = malloc(info_len);
200 	if (!info)
201 		return NULL;
202 
203 	/* zero out bytes kernel does not understand */
204 	nrecord = info;
205 	for (i = 0; i < cnt; i++) {
206 		memcpy(nrecord, orecord, expected_rec_size);
207 		memset(nrecord + expected_rec_size, 0,
208 		       actual_rec_size - expected_rec_size);
209 		orecord += actual_rec_size;
210 		nrecord += actual_rec_size;
211 	}
212 
213 	return info;
214 }
215 
216 int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
217 			   char *log_buf, size_t log_buf_sz)
218 {
219 	void *finfo = NULL, *linfo = NULL;
220 	union bpf_attr attr;
221 	__u32 log_level;
222 	int fd;
223 
224 	if (!load_attr || !log_buf != !log_buf_sz)
225 		return -EINVAL;
226 
227 	log_level = load_attr->log_level;
228 	if (log_level > (4 | 2 | 1) || (log_level && !log_buf))
229 		return -EINVAL;
230 
231 	memset(&attr, 0, sizeof(attr));
232 	attr.prog_type = load_attr->prog_type;
233 	attr.expected_attach_type = load_attr->expected_attach_type;
234 	attr.insn_cnt = (__u32)load_attr->insns_cnt;
235 	attr.insns = ptr_to_u64(load_attr->insns);
236 	attr.license = ptr_to_u64(load_attr->license);
237 
238 	attr.log_level = log_level;
239 	if (log_level) {
240 		attr.log_buf = ptr_to_u64(log_buf);
241 		attr.log_size = log_buf_sz;
242 	} else {
243 		attr.log_buf = ptr_to_u64(NULL);
244 		attr.log_size = 0;
245 	}
246 
247 	attr.kern_version = load_attr->kern_version;
248 	attr.prog_ifindex = load_attr->prog_ifindex;
249 	attr.prog_btf_fd = load_attr->prog_btf_fd;
250 	attr.func_info_rec_size = load_attr->func_info_rec_size;
251 	attr.func_info_cnt = load_attr->func_info_cnt;
252 	attr.func_info = ptr_to_u64(load_attr->func_info);
253 	attr.line_info_rec_size = load_attr->line_info_rec_size;
254 	attr.line_info_cnt = load_attr->line_info_cnt;
255 	attr.line_info = ptr_to_u64(load_attr->line_info);
256 	if (load_attr->name)
257 		memcpy(attr.prog_name, load_attr->name,
258 		       min(strlen(load_attr->name), BPF_OBJ_NAME_LEN - 1));
259 
260 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
261 	if (fd >= 0)
262 		return fd;
263 
264 	/* After bpf_prog_load, the kernel may modify certain attributes
265 	 * to give user space a hint how to deal with loading failure.
266 	 * Check to see whether we can make some changes and load again.
267 	 */
268 	while (errno == E2BIG && (!finfo || !linfo)) {
269 		if (!finfo && attr.func_info_cnt &&
270 		    attr.func_info_rec_size < load_attr->func_info_rec_size) {
271 			/* try with corrected func info records */
272 			finfo = alloc_zero_tailing_info(load_attr->func_info,
273 							load_attr->func_info_cnt,
274 							load_attr->func_info_rec_size,
275 							attr.func_info_rec_size);
276 			if (!finfo)
277 				goto done;
278 
279 			attr.func_info = ptr_to_u64(finfo);
280 			attr.func_info_rec_size = load_attr->func_info_rec_size;
281 		} else if (!linfo && attr.line_info_cnt &&
282 			   attr.line_info_rec_size <
283 			   load_attr->line_info_rec_size) {
284 			linfo = alloc_zero_tailing_info(load_attr->line_info,
285 							load_attr->line_info_cnt,
286 							load_attr->line_info_rec_size,
287 							attr.line_info_rec_size);
288 			if (!linfo)
289 				goto done;
290 
291 			attr.line_info = ptr_to_u64(linfo);
292 			attr.line_info_rec_size = load_attr->line_info_rec_size;
293 		} else {
294 			break;
295 		}
296 
297 		fd = sys_bpf_prog_load(&attr, sizeof(attr));
298 
299 		if (fd >= 0)
300 			goto done;
301 	}
302 
303 	if (log_level || !log_buf)
304 		goto done;
305 
306 	/* Try again with log */
307 	attr.log_buf = ptr_to_u64(log_buf);
308 	attr.log_size = log_buf_sz;
309 	attr.log_level = 1;
310 	log_buf[0] = 0;
311 	fd = sys_bpf_prog_load(&attr, sizeof(attr));
312 done:
313 	free(finfo);
314 	free(linfo);
315 	return fd;
316 }
317 
318 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
319 		     size_t insns_cnt, const char *license,
320 		     __u32 kern_version, char *log_buf,
321 		     size_t log_buf_sz)
322 {
323 	struct bpf_load_program_attr load_attr;
324 
325 	memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
326 	load_attr.prog_type = type;
327 	load_attr.expected_attach_type = 0;
328 	load_attr.name = NULL;
329 	load_attr.insns = insns;
330 	load_attr.insns_cnt = insns_cnt;
331 	load_attr.license = license;
332 	load_attr.kern_version = kern_version;
333 
334 	return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz);
335 }
336 
337 int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
338 		       size_t insns_cnt, __u32 prog_flags, const char *license,
339 		       __u32 kern_version, char *log_buf, size_t log_buf_sz,
340 		       int log_level)
341 {
342 	union bpf_attr attr;
343 
344 	memset(&attr, 0, sizeof(attr));
345 	attr.prog_type = type;
346 	attr.insn_cnt = (__u32)insns_cnt;
347 	attr.insns = ptr_to_u64(insns);
348 	attr.license = ptr_to_u64(license);
349 	attr.log_buf = ptr_to_u64(log_buf);
350 	attr.log_size = log_buf_sz;
351 	attr.log_level = log_level;
352 	log_buf[0] = 0;
353 	attr.kern_version = kern_version;
354 	attr.prog_flags = prog_flags;
355 
356 	return sys_bpf_prog_load(&attr, sizeof(attr));
357 }
358 
359 int bpf_map_update_elem(int fd, const void *key, const void *value,
360 			__u64 flags)
361 {
362 	union bpf_attr attr;
363 
364 	memset(&attr, 0, sizeof(attr));
365 	attr.map_fd = fd;
366 	attr.key = ptr_to_u64(key);
367 	attr.value = ptr_to_u64(value);
368 	attr.flags = flags;
369 
370 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
371 }
372 
373 int bpf_map_lookup_elem(int fd, const void *key, void *value)
374 {
375 	union bpf_attr attr;
376 
377 	memset(&attr, 0, sizeof(attr));
378 	attr.map_fd = fd;
379 	attr.key = ptr_to_u64(key);
380 	attr.value = ptr_to_u64(value);
381 
382 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
383 }
384 
385 int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags)
386 {
387 	union bpf_attr attr;
388 
389 	memset(&attr, 0, sizeof(attr));
390 	attr.map_fd = fd;
391 	attr.key = ptr_to_u64(key);
392 	attr.value = ptr_to_u64(value);
393 	attr.flags = flags;
394 
395 	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
396 }
397 
398 int bpf_map_lookup_and_delete_elem(int fd, const void *key, void *value)
399 {
400 	union bpf_attr attr;
401 
402 	memset(&attr, 0, sizeof(attr));
403 	attr.map_fd = fd;
404 	attr.key = ptr_to_u64(key);
405 	attr.value = ptr_to_u64(value);
406 
407 	return sys_bpf(BPF_MAP_LOOKUP_AND_DELETE_ELEM, &attr, sizeof(attr));
408 }
409 
410 int bpf_map_delete_elem(int fd, const void *key)
411 {
412 	union bpf_attr attr;
413 
414 	memset(&attr, 0, sizeof(attr));
415 	attr.map_fd = fd;
416 	attr.key = ptr_to_u64(key);
417 
418 	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
419 }
420 
421 int bpf_map_get_next_key(int fd, const void *key, void *next_key)
422 {
423 	union bpf_attr attr;
424 
425 	memset(&attr, 0, sizeof(attr));
426 	attr.map_fd = fd;
427 	attr.key = ptr_to_u64(key);
428 	attr.next_key = ptr_to_u64(next_key);
429 
430 	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
431 }
432 
433 int bpf_map_freeze(int fd)
434 {
435 	union bpf_attr attr;
436 
437 	memset(&attr, 0, sizeof(attr));
438 	attr.map_fd = fd;
439 
440 	return sys_bpf(BPF_MAP_FREEZE, &attr, sizeof(attr));
441 }
442 
443 int bpf_obj_pin(int fd, const char *pathname)
444 {
445 	union bpf_attr attr;
446 
447 	memset(&attr, 0, sizeof(attr));
448 	attr.pathname = ptr_to_u64((void *)pathname);
449 	attr.bpf_fd = fd;
450 
451 	return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
452 }
453 
454 int bpf_obj_get(const char *pathname)
455 {
456 	union bpf_attr attr;
457 
458 	memset(&attr, 0, sizeof(attr));
459 	attr.pathname = ptr_to_u64((void *)pathname);
460 
461 	return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
462 }
463 
464 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
465 		    unsigned int flags)
466 {
467 	union bpf_attr attr;
468 
469 	memset(&attr, 0, sizeof(attr));
470 	attr.target_fd	   = target_fd;
471 	attr.attach_bpf_fd = prog_fd;
472 	attr.attach_type   = type;
473 	attr.attach_flags  = flags;
474 
475 	return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
476 }
477 
478 int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
479 {
480 	union bpf_attr attr;
481 
482 	memset(&attr, 0, sizeof(attr));
483 	attr.target_fd	 = target_fd;
484 	attr.attach_type = type;
485 
486 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
487 }
488 
489 int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
490 {
491 	union bpf_attr attr;
492 
493 	memset(&attr, 0, sizeof(attr));
494 	attr.target_fd	 = target_fd;
495 	attr.attach_bpf_fd = prog_fd;
496 	attr.attach_type = type;
497 
498 	return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
499 }
500 
501 int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags,
502 		   __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt)
503 {
504 	union bpf_attr attr;
505 	int ret;
506 
507 	memset(&attr, 0, sizeof(attr));
508 	attr.query.target_fd	= target_fd;
509 	attr.query.attach_type	= type;
510 	attr.query.query_flags	= query_flags;
511 	attr.query.prog_cnt	= *prog_cnt;
512 	attr.query.prog_ids	= ptr_to_u64(prog_ids);
513 
514 	ret = sys_bpf(BPF_PROG_QUERY, &attr, sizeof(attr));
515 	if (attach_flags)
516 		*attach_flags = attr.query.attach_flags;
517 	*prog_cnt = attr.query.prog_cnt;
518 	return ret;
519 }
520 
521 int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
522 		      void *data_out, __u32 *size_out, __u32 *retval,
523 		      __u32 *duration)
524 {
525 	union bpf_attr attr;
526 	int ret;
527 
528 	memset(&attr, 0, sizeof(attr));
529 	attr.test.prog_fd = prog_fd;
530 	attr.test.data_in = ptr_to_u64(data);
531 	attr.test.data_out = ptr_to_u64(data_out);
532 	attr.test.data_size_in = size;
533 	attr.test.repeat = repeat;
534 
535 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
536 	if (size_out)
537 		*size_out = attr.test.data_size_out;
538 	if (retval)
539 		*retval = attr.test.retval;
540 	if (duration)
541 		*duration = attr.test.duration;
542 	return ret;
543 }
544 
545 int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
546 {
547 	union bpf_attr attr;
548 	int ret;
549 
550 	if (!test_attr->data_out && test_attr->data_size_out > 0)
551 		return -EINVAL;
552 
553 	memset(&attr, 0, sizeof(attr));
554 	attr.test.prog_fd = test_attr->prog_fd;
555 	attr.test.data_in = ptr_to_u64(test_attr->data_in);
556 	attr.test.data_out = ptr_to_u64(test_attr->data_out);
557 	attr.test.data_size_in = test_attr->data_size_in;
558 	attr.test.data_size_out = test_attr->data_size_out;
559 	attr.test.ctx_in = ptr_to_u64(test_attr->ctx_in);
560 	attr.test.ctx_out = ptr_to_u64(test_attr->ctx_out);
561 	attr.test.ctx_size_in = test_attr->ctx_size_in;
562 	attr.test.ctx_size_out = test_attr->ctx_size_out;
563 	attr.test.repeat = test_attr->repeat;
564 
565 	ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
566 	test_attr->data_size_out = attr.test.data_size_out;
567 	test_attr->ctx_size_out = attr.test.ctx_size_out;
568 	test_attr->retval = attr.test.retval;
569 	test_attr->duration = attr.test.duration;
570 	return ret;
571 }
572 
573 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
574 {
575 	union bpf_attr attr;
576 	int err;
577 
578 	memset(&attr, 0, sizeof(attr));
579 	attr.start_id = start_id;
580 
581 	err = sys_bpf(BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
582 	if (!err)
583 		*next_id = attr.next_id;
584 
585 	return err;
586 }
587 
588 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
589 {
590 	union bpf_attr attr;
591 	int err;
592 
593 	memset(&attr, 0, sizeof(attr));
594 	attr.start_id = start_id;
595 
596 	err = sys_bpf(BPF_MAP_GET_NEXT_ID, &attr, sizeof(attr));
597 	if (!err)
598 		*next_id = attr.next_id;
599 
600 	return err;
601 }
602 
603 int bpf_prog_get_fd_by_id(__u32 id)
604 {
605 	union bpf_attr attr;
606 
607 	memset(&attr, 0, sizeof(attr));
608 	attr.prog_id = id;
609 
610 	return sys_bpf(BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
611 }
612 
613 int bpf_map_get_fd_by_id(__u32 id)
614 {
615 	union bpf_attr attr;
616 
617 	memset(&attr, 0, sizeof(attr));
618 	attr.map_id = id;
619 
620 	return sys_bpf(BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
621 }
622 
623 int bpf_btf_get_fd_by_id(__u32 id)
624 {
625 	union bpf_attr attr;
626 
627 	memset(&attr, 0, sizeof(attr));
628 	attr.btf_id = id;
629 
630 	return sys_bpf(BPF_BTF_GET_FD_BY_ID, &attr, sizeof(attr));
631 }
632 
633 int bpf_obj_get_info_by_fd(int prog_fd, void *info, __u32 *info_len)
634 {
635 	union bpf_attr attr;
636 	int err;
637 
638 	memset(&attr, 0, sizeof(attr));
639 	attr.info.bpf_fd = prog_fd;
640 	attr.info.info_len = *info_len;
641 	attr.info.info = ptr_to_u64(info);
642 
643 	err = sys_bpf(BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
644 	if (!err)
645 		*info_len = attr.info.info_len;
646 
647 	return err;
648 }
649 
650 int bpf_raw_tracepoint_open(const char *name, int prog_fd)
651 {
652 	union bpf_attr attr;
653 
654 	memset(&attr, 0, sizeof(attr));
655 	attr.raw_tracepoint.name = ptr_to_u64(name);
656 	attr.raw_tracepoint.prog_fd = prog_fd;
657 
658 	return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr));
659 }
660 
661 int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size,
662 		 bool do_log)
663 {
664 	union bpf_attr attr = {};
665 	int fd;
666 
667 	attr.btf = ptr_to_u64(btf);
668 	attr.btf_size = btf_size;
669 
670 retry:
671 	if (do_log && log_buf && log_buf_size) {
672 		attr.btf_log_level = 1;
673 		attr.btf_log_size = log_buf_size;
674 		attr.btf_log_buf = ptr_to_u64(log_buf);
675 	}
676 
677 	fd = sys_bpf(BPF_BTF_LOAD, &attr, sizeof(attr));
678 	if (fd == -1 && !do_log && log_buf && log_buf_size) {
679 		do_log = true;
680 		goto retry;
681 	}
682 
683 	return fd;
684 }
685 
686 int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, __u32 *buf_len,
687 		      __u32 *prog_id, __u32 *fd_type, __u64 *probe_offset,
688 		      __u64 *probe_addr)
689 {
690 	union bpf_attr attr = {};
691 	int err;
692 
693 	attr.task_fd_query.pid = pid;
694 	attr.task_fd_query.fd = fd;
695 	attr.task_fd_query.flags = flags;
696 	attr.task_fd_query.buf = ptr_to_u64(buf);
697 	attr.task_fd_query.buf_len = *buf_len;
698 
699 	err = sys_bpf(BPF_TASK_FD_QUERY, &attr, sizeof(attr));
700 	*buf_len = attr.task_fd_query.buf_len;
701 	*prog_id = attr.task_fd_query.prog_id;
702 	*fd_type = attr.task_fd_query.fd_type;
703 	*probe_offset = attr.task_fd_query.probe_offset;
704 	*probe_addr = attr.task_fd_query.probe_addr;
705 
706 	return err;
707 }
708