xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision 2243acd5)
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_prog_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, info->id)
208 			jsonw_string(json_wtr, entry->pvalue);
209 		jsonw_end_array(json_wtr);
210 	}
211 
212 	emit_obj_refs_json(refs_table, info->id, json_wtr);
213 
214 	jsonw_end_object(json_wtr);
215 
216 	return 0;
217 }
218 
219 static void show_link_header_plain(struct bpf_link_info *info)
220 {
221 	const char *link_type_str;
222 
223 	printf("%u: ", info->id);
224 	link_type_str = libbpf_bpf_link_type_str(info->type);
225 	if (link_type_str)
226 		printf("%s  ", link_type_str);
227 	else
228 		printf("type %u  ", info->type);
229 
230 	printf("prog %u  ", info->prog_id);
231 }
232 
233 static void show_link_attach_type_plain(__u32 attach_type)
234 {
235 	const char *attach_type_str;
236 
237 	attach_type_str = libbpf_bpf_attach_type_str(attach_type);
238 	if (attach_type_str)
239 		printf("attach_type %s  ", attach_type_str);
240 	else
241 		printf("attach_type %u  ", attach_type);
242 }
243 
244 static void show_iter_plain(struct bpf_link_info *info)
245 {
246 	const char *target_name = u64_to_ptr(info->iter.target_name);
247 
248 	printf("target_name %s  ", target_name);
249 
250 	if (is_iter_map_target(target_name))
251 		printf("map_id %u  ", info->iter.map.map_id);
252 	else if (is_iter_task_target(target_name)) {
253 		if (info->iter.task.tid)
254 			printf("tid %u ", info->iter.task.tid);
255 		else if (info->iter.task.pid)
256 			printf("pid %u ", info->iter.task.pid);
257 	}
258 
259 	if (is_iter_cgroup_target(target_name)) {
260 		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
261 		printf("order %s  ",
262 		       cgroup_order_string(info->iter.cgroup.order));
263 	}
264 }
265 
266 static int show_link_close_plain(int fd, struct bpf_link_info *info)
267 {
268 	struct bpf_prog_info prog_info;
269 	const char *prog_type_str;
270 	int err;
271 
272 	show_link_header_plain(info);
273 
274 	switch (info->type) {
275 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
276 		printf("\n\ttp '%s'  ",
277 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
278 		break;
279 	case BPF_LINK_TYPE_TRACING:
280 		err = get_prog_info(info->prog_id, &prog_info);
281 		if (err)
282 			return err;
283 
284 		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
285 		/* libbpf will return NULL for variants unknown to it. */
286 		if (prog_type_str)
287 			printf("\n\tprog_type %s  ", prog_type_str);
288 		else
289 			printf("\n\tprog_type %u  ", prog_info.type);
290 
291 		show_link_attach_type_plain(info->tracing.attach_type);
292 		break;
293 	case BPF_LINK_TYPE_CGROUP:
294 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
295 		show_link_attach_type_plain(info->cgroup.attach_type);
296 		break;
297 	case BPF_LINK_TYPE_ITER:
298 		show_iter_plain(info);
299 		break;
300 	case BPF_LINK_TYPE_NETNS:
301 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
302 		show_link_attach_type_plain(info->netns.attach_type);
303 		break;
304 	default:
305 		break;
306 	}
307 
308 	if (!hashmap__empty(link_table)) {
309 		struct hashmap_entry *entry;
310 
311 		hashmap__for_each_key_entry(link_table, entry, info->id)
312 			printf("\n\tpinned %s", (char *)entry->pvalue);
313 	}
314 	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
315 
316 	printf("\n");
317 
318 	return 0;
319 }
320 
321 static int do_show_link(int fd)
322 {
323 	struct bpf_link_info info;
324 	__u32 len = sizeof(info);
325 	char buf[256];
326 	int err;
327 
328 	memset(&info, 0, sizeof(info));
329 again:
330 	err = bpf_link_get_info_by_fd(fd, &info, &len);
331 	if (err) {
332 		p_err("can't get link info: %s",
333 		      strerror(errno));
334 		close(fd);
335 		return err;
336 	}
337 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
338 	    !info.raw_tracepoint.tp_name) {
339 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
340 		info.raw_tracepoint.tp_name_len = sizeof(buf);
341 		goto again;
342 	}
343 	if (info.type == BPF_LINK_TYPE_ITER &&
344 	    !info.iter.target_name) {
345 		info.iter.target_name = (unsigned long)&buf;
346 		info.iter.target_name_len = sizeof(buf);
347 		goto again;
348 	}
349 
350 	if (json_output)
351 		show_link_close_json(fd, &info);
352 	else
353 		show_link_close_plain(fd, &info);
354 
355 	close(fd);
356 	return 0;
357 }
358 
359 static int do_show(int argc, char **argv)
360 {
361 	__u32 id = 0;
362 	int err, fd;
363 
364 	if (show_pinned) {
365 		link_table = hashmap__new(hash_fn_for_key_as_id,
366 					  equal_fn_for_key_as_id, NULL);
367 		if (IS_ERR(link_table)) {
368 			p_err("failed to create hashmap for pinned paths");
369 			return -1;
370 		}
371 		build_pinned_obj_table(link_table, BPF_OBJ_LINK);
372 	}
373 	build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
374 
375 	if (argc == 2) {
376 		fd = link_parse_fd(&argc, &argv);
377 		if (fd < 0)
378 			return fd;
379 		return do_show_link(fd);
380 	}
381 
382 	if (argc)
383 		return BAD_ARG();
384 
385 	if (json_output)
386 		jsonw_start_array(json_wtr);
387 	while (true) {
388 		err = bpf_link_get_next_id(id, &id);
389 		if (err) {
390 			if (errno == ENOENT)
391 				break;
392 			p_err("can't get next link: %s%s", strerror(errno),
393 			      errno == EINVAL ? " -- kernel too old?" : "");
394 			break;
395 		}
396 
397 		fd = bpf_link_get_fd_by_id(id);
398 		if (fd < 0) {
399 			if (errno == ENOENT)
400 				continue;
401 			p_err("can't get link by id (%u): %s",
402 			      id, strerror(errno));
403 			break;
404 		}
405 
406 		err = do_show_link(fd);
407 		if (err)
408 			break;
409 	}
410 	if (json_output)
411 		jsonw_end_array(json_wtr);
412 
413 	delete_obj_refs_table(refs_table);
414 
415 	if (show_pinned)
416 		delete_pinned_obj_table(link_table);
417 
418 	return errno == ENOENT ? 0 : -1;
419 }
420 
421 static int do_pin(int argc, char **argv)
422 {
423 	int err;
424 
425 	err = do_pin_any(argc, argv, link_parse_fd);
426 	if (!err && json_output)
427 		jsonw_null(json_wtr);
428 	return err;
429 }
430 
431 static int do_detach(int argc, char **argv)
432 {
433 	int err, fd;
434 
435 	if (argc != 2) {
436 		p_err("link specifier is invalid or missing\n");
437 		return 1;
438 	}
439 
440 	fd = link_parse_fd(&argc, &argv);
441 	if (fd < 0)
442 		return 1;
443 
444 	err = bpf_link_detach(fd);
445 	if (err)
446 		err = -errno;
447 	close(fd);
448 	if (err) {
449 		p_err("failed link detach: %s", strerror(-err));
450 		return 1;
451 	}
452 
453 	if (json_output)
454 		jsonw_null(json_wtr);
455 
456 	return 0;
457 }
458 
459 static int do_help(int argc, char **argv)
460 {
461 	if (json_output) {
462 		jsonw_null(json_wtr);
463 		return 0;
464 	}
465 
466 	fprintf(stderr,
467 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
468 		"       %1$s %2$s pin        LINK  FILE\n"
469 		"       %1$s %2$s detach     LINK\n"
470 		"       %1$s %2$s help\n"
471 		"\n"
472 		"       " HELP_SPEC_LINK "\n"
473 		"       " HELP_SPEC_OPTIONS " |\n"
474 		"                    {-f|--bpffs} | {-n|--nomount} }\n"
475 		"",
476 		bin_name, argv[-2]);
477 
478 	return 0;
479 }
480 
481 static const struct cmd cmds[] = {
482 	{ "show",	do_show },
483 	{ "list",	do_show },
484 	{ "help",	do_help },
485 	{ "pin",	do_pin },
486 	{ "detach",	do_detach },
487 	{ 0 }
488 };
489 
490 int do_link(int argc, char **argv)
491 {
492 	return cmd_select(cmds, argc, argv, do_help);
493 }
494