prog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <errno.h>
  35. #include <fcntl.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <bpf.h>
  45. #include "main.h"
  46. #include "disasm.h"
  47. static const char * const prog_type_name[] = {
  48. [BPF_PROG_TYPE_UNSPEC] = "unspec",
  49. [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
  50. [BPF_PROG_TYPE_KPROBE] = "kprobe",
  51. [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
  52. [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
  53. [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
  54. [BPF_PROG_TYPE_XDP] = "xdp",
  55. [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
  56. [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
  57. [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
  58. [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
  59. [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
  60. [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
  61. [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
  62. [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
  63. };
  64. static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
  65. {
  66. struct timespec real_time_ts, boot_time_ts;
  67. time_t wallclock_secs;
  68. struct tm load_tm;
  69. buf[--size] = '\0';
  70. if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
  71. clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
  72. perror("Can't read clocks");
  73. snprintf(buf, size, "%llu", nsecs / 1000000000);
  74. return;
  75. }
  76. wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
  77. nsecs / 1000000000;
  78. if (!localtime_r(&wallclock_secs, &load_tm)) {
  79. snprintf(buf, size, "%llu", nsecs / 1000000000);
  80. return;
  81. }
  82. strftime(buf, size, "%b %d/%H:%M", &load_tm);
  83. }
  84. static int prog_fd_by_tag(unsigned char *tag)
  85. {
  86. struct bpf_prog_info info = {};
  87. __u32 len = sizeof(info);
  88. unsigned int id = 0;
  89. int err;
  90. int fd;
  91. while (true) {
  92. err = bpf_prog_get_next_id(id, &id);
  93. if (err) {
  94. err("%s\n", strerror(errno));
  95. return -1;
  96. }
  97. fd = bpf_prog_get_fd_by_id(id);
  98. if (fd < 0) {
  99. err("can't get prog by id (%u): %s\n",
  100. id, strerror(errno));
  101. return -1;
  102. }
  103. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  104. if (err) {
  105. err("can't get prog info (%u): %s\n",
  106. id, strerror(errno));
  107. close(fd);
  108. return -1;
  109. }
  110. if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
  111. return fd;
  112. close(fd);
  113. }
  114. }
  115. int prog_parse_fd(int *argc, char ***argv)
  116. {
  117. int fd;
  118. if (is_prefix(**argv, "id")) {
  119. unsigned int id;
  120. char *endptr;
  121. NEXT_ARGP();
  122. id = strtoul(**argv, &endptr, 0);
  123. if (*endptr) {
  124. err("can't parse %s as ID\n", **argv);
  125. return -1;
  126. }
  127. NEXT_ARGP();
  128. fd = bpf_prog_get_fd_by_id(id);
  129. if (fd < 0)
  130. err("get by id (%u): %s\n", id, strerror(errno));
  131. return fd;
  132. } else if (is_prefix(**argv, "tag")) {
  133. unsigned char tag[BPF_TAG_SIZE];
  134. NEXT_ARGP();
  135. if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
  136. tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
  137. != BPF_TAG_SIZE) {
  138. err("can't parse tag\n");
  139. return -1;
  140. }
  141. NEXT_ARGP();
  142. return prog_fd_by_tag(tag);
  143. } else if (is_prefix(**argv, "pinned")) {
  144. char *path;
  145. NEXT_ARGP();
  146. path = **argv;
  147. NEXT_ARGP();
  148. return open_obj_pinned_any(path, BPF_OBJ_PROG);
  149. }
  150. err("expected 'id', 'tag' or 'pinned', got: '%s'?\n", **argv);
  151. return -1;
  152. }
  153. static void show_prog_maps(int fd, u32 num_maps)
  154. {
  155. struct bpf_prog_info info = {};
  156. __u32 len = sizeof(info);
  157. __u32 map_ids[num_maps];
  158. unsigned int i;
  159. int err;
  160. info.nr_map_ids = num_maps;
  161. info.map_ids = ptr_to_u64(map_ids);
  162. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  163. if (err || !info.nr_map_ids)
  164. return;
  165. printf(" map_ids ");
  166. for (i = 0; i < info.nr_map_ids; i++)
  167. printf("%u%s", map_ids[i],
  168. i == info.nr_map_ids - 1 ? "" : ",");
  169. }
  170. static int show_prog(int fd)
  171. {
  172. struct bpf_prog_info info = {};
  173. __u32 len = sizeof(info);
  174. char *memlock;
  175. int err;
  176. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  177. if (err) {
  178. err("can't get prog info: %s\n", strerror(errno));
  179. return -1;
  180. }
  181. printf("%u: ", info.id);
  182. if (info.type < ARRAY_SIZE(prog_type_name))
  183. printf("%s ", prog_type_name[info.type]);
  184. else
  185. printf("type %u ", info.type);
  186. if (*info.name)
  187. printf("name %s ", info.name);
  188. printf("tag ");
  189. fprint_hex(stdout, info.tag, BPF_TAG_SIZE, "");
  190. printf("\n");
  191. if (info.load_time) {
  192. char buf[32];
  193. print_boot_time(info.load_time, buf, sizeof(buf));
  194. /* Piggy back on load_time, since 0 uid is a valid one */
  195. printf("\tloaded_at %s uid %u\n", buf, info.created_by_uid);
  196. }
  197. printf("\txlated %uB", info.xlated_prog_len);
  198. if (info.jited_prog_len)
  199. printf(" jited %uB", info.jited_prog_len);
  200. else
  201. printf(" not jited");
  202. memlock = get_fdinfo(fd, "memlock");
  203. if (memlock)
  204. printf(" memlock %sB", memlock);
  205. free(memlock);
  206. if (info.nr_map_ids)
  207. show_prog_maps(fd, info.nr_map_ids);
  208. printf("\n");
  209. return 0;
  210. }
  211. static int do_show(int argc, char **argv)
  212. { __u32 id = 0;
  213. int err;
  214. int fd;
  215. if (argc == 2) {
  216. fd = prog_parse_fd(&argc, &argv);
  217. if (fd < 0)
  218. return -1;
  219. return show_prog(fd);
  220. }
  221. if (argc)
  222. return BAD_ARG();
  223. while (true) {
  224. err = bpf_prog_get_next_id(id, &id);
  225. if (err) {
  226. if (errno == ENOENT) {
  227. err = 0;
  228. break;
  229. }
  230. err("can't get next program: %s\n", strerror(errno));
  231. if (errno == EINVAL)
  232. err("kernel too old?\n");
  233. return -1;
  234. }
  235. fd = bpf_prog_get_fd_by_id(id);
  236. if (fd < 0) {
  237. err("can't get prog by id (%u): %s\n",
  238. id, strerror(errno));
  239. return -1;
  240. }
  241. err = show_prog(fd);
  242. close(fd);
  243. if (err)
  244. return err;
  245. }
  246. return 0;
  247. }
  248. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  249. {
  250. va_list args;
  251. va_start(args, fmt);
  252. vprintf(fmt, args);
  253. va_end(args);
  254. }
  255. static void dump_xlated(void *buf, unsigned int len, bool opcodes)
  256. {
  257. struct bpf_insn *insn = buf;
  258. unsigned int i;
  259. for (i = 0; i < len / sizeof(*insn); i++) {
  260. printf("% 4d: ", i);
  261. print_bpf_insn(print_insn, NULL, insn + i, true);
  262. if (opcodes) {
  263. printf(" ");
  264. fprint_hex(stdout, insn + i, 8, " ");
  265. printf("\n");
  266. }
  267. if (insn[i].code == (BPF_LD | BPF_IMM | BPF_DW))
  268. i++;
  269. }
  270. }
  271. static int do_dump(int argc, char **argv)
  272. {
  273. struct bpf_prog_info info = {};
  274. __u32 len = sizeof(info);
  275. unsigned int buf_size;
  276. char *filepath = NULL;
  277. bool opcodes = false;
  278. unsigned char *buf;
  279. __u32 *member_len;
  280. __u64 *member_ptr;
  281. ssize_t n;
  282. int err;
  283. int fd;
  284. if (is_prefix(*argv, "jited")) {
  285. member_len = &info.jited_prog_len;
  286. member_ptr = &info.jited_prog_insns;
  287. } else if (is_prefix(*argv, "xlated")) {
  288. member_len = &info.xlated_prog_len;
  289. member_ptr = &info.xlated_prog_insns;
  290. } else {
  291. err("expected 'xlated' or 'jited', got: %s\n", *argv);
  292. return -1;
  293. }
  294. NEXT_ARG();
  295. if (argc < 2)
  296. usage();
  297. fd = prog_parse_fd(&argc, &argv);
  298. if (fd < 0)
  299. return -1;
  300. if (is_prefix(*argv, "file")) {
  301. NEXT_ARG();
  302. if (!argc) {
  303. err("expected file path\n");
  304. return -1;
  305. }
  306. filepath = *argv;
  307. NEXT_ARG();
  308. } else if (is_prefix(*argv, "opcodes")) {
  309. opcodes = true;
  310. NEXT_ARG();
  311. }
  312. if (argc) {
  313. usage();
  314. return -1;
  315. }
  316. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  317. if (err) {
  318. err("can't get prog info: %s\n", strerror(errno));
  319. return -1;
  320. }
  321. if (!*member_len) {
  322. info("no instructions returned\n");
  323. close(fd);
  324. return 0;
  325. }
  326. buf_size = *member_len;
  327. buf = malloc(buf_size);
  328. if (!buf) {
  329. err("mem alloc failed\n");
  330. close(fd);
  331. return -1;
  332. }
  333. memset(&info, 0, sizeof(info));
  334. *member_ptr = ptr_to_u64(buf);
  335. *member_len = buf_size;
  336. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  337. close(fd);
  338. if (err) {
  339. err("can't get prog info: %s\n", strerror(errno));
  340. goto err_free;
  341. }
  342. if (*member_len > buf_size) {
  343. err("too many instructions returned\n");
  344. goto err_free;
  345. }
  346. if (filepath) {
  347. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  348. if (fd < 0) {
  349. err("can't open file %s: %s\n", filepath,
  350. strerror(errno));
  351. goto err_free;
  352. }
  353. n = write(fd, buf, *member_len);
  354. close(fd);
  355. if (n != *member_len) {
  356. err("error writing output file: %s\n",
  357. n < 0 ? strerror(errno) : "short write");
  358. goto err_free;
  359. }
  360. } else {
  361. if (member_len == &info.jited_prog_len)
  362. disasm_print_insn(buf, *member_len, opcodes);
  363. else
  364. dump_xlated(buf, *member_len, opcodes);
  365. }
  366. free(buf);
  367. return 0;
  368. err_free:
  369. free(buf);
  370. return -1;
  371. }
  372. static int do_pin(int argc, char **argv)
  373. {
  374. return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  375. }
  376. static int do_help(int argc, char **argv)
  377. {
  378. fprintf(stderr,
  379. "Usage: %s %s show [PROG]\n"
  380. " %s %s dump xlated PROG [file FILE] [opcodes]\n"
  381. " %s %s dump jited PROG [file FILE] [opcodes]\n"
  382. " %s %s pin PROG FILE\n"
  383. " %s %s help\n"
  384. "\n"
  385. " " HELP_SPEC_PROGRAM "\n"
  386. "",
  387. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  388. bin_name, argv[-2], bin_name, argv[-2]);
  389. return 0;
  390. }
  391. static const struct cmd cmds[] = {
  392. { "show", do_show },
  393. { "dump", do_dump },
  394. { "pin", do_pin },
  395. { 0 }
  396. };
  397. int do_prog(int argc, char **argv)
  398. {
  399. return cmd_select(cmds, argc, argv, do_help);
  400. }