prog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. p_err("%s", strerror(errno));
  95. return -1;
  96. }
  97. fd = bpf_prog_get_fd_by_id(id);
  98. if (fd < 0) {
  99. p_err("can't get prog by id (%u): %s",
  100. id, strerror(errno));
  101. return -1;
  102. }
  103. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  104. if (err) {
  105. p_err("can't get prog info (%u): %s",
  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. p_err("can't parse %s as ID", **argv);
  125. return -1;
  126. }
  127. NEXT_ARGP();
  128. fd = bpf_prog_get_fd_by_id(id);
  129. if (fd < 0)
  130. p_err("get by id (%u): %s", 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. p_err("can't parse tag");
  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. p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **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. p_err("can't get prog info: %s", 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. p_err("can't get next program: %s%s", strerror(errno),
  287. errno == EINVAL ? " -- kernel too old?" : "");
  288. err = -1;
  289. break;
  290. }
  291. fd = bpf_prog_get_fd_by_id(id);
  292. if (fd < 0) {
  293. p_err("can't get prog by id (%u): %s",
  294. id, strerror(errno));
  295. err = -1;
  296. break;
  297. }
  298. err = show_prog(fd);
  299. close(fd);
  300. if (err)
  301. break;
  302. }
  303. if (json_output)
  304. jsonw_end_array(json_wtr);
  305. return err;
  306. }
  307. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  308. {
  309. va_list args;
  310. va_start(args, fmt);
  311. vprintf(fmt, args);
  312. va_end(args);
  313. }
  314. static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes)
  315. {
  316. struct bpf_insn *insn = buf;
  317. bool double_insn = false;
  318. unsigned int i;
  319. for (i = 0; i < len / sizeof(*insn); i++) {
  320. if (double_insn) {
  321. double_insn = false;
  322. continue;
  323. }
  324. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  325. printf("% 4d: ", i);
  326. print_bpf_insn(print_insn, NULL, insn + i, true);
  327. if (opcodes) {
  328. printf(" ");
  329. fprint_hex(stdout, insn + i, 8, " ");
  330. if (double_insn && i < len - 1) {
  331. printf(" ");
  332. fprint_hex(stdout, insn + i + 1, 8, " ");
  333. }
  334. printf("\n");
  335. }
  336. }
  337. }
  338. static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
  339. {
  340. unsigned int l = strlen(fmt);
  341. char chomped_fmt[l];
  342. va_list args;
  343. va_start(args, fmt);
  344. if (l > 0) {
  345. strncpy(chomped_fmt, fmt, l - 1);
  346. chomped_fmt[l - 1] = '\0';
  347. }
  348. jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
  349. va_end(args);
  350. }
  351. static void dump_xlated_json(void *buf, unsigned int len, bool opcodes)
  352. {
  353. struct bpf_insn *insn = buf;
  354. bool double_insn = false;
  355. unsigned int i;
  356. jsonw_start_array(json_wtr);
  357. for (i = 0; i < len / sizeof(*insn); i++) {
  358. if (double_insn) {
  359. double_insn = false;
  360. continue;
  361. }
  362. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  363. jsonw_start_object(json_wtr);
  364. jsonw_name(json_wtr, "disasm");
  365. print_bpf_insn(print_insn_json, NULL, insn + i, true);
  366. if (opcodes) {
  367. jsonw_name(json_wtr, "opcodes");
  368. jsonw_start_object(json_wtr);
  369. jsonw_name(json_wtr, "code");
  370. jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
  371. jsonw_name(json_wtr, "src_reg");
  372. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
  373. jsonw_name(json_wtr, "dst_reg");
  374. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
  375. jsonw_name(json_wtr, "off");
  376. print_hex_data_json((uint8_t *)(&insn[i].off), 2);
  377. jsonw_name(json_wtr, "imm");
  378. if (double_insn && i < len - 1)
  379. print_hex_data_json((uint8_t *)(&insn[i].imm),
  380. 12);
  381. else
  382. print_hex_data_json((uint8_t *)(&insn[i].imm),
  383. 4);
  384. jsonw_end_object(json_wtr);
  385. }
  386. jsonw_end_object(json_wtr);
  387. }
  388. jsonw_end_array(json_wtr);
  389. }
  390. static int do_dump(int argc, char **argv)
  391. {
  392. struct bpf_prog_info info = {};
  393. __u32 len = sizeof(info);
  394. unsigned int buf_size;
  395. char *filepath = NULL;
  396. bool opcodes = false;
  397. unsigned char *buf;
  398. __u32 *member_len;
  399. __u64 *member_ptr;
  400. ssize_t n;
  401. int err;
  402. int fd;
  403. if (is_prefix(*argv, "jited")) {
  404. member_len = &info.jited_prog_len;
  405. member_ptr = &info.jited_prog_insns;
  406. } else if (is_prefix(*argv, "xlated")) {
  407. member_len = &info.xlated_prog_len;
  408. member_ptr = &info.xlated_prog_insns;
  409. } else {
  410. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  411. return -1;
  412. }
  413. NEXT_ARG();
  414. if (argc < 2)
  415. usage();
  416. fd = prog_parse_fd(&argc, &argv);
  417. if (fd < 0)
  418. return -1;
  419. if (is_prefix(*argv, "file")) {
  420. NEXT_ARG();
  421. if (!argc) {
  422. p_err("expected file path");
  423. return -1;
  424. }
  425. filepath = *argv;
  426. NEXT_ARG();
  427. } else if (is_prefix(*argv, "opcodes")) {
  428. opcodes = true;
  429. NEXT_ARG();
  430. }
  431. if (argc) {
  432. usage();
  433. return -1;
  434. }
  435. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  436. if (err) {
  437. p_err("can't get prog info: %s", strerror(errno));
  438. return -1;
  439. }
  440. if (!*member_len) {
  441. p_info("no instructions returned");
  442. close(fd);
  443. return 0;
  444. }
  445. buf_size = *member_len;
  446. buf = malloc(buf_size);
  447. if (!buf) {
  448. p_err("mem alloc failed");
  449. close(fd);
  450. return -1;
  451. }
  452. memset(&info, 0, sizeof(info));
  453. *member_ptr = ptr_to_u64(buf);
  454. *member_len = buf_size;
  455. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  456. close(fd);
  457. if (err) {
  458. p_err("can't get prog info: %s", strerror(errno));
  459. goto err_free;
  460. }
  461. if (*member_len > buf_size) {
  462. p_err("too many instructions returned");
  463. goto err_free;
  464. }
  465. if (filepath) {
  466. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  467. if (fd < 0) {
  468. p_err("can't open file %s: %s", filepath,
  469. strerror(errno));
  470. goto err_free;
  471. }
  472. n = write(fd, buf, *member_len);
  473. close(fd);
  474. if (n != *member_len) {
  475. p_err("error writing output file: %s",
  476. n < 0 ? strerror(errno) : "short write");
  477. goto err_free;
  478. }
  479. } else {
  480. if (member_len == &info.jited_prog_len)
  481. disasm_print_insn(buf, *member_len, opcodes);
  482. else
  483. if (json_output)
  484. dump_xlated_json(buf, *member_len, opcodes);
  485. else
  486. dump_xlated_plain(buf, *member_len, opcodes);
  487. }
  488. free(buf);
  489. return 0;
  490. err_free:
  491. free(buf);
  492. return -1;
  493. }
  494. static int do_pin(int argc, char **argv)
  495. {
  496. return do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  497. }
  498. static int do_help(int argc, char **argv)
  499. {
  500. fprintf(stderr,
  501. "Usage: %s %s show [PROG]\n"
  502. " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
  503. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  504. " %s %s pin PROG FILE\n"
  505. " %s %s help\n"
  506. "\n"
  507. " " HELP_SPEC_PROGRAM "\n"
  508. "",
  509. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  510. bin_name, argv[-2], bin_name, argv[-2]);
  511. return 0;
  512. }
  513. static const struct cmd cmds[] = {
  514. { "show", do_show },
  515. { "help", do_help },
  516. { "dump", do_dump },
  517. { "pin", do_pin },
  518. { 0 }
  519. };
  520. int do_prog(int argc, char **argv)
  521. {
  522. return cmd_select(cmds, argc, argv, do_help);
  523. }