1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
11 #include "json_writer.h"
14 static const char * const link_type_name[] = {
15 [BPF_LINK_TYPE_UNSPEC] = "unspec",
16 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
17 [BPF_LINK_TYPE_TRACING] = "tracing",
18 [BPF_LINK_TYPE_CGROUP] = "cgroup",
19 [BPF_LINK_TYPE_ITER] = "iter",
20 [BPF_LINK_TYPE_NETNS] = "netns",
23 static int link_parse_fd(int *argc, char ***argv)
25 if (is_prefix(**argv, "id")) {
31 id = strtoul(**argv, &endptr, 0);
33 p_err("can't parse %s as ID", **argv);
38 return bpf_link_get_fd_by_id(id);
39 } else if (is_prefix(**argv, "pinned")) {
47 return open_obj_pinned_any(path, BPF_OBJ_LINK);
50 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
55 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
57 jsonw_uint_field(wtr, "id", info->id);
58 if (info->type < ARRAY_SIZE(link_type_name))
59 jsonw_string_field(wtr, "type", link_type_name[info->type]);
61 jsonw_uint_field(wtr, "type", info->type);
63 jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
66 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
68 if (attach_type < ARRAY_SIZE(attach_type_name))
69 jsonw_string_field(wtr, "attach_type",
70 attach_type_name[attach_type]);
72 jsonw_uint_field(wtr, "attach_type", attach_type);
75 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
77 __u32 len = sizeof(*info);
80 prog_fd = bpf_prog_get_fd_by_id(prog_id);
84 memset(info, 0, sizeof(*info));
85 err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
87 p_err("can't get prog info: %s", strerror(errno));
92 static int show_link_close_json(int fd, struct bpf_link_info *info)
94 struct bpf_prog_info prog_info;
97 jsonw_start_object(json_wtr);
99 show_link_header_json(info, json_wtr);
101 switch (info->type) {
102 case BPF_LINK_TYPE_RAW_TRACEPOINT:
103 jsonw_string_field(json_wtr, "tp_name",
104 (const char *)info->raw_tracepoint.tp_name);
106 case BPF_LINK_TYPE_TRACING:
107 err = get_prog_info(info->prog_id, &prog_info);
111 if (prog_info.type < ARRAY_SIZE(prog_type_name))
112 jsonw_string_field(json_wtr, "prog_type",
113 prog_type_name[prog_info.type]);
115 jsonw_uint_field(json_wtr, "prog_type",
118 show_link_attach_type_json(info->tracing.attach_type,
121 case BPF_LINK_TYPE_CGROUP:
122 jsonw_lluint_field(json_wtr, "cgroup_id",
123 info->cgroup.cgroup_id);
124 show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
126 case BPF_LINK_TYPE_NETNS:
127 jsonw_uint_field(json_wtr, "netns_ino",
128 info->netns.netns_ino);
129 show_link_attach_type_json(info->netns.attach_type, json_wtr);
135 if (!hash_empty(link_table.table)) {
136 struct pinned_obj *obj;
138 jsonw_name(json_wtr, "pinned");
139 jsonw_start_array(json_wtr);
140 hash_for_each_possible(link_table.table, obj, hash, info->id) {
141 if (obj->id == info->id)
142 jsonw_string(json_wtr, obj->path);
144 jsonw_end_array(json_wtr);
146 jsonw_end_object(json_wtr);
151 static void show_link_header_plain(struct bpf_link_info *info)
153 printf("%u: ", info->id);
154 if (info->type < ARRAY_SIZE(link_type_name))
155 printf("%s ", link_type_name[info->type]);
157 printf("type %u ", info->type);
159 printf("prog %u ", info->prog_id);
162 static void show_link_attach_type_plain(__u32 attach_type)
164 if (attach_type < ARRAY_SIZE(attach_type_name))
165 printf("attach_type %s ", attach_type_name[attach_type]);
167 printf("attach_type %u ", attach_type);
170 static int show_link_close_plain(int fd, struct bpf_link_info *info)
172 struct bpf_prog_info prog_info;
175 show_link_header_plain(info);
177 switch (info->type) {
178 case BPF_LINK_TYPE_RAW_TRACEPOINT:
179 printf("\n\ttp '%s' ",
180 (const char *)info->raw_tracepoint.tp_name);
182 case BPF_LINK_TYPE_TRACING:
183 err = get_prog_info(info->prog_id, &prog_info);
187 if (prog_info.type < ARRAY_SIZE(prog_type_name))
188 printf("\n\tprog_type %s ",
189 prog_type_name[prog_info.type]);
191 printf("\n\tprog_type %u ", prog_info.type);
193 show_link_attach_type_plain(info->tracing.attach_type);
195 case BPF_LINK_TYPE_CGROUP:
196 printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
197 show_link_attach_type_plain(info->cgroup.attach_type);
199 case BPF_LINK_TYPE_NETNS:
200 printf("\n\tnetns_ino %u ", info->netns.netns_ino);
201 show_link_attach_type_plain(info->netns.attach_type);
207 if (!hash_empty(link_table.table)) {
208 struct pinned_obj *obj;
210 hash_for_each_possible(link_table.table, obj, hash, info->id) {
211 if (obj->id == info->id)
212 printf("\n\tpinned %s", obj->path);
221 static int do_show_link(int fd)
223 struct bpf_link_info info;
224 __u32 len = sizeof(info);
225 char raw_tp_name[256];
228 memset(&info, 0, sizeof(info));
230 err = bpf_obj_get_info_by_fd(fd, &info, &len);
232 p_err("can't get link info: %s",
237 if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
238 !info.raw_tracepoint.tp_name) {
239 info.raw_tracepoint.tp_name = (unsigned long)&raw_tp_name;
240 info.raw_tracepoint.tp_name_len = sizeof(raw_tp_name);
245 show_link_close_json(fd, &info);
247 show_link_close_plain(fd, &info);
253 static int do_show(int argc, char **argv)
259 build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
262 fd = link_parse_fd(&argc, &argv);
265 return do_show_link(fd);
272 jsonw_start_array(json_wtr);
274 err = bpf_link_get_next_id(id, &id);
278 p_err("can't get next link: %s%s", strerror(errno),
279 errno == EINVAL ? " -- kernel too old?" : "");
283 fd = bpf_link_get_fd_by_id(id);
287 p_err("can't get link by id (%u): %s",
288 id, strerror(errno));
292 err = do_show_link(fd);
297 jsonw_end_array(json_wtr);
299 return errno == ENOENT ? 0 : -1;
302 static int do_pin(int argc, char **argv)
306 err = do_pin_any(argc, argv, link_parse_fd);
307 if (!err && json_output)
308 jsonw_null(json_wtr);
312 static int do_help(int argc, char **argv)
315 jsonw_null(json_wtr);
320 "Usage: %1$s %2$s { show | list } [LINK]\n"
321 " %1$s %2$s pin LINK FILE\n"
324 " " HELP_SPEC_LINK "\n"
325 " " HELP_SPEC_OPTIONS "\n"
332 static const struct cmd cmds[] = {
340 int do_link(int argc, char **argv)
342 return cmd_select(cmds, argc, argv, do_help);