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");
303 while ((n
= getline(&line
, &line_n
, fdi
)) > 0) {
307 if (!strstr(line
, key
))
312 value
= strchr(line
, '\t');
313 if (!value
|| !value
[1]) {
320 memmove(line
, value
, len
);
321 line
[len
- 1] = '\0';
331 void print_data_json(uint8_t *data
, size_t len
)
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
);
341 void print_hex_data_json(uint8_t *data
, size_t len
)
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
);
351 int build_pinned_obj_table(struct pinned_obj_table
*tab
,
352 enum bpf_obj_type type
)
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
;
364 mntfile
= setmntent("/proc/mounts", "r");
368 while ((mntent
= getmntent(mntfile
))) {
369 char *path
[] = { mntent
->mnt_dir
, NULL
};
371 if (strncmp(mntent
->mnt_type
, "bpf", 3) != 0)
374 fts
= fts_open(path
, 0, NULL
);
378 while ((ftse
= fts_read(fts
))) {
379 if (!(ftse
->fts_info
& FTS_F
))
381 fd
= open_obj_pinned(ftse
->fts_path
, true);
385 objtype
= get_fd_type(fd
);
386 if (objtype
!= type
) {
390 memset(&pinned_info
, 0, sizeof(pinned_info
));
391 err
= bpf_obj_get_info_by_fd(fd
, &pinned_info
, &len
);
397 obj_node
= malloc(sizeof(*obj_node
));
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
);
418 void delete_pinned_obj_table(struct pinned_obj_table
*tab
)
420 struct pinned_obj
*obj
;
421 struct hlist_node
*tmp
;
424 hash_for_each_safe(tab
->table
, bkt
, tmp
, obj
, hash
) {
425 hash_del(&obj
->hash
);
431 unsigned int get_page_size(void)
436 result
= getpagesize();
440 unsigned int get_possible_cpus(void)
442 static unsigned int result
;
451 fd
= open("/sys/devices/system/cpu/possible", O_RDONLY
);
453 p_err("can't open sysfs possible cpus");
457 n
= read(fd
, buf
, sizeof(buf
));
459 p_err("can't read sysfs possible cpus");
464 if (n
== sizeof(buf
)) {
465 p_err("read sysfs possible cpus overflow");
471 while (*ptr
&& *ptr
!= '\n') {
474 if (sscanf(ptr
, "%u-%u", &a
, &b
) == 2) {
477 ptr
= strchr(ptr
, '-') + 1;
478 } else if (sscanf(ptr
, "%u", &a
) == 1) {
484 while (isdigit(*ptr
))
496 ifindex_to_name_ns(__u32 ifindex
, __u32 ns_dev
, __u32 ns_ino
, char *buf
)
501 err
= stat("/proc/self/ns/net", &st
);
503 p_err("Can't stat /proc/self: %s", strerror(errno
));
507 if (st
.st_dev
!= ns_dev
|| st
.st_ino
!= ns_ino
)
510 return if_indextoname(ifindex
, buf
);
513 static int read_sysfs_hex_int(char *path
)
515 char vendor_id_buf
[8];
519 fd
= open(path
, O_RDONLY
);
521 p_err("Can't open %s: %s", path
, strerror(errno
));
525 len
= read(fd
, vendor_id_buf
, sizeof(vendor_id_buf
));
528 p_err("Can't read %s: %s", path
, strerror(errno
));
531 if (len
>= (int)sizeof(vendor_id_buf
)) {
532 p_err("Value in %s too long", path
);
536 vendor_id_buf
[len
] = 0;
538 return strtol(vendor_id_buf
, NULL
, 0);
541 static int read_sysfs_netdev_hex_int(char *devname
, const char *entry_name
)
545 snprintf(full_path
, sizeof(full_path
), "/sys/class/net/%s/device/%s",
546 devname
, entry_name
);
548 return read_sysfs_hex_int(full_path
);
552 ifindex_to_bfd_params(__u32 ifindex
, __u64 ns_dev
, __u64 ns_ino
,
555 char devname
[IF_NAMESIZE
];
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
,
565 vendor_id
= read_sysfs_netdev_hex_int(devname
, "vendor");
567 p_err("Can't get device vendor id for %s", devname
);
573 device_id
= read_sysfs_netdev_hex_int(devname
, "device");
574 if (device_id
!= 0x4000 &&
575 device_id
!= 0x6000 &&
577 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
581 p_err("Can't get bfd arch name for device vendor id 0x%04x",
587 void print_dev_plain(__u32 ifindex
, __u64 ns_dev
, __u64 ns_inode
)
589 char name
[IF_NAMESIZE
];
594 printf(" offloaded_to ");
595 if (ifindex_to_name_ns(ifindex
, ns_dev
, ns_inode
, name
))
598 printf("ifindex %u ns_dev %llu ns_ino %llu",
599 ifindex
, ns_dev
, ns_inode
);
602 void print_dev_json(__u32 ifindex
, __u64 ns_dev
, __u64 ns_inode
)
604 char name
[IF_NAMESIZE
];
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
);
619 int parse_u32_arg(int *argc
, char ***argv
, __u32
*val
, const char *what
)
626 p_err("%s already specified", what
);
630 *val
= strtoul(**argv
, &endptr
, 0);
632 p_err("can't parse %s as %s", **argv
, what
);