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