prog.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. printf("\n");
  294. hash_for_each_possible(prog_table.table, obj, hash, info->id) {
  295. if (obj->id == info->id)
  296. printf("\tpinned %s\n", obj->path);
  297. }
  298. }
  299. printf("\n");
  300. }
  301. static int show_prog(int fd)
  302. {
  303. struct bpf_prog_info info = {};
  304. __u32 len = sizeof(info);
  305. int err;
  306. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  307. if (err) {
  308. p_err("can't get prog info: %s", strerror(errno));
  309. return -1;
  310. }
  311. if (json_output)
  312. print_prog_json(&info, fd);
  313. else
  314. print_prog_plain(&info, fd);
  315. return 0;
  316. }
  317. static int do_show(int argc, char **argv)
  318. {
  319. __u32 id = 0;
  320. int err;
  321. int fd;
  322. if (show_pinned)
  323. build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
  324. if (argc == 2) {
  325. fd = prog_parse_fd(&argc, &argv);
  326. if (fd < 0)
  327. return -1;
  328. return show_prog(fd);
  329. }
  330. if (argc)
  331. return BAD_ARG();
  332. if (json_output)
  333. jsonw_start_array(json_wtr);
  334. while (true) {
  335. err = bpf_prog_get_next_id(id, &id);
  336. if (err) {
  337. if (errno == ENOENT) {
  338. err = 0;
  339. break;
  340. }
  341. p_err("can't get next program: %s%s", strerror(errno),
  342. errno == EINVAL ? " -- kernel too old?" : "");
  343. err = -1;
  344. break;
  345. }
  346. fd = bpf_prog_get_fd_by_id(id);
  347. if (fd < 0) {
  348. if (errno == ENOENT)
  349. continue;
  350. p_err("can't get prog by id (%u): %s",
  351. id, strerror(errno));
  352. err = -1;
  353. break;
  354. }
  355. err = show_prog(fd);
  356. close(fd);
  357. if (err)
  358. break;
  359. }
  360. if (json_output)
  361. jsonw_end_array(json_wtr);
  362. return err;
  363. }
  364. static int do_dump(int argc, char **argv)
  365. {
  366. unsigned long *func_ksyms = NULL;
  367. struct bpf_prog_info info = {};
  368. unsigned int *func_lens = NULL;
  369. const char *disasm_opt = NULL;
  370. unsigned int nr_func_ksyms;
  371. unsigned int nr_func_lens;
  372. struct dump_data dd = {};
  373. __u32 len = sizeof(info);
  374. unsigned int buf_size;
  375. char *filepath = NULL;
  376. bool opcodes = false;
  377. bool visual = false;
  378. unsigned char *buf;
  379. __u32 *member_len;
  380. __u64 *member_ptr;
  381. ssize_t n;
  382. int err;
  383. int fd;
  384. if (is_prefix(*argv, "jited")) {
  385. member_len = &info.jited_prog_len;
  386. member_ptr = &info.jited_prog_insns;
  387. } else if (is_prefix(*argv, "xlated")) {
  388. member_len = &info.xlated_prog_len;
  389. member_ptr = &info.xlated_prog_insns;
  390. } else {
  391. p_err("expected 'xlated' or 'jited', got: %s", *argv);
  392. return -1;
  393. }
  394. NEXT_ARG();
  395. if (argc < 2)
  396. usage();
  397. fd = prog_parse_fd(&argc, &argv);
  398. if (fd < 0)
  399. return -1;
  400. if (is_prefix(*argv, "file")) {
  401. NEXT_ARG();
  402. if (!argc) {
  403. p_err("expected file path");
  404. return -1;
  405. }
  406. filepath = *argv;
  407. NEXT_ARG();
  408. } else if (is_prefix(*argv, "opcodes")) {
  409. opcodes = true;
  410. NEXT_ARG();
  411. } else if (is_prefix(*argv, "visual")) {
  412. visual = true;
  413. NEXT_ARG();
  414. }
  415. if (argc) {
  416. usage();
  417. return -1;
  418. }
  419. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  420. if (err) {
  421. p_err("can't get prog info: %s", strerror(errno));
  422. return -1;
  423. }
  424. if (!*member_len) {
  425. p_info("no instructions returned");
  426. close(fd);
  427. return 0;
  428. }
  429. buf_size = *member_len;
  430. buf = malloc(buf_size);
  431. if (!buf) {
  432. p_err("mem alloc failed");
  433. close(fd);
  434. return -1;
  435. }
  436. nr_func_ksyms = info.nr_jited_ksyms;
  437. if (nr_func_ksyms) {
  438. func_ksyms = malloc(nr_func_ksyms * sizeof(__u64));
  439. if (!func_ksyms) {
  440. p_err("mem alloc failed");
  441. close(fd);
  442. goto err_free;
  443. }
  444. }
  445. nr_func_lens = info.nr_jited_func_lens;
  446. if (nr_func_lens) {
  447. func_lens = malloc(nr_func_lens * sizeof(__u32));
  448. if (!func_lens) {
  449. p_err("mem alloc failed");
  450. close(fd);
  451. goto err_free;
  452. }
  453. }
  454. memset(&info, 0, sizeof(info));
  455. *member_ptr = ptr_to_u64(buf);
  456. *member_len = buf_size;
  457. info.jited_ksyms = ptr_to_u64(func_ksyms);
  458. info.nr_jited_ksyms = nr_func_ksyms;
  459. info.jited_func_lens = ptr_to_u64(func_lens);
  460. info.nr_jited_func_lens = nr_func_lens;
  461. err = bpf_obj_get_info_by_fd(fd, &info, &len);
  462. close(fd);
  463. if (err) {
  464. p_err("can't get prog info: %s", strerror(errno));
  465. goto err_free;
  466. }
  467. if (*member_len > buf_size) {
  468. p_err("too many instructions returned");
  469. goto err_free;
  470. }
  471. if (info.nr_jited_ksyms > nr_func_ksyms) {
  472. p_err("too many addresses returned");
  473. goto err_free;
  474. }
  475. if (info.nr_jited_func_lens > nr_func_lens) {
  476. p_err("too many values returned");
  477. goto err_free;
  478. }
  479. if ((member_len == &info.jited_prog_len &&
  480. info.jited_prog_insns == 0) ||
  481. (member_len == &info.xlated_prog_len &&
  482. info.xlated_prog_insns == 0)) {
  483. p_err("error retrieving insn dump: kernel.kptr_restrict set?");
  484. goto err_free;
  485. }
  486. if (filepath) {
  487. fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  488. if (fd < 0) {
  489. p_err("can't open file %s: %s", filepath,
  490. strerror(errno));
  491. goto err_free;
  492. }
  493. n = write(fd, buf, *member_len);
  494. close(fd);
  495. if (n != *member_len) {
  496. p_err("error writing output file: %s",
  497. n < 0 ? strerror(errno) : "short write");
  498. goto err_free;
  499. }
  500. if (json_output)
  501. jsonw_null(json_wtr);
  502. } else if (member_len == &info.jited_prog_len) {
  503. const char *name = NULL;
  504. if (info.ifindex) {
  505. name = ifindex_to_bfd_params(info.ifindex,
  506. info.netns_dev,
  507. info.netns_ino,
  508. &disasm_opt);
  509. if (!name)
  510. goto err_free;
  511. }
  512. if (info.nr_jited_func_lens && info.jited_func_lens) {
  513. struct kernel_sym *sym = NULL;
  514. char sym_name[SYM_MAX_NAME];
  515. unsigned char *img = buf;
  516. __u64 *ksyms = NULL;
  517. __u32 *lens;
  518. __u32 i;
  519. if (info.nr_jited_ksyms) {
  520. kernel_syms_load(&dd);
  521. ksyms = (__u64 *) info.jited_ksyms;
  522. }
  523. if (json_output)
  524. jsonw_start_array(json_wtr);
  525. lens = (__u32 *) info.jited_func_lens;
  526. for (i = 0; i < info.nr_jited_func_lens; i++) {
  527. if (ksyms) {
  528. sym = kernel_syms_search(&dd, ksyms[i]);
  529. if (sym)
  530. sprintf(sym_name, "%s", sym->name);
  531. else
  532. sprintf(sym_name, "0x%016llx", ksyms[i]);
  533. } else {
  534. strcpy(sym_name, "unknown");
  535. }
  536. if (json_output) {
  537. jsonw_start_object(json_wtr);
  538. jsonw_name(json_wtr, "name");
  539. jsonw_string(json_wtr, sym_name);
  540. jsonw_name(json_wtr, "insns");
  541. } else {
  542. printf("%s:\n", sym_name);
  543. }
  544. disasm_print_insn(img, lens[i], opcodes, name,
  545. disasm_opt);
  546. img += lens[i];
  547. if (json_output)
  548. jsonw_end_object(json_wtr);
  549. else
  550. printf("\n");
  551. }
  552. if (json_output)
  553. jsonw_end_array(json_wtr);
  554. } else {
  555. disasm_print_insn(buf, *member_len, opcodes, name,
  556. disasm_opt);
  557. }
  558. } else if (visual) {
  559. if (json_output)
  560. jsonw_null(json_wtr);
  561. else
  562. dump_xlated_cfg(buf, *member_len);
  563. } else {
  564. kernel_syms_load(&dd);
  565. dd.nr_jited_ksyms = info.nr_jited_ksyms;
  566. dd.jited_ksyms = (__u64 *) info.jited_ksyms;
  567. if (json_output)
  568. dump_xlated_json(&dd, buf, *member_len, opcodes);
  569. else
  570. dump_xlated_plain(&dd, buf, *member_len, opcodes);
  571. kernel_syms_destroy(&dd);
  572. }
  573. free(buf);
  574. free(func_ksyms);
  575. free(func_lens);
  576. return 0;
  577. err_free:
  578. free(buf);
  579. free(func_ksyms);
  580. free(func_lens);
  581. return -1;
  582. }
  583. static int do_pin(int argc, char **argv)
  584. {
  585. int err;
  586. err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
  587. if (!err && json_output)
  588. jsonw_null(json_wtr);
  589. return err;
  590. }
  591. struct map_replace {
  592. int idx;
  593. int fd;
  594. char *name;
  595. };
  596. int map_replace_compar(const void *p1, const void *p2)
  597. {
  598. const struct map_replace *a = p1, *b = p2;
  599. return a->idx - b->idx;
  600. }
  601. static int do_attach(int argc, char **argv)
  602. {
  603. enum bpf_attach_type attach_type;
  604. int err, mapfd, progfd;
  605. if (!REQ_ARGS(5)) {
  606. p_err("too few parameters for map attach");
  607. return -EINVAL;
  608. }
  609. progfd = prog_parse_fd(&argc, &argv);
  610. if (progfd < 0)
  611. return progfd;
  612. attach_type = parse_attach_type(*argv);
  613. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  614. p_err("invalid attach type");
  615. return -EINVAL;
  616. }
  617. NEXT_ARG();
  618. mapfd = map_parse_fd(&argc, &argv);
  619. if (mapfd < 0)
  620. return mapfd;
  621. err = bpf_prog_attach(progfd, mapfd, attach_type, 0);
  622. if (err) {
  623. p_err("failed prog attach to map");
  624. return -EINVAL;
  625. }
  626. if (json_output)
  627. jsonw_null(json_wtr);
  628. return 0;
  629. }
  630. static int do_detach(int argc, char **argv)
  631. {
  632. enum bpf_attach_type attach_type;
  633. int err, mapfd, progfd;
  634. if (!REQ_ARGS(5)) {
  635. p_err("too few parameters for map detach");
  636. return -EINVAL;
  637. }
  638. progfd = prog_parse_fd(&argc, &argv);
  639. if (progfd < 0)
  640. return progfd;
  641. attach_type = parse_attach_type(*argv);
  642. if (attach_type == __MAX_BPF_ATTACH_TYPE) {
  643. p_err("invalid attach type");
  644. return -EINVAL;
  645. }
  646. NEXT_ARG();
  647. mapfd = map_parse_fd(&argc, &argv);
  648. if (mapfd < 0)
  649. return mapfd;
  650. err = bpf_prog_detach2(progfd, mapfd, attach_type);
  651. if (err) {
  652. p_err("failed prog detach from map");
  653. return -EINVAL;
  654. }
  655. if (json_output)
  656. jsonw_null(json_wtr);
  657. return 0;
  658. }
  659. static int do_load(int argc, char **argv)
  660. {
  661. enum bpf_attach_type expected_attach_type;
  662. struct bpf_object_open_attr attr = {
  663. .prog_type = BPF_PROG_TYPE_UNSPEC,
  664. };
  665. struct map_replace *map_replace = NULL;
  666. unsigned int old_map_fds = 0;
  667. struct bpf_program *prog;
  668. struct bpf_object *obj;
  669. struct bpf_map *map;
  670. const char *pinfile;
  671. unsigned int i, j;
  672. __u32 ifindex = 0;
  673. int idx, err;
  674. if (!REQ_ARGS(2))
  675. return -1;
  676. attr.file = GET_ARG();
  677. pinfile = GET_ARG();
  678. while (argc) {
  679. if (is_prefix(*argv, "type")) {
  680. char *type;
  681. NEXT_ARG();
  682. if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
  683. p_err("program type already specified");
  684. goto err_free_reuse_maps;
  685. }
  686. if (!REQ_ARGS(1))
  687. goto err_free_reuse_maps;
  688. /* Put a '/' at the end of type to appease libbpf */
  689. type = malloc(strlen(*argv) + 2);
  690. if (!type) {
  691. p_err("mem alloc failed");
  692. goto err_free_reuse_maps;
  693. }
  694. *type = 0;
  695. strcat(type, *argv);
  696. strcat(type, "/");
  697. err = libbpf_prog_type_by_name(type, &attr.prog_type,
  698. &expected_attach_type);
  699. free(type);
  700. if (err < 0) {
  701. p_err("unknown program type '%s'", *argv);
  702. goto err_free_reuse_maps;
  703. }
  704. NEXT_ARG();
  705. } else if (is_prefix(*argv, "map")) {
  706. char *endptr, *name;
  707. int fd;
  708. NEXT_ARG();
  709. if (!REQ_ARGS(4))
  710. goto err_free_reuse_maps;
  711. if (is_prefix(*argv, "idx")) {
  712. NEXT_ARG();
  713. idx = strtoul(*argv, &endptr, 0);
  714. if (*endptr) {
  715. p_err("can't parse %s as IDX", *argv);
  716. goto err_free_reuse_maps;
  717. }
  718. name = NULL;
  719. } else if (is_prefix(*argv, "name")) {
  720. NEXT_ARG();
  721. name = *argv;
  722. idx = -1;
  723. } else {
  724. p_err("expected 'idx' or 'name', got: '%s'?",
  725. *argv);
  726. goto err_free_reuse_maps;
  727. }
  728. NEXT_ARG();
  729. fd = map_parse_fd(&argc, &argv);
  730. if (fd < 0)
  731. goto err_free_reuse_maps;
  732. map_replace = reallocarray(map_replace, old_map_fds + 1,
  733. sizeof(*map_replace));
  734. if (!map_replace) {
  735. p_err("mem alloc failed");
  736. goto err_free_reuse_maps;
  737. }
  738. map_replace[old_map_fds].idx = idx;
  739. map_replace[old_map_fds].name = name;
  740. map_replace[old_map_fds].fd = fd;
  741. old_map_fds++;
  742. } else if (is_prefix(*argv, "dev")) {
  743. NEXT_ARG();
  744. if (ifindex) {
  745. p_err("offload device already specified");
  746. goto err_free_reuse_maps;
  747. }
  748. if (!REQ_ARGS(1))
  749. goto err_free_reuse_maps;
  750. ifindex = if_nametoindex(*argv);
  751. if (!ifindex) {
  752. p_err("unrecognized netdevice '%s': %s",
  753. *argv, strerror(errno));
  754. goto err_free_reuse_maps;
  755. }
  756. NEXT_ARG();
  757. } else {
  758. p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
  759. *argv);
  760. goto err_free_reuse_maps;
  761. }
  762. }
  763. obj = __bpf_object__open_xattr(&attr, bpf_flags);
  764. if (IS_ERR_OR_NULL(obj)) {
  765. p_err("failed to open object file");
  766. goto err_free_reuse_maps;
  767. }
  768. prog = bpf_program__next(NULL, obj);
  769. if (!prog) {
  770. p_err("object file doesn't contain any bpf program");
  771. goto err_close_obj;
  772. }
  773. bpf_program__set_ifindex(prog, ifindex);
  774. if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
  775. const char *sec_name = bpf_program__title(prog, false);
  776. err = libbpf_prog_type_by_name(sec_name, &attr.prog_type,
  777. &expected_attach_type);
  778. if (err < 0) {
  779. p_err("failed to guess program type based on section name %s\n",
  780. sec_name);
  781. goto err_close_obj;
  782. }
  783. }
  784. bpf_program__set_type(prog, attr.prog_type);
  785. bpf_program__set_expected_attach_type(prog, expected_attach_type);
  786. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  787. map_replace_compar);
  788. /* After the sort maps by name will be first on the list, because they
  789. * have idx == -1. Resolve them.
  790. */
  791. j = 0;
  792. while (j < old_map_fds && map_replace[j].name) {
  793. i = 0;
  794. bpf_map__for_each(map, obj) {
  795. if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
  796. map_replace[j].idx = i;
  797. break;
  798. }
  799. i++;
  800. }
  801. if (map_replace[j].idx == -1) {
  802. p_err("unable to find map '%s'", map_replace[j].name);
  803. goto err_close_obj;
  804. }
  805. j++;
  806. }
  807. /* Resort if any names were resolved */
  808. if (j)
  809. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  810. map_replace_compar);
  811. /* Set ifindex and name reuse */
  812. j = 0;
  813. idx = 0;
  814. bpf_map__for_each(map, obj) {
  815. if (!bpf_map__is_offload_neutral(map))
  816. bpf_map__set_ifindex(map, ifindex);
  817. if (j < old_map_fds && idx == map_replace[j].idx) {
  818. err = bpf_map__reuse_fd(map, map_replace[j++].fd);
  819. if (err) {
  820. p_err("unable to set up map reuse: %d", err);
  821. goto err_close_obj;
  822. }
  823. /* Next reuse wants to apply to the same map */
  824. if (j < old_map_fds && map_replace[j].idx == idx) {
  825. p_err("replacement for map idx %d specified more than once",
  826. idx);
  827. goto err_close_obj;
  828. }
  829. }
  830. idx++;
  831. }
  832. if (j < old_map_fds) {
  833. p_err("map idx '%d' not used", map_replace[j].idx);
  834. goto err_close_obj;
  835. }
  836. err = bpf_object__load(obj);
  837. if (err) {
  838. p_err("failed to load object file");
  839. goto err_close_obj;
  840. }
  841. if (do_pin_fd(bpf_program__fd(prog), pinfile))
  842. goto err_close_obj;
  843. if (json_output)
  844. jsonw_null(json_wtr);
  845. bpf_object__close(obj);
  846. for (i = 0; i < old_map_fds; i++)
  847. close(map_replace[i].fd);
  848. free(map_replace);
  849. return 0;
  850. err_close_obj:
  851. bpf_object__close(obj);
  852. err_free_reuse_maps:
  853. for (i = 0; i < old_map_fds; i++)
  854. close(map_replace[i].fd);
  855. free(map_replace);
  856. return -1;
  857. }
  858. static int do_help(int argc, char **argv)
  859. {
  860. if (json_output) {
  861. jsonw_null(json_wtr);
  862. return 0;
  863. }
  864. fprintf(stderr,
  865. "Usage: %s %s { show | list } [PROG]\n"
  866. " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
  867. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  868. " %s %s pin PROG FILE\n"
  869. " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n"
  870. " [map { idx IDX | name NAME } MAP]\n"
  871. " %s %s attach PROG ATTACH_TYPE MAP\n"
  872. " %s %s detach PROG ATTACH_TYPE MAP\n"
  873. " %s %s help\n"
  874. "\n"
  875. " " HELP_SPEC_MAP "\n"
  876. " " HELP_SPEC_PROGRAM "\n"
  877. " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
  878. " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
  879. " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
  880. " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
  881. " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
  882. " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
  883. " cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
  884. " ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n"
  885. " " HELP_SPEC_OPTIONS "\n"
  886. "",
  887. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  888. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  889. bin_name, argv[-2], bin_name, argv[-2]);
  890. return 0;
  891. }
  892. static const struct cmd cmds[] = {
  893. { "show", do_show },
  894. { "list", do_show },
  895. { "help", do_help },
  896. { "dump", do_dump },
  897. { "pin", do_pin },
  898. { "load", do_load },
  899. { "attach", do_attach },
  900. { "detach", do_detach },
  901. { 0 }
  902. };
  903. int do_prog(int argc, char **argv)
  904. {
  905. return cmd_select(cmds, argc, argv, do_help);
  906. }