Jakub Kicinski | 02ff58d | 2018-12-12 19:59:25 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
| 2 | /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 3 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 4 | #include <ctype.h> |
| 5 | #include <errno.h> |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 6 | #include <getopt.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 7 | #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 Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 16 | #define BATCH_LINE_LEN_MAX 65536 |
| 17 | #define BATCH_ARG_NB_MAX 4096 |
| 18 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 19 | const char *bin_name; |
| 20 | static int last_argc; |
| 21 | static char **last_argv; |
| 22 | static int (*last_do_help)(int argc, char **argv); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 23 | json_writer_t *json_wtr; |
| 24 | bool pretty_output; |
| 25 | bool json_output; |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 26 | bool show_pinned; |
Quentin Monnet | 3322130 | 2018-12-18 10:13:19 +0000 | [diff] [blame] | 27 | bool block_mount; |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 28 | int bpf_flags; |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 29 | struct pinned_obj_table prog_table; |
| 30 | struct pinned_obj_table map_table; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 31 | |
Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 32 | static void __noreturn clean_and_exit(int i) |
| 33 | { |
| 34 | if (json_output) |
| 35 | jsonw_destroy(&json_wtr); |
| 36 | |
| 37 | exit(i); |
| 38 | } |
| 39 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 40 | void usage(void) |
| 41 | { |
| 42 | last_do_help(last_argc - 1, last_argv + 1); |
| 43 | |
Quentin Monnet | 7868620 | 2017-11-28 17:44:29 -0800 | [diff] [blame] | 44 | clean_and_exit(-1); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int do_help(int argc, char **argv) |
| 48 | { |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 49 | if (json_output) { |
| 50 | jsonw_null(json_wtr); |
| 51 | return 0; |
| 52 | } |
| 53 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 54 | fprintf(stderr, |
Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame] | 55 | "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 56 | " %s batch file FILE\n" |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 57 | " %s version\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 58 | "\n" |
Andrii Nakryiko | c93cc69 | 2019-04-25 15:30:08 -0700 | [diff] [blame^] | 59 | " OBJECT := { prog | map | cgroup | perf | net | feature | btf }\n" |
Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame] | 60 | " " HELP_SPEC_OPTIONS "\n" |
| 61 | "", |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 62 | bin_name, bin_name, bin_name); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 67 | static int do_version(int argc, char **argv) |
| 68 | { |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 69 | if (json_output) { |
| 70 | jsonw_start_object(json_wtr); |
| 71 | jsonw_name(json_wtr, "version"); |
Roman Gushchin | 4bfe3bd | 2017-12-27 19:16:28 +0000 | [diff] [blame] | 72 | jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION); |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 73 | jsonw_end_object(json_wtr); |
| 74 | } else { |
Roman Gushchin | 4bfe3bd | 2017-12-27 19:16:28 +0000 | [diff] [blame] | 75 | printf("%s v%s\n", bin_name, BPFTOOL_VERSION); |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 76 | } |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 80 | int 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 | |
| 101 | bool 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 Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 111 | void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep) |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 112 | { |
| 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 Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 122 | fprintf(f, "\n"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 123 | else if (!(i % 8)) |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 124 | fprintf(f, " "); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 125 | else |
| 126 | pfx = sep; |
| 127 | |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 128 | fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 132 | /* Split command line into argument vector. */ |
| 133 | static 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 Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 180 | static int do_batch(int argc, char **argv); |
| 181 | |
| 182 | static const struct cmd cmds[] = { |
| 183 | { "help", do_help }, |
| 184 | { "batch", do_batch }, |
| 185 | { "prog", do_prog }, |
| 186 | { "map", do_map }, |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 187 | { "cgroup", do_cgroup }, |
Yonghong Song | b04df40 | 2018-05-24 11:21:58 -0700 | [diff] [blame] | 188 | { "perf", do_perf }, |
Yonghong Song | f6f3bac | 2018-09-05 16:58:06 -0700 | [diff] [blame] | 189 | { "net", do_net }, |
Quentin Monnet | 49eb7ab | 2019-01-17 15:27:50 +0000 | [diff] [blame] | 190 | { "feature", do_feature }, |
Andrii Nakryiko | c93cc69 | 2019-04-25 15:30:08 -0700 | [diff] [blame^] | 191 | { "btf", do_btf }, |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 192 | { "version", do_version }, |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 193 | { 0 } |
| 194 | }; |
| 195 | |
| 196 | static int do_batch(int argc, char **argv) |
| 197 | { |
Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 198 | char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX]; |
| 199 | char *n_argv[BATCH_ARG_NB_MAX]; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 200 | unsigned int lines = 0; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 201 | int n_argc; |
| 202 | FILE *fp; |
Quentin Monnet | 06cc7fe | 2018-03-01 20:20:08 -0800 | [diff] [blame] | 203 | char *cp; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 204 | int err; |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 205 | int i; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 206 | |
| 207 | if (argc < 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 208 | p_err("too few parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 209 | return -1; |
| 210 | } else if (!is_prefix(*argv, "file")) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 211 | p_err("expected 'file', got: %s", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 212 | return -1; |
| 213 | } else if (argc > 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 214 | p_err("too many parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | NEXT_ARG(); |
| 218 | |
Quentin Monnet | 416656b | 2018-03-01 20:20:10 -0800 | [diff] [blame] | 219 | if (!strcmp(*argv, "-")) |
| 220 | fp = stdin; |
| 221 | else |
| 222 | fp = fopen(*argv, "r"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 223 | if (!fp) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 224 | p_err("Can't open file (%s): %s", *argv, strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 228 | if (json_output) |
| 229 | jsonw_start_array(json_wtr); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 230 | while (fgets(buf, sizeof(buf), fp)) { |
Quentin Monnet | 06cc7fe | 2018-03-01 20:20:08 -0800 | [diff] [blame] | 231 | cp = strchr(buf, '#'); |
| 232 | if (cp) |
| 233 | *cp = '\0'; |
| 234 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 235 | if (strlen(buf) == sizeof(buf) - 1) { |
| 236 | errno = E2BIG; |
| 237 | break; |
| 238 | } |
| 239 | |
Quentin Monnet | 65d538d | 2018-03-01 20:20:09 -0800 | [diff] [blame] | 240 | /* Append continuation lines if any (coming after a line ending |
| 241 | * with '\' in the batch file). |
| 242 | */ |
| 243 | while ((cp = strstr(buf, "\\\n")) != NULL) { |
| 244 | if (!fgets(contline, sizeof(contline), fp) || |
| 245 | strlen(contline) == 0) { |
| 246 | p_err("missing continuation line on command %d", |
| 247 | lines); |
| 248 | err = -1; |
| 249 | goto err_close; |
| 250 | } |
| 251 | |
| 252 | cp = strchr(contline, '#'); |
| 253 | if (cp) |
| 254 | *cp = '\0'; |
| 255 | |
| 256 | if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) { |
| 257 | p_err("command %d is too long", lines); |
| 258 | err = -1; |
| 259 | goto err_close; |
| 260 | } |
| 261 | buf[strlen(buf) - 2] = '\0'; |
| 262 | strcat(buf, contline); |
| 263 | } |
| 264 | |
Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 265 | n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 266 | if (!n_argc) |
| 267 | continue; |
Quentin Monnet | 668da74 | 2018-03-01 20:20:11 -0800 | [diff] [blame] | 268 | if (n_argc < 0) |
| 269 | goto err_close; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 270 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 271 | if (json_output) { |
| 272 | jsonw_start_object(json_wtr); |
| 273 | jsonw_name(json_wtr, "command"); |
| 274 | jsonw_start_array(json_wtr); |
| 275 | for (i = 0; i < n_argc; i++) |
| 276 | jsonw_string(json_wtr, n_argv[i]); |
| 277 | jsonw_end_array(json_wtr); |
| 278 | jsonw_name(json_wtr, "output"); |
| 279 | } |
| 280 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 281 | err = cmd_select(cmds, n_argc, n_argv, do_help); |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 282 | |
| 283 | if (json_output) |
| 284 | jsonw_end_object(json_wtr); |
| 285 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 286 | if (err) |
| 287 | goto err_close; |
| 288 | |
| 289 | lines++; |
| 290 | } |
| 291 | |
| 292 | if (errno && errno != ENOENT) { |
Quentin Monnet | 9be6d41 | 2018-02-14 22:42:55 -0800 | [diff] [blame] | 293 | p_err("reading batch file failed: %s", strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 294 | err = -1; |
| 295 | } else { |
Quentin Monnet | 06ee9da | 2018-10-20 23:01:49 +0100 | [diff] [blame] | 296 | if (!json_output) |
| 297 | printf("processed %d commands\n", lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 298 | err = 0; |
| 299 | } |
| 300 | err_close: |
Quentin Monnet | 416656b | 2018-03-01 20:20:10 -0800 | [diff] [blame] | 301 | if (fp != stdin) |
| 302 | fclose(fp); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 303 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 304 | if (json_output) |
| 305 | jsonw_end_array(json_wtr); |
| 306 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 307 | return err; |
| 308 | } |
| 309 | |
| 310 | int main(int argc, char **argv) |
| 311 | { |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 312 | static const struct option options[] = { |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 313 | { "json", no_argument, NULL, 'j' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 314 | { "help", no_argument, NULL, 'h' }, |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 315 | { "pretty", no_argument, NULL, 'p' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 316 | { "version", no_argument, NULL, 'V' }, |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 317 | { "bpffs", no_argument, NULL, 'f' }, |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 318 | { "mapcompat", no_argument, NULL, 'm' }, |
Quentin Monnet | 3322130 | 2018-12-18 10:13:19 +0000 | [diff] [blame] | 319 | { "nomount", no_argument, NULL, 'n' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 320 | { 0 } |
| 321 | }; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 322 | int opt, ret; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 323 | |
| 324 | last_do_help = do_help; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 325 | pretty_output = false; |
| 326 | json_output = false; |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 327 | show_pinned = false; |
Quentin Monnet | 3322130 | 2018-12-18 10:13:19 +0000 | [diff] [blame] | 328 | block_mount = false; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 329 | bin_name = argv[0]; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 330 | |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 331 | hash_init(prog_table.table); |
| 332 | hash_init(map_table.table); |
| 333 | |
Quentin Monnet | 146882a | 2017-11-28 17:44:30 -0800 | [diff] [blame] | 334 | opterr = 0; |
Quentin Monnet | 3322130 | 2018-12-18 10:13:19 +0000 | [diff] [blame] | 335 | while ((opt = getopt_long(argc, argv, "Vhpjfmn", |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 336 | options, NULL)) >= 0) { |
| 337 | switch (opt) { |
| 338 | case 'V': |
| 339 | return do_version(argc, argv); |
| 340 | case 'h': |
| 341 | return do_help(argc, argv); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 342 | case 'p': |
| 343 | pretty_output = true; |
| 344 | /* fall through */ |
| 345 | case 'j': |
Quentin Monnet | 9b85c2d | 2017-11-28 17:44:28 -0800 | [diff] [blame] | 346 | if (!json_output) { |
| 347 | json_wtr = jsonw_new(stdout); |
| 348 | if (!json_wtr) { |
| 349 | p_err("failed to create JSON writer"); |
| 350 | return -1; |
| 351 | } |
| 352 | json_output = true; |
| 353 | } |
| 354 | jsonw_pretty(json_wtr, pretty_output); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 355 | break; |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 356 | case 'f': |
| 357 | show_pinned = true; |
| 358 | break; |
John Fastabend | c034a17 | 2018-10-15 11:19:55 -0700 | [diff] [blame] | 359 | case 'm': |
| 360 | bpf_flags = MAPS_RELAX_COMPAT; |
| 361 | break; |
Quentin Monnet | 3322130 | 2018-12-18 10:13:19 +0000 | [diff] [blame] | 362 | case 'n': |
| 363 | block_mount = true; |
| 364 | break; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 365 | default: |
Quentin Monnet | 146882a | 2017-11-28 17:44:30 -0800 | [diff] [blame] | 366 | p_err("unrecognized option '%s'", argv[optind - 1]); |
| 367 | if (json_output) |
| 368 | clean_and_exit(-1); |
| 369 | else |
| 370 | usage(); |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | argc -= optind; |
| 375 | argv += optind; |
| 376 | if (argc < 0) |
| 377 | usage(); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 378 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 379 | ret = cmd_select(cmds, argc, argv, do_help); |
| 380 | |
| 381 | if (json_output) |
| 382 | jsonw_destroy(&json_wtr); |
| 383 | |
Prashant Bhole | c541b73 | 2017-11-08 13:55:49 +0900 | [diff] [blame] | 384 | if (show_pinned) { |
| 385 | delete_pinned_obj_table(&prog_table); |
| 386 | delete_pinned_obj_table(&map_table); |
| 387 | } |
Prashant Bhole | 4990f1f | 2017-11-08 13:55:48 +0900 | [diff] [blame] | 388 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 389 | return ret; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 390 | } |