xref: /openbmc/linux/tools/bpf/bpftool/prog.c (revision 6bc209db)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #ifndef _GNU_SOURCE
5 #define _GNU_SOURCE
6 #endif
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <net/if.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/syscall.h>
21 #include <dirent.h>
22 
23 #include <linux/err.h>
24 #include <linux/perf_event.h>
25 #include <linux/sizes.h>
26 
27 #include <bpf/bpf.h>
28 #include <bpf/btf.h>
29 #include <bpf/hashmap.h>
30 #include <bpf/libbpf.h>
31 #include <bpf/libbpf_internal.h>
32 #include <bpf/skel_internal.h>
33 
34 #include "cfg.h"
35 #include "main.h"
36 #include "xlated_dumper.h"
37 
38 #define BPF_METADATA_PREFIX "bpf_metadata_"
39 #define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1)
40 
41 enum dump_mode {
42 	DUMP_JITED,
43 	DUMP_XLATED,
44 };
45 
46 static const bool attach_types[] = {
47 	[BPF_SK_SKB_STREAM_PARSER] = true,
48 	[BPF_SK_SKB_STREAM_VERDICT] = true,
49 	[BPF_SK_SKB_VERDICT] = true,
50 	[BPF_SK_MSG_VERDICT] = true,
51 	[BPF_FLOW_DISSECTOR] = true,
52 	[__MAX_BPF_ATTACH_TYPE] = false,
53 };
54 
55 /* Textual representations traditionally used by the program and kept around
56  * for the sake of backwards compatibility.
57  */
58 static const char * const attach_type_strings[] = {
59 	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
60 	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
61 	[BPF_SK_SKB_VERDICT] = "skb_verdict",
62 	[BPF_SK_MSG_VERDICT] = "msg_verdict",
63 	[__MAX_BPF_ATTACH_TYPE] = NULL,
64 };
65 
66 static struct hashmap *prog_table;
67 
parse_attach_type(const char * str)68 static enum bpf_attach_type parse_attach_type(const char *str)
69 {
70 	enum bpf_attach_type type;
71 
72 	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
73 		if (attach_types[type]) {
74 			const char *attach_type_str;
75 
76 			attach_type_str = libbpf_bpf_attach_type_str(type);
77 			if (!strcmp(str, attach_type_str))
78 				return type;
79 		}
80 
81 		if (attach_type_strings[type] &&
82 		    is_prefix(str, attach_type_strings[type]))
83 			return type;
84 	}
85 
86 	return __MAX_BPF_ATTACH_TYPE;
87 }
88 
prep_prog_info(struct bpf_prog_info * const info,enum dump_mode mode,void ** info_data,size_t * const info_data_sz)89 static int prep_prog_info(struct bpf_prog_info *const info, enum dump_mode mode,
90 			  void **info_data, size_t *const info_data_sz)
91 {
92 	struct bpf_prog_info holder = {};
93 	size_t needed = 0;
94 	void *ptr;
95 
96 	if (mode == DUMP_JITED) {
97 		holder.jited_prog_len = info->jited_prog_len;
98 		needed += info->jited_prog_len;
99 	} else {
100 		holder.xlated_prog_len = info->xlated_prog_len;
101 		needed += info->xlated_prog_len;
102 	}
103 
104 	holder.nr_jited_ksyms = info->nr_jited_ksyms;
105 	needed += info->nr_jited_ksyms * sizeof(__u64);
106 
107 	holder.nr_jited_func_lens = info->nr_jited_func_lens;
108 	needed += info->nr_jited_func_lens * sizeof(__u32);
109 
110 	holder.nr_func_info = info->nr_func_info;
111 	holder.func_info_rec_size = info->func_info_rec_size;
112 	needed += info->nr_func_info * info->func_info_rec_size;
113 
114 	holder.nr_line_info = info->nr_line_info;
115 	holder.line_info_rec_size = info->line_info_rec_size;
116 	needed += info->nr_line_info * info->line_info_rec_size;
117 
118 	holder.nr_jited_line_info = info->nr_jited_line_info;
119 	holder.jited_line_info_rec_size = info->jited_line_info_rec_size;
120 	needed += info->nr_jited_line_info * info->jited_line_info_rec_size;
121 
122 	if (needed > *info_data_sz) {
123 		ptr = realloc(*info_data, needed);
124 		if (!ptr)
125 			return -1;
126 
127 		*info_data = ptr;
128 		*info_data_sz = needed;
129 	}
130 	ptr = *info_data;
131 
132 	if (mode == DUMP_JITED) {
133 		holder.jited_prog_insns = ptr_to_u64(ptr);
134 		ptr += holder.jited_prog_len;
135 	} else {
136 		holder.xlated_prog_insns = ptr_to_u64(ptr);
137 		ptr += holder.xlated_prog_len;
138 	}
139 
140 	holder.jited_ksyms = ptr_to_u64(ptr);
141 	ptr += holder.nr_jited_ksyms * sizeof(__u64);
142 
143 	holder.jited_func_lens = ptr_to_u64(ptr);
144 	ptr += holder.nr_jited_func_lens * sizeof(__u32);
145 
146 	holder.func_info = ptr_to_u64(ptr);
147 	ptr += holder.nr_func_info * holder.func_info_rec_size;
148 
149 	holder.line_info = ptr_to_u64(ptr);
150 	ptr += holder.nr_line_info * holder.line_info_rec_size;
151 
152 	holder.jited_line_info = ptr_to_u64(ptr);
153 	ptr += holder.nr_jited_line_info * holder.jited_line_info_rec_size;
154 
155 	*info = holder;
156 	return 0;
157 }
158 
print_boot_time(__u64 nsecs,char * buf,unsigned int size)159 static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
160 {
161 	struct timespec real_time_ts, boot_time_ts;
162 	time_t wallclock_secs;
163 	struct tm load_tm;
164 
165 	buf[--size] = '\0';
166 
167 	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
168 	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
169 		perror("Can't read clocks");
170 		snprintf(buf, size, "%llu", nsecs / 1000000000);
171 		return;
172 	}
173 
174 	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
175 		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
176 		1000000000;
177 
178 
179 	if (!localtime_r(&wallclock_secs, &load_tm)) {
180 		snprintf(buf, size, "%llu", nsecs / 1000000000);
181 		return;
182 	}
183 
184 	if (json_output)
185 		strftime(buf, size, "%s", &load_tm);
186 	else
187 		strftime(buf, size, "%FT%T%z", &load_tm);
188 }
189 
show_prog_maps(int fd,__u32 num_maps)190 static void show_prog_maps(int fd, __u32 num_maps)
191 {
192 	struct bpf_prog_info info = {};
193 	__u32 len = sizeof(info);
194 	__u32 map_ids[num_maps];
195 	unsigned int i;
196 	int err;
197 
198 	info.nr_map_ids = num_maps;
199 	info.map_ids = ptr_to_u64(map_ids);
200 
201 	err = bpf_prog_get_info_by_fd(fd, &info, &len);
202 	if (err || !info.nr_map_ids)
203 		return;
204 
205 	if (json_output) {
206 		jsonw_name(json_wtr, "map_ids");
207 		jsonw_start_array(json_wtr);
208 		for (i = 0; i < info.nr_map_ids; i++)
209 			jsonw_uint(json_wtr, map_ids[i]);
210 		jsonw_end_array(json_wtr);
211 	} else {
212 		printf("  map_ids ");
213 		for (i = 0; i < info.nr_map_ids; i++)
214 			printf("%u%s", map_ids[i],
215 			       i == info.nr_map_ids - 1 ? "" : ",");
216 	}
217 }
218 
find_metadata(int prog_fd,struct bpf_map_info * map_info)219 static void *find_metadata(int prog_fd, struct bpf_map_info *map_info)
220 {
221 	struct bpf_prog_info prog_info;
222 	__u32 prog_info_len;
223 	__u32 map_info_len;
224 	void *value = NULL;
225 	__u32 *map_ids;
226 	int nr_maps;
227 	int key = 0;
228 	int map_fd;
229 	int ret;
230 	__u32 i;
231 
232 	memset(&prog_info, 0, sizeof(prog_info));
233 	prog_info_len = sizeof(prog_info);
234 	ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
235 	if (ret)
236 		return NULL;
237 
238 	if (!prog_info.nr_map_ids)
239 		return NULL;
240 
241 	map_ids = calloc(prog_info.nr_map_ids, sizeof(__u32));
242 	if (!map_ids)
243 		return NULL;
244 
245 	nr_maps = prog_info.nr_map_ids;
246 	memset(&prog_info, 0, sizeof(prog_info));
247 	prog_info.nr_map_ids = nr_maps;
248 	prog_info.map_ids = ptr_to_u64(map_ids);
249 	prog_info_len = sizeof(prog_info);
250 
251 	ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
252 	if (ret)
253 		goto free_map_ids;
254 
255 	for (i = 0; i < prog_info.nr_map_ids; i++) {
256 		map_fd = bpf_map_get_fd_by_id(map_ids[i]);
257 		if (map_fd < 0)
258 			goto free_map_ids;
259 
260 		memset(map_info, 0, sizeof(*map_info));
261 		map_info_len = sizeof(*map_info);
262 		ret = bpf_map_get_info_by_fd(map_fd, map_info, &map_info_len);
263 		if (ret < 0) {
264 			close(map_fd);
265 			goto free_map_ids;
266 		}
267 
268 		if (map_info->type != BPF_MAP_TYPE_ARRAY ||
269 		    map_info->key_size != sizeof(int) ||
270 		    map_info->max_entries != 1 ||
271 		    !map_info->btf_value_type_id ||
272 		    !strstr(map_info->name, ".rodata")) {
273 			close(map_fd);
274 			continue;
275 		}
276 
277 		value = malloc(map_info->value_size);
278 		if (!value) {
279 			close(map_fd);
280 			goto free_map_ids;
281 		}
282 
283 		if (bpf_map_lookup_elem(map_fd, &key, value)) {
284 			close(map_fd);
285 			free(value);
286 			value = NULL;
287 			goto free_map_ids;
288 		}
289 
290 		close(map_fd);
291 		break;
292 	}
293 
294 free_map_ids:
295 	free(map_ids);
296 	return value;
297 }
298 
has_metadata_prefix(const char * s)299 static bool has_metadata_prefix(const char *s)
300 {
301 	return strncmp(s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) == 0;
302 }
303 
show_prog_metadata(int fd,__u32 num_maps)304 static void show_prog_metadata(int fd, __u32 num_maps)
305 {
306 	const struct btf_type *t_datasec, *t_var;
307 	struct bpf_map_info map_info;
308 	struct btf_var_secinfo *vsi;
309 	bool printed_header = false;
310 	unsigned int i, vlen;
311 	void *value = NULL;
312 	const char *name;
313 	struct btf *btf;
314 	int err;
315 
316 	if (!num_maps)
317 		return;
318 
319 	memset(&map_info, 0, sizeof(map_info));
320 	value = find_metadata(fd, &map_info);
321 	if (!value)
322 		return;
323 
324 	btf = btf__load_from_kernel_by_id(map_info.btf_id);
325 	if (!btf)
326 		goto out_free;
327 
328 	t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
329 	if (!btf_is_datasec(t_datasec))
330 		goto out_free;
331 
332 	vlen = btf_vlen(t_datasec);
333 	vsi = btf_var_secinfos(t_datasec);
334 
335 	/* We don't proceed to check the kinds of the elements of the DATASEC.
336 	 * The verifier enforces them to be BTF_KIND_VAR.
337 	 */
338 
339 	if (json_output) {
340 		struct btf_dumper d = {
341 			.btf = btf,
342 			.jw = json_wtr,
343 			.is_plain_text = false,
344 		};
345 
346 		for (i = 0; i < vlen; i++, vsi++) {
347 			t_var = btf__type_by_id(btf, vsi->type);
348 			name = btf__name_by_offset(btf, t_var->name_off);
349 
350 			if (!has_metadata_prefix(name))
351 				continue;
352 
353 			if (!printed_header) {
354 				jsonw_name(json_wtr, "metadata");
355 				jsonw_start_object(json_wtr);
356 				printed_header = true;
357 			}
358 
359 			jsonw_name(json_wtr, name + BPF_METADATA_PREFIX_LEN);
360 			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
361 			if (err) {
362 				p_err("btf dump failed: %d", err);
363 				break;
364 			}
365 		}
366 		if (printed_header)
367 			jsonw_end_object(json_wtr);
368 	} else {
369 		json_writer_t *btf_wtr;
370 		struct btf_dumper d = {
371 			.btf = btf,
372 			.is_plain_text = true,
373 		};
374 
375 		for (i = 0; i < vlen; i++, vsi++) {
376 			t_var = btf__type_by_id(btf, vsi->type);
377 			name = btf__name_by_offset(btf, t_var->name_off);
378 
379 			if (!has_metadata_prefix(name))
380 				continue;
381 
382 			if (!printed_header) {
383 				printf("\tmetadata:");
384 
385 				btf_wtr = jsonw_new(stdout);
386 				if (!btf_wtr) {
387 					p_err("jsonw alloc failed");
388 					goto out_free;
389 				}
390 				d.jw = btf_wtr,
391 
392 				printed_header = true;
393 			}
394 
395 			printf("\n\t\t%s = ", name + BPF_METADATA_PREFIX_LEN);
396 
397 			jsonw_reset(btf_wtr);
398 			err = btf_dumper_type(&d, t_var->type, value + vsi->offset);
399 			if (err) {
400 				p_err("btf dump failed: %d", err);
401 				break;
402 			}
403 		}
404 		if (printed_header)
405 			jsonw_destroy(&btf_wtr);
406 	}
407 
408 out_free:
409 	btf__free(btf);
410 	free(value);
411 }
412 
print_prog_header_json(struct bpf_prog_info * info,int fd)413 static void print_prog_header_json(struct bpf_prog_info *info, int fd)
414 {
415 	const char *prog_type_str;
416 	char prog_name[MAX_PROG_FULL_NAME];
417 
418 	jsonw_uint_field(json_wtr, "id", info->id);
419 	prog_type_str = libbpf_bpf_prog_type_str(info->type);
420 
421 	if (prog_type_str)
422 		jsonw_string_field(json_wtr, "type", prog_type_str);
423 	else
424 		jsonw_uint_field(json_wtr, "type", info->type);
425 
426 	if (*info->name) {
427 		get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
428 		jsonw_string_field(json_wtr, "name", prog_name);
429 	}
430 
431 	jsonw_name(json_wtr, "tag");
432 	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
433 		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
434 		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
435 
436 	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
437 	if (info->run_time_ns) {
438 		jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
439 		jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
440 	}
441 	if (info->recursion_misses)
442 		jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses);
443 }
444 
print_prog_json(struct bpf_prog_info * info,int fd)445 static void print_prog_json(struct bpf_prog_info *info, int fd)
446 {
447 	char *memlock;
448 
449 	jsonw_start_object(json_wtr);
450 	print_prog_header_json(info, fd);
451 	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
452 
453 	if (info->load_time) {
454 		char buf[32];
455 
456 		print_boot_time(info->load_time, buf, sizeof(buf));
457 
458 		/* Piggy back on load_time, since 0 uid is a valid one */
459 		jsonw_name(json_wtr, "loaded_at");
460 		jsonw_printf(json_wtr, "%s", buf);
461 		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
462 	}
463 
464 	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
465 
466 	if (info->jited_prog_len) {
467 		jsonw_bool_field(json_wtr, "jited", true);
468 		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
469 	} else {
470 		jsonw_bool_field(json_wtr, "jited", false);
471 	}
472 
473 	memlock = get_fdinfo(fd, "memlock");
474 	if (memlock)
475 		jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock));
476 	free(memlock);
477 
478 	if (info->nr_map_ids)
479 		show_prog_maps(fd, info->nr_map_ids);
480 
481 	if (info->btf_id)
482 		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
483 
484 	if (!hashmap__empty(prog_table)) {
485 		struct hashmap_entry *entry;
486 
487 		jsonw_name(json_wtr, "pinned");
488 		jsonw_start_array(json_wtr);
489 		hashmap__for_each_key_entry(prog_table, entry, info->id)
490 			jsonw_string(json_wtr, entry->pvalue);
491 		jsonw_end_array(json_wtr);
492 	}
493 
494 	emit_obj_refs_json(refs_table, info->id, json_wtr);
495 
496 	show_prog_metadata(fd, info->nr_map_ids);
497 
498 	jsonw_end_object(json_wtr);
499 }
500 
print_prog_header_plain(struct bpf_prog_info * info,int fd)501 static void print_prog_header_plain(struct bpf_prog_info *info, int fd)
502 {
503 	const char *prog_type_str;
504 	char prog_name[MAX_PROG_FULL_NAME];
505 
506 	printf("%u: ", info->id);
507 	prog_type_str = libbpf_bpf_prog_type_str(info->type);
508 	if (prog_type_str)
509 		printf("%s  ", prog_type_str);
510 	else
511 		printf("type %u  ", info->type);
512 
513 	if (*info->name) {
514 		get_prog_full_name(info, fd, prog_name, sizeof(prog_name));
515 		printf("name %s  ", prog_name);
516 	}
517 
518 	printf("tag ");
519 	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
520 	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
521 	printf("%s", info->gpl_compatible ? "  gpl" : "");
522 	if (info->run_time_ns)
523 		printf(" run_time_ns %lld run_cnt %lld",
524 		       info->run_time_ns, info->run_cnt);
525 	if (info->recursion_misses)
526 		printf(" recursion_misses %lld", info->recursion_misses);
527 	printf("\n");
528 }
529 
print_prog_plain(struct bpf_prog_info * info,int fd)530 static void print_prog_plain(struct bpf_prog_info *info, int fd)
531 {
532 	char *memlock;
533 
534 	print_prog_header_plain(info, fd);
535 
536 	if (info->load_time) {
537 		char buf[32];
538 
539 		print_boot_time(info->load_time, buf, sizeof(buf));
540 
541 		/* Piggy back on load_time, since 0 uid is a valid one */
542 		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
543 	}
544 
545 	printf("\txlated %uB", info->xlated_prog_len);
546 
547 	if (info->jited_prog_len)
548 		printf("  jited %uB", info->jited_prog_len);
549 	else
550 		printf("  not jited");
551 
552 	memlock = get_fdinfo(fd, "memlock");
553 	if (memlock)
554 		printf("  memlock %sB", memlock);
555 	free(memlock);
556 
557 	if (info->nr_map_ids)
558 		show_prog_maps(fd, info->nr_map_ids);
559 
560 	if (!hashmap__empty(prog_table)) {
561 		struct hashmap_entry *entry;
562 
563 		hashmap__for_each_key_entry(prog_table, entry, info->id)
564 			printf("\n\tpinned %s", (char *)entry->pvalue);
565 	}
566 
567 	if (info->btf_id)
568 		printf("\n\tbtf_id %d", info->btf_id);
569 
570 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
571 
572 	printf("\n");
573 
574 	show_prog_metadata(fd, info->nr_map_ids);
575 }
576 
show_prog(int fd)577 static int show_prog(int fd)
578 {
579 	struct bpf_prog_info info = {};
580 	__u32 len = sizeof(info);
581 	int err;
582 
583 	err = bpf_prog_get_info_by_fd(fd, &info, &len);
584 	if (err) {
585 		p_err("can't get prog info: %s", strerror(errno));
586 		return -1;
587 	}
588 
589 	if (json_output)
590 		print_prog_json(&info, fd);
591 	else
592 		print_prog_plain(&info, fd);
593 
594 	return 0;
595 }
596 
do_show_subset(int argc,char ** argv)597 static int do_show_subset(int argc, char **argv)
598 {
599 	int *fds = NULL;
600 	int nb_fds, i;
601 	int err = -1;
602 
603 	fds = malloc(sizeof(int));
604 	if (!fds) {
605 		p_err("mem alloc failed");
606 		return -1;
607 	}
608 	nb_fds = prog_parse_fds(&argc, &argv, &fds);
609 	if (nb_fds < 1)
610 		goto exit_free;
611 
612 	if (json_output && nb_fds > 1)
613 		jsonw_start_array(json_wtr);	/* root array */
614 	for (i = 0; i < nb_fds; i++) {
615 		err = show_prog(fds[i]);
616 		if (err) {
617 			for (; i < nb_fds; i++)
618 				close(fds[i]);
619 			break;
620 		}
621 		close(fds[i]);
622 	}
623 	if (json_output && nb_fds > 1)
624 		jsonw_end_array(json_wtr);	/* root array */
625 
626 exit_free:
627 	free(fds);
628 	return err;
629 }
630 
do_show(int argc,char ** argv)631 static int do_show(int argc, char **argv)
632 {
633 	__u32 id = 0;
634 	int err;
635 	int fd;
636 
637 	if (show_pinned) {
638 		prog_table = hashmap__new(hash_fn_for_key_as_id,
639 					  equal_fn_for_key_as_id, NULL);
640 		if (IS_ERR(prog_table)) {
641 			p_err("failed to create hashmap for pinned paths");
642 			return -1;
643 		}
644 		build_pinned_obj_table(prog_table, BPF_OBJ_PROG);
645 	}
646 	build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
647 
648 	if (argc == 2)
649 		return do_show_subset(argc, argv);
650 
651 	if (argc)
652 		return BAD_ARG();
653 
654 	if (json_output)
655 		jsonw_start_array(json_wtr);
656 	while (true) {
657 		err = bpf_prog_get_next_id(id, &id);
658 		if (err) {
659 			if (errno == ENOENT) {
660 				err = 0;
661 				break;
662 			}
663 			p_err("can't get next program: %s%s", strerror(errno),
664 			      errno == EINVAL ? " -- kernel too old?" : "");
665 			err = -1;
666 			break;
667 		}
668 
669 		fd = bpf_prog_get_fd_by_id(id);
670 		if (fd < 0) {
671 			if (errno == ENOENT)
672 				continue;
673 			p_err("can't get prog by id (%u): %s",
674 			      id, strerror(errno));
675 			err = -1;
676 			break;
677 		}
678 
679 		err = show_prog(fd);
680 		close(fd);
681 		if (err)
682 			break;
683 	}
684 
685 	if (json_output)
686 		jsonw_end_array(json_wtr);
687 
688 	delete_obj_refs_table(refs_table);
689 
690 	if (show_pinned)
691 		delete_pinned_obj_table(prog_table);
692 
693 	return err;
694 }
695 
696 static int
prog_dump(struct bpf_prog_info * info,enum dump_mode mode,char * filepath,bool opcodes,bool visual,bool linum)697 prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
698 	  char *filepath, bool opcodes, bool visual, bool linum)
699 {
700 	struct bpf_prog_linfo *prog_linfo = NULL;
701 	const char *disasm_opt = NULL;
702 	struct dump_data dd = {};
703 	void *func_info = NULL;
704 	struct btf *btf = NULL;
705 	char func_sig[1024];
706 	unsigned char *buf;
707 	__u32 member_len;
708 	int fd, err = -1;
709 	ssize_t n;
710 
711 	if (mode == DUMP_JITED) {
712 		if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
713 			p_info("no instructions returned");
714 			return -1;
715 		}
716 		buf = u64_to_ptr(info->jited_prog_insns);
717 		member_len = info->jited_prog_len;
718 	} else {	/* DUMP_XLATED */
719 		if (info->xlated_prog_len == 0 || !info->xlated_prog_insns) {
720 			p_err("error retrieving insn dump: kernel.kptr_restrict set?");
721 			return -1;
722 		}
723 		buf = u64_to_ptr(info->xlated_prog_insns);
724 		member_len = info->xlated_prog_len;
725 	}
726 
727 	if (info->btf_id) {
728 		btf = btf__load_from_kernel_by_id(info->btf_id);
729 		if (!btf) {
730 			p_err("failed to get btf");
731 			return -1;
732 		}
733 	}
734 
735 	func_info = u64_to_ptr(info->func_info);
736 
737 	if (info->nr_line_info) {
738 		prog_linfo = bpf_prog_linfo__new(info);
739 		if (!prog_linfo)
740 			p_info("error in processing bpf_line_info.  continue without it.");
741 	}
742 
743 	if (filepath) {
744 		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
745 		if (fd < 0) {
746 			p_err("can't open file %s: %s", filepath,
747 			      strerror(errno));
748 			goto exit_free;
749 		}
750 
751 		n = write(fd, buf, member_len);
752 		close(fd);
753 		if (n != (ssize_t)member_len) {
754 			p_err("error writing output file: %s",
755 			      n < 0 ? strerror(errno) : "short write");
756 			goto exit_free;
757 		}
758 
759 		if (json_output)
760 			jsonw_null(json_wtr);
761 	} else if (mode == DUMP_JITED) {
762 		const char *name = NULL;
763 
764 		if (info->ifindex) {
765 			name = ifindex_to_arch(info->ifindex, info->netns_dev,
766 					       info->netns_ino, &disasm_opt);
767 			if (!name)
768 				goto exit_free;
769 		}
770 
771 		if (info->nr_jited_func_lens && info->jited_func_lens) {
772 			struct kernel_sym *sym = NULL;
773 			struct bpf_func_info *record;
774 			char sym_name[SYM_MAX_NAME];
775 			unsigned char *img = buf;
776 			__u64 *ksyms = NULL;
777 			__u32 *lens;
778 			__u32 i;
779 			if (info->nr_jited_ksyms) {
780 				kernel_syms_load(&dd);
781 				ksyms = u64_to_ptr(info->jited_ksyms);
782 			}
783 
784 			if (json_output)
785 				jsonw_start_array(json_wtr);
786 
787 			lens = u64_to_ptr(info->jited_func_lens);
788 			for (i = 0; i < info->nr_jited_func_lens; i++) {
789 				if (ksyms) {
790 					sym = kernel_syms_search(&dd, ksyms[i]);
791 					if (sym)
792 						sprintf(sym_name, "%s", sym->name);
793 					else
794 						sprintf(sym_name, "0x%016llx", ksyms[i]);
795 				} else {
796 					strcpy(sym_name, "unknown");
797 				}
798 
799 				if (func_info) {
800 					record = func_info + i * info->func_info_rec_size;
801 					btf_dumper_type_only(btf, record->type_id,
802 							     func_sig,
803 							     sizeof(func_sig));
804 				}
805 
806 				if (json_output) {
807 					jsonw_start_object(json_wtr);
808 					if (func_info && func_sig[0] != '\0') {
809 						jsonw_name(json_wtr, "proto");
810 						jsonw_string(json_wtr, func_sig);
811 					}
812 					jsonw_name(json_wtr, "name");
813 					jsonw_string(json_wtr, sym_name);
814 					jsonw_name(json_wtr, "insns");
815 				} else {
816 					if (func_info && func_sig[0] != '\0')
817 						printf("%s:\n", func_sig);
818 					printf("%s:\n", sym_name);
819 				}
820 
821 				if (ksyms) {
822 					if (disasm_print_insn(img, lens[i], opcodes,
823 							      name, disasm_opt, btf,
824 							      prog_linfo, ksyms[i], i,
825 							      linum))
826 						goto exit_free;
827 				} else {
828 					if (disasm_print_insn(img, lens[i], opcodes,
829 							      name, disasm_opt, btf,
830 							      NULL, 0, 0, false))
831 						goto exit_free;
832 				}
833 
834 				img += lens[i];
835 
836 				if (json_output)
837 					jsonw_end_object(json_wtr);
838 				else
839 					printf("\n");
840 			}
841 
842 			if (json_output)
843 				jsonw_end_array(json_wtr);
844 		} else {
845 			if (disasm_print_insn(buf, member_len, opcodes, name,
846 					      disasm_opt, btf, NULL, 0, 0,
847 					      false))
848 				goto exit_free;
849 		}
850 	} else {
851 		kernel_syms_load(&dd);
852 		dd.nr_jited_ksyms = info->nr_jited_ksyms;
853 		dd.jited_ksyms = u64_to_ptr(info->jited_ksyms);
854 		dd.btf = btf;
855 		dd.func_info = func_info;
856 		dd.finfo_rec_size = info->func_info_rec_size;
857 		dd.prog_linfo = prog_linfo;
858 
859 		if (json_output)
860 			dump_xlated_json(&dd, buf, member_len, opcodes, linum);
861 		else if (visual)
862 			dump_xlated_cfg(&dd, buf, member_len, opcodes, linum);
863 		else
864 			dump_xlated_plain(&dd, buf, member_len, opcodes, linum);
865 		kernel_syms_destroy(&dd);
866 	}
867 
868 	err = 0;
869 
870 exit_free:
871 	btf__free(btf);
872 	bpf_prog_linfo__free(prog_linfo);
873 	return err;
874 }
875 
do_dump(int argc,char ** argv)876 static int do_dump(int argc, char **argv)
877 {
878 	struct bpf_prog_info info;
879 	__u32 info_len = sizeof(info);
880 	size_t info_data_sz = 0;
881 	void *info_data = NULL;
882 	char *filepath = NULL;
883 	bool opcodes = false;
884 	bool visual = false;
885 	enum dump_mode mode;
886 	bool linum = false;
887 	int nb_fds, i = 0;
888 	int *fds = NULL;
889 	int err = -1;
890 
891 	if (is_prefix(*argv, "jited")) {
892 		if (disasm_init())
893 			return -1;
894 		mode = DUMP_JITED;
895 	} else if (is_prefix(*argv, "xlated")) {
896 		mode = DUMP_XLATED;
897 	} else {
898 		p_err("expected 'xlated' or 'jited', got: %s", *argv);
899 		return -1;
900 	}
901 	NEXT_ARG();
902 
903 	if (argc < 2)
904 		usage();
905 
906 	fds = malloc(sizeof(int));
907 	if (!fds) {
908 		p_err("mem alloc failed");
909 		return -1;
910 	}
911 	nb_fds = prog_parse_fds(&argc, &argv, &fds);
912 	if (nb_fds < 1)
913 		goto exit_free;
914 
915 	while (argc) {
916 		if (is_prefix(*argv, "file")) {
917 			NEXT_ARG();
918 			if (!argc) {
919 				p_err("expected file path");
920 				goto exit_close;
921 			}
922 			if (nb_fds > 1) {
923 				p_err("several programs matched");
924 				goto exit_close;
925 			}
926 
927 			filepath = *argv;
928 			NEXT_ARG();
929 		} else if (is_prefix(*argv, "opcodes")) {
930 			opcodes = true;
931 			NEXT_ARG();
932 		} else if (is_prefix(*argv, "visual")) {
933 			if (nb_fds > 1) {
934 				p_err("several programs matched");
935 				goto exit_close;
936 			}
937 
938 			visual = true;
939 			NEXT_ARG();
940 		} else if (is_prefix(*argv, "linum")) {
941 			linum = true;
942 			NEXT_ARG();
943 		} else {
944 			usage();
945 			goto exit_close;
946 		}
947 	}
948 
949 	if (filepath && (opcodes || visual || linum)) {
950 		p_err("'file' is not compatible with 'opcodes', 'visual', or 'linum'");
951 		goto exit_close;
952 	}
953 	if (json_output && visual) {
954 		p_err("'visual' is not compatible with JSON output");
955 		goto exit_close;
956 	}
957 
958 	if (json_output && nb_fds > 1)
959 		jsonw_start_array(json_wtr);	/* root array */
960 	for (i = 0; i < nb_fds; i++) {
961 		memset(&info, 0, sizeof(info));
962 
963 		err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
964 		if (err) {
965 			p_err("can't get prog info: %s", strerror(errno));
966 			break;
967 		}
968 
969 		err = prep_prog_info(&info, mode, &info_data, &info_data_sz);
970 		if (err) {
971 			p_err("can't grow prog info_data");
972 			break;
973 		}
974 
975 		err = bpf_prog_get_info_by_fd(fds[i], &info, &info_len);
976 		if (err) {
977 			p_err("can't get prog info: %s", strerror(errno));
978 			break;
979 		}
980 
981 		if (json_output && nb_fds > 1) {
982 			jsonw_start_object(json_wtr);	/* prog object */
983 			print_prog_header_json(&info, fds[i]);
984 			jsonw_name(json_wtr, "insns");
985 		} else if (nb_fds > 1) {
986 			print_prog_header_plain(&info, fds[i]);
987 		}
988 
989 		err = prog_dump(&info, mode, filepath, opcodes, visual, linum);
990 
991 		if (json_output && nb_fds > 1)
992 			jsonw_end_object(json_wtr);	/* prog object */
993 		else if (i != nb_fds - 1 && nb_fds > 1)
994 			printf("\n");
995 
996 		if (err)
997 			break;
998 		close(fds[i]);
999 	}
1000 	if (json_output && nb_fds > 1)
1001 		jsonw_end_array(json_wtr);	/* root array */
1002 
1003 exit_close:
1004 	for (; i < nb_fds; i++)
1005 		close(fds[i]);
1006 exit_free:
1007 	free(info_data);
1008 	free(fds);
1009 	return err;
1010 }
1011 
do_pin(int argc,char ** argv)1012 static int do_pin(int argc, char **argv)
1013 {
1014 	int err;
1015 
1016 	err = do_pin_any(argc, argv, prog_parse_fd);
1017 	if (!err && json_output)
1018 		jsonw_null(json_wtr);
1019 	return err;
1020 }
1021 
1022 struct map_replace {
1023 	int idx;
1024 	int fd;
1025 	char *name;
1026 };
1027 
map_replace_compar(const void * p1,const void * p2)1028 static int map_replace_compar(const void *p1, const void *p2)
1029 {
1030 	const struct map_replace *a = p1, *b = p2;
1031 
1032 	return a->idx - b->idx;
1033 }
1034 
parse_attach_detach_args(int argc,char ** argv,int * progfd,enum bpf_attach_type * attach_type,int * mapfd)1035 static int parse_attach_detach_args(int argc, char **argv, int *progfd,
1036 				    enum bpf_attach_type *attach_type,
1037 				    int *mapfd)
1038 {
1039 	if (!REQ_ARGS(3))
1040 		return -EINVAL;
1041 
1042 	*progfd = prog_parse_fd(&argc, &argv);
1043 	if (*progfd < 0)
1044 		return *progfd;
1045 
1046 	*attach_type = parse_attach_type(*argv);
1047 	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
1048 		p_err("invalid attach/detach type");
1049 		return -EINVAL;
1050 	}
1051 
1052 	if (*attach_type == BPF_FLOW_DISSECTOR) {
1053 		*mapfd = 0;
1054 		return 0;
1055 	}
1056 
1057 	NEXT_ARG();
1058 	if (!REQ_ARGS(2))
1059 		return -EINVAL;
1060 
1061 	*mapfd = map_parse_fd(&argc, &argv);
1062 	if (*mapfd < 0)
1063 		return *mapfd;
1064 
1065 	return 0;
1066 }
1067 
do_attach(int argc,char ** argv)1068 static int do_attach(int argc, char **argv)
1069 {
1070 	enum bpf_attach_type attach_type;
1071 	int err, progfd;
1072 	int mapfd;
1073 
1074 	err = parse_attach_detach_args(argc, argv,
1075 				       &progfd, &attach_type, &mapfd);
1076 	if (err)
1077 		return err;
1078 
1079 	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
1080 	if (err) {
1081 		p_err("failed prog attach to map");
1082 		return -EINVAL;
1083 	}
1084 
1085 	if (json_output)
1086 		jsonw_null(json_wtr);
1087 	return 0;
1088 }
1089 
do_detach(int argc,char ** argv)1090 static int do_detach(int argc, char **argv)
1091 {
1092 	enum bpf_attach_type attach_type;
1093 	int err, progfd;
1094 	int mapfd;
1095 
1096 	err = parse_attach_detach_args(argc, argv,
1097 				       &progfd, &attach_type, &mapfd);
1098 	if (err)
1099 		return err;
1100 
1101 	err = bpf_prog_detach2(progfd, mapfd, attach_type);
1102 	if (err) {
1103 		p_err("failed prog detach from map");
1104 		return -EINVAL;
1105 	}
1106 
1107 	if (json_output)
1108 		jsonw_null(json_wtr);
1109 	return 0;
1110 }
1111 
check_single_stdin(char * file_data_in,char * file_ctx_in)1112 static int check_single_stdin(char *file_data_in, char *file_ctx_in)
1113 {
1114 	if (file_data_in && file_ctx_in &&
1115 	    !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
1116 		p_err("cannot use standard input for both data_in and ctx_in");
1117 		return -1;
1118 	}
1119 
1120 	return 0;
1121 }
1122 
get_run_data(const char * fname,void ** data_ptr,unsigned int * size)1123 static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
1124 {
1125 	size_t block_size = 256;
1126 	size_t buf_size = block_size;
1127 	size_t nb_read = 0;
1128 	void *tmp;
1129 	FILE *f;
1130 
1131 	if (!fname) {
1132 		*data_ptr = NULL;
1133 		*size = 0;
1134 		return 0;
1135 	}
1136 
1137 	if (!strcmp(fname, "-"))
1138 		f = stdin;
1139 	else
1140 		f = fopen(fname, "r");
1141 	if (!f) {
1142 		p_err("failed to open %s: %s", fname, strerror(errno));
1143 		return -1;
1144 	}
1145 
1146 	*data_ptr = malloc(block_size);
1147 	if (!*data_ptr) {
1148 		p_err("failed to allocate memory for data_in/ctx_in: %s",
1149 		      strerror(errno));
1150 		goto err_fclose;
1151 	}
1152 
1153 	while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
1154 		if (feof(f))
1155 			break;
1156 		if (ferror(f)) {
1157 			p_err("failed to read data_in/ctx_in from %s: %s",
1158 			      fname, strerror(errno));
1159 			goto err_free;
1160 		}
1161 		if (nb_read > buf_size - block_size) {
1162 			if (buf_size == UINT32_MAX) {
1163 				p_err("data_in/ctx_in is too long (max: %d)",
1164 				      UINT32_MAX);
1165 				goto err_free;
1166 			}
1167 			/* No space for fread()-ing next chunk; realloc() */
1168 			buf_size *= 2;
1169 			tmp = realloc(*data_ptr, buf_size);
1170 			if (!tmp) {
1171 				p_err("failed to reallocate data_in/ctx_in: %s",
1172 				      strerror(errno));
1173 				goto err_free;
1174 			}
1175 			*data_ptr = tmp;
1176 		}
1177 	}
1178 	if (f != stdin)
1179 		fclose(f);
1180 
1181 	*size = nb_read;
1182 	return 0;
1183 
1184 err_free:
1185 	free(*data_ptr);
1186 	*data_ptr = NULL;
1187 err_fclose:
1188 	if (f != stdin)
1189 		fclose(f);
1190 	return -1;
1191 }
1192 
hex_print(void * data,unsigned int size,FILE * f)1193 static void hex_print(void *data, unsigned int size, FILE *f)
1194 {
1195 	size_t i, j;
1196 	char c;
1197 
1198 	for (i = 0; i < size; i += 16) {
1199 		/* Row offset */
1200 		fprintf(f, "%07zx\t", i);
1201 
1202 		/* Hexadecimal values */
1203 		for (j = i; j < i + 16 && j < size; j++)
1204 			fprintf(f, "%02x%s", *(uint8_t *)(data + j),
1205 				j % 2 ? " " : "");
1206 		for (; j < i + 16; j++)
1207 			fprintf(f, "  %s", j % 2 ? " " : "");
1208 
1209 		/* ASCII values (if relevant), '.' otherwise */
1210 		fprintf(f, "| ");
1211 		for (j = i; j < i + 16 && j < size; j++) {
1212 			c = *(char *)(data + j);
1213 			if (c < ' ' || c > '~')
1214 				c = '.';
1215 			fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
1216 		}
1217 
1218 		fprintf(f, "\n");
1219 	}
1220 }
1221 
1222 static int
print_run_output(void * data,unsigned int size,const char * fname,const char * json_key)1223 print_run_output(void *data, unsigned int size, const char *fname,
1224 		 const char *json_key)
1225 {
1226 	size_t nb_written;
1227 	FILE *f;
1228 
1229 	if (!fname)
1230 		return 0;
1231 
1232 	if (!strcmp(fname, "-")) {
1233 		f = stdout;
1234 		if (json_output) {
1235 			jsonw_name(json_wtr, json_key);
1236 			print_data_json(data, size);
1237 		} else {
1238 			hex_print(data, size, f);
1239 		}
1240 		return 0;
1241 	}
1242 
1243 	f = fopen(fname, "w");
1244 	if (!f) {
1245 		p_err("failed to open %s: %s", fname, strerror(errno));
1246 		return -1;
1247 	}
1248 
1249 	nb_written = fwrite(data, 1, size, f);
1250 	fclose(f);
1251 	if (nb_written != size) {
1252 		p_err("failed to write output data/ctx: %s", strerror(errno));
1253 		return -1;
1254 	}
1255 
1256 	return 0;
1257 }
1258 
alloc_run_data(void ** data_ptr,unsigned int size_out)1259 static int alloc_run_data(void **data_ptr, unsigned int size_out)
1260 {
1261 	*data_ptr = calloc(size_out, 1);
1262 	if (!*data_ptr) {
1263 		p_err("failed to allocate memory for output data/ctx: %s",
1264 		      strerror(errno));
1265 		return -1;
1266 	}
1267 
1268 	return 0;
1269 }
1270 
do_run(int argc,char ** argv)1271 static int do_run(int argc, char **argv)
1272 {
1273 	char *data_fname_in = NULL, *data_fname_out = NULL;
1274 	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
1275 	const unsigned int default_size = SZ_32K;
1276 	void *data_in = NULL, *data_out = NULL;
1277 	void *ctx_in = NULL, *ctx_out = NULL;
1278 	unsigned int repeat = 1;
1279 	int fd, err;
1280 	LIBBPF_OPTS(bpf_test_run_opts, test_attr);
1281 
1282 	if (!REQ_ARGS(4))
1283 		return -1;
1284 
1285 	fd = prog_parse_fd(&argc, &argv);
1286 	if (fd < 0)
1287 		return -1;
1288 
1289 	while (argc) {
1290 		if (detect_common_prefix(*argv, "data_in", "data_out",
1291 					 "data_size_out", NULL))
1292 			return -1;
1293 		if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
1294 					 "ctx_size_out", NULL))
1295 			return -1;
1296 
1297 		if (is_prefix(*argv, "data_in")) {
1298 			NEXT_ARG();
1299 			if (!REQ_ARGS(1))
1300 				return -1;
1301 
1302 			data_fname_in = GET_ARG();
1303 			if (check_single_stdin(data_fname_in, ctx_fname_in))
1304 				return -1;
1305 		} else if (is_prefix(*argv, "data_out")) {
1306 			NEXT_ARG();
1307 			if (!REQ_ARGS(1))
1308 				return -1;
1309 
1310 			data_fname_out = GET_ARG();
1311 		} else if (is_prefix(*argv, "data_size_out")) {
1312 			char *endptr;
1313 
1314 			NEXT_ARG();
1315 			if (!REQ_ARGS(1))
1316 				return -1;
1317 
1318 			test_attr.data_size_out = strtoul(*argv, &endptr, 0);
1319 			if (*endptr) {
1320 				p_err("can't parse %s as output data size",
1321 				      *argv);
1322 				return -1;
1323 			}
1324 			NEXT_ARG();
1325 		} else if (is_prefix(*argv, "ctx_in")) {
1326 			NEXT_ARG();
1327 			if (!REQ_ARGS(1))
1328 				return -1;
1329 
1330 			ctx_fname_in = GET_ARG();
1331 			if (check_single_stdin(data_fname_in, ctx_fname_in))
1332 				return -1;
1333 		} else if (is_prefix(*argv, "ctx_out")) {
1334 			NEXT_ARG();
1335 			if (!REQ_ARGS(1))
1336 				return -1;
1337 
1338 			ctx_fname_out = GET_ARG();
1339 		} else if (is_prefix(*argv, "ctx_size_out")) {
1340 			char *endptr;
1341 
1342 			NEXT_ARG();
1343 			if (!REQ_ARGS(1))
1344 				return -1;
1345 
1346 			test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
1347 			if (*endptr) {
1348 				p_err("can't parse %s as output context size",
1349 				      *argv);
1350 				return -1;
1351 			}
1352 			NEXT_ARG();
1353 		} else if (is_prefix(*argv, "repeat")) {
1354 			char *endptr;
1355 
1356 			NEXT_ARG();
1357 			if (!REQ_ARGS(1))
1358 				return -1;
1359 
1360 			repeat = strtoul(*argv, &endptr, 0);
1361 			if (*endptr) {
1362 				p_err("can't parse %s as repeat number",
1363 				      *argv);
1364 				return -1;
1365 			}
1366 			NEXT_ARG();
1367 		} else {
1368 			p_err("expected no more arguments, 'data_in', 'data_out', 'data_size_out', 'ctx_in', 'ctx_out', 'ctx_size_out' or 'repeat', got: '%s'?",
1369 			      *argv);
1370 			return -1;
1371 		}
1372 	}
1373 
1374 	err = get_run_data(data_fname_in, &data_in, &test_attr.data_size_in);
1375 	if (err)
1376 		return -1;
1377 
1378 	if (data_in) {
1379 		if (!test_attr.data_size_out)
1380 			test_attr.data_size_out = default_size;
1381 		err = alloc_run_data(&data_out, test_attr.data_size_out);
1382 		if (err)
1383 			goto free_data_in;
1384 	}
1385 
1386 	err = get_run_data(ctx_fname_in, &ctx_in, &test_attr.ctx_size_in);
1387 	if (err)
1388 		goto free_data_out;
1389 
1390 	if (ctx_in) {
1391 		if (!test_attr.ctx_size_out)
1392 			test_attr.ctx_size_out = default_size;
1393 		err = alloc_run_data(&ctx_out, test_attr.ctx_size_out);
1394 		if (err)
1395 			goto free_ctx_in;
1396 	}
1397 
1398 	test_attr.repeat	= repeat;
1399 	test_attr.data_in	= data_in;
1400 	test_attr.data_out	= data_out;
1401 	test_attr.ctx_in	= ctx_in;
1402 	test_attr.ctx_out	= ctx_out;
1403 
1404 	err = bpf_prog_test_run_opts(fd, &test_attr);
1405 	if (err) {
1406 		p_err("failed to run program: %s", strerror(errno));
1407 		goto free_ctx_out;
1408 	}
1409 
1410 	err = 0;
1411 
1412 	if (json_output)
1413 		jsonw_start_object(json_wtr);	/* root */
1414 
1415 	/* Do not exit on errors occurring when printing output data/context,
1416 	 * we still want to print return value and duration for program run.
1417 	 */
1418 	if (test_attr.data_size_out)
1419 		err += print_run_output(test_attr.data_out,
1420 					test_attr.data_size_out,
1421 					data_fname_out, "data_out");
1422 	if (test_attr.ctx_size_out)
1423 		err += print_run_output(test_attr.ctx_out,
1424 					test_attr.ctx_size_out,
1425 					ctx_fname_out, "ctx_out");
1426 
1427 	if (json_output) {
1428 		jsonw_uint_field(json_wtr, "retval", test_attr.retval);
1429 		jsonw_uint_field(json_wtr, "duration", test_attr.duration);
1430 		jsonw_end_object(json_wtr);	/* root */
1431 	} else {
1432 		fprintf(stdout, "Return value: %u, duration%s: %uns\n",
1433 			test_attr.retval,
1434 			repeat > 1 ? " (average)" : "", test_attr.duration);
1435 	}
1436 
1437 free_ctx_out:
1438 	free(ctx_out);
1439 free_ctx_in:
1440 	free(ctx_in);
1441 free_data_out:
1442 	free(data_out);
1443 free_data_in:
1444 	free(data_in);
1445 
1446 	return err;
1447 }
1448 
1449 static int
get_prog_type_by_name(const char * name,enum bpf_prog_type * prog_type,enum bpf_attach_type * expected_attach_type)1450 get_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
1451 		      enum bpf_attach_type *expected_attach_type)
1452 {
1453 	libbpf_print_fn_t print_backup;
1454 	int ret;
1455 
1456 	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1457 	if (!ret)
1458 		return ret;
1459 
1460 	/* libbpf_prog_type_by_name() failed, let's re-run with debug level */
1461 	print_backup = libbpf_set_print(print_all_levels);
1462 	ret = libbpf_prog_type_by_name(name, prog_type, expected_attach_type);
1463 	libbpf_set_print(print_backup);
1464 
1465 	return ret;
1466 }
1467 
1468 static int
auto_attach_program(struct bpf_program * prog,const char * path)1469 auto_attach_program(struct bpf_program *prog, const char *path)
1470 {
1471 	struct bpf_link *link;
1472 	int err;
1473 
1474 	link = bpf_program__attach(prog);
1475 	if (!link) {
1476 		p_info("Program %s does not support autoattach, falling back to pinning",
1477 		       bpf_program__name(prog));
1478 		return bpf_obj_pin(bpf_program__fd(prog), path);
1479 	}
1480 
1481 	err = bpf_link__pin(link, path);
1482 	bpf_link__destroy(link);
1483 	return err;
1484 }
1485 
1486 static int
auto_attach_programs(struct bpf_object * obj,const char * path)1487 auto_attach_programs(struct bpf_object *obj, const char *path)
1488 {
1489 	struct bpf_program *prog;
1490 	char buf[PATH_MAX];
1491 	int err;
1492 
1493 	bpf_object__for_each_program(prog, obj) {
1494 		err = pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog));
1495 		if (err)
1496 			goto err_unpin_programs;
1497 
1498 		err = auto_attach_program(prog, buf);
1499 		if (err)
1500 			goto err_unpin_programs;
1501 	}
1502 
1503 	return 0;
1504 
1505 err_unpin_programs:
1506 	while ((prog = bpf_object__prev_program(obj, prog))) {
1507 		if (pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog)))
1508 			continue;
1509 
1510 		bpf_program__unpin(prog, buf);
1511 	}
1512 
1513 	return err;
1514 }
1515 
load_with_options(int argc,char ** argv,bool first_prog_only)1516 static int load_with_options(int argc, char **argv, bool first_prog_only)
1517 {
1518 	enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
1519 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
1520 		.relaxed_maps = relaxed_maps,
1521 	);
1522 	enum bpf_attach_type expected_attach_type;
1523 	struct map_replace *map_replace = NULL;
1524 	struct bpf_program *prog = NULL, *pos;
1525 	unsigned int old_map_fds = 0;
1526 	const char *pinmaps = NULL;
1527 	__u32 xdpmeta_ifindex = 0;
1528 	__u32 offload_ifindex = 0;
1529 	bool auto_attach = false;
1530 	struct bpf_object *obj;
1531 	struct bpf_map *map;
1532 	const char *pinfile;
1533 	unsigned int i, j;
1534 	const char *file;
1535 	int idx, err;
1536 
1537 
1538 	if (!REQ_ARGS(2))
1539 		return -1;
1540 	file = GET_ARG();
1541 	pinfile = GET_ARG();
1542 
1543 	while (argc) {
1544 		if (is_prefix(*argv, "type")) {
1545 			NEXT_ARG();
1546 
1547 			if (common_prog_type != BPF_PROG_TYPE_UNSPEC) {
1548 				p_err("program type already specified");
1549 				goto err_free_reuse_maps;
1550 			}
1551 			if (!REQ_ARGS(1))
1552 				goto err_free_reuse_maps;
1553 
1554 			err = libbpf_prog_type_by_name(*argv, &common_prog_type,
1555 						       &expected_attach_type);
1556 			if (err < 0) {
1557 				/* Put a '/' at the end of type to appease libbpf */
1558 				char *type = malloc(strlen(*argv) + 2);
1559 
1560 				if (!type) {
1561 					p_err("mem alloc failed");
1562 					goto err_free_reuse_maps;
1563 				}
1564 				*type = 0;
1565 				strcat(type, *argv);
1566 				strcat(type, "/");
1567 
1568 				err = get_prog_type_by_name(type, &common_prog_type,
1569 							    &expected_attach_type);
1570 				free(type);
1571 				if (err < 0)
1572 					goto err_free_reuse_maps;
1573 			}
1574 
1575 			NEXT_ARG();
1576 		} else if (is_prefix(*argv, "map")) {
1577 			void *new_map_replace;
1578 			char *endptr, *name;
1579 			int fd;
1580 
1581 			NEXT_ARG();
1582 
1583 			if (!REQ_ARGS(4))
1584 				goto err_free_reuse_maps;
1585 
1586 			if (is_prefix(*argv, "idx")) {
1587 				NEXT_ARG();
1588 
1589 				idx = strtoul(*argv, &endptr, 0);
1590 				if (*endptr) {
1591 					p_err("can't parse %s as IDX", *argv);
1592 					goto err_free_reuse_maps;
1593 				}
1594 				name = NULL;
1595 			} else if (is_prefix(*argv, "name")) {
1596 				NEXT_ARG();
1597 
1598 				name = *argv;
1599 				idx = -1;
1600 			} else {
1601 				p_err("expected 'idx' or 'name', got: '%s'?",
1602 				      *argv);
1603 				goto err_free_reuse_maps;
1604 			}
1605 			NEXT_ARG();
1606 
1607 			fd = map_parse_fd(&argc, &argv);
1608 			if (fd < 0)
1609 				goto err_free_reuse_maps;
1610 
1611 			new_map_replace = libbpf_reallocarray(map_replace,
1612 							      old_map_fds + 1,
1613 							      sizeof(*map_replace));
1614 			if (!new_map_replace) {
1615 				p_err("mem alloc failed");
1616 				goto err_free_reuse_maps;
1617 			}
1618 			map_replace = new_map_replace;
1619 
1620 			map_replace[old_map_fds].idx = idx;
1621 			map_replace[old_map_fds].name = name;
1622 			map_replace[old_map_fds].fd = fd;
1623 			old_map_fds++;
1624 		} else if (is_prefix(*argv, "dev")) {
1625 			p_info("Warning: 'bpftool prog load [...] dev <ifname>' syntax is deprecated.\n"
1626 			       "Going further, please use 'offload_dev <ifname>' to offload program to device.\n"
1627 			       "For applications using XDP hints only, use 'xdpmeta_dev <ifname>'.");
1628 			goto offload_dev;
1629 		} else if (is_prefix(*argv, "offload_dev")) {
1630 offload_dev:
1631 			NEXT_ARG();
1632 
1633 			if (offload_ifindex) {
1634 				p_err("offload_dev already specified");
1635 				goto err_free_reuse_maps;
1636 			} else if (xdpmeta_ifindex) {
1637 				p_err("xdpmeta_dev and offload_dev are mutually exclusive");
1638 				goto err_free_reuse_maps;
1639 			}
1640 			if (!REQ_ARGS(1))
1641 				goto err_free_reuse_maps;
1642 
1643 			offload_ifindex = if_nametoindex(*argv);
1644 			if (!offload_ifindex) {
1645 				p_err("unrecognized netdevice '%s': %s",
1646 				      *argv, strerror(errno));
1647 				goto err_free_reuse_maps;
1648 			}
1649 			NEXT_ARG();
1650 		} else if (is_prefix(*argv, "xdpmeta_dev")) {
1651 			NEXT_ARG();
1652 
1653 			if (xdpmeta_ifindex) {
1654 				p_err("xdpmeta_dev already specified");
1655 				goto err_free_reuse_maps;
1656 			} else if (offload_ifindex) {
1657 				p_err("xdpmeta_dev and offload_dev are mutually exclusive");
1658 				goto err_free_reuse_maps;
1659 			}
1660 			if (!REQ_ARGS(1))
1661 				goto err_free_reuse_maps;
1662 
1663 			xdpmeta_ifindex = if_nametoindex(*argv);
1664 			if (!xdpmeta_ifindex) {
1665 				p_err("unrecognized netdevice '%s': %s",
1666 				      *argv, strerror(errno));
1667 				goto err_free_reuse_maps;
1668 			}
1669 			NEXT_ARG();
1670 		} else if (is_prefix(*argv, "pinmaps")) {
1671 			NEXT_ARG();
1672 
1673 			if (!REQ_ARGS(1))
1674 				goto err_free_reuse_maps;
1675 
1676 			pinmaps = GET_ARG();
1677 		} else if (is_prefix(*argv, "autoattach")) {
1678 			auto_attach = true;
1679 			NEXT_ARG();
1680 		} else {
1681 			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1682 			      *argv);
1683 			goto err_free_reuse_maps;
1684 		}
1685 	}
1686 
1687 	set_max_rlimit();
1688 
1689 	if (verifier_logs)
1690 		/* log_level1 + log_level2 + stats, but not stable UAPI */
1691 		open_opts.kernel_log_level = 1 + 2 + 4;
1692 
1693 	obj = bpf_object__open_file(file, &open_opts);
1694 	if (!obj) {
1695 		p_err("failed to open object file");
1696 		goto err_free_reuse_maps;
1697 	}
1698 
1699 	bpf_object__for_each_program(pos, obj) {
1700 		enum bpf_prog_type prog_type = common_prog_type;
1701 
1702 		if (prog_type == BPF_PROG_TYPE_UNSPEC) {
1703 			const char *sec_name = bpf_program__section_name(pos);
1704 
1705 			err = get_prog_type_by_name(sec_name, &prog_type,
1706 						    &expected_attach_type);
1707 			if (err < 0)
1708 				goto err_close_obj;
1709 		}
1710 
1711 		if (prog_type == BPF_PROG_TYPE_XDP && xdpmeta_ifindex) {
1712 			bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
1713 			bpf_program__set_ifindex(pos, xdpmeta_ifindex);
1714 		} else {
1715 			bpf_program__set_ifindex(pos, offload_ifindex);
1716 		}
1717 		if (bpf_program__type(pos) != prog_type)
1718 			bpf_program__set_type(pos, prog_type);
1719 		bpf_program__set_expected_attach_type(pos, expected_attach_type);
1720 	}
1721 
1722 	qsort(map_replace, old_map_fds, sizeof(*map_replace),
1723 	      map_replace_compar);
1724 
1725 	/* After the sort maps by name will be first on the list, because they
1726 	 * have idx == -1.  Resolve them.
1727 	 */
1728 	j = 0;
1729 	while (j < old_map_fds && map_replace[j].name) {
1730 		i = 0;
1731 		bpf_object__for_each_map(map, obj) {
1732 			if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
1733 				map_replace[j].idx = i;
1734 				break;
1735 			}
1736 			i++;
1737 		}
1738 		if (map_replace[j].idx == -1) {
1739 			p_err("unable to find map '%s'", map_replace[j].name);
1740 			goto err_close_obj;
1741 		}
1742 		j++;
1743 	}
1744 	/* Resort if any names were resolved */
1745 	if (j)
1746 		qsort(map_replace, old_map_fds, sizeof(*map_replace),
1747 		      map_replace_compar);
1748 
1749 	/* Set ifindex and name reuse */
1750 	j = 0;
1751 	idx = 0;
1752 	bpf_object__for_each_map(map, obj) {
1753 		if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1754 			bpf_map__set_ifindex(map, offload_ifindex);
1755 
1756 		if (j < old_map_fds && idx == map_replace[j].idx) {
1757 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
1758 			if (err) {
1759 				p_err("unable to set up map reuse: %d", err);
1760 				goto err_close_obj;
1761 			}
1762 
1763 			/* Next reuse wants to apply to the same map */
1764 			if (j < old_map_fds && map_replace[j].idx == idx) {
1765 				p_err("replacement for map idx %d specified more than once",
1766 				      idx);
1767 				goto err_close_obj;
1768 			}
1769 		}
1770 
1771 		idx++;
1772 	}
1773 	if (j < old_map_fds) {
1774 		p_err("map idx '%d' not used", map_replace[j].idx);
1775 		goto err_close_obj;
1776 	}
1777 
1778 	err = bpf_object__load(obj);
1779 	if (err) {
1780 		p_err("failed to load object file");
1781 		goto err_close_obj;
1782 	}
1783 
1784 	if (first_prog_only)
1785 		err = mount_bpffs_for_file(pinfile);
1786 	else
1787 		err = create_and_mount_bpffs_dir(pinfile);
1788 	if (err)
1789 		goto err_close_obj;
1790 
1791 	if (first_prog_only) {
1792 		prog = bpf_object__next_program(obj, NULL);
1793 		if (!prog) {
1794 			p_err("object file doesn't contain any bpf program");
1795 			goto err_close_obj;
1796 		}
1797 
1798 		if (auto_attach)
1799 			err = auto_attach_program(prog, pinfile);
1800 		else
1801 			err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1802 		if (err) {
1803 			p_err("failed to pin program %s",
1804 			      bpf_program__section_name(prog));
1805 			goto err_close_obj;
1806 		}
1807 	} else {
1808 		if (auto_attach)
1809 			err = auto_attach_programs(obj, pinfile);
1810 		else
1811 			err = bpf_object__pin_programs(obj, pinfile);
1812 		if (err) {
1813 			p_err("failed to pin all programs");
1814 			goto err_close_obj;
1815 		}
1816 	}
1817 
1818 	if (pinmaps) {
1819 		err = create_and_mount_bpffs_dir(pinmaps);
1820 		if (err)
1821 			goto err_unpin;
1822 
1823 		err = bpf_object__pin_maps(obj, pinmaps);
1824 		if (err) {
1825 			p_err("failed to pin all maps");
1826 			goto err_unpin;
1827 		}
1828 	}
1829 
1830 	if (json_output)
1831 		jsonw_null(json_wtr);
1832 
1833 	bpf_object__close(obj);
1834 	for (i = 0; i < old_map_fds; i++)
1835 		close(map_replace[i].fd);
1836 	free(map_replace);
1837 
1838 	return 0;
1839 
1840 err_unpin:
1841 	if (first_prog_only)
1842 		unlink(pinfile);
1843 	else
1844 		bpf_object__unpin_programs(obj, pinfile);
1845 err_close_obj:
1846 	bpf_object__close(obj);
1847 err_free_reuse_maps:
1848 	for (i = 0; i < old_map_fds; i++)
1849 		close(map_replace[i].fd);
1850 	free(map_replace);
1851 	return -1;
1852 }
1853 
count_open_fds(void)1854 static int count_open_fds(void)
1855 {
1856 	DIR *dp = opendir("/proc/self/fd");
1857 	struct dirent *de;
1858 	int cnt = -3;
1859 
1860 	if (!dp)
1861 		return -1;
1862 
1863 	while ((de = readdir(dp)))
1864 		cnt++;
1865 
1866 	closedir(dp);
1867 	return cnt;
1868 }
1869 
try_loader(struct gen_loader_opts * gen)1870 static int try_loader(struct gen_loader_opts *gen)
1871 {
1872 	struct bpf_load_and_run_opts opts = {};
1873 	struct bpf_loader_ctx *ctx;
1874 	int ctx_sz = sizeof(*ctx) + 64 * max(sizeof(struct bpf_map_desc),
1875 					     sizeof(struct bpf_prog_desc));
1876 	int log_buf_sz = (1u << 24) - 1;
1877 	int err, fds_before, fd_delta;
1878 	char *log_buf = NULL;
1879 
1880 	ctx = alloca(ctx_sz);
1881 	memset(ctx, 0, ctx_sz);
1882 	ctx->sz = ctx_sz;
1883 	if (verifier_logs) {
1884 		ctx->log_level = 1 + 2 + 4;
1885 		ctx->log_size = log_buf_sz;
1886 		log_buf = malloc(log_buf_sz);
1887 		if (!log_buf)
1888 			return -ENOMEM;
1889 		ctx->log_buf = (long) log_buf;
1890 	}
1891 	opts.ctx = ctx;
1892 	opts.data = gen->data;
1893 	opts.data_sz = gen->data_sz;
1894 	opts.insns = gen->insns;
1895 	opts.insns_sz = gen->insns_sz;
1896 	fds_before = count_open_fds();
1897 	err = bpf_load_and_run(&opts);
1898 	fd_delta = count_open_fds() - fds_before;
1899 	if (err < 0 || verifier_logs) {
1900 		fprintf(stderr, "err %d\n%s\n%s", err, opts.errstr, log_buf);
1901 		if (fd_delta && err < 0)
1902 			fprintf(stderr, "loader prog leaked %d FDs\n",
1903 				fd_delta);
1904 	}
1905 	free(log_buf);
1906 	return err;
1907 }
1908 
do_loader(int argc,char ** argv)1909 static int do_loader(int argc, char **argv)
1910 {
1911 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
1912 	DECLARE_LIBBPF_OPTS(gen_loader_opts, gen);
1913 	struct bpf_object *obj;
1914 	const char *file;
1915 	int err = 0;
1916 
1917 	if (!REQ_ARGS(1))
1918 		return -1;
1919 	file = GET_ARG();
1920 
1921 	if (verifier_logs)
1922 		/* log_level1 + log_level2 + stats, but not stable UAPI */
1923 		open_opts.kernel_log_level = 1 + 2 + 4;
1924 
1925 	obj = bpf_object__open_file(file, &open_opts);
1926 	if (!obj) {
1927 		p_err("failed to open object file");
1928 		goto err_close_obj;
1929 	}
1930 
1931 	err = bpf_object__gen_loader(obj, &gen);
1932 	if (err)
1933 		goto err_close_obj;
1934 
1935 	err = bpf_object__load(obj);
1936 	if (err) {
1937 		p_err("failed to load object file");
1938 		goto err_close_obj;
1939 	}
1940 
1941 	if (verifier_logs) {
1942 		struct dump_data dd = {};
1943 
1944 		kernel_syms_load(&dd);
1945 		dump_xlated_plain(&dd, (void *)gen.insns, gen.insns_sz, false, false);
1946 		kernel_syms_destroy(&dd);
1947 	}
1948 	err = try_loader(&gen);
1949 err_close_obj:
1950 	bpf_object__close(obj);
1951 	return err;
1952 }
1953 
do_load(int argc,char ** argv)1954 static int do_load(int argc, char **argv)
1955 {
1956 	if (use_loader)
1957 		return do_loader(argc, argv);
1958 	return load_with_options(argc, argv, true);
1959 }
1960 
do_loadall(int argc,char ** argv)1961 static int do_loadall(int argc, char **argv)
1962 {
1963 	return load_with_options(argc, argv, false);
1964 }
1965 
1966 #ifdef BPFTOOL_WITHOUT_SKELETONS
1967 
do_profile(int argc,char ** argv)1968 static int do_profile(int argc, char **argv)
1969 {
1970 	p_err("bpftool prog profile command is not supported. Please build bpftool with clang >= 10.0.0");
1971 	return 0;
1972 }
1973 
1974 #else /* BPFTOOL_WITHOUT_SKELETONS */
1975 
1976 #include "profiler.skel.h"
1977 
1978 struct profile_metric {
1979 	const char *name;
1980 	struct bpf_perf_event_value val;
1981 	struct perf_event_attr attr;
1982 	bool selected;
1983 
1984 	/* calculate ratios like instructions per cycle */
1985 	const int ratio_metric; /* 0 for N/A, 1 for index 0 (cycles) */
1986 	const char *ratio_desc;
1987 	const float ratio_mul;
1988 } metrics[] = {
1989 	{
1990 		.name = "cycles",
1991 		.attr = {
1992 			.type = PERF_TYPE_HARDWARE,
1993 			.config = PERF_COUNT_HW_CPU_CYCLES,
1994 			.exclude_user = 1,
1995 		},
1996 	},
1997 	{
1998 		.name = "instructions",
1999 		.attr = {
2000 			.type = PERF_TYPE_HARDWARE,
2001 			.config = PERF_COUNT_HW_INSTRUCTIONS,
2002 			.exclude_user = 1,
2003 		},
2004 		.ratio_metric = 1,
2005 		.ratio_desc = "insns per cycle",
2006 		.ratio_mul = 1.0,
2007 	},
2008 	{
2009 		.name = "l1d_loads",
2010 		.attr = {
2011 			.type = PERF_TYPE_HW_CACHE,
2012 			.config =
2013 				PERF_COUNT_HW_CACHE_L1D |
2014 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
2015 				(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
2016 			.exclude_user = 1,
2017 		},
2018 	},
2019 	{
2020 		.name = "llc_misses",
2021 		.attr = {
2022 			.type = PERF_TYPE_HW_CACHE,
2023 			.config =
2024 				PERF_COUNT_HW_CACHE_LL |
2025 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
2026 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
2027 			.exclude_user = 1
2028 		},
2029 		.ratio_metric = 2,
2030 		.ratio_desc = "LLC misses per million insns",
2031 		.ratio_mul = 1e6,
2032 	},
2033 	{
2034 		.name = "itlb_misses",
2035 		.attr = {
2036 			.type = PERF_TYPE_HW_CACHE,
2037 			.config =
2038 				PERF_COUNT_HW_CACHE_ITLB |
2039 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
2040 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
2041 			.exclude_user = 1
2042 		},
2043 		.ratio_metric = 2,
2044 		.ratio_desc = "itlb misses per million insns",
2045 		.ratio_mul = 1e6,
2046 	},
2047 	{
2048 		.name = "dtlb_misses",
2049 		.attr = {
2050 			.type = PERF_TYPE_HW_CACHE,
2051 			.config =
2052 				PERF_COUNT_HW_CACHE_DTLB |
2053 				(PERF_COUNT_HW_CACHE_OP_READ << 8) |
2054 				(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
2055 			.exclude_user = 1
2056 		},
2057 		.ratio_metric = 2,
2058 		.ratio_desc = "dtlb misses per million insns",
2059 		.ratio_mul = 1e6,
2060 	},
2061 };
2062 
2063 static __u64 profile_total_count;
2064 
2065 #define MAX_NUM_PROFILE_METRICS 4
2066 
profile_parse_metrics(int argc,char ** argv)2067 static int profile_parse_metrics(int argc, char **argv)
2068 {
2069 	unsigned int metric_cnt;
2070 	int selected_cnt = 0;
2071 	unsigned int i;
2072 
2073 	metric_cnt = ARRAY_SIZE(metrics);
2074 
2075 	while (argc > 0) {
2076 		for (i = 0; i < metric_cnt; i++) {
2077 			if (is_prefix(argv[0], metrics[i].name)) {
2078 				if (!metrics[i].selected)
2079 					selected_cnt++;
2080 				metrics[i].selected = true;
2081 				break;
2082 			}
2083 		}
2084 		if (i == metric_cnt) {
2085 			p_err("unknown metric %s", argv[0]);
2086 			return -1;
2087 		}
2088 		NEXT_ARG();
2089 	}
2090 	if (selected_cnt > MAX_NUM_PROFILE_METRICS) {
2091 		p_err("too many (%d) metrics, please specify no more than %d metrics at at time",
2092 		      selected_cnt, MAX_NUM_PROFILE_METRICS);
2093 		return -1;
2094 	}
2095 	return selected_cnt;
2096 }
2097 
profile_read_values(struct profiler_bpf * obj)2098 static void profile_read_values(struct profiler_bpf *obj)
2099 {
2100 	__u32 m, cpu, num_cpu = obj->rodata->num_cpu;
2101 	int reading_map_fd, count_map_fd;
2102 	__u64 counts[num_cpu];
2103 	__u32 key = 0;
2104 	int err;
2105 
2106 	reading_map_fd = bpf_map__fd(obj->maps.accum_readings);
2107 	count_map_fd = bpf_map__fd(obj->maps.counts);
2108 	if (reading_map_fd < 0 || count_map_fd < 0) {
2109 		p_err("failed to get fd for map");
2110 		return;
2111 	}
2112 
2113 	err = bpf_map_lookup_elem(count_map_fd, &key, counts);
2114 	if (err) {
2115 		p_err("failed to read count_map: %s", strerror(errno));
2116 		return;
2117 	}
2118 
2119 	profile_total_count = 0;
2120 	for (cpu = 0; cpu < num_cpu; cpu++)
2121 		profile_total_count += counts[cpu];
2122 
2123 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
2124 		struct bpf_perf_event_value values[num_cpu];
2125 
2126 		if (!metrics[m].selected)
2127 			continue;
2128 
2129 		err = bpf_map_lookup_elem(reading_map_fd, &key, values);
2130 		if (err) {
2131 			p_err("failed to read reading_map: %s",
2132 			      strerror(errno));
2133 			return;
2134 		}
2135 		for (cpu = 0; cpu < num_cpu; cpu++) {
2136 			metrics[m].val.counter += values[cpu].counter;
2137 			metrics[m].val.enabled += values[cpu].enabled;
2138 			metrics[m].val.running += values[cpu].running;
2139 		}
2140 		key++;
2141 	}
2142 }
2143 
profile_print_readings_json(void)2144 static void profile_print_readings_json(void)
2145 {
2146 	__u32 m;
2147 
2148 	jsonw_start_array(json_wtr);
2149 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
2150 		if (!metrics[m].selected)
2151 			continue;
2152 		jsonw_start_object(json_wtr);
2153 		jsonw_string_field(json_wtr, "metric", metrics[m].name);
2154 		jsonw_lluint_field(json_wtr, "run_cnt", profile_total_count);
2155 		jsonw_lluint_field(json_wtr, "value", metrics[m].val.counter);
2156 		jsonw_lluint_field(json_wtr, "enabled", metrics[m].val.enabled);
2157 		jsonw_lluint_field(json_wtr, "running", metrics[m].val.running);
2158 
2159 		jsonw_end_object(json_wtr);
2160 	}
2161 	jsonw_end_array(json_wtr);
2162 }
2163 
profile_print_readings_plain(void)2164 static void profile_print_readings_plain(void)
2165 {
2166 	__u32 m;
2167 
2168 	printf("\n%18llu %-20s\n", profile_total_count, "run_cnt");
2169 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
2170 		struct bpf_perf_event_value *val = &metrics[m].val;
2171 		int r;
2172 
2173 		if (!metrics[m].selected)
2174 			continue;
2175 		printf("%18llu %-20s", val->counter, metrics[m].name);
2176 
2177 		r = metrics[m].ratio_metric - 1;
2178 		if (r >= 0 && metrics[r].selected &&
2179 		    metrics[r].val.counter > 0) {
2180 			printf("# %8.2f %-30s",
2181 			       val->counter * metrics[m].ratio_mul /
2182 			       metrics[r].val.counter,
2183 			       metrics[m].ratio_desc);
2184 		} else {
2185 			printf("%-41s", "");
2186 		}
2187 
2188 		if (val->enabled > val->running)
2189 			printf("(%4.2f%%)",
2190 			       val->running * 100.0 / val->enabled);
2191 		printf("\n");
2192 	}
2193 }
2194 
profile_print_readings(void)2195 static void profile_print_readings(void)
2196 {
2197 	if (json_output)
2198 		profile_print_readings_json();
2199 	else
2200 		profile_print_readings_plain();
2201 }
2202 
profile_target_name(int tgt_fd)2203 static char *profile_target_name(int tgt_fd)
2204 {
2205 	struct bpf_func_info func_info;
2206 	struct bpf_prog_info info = {};
2207 	__u32 info_len = sizeof(info);
2208 	const struct btf_type *t;
2209 	__u32 func_info_rec_size;
2210 	struct btf *btf = NULL;
2211 	char *name = NULL;
2212 	int err;
2213 
2214 	err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
2215 	if (err) {
2216 		p_err("failed to get info for prog FD %d", tgt_fd);
2217 		goto out;
2218 	}
2219 
2220 	if (info.btf_id == 0) {
2221 		p_err("prog FD %d doesn't have valid btf", tgt_fd);
2222 		goto out;
2223 	}
2224 
2225 	func_info_rec_size = info.func_info_rec_size;
2226 	if (info.nr_func_info == 0) {
2227 		p_err("found 0 func_info for prog FD %d", tgt_fd);
2228 		goto out;
2229 	}
2230 
2231 	memset(&info, 0, sizeof(info));
2232 	info.nr_func_info = 1;
2233 	info.func_info_rec_size = func_info_rec_size;
2234 	info.func_info = ptr_to_u64(&func_info);
2235 
2236 	err = bpf_prog_get_info_by_fd(tgt_fd, &info, &info_len);
2237 	if (err) {
2238 		p_err("failed to get func_info for prog FD %d", tgt_fd);
2239 		goto out;
2240 	}
2241 
2242 	btf = btf__load_from_kernel_by_id(info.btf_id);
2243 	if (!btf) {
2244 		p_err("failed to load btf for prog FD %d", tgt_fd);
2245 		goto out;
2246 	}
2247 
2248 	t = btf__type_by_id(btf, func_info.type_id);
2249 	if (!t) {
2250 		p_err("btf %d doesn't have type %d",
2251 		      info.btf_id, func_info.type_id);
2252 		goto out;
2253 	}
2254 	name = strdup(btf__name_by_offset(btf, t->name_off));
2255 out:
2256 	btf__free(btf);
2257 	return name;
2258 }
2259 
2260 static struct profiler_bpf *profile_obj;
2261 static int profile_tgt_fd = -1;
2262 static char *profile_tgt_name;
2263 static int *profile_perf_events;
2264 static int profile_perf_event_cnt;
2265 
profile_close_perf_events(struct profiler_bpf * obj)2266 static void profile_close_perf_events(struct profiler_bpf *obj)
2267 {
2268 	int i;
2269 
2270 	for (i = profile_perf_event_cnt - 1; i >= 0; i--)
2271 		close(profile_perf_events[i]);
2272 
2273 	free(profile_perf_events);
2274 	profile_perf_event_cnt = 0;
2275 }
2276 
profile_open_perf_event(int mid,int cpu,int map_fd)2277 static int profile_open_perf_event(int mid, int cpu, int map_fd)
2278 {
2279 	int pmu_fd;
2280 
2281 	pmu_fd = syscall(__NR_perf_event_open, &metrics[mid].attr,
2282 			 -1 /*pid*/, cpu, -1 /*group_fd*/, 0);
2283 	if (pmu_fd < 0) {
2284 		if (errno == ENODEV) {
2285 			p_info("cpu %d may be offline, skip %s profiling.",
2286 				cpu, metrics[mid].name);
2287 			profile_perf_event_cnt++;
2288 			return 0;
2289 		}
2290 		return -1;
2291 	}
2292 
2293 	if (bpf_map_update_elem(map_fd,
2294 				&profile_perf_event_cnt,
2295 				&pmu_fd, BPF_ANY) ||
2296 	    ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) {
2297 		close(pmu_fd);
2298 		return -1;
2299 	}
2300 
2301 	profile_perf_events[profile_perf_event_cnt++] = pmu_fd;
2302 	return 0;
2303 }
2304 
profile_open_perf_events(struct profiler_bpf * obj)2305 static int profile_open_perf_events(struct profiler_bpf *obj)
2306 {
2307 	unsigned int cpu, m;
2308 	int map_fd;
2309 
2310 	profile_perf_events = calloc(
2311 		obj->rodata->num_cpu * obj->rodata->num_metric, sizeof(int));
2312 	if (!profile_perf_events) {
2313 		p_err("failed to allocate memory for perf_event array: %s",
2314 		      strerror(errno));
2315 		return -1;
2316 	}
2317 	map_fd = bpf_map__fd(obj->maps.events);
2318 	if (map_fd < 0) {
2319 		p_err("failed to get fd for events map");
2320 		return -1;
2321 	}
2322 
2323 	for (m = 0; m < ARRAY_SIZE(metrics); m++) {
2324 		if (!metrics[m].selected)
2325 			continue;
2326 		for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) {
2327 			if (profile_open_perf_event(m, cpu, map_fd)) {
2328 				p_err("failed to create event %s on cpu %d",
2329 				      metrics[m].name, cpu);
2330 				return -1;
2331 			}
2332 		}
2333 	}
2334 	return 0;
2335 }
2336 
profile_print_and_cleanup(void)2337 static void profile_print_and_cleanup(void)
2338 {
2339 	profile_close_perf_events(profile_obj);
2340 	profile_read_values(profile_obj);
2341 	profile_print_readings();
2342 	profiler_bpf__destroy(profile_obj);
2343 
2344 	close(profile_tgt_fd);
2345 	free(profile_tgt_name);
2346 }
2347 
int_exit(int signo)2348 static void int_exit(int signo)
2349 {
2350 	profile_print_and_cleanup();
2351 	exit(0);
2352 }
2353 
do_profile(int argc,char ** argv)2354 static int do_profile(int argc, char **argv)
2355 {
2356 	int num_metric, num_cpu, err = -1;
2357 	struct bpf_program *prog;
2358 	unsigned long duration;
2359 	char *endptr;
2360 
2361 	/* we at least need two args for the prog and one metric */
2362 	if (!REQ_ARGS(3))
2363 		return -EINVAL;
2364 
2365 	/* parse target fd */
2366 	profile_tgt_fd = prog_parse_fd(&argc, &argv);
2367 	if (profile_tgt_fd < 0) {
2368 		p_err("failed to parse fd");
2369 		return -1;
2370 	}
2371 
2372 	/* parse profiling optional duration */
2373 	if (argc > 2 && is_prefix(argv[0], "duration")) {
2374 		NEXT_ARG();
2375 		duration = strtoul(*argv, &endptr, 0);
2376 		if (*endptr)
2377 			usage();
2378 		NEXT_ARG();
2379 	} else {
2380 		duration = UINT_MAX;
2381 	}
2382 
2383 	num_metric = profile_parse_metrics(argc, argv);
2384 	if (num_metric <= 0)
2385 		goto out;
2386 
2387 	num_cpu = libbpf_num_possible_cpus();
2388 	if (num_cpu <= 0) {
2389 		p_err("failed to identify number of CPUs");
2390 		goto out;
2391 	}
2392 
2393 	profile_obj = profiler_bpf__open();
2394 	if (!profile_obj) {
2395 		p_err("failed to open and/or load BPF object");
2396 		goto out;
2397 	}
2398 
2399 	profile_obj->rodata->num_cpu = num_cpu;
2400 	profile_obj->rodata->num_metric = num_metric;
2401 
2402 	/* adjust map sizes */
2403 	bpf_map__set_max_entries(profile_obj->maps.events, num_metric * num_cpu);
2404 	bpf_map__set_max_entries(profile_obj->maps.fentry_readings, num_metric);
2405 	bpf_map__set_max_entries(profile_obj->maps.accum_readings, num_metric);
2406 	bpf_map__set_max_entries(profile_obj->maps.counts, 1);
2407 
2408 	/* change target name */
2409 	profile_tgt_name = profile_target_name(profile_tgt_fd);
2410 	if (!profile_tgt_name)
2411 		goto out;
2412 
2413 	bpf_object__for_each_program(prog, profile_obj->obj) {
2414 		err = bpf_program__set_attach_target(prog, profile_tgt_fd,
2415 						     profile_tgt_name);
2416 		if (err) {
2417 			p_err("failed to set attach target\n");
2418 			goto out;
2419 		}
2420 	}
2421 
2422 	set_max_rlimit();
2423 	err = profiler_bpf__load(profile_obj);
2424 	if (err) {
2425 		p_err("failed to load profile_obj");
2426 		goto out;
2427 	}
2428 
2429 	err = profile_open_perf_events(profile_obj);
2430 	if (err)
2431 		goto out;
2432 
2433 	err = profiler_bpf__attach(profile_obj);
2434 	if (err) {
2435 		p_err("failed to attach profile_obj");
2436 		goto out;
2437 	}
2438 	signal(SIGINT, int_exit);
2439 
2440 	sleep(duration);
2441 	profile_print_and_cleanup();
2442 	return 0;
2443 
2444 out:
2445 	profile_close_perf_events(profile_obj);
2446 	if (profile_obj)
2447 		profiler_bpf__destroy(profile_obj);
2448 	close(profile_tgt_fd);
2449 	free(profile_tgt_name);
2450 	return err;
2451 }
2452 
2453 #endif /* BPFTOOL_WITHOUT_SKELETONS */
2454 
do_help(int argc,char ** argv)2455 static int do_help(int argc, char **argv)
2456 {
2457 	if (json_output) {
2458 		jsonw_null(json_wtr);
2459 		return 0;
2460 	}
2461 
2462 	fprintf(stderr,
2463 		"Usage: %1$s %2$s { show | list } [PROG]\n"
2464 		"       %1$s %2$s dump xlated PROG [{ file FILE | [opcodes] [linum] [visual] }]\n"
2465 		"       %1$s %2$s dump jited  PROG [{ file FILE | [opcodes] [linum] }]\n"
2466 		"       %1$s %2$s pin   PROG FILE\n"
2467 		"       %1$s %2$s { load | loadall } OBJ  PATH \\\n"
2468 		"                         [type TYPE] [{ offload_dev | xdpmeta_dev } NAME] \\\n"
2469 		"                         [map { idx IDX | name NAME } MAP]\\\n"
2470 		"                         [pinmaps MAP_DIR]\n"
2471 		"                         [autoattach]\n"
2472 		"       %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
2473 		"       %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
2474 		"       %1$s %2$s run PROG \\\n"
2475 		"                         data_in FILE \\\n"
2476 		"                         [data_out FILE [data_size_out L]] \\\n"
2477 		"                         [ctx_in FILE [ctx_out FILE [ctx_size_out M]]] \\\n"
2478 		"                         [repeat N]\n"
2479 		"       %1$s %2$s profile PROG [duration DURATION] METRICs\n"
2480 		"       %1$s %2$s tracelog\n"
2481 		"       %1$s %2$s help\n"
2482 		"\n"
2483 		"       " HELP_SPEC_MAP "\n"
2484 		"       " HELP_SPEC_PROGRAM "\n"
2485 		"       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
2486 		"                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
2487 		"                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
2488 		"                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
2489 		"                 sk_reuseport | flow_dissector | cgroup/sysctl |\n"
2490 		"                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
2491 		"                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
2492 		"                 cgroup/getpeername4 | cgroup/getpeername6 |\n"
2493 		"                 cgroup/getsockname4 | cgroup/getsockname6 | cgroup/sendmsg4 |\n"
2494 		"                 cgroup/sendmsg6 | cgroup/recvmsg4 | cgroup/recvmsg6 |\n"
2495 		"                 cgroup/getsockopt | cgroup/setsockopt | cgroup/sock_release |\n"
2496 		"                 struct_ops | fentry | fexit | freplace | sk_lookup }\n"
2497 		"       ATTACH_TYPE := { sk_msg_verdict | sk_skb_verdict | sk_skb_stream_verdict |\n"
2498 		"                        sk_skb_stream_parser | flow_dissector }\n"
2499 		"       METRIC := { cycles | instructions | l1d_loads | llc_misses | itlb_misses | dtlb_misses }\n"
2500 		"       " HELP_SPEC_OPTIONS " |\n"
2501 		"                    {-f|--bpffs} | {-m|--mapcompat} | {-n|--nomount} |\n"
2502 		"                    {-L|--use-loader} }\n"
2503 		"",
2504 		bin_name, argv[-2]);
2505 
2506 	return 0;
2507 }
2508 
2509 static const struct cmd cmds[] = {
2510 	{ "show",	do_show },
2511 	{ "list",	do_show },
2512 	{ "help",	do_help },
2513 	{ "dump",	do_dump },
2514 	{ "pin",	do_pin },
2515 	{ "load",	do_load },
2516 	{ "loadall",	do_loadall },
2517 	{ "attach",	do_attach },
2518 	{ "detach",	do_detach },
2519 	{ "tracelog",	do_tracelog },
2520 	{ "run",	do_run },
2521 	{ "profile",	do_profile },
2522 	{ 0 }
2523 };
2524 
do_prog(int argc,char ** argv)2525 int do_prog(int argc, char **argv)
2526 {
2527 	return cmd_select(cmds, argc, argv, do_help);
2528 }
2529