prog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. print_hex(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. break;
  228. err("can't get next program: %s\n", strerror(errno));
  229. if (errno == EINVAL)
  230. err("kernel too old?\n");
  231. return -1;
  232. }
  233. fd = bpf_prog_get_fd_by_id(id);
  234. if (fd < 0) {
  235. err("can't get prog by id (%u): %s\n",
  236. id, strerror(errno));
  237. return -1;
  238. }
  239. err = show_prog(fd);
  240. close(fd);
  241. if (err)
  242. return err;
  243. }
  244. return 0;
  245. }
  246. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  247. {
  248. va_list args;
  249. va_start(args, fmt);
  250. vprintf(fmt, args);
  251. va_end(args);
  252. }
  253. static void dump_xlated(void *buf, unsigned int len, bool opcodes)
  254. {
  255. struct bpf_insn *insn = buf;
  256. unsigned int i;
  257. for (i = 0; i < len / sizeof(*insn); i++) {
  258. printf("% 4d: ", i);
  259. print_bpf_insn(print_insn, NULL, insn + i, true);
  260. if (opcodes) {
  261. printf(" ");
  262. print_hex(insn + i, 8, " ");
  263. printf("\n");
  264. }
  265. if (insn[i].code == (BPF_LD | BPF_IMM | BPF_DW))
  266. i++;
  267. }
  268. }
  269. static int do_dump(int argc, char **argv)
  270. {
  271. struct bpf_prog_info info = {};
  272. __u32 len = sizeof(info);
  273. unsigned int buf_size;
  274. char *filepath = NULL;
  275. bool opcodes = false;
  276. unsigned char *buf;
  277. __u32 *member_len;
  278. __u64 *member_ptr;
  279. ssize_t n;
  280. int err;
  281. int fd;
  282. if (is_prefix(*argv, "jited")) {
  283. member_len = &info.jited_prog_len;
  284. member_ptr = &info.jited_prog_insns;
  285. } else if (is_prefix(*argv, "xlated")) {
  286. member_len = &info.xlated_prog_len;
  287. member_ptr = &info.xlated_prog_insns;
  288. } else {
  289. err("expected 'xlated' or 'jited', got: %s\n", *argv);
  290. return -1;
  291. }
  292. NEXT_ARG();
  293. if (argc < 2)
  294. usage();
  295. fd = prog_parse_fd(&argc, &argv);
  296. if (fd < 0)
  297. return -1;
  298. if (is_prefix(*argv, "file")) {
  299. NEXT_ARG();
  300. if (!argc) {
  301. err("expected file path\n");
  302. return -1;
  303. }
  304. filepath = *argv;
  305. NEXT_ARG();
  306. } else if (is_prefix(*argv, "opcodes")) {
  307. opcodes = true;
  308. NEXT_ARG();
  309. }
  310. if (argc) {
  311. usage();
  312. return -1;
  313. }
  314. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  315. if (err) {
  316. err("can't get prog info: %s\n", strerror(errno));
  317. return -1;
  318. }
  319. if (!*member_len) {
  320. info("no instructions returned\n");
  321. close(fd);
  322. return 0;
  323. }
  324. buf_size = *member_len;
  325. buf = malloc(buf_size);
  326. if (!buf) {
  327. err("mem alloc failed\n");
  328. close(fd);
  329. return -1;
  330. }
  331. memset(&info, 0, sizeof(info));
  332. *member_ptr = ptr_to_u64(buf);
  333. *member_len = buf_size;
  334. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  335. close(fd);
  336. if (err) {
  337. err("can't get prog info: %s\n", strerror(errno));
  338. goto err_free;
  339. }
  340. if (*member_len > buf_size) {
  341. info("too many instructions returned\n");
  342. goto err_free;
  343. }
  344. if (filepath) {
  345. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  346. if (fd < 0) {
  347. err("can't open file %s: %s\n", filepath,
  348. strerror(errno));
  349. goto err_free;
  350. }
  351. n = write(fd, buf, *member_len);
  352. close(fd);
  353. if (n != *member_len) {
  354. err("error writing output file: %s\n",
  355. n < 0 ? strerror(errno) : "short write");
  356. goto err_free;
  357. }
  358. } else {
  359. if (member_len == &info.jited_prog_len)
  360. disasm_print_insn(buf, *member_len, opcodes);
  361. else
  362. dump_xlated(buf, *member_len, opcodes);
  363. }
  364. free(buf);
  365. return 0;
  366. err_free:
  367. free(buf);
  368. return -1;
  369. }
  370. static int do_pin(int argc, char **argv)
  371. {
  372. return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  373. }
  374. static int do_help(int argc, char **argv)
  375. {
  376. fprintf(stderr,
  377. "Usage: %s %s show [PROG]\n"
  378. " %s %s dump xlated PROG [file FILE] [opcodes]\n"
  379. " %s %s dump jited PROG [file FILE] [opcodes]\n"
  380. " %s %s pin PROG FILE\n"
  381. " %s %s help\n"
  382. "\n"
  383. " " HELP_SPEC_PROGRAM "\n"
  384. "",
  385. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  386. bin_name, argv[-2], bin_name, argv[-2]);
  387. return 0;
  388. }
  389. static const struct cmd cmds[] = {
  390. { "show", do_show },
  391. { "dump", do_dump },
  392. { "pin", do_pin },
  393. { 0 }
  394. };
  395. int do_prog(int argc, char **argv)
  396. {
  397. return cmd_select(cmds, argc, argv, do_help);
  398. }