xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision 4a4c8482)
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 struct hashmap *link_table;
17 
18 static int link_parse_fd(int *argc, char ***argv)
19 {
20 	int fd;
21 
22 	if (is_prefix(**argv, "id")) {
23 		unsigned int id;
24 		char *endptr;
25 
26 		NEXT_ARGP();
27 
28 		id = strtoul(**argv, &endptr, 0);
29 		if (*endptr) {
30 			p_err("can't parse %s as ID", **argv);
31 			return -1;
32 		}
33 		NEXT_ARGP();
34 
35 		fd = bpf_link_get_fd_by_id(id);
36 		if (fd < 0)
37 			p_err("failed to get link with ID %d: %s", id, strerror(errno));
38 		return fd;
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 	const char *link_type_str;
58 
59 	jsonw_uint_field(wtr, "id", info->id);
60 	link_type_str = libbpf_bpf_link_type_str(info->type);
61 	if (link_type_str)
62 		jsonw_string_field(wtr, "type", link_type_str);
63 	else
64 		jsonw_uint_field(wtr, "type", info->type);
65 
66 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
67 }
68 
69 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
70 {
71 	const char *attach_type_str;
72 
73 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
74 	if (attach_type_str)
75 		jsonw_string_field(wtr, "attach_type", attach_type_str);
76 	else
77 		jsonw_uint_field(wtr, "attach_type", attach_type);
78 }
79 
80 static bool is_iter_map_target(const char *target_name)
81 {
82 	return strcmp(target_name, "bpf_map_elem") == 0 ||
83 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
84 }
85 
86 static bool is_iter_cgroup_target(const char *target_name)
87 {
88 	return strcmp(target_name, "cgroup") == 0;
89 }
90 
91 static const char *cgroup_order_string(__u32 order)
92 {
93 	switch (order) {
94 	case BPF_CGROUP_ITER_ORDER_UNSPEC:
95 		return "order_unspec";
96 	case BPF_CGROUP_ITER_SELF_ONLY:
97 		return "self_only";
98 	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
99 		return "descendants_pre";
100 	case BPF_CGROUP_ITER_DESCENDANTS_POST:
101 		return "descendants_post";
102 	case BPF_CGROUP_ITER_ANCESTORS_UP:
103 		return "ancestors_up";
104 	default: /* won't happen */
105 		return "unknown";
106 	}
107 }
108 
109 static bool is_iter_task_target(const char *target_name)
110 {
111 	return strcmp(target_name, "task") == 0 ||
112 		strcmp(target_name, "task_file") == 0 ||
113 		strcmp(target_name, "task_vma") == 0;
114 }
115 
116 static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
117 {
118 	const char *target_name = u64_to_ptr(info->iter.target_name);
119 
120 	jsonw_string_field(wtr, "target_name", target_name);
121 
122 	if (is_iter_map_target(target_name))
123 		jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
124 	else if (is_iter_task_target(target_name)) {
125 		if (info->iter.task.tid)
126 			jsonw_uint_field(wtr, "tid", info->iter.task.tid);
127 		else if (info->iter.task.pid)
128 			jsonw_uint_field(wtr, "pid", info->iter.task.pid);
129 	}
130 
131 	if (is_iter_cgroup_target(target_name)) {
132 		jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
133 		jsonw_string_field(wtr, "order",
134 				   cgroup_order_string(info->iter.cgroup.order));
135 	}
136 }
137 
138 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
139 {
140 	__u32 len = sizeof(*info);
141 	int err, prog_fd;
142 
143 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
144 	if (prog_fd < 0)
145 		return prog_fd;
146 
147 	memset(info, 0, sizeof(*info));
148 	err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
149 	if (err)
150 		p_err("can't get prog info: %s", strerror(errno));
151 	close(prog_fd);
152 	return err;
153 }
154 
155 static int show_link_close_json(int fd, struct bpf_link_info *info)
156 {
157 	struct bpf_prog_info prog_info;
158 	const char *prog_type_str;
159 	int err;
160 
161 	jsonw_start_object(json_wtr);
162 
163 	show_link_header_json(info, json_wtr);
164 
165 	switch (info->type) {
166 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
167 		jsonw_string_field(json_wtr, "tp_name",
168 				   u64_to_ptr(info->raw_tracepoint.tp_name));
169 		break;
170 	case BPF_LINK_TYPE_TRACING:
171 		err = get_prog_info(info->prog_id, &prog_info);
172 		if (err)
173 			return err;
174 
175 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
176 		/* libbpf will return NULL for variants unknown to it. */
177 		if (prog_type_str)
178 			jsonw_string_field(json_wtr, "prog_type", prog_type_str);
179 		else
180 			jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
181 
182 		show_link_attach_type_json(info->tracing.attach_type,
183 					   json_wtr);
184 		break;
185 	case BPF_LINK_TYPE_CGROUP:
186 		jsonw_lluint_field(json_wtr, "cgroup_id",
187 				   info->cgroup.cgroup_id);
188 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
189 		break;
190 	case BPF_LINK_TYPE_ITER:
191 		show_iter_json(info, json_wtr);
192 		break;
193 	case BPF_LINK_TYPE_NETNS:
194 		jsonw_uint_field(json_wtr, "netns_ino",
195 				 info->netns.netns_ino);
196 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
197 		break;
198 	default:
199 		break;
200 	}
201 
202 	if (!hashmap__empty(link_table)) {
203 		struct hashmap_entry *entry;
204 
205 		jsonw_name(json_wtr, "pinned");
206 		jsonw_start_array(json_wtr);
207 		hashmap__for_each_key_entry(link_table, entry,
208 					    u32_as_hash_field(info->id))
209 			jsonw_string(json_wtr, entry->value);
210 		jsonw_end_array(json_wtr);
211 	}
212 
213 	emit_obj_refs_json(refs_table, info->id, json_wtr);
214 
215 	jsonw_end_object(json_wtr);
216 
217 	return 0;
218 }
219 
220 static void show_link_header_plain(struct bpf_link_info *info)
221 {
222 	const char *link_type_str;
223 
224 	printf("%u: ", info->id);
225 	link_type_str = libbpf_bpf_link_type_str(info->type);
226 	if (link_type_str)
227 		printf("%s  ", link_type_str);
228 	else
229 		printf("type %u  ", info->type);
230 
231 	printf("prog %u  ", info->prog_id);
232 }
233 
234 static void show_link_attach_type_plain(__u32 attach_type)
235 {
236 	const char *attach_type_str;
237 
238 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
239 	if (attach_type_str)
240 		printf("attach_type %s  ", attach_type_str);
241 	else
242 		printf("attach_type %u  ", attach_type);
243 }
244 
245 static void show_iter_plain(struct bpf_link_info *info)
246 {
247 	const char *target_name = u64_to_ptr(info->iter.target_name);
248 
249 	printf("target_name %s  ", target_name);
250 
251 	if (is_iter_map_target(target_name))
252 		printf("map_id %u  ", info->iter.map.map_id);
253 	else if (is_iter_task_target(target_name)) {
254 		if (info->iter.task.tid)
255 			printf("tid %u ", info->iter.task.tid);
256 		else if (info->iter.task.pid)
257 			printf("pid %u ", info->iter.task.pid);
258 	}
259 
260 	if (is_iter_cgroup_target(target_name)) {
261 		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
262 		printf("order %s  ",
263 		       cgroup_order_string(info->iter.cgroup.order));
264 	}
265 }
266 
267 static int show_link_close_plain(int fd, struct bpf_link_info *info)
268 {
269 	struct bpf_prog_info prog_info;
270 	const char *prog_type_str;
271 	int err;
272 
273 	show_link_header_plain(info);
274 
275 	switch (info->type) {
276 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
277 		printf("\n\ttp '%s'  ",
278 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
279 		break;
280 	case BPF_LINK_TYPE_TRACING:
281 		err = get_prog_info(info->prog_id, &prog_info);
282 		if (err)
283 			return err;
284 
285 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
286 		/* libbpf will return NULL for variants unknown to it. */
287 		if (prog_type_str)
288 			printf("\n\tprog_type %s  ", prog_type_str);
289 		else
290 			printf("\n\tprog_type %u  ", prog_info.type);
291 
292 		show_link_attach_type_plain(info->tracing.attach_type);
293 		break;
294 	case BPF_LINK_TYPE_CGROUP:
295 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
296 		show_link_attach_type_plain(info->cgroup.attach_type);
297 		break;
298 	case BPF_LINK_TYPE_ITER:
299 		show_iter_plain(info);
300 		break;
301 	case BPF_LINK_TYPE_NETNS:
302 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
303 		show_link_attach_type_plain(info->netns.attach_type);
304 		break;
305 	default:
306 		break;
307 	}
308 
309 	if (!hashmap__empty(link_table)) {
310 		struct hashmap_entry *entry;
311 
312 		hashmap__for_each_key_entry(link_table, entry,
313 					    u32_as_hash_field(info->id))
314 			printf("\n\tpinned %s", (char *)entry->value);
315 	}
316 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
317 
318 	printf("\n");
319 
320 	return 0;
321 }
322 
323 static int do_show_link(int fd)
324 {
325 	struct bpf_link_info info;
326 	__u32 len = sizeof(info);
327 	char buf[256];
328 	int err;
329 
330 	memset(&info, 0, sizeof(info));
331 again:
332 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
333 	if (err) {
334 		p_err("can't get link info: %s",
335 		      strerror(errno));
336 		close(fd);
337 		return err;
338 	}
339 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
340 	    !info.raw_tracepoint.tp_name) {
341 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
342 		info.raw_tracepoint.tp_name_len = sizeof(buf);
343 		goto again;
344 	}
345 	if (info.type == BPF_LINK_TYPE_ITER &&
346 	    !info.iter.target_name) {
347 		info.iter.target_name = (unsigned long)&buf;
348 		info.iter.target_name_len = sizeof(buf);
349 		goto again;
350 	}
351 
352 	if (json_output)
353 		show_link_close_json(fd, &info);
354 	else
355 		show_link_close_plain(fd, &info);
356 
357 	close(fd);
358 	return 0;
359 }
360 
361 static int do_show(int argc, char **argv)
362 {
363 	__u32 id = 0;
364 	int err, fd;
365 
366 	if (show_pinned) {
367 		link_table = hashmap__new(hash_fn_for_key_as_id,
368 					  equal_fn_for_key_as_id, NULL);
369 		if (IS_ERR(link_table)) {
370 			p_err("failed to create hashmap for pinned paths");
371 			return -1;
372 		}
373 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
374 	}
375 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
376 
377 	if (argc == 2) {
378 		fd = link_parse_fd(&argc, &argv);
379 		if (fd < 0)
380 			return fd;
381 		return do_show_link(fd);
382 	}
383 
384 	if (argc)
385 		return BAD_ARG();
386 
387 	if (json_output)
388 		jsonw_start_array(json_wtr);
389 	while (true) {
390 		err = bpf_link_get_next_id(id, &id);
391 		if (err) {
392 			if (errno == ENOENT)
393 				break;
394 			p_err("can't get next link: %s%s", strerror(errno),
395 			      errno == EINVAL ? " -- kernel too old?" : "");
396 			break;
397 		}
398 
399 		fd = bpf_link_get_fd_by_id(id);
400 		if (fd < 0) {
401 			if (errno == ENOENT)
402 				continue;
403 			p_err("can't get link by id (%u): %s",
404 			      id, strerror(errno));
405 			break;
406 		}
407 
408 		err = do_show_link(fd);
409 		if (err)
410 			break;
411 	}
412 	if (json_output)
413 		jsonw_end_array(json_wtr);
414 
415 	delete_obj_refs_table(refs_table);
416 
417 	if (show_pinned)
418 		delete_pinned_obj_table(link_table);
419 
420 	return errno == ENOENT ? 0 : -1;
421 }
422 
423 static int do_pin(int argc, char **argv)
424 {
425 	int err;
426 
427 	err = do_pin_any(argc, argv, link_parse_fd);
428 	if (!err && json_output)
429 		jsonw_null(json_wtr);
430 	return err;
431 }
432 
433 static int do_detach(int argc, char **argv)
434 {
435 	int err, fd;
436 
437 	if (argc != 2) {
438 		p_err("link specifier is invalid or missing\n");
439 		return 1;
440 	}
441 
442 	fd = link_parse_fd(&argc, &argv);
443 	if (fd < 0)
444 		return 1;
445 
446 	err = bpf_link_detach(fd);
447 	if (err)
448 		err = -errno;
449 	close(fd);
450 	if (err) {
451 		p_err("failed link detach: %s", strerror(-err));
452 		return 1;
453 	}
454 
455 	if (json_output)
456 		jsonw_null(json_wtr);
457 
458 	return 0;
459 }
460 
461 static int do_help(int argc, char **argv)
462 {
463 	if (json_output) {
464 		jsonw_null(json_wtr);
465 		return 0;
466 	}
467 
468 	fprintf(stderr,
469 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
470 		"       %1$s %2$s pin        LINK  FILE\n"
471 		"       %1$s %2$s detach     LINK\n"
472 		"       %1$s %2$s help\n"
473 		"\n"
474 		"       " HELP_SPEC_LINK "\n"
475 		"       " HELP_SPEC_OPTIONS " |\n"
476 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
477 		"",
478 		bin_name, argv[-2]);
479 
480 	return 0;
481 }
482 
483 static const struct cmd cmds[] = {
484 	{ "show",	do_show },
485 	{ "list",	do_show },
486 	{ "help",	do_help },
487 	{ "pin",	do_pin },
488 	{ "detach",	do_detach },
489 	{ 0 }
490 };
491 
492 int do_link(int argc, char **argv)
493 {
494 	return cmd_select(cmds, argc, argv, do_help);
495 }
496