1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
11 #include <bpf/hashmap.h>
13 #include "json_writer.h"
16 static const char * const link_type_name[] = {
17 [BPF_LINK_TYPE_UNSPEC] = "unspec",
18 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
19 [BPF_LINK_TYPE_TRACING] = "tracing",
20 [BPF_LINK_TYPE_CGROUP] = "cgroup",
21 [BPF_LINK_TYPE_ITER] = "iter",
22 [BPF_LINK_TYPE_NETNS] = "netns",
23 [BPF_LINK_TYPE_XDP] = "xdp",
24 [BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
25 [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
26 [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
29 static struct hashmap *link_table;
31 static int link_parse_fd(int *argc, char ***argv)
35 if (is_prefix(**argv, "id")) {
41 id = strtoul(**argv, &endptr, 0);
43 p_err("can't parse %s as ID", **argv);
48 fd = bpf_link_get_fd_by_id(id);
50 p_err("failed to get link with ID %d: %s", id, strerror(errno));
52 } else if (is_prefix(**argv, "pinned")) {
60 return open_obj_pinned_any(path, BPF_OBJ_LINK);
63 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
68 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
70 jsonw_uint_field(wtr, "id", info->id);
71 if (info->type < ARRAY_SIZE(link_type_name))
72 jsonw_string_field(wtr, "type", link_type_name[info->type]);
74 jsonw_uint_field(wtr, "type", info->type);
76 jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
79 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
81 const char *attach_type_str;
83 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
85 jsonw_string_field(wtr, "attach_type", attach_type_str);
87 jsonw_uint_field(wtr, "attach_type", attach_type);
90 static bool is_iter_map_target(const char *target_name)
92 return strcmp(target_name, "bpf_map_elem") == 0 ||
93 strcmp(target_name, "bpf_sk_storage_map") == 0;
96 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
98 const char *target_name = u64_to_ptr(info->iter.target_name);
100 jsonw_string_field(wtr, "target_name", target_name);
102 if (is_iter_map_target(target_name))
103 jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
106 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
108 __u32 len = sizeof(*info);
111 prog_fd = bpf_prog_get_fd_by_id(prog_id);
115 memset(info, 0, sizeof(*info));
116 err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
118 p_err("can't get prog info: %s", strerror(errno));
123 static int show_link_close_json(int fd, struct bpf_link_info *info)
125 struct bpf_prog_info prog_info;
126 const char *prog_type_str;
129 jsonw_start_object(json_wtr);
131 show_link_header_json(info, json_wtr);
133 switch (info->type) {
134 case BPF_LINK_TYPE_RAW_TRACEPOINT:
135 jsonw_string_field(json_wtr, "tp_name",
136 u64_to_ptr(info->raw_tracepoint.tp_name));
138 case BPF_LINK_TYPE_TRACING:
139 err = get_prog_info(info->prog_id, &prog_info);
143 prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
144 /* libbpf will return NULL for variants unknown to it. */
146 jsonw_string_field(json_wtr, "prog_type", prog_type_str);
148 jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
150 show_link_attach_type_json(info->tracing.attach_type,
153 case BPF_LINK_TYPE_CGROUP:
154 jsonw_lluint_field(json_wtr, "cgroup_id",
155 info->cgroup.cgroup_id);
156 show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
158 case BPF_LINK_TYPE_ITER:
159 show_iter_json(info, json_wtr);
161 case BPF_LINK_TYPE_NETNS:
162 jsonw_uint_field(json_wtr, "netns_ino",
163 info->netns.netns_ino);
164 show_link_attach_type_json(info->netns.attach_type, json_wtr);
170 if (!hashmap__empty(link_table)) {
171 struct hashmap_entry *entry;
173 jsonw_name(json_wtr, "pinned");
174 jsonw_start_array(json_wtr);
175 hashmap__for_each_key_entry(link_table, entry,
176 u32_as_hash_field(info->id))
177 jsonw_string(json_wtr, entry->value);
178 jsonw_end_array(json_wtr);
181 emit_obj_refs_json(refs_table, info->id, json_wtr);
183 jsonw_end_object(json_wtr);
188 static void show_link_header_plain(struct bpf_link_info *info)
190 printf("%u: ", info->id);
191 if (info->type < ARRAY_SIZE(link_type_name))
192 printf("%s ", link_type_name[info->type]);
194 printf("type %u ", info->type);
196 printf("prog %u ", info->prog_id);
199 static void show_link_attach_type_plain(__u32 attach_type)
201 const char *attach_type_str;
203 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
205 printf("attach_type %s ", attach_type_str);
207 printf("attach_type %u ", attach_type);
210 static void show_iter_plain(struct bpf_link_info *info)
212 const char *target_name = u64_to_ptr(info->iter.target_name);
214 printf("target_name %s ", target_name);
216 if (is_iter_map_target(target_name))
217 printf("map_id %u ", info->iter.map.map_id);
220 static int show_link_close_plain(int fd, struct bpf_link_info *info)
222 struct bpf_prog_info prog_info;
223 const char *prog_type_str;
226 show_link_header_plain(info);
228 switch (info->type) {
229 case BPF_LINK_TYPE_RAW_TRACEPOINT:
230 printf("\n\ttp '%s' ",
231 (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
233 case BPF_LINK_TYPE_TRACING:
234 err = get_prog_info(info->prog_id, &prog_info);
238 prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
239 /* libbpf will return NULL for variants unknown to it. */
241 printf("\n\tprog_type %s ", prog_type_str);
243 printf("\n\tprog_type %u ", prog_info.type);
245 show_link_attach_type_plain(info->tracing.attach_type);
247 case BPF_LINK_TYPE_CGROUP:
248 printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
249 show_link_attach_type_plain(info->cgroup.attach_type);
251 case BPF_LINK_TYPE_ITER:
252 show_iter_plain(info);
254 case BPF_LINK_TYPE_NETNS:
255 printf("\n\tnetns_ino %u ", info->netns.netns_ino);
256 show_link_attach_type_plain(info->netns.attach_type);
262 if (!hashmap__empty(link_table)) {
263 struct hashmap_entry *entry;
265 hashmap__for_each_key_entry(link_table, entry,
266 u32_as_hash_field(info->id))
267 printf("\n\tpinned %s", (char *)entry->value);
269 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
276 static int do_show_link(int fd)
278 struct bpf_link_info info;
279 __u32 len = sizeof(info);
283 memset(&info, 0, sizeof(info));
285 err = bpf_obj_get_info_by_fd(fd, &info, &len);
287 p_err("can't get link info: %s",
292 if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
293 !info.raw_tracepoint.tp_name) {
294 info.raw_tracepoint.tp_name = (unsigned long)&buf;
295 info.raw_tracepoint.tp_name_len = sizeof(buf);
298 if (info.type == BPF_LINK_TYPE_ITER &&
299 !info.iter.target_name) {
300 info.iter.target_name = (unsigned long)&buf;
301 info.iter.target_name_len = sizeof(buf);
306 show_link_close_json(fd, &info);
308 show_link_close_plain(fd, &info);
314 static int do_show(int argc, char **argv)
320 link_table = hashmap__new(hash_fn_for_key_as_id,
321 equal_fn_for_key_as_id, NULL);
322 if (IS_ERR(link_table)) {
323 p_err("failed to create hashmap for pinned paths");
326 build_pinned_obj_table(link_table, BPF_OBJ_LINK);
328 build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
331 fd = link_parse_fd(&argc, &argv);
334 return do_show_link(fd);
341 jsonw_start_array(json_wtr);
343 err = bpf_link_get_next_id(id, &id);
347 p_err("can't get next link: %s%s", strerror(errno),
348 errno == EINVAL ? " -- kernel too old?" : "");
352 fd = bpf_link_get_fd_by_id(id);
356 p_err("can't get link by id (%u): %s",
357 id, strerror(errno));
361 err = do_show_link(fd);
366 jsonw_end_array(json_wtr);
368 delete_obj_refs_table(refs_table);
371 delete_pinned_obj_table(link_table);
373 return errno == ENOENT ? 0 : -1;
376 static int do_pin(int argc, char **argv)
380 err = do_pin_any(argc, argv, link_parse_fd);
381 if (!err && json_output)
382 jsonw_null(json_wtr);
386 static int do_detach(int argc, char **argv)
391 p_err("link specifier is invalid or missing\n");
395 fd = link_parse_fd(&argc, &argv);
399 err = bpf_link_detach(fd);
404 p_err("failed link detach: %s", strerror(-err));
409 jsonw_null(json_wtr);
414 static int do_help(int argc, char **argv)
417 jsonw_null(json_wtr);
422 "Usage: %1$s %2$s { show | list } [LINK]\n"
423 " %1$s %2$s pin LINK FILE\n"
424 " %1$s %2$s detach LINK\n"
427 " " HELP_SPEC_LINK "\n"
428 " " HELP_SPEC_OPTIONS " |\n"
429 " {-f|--bpffs} | {-n|--nomount} }\n"
436 static const struct cmd cmds[] = {
441 { "detach", do_detach },
445 int do_link(int argc, char **argv)
447 return cmd_select(cmds, argc, argv, do_help);