1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ 3 4 #include <ctype.h> 5 #include <errno.h> 6 #include <getopt.h> 7 #include <linux/bpf.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 #include <bpf/bpf.h> 13 #include <bpf/libbpf.h> 14 15 #include "main.h" 16 17 #define BATCH_LINE_LEN_MAX 65536 18 #define BATCH_ARG_NB_MAX 4096 19 20 const char *bin_name; 21 static int last_argc; 22 static char **last_argv; 23 static int (*last_do_help)(int argc, char **argv); 24 json_writer_t *json_wtr; 25 bool pretty_output; 26 bool json_output; 27 bool show_pinned; 28 bool block_mount; 29 bool verifier_logs; 30 bool relaxed_maps; 31 struct pinned_obj_table prog_table; 32 struct pinned_obj_table map_table; 33 struct pinned_obj_table link_table; 34 struct obj_refs_table refs_table; 35 clean_and_exit(int i)36 static void __noreturn clean_and_exit(int i) 37 { 38 if (json_output) 39 jsonw_destroy(&json_wtr); 40 41 exit(i); 42 } 43 usage(void)44 void usage(void) 45 { 46 last_do_help(last_argc - 1, last_argv + 1); 47 48 clean_and_exit(-1); 49 } 50 do_help(int argc,char ** argv)51 static int do_help(int argc, char **argv) 52 { 53 if (json_output) { 54 jsonw_null(json_wtr); 55 return 0; 56 } 57 58 fprintf(stderr, 59 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n" 60 " %s batch file FILE\n" 61 " %s version\n" 62 "\n" 63 " OBJECT := { prog | map | link | cgroup | perf | net | feature | btf | gen | struct_ops | iter }\n" 64 " " HELP_SPEC_OPTIONS "\n" 65 "", 66 bin_name, bin_name, bin_name); 67 68 return 0; 69 } 70 do_version(int argc,char ** argv)71 static int do_version(int argc, char **argv) 72 { 73 #ifdef HAVE_LIBBFD_SUPPORT 74 const bool has_libbfd = true; 75 #else 76 const bool has_libbfd = false; 77 #endif 78 #ifdef BPFTOOL_WITHOUT_SKELETONS 79 const bool has_skeletons = false; 80 #else 81 const bool has_skeletons = true; 82 #endif 83 84 if (json_output) { 85 jsonw_start_object(json_wtr); /* root object */ 86 87 jsonw_name(json_wtr, "version"); 88 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION); 89 90 jsonw_name(json_wtr, "features"); 91 jsonw_start_object(json_wtr); /* features */ 92 jsonw_bool_field(json_wtr, "libbfd", has_libbfd); 93 jsonw_bool_field(json_wtr, "skeletons", has_skeletons); 94 jsonw_end_object(json_wtr); /* features */ 95 96 jsonw_end_object(json_wtr); /* root object */ 97 } else { 98 unsigned int nb_features = 0; 99 100 printf("%s v%s\n", bin_name, BPFTOOL_VERSION); 101 printf("features:"); 102 if (has_libbfd) { 103 printf(" libbfd"); 104 nb_features++; 105 } 106 if (has_skeletons) 107 printf("%s skeletons", nb_features++ ? "," : ""); 108 printf("\n"); 109 } 110 return 0; 111 } 112 cmd_select(const struct cmd * cmds,int argc,char ** argv,int (* help)(int argc,char ** argv))113 int cmd_select(const struct cmd *cmds, int argc, char **argv, 114 int (*help)(int argc, char **argv)) 115 { 116 unsigned int i; 117 118 last_argc = argc; 119 last_argv = argv; 120 last_do_help = help; 121 122 if (argc < 1 && cmds[0].func) 123 return cmds[0].func(argc, argv); 124 125 for (i = 0; cmds[i].cmd; i++) { 126 if (is_prefix(*argv, cmds[i].cmd)) { 127 if (!cmds[i].func) { 128 p_err("command '%s' is not supported in bootstrap mode", 129 cmds[i].cmd); 130 return -1; 131 } 132 return cmds[i].func(argc - 1, argv + 1); 133 } 134 } 135 136 help(argc - 1, argv + 1); 137 138 return -1; 139 } 140 is_prefix(const char * pfx,const char * str)141 bool is_prefix(const char *pfx, const char *str) 142 { 143 if (!pfx) 144 return false; 145 if (strlen(str) < strlen(pfx)) 146 return false; 147 148 return !memcmp(str, pfx, strlen(pfx)); 149 } 150 151 /* Last argument MUST be NULL pointer */ detect_common_prefix(const char * arg,...)152 int detect_common_prefix(const char *arg, ...) 153 { 154 unsigned int count = 0; 155 const char *ref; 156 char msg[256]; 157 va_list ap; 158 159 snprintf(msg, sizeof(msg), "ambiguous prefix: '%s' could be '", arg); 160 va_start(ap, arg); 161 while ((ref = va_arg(ap, const char *))) { 162 if (!is_prefix(arg, ref)) 163 continue; 164 count++; 165 if (count > 1) 166 strncat(msg, "' or '", sizeof(msg) - strlen(msg) - 1); 167 strncat(msg, ref, sizeof(msg) - strlen(msg) - 1); 168 } 169 va_end(ap); 170 strncat(msg, "'", sizeof(msg) - strlen(msg) - 1); 171 172 if (count >= 2) { 173 p_err("%s", msg); 174 return -1; 175 } 176 177 return 0; 178 } 179 fprint_hex(FILE * f,void * arg,unsigned int n,const char * sep)180 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep) 181 { 182 unsigned char *data = arg; 183 unsigned int i; 184 185 for (i = 0; i < n; i++) { 186 const char *pfx = ""; 187 188 if (!i) 189 /* nothing */; 190 else if (!(i % 16)) 191 fprintf(f, "\n"); 192 else if (!(i % 8)) 193 fprintf(f, " "); 194 else 195 pfx = sep; 196 197 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]); 198 } 199 } 200 201 /* Split command line into argument vector. */ make_args(char * line,char * n_argv[],int maxargs,int cmd_nb)202 static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb) 203 { 204 static const char ws[] = " \t\r\n"; 205 char *cp = line; 206 int n_argc = 0; 207 208 while (*cp) { 209 /* Skip leading whitespace. */ 210 cp += strspn(cp, ws); 211 212 if (*cp == '\0') 213 break; 214 215 if (n_argc >= (maxargs - 1)) { 216 p_err("too many arguments to command %d", cmd_nb); 217 return -1; 218 } 219 220 /* Word begins with quote. */ 221 if (*cp == '\'' || *cp == '"') { 222 char quote = *cp++; 223 224 n_argv[n_argc++] = cp; 225 /* Find ending quote. */ 226 cp = strchr(cp, quote); 227 if (!cp) { 228 p_err("unterminated quoted string in command %d", 229 cmd_nb); 230 return -1; 231 } 232 } else { 233 n_argv[n_argc++] = cp; 234 235 /* Find end of word. */ 236 cp += strcspn(cp, ws); 237 if (*cp == '\0') 238 break; 239 } 240 241 /* Separate words. */ 242 *cp++ = 0; 243 } 244 n_argv[n_argc] = NULL; 245 246 return n_argc; 247 } 248 249 static int do_batch(int argc, char **argv); 250 251 static const struct cmd cmds[] = { 252 { "help", do_help }, 253 { "batch", do_batch }, 254 { "prog", do_prog }, 255 { "map", do_map }, 256 { "link", do_link }, 257 { "cgroup", do_cgroup }, 258 { "perf", do_perf }, 259 { "net", do_net }, 260 { "feature", do_feature }, 261 { "btf", do_btf }, 262 { "gen", do_gen }, 263 { "struct_ops", do_struct_ops }, 264 { "iter", do_iter }, 265 { "version", do_version }, 266 { 0 } 267 }; 268 do_batch(int argc,char ** argv)269 static int do_batch(int argc, char **argv) 270 { 271 char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX]; 272 char *n_argv[BATCH_ARG_NB_MAX]; 273 unsigned int lines = 0; 274 int n_argc; 275 FILE *fp; 276 char *cp; 277 int err = 0; 278 int i; 279 280 if (argc < 2) { 281 p_err("too few parameters for batch"); 282 return -1; 283 } else if (!is_prefix(*argv, "file")) { 284 p_err("expected 'file', got: %s", *argv); 285 return -1; 286 } else if (argc > 2) { 287 p_err("too many parameters for batch"); 288 return -1; 289 } 290 NEXT_ARG(); 291 292 if (!strcmp(*argv, "-")) 293 fp = stdin; 294 else 295 fp = fopen(*argv, "r"); 296 if (!fp) { 297 p_err("Can't open file (%s): %s", *argv, strerror(errno)); 298 return -1; 299 } 300 301 if (json_output) 302 jsonw_start_array(json_wtr); 303 while (fgets(buf, sizeof(buf), fp)) { 304 cp = strchr(buf, '#'); 305 if (cp) 306 *cp = '\0'; 307 308 if (strlen(buf) == sizeof(buf) - 1) { 309 errno = E2BIG; 310 break; 311 } 312 313 /* Append continuation lines if any (coming after a line ending 314 * with '\' in the batch file). 315 */ 316 while ((cp = strstr(buf, "\\\n")) != NULL) { 317 if (!fgets(contline, sizeof(contline), fp) || 318 strlen(contline) == 0) { 319 p_err("missing continuation line on command %d", 320 lines); 321 err = -1; 322 goto err_close; 323 } 324 325 cp = strchr(contline, '#'); 326 if (cp) 327 *cp = '\0'; 328 329 if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) { 330 p_err("command %d is too long", lines); 331 err = -1; 332 goto err_close; 333 } 334 buf[strlen(buf) - 2] = '\0'; 335 strcat(buf, contline); 336 } 337 338 n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines); 339 if (!n_argc) 340 continue; 341 if (n_argc < 0) { 342 err = n_argc; 343 goto err_close; 344 } 345 346 if (json_output) { 347 jsonw_start_object(json_wtr); 348 jsonw_name(json_wtr, "command"); 349 jsonw_start_array(json_wtr); 350 for (i = 0; i < n_argc; i++) 351 jsonw_string(json_wtr, n_argv[i]); 352 jsonw_end_array(json_wtr); 353 jsonw_name(json_wtr, "output"); 354 } 355 356 err = cmd_select(cmds, n_argc, n_argv, do_help); 357 358 if (json_output) 359 jsonw_end_object(json_wtr); 360 361 if (err) 362 goto err_close; 363 364 lines++; 365 } 366 367 if (errno && errno != ENOENT) { 368 p_err("reading batch file failed: %s", strerror(errno)); 369 err = -1; 370 } else { 371 if (!json_output) 372 printf("processed %d commands\n", lines); 373 } 374 err_close: 375 if (fp != stdin) 376 fclose(fp); 377 378 if (json_output) 379 jsonw_end_array(json_wtr); 380 381 return err; 382 } 383 main(int argc,char ** argv)384 int main(int argc, char **argv) 385 { 386 static const struct option options[] = { 387 { "json", no_argument, NULL, 'j' }, 388 { "help", no_argument, NULL, 'h' }, 389 { "pretty", no_argument, NULL, 'p' }, 390 { "version", no_argument, NULL, 'V' }, 391 { "bpffs", no_argument, NULL, 'f' }, 392 { "mapcompat", no_argument, NULL, 'm' }, 393 { "nomount", no_argument, NULL, 'n' }, 394 { "debug", no_argument, NULL, 'd' }, 395 { 0 } 396 }; 397 int opt, ret; 398 399 setlinebuf(stdout); 400 401 #ifdef USE_LIBCAP 402 /* Libcap < 2.63 hooks before main() to compute the number of 403 * capabilities of the running kernel, and doing so it calls prctl() 404 * which may fail and set errno to non-zero. 405 * Let's reset errno to make sure this does not interfere with the 406 * batch mode. 407 */ 408 errno = 0; 409 #endif 410 411 last_do_help = do_help; 412 pretty_output = false; 413 json_output = false; 414 show_pinned = false; 415 block_mount = false; 416 bin_name = argv[0]; 417 418 hash_init(prog_table.table); 419 hash_init(map_table.table); 420 hash_init(link_table.table); 421 422 opterr = 0; 423 while ((opt = getopt_long(argc, argv, "Vhpjfmnd", 424 options, NULL)) >= 0) { 425 switch (opt) { 426 case 'V': 427 return do_version(argc, argv); 428 case 'h': 429 return do_help(argc, argv); 430 case 'p': 431 pretty_output = true; 432 /* fall through */ 433 case 'j': 434 if (!json_output) { 435 json_wtr = jsonw_new(stdout); 436 if (!json_wtr) { 437 p_err("failed to create JSON writer"); 438 return -1; 439 } 440 json_output = true; 441 } 442 jsonw_pretty(json_wtr, pretty_output); 443 break; 444 case 'f': 445 show_pinned = true; 446 break; 447 case 'm': 448 relaxed_maps = true; 449 break; 450 case 'n': 451 block_mount = true; 452 break; 453 case 'd': 454 libbpf_set_print(print_all_levels); 455 verifier_logs = true; 456 break; 457 default: 458 p_err("unrecognized option '%s'", argv[optind - 1]); 459 if (json_output) 460 clean_and_exit(-1); 461 else 462 usage(); 463 } 464 } 465 466 argc -= optind; 467 argv += optind; 468 if (argc < 0) 469 usage(); 470 471 ret = cmd_select(cmds, argc, argv, do_help); 472 473 if (json_output) 474 jsonw_destroy(&json_wtr); 475 476 if (show_pinned) { 477 delete_pinned_obj_table(&prog_table); 478 delete_pinned_obj_table(&map_table); 479 delete_pinned_obj_table(&link_table); 480 } 481 482 return ret; 483 } 484