xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision c9933d49)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
3 
4 #include <errno.h>
5 #include <linux/err.h>
6 #include <net/if.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 
10 #include <bpf/bpf.h>
11 #include <bpf/hashmap.h>
12 
13 #include "json_writer.h"
14 #include "main.h"
15 
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 };
27 
28 static struct hashmap *link_table;
29 
30 static int link_parse_fd(int *argc, char ***argv)
31 {
32 	int fd;
33 
34 	if (is_prefix(**argv, "id")) {
35 		unsigned int id;
36 		char *endptr;
37 
38 		NEXT_ARGP();
39 
40 		id = strtoul(**argv, &endptr, 0);
41 		if (*endptr) {
42 			p_err("can't parse %s as ID", **argv);
43 			return -1;
44 		}
45 		NEXT_ARGP();
46 
47 		fd = bpf_link_get_fd_by_id(id);
48 		if (fd < 0)
49 			p_err("failed to get link with ID %d: %s", id, strerror(errno));
50 		return fd;
51 	} else if (is_prefix(**argv, "pinned")) {
52 		char *path;
53 
54 		NEXT_ARGP();
55 
56 		path = **argv;
57 		NEXT_ARGP();
58 
59 		return open_obj_pinned_any(path, BPF_OBJ_LINK);
60 	}
61 
62 	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
63 	return -1;
64 }
65 
66 static void
67 show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
68 {
69 	jsonw_uint_field(wtr, "id", info->id);
70 	if (info->type < ARRAY_SIZE(link_type_name))
71 		jsonw_string_field(wtr, "type", link_type_name[info->type]);
72 	else
73 		jsonw_uint_field(wtr, "type", info->type);
74 
75 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
76 }
77 
78 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
79 {
80 	if (attach_type < ARRAY_SIZE(attach_type_name))
81 		jsonw_string_field(wtr, "attach_type",
82 				   attach_type_name[attach_type]);
83 	else
84 		jsonw_uint_field(wtr, "attach_type", attach_type);
85 }
86 
87 static bool is_iter_map_target(const char *target_name)
88 {
89 	return strcmp(target_name, "bpf_map_elem") == 0 ||
90 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
91 }
92 
93 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
94 {
95 	const char *target_name = u64_to_ptr(info->iter.target_name);
96 
97 	jsonw_string_field(wtr, "target_name", target_name);
98 
99 	if (is_iter_map_target(target_name))
100 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
101 }
102 
103 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
104 {
105 	__u32 len = sizeof(*info);
106 	int err, prog_fd;
107 
108 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
109 	if (prog_fd < 0)
110 		return prog_fd;
111 
112 	memset(info, 0, sizeof(*info));
113 	err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
114 	if (err)
115 		p_err("can't get prog info: %s", strerror(errno));
116 	close(prog_fd);
117 	return err;
118 }
119 
120 static int show_link_close_json(int fd, struct bpf_link_info *info)
121 {
122 	struct bpf_prog_info prog_info;
123 	int err;
124 
125 	jsonw_start_object(json_wtr);
126 
127 	show_link_header_json(info, json_wtr);
128 
129 	switch (info->type) {
130 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
131 		jsonw_string_field(json_wtr, "tp_name",
132 				   u64_to_ptr(info->raw_tracepoint.tp_name));
133 		break;
134 	case BPF_LINK_TYPE_TRACING:
135 		err = get_prog_info(info->prog_id, &prog_info);
136 		if (err)
137 			return err;
138 
139 		if (prog_info.type < prog_type_name_size)
140 			jsonw_string_field(json_wtr, "prog_type",
141 					   prog_type_name[prog_info.type]);
142 		else
143 			jsonw_uint_field(json_wtr, "prog_type",
144 					 prog_info.type);
145 
146 		show_link_attach_type_json(info->tracing.attach_type,
147 					   json_wtr);
148 		break;
149 	case BPF_LINK_TYPE_CGROUP:
150 		jsonw_lluint_field(json_wtr, "cgroup_id",
151 				   info->cgroup.cgroup_id);
152 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
153 		break;
154 	case BPF_LINK_TYPE_ITER:
155 		show_iter_json(info, json_wtr);
156 		break;
157 	case BPF_LINK_TYPE_NETNS:
158 		jsonw_uint_field(json_wtr, "netns_ino",
159 				 info->netns.netns_ino);
160 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
161 		break;
162 	default:
163 		break;
164 	}
165 
166 	if (!hashmap__empty(link_table)) {
167 		struct hashmap_entry *entry;
168 
169 		jsonw_name(json_wtr, "pinned");
170 		jsonw_start_array(json_wtr);
171 		hashmap__for_each_key_entry(link_table, entry,
172 					    u32_as_hash_field(info->id))
173 			jsonw_string(json_wtr, entry->value);
174 		jsonw_end_array(json_wtr);
175 	}
176 
177 	emit_obj_refs_json(refs_table, info->id, json_wtr);
178 
179 	jsonw_end_object(json_wtr);
180 
181 	return 0;
182 }
183 
184 static void show_link_header_plain(struct bpf_link_info *info)
185 {
186 	printf("%u: ", info->id);
187 	if (info->type < ARRAY_SIZE(link_type_name))
188 		printf("%s  ", link_type_name[info->type]);
189 	else
190 		printf("type %u  ", info->type);
191 
192 	printf("prog %u  ", info->prog_id);
193 }
194 
195 static void show_link_attach_type_plain(__u32 attach_type)
196 {
197 	if (attach_type < ARRAY_SIZE(attach_type_name))
198 		printf("attach_type %s  ", attach_type_name[attach_type]);
199 	else
200 		printf("attach_type %u  ", attach_type);
201 }
202 
203 static void show_iter_plain(struct bpf_link_info *info)
204 {
205 	const char *target_name = u64_to_ptr(info->iter.target_name);
206 
207 	printf("target_name %s  ", target_name);
208 
209 	if (is_iter_map_target(target_name))
210 		printf("map_id %u  ", info->iter.map.map_id);
211 }
212 
213 static int show_link_close_plain(int fd, struct bpf_link_info *info)
214 {
215 	struct bpf_prog_info prog_info;
216 	int err;
217 
218 	show_link_header_plain(info);
219 
220 	switch (info->type) {
221 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
222 		printf("\n\ttp '%s'  ",
223 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
224 		break;
225 	case BPF_LINK_TYPE_TRACING:
226 		err = get_prog_info(info->prog_id, &prog_info);
227 		if (err)
228 			return err;
229 
230 		if (prog_info.type < prog_type_name_size)
231 			printf("\n\tprog_type %s  ",
232 			       prog_type_name[prog_info.type]);
233 		else
234 			printf("\n\tprog_type %u  ", prog_info.type);
235 
236 		show_link_attach_type_plain(info->tracing.attach_type);
237 		break;
238 	case BPF_LINK_TYPE_CGROUP:
239 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
240 		show_link_attach_type_plain(info->cgroup.attach_type);
241 		break;
242 	case BPF_LINK_TYPE_ITER:
243 		show_iter_plain(info);
244 		break;
245 	case BPF_LINK_TYPE_NETNS:
246 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
247 		show_link_attach_type_plain(info->netns.attach_type);
248 		break;
249 	default:
250 		break;
251 	}
252 
253 	if (!hashmap__empty(link_table)) {
254 		struct hashmap_entry *entry;
255 
256 		hashmap__for_each_key_entry(link_table, entry,
257 					    u32_as_hash_field(info->id))
258 			printf("\n\tpinned %s", (char *)entry->value);
259 	}
260 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
261 
262 	printf("\n");
263 
264 	return 0;
265 }
266 
267 static int do_show_link(int fd)
268 {
269 	struct bpf_link_info info;
270 	__u32 len = sizeof(info);
271 	char buf[256];
272 	int err;
273 
274 	memset(&info, 0, sizeof(info));
275 again:
276 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
277 	if (err) {
278 		p_err("can't get link info: %s",
279 		      strerror(errno));
280 		close(fd);
281 		return err;
282 	}
283 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
284 	    !info.raw_tracepoint.tp_name) {
285 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
286 		info.raw_tracepoint.tp_name_len = sizeof(buf);
287 		goto again;
288 	}
289 	if (info.type == BPF_LINK_TYPE_ITER &&
290 	    !info.iter.target_name) {
291 		info.iter.target_name = (unsigned long)&buf;
292 		info.iter.target_name_len = sizeof(buf);
293 		goto again;
294 	}
295 
296 	if (json_output)
297 		show_link_close_json(fd, &info);
298 	else
299 		show_link_close_plain(fd, &info);
300 
301 	close(fd);
302 	return 0;
303 }
304 
305 static int do_show(int argc, char **argv)
306 {
307 	__u32 id = 0;
308 	int err, fd;
309 
310 	if (show_pinned) {
311 		link_table = hashmap__new(hash_fn_for_key_as_id,
312 					  equal_fn_for_key_as_id, NULL);
313 		if (IS_ERR(link_table)) {
314 			p_err("failed to create hashmap for pinned paths");
315 			return -1;
316 		}
317 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
318 	}
319 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
320 
321 	if (argc == 2) {
322 		fd = link_parse_fd(&argc, &argv);
323 		if (fd < 0)
324 			return fd;
325 		return do_show_link(fd);
326 	}
327 
328 	if (argc)
329 		return BAD_ARG();
330 
331 	if (json_output)
332 		jsonw_start_array(json_wtr);
333 	while (true) {
334 		err = bpf_link_get_next_id(id, &id);
335 		if (err) {
336 			if (errno == ENOENT)
337 				break;
338 			p_err("can't get next link: %s%s", strerror(errno),
339 			      errno == EINVAL ? " -- kernel too old?" : "");
340 			break;
341 		}
342 
343 		fd = bpf_link_get_fd_by_id(id);
344 		if (fd < 0) {
345 			if (errno == ENOENT)
346 				continue;
347 			p_err("can't get link by id (%u): %s",
348 			      id, strerror(errno));
349 			break;
350 		}
351 
352 		err = do_show_link(fd);
353 		if (err)
354 			break;
355 	}
356 	if (json_output)
357 		jsonw_end_array(json_wtr);
358 
359 	delete_obj_refs_table(refs_table);
360 
361 	if (show_pinned)
362 		delete_pinned_obj_table(link_table);
363 
364 	return errno == ENOENT ? 0 : -1;
365 }
366 
367 static int do_pin(int argc, char **argv)
368 {
369 	int err;
370 
371 	err = do_pin_any(argc, argv, link_parse_fd);
372 	if (!err && json_output)
373 		jsonw_null(json_wtr);
374 	return err;
375 }
376 
377 static int do_detach(int argc, char **argv)
378 {
379 	int err, fd;
380 
381 	if (argc != 2) {
382 		p_err("link specifier is invalid or missing\n");
383 		return 1;
384 	}
385 
386 	fd = link_parse_fd(&argc, &argv);
387 	if (fd < 0)
388 		return 1;
389 
390 	err = bpf_link_detach(fd);
391 	if (err)
392 		err = -errno;
393 	close(fd);
394 	if (err) {
395 		p_err("failed link detach: %s", strerror(-err));
396 		return 1;
397 	}
398 
399 	if (json_output)
400 		jsonw_null(json_wtr);
401 
402 	return 0;
403 }
404 
405 static int do_help(int argc, char **argv)
406 {
407 	if (json_output) {
408 		jsonw_null(json_wtr);
409 		return 0;
410 	}
411 
412 	fprintf(stderr,
413 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
414 		"       %1$s %2$s pin        LINK  FILE\n"
415 		"       %1$s %2$s detach     LINK\n"
416 		"       %1$s %2$s help\n"
417 		"\n"
418 		"       " HELP_SPEC_LINK "\n"
419 		"       " HELP_SPEC_OPTIONS " |\n"
420 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
421 		"",
422 		bin_name, argv[-2]);
423 
424 	return 0;
425 }
426 
427 static const struct cmd cmds[] = {
428 	{ "show",	do_show },
429 	{ "list",	do_show },
430 	{ "help",	do_help },
431 	{ "pin",	do_pin },
432 	{ "detach",	do_detach },
433 	{ 0 }
434 };
435 
436 int do_link(int argc, char **argv)
437 {
438 	return cmd_select(cmds, argc, argv, do_help);
439 }
440