prog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
  201. print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
  202. if (info->load_time) {
  203. char buf[32];
  204. print_boot_time(info->load_time, buf, sizeof(buf));
  205. /* Piggy back on load_time, since 0 uid is a valid one */
  206. jsonw_string_field(json_wtr, "loaded_at", buf);
  207. jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
  208. }
  209. jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
  210. if (info->jited_prog_len) {
  211. jsonw_bool_field(json_wtr, "jited", true);
  212. jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
  213. } else {
  214. jsonw_bool_field(json_wtr, "jited", false);
  215. }
  216. memlock = get_fdinfo(fd, "memlock");
  217. if (memlock)
  218. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  219. free(memlock);
  220. if (info->nr_map_ids)
  221. show_prog_maps(fd, info->nr_map_ids);
  222. if (!hash_empty(prog_table.table)) {
  223. struct pinned_obj *obj;
  224. jsonw_name(json_wtr, "pinned");
  225. jsonw_start_array(json_wtr);
  226. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  227. if (obj->id == info->id)
  228. jsonw_string(json_wtr, obj->path);
  229. }
  230. jsonw_end_array(json_wtr);
  231. }
  232. jsonw_end_object(json_wtr);
  233. }
  234. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  235. {
  236. char *memlock;
  237. printf("%u: ", info->id);
  238. if (info->type < ARRAY_SIZE(prog_type_name))
  239. printf("%s ", prog_type_name[info->type]);
  240. else
  241. printf("type %u ", info->type);
  242. if (*info->name)
  243. printf("name %s ", info->name);
  244. printf("tag ");
  245. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  246. print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
  247. printf("%s", info->gpl_compatible ? " gpl" : "");
  248. printf("\n");
  249. if (info->load_time) {
  250. char buf[32];
  251. print_boot_time(info->load_time, buf, sizeof(buf));
  252. /* Piggy back on load_time, since 0 uid is a valid one */
  253. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  254. }
  255. printf("\txlated %uB", info->xlated_prog_len);
  256. if (info->jited_prog_len)
  257. printf(" jited %uB", info->jited_prog_len);
  258. else
  259. printf(" not jited");
  260. memlock = get_fdinfo(fd, "memlock");
  261. if (memlock)
  262. printf(" memlock %sB", memlock);
  263. free(memlock);
  264. if (info->nr_map_ids)
  265. show_prog_maps(fd, info->nr_map_ids);
  266. if (!hash_empty(prog_table.table)) {
  267. struct pinned_obj *obj;
  268. printf("\n");
  269. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  270. if (obj->id == info->id)
  271. printf("\tpinned %s\n", obj->path);
  272. }
  273. }
  274. printf("\n");
  275. }
  276. static int show_prog(int fd)
  277. {
  278. struct bpf_prog_info info = {};
  279. __u32 len = sizeof(info);
  280. int err;
  281. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  282. if (err) {
  283. p_err("can't get prog info: %s", strerror(errno));
  284. return -1;
  285. }
  286. if (json_output)
  287. print_prog_json(&info, fd);
  288. else
  289. print_prog_plain(&info, fd);
  290. return 0;
  291. }
  292. static int do_show(int argc, char **argv)
  293. {
  294. __u32 id = 0;
  295. int err;
  296. int fd;
  297. if (show_pinned)
  298. build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
  299. if (argc == 2) {
  300. fd = prog_parse_fd(&argc, &argv);
  301. if (fd < 0)
  302. return -1;
  303. return show_prog(fd);
  304. }
  305. if (argc)
  306. return BAD_ARG();
  307. if (json_output)
  308. jsonw_start_array(json_wtr);
  309. while (true) {
  310. err = bpf_prog_get_next_id(id, &id);
  311. if (err) {
  312. if (errno == ENOENT) {
  313. err = 0;
  314. break;
  315. }
  316. p_err("can't get next program: %s%s", strerror(errno),
  317. errno == EINVAL ? " -- kernel too old?" : "");
  318. err = -1;
  319. break;
  320. }
  321. fd = bpf_prog_get_fd_by_id(id);
  322. if (fd < 0) {
  323. if (errno == ENOENT)
  324. continue;
  325. p_err("can't get prog by id (%u): %s",
  326. id, strerror(errno));
  327. err = -1;
  328. break;
  329. }
  330. err = show_prog(fd);
  331. close(fd);
  332. if (err)
  333. break;
  334. }
  335. if (json_output)
  336. jsonw_end_array(json_wtr);
  337. return err;
  338. }
  339. static int do_dump(int argc, char **argv)
  340. {
  341. struct bpf_prog_info info = {};
  342. struct dump_data dd = {};
  343. __u32 len = sizeof(info);
  344. unsigned int buf_size;
  345. char *filepath = NULL;
  346. bool opcodes = false;
  347. bool visual = false;
  348. unsigned char *buf;
  349. __u32 *member_len;
  350. __u64 *member_ptr;
  351. ssize_t n;
  352. int err;
  353. int fd;
  354. if (is_prefix(*argv, "jited")) {
  355. member_len = &info.jited_prog_len;
  356. member_ptr = &info.jited_prog_insns;
  357. } else if (is_prefix(*argv, "xlated")) {
  358. member_len = &info.xlated_prog_len;
  359. member_ptr = &info.xlated_prog_insns;
  360. } else {
  361. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  362. return -1;
  363. }
  364. NEXT_ARG();
  365. if (argc < 2)
  366. usage();
  367. fd = prog_parse_fd(&argc, &argv);
  368. if (fd < 0)
  369. return -1;
  370. if (is_prefix(*argv, "file")) {
  371. NEXT_ARG();
  372. if (!argc) {
  373. p_err("expected file path");
  374. return -1;
  375. }
  376. filepath = *argv;
  377. NEXT_ARG();
  378. } else if (is_prefix(*argv, "opcodes")) {
  379. opcodes = true;
  380. NEXT_ARG();
  381. } else if (is_prefix(*argv, "visual")) {
  382. visual = true;
  383. NEXT_ARG();
  384. }
  385. if (argc) {
  386. usage();
  387. return -1;
  388. }
  389. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  390. if (err) {
  391. p_err("can't get prog info: %s", strerror(errno));
  392. return -1;
  393. }
  394. if (!*member_len) {
  395. p_info("no instructions returned");
  396. close(fd);
  397. return 0;
  398. }
  399. buf_size = *member_len;
  400. buf = malloc(buf_size);
  401. if (!buf) {
  402. p_err("mem alloc failed");
  403. close(fd);
  404. return -1;
  405. }
  406. memset(&info, 0, sizeof(info));
  407. *member_ptr = ptr_to_u64(buf);
  408. *member_len = buf_size;
  409. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  410. close(fd);
  411. if (err) {
  412. p_err("can't get prog info: %s", strerror(errno));
  413. goto err_free;
  414. }
  415. if (*member_len > buf_size) {
  416. p_err("too many instructions returned");
  417. goto err_free;
  418. }
  419. if ((member_len == &info.jited_prog_len &&
  420. info.jited_prog_insns == 0) ||
  421. (member_len == &info.xlated_prog_len &&
  422. info.xlated_prog_insns == 0)) {
  423. p_err("error retrieving insn dump: kernel.kptr_restrict set?");
  424. goto err_free;
  425. }
  426. if (filepath) {
  427. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  428. if (fd < 0) {
  429. p_err("can't open file %s: %s", filepath,
  430. strerror(errno));
  431. goto err_free;
  432. }
  433. n = write(fd, buf, *member_len);
  434. close(fd);
  435. if (n != *member_len) {
  436. p_err("error writing output file: %s",
  437. n < 0 ? strerror(errno) : "short write");
  438. goto err_free;
  439. }
  440. if (json_output)
  441. jsonw_null(json_wtr);
  442. } else if (member_len == &info.jited_prog_len) {
  443. const char *name = NULL;
  444. if (info.ifindex) {
  445. name = ifindex_to_bfd_name_ns(info.ifindex,
  446. info.netns_dev,
  447. info.netns_ino);
  448. if (!name)
  449. goto err_free;
  450. }
  451. disasm_print_insn(buf, *member_len, opcodes, name);
  452. } else if (visual) {
  453. if (json_output)
  454. jsonw_null(json_wtr);
  455. else
  456. dump_xlated_cfg(buf, *member_len);
  457. } else {
  458. kernel_syms_load(&dd);
  459. if (json_output)
  460. dump_xlated_json(&dd, buf, *member_len, opcodes);
  461. else
  462. dump_xlated_plain(&dd, buf, *member_len, opcodes);
  463. kernel_syms_destroy(&dd);
  464. }
  465. free(buf);
  466. return 0;
  467. err_free:
  468. free(buf);
  469. return -1;
  470. }
  471. static int do_pin(int argc, char **argv)
  472. {
  473. int err;
  474. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  475. if (!err && json_output)
  476. jsonw_null(json_wtr);
  477. return err;
  478. }
  479. static int do_load(int argc, char **argv)
  480. {
  481. struct bpf_object *obj;
  482. int prog_fd;
  483. if (argc != 2)
  484. usage();
  485. if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
  486. p_err("failed to load program");
  487. return -1;
  488. }
  489. if (do_pin_fd(prog_fd, argv[1])) {
  490. p_err("failed to pin program");
  491. return -1;
  492. }
  493. if (json_output)
  494. jsonw_null(json_wtr);
  495. return 0;
  496. }
  497. static int do_help(int argc, char **argv)
  498. {
  499. if (json_output) {
  500. jsonw_null(json_wtr);
  501. return 0;
  502. }
  503. fprintf(stderr,
  504. "Usage: %s %s { show | list } [PROG]\n"
  505. " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
  506. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  507. " %s %s pin PROG FILE\n"
  508. " %s %s load OBJ FILE\n"
  509. " %s %s help\n"
  510. "\n"
  511. " " HELP_SPEC_PROGRAM "\n"
  512. " " HELP_SPEC_OPTIONS "\n"
  513. "",
  514. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  515. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
  516. return 0;
  517. }
  518. static const struct cmd cmds[] = {
  519. { "show", do_show },
  520. { "list", do_show },
  521. { "help", do_help },
  522. { "dump", do_dump },
  523. { "pin", do_pin },
  524. { "load", do_load },
  525. { 0 }
  526. };
  527. int do_prog(int argc, char **argv)
  528. {
  529. return cmd_select(cmds, argc, argv, do_help);
  530. }