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