main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /* Author: Jakub Kicinski <kubakici@wp.pl> */
  34. #include <bfd.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include <getopt.h>
  38. #include <linux/bpf.h>
  39. #include <linux/version.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <bpf.h>
  44. #include "main.h"
  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 }\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. unsigned int version[3];
  86. version[0] = LINUX_VERSION_CODE >> 16;
  87. version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
  88. version[2] = LINUX_VERSION_CODE & 0xf;
  89. if (json_output) {
  90. jsonw_start_object(json_wtr);
  91. jsonw_name(json_wtr, "version");
  92. jsonw_printf(json_wtr, "\"%u.%u.%u\"",
  93. version[0], version[1], version[2]);
  94. jsonw_end_object(json_wtr);
  95. } else {
  96. printf("%s v%u.%u.%u\n", bin_name,
  97. version[0], version[1], version[2]);
  98. }
  99. return 0;
  100. }
  101. int cmd_select(const struct cmd *cmds, int argc, char **argv,
  102. int (*help)(int argc, char **argv))
  103. {
  104. unsigned int i;
  105. last_argc = argc;
  106. last_argv = argv;
  107. last_do_help = help;
  108. if (argc < 1 && cmds[0].func)
  109. return cmds[0].func(argc, argv);
  110. for (i = 0; cmds[i].func; i++)
  111. if (is_prefix(*argv, cmds[i].cmd))
  112. return cmds[i].func(argc - 1, argv + 1);
  113. help(argc - 1, argv + 1);
  114. return -1;
  115. }
  116. bool is_prefix(const char *pfx, const char *str)
  117. {
  118. if (!pfx)
  119. return false;
  120. if (strlen(str) < strlen(pfx))
  121. return false;
  122. return !memcmp(str, pfx, strlen(pfx));
  123. }
  124. void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
  125. {
  126. unsigned char *data = arg;
  127. unsigned int i;
  128. for (i = 0; i < n; i++) {
  129. const char *pfx = "";
  130. if (!i)
  131. /* nothing */;
  132. else if (!(i % 16))
  133. fprintf(f, "\n");
  134. else if (!(i % 8))
  135. fprintf(f, " ");
  136. else
  137. pfx = sep;
  138. fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
  139. }
  140. }
  141. static int do_batch(int argc, char **argv);
  142. static const struct cmd cmds[] = {
  143. { "help", do_help },
  144. { "batch", do_batch },
  145. { "prog", do_prog },
  146. { "map", do_map },
  147. { "version", do_version },
  148. { 0 }
  149. };
  150. static int do_batch(int argc, char **argv)
  151. {
  152. unsigned int lines = 0;
  153. char *n_argv[4096];
  154. char buf[65536];
  155. int n_argc;
  156. FILE *fp;
  157. int err;
  158. int i;
  159. if (argc < 2) {
  160. p_err("too few parameters for batch");
  161. return -1;
  162. } else if (!is_prefix(*argv, "file")) {
  163. p_err("expected 'file', got: %s", *argv);
  164. return -1;
  165. } else if (argc > 2) {
  166. p_err("too many parameters for batch");
  167. return -1;
  168. }
  169. NEXT_ARG();
  170. fp = fopen(*argv, "r");
  171. if (!fp) {
  172. p_err("Can't open file (%s): %s", *argv, strerror(errno));
  173. return -1;
  174. }
  175. if (json_output)
  176. jsonw_start_array(json_wtr);
  177. while (fgets(buf, sizeof(buf), fp)) {
  178. if (strlen(buf) == sizeof(buf) - 1) {
  179. errno = E2BIG;
  180. break;
  181. }
  182. n_argc = 0;
  183. n_argv[n_argc] = strtok(buf, " \t\n");
  184. while (n_argv[n_argc]) {
  185. n_argc++;
  186. if (n_argc == ARRAY_SIZE(n_argv)) {
  187. p_err("line %d has too many arguments, skip",
  188. lines);
  189. n_argc = 0;
  190. break;
  191. }
  192. n_argv[n_argc] = strtok(NULL, " \t\n");
  193. }
  194. if (!n_argc)
  195. continue;
  196. if (json_output) {
  197. jsonw_start_object(json_wtr);
  198. jsonw_name(json_wtr, "command");
  199. jsonw_start_array(json_wtr);
  200. for (i = 0; i < n_argc; i++)
  201. jsonw_string(json_wtr, n_argv[i]);
  202. jsonw_end_array(json_wtr);
  203. jsonw_name(json_wtr, "output");
  204. }
  205. err = cmd_select(cmds, n_argc, n_argv, do_help);
  206. if (json_output)
  207. jsonw_end_object(json_wtr);
  208. if (err)
  209. goto err_close;
  210. lines++;
  211. }
  212. if (errno && errno != ENOENT) {
  213. perror("reading batch file failed");
  214. err = -1;
  215. } else {
  216. p_info("processed %d lines", lines);
  217. err = 0;
  218. }
  219. err_close:
  220. fclose(fp);
  221. if (json_output)
  222. jsonw_end_array(json_wtr);
  223. return err;
  224. }
  225. int main(int argc, char **argv)
  226. {
  227. static const struct option options[] = {
  228. { "json", no_argument, NULL, 'j' },
  229. { "help", no_argument, NULL, 'h' },
  230. { "pretty", no_argument, NULL, 'p' },
  231. { "version", no_argument, NULL, 'V' },
  232. { "bpffs", no_argument, NULL, 'f' },
  233. { 0 }
  234. };
  235. int opt, ret;
  236. last_do_help = do_help;
  237. pretty_output = false;
  238. json_output = false;
  239. show_pinned = false;
  240. bin_name = argv[0];
  241. hash_init(prog_table.table);
  242. hash_init(map_table.table);
  243. opterr = 0;
  244. while ((opt = getopt_long(argc, argv, "Vhpjf",
  245. options, NULL)) >= 0) {
  246. switch (opt) {
  247. case 'V':
  248. return do_version(argc, argv);
  249. case 'h':
  250. return do_help(argc, argv);
  251. case 'p':
  252. pretty_output = true;
  253. /* fall through */
  254. case 'j':
  255. if (!json_output) {
  256. json_wtr = jsonw_new(stdout);
  257. if (!json_wtr) {
  258. p_err("failed to create JSON writer");
  259. return -1;
  260. }
  261. json_output = true;
  262. }
  263. jsonw_pretty(json_wtr, pretty_output);
  264. break;
  265. case 'f':
  266. show_pinned = true;
  267. break;
  268. default:
  269. p_err("unrecognized option '%s'", argv[optind - 1]);
  270. if (json_output)
  271. clean_and_exit(-1);
  272. else
  273. usage();
  274. }
  275. }
  276. argc -= optind;
  277. argv += optind;
  278. if (argc < 0)
  279. usage();
  280. bfd_init();
  281. ret = cmd_select(cmds, argc, argv, do_help);
  282. if (json_output)
  283. jsonw_destroy(&json_wtr);
  284. if (show_pinned) {
  285. delete_pinned_obj_table(&prog_table);
  286. delete_pinned_obj_table(&map_table);
  287. }
  288. return ret;
  289. }