2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
38 #include <linux/bpf.h>
47 #define BATCH_LINE_LEN_MAX 65536
48 #define BATCH_ARG_NB_MAX 4096
52 static char **last_argv;
53 static int (*last_do_help)(int argc, char **argv);
54 json_writer_t *json_wtr;
58 struct pinned_obj_table prog_table;
59 struct pinned_obj_table map_table;
61 static void __noreturn clean_and_exit(int i)
64 jsonw_destroy(&json_wtr);
71 last_do_help(last_argc - 1, last_argv + 1);
76 static int do_help(int argc, char **argv)
84 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
85 " %s batch file FILE\n"
88 " OBJECT := { prog | map | cgroup | perf }\n"
89 " " HELP_SPEC_OPTIONS "\n"
91 bin_name, bin_name, bin_name);
96 static int do_version(int argc, char **argv)
99 jsonw_start_object(json_wtr);
100 jsonw_name(json_wtr, "version");
101 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
102 jsonw_end_object(json_wtr);
104 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
109 int cmd_select(const struct cmd *cmds, int argc, char **argv,
110 int (*help)(int argc, char **argv))
118 if (argc < 1 && cmds[0].func)
119 return cmds[0].func(argc, argv);
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);
125 help(argc - 1, argv + 1);
130 bool is_prefix(const char *pfx, const char *str)
134 if (strlen(str) < strlen(pfx))
137 return !memcmp(str, pfx, strlen(pfx));
140 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
142 unsigned char *data = arg;
145 for (i = 0; i < n; i++) {
146 const char *pfx = "";
157 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
161 /* Split command line into argument vector. */
162 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
164 static const char ws[] = " \t\r\n";
169 /* Skip leading whitespace. */
170 cp += strspn(cp, ws);
175 if (n_argc >= (maxargs - 1)) {
176 p_err("too many arguments to command %d", cmd_nb);
180 /* Word begins with quote. */
181 if (*cp == '\'' || *cp == '"') {
184 n_argv[n_argc++] = cp;
185 /* Find ending quote. */
186 cp = strchr(cp, quote);
188 p_err("unterminated quoted string in command %d",
193 n_argv[n_argc++] = cp;
195 /* Find end of word. */
196 cp += strcspn(cp, ws);
201 /* Separate words. */
204 n_argv[n_argc] = NULL;
209 static int do_batch(int argc, char **argv);
211 static const struct cmd cmds[] = {
213 { "batch", do_batch },
216 { "cgroup", do_cgroup },
218 { "version", do_version },
222 static int do_batch(int argc, char **argv)
224 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
225 char *n_argv[BATCH_ARG_NB_MAX];
226 unsigned int lines = 0;
234 p_err("too few parameters for batch");
236 } else if (!is_prefix(*argv, "file")) {
237 p_err("expected 'file', got: %s", *argv);
239 } else if (argc > 2) {
240 p_err("too many parameters for batch");
245 if (!strcmp(*argv, "-"))
248 fp = fopen(*argv, "r");
250 p_err("Can't open file (%s): %s", *argv, strerror(errno));
255 jsonw_start_array(json_wtr);
256 while (fgets(buf, sizeof(buf), fp)) {
257 cp = strchr(buf, '#');
261 if (strlen(buf) == sizeof(buf) - 1) {
266 /* Append continuation lines if any (coming after a line ending
267 * with '\' in the batch file).
269 while ((cp = strstr(buf, "\\\n")) != NULL) {
270 if (!fgets(contline, sizeof(contline), fp) ||
271 strlen(contline) == 0) {
272 p_err("missing continuation line on command %d",
278 cp = strchr(contline, '#');
282 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
283 p_err("command %d is too long", lines);
287 buf[strlen(buf) - 2] = '\0';
288 strcat(buf, contline);
291 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
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");
309 err = cmd_select(cmds, n_argc, n_argv, do_help);
312 jsonw_end_object(json_wtr);
320 if (errno && errno != ENOENT) {
321 p_err("reading batch file failed: %s", strerror(errno));
324 p_info("processed %d commands", lines);
332 jsonw_end_array(json_wtr);
337 int main(int argc, char **argv)
339 static const struct option options[] = {
340 { "json", no_argument, NULL, 'j' },
341 { "help", no_argument, NULL, 'h' },
342 { "pretty", no_argument, NULL, 'p' },
343 { "version", no_argument, NULL, 'V' },
344 { "bpffs", no_argument, NULL, 'f' },
349 last_do_help = do_help;
350 pretty_output = false;
355 hash_init(prog_table.table);
356 hash_init(map_table.table);
359 while ((opt = getopt_long(argc, argv, "Vhpjf",
360 options, NULL)) >= 0) {
363 return do_version(argc, argv);
365 return do_help(argc, argv);
367 pretty_output = true;
371 json_wtr = jsonw_new(stdout);
373 p_err("failed to create JSON writer");
378 jsonw_pretty(json_wtr, pretty_output);
384 p_err("unrecognized option '%s'", argv[optind - 1]);
399 ret = cmd_select(cmds, argc, argv, do_help);
402 jsonw_destroy(&json_wtr);
405 delete_pinned_obj_table(&prog_table);
406 delete_pinned_obj_table(&map_table);