main.c 8.4 KB
Newer Older
1 2
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
J
Jakub Kicinski 已提交
3 4 5

#include <ctype.h>
#include <errno.h>
6
#include <getopt.h>
J
Jakub Kicinski 已提交
7 8 9 10 11
#include <linux/bpf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

12 13
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
J
Jakub Kicinski 已提交
14 15 16

#include "main.h"

17 18 19
#define BATCH_LINE_LEN_MAX 65536
#define BATCH_ARG_NB_MAX 4096

J
Jakub Kicinski 已提交
20 21 22 23
const char *bin_name;
static int last_argc;
static char **last_argv;
static int (*last_do_help)(int argc, char **argv);
24 25 26
json_writer_t *json_wtr;
bool pretty_output;
bool json_output;
27
bool show_pinned;
28
bool block_mount;
29
bool verifier_logs;
30
bool relaxed_maps;
31 32
struct pinned_obj_table prog_table;
struct pinned_obj_table map_table;
33
struct pinned_obj_table link_table;
J
Jakub Kicinski 已提交
34

35 36 37 38 39 40 41 42
static void __noreturn clean_and_exit(int i)
{
	if (json_output)
		jsonw_destroy(&json_wtr);

	exit(i);
}

J
Jakub Kicinski 已提交
43 44 45 46
void usage(void)
{
	last_do_help(last_argc - 1, last_argv + 1);

47
	clean_and_exit(-1);
J
Jakub Kicinski 已提交
48 49 50 51
}

static int do_help(int argc, char **argv)
{
52 53 54 55 56
	if (json_output) {
		jsonw_null(json_wtr);
		return 0;
	}

J
Jakub Kicinski 已提交
57
	fprintf(stderr,
58
		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
J
Jakub Kicinski 已提交
59
		"       %s batch file FILE\n"
60
		"       %s version\n"
J
Jakub Kicinski 已提交
61
		"\n"
62
		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops }\n"
63 64
		"       " HELP_SPEC_OPTIONS "\n"
		"",
65
		bin_name, bin_name, bin_name);
J
Jakub Kicinski 已提交
66 67 68 69

	return 0;
}

70 71
static int do_version(int argc, char **argv)
{
72 73 74
	if (json_output) {
		jsonw_start_object(json_wtr);
		jsonw_name(json_wtr, "version");
75
		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
76 77
		jsonw_end_object(json_wtr);
	} else {
78
		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
79
	}
80 81 82
	return 0;
}

J
Jakub Kicinski 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
int cmd_select(const struct cmd *cmds, int argc, char **argv,
	       int (*help)(int argc, char **argv))
{
	unsigned int i;

	last_argc = argc;
	last_argv = argv;
	last_do_help = help;

	if (argc < 1 && cmds[0].func)
		return cmds[0].func(argc, argv);

	for (i = 0; cmds[i].func; i++)
		if (is_prefix(*argv, cmds[i].cmd))
			return cmds[i].func(argc - 1, argv + 1);

	help(argc - 1, argv + 1);

	return -1;
}

bool is_prefix(const char *pfx, const char *str)
{
	if (!pfx)
		return false;
	if (strlen(str) < strlen(pfx))
		return false;

	return !memcmp(str, pfx, strlen(pfx));
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* Last argument MUST be NULL pointer */
int detect_common_prefix(const char *arg, ...)
{
	unsigned int count = 0;
	const char *ref;
	char msg[256];
	va_list ap;

	snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
	va_start(ap, arg);
	while ((ref = va_arg(ap, const char *))) {
		if (!is_prefix(arg, ref))
			continue;
		count++;
		if (count > 1)
			strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
		strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
	}
	va_end(ap);
	strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);

	if (count >= 2) {
136
		p_err("%s", msg);
137 138 139 140 141 142
		return -1;
	}

	return 0;
}

143
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
J
Jakub Kicinski 已提交
144 145 146 147 148 149 150 151 152 153
{
	unsigned char *data = arg;
	unsigned int i;

	for (i = 0; i < n; i++) {
		const char *pfx = "";

		if (!i)
			/* nothing */;
		else if (!(i % 16))
154
			fprintf(f, "\n");
J
Jakub Kicinski 已提交
155
		else if (!(i % 8))
156
			fprintf(f, "  ");
J
Jakub Kicinski 已提交
157 158 159
		else
			pfx = sep;

160
		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
J
Jakub Kicinski 已提交
161 162 163
	}
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
/* Split command line into argument vector. */
static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
{
	static const char ws[] = " \t\r\n";
	char *cp = line;
	int n_argc = 0;

	while (*cp) {
		/* Skip leading whitespace. */
		cp += strspn(cp, ws);

		if (*cp == '\0')
			break;

		if (n_argc >= (maxargs - 1)) {
			p_err("too many arguments to command %d", cmd_nb);
			return -1;
		}

		/* Word begins with quote. */
		if (*cp == '\'' || *cp == '"') {
			char quote = *cp++;

			n_argv[n_argc++] = cp;
			/* Find ending quote. */
			cp = strchr(cp, quote);
			if (!cp) {
				p_err("unterminated quoted string in command %d",
				      cmd_nb);
				return -1;
			}
		} else {
			n_argv[n_argc++] = cp;

			/* Find end of word. */
			cp += strcspn(cp, ws);
			if (*cp == '\0')
				break;
		}

		/* Separate words. */
		*cp++ = 0;
	}
	n_argv[n_argc] = NULL;

	return n_argc;
}

J
Jakub Kicinski 已提交
212 213 214 215 216 217 218
static int do_batch(int argc, char **argv);

static const struct cmd cmds[] = {
	{ "help",	do_help },
	{ "batch",	do_batch },
	{ "prog",	do_prog },
	{ "map",	do_map },
219
	{ "link",	do_link },
220
	{ "cgroup",	do_cgroup },
221
	{ "perf",	do_perf },
222
	{ "net",	do_net },
223
	{ "feature",	do_feature },
224
	{ "btf",	do_btf },
225
	{ "gen",	do_gen },
226
	{ "struct_ops",	do_struct_ops },
227
	{ "version",	do_version },
J
Jakub Kicinski 已提交
228 229 230 231 232
	{ 0 }
};

static int do_batch(int argc, char **argv)
{
233 234
	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
	char *n_argv[BATCH_ARG_NB_MAX];
J
Jakub Kicinski 已提交
235 236 237
	unsigned int lines = 0;
	int n_argc;
	FILE *fp;
238
	char *cp;
J
Jakub Kicinski 已提交
239
	int err;
240
	int i;
J
Jakub Kicinski 已提交
241 242

	if (argc < 2) {
243
		p_err("too few parameters for batch");
J
Jakub Kicinski 已提交
244 245
		return -1;
	} else if (!is_prefix(*argv, "file")) {
246
		p_err("expected 'file', got: %s", *argv);
J
Jakub Kicinski 已提交
247 248
		return -1;
	} else if (argc > 2) {
249
		p_err("too many parameters for batch");
J
Jakub Kicinski 已提交
250 251 252 253
		return -1;
	}
	NEXT_ARG();

254 255 256 257
	if (!strcmp(*argv, "-"))
		fp = stdin;
	else
		fp = fopen(*argv, "r");
J
Jakub Kicinski 已提交
258
	if (!fp) {
259
		p_err("Can't open file (%s): %s", *argv, strerror(errno));
J
Jakub Kicinski 已提交
260 261 262
		return -1;
	}

263 264
	if (json_output)
		jsonw_start_array(json_wtr);
J
Jakub Kicinski 已提交
265
	while (fgets(buf, sizeof(buf), fp)) {
266 267 268 269
		cp = strchr(buf, '#');
		if (cp)
			*cp = '\0';

J
Jakub Kicinski 已提交
270 271 272 273 274
		if (strlen(buf) == sizeof(buf) - 1) {
			errno = E2BIG;
			break;
		}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		/* Append continuation lines if any (coming after a line ending
		 * with '\' in the batch file).
		 */
		while ((cp = strstr(buf, "\\\n")) != NULL) {
			if (!fgets(contline, sizeof(contline), fp) ||
			    strlen(contline) == 0) {
				p_err("missing continuation line on command %d",
				      lines);
				err = -1;
				goto err_close;
			}

			cp = strchr(contline, '#');
			if (cp)
				*cp = '\0';

			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
				p_err("command %d is too long", lines);
				err = -1;
				goto err_close;
			}
			buf[strlen(buf) - 2] = '\0';
			strcat(buf, contline);
		}

300
		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
J
Jakub Kicinski 已提交
301 302
		if (!n_argc)
			continue;
303 304
		if (n_argc < 0)
			goto err_close;
J
Jakub Kicinski 已提交
305

306 307 308 309 310 311 312 313 314 315
		if (json_output) {
			jsonw_start_object(json_wtr);
			jsonw_name(json_wtr, "command");
			jsonw_start_array(json_wtr);
			for (i = 0; i < n_argc; i++)
				jsonw_string(json_wtr, n_argv[i]);
			jsonw_end_array(json_wtr);
			jsonw_name(json_wtr, "output");
		}

J
Jakub Kicinski 已提交
316
		err = cmd_select(cmds, n_argc, n_argv, do_help);
317 318 319 320

		if (json_output)
			jsonw_end_object(json_wtr);

J
Jakub Kicinski 已提交
321 322 323 324 325 326 327
		if (err)
			goto err_close;

		lines++;
	}

	if (errno && errno != ENOENT) {
328
		p_err("reading batch file failed: %s", strerror(errno));
J
Jakub Kicinski 已提交
329 330
		err = -1;
	} else {
331 332
		if (!json_output)
			printf("processed %d commands\n", lines);
J
Jakub Kicinski 已提交
333 334 335
		err = 0;
	}
err_close:
336 337
	if (fp != stdin)
		fclose(fp);
J
Jakub Kicinski 已提交
338

339 340 341
	if (json_output)
		jsonw_end_array(json_wtr);

J
Jakub Kicinski 已提交
342 343 344 345 346
	return err;
}

int main(int argc, char **argv)
{
347
	static const struct option options[] = {
348
		{ "json",	no_argument,	NULL,	'j' },
349
		{ "help",	no_argument,	NULL,	'h' },
350
		{ "pretty",	no_argument,	NULL,	'p' },
351
		{ "version",	no_argument,	NULL,	'V' },
352
		{ "bpffs",	no_argument,	NULL,	'f' },
353
		{ "mapcompat",	no_argument,	NULL,	'm' },
354
		{ "nomount",	no_argument,	NULL,	'n' },
355
		{ "debug",	no_argument,	NULL,	'd' },
356 357
		{ 0 }
	};
358
	int opt, ret;
359 360

	last_do_help = do_help;
361 362
	pretty_output = false;
	json_output = false;
363
	show_pinned = false;
364
	block_mount = false;
J
Jakub Kicinski 已提交
365
	bin_name = argv[0];
366

367 368
	hash_init(prog_table.table);
	hash_init(map_table.table);
369
	hash_init(link_table.table);
370

371
	opterr = 0;
372
	while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
373 374 375 376 377 378
				  options, NULL)) >= 0) {
		switch (opt) {
		case 'V':
			return do_version(argc, argv);
		case 'h':
			return do_help(argc, argv);
379 380 381 382
		case 'p':
			pretty_output = true;
			/* fall through */
		case 'j':
383 384 385 386 387 388 389 390 391
			if (!json_output) {
				json_wtr = jsonw_new(stdout);
				if (!json_wtr) {
					p_err("failed to create JSON writer");
					return -1;
				}
				json_output = true;
			}
			jsonw_pretty(json_wtr, pretty_output);
392
			break;
393 394 395
		case 'f':
			show_pinned = true;
			break;
396
		case 'm':
397
			relaxed_maps = true;
398
			break;
399 400 401
		case 'n':
			block_mount = true;
			break;
402 403
		case 'd':
			libbpf_set_print(print_all_levels);
404
			verifier_logs = true;
405
			break;
406
		default:
407 408 409 410 411
			p_err("unrecognized option '%s'", argv[optind - 1]);
			if (json_output)
				clean_and_exit(-1);
			else
				usage();
412 413 414 415 416 417 418
		}
	}

	argc -= optind;
	argv += optind;
	if (argc < 0)
		usage();
J
Jakub Kicinski 已提交
419

420 421 422 423 424
	ret = cmd_select(cmds, argc, argv, do_help);

	if (json_output)
		jsonw_destroy(&json_wtr);

425 426 427
	if (show_pinned) {
		delete_pinned_obj_table(&prog_table);
		delete_pinned_obj_table(&map_table);
428
		delete_pinned_obj_table(&link_table);
429
	}
430

431
	return ret;
J
Jakub Kicinski 已提交
432
}