• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <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 
p_err(const char * fmt,...)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 
p_info(const char * fmt,...)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 
is_bpffs(char * path)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 
set_max_rlimit(void)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
mnt_fs(const char * target,const char * type,char * buff,size_t bufflen)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 
mount_tracefs(const char * target)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 
open_obj_pinned(char * path,bool quiet)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 
open_obj_pinned_any(char * path,enum bpf_obj_type exp_type)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 
mount_bpffs_for_pin(const char * name)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 	if (!file) {
175 		p_err("mem alloc failed");
176 		return -1;
177 	}
178 
179 	strcpy(file, name);
180 	dir = dirname(file);
181 
182 	if (is_bpffs(dir))
183 		/* nothing to do if already mounted */
184 		goto out_free;
185 
186 	if (block_mount) {
187 		p_err("no BPF file system found, not mounting it due to --nomount option");
188 		err = -1;
189 		goto out_free;
190 	}
191 
192 	err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
193 	if (err) {
194 		err_str[ERR_MAX_LEN - 1] = '\0';
195 		p_err("can't mount BPF file system to pin the object (%s): %s",
196 		      name, err_str);
197 	}
198 
199 out_free:
200 	free(file);
201 	return err;
202 }
203 
do_pin_fd(int fd,const char * name)204 int do_pin_fd(int fd, const char *name)
205 {
206 	int err;
207 
208 	err = mount_bpffs_for_pin(name);
209 	if (err)
210 		return err;
211 
212 	err = bpf_obj_pin(fd, name);
213 	if (err)
214 		p_err("can't pin the object (%s): %s", name, strerror(errno));
215 
216 	return err;
217 }
218 
do_pin_any(int argc,char ** argv,int (* get_fd_by_id)(__u32))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 open object 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 
get_fd_type_name(enum bpf_obj_type type)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 
get_fd_type(int fd)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/self/fd/%d", 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 
get_fdinfo(int fd,const char * key)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/self/fdinfo/%d", fd);
308 
309 	fdi = fopen(path, "r");
310 	if (!fdi)
311 		return NULL;
312 
313 	while ((n = getline(&line, &line_n, fdi)) > 0) {
314 		char *value;
315 		int len;
316 
317 		if (!strstr(line, key))
318 			continue;
319 
320 		fclose(fdi);
321 
322 		value = strchr(line, '\t');
323 		if (!value || !value[1]) {
324 			free(line);
325 			return NULL;
326 		}
327 		value++;
328 
329 		len = strlen(value);
330 		memmove(line, value, len);
331 		line[len - 1] = '\0';
332 
333 		return line;
334 	}
335 
336 	free(line);
337 	fclose(fdi);
338 	return NULL;
339 }
340 
print_data_json(uint8_t * data,size_t len)341 void print_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, "%d", data[i]);
348 	jsonw_end_array(json_wtr);
349 }
350 
print_hex_data_json(uint8_t * data,size_t len)351 void print_hex_data_json(uint8_t *data, size_t len)
352 {
353 	unsigned int i;
354 
355 	jsonw_start_array(json_wtr);
356 	for (i = 0; i < len; i++)
357 		jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
358 	jsonw_end_array(json_wtr);
359 }
360 
build_pinned_obj_table(struct pinned_obj_table * tab,enum bpf_obj_type type)361 int build_pinned_obj_table(struct pinned_obj_table *tab,
362 			   enum bpf_obj_type type)
363 {
364 	struct bpf_prog_info pinned_info = {};
365 	struct pinned_obj *obj_node = NULL;
366 	__u32 len = sizeof(pinned_info);
367 	struct mntent *mntent = NULL;
368 	enum bpf_obj_type objtype;
369 	FILE *mntfile = NULL;
370 	FTSENT *ftse = NULL;
371 	FTS *fts = NULL;
372 	int fd, err;
373 
374 	mntfile = setmntent("/proc/mounts", "r");
375 	if (!mntfile)
376 		return -1;
377 
378 	while ((mntent = getmntent(mntfile))) {
379 		char *path[] = { mntent->mnt_dir, NULL };
380 
381 		if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
382 			continue;
383 
384 		fts = fts_open(path, 0, NULL);
385 		if (!fts)
386 			continue;
387 
388 		while ((ftse = fts_read(fts))) {
389 			if (!(ftse->fts_info & FTS_F))
390 				continue;
391 			fd = open_obj_pinned(ftse->fts_path, true);
392 			if (fd < 0)
393 				continue;
394 
395 			objtype = get_fd_type(fd);
396 			if (objtype != type) {
397 				close(fd);
398 				continue;
399 			}
400 			memset(&pinned_info, 0, sizeof(pinned_info));
401 			err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
402 			if (err) {
403 				close(fd);
404 				continue;
405 			}
406 
407 			obj_node = malloc(sizeof(*obj_node));
408 			if (!obj_node) {
409 				close(fd);
410 				fts_close(fts);
411 				fclose(mntfile);
412 				return -1;
413 			}
414 
415 			memset(obj_node, 0, sizeof(*obj_node));
416 			obj_node->id = pinned_info.id;
417 			obj_node->path = strdup(ftse->fts_path);
418 			hash_add(tab->table, &obj_node->hash, obj_node->id);
419 
420 			close(fd);
421 		}
422 		fts_close(fts);
423 	}
424 	fclose(mntfile);
425 	return 0;
426 }
427 
delete_pinned_obj_table(struct pinned_obj_table * tab)428 void delete_pinned_obj_table(struct pinned_obj_table *tab)
429 {
430 	struct pinned_obj *obj;
431 	struct hlist_node *tmp;
432 	unsigned int bkt;
433 
434 	hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
435 		hash_del(&obj->hash);
436 		free(obj->path);
437 		free(obj);
438 	}
439 }
440 
get_page_size(void)441 unsigned int get_page_size(void)
442 {
443 	static int result;
444 
445 	if (!result)
446 		result = getpagesize();
447 	return result;
448 }
449 
get_possible_cpus(void)450 unsigned int get_possible_cpus(void)
451 {
452 	int cpus = libbpf_num_possible_cpus();
453 
454 	if (cpus < 0) {
455 		p_err("Can't get # of possible cpus: %s", strerror(-cpus));
456 		exit(-1);
457 	}
458 	return cpus;
459 }
460 
461 static char *
ifindex_to_name_ns(__u32 ifindex,__u32 ns_dev,__u32 ns_ino,char * buf)462 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
463 {
464 	struct stat st;
465 	int err;
466 
467 	err = stat("/proc/self/ns/net", &st);
468 	if (err) {
469 		p_err("Can't stat /proc/self: %s", strerror(errno));
470 		return NULL;
471 	}
472 
473 	if (st.st_dev != ns_dev || st.st_ino != ns_ino)
474 		return NULL;
475 
476 	return if_indextoname(ifindex, buf);
477 }
478 
read_sysfs_hex_int(char * path)479 static int read_sysfs_hex_int(char *path)
480 {
481 	char vendor_id_buf[8];
482 	int len;
483 	int fd;
484 
485 	fd = open(path, O_RDONLY);
486 	if (fd < 0) {
487 		p_err("Can't open %s: %s", path, strerror(errno));
488 		return -1;
489 	}
490 
491 	len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
492 	close(fd);
493 	if (len < 0) {
494 		p_err("Can't read %s: %s", path, strerror(errno));
495 		return -1;
496 	}
497 	if (len >= (int)sizeof(vendor_id_buf)) {
498 		p_err("Value in %s too long", path);
499 		return -1;
500 	}
501 
502 	vendor_id_buf[len] = 0;
503 
504 	return strtol(vendor_id_buf, NULL, 0);
505 }
506 
read_sysfs_netdev_hex_int(char * devname,const char * entry_name)507 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
508 {
509 	char full_path[64];
510 
511 	snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
512 		 devname, entry_name);
513 
514 	return read_sysfs_hex_int(full_path);
515 }
516 
517 const char *
ifindex_to_bfd_params(__u32 ifindex,__u64 ns_dev,__u64 ns_ino,const char ** opt)518 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
519 		      const char **opt)
520 {
521 	char devname[IF_NAMESIZE];
522 	int vendor_id;
523 	int device_id;
524 
525 	if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
526 		p_err("Can't get net device name for ifindex %d: %s", ifindex,
527 		      strerror(errno));
528 		return NULL;
529 	}
530 
531 	vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
532 	if (vendor_id < 0) {
533 		p_err("Can't get device vendor id for %s", devname);
534 		return NULL;
535 	}
536 
537 	switch (vendor_id) {
538 	case 0x19ee:
539 		device_id = read_sysfs_netdev_hex_int(devname, "device");
540 		if (device_id != 0x4000 &&
541 		    device_id != 0x6000 &&
542 		    device_id != 0x6003)
543 			p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
544 		*opt = "ctx4";
545 		return "NFP-6xxx";
546 	default:
547 		p_err("Can't get bfd arch name for device vendor id 0x%04x",
548 		      vendor_id);
549 		return NULL;
550 	}
551 }
552 
print_dev_plain(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)553 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
554 {
555 	char name[IF_NAMESIZE];
556 
557 	if (!ifindex)
558 		return;
559 
560 	printf("  offloaded_to ");
561 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
562 		printf("%s", name);
563 	else
564 		printf("ifindex %u ns_dev %llu ns_ino %llu",
565 		       ifindex, ns_dev, ns_inode);
566 }
567 
print_dev_json(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)568 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
569 {
570 	char name[IF_NAMESIZE];
571 
572 	if (!ifindex)
573 		return;
574 
575 	jsonw_name(json_wtr, "dev");
576 	jsonw_start_object(json_wtr);
577 	jsonw_uint_field(json_wtr, "ifindex", ifindex);
578 	jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
579 	jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
580 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
581 		jsonw_string_field(json_wtr, "ifname", name);
582 	jsonw_end_object(json_wtr);
583 }
584 
parse_u32_arg(int * argc,char *** argv,__u32 * val,const char * what)585 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
586 {
587 	char *endptr;
588 
589 	NEXT_ARGP();
590 
591 	if (*val) {
592 		p_err("%s already specified", what);
593 		return -1;
594 	}
595 
596 	*val = strtoul(**argv, &endptr, 0);
597 	if (*endptr) {
598 		p_err("can't parse %s as %s", **argv, what);
599 		return -1;
600 	}
601 	NEXT_ARGP();
602 
603 	return 0;
604 }
605