prog.c 14 KB

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