1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
15 #include <linux/limits.h>
16 #include <linux/magic.h>
18 #include <sys/mount.h>
19 #include <sys/resource.h>
28 #define BPF_FS_MAGIC 0xcafe4a11
31 void __printf(1, 2) p_err(const char *fmt, ...)
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);
42 fprintf(stderr, "Error: ");
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
49 void __printf(1, 2) p_info(const char *fmt, ...)
57 vfprintf(stderr, fmt, ap);
58 fprintf(stderr, "\n");
62 static bool is_bpffs(char *path)
66 if (statfs(path, &st_fs) < 0)
69 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
72 void set_max_rlimit(void)
74 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
76 setrlimit(RLIMIT_MEMLOCK, &rinf);
80 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
82 bool bind_done = false;
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));
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));
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));
111 int mount_tracefs(const char *target)
113 char err_str[ERR_MAX_LEN];
116 err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
118 err_str[ERR_MAX_LEN - 1] = '\0';
119 p_err("can't mount tracefs: %s", err_str);
125 int open_obj_pinned(char *path, bool quiet)
129 fd = bpf_obj_get(path);
132 p_err("bpf obj get (%s): %s", path,
133 errno == EACCES && !is_bpffs(dirname(path)) ?
134 "directory not in bpf file system (bpffs)" :
142 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
144 enum bpf_obj_type type;
147 fd = open_obj_pinned(path, false);
151 type = get_fd_type(fd);
156 if (type != exp_type) {
157 p_err("incorrect object type: %s", get_fd_type_name(type));
165 int mount_bpffs_for_pin(const char *name)
167 char err_str[ERR_MAX_LEN];
172 file = malloc(strlen(name) + 1);
177 /* nothing to do if already mounted */
181 p_err("no BPF file system found, not mounting it due to --nomount option");
186 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
188 err_str[ERR_MAX_LEN - 1] = '\0';
189 p_err("can't mount BPF file system to pin the object (%s): %s",
198 int do_pin_fd(int fd, const char *name)
202 err = mount_bpffs_for_pin(name);
206 return bpf_obj_pin(fd, name);
209 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
217 p_err("too few arguments, id ID and FILE path is required");
219 } else if (argc > 3) {
220 p_err("too many arguments");
224 if (!is_prefix(*argv, "id")) {
225 p_err("expected 'id' got %s", *argv);
230 id = strtoul(*argv, &endptr, 0);
232 p_err("can't parse %s as ID", *argv);
237 fd = get_fd_by_id(id);
239 p_err("can't get prog by id (%u): %s", id, strerror(errno));
243 err = do_pin_fd(fd, *argv);
249 const char *get_fd_type_name(enum bpf_obj_type type)
251 static const char * const names[] = {
252 [BPF_OBJ_UNKNOWN] = "unknown",
253 [BPF_OBJ_PROG] = "prog",
254 [BPF_OBJ_MAP] = "map",
257 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
258 return names[BPF_OBJ_UNKNOWN];
263 int get_fd_type(int fd)
269 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
271 n = readlink(path, buf, sizeof(buf));
273 p_err("can't read link type: %s", strerror(errno));
276 if (n == sizeof(path)) {
277 p_err("can't read link type: path too long!");
281 if (strstr(buf, "bpf-map"))
283 else if (strstr(buf, "bpf-prog"))
286 return BPF_OBJ_UNKNOWN;
289 char *get_fdinfo(int fd, const char *key)
297 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
299 fdi = fopen(path, "r");
301 p_err("can't open fdinfo: %s", strerror(errno));
305 while ((n = getline(&line, &line_n, fdi)) > 0) {
309 if (!strstr(line, key))
314 value = strchr(line, '\t');
315 if (!value || !value[1]) {
316 p_err("malformed fdinfo!?");
323 memmove(line, value, len);
324 line[len - 1] = '\0';
329 p_err("key '%s' not found in fdinfo", key);
335 void print_data_json(uint8_t *data, size_t len)
339 jsonw_start_array(json_wtr);
340 for (i = 0; i < len; i++)
341 jsonw_printf(json_wtr, "%d", data[i]);
342 jsonw_end_array(json_wtr);
345 void print_hex_data_json(uint8_t *data, size_t len)
349 jsonw_start_array(json_wtr);
350 for (i = 0; i < len; i++)
351 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
352 jsonw_end_array(json_wtr);
355 int build_pinned_obj_table(struct pinned_obj_table *tab,
356 enum bpf_obj_type type)
358 struct bpf_prog_info pinned_info = {};
359 struct pinned_obj *obj_node = NULL;
360 __u32 len = sizeof(pinned_info);
361 struct mntent *mntent = NULL;
362 enum bpf_obj_type objtype;
363 FILE *mntfile = NULL;
368 mntfile = setmntent("/proc/mounts", "r");
372 while ((mntent = getmntent(mntfile))) {
373 char *path[] = { mntent->mnt_dir, NULL };
375 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
378 fts = fts_open(path, 0, NULL);
382 while ((ftse = fts_read(fts))) {
383 if (!(ftse->fts_info & FTS_F))
385 fd = open_obj_pinned(ftse->fts_path, true);
389 objtype = get_fd_type(fd);
390 if (objtype != type) {
394 memset(&pinned_info, 0, sizeof(pinned_info));
395 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
401 obj_node = malloc(sizeof(*obj_node));
409 memset(obj_node, 0, sizeof(*obj_node));
410 obj_node->id = pinned_info.id;
411 obj_node->path = strdup(ftse->fts_path);
412 hash_add(tab->table, &obj_node->hash, obj_node->id);
422 void delete_pinned_obj_table(struct pinned_obj_table *tab)
424 struct pinned_obj *obj;
425 struct hlist_node *tmp;
428 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
429 hash_del(&obj->hash);
435 unsigned int get_page_size(void)
440 result = getpagesize();
444 unsigned int get_possible_cpus(void)
446 static unsigned int result;
455 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
457 p_err("can't open sysfs possible cpus");
461 n = read(fd, buf, sizeof(buf));
463 p_err("can't read sysfs possible cpus");
468 if (n == sizeof(buf)) {
469 p_err("read sysfs possible cpus overflow");
475 while (*ptr && *ptr != '\n') {
478 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
481 ptr = strchr(ptr, '-') + 1;
482 } else if (sscanf(ptr, "%u", &a) == 1) {
488 while (isdigit(*ptr))
500 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
505 err = stat("/proc/self/ns/net", &st);
507 p_err("Can't stat /proc/self: %s", strerror(errno));
511 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
514 return if_indextoname(ifindex, buf);
517 static int read_sysfs_hex_int(char *path)
519 char vendor_id_buf[8];
523 fd = open(path, O_RDONLY);
525 p_err("Can't open %s: %s", path, strerror(errno));
529 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
532 p_err("Can't read %s: %s", path, strerror(errno));
535 if (len >= (int)sizeof(vendor_id_buf)) {
536 p_err("Value in %s too long", path);
540 vendor_id_buf[len] = 0;
542 return strtol(vendor_id_buf, NULL, 0);
545 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
549 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
550 devname, entry_name);
552 return read_sysfs_hex_int(full_path);
556 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
559 char devname[IF_NAMESIZE];
563 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
564 p_err("Can't get net device name for ifindex %d: %s", ifindex,
569 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
571 p_err("Can't get device vendor id for %s", devname);
577 device_id = read_sysfs_netdev_hex_int(devname, "device");
578 if (device_id != 0x4000 &&
579 device_id != 0x6000 &&
581 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
585 p_err("Can't get bfd arch name for device vendor id 0x%04x",
591 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
593 char name[IF_NAMESIZE];
598 printf(" offloaded_to ");
599 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
602 printf("ifindex %u ns_dev %llu ns_ino %llu",
603 ifindex, ns_dev, ns_inode);
606 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
608 char name[IF_NAMESIZE];
613 jsonw_name(json_wtr, "dev");
614 jsonw_start_object(json_wtr);
615 jsonw_uint_field(json_wtr, "ifindex", ifindex);
616 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
617 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
618 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
619 jsonw_string_field(json_wtr, "ifname", name);
620 jsonw_end_object(json_wtr);
623 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
630 p_err("%s already specified", what);
634 *val = strtoul(**argv, &endptr, 0);
636 p_err("can't parse %s as %s", **argv, what);