1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
14 #include <bpf/hashmap.h>
15 #include <bpf/libbpf.h>
19 #define BATCH_LINE_LEN_MAX 65536
20 #define BATCH_ARG_NB_MAX 4096
24 static char **last_argv;
25 static int (*last_do_help)(int argc, char **argv);
26 json_writer_t *json_wtr;
36 struct hashmap *refs_table;
38 static void __noreturn clean_and_exit(int i)
41 jsonw_destroy(&json_wtr);
48 last_do_help(last_argc - 1, last_argv + 1);
53 static int do_help(int argc, char **argv)
61 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
62 " %s batch file FILE\n"
65 " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
66 " " HELP_SPEC_OPTIONS " |\n"
69 bin_name, bin_name, bin_name);
74 static int do_version(int argc, char **argv)
76 #ifdef HAVE_LIBBFD_SUPPORT
77 const bool has_libbfd = true;
79 const bool has_libbfd = false;
81 #ifdef BPFTOOL_WITHOUT_SKELETONS
82 const bool has_skeletons = false;
84 const bool has_skeletons = true;
88 jsonw_start_object(json_wtr); /* root object */
90 jsonw_name(json_wtr, "version");
91 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
92 jsonw_name(json_wtr, "libbpf_version");
93 jsonw_printf(json_wtr, "\"%d.%d\"",
94 libbpf_major_version(), libbpf_minor_version());
96 jsonw_name(json_wtr, "features");
97 jsonw_start_object(json_wtr); /* features */
98 jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
99 jsonw_bool_field(json_wtr, "libbpf_strict", !legacy_libbpf);
100 jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
101 jsonw_end_object(json_wtr); /* features */
103 jsonw_end_object(json_wtr); /* root object */
105 unsigned int nb_features = 0;
107 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
108 printf("using libbpf %s\n", libbpf_version_string());
114 if (!legacy_libbpf) {
115 printf("%s libbpf_strict", nb_features++ ? "," : "");
119 printf("%s skeletons", nb_features++ ? "," : "");
125 int cmd_select(const struct cmd *cmds, int argc, char **argv,
126 int (*help)(int argc, char **argv))
134 if (argc < 1 && cmds[0].func)
135 return cmds[0].func(argc, argv);
137 for (i = 0; cmds[i].cmd; i++) {
138 if (is_prefix(*argv, cmds[i].cmd)) {
140 p_err("command '%s' is not supported in bootstrap mode",
144 return cmds[i].func(argc - 1, argv + 1);
148 help(argc - 1, argv + 1);
153 bool is_prefix(const char *pfx, const char *str)
157 if (strlen(str) < strlen(pfx))
160 return !memcmp(str, pfx, strlen(pfx));
163 /* Last argument MUST be NULL pointer */
164 int detect_common_prefix(const char *arg, ...)
166 unsigned int count = 0;
171 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
173 while ((ref = va_arg(ap, const char *))) {
174 if (!is_prefix(arg, ref))
178 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
179 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
182 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
192 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
194 unsigned char *data = arg;
197 for (i = 0; i < n; i++) {
198 const char *pfx = "";
209 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
213 /* Split command line into argument vector. */
214 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
216 static const char ws[] = " \t\r\n";
221 /* Skip leading whitespace. */
222 cp += strspn(cp, ws);
227 if (n_argc >= (maxargs - 1)) {
228 p_err("too many arguments to command %d", cmd_nb);
232 /* Word begins with quote. */
233 if (*cp == '\'' || *cp == '"') {
236 n_argv[n_argc++] = cp;
237 /* Find ending quote. */
238 cp = strchr(cp, quote);
240 p_err("unterminated quoted string in command %d",
245 n_argv[n_argc++] = cp;
247 /* Find end of word. */
248 cp += strcspn(cp, ws);
253 /* Separate words. */
256 n_argv[n_argc] = NULL;
261 static int do_batch(int argc, char **argv);
263 static const struct cmd cmds[] = {
265 { "batch", do_batch },
269 { "cgroup", do_cgroup },
272 { "feature", do_feature },
275 { "struct_ops", do_struct_ops },
277 { "version", do_version },
281 static int do_batch(int argc, char **argv)
283 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
284 char *n_argv[BATCH_ARG_NB_MAX];
285 unsigned int lines = 0;
293 p_err("too few parameters for batch");
295 } else if (!is_prefix(*argv, "file")) {
296 p_err("expected 'file', got: %s", *argv);
298 } else if (argc > 2) {
299 p_err("too many parameters for batch");
304 if (!strcmp(*argv, "-"))
307 fp = fopen(*argv, "r");
309 p_err("Can't open file (%s): %s", *argv, strerror(errno));
314 jsonw_start_array(json_wtr);
315 while (fgets(buf, sizeof(buf), fp)) {
316 cp = strchr(buf, '#');
320 if (strlen(buf) == sizeof(buf) - 1) {
325 /* Append continuation lines if any (coming after a line ending
326 * with '\' in the batch file).
328 while ((cp = strstr(buf, "\\\n")) != NULL) {
329 if (!fgets(contline, sizeof(contline), fp) ||
330 strlen(contline) == 0) {
331 p_err("missing continuation line on command %d",
337 cp = strchr(contline, '#');
341 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
342 p_err("command %d is too long", lines);
346 buf[strlen(buf) - 2] = '\0';
347 strcat(buf, contline);
350 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
359 jsonw_start_object(json_wtr);
360 jsonw_name(json_wtr, "command");
361 jsonw_start_array(json_wtr);
362 for (i = 0; i < n_argc; i++)
363 jsonw_string(json_wtr, n_argv[i]);
364 jsonw_end_array(json_wtr);
365 jsonw_name(json_wtr, "output");
368 err = cmd_select(cmds, n_argc, n_argv, do_help);
371 jsonw_end_object(json_wtr);
379 if (errno && errno != ENOENT) {
380 p_err("reading batch file failed: %s", strerror(errno));
384 printf("processed %d commands\n", lines);
391 jsonw_end_array(json_wtr);
396 int main(int argc, char **argv)
398 static const struct option options[] = {
399 { "json", no_argument, NULL, 'j' },
400 { "help", no_argument, NULL, 'h' },
401 { "pretty", no_argument, NULL, 'p' },
402 { "version", no_argument, NULL, 'V' },
403 { "bpffs", no_argument, NULL, 'f' },
404 { "mapcompat", no_argument, NULL, 'm' },
405 { "nomount", no_argument, NULL, 'n' },
406 { "debug", no_argument, NULL, 'd' },
407 { "use-loader", no_argument, NULL, 'L' },
408 { "base-btf", required_argument, NULL, 'B' },
409 { "legacy", no_argument, NULL, 'l' },
412 bool version_requested = false;
417 last_do_help = do_help;
418 pretty_output = false;
425 while ((opt = getopt_long(argc, argv, "VhpjfLmndB:l",
426 options, NULL)) >= 0) {
429 version_requested = true;
432 return do_help(argc, argv);
434 pretty_output = true;
438 json_wtr = jsonw_new(stdout);
440 p_err("failed to create JSON writer");
445 jsonw_pretty(json_wtr, pretty_output);
457 libbpf_set_print(print_all_levels);
458 verifier_logs = true;
461 base_btf = btf__parse(optarg, NULL);
462 if (libbpf_get_error(base_btf)) {
463 p_err("failed to parse base BTF at '%s': %ld\n",
464 optarg, libbpf_get_error(base_btf));
473 legacy_libbpf = true;
476 p_err("unrecognized option '%s'", argv[optind - 1]);
484 if (!legacy_libbpf) {
485 /* Allow legacy map definitions for skeleton generation.
486 * It will still be rejected if users use LIBBPF_STRICT_ALL
487 * mode for loading generated skeleton.
489 ret = libbpf_set_strict_mode(LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS);
491 p_err("failed to enable libbpf strict mode: %d", ret);
499 if (version_requested)
500 return do_version(argc, argv);
502 ret = cmd_select(cmds, argc, argv, do_help);
505 jsonw_destroy(&json_wtr);