1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
10 #include <bpf/hashmap.h>
12 #include "json_writer.h"
15 static const char * const link_type_name[] = {
16 [BPF_LINK_TYPE_UNSPEC] = "unspec",
17 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
18 [BPF_LINK_TYPE_TRACING] = "tracing",
19 [BPF_LINK_TYPE_CGROUP] = "cgroup",
20 [BPF_LINK_TYPE_ITER] = "iter",
21 [BPF_LINK_TYPE_NETNS] = "netns",
24 static struct hashmap *link_table;
26 static int link_parse_fd(int *argc, char ***argv)
30 if (is_prefix(**argv, "id")) {
36 id = strtoul(**argv, &endptr, 0);
38 p_err("can't parse %s as ID", **argv);
43 fd = bpf_link_get_fd_by_id(id);
45 p_err("failed to get link with ID %d: %s", id, strerror(errno));
47 } else if (is_prefix(**argv, "pinned")) {
55 return open_obj_pinned_any(path, BPF_OBJ_LINK);
58 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
63 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
65 jsonw_uint_field(wtr, "id", info->id);
66 if (info->type < ARRAY_SIZE(link_type_name))
67 jsonw_string_field(wtr, "type", link_type_name[info->type]);
69 jsonw_uint_field(wtr, "type", info->type);
71 jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
74 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
76 if (attach_type < ARRAY_SIZE(attach_type_name))
77 jsonw_string_field(wtr, "attach_type",
78 attach_type_name[attach_type]);
80 jsonw_uint_field(wtr, "attach_type", attach_type);
83 static bool is_iter_map_target(const char *target_name)
85 return strcmp(target_name, "bpf_map_elem") == 0 ||
86 strcmp(target_name, "bpf_sk_storage_map") == 0;
89 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
91 const char *target_name = u64_to_ptr(info->iter.target_name);
93 jsonw_string_field(wtr, "target_name", target_name);
95 if (is_iter_map_target(target_name))
96 jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
99 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
101 __u32 len = sizeof(*info);
104 prog_fd = bpf_prog_get_fd_by_id(prog_id);
108 memset(info, 0, sizeof(*info));
109 err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
111 p_err("can't get prog info: %s", strerror(errno));
116 static int show_link_close_json(int fd, struct bpf_link_info *info)
118 struct bpf_prog_info prog_info;
121 jsonw_start_object(json_wtr);
123 show_link_header_json(info, json_wtr);
125 switch (info->type) {
126 case BPF_LINK_TYPE_RAW_TRACEPOINT:
127 jsonw_string_field(json_wtr, "tp_name",
128 u64_to_ptr(info->raw_tracepoint.tp_name));
130 case BPF_LINK_TYPE_TRACING:
131 err = get_prog_info(info->prog_id, &prog_info);
135 if (prog_info.type < prog_type_name_size)
136 jsonw_string_field(json_wtr, "prog_type",
137 prog_type_name[prog_info.type]);
139 jsonw_uint_field(json_wtr, "prog_type",
142 show_link_attach_type_json(info->tracing.attach_type,
145 case BPF_LINK_TYPE_CGROUP:
146 jsonw_lluint_field(json_wtr, "cgroup_id",
147 info->cgroup.cgroup_id);
148 show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
150 case BPF_LINK_TYPE_ITER:
151 show_iter_json(info, json_wtr);
153 case BPF_LINK_TYPE_NETNS:
154 jsonw_uint_field(json_wtr, "netns_ino",
155 info->netns.netns_ino);
156 show_link_attach_type_json(info->netns.attach_type, json_wtr);
162 if (!hashmap__empty(link_table)) {
163 struct hashmap_entry *entry;
165 jsonw_name(json_wtr, "pinned");
166 jsonw_start_array(json_wtr);
167 hashmap__for_each_key_entry(link_table, entry,
168 u32_as_hash_field(info->id))
169 jsonw_string(json_wtr, entry->value);
170 jsonw_end_array(json_wtr);
173 emit_obj_refs_json(refs_table, info->id, json_wtr);
175 jsonw_end_object(json_wtr);
180 static void show_link_header_plain(struct bpf_link_info *info)
182 printf("%u: ", info->id);
183 if (info->type < ARRAY_SIZE(link_type_name))
184 printf("%s ", link_type_name[info->type]);
186 printf("type %u ", info->type);
188 printf("prog %u ", info->prog_id);
191 static void show_link_attach_type_plain(__u32 attach_type)
193 if (attach_type < ARRAY_SIZE(attach_type_name))
194 printf("attach_type %s ", attach_type_name[attach_type]);
196 printf("attach_type %u ", attach_type);
199 static void show_iter_plain(struct bpf_link_info *info)
201 const char *target_name = u64_to_ptr(info->iter.target_name);
203 printf("target_name %s ", target_name);
205 if (is_iter_map_target(target_name))
206 printf("map_id %u ", info->iter.map.map_id);
209 static int show_link_close_plain(int fd, struct bpf_link_info *info)
211 struct bpf_prog_info prog_info;
214 show_link_header_plain(info);
216 switch (info->type) {
217 case BPF_LINK_TYPE_RAW_TRACEPOINT:
218 printf("\n\ttp '%s' ",
219 (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
221 case BPF_LINK_TYPE_TRACING:
222 err = get_prog_info(info->prog_id, &prog_info);
226 if (prog_info.type < prog_type_name_size)
227 printf("\n\tprog_type %s ",
228 prog_type_name[prog_info.type]);
230 printf("\n\tprog_type %u ", prog_info.type);
232 show_link_attach_type_plain(info->tracing.attach_type);
234 case BPF_LINK_TYPE_CGROUP:
235 printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
236 show_link_attach_type_plain(info->cgroup.attach_type);
238 case BPF_LINK_TYPE_ITER:
239 show_iter_plain(info);
241 case BPF_LINK_TYPE_NETNS:
242 printf("\n\tnetns_ino %u ", info->netns.netns_ino);
243 show_link_attach_type_plain(info->netns.attach_type);
249 if (!hashmap__empty(link_table)) {
250 struct hashmap_entry *entry;
252 hashmap__for_each_key_entry(link_table, entry,
253 u32_as_hash_field(info->id))
254 printf("\n\tpinned %s", (char *)entry->value);
256 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
263 static int do_show_link(int fd)
265 struct bpf_link_info info;
266 __u32 len = sizeof(info);
270 memset(&info, 0, sizeof(info));
272 err = bpf_obj_get_info_by_fd(fd, &info, &len);
274 p_err("can't get link info: %s",
279 if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
280 !info.raw_tracepoint.tp_name) {
281 info.raw_tracepoint.tp_name = (unsigned long)&buf;
282 info.raw_tracepoint.tp_name_len = sizeof(buf);
285 if (info.type == BPF_LINK_TYPE_ITER &&
286 !info.iter.target_name) {
287 info.iter.target_name = (unsigned long)&buf;
288 info.iter.target_name_len = sizeof(buf);
293 show_link_close_json(fd, &info);
295 show_link_close_plain(fd, &info);
301 static int do_show(int argc, char **argv)
307 link_table = hashmap__new(hash_fn_for_key_as_id,
308 equal_fn_for_key_as_id, NULL);
310 p_err("failed to create hashmap for pinned paths");
313 build_pinned_obj_table(link_table, BPF_OBJ_LINK);
315 build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
318 fd = link_parse_fd(&argc, &argv);
321 return do_show_link(fd);
328 jsonw_start_array(json_wtr);
330 err = bpf_link_get_next_id(id, &id);
334 p_err("can't get next link: %s%s", strerror(errno),
335 errno == EINVAL ? " -- kernel too old?" : "");
339 fd = bpf_link_get_fd_by_id(id);
343 p_err("can't get link by id (%u): %s",
344 id, strerror(errno));
348 err = do_show_link(fd);
353 jsonw_end_array(json_wtr);
355 delete_obj_refs_table(refs_table);
358 delete_pinned_obj_table(link_table);
360 return errno == ENOENT ? 0 : -1;
363 static int do_pin(int argc, char **argv)
367 err = do_pin_any(argc, argv, link_parse_fd);
368 if (!err && json_output)
369 jsonw_null(json_wtr);
373 static int do_detach(int argc, char **argv)
378 p_err("link specifier is invalid or missing\n");
382 fd = link_parse_fd(&argc, &argv);
386 err = bpf_link_detach(fd);
391 p_err("failed link detach: %s", strerror(-err));
396 jsonw_null(json_wtr);
401 static int do_help(int argc, char **argv)
404 jsonw_null(json_wtr);
409 "Usage: %1$s %2$s { show | list } [LINK]\n"
410 " %1$s %2$s pin LINK FILE\n"
411 " %1$s %2$s detach LINK\n"
414 " " HELP_SPEC_LINK "\n"
415 " " HELP_SPEC_OPTIONS " |\n"
416 " {-f|--bpffs} | {-n|--nomount} }\n"
423 static const struct cmd cmds[] = {
428 { "detach", do_detach },
432 int do_link(int argc, char **argv)
434 return cmd_select(cmds, argc, argv, do_help);