xref: /linux/tools/bpf/bpftool/main.c (revision e60e1ee60630cafef5e430c2ae364877e061d980)
1 /*
2  * Copyright (C) 2017 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 /* Author: Jakub Kicinski <kubakici@wp.pl> */
35 
36 #include <bfd.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <getopt.h>
40 #include <linux/bpf.h>
41 #include <linux/version.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include <bpf.h>
47 
48 #include "main.h"
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 struct pinned_obj_table prog_table;
59 struct pinned_obj_table map_table;
60 
61 void usage(void)
62 {
63 	last_do_help(last_argc - 1, last_argv + 1);
64 
65 	exit(-1);
66 }
67 
68 static int do_help(int argc, char **argv)
69 {
70 	if (json_output) {
71 		jsonw_null(json_wtr);
72 		return 0;
73 	}
74 
75 	fprintf(stderr,
76 		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
77 		"       %s batch file FILE\n"
78 		"       %s version\n"
79 		"\n"
80 		"       OBJECT := { prog | map }\n"
81 		"       " HELP_SPEC_OPTIONS "\n"
82 		"",
83 		bin_name, bin_name, bin_name);
84 
85 	return 0;
86 }
87 
88 static int do_version(int argc, char **argv)
89 {
90 	unsigned int version[3];
91 
92 	version[0] = LINUX_VERSION_CODE >> 16;
93 	version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
94 	version[2] = LINUX_VERSION_CODE & 0xf;
95 
96 	if (json_output) {
97 		jsonw_start_object(json_wtr);
98 		jsonw_name(json_wtr, "version");
99 		jsonw_printf(json_wtr, "\"%u.%u.%u\"",
100 			     version[0], version[1], version[2]);
101 		jsonw_end_object(json_wtr);
102 	} else {
103 		printf("%s v%u.%u.%u\n", bin_name,
104 		       version[0], version[1], version[2]);
105 	}
106 	return 0;
107 }
108 
109 int cmd_select(const struct cmd *cmds, int argc, char **argv,
110 	       int (*help)(int argc, char **argv))
111 {
112 	unsigned int i;
113 
114 	last_argc = argc;
115 	last_argv = argv;
116 	last_do_help = help;
117 
118 	if (argc < 1 && cmds[0].func)
119 		return cmds[0].func(argc, argv);
120 
121 	for (i = 0; cmds[i].func; i++)
122 		if (is_prefix(*argv, cmds[i].cmd))
123 			return cmds[i].func(argc - 1, argv + 1);
124 
125 	help(argc - 1, argv + 1);
126 
127 	return -1;
128 }
129 
130 bool is_prefix(const char *pfx, const char *str)
131 {
132 	if (!pfx)
133 		return false;
134 	if (strlen(str) < strlen(pfx))
135 		return false;
136 
137 	return !memcmp(str, pfx, strlen(pfx));
138 }
139 
140 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
141 {
142 	unsigned char *data = arg;
143 	unsigned int i;
144 
145 	for (i = 0; i < n; i++) {
146 		const char *pfx = "";
147 
148 		if (!i)
149 			/* nothing */;
150 		else if (!(i % 16))
151 			fprintf(f, "\n");
152 		else if (!(i % 8))
153 			fprintf(f, "  ");
154 		else
155 			pfx = sep;
156 
157 		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
158 	}
159 }
160 
161 static int do_batch(int argc, char **argv);
162 
163 static const struct cmd cmds[] = {
164 	{ "help",	do_help },
165 	{ "batch",	do_batch },
166 	{ "prog",	do_prog },
167 	{ "map",	do_map },
168 	{ "version",	do_version },
169 	{ 0 }
170 };
171 
172 static int do_batch(int argc, char **argv)
173 {
174 	unsigned int lines = 0;
175 	char *n_argv[4096];
176 	char buf[65536];
177 	int n_argc;
178 	FILE *fp;
179 	int err;
180 	int i;
181 
182 	if (argc < 2) {
183 		p_err("too few parameters for batch");
184 		return -1;
185 	} else if (!is_prefix(*argv, "file")) {
186 		p_err("expected 'file', got: %s", *argv);
187 		return -1;
188 	} else if (argc > 2) {
189 		p_err("too many parameters for batch");
190 		return -1;
191 	}
192 	NEXT_ARG();
193 
194 	fp = fopen(*argv, "r");
195 	if (!fp) {
196 		p_err("Can't open file (%s): %s", *argv, strerror(errno));
197 		return -1;
198 	}
199 
200 	if (json_output)
201 		jsonw_start_array(json_wtr);
202 	while (fgets(buf, sizeof(buf), fp)) {
203 		if (strlen(buf) == sizeof(buf) - 1) {
204 			errno = E2BIG;
205 			break;
206 		}
207 
208 		n_argc = 0;
209 		n_argv[n_argc] = strtok(buf, " \t\n");
210 
211 		while (n_argv[n_argc]) {
212 			n_argc++;
213 			if (n_argc == ARRAY_SIZE(n_argv)) {
214 				p_err("line %d has too many arguments, skip",
215 				      lines);
216 				n_argc = 0;
217 				break;
218 			}
219 			n_argv[n_argc] = strtok(NULL, " \t\n");
220 		}
221 
222 		if (!n_argc)
223 			continue;
224 
225 		if (json_output) {
226 			jsonw_start_object(json_wtr);
227 			jsonw_name(json_wtr, "command");
228 			jsonw_start_array(json_wtr);
229 			for (i = 0; i < n_argc; i++)
230 				jsonw_string(json_wtr, n_argv[i]);
231 			jsonw_end_array(json_wtr);
232 			jsonw_name(json_wtr, "output");
233 		}
234 
235 		err = cmd_select(cmds, n_argc, n_argv, do_help);
236 
237 		if (json_output)
238 			jsonw_end_object(json_wtr);
239 
240 		if (err)
241 			goto err_close;
242 
243 		lines++;
244 	}
245 
246 	if (errno && errno != ENOENT) {
247 		perror("reading batch file failed");
248 		err = -1;
249 	} else {
250 		p_info("processed %d lines", lines);
251 		err = 0;
252 	}
253 err_close:
254 	fclose(fp);
255 
256 	if (json_output)
257 		jsonw_end_array(json_wtr);
258 
259 	return err;
260 }
261 
262 int main(int argc, char **argv)
263 {
264 	static const struct option options[] = {
265 		{ "json",	no_argument,	NULL,	'j' },
266 		{ "help",	no_argument,	NULL,	'h' },
267 		{ "pretty",	no_argument,	NULL,	'p' },
268 		{ "version",	no_argument,	NULL,	'V' },
269 		{ "bpffs",	no_argument,	NULL,	'f' },
270 		{ 0 }
271 	};
272 	int opt, ret;
273 
274 	last_do_help = do_help;
275 	pretty_output = false;
276 	json_output = false;
277 	show_pinned = false;
278 	bin_name = argv[0];
279 
280 	hash_init(prog_table.table);
281 	hash_init(map_table.table);
282 
283 	while ((opt = getopt_long(argc, argv, "Vhpjf",
284 				  options, NULL)) >= 0) {
285 		switch (opt) {
286 		case 'V':
287 			return do_version(argc, argv);
288 		case 'h':
289 			return do_help(argc, argv);
290 		case 'p':
291 			pretty_output = true;
292 			/* fall through */
293 		case 'j':
294 			json_output = true;
295 			break;
296 		case 'f':
297 			show_pinned = true;
298 			break;
299 		default:
300 			usage();
301 		}
302 	}
303 
304 	argc -= optind;
305 	argv += optind;
306 	if (argc < 0)
307 		usage();
308 
309 	if (json_output) {
310 		json_wtr = jsonw_new(stdout);
311 		if (!json_wtr) {
312 			p_err("failed to create JSON writer");
313 			return -1;
314 		}
315 		jsonw_pretty(json_wtr, pretty_output);
316 	}
317 
318 	bfd_init();
319 
320 	ret = cmd_select(cmds, argc, argv, do_help);
321 
322 	if (json_output)
323 		jsonw_destroy(&json_wtr);
324 
325 	if (show_pinned) {
326 		delete_pinned_obj_table(&prog_table);
327 		delete_pinned_obj_table(&map_table);
328 	}
329 
330 	return ret;
331 }
332