xref: /openbmc/linux/tools/bpf/bpftool/link.c (revision 547840bd)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2020 Facebook */
3 
4 #include <errno.h>
5 #include <net/if.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 
9 #include <bpf/bpf.h>
10 
11 #include "json_writer.h"
12 #include "main.h"
13 
14 static const char * const link_type_name[] = {
15 	[BPF_LINK_TYPE_UNSPEC]			= "unspec",
16 	[BPF_LINK_TYPE_RAW_TRACEPOINT]		= "raw_tracepoint",
17 	[BPF_LINK_TYPE_TRACING]			= "tracing",
18 	[BPF_LINK_TYPE_CGROUP]			= "cgroup",
19 	[BPF_LINK_TYPE_ITER]			= "iter",
20 	[BPF_LINK_TYPE_NETNS]			= "netns",
21 };
22 
23 static int link_parse_fd(int *argc, char ***argv)
24 {
25 	if (is_prefix(**argv, "id")) {
26 		unsigned int id;
27 		char *endptr;
28 
29 		NEXT_ARGP();
30 
31 		id = strtoul(**argv, &endptr, 0);
32 		if (*endptr) {
33 			p_err("can't parse %s as ID", **argv);
34 			return -1;
35 		}
36 		NEXT_ARGP();
37 
38 		return bpf_link_get_fd_by_id(id);
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 	jsonw_uint_field(wtr, "id", info->id);
58 	if (info->type < ARRAY_SIZE(link_type_name))
59 		jsonw_string_field(wtr, "type", link_type_name[info->type]);
60 	else
61 		jsonw_uint_field(wtr, "type", info->type);
62 
63 	jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
64 }
65 
66 static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
67 {
68 	if (attach_type < ARRAY_SIZE(attach_type_name))
69 		jsonw_string_field(wtr, "attach_type",
70 				   attach_type_name[attach_type]);
71 	else
72 		jsonw_uint_field(wtr, "attach_type", attach_type);
73 }
74 
75 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
76 {
77 	__u32 len = sizeof(*info);
78 	int err, prog_fd;
79 
80 	prog_fd = bpf_prog_get_fd_by_id(prog_id);
81 	if (prog_fd < 0)
82 		return prog_fd;
83 
84 	memset(info, 0, sizeof(*info));
85 	err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
86 	if (err)
87 		p_err("can't get prog info: %s", strerror(errno));
88 	close(prog_fd);
89 	return err;
90 }
91 
92 static int show_link_close_json(int fd, struct bpf_link_info *info)
93 {
94 	struct bpf_prog_info prog_info;
95 	int err;
96 
97 	jsonw_start_object(json_wtr);
98 
99 	show_link_header_json(info, json_wtr);
100 
101 	switch (info->type) {
102 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
103 		jsonw_string_field(json_wtr, "tp_name",
104 				   (const char *)info->raw_tracepoint.tp_name);
105 		break;
106 	case BPF_LINK_TYPE_TRACING:
107 		err = get_prog_info(info->prog_id, &prog_info);
108 		if (err)
109 			return err;
110 
111 		if (prog_info.type < ARRAY_SIZE(prog_type_name))
112 			jsonw_string_field(json_wtr, "prog_type",
113 					   prog_type_name[prog_info.type]);
114 		else
115 			jsonw_uint_field(json_wtr, "prog_type",
116 					 prog_info.type);
117 
118 		show_link_attach_type_json(info->tracing.attach_type,
119 					   json_wtr);
120 		break;
121 	case BPF_LINK_TYPE_CGROUP:
122 		jsonw_lluint_field(json_wtr, "cgroup_id",
123 				   info->cgroup.cgroup_id);
124 		show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
125 		break;
126 	case BPF_LINK_TYPE_NETNS:
127 		jsonw_uint_field(json_wtr, "netns_ino",
128 				 info->netns.netns_ino);
129 		show_link_attach_type_json(info->netns.attach_type, json_wtr);
130 		break;
131 	default:
132 		break;
133 	}
134 
135 	if (!hash_empty(link_table.table)) {
136 		struct pinned_obj *obj;
137 
138 		jsonw_name(json_wtr, "pinned");
139 		jsonw_start_array(json_wtr);
140 		hash_for_each_possible(link_table.table, obj, hash, info->id) {
141 			if (obj->id == info->id)
142 				jsonw_string(json_wtr, obj->path);
143 		}
144 		jsonw_end_array(json_wtr);
145 	}
146 	jsonw_end_object(json_wtr);
147 
148 	return 0;
149 }
150 
151 static void show_link_header_plain(struct bpf_link_info *info)
152 {
153 	printf("%u: ", info->id);
154 	if (info->type < ARRAY_SIZE(link_type_name))
155 		printf("%s  ", link_type_name[info->type]);
156 	else
157 		printf("type %u  ", info->type);
158 
159 	printf("prog %u  ", info->prog_id);
160 }
161 
162 static void show_link_attach_type_plain(__u32 attach_type)
163 {
164 	if (attach_type < ARRAY_SIZE(attach_type_name))
165 		printf("attach_type %s  ", attach_type_name[attach_type]);
166 	else
167 		printf("attach_type %u  ", attach_type);
168 }
169 
170 static int show_link_close_plain(int fd, struct bpf_link_info *info)
171 {
172 	struct bpf_prog_info prog_info;
173 	int err;
174 
175 	show_link_header_plain(info);
176 
177 	switch (info->type) {
178 	case BPF_LINK_TYPE_RAW_TRACEPOINT:
179 		printf("\n\ttp '%s'  ",
180 		       (const char *)info->raw_tracepoint.tp_name);
181 		break;
182 	case BPF_LINK_TYPE_TRACING:
183 		err = get_prog_info(info->prog_id, &prog_info);
184 		if (err)
185 			return err;
186 
187 		if (prog_info.type < ARRAY_SIZE(prog_type_name))
188 			printf("\n\tprog_type %s  ",
189 			       prog_type_name[prog_info.type]);
190 		else
191 			printf("\n\tprog_type %u  ", prog_info.type);
192 
193 		show_link_attach_type_plain(info->tracing.attach_type);
194 		break;
195 	case BPF_LINK_TYPE_CGROUP:
196 		printf("\n\tcgroup_id %zu  ", (size_t)info->cgroup.cgroup_id);
197 		show_link_attach_type_plain(info->cgroup.attach_type);
198 		break;
199 	case BPF_LINK_TYPE_NETNS:
200 		printf("\n\tnetns_ino %u  ", info->netns.netns_ino);
201 		show_link_attach_type_plain(info->netns.attach_type);
202 		break;
203 	default:
204 		break;
205 	}
206 
207 	if (!hash_empty(link_table.table)) {
208 		struct pinned_obj *obj;
209 
210 		hash_for_each_possible(link_table.table, obj, hash, info->id) {
211 			if (obj->id == info->id)
212 				printf("\n\tpinned %s", obj->path);
213 		}
214 	}
215 
216 	printf("\n");
217 
218 	return 0;
219 }
220 
221 static int do_show_link(int fd)
222 {
223 	struct bpf_link_info info;
224 	__u32 len = sizeof(info);
225 	char raw_tp_name[256];
226 	int err;
227 
228 	memset(&info, 0, sizeof(info));
229 again:
230 	err = bpf_obj_get_info_by_fd(fd, &info, &len);
231 	if (err) {
232 		p_err("can't get link info: %s",
233 		      strerror(errno));
234 		close(fd);
235 		return err;
236 	}
237 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
238 	    !info.raw_tracepoint.tp_name) {
239 		info.raw_tracepoint.tp_name = (unsigned long)&raw_tp_name;
240 		info.raw_tracepoint.tp_name_len = sizeof(raw_tp_name);
241 		goto again;
242 	}
243 
244 	if (json_output)
245 		show_link_close_json(fd, &info);
246 	else
247 		show_link_close_plain(fd, &info);
248 
249 	close(fd);
250 	return 0;
251 }
252 
253 static int do_show(int argc, char **argv)
254 {
255 	__u32 id = 0;
256 	int err, fd;
257 
258 	if (show_pinned)
259 		build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
260 
261 	if (argc == 2) {
262 		fd = link_parse_fd(&argc, &argv);
263 		if (fd < 0)
264 			return fd;
265 		return do_show_link(fd);
266 	}
267 
268 	if (argc)
269 		return BAD_ARG();
270 
271 	if (json_output)
272 		jsonw_start_array(json_wtr);
273 	while (true) {
274 		err = bpf_link_get_next_id(id, &id);
275 		if (err) {
276 			if (errno == ENOENT)
277 				break;
278 			p_err("can't get next link: %s%s", strerror(errno),
279 			      errno == EINVAL ? " -- kernel too old?" : "");
280 			break;
281 		}
282 
283 		fd = bpf_link_get_fd_by_id(id);
284 		if (fd < 0) {
285 			if (errno == ENOENT)
286 				continue;
287 			p_err("can't get link by id (%u): %s",
288 			      id, strerror(errno));
289 			break;
290 		}
291 
292 		err = do_show_link(fd);
293 		if (err)
294 			break;
295 	}
296 	if (json_output)
297 		jsonw_end_array(json_wtr);
298 
299 	return errno == ENOENT ? 0 : -1;
300 }
301 
302 static int do_pin(int argc, char **argv)
303 {
304 	int err;
305 
306 	err = do_pin_any(argc, argv, link_parse_fd);
307 	if (!err && json_output)
308 		jsonw_null(json_wtr);
309 	return err;
310 }
311 
312 static int do_help(int argc, char **argv)
313 {
314 	if (json_output) {
315 		jsonw_null(json_wtr);
316 		return 0;
317 	}
318 
319 	fprintf(stderr,
320 		"Usage: %1$s %2$s { show | list }   [LINK]\n"
321 		"       %1$s %2$s pin        LINK  FILE\n"
322 		"       %1$s %2$s help\n"
323 		"\n"
324 		"       " HELP_SPEC_LINK "\n"
325 		"       " HELP_SPEC_OPTIONS "\n"
326 		"",
327 		bin_name, argv[-2]);
328 
329 	return 0;
330 }
331 
332 static const struct cmd cmds[] = {
333 	{ "show",	do_show },
334 	{ "list",	do_show },
335 	{ "help",	do_help },
336 	{ "pin",	do_pin },
337 	{ 0 }
338 };
339 
340 int do_link(int argc, char **argv)
341 {
342 	return cmd_select(cmds, argc, argv, do_help);
343 }
344