1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
17 #define BATCH_LINE_LEN_MAX 65536
18 #define BATCH_ARG_NB_MAX 4096
22 static char **last_argv;
23 static int (*last_do_help)(int argc, char **argv);
24 json_writer_t *json_wtr;
31 struct pinned_obj_table prog_table;
32 struct pinned_obj_table map_table;
34 static void __noreturn clean_and_exit(int i)
37 jsonw_destroy(&json_wtr);
44 last_do_help(last_argc - 1, last_argv + 1);
49 static int do_help(int argc, char **argv)
57 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
58 " %s batch file FILE\n"
61 " OBJECT := { prog | map | cgroup | perf | net | feature | btf }\n"
62 " " HELP_SPEC_OPTIONS "\n"
64 bin_name, bin_name, bin_name);
69 static int do_version(int argc, char **argv)
72 jsonw_start_object(json_wtr);
73 jsonw_name(json_wtr, "version");
74 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
75 jsonw_end_object(json_wtr);
77 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
82 static int __printf(2, 0)
83 print_all_levels(__maybe_unused enum libbpf_print_level level,
84 const char *format, va_list args)
86 return vfprintf(stderr, format, args);
89 int cmd_select(const struct cmd *cmds, int argc, char **argv,
90 int (*help)(int argc, char **argv))
98 if (argc < 1 && cmds[0].func)
99 return cmds[0].func(argc, argv);
101 for (i = 0; cmds[i].func; i++)
102 if (is_prefix(*argv, cmds[i].cmd))
103 return cmds[i].func(argc - 1, argv + 1);
105 help(argc - 1, argv + 1);
110 bool is_prefix(const char *pfx, const char *str)
114 if (strlen(str) < strlen(pfx))
117 return !memcmp(str, pfx, strlen(pfx));
120 /* Last argument MUST be NULL pointer */
121 int detect_common_prefix(const char *arg, ...)
123 unsigned int count = 0;
128 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
130 while ((ref = va_arg(ap, const char *))) {
131 if (!is_prefix(arg, ref))
135 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
136 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
139 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
149 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
151 unsigned char *data = arg;
154 for (i = 0; i < n; i++) {
155 const char *pfx = "";
166 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
170 /* Split command line into argument vector. */
171 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
173 static const char ws[] = " \t\r\n";
178 /* Skip leading whitespace. */
179 cp += strspn(cp, ws);
184 if (n_argc >= (maxargs - 1)) {
185 p_err("too many arguments to command %d", cmd_nb);
189 /* Word begins with quote. */
190 if (*cp == '\'' || *cp == '"') {
193 n_argv[n_argc++] = cp;
194 /* Find ending quote. */
195 cp = strchr(cp, quote);
197 p_err("unterminated quoted string in command %d",
202 n_argv[n_argc++] = cp;
204 /* Find end of word. */
205 cp += strcspn(cp, ws);
210 /* Separate words. */
213 n_argv[n_argc] = NULL;
218 static int do_batch(int argc, char **argv);
220 static const struct cmd cmds[] = {
222 { "batch", do_batch },
225 { "cgroup", do_cgroup },
228 { "feature", do_feature },
230 { "version", do_version },
234 static int do_batch(int argc, char **argv)
236 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
237 char *n_argv[BATCH_ARG_NB_MAX];
238 unsigned int lines = 0;
246 p_err("too few parameters for batch");
248 } else if (!is_prefix(*argv, "file")) {
249 p_err("expected 'file', got: %s", *argv);
251 } else if (argc > 2) {
252 p_err("too many parameters for batch");
257 if (!strcmp(*argv, "-"))
260 fp = fopen(*argv, "r");
262 p_err("Can't open file (%s): %s", *argv, strerror(errno));
267 jsonw_start_array(json_wtr);
268 while (fgets(buf, sizeof(buf), fp)) {
269 cp = strchr(buf, '#');
273 if (strlen(buf) == sizeof(buf) - 1) {
278 /* Append continuation lines if any (coming after a line ending
279 * with '\' in the batch file).
281 while ((cp = strstr(buf, "\\\n")) != NULL) {
282 if (!fgets(contline, sizeof(contline), fp) ||
283 strlen(contline) == 0) {
284 p_err("missing continuation line on command %d",
290 cp = strchr(contline, '#');
294 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
295 p_err("command %d is too long", lines);
299 buf[strlen(buf) - 2] = '\0';
300 strcat(buf, contline);
303 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
310 jsonw_start_object(json_wtr);
311 jsonw_name(json_wtr, "command");
312 jsonw_start_array(json_wtr);
313 for (i = 0; i < n_argc; i++)
314 jsonw_string(json_wtr, n_argv[i]);
315 jsonw_end_array(json_wtr);
316 jsonw_name(json_wtr, "output");
319 err = cmd_select(cmds, n_argc, n_argv, do_help);
322 jsonw_end_object(json_wtr);
330 if (errno && errno != ENOENT) {
331 p_err("reading batch file failed: %s", strerror(errno));
335 printf("processed %d commands\n", lines);
343 jsonw_end_array(json_wtr);
348 int main(int argc, char **argv)
350 static const struct option options[] = {
351 { "json", no_argument, NULL, 'j' },
352 { "help", no_argument, NULL, 'h' },
353 { "pretty", no_argument, NULL, 'p' },
354 { "version", no_argument, NULL, 'V' },
355 { "bpffs", no_argument, NULL, 'f' },
356 { "mapcompat", no_argument, NULL, 'm' },
357 { "nomount", no_argument, NULL, 'n' },
358 { "debug", no_argument, NULL, 'd' },
363 last_do_help = do_help;
364 pretty_output = false;
370 hash_init(prog_table.table);
371 hash_init(map_table.table);
374 while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
375 options, NULL)) >= 0) {
378 return do_version(argc, argv);
380 return do_help(argc, argv);
382 pretty_output = true;
386 json_wtr = jsonw_new(stdout);
388 p_err("failed to create JSON writer");
393 jsonw_pretty(json_wtr, pretty_output);
399 bpf_flags = MAPS_RELAX_COMPAT;
405 libbpf_set_print(print_all_levels);
406 verifier_logs = true;
409 p_err("unrecognized option '%s'", argv[optind - 1]);
422 ret = cmd_select(cmds, argc, argv, do_help);
425 jsonw_destroy(&json_wtr);
428 delete_pinned_obj_table(&prog_table);
429 delete_pinned_obj_table(&map_table);