xref: /openbmc/linux/tools/bpf/bpftool/main.c (revision 6acb47d1)
1 /*
2  * Copyright (C) 2017-2018 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <bfd.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <linux/bpf.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <bpf.h>
44 
45 #include "main.h"
46 
47 #define BATCH_LINE_LEN_MAX 65536
48 #define BATCH_ARG_NB_MAX 4096
49 
50 const char *bin_name;
51 static int last_argc;
52 static char **last_argv;
53 static int (*last_do_help)(int argc, char **argv);
54 json_writer_t *json_wtr;
55 bool pretty_output;
56 bool json_output;
57 bool show_pinned;
58 int bpf_flags;
59 struct pinned_obj_table prog_table;
60 struct pinned_obj_table map_table;
61 
62 static void __noreturn clean_and_exit(int i)
63 {
64 	if (json_output)
65 		jsonw_destroy(&json_wtr);
66 
67 	exit(i);
68 }
69 
70 void usage(void)
71 {
72 	last_do_help(last_argc - 1, last_argv + 1);
73 
74 	clean_and_exit(-1);
75 }
76 
77 static int do_help(int argc, char **argv)
78 {
79 	if (json_output) {
80 		jsonw_null(json_wtr);
81 		return 0;
82 	}
83 
84 	fprintf(stderr,
85 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
86 		"       %s batch file FILE\n"
87 		"       %s version\n"
88 		"\n"
89 		"       OBJECT := { prog | map | cgroup | perf | net }\n"
90 		"       " HELP_SPEC_OPTIONS "\n"
91 		"",
92 		bin_name, bin_name, bin_name);
93 
94 	return 0;
95 }
96 
97 static int do_version(int argc, char **argv)
98 {
99 	if (json_output) {
100 		jsonw_start_object(json_wtr);
101 		jsonw_name(json_wtr, "version");
102 		jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
103 		jsonw_end_object(json_wtr);
104 	} else {
105 		printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
106 	}
107 	return 0;
108 }
109 
110 int cmd_select(const struct cmd *cmds, int argc, char **argv,
111 	       int (*help)(int argc, char **argv))
112 {
113 	unsigned int i;
114 
115 	last_argc = argc;
116 	last_argv = argv;
117 	last_do_help = help;
118 
119 	if (argc < 1 && cmds[0].func)
120 		return cmds[0].func(argc, argv);
121 
122 	for (i = 0; cmds[i].func; i++)
123 		if (is_prefix(*argv, cmds[i].cmd))
124 			return cmds[i].func(argc - 1, argv + 1);
125 
126 	help(argc - 1, argv + 1);
127 
128 	return -1;
129 }
130 
131 bool is_prefix(const char *pfx, const char *str)
132 {
133 	if (!pfx)
134 		return false;
135 	if (strlen(str) < strlen(pfx))
136 		return false;
137 
138 	return !memcmp(str, pfx, strlen(pfx));
139 }
140 
141 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
142 {
143 	unsigned char *data = arg;
144 	unsigned int i;
145 
146 	for (i = 0; i < n; i++) {
147 		const char *pfx = "";
148 
149 		if (!i)
150 			/* nothing */;
151 		else if (!(i % 16))
152 			fprintf(f, "\n");
153 		else if (!(i % 8))
154 			fprintf(f, "  ");
155 		else
156 			pfx = sep;
157 
158 		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
159 	}
160 }
161 
162 /* Split command line into argument vector. */
163 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
164 {
165 	static const char ws[] = " \t\r\n";
166 	char *cp = line;
167 	int n_argc = 0;
168 
169 	while (*cp) {
170 		/* Skip leading whitespace. */
171 		cp += strspn(cp, ws);
172 
173 		if (*cp == '\0')
174 			break;
175 
176 		if (n_argc >= (maxargs - 1)) {
177 			p_err("too many arguments to command %d", cmd_nb);
178 			return -1;
179 		}
180 
181 		/* Word begins with quote. */
182 		if (*cp == '\'' || *cp == '"') {
183 			char quote = *cp++;
184 
185 			n_argv[n_argc++] = cp;
186 			/* Find ending quote. */
187 			cp = strchr(cp, quote);
188 			if (!cp) {
189 				p_err("unterminated quoted string in command %d",
190 				      cmd_nb);
191 				return -1;
192 			}
193 		} else {
194 			n_argv[n_argc++] = cp;
195 
196 			/* Find end of word. */
197 			cp += strcspn(cp, ws);
198 			if (*cp == '\0')
199 				break;
200 		}
201 
202 		/* Separate words. */
203 		*cp++ = 0;
204 	}
205 	n_argv[n_argc] = NULL;
206 
207 	return n_argc;
208 }
209 
210 static int do_batch(int argc, char **argv);
211 
212 static const struct cmd cmds[] = {
213 	{ "help",	do_help },
214 	{ "batch",	do_batch },
215 	{ "prog",	do_prog },
216 	{ "map",	do_map },
217 	{ "cgroup",	do_cgroup },
218 	{ "perf",	do_perf },
219 	{ "net",	do_net },
220 	{ "version",	do_version },
221 	{ 0 }
222 };
223 
224 static int do_batch(int argc, char **argv)
225 {
226 	char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
227 	char *n_argv[BATCH_ARG_NB_MAX];
228 	unsigned int lines = 0;
229 	int n_argc;
230 	FILE *fp;
231 	char *cp;
232 	int err;
233 	int i;
234 
235 	if (argc < 2) {
236 		p_err("too few parameters for batch");
237 		return -1;
238 	} else if (!is_prefix(*argv, "file")) {
239 		p_err("expected 'file', got: %s", *argv);
240 		return -1;
241 	} else if (argc > 2) {
242 		p_err("too many parameters for batch");
243 		return -1;
244 	}
245 	NEXT_ARG();
246 
247 	if (!strcmp(*argv, "-"))
248 		fp = stdin;
249 	else
250 		fp = fopen(*argv, "r");
251 	if (!fp) {
252 		p_err("Can't open file (%s): %s", *argv, strerror(errno));
253 		return -1;
254 	}
255 
256 	if (json_output)
257 		jsonw_start_array(json_wtr);
258 	while (fgets(buf, sizeof(buf), fp)) {
259 		cp = strchr(buf, '#');
260 		if (cp)
261 			*cp = '\0';
262 
263 		if (strlen(buf) == sizeof(buf) - 1) {
264 			errno = E2BIG;
265 			break;
266 		}
267 
268 		/* Append continuation lines if any (coming after a line ending
269 		 * with '\' in the batch file).
270 		 */
271 		while ((cp = strstr(buf, "\\\n")) != NULL) {
272 			if (!fgets(contline, sizeof(contline), fp) ||
273 			    strlen(contline) == 0) {
274 				p_err("missing continuation line on command %d",
275 				      lines);
276 				err = -1;
277 				goto err_close;
278 			}
279 
280 			cp = strchr(contline, '#');
281 			if (cp)
282 				*cp = '\0';
283 
284 			if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
285 				p_err("command %d is too long", lines);
286 				err = -1;
287 				goto err_close;
288 			}
289 			buf[strlen(buf) - 2] = '\0';
290 			strcat(buf, contline);
291 		}
292 
293 		n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
294 		if (!n_argc)
295 			continue;
296 		if (n_argc < 0)
297 			goto err_close;
298 
299 		if (json_output) {
300 			jsonw_start_object(json_wtr);
301 			jsonw_name(json_wtr, "command");
302 			jsonw_start_array(json_wtr);
303 			for (i = 0; i < n_argc; i++)
304 				jsonw_string(json_wtr, n_argv[i]);
305 			jsonw_end_array(json_wtr);
306 			jsonw_name(json_wtr, "output");
307 		}
308 
309 		err = cmd_select(cmds, n_argc, n_argv, do_help);
310 
311 		if (json_output)
312 			jsonw_end_object(json_wtr);
313 
314 		if (err)
315 			goto err_close;
316 
317 		lines++;
318 	}
319 
320 	if (errno && errno != ENOENT) {
321 		p_err("reading batch file failed: %s", strerror(errno));
322 		err = -1;
323 	} else {
324 		if (!json_output)
325 			printf("processed %d commands\n", lines);
326 		err = 0;
327 	}
328 err_close:
329 	if (fp != stdin)
330 		fclose(fp);
331 
332 	if (json_output)
333 		jsonw_end_array(json_wtr);
334 
335 	return err;
336 }
337 
338 int main(int argc, char **argv)
339 {
340 	static const struct option options[] = {
341 		{ "json",	no_argument,	NULL,	'j' },
342 		{ "help",	no_argument,	NULL,	'h' },
343 		{ "pretty",	no_argument,	NULL,	'p' },
344 		{ "version",	no_argument,	NULL,	'V' },
345 		{ "bpffs",	no_argument,	NULL,	'f' },
346 		{ "mapcompat",	no_argument,	NULL,	'm' },
347 		{ 0 }
348 	};
349 	int opt, ret;
350 
351 	last_do_help = do_help;
352 	pretty_output = false;
353 	json_output = false;
354 	show_pinned = false;
355 	bin_name = argv[0];
356 
357 	hash_init(prog_table.table);
358 	hash_init(map_table.table);
359 
360 	opterr = 0;
361 	while ((opt = getopt_long(argc, argv, "Vhpjfm",
362 				  options, NULL)) >= 0) {
363 		switch (opt) {
364 		case 'V':
365 			return do_version(argc, argv);
366 		case 'h':
367 			return do_help(argc, argv);
368 		case 'p':
369 			pretty_output = true;
370 			/* fall through */
371 		case 'j':
372 			if (!json_output) {
373 				json_wtr = jsonw_new(stdout);
374 				if (!json_wtr) {
375 					p_err("failed to create JSON writer");
376 					return -1;
377 				}
378 				json_output = true;
379 			}
380 			jsonw_pretty(json_wtr, pretty_output);
381 			break;
382 		case 'f':
383 			show_pinned = true;
384 			break;
385 		case 'm':
386 			bpf_flags = MAPS_RELAX_COMPAT;
387 			break;
388 		default:
389 			p_err("unrecognized option '%s'", argv[optind - 1]);
390 			if (json_output)
391 				clean_and_exit(-1);
392 			else
393 				usage();
394 		}
395 	}
396 
397 	argc -= optind;
398 	argv += optind;
399 	if (argc < 0)
400 		usage();
401 
402 	bfd_init();
403 
404 	ret = cmd_select(cmds, argc, argv, do_help);
405 
406 	if (json_output)
407 		jsonw_destroy(&json_wtr);
408 
409 	if (show_pinned) {
410 		delete_pinned_obj_table(&prog_table);
411 		delete_pinned_obj_table(&map_table);
412 	}
413 
414 	return ret;
415 }
416