blob: a9d5e9e6a7327d9a8af33825e8daa93f4c06fc4e [file] [log] [blame]
Jakub Kicinski02ff58d2018-12-12 19:59:25 -08001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003
Jakub Kicinski71bb4282017-10-04 20:10:04 -07004#include <ctype.h>
5#include <errno.h>
Quentin Monneta2bc2e52017-10-23 09:24:06 -07006#include <getopt.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -07007#include <linux/bpf.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <bpf.h>
13
14#include "main.h"
15
Quentin Monnet65d538d2018-03-01 20:20:09 -080016#define BATCH_LINE_LEN_MAX 65536
17#define BATCH_ARG_NB_MAX 4096
18
Jakub Kicinski71bb4282017-10-04 20:10:04 -070019const char *bin_name;
20static int last_argc;
21static char **last_argv;
22static int (*last_do_help)(int argc, char **argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -070023json_writer_t *json_wtr;
24bool pretty_output;
25bool json_output;
Prashant Bholec541b732017-11-08 13:55:49 +090026bool show_pinned;
Quentin Monnet33221302018-12-18 10:13:19 +000027bool block_mount;
John Fastabendc034a172018-10-15 11:19:55 -070028int bpf_flags;
Prashant Bhole4990f1f2017-11-08 13:55:48 +090029struct pinned_obj_table prog_table;
30struct pinned_obj_table map_table;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070031
Quentin Monnet78686202017-11-28 17:44:29 -080032static void __noreturn clean_and_exit(int i)
33{
34 if (json_output)
35 jsonw_destroy(&json_wtr);
36
37 exit(i);
38}
39
Jakub Kicinski71bb4282017-10-04 20:10:04 -070040void usage(void)
41{
42 last_do_help(last_argc - 1, last_argv + 1);
43
Quentin Monnet78686202017-11-28 17:44:29 -080044 clean_and_exit(-1);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070045}
46
47static int do_help(int argc, char **argv)
48{
Quentin Monnet004b45c2017-10-23 09:24:14 -070049 if (json_output) {
50 jsonw_null(json_wtr);
51 return 0;
52 }
53
Jakub Kicinski71bb4282017-10-04 20:10:04 -070054 fprintf(stderr,
Quentin Monnet0641c3c2017-10-23 09:24:16 -070055 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070056 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070057 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070058 "\n"
Quentin Monnet49eb7ab2019-01-17 15:27:50 +000059 " OBJECT := { prog | map | cgroup | perf | net | feature }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070060 " " HELP_SPEC_OPTIONS "\n"
61 "",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070062 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070063
64 return 0;
65}
66
Quentin Monnet821cfbb2017-10-19 15:46:26 -070067static int do_version(int argc, char **argv)
68{
Quentin Monnet004b45c2017-10-23 09:24:14 -070069 if (json_output) {
70 jsonw_start_object(json_wtr);
71 jsonw_name(json_wtr, "version");
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +000072 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
Quentin Monnet004b45c2017-10-23 09:24:14 -070073 jsonw_end_object(json_wtr);
74 } else {
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +000075 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
Quentin Monnet004b45c2017-10-23 09:24:14 -070076 }
Quentin Monnet821cfbb2017-10-19 15:46:26 -070077 return 0;
78}
79
Jakub Kicinski71bb4282017-10-04 20:10:04 -070080int cmd_select(const struct cmd *cmds, int argc, char **argv,
81 int (*help)(int argc, char **argv))
82{
83 unsigned int i;
84
85 last_argc = argc;
86 last_argv = argv;
87 last_do_help = help;
88
89 if (argc < 1 && cmds[0].func)
90 return cmds[0].func(argc, argv);
91
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);
95
96 help(argc - 1, argv + 1);
97
98 return -1;
99}
100
101bool is_prefix(const char *pfx, const char *str)
102{
103 if (!pfx)
104 return false;
105 if (strlen(str) < strlen(pfx))
106 return false;
107
108 return !memcmp(str, pfx, strlen(pfx));
109}
110
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700111void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700112{
113 unsigned char *data = arg;
114 unsigned int i;
115
116 for (i = 0; i < n; i++) {
117 const char *pfx = "";
118
119 if (!i)
120 /* nothing */;
121 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700122 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700123 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700124 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700125 else
126 pfx = sep;
127
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700128 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700129 }
130}
131
Quentin Monnet668da742018-03-01 20:20:11 -0800132/* Split command line into argument vector. */
133static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
134{
135 static const char ws[] = " \t\r\n";
136 char *cp = line;
137 int n_argc = 0;
138
139 while (*cp) {
140 /* Skip leading whitespace. */
141 cp += strspn(cp, ws);
142
143 if (*cp == '\0')
144 break;
145
146 if (n_argc >= (maxargs - 1)) {
147 p_err("too many arguments to command %d", cmd_nb);
148 return -1;
149 }
150
151 /* Word begins with quote. */
152 if (*cp == '\'' || *cp == '"') {
153 char quote = *cp++;
154
155 n_argv[n_argc++] = cp;
156 /* Find ending quote. */
157 cp = strchr(cp, quote);
158 if (!cp) {
159 p_err("unterminated quoted string in command %d",
160 cmd_nb);
161 return -1;
162 }
163 } else {
164 n_argv[n_argc++] = cp;
165
166 /* Find end of word. */
167 cp += strcspn(cp, ws);
168 if (*cp == '\0')
169 break;
170 }
171
172 /* Separate words. */
173 *cp++ = 0;
174 }
175 n_argv[n_argc] = NULL;
176
177 return n_argc;
178}
179
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700180static int do_batch(int argc, char **argv);
181
182static const struct cmd cmds[] = {
183 { "help", do_help },
184 { "batch", do_batch },
185 { "prog", do_prog },
186 { "map", do_map },
Roman Gushchin5ccda642017-12-13 15:18:54 +0000187 { "cgroup", do_cgroup },
Yonghong Songb04df402018-05-24 11:21:58 -0700188 { "perf", do_perf },
Yonghong Songf6f3bac2018-09-05 16:58:06 -0700189 { "net", do_net },
Quentin Monnet49eb7ab2019-01-17 15:27:50 +0000190 { "feature", do_feature },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700191 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700192 { 0 }
193};
194
195static int do_batch(int argc, char **argv)
196{
Quentin Monnet65d538d2018-03-01 20:20:09 -0800197 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
198 char *n_argv[BATCH_ARG_NB_MAX];
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700199 unsigned int lines = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700200 int n_argc;
201 FILE *fp;
Quentin Monnet06cc7fe2018-03-01 20:20:08 -0800202 char *cp;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700203 int err;
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700204 int i;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700205
206 if (argc < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700207 p_err("too few parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700208 return -1;
209 } else if (!is_prefix(*argv, "file")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700210 p_err("expected 'file', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700211 return -1;
212 } else if (argc > 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700213 p_err("too many parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700214 return -1;
215 }
216 NEXT_ARG();
217
Quentin Monnet416656b2018-03-01 20:20:10 -0800218 if (!strcmp(*argv, "-"))
219 fp = stdin;
220 else
221 fp = fopen(*argv, "r");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700222 if (!fp) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700223 p_err("Can't open file (%s): %s", *argv, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700224 return -1;
225 }
226
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700227 if (json_output)
228 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700229 while (fgets(buf, sizeof(buf), fp)) {
Quentin Monnet06cc7fe2018-03-01 20:20:08 -0800230 cp = strchr(buf, '#');
231 if (cp)
232 *cp = '\0';
233
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700234 if (strlen(buf) == sizeof(buf) - 1) {
235 errno = E2BIG;
236 break;
237 }
238
Quentin Monnet65d538d2018-03-01 20:20:09 -0800239 /* Append continuation lines if any (coming after a line ending
240 * with '\' in the batch file).
241 */
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",
246 lines);
247 err = -1;
248 goto err_close;
249 }
250
251 cp = strchr(contline, '#');
252 if (cp)
253 *cp = '\0';
254
255 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
256 p_err("command %d is too long", lines);
257 err = -1;
258 goto err_close;
259 }
260 buf[strlen(buf) - 2] = '\0';
261 strcat(buf, contline);
262 }
263
Quentin Monnet668da742018-03-01 20:20:11 -0800264 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700265 if (!n_argc)
266 continue;
Quentin Monnet668da742018-03-01 20:20:11 -0800267 if (n_argc < 0)
268 goto err_close;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700269
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700270 if (json_output) {
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");
278 }
279
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700280 err = cmd_select(cmds, n_argc, n_argv, do_help);
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700281
282 if (json_output)
283 jsonw_end_object(json_wtr);
284
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700285 if (err)
286 goto err_close;
287
288 lines++;
289 }
290
291 if (errno && errno != ENOENT) {
Quentin Monnet9be6d412018-02-14 22:42:55 -0800292 p_err("reading batch file failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700293 err = -1;
294 } else {
Quentin Monnet06ee9da2018-10-20 23:01:49 +0100295 if (!json_output)
296 printf("processed %d commands\n", lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700297 err = 0;
298 }
299err_close:
Quentin Monnet416656b2018-03-01 20:20:10 -0800300 if (fp != stdin)
301 fclose(fp);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700302
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700303 if (json_output)
304 jsonw_end_array(json_wtr);
305
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700306 return err;
307}
308
309int main(int argc, char **argv)
310{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700311 static const struct option options[] = {
Quentin Monnetd35efba2017-10-23 09:24:07 -0700312 { "json", no_argument, NULL, 'j' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700313 { "help", no_argument, NULL, 'h' },
Quentin Monnetd35efba2017-10-23 09:24:07 -0700314 { "pretty", no_argument, NULL, 'p' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700315 { "version", no_argument, NULL, 'V' },
Prashant Bholec541b732017-11-08 13:55:49 +0900316 { "bpffs", no_argument, NULL, 'f' },
John Fastabendc034a172018-10-15 11:19:55 -0700317 { "mapcompat", no_argument, NULL, 'm' },
Quentin Monnet33221302018-12-18 10:13:19 +0000318 { "nomount", no_argument, NULL, 'n' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700319 { 0 }
320 };
Quentin Monnetd35efba2017-10-23 09:24:07 -0700321 int opt, ret;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700322
323 last_do_help = do_help;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700324 pretty_output = false;
325 json_output = false;
Prashant Bholec541b732017-11-08 13:55:49 +0900326 show_pinned = false;
Quentin Monnet33221302018-12-18 10:13:19 +0000327 block_mount = false;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700328 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700329
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900330 hash_init(prog_table.table);
331 hash_init(map_table.table);
332
Quentin Monnet146882a2017-11-28 17:44:30 -0800333 opterr = 0;
Quentin Monnet33221302018-12-18 10:13:19 +0000334 while ((opt = getopt_long(argc, argv, "Vhpjfmn",
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700335 options, NULL)) >= 0) {
336 switch (opt) {
337 case 'V':
338 return do_version(argc, argv);
339 case 'h':
340 return do_help(argc, argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700341 case 'p':
342 pretty_output = true;
343 /* fall through */
344 case 'j':
Quentin Monnet9b85c2d2017-11-28 17:44:28 -0800345 if (!json_output) {
346 json_wtr = jsonw_new(stdout);
347 if (!json_wtr) {
348 p_err("failed to create JSON writer");
349 return -1;
350 }
351 json_output = true;
352 }
353 jsonw_pretty(json_wtr, pretty_output);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700354 break;
Prashant Bholec541b732017-11-08 13:55:49 +0900355 case 'f':
356 show_pinned = true;
357 break;
John Fastabendc034a172018-10-15 11:19:55 -0700358 case 'm':
359 bpf_flags = MAPS_RELAX_COMPAT;
360 break;
Quentin Monnet33221302018-12-18 10:13:19 +0000361 case 'n':
362 block_mount = true;
363 break;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700364 default:
Quentin Monnet146882a2017-11-28 17:44:30 -0800365 p_err("unrecognized option '%s'", argv[optind - 1]);
366 if (json_output)
367 clean_and_exit(-1);
368 else
369 usage();
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700370 }
371 }
372
373 argc -= optind;
374 argv += optind;
375 if (argc < 0)
376 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700377
Quentin Monnetd35efba2017-10-23 09:24:07 -0700378 ret = cmd_select(cmds, argc, argv, do_help);
379
380 if (json_output)
381 jsonw_destroy(&json_wtr);
382
Prashant Bholec541b732017-11-08 13:55:49 +0900383 if (show_pinned) {
384 delete_pinned_obj_table(&prog_table);
385 delete_pinned_obj_table(&map_table);
386 }
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900387
Quentin Monnetd35efba2017-10-23 09:24:07 -0700388 return ret;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700389}