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