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