main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2017-2018 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. #include <bfd.h>
  34. #include <ctype.h>
  35. #include <errno.h>
  36. #include <getopt.h>
  37. #include <linux/bpf.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <bpf.h>
  42. #include "main.h"
  43. #define BATCH_LINE_LEN_MAX 65536
  44. #define BATCH_ARG_NB_MAX 4096
  45. const char *bin_name;
  46. static int last_argc;
  47. static char **last_argv;
  48. static int (*last_do_help)(int argc, char **argv);
  49. json_writer_t *json_wtr;
  50. bool pretty_output;
  51. bool json_output;
  52. bool show_pinned;
  53. int bpf_flags;
  54. struct pinned_obj_table prog_table;
  55. struct pinned_obj_table map_table;
  56. static void __noreturn clean_and_exit(int i)
  57. {
  58. if (json_output)
  59. jsonw_destroy(&json_wtr);
  60. exit(i);
  61. }
  62. void usage(void)
  63. {
  64. last_do_help(last_argc - 1, last_argv + 1);
  65. clean_and_exit(-1);
  66. }
  67. static int do_help(int argc, char **argv)
  68. {
  69. if (json_output) {
  70. jsonw_null(json_wtr);
  71. return 0;
  72. }
  73. fprintf(stderr,
  74. "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
  75. " %s batch file FILE\n"
  76. " %s version\n"
  77. "\n"
  78. " OBJECT := { prog | map | cgroup | perf | net }\n"
  79. " " HELP_SPEC_OPTIONS "\n"
  80. "",
  81. bin_name, bin_name, bin_name);
  82. return 0;
  83. }
  84. static int do_version(int argc, char **argv)
  85. {
  86. if (json_output) {
  87. jsonw_start_object(json_wtr);
  88. jsonw_name(json_wtr, "version");
  89. jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
  90. jsonw_end_object(json_wtr);
  91. } else {
  92. printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
  93. }
  94. return 0;
  95. }
  96. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  97. int (*help)(int argc, char **argv))
  98. {
  99. unsigned int i;
  100. last_argc = argc;
  101. last_argv = argv;
  102. last_do_help = help;
  103. if (argc < 1 && cmds[0].func)
  104. return cmds[0].func(argc, argv);
  105. for (i = 0; cmds[i].func; i++)
  106. if (is_prefix(*argv, cmds[i].cmd))
  107. return cmds[i].func(argc - 1, argv + 1);
  108. help(argc - 1, argv + 1);
  109. return -1;
  110. }
  111. bool is_prefix(const char *pfx, const char *str)
  112. {
  113. if (!pfx)
  114. return false;
  115. if (strlen(str) < strlen(pfx))
  116. return false;
  117. return !memcmp(str, pfx, strlen(pfx));
  118. }
  119. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
  120. {
  121. unsigned char *data = arg;
  122. unsigned int i;
  123. for (i = 0; i < n; i++) {
  124. const char *pfx = "";
  125. if (!i)
  126. /* nothing */;
  127. else if (!(i % 16))
  128. fprintf(f, "\n");
  129. else if (!(i % 8))
  130. fprintf(f, " ");
  131. else
  132. pfx = sep;
  133. fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
  134. }
  135. }
  136. /* Split command line into argument vector. */
  137. static int make_args(char *line, char *n_argv[], int maxargs, int cmd_nb)
  138. {
  139. static const char ws[] = " \t\r\n";
  140. char *cp = line;
  141. int n_argc = 0;
  142. while (*cp) {
  143. /* Skip leading whitespace. */
  144. cp += strspn(cp, ws);
  145. if (*cp == '\0')
  146. break;
  147. if (n_argc >= (maxargs - 1)) {
  148. p_err("too many arguments to command %d", cmd_nb);
  149. return -1;
  150. }
  151. /* Word begins with quote. */
  152. if (*cp == '\'' || *cp == '"') {
  153. char quote = *cp++;
  154. n_argv[n_argc++] = cp;
  155. /* Find ending quote. */
  156. cp = strchr(cp, quote);
  157. if (!cp) {
  158. p_err("unterminated quoted string in command %d",
  159. cmd_nb);
  160. return -1;
  161. }
  162. } else {
  163. n_argv[n_argc++] = cp;
  164. /* Find end of word. */
  165. cp += strcspn(cp, ws);
  166. if (*cp == '\0')
  167. break;
  168. }
  169. /* Separate words. */
  170. *cp++ = 0;
  171. }
  172. n_argv[n_argc] = NULL;
  173. return n_argc;
  174. }
  175. static int do_batch(int argc, char **argv);
  176. static const struct cmd cmds[] = {
  177. { "help", do_help },
  178. { "batch", do_batch },
  179. { "prog", do_prog },
  180. { "map", do_map },
  181. { "cgroup", do_cgroup },
  182. { "perf", do_perf },
  183. { "net", do_net },
  184. { "version", do_version },
  185. { 0 }
  186. };
  187. static int do_batch(int argc, char **argv)
  188. {
  189. char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX];
  190. char *n_argv[BATCH_ARG_NB_MAX];
  191. unsigned int lines = 0;
  192. int n_argc;
  193. FILE *fp;
  194. char *cp;
  195. int err;
  196. int i;
  197. if (argc < 2) {
  198. p_err("too few parameters for batch");
  199. return -1;
  200. } else if (!is_prefix(*argv, "file")) {
  201. p_err("expected 'file', got: %s", *argv);
  202. return -1;
  203. } else if (argc > 2) {
  204. p_err("too many parameters for batch");
  205. return -1;
  206. }
  207. NEXT_ARG();
  208. if (!strcmp(*argv, "-"))
  209. fp = stdin;
  210. else
  211. fp = fopen(*argv, "r");
  212. if (!fp) {
  213. p_err("Can't open file (%s): %s", *argv, strerror(errno));
  214. return -1;
  215. }
  216. if (json_output)
  217. jsonw_start_array(json_wtr);
  218. while (fgets(buf, sizeof(buf), fp)) {
  219. cp = strchr(buf, '#');
  220. if (cp)
  221. *cp = '\0';
  222. if (strlen(buf) == sizeof(buf) - 1) {
  223. errno = E2BIG;
  224. break;
  225. }
  226. /* Append continuation lines if any (coming after a line ending
  227. * with '\' in the batch file).
  228. */
  229. while ((cp = strstr(buf, "\\\n")) != NULL) {
  230. if (!fgets(contline, sizeof(contline), fp) ||
  231. strlen(contline) == 0) {
  232. p_err("missing continuation line on command %d",
  233. lines);
  234. err = -1;
  235. goto err_close;
  236. }
  237. cp = strchr(contline, '#');
  238. if (cp)
  239. *cp = '\0';
  240. if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) {
  241. p_err("command %d is too long", lines);
  242. err = -1;
  243. goto err_close;
  244. }
  245. buf[strlen(buf) - 2] = '\0';
  246. strcat(buf, contline);
  247. }
  248. n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines);
  249. if (!n_argc)
  250. continue;
  251. if (n_argc < 0)
  252. goto err_close;
  253. if (json_output) {
  254. jsonw_start_object(json_wtr);
  255. jsonw_name(json_wtr, "command");
  256. jsonw_start_array(json_wtr);
  257. for (i = 0; i < n_argc; i++)
  258. jsonw_string(json_wtr, n_argv[i]);
  259. jsonw_end_array(json_wtr);
  260. jsonw_name(json_wtr, "output");
  261. }
  262. err = cmd_select(cmds, n_argc, n_argv, do_help);
  263. if (json_output)
  264. jsonw_end_object(json_wtr);
  265. if (err)
  266. goto err_close;
  267. lines++;
  268. }
  269. if (errno && errno != ENOENT) {
  270. p_err("reading batch file failed: %s", strerror(errno));
  271. err = -1;
  272. } else {
  273. if (!json_output)
  274. printf("processed %d commands\n", lines);
  275. err = 0;
  276. }
  277. err_close:
  278. if (fp != stdin)
  279. fclose(fp);
  280. if (json_output)
  281. jsonw_end_array(json_wtr);
  282. return err;
  283. }
  284. int main(int argc, char **argv)
  285. {
  286. static const struct option options[] = {
  287. { "json", no_argument, NULL, 'j' },
  288. { "help", no_argument, NULL, 'h' },
  289. { "pretty", no_argument, NULL, 'p' },
  290. { "version", no_argument, NULL, 'V' },
  291. { "bpffs", no_argument, NULL, 'f' },
  292. { "mapcompat", no_argument, NULL, 'm' },
  293. { 0 }
  294. };
  295. int opt, ret;
  296. last_do_help = do_help;
  297. pretty_output = false;
  298. json_output = false;
  299. show_pinned = false;
  300. bin_name = argv[0];
  301. hash_init(prog_table.table);
  302. hash_init(map_table.table);
  303. opterr = 0;
  304. while ((opt = getopt_long(argc, argv, "Vhpjfm",
  305. options, NULL)) >= 0) {
  306. switch (opt) {
  307. case 'V':
  308. return do_version(argc, argv);
  309. case 'h':
  310. return do_help(argc, argv);
  311. case 'p':
  312. pretty_output = true;
  313. /* fall through */
  314. case 'j':
  315. if (!json_output) {
  316. json_wtr = jsonw_new(stdout);
  317. if (!json_wtr) {
  318. p_err("failed to create JSON writer");
  319. return -1;
  320. }
  321. json_output = true;
  322. }
  323. jsonw_pretty(json_wtr, pretty_output);
  324. break;
  325. case 'f':
  326. show_pinned = true;
  327. break;
  328. case 'm':
  329. bpf_flags = MAPS_RELAX_COMPAT;
  330. break;
  331. default:
  332. p_err("unrecognized option '%s'", argv[optind - 1]);
  333. if (json_output)
  334. clean_and_exit(-1);
  335. else
  336. usage();
  337. }
  338. }
  339. argc -= optind;
  340. argv += optind;
  341. if (argc < 0)
  342. usage();
  343. bfd_init();
  344. ret = cmd_select(cmds, argc, argv, do_help);
  345. if (json_output)
  346. jsonw_destroy(&json_wtr);
  347. if (show_pinned) {
  348. delete_pinned_obj_table(&prog_table);
  349. delete_pinned_obj_table(&map_table);
  350. }
  351. return ret;
  352. }