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