prog.c 14 KB

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