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 #ifndef BPFTOOL_VERSION
75 /* bpftool's major and minor version numbers are aligned on libbpf's. There is
76 * an offset of 6 for the version number, because bpftool's version was higher
77 * than libbpf's when we adopted this scheme. The patch number remains at 0
78 * for now. Set BPFTOOL_VERSION to override.
80 #define BPFTOOL_MAJOR_VERSION (LIBBPF_MAJOR_VERSION + 6)
81 #define BPFTOOL_MINOR_VERSION LIBBPF_MINOR_VERSION
82 #define BPFTOOL_PATCH_VERSION 0
85 static int do_version(int argc, char **argv)
87 #ifdef HAVE_LIBBFD_SUPPORT
88 const bool has_libbfd = true;
90 const bool has_libbfd = false;
92 #ifdef BPFTOOL_WITHOUT_SKELETONS
93 const bool has_skeletons = false;
95 const bool has_skeletons = true;
99 jsonw_start_object(json_wtr); /* root object */
101 jsonw_name(json_wtr, "version");
102 #ifdef BPFTOOL_VERSION
103 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
105 jsonw_printf(json_wtr, "\"%d.%d.%d\"", BPFTOOL_MAJOR_VERSION,
106 BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
108 jsonw_name(json_wtr, "libbpf_version");
109 jsonw_printf(json_wtr, "\"%d.%d\"",
110 libbpf_major_version(), libbpf_minor_version());
112 jsonw_name(json_wtr, "features");
113 jsonw_start_object(json_wtr); /* features */
114 jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
115 jsonw_bool_field(json_wtr, "libbpf_strict", !legacy_libbpf);
116 jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
117 jsonw_end_object(json_wtr); /* features */
119 jsonw_end_object(json_wtr); /* root object */
121 unsigned int nb_features = 0;
123 #ifdef BPFTOOL_VERSION
124 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
126 printf("%s v%d.%d.%d\n", bin_name, BPFTOOL_MAJOR_VERSION,
127 BPFTOOL_MINOR_VERSION, BPFTOOL_PATCH_VERSION);
129 printf("using libbpf %s\n", libbpf_version_string());
135 if (!legacy_libbpf) {
136 printf("%s libbpf_strict", nb_features++ ? "," : "");
140 printf("%s skeletons", nb_features++ ? "," : "");
146 int cmd_select(const struct cmd *cmds, int argc, char **argv,
147 int (*help)(int argc, char **argv))
155 if (argc < 1 && cmds[0].func)
156 return cmds[0].func(argc, argv);
158 for (i = 0; cmds[i].cmd; i++) {
159 if (is_prefix(*argv, cmds[i].cmd)) {
161 p_err("command '%s' is not supported in bootstrap mode",
165 return cmds[i].func(argc - 1, argv + 1);
169 help(argc - 1, argv + 1);
174 bool is_prefix(const char *pfx, const char *str)
178 if (strlen(str) < strlen(pfx))
181 return !memcmp(str, pfx, strlen(pfx));
184 /* Last argument MUST be NULL pointer */
185 int detect_common_prefix(const char *arg, ...)
187 unsigned int count = 0;
192 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
194 while ((ref = va_arg(ap, const char *))) {
195 if (!is_prefix(arg, ref))
199 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
200 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
203 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
213 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
215 unsigned char *data = arg;
218 for (i = 0; i < n; i++) {
219 const char *pfx = "";
230 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
234 /* Split command line into argument vector. */
235 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
237 static const char ws[] = " \t\r\n";
242 /* Skip leading whitespace. */
243 cp += strspn(cp, ws);
248 if (n_argc >= (maxargs - 1)) {
249 p_err("too many arguments to command %d", cmd_nb);
253 /* Word begins with quote. */
254 if (*cp == '\'' || *cp == '"') {
257 n_argv[n_argc++] = cp;
258 /* Find ending quote. */
259 cp = strchr(cp, quote);
261 p_err("unterminated quoted string in command %d",
266 n_argv[n_argc++] = cp;
268 /* Find end of word. */
269 cp += strcspn(cp, ws);
274 /* Separate words. */
277 n_argv[n_argc] = NULL;
282 static int do_batch(int argc, char **argv);
284 static const struct cmd cmds[] = {
286 { "batch", do_batch },
290 { "cgroup", do_cgroup },
293 { "feature", do_feature },
296 { "struct_ops", do_struct_ops },
298 { "version", do_version },
302 static int do_batch(int argc, char **argv)
304 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
305 char *n_argv[BATCH_ARG_NB_MAX];
306 unsigned int lines = 0;
314 p_err("too few parameters for batch");
316 } else if (!is_prefix(*argv, "file")) {
317 p_err("expected 'file', got: %s", *argv);
319 } else if (argc > 2) {
320 p_err("too many parameters for batch");
325 if (!strcmp(*argv, "-"))
328 fp = fopen(*argv, "r");
330 p_err("Can't open file (%s): %s", *argv, strerror(errno));
335 jsonw_start_array(json_wtr);
336 while (fgets(buf, sizeof(buf), fp)) {
337 cp = strchr(buf, '#');
341 if (strlen(buf) == sizeof(buf) - 1) {
346 /* Append continuation lines if any (coming after a line ending
347 * with '\' in the batch file).
349 while ((cp = strstr(buf, "\\\n")) != NULL) {
350 if (!fgets(contline, sizeof(contline), fp) ||
351 strlen(contline) == 0) {
352 p_err("missing continuation line on command %d",
358 cp = strchr(contline, '#');
362 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
363 p_err("command %d is too long", lines);
367 buf[strlen(buf) - 2] = '\0';
368 strcat(buf, contline);
371 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
380 jsonw_start_object(json_wtr);
381 jsonw_name(json_wtr, "command");
382 jsonw_start_array(json_wtr);
383 for (i = 0; i < n_argc; i++)
384 jsonw_string(json_wtr, n_argv[i]);
385 jsonw_end_array(json_wtr);
386 jsonw_name(json_wtr, "output");
389 err = cmd_select(cmds, n_argc, n_argv, do_help);
392 jsonw_end_object(json_wtr);
400 if (errno && errno != ENOENT) {
401 p_err("reading batch file failed: %s", strerror(errno));
405 printf("processed %d commands\n", lines);
412 jsonw_end_array(json_wtr);
417 int main(int argc, char **argv)
419 static const struct option options[] = {
420 { "json", no_argument, NULL, 'j' },
421 { "help", no_argument, NULL, 'h' },
422 { "pretty", no_argument, NULL, 'p' },
423 { "version", no_argument, NULL, 'V' },
424 { "bpffs", no_argument, NULL, 'f' },
425 { "mapcompat", no_argument, NULL, 'm' },
426 { "nomount", no_argument, NULL, 'n' },
427 { "debug", no_argument, NULL, 'd' },
428 { "use-loader", no_argument, NULL, 'L' },
429 { "base-btf", required_argument, NULL, 'B' },
430 { "legacy", no_argument, NULL, 'l' },
433 bool version_requested = false;
439 /* Libcap < 2.63 hooks before main() to compute the number of
440 * capabilities of the running kernel, and doing so it calls prctl()
441 * which may fail and set errno to non-zero.
442 * Let's reset errno to make sure this does not interfere with the
448 last_do_help = do_help;
449 pretty_output = false;
456 while ((opt = getopt_long(argc, argv, "VhpjfLmndB:l",
457 options, NULL)) >= 0) {
460 version_requested = true;
463 return do_help(argc, argv);
465 pretty_output = true;
469 json_wtr = jsonw_new(stdout);
471 p_err("failed to create JSON writer");
476 jsonw_pretty(json_wtr, pretty_output);
488 libbpf_set_print(print_all_levels);
489 verifier_logs = true;
492 base_btf = btf__parse(optarg, NULL);
493 if (libbpf_get_error(base_btf)) {
494 p_err("failed to parse base BTF at '%s': %ld\n",
495 optarg, libbpf_get_error(base_btf));
504 legacy_libbpf = true;
507 p_err("unrecognized option '%s'", argv[optind - 1]);
515 if (!legacy_libbpf) {
516 /* Allow legacy map definitions for skeleton generation.
517 * It will still be rejected if users use LIBBPF_STRICT_ALL
518 * mode for loading generated skeleton.
520 libbpf_set_strict_mode(LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS);
528 if (version_requested)
529 return do_version(argc, argv);
531 ret = cmd_select(cmds, argc, argv, do_help);
534 jsonw_destroy(&json_wtr);