prog.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. void *new_map_replace;
  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. new_map_replace = reallocarray(map_replace,
  733. old_map_fds + 1,
  734. sizeof(*map_replace));
  735. if (!new_map_replace) {
  736. p_err("mem alloc failed");
  737. goto err_free_reuse_maps;
  738. }
  739. map_replace = new_map_replace;
  740. map_replace[old_map_fds].idx = idx;
  741. map_replace[old_map_fds].name = name;
  742. map_replace[old_map_fds].fd = fd;
  743. old_map_fds++;
  744. } else if (is_prefix(*argv, "dev")) {
  745. NEXT_ARG();
  746. if (ifindex) {
  747. p_err("offload device already specified");
  748. goto err_free_reuse_maps;
  749. }
  750. if (!REQ_ARGS(1))
  751. goto err_free_reuse_maps;
  752. ifindex = if_nametoindex(*argv);
  753. if (!ifindex) {
  754. p_err("unrecognized netdevice '%s': %s",
  755. *argv, strerror(errno));
  756. goto err_free_reuse_maps;
  757. }
  758. NEXT_ARG();
  759. } else {
  760. p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
  761. *argv);
  762. goto err_free_reuse_maps;
  763. }
  764. }
  765. obj = __bpf_object__open_xattr(&attr, bpf_flags);
  766. if (IS_ERR_OR_NULL(obj)) {
  767. p_err("failed to open object file");
  768. goto err_free_reuse_maps;
  769. }
  770. prog = bpf_program__next(NULL, obj);
  771. if (!prog) {
  772. p_err("object file doesn't contain any bpf program");
  773. goto err_close_obj;
  774. }
  775. bpf_program__set_ifindex(prog, ifindex);
  776. if (attr.prog_type == BPF_PROG_TYPE_UNSPEC) {
  777. const char *sec_name = bpf_program__title(prog, false);
  778. err = libbpf_prog_type_by_name(sec_name, &attr.prog_type,
  779. &expected_attach_type);
  780. if (err < 0) {
  781. p_err("failed to guess program type based on section name %s\n",
  782. sec_name);
  783. goto err_close_obj;
  784. }
  785. }
  786. bpf_program__set_type(prog, attr.prog_type);
  787. bpf_program__set_expected_attach_type(prog, expected_attach_type);
  788. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  789. map_replace_compar);
  790. /* After the sort maps by name will be first on the list, because they
  791. * have idx == -1. Resolve them.
  792. */
  793. j = 0;
  794. while (j < old_map_fds && map_replace[j].name) {
  795. i = 0;
  796. bpf_map__for_each(map, obj) {
  797. if (!strcmp(bpf_map__name(map), map_replace[j].name)) {
  798. map_replace[j].idx = i;
  799. break;
  800. }
  801. i++;
  802. }
  803. if (map_replace[j].idx == -1) {
  804. p_err("unable to find map '%s'", map_replace[j].name);
  805. goto err_close_obj;
  806. }
  807. j++;
  808. }
  809. /* Resort if any names were resolved */
  810. if (j)
  811. qsort(map_replace, old_map_fds, sizeof(*map_replace),
  812. map_replace_compar);
  813. /* Set ifindex and name reuse */
  814. j = 0;
  815. idx = 0;
  816. bpf_map__for_each(map, obj) {
  817. if (!bpf_map__is_offload_neutral(map))
  818. bpf_map__set_ifindex(map, ifindex);
  819. if (j < old_map_fds && idx == map_replace[j].idx) {
  820. err = bpf_map__reuse_fd(map, map_replace[j++].fd);
  821. if (err) {
  822. p_err("unable to set up map reuse: %d", err);
  823. goto err_close_obj;
  824. }
  825. /* Next reuse wants to apply to the same map */
  826. if (j < old_map_fds && map_replace[j].idx == idx) {
  827. p_err("replacement for map idx %d specified more than once",
  828. idx);
  829. goto err_close_obj;
  830. }
  831. }
  832. idx++;
  833. }
  834. if (j < old_map_fds) {
  835. p_err("map idx '%d' not used", map_replace[j].idx);
  836. goto err_close_obj;
  837. }
  838. err = bpf_object__load(obj);
  839. if (err) {
  840. p_err("failed to load object file");
  841. goto err_close_obj;
  842. }
  843. if (do_pin_fd(bpf_program__fd(prog), pinfile))
  844. goto err_close_obj;
  845. if (json_output)
  846. jsonw_null(json_wtr);
  847. bpf_object__close(obj);
  848. for (i = 0; i < old_map_fds; i++)
  849. close(map_replace[i].fd);
  850. free(map_replace);
  851. return 0;
  852. err_close_obj:
  853. bpf_object__close(obj);
  854. err_free_reuse_maps:
  855. for (i = 0; i < old_map_fds; i++)
  856. close(map_replace[i].fd);
  857. free(map_replace);
  858. return -1;
  859. }
  860. static int do_help(int argc, char **argv)
  861. {
  862. if (json_output) {
  863. jsonw_null(json_wtr);
  864. return 0;
  865. }
  866. fprintf(stderr,
  867. "Usage: %s %s { show | list } [PROG]\n"
  868. " %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
  869. " %s %s dump jited PROG [{ file FILE | opcodes }]\n"
  870. " %s %s pin PROG FILE\n"
  871. " %s %s load OBJ FILE [type TYPE] [dev NAME] \\\n"
  872. " [map { idx IDX | name NAME } MAP]\n"
  873. " %s %s attach PROG ATTACH_TYPE MAP\n"
  874. " %s %s detach PROG ATTACH_TYPE MAP\n"
  875. " %s %s help\n"
  876. "\n"
  877. " " HELP_SPEC_MAP "\n"
  878. " " HELP_SPEC_PROGRAM "\n"
  879. " TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
  880. " tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
  881. " cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
  882. " lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
  883. " cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
  884. " cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
  885. " cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
  886. " ATTACH_TYPE := { msg_verdict | skb_verdict | skb_parse }\n"
  887. " " HELP_SPEC_OPTIONS "\n"
  888. "",
  889. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  890. bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
  891. bin_name, argv[-2], bin_name, argv[-2]);
  892. return 0;
  893. }
  894. static const struct cmd cmds[] = {
  895. { "show", do_show },
  896. { "list", do_show },
  897. { "help", do_help },
  898. { "dump", do_dump },
  899. { "pin", do_pin },
  900. { "load", do_load },
  901. { "attach", do_attach },
  902. { "detach", do_detach },
  903. { 0 }
  904. };
  905. int do_prog(int argc, char **argv)
  906. {
  907. return cmd_select(cmds, argc, argv, do_help);
  908. }