1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <ctype.h>
5 #include <errno.h>
6 #include <getopt.h>
7 #include <linux/bpf.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <bpf/bpf.h>
13 #include <bpf/libbpf.h>
14 
15 #include "main.h"
16 
17 #define BATCH_LINE_LEN_MAX 65536
18 #define BATCH_ARG_NB_MAX 4096
19 
20 const char *bin_name;
21 static int last_argc;
22 static char **last_argv;
23 static int (*last_do_help)(int argc, char **argv);
24 json_writer_t *json_wtr;
25 bool pretty_output;
26 bool json_output;
27 bool show_pinned;
28 bool block_mount;
29 bool verifier_logs;
30 bool relaxed_maps;
31 struct pinned_obj_table prog_table;
32 struct pinned_obj_table map_table;
33 struct pinned_obj_table link_table;
34 struct obj_refs_table refs_table;
35 
clean_and_exit(int i)36 static void __noreturn clean_and_exit(int i)
37 {
38 	if (json_output)
39 		jsonw_destroy(&json_wtr);
40 
41 	exit(i);
42 }
43 
usage(void)44 void usage(void)
45 {
46 	last_do_help(last_argc - 1, last_argv + 1);
47 
48 	clean_and_exit(-1);
49 }
50 
do_help(int argc,char ** argv)51 static int do_help(int argc, char **argv)
52 {
53 	if (json_output) {
54 		jsonw_null(json_wtr);
55 		return 0;
56 	}
57 
58 	fprintf(stderr,
59 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
60 		"       %s batch file FILE\n"
61 		"       %s version\n"
62 		"\n"
63 		"       OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
64 		"       " HELP_SPEC_OPTIONS "\n"
65 		"",
66 		bin_name, bin_name, bin_name);
67 
68 	return 0;
69 }
70 
do_version(int argc,char ** argv)71 static int do_version(int argc, char **argv)
72 {
73 #ifdef HAVE_LIBBFD_SUPPORT
74 	const bool has_libbfd = true;
75 #else
76 	const bool has_libbfd = false;
77 #endif
78 #ifdef BPFTOOL_WITHOUT_SKELETONS
79 	const bool has_skeletons = false;
80 #else
81 	const bool has_skeletons = true;
82 #endif
83 
84 	if (json_output) {
85 		jsonw_start_object(json_wtr);	/* root object */
86 
87 		jsonw_name(json_wtr, "version");
88 		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
89 
90 		jsonw_name(json_wtr, "features");
91 		jsonw_start_object(json_wtr);	/* features */
92 		jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
93 		jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
94 		jsonw_end_object(json_wtr);	/* features */
95 
96 		jsonw_end_object(json_wtr);	/* root object */
97 	} else {
98 		unsigned int nb_features = 0;
99 
100 		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
101 		printf("features:");
102 		if (has_libbfd) {
103 			printf(" libbfd");
104 			nb_features++;
105 		}
106 		if (has_skeletons)
107 			printf("%s skeletons", nb_features++ ? "," : "");
108 		printf("\n");
109 	}
110 	return 0;
111 }
112 
cmd_select(const struct cmd * cmds,int argc,char ** argv,int (* help)(int argc,char ** argv))113 int cmd_select(const struct cmd *cmds, int argc, char **argv,
114 	       int (*help)(int argc, char **argv))
115 {
116 	unsigned int i;
117 
118 	last_argc = argc;
119 	last_argv = argv;
120 	last_do_help = help;
121 
122 	if (argc < 1 && cmds[0].func)
123 		return cmds[0].func(argc, argv);
124 
125 	for (i = 0; cmds[i].cmd; i++) {
126 		if (is_prefix(*argv, cmds[i].cmd)) {
127 			if (!cmds[i].func) {
128 				p_err("command '%s' is not supported in bootstrap mode",
129 				      cmds[i].cmd);
130 				return -1;
131 			}
132 			return cmds[i].func(argc - 1, argv + 1);
133 		}
134 	}
135 
136 	help(argc - 1, argv + 1);
137 
138 	return -1;
139 }
140 
is_prefix(const char * pfx,const char * str)141 bool is_prefix(const char *pfx, const char *str)
142 {
143 	if (!pfx)
144 		return false;
145 	if (strlen(str) < strlen(pfx))
146 		return false;
147 
148 	return !memcmp(str, pfx, strlen(pfx));
149 }
150 
151 /* Last argument MUST be NULL pointer */
detect_common_prefix(const char * arg,...)152 int detect_common_prefix(const char *arg, ...)
153 {
154 	unsigned int count = 0;
155 	const char *ref;
156 	char msg[256];
157 	va_list ap;
158 
159 	snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
160 	va_start(ap, arg);
161 	while ((ref = va_arg(ap, const char *))) {
162 		if (!is_prefix(arg, ref))
163 			continue;
164 		count++;
165 		if (count > 1)
166 			strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
167 		strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
168 	}
169 	va_end(ap);
170 	strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
171 
172 	if (count >= 2) {
173 		p_err("%s", msg);
174 		return -1;
175 	}
176 
177 	return 0;
178 }
179 
fprint_hex(FILE * f,void * arg,unsigned int n,const char * sep)180 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
181 {
182 	unsigned char *data = arg;
183 	unsigned int i;
184 
185 	for (i = 0; i < n; i++) {
186 		const char *pfx = "";
187 
188 		if (!i)
189 			/* nothing */;
190 		else if (!(i % 16))
191 			fprintf(f, "\n");
192 		else if (!(i % 8))
193 			fprintf(f, "  ");
194 		else
195 			pfx = sep;
196 
197 		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
198 	}
199 }
200 
201 /* Split command line into argument vector. */
make_args(char * line,char * n_argv[],int maxargs,int cmd_nb)202 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
203 {
204 	static const char ws[] = " \t\r\n";
205 	char *cp = line;
206 	int n_argc = 0;
207 
208 	while (*cp) {
209 		/* Skip leading whitespace. */
210 		cp += strspn(cp, ws);
211 
212 		if (*cp == '\0')
213 			break;
214 
215 		if (n_argc >= (maxargs - 1)) {
216 			p_err("too many arguments to command %d", cmd_nb);
217 			return -1;
218 		}
219 
220 		/* Word begins with quote. */
221 		if (*cp == '\'' || *cp == '"') {
222 			char quote = *cp++;
223 
224 			n_argv[n_argc++] = cp;
225 			/* Find ending quote. */
226 			cp = strchr(cp, quote);
227 			if (!cp) {
228 				p_err("unterminated quoted string in command %d",
229 				      cmd_nb);
230 				return -1;
231 			}
232 		} else {
233 			n_argv[n_argc++] = cp;
234 
235 			/* Find end of word. */
236 			cp += strcspn(cp, ws);
237 			if (*cp == '\0')
238 				break;
239 		}
240 
241 		/* Separate words. */
242 		*cp++ = 0;
243 	}
244 	n_argv[n_argc] = NULL;
245 
246 	return n_argc;
247 }
248 
249 static int do_batch(int argc, char **argv);
250 
251 static const struct cmd cmds[] = {
252 	{ "help",	do_help },
253 	{ "batch",	do_batch },
254 	{ "prog",	do_prog },
255 	{ "map",	do_map },
256 	{ "link",	do_link },
257 	{ "cgroup",	do_cgroup },
258 	{ "perf",	do_perf },
259 	{ "net",	do_net },
260 	{ "feature",	do_feature },
261 	{ "btf",	do_btf },
262 	{ "gen",	do_gen },
263 	{ "struct_ops",	do_struct_ops },
264 	{ "iter",	do_iter },
265 	{ "version",	do_version },
266 	{ 0 }
267 };
268 
do_batch(int argc,char ** argv)269 static int do_batch(int argc, char **argv)
270 {
271 	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
272 	char *n_argv[BATCH_ARG_NB_MAX];
273 	unsigned int lines = 0;
274 	int n_argc;
275 	FILE *fp;
276 	char *cp;
277 	int err;
278 	int i;
279 
280 	if (argc < 2) {
281 		p_err("too few parameters for batch");
282 		return -1;
283 	} else if (!is_prefix(*argv, "file")) {
284 		p_err("expected 'file', got: %s", *argv);
285 		return -1;
286 	} else if (argc > 2) {
287 		p_err("too many parameters for batch");
288 		return -1;
289 	}
290 	NEXT_ARG();
291 
292 	if (!strcmp(*argv, "-"))
293 		fp = stdin;
294 	else
295 		fp = fopen(*argv, "r");
296 	if (!fp) {
297 		p_err("Can't open file (%s): %s", *argv, strerror(errno));
298 		return -1;
299 	}
300 
301 	if (json_output)
302 		jsonw_start_array(json_wtr);
303 	while (fgets(buf, sizeof(buf), fp)) {
304 		cp = strchr(buf, '#');
305 		if (cp)
306 			*cp = '\0';
307 
308 		if (strlen(buf) == sizeof(buf) - 1) {
309 			errno = E2BIG;
310 			break;
311 		}
312 
313 		/* Append continuation lines if any (coming after a line ending
314 		 * with '\' in the batch file).
315 		 */
316 		while ((cp = strstr(buf, "\\\n")) != NULL) {
317 			if (!fgets(contline, sizeof(contline), fp) ||
318 			    strlen(contline) == 0) {
319 				p_err("missing continuation line on command %d",
320 				      lines);
321 				err = -1;
322 				goto err_close;
323 			}
324 
325 			cp = strchr(contline, '#');
326 			if (cp)
327 				*cp = '\0';
328 
329 			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
330 				p_err("command %d is too long", lines);
331 				err = -1;
332 				goto err_close;
333 			}
334 			buf[strlen(buf) - 2] = '\0';
335 			strcat(buf, contline);
336 		}
337 
338 		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
339 		if (!n_argc)
340 			continue;
341 		if (n_argc < 0)
342 			goto err_close;
343 
344 		if (json_output) {
345 			jsonw_start_object(json_wtr);
346 			jsonw_name(json_wtr, "command");
347 			jsonw_start_array(json_wtr);
348 			for (i = 0; i < n_argc; i++)
349 				jsonw_string(json_wtr, n_argv[i]);
350 			jsonw_end_array(json_wtr);
351 			jsonw_name(json_wtr, "output");
352 		}
353 
354 		err = cmd_select(cmds, n_argc, n_argv, do_help);
355 
356 		if (json_output)
357 			jsonw_end_object(json_wtr);
358 
359 		if (err)
360 			goto err_close;
361 
362 		lines++;
363 	}
364 
365 	if (errno && errno != ENOENT) {
366 		p_err("reading batch file failed: %s", strerror(errno));
367 		err = -1;
368 	} else {
369 		if (!json_output)
370 			printf("processed %d commands\n", lines);
371 		err = 0;
372 	}
373 err_close:
374 	if (fp != stdin)
375 		fclose(fp);
376 
377 	if (json_output)
378 		jsonw_end_array(json_wtr);
379 
380 	return err;
381 }
382 
main(int argc,char ** argv)383 int main(int argc, char **argv)
384 {
385 	static const struct option options[] = {
386 		{ "json",	no_argument,	NULL,	'j' },
387 		{ "help",	no_argument,	NULL,	'h' },
388 		{ "pretty",	no_argument,	NULL,	'p' },
389 		{ "version",	no_argument,	NULL,	'V' },
390 		{ "bpffs",	no_argument,	NULL,	'f' },
391 		{ "mapcompat",	no_argument,	NULL,	'm' },
392 		{ "nomount",	no_argument,	NULL,	'n' },
393 		{ "debug",	no_argument,	NULL,	'd' },
394 		{ 0 }
395 	};
396 	int opt, ret;
397 
398 	last_do_help = do_help;
399 	pretty_output = false;
400 	json_output = false;
401 	show_pinned = false;
402 	block_mount = false;
403 	bin_name = argv[0];
404 
405 	hash_init(prog_table.table);
406 	hash_init(map_table.table);
407 	hash_init(link_table.table);
408 
409 	opterr = 0;
410 	while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
411 				  options, NULL)) >= 0) {
412 		switch (opt) {
413 		case 'V':
414 			return do_version(argc, argv);
415 		case 'h':
416 			return do_help(argc, argv);
417 		case 'p':
418 			pretty_output = true;
419 			/* fall through */
420 		case 'j':
421 			if (!json_output) {
422 				json_wtr = jsonw_new(stdout);
423 				if (!json_wtr) {
424 					p_err("failed to create JSON writer");
425 					return -1;
426 				}
427 				json_output = true;
428 			}
429 			jsonw_pretty(json_wtr, pretty_output);
430 			break;
431 		case 'f':
432 			show_pinned = true;
433 			break;
434 		case 'm':
435 			relaxed_maps = true;
436 			break;
437 		case 'n':
438 			block_mount = true;
439 			break;
440 		case 'd':
441 			libbpf_set_print(print_all_levels);
442 			verifier_logs = true;
443 			break;
444 		default:
445 			p_err("unrecognized option '%s'", argv[optind - 1]);
446 			if (json_output)
447 				clean_and_exit(-1);
448 			else
449 				usage();
450 		}
451 	}
452 
453 	argc -= optind;
454 	argv += optind;
455 	if (argc < 0)
456 		usage();
457 
458 	ret = cmd_select(cmds, argc, argv, do_help);
459 
460 	if (json_output)
461 		jsonw_destroy(&json_wtr);
462 
463 	if (show_pinned) {
464 		delete_pinned_obj_table(&prog_table);
465 		delete_pinned_obj_table(&map_table);
466 		delete_pinned_obj_table(&link_table);
467 	}
468 
469 	return ret;
470 }
471