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 | { |
| 67 | fprintf(stderr, |
| 68 | "Usage: %s OBJECT { COMMAND | help }\n" |
| 69 | " %s batch file FILE\n" |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 70 | " %s version\n" |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 71 | "\n" |
| 72 | " OBJECT := { prog | map }\n", |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 73 | bin_name, bin_name, bin_name); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 78 | static int do_version(int argc, char **argv) |
| 79 | { |
| 80 | printf("%s v%d.%d.%d\n", bin_name, |
| 81 | LINUX_VERSION_CODE >> 16, |
| 82 | LINUX_VERSION_CODE >> 8 & 0xf, |
| 83 | LINUX_VERSION_CODE & 0xf); |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 87 | int cmd_select(const struct cmd *cmds, int argc, char **argv, |
| 88 | int (*help)(int argc, char **argv)) |
| 89 | { |
| 90 | unsigned int i; |
| 91 | |
| 92 | last_argc = argc; |
| 93 | last_argv = argv; |
| 94 | last_do_help = help; |
| 95 | |
| 96 | if (argc < 1 && cmds[0].func) |
| 97 | return cmds[0].func(argc, argv); |
| 98 | |
| 99 | for (i = 0; cmds[i].func; i++) |
| 100 | if (is_prefix(*argv, cmds[i].cmd)) |
| 101 | return cmds[i].func(argc - 1, argv + 1); |
| 102 | |
| 103 | help(argc - 1, argv + 1); |
| 104 | |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | bool is_prefix(const char *pfx, const char *str) |
| 109 | { |
| 110 | if (!pfx) |
| 111 | return false; |
| 112 | if (strlen(str) < strlen(pfx)) |
| 113 | return false; |
| 114 | |
| 115 | return !memcmp(str, pfx, strlen(pfx)); |
| 116 | } |
| 117 | |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 118 | 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] | 119 | { |
| 120 | unsigned char *data = arg; |
| 121 | unsigned int i; |
| 122 | |
| 123 | for (i = 0; i < n; i++) { |
| 124 | const char *pfx = ""; |
| 125 | |
| 126 | if (!i) |
| 127 | /* nothing */; |
| 128 | else if (!(i % 16)) |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 129 | fprintf(f, "\n"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 130 | else if (!(i % 8)) |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 131 | fprintf(f, " "); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 132 | else |
| 133 | pfx = sep; |
| 134 | |
Quentin Monnet | 9cbe1f58 | 2017-10-19 15:46:19 -0700 | [diff] [blame] | 135 | fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | static int do_batch(int argc, char **argv); |
| 140 | |
| 141 | static const struct cmd cmds[] = { |
| 142 | { "help", do_help }, |
| 143 | { "batch", do_batch }, |
| 144 | { "prog", do_prog }, |
| 145 | { "map", do_map }, |
Quentin Monnet | 821cfbb | 2017-10-19 15:46:26 -0700 | [diff] [blame] | 146 | { "version", do_version }, |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 147 | { 0 } |
| 148 | }; |
| 149 | |
| 150 | static int do_batch(int argc, char **argv) |
| 151 | { |
| 152 | unsigned int lines = 0; |
| 153 | char *n_argv[4096]; |
| 154 | char buf[65536]; |
| 155 | int n_argc; |
| 156 | FILE *fp; |
| 157 | int err; |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 158 | int i; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 159 | |
| 160 | if (argc < 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 161 | p_err("too few parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 162 | return -1; |
| 163 | } else if (!is_prefix(*argv, "file")) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 164 | p_err("expected 'file', got: %s", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 165 | return -1; |
| 166 | } else if (argc > 2) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 167 | p_err("too many parameters for batch"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | NEXT_ARG(); |
| 171 | |
| 172 | fp = fopen(*argv, "r"); |
| 173 | if (!fp) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 174 | p_err("Can't open file (%s): %s", *argv, strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 178 | if (json_output) |
| 179 | jsonw_start_array(json_wtr); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 180 | while (fgets(buf, sizeof(buf), fp)) { |
| 181 | if (strlen(buf) == sizeof(buf) - 1) { |
| 182 | errno = E2BIG; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | n_argc = 0; |
| 187 | n_argv[n_argc] = strtok(buf, " \t\n"); |
| 188 | |
| 189 | while (n_argv[n_argc]) { |
| 190 | n_argc++; |
| 191 | if (n_argc == ARRAY_SIZE(n_argv)) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 192 | p_err("line %d has too many arguments, skip", |
| 193 | lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 194 | n_argc = 0; |
| 195 | break; |
| 196 | } |
| 197 | n_argv[n_argc] = strtok(NULL, " \t\n"); |
| 198 | } |
| 199 | |
| 200 | if (!n_argc) |
| 201 | continue; |
| 202 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 203 | if (json_output) { |
| 204 | jsonw_start_object(json_wtr); |
| 205 | jsonw_name(json_wtr, "command"); |
| 206 | jsonw_start_array(json_wtr); |
| 207 | for (i = 0; i < n_argc; i++) |
| 208 | jsonw_string(json_wtr, n_argv[i]); |
| 209 | jsonw_end_array(json_wtr); |
| 210 | jsonw_name(json_wtr, "output"); |
| 211 | } |
| 212 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 213 | err = cmd_select(cmds, n_argc, n_argv, do_help); |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 214 | |
| 215 | if (json_output) |
| 216 | jsonw_end_object(json_wtr); |
| 217 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 218 | if (err) |
| 219 | goto err_close; |
| 220 | |
| 221 | lines++; |
| 222 | } |
| 223 | |
| 224 | if (errno && errno != ENOENT) { |
| 225 | perror("reading batch file failed"); |
| 226 | err = -1; |
| 227 | } else { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 228 | p_info("processed %d lines", lines); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 229 | err = 0; |
| 230 | } |
| 231 | err_close: |
| 232 | fclose(fp); |
| 233 | |
Quentin Monnet | 3aaca6b | 2017-10-23 09:24:12 -0700 | [diff] [blame] | 234 | if (json_output) |
| 235 | jsonw_end_array(json_wtr); |
| 236 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 237 | return err; |
| 238 | } |
| 239 | |
| 240 | int main(int argc, char **argv) |
| 241 | { |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 242 | static const struct option options[] = { |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 243 | { "json", no_argument, NULL, 'j' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 244 | { "help", no_argument, NULL, 'h' }, |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 245 | { "pretty", no_argument, NULL, 'p' }, |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 246 | { "version", no_argument, NULL, 'V' }, |
| 247 | { 0 } |
| 248 | }; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 249 | int opt, ret; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 250 | |
| 251 | last_do_help = do_help; |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 252 | pretty_output = false; |
| 253 | json_output = false; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 254 | bin_name = argv[0]; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 255 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 256 | while ((opt = getopt_long(argc, argv, "Vhpj", |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 257 | options, NULL)) >= 0) { |
| 258 | switch (opt) { |
| 259 | case 'V': |
| 260 | return do_version(argc, argv); |
| 261 | case 'h': |
| 262 | return do_help(argc, argv); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 263 | case 'p': |
| 264 | pretty_output = true; |
| 265 | /* fall through */ |
| 266 | case 'j': |
| 267 | json_output = true; |
| 268 | break; |
Quentin Monnet | a2bc2e5 | 2017-10-23 09:24:06 -0700 | [diff] [blame] | 269 | default: |
| 270 | usage(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | argc -= optind; |
| 275 | argv += optind; |
| 276 | if (argc < 0) |
| 277 | usage(); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 278 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 279 | if (json_output) { |
| 280 | json_wtr = jsonw_new(stdout); |
| 281 | if (!json_wtr) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame^] | 282 | p_err("failed to create JSON writer"); |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 283 | return -1; |
| 284 | } |
| 285 | jsonw_pretty(json_wtr, pretty_output); |
| 286 | } |
| 287 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 288 | bfd_init(); |
| 289 | |
Quentin Monnet | d35efba | 2017-10-23 09:24:07 -0700 | [diff] [blame] | 290 | ret = cmd_select(cmds, argc, argv, do_help); |
| 291 | |
| 292 | if (json_output) |
| 293 | jsonw_destroy(&json_wtr); |
| 294 | |
| 295 | return ret; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 296 | } |