blob: 1854d6b978604013b2decac6b983d7468dfe345c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004#include <ctype.h>
5#include <errno.h>
6#include <getopt.h>
7#include <linux/bpf.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
Olivier Deprez157378f2022-04-04 15:47:50 +020012#include <bpf/bpf.h>
13#include <bpf/libbpf.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014
15#include "main.h"
16
17#define BATCH_LINE_LEN_MAX 65536
18#define BATCH_ARG_NB_MAX 4096
19
20const char *bin_name;
21static int last_argc;
22static char **last_argv;
23static int (*last_do_help)(int argc, char **argv);
24json_writer_t *json_wtr;
25bool pretty_output;
26bool json_output;
27bool show_pinned;
David Brazdil0f672f62019-12-10 10:32:29 +000028bool block_mount;
29bool verifier_logs;
Olivier Deprez157378f2022-04-04 15:47:50 +020030bool relaxed_maps;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031struct pinned_obj_table prog_table;
32struct pinned_obj_table map_table;
Olivier Deprez157378f2022-04-04 15:47:50 +020033struct pinned_obj_table link_table;
34struct obj_refs_table refs_table;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035
36static void __noreturn clean_and_exit(int i)
37{
38 if (json_output)
39 jsonw_destroy(&json_wtr);
40
41 exit(i);
42}
43
44void usage(void)
45{
46 last_do_help(last_argc - 1, last_argv + 1);
47
48 clean_and_exit(-1);
49}
50
51static int do_help(int argc, char **argv)
52{
53 if (json_output) {
54 jsonw_null(json_wtr);
55 return 0;
56 }
57
58 fprintf(stderr,
59 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
60 " %s batch file FILE\n"
61 " %s version\n"
62 "\n"
Olivier Deprez157378f2022-04-04 15:47:50 +020063 " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 " " HELP_SPEC_OPTIONS "\n"
65 "",
66 bin_name, bin_name, bin_name);
67
68 return 0;
69}
70
71static int do_version(int argc, char **argv)
72{
Olivier Deprez157378f2022-04-04 15:47:50 +020073#ifdef HAVE_LIBBFD_SUPPORT
74 const bool has_libbfd = true;
75#else
76 const bool has_libbfd = false;
77#endif
78#ifdef BPFTOOL_WITHOUT_SKELETONS
79 const bool has_skeletons = false;
80#else
81 const bool has_skeletons = true;
82#endif
83
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 if (json_output) {
Olivier Deprez157378f2022-04-04 15:47:50 +020085 jsonw_start_object(json_wtr); /* root object */
86
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 jsonw_name(json_wtr, "version");
88 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
Olivier Deprez157378f2022-04-04 15:47:50 +020089
90 jsonw_name(json_wtr, "features");
91 jsonw_start_object(json_wtr); /* features */
92 jsonw_bool_field(json_wtr, "libbfd", has_libbfd);
93 jsonw_bool_field(json_wtr, "skeletons", has_skeletons);
94 jsonw_end_object(json_wtr); /* features */
95
96 jsonw_end_object(json_wtr); /* root object */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +020098 unsigned int nb_features = 0;
99
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
Olivier Deprez157378f2022-04-04 15:47:50 +0200101 printf("features:");
102 if (has_libbfd) {
103 printf(" libbfd");
104 nb_features++;
105 }
106 if (has_skeletons)
107 printf("%s skeletons", nb_features++ ? "," : "");
108 printf("\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109 }
110 return 0;
111}
112
113int cmd_select(const struct cmd *cmds, int argc, char **argv,
114 int (*help)(int argc, char **argv))
115{
116 unsigned int i;
117
118 last_argc = argc;
119 last_argv = argv;
120 last_do_help = help;
121
122 if (argc < 1 && cmds[0].func)
123 return cmds[0].func(argc, argv);
124
Olivier Deprez157378f2022-04-04 15:47:50 +0200125 for (i = 0; cmds[i].cmd; i++) {
126 if (is_prefix(*argv, cmds[i].cmd)) {
127 if (!cmds[i].func) {
128 p_err("command '%s' is not supported in bootstrap mode",
129 cmds[i].cmd);
130 return -1;
131 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 return cmds[i].func(argc - 1, argv + 1);
Olivier Deprez157378f2022-04-04 15:47:50 +0200133 }
134 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135
136 help(argc - 1, argv + 1);
137
138 return -1;
139}
140
141bool is_prefix(const char *pfx, const char *str)
142{
143 if (!pfx)
144 return false;
145 if (strlen(str) < strlen(pfx))
146 return false;
147
148 return !memcmp(str, pfx, strlen(pfx));
149}
150
David Brazdil0f672f62019-12-10 10:32:29 +0000151/* Last argument MUST be NULL pointer */
152int detect_common_prefix(const char *arg, ...)
153{
154 unsigned int count = 0;
155 const char *ref;
156 char msg[256];
157 va_list ap;
158
159 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg);
160 va_start(ap, arg);
161 while ((ref = va_arg(ap, const char *))) {
162 if (!is_prefix(arg, ref))
163 continue;
164 count++;
165 if (count > 1)
166 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1);
167 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1);
168 }
169 va_end(ap);
170 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1);
171
172 if (count >= 2) {
173 p_err("%s", msg);
174 return -1;
175 }
176
177 return 0;
178}
179
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
181{
182 unsigned char *data = arg;
183 unsigned int i;
184
185 for (i = 0; i < n; i++) {
186 const char *pfx = "";
187
188 if (!i)
189 /* nothing */;
190 else if (!(i % 16))
191 fprintf(f, "\n");
192 else if (!(i % 8))
193 fprintf(f, " ");
194 else
195 pfx = sep;
196
197 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
198 }
199}
200
201/* Split command line into argument vector. */
202static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
203{
204 static const char ws[] = " \t\r\n";
205 char *cp = line;
206 int n_argc = 0;
207
208 while (*cp) {
209 /* Skip leading whitespace. */
210 cp += strspn(cp, ws);
211
212 if (*cp == '\0')
213 break;
214
215 if (n_argc >= (maxargs - 1)) {
216 p_err("too many arguments to command %d", cmd_nb);
217 return -1;
218 }
219
220 /* Word begins with quote. */
221 if (*cp == '\'' || *cp == '"') {
222 char quote = *cp++;
223
224 n_argv[n_argc++] = cp;
225 /* Find ending quote. */
226 cp = strchr(cp, quote);
227 if (!cp) {
228 p_err("unterminated quoted string in command %d",
229 cmd_nb);
230 return -1;
231 }
232 } else {
233 n_argv[n_argc++] = cp;
234
235 /* Find end of word. */
236 cp += strcspn(cp, ws);
237 if (*cp == '\0')
238 break;
239 }
240
241 /* Separate words. */
242 *cp++ = 0;
243 }
244 n_argv[n_argc] = NULL;
245
246 return n_argc;
247}
248
249static int do_batch(int argc, char **argv);
250
251static const struct cmd cmds[] = {
252 { "help", do_help },
253 { "batch", do_batch },
254 { "prog", do_prog },
255 { "map", do_map },
Olivier Deprez157378f2022-04-04 15:47:50 +0200256 { "link", do_link },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 { "cgroup", do_cgroup },
258 { "perf", do_perf },
David Brazdil0f672f62019-12-10 10:32:29 +0000259 { "net", do_net },
260 { "feature", do_feature },
261 { "btf", do_btf },
Olivier Deprez157378f2022-04-04 15:47:50 +0200262 { "gen", do_gen },
263 { "struct_ops", do_struct_ops },
264 { "iter", do_iter },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265 { "version", do_version },
266 { 0 }
267};
268
269static int do_batch(int argc, char **argv)
270{
271 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
272 char *n_argv[BATCH_ARG_NB_MAX];
273 unsigned int lines = 0;
274 int n_argc;
275 FILE *fp;
276 char *cp;
Olivier Deprez157378f2022-04-04 15:47:50 +0200277 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 int i;
279
280 if (argc < 2) {
281 p_err("too few parameters for batch");
282 return -1;
283 } else if (!is_prefix(*argv, "file")) {
284 p_err("expected 'file', got: %s", *argv);
285 return -1;
286 } else if (argc > 2) {
287 p_err("too many parameters for batch");
288 return -1;
289 }
290 NEXT_ARG();
291
292 if (!strcmp(*argv, "-"))
293 fp = stdin;
294 else
295 fp = fopen(*argv, "r");
296 if (!fp) {
297 p_err("Can't open file (%s): %s", *argv, strerror(errno));
298 return -1;
299 }
300
301 if (json_output)
302 jsonw_start_array(json_wtr);
303 while (fgets(buf, sizeof(buf), fp)) {
304 cp = strchr(buf, '#');
305 if (cp)
306 *cp = '\0';
307
308 if (strlen(buf) == sizeof(buf) - 1) {
309 errno = E2BIG;
310 break;
311 }
312
313 /* Append continuation lines if any (coming after a line ending
314 * with '\' in the batch file).
315 */
316 while ((cp = strstr(buf, "\\\n")) != NULL) {
317 if (!fgets(contline, sizeof(contline), fp) ||
318 strlen(contline) == 0) {
319 p_err("missing continuation line on command %d",
320 lines);
321 err = -1;
322 goto err_close;
323 }
324
325 cp = strchr(contline, '#');
326 if (cp)
327 *cp = '\0';
328
329 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
330 p_err("command %d is too long", lines);
331 err = -1;
332 goto err_close;
333 }
334 buf[strlen(buf) - 2] = '\0';
335 strcat(buf, contline);
336 }
337
338 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
339 if (!n_argc)
340 continue;
Olivier Deprez0e641232021-09-23 10:07:05 +0200341 if (n_argc < 0) {
342 err = n_argc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343 goto err_close;
Olivier Deprez0e641232021-09-23 10:07:05 +0200344 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345
346 if (json_output) {
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");
354 }
355
356 err = cmd_select(cmds, n_argc, n_argv, do_help);
357
358 if (json_output)
359 jsonw_end_object(json_wtr);
360
361 if (err)
362 goto err_close;
363
364 lines++;
365 }
366
367 if (errno && errno != ENOENT) {
368 p_err("reading batch file failed: %s", strerror(errno));
369 err = -1;
370 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000371 if (!json_output)
372 printf("processed %d commands\n", lines);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000373 }
374err_close:
375 if (fp != stdin)
376 fclose(fp);
377
378 if (json_output)
379 jsonw_end_array(json_wtr);
380
381 return err;
382}
383
384int main(int argc, char **argv)
385{
386 static const struct option options[] = {
387 { "json", no_argument, NULL, 'j' },
388 { "help", no_argument, NULL, 'h' },
389 { "pretty", no_argument, NULL, 'p' },
390 { "version", no_argument, NULL, 'V' },
391 { "bpffs", no_argument, NULL, 'f' },
David Brazdil0f672f62019-12-10 10:32:29 +0000392 { "mapcompat", no_argument, NULL, 'm' },
393 { "nomount", no_argument, NULL, 'n' },
394 { "debug", no_argument, NULL, 'd' },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395 { 0 }
396 };
397 int opt, ret;
398
Olivier Deprez157378f2022-04-04 15:47:50 +0200399 setlinebuf(stdout);
400
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 last_do_help = do_help;
402 pretty_output = false;
403 json_output = false;
404 show_pinned = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000405 block_mount = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406 bin_name = argv[0];
407
408 hash_init(prog_table.table);
409 hash_init(map_table.table);
Olivier Deprez157378f2022-04-04 15:47:50 +0200410 hash_init(link_table.table);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411
412 opterr = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000413 while ((opt = getopt_long(argc, argv, "Vhpjfmnd",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414 options, NULL)) >= 0) {
415 switch (opt) {
416 case 'V':
417 return do_version(argc, argv);
418 case 'h':
419 return do_help(argc, argv);
420 case 'p':
421 pretty_output = true;
422 /* fall through */
423 case 'j':
424 if (!json_output) {
425 json_wtr = jsonw_new(stdout);
426 if (!json_wtr) {
427 p_err("failed to create JSON writer");
428 return -1;
429 }
430 json_output = true;
431 }
432 jsonw_pretty(json_wtr, pretty_output);
433 break;
434 case 'f':
435 show_pinned = true;
436 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000437 case 'm':
Olivier Deprez157378f2022-04-04 15:47:50 +0200438 relaxed_maps = true;
David Brazdil0f672f62019-12-10 10:32:29 +0000439 break;
440 case 'n':
441 block_mount = true;
442 break;
443 case 'd':
444 libbpf_set_print(print_all_levels);
445 verifier_logs = true;
446 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000447 default:
448 p_err("unrecognized option '%s'", argv[optind - 1]);
449 if (json_output)
450 clean_and_exit(-1);
451 else
452 usage();
453 }
454 }
455
456 argc -= optind;
457 argv += optind;
458 if (argc < 0)
459 usage();
460
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461 ret = cmd_select(cmds, argc, argv, do_help);
462
463 if (json_output)
464 jsonw_destroy(&json_wtr);
465
466 if (show_pinned) {
467 delete_pinned_obj_table(&prog_table);
468 delete_pinned_obj_table(&map_table);
Olivier Deprez157378f2022-04-04 15:47:50 +0200469 delete_pinned_obj_table(&link_table);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000470 }
471
472 return ret;
473}