1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
13 #include <bpf/libbpf.h>
18 #define BATCH_LINE_LEN_MAX 65536
19 #define BATCH_ARG_NB_MAX 4096
23 static char **last_argv
;
24 static int (*last_do_help
)(int argc
, char **argv
);
25 json_writer_t
*json_wtr
;
33 struct pinned_obj_table prog_table
;
34 struct pinned_obj_table map_table
;
35 struct pinned_obj_table link_table
;
36 struct obj_refs_table 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"
68 bin_name
, bin_name
, bin_name
);
73 static int do_version(int argc
, char **argv
)
75 #ifdef HAVE_LIBBFD_SUPPORT
76 const bool has_libbfd
= true;
78 const bool has_libbfd
= false;
80 #ifdef BPFTOOL_WITHOUT_SKELETONS
81 const bool has_skeletons
= false;
83 const bool has_skeletons
= true;
87 jsonw_start_object(json_wtr
); /* root object */
89 jsonw_name(json_wtr
, "version");
90 jsonw_printf(json_wtr
, "\"%s\"", BPFTOOL_VERSION
);
92 jsonw_name(json_wtr
, "features");
93 jsonw_start_object(json_wtr
); /* features */
94 jsonw_bool_field(json_wtr
, "libbfd", has_libbfd
);
95 jsonw_bool_field(json_wtr
, "skeletons", has_skeletons
);
96 jsonw_end_object(json_wtr
); /* features */
98 jsonw_end_object(json_wtr
); /* root object */
100 unsigned int nb_features
= 0;
102 printf("%s v%s\n", bin_name
, BPFTOOL_VERSION
);
109 printf("%s skeletons", nb_features
++ ? "," : "");
115 int cmd_select(const struct cmd
*cmds
, int argc
, char **argv
,
116 int (*help
)(int argc
, char **argv
))
124 if (argc
< 1 && cmds
[0].func
)
125 return cmds
[0].func(argc
, argv
);
127 for (i
= 0; cmds
[i
].cmd
; i
++) {
128 if (is_prefix(*argv
, cmds
[i
].cmd
)) {
130 p_err("command '%s' is not supported in bootstrap mode",
134 return cmds
[i
].func(argc
- 1, argv
+ 1);
138 help(argc
- 1, argv
+ 1);
143 bool is_prefix(const char *pfx
, const char *str
)
147 if (strlen(str
) < strlen(pfx
))
150 return !memcmp(str
, pfx
, strlen(pfx
));
153 /* Last argument MUST be NULL pointer */
154 int detect_common_prefix(const char *arg
, ...)
156 unsigned int count
= 0;
161 snprintf(msg
, sizeof(msg
), "ambiguous prefix: '%s' could be '", arg
);
163 while ((ref
= va_arg(ap
, const char *))) {
164 if (!is_prefix(arg
, ref
))
168 strncat(msg
, "' or '", sizeof(msg
) - strlen(msg
) - 1);
169 strncat(msg
, ref
, sizeof(msg
) - strlen(msg
) - 1);
172 strncat(msg
, "'", sizeof(msg
) - strlen(msg
) - 1);
182 void fprint_hex(FILE *f
, void *arg
, unsigned int n
, const char *sep
)
184 unsigned char *data
= arg
;
187 for (i
= 0; i
< n
; i
++) {
188 const char *pfx
= "";
199 fprintf(f
, "%s%02hhx", i
? pfx
: "", data
[i
]);
203 /* Split command line into argument vector. */
204 static int make_args(char *line
, char *n_argv
[], int maxargs
, int cmd_nb
)
206 static const char ws
[] = " \t\r\n";
211 /* Skip leading whitespace. */
212 cp
+= strspn(cp
, ws
);
217 if (n_argc
>= (maxargs
- 1)) {
218 p_err("too many arguments to command %d", cmd_nb
);
222 /* Word begins with quote. */
223 if (*cp
== '\'' || *cp
== '"') {
226 n_argv
[n_argc
++] = cp
;
227 /* Find ending quote. */
228 cp
= strchr(cp
, quote
);
230 p_err("unterminated quoted string in command %d",
235 n_argv
[n_argc
++] = cp
;
237 /* Find end of word. */
238 cp
+= strcspn(cp
, ws
);
243 /* Separate words. */
246 n_argv
[n_argc
] = NULL
;
251 static int do_batch(int argc
, char **argv
);
253 static const struct cmd cmds
[] = {
255 { "batch", do_batch
},
259 { "cgroup", do_cgroup
},
262 { "feature", do_feature
},
265 { "struct_ops", do_struct_ops
},
267 { "version", do_version
},
271 static int do_batch(int argc
, char **argv
)
273 char buf
[BATCH_LINE_LEN_MAX
], contline
[BATCH_LINE_LEN_MAX
];
274 char *n_argv
[BATCH_ARG_NB_MAX
];
275 unsigned int lines
= 0;
283 p_err("too few parameters for batch");
285 } else if (!is_prefix(*argv
, "file")) {
286 p_err("expected 'file', got: %s", *argv
);
288 } else if (argc
> 2) {
289 p_err("too many parameters for batch");
294 if (!strcmp(*argv
, "-"))
297 fp
= fopen(*argv
, "r");
299 p_err("Can't open file (%s): %s", *argv
, strerror(errno
));
304 jsonw_start_array(json_wtr
);
305 while (fgets(buf
, sizeof(buf
), fp
)) {
306 cp
= strchr(buf
, '#');
310 if (strlen(buf
) == sizeof(buf
) - 1) {
315 /* Append continuation lines if any (coming after a line ending
316 * with '\' in the batch file).
318 while ((cp
= strstr(buf
, "\\\n")) != NULL
) {
319 if (!fgets(contline
, sizeof(contline
), fp
) ||
320 strlen(contline
) == 0) {
321 p_err("missing continuation line on command %d",
327 cp
= strchr(contline
, '#');
331 if (strlen(buf
) + strlen(contline
) + 1 > sizeof(buf
)) {
332 p_err("command %d is too long", lines
);
336 buf
[strlen(buf
) - 2] = '\0';
337 strcat(buf
, contline
);
340 n_argc
= make_args(buf
, n_argv
, BATCH_ARG_NB_MAX
, lines
);
347 jsonw_start_object(json_wtr
);
348 jsonw_name(json_wtr
, "command");
349 jsonw_start_array(json_wtr
);
350 for (i
= 0; i
< n_argc
; i
++)
351 jsonw_string(json_wtr
, n_argv
[i
]);
352 jsonw_end_array(json_wtr
);
353 jsonw_name(json_wtr
, "output");
356 err
= cmd_select(cmds
, n_argc
, n_argv
, do_help
);
359 jsonw_end_object(json_wtr
);
367 if (errno
&& errno
!= ENOENT
) {
368 p_err("reading batch file failed: %s", strerror(errno
));
372 printf("processed %d commands\n", lines
);
380 jsonw_end_array(json_wtr
);
385 int main(int argc
, char **argv
)
387 static const struct option options
[] = {
388 { "json", no_argument
, NULL
, 'j' },
389 { "help", no_argument
, NULL
, 'h' },
390 { "pretty", no_argument
, NULL
, 'p' },
391 { "version", no_argument
, NULL
, 'V' },
392 { "bpffs", no_argument
, NULL
, 'f' },
393 { "mapcompat", no_argument
, NULL
, 'm' },
394 { "nomount", no_argument
, NULL
, 'n' },
395 { "debug", no_argument
, NULL
, 'd' },
396 { "base-btf", required_argument
, NULL
, 'B' },
401 last_do_help
= do_help
;
402 pretty_output
= false;
408 hash_init(prog_table
.table
);
409 hash_init(map_table
.table
);
410 hash_init(link_table
.table
);
413 while ((opt
= getopt_long(argc
, argv
, "VhpjfmndB:",
414 options
, NULL
)) >= 0) {
417 return do_version(argc
, argv
);
419 return do_help(argc
, argv
);
421 pretty_output
= true;
425 json_wtr
= jsonw_new(stdout
);
427 p_err("failed to create JSON writer");
432 jsonw_pretty(json_wtr
, pretty_output
);
444 libbpf_set_print(print_all_levels
);
445 verifier_logs
= true;
448 base_btf
= btf__parse(optarg
, NULL
);
449 if (libbpf_get_error(base_btf
)) {
450 p_err("failed to parse base BTF at '%s': %ld\n",
451 optarg
, libbpf_get_error(base_btf
));
457 p_err("unrecognized option '%s'", argv
[optind
- 1]);
470 ret
= cmd_select(cmds
, argc
, argv
, do_help
);
473 jsonw_destroy(&json_wtr
);
476 delete_pinned_obj_table(&prog_table
);
477 delete_pinned_obj_table(&map_table
);
478 delete_pinned_obj_table(&link_table
);