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