main.c 8.5 KB

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