prog.c 14 KB

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