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