xref: /openbmc/linux/tools/bpf/bpftool/common.c (revision 82df5b73)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <ctype.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <fts.h>
8 #include <libgen.h>
9 #include <mntent.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <linux/limits.h>
16 #include <linux/magic.h>
17 #include <net/if.h>
18 #include <sys/mount.h>
19 #include <sys/resource.h>
20 #include <sys/stat.h>
21 #include <sys/vfs.h>
22 
23 #include <bpf/bpf.h>
24 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
25 
26 #include "main.h"
27 
28 #ifndef BPF_FS_MAGIC
29 #define BPF_FS_MAGIC		0xcafe4a11
30 #endif
31 
32 void p_err(const char *fmt, ...)
33 {
34 	va_list ap;
35 
36 	va_start(ap, fmt);
37 	if (json_output) {
38 		jsonw_start_object(json_wtr);
39 		jsonw_name(json_wtr, "error");
40 		jsonw_vprintf_enquote(json_wtr, fmt, ap);
41 		jsonw_end_object(json_wtr);
42 	} else {
43 		fprintf(stderr, "Error: ");
44 		vfprintf(stderr, fmt, ap);
45 		fprintf(stderr, "\n");
46 	}
47 	va_end(ap);
48 }
49 
50 void p_info(const char *fmt, ...)
51 {
52 	va_list ap;
53 
54 	if (json_output)
55 		return;
56 
57 	va_start(ap, fmt);
58 	vfprintf(stderr, fmt, ap);
59 	fprintf(stderr, "\n");
60 	va_end(ap);
61 }
62 
63 static bool is_bpffs(char *path)
64 {
65 	struct statfs st_fs;
66 
67 	if (statfs(path, &st_fs) < 0)
68 		return false;
69 
70 	return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
71 }
72 
73 void set_max_rlimit(void)
74 {
75 	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
76 
77 	setrlimit(RLIMIT_MEMLOCK, &rinf);
78 }
79 
80 static int
81 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
82 {
83 	bool bind_done = false;
84 
85 	while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
86 		if (errno != EINVAL || bind_done) {
87 			snprintf(buff, bufflen,
88 				 "mount --make-private %s failed: %s",
89 				 target, strerror(errno));
90 			return -1;
91 		}
92 
93 		if (mount(target, target, "none", MS_BIND, NULL)) {
94 			snprintf(buff, bufflen,
95 				 "mount --bind %s %s failed: %s",
96 				 target, target, strerror(errno));
97 			return -1;
98 		}
99 
100 		bind_done = true;
101 	}
102 
103 	if (mount(type, target, type, 0, "mode=0700")) {
104 		snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
105 			 type, type, target, strerror(errno));
106 		return -1;
107 	}
108 
109 	return 0;
110 }
111 
112 int mount_tracefs(const char *target)
113 {
114 	char err_str[ERR_MAX_LEN];
115 	int err;
116 
117 	err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
118 	if (err) {
119 		err_str[ERR_MAX_LEN - 1] = '\0';
120 		p_err("can't mount tracefs: %s", err_str);
121 	}
122 
123 	return err;
124 }
125 
126 int open_obj_pinned(char *path, bool quiet)
127 {
128 	int fd;
129 
130 	fd = bpf_obj_get(path);
131 	if (fd < 0) {
132 		if (!quiet)
133 			p_err("bpf obj get (%s): %s", path,
134 			      errno == EACCES && !is_bpffs(dirname(path)) ?
135 			    "directory not in bpf file system (bpffs)" :
136 			    strerror(errno));
137 		return -1;
138 	}
139 
140 	return fd;
141 }
142 
143 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
144 {
145 	enum bpf_obj_type type;
146 	int fd;
147 
148 	fd = open_obj_pinned(path, false);
149 	if (fd < 0)
150 		return -1;
151 
152 	type = get_fd_type(fd);
153 	if (type < 0) {
154 		close(fd);
155 		return type;
156 	}
157 	if (type != exp_type) {
158 		p_err("incorrect object type: %s", get_fd_type_name(type));
159 		close(fd);
160 		return -1;
161 	}
162 
163 	return fd;
164 }
165 
166 int mount_bpffs_for_pin(const char *name)
167 {
168 	char err_str[ERR_MAX_LEN];
169 	char *file;
170 	char *dir;
171 	int err = 0;
172 
173 	file = malloc(strlen(name) + 1);
174 	strcpy(file, name);
175 	dir = dirname(file);
176 
177 	if (is_bpffs(dir))
178 		/* nothing to do if already mounted */
179 		goto out_free;
180 
181 	if (block_mount) {
182 		p_err("no BPF file system found, not mounting it due to --nomount option");
183 		err = -1;
184 		goto out_free;
185 	}
186 
187 	err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
188 	if (err) {
189 		err_str[ERR_MAX_LEN - 1] = '\0';
190 		p_err("can't mount BPF file system to pin the object (%s): %s",
191 		      name, err_str);
192 	}
193 
194 out_free:
195 	free(file);
196 	return err;
197 }
198 
199 int do_pin_fd(int fd, const char *name)
200 {
201 	int err;
202 
203 	err = mount_bpffs_for_pin(name);
204 	if (err)
205 		return err;
206 
207 	err = bpf_obj_pin(fd, name);
208 	if (err)
209 		p_err("can't pin the object (%s): %s", name, strerror(errno));
210 
211 	return err;
212 }
213 
214 int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
215 {
216 	int err;
217 	int fd;
218 
219 	fd = get_fd(&argc, &argv);
220 	if (fd < 0)
221 		return fd;
222 
223 	err = do_pin_fd(fd, *argv);
224 
225 	close(fd);
226 	return err;
227 }
228 
229 const char *get_fd_type_name(enum bpf_obj_type type)
230 {
231 	static const char * const names[] = {
232 		[BPF_OBJ_UNKNOWN]	= "unknown",
233 		[BPF_OBJ_PROG]		= "prog",
234 		[BPF_OBJ_MAP]		= "map",
235 	};
236 
237 	if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
238 		return names[BPF_OBJ_UNKNOWN];
239 
240 	return names[type];
241 }
242 
243 int get_fd_type(int fd)
244 {
245 	char path[PATH_MAX];
246 	char buf[512];
247 	ssize_t n;
248 
249 	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
250 
251 	n = readlink(path, buf, sizeof(buf));
252 	if (n < 0) {
253 		p_err("can't read link type: %s", strerror(errno));
254 		return -1;
255 	}
256 	if (n == sizeof(path)) {
257 		p_err("can't read link type: path too long!");
258 		return -1;
259 	}
260 
261 	if (strstr(buf, "bpf-map"))
262 		return BPF_OBJ_MAP;
263 	else if (strstr(buf, "bpf-prog"))
264 		return BPF_OBJ_PROG;
265 	else if (strstr(buf, "bpf-link"))
266 		return BPF_OBJ_LINK;
267 
268 	return BPF_OBJ_UNKNOWN;
269 }
270 
271 char *get_fdinfo(int fd, const char *key)
272 {
273 	char path[PATH_MAX];
274 	char *line = NULL;
275 	size_t line_n = 0;
276 	ssize_t n;
277 	FILE *fdi;
278 
279 	snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
280 
281 	fdi = fopen(path, "r");
282 	if (!fdi)
283 		return NULL;
284 
285 	while ((n = getline(&line, &line_n, fdi)) > 0) {
286 		char *value;
287 		int len;
288 
289 		if (!strstr(line, key))
290 			continue;
291 
292 		fclose(fdi);
293 
294 		value = strchr(line, '\t');
295 		if (!value || !value[1]) {
296 			free(line);
297 			return NULL;
298 		}
299 		value++;
300 
301 		len = strlen(value);
302 		memmove(line, value, len);
303 		line[len - 1] = '\0';
304 
305 		return line;
306 	}
307 
308 	free(line);
309 	fclose(fdi);
310 	return NULL;
311 }
312 
313 void print_data_json(uint8_t *data, size_t len)
314 {
315 	unsigned int i;
316 
317 	jsonw_start_array(json_wtr);
318 	for (i = 0; i < len; i++)
319 		jsonw_printf(json_wtr, "%d", data[i]);
320 	jsonw_end_array(json_wtr);
321 }
322 
323 void print_hex_data_json(uint8_t *data, size_t len)
324 {
325 	unsigned int i;
326 
327 	jsonw_start_array(json_wtr);
328 	for (i = 0; i < len; i++)
329 		jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
330 	jsonw_end_array(json_wtr);
331 }
332 
333 int build_pinned_obj_table(struct pinned_obj_table *tab,
334 			   enum bpf_obj_type type)
335 {
336 	struct bpf_prog_info pinned_info = {};
337 	struct pinned_obj *obj_node = NULL;
338 	__u32 len = sizeof(pinned_info);
339 	struct mntent *mntent = NULL;
340 	enum bpf_obj_type objtype;
341 	FILE *mntfile = NULL;
342 	FTSENT *ftse = NULL;
343 	FTS *fts = NULL;
344 	int fd, err;
345 
346 	mntfile = setmntent("/proc/mounts", "r");
347 	if (!mntfile)
348 		return -1;
349 
350 	while ((mntent = getmntent(mntfile))) {
351 		char *path[] = { mntent->mnt_dir, NULL };
352 
353 		if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
354 			continue;
355 
356 		fts = fts_open(path, 0, NULL);
357 		if (!fts)
358 			continue;
359 
360 		while ((ftse = fts_read(fts))) {
361 			if (!(ftse->fts_info & FTS_F))
362 				continue;
363 			fd = open_obj_pinned(ftse->fts_path, true);
364 			if (fd < 0)
365 				continue;
366 
367 			objtype = get_fd_type(fd);
368 			if (objtype != type) {
369 				close(fd);
370 				continue;
371 			}
372 			memset(&pinned_info, 0, sizeof(pinned_info));
373 			err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
374 			if (err) {
375 				close(fd);
376 				continue;
377 			}
378 
379 			obj_node = malloc(sizeof(*obj_node));
380 			if (!obj_node) {
381 				close(fd);
382 				fts_close(fts);
383 				fclose(mntfile);
384 				return -1;
385 			}
386 
387 			memset(obj_node, 0, sizeof(*obj_node));
388 			obj_node->id = pinned_info.id;
389 			obj_node->path = strdup(ftse->fts_path);
390 			hash_add(tab->table, &obj_node->hash, obj_node->id);
391 
392 			close(fd);
393 		}
394 		fts_close(fts);
395 	}
396 	fclose(mntfile);
397 	return 0;
398 }
399 
400 void delete_pinned_obj_table(struct pinned_obj_table *tab)
401 {
402 	struct pinned_obj *obj;
403 	struct hlist_node *tmp;
404 	unsigned int bkt;
405 
406 	hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
407 		hash_del(&obj->hash);
408 		free(obj->path);
409 		free(obj);
410 	}
411 }
412 
413 unsigned int get_page_size(void)
414 {
415 	static int result;
416 
417 	if (!result)
418 		result = getpagesize();
419 	return result;
420 }
421 
422 unsigned int get_possible_cpus(void)
423 {
424 	int cpus = libbpf_num_possible_cpus();
425 
426 	if (cpus < 0) {
427 		p_err("Can't get # of possible cpus: %s", strerror(-cpus));
428 		exit(-1);
429 	}
430 	return cpus;
431 }
432 
433 static char *
434 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
435 {
436 	struct stat st;
437 	int err;
438 
439 	err = stat("/proc/self/ns/net", &st);
440 	if (err) {
441 		p_err("Can't stat /proc/self: %s", strerror(errno));
442 		return NULL;
443 	}
444 
445 	if (st.st_dev != ns_dev || st.st_ino != ns_ino)
446 		return NULL;
447 
448 	return if_indextoname(ifindex, buf);
449 }
450 
451 static int read_sysfs_hex_int(char *path)
452 {
453 	char vendor_id_buf[8];
454 	int len;
455 	int fd;
456 
457 	fd = open(path, O_RDONLY);
458 	if (fd < 0) {
459 		p_err("Can't open %s: %s", path, strerror(errno));
460 		return -1;
461 	}
462 
463 	len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
464 	close(fd);
465 	if (len < 0) {
466 		p_err("Can't read %s: %s", path, strerror(errno));
467 		return -1;
468 	}
469 	if (len >= (int)sizeof(vendor_id_buf)) {
470 		p_err("Value in %s too long", path);
471 		return -1;
472 	}
473 
474 	vendor_id_buf[len] = 0;
475 
476 	return strtol(vendor_id_buf, NULL, 0);
477 }
478 
479 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
480 {
481 	char full_path[64];
482 
483 	snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
484 		 devname, entry_name);
485 
486 	return read_sysfs_hex_int(full_path);
487 }
488 
489 const char *
490 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
491 		      const char **opt)
492 {
493 	char devname[IF_NAMESIZE];
494 	int vendor_id;
495 	int device_id;
496 
497 	if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
498 		p_err("Can't get net device name for ifindex %d: %s", ifindex,
499 		      strerror(errno));
500 		return NULL;
501 	}
502 
503 	vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
504 	if (vendor_id < 0) {
505 		p_err("Can't get device vendor id for %s", devname);
506 		return NULL;
507 	}
508 
509 	switch (vendor_id) {
510 	case 0x19ee:
511 		device_id = read_sysfs_netdev_hex_int(devname, "device");
512 		if (device_id != 0x4000 &&
513 		    device_id != 0x6000 &&
514 		    device_id != 0x6003)
515 			p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
516 		*opt = "ctx4";
517 		return "NFP-6xxx";
518 	default:
519 		p_err("Can't get bfd arch name for device vendor id 0x%04x",
520 		      vendor_id);
521 		return NULL;
522 	}
523 }
524 
525 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
526 {
527 	char name[IF_NAMESIZE];
528 
529 	if (!ifindex)
530 		return;
531 
532 	printf("  offloaded_to ");
533 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
534 		printf("%s", name);
535 	else
536 		printf("ifindex %u ns_dev %llu ns_ino %llu",
537 		       ifindex, ns_dev, ns_inode);
538 }
539 
540 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
541 {
542 	char name[IF_NAMESIZE];
543 
544 	if (!ifindex)
545 		return;
546 
547 	jsonw_name(json_wtr, "dev");
548 	jsonw_start_object(json_wtr);
549 	jsonw_uint_field(json_wtr, "ifindex", ifindex);
550 	jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
551 	jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
552 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
553 		jsonw_string_field(json_wtr, "ifname", name);
554 	jsonw_end_object(json_wtr);
555 }
556 
557 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
558 {
559 	char *endptr;
560 
561 	NEXT_ARGP();
562 
563 	if (*val) {
564 		p_err("%s already specified", what);
565 		return -1;
566 	}
567 
568 	*val = strtoul(**argv, &endptr, 0);
569 	if (*endptr) {
570 		p_err("can't parse %s as %s", **argv, what);
571 		return -1;
572 	}
573 	NEXT_ARGP();
574 
575 	return 0;
576 }
577 
578 int __printf(2, 0)
579 print_all_levels(__maybe_unused enum libbpf_print_level level,
580 		 const char *format, va_list args)
581 {
582 	return vfprintf(stderr, format, args);
583 }
584