prog.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. if (json_output) {
  166. jsonw_name(json_wtr, "map_ids");
  167. jsonw_start_array(json_wtr);
  168. for (i = 0; i < info.nr_map_ids; i++)
  169. jsonw_uint(json_wtr, map_ids[i]);
  170. jsonw_end_array(json_wtr);
  171. } else {
  172. printf(" map_ids ");
  173. for (i = 0; i < info.nr_map_ids; i++)
  174. printf("%u%s", map_ids[i],
  175. i == info.nr_map_ids - 1 ? "" : ",");
  176. }
  177. }
  178. static void print_prog_json(struct bpf_prog_info *info, int fd)
  179. {
  180. char *memlock;
  181. jsonw_start_object(json_wtr);
  182. jsonw_uint_field(json_wtr, "id", info->id);
  183. if (info->type < ARRAY_SIZE(prog_type_name))
  184. jsonw_string_field(json_wtr, "type",
  185. prog_type_name[info->type]);
  186. else
  187. jsonw_uint_field(json_wtr, "type", info->type);
  188. if (*info->name)
  189. jsonw_string_field(json_wtr, "name", info->name);
  190. jsonw_name(json_wtr, "tag");
  191. jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
  192. info->tag[0], info->tag[1], info->tag[2], info->tag[3],
  193. info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
  194. if (info->load_time) {
  195. char buf[32];
  196. print_boot_time(info->load_time, buf, sizeof(buf));
  197. /* Piggy back on load_time, since 0 uid is a valid one */
  198. jsonw_string_field(json_wtr, "loaded_at", buf);
  199. jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
  200. }
  201. jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
  202. if (info->jited_prog_len) {
  203. jsonw_bool_field(json_wtr, "jited", true);
  204. jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
  205. } else {
  206. jsonw_bool_field(json_wtr, "jited", false);
  207. }
  208. memlock = get_fdinfo(fd, "memlock");
  209. if (memlock)
  210. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  211. free(memlock);
  212. if (info->nr_map_ids)
  213. show_prog_maps(fd, info->nr_map_ids);
  214. jsonw_end_object(json_wtr);
  215. }
  216. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  217. {
  218. char *memlock;
  219. printf("%u: ", info->id);
  220. if (info->type < ARRAY_SIZE(prog_type_name))
  221. printf("%s ", prog_type_name[info->type]);
  222. else
  223. printf("type %u ", info->type);
  224. if (*info->name)
  225. printf("name %s ", info->name);
  226. printf("tag ");
  227. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  228. printf("\n");
  229. if (info->load_time) {
  230. char buf[32];
  231. print_boot_time(info->load_time, buf, sizeof(buf));
  232. /* Piggy back on load_time, since 0 uid is a valid one */
  233. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  234. }
  235. printf("\txlated %uB", info->xlated_prog_len);
  236. if (info->jited_prog_len)
  237. printf(" jited %uB", info->jited_prog_len);
  238. else
  239. printf(" not jited");
  240. memlock = get_fdinfo(fd, "memlock");
  241. if (memlock)
  242. printf(" memlock %sB", memlock);
  243. free(memlock);
  244. if (info->nr_map_ids)
  245. show_prog_maps(fd, info->nr_map_ids);
  246. printf("\n");
  247. }
  248. static int show_prog(int fd)
  249. {
  250. struct bpf_prog_info info = {};
  251. __u32 len = sizeof(info);
  252. int err;
  253. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  254. if (err) {
  255. err("can't get prog info: %s\n", strerror(errno));
  256. return -1;
  257. }
  258. if (json_output)
  259. print_prog_json(&info, fd);
  260. else
  261. print_prog_plain(&info, fd);
  262. return 0;
  263. }
  264. static int do_show(int argc, char **argv)
  265. {
  266. __u32 id = 0;
  267. int err;
  268. int fd;
  269. if (argc == 2) {
  270. fd = prog_parse_fd(&argc, &argv);
  271. if (fd < 0)
  272. return -1;
  273. return show_prog(fd);
  274. }
  275. if (argc)
  276. return BAD_ARG();
  277. if (json_output)
  278. jsonw_start_array(json_wtr);
  279. while (true) {
  280. err = bpf_prog_get_next_id(id, &id);
  281. if (err) {
  282. if (errno == ENOENT) {
  283. err = 0;
  284. break;
  285. }
  286. err("can't get next program: %s\n", strerror(errno));
  287. if (errno == EINVAL)
  288. err("kernel too old?\n");
  289. err = -1;
  290. break;
  291. }
  292. fd = bpf_prog_get_fd_by_id(id);
  293. if (fd < 0) {
  294. err("can't get prog by id (%u): %s\n",
  295. id, strerror(errno));
  296. err = -1;
  297. break;
  298. }
  299. err = show_prog(fd);
  300. close(fd);
  301. if (err)
  302. break;
  303. }
  304. if (json_output)
  305. jsonw_end_array(json_wtr);
  306. return err;
  307. }
  308. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  309. {
  310. va_list args;
  311. va_start(args, fmt);
  312. vprintf(fmt, args);
  313. va_end(args);
  314. }
  315. static void dump_xlated(void *buf, unsigned int len, bool opcodes)
  316. {
  317. struct bpf_insn *insn = buf;
  318. bool double_insn = false;
  319. unsigned int i;
  320. for (i = 0; i < len / sizeof(*insn); i++) {
  321. if (double_insn) {
  322. double_insn = false;
  323. continue;
  324. }
  325. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  326. printf("% 4d: ", i);
  327. print_bpf_insn(print_insn, NULL, insn + i, true);
  328. if (opcodes) {
  329. printf(" ");
  330. fprint_hex(stdout, insn + i, 8, " ");
  331. if (double_insn && i < len - 1) {
  332. printf(" ");
  333. fprint_hex(stdout, insn + i + 1, 8, " ");
  334. }
  335. printf("\n");
  336. }
  337. }
  338. }
  339. static int do_dump(int argc, char **argv)
  340. {
  341. struct bpf_prog_info info = {};
  342. __u32 len = sizeof(info);
  343. unsigned int buf_size;
  344. char *filepath = NULL;
  345. bool opcodes = false;
  346. unsigned char *buf;
  347. __u32 *member_len;
  348. __u64 *member_ptr;
  349. ssize_t n;
  350. int err;
  351. int fd;
  352. if (is_prefix(*argv, "jited")) {
  353. member_len = &info.jited_prog_len;
  354. member_ptr = &info.jited_prog_insns;
  355. } else if (is_prefix(*argv, "xlated")) {
  356. member_len = &info.xlated_prog_len;
  357. member_ptr = &info.xlated_prog_insns;
  358. } else {
  359. err("expected 'xlated' or 'jited', got: %s\n", *argv);
  360. return -1;
  361. }
  362. NEXT_ARG();
  363. if (argc < 2)
  364. usage();
  365. fd = prog_parse_fd(&argc, &argv);
  366. if (fd < 0)
  367. return -1;
  368. if (is_prefix(*argv, "file")) {
  369. NEXT_ARG();
  370. if (!argc) {
  371. err("expected file path\n");
  372. return -1;
  373. }
  374. filepath = *argv;
  375. NEXT_ARG();
  376. } else if (is_prefix(*argv, "opcodes")) {
  377. opcodes = true;
  378. NEXT_ARG();
  379. }
  380. if (argc) {
  381. usage();
  382. return -1;
  383. }
  384. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  385. if (err) {
  386. err("can't get prog info: %s\n", strerror(errno));
  387. return -1;
  388. }
  389. if (!*member_len) {
  390. info("no instructions returned\n");
  391. close(fd);
  392. return 0;
  393. }
  394. buf_size = *member_len;
  395. buf = malloc(buf_size);
  396. if (!buf) {
  397. err("mem alloc failed\n");
  398. close(fd);
  399. return -1;
  400. }
  401. memset(&info, 0, sizeof(info));
  402. *member_ptr = ptr_to_u64(buf);
  403. *member_len = buf_size;
  404. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  405. close(fd);
  406. if (err) {
  407. err("can't get prog info: %s\n", strerror(errno));
  408. goto err_free;
  409. }
  410. if (*member_len > buf_size) {
  411. err("too many instructions returned\n");
  412. goto err_free;
  413. }
  414. if (filepath) {
  415. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  416. if (fd < 0) {
  417. err("can't open file %s: %s\n", filepath,
  418. strerror(errno));
  419. goto err_free;
  420. }
  421. n = write(fd, buf, *member_len);
  422. close(fd);
  423. if (n != *member_len) {
  424. err("error writing output file: %s\n",
  425. n < 0 ? strerror(errno) : "short write");
  426. goto err_free;
  427. }
  428. } else {
  429. if (member_len == &info.jited_prog_len)
  430. disasm_print_insn(buf, *member_len, opcodes);
  431. else
  432. dump_xlated(buf, *member_len, opcodes);
  433. }
  434. free(buf);
  435. return 0;
  436. err_free:
  437. free(buf);
  438. return -1;
  439. }
  440. static int do_pin(int argc, char **argv)
  441. {
  442. return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  443. }
  444. static int do_help(int argc, char **argv)
  445. {
  446. fprintf(stderr,
  447. "Usage: %s %s show [PROG]\n"
  448. " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
  449. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  450. " %s %s pin PROG FILE\n"
  451. " %s %s help\n"
  452. "\n"
  453. " " HELP_SPEC_PROGRAM "\n"
  454. "",
  455. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  456. bin_name, argv[-2], bin_name, argv[-2]);
  457. return 0;
  458. }
  459. static const struct cmd cmds[] = {
  460. { "show", do_show },
  461. { "help", do_help },
  462. { "dump", do_dump },
  463. { "pin", do_pin },
  464. { 0 }
  465. };
  466. int do_prog(int argc, char **argv)
  467. {
  468. return cmd_select(cmds, argc, argv, do_help);
  469. }