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>
24 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
29 #define BPF_FS_MAGIC 0xcafe4a11
32 void p_err(const char *fmt
, ...)
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
);
43 fprintf(stderr
, "Error: ");
44 vfprintf(stderr
, fmt
, ap
);
45 fprintf(stderr
, "\n");
50 void p_info(const char *fmt
, ...)
58 vfprintf(stderr
, fmt
, ap
);
59 fprintf(stderr
, "\n");
63 static bool is_bpffs(char *path
)
67 if (statfs(path
, &st_fs
) < 0)
70 return (unsigned long)st_fs
.f_type
== BPF_FS_MAGIC
;
73 void set_max_rlimit(void)
75 struct rlimit rinf
= { RLIM_INFINITY
, RLIM_INFINITY
};
77 setrlimit(RLIMIT_MEMLOCK
, &rinf
);
81 mnt_fs(const char *target
, const char *type
, char *buff
, size_t bufflen
)
83 bool bind_done
= false;
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
));
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
));
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
));
112 int mount_tracefs(const char *target
)
114 char err_str
[ERR_MAX_LEN
];
117 err
= mnt_fs(target
, "tracefs", err_str
, ERR_MAX_LEN
);
119 err_str
[ERR_MAX_LEN
- 1] = '\0';
120 p_err("can't mount tracefs: %s", err_str
);
126 int open_obj_pinned(char *path
, bool quiet
)
130 fd
= bpf_obj_get(path
);
133 p_err("bpf obj get (%s): %s", path
,
134 errno
== EACCES
&& !is_bpffs(dirname(path
)) ?
135 "directory not in bpf file system (bpffs)" :
143 int open_obj_pinned_any(char *path
, enum bpf_obj_type exp_type
)
145 enum bpf_obj_type type
;
148 fd
= open_obj_pinned(path
, false);
152 type
= get_fd_type(fd
);
157 if (type
!= exp_type
) {
158 p_err("incorrect object type: %s", get_fd_type_name(type
));
166 int mount_bpffs_for_pin(const char *name
)
168 char err_str
[ERR_MAX_LEN
];
173 file
= malloc(strlen(name
) + 1);
178 /* nothing to do if already mounted */
182 p_err("no BPF file system found, not mounting it due to --nomount option");
187 err
= mnt_fs(dir
, "bpf", err_str
, ERR_MAX_LEN
);
189 err_str
[ERR_MAX_LEN
- 1] = '\0';
190 p_err("can't mount BPF file system to pin the object (%s): %s",
199 int do_pin_fd(int fd
, const char *name
)
203 err
= mount_bpffs_for_pin(name
);
207 err
= bpf_obj_pin(fd
, name
);
209 p_err("can't pin the object (%s): %s", name
, strerror(errno
));
214 int do_pin_any(int argc
, char **argv
, int (*get_fd
)(int *, char ***))
219 fd
= get_fd(&argc
, &argv
);
223 err
= do_pin_fd(fd
, *argv
);
229 const char *get_fd_type_name(enum bpf_obj_type type
)
231 static const char * const names
[] = {
232 [BPF_OBJ_UNKNOWN
] = "unknown",
233 [BPF_OBJ_PROG
] = "prog",
234 [BPF_OBJ_MAP
] = "map",
237 if (type
< 0 || type
>= ARRAY_SIZE(names
) || !names
[type
])
238 return names
[BPF_OBJ_UNKNOWN
];
243 int get_fd_type(int fd
)
249 snprintf(path
, sizeof(path
), "/proc/self/fd/%d", fd
);
251 n
= readlink(path
, buf
, sizeof(buf
));
253 p_err("can't read link type: %s", strerror(errno
));
256 if (n
== sizeof(path
)) {
257 p_err("can't read link type: path too long!");
261 if (strstr(buf
, "bpf-map"))
263 else if (strstr(buf
, "bpf-prog"))
266 return BPF_OBJ_UNKNOWN
;
269 char *get_fdinfo(int fd
, const char *key
)
277 snprintf(path
, sizeof(path
), "/proc/self/fdinfo/%d", fd
);
279 fdi
= fopen(path
, "r");
283 while ((n
= getline(&line
, &line_n
, fdi
)) > 0) {
287 if (!strstr(line
, key
))
292 value
= strchr(line
, '\t');
293 if (!value
|| !value
[1]) {
300 memmove(line
, value
, len
);
301 line
[len
- 1] = '\0';
311 void print_data_json(uint8_t *data
, size_t len
)
315 jsonw_start_array(json_wtr
);
316 for (i
= 0; i
< len
; i
++)
317 jsonw_printf(json_wtr
, "%d", data
[i
]);
318 jsonw_end_array(json_wtr
);
321 void print_hex_data_json(uint8_t *data
, size_t len
)
325 jsonw_start_array(json_wtr
);
326 for (i
= 0; i
< len
; i
++)
327 jsonw_printf(json_wtr
, "\"0x%02hhx\"", data
[i
]);
328 jsonw_end_array(json_wtr
);
331 int build_pinned_obj_table(struct pinned_obj_table
*tab
,
332 enum bpf_obj_type type
)
334 struct bpf_prog_info pinned_info
= {};
335 struct pinned_obj
*obj_node
= NULL
;
336 __u32 len
= sizeof(pinned_info
);
337 struct mntent
*mntent
= NULL
;
338 enum bpf_obj_type objtype
;
339 FILE *mntfile
= NULL
;
344 mntfile
= setmntent("/proc/mounts", "r");
348 while ((mntent
= getmntent(mntfile
))) {
349 char *path
[] = { mntent
->mnt_dir
, NULL
};
351 if (strncmp(mntent
->mnt_type
, "bpf", 3) != 0)
354 fts
= fts_open(path
, 0, NULL
);
358 while ((ftse
= fts_read(fts
))) {
359 if (!(ftse
->fts_info
& FTS_F
))
361 fd
= open_obj_pinned(ftse
->fts_path
, true);
365 objtype
= get_fd_type(fd
);
366 if (objtype
!= type
) {
370 memset(&pinned_info
, 0, sizeof(pinned_info
));
371 err
= bpf_obj_get_info_by_fd(fd
, &pinned_info
, &len
);
377 obj_node
= malloc(sizeof(*obj_node
));
385 memset(obj_node
, 0, sizeof(*obj_node
));
386 obj_node
->id
= pinned_info
.id
;
387 obj_node
->path
= strdup(ftse
->fts_path
);
388 hash_add(tab
->table
, &obj_node
->hash
, obj_node
->id
);
398 void delete_pinned_obj_table(struct pinned_obj_table
*tab
)
400 struct pinned_obj
*obj
;
401 struct hlist_node
*tmp
;
404 hash_for_each_safe(tab
->table
, bkt
, tmp
, obj
, hash
) {
405 hash_del(&obj
->hash
);
411 unsigned int get_page_size(void)
416 result
= getpagesize();
420 unsigned int get_possible_cpus(void)
422 int cpus
= libbpf_num_possible_cpus();
425 p_err("Can't get # of possible cpus: %s", strerror(-cpus
));
432 ifindex_to_name_ns(__u32 ifindex
, __u32 ns_dev
, __u32 ns_ino
, char *buf
)
437 err
= stat("/proc/self/ns/net", &st
);
439 p_err("Can't stat /proc/self: %s", strerror(errno
));
443 if (st
.st_dev
!= ns_dev
|| st
.st_ino
!= ns_ino
)
446 return if_indextoname(ifindex
, buf
);
449 static int read_sysfs_hex_int(char *path
)
451 char vendor_id_buf
[8];
455 fd
= open(path
, O_RDONLY
);
457 p_err("Can't open %s: %s", path
, strerror(errno
));
461 len
= read(fd
, vendor_id_buf
, sizeof(vendor_id_buf
));
464 p_err("Can't read %s: %s", path
, strerror(errno
));
467 if (len
>= (int)sizeof(vendor_id_buf
)) {
468 p_err("Value in %s too long", path
);
472 vendor_id_buf
[len
] = 0;
474 return strtol(vendor_id_buf
, NULL
, 0);
477 static int read_sysfs_netdev_hex_int(char *devname
, const char *entry_name
)
481 snprintf(full_path
, sizeof(full_path
), "/sys/class/net/%s/device/%s",
482 devname
, entry_name
);
484 return read_sysfs_hex_int(full_path
);
488 ifindex_to_bfd_params(__u32 ifindex
, __u64 ns_dev
, __u64 ns_ino
,
491 char devname
[IF_NAMESIZE
];
495 if (!ifindex_to_name_ns(ifindex
, ns_dev
, ns_ino
, devname
)) {
496 p_err("Can't get net device name for ifindex %d: %s", ifindex
,
501 vendor_id
= read_sysfs_netdev_hex_int(devname
, "vendor");
503 p_err("Can't get device vendor id for %s", devname
);
509 device_id
= read_sysfs_netdev_hex_int(devname
, "device");
510 if (device_id
!= 0x4000 &&
511 device_id
!= 0x6000 &&
513 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
517 p_err("Can't get bfd arch name for device vendor id 0x%04x",
523 void print_dev_plain(__u32 ifindex
, __u64 ns_dev
, __u64 ns_inode
)
525 char name
[IF_NAMESIZE
];
530 printf(" offloaded_to ");
531 if (ifindex_to_name_ns(ifindex
, ns_dev
, ns_inode
, name
))
534 printf("ifindex %u ns_dev %llu ns_ino %llu",
535 ifindex
, ns_dev
, ns_inode
);
538 void print_dev_json(__u32 ifindex
, __u64 ns_dev
, __u64 ns_inode
)
540 char name
[IF_NAMESIZE
];
545 jsonw_name(json_wtr
, "dev");
546 jsonw_start_object(json_wtr
);
547 jsonw_uint_field(json_wtr
, "ifindex", ifindex
);
548 jsonw_uint_field(json_wtr
, "ns_dev", ns_dev
);
549 jsonw_uint_field(json_wtr
, "ns_inode", ns_inode
);
550 if (ifindex_to_name_ns(ifindex
, ns_dev
, ns_inode
, name
))
551 jsonw_string_field(json_wtr
, "ifname", name
);
552 jsonw_end_object(json_wtr
);
555 int parse_u32_arg(int *argc
, char ***argv
, __u32
*val
, const char *what
)
562 p_err("%s already specified", what
);
566 *val
= strtoul(**argv
, &endptr
, 0);
568 p_err("can't parse %s as %s", **argv
, what
);
577 print_all_levels(__maybe_unused
enum libbpf_print_level level
,
578 const char *format
, va_list args
)
580 return vfprintf(stderr
, format
, args
);