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> 39 #include <getopt.h> 40 #include <linux/bpf.h> 41 #include <linux/version.h> 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); 54 json_writer_t *json_wtr; 55 bool pretty_output; 56 bool json_output; 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 if (json_output) { 68 jsonw_null(json_wtr); 69 return 0; 70 } 71 72 fprintf(stderr, 73 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n" 74 " %s batch file FILE\n" 75 " %s version\n" 76 "\n" 77 " OBJECT := { prog | map }\n" 78 " " HELP_SPEC_OPTIONS "\n" 79 "", 80 bin_name, bin_name, bin_name); 81 82 return 0; 83 } 84 85 static int do_version(int argc, char **argv) 86 { 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 } 103 return 0; 104 } 105 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 137 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep) 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)) 148 fprintf(f, "\n"); 149 else if (!(i % 8)) 150 fprintf(f, " "); 151 else 152 pfx = sep; 153 154 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); 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 }, 165 { "version", do_version }, 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; 177 int i; 178 179 if (argc < 2) { 180 p_err("too few parameters for batch"); 181 return -1; 182 } else if (!is_prefix(*argv, "file")) { 183 p_err("expected 'file', got: %s", *argv); 184 return -1; 185 } else if (argc > 2) { 186 p_err("too many parameters for batch"); 187 return -1; 188 } 189 NEXT_ARG(); 190 191 fp = fopen(*argv, "r"); 192 if (!fp) { 193 p_err("Can't open file (%s): %s", *argv, strerror(errno)); 194 return -1; 195 } 196 197 if (json_output) 198 jsonw_start_array(json_wtr); 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)) { 211 p_err("line %d has too many arguments, skip", 212 lines); 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 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 232 err = cmd_select(cmds, n_argc, n_argv, do_help); 233 234 if (json_output) 235 jsonw_end_object(json_wtr); 236 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 { 247 p_info("processed %d lines", lines); 248 err = 0; 249 } 250 err_close: 251 fclose(fp); 252 253 if (json_output) 254 jsonw_end_array(json_wtr); 255 256 return err; 257 } 258 259 int main(int argc, char **argv) 260 { 261 static const struct option options[] = { 262 { "json", no_argument, NULL, 'j' }, 263 { "help", no_argument, NULL, 'h' }, 264 { "pretty", no_argument, NULL, 'p' }, 265 { "version", no_argument, NULL, 'V' }, 266 { 0 } 267 }; 268 int opt, ret; 269 270 last_do_help = do_help; 271 pretty_output = false; 272 json_output = false; 273 bin_name = argv[0]; 274 275 while ((opt = getopt_long(argc, argv, "Vhpj", 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); 282 case 'p': 283 pretty_output = true; 284 /* fall through */ 285 case 'j': 286 json_output = true; 287 break; 288 default: 289 usage(); 290 } 291 } 292 293 argc -= optind; 294 argv += optind; 295 if (argc < 0) 296 usage(); 297 298 if (json_output) { 299 json_wtr = jsonw_new(stdout); 300 if (!json_wtr) { 301 p_err("failed to create JSON writer"); 302 return -1; 303 } 304 jsonw_pretty(json_wtr, pretty_output); 305 } 306 307 bfd_init(); 308 309 ret = cmd_select(cmds, argc, argv, do_help); 310 311 if (json_output) 312 jsonw_destroy(&json_wtr); 313 314 return ret; 315 } 316