prog.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * Copyright (C) 2017-2018 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. #define _GNU_SOURCE
  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 <linux/err.h>
  46. #include <bpf.h>
  47. #include <libbpf.h>
  48. #include "cfg.h"
  49. #include "main.h"
  50. #include "xlated_dumper.h"
  51. static const char * const prog_type_name[] = {
  52. [BPF_PROG_TYPE_UNSPEC] = "unspec",
  53. [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
  54. [BPF_PROG_TYPE_KPROBE] = "kprobe",
  55. [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
  56. [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
  57. [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
  58. [BPF_PROG_TYPE_XDP] = "xdp",
  59. [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
  60. [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
  61. [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
  62. [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
  63. [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
  64. [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
  65. [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
  66. [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
  67. [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
  68. [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
  69. [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
  70. [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
  71. [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
  72. [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
  73. };
  74. static const char * const attach_type_strings[] = {
  75. [BPF_SK_SKB_STREAM_PARSER] = "stream_parser",
  76. [BPF_SK_SKB_STREAM_VERDICT] = "stream_verdict",
  77. [BPF_SK_MSG_VERDICT] = "msg_verdict",
  78. [__MAX_BPF_ATTACH_TYPE] = NULL,
  79. };
  80. enum bpf_attach_type parse_attach_type(const char *str)
  81. {
  82. enum bpf_attach_type type;
  83. for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
  84. if (attach_type_strings[type] &&
  85. is_prefix(str, attach_type_strings[type]))
  86. return type;
  87. }
  88. return __MAX_BPF_ATTACH_TYPE;
  89. }
  90. static void print_boot_time(__u64 nsecs, char *buf, unsigned int size)
  91. {
  92. struct timespec real_time_ts, boot_time_ts;
  93. time_t wallclock_secs;
  94. struct tm load_tm;
  95. buf[--size] = '\0';
  96. if (clock_gettime(CLOCK_REALTIME, &real_time_ts) ||
  97. clock_gettime(CLOCK_BOOTTIME, &boot_time_ts)) {
  98. perror("Can't read clocks");
  99. snprintf(buf, size, "%llu", nsecs / 1000000000);
  100. return;
  101. }
  102. wallclock_secs = (real_time_ts.tv_sec - boot_time_ts.tv_sec) +
  103. (real_time_ts.tv_nsec - boot_time_ts.tv_nsec + nsecs) /
  104. 1000000000;
  105. if (!localtime_r(&wallclock_secs, &load_tm)) {
  106. snprintf(buf, size, "%llu", nsecs / 1000000000);
  107. return;
  108. }
  109. if (json_output)
  110. strftime(buf, size, "%s", &load_tm);
  111. else
  112. strftime(buf, size, "%FT%T%z", &load_tm);
  113. }
  114. static int prog_fd_by_tag(unsigned char *tag)
  115. {
  116. struct bpf_prog_info info = {};
  117. __u32 len = sizeof(info);
  118. unsigned int id = 0;
  119. int err;
  120. int fd;
  121. while (true) {
  122. err = bpf_prog_get_next_id(id, &id);
  123. if (err) {
  124. p_err("%s", strerror(errno));
  125. return -1;
  126. }
  127. fd = bpf_prog_get_fd_by_id(id);
  128. if (fd < 0) {
  129. p_err("can't get prog by id (%u): %s",
  130. id, strerror(errno));
  131. return -1;
  132. }
  133. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  134. if (err) {
  135. p_err("can't get prog info (%u): %s",
  136. id, strerror(errno));
  137. close(fd);
  138. return -1;
  139. }
  140. if (!memcmp(tag, info.tag, BPF_TAG_SIZE))
  141. return fd;
  142. close(fd);
  143. }
  144. }
  145. int prog_parse_fd(int *argc, char ***argv)
  146. {
  147. int fd;
  148. if (is_prefix(**argv, "id")) {
  149. unsigned int id;
  150. char *endptr;
  151. NEXT_ARGP();
  152. id = strtoul(**argv, &endptr, 0);
  153. if (*endptr) {
  154. p_err("can't parse %s as ID", **argv);
  155. return -1;
  156. }
  157. NEXT_ARGP();
  158. fd = bpf_prog_get_fd_by_id(id);
  159. if (fd < 0)
  160. p_err("get by id (%u): %s", id, strerror(errno));
  161. return fd;
  162. } else if (is_prefix(**argv, "tag")) {
  163. unsigned char tag[BPF_TAG_SIZE];
  164. NEXT_ARGP();
  165. if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
  166. tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
  167. != BPF_TAG_SIZE) {
  168. p_err("can't parse tag");
  169. return -1;
  170. }
  171. NEXT_ARGP();
  172. return prog_fd_by_tag(tag);
  173. } else if (is_prefix(**argv, "pinned")) {
  174. char *path;
  175. NEXT_ARGP();
  176. path = **argv;
  177. NEXT_ARGP();
  178. return open_obj_pinned_any(path, BPF_OBJ_PROG);
  179. }
  180. p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv);
  181. return -1;
  182. }
  183. static void show_prog_maps(int fd, u32 num_maps)
  184. {
  185. struct bpf_prog_info info = {};
  186. __u32 len = sizeof(info);
  187. __u32 map_ids[num_maps];
  188. unsigned int i;
  189. int err;
  190. info.nr_map_ids = num_maps;
  191. info.map_ids = ptr_to_u64(map_ids);
  192. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  193. if (err || !info.nr_map_ids)
  194. return;
  195. if (json_output) {
  196. jsonw_name(json_wtr, "map_ids");
  197. jsonw_start_array(json_wtr);
  198. for (i = 0; i < info.nr_map_ids; i++)
  199. jsonw_uint(json_wtr, map_ids[i]);
  200. jsonw_end_array(json_wtr);
  201. } else {
  202. printf(" map_ids ");
  203. for (i = 0; i < info.nr_map_ids; i++)
  204. printf("%u%s", map_ids[i],
  205. i == info.nr_map_ids - 1 ? "" : ",");
  206. }
  207. }
  208. static void print_prog_json(struct bpf_prog_info *info, int fd)
  209. {
  210. char *memlock;
  211. jsonw_start_object(json_wtr);
  212. jsonw_uint_field(json_wtr, "id", info->id);
  213. if (info->type < ARRAY_SIZE(prog_type_name))
  214. jsonw_string_field(json_wtr, "type",
  215. prog_type_name[info->type]);
  216. else
  217. jsonw_uint_field(json_wtr, "type", info->type);
  218. if (*info->name)
  219. jsonw_string_field(json_wtr, "name", info->name);
  220. jsonw_name(json_wtr, "tag");
  221. jsonw_printf(json_wtr, "\"" BPF_TAG_FMT "\"",
  222. info->tag[0], info->tag[1], info->tag[2], info->tag[3],
  223. info->tag[4], info->tag[5], info->tag[6], info->tag[7]);
  224. jsonw_bool_field(json_wtr, "gpl_compatible", info->gpl_compatible);
  225. print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
  226. if (info->load_time) {
  227. char buf[32];
  228. print_boot_time(info->load_time, buf, sizeof(buf));
  229. /* Piggy back on load_time, since 0 uid is a valid one */
  230. jsonw_name(json_wtr, "loaded_at");
  231. jsonw_printf(json_wtr, "%s", buf);
  232. jsonw_uint_field(json_wtr, "uid", info->created_by_uid);
  233. }
  234. jsonw_uint_field(json_wtr, "bytes_xlated", info->xlated_prog_len);
  235. if (info->jited_prog_len) {
  236. jsonw_bool_field(json_wtr, "jited", true);
  237. jsonw_uint_field(json_wtr, "bytes_jited", info->jited_prog_len);
  238. } else {
  239. jsonw_bool_field(json_wtr, "jited", false);
  240. }
  241. memlock = get_fdinfo(fd, "memlock");
  242. if (memlock)
  243. jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
  244. free(memlock);
  245. if (info->nr_map_ids)
  246. show_prog_maps(fd, info->nr_map_ids);
  247. if (!hash_empty(prog_table.table)) {
  248. struct pinned_obj *obj;
  249. jsonw_name(json_wtr, "pinned");
  250. jsonw_start_array(json_wtr);
  251. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  252. if (obj->id == info->id)
  253. jsonw_string(json_wtr, obj->path);
  254. }
  255. jsonw_end_array(json_wtr);
  256. }
  257. jsonw_end_object(json_wtr);
  258. }
  259. static void print_prog_plain(struct bpf_prog_info *info, int fd)
  260. {
  261. char *memlock;
  262. printf("%u: ", info->id);
  263. if (info->type < ARRAY_SIZE(prog_type_name))
  264. printf("%s ", prog_type_name[info->type]);
  265. else
  266. printf("type %u ", info->type);
  267. if (*info->name)
  268. printf("name %s ", info->name);
  269. printf("tag ");
  270. fprint_hex(stdout, info->tag, BPF_TAG_SIZE, "");
  271. print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
  272. printf("%s", info->gpl_compatible ? " gpl" : "");
  273. printf("\n");
  274. if (info->load_time) {
  275. char buf[32];
  276. print_boot_time(info->load_time, buf, sizeof(buf));
  277. /* Piggy back on load_time, since 0 uid is a valid one */
  278. printf("\tloaded_at %s uid %u\n", buf, info->created_by_uid);
  279. }
  280. printf("\txlated %uB", info->xlated_prog_len);
  281. if (info->jited_prog_len)
  282. printf(" jited %uB", info->jited_prog_len);
  283. else
  284. printf(" not jited");
  285. memlock = get_fdinfo(fd, "memlock");
  286. if (memlock)
  287. printf(" memlock %sB", memlock);
  288. free(memlock);
  289. if (info->nr_map_ids)
  290. show_prog_maps(fd, info->nr_map_ids);
  291. if (!hash_empty(prog_table.table)) {
  292. struct pinned_obj *obj;
  293. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  294. if (obj->id == info->id)
  295. printf("\n\tpinned %s", obj->path);
  296. }
  297. }
  298. printf("\n");
  299. }
  300. static int show_prog(int fd)
  301. {
  302. struct bpf_prog_info info = {};
  303. __u32 len = sizeof(info);
  304. int err;
  305. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  306. if (err) {
  307. p_err("can't get prog info: %s", strerror(errno));
  308. return -1;
  309. }
  310. if (json_output)
  311. print_prog_json(&info, fd);
  312. else
  313. print_prog_plain(&info, fd);
  314. return 0;
  315. }
  316. static int do_show(int argc, char **argv)
  317. {
  318. __u32 id = 0;
  319. int err;
  320. int fd;
  321. if (show_pinned)
  322. build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
  323. if (argc == 2) {
  324. fd = prog_parse_fd(&argc, &argv);
  325. if (fd < 0)
  326. return -1;
  327. return show_prog(fd);
  328. }
  329. if (argc)
  330. return BAD_ARG();
  331. if (json_output)
  332. jsonw_start_array(json_wtr);
  333. while (true) {
  334. err = bpf_prog_get_next_id(id, &id);
  335. if (err) {
  336. if (errno == ENOENT) {
  337. err = 0;
  338. break;
  339. }
  340. p_err("can't get next program: %s%s", strerror(errno),
  341. errno == EINVAL ? " -- kernel too old?" : "");
  342. err = -1;
  343. break;
  344. }
  345. fd = bpf_prog_get_fd_by_id(id);
  346. if (fd < 0) {
  347. if (errno == ENOENT)
  348. continue;
  349. p_err("can't get prog by id (%u): %s",
  350. id, strerror(errno));
  351. err = -1;
  352. break;
  353. }
  354. err = show_prog(fd);
  355. close(fd);
  356. if (err)
  357. break;
  358. }
  359. if (json_output)
  360. jsonw_end_array(json_wtr);
  361. return err;
  362. }
  363. static int do_dump(int argc, char **argv)
  364. {
  365. unsigned long *func_ksyms = NULL;
  366. struct bpf_prog_info info = {};
  367. unsigned int *func_lens = NULL;
  368. const char *disasm_opt = NULL;
  369. unsigned int nr_func_ksyms;
  370. unsigned int nr_func_lens;
  371. struct dump_data dd = {};
  372. __u32 len = sizeof(info);
  373. unsigned int buf_size;
  374. char *filepath = NULL;
  375. bool opcodes = false;
  376. bool visual = false;
  377. unsigned char *buf;
  378. __u32 *member_len;
  379. __u64 *member_ptr;
  380. ssize_t n;
  381. int err;
  382. int fd;
  383. if (is_prefix(*argv, "jited")) {
  384. member_len = &info.jited_prog_len;
  385. member_ptr = &info.jited_prog_insns;
  386. } else if (is_prefix(*argv, "xlated")) {
  387. member_len = &info.xlated_prog_len;
  388. member_ptr = &info.xlated_prog_insns;
  389. } else {
  390. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  391. return -1;
  392. }
  393. NEXT_ARG();
  394. if (argc < 2)
  395. usage();
  396. fd = prog_parse_fd(&argc, &argv);
  397. if (fd < 0)
  398. return -1;
  399. if (is_prefix(*argv, "file")) {
  400. NEXT_ARG();
  401. if (!argc) {
  402. p_err("expected file path");
  403. return -1;
  404. }
  405. filepath = *argv;
  406. NEXT_ARG();
  407. } else if (is_prefix(*argv, "opcodes")) {
  408. opcodes = true;
  409. NEXT_ARG();
  410. } else if (is_prefix(*argv, "visual")) {
  411. visual = true;
  412. NEXT_ARG();
  413. }
  414. if (argc) {
  415. usage();
  416. return -1;
  417. }
  418. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  419. if (err) {
  420. p_err("can't get prog info: %s", strerror(errno));
  421. return -1;
  422. }
  423. if (!*member_len) {
  424. p_info("no instructions returned");
  425. close(fd);
  426. return 0;
  427. }
  428. buf_size = *member_len;
  429. buf = malloc(buf_size);
  430. if (!buf) {
  431. p_err("mem alloc failed");
  432. close(fd);
  433. return -1;
  434. }
  435. nr_func_ksyms = info.nr_jited_ksyms;
  436. if (nr_func_ksyms) {
  437. func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
  438. if (!func_ksyms) {
  439. p_err("mem alloc failed");
  440. close(fd);
  441. goto err_free;
  442. }
  443. }
  444. nr_func_lens = info.nr_jited_func_lens;
  445. if (nr_func_lens) {
  446. func_lens = malloc(nr_func_lens * sizeof(__u32));
  447. if (!func_lens) {
  448. p_err("mem alloc failed");
  449. close(fd);
  450. goto err_free;
  451. }
  452. }
  453. memset(&info, 0, sizeof(info));
  454. *member_ptr = ptr_to_u64(buf);
  455. *member_len = buf_size;
  456. info.jited_ksyms = ptr_to_u64(func_ksyms);
  457. info.nr_jited_ksyms = nr_func_ksyms;
  458. info.jited_func_lens = ptr_to_u64(func_lens);
  459. info.nr_jited_func_lens = nr_func_lens;
  460. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  461. close(fd);
  462. if (err) {
  463. p_err("can't get prog info: %s", strerror(errno));
  464. goto err_free;
  465. }
  466. if (*member_len > buf_size) {
  467. p_err("too many instructions returned");
  468. goto err_free;
  469. }
  470. if (info.nr_jited_ksyms > nr_func_ksyms) {
  471. p_err("too many addresses returned");
  472. goto err_free;
  473. }
  474. if (info.nr_jited_func_lens > nr_func_lens) {
  475. p_err("too many values returned");
  476. goto err_free;
  477. }
  478. if ((member_len == &info.jited_prog_len &&
  479. info.jited_prog_insns == 0) ||
  480. (member_len == &info.xlated_prog_len &&
  481. info.xlated_prog_insns == 0)) {
  482. p_err("error retrieving insn dump: kernel.kptr_restrict set?");
  483. goto err_free;
  484. }
  485. if (filepath) {
  486. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  487. if (fd < 0) {
  488. p_err("can't open file %s: %s", filepath,
  489. strerror(errno));
  490. goto err_free;
  491. }
  492. n = write(fd, buf, *member_len);
  493. close(fd);
  494. if (n != *member_len) {
  495. p_err("error writing output file: %s",
  496. n < 0 ? strerror(errno) : "short write");
  497. goto err_free;
  498. }
  499. if (json_output)
  500. jsonw_null(json_wtr);
  501. } else if (member_len == &info.jited_prog_len) {
  502. const char *name = NULL;
  503. if (info.ifindex) {
  504. name = ifindex_to_bfd_params(info.ifindex,
  505. info.netns_dev,
  506. info.netns_ino,
  507. &disasm_opt);
  508. if (!name)
  509. goto err_free;
  510. }
  511. if (info.nr_jited_func_lens && info.jited_func_lens) {
  512. struct kernel_sym *sym = NULL;
  513. char sym_name[SYM_MAX_NAME];
  514. unsigned char *img = buf;
  515. __u64 *ksyms = NULL;
  516. __u32 *lens;
  517. __u32 i;
  518. if (info.nr_jited_ksyms) {
  519. kernel_syms_load(&dd);
  520. ksyms = (__u64 *) info.jited_ksyms;
  521. }
  522. if (json_output)
  523. jsonw_start_array(json_wtr);
  524. lens = (__u32 *) info.jited_func_lens;
  525. for (i = 0; i < info.nr_jited_func_lens; i++) {
  526. if (ksyms) {
  527. sym = kernel_syms_search(&dd, ksyms[i]);
  528. if (sym)
  529. sprintf(sym_name, "%s", sym->name);
  530. else
  531. sprintf(sym_name, "0x%016llx", ksyms[i]);
  532. } else {
  533. strcpy(sym_name, "unknown");
  534. }
  535. if (json_output) {
  536. jsonw_start_object(json_wtr);
  537. jsonw_name(json_wtr, "name");
  538. jsonw_string(json_wtr, sym_name);
  539. jsonw_name(json_wtr, "insns");
  540. } else {
  541. printf("%s:\n", sym_name);
  542. }
  543. disasm_print_insn(img, lens[i], opcodes, name,
  544. disasm_opt);
  545. img += lens[i];
  546. if (json_output)
  547. jsonw_end_object(json_wtr);
  548. else
  549. printf("\n");
  550. }
  551. if (json_output)
  552. jsonw_end_array(json_wtr);
  553. } else {
  554. disasm_print_insn(buf, *member_len, opcodes, name,
  555. disasm_opt);
  556. }
  557. } else if (visual) {
  558. if (json_output)
  559. jsonw_null(json_wtr);
  560. else
  561. dump_xlated_cfg(buf, *member_len);
  562. } else {
  563. kernel_syms_load(&dd);
  564. dd.nr_jited_ksyms = info.nr_jited_ksyms;
  565. dd.jited_ksyms = (__u64 *) info.jited_ksyms;
  566. if (json_output)
  567. dump_xlated_json(&dd, buf, *member_len, opcodes);
  568. else
  569. dump_xlated_plain(&dd, buf, *member_len, opcodes);
  570. kernel_syms_destroy(&dd);
  571. }
  572. free(buf);
  573. free(func_ksyms);
  574. free(func_lens);
  575. return 0;
  576. err_free:
  577. free(buf);
  578. free(func_ksyms);
  579. free(func_lens);
  580. return -1;
  581. }
  582. static int do_pin(int argc, char **argv)
  583. {
  584. int err;
  585. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  586. if (!err && json_output)
  587. jsonw_null(json_wtr);
  588. return err;
  589. }
  590. struct map_replace {
  591. int idx;
  592. int fd;
  593. char *name;
  594. };
  595. int map_replace_compar(const void *p1, const void *p2)
  596. {
  597. const struct map_replace *a = p1, *b = p2;
  598. return a->idx - b->idx;
  599. }
  600. static int do_attach(int argc, char **argv)
  601. {
  602. enum bpf_attach_type attach_type;
  603. int err, mapfd, progfd;
  604. if (!REQ_ARGS(5)) {
  605. p_err("too few parameters for map attach");
  606. return -EINVAL;
  607. }
  608. progfd = prog_parse_fd(&argc, &argv);
  609. if (progfd < 0)
  610. return progfd;
  611. attach_type = parse_attach_type(*argv);
  612. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  613. p_err("invalid attach type");
  614. return -EINVAL;
  615. }
  616. NEXT_ARG();
  617. mapfd = map_parse_fd(&argc, &argv);
  618. if (mapfd < 0)
  619. return mapfd;
  620. err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
  621. if (err) {
  622. p_err("failed prog attach to map");
  623. return -EINVAL;
  624. }
  625. if (json_output)
  626. jsonw_null(json_wtr);
  627. return 0;
  628. }
  629. static int do_detach(int argc, char **argv)
  630. {
  631. enum bpf_attach_type attach_type;
  632. int err, mapfd, progfd;
  633. if (!REQ_ARGS(5)) {
  634. p_err("too few parameters for map detach");
  635. return -EINVAL;
  636. }
  637. progfd = prog_parse_fd(&argc, &argv);
  638. if (progfd < 0)
  639. return progfd;
  640. attach_type = parse_attach_type(*argv);
  641. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  642. p_err("invalid attach type");
  643. return -EINVAL;
  644. }
  645. NEXT_ARG();
  646. mapfd = map_parse_fd(&argc, &argv);
  647. if (mapfd < 0)
  648. return mapfd;
  649. err = bpf_prog_detach2(progfd, mapfd, attach_type);
  650. if (err) {
  651. p_err("failed prog detach from map");
  652. return -EINVAL;
  653. }
  654. if (json_output)
  655. jsonw_null(json_wtr);
  656. return 0;
  657. }
  658. static int do_load(int argc, char **argv)
  659. {
  660. enum bpf_attach_type expected_attach_type;
  661. struct bpf_object_open_attr attr = {
  662. .prog_type = BPF_PROG_TYPE_UNSPEC,
  663. };
  664. struct map_replace *map_replace = NULL;
  665. unsigned int old_map_fds = 0;
  666. struct bpf_program *prog;
  667. struct bpf_object *obj;
  668. struct bpf_map *map;
  669. const char *pinfile;
  670. unsigned int i, j;
  671. __u32 ifindex = 0;
  672. int idx, err;
  673. if (!REQ_ARGS(2))
  674. return -1;
  675. attr.file = GET_ARG();
  676. pinfile = GET_ARG();
  677. while (argc) {
  678. if (is_prefix(*argv, "type")) {
  679. char *type;
  680. NEXT_ARG();
  681. if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
  682. p_err("program type already specified");
  683. goto err_free_reuse_maps;
  684. }
  685. if (!REQ_ARGS(1))
  686. goto err_free_reuse_maps;
  687. /* Put a '/' at the end of type to appease libbpf */
  688. type = malloc(strlen(*argv) + 2);
  689. if (!type) {
  690. p_err("mem alloc failed");
  691. goto err_free_reuse_maps;
  692. }
  693. *type = 0;
  694. strcat(type, *argv);
  695. strcat(type, "/");
  696. err = libbpf_prog_type_by_name(type, &attr.prog_type,
  697. &expected_attach_type);
  698. free(type);
  699. if (err < 0) {
  700. p_err("unknown program type '%s'", *argv);
  701. goto err_free_reuse_maps;
  702. }
  703. NEXT_ARG();
  704. } else if (is_prefix(*argv, "map")) {
  705. char *endptr, *name;
  706. int fd;
  707. NEXT_ARG();
  708. if (!REQ_ARGS(4))
  709. goto err_free_reuse_maps;
  710. if (is_prefix(*argv, "idx")) {
  711. NEXT_ARG();
  712. idx = strtoul(*argv, &endptr, 0);
  713. if (*endptr) {
  714. p_err("can't parse %s as IDX", *argv);
  715. goto err_free_reuse_maps;
  716. }
  717. name = NULL;
  718. } else if (is_prefix(*argv, "name")) {
  719. NEXT_ARG();
  720. name = *argv;
  721. idx = -1;
  722. } else {
  723. p_err("expected 'idx' or 'name', got: '%s'?",
  724. *argv);
  725. goto err_free_reuse_maps;
  726. }
  727. NEXT_ARG();
  728. fd = map_parse_fd(&argc, &argv);
  729. if (fd < 0)
  730. goto err_free_reuse_maps;
  731. map_replace = reallocarray(map_replace, old_map_fds + 1,
  732. sizeof(*map_replace));
  733. if (!map_replace) {
  734. p_err("mem alloc failed");
  735. goto err_free_reuse_maps;
  736. }
  737. map_replace[old_map_fds].idx = idx;
  738. map_replace[old_map_fds].name = name;
  739. map_replace[old_map_fds].fd = fd;
  740. old_map_fds++;
  741. } else if (is_prefix(*argv, "dev")) {
  742. NEXT_ARG();
  743. if (ifindex) {
  744. p_err("offload device already specified");
  745. goto err_free_reuse_maps;
  746. }
  747. if (!REQ_ARGS(1))
  748. goto err_free_reuse_maps;
  749. ifindex = if_nametoindex(*argv);
  750. if (!ifindex) {
  751. p_err("unrecognized netdevice '%s': %s",
  752. *argv, strerror(errno));
  753. goto err_free_reuse_maps;
  754. }
  755. NEXT_ARG();
  756. } else {
  757. p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
  758. *argv);
  759. goto err_free_reuse_maps;
  760. }
  761. }
  762. obj = __bpf_object__open_xattr(&attr, bpf_flags);
  763. if (IS_ERR_OR_NULL(obj)) {
  764. p_err("failed to open object file");
  765. goto err_free_reuse_maps;
  766. }
  767. prog = bpf_program__next(NULL, obj);
  768. if (!prog) {
  769. p_err("object file doesn't contain any bpf program");
  770. goto err_close_obj;
  771. }
  772. bpf_program__set_ifindex(prog, ifindex);
  773. if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
  774. const char *sec_name = bpf_program__title(prog, false);
  775. err = libbpf_prog_type_by_name(sec_name, &attr.prog_type,
  776. &expected_attach_type);
  777. if (err < 0) {
  778. p_err("failed to guess program type based on section name %s\n",
  779. sec_name);
  780. goto err_close_obj;
  781. }
  782. }
  783. bpf_program__set_type(prog, attr.prog_type);
  784. bpf_program__set_expected_attach_type(prog, expected_attach_type);
  785. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  786. map_replace_compar);
  787. /* After the sort maps by name will be first on the list, because they
  788. * have idx == -1. Resolve them.
  789. */
  790. j = 0;
  791. while (j < old_map_fds && map_replace[j].name) {
  792. i = 0;
  793. bpf_map__for_each(map, obj) {
  794. if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
  795. map_replace[j].idx = i;
  796. break;
  797. }
  798. i++;
  799. }
  800. if (map_replace[j].idx == -1) {
  801. p_err("unable to find map '%s'", map_replace[j].name);
  802. goto err_close_obj;
  803. }
  804. j++;
  805. }
  806. /* Resort if any names were resolved */
  807. if (j)
  808. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  809. map_replace_compar);
  810. /* Set ifindex and name reuse */
  811. j = 0;
  812. idx = 0;
  813. bpf_map__for_each(map, obj) {
  814. if (!bpf_map__is_offload_neutral(map))
  815. bpf_map__set_ifindex(map, ifindex);
  816. if (j < old_map_fds && idx == map_replace[j].idx) {
  817. err = bpf_map__reuse_fd(map, map_replace[j++].fd);
  818. if (err) {
  819. p_err("unable to set up map reuse: %d", err);
  820. goto err_close_obj;
  821. }
  822. /* Next reuse wants to apply to the same map */
  823. if (j < old_map_fds && map_replace[j].idx == idx) {
  824. p_err("replacement for map idx %d specified more than once",
  825. idx);
  826. goto err_close_obj;
  827. }
  828. }
  829. idx++;
  830. }
  831. if (j < old_map_fds) {
  832. p_err("map idx '%d' not used", map_replace[j].idx);
  833. goto err_close_obj;
  834. }
  835. err = bpf_object__load(obj);
  836. if (err) {
  837. p_err("failed to load object file");
  838. goto err_close_obj;
  839. }
  840. if (do_pin_fd(bpf_program__fd(prog), pinfile))
  841. goto err_close_obj;
  842. if (json_output)
  843. jsonw_null(json_wtr);
  844. bpf_object__close(obj);
  845. for (i = 0; i < old_map_fds; i++)
  846. close(map_replace[i].fd);
  847. free(map_replace);
  848. return 0;
  849. err_close_obj:
  850. bpf_object__close(obj);
  851. err_free_reuse_maps:
  852. for (i = 0; i < old_map_fds; i++)
  853. close(map_replace[i].fd);
  854. free(map_replace);
  855. return -1;
  856. }
  857. static int do_help(int argc, char **argv)
  858. {
  859. if (json_output) {
  860. jsonw_null(json_wtr);
  861. return 0;
  862. }
  863. fprintf(stderr,
  864. "Usage: %s %s { show | list } [PROG]\n"
  865. " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
  866. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  867. " %s %s pin PROG FILE\n"
  868. " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n"
  869. " [map { idx IDX | name NAME } MAP]\n"
  870. " %s %s attach PROG ATTACH_TYPE MAP\n"
  871. " %s %s detach PROG ATTACH_TYPE MAP\n"
  872. " %s %s help\n"
  873. "\n"
  874. " " HELP_SPEC_MAP "\n"
  875. " " HELP_SPEC_PROGRAM "\n"
  876. " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
  877. " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
  878. " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
  879. " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
  880. " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
  881. " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
  882. " cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
  883. " ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n"
  884. " " HELP_SPEC_OPTIONS "\n"
  885. "",
  886. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  887. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  888. bin_name, argv[-2], bin_name, argv[-2]);
  889. return 0;
  890. }
  891. static const struct cmd cmds[] = {
  892. { "show", do_show },
  893. { "list", do_show },
  894. { "help", do_help },
  895. { "dump", do_dump },
  896. { "pin", do_pin },
  897. { "load", do_load },
  898. { "attach", do_attach },
  899. { "detach", do_detach },
  900. { 0 }
  901. };
  902. int do_prog(int argc, char **argv)
  903. {
  904. return cmd_select(cmds, argc, argv, do_help);
  905. }