xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision b8265621)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
3 
4 #include <errno.h>
5 #include <net/if.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 
9 #include <bpf/bpf.h>
10 
11 #include "json_writer.h"
12 #include "main.h"
13 
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",
21 };
22 
23 static int link_parse_fd(int *argc, char ***argv)
24 {
25 	if (is_prefix(**argv, "id")) {
26 		unsigned int id;
27 		char *endptr;
28 
29 		NEXT_ARGP();
30 
31 		id = strtoul(**argv, &endptr, 0);
32 		if (*endptr) {
33 			p_err("can't parse %s as ID", **argv);
34 			return -1;
35 		}
36 		NEXT_ARGP();
37 
38 		return bpf_link_get_fd_by_id(id);
39 	} else if (is_prefix(**argv, "pinned")) {
40 		char *path;
41 
42 		NEXT_ARGP();
43 
44 		path = **argv;
45 		NEXT_ARGP();
46 
47 		return open_obj_pinned_any(path, BPF_OBJ_LINK);
48 	}
49 
50 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
51 	return -1;
52 }
53 
54 static void
55 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
56 {
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]);
60 	else
61 		jsonw_uint_field(wtr, "type", info->type);
62 
63 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
64 }
65 
66 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
67 {
68 	if (attach_type < ARRAY_SIZE(attach_type_name))
69 		jsonw_string_field(wtr, "attach_type",
70 				   attach_type_name[attach_type]);
71 	else
72 		jsonw_uint_field(wtr, "attach_type", attach_type);
73 }
74 
75 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
76 {
77 	__u32 len = sizeof(*info);
78 	int err, prog_fd;
79 
80 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
81 	if (prog_fd < 0)
82 		return prog_fd;
83 
84 	memset(info, 0, sizeof(*info));
85 	err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
86 	if (err)
87 		p_err("can't get prog info: %s", strerror(errno));
88 	close(prog_fd);
89 	return err;
90 }
91 
92 static int show_link_close_json(int fd, struct bpf_link_info *info)
93 {
94 	struct bpf_prog_info prog_info;
95 	int err;
96 
97 	jsonw_start_object(json_wtr);
98 
99 	show_link_header_json(info, json_wtr);
100 
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);
105 		break;
106 	case BPF_LINK_TYPE_TRACING:
107 		err = get_prog_info(info->prog_id, &prog_info);
108 		if (err)
109 			return err;
110 
111 		if (prog_info.type < prog_type_name_size)
112 			jsonw_string_field(json_wtr, "prog_type",
113 					   prog_type_name[prog_info.type]);
114 		else
115 			jsonw_uint_field(json_wtr, "prog_type",
116 					 prog_info.type);
117 
118 		show_link_attach_type_json(info->tracing.attach_type,
119 					   json_wtr);
120 		break;
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);
125 		break;
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);
130 		break;
131 	default:
132 		break;
133 	}
134 
135 	if (!hash_empty(link_table.table)) {
136 		struct pinned_obj *obj;
137 
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);
143 		}
144 		jsonw_end_array(json_wtr);
145 	}
146 
147 	emit_obj_refs_json(&refs_table, info->id, json_wtr);
148 
149 	jsonw_end_object(json_wtr);
150 
151 	return 0;
152 }
153 
154 static void show_link_header_plain(struct bpf_link_info *info)
155 {
156 	printf("%u: ", info->id);
157 	if (info->type < ARRAY_SIZE(link_type_name))
158 		printf("%s  ", link_type_name[info->type]);
159 	else
160 		printf("type %u  ", info->type);
161 
162 	printf("prog %u  ", info->prog_id);
163 }
164 
165 static void show_link_attach_type_plain(__u32 attach_type)
166 {
167 	if (attach_type < ARRAY_SIZE(attach_type_name))
168 		printf("attach_type %s  ", attach_type_name[attach_type]);
169 	else
170 		printf("attach_type %u  ", attach_type);
171 }
172 
173 static int show_link_close_plain(int fd, struct bpf_link_info *info)
174 {
175 	struct bpf_prog_info prog_info;
176 	int err;
177 
178 	show_link_header_plain(info);
179 
180 	switch (info->type) {
181 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
182 		printf("\n\ttp '%s'  ",
183 		       (const char *)info->raw_tracepoint.tp_name);
184 		break;
185 	case BPF_LINK_TYPE_TRACING:
186 		err = get_prog_info(info->prog_id, &prog_info);
187 		if (err)
188 			return err;
189 
190 		if (prog_info.type < prog_type_name_size)
191 			printf("\n\tprog_type %s  ",
192 			       prog_type_name[prog_info.type]);
193 		else
194 			printf("\n\tprog_type %u  ", prog_info.type);
195 
196 		show_link_attach_type_plain(info->tracing.attach_type);
197 		break;
198 	case BPF_LINK_TYPE_CGROUP:
199 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
200 		show_link_attach_type_plain(info->cgroup.attach_type);
201 		break;
202 	case BPF_LINK_TYPE_NETNS:
203 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
204 		show_link_attach_type_plain(info->netns.attach_type);
205 		break;
206 	default:
207 		break;
208 	}
209 
210 	if (!hash_empty(link_table.table)) {
211 		struct pinned_obj *obj;
212 
213 		hash_for_each_possible(link_table.table, obj, hash, info->id) {
214 			if (obj->id == info->id)
215 				printf("\n\tpinned %s", obj->path);
216 		}
217 	}
218 	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
219 
220 	printf("\n");
221 
222 	return 0;
223 }
224 
225 static int do_show_link(int fd)
226 {
227 	struct bpf_link_info info;
228 	__u32 len = sizeof(info);
229 	char raw_tp_name[256];
230 	int err;
231 
232 	memset(&info, 0, sizeof(info));
233 again:
234 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
235 	if (err) {
236 		p_err("can't get link info: %s",
237 		      strerror(errno));
238 		close(fd);
239 		return err;
240 	}
241 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
242 	    !info.raw_tracepoint.tp_name) {
243 		info.raw_tracepoint.tp_name = (unsigned long)&raw_tp_name;
244 		info.raw_tracepoint.tp_name_len = sizeof(raw_tp_name);
245 		goto again;
246 	}
247 
248 	if (json_output)
249 		show_link_close_json(fd, &info);
250 	else
251 		show_link_close_plain(fd, &info);
252 
253 	close(fd);
254 	return 0;
255 }
256 
257 static int do_show(int argc, char **argv)
258 {
259 	__u32 id = 0;
260 	int err, fd;
261 
262 	if (show_pinned)
263 		build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
264 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
265 
266 	if (argc == 2) {
267 		fd = link_parse_fd(&argc, &argv);
268 		if (fd < 0)
269 			return fd;
270 		return do_show_link(fd);
271 	}
272 
273 	if (argc)
274 		return BAD_ARG();
275 
276 	if (json_output)
277 		jsonw_start_array(json_wtr);
278 	while (true) {
279 		err = bpf_link_get_next_id(id, &id);
280 		if (err) {
281 			if (errno == ENOENT)
282 				break;
283 			p_err("can't get next link: %s%s", strerror(errno),
284 			      errno == EINVAL ? " -- kernel too old?" : "");
285 			break;
286 		}
287 
288 		fd = bpf_link_get_fd_by_id(id);
289 		if (fd < 0) {
290 			if (errno == ENOENT)
291 				continue;
292 			p_err("can't get link by id (%u): %s",
293 			      id, strerror(errno));
294 			break;
295 		}
296 
297 		err = do_show_link(fd);
298 		if (err)
299 			break;
300 	}
301 	if (json_output)
302 		jsonw_end_array(json_wtr);
303 
304 	delete_obj_refs_table(&refs_table);
305 
306 	return errno == ENOENT ? 0 : -1;
307 }
308 
309 static int do_pin(int argc, char **argv)
310 {
311 	int err;
312 
313 	err = do_pin_any(argc, argv, link_parse_fd);
314 	if (!err && json_output)
315 		jsonw_null(json_wtr);
316 	return err;
317 }
318 
319 static int do_help(int argc, char **argv)
320 {
321 	if (json_output) {
322 		jsonw_null(json_wtr);
323 		return 0;
324 	}
325 
326 	fprintf(stderr,
327 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
328 		"       %1$s %2$s pin        LINK  FILE\n"
329 		"       %1$s %2$s help\n"
330 		"\n"
331 		"       " HELP_SPEC_LINK "\n"
332 		"       " HELP_SPEC_OPTIONS "\n"
333 		"",
334 		bin_name, argv[-2]);
335 
336 	return 0;
337 }
338 
339 static const struct cmd cmds[] = {
340 	{ "show",	do_show },
341 	{ "list",	do_show },
342 	{ "help",	do_help },
343 	{ "pin",	do_pin },
344 	{ 0 }
345 };
346 
347 int do_link(int argc, char **argv)
348 {
349 	return cmd_select(cmds, argc, argv, do_help);
350 }
351