prog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 <net/if.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <bpf.h>
  46. #include "main.h"
  47. #include "disasm.h"
  48. static const char * const prog_type_name[] = {
  49. [BPF_PROG_TYPE_UNSPEC] = "unspec",
  50. [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
  51. [BPF_PROG_TYPE_KPROBE] = "kprobe",
  52. [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
  53. [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
  54. [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
  55. [BPF_PROG_TYPE_XDP] = "xdp",
  56. [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
  57. [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
  58. [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
  59. [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
  60. [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
  61. [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
  62. [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
  63. [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
  64. };
  65. static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
  66. {
  67. struct timespec real_time_ts, boot_time_ts;
  68. time_t wallclock_secs;
  69. struct tm load_tm;
  70. buf[--size] = '\0';
  71. if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
  72. clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
  73. perror("Can't read clocks");
  74. snprintf(buf, size, "%llu", nsecs / 1000000000);
  75. return;
  76. }
  77. wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
  78. nsecs / 1000000000;
  79. if (!localtime_r(&wallclock_secs, &load_tm)) {
  80. snprintf(buf, size, "%llu", nsecs / 1000000000);
  81. return;
  82. }
  83. strftime(buf, size, "%b %d/%H:%M", &load_tm);
  84. }
  85. static int prog_fd_by_tag(unsigned char *tag)
  86. {
  87. struct bpf_prog_info info = {};
  88. __u32 len = sizeof(info);
  89. unsigned int id = 0;
  90. int err;
  91. int fd;
  92. while (true) {
  93. err = bpf_prog_get_next_id(id, &id);
  94. if (err) {
  95. p_err("%s", strerror(errno));
  96. return -1;
  97. }
  98. fd = bpf_prog_get_fd_by_id(id);
  99. if (fd < 0) {
  100. p_err("can't get prog by id (%u): %s",
  101. id, strerror(errno));
  102. return -1;
  103. }
  104. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  105. if (err) {
  106. p_err("can't get prog info (%u): %s",
  107. id, strerror(errno));
  108. close(fd);
  109. return -1;
  110. }
  111. if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
  112. return fd;
  113. close(fd);
  114. }
  115. }
  116. int prog_parse_fd(int *argc, char ***argv)
  117. {
  118. int fd;
  119. if (is_prefix(**argv, "id")) {
  120. unsigned int id;
  121. char *endptr;
  122. NEXT_ARGP();
  123. id = strtoul(**argv, &endptr, 0);
  124. if (*endptr) {
  125. p_err("can't parse %s as ID", **argv);
  126. return -1;
  127. }
  128. NEXT_ARGP();
  129. fd = bpf_prog_get_fd_by_id(id);
  130. if (fd < 0)
  131. p_err("get by id (%u): %s", id, strerror(errno));
  132. return fd;
  133. } else if (is_prefix(**argv, "tag")) {
  134. unsigned char tag[BPF_TAG_SIZE];
  135. NEXT_ARGP();
  136. if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
  137. tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
  138. != BPF_TAG_SIZE) {
  139. p_err("can't parse tag");
  140. return -1;
  141. }
  142. NEXT_ARGP();
  143. return prog_fd_by_tag(tag);
  144. } else if (is_prefix(**argv, "pinned")) {
  145. char *path;
  146. NEXT_ARGP();
  147. path = **argv;
  148. NEXT_ARGP();
  149. return open_obj_pinned_any(path, BPF_OBJ_PROG);
  150. }
  151. p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
  152. return -1;
  153. }
  154. static void show_prog_maps(int fd, u32 num_maps)
  155. {
  156. struct bpf_prog_info info = {};
  157. __u32 len = sizeof(info);
  158. __u32 map_ids[num_maps];
  159. unsigned int i;
  160. int err;
  161. info.nr_map_ids = num_maps;
  162. info.map_ids = ptr_to_u64(map_ids);
  163. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  164. if (err || !info.nr_map_ids)
  165. return;
  166. if (json_output) {
  167. jsonw_name(json_wtr, "map_ids");
  168. jsonw_start_array(json_wtr);
  169. for (i = 0; i < info.nr_map_ids; i++)
  170. jsonw_uint(json_wtr, map_ids[i]);
  171. jsonw_end_array(json_wtr);
  172. } else {
  173. printf(" map_ids ");
  174. for (i = 0; i < info.nr_map_ids; i++)
  175. printf("%u%s", map_ids[i],
  176. i == info.nr_map_ids - 1 ? "" : ",");
  177. }
  178. }
  179. static void print_prog_json(struct bpf_prog_info *info, int fd)
  180. {
  181. char *memlock;
  182. jsonw_start_object(json_wtr);
  183. jsonw_uint_field(json_wtr, "id", info->id);
  184. if (info->type < ARRAY_SIZE(prog_type_name))
  185. jsonw_string_field(json_wtr, "type",
  186. prog_type_name[info->type]);
  187. else
  188. jsonw_uint_field(json_wtr, "type", info->type);
  189. if (*info->name)
  190. jsonw_string_field(json_wtr, "name", info->name);
  191. jsonw_name(json_wtr, "tag");
  192. jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
  193. info->tag[0], info->tag[1], info->tag[2], info->tag[3],
  194. info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
  195. if (info->status & BPF_PROG_STATUS_DEV_BOUND) {
  196. jsonw_name(json_wtr, "dev");
  197. if (info->ifindex) {
  198. char name[IF_NAMESIZE];
  199. if (!if_indextoname(info->ifindex, name))
  200. jsonw_printf(json_wtr, "\"ifindex:%d\"",
  201. info->ifindex);
  202. else
  203. jsonw_printf(json_wtr, "\"%s\"", name);
  204. } else {
  205. jsonw_printf(json_wtr, "\"unknown\"");
  206. }
  207. }
  208. if (info->load_time) {
  209. char buf[32];
  210. print_boot_time(info->load_time, buf, sizeof(buf));
  211. /* Piggy back on load_time, since 0 uid is a valid one */
  212. jsonw_string_field(json_wtr, "loaded_at", buf);
  213. jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
  214. }
  215. jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
  216. if (info->jited_prog_len) {
  217. jsonw_bool_field(json_wtr, "jited", true);
  218. jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
  219. } else {
  220. jsonw_bool_field(json_wtr, "jited", false);
  221. }
  222. memlock = get_fdinfo(fd, "memlock");
  223. if (memlock)
  224. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  225. free(memlock);
  226. if (info->nr_map_ids)
  227. show_prog_maps(fd, info->nr_map_ids);
  228. jsonw_end_object(json_wtr);
  229. }
  230. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  231. {
  232. char *memlock;
  233. printf("%u: ", info->id);
  234. if (info->type < ARRAY_SIZE(prog_type_name))
  235. printf("%s ", prog_type_name[info->type]);
  236. else
  237. printf("type %u ", info->type);
  238. if (*info->name)
  239. printf("name %s ", info->name);
  240. printf("tag ");
  241. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  242. printf(" ");
  243. if (info->status & BPF_PROG_STATUS_DEV_BOUND) {
  244. printf("dev ");
  245. if (info->ifindex) {
  246. char name[IF_NAMESIZE];
  247. if (!if_indextoname(info->ifindex, name))
  248. printf("ifindex:%d ", info->ifindex);
  249. else
  250. printf("%s ", name);
  251. } else {
  252. printf("unknown ");
  253. }
  254. }
  255. printf("\n");
  256. if (info->load_time) {
  257. char buf[32];
  258. print_boot_time(info->load_time, buf, sizeof(buf));
  259. /* Piggy back on load_time, since 0 uid is a valid one */
  260. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  261. }
  262. printf("\txlated %uB", info->xlated_prog_len);
  263. if (info->jited_prog_len)
  264. printf(" jited %uB", info->jited_prog_len);
  265. else
  266. printf(" not jited");
  267. memlock = get_fdinfo(fd, "memlock");
  268. if (memlock)
  269. printf(" memlock %sB", memlock);
  270. free(memlock);
  271. if (info->nr_map_ids)
  272. show_prog_maps(fd, info->nr_map_ids);
  273. printf("\n");
  274. }
  275. static int show_prog(int fd)
  276. {
  277. struct bpf_prog_info info = {};
  278. __u32 len = sizeof(info);
  279. int err;
  280. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  281. if (err) {
  282. p_err("can't get prog info: %s", strerror(errno));
  283. return -1;
  284. }
  285. if (json_output)
  286. print_prog_json(&info, fd);
  287. else
  288. print_prog_plain(&info, fd);
  289. return 0;
  290. }
  291. static int do_show(int argc, char **argv)
  292. {
  293. __u32 id = 0;
  294. int err;
  295. int fd;
  296. if (argc == 2) {
  297. fd = prog_parse_fd(&argc, &argv);
  298. if (fd < 0)
  299. return -1;
  300. return show_prog(fd);
  301. }
  302. if (argc)
  303. return BAD_ARG();
  304. if (json_output)
  305. jsonw_start_array(json_wtr);
  306. while (true) {
  307. err = bpf_prog_get_next_id(id, &id);
  308. if (err) {
  309. if (errno == ENOENT) {
  310. err = 0;
  311. break;
  312. }
  313. p_err("can't get next program: %s%s", strerror(errno),
  314. errno == EINVAL ? " -- kernel too old?" : "");
  315. err = -1;
  316. break;
  317. }
  318. fd = bpf_prog_get_fd_by_id(id);
  319. if (fd < 0) {
  320. p_err("can't get prog by id (%u): %s",
  321. id, strerror(errno));
  322. err = -1;
  323. break;
  324. }
  325. err = show_prog(fd);
  326. close(fd);
  327. if (err)
  328. break;
  329. }
  330. if (json_output)
  331. jsonw_end_array(json_wtr);
  332. return err;
  333. }
  334. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  335. {
  336. va_list args;
  337. va_start(args, fmt);
  338. vprintf(fmt, args);
  339. va_end(args);
  340. }
  341. static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes)
  342. {
  343. struct bpf_insn *insn = buf;
  344. bool double_insn = false;
  345. unsigned int i;
  346. for (i = 0; i < len / sizeof(*insn); i++) {
  347. if (double_insn) {
  348. double_insn = false;
  349. continue;
  350. }
  351. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  352. printf("% 4d: ", i);
  353. print_bpf_insn(print_insn, NULL, insn + i, true);
  354. if (opcodes) {
  355. printf(" ");
  356. fprint_hex(stdout, insn + i, 8, " ");
  357. if (double_insn && i < len - 1) {
  358. printf(" ");
  359. fprint_hex(stdout, insn + i + 1, 8, " ");
  360. }
  361. printf("\n");
  362. }
  363. }
  364. }
  365. static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
  366. {
  367. unsigned int l = strlen(fmt);
  368. char chomped_fmt[l];
  369. va_list args;
  370. va_start(args, fmt);
  371. if (l > 0) {
  372. strncpy(chomped_fmt, fmt, l - 1);
  373. chomped_fmt[l - 1] = '\0';
  374. }
  375. jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
  376. va_end(args);
  377. }
  378. static void dump_xlated_json(void *buf, unsigned int len, bool opcodes)
  379. {
  380. struct bpf_insn *insn = buf;
  381. bool double_insn = false;
  382. unsigned int i;
  383. jsonw_start_array(json_wtr);
  384. for (i = 0; i < len / sizeof(*insn); i++) {
  385. if (double_insn) {
  386. double_insn = false;
  387. continue;
  388. }
  389. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  390. jsonw_start_object(json_wtr);
  391. jsonw_name(json_wtr, "disasm");
  392. print_bpf_insn(print_insn_json, NULL, insn + i, true);
  393. if (opcodes) {
  394. jsonw_name(json_wtr, "opcodes");
  395. jsonw_start_object(json_wtr);
  396. jsonw_name(json_wtr, "code");
  397. jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
  398. jsonw_name(json_wtr, "src_reg");
  399. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
  400. jsonw_name(json_wtr, "dst_reg");
  401. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
  402. jsonw_name(json_wtr, "off");
  403. print_hex_data_json((uint8_t *)(&insn[i].off), 2);
  404. jsonw_name(json_wtr, "imm");
  405. if (double_insn && i < len - 1)
  406. print_hex_data_json((uint8_t *)(&insn[i].imm),
  407. 12);
  408. else
  409. print_hex_data_json((uint8_t *)(&insn[i].imm),
  410. 4);
  411. jsonw_end_object(json_wtr);
  412. }
  413. jsonw_end_object(json_wtr);
  414. }
  415. jsonw_end_array(json_wtr);
  416. }
  417. static int do_dump(int argc, char **argv)
  418. {
  419. struct bpf_prog_info info = {};
  420. __u32 len = sizeof(info);
  421. unsigned int buf_size;
  422. char *filepath = NULL;
  423. bool opcodes = false;
  424. unsigned char *buf;
  425. __u32 *member_len;
  426. __u64 *member_ptr;
  427. ssize_t n;
  428. int err;
  429. int fd;
  430. if (is_prefix(*argv, "jited")) {
  431. member_len = &info.jited_prog_len;
  432. member_ptr = &info.jited_prog_insns;
  433. } else if (is_prefix(*argv, "xlated")) {
  434. member_len = &info.xlated_prog_len;
  435. member_ptr = &info.xlated_prog_insns;
  436. } else {
  437. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  438. return -1;
  439. }
  440. NEXT_ARG();
  441. if (argc < 2)
  442. usage();
  443. fd = prog_parse_fd(&argc, &argv);
  444. if (fd < 0)
  445. return -1;
  446. if (is_prefix(*argv, "file")) {
  447. NEXT_ARG();
  448. if (!argc) {
  449. p_err("expected file path");
  450. return -1;
  451. }
  452. filepath = *argv;
  453. NEXT_ARG();
  454. } else if (is_prefix(*argv, "opcodes")) {
  455. opcodes = true;
  456. NEXT_ARG();
  457. }
  458. if (argc) {
  459. usage();
  460. return -1;
  461. }
  462. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  463. if (err) {
  464. p_err("can't get prog info: %s", strerror(errno));
  465. return -1;
  466. }
  467. if (!*member_len) {
  468. p_info("no instructions returned");
  469. close(fd);
  470. return 0;
  471. }
  472. buf_size = *member_len;
  473. buf = malloc(buf_size);
  474. if (!buf) {
  475. p_err("mem alloc failed");
  476. close(fd);
  477. return -1;
  478. }
  479. memset(&info, 0, sizeof(info));
  480. *member_ptr = ptr_to_u64(buf);
  481. *member_len = buf_size;
  482. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  483. close(fd);
  484. if (err) {
  485. p_err("can't get prog info: %s", strerror(errno));
  486. goto err_free;
  487. }
  488. if (*member_len > buf_size) {
  489. p_err("too many instructions returned");
  490. goto err_free;
  491. }
  492. if (filepath) {
  493. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  494. if (fd < 0) {
  495. p_err("can't open file %s: %s", filepath,
  496. strerror(errno));
  497. goto err_free;
  498. }
  499. n = write(fd, buf, *member_len);
  500. close(fd);
  501. if (n != *member_len) {
  502. p_err("error writing output file: %s",
  503. n < 0 ? strerror(errno) : "short write");
  504. goto err_free;
  505. }
  506. } else {
  507. if (member_len == &info.jited_prog_len)
  508. disasm_print_insn(buf, *member_len, opcodes);
  509. else
  510. if (json_output)
  511. dump_xlated_json(buf, *member_len, opcodes);
  512. else
  513. dump_xlated_plain(buf, *member_len, opcodes);
  514. }
  515. free(buf);
  516. return 0;
  517. err_free:
  518. free(buf);
  519. return -1;
  520. }
  521. static int do_pin(int argc, char **argv)
  522. {
  523. int err;
  524. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  525. if (!err && json_output)
  526. jsonw_null(json_wtr);
  527. return err;
  528. }
  529. static int do_help(int argc, char **argv)
  530. {
  531. if (json_output) {
  532. jsonw_null(json_wtr);
  533. return 0;
  534. }
  535. fprintf(stderr,
  536. "Usage: %s %s show [PROG]\n"
  537. " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
  538. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  539. " %s %s pin PROG FILE\n"
  540. " %s %s help\n"
  541. "\n"
  542. " " HELP_SPEC_PROGRAM "\n"
  543. " " HELP_SPEC_OPTIONS "\n"
  544. "",
  545. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  546. bin_name, argv[-2], bin_name, argv[-2]);
  547. return 0;
  548. }
  549. static const struct cmd cmds[] = {
  550. { "show", do_show },
  551. { "help", do_help },
  552. { "dump", do_dump },
  553. { "pin", do_pin },
  554. { 0 }
  555. };
  556. int do_prog(int argc, char **argv)
  557. {
  558. return cmd_select(cmds, argc, argv, do_help);
  559. }