Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. |
| 3 | * |
| 4 | * This software is dual licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| 6 | * source tree or the BSD 2-Clause License provided below. You have the |
| 7 | * option to license this software under the complete terms of either license. |
| 8 | * |
| 9 | * The BSD 2-Clause License: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * 2. Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ |
| 35 | |
| 36 | #include <bfd.h> |
| 37 | #include <ctype.h> |
| 38 | #include <errno.h> |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 39 | #include <getopt.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 40 | #include <linux/bpf.h> |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 41 | #include <linux/version.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | |
| 46 | #include <bpf.h> |
| 47 | |
| 48 | #include "main.h" |
| 49 | |
| 50 | const char *bin_name; |
| 51 | static int last_argc; |
| 52 | static char **last_argv; |
| 53 | static int (*last_do_help)(int argc, char **argv); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 54 | json_writer_t *json_wtr; |
| 55 | bool pretty_output; |
| 56 | bool json_output; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 57 | |
| 58 | void usage(void) |
| 59 | { |
| 60 | last_do_help(last_argc - 1, last_argv + 1); |
| 61 | |
| 62 | exit(-1); |
| 63 | } |
| 64 | |
| 65 | static int do_help(int argc, char **argv) |
| 66 | { |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 67 | if (json_output) { |
| 68 | jsonw_null(json_wtr); |
| 69 | return 0; |
| 70 | } |
| 71 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 72 | fprintf(stderr, |
Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame^] | 73 | "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 74 | " %s batch file FILE\n" |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 75 | " %s version\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 76 | "\n" |
Quentin Monnet | 0641c3c | 2017-10-23 09:24:16 -0700 | [diff] [blame^] | 77 | " OBJECT := { prog | map }\n" |
| 78 | " " HELP_SPEC_OPTIONS "\n" |
| 79 | "", |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 80 | bin_name, bin_name, bin_name); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 85 | static int do_version(int argc, char **argv) |
| 86 | { |
Quentin Monnet | 004b45c | 2017-10-23 09:24:14 -0700 | [diff] [blame] | 87 | unsigned int version[3]; |
| 88 | |
| 89 | version[0] = LINUX_VERSION_CODE >> 16; |
| 90 | version[1] = LINUX_VERSION_CODE >> 8 & 0xf; |
| 91 | version[2] = LINUX_VERSION_CODE & 0xf; |
| 92 | |
| 93 | if (json_output) { |
| 94 | jsonw_start_object(json_wtr); |
| 95 | jsonw_name(json_wtr, "version"); |
| 96 | jsonw_printf(json_wtr, "\"%u.%u.%u\"", |
| 97 | version[0], version[1], version[2]); |
| 98 | jsonw_end_object(json_wtr); |
| 99 | } else { |
| 100 | printf("%s v%u.%u.%u\n", bin_name, |
| 101 | version[0], version[1], version[2]); |
| 102 | } |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 106 | int cmd_select(const struct cmd *cmds, int argc, char **argv, |
| 107 | int (*help)(int argc, char **argv)) |
| 108 | { |
| 109 | unsigned int i; |
| 110 | |
| 111 | last_argc = argc; |
| 112 | last_argv = argv; |
| 113 | last_do_help = help; |
| 114 | |
| 115 | if (argc < 1 && cmds[0].func) |
| 116 | return cmds[0].func(argc, argv); |
| 117 | |
| 118 | for (i = 0; cmds[i].func; i++) |
| 119 | if (is_prefix(*argv, cmds[i].cmd)) |
| 120 | return cmds[i].func(argc - 1, argv + 1); |
| 121 | |
| 122 | help(argc - 1, argv + 1); |
| 123 | |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | bool is_prefix(const char *pfx, const char *str) |
| 128 | { |
| 129 | if (!pfx) |
| 130 | return false; |
| 131 | if (strlen(str) < strlen(pfx)) |
| 132 | return false; |
| 133 | |
| 134 | return !memcmp(str, pfx, strlen(pfx)); |
| 135 | } |
| 136 | |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 137 | 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] | 138 | { |
| 139 | unsigned char *data = arg; |
| 140 | unsigned int i; |
| 141 | |
| 142 | for (i = 0; i < n; i++) { |
| 143 | const char *pfx = ""; |
| 144 | |
| 145 | if (!i) |
| 146 | /* nothing */; |
| 147 | else if (!(i % 16)) |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 148 | fprintf(f, "\n"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 149 | else if (!(i % 8)) |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 150 | fprintf(f, " "); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 151 | else |
| 152 | pfx = sep; |
| 153 | |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 154 | fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | static int do_batch(int argc, char **argv); |
| 159 | |
| 160 | static const struct cmd cmds[] = { |
| 161 | { "help", do_help }, |
| 162 | { "batch", do_batch }, |
| 163 | { "prog", do_prog }, |
| 164 | { "map", do_map }, |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 165 | { "version", do_version }, |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 166 | { 0 } |
| 167 | }; |
| 168 | |
| 169 | static int do_batch(int argc, char **argv) |
| 170 | { |
| 171 | unsigned int lines = 0; |
| 172 | char *n_argv[4096]; |
| 173 | char buf[65536]; |
| 174 | int n_argc; |
| 175 | FILE *fp; |
| 176 | int err; |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 177 | int i; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 178 | |
| 179 | if (argc < 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 180 | p_err("too few parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 181 | return -1; |
| 182 | } else if (!is_prefix(*argv, "file")) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 183 | p_err("expected 'file', got: %s", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 184 | return -1; |
| 185 | } else if (argc > 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 186 | p_err("too many parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | NEXT_ARG(); |
| 190 | |
| 191 | fp = fopen(*argv, "r"); |
| 192 | if (!fp) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 193 | p_err("Can't open file (%s): %s", *argv, strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 197 | if (json_output) |
| 198 | jsonw_start_array(json_wtr); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 199 | while (fgets(buf, sizeof(buf), fp)) { |
| 200 | if (strlen(buf) == sizeof(buf) - 1) { |
| 201 | errno = E2BIG; |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | n_argc = 0; |
| 206 | n_argv[n_argc] = strtok(buf, " \t\n"); |
| 207 | |
| 208 | while (n_argv[n_argc]) { |
| 209 | n_argc++; |
| 210 | if (n_argc == ARRAY_SIZE(n_argv)) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 211 | p_err("line %d has too many arguments, skip", |
| 212 | lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 213 | n_argc = 0; |
| 214 | break; |
| 215 | } |
| 216 | n_argv[n_argc] = strtok(NULL, " \t\n"); |
| 217 | } |
| 218 | |
| 219 | if (!n_argc) |
| 220 | continue; |
| 221 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 222 | if (json_output) { |
| 223 | jsonw_start_object(json_wtr); |
| 224 | jsonw_name(json_wtr, "command"); |
| 225 | jsonw_start_array(json_wtr); |
| 226 | for (i = 0; i < n_argc; i++) |
| 227 | jsonw_string(json_wtr, n_argv[i]); |
| 228 | jsonw_end_array(json_wtr); |
| 229 | jsonw_name(json_wtr, "output"); |
| 230 | } |
| 231 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 232 | err = cmd_select(cmds, n_argc, n_argv, do_help); |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 233 | |
| 234 | if (json_output) |
| 235 | jsonw_end_object(json_wtr); |
| 236 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 237 | if (err) |
| 238 | goto err_close; |
| 239 | |
| 240 | lines++; |
| 241 | } |
| 242 | |
| 243 | if (errno && errno != ENOENT) { |
| 244 | perror("reading batch file failed"); |
| 245 | err = -1; |
| 246 | } else { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 247 | p_info("processed %d lines", lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 248 | err = 0; |
| 249 | } |
| 250 | err_close: |
| 251 | fclose(fp); |
| 252 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 253 | if (json_output) |
| 254 | jsonw_end_array(json_wtr); |
| 255 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 256 | return err; |
| 257 | } |
| 258 | |
| 259 | int main(int argc, char **argv) |
| 260 | { |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 261 | static const struct option options[] = { |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 262 | { "json", no_argument, NULL, 'j' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 263 | { "help", no_argument, NULL, 'h' }, |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 264 | { "pretty", no_argument, NULL, 'p' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 265 | { "version", no_argument, NULL, 'V' }, |
| 266 | { 0 } |
| 267 | }; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 268 | int opt, ret; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 269 | |
| 270 | last_do_help = do_help; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 271 | pretty_output = false; |
| 272 | json_output = false; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 273 | bin_name = argv[0]; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 274 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 275 | while ((opt = getopt_long(argc, argv, "Vhpj", |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 276 | options, NULL)) >= 0) { |
| 277 | switch (opt) { |
| 278 | case 'V': |
| 279 | return do_version(argc, argv); |
| 280 | case 'h': |
| 281 | return do_help(argc, argv); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 282 | case 'p': |
| 283 | pretty_output = true; |
| 284 | /* fall through */ |
| 285 | case 'j': |
| 286 | json_output = true; |
| 287 | break; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 288 | default: |
| 289 | usage(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | argc -= optind; |
| 294 | argv += optind; |
| 295 | if (argc < 0) |
| 296 | usage(); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 297 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 298 | if (json_output) { |
| 299 | json_wtr = jsonw_new(stdout); |
| 300 | if (!json_wtr) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 301 | p_err("failed to create JSON writer"); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | jsonw_pretty(json_wtr, pretty_output); |
| 305 | } |
| 306 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 307 | bfd_init(); |
| 308 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 309 | ret = cmd_select(cmds, argc, argv, do_help); |
| 310 | |
| 311 | if (json_output) |
| 312 | jsonw_destroy(&json_wtr); |
| 313 | |
| 314 | return ret; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 315 | } |