link.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2020 Facebook */
  3. #include <errno.h>
  4. #include <linux/err.h>
  5. #include <linux/netfilter.h>
  6. #include <linux/netfilter_arp.h>
  7. #include <linux/perf_event.h>
  8. #include <net/if.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <bpf/bpf.h>
  12. #include <bpf/hashmap.h>
  13. #include "json_writer.h"
  14. #include "main.h"
  15. #include "xlated_dumper.h"
  16. #define PERF_HW_CACHE_LEN 128
  17. static struct hashmap *link_table;
  18. static struct dump_data dd;
  19. static const char *perf_type_name[PERF_TYPE_MAX] = {
  20. [PERF_TYPE_HARDWARE] = "hardware",
  21. [PERF_TYPE_SOFTWARE] = "software",
  22. [PERF_TYPE_TRACEPOINT] = "tracepoint",
  23. [PERF_TYPE_HW_CACHE] = "hw-cache",
  24. [PERF_TYPE_RAW] = "raw",
  25. [PERF_TYPE_BREAKPOINT] = "breakpoint",
  26. };
  27. const char *event_symbols_hw[PERF_COUNT_HW_MAX] = {
  28. [PERF_COUNT_HW_CPU_CYCLES] = "cpu-cycles",
  29. [PERF_COUNT_HW_INSTRUCTIONS] = "instructions",
  30. [PERF_COUNT_HW_CACHE_REFERENCES] = "cache-references",
  31. [PERF_COUNT_HW_CACHE_MISSES] = "cache-misses",
  32. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = "branch-instructions",
  33. [PERF_COUNT_HW_BRANCH_MISSES] = "branch-misses",
  34. [PERF_COUNT_HW_BUS_CYCLES] = "bus-cycles",
  35. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = "stalled-cycles-frontend",
  36. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = "stalled-cycles-backend",
  37. [PERF_COUNT_HW_REF_CPU_CYCLES] = "ref-cycles",
  38. };
  39. const char *event_symbols_sw[PERF_COUNT_SW_MAX] = {
  40. [PERF_COUNT_SW_CPU_CLOCK] = "cpu-clock",
  41. [PERF_COUNT_SW_TASK_CLOCK] = "task-clock",
  42. [PERF_COUNT_SW_PAGE_FAULTS] = "page-faults",
  43. [PERF_COUNT_SW_CONTEXT_SWITCHES] = "context-switches",
  44. [PERF_COUNT_SW_CPU_MIGRATIONS] = "cpu-migrations",
  45. [PERF_COUNT_SW_PAGE_FAULTS_MIN] = "minor-faults",
  46. [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = "major-faults",
  47. [PERF_COUNT_SW_ALIGNMENT_FAULTS] = "alignment-faults",
  48. [PERF_COUNT_SW_EMULATION_FAULTS] = "emulation-faults",
  49. [PERF_COUNT_SW_DUMMY] = "dummy",
  50. [PERF_COUNT_SW_BPF_OUTPUT] = "bpf-output",
  51. [PERF_COUNT_SW_CGROUP_SWITCHES] = "cgroup-switches",
  52. };
  53. const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] = {
  54. [PERF_COUNT_HW_CACHE_L1D] = "L1-dcache",
  55. [PERF_COUNT_HW_CACHE_L1I] = "L1-icache",
  56. [PERF_COUNT_HW_CACHE_LL] = "LLC",
  57. [PERF_COUNT_HW_CACHE_DTLB] = "dTLB",
  58. [PERF_COUNT_HW_CACHE_ITLB] = "iTLB",
  59. [PERF_COUNT_HW_CACHE_BPU] = "branch",
  60. [PERF_COUNT_HW_CACHE_NODE] = "node",
  61. };
  62. const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] = {
  63. [PERF_COUNT_HW_CACHE_OP_READ] = "load",
  64. [PERF_COUNT_HW_CACHE_OP_WRITE] = "store",
  65. [PERF_COUNT_HW_CACHE_OP_PREFETCH] = "prefetch",
  66. };
  67. const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  68. [PERF_COUNT_HW_CACHE_RESULT_ACCESS] = "refs",
  69. [PERF_COUNT_HW_CACHE_RESULT_MISS] = "misses",
  70. };
  71. #define perf_event_name(array, id) ({ \
  72. const char *event_str = NULL; \
  73. \
  74. if ((id) < ARRAY_SIZE(array)) \
  75. event_str = array[id]; \
  76. event_str; \
  77. })
  78. static int link_parse_fd(int *argc, char ***argv)
  79. {
  80. int fd;
  81. if (is_prefix(**argv, "id")) {
  82. unsigned int id;
  83. char *endptr;
  84. NEXT_ARGP();
  85. id = strtoul(**argv, &endptr, 0);
  86. if (*endptr) {
  87. p_err("can't parse %s as ID", **argv);
  88. return -1;
  89. }
  90. NEXT_ARGP();
  91. fd = bpf_link_get_fd_by_id(id);
  92. if (fd < 0)
  93. p_err("failed to get link with ID %u: %s", id, strerror(errno));
  94. return fd;
  95. } else if (is_prefix(**argv, "pinned")) {
  96. char *path;
  97. NEXT_ARGP();
  98. path = **argv;
  99. NEXT_ARGP();
  100. return open_obj_pinned_any(path, BPF_OBJ_LINK, NULL);
  101. }
  102. p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
  103. return -1;
  104. }
  105. static void
  106. show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
  107. {
  108. const char *link_type_str;
  109. jsonw_uint_field(wtr, "id", info->id);
  110. link_type_str = libbpf_bpf_link_type_str(info->type);
  111. if (link_type_str)
  112. jsonw_string_field(wtr, "type", link_type_str);
  113. else
  114. jsonw_uint_field(wtr, "type", info->type);
  115. jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
  116. }
  117. static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
  118. {
  119. const char *attach_type_str;
  120. attach_type_str = libbpf_bpf_attach_type_str(attach_type);
  121. if (attach_type_str)
  122. jsonw_string_field(wtr, "attach_type", attach_type_str);
  123. else
  124. jsonw_uint_field(wtr, "attach_type", attach_type);
  125. }
  126. static void show_link_ifindex_json(__u32 ifindex, json_writer_t *wtr)
  127. {
  128. char devname[IF_NAMESIZE] = "(unknown)";
  129. if (ifindex)
  130. if_indextoname(ifindex, devname);
  131. else
  132. snprintf(devname, sizeof(devname), "(detached)");
  133. jsonw_string_field(wtr, "devname", devname);
  134. jsonw_uint_field(wtr, "ifindex", ifindex);
  135. }
  136. static bool is_iter_map_target(const char *target_name)
  137. {
  138. return strcmp(target_name, "bpf_map_elem") == 0 ||
  139. strcmp(target_name, "bpf_sk_storage_map") == 0;
  140. }
  141. static bool is_iter_cgroup_target(const char *target_name)
  142. {
  143. return strcmp(target_name, "cgroup") == 0;
  144. }
  145. static const char *cgroup_order_string(__u32 order)
  146. {
  147. switch (order) {
  148. case BPF_CGROUP_ITER_ORDER_UNSPEC:
  149. return "order_unspec";
  150. case BPF_CGROUP_ITER_SELF_ONLY:
  151. return "self_only";
  152. case BPF_CGROUP_ITER_DESCENDANTS_PRE:
  153. return "descendants_pre";
  154. case BPF_CGROUP_ITER_DESCENDANTS_POST:
  155. return "descendants_post";
  156. case BPF_CGROUP_ITER_ANCESTORS_UP:
  157. return "ancestors_up";
  158. default: /* won't happen */
  159. return "unknown";
  160. }
  161. }
  162. static bool is_iter_task_target(const char *target_name)
  163. {
  164. return strcmp(target_name, "task") == 0 ||
  165. strcmp(target_name, "task_file") == 0 ||
  166. strcmp(target_name, "task_vma") == 0;
  167. }
  168. static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
  169. {
  170. const char *target_name = u64_to_ptr(info->iter.target_name);
  171. jsonw_string_field(wtr, "target_name", target_name);
  172. if (is_iter_map_target(target_name))
  173. jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
  174. else if (is_iter_task_target(target_name)) {
  175. if (info->iter.task.tid)
  176. jsonw_uint_field(wtr, "tid", info->iter.task.tid);
  177. else if (info->iter.task.pid)
  178. jsonw_uint_field(wtr, "pid", info->iter.task.pid);
  179. }
  180. if (is_iter_cgroup_target(target_name)) {
  181. jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
  182. jsonw_string_field(wtr, "order",
  183. cgroup_order_string(info->iter.cgroup.order));
  184. }
  185. }
  186. void netfilter_dump_json(const struct bpf_link_info *info, json_writer_t *wtr)
  187. {
  188. jsonw_uint_field(json_wtr, "pf",
  189. info->netfilter.pf);
  190. jsonw_uint_field(json_wtr, "hook",
  191. info->netfilter.hooknum);
  192. jsonw_int_field(json_wtr, "prio",
  193. info->netfilter.priority);
  194. jsonw_uint_field(json_wtr, "flags",
  195. info->netfilter.flags);
  196. }
  197. static int get_prog_info(int prog_id, struct bpf_prog_info *info)
  198. {
  199. __u32 len = sizeof(*info);
  200. int err, prog_fd;
  201. prog_fd = bpf_prog_get_fd_by_id(prog_id);
  202. if (prog_fd < 0)
  203. return prog_fd;
  204. memset(info, 0, sizeof(*info));
  205. err = bpf_prog_get_info_by_fd(prog_fd, info, &len);
  206. if (err)
  207. p_err("can't get prog info: %s", strerror(errno));
  208. close(prog_fd);
  209. return err;
  210. }
  211. struct addr_cookie {
  212. __u64 addr;
  213. __u64 cookie;
  214. };
  215. static int cmp_addr_cookie(const void *A, const void *B)
  216. {
  217. const struct addr_cookie *a = A, *b = B;
  218. if (a->addr == b->addr)
  219. return 0;
  220. return a->addr < b->addr ? -1 : 1;
  221. }
  222. static struct addr_cookie *
  223. get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count)
  224. {
  225. struct addr_cookie *data;
  226. __u32 i;
  227. data = calloc(count, sizeof(data[0]));
  228. if (!data) {
  229. p_err("mem alloc failed");
  230. return NULL;
  231. }
  232. for (i = 0; i < count; i++) {
  233. data[i].addr = addrs[i];
  234. data[i].cookie = cookies[i];
  235. }
  236. qsort(data, count, sizeof(data[0]), cmp_addr_cookie);
  237. return data;
  238. }
  239. static bool is_x86_ibt_enabled(void)
  240. {
  241. #if defined(__x86_64__)
  242. struct kernel_config_option options[] = {
  243. { "CONFIG_X86_KERNEL_IBT", },
  244. };
  245. char *values[ARRAY_SIZE(options)] = { };
  246. bool ret;
  247. if (read_kernel_config(options, ARRAY_SIZE(options), values, NULL))
  248. return false;
  249. ret = !!values[0];
  250. free(values[0]);
  251. return ret;
  252. #else
  253. return false;
  254. #endif
  255. }
  256. static bool
  257. symbol_matches_target(__u64 sym_addr, __u64 target_addr, bool is_ibt_enabled)
  258. {
  259. if (sym_addr == target_addr)
  260. return true;
  261. /*
  262. * On x86_64 architectures with CET (Control-flow Enforcement Technology),
  263. * function entry points have a 4-byte 'endbr' instruction prefix.
  264. * This causes kprobe hooks to target the address *after* 'endbr'
  265. * (symbol address + 4), preserving the CET instruction.
  266. * Here we check if the symbol address matches the hook target address
  267. * minus 4, indicating a CET-enabled function entry point.
  268. */
  269. if (is_ibt_enabled && sym_addr == target_addr - 4)
  270. return true;
  271. return false;
  272. }
  273. static void
  274. show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
  275. {
  276. struct addr_cookie *data;
  277. __u32 i, j = 0;
  278. bool is_ibt_enabled;
  279. jsonw_bool_field(json_wtr, "retprobe",
  280. info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN);
  281. jsonw_uint_field(json_wtr, "func_cnt", info->kprobe_multi.count);
  282. jsonw_uint_field(json_wtr, "missed", info->kprobe_multi.missed);
  283. jsonw_name(json_wtr, "funcs");
  284. jsonw_start_array(json_wtr);
  285. data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs),
  286. u64_to_ptr(info->kprobe_multi.cookies),
  287. info->kprobe_multi.count);
  288. if (!data)
  289. return;
  290. /* Load it once for all. */
  291. if (!dd.sym_count)
  292. kernel_syms_load(&dd);
  293. if (!dd.sym_count)
  294. goto error;
  295. is_ibt_enabled = is_x86_ibt_enabled();
  296. for (i = 0; i < dd.sym_count; i++) {
  297. if (!symbol_matches_target(dd.sym_mapping[i].address,
  298. data[j].addr, is_ibt_enabled))
  299. continue;
  300. jsonw_start_object(json_wtr);
  301. jsonw_uint_field(json_wtr, "addr", (unsigned long)data[j].addr);
  302. jsonw_string_field(json_wtr, "func", dd.sym_mapping[i].name);
  303. /* Print null if it is vmlinux */
  304. if (dd.sym_mapping[i].module[0] == '\0') {
  305. jsonw_name(json_wtr, "module");
  306. jsonw_null(json_wtr);
  307. } else {
  308. jsonw_string_field(json_wtr, "module", dd.sym_mapping[i].module);
  309. }
  310. jsonw_uint_field(json_wtr, "cookie", data[j].cookie);
  311. jsonw_end_object(json_wtr);
  312. if (j++ == info->kprobe_multi.count)
  313. break;
  314. }
  315. jsonw_end_array(json_wtr);
  316. error:
  317. free(data);
  318. }
  319. static __u64 *u64_to_arr(__u64 val)
  320. {
  321. return (__u64 *) u64_to_ptr(val);
  322. }
  323. static void
  324. show_uprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr)
  325. {
  326. __u32 i;
  327. jsonw_bool_field(json_wtr, "retprobe",
  328. info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN);
  329. jsonw_string_field(json_wtr, "path", (char *) u64_to_ptr(info->uprobe_multi.path));
  330. jsonw_uint_field(json_wtr, "func_cnt", info->uprobe_multi.count);
  331. jsonw_int_field(json_wtr, "pid", (int) info->uprobe_multi.pid);
  332. jsonw_name(json_wtr, "funcs");
  333. jsonw_start_array(json_wtr);
  334. for (i = 0; i < info->uprobe_multi.count; i++) {
  335. jsonw_start_object(json_wtr);
  336. jsonw_uint_field(json_wtr, "offset",
  337. u64_to_arr(info->uprobe_multi.offsets)[i]);
  338. jsonw_uint_field(json_wtr, "ref_ctr_offset",
  339. u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i]);
  340. jsonw_uint_field(json_wtr, "cookie",
  341. u64_to_arr(info->uprobe_multi.cookies)[i]);
  342. jsonw_end_object(json_wtr);
  343. }
  344. jsonw_end_array(json_wtr);
  345. }
  346. static void
  347. show_perf_event_kprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
  348. {
  349. jsonw_bool_field(wtr, "retprobe", info->perf_event.type == BPF_PERF_EVENT_KRETPROBE);
  350. jsonw_uint_field(wtr, "addr", info->perf_event.kprobe.addr);
  351. jsonw_string_field(wtr, "func",
  352. u64_to_ptr(info->perf_event.kprobe.func_name));
  353. jsonw_uint_field(wtr, "offset", info->perf_event.kprobe.offset);
  354. jsonw_uint_field(wtr, "missed", info->perf_event.kprobe.missed);
  355. jsonw_uint_field(wtr, "cookie", info->perf_event.kprobe.cookie);
  356. }
  357. static void
  358. show_perf_event_uprobe_json(struct bpf_link_info *info, json_writer_t *wtr)
  359. {
  360. jsonw_bool_field(wtr, "retprobe", info->perf_event.type == BPF_PERF_EVENT_URETPROBE);
  361. jsonw_string_field(wtr, "file",
  362. u64_to_ptr(info->perf_event.uprobe.file_name));
  363. jsonw_uint_field(wtr, "offset", info->perf_event.uprobe.offset);
  364. jsonw_uint_field(wtr, "cookie", info->perf_event.uprobe.cookie);
  365. jsonw_uint_field(wtr, "ref_ctr_offset", info->perf_event.uprobe.ref_ctr_offset);
  366. }
  367. static void
  368. show_perf_event_tracepoint_json(struct bpf_link_info *info, json_writer_t *wtr)
  369. {
  370. jsonw_string_field(wtr, "tracepoint",
  371. u64_to_ptr(info->perf_event.tracepoint.tp_name));
  372. jsonw_uint_field(wtr, "cookie", info->perf_event.tracepoint.cookie);
  373. }
  374. static char *perf_config_hw_cache_str(__u64 config)
  375. {
  376. const char *hw_cache, *result, *op;
  377. char *str = malloc(PERF_HW_CACHE_LEN);
  378. if (!str) {
  379. p_err("mem alloc failed");
  380. return NULL;
  381. }
  382. hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
  383. if (hw_cache)
  384. snprintf(str, PERF_HW_CACHE_LEN, "%s-", hw_cache);
  385. else
  386. snprintf(str, PERF_HW_CACHE_LEN, "%llu-", config & 0xff);
  387. op = perf_event_name(evsel__hw_cache_op, (config >> 8) & 0xff);
  388. if (op)
  389. snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
  390. "%s-", op);
  391. else
  392. snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
  393. "%llu-", (config >> 8) & 0xff);
  394. result = perf_event_name(evsel__hw_cache_result, config >> 16);
  395. if (result)
  396. snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
  397. "%s", result);
  398. else
  399. snprintf(str + strlen(str), PERF_HW_CACHE_LEN - strlen(str),
  400. "%llu", config >> 16);
  401. return str;
  402. }
  403. static const char *perf_config_str(__u32 type, __u64 config)
  404. {
  405. const char *perf_config;
  406. switch (type) {
  407. case PERF_TYPE_HARDWARE:
  408. perf_config = perf_event_name(event_symbols_hw, config);
  409. break;
  410. case PERF_TYPE_SOFTWARE:
  411. perf_config = perf_event_name(event_symbols_sw, config);
  412. break;
  413. case PERF_TYPE_HW_CACHE:
  414. perf_config = perf_config_hw_cache_str(config);
  415. break;
  416. default:
  417. perf_config = NULL;
  418. break;
  419. }
  420. return perf_config;
  421. }
  422. static void
  423. show_perf_event_event_json(struct bpf_link_info *info, json_writer_t *wtr)
  424. {
  425. __u64 config = info->perf_event.event.config;
  426. __u32 type = info->perf_event.event.type;
  427. const char *perf_type, *perf_config;
  428. perf_type = perf_event_name(perf_type_name, type);
  429. if (perf_type)
  430. jsonw_string_field(wtr, "event_type", perf_type);
  431. else
  432. jsonw_uint_field(wtr, "event_type", type);
  433. perf_config = perf_config_str(type, config);
  434. if (perf_config)
  435. jsonw_string_field(wtr, "event_config", perf_config);
  436. else
  437. jsonw_uint_field(wtr, "event_config", config);
  438. jsonw_uint_field(wtr, "cookie", info->perf_event.event.cookie);
  439. if (type == PERF_TYPE_HW_CACHE && perf_config)
  440. free((void *)perf_config);
  441. }
  442. static int show_link_close_json(int fd, struct bpf_link_info *info)
  443. {
  444. struct bpf_prog_info prog_info;
  445. const char *prog_type_str;
  446. int err;
  447. jsonw_start_object(json_wtr);
  448. show_link_header_json(info, json_wtr);
  449. switch (info->type) {
  450. case BPF_LINK_TYPE_RAW_TRACEPOINT:
  451. jsonw_string_field(json_wtr, "tp_name",
  452. u64_to_ptr(info->raw_tracepoint.tp_name));
  453. jsonw_uint_field(json_wtr, "cookie", info->raw_tracepoint.cookie);
  454. break;
  455. case BPF_LINK_TYPE_TRACING:
  456. err = get_prog_info(info->prog_id, &prog_info);
  457. if (err)
  458. return err;
  459. prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
  460. /* libbpf will return NULL for variants unknown to it. */
  461. if (prog_type_str)
  462. jsonw_string_field(json_wtr, "prog_type", prog_type_str);
  463. else
  464. jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
  465. show_link_attach_type_json(info->tracing.attach_type,
  466. json_wtr);
  467. jsonw_uint_field(json_wtr, "target_obj_id", info->tracing.target_obj_id);
  468. jsonw_uint_field(json_wtr, "target_btf_id", info->tracing.target_btf_id);
  469. jsonw_uint_field(json_wtr, "cookie", info->tracing.cookie);
  470. break;
  471. case BPF_LINK_TYPE_CGROUP:
  472. jsonw_lluint_field(json_wtr, "cgroup_id",
  473. info->cgroup.cgroup_id);
  474. show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
  475. break;
  476. case BPF_LINK_TYPE_ITER:
  477. show_iter_json(info, json_wtr);
  478. break;
  479. case BPF_LINK_TYPE_NETNS:
  480. jsonw_uint_field(json_wtr, "netns_ino",
  481. info->netns.netns_ino);
  482. show_link_attach_type_json(info->netns.attach_type, json_wtr);
  483. break;
  484. case BPF_LINK_TYPE_NETFILTER:
  485. netfilter_dump_json(info, json_wtr);
  486. break;
  487. case BPF_LINK_TYPE_TCX:
  488. show_link_ifindex_json(info->tcx.ifindex, json_wtr);
  489. show_link_attach_type_json(info->tcx.attach_type, json_wtr);
  490. break;
  491. case BPF_LINK_TYPE_NETKIT:
  492. show_link_ifindex_json(info->netkit.ifindex, json_wtr);
  493. show_link_attach_type_json(info->netkit.attach_type, json_wtr);
  494. break;
  495. case BPF_LINK_TYPE_SOCKMAP:
  496. jsonw_uint_field(json_wtr, "map_id", info->sockmap.map_id);
  497. show_link_attach_type_json(info->sockmap.attach_type, json_wtr);
  498. break;
  499. case BPF_LINK_TYPE_XDP:
  500. show_link_ifindex_json(info->xdp.ifindex, json_wtr);
  501. break;
  502. case BPF_LINK_TYPE_STRUCT_OPS:
  503. jsonw_uint_field(json_wtr, "map_id",
  504. info->struct_ops.map_id);
  505. break;
  506. case BPF_LINK_TYPE_KPROBE_MULTI:
  507. show_kprobe_multi_json(info, json_wtr);
  508. break;
  509. case BPF_LINK_TYPE_UPROBE_MULTI:
  510. show_uprobe_multi_json(info, json_wtr);
  511. break;
  512. case BPF_LINK_TYPE_PERF_EVENT:
  513. switch (info->perf_event.type) {
  514. case BPF_PERF_EVENT_EVENT:
  515. show_perf_event_event_json(info, json_wtr);
  516. break;
  517. case BPF_PERF_EVENT_TRACEPOINT:
  518. show_perf_event_tracepoint_json(info, json_wtr);
  519. break;
  520. case BPF_PERF_EVENT_KPROBE:
  521. case BPF_PERF_EVENT_KRETPROBE:
  522. show_perf_event_kprobe_json(info, json_wtr);
  523. break;
  524. case BPF_PERF_EVENT_UPROBE:
  525. case BPF_PERF_EVENT_URETPROBE:
  526. show_perf_event_uprobe_json(info, json_wtr);
  527. break;
  528. default:
  529. break;
  530. }
  531. break;
  532. default:
  533. break;
  534. }
  535. if (!hashmap__empty(link_table)) {
  536. struct hashmap_entry *entry;
  537. jsonw_name(json_wtr, "pinned");
  538. jsonw_start_array(json_wtr);
  539. hashmap__for_each_key_entry(link_table, entry, info->id)
  540. jsonw_string(json_wtr, entry->pvalue);
  541. jsonw_end_array(json_wtr);
  542. }
  543. emit_obj_refs_json(refs_table, info->id, json_wtr);
  544. jsonw_end_object(json_wtr);
  545. return 0;
  546. }
  547. static void show_link_header_plain(struct bpf_link_info *info)
  548. {
  549. const char *link_type_str;
  550. printf("%u: ", info->id);
  551. link_type_str = libbpf_bpf_link_type_str(info->type);
  552. if (link_type_str)
  553. printf("%s ", link_type_str);
  554. else
  555. printf("type %u ", info->type);
  556. if (info->type == BPF_LINK_TYPE_STRUCT_OPS)
  557. printf("map %u ", info->struct_ops.map_id);
  558. else
  559. printf("prog %u ", info->prog_id);
  560. }
  561. static void show_link_attach_type_plain(__u32 attach_type)
  562. {
  563. const char *attach_type_str;
  564. attach_type_str = libbpf_bpf_attach_type_str(attach_type);
  565. if (attach_type_str)
  566. printf("attach_type %s ", attach_type_str);
  567. else
  568. printf("attach_type %u ", attach_type);
  569. }
  570. static void show_link_ifindex_plain(__u32 ifindex)
  571. {
  572. char devname[IF_NAMESIZE * 2] = "(unknown)";
  573. char tmpname[IF_NAMESIZE];
  574. char *ret = NULL;
  575. if (ifindex)
  576. ret = if_indextoname(ifindex, tmpname);
  577. else
  578. snprintf(devname, sizeof(devname), "(detached)");
  579. if (ret)
  580. snprintf(devname, sizeof(devname), "%s(%u)",
  581. tmpname, ifindex);
  582. printf("ifindex %s ", devname);
  583. }
  584. static void show_iter_plain(struct bpf_link_info *info)
  585. {
  586. const char *target_name = u64_to_ptr(info->iter.target_name);
  587. printf("target_name %s ", target_name);
  588. if (is_iter_map_target(target_name))
  589. printf("map_id %u ", info->iter.map.map_id);
  590. else if (is_iter_task_target(target_name)) {
  591. if (info->iter.task.tid)
  592. printf("tid %u ", info->iter.task.tid);
  593. else if (info->iter.task.pid)
  594. printf("pid %u ", info->iter.task.pid);
  595. }
  596. if (is_iter_cgroup_target(target_name)) {
  597. printf("cgroup_id %llu ", info->iter.cgroup.cgroup_id);
  598. printf("order %s ",
  599. cgroup_order_string(info->iter.cgroup.order));
  600. }
  601. }
  602. static const char * const pf2name[] = {
  603. [NFPROTO_INET] = "inet",
  604. [NFPROTO_IPV4] = "ip",
  605. [NFPROTO_ARP] = "arp",
  606. [NFPROTO_NETDEV] = "netdev",
  607. [NFPROTO_BRIDGE] = "bridge",
  608. [NFPROTO_IPV6] = "ip6",
  609. };
  610. static const char * const inethook2name[] = {
  611. [NF_INET_PRE_ROUTING] = "prerouting",
  612. [NF_INET_LOCAL_IN] = "input",
  613. [NF_INET_FORWARD] = "forward",
  614. [NF_INET_LOCAL_OUT] = "output",
  615. [NF_INET_POST_ROUTING] = "postrouting",
  616. };
  617. static const char * const arphook2name[] = {
  618. [NF_ARP_IN] = "input",
  619. [NF_ARP_OUT] = "output",
  620. };
  621. void netfilter_dump_plain(const struct bpf_link_info *info)
  622. {
  623. const char *hookname = NULL, *pfname = NULL;
  624. unsigned int hook = info->netfilter.hooknum;
  625. unsigned int pf = info->netfilter.pf;
  626. if (pf < ARRAY_SIZE(pf2name))
  627. pfname = pf2name[pf];
  628. switch (pf) {
  629. case NFPROTO_BRIDGE: /* bridge shares numbers with enum nf_inet_hooks */
  630. case NFPROTO_IPV4:
  631. case NFPROTO_IPV6:
  632. case NFPROTO_INET:
  633. if (hook < ARRAY_SIZE(inethook2name))
  634. hookname = inethook2name[hook];
  635. break;
  636. case NFPROTO_ARP:
  637. if (hook < ARRAY_SIZE(arphook2name))
  638. hookname = arphook2name[hook];
  639. default:
  640. break;
  641. }
  642. if (pfname)
  643. printf("\n\t%s", pfname);
  644. else
  645. printf("\n\tpf: %u", pf);
  646. if (hookname)
  647. printf(" %s", hookname);
  648. else
  649. printf(", hook %u,", hook);
  650. printf(" prio %d", info->netfilter.priority);
  651. if (info->netfilter.flags)
  652. printf(" flags 0x%x", info->netfilter.flags);
  653. }
  654. static void show_kprobe_multi_plain(struct bpf_link_info *info)
  655. {
  656. struct addr_cookie *data;
  657. __u32 i, j = 0;
  658. bool is_ibt_enabled;
  659. if (!info->kprobe_multi.count)
  660. return;
  661. if (info->kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN)
  662. printf("\n\tkretprobe.multi ");
  663. else
  664. printf("\n\tkprobe.multi ");
  665. printf("func_cnt %u ", info->kprobe_multi.count);
  666. if (info->kprobe_multi.missed)
  667. printf("missed %llu ", info->kprobe_multi.missed);
  668. data = get_addr_cookie_array(u64_to_ptr(info->kprobe_multi.addrs),
  669. u64_to_ptr(info->kprobe_multi.cookies),
  670. info->kprobe_multi.count);
  671. if (!data)
  672. return;
  673. /* Load it once for all. */
  674. if (!dd.sym_count)
  675. kernel_syms_load(&dd);
  676. if (!dd.sym_count)
  677. goto error;
  678. is_ibt_enabled = is_x86_ibt_enabled();
  679. printf("\n\t%-16s %-16s %s", "addr", "cookie", "func [module]");
  680. for (i = 0; i < dd.sym_count; i++) {
  681. if (!symbol_matches_target(dd.sym_mapping[i].address,
  682. data[j].addr, is_ibt_enabled))
  683. continue;
  684. printf("\n\t%016lx %-16llx %s",
  685. (unsigned long)data[j].addr, data[j].cookie, dd.sym_mapping[i].name);
  686. if (dd.sym_mapping[i].module[0] != '\0')
  687. printf(" [%s] ", dd.sym_mapping[i].module);
  688. else
  689. printf(" ");
  690. if (j++ == info->kprobe_multi.count)
  691. break;
  692. }
  693. error:
  694. free(data);
  695. }
  696. static void show_uprobe_multi_plain(struct bpf_link_info *info)
  697. {
  698. __u32 i;
  699. if (!info->uprobe_multi.count)
  700. return;
  701. if (info->uprobe_multi.flags & BPF_F_UPROBE_MULTI_RETURN)
  702. printf("\n\turetprobe.multi ");
  703. else
  704. printf("\n\tuprobe.multi ");
  705. printf("path %s ", (char *) u64_to_ptr(info->uprobe_multi.path));
  706. printf("func_cnt %u ", info->uprobe_multi.count);
  707. if (info->uprobe_multi.pid)
  708. printf("pid %u ", info->uprobe_multi.pid);
  709. printf("\n\t%-16s %-16s %-16s", "offset", "ref_ctr_offset", "cookies");
  710. for (i = 0; i < info->uprobe_multi.count; i++) {
  711. printf("\n\t0x%-16llx 0x%-16llx 0x%-16llx",
  712. u64_to_arr(info->uprobe_multi.offsets)[i],
  713. u64_to_arr(info->uprobe_multi.ref_ctr_offsets)[i],
  714. u64_to_arr(info->uprobe_multi.cookies)[i]);
  715. }
  716. }
  717. static void show_perf_event_kprobe_plain(struct bpf_link_info *info)
  718. {
  719. const char *buf;
  720. buf = u64_to_ptr(info->perf_event.kprobe.func_name);
  721. if (buf[0] == '\0' && !info->perf_event.kprobe.addr)
  722. return;
  723. if (info->perf_event.type == BPF_PERF_EVENT_KRETPROBE)
  724. printf("\n\tkretprobe ");
  725. else
  726. printf("\n\tkprobe ");
  727. if (info->perf_event.kprobe.addr)
  728. printf("%llx ", info->perf_event.kprobe.addr);
  729. printf("%s", buf);
  730. if (info->perf_event.kprobe.offset)
  731. printf("+%#x", info->perf_event.kprobe.offset);
  732. if (info->perf_event.kprobe.missed)
  733. printf(" missed %llu", info->perf_event.kprobe.missed);
  734. if (info->perf_event.kprobe.cookie)
  735. printf(" cookie %llu", info->perf_event.kprobe.cookie);
  736. printf(" ");
  737. }
  738. static void show_perf_event_uprobe_plain(struct bpf_link_info *info)
  739. {
  740. const char *buf;
  741. buf = u64_to_ptr(info->perf_event.uprobe.file_name);
  742. if (buf[0] == '\0')
  743. return;
  744. if (info->perf_event.type == BPF_PERF_EVENT_URETPROBE)
  745. printf("\n\turetprobe ");
  746. else
  747. printf("\n\tuprobe ");
  748. printf("%s+%#x ", buf, info->perf_event.uprobe.offset);
  749. if (info->perf_event.uprobe.cookie)
  750. printf("cookie %llu ", info->perf_event.uprobe.cookie);
  751. if (info->perf_event.uprobe.ref_ctr_offset)
  752. printf("ref_ctr_offset 0x%llx ", info->perf_event.uprobe.ref_ctr_offset);
  753. }
  754. static void show_perf_event_tracepoint_plain(struct bpf_link_info *info)
  755. {
  756. const char *buf;
  757. buf = u64_to_ptr(info->perf_event.tracepoint.tp_name);
  758. if (buf[0] == '\0')
  759. return;
  760. printf("\n\ttracepoint %s ", buf);
  761. if (info->perf_event.tracepoint.cookie)
  762. printf("cookie %llu ", info->perf_event.tracepoint.cookie);
  763. }
  764. static void show_perf_event_event_plain(struct bpf_link_info *info)
  765. {
  766. __u64 config = info->perf_event.event.config;
  767. __u32 type = info->perf_event.event.type;
  768. const char *perf_type, *perf_config;
  769. printf("\n\tevent ");
  770. perf_type = perf_event_name(perf_type_name, type);
  771. if (perf_type)
  772. printf("%s:", perf_type);
  773. else
  774. printf("%u :", type);
  775. perf_config = perf_config_str(type, config);
  776. if (perf_config)
  777. printf("%s ", perf_config);
  778. else
  779. printf("%llu ", config);
  780. if (info->perf_event.event.cookie)
  781. printf("cookie %llu ", info->perf_event.event.cookie);
  782. if (type == PERF_TYPE_HW_CACHE && perf_config)
  783. free((void *)perf_config);
  784. }
  785. static int show_link_close_plain(int fd, struct bpf_link_info *info)
  786. {
  787. struct bpf_prog_info prog_info;
  788. const char *prog_type_str;
  789. int err;
  790. show_link_header_plain(info);
  791. switch (info->type) {
  792. case BPF_LINK_TYPE_RAW_TRACEPOINT:
  793. printf("\n\ttp '%s' ",
  794. (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
  795. if (info->raw_tracepoint.cookie)
  796. printf("cookie %llu ", info->raw_tracepoint.cookie);
  797. break;
  798. case BPF_LINK_TYPE_TRACING:
  799. err = get_prog_info(info->prog_id, &prog_info);
  800. if (err)
  801. return err;
  802. prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
  803. /* libbpf will return NULL for variants unknown to it. */
  804. if (prog_type_str)
  805. printf("\n\tprog_type %s ", prog_type_str);
  806. else
  807. printf("\n\tprog_type %u ", prog_info.type);
  808. show_link_attach_type_plain(info->tracing.attach_type);
  809. if (info->tracing.target_obj_id || info->tracing.target_btf_id)
  810. printf("\n\ttarget_obj_id %u target_btf_id %u ",
  811. info->tracing.target_obj_id,
  812. info->tracing.target_btf_id);
  813. if (info->tracing.cookie)
  814. printf("\n\tcookie %llu ", info->tracing.cookie);
  815. break;
  816. case BPF_LINK_TYPE_CGROUP:
  817. printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
  818. show_link_attach_type_plain(info->cgroup.attach_type);
  819. break;
  820. case BPF_LINK_TYPE_ITER:
  821. show_iter_plain(info);
  822. break;
  823. case BPF_LINK_TYPE_NETNS:
  824. printf("\n\tnetns_ino %u ", info->netns.netns_ino);
  825. show_link_attach_type_plain(info->netns.attach_type);
  826. break;
  827. case BPF_LINK_TYPE_NETFILTER:
  828. netfilter_dump_plain(info);
  829. break;
  830. case BPF_LINK_TYPE_TCX:
  831. printf("\n\t");
  832. show_link_ifindex_plain(info->tcx.ifindex);
  833. show_link_attach_type_plain(info->tcx.attach_type);
  834. break;
  835. case BPF_LINK_TYPE_NETKIT:
  836. printf("\n\t");
  837. show_link_ifindex_plain(info->netkit.ifindex);
  838. show_link_attach_type_plain(info->netkit.attach_type);
  839. break;
  840. case BPF_LINK_TYPE_SOCKMAP:
  841. printf("\n\t");
  842. printf("map_id %u ", info->sockmap.map_id);
  843. show_link_attach_type_plain(info->sockmap.attach_type);
  844. break;
  845. case BPF_LINK_TYPE_XDP:
  846. printf("\n\t");
  847. show_link_ifindex_plain(info->xdp.ifindex);
  848. break;
  849. case BPF_LINK_TYPE_KPROBE_MULTI:
  850. show_kprobe_multi_plain(info);
  851. break;
  852. case BPF_LINK_TYPE_UPROBE_MULTI:
  853. show_uprobe_multi_plain(info);
  854. break;
  855. case BPF_LINK_TYPE_PERF_EVENT:
  856. switch (info->perf_event.type) {
  857. case BPF_PERF_EVENT_EVENT:
  858. show_perf_event_event_plain(info);
  859. break;
  860. case BPF_PERF_EVENT_TRACEPOINT:
  861. show_perf_event_tracepoint_plain(info);
  862. break;
  863. case BPF_PERF_EVENT_KPROBE:
  864. case BPF_PERF_EVENT_KRETPROBE:
  865. show_perf_event_kprobe_plain(info);
  866. break;
  867. case BPF_PERF_EVENT_UPROBE:
  868. case BPF_PERF_EVENT_URETPROBE:
  869. show_perf_event_uprobe_plain(info);
  870. break;
  871. default:
  872. break;
  873. }
  874. break;
  875. default:
  876. break;
  877. }
  878. if (!hashmap__empty(link_table)) {
  879. struct hashmap_entry *entry;
  880. hashmap__for_each_key_entry(link_table, entry, info->id)
  881. printf("\n\tpinned %s", (char *)entry->pvalue);
  882. }
  883. emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
  884. printf("\n");
  885. return 0;
  886. }
  887. static int do_show_link(int fd)
  888. {
  889. __u64 *ref_ctr_offsets = NULL, *offsets = NULL, *cookies = NULL;
  890. struct bpf_link_info info;
  891. __u32 len = sizeof(info);
  892. char path_buf[PATH_MAX];
  893. __u64 *addrs = NULL;
  894. char buf[PATH_MAX];
  895. int count;
  896. int err;
  897. memset(&info, 0, sizeof(info));
  898. buf[0] = '\0';
  899. again:
  900. err = bpf_link_get_info_by_fd(fd, &info, &len);
  901. if (err) {
  902. p_err("can't get link info: %s",
  903. strerror(errno));
  904. close(fd);
  905. return err;
  906. }
  907. if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
  908. !info.raw_tracepoint.tp_name) {
  909. info.raw_tracepoint.tp_name = ptr_to_u64(&buf);
  910. info.raw_tracepoint.tp_name_len = sizeof(buf);
  911. goto again;
  912. }
  913. if (info.type == BPF_LINK_TYPE_ITER &&
  914. !info.iter.target_name) {
  915. info.iter.target_name = ptr_to_u64(&buf);
  916. info.iter.target_name_len = sizeof(buf);
  917. goto again;
  918. }
  919. if (info.type == BPF_LINK_TYPE_KPROBE_MULTI &&
  920. !info.kprobe_multi.addrs) {
  921. count = info.kprobe_multi.count;
  922. if (count) {
  923. addrs = calloc(count, sizeof(__u64));
  924. if (!addrs) {
  925. p_err("mem alloc failed");
  926. close(fd);
  927. return -ENOMEM;
  928. }
  929. info.kprobe_multi.addrs = ptr_to_u64(addrs);
  930. cookies = calloc(count, sizeof(__u64));
  931. if (!cookies) {
  932. p_err("mem alloc failed");
  933. free(addrs);
  934. close(fd);
  935. return -ENOMEM;
  936. }
  937. info.kprobe_multi.cookies = ptr_to_u64(cookies);
  938. goto again;
  939. }
  940. }
  941. if (info.type == BPF_LINK_TYPE_UPROBE_MULTI &&
  942. !info.uprobe_multi.offsets) {
  943. count = info.uprobe_multi.count;
  944. if (count) {
  945. offsets = calloc(count, sizeof(__u64));
  946. if (!offsets) {
  947. p_err("mem alloc failed");
  948. close(fd);
  949. return -ENOMEM;
  950. }
  951. info.uprobe_multi.offsets = ptr_to_u64(offsets);
  952. ref_ctr_offsets = calloc(count, sizeof(__u64));
  953. if (!ref_ctr_offsets) {
  954. p_err("mem alloc failed");
  955. free(offsets);
  956. close(fd);
  957. return -ENOMEM;
  958. }
  959. info.uprobe_multi.ref_ctr_offsets = ptr_to_u64(ref_ctr_offsets);
  960. cookies = calloc(count, sizeof(__u64));
  961. if (!cookies) {
  962. p_err("mem alloc failed");
  963. free(ref_ctr_offsets);
  964. free(offsets);
  965. close(fd);
  966. return -ENOMEM;
  967. }
  968. info.uprobe_multi.cookies = ptr_to_u64(cookies);
  969. info.uprobe_multi.path = ptr_to_u64(path_buf);
  970. info.uprobe_multi.path_size = sizeof(path_buf);
  971. goto again;
  972. }
  973. }
  974. if (info.type == BPF_LINK_TYPE_PERF_EVENT) {
  975. switch (info.perf_event.type) {
  976. case BPF_PERF_EVENT_TRACEPOINT:
  977. if (!info.perf_event.tracepoint.tp_name) {
  978. info.perf_event.tracepoint.tp_name = ptr_to_u64(&buf);
  979. info.perf_event.tracepoint.name_len = sizeof(buf);
  980. goto again;
  981. }
  982. break;
  983. case BPF_PERF_EVENT_KPROBE:
  984. case BPF_PERF_EVENT_KRETPROBE:
  985. if (!info.perf_event.kprobe.func_name) {
  986. info.perf_event.kprobe.func_name = ptr_to_u64(&buf);
  987. info.perf_event.kprobe.name_len = sizeof(buf);
  988. goto again;
  989. }
  990. break;
  991. case BPF_PERF_EVENT_UPROBE:
  992. case BPF_PERF_EVENT_URETPROBE:
  993. if (!info.perf_event.uprobe.file_name) {
  994. info.perf_event.uprobe.file_name = ptr_to_u64(&buf);
  995. info.perf_event.uprobe.name_len = sizeof(buf);
  996. goto again;
  997. }
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. }
  1003. if (json_output)
  1004. show_link_close_json(fd, &info);
  1005. else
  1006. show_link_close_plain(fd, &info);
  1007. free(ref_ctr_offsets);
  1008. free(cookies);
  1009. free(offsets);
  1010. free(addrs);
  1011. close(fd);
  1012. return 0;
  1013. }
  1014. static int do_show(int argc, char **argv)
  1015. {
  1016. __u32 id = 0;
  1017. int err, fd;
  1018. if (show_pinned) {
  1019. link_table = hashmap__new(hash_fn_for_key_as_id,
  1020. equal_fn_for_key_as_id, NULL);
  1021. if (IS_ERR(link_table)) {
  1022. p_err("failed to create hashmap for pinned paths");
  1023. return -1;
  1024. }
  1025. build_pinned_obj_table(link_table, BPF_OBJ_LINK);
  1026. }
  1027. build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
  1028. if (argc == 2) {
  1029. fd = link_parse_fd(&argc, &argv);
  1030. if (fd < 0)
  1031. return fd;
  1032. do_show_link(fd);
  1033. goto out;
  1034. }
  1035. if (argc)
  1036. return BAD_ARG();
  1037. if (json_output)
  1038. jsonw_start_array(json_wtr);
  1039. while (true) {
  1040. err = bpf_link_get_next_id(id, &id);
  1041. if (err) {
  1042. if (errno == ENOENT)
  1043. break;
  1044. p_err("can't get next link: %s%s", strerror(errno),
  1045. errno == EINVAL ? " -- kernel too old?" : "");
  1046. break;
  1047. }
  1048. fd = bpf_link_get_fd_by_id(id);
  1049. if (fd < 0) {
  1050. if (errno == ENOENT)
  1051. continue;
  1052. p_err("can't get link by id (%u): %s",
  1053. id, strerror(errno));
  1054. break;
  1055. }
  1056. err = do_show_link(fd);
  1057. if (err)
  1058. break;
  1059. }
  1060. if (json_output)
  1061. jsonw_end_array(json_wtr);
  1062. delete_obj_refs_table(refs_table);
  1063. if (show_pinned)
  1064. delete_pinned_obj_table(link_table);
  1065. out:
  1066. if (dd.sym_count)
  1067. kernel_syms_destroy(&dd);
  1068. return errno == ENOENT ? 0 : -1;
  1069. }
  1070. static int do_pin(int argc, char **argv)
  1071. {
  1072. int err;
  1073. err = do_pin_any(argc, argv, link_parse_fd);
  1074. if (!err && json_output)
  1075. jsonw_null(json_wtr);
  1076. return err;
  1077. }
  1078. static int do_detach(int argc, char **argv)
  1079. {
  1080. int err, fd;
  1081. if (argc != 2) {
  1082. p_err("link specifier is invalid or missing\n");
  1083. return 1;
  1084. }
  1085. fd = link_parse_fd(&argc, &argv);
  1086. if (fd < 0)
  1087. return 1;
  1088. err = bpf_link_detach(fd);
  1089. if (err)
  1090. err = -errno;
  1091. close(fd);
  1092. if (err) {
  1093. p_err("failed link detach: %s", strerror(-err));
  1094. return 1;
  1095. }
  1096. if (json_output)
  1097. jsonw_null(json_wtr);
  1098. return 0;
  1099. }
  1100. static int do_help(int argc, char **argv)
  1101. {
  1102. if (json_output) {
  1103. jsonw_null(json_wtr);
  1104. return 0;
  1105. }
  1106. fprintf(stderr,
  1107. "Usage: %1$s %2$s { show | list } [LINK]\n"
  1108. " %1$s %2$s pin LINK FILE\n"
  1109. " %1$s %2$s detach LINK\n"
  1110. " %1$s %2$s help\n"
  1111. "\n"
  1112. " " HELP_SPEC_LINK "\n"
  1113. " " HELP_SPEC_OPTIONS " |\n"
  1114. " {-f|--bpffs} | {-n|--nomount} }\n"
  1115. "",
  1116. bin_name, argv[-2]);
  1117. return 0;
  1118. }
  1119. static const struct cmd cmds[] = {
  1120. { "show", do_show },
  1121. { "list", do_show },
  1122. { "help", do_help },
  1123. { "pin", do_pin },
  1124. { "detach", do_detach },
  1125. { 0 }
  1126. };
  1127. int do_link(int argc, char **argv)
  1128. {
  1129. return cmd_select(cmds, argc, argv, do_help);
  1130. }