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