prog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. if (!hash_empty(prog_table.table)) {
  229. struct pinned_obj *obj;
  230. jsonw_name(json_wtr, "pinned");
  231. jsonw_start_array(json_wtr);
  232. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  233. if (obj->id == info->id)
  234. jsonw_string(json_wtr, obj->path);
  235. }
  236. jsonw_end_array(json_wtr);
  237. }
  238. jsonw_end_object(json_wtr);
  239. }
  240. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  241. {
  242. char *memlock;
  243. printf("%u: ", info->id);
  244. if (info->type < ARRAY_SIZE(prog_type_name))
  245. printf("%s ", prog_type_name[info->type]);
  246. else
  247. printf("type %u ", info->type);
  248. if (*info->name)
  249. printf("name %s ", info->name);
  250. printf("tag ");
  251. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  252. printf(" ");
  253. if (info->status & BPF_PROG_STATUS_DEV_BOUND) {
  254. printf("dev ");
  255. if (info->ifindex) {
  256. char name[IF_NAMESIZE];
  257. if (!if_indextoname(info->ifindex, name))
  258. printf("ifindex:%d ", info->ifindex);
  259. else
  260. printf("%s ", name);
  261. } else {
  262. printf("unknown ");
  263. }
  264. }
  265. printf("\n");
  266. if (info->load_time) {
  267. char buf[32];
  268. print_boot_time(info->load_time, buf, sizeof(buf));
  269. /* Piggy back on load_time, since 0 uid is a valid one */
  270. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  271. }
  272. printf("\txlated %uB", info->xlated_prog_len);
  273. if (info->jited_prog_len)
  274. printf(" jited %uB", info->jited_prog_len);
  275. else
  276. printf(" not jited");
  277. memlock = get_fdinfo(fd, "memlock");
  278. if (memlock)
  279. printf(" memlock %sB", memlock);
  280. free(memlock);
  281. if (info->nr_map_ids)
  282. show_prog_maps(fd, info->nr_map_ids);
  283. if (!hash_empty(prog_table.table)) {
  284. struct pinned_obj *obj;
  285. printf("\n");
  286. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  287. if (obj->id == info->id)
  288. printf("\tpinned %s\n", obj->path);
  289. }
  290. }
  291. printf("\n");
  292. }
  293. static int show_prog(int fd)
  294. {
  295. struct bpf_prog_info info = {};
  296. __u32 len = sizeof(info);
  297. int err;
  298. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  299. if (err) {
  300. p_err("can't get prog info: %s", strerror(errno));
  301. return -1;
  302. }
  303. if (json_output)
  304. print_prog_json(&info, fd);
  305. else
  306. print_prog_plain(&info, fd);
  307. return 0;
  308. }
  309. static int do_show(int argc, char **argv)
  310. {
  311. __u32 id = 0;
  312. int err;
  313. int fd;
  314. if (show_pinned)
  315. build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
  316. if (argc == 2) {
  317. fd = prog_parse_fd(&argc, &argv);
  318. if (fd < 0)
  319. return -1;
  320. return show_prog(fd);
  321. }
  322. if (argc)
  323. return BAD_ARG();
  324. if (json_output)
  325. jsonw_start_array(json_wtr);
  326. while (true) {
  327. err = bpf_prog_get_next_id(id, &id);
  328. if (err) {
  329. if (errno == ENOENT) {
  330. err = 0;
  331. break;
  332. }
  333. p_err("can't get next program: %s%s", strerror(errno),
  334. errno == EINVAL ? " -- kernel too old?" : "");
  335. err = -1;
  336. break;
  337. }
  338. fd = bpf_prog_get_fd_by_id(id);
  339. if (fd < 0) {
  340. p_err("can't get prog by id (%u): %s",
  341. id, strerror(errno));
  342. err = -1;
  343. break;
  344. }
  345. err = show_prog(fd);
  346. close(fd);
  347. if (err)
  348. break;
  349. }
  350. if (json_output)
  351. jsonw_end_array(json_wtr);
  352. return err;
  353. }
  354. static void print_insn(struct bpf_verifier_env *env, const char *fmt, ...)
  355. {
  356. va_list args;
  357. va_start(args, fmt);
  358. vprintf(fmt, args);
  359. va_end(args);
  360. }
  361. static void dump_xlated_plain(void *buf, unsigned int len, bool opcodes)
  362. {
  363. struct bpf_insn *insn = buf;
  364. bool double_insn = false;
  365. unsigned int i;
  366. for (i = 0; i < len / sizeof(*insn); i++) {
  367. if (double_insn) {
  368. double_insn = false;
  369. continue;
  370. }
  371. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  372. printf("% 4d: ", i);
  373. print_bpf_insn(print_insn, NULL, insn + i, true);
  374. if (opcodes) {
  375. printf(" ");
  376. fprint_hex(stdout, insn + i, 8, " ");
  377. if (double_insn && i < len - 1) {
  378. printf(" ");
  379. fprint_hex(stdout, insn + i + 1, 8, " ");
  380. }
  381. printf("\n");
  382. }
  383. }
  384. }
  385. static void print_insn_json(struct bpf_verifier_env *env, const char *fmt, ...)
  386. {
  387. unsigned int l = strlen(fmt);
  388. char chomped_fmt[l];
  389. va_list args;
  390. va_start(args, fmt);
  391. if (l > 0) {
  392. strncpy(chomped_fmt, fmt, l - 1);
  393. chomped_fmt[l - 1] = '\0';
  394. }
  395. jsonw_vprintf_enquote(json_wtr, chomped_fmt, args);
  396. va_end(args);
  397. }
  398. static void dump_xlated_json(void *buf, unsigned int len, bool opcodes)
  399. {
  400. struct bpf_insn *insn = buf;
  401. bool double_insn = false;
  402. unsigned int i;
  403. jsonw_start_array(json_wtr);
  404. for (i = 0; i < len / sizeof(*insn); i++) {
  405. if (double_insn) {
  406. double_insn = false;
  407. continue;
  408. }
  409. double_insn = insn[i].code == (BPF_LD | BPF_IMM | BPF_DW);
  410. jsonw_start_object(json_wtr);
  411. jsonw_name(json_wtr, "disasm");
  412. print_bpf_insn(print_insn_json, NULL, insn + i, true);
  413. if (opcodes) {
  414. jsonw_name(json_wtr, "opcodes");
  415. jsonw_start_object(json_wtr);
  416. jsonw_name(json_wtr, "code");
  417. jsonw_printf(json_wtr, "\"0x%02hhx\"", insn[i].code);
  418. jsonw_name(json_wtr, "src_reg");
  419. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].src_reg);
  420. jsonw_name(json_wtr, "dst_reg");
  421. jsonw_printf(json_wtr, "\"0x%hhx\"", insn[i].dst_reg);
  422. jsonw_name(json_wtr, "off");
  423. print_hex_data_json((uint8_t *)(&insn[i].off), 2);
  424. jsonw_name(json_wtr, "imm");
  425. if (double_insn && i < len - 1)
  426. print_hex_data_json((uint8_t *)(&insn[i].imm),
  427. 12);
  428. else
  429. print_hex_data_json((uint8_t *)(&insn[i].imm),
  430. 4);
  431. jsonw_end_object(json_wtr);
  432. }
  433. jsonw_end_object(json_wtr);
  434. }
  435. jsonw_end_array(json_wtr);
  436. }
  437. static int do_dump(int argc, char **argv)
  438. {
  439. struct bpf_prog_info info = {};
  440. __u32 len = sizeof(info);
  441. unsigned int buf_size;
  442. char *filepath = NULL;
  443. bool opcodes = false;
  444. unsigned char *buf;
  445. __u32 *member_len;
  446. __u64 *member_ptr;
  447. ssize_t n;
  448. int err;
  449. int fd;
  450. if (is_prefix(*argv, "jited")) {
  451. member_len = &info.jited_prog_len;
  452. member_ptr = &info.jited_prog_insns;
  453. } else if (is_prefix(*argv, "xlated")) {
  454. member_len = &info.xlated_prog_len;
  455. member_ptr = &info.xlated_prog_insns;
  456. } else {
  457. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  458. return -1;
  459. }
  460. NEXT_ARG();
  461. if (argc < 2)
  462. usage();
  463. fd = prog_parse_fd(&argc, &argv);
  464. if (fd < 0)
  465. return -1;
  466. if (is_prefix(*argv, "file")) {
  467. NEXT_ARG();
  468. if (!argc) {
  469. p_err("expected file path");
  470. return -1;
  471. }
  472. filepath = *argv;
  473. NEXT_ARG();
  474. } else if (is_prefix(*argv, "opcodes")) {
  475. opcodes = true;
  476. NEXT_ARG();
  477. }
  478. if (argc) {
  479. usage();
  480. return -1;
  481. }
  482. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  483. if (err) {
  484. p_err("can't get prog info: %s", strerror(errno));
  485. return -1;
  486. }
  487. if (!*member_len) {
  488. p_info("no instructions returned");
  489. close(fd);
  490. return 0;
  491. }
  492. buf_size = *member_len;
  493. buf = malloc(buf_size);
  494. if (!buf) {
  495. p_err("mem alloc failed");
  496. close(fd);
  497. return -1;
  498. }
  499. memset(&info, 0, sizeof(info));
  500. *member_ptr = ptr_to_u64(buf);
  501. *member_len = buf_size;
  502. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  503. close(fd);
  504. if (err) {
  505. p_err("can't get prog info: %s", strerror(errno));
  506. goto err_free;
  507. }
  508. if (*member_len > buf_size) {
  509. p_err("too many instructions returned");
  510. goto err_free;
  511. }
  512. if (filepath) {
  513. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  514. if (fd < 0) {
  515. p_err("can't open file %s: %s", filepath,
  516. strerror(errno));
  517. goto err_free;
  518. }
  519. n = write(fd, buf, *member_len);
  520. close(fd);
  521. if (n != *member_len) {
  522. p_err("error writing output file: %s",
  523. n < 0 ? strerror(errno) : "short write");
  524. goto err_free;
  525. }
  526. } else {
  527. if (member_len == &info.jited_prog_len)
  528. disasm_print_insn(buf, *member_len, opcodes);
  529. else
  530. if (json_output)
  531. dump_xlated_json(buf, *member_len, opcodes);
  532. else
  533. dump_xlated_plain(buf, *member_len, opcodes);
  534. }
  535. free(buf);
  536. return 0;
  537. err_free:
  538. free(buf);
  539. return -1;
  540. }
  541. static int do_pin(int argc, char **argv)
  542. {
  543. int err;
  544. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  545. if (!err && json_output)
  546. jsonw_null(json_wtr);
  547. return err;
  548. }
  549. static int do_help(int argc, char **argv)
  550. {
  551. if (json_output) {
  552. jsonw_null(json_wtr);
  553. return 0;
  554. }
  555. fprintf(stderr,
  556. "Usage: %s %s show [PROG]\n"
  557. " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"
  558. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  559. " %s %s pin PROG FILE\n"
  560. " %s %s help\n"
  561. "\n"
  562. " " HELP_SPEC_PROGRAM "\n"
  563. " " HELP_SPEC_OPTIONS "\n"
  564. "",
  565. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  566. bin_name, argv[-2], bin_name, argv[-2]);
  567. return 0;
  568. }
  569. static const struct cmd cmds[] = {
  570. { "show", do_show },
  571. { "help", do_help },
  572. { "dump", do_dump },
  573. { "pin", do_pin },
  574. { 0 }
  575. };
  576. int do_prog(int argc, char **argv)
  577. {
  578. return cmd_select(cmds, argc, argv, do_help);
  579. }