1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
16 #define BATCH_LINE_LEN_MAX 65536
17 #define BATCH_ARG_NB_MAX 4096
21 static char **last_argv
;
22 static int (*last_do_help
)(int argc
, char **argv
);
23 json_writer_t
*json_wtr
;
29 struct pinned_obj_table prog_table
;
30 struct pinned_obj_table map_table
;
32 static void __noreturn
clean_and_exit(int i
)
35 jsonw_destroy(&json_wtr
);
42 last_do_help(last_argc
- 1, last_argv
+ 1);
47 static int do_help(int argc
, char **argv
)
55 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
56 " %s batch file FILE\n"
59 " OBJECT := { prog | map | cgroup | perf | net | feature }\n"
60 " " HELP_SPEC_OPTIONS
"\n"
62 bin_name
, bin_name
, bin_name
);
67 static int do_version(int argc
, char **argv
)
70 jsonw_start_object(json_wtr
);
71 jsonw_name(json_wtr
, "version");
72 jsonw_printf(json_wtr
, "\"%s\"", BPFTOOL_VERSION
);
73 jsonw_end_object(json_wtr
);
75 printf("%s v%s\n", bin_name
, BPFTOOL_VERSION
);
80 int cmd_select(const struct cmd
*cmds
, int argc
, char **argv
,
81 int (*help
)(int argc
, char **argv
))
89 if (argc
< 1 && cmds
[0].func
)
90 return cmds
[0].func(argc
, argv
);
92 for (i
= 0; cmds
[i
].func
; i
++)
93 if (is_prefix(*argv
, cmds
[i
].cmd
))
94 return cmds
[i
].func(argc
- 1, argv
+ 1);
96 help(argc
- 1, argv
+ 1);
101 bool is_prefix(const char *pfx
, const char *str
)
105 if (strlen(str
) < strlen(pfx
))
108 return !memcmp(str
, pfx
, strlen(pfx
));
111 void fprint_hex(FILE *f
, void *arg
, unsigned int n
, const char *sep
)
113 unsigned char *data
= arg
;
116 for (i
= 0; i
< n
; i
++) {
117 const char *pfx
= "";
128 fprintf(f
, "%s%02hhx", i
? pfx
: "", data
[i
]);
132 /* Split command line into argument vector. */
133 static int make_args(char *line
, char *n_argv
[], int maxargs
, int cmd_nb
)
135 static const char ws
[] = " \t\r\n";
140 /* Skip leading whitespace. */
141 cp
+= strspn(cp
, ws
);
146 if (n_argc
>= (maxargs
- 1)) {
147 p_err("too many arguments to command %d", cmd_nb
);
151 /* Word begins with quote. */
152 if (*cp
== '\'' || *cp
== '"') {
155 n_argv
[n_argc
++] = cp
;
156 /* Find ending quote. */
157 cp
= strchr(cp
, quote
);
159 p_err("unterminated quoted string in command %d",
164 n_argv
[n_argc
++] = cp
;
166 /* Find end of word. */
167 cp
+= strcspn(cp
, ws
);
172 /* Separate words. */
175 n_argv
[n_argc
] = NULL
;
180 static int do_batch(int argc
, char **argv
);
182 static const struct cmd cmds
[] = {
184 { "batch", do_batch
},
187 { "cgroup", do_cgroup
},
190 { "feature", do_feature
},
191 { "version", do_version
},
195 static int do_batch(int argc
, char **argv
)
197 char buf
[BATCH_LINE_LEN_MAX
], contline
[BATCH_LINE_LEN_MAX
];
198 char *n_argv
[BATCH_ARG_NB_MAX
];
199 unsigned int lines
= 0;
207 p_err("too few parameters for batch");
209 } else if (!is_prefix(*argv
, "file")) {
210 p_err("expected 'file', got: %s", *argv
);
212 } else if (argc
> 2) {
213 p_err("too many parameters for batch");
218 if (!strcmp(*argv
, "-"))
221 fp
= fopen(*argv
, "r");
223 p_err("Can't open file (%s): %s", *argv
, strerror(errno
));
228 jsonw_start_array(json_wtr
);
229 while (fgets(buf
, sizeof(buf
), fp
)) {
230 cp
= strchr(buf
, '#');
234 if (strlen(buf
) == sizeof(buf
) - 1) {
239 /* Append continuation lines if any (coming after a line ending
240 * with '\' in the batch file).
242 while ((cp
= strstr(buf
, "\\\n")) != NULL
) {
243 if (!fgets(contline
, sizeof(contline
), fp
) ||
244 strlen(contline
) == 0) {
245 p_err("missing continuation line on command %d",
251 cp
= strchr(contline
, '#');
255 if (strlen(buf
) + strlen(contline
) + 1 > sizeof(buf
)) {
256 p_err("command %d is too long", lines
);
260 buf
[strlen(buf
) - 2] = '\0';
261 strcat(buf
, contline
);
264 n_argc
= make_args(buf
, n_argv
, BATCH_ARG_NB_MAX
, lines
);
271 jsonw_start_object(json_wtr
);
272 jsonw_name(json_wtr
, "command");
273 jsonw_start_array(json_wtr
);
274 for (i
= 0; i
< n_argc
; i
++)
275 jsonw_string(json_wtr
, n_argv
[i
]);
276 jsonw_end_array(json_wtr
);
277 jsonw_name(json_wtr
, "output");
280 err
= cmd_select(cmds
, n_argc
, n_argv
, do_help
);
283 jsonw_end_object(json_wtr
);
291 if (errno
&& errno
!= ENOENT
) {
292 p_err("reading batch file failed: %s", strerror(errno
));
296 printf("processed %d commands\n", lines
);
304 jsonw_end_array(json_wtr
);
309 int main(int argc
, char **argv
)
311 static const struct option options
[] = {
312 { "json", no_argument
, NULL
, 'j' },
313 { "help", no_argument
, NULL
, 'h' },
314 { "pretty", no_argument
, NULL
, 'p' },
315 { "version", no_argument
, NULL
, 'V' },
316 { "bpffs", no_argument
, NULL
, 'f' },
317 { "mapcompat", no_argument
, NULL
, 'm' },
318 { "nomount", no_argument
, NULL
, 'n' },
323 last_do_help
= do_help
;
324 pretty_output
= false;
330 hash_init(prog_table
.table
);
331 hash_init(map_table
.table
);
334 while ((opt
= getopt_long(argc
, argv
, "Vhpjfmn",
335 options
, NULL
)) >= 0) {
338 return do_version(argc
, argv
);
340 return do_help(argc
, argv
);
342 pretty_output
= true;
346 json_wtr
= jsonw_new(stdout
);
348 p_err("failed to create JSON writer");
353 jsonw_pretty(json_wtr
, pretty_output
);
359 bpf_flags
= MAPS_RELAX_COMPAT
;
365 p_err("unrecognized option '%s'", argv
[optind
- 1]);
378 ret
= cmd_select(cmds
, argc
, argv
, do_help
);
381 jsonw_destroy(&json_wtr
);
384 delete_pinned_obj_table(&prog_table
);
385 delete_pinned_obj_table(&map_table
);