prog.c 32.2 KB
Newer Older
1
2
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
Jakub Kicinski's avatar
Jakub Kicinski committed
3

4
#define _GNU_SOURCE
Jakub Kicinski's avatar
Jakub Kicinski committed
5
6
#include <errno.h>
#include <fcntl.h>
7
#include <stdarg.h>
Jakub Kicinski's avatar
Jakub Kicinski committed
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
13
#include <net/if.h>
Jakub Kicinski's avatar
Jakub Kicinski committed
14
15
16
#include <sys/types.h>
#include <sys/stat.h>

17
#include <linux/err.h>
18
#include <linux/sizes.h>
19

Jakub Kicinski's avatar
Jakub Kicinski committed
20
#include <bpf.h>
21
#include <btf.h>
22
#include <libbpf.h>
Jakub Kicinski's avatar
Jakub Kicinski committed
23

24
#include "cfg.h"
Jakub Kicinski's avatar
Jakub Kicinski committed
25
#include "main.h"
26
#include "xlated_dumper.h"
Jakub Kicinski's avatar
Jakub Kicinski committed
27

28
29
30
31
static const char * const attach_type_strings[] = {
	[BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
	[BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
	[BPF_SK_MSG_VERDICT] = "msg_verdict",
32
	[BPF_FLOW_DISSECTOR] = "flow_dissector",
33
34
35
	[__MAX_BPF_ATTACH_TYPE] = NULL,
};

36
static enum bpf_attach_type parse_attach_type(const char *str)
37
38
39
40
41
42
43
44
45
46
47
48
{
	enum bpf_attach_type type;

	for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
		if (attach_type_strings[type] &&
		    is_prefix(str, attach_type_strings[type]))
			return type;
	}

	return __MAX_BPF_ATTACH_TYPE;
}

Jakub Kicinski's avatar
Jakub Kicinski committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
{
	struct timespec real_time_ts, boot_time_ts;
	time_t wallclock_secs;
	struct tm load_tm;

	buf[--size] = '\0';

	if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
	    clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
		perror("Can't read clocks");
		snprintf(buf, size, "%llu", nsecs / 1000000000);
		return;
	}

	wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
65
66
67
		(real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
		1000000000;

Jakub Kicinski's avatar
Jakub Kicinski committed
68
69
70
71
72
73

	if (!localtime_r(&wallclock_secs, &load_tm)) {
		snprintf(buf, size, "%llu", nsecs / 1000000000);
		return;
	}

74
75
76
77
	if (json_output)
		strftime(buf, size, "%s", &load_tm);
	else
		strftime(buf, size, "%FT%T%z", &load_tm);
Jakub Kicinski's avatar
Jakub Kicinski committed
78
79
80
81
82
83
84
85
86
}

static int prog_fd_by_tag(unsigned char *tag)
{
	unsigned int id = 0;
	int err;
	int fd;

	while (true) {
Jiri Olsa's avatar
Jiri Olsa committed
87
88
89
		struct bpf_prog_info info = {};
		__u32 len = sizeof(info);

Jakub Kicinski's avatar
Jakub Kicinski committed
90
91
		err = bpf_prog_get_next_id(id, &id);
		if (err) {
92
			p_err("%s", strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
93
94
95
96
97
			return -1;
		}

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0) {
98
99
			p_err("can't get prog by id (%u): %s",
			      id, strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
100
101
102
103
104
			return -1;
		}

		err = bpf_obj_get_info_by_fd(fd, &info, &len);
		if (err) {
105
106
			p_err("can't get prog info (%u): %s",
			      id, strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
			close(fd);
			return -1;
		}

		if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
			return fd;

		close(fd);
	}
}

int prog_parse_fd(int *argc, char ***argv)
{
	int fd;

	if (is_prefix(**argv, "id")) {
		unsigned int id;
		char *endptr;

		NEXT_ARGP();

		id = strtoul(**argv, &endptr, 0);
		if (*endptr) {
130
			p_err("can't parse %s as ID", **argv);
Jakub Kicinski's avatar
Jakub Kicinski committed
131
132
133
134
135
136
			return -1;
		}
		NEXT_ARGP();

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0)
137
			p_err("get by id (%u): %s", id, strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
138
139
140
141
142
143
144
145
146
		return fd;
	} else if (is_prefix(**argv, "tag")) {
		unsigned char tag[BPF_TAG_SIZE];

		NEXT_ARGP();

		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
		    != BPF_TAG_SIZE) {
147
			p_err("can't parse tag");
Jakub Kicinski's avatar
Jakub Kicinski committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
			return -1;
		}
		NEXT_ARGP();

		return prog_fd_by_tag(tag);
	} else if (is_prefix(**argv, "pinned")) {
		char *path;

		NEXT_ARGP();

		path = **argv;
		NEXT_ARGP();

		return open_obj_pinned_any(path, BPF_OBJ_PROG);
	}

164
	p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
Jakub Kicinski's avatar
Jakub Kicinski committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	return -1;
}

static void show_prog_maps(int fd, u32 num_maps)
{
	struct bpf_prog_info info = {};
	__u32 len = sizeof(info);
	__u32 map_ids[num_maps];
	unsigned int i;
	int err;

	info.nr_map_ids = num_maps;
	info.map_ids = ptr_to_u64(map_ids);

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	if (err || !info.nr_map_ids)
		return;

183
184
185
186
187
188
189
190
191
192
193
194
	if (json_output) {
		jsonw_name(json_wtr, "map_ids");
		jsonw_start_array(json_wtr);
		for (i = 0; i < info.nr_map_ids; i++)
			jsonw_uint(json_wtr, map_ids[i]);
		jsonw_end_array(json_wtr);
	} else {
		printf("  map_ids ");
		for (i = 0; i < info.nr_map_ids; i++)
			printf("%u%s", map_ids[i],
			       i == info.nr_map_ids - 1 ? "" : ",");
	}
Jakub Kicinski's avatar
Jakub Kicinski committed
195
196
}

197
static void print_prog_json(struct bpf_prog_info *info, int fd)
Jakub Kicinski's avatar
Jakub Kicinski committed
198
199
200
{
	char *memlock;

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	jsonw_start_object(json_wtr);
	jsonw_uint_field(json_wtr, "id", info->id);
	if (info->type < ARRAY_SIZE(prog_type_name))
		jsonw_string_field(json_wtr, "type",
				   prog_type_name[info->type]);
	else
		jsonw_uint_field(json_wtr, "type", info->type);

	if (*info->name)
		jsonw_string_field(json_wtr, "name", info->name);

	jsonw_name(json_wtr, "tag");
	jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
		     info->tag[0], info->tag[1], info->tag[2], info->tag[3],
		     info->tag[4], info->tag[5], info->tag[6], info->tag[7]);

217
	jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
218
219
220
221
	if (info->run_time_ns) {
		jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns);
		jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt);
	}
222

223
224
	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);

225
226
227
228
229
230
	if (info->load_time) {
		char buf[32];

		print_boot_time(info->load_time, buf, sizeof(buf));

		/* Piggy back on load_time, since 0 uid is a valid one */
231
232
		jsonw_name(json_wtr, "loaded_at");
		jsonw_printf(json_wtr, "%s", buf);
233
234
235
236
237
238
239
240
241
242
		jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
	}

	jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);

	if (info->jited_prog_len) {
		jsonw_bool_field(json_wtr, "jited", true);
		jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
	} else {
		jsonw_bool_field(json_wtr, "jited", false);
Jakub Kicinski's avatar
Jakub Kicinski committed
243
244
	}

245
246
247
248
249
250
251
252
	memlock = get_fdinfo(fd, "memlock");
	if (memlock)
		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
	free(memlock);

	if (info->nr_map_ids)
		show_prog_maps(fd, info->nr_map_ids);

253
254
255
	if (info->btf_id)
		jsonw_int_field(json_wtr, "btf_id", info->btf_id);

256
257
258
259
260
261
262
263
264
265
266
267
	if (!hash_empty(prog_table.table)) {
		struct pinned_obj *obj;

		jsonw_name(json_wtr, "pinned");
		jsonw_start_array(json_wtr);
		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
				jsonw_string(json_wtr, obj->path);
		}
		jsonw_end_array(json_wtr);
	}

268
269
270
271
272
273
274
275
276
277
	jsonw_end_object(json_wtr);
}

static void print_prog_plain(struct bpf_prog_info *info, int fd)
{
	char *memlock;

	printf("%u: ", info->id);
	if (info->type < ARRAY_SIZE(prog_type_name))
		printf("%s  ", prog_type_name[info->type]);
Jakub Kicinski's avatar
Jakub Kicinski committed
278
	else
279
		printf("type %u  ", info->type);
Jakub Kicinski's avatar
Jakub Kicinski committed
280

281
282
	if (*info->name)
		printf("name %s  ", info->name);
Jakub Kicinski's avatar
Jakub Kicinski committed
283
284

	printf("tag ");
285
	fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
286
	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
287
	printf("%s", info->gpl_compatible ? "  gpl" : "");
288
289
290
	if (info->run_time_ns)
		printf(" run_time_ns %lld run_cnt %lld",
		       info->run_time_ns, info->run_cnt);
Jakub Kicinski's avatar
Jakub Kicinski committed
291
292
	printf("\n");

293
	if (info->load_time) {
Jakub Kicinski's avatar
Jakub Kicinski committed
294
295
		char buf[32];

296
		print_boot_time(info->load_time, buf, sizeof(buf));
Jakub Kicinski's avatar
Jakub Kicinski committed
297
298

		/* Piggy back on load_time, since 0 uid is a valid one */
299
		printf("\tloaded_at %s  uid %u\n", buf, info->created_by_uid);
Jakub Kicinski's avatar
Jakub Kicinski committed
300
301
	}

302
	printf("\txlated %uB", info->xlated_prog_len);
Jakub Kicinski's avatar
Jakub Kicinski committed
303

304
305
	if (info->jited_prog_len)
		printf("  jited %uB", info->jited_prog_len);
Jakub Kicinski's avatar
Jakub Kicinski committed
306
307
308
309
310
311
312
313
	else
		printf("  not jited");

	memlock = get_fdinfo(fd, "memlock");
	if (memlock)
		printf("  memlock %sB", memlock);
	free(memlock);

314
315
	if (info->nr_map_ids)
		show_prog_maps(fd, info->nr_map_ids);
Jakub Kicinski's avatar
Jakub Kicinski committed
316

317
318
319
320
321
	if (!hash_empty(prog_table.table)) {
		struct pinned_obj *obj;

		hash_for_each_possible(prog_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
322
				printf("\n\tpinned %s", obj->path);
323
324
325
		}
	}

326
	if (info->btf_id)
327
		printf("\n\tbtf_id %d", info->btf_id);
328

Jakub Kicinski's avatar
Jakub Kicinski committed
329
	printf("\n");
330
331
332
333
334
335
336
337
338
339
}

static int show_prog(int fd)
{
	struct bpf_prog_info info = {};
	__u32 len = sizeof(info);
	int err;

	err = bpf_obj_get_info_by_fd(fd, &info, &len);
	if (err) {
340
		p_err("can't get prog info: %s", strerror(errno));
341
342
343
344
345
346
347
		return -1;
	}

	if (json_output)
		print_prog_json(&info, fd);
	else
		print_prog_plain(&info, fd);
Jakub Kicinski's avatar
Jakub Kicinski committed
348
349
350
351
352

	return 0;
}

static int do_show(int argc, char **argv)
353
354
{
	__u32 id = 0;
Jakub Kicinski's avatar
Jakub Kicinski committed
355
356
357
	int err;
	int fd;

358
359
	if (show_pinned)
		build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
360

Jakub Kicinski's avatar
Jakub Kicinski committed
361
362
363
364
365
	if (argc == 2) {
		fd = prog_parse_fd(&argc, &argv);
		if (fd < 0)
			return -1;

366
367
368
		err = show_prog(fd);
		close(fd);
		return err;
Jakub Kicinski's avatar
Jakub Kicinski committed
369
370
371
372
373
	}

	if (argc)
		return BAD_ARG();

374
375
	if (json_output)
		jsonw_start_array(json_wtr);
Jakub Kicinski's avatar
Jakub Kicinski committed
376
377
378
	while (true) {
		err = bpf_prog_get_next_id(id, &id);
		if (err) {
379
380
			if (errno == ENOENT) {
				err = 0;
Jakub Kicinski's avatar
Jakub Kicinski committed
381
				break;
382
			}
383
384
			p_err("can't get next program: %s%s", strerror(errno),
			      errno == EINVAL ? " -- kernel too old?" : "");
385
386
			err = -1;
			break;
Jakub Kicinski's avatar
Jakub Kicinski committed
387
388
389
390
		}

		fd = bpf_prog_get_fd_by_id(id);
		if (fd < 0) {
391
392
			if (errno == ENOENT)
				continue;
393
394
			p_err("can't get prog by id (%u): %s",
			      id, strerror(errno));
395
396
			err = -1;
			break;
Jakub Kicinski's avatar
Jakub Kicinski committed
397
398
399
400
401
		}

		err = show_prog(fd);
		close(fd);
		if (err)
402
			break;
Jakub Kicinski's avatar
Jakub Kicinski committed
403
404
	}

405
406
407
408
	if (json_output)
		jsonw_end_array(json_wtr);

	return err;
Jakub Kicinski's avatar
Jakub Kicinski committed
409
410
411
412
}

static int do_dump(int argc, char **argv)
{
413
	struct bpf_prog_info_linear *info_linear;
414
	struct bpf_prog_linfo *prog_linfo = NULL;
415
	enum {DUMP_JITED, DUMP_XLATED} mode;
416
	const char *disasm_opt = NULL;
417
	struct bpf_prog_info *info;
418
	struct dump_data dd = {};
419
	void *func_info = NULL;
420
	struct btf *btf = NULL;
Jakub Kicinski's avatar
Jakub Kicinski committed
421
422
	char *filepath = NULL;
	bool opcodes = false;
423
	bool visual = false;
424
	char func_sig[1024];
Jakub Kicinski's avatar
Jakub Kicinski committed
425
	unsigned char *buf;
426
	bool linum = false;
427
428
	__u32 member_len;
	__u64 arrays;
Jakub Kicinski's avatar
Jakub Kicinski committed
429
430
431
432
	ssize_t n;
	int fd;

	if (is_prefix(*argv, "jited")) {
433
434
		if (disasm_init())
			return -1;
435
		mode = DUMP_JITED;
Jakub Kicinski's avatar
Jakub Kicinski committed
436
	} else if (is_prefix(*argv, "xlated")) {
437
		mode = DUMP_XLATED;
Jakub Kicinski's avatar
Jakub Kicinski committed
438
	} else {
439
		p_err("expected 'xlated' or 'jited', got: %s", *argv);
Jakub Kicinski's avatar
Jakub Kicinski committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
		return -1;
	}
	NEXT_ARG();

	if (argc < 2)
		usage();

	fd = prog_parse_fd(&argc, &argv);
	if (fd < 0)
		return -1;

	if (is_prefix(*argv, "file")) {
		NEXT_ARG();
		if (!argc) {
454
			p_err("expected file path");
Jakub Kicinski's avatar
Jakub Kicinski committed
455
456
457
458
459
460
461
462
			return -1;
		}

		filepath = *argv;
		NEXT_ARG();
	} else if (is_prefix(*argv, "opcodes")) {
		opcodes = true;
		NEXT_ARG();
463
464
465
	} else if (is_prefix(*argv, "visual")) {
		visual = true;
		NEXT_ARG();
466
467
468
	} else if (is_prefix(*argv, "linum")) {
		linum = true;
		NEXT_ARG();
Jakub Kicinski's avatar
Jakub Kicinski committed
469
470
471
472
473
474
475
	}

	if (argc) {
		usage();
		return -1;
	}

476
477
478
479
	if (mode == DUMP_JITED)
		arrays = 1UL << BPF_PROG_INFO_JITED_INSNS;
	else
		arrays = 1UL << BPF_PROG_INFO_XLATED_INSNS;
Jakub Kicinski's avatar
Jakub Kicinski committed
480

481
482
483
484
485
	arrays |= 1UL << BPF_PROG_INFO_JITED_KSYMS;
	arrays |= 1UL << BPF_PROG_INFO_JITED_FUNC_LENS;
	arrays |= 1UL << BPF_PROG_INFO_FUNC_INFO;
	arrays |= 1UL << BPF_PROG_INFO_LINE_INFO;
	arrays |= 1UL << BPF_PROG_INFO_JITED_LINE_INFO;
Jakub Kicinski's avatar
Jakub Kicinski committed
486

487
488
489
490
	info_linear = bpf_program__get_prog_info_linear(fd, arrays);
	close(fd);
	if (IS_ERR_OR_NULL(info_linear)) {
		p_err("can't get prog info: %s", strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
491
492
493
		return -1;
	}

494
495
	info = &info_linear->info;
	if (mode == DUMP_JITED) {
496
		if (info->jited_prog_len == 0 || !info->jited_prog_insns) {
497
			p_info("no instructions returned");
498
499
			goto err_free;
		}
500
501
502
503
504
		buf = (unsigned char *)(info->jited_prog_insns);
		member_len = info->jited_prog_len;
	} else {	/* DUMP_XLATED */
		if (info->xlated_prog_len == 0) {
			p_err("error retrieving insn dump: kernel.kptr_restrict set?");
505
506
			goto err_free;
		}
507
508
		buf = (unsigned char *)info->xlated_prog_insns;
		member_len = info->xlated_prog_len;
509
510
	}

511
	if (info->btf_id && btf__get_from_id(info->btf_id, &btf)) {
512
513
514
515
		p_err("failed to get btf");
		goto err_free;
	}

516
517
518
519
	func_info = (void *)info->func_info;

	if (info->nr_line_info) {
		prog_linfo = bpf_prog_linfo__new(info);
520
		if (!prog_linfo)
521
			p_info("error in processing bpf_line_info.  continue without it.");
522
523
	}

Jakub Kicinski's avatar
Jakub Kicinski committed
524
525
526
	if (filepath) {
		fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
		if (fd < 0) {
527
528
			p_err("can't open file %s: %s", filepath,
			      strerror(errno));
Jakub Kicinski's avatar
Jakub Kicinski committed
529
530
531
			goto err_free;
		}

532
		n = write(fd, buf, member_len);
Jakub Kicinski's avatar
Jakub Kicinski committed
533
		close(fd);
534
		if (n != member_len) {
535
536
			p_err("error writing output file: %s",
			      n < 0 ? strerror(errno) : "short write");
Jakub Kicinski's avatar
Jakub Kicinski committed
537
538
			goto err_free;
		}
539
540
541

		if (json_output)
			jsonw_null(json_wtr);
542
	} else if (mode == DUMP_JITED) {
543
544
		const char *name = NULL;

545
546
547
548
		if (info->ifindex) {
			name = ifindex_to_bfd_params(info->ifindex,
						     info->netns_dev,
						     info->netns_ino,
549
						     &disasm_opt);
550
551
			if (!name)
				goto err_free;
552
		}
553

554
		if (info->nr_jited_func_lens && info->jited_func_lens) {
555
			struct kernel_sym *sym = NULL;
556
			struct bpf_func_info *record;
557
558
559
560
561
			char sym_name[SYM_MAX_NAME];
			unsigned char *img = buf;
			__u64 *ksyms = NULL;
			__u32 *lens;
			__u32 i;
562
			if (info->nr_jited_ksyms) {
563
				kernel_syms_load(&dd);
564
				ksyms = (__u64 *) info->jited_ksyms;
565
566
567
568
569
			}

			if (json_output)
				jsonw_start_array(json_wtr);

570
571
			lens = (__u32 *) info->jited_func_lens;
			for (i = 0; i < info->nr_jited_func_lens; i++) {
572
573
574
575
576
577
578
579
580
581
				if (ksyms) {
					sym = kernel_syms_search(&dd, ksyms[i]);
					if (sym)
						sprintf(sym_name, "%s", sym->name);
					else
						sprintf(sym_name, "0x%016llx", ksyms[i]);
				} else {
					strcpy(sym_name, "unknown");
				}

582
				if (func_info) {
583
					record = func_info + i * info->func_info_rec_size;
584
585
586
587
588
					btf_dumper_type_only(btf, record->type_id,
							     func_sig,
							     sizeof(func_sig));
				}

589
590
				if (json_output) {
					jsonw_start_object(json_wtr);
591
592
593
594
					if (func_info && func_sig[0] != '\0') {
						jsonw_name(json_wtr, "proto");
						jsonw_string(json_wtr, func_sig);
					}
595
596
597
598
					jsonw_name(json_wtr, "name");
					jsonw_string(json_wtr, sym_name);
					jsonw_name(json_wtr, "insns");
				} else {
599
600
					if (func_info && func_sig[0] != '\0')
						printf("%s:\n", func_sig);
601
602
603
					printf("%s:\n", sym_name);
				}

604
605
606
607
608
				disasm_print_insn(img, lens[i], opcodes,
						  name, disasm_opt, btf,
						  prog_linfo, ksyms[i], i,
						  linum);

609
610
611
612
613
614
615
616
617
618
619
				img += lens[i];

				if (json_output)
					jsonw_end_object(json_wtr);
				else
					printf("\n");
			}

			if (json_output)
				jsonw_end_array(json_wtr);
		} else {
620
			disasm_print_insn(buf, member_len, opcodes, name,
621
					  disasm_opt, btf, NULL, 0, 0, false);
622
		}
623
624
625
626
	} else if (visual) {
		if (json_output)
			jsonw_null(json_wtr);
		else
627
			dump_xlated_cfg(buf, member_len);
628
629
	} else {
		kernel_syms_load(&dd);
630
631
		dd.nr_jited_ksyms = info->nr_jited_ksyms;
		dd.jited_ksyms = (__u64 *) info->jited_ksyms;
632
633
		dd.btf = btf;
		dd.func_info = func_info;
634
		dd.finfo_rec_size = info->func_info_rec_size;
635
		dd.prog_linfo = prog_linfo;
636

637
		if (json_output)
638
			dump_xlated_json(&dd, buf, member_len, opcodes,
639
					 linum);
640
		else
641
			dump_xlated_plain(&dd, buf, member_len, opcodes,
642
					  linum);
643
		kernel_syms_destroy(&dd);
Jakub Kicinski's avatar
Jakub Kicinski committed
644
645
	}

646
	free(info_linear);
Jakub Kicinski's avatar
Jakub Kicinski committed
647
648
649
	return 0;

err_free:
650
	free(info_linear);
Jakub Kicinski's avatar
Jakub Kicinski committed
651
652
653
654
655
	return -1;
}

static int do_pin(int argc, char **argv)
{
656
657
658
659
660
661
	int err;

	err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
	if (!err && json_output)
		jsonw_null(json_wtr);
	return err;
Jakub Kicinski's avatar
Jakub Kicinski committed
662
663
}

664
665
666
667
668
669
struct map_replace {
	int idx;
	int fd;
	char *name;
};

670
static int map_replace_compar(const void *p1, const void *p2)
671
672
673
674
675
676
{
	const struct map_replace *a = p1, *b = p2;

	return a->idx - b->idx;
}

677
678
679
static int parse_attach_detach_args(int argc, char **argv, int *progfd,
				    enum bpf_attach_type *attach_type,
				    int *mapfd)
680
{
681
	if (!REQ_ARGS(3))
682
683
		return -EINVAL;

684
685
686
	*progfd = prog_parse_fd(&argc, &argv);
	if (*progfd < 0)
		return *progfd;
687

688
689
690
	*attach_type = parse_attach_type(*argv);
	if (*attach_type == __MAX_BPF_ATTACH_TYPE) {
		p_err("invalid attach/detach type");
691
692
		return -EINVAL;
	}
693
694
695
696
697
698

	if (*attach_type == BPF_FLOW_DISSECTOR) {
		*mapfd = -1;
		return 0;
	}

699
	NEXT_ARG();
700
701
702
703
704
705
706
707
708
	if (!REQ_ARGS(2))
		return -EINVAL;

	*mapfd = map_parse_fd(&argc, &argv);
	if (*mapfd < 0)
		return *mapfd;

	return 0;
}
709

710
711
712
713
714
715
716
717
718
719
static int do_attach(int argc, char **argv)
{
	enum bpf_attach_type attach_type;
	int err, progfd;
	int mapfd;

	err = parse_attach_detach_args(argc, argv,
				       &progfd, &attach_type, &mapfd);
	if (err)
		return err;
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734

	err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
	if (err) {
		p_err("failed prog attach to map");
		return -EINVAL;
	}

	if (json_output)
		jsonw_null(json_wtr);
	return 0;
}

static int do_detach(int argc, char **argv)
{
	enum bpf_attach_type attach_type;
735
736
	int err, progfd;
	int mapfd;
737

738
739
740
741
	err = parse_attach_detach_args(argc, argv,
				       &progfd, &attach_type, &mapfd);
	if (err)
		return err;
742
743
744
745
746
747
748
749
750
751
752

	err = bpf_prog_detach2(progfd, mapfd, attach_type);
	if (err) {
		p_err("failed prog detach from map");
		return -EINVAL;
	}

	if (json_output)
		jsonw_null(json_wtr);
	return 0;
}
753

754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static int check_single_stdin(char *file_data_in, char *file_ctx_in)
{
	if (file_data_in && file_ctx_in &&
	    !strcmp(file_data_in, "-") && !strcmp(file_ctx_in, "-")) {
		p_err("cannot use standard input for both data_in and ctx_in");
		return -1;
	}

	return 0;
}

static int get_run_data(const char *fname, void **data_ptr, unsigned int *size)
{
	size_t block_size = 256;
	size_t buf_size = block_size;
	size_t nb_read = 0;
	void *tmp;
	FILE *f;

	if (!fname) {
		*data_ptr = NULL;
		*size = 0;
		return 0;
	}

	if (!strcmp(fname, "-"))
		f = stdin;
	else
		f = fopen(fname, "r");
	if (!f) {
		p_err("failed to open %s: %s", fname, strerror(errno));
		return -1;
	}

	*data_ptr = malloc(block_size);
	if (!*data_ptr) {
		p_err("failed to allocate memory for data_in/ctx_in: %s",
		      strerror(errno));
		goto err_fclose;
	}

	while ((nb_read += fread(*data_ptr + nb_read, 1, block_size, f))) {
		if (feof(f))
			break;
		if (ferror(f)) {
			p_err("failed to read data_in/ctx_in from %s: %s",
			      fname, strerror(errno));
			goto err_free;
		}
		if (nb_read > buf_size - block_size) {
			if (buf_size == UINT32_MAX) {
				p_err("data_in/ctx_in is too long (max: %d)",
				      UINT32_MAX);
				goto err_free;
			}
			/* No space for fread()-ing next chunk; realloc() */
			buf_size *= 2;
			tmp = realloc(*data_ptr, buf_size);
			if (!tmp) {
				p_err("failed to reallocate data_in/ctx_in: %s",
				      strerror(errno));
				goto err_free;
			}
			*data_ptr = tmp;
		}
	}
	if (f != stdin)
		fclose(f);

	*size = nb_read;
	return 0;

err_free:
	free(*data_ptr);
	*data_ptr = NULL;
err_fclose:
	if (f != stdin)
		fclose(f);
	return -1;
}

static void hex_print(void *data, unsigned int size, FILE *f)
{
	size_t i, j;
	char c;

	for (i = 0; i < size; i += 16) {
		/* Row offset */
		fprintf(f, "%07zx\t", i);

		/* Hexadecimal values */
		for (j = i; j < i + 16 && j < size; j++)
			fprintf(f, "%02x%s", *(uint8_t *)(data + j),
				j % 2 ? " " : "");
		for (; j < i + 16; j++)
			fprintf(f, "  %s", j % 2 ? " " : "");

		/* ASCII values (if relevant), '.' otherwise */
		fprintf(f, "| ");
		for (j = i; j < i + 16 && j < size; j++) {
			c = *(char *)(data + j);
			if (c < ' ' || c > '~')
				c = '.';
			fprintf(f, "%c%s", c, j == i + 7 ? " " : "");
		}

		fprintf(f, "\n");
	}
}

static int
print_run_output(void *data, unsigned int size, const char *fname,
		 const char *json_key)
{
	size_t nb_written;
	FILE *f;

	if (!fname)
		return 0;

	if (!strcmp(fname, "-")) {
		f = stdout;
		if (json_output) {
			jsonw_name(json_wtr, json_key);
			print_data_json(data, size);
		} else {
			hex_print(data, size, f);
		}
		return 0;
	}

	f = fopen(fname, "w");
	if (!f) {
		p_err("failed to open %s: %s", fname, strerror(errno));
		return -1;
	}

	nb_written = fwrite(data, 1, size, f);
	fclose(f);
	if (nb_written != size) {
		p_err("failed to write output data/ctx: %s", strerror(errno));
		return -1;
	}

	return 0;
}

static int alloc_run_data(void **data_ptr, unsigned int size_out)
{
	*data_ptr = calloc(size_out, 1);
	if (!*data_ptr) {
		p_err("failed to allocate memory for output data/ctx: %s",
		      strerror(errno));
		return -1;
	}

	return 0;
}

static int do_run(int argc, char **argv)
{
	char *data_fname_in = NULL, *data_fname_out = NULL;
	char *ctx_fname_in = NULL, *ctx_fname_out = NULL;
	struct bpf_prog_test_run_attr test_attr = {0};
	const unsigned int default_size = SZ_32K;
	void *data_in = NULL, *data_out = NULL;
	void *ctx_in = NULL, *ctx_out = NULL;
	unsigned int repeat = 1;
	int fd, err;

	if (!REQ_ARGS(4))
		return -1;

	fd = prog_parse_fd(&argc, &argv);
	if (fd < 0)
		return -1;

	while (argc) {
		if (detect_common_prefix(*argv, "data_in", "data_out",
					 "data_size_out", NULL))
			return -1;
		if (detect_common_prefix(*argv, "ctx_in", "ctx_out",
					 "ctx_size_out", NULL))
			return -1;

		if (is_prefix(*argv, "data_in")) {
			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			data_fname_in = GET_ARG();
			if (check_single_stdin(data_fname_in, ctx_fname_in))
				return -1;
		} else if (is_prefix(*argv, "data_out")) {
			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			data_fname_out = GET_ARG();
		} else if (is_prefix(*argv, "data_size_out")) {
			char *endptr;

			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			test_attr.data_size_out = strtoul(*argv, &endptr, 0);
			if (*endptr) {
				p_err("can't parse %s as output data size",
				      *argv);
				return -1;
			}
			NEXT_ARG();
		} else if (is_prefix(*argv, "ctx_in")) {
			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			ctx_fname_in = GET_ARG();
			if (check_single_stdin(data_fname_in, ctx_fname_in))
				return -1;
		} else if (is_prefix(*argv, "ctx_out")) {
			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			ctx_fname_out = GET_ARG();
		} else if (is_prefix(*argv, "ctx_size_out")) {
			char *endptr;

			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;

			test_attr.ctx_size_out = strtoul(*argv, &endptr, 0);
			if (*endptr) {
				p_err("can't parse %s as output context size",
				      *argv);
				return -1;
			}
			NEXT_ARG();
		} else if (is_prefix(*argv, "repeat")) {
			char *endptr;

			NEXT_ARG();
			if (!REQ_ARGS(1))
				return -1;
For faster browsing, not all history is shown. View entire blame