prog.c 22 KB

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