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>
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 | gen | struct_ops }\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 int cmd_select(const struct cmd
*cmds
, int argc
, char **argv
,
83 int (*help
)(int argc
, char **argv
))
91 if (argc
< 1 && cmds
[0].func
)
92 return cmds
[0].func(argc
, argv
);
94 for (i
= 0; cmds
[i
].func
; i
++)
95 if (is_prefix(*argv
, cmds
[i
].cmd
))
96 return cmds
[i
].func(argc
- 1, argv
+ 1);
98 help(argc
- 1, argv
+ 1);
103 bool is_prefix(const char *pfx
, const char *str
)
107 if (strlen(str
) < strlen(pfx
))
110 return !memcmp(str
, pfx
, strlen(pfx
));
113 /* Last argument MUST be NULL pointer */
114 int detect_common_prefix(const char *arg
, ...)
116 unsigned int count
= 0;
121 snprintf(msg
, sizeof(msg
), "ambiguous prefix: '%s' could be '", arg
);
123 while ((ref
= va_arg(ap
, const char *))) {
124 if (!is_prefix(arg
, ref
))
128 strncat(msg
, "' or '", sizeof(msg
) - strlen(msg
) - 1);
129 strncat(msg
, ref
, sizeof(msg
) - strlen(msg
) - 1);
132 strncat(msg
, "'", sizeof(msg
) - strlen(msg
) - 1);
142 void fprint_hex(FILE *f
, void *arg
, unsigned int n
, const char *sep
)
144 unsigned char *data
= arg
;
147 for (i
= 0; i
< n
; i
++) {
148 const char *pfx
= "";
159 fprintf(f
, "%s%02hhx", i
? pfx
: "", data
[i
]);
163 /* Split command line into argument vector. */
164 static int make_args(char *line
, char *n_argv
[], int maxargs
, int cmd_nb
)
166 static const char ws
[] = " \t\r\n";
171 /* Skip leading whitespace. */
172 cp
+= strspn(cp
, ws
);
177 if (n_argc
>= (maxargs
- 1)) {
178 p_err("too many arguments to command %d", cmd_nb
);
182 /* Word begins with quote. */
183 if (*cp
== '\'' || *cp
== '"') {
186 n_argv
[n_argc
++] = cp
;
187 /* Find ending quote. */
188 cp
= strchr(cp
, quote
);
190 p_err("unterminated quoted string in command %d",
195 n_argv
[n_argc
++] = cp
;
197 /* Find end of word. */
198 cp
+= strcspn(cp
, ws
);
203 /* Separate words. */
206 n_argv
[n_argc
] = NULL
;
211 static int do_batch(int argc
, char **argv
);
213 static const struct cmd cmds
[] = {
215 { "batch", do_batch
},
218 { "cgroup", do_cgroup
},
221 { "feature", do_feature
},
224 { "struct_ops", do_struct_ops
},
225 { "version", do_version
},
229 static int do_batch(int argc
, char **argv
)
231 char buf
[BATCH_LINE_LEN_MAX
], contline
[BATCH_LINE_LEN_MAX
];
232 char *n_argv
[BATCH_ARG_NB_MAX
];
233 unsigned int lines
= 0;
241 p_err("too few parameters for batch");
243 } else if (!is_prefix(*argv
, "file")) {
244 p_err("expected 'file', got: %s", *argv
);
246 } else if (argc
> 2) {
247 p_err("too many parameters for batch");
252 if (!strcmp(*argv
, "-"))
255 fp
= fopen(*argv
, "r");
257 p_err("Can't open file (%s): %s", *argv
, strerror(errno
));
262 jsonw_start_array(json_wtr
);
263 while (fgets(buf
, sizeof(buf
), fp
)) {
264 cp
= strchr(buf
, '#');
268 if (strlen(buf
) == sizeof(buf
) - 1) {
273 /* Append continuation lines if any (coming after a line ending
274 * with '\' in the batch file).
276 while ((cp
= strstr(buf
, "\\\n")) != NULL
) {
277 if (!fgets(contline
, sizeof(contline
), fp
) ||
278 strlen(contline
) == 0) {
279 p_err("missing continuation line on command %d",
285 cp
= strchr(contline
, '#');
289 if (strlen(buf
) + strlen(contline
) + 1 > sizeof(buf
)) {
290 p_err("command %d is too long", lines
);
294 buf
[strlen(buf
) - 2] = '\0';
295 strcat(buf
, contline
);
298 n_argc
= make_args(buf
, n_argv
, BATCH_ARG_NB_MAX
, lines
);
305 jsonw_start_object(json_wtr
);
306 jsonw_name(json_wtr
, "command");
307 jsonw_start_array(json_wtr
);
308 for (i
= 0; i
< n_argc
; i
++)
309 jsonw_string(json_wtr
, n_argv
[i
]);
310 jsonw_end_array(json_wtr
);
311 jsonw_name(json_wtr
, "output");
314 err
= cmd_select(cmds
, n_argc
, n_argv
, do_help
);
317 jsonw_end_object(json_wtr
);
325 if (errno
&& errno
!= ENOENT
) {
326 p_err("reading batch file failed: %s", strerror(errno
));
330 printf("processed %d commands\n", lines
);
338 jsonw_end_array(json_wtr
);
343 int main(int argc
, char **argv
)
345 static const struct option options
[] = {
346 { "json", no_argument
, NULL
, 'j' },
347 { "help", no_argument
, NULL
, 'h' },
348 { "pretty", no_argument
, NULL
, 'p' },
349 { "version", no_argument
, NULL
, 'V' },
350 { "bpffs", no_argument
, NULL
, 'f' },
351 { "mapcompat", no_argument
, NULL
, 'm' },
352 { "nomount", no_argument
, NULL
, 'n' },
353 { "debug", no_argument
, NULL
, 'd' },
358 last_do_help
= do_help
;
359 pretty_output
= false;
365 hash_init(prog_table
.table
);
366 hash_init(map_table
.table
);
369 while ((opt
= getopt_long(argc
, argv
, "Vhpjfmnd",
370 options
, NULL
)) >= 0) {
373 return do_version(argc
, argv
);
375 return do_help(argc
, argv
);
377 pretty_output
= true;
381 json_wtr
= jsonw_new(stdout
);
383 p_err("failed to create JSON writer");
388 jsonw_pretty(json_wtr
, pretty_output
);
400 libbpf_set_print(print_all_levels
);
401 verifier_logs
= true;
404 p_err("unrecognized option '%s'", argv
[optind
- 1]);
417 ret
= cmd_select(cmds
, argc
, argv
, do_help
);
420 jsonw_destroy(&json_wtr
);
423 delete_pinned_obj_table(&prog_table
);
424 delete_pinned_obj_table(&map_table
);