2 * Copyright (C) 2017-2018 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
47 #include <linux/limits.h>
48 #include <linux/magic.h>
50 #include <sys/mount.h>
52 #include <sys/types.h>
60 #define BPF_FS_MAGIC 0xcafe4a11
63 void p_err(const char *fmt, ...)
69 jsonw_start_object(json_wtr);
70 jsonw_name(json_wtr, "error");
71 jsonw_vprintf_enquote(json_wtr, fmt, ap);
72 jsonw_end_object(json_wtr);
74 fprintf(stderr, "Error: ");
75 vfprintf(stderr, fmt, ap);
76 fprintf(stderr, "\n");
81 void p_info(const char *fmt, ...)
89 vfprintf(stderr, fmt, ap);
90 fprintf(stderr, "\n");
94 static bool is_bpffs(char *path)
98 if (statfs(path, &st_fs) < 0)
101 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
104 static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
106 bool bind_done = false;
108 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
109 if (errno != EINVAL || bind_done) {
110 snprintf(buff, bufflen,
111 "mount --make-private %s failed: %s",
112 target, strerror(errno));
116 if (mount(target, target, "none", MS_BIND, NULL)) {
117 snprintf(buff, bufflen,
118 "mount --bind %s %s failed: %s",
119 target, target, strerror(errno));
126 if (mount("bpf", target, "bpf", 0, "mode=0700")) {
127 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
128 target, strerror(errno));
135 int open_obj_pinned(char *path)
139 fd = bpf_obj_get(path);
141 p_err("bpf obj get (%s): %s", path,
142 errno == EACCES && !is_bpffs(dirname(path)) ?
143 "directory not in bpf file system (bpffs)" :
151 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
153 enum bpf_obj_type type;
156 fd = open_obj_pinned(path);
160 type = get_fd_type(fd);
165 if (type != exp_type) {
166 p_err("incorrect object type: %s", get_fd_type_name(type));
174 int do_pin_fd(int fd, const char *name)
176 char err_str[ERR_MAX_LEN];
181 err = bpf_obj_pin(fd, name);
185 file = malloc(strlen(name) + 1);
189 if (errno != EPERM || is_bpffs(dir)) {
190 p_err("can't pin the object (%s): %s", name, strerror(errno));
194 /* Attempt to mount bpffs, then retry pinning. */
195 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
197 err = bpf_obj_pin(fd, name);
199 p_err("can't pin the object (%s): %s", name,
202 err_str[ERR_MAX_LEN - 1] = '\0';
203 p_err("can't mount BPF file system to pin the object (%s): %s",
213 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
220 if (!is_prefix(*argv, "id")) {
221 p_err("expected 'id' got %s", *argv);
226 id = strtoul(*argv, &endptr, 0);
228 p_err("can't parse %s as ID", *argv);
236 fd = get_fd_by_id(id);
238 p_err("can't get prog by id (%u): %s", id, strerror(errno));
242 err = do_pin_fd(fd, *argv);
248 const char *get_fd_type_name(enum bpf_obj_type type)
250 static const char * const names[] = {
251 [BPF_OBJ_UNKNOWN] = "unknown",
252 [BPF_OBJ_PROG] = "prog",
253 [BPF_OBJ_MAP] = "map",
256 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
257 return names[BPF_OBJ_UNKNOWN];
262 int get_fd_type(int fd)
268 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
270 n = readlink(path, buf, sizeof(buf));
272 p_err("can't read link type: %s", strerror(errno));
275 if (n == sizeof(path)) {
276 p_err("can't read link type: path too long!");
280 if (strstr(buf, "bpf-map"))
282 else if (strstr(buf, "bpf-prog"))
285 return BPF_OBJ_UNKNOWN;
288 char *get_fdinfo(int fd, const char *key)
296 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
298 fdi = fopen(path, "r");
300 p_err("can't open fdinfo: %s", strerror(errno));
304 while ((n = getline(&line, &line_n, fdi))) {
308 if (!strstr(line, key))
313 value = strchr(line, '\t');
314 if (!value || !value[1]) {
315 p_err("malformed fdinfo!?");
322 memmove(line, value, len);
323 line[len - 1] = '\0';
328 p_err("key '%s' not found in fdinfo", key);
334 void print_data_json(uint8_t *data, size_t len)
338 jsonw_start_array(json_wtr);
339 for (i = 0; i < len; i++)
340 jsonw_printf(json_wtr, "%d", data[i]);
341 jsonw_end_array(json_wtr);
344 void print_hex_data_json(uint8_t *data, size_t len)
348 jsonw_start_array(json_wtr);
349 for (i = 0; i < len; i++)
350 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
351 jsonw_end_array(json_wtr);
354 int build_pinned_obj_table(struct pinned_obj_table *tab,
355 enum bpf_obj_type type)
357 struct bpf_prog_info pinned_info = {};
358 struct pinned_obj *obj_node = NULL;
359 __u32 len = sizeof(pinned_info);
360 struct mntent *mntent = NULL;
361 enum bpf_obj_type objtype;
362 FILE *mntfile = NULL;
367 mntfile = setmntent("/proc/mounts", "r");
371 while ((mntent = getmntent(mntfile))) {
372 char *path[] = { mntent->mnt_dir, NULL };
374 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
377 fts = fts_open(path, 0, NULL);
381 while ((ftse = fts_read(fts))) {
382 if (!(ftse->fts_info & FTS_F))
384 fd = open_obj_pinned(ftse->fts_path);
388 objtype = get_fd_type(fd);
389 if (objtype != type) {
393 memset(&pinned_info, 0, sizeof(pinned_info));
394 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
400 obj_node = malloc(sizeof(*obj_node));
408 memset(obj_node, 0, sizeof(*obj_node));
409 obj_node->id = pinned_info.id;
410 obj_node->path = strdup(ftse->fts_path);
411 hash_add(tab->table, &obj_node->hash, obj_node->id);
421 void delete_pinned_obj_table(struct pinned_obj_table *tab)
423 struct pinned_obj *obj;
424 struct hlist_node *tmp;
427 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
428 hash_del(&obj->hash);
434 unsigned int get_page_size(void)
439 result = getpagesize();
443 unsigned int get_possible_cpus(void)
445 static unsigned int result;
454 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
456 p_err("can't open sysfs possible cpus");
460 n = read(fd, buf, sizeof(buf));
462 p_err("can't read sysfs possible cpus");
467 if (n == sizeof(buf)) {
468 p_err("read sysfs possible cpus overflow");
474 while (*ptr && *ptr != '\n') {
477 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
480 ptr = strchr(ptr, '-') + 1;
481 } else if (sscanf(ptr, "%u", &a) == 1) {
487 while (isdigit(*ptr))
499 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
504 err = stat("/proc/self/ns/net", &st);
506 p_err("Can't stat /proc/self: %s", strerror(errno));
510 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
513 return if_indextoname(ifindex, buf);
516 static int read_sysfs_hex_int(char *path)
518 char vendor_id_buf[8];
522 fd = open(path, O_RDONLY);
524 p_err("Can't open %s: %s", path, strerror(errno));
528 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
531 p_err("Can't read %s: %s", path, strerror(errno));
534 if (len >= (int)sizeof(vendor_id_buf)) {
535 p_err("Value in %s too long", path);
539 vendor_id_buf[len] = 0;
541 return strtol(vendor_id_buf, NULL, 0);
544 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
548 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
549 devname, entry_name);
551 return read_sysfs_hex_int(full_path);
554 const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino)
556 char devname[IF_NAMESIZE];
560 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
561 p_err("Can't get net device name for ifindex %d: %s", ifindex,
566 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
568 p_err("Can't get device vendor id for %s", devname);
574 device_id = read_sysfs_netdev_hex_int(devname, "device");
575 if (device_id != 0x4000 &&
576 device_id != 0x6000 &&
578 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];
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);