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
45 #include <linux/limits.h>
46 #include <linux/magic.h>
48 #include <sys/mount.h>
50 #include <sys/types.h>
58 #define BPF_FS_MAGIC 0xcafe4a11
61 void p_err(const char *fmt, ...)
67 jsonw_start_object(json_wtr);
68 jsonw_name(json_wtr, "error");
69 jsonw_vprintf_enquote(json_wtr, fmt, ap);
70 jsonw_end_object(json_wtr);
72 fprintf(stderr, "Error: ");
73 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
79 void p_info(const char *fmt, ...)
87 vfprintf(stderr, fmt, ap);
88 fprintf(stderr, "\n");
92 static bool is_bpffs(char *path)
96 if (statfs(path, &st_fs) < 0)
99 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
102 static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
104 bool bind_done = false;
106 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
107 if (errno != EINVAL || bind_done) {
108 snprintf(buff, bufflen,
109 "mount --make-private %s failed: %s",
110 target, strerror(errno));
114 if (mount(target, target, "none", MS_BIND, NULL)) {
115 snprintf(buff, bufflen,
116 "mount --bind %s %s failed: %s",
117 target, target, strerror(errno));
124 if (mount("bpf", target, "bpf", 0, "mode=0700")) {
125 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
126 target, strerror(errno));
133 int open_obj_pinned(char *path)
137 fd = bpf_obj_get(path);
139 p_err("bpf obj get (%s): %s", path,
140 errno == EACCES && !is_bpffs(dirname(path)) ?
141 "directory not in bpf file system (bpffs)" :
149 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
151 enum bpf_obj_type type;
154 fd = open_obj_pinned(path);
158 type = get_fd_type(fd);
163 if (type != exp_type) {
164 p_err("incorrect object type: %s", get_fd_type_name(type));
172 int do_pin_fd(int fd, const char *name)
174 char err_str[ERR_MAX_LEN];
179 err = bpf_obj_pin(fd, name);
183 file = malloc(strlen(name) + 1);
187 if (errno != EPERM || is_bpffs(dir)) {
188 p_err("can't pin the object (%s): %s", name, strerror(errno));
192 /* Attempt to mount bpffs, then retry pinning. */
193 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
195 err = bpf_obj_pin(fd, name);
197 p_err("can't pin the object (%s): %s", name,
200 err_str[ERR_MAX_LEN - 1] = '\0';
201 p_err("can't mount BPF file system to pin the object (%s): %s",
211 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
218 if (!is_prefix(*argv, "id")) {
219 p_err("expected 'id' got %s", *argv);
224 id = strtoul(*argv, &endptr, 0);
226 p_err("can't parse %s as ID", *argv);
234 fd = get_fd_by_id(id);
236 p_err("can't get prog by id (%u): %s", id, strerror(errno));
240 err = do_pin_fd(fd, *argv);
246 const char *get_fd_type_name(enum bpf_obj_type type)
248 static const char * const names[] = {
249 [BPF_OBJ_UNKNOWN] = "unknown",
250 [BPF_OBJ_PROG] = "prog",
251 [BPF_OBJ_MAP] = "map",
254 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
255 return names[BPF_OBJ_UNKNOWN];
260 int get_fd_type(int fd)
266 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
268 n = readlink(path, buf, sizeof(buf));
270 p_err("can't read link type: %s", strerror(errno));
273 if (n == sizeof(path)) {
274 p_err("can't read link type: path too long!");
278 if (strstr(buf, "bpf-map"))
280 else if (strstr(buf, "bpf-prog"))
283 return BPF_OBJ_UNKNOWN;
286 char *get_fdinfo(int fd, const char *key)
294 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
296 fdi = fopen(path, "r");
298 p_err("can't open fdinfo: %s", strerror(errno));
302 while ((n = getline(&line, &line_n, fdi))) {
306 if (!strstr(line, key))
311 value = strchr(line, '\t');
312 if (!value || !value[1]) {
313 p_err("malformed fdinfo!?");
320 memmove(line, value, len);
321 line[len - 1] = '\0';
326 p_err("key '%s' not found in fdinfo", key);
332 void print_data_json(uint8_t *data, size_t len)
336 jsonw_start_array(json_wtr);
337 for (i = 0; i < len; i++)
338 jsonw_printf(json_wtr, "%d", data[i]);
339 jsonw_end_array(json_wtr);
342 void print_hex_data_json(uint8_t *data, size_t len)
346 jsonw_start_array(json_wtr);
347 for (i = 0; i < len; i++)
348 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
349 jsonw_end_array(json_wtr);
352 int build_pinned_obj_table(struct pinned_obj_table *tab,
353 enum bpf_obj_type type)
355 struct bpf_prog_info pinned_info = {};
356 struct pinned_obj *obj_node = NULL;
357 __u32 len = sizeof(pinned_info);
358 struct mntent *mntent = NULL;
359 enum bpf_obj_type objtype;
360 FILE *mntfile = NULL;
365 mntfile = setmntent("/proc/mounts", "r");
369 while ((mntent = getmntent(mntfile))) {
370 char *path[] = { mntent->mnt_dir, NULL };
372 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
375 fts = fts_open(path, 0, NULL);
379 while ((ftse = fts_read(fts))) {
380 if (!(ftse->fts_info & FTS_F))
382 fd = open_obj_pinned(ftse->fts_path);
386 objtype = get_fd_type(fd);
387 if (objtype != type) {
391 memset(&pinned_info, 0, sizeof(pinned_info));
392 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
398 obj_node = malloc(sizeof(*obj_node));
406 memset(obj_node, 0, sizeof(*obj_node));
407 obj_node->id = pinned_info.id;
408 obj_node->path = strdup(ftse->fts_path);
409 hash_add(tab->table, &obj_node->hash, obj_node->id);
419 void delete_pinned_obj_table(struct pinned_obj_table *tab)
421 struct pinned_obj *obj;
422 struct hlist_node *tmp;
425 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
426 hash_del(&obj->hash);
432 unsigned int get_page_size(void)
437 result = getpagesize();
441 unsigned int get_possible_cpus(void)
443 static unsigned int result;
452 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
454 p_err("can't open sysfs possible cpus");
458 n = read(fd, buf, sizeof(buf));
460 p_err("can't read sysfs possible cpus");
465 if (n == sizeof(buf)) {
466 p_err("read sysfs possible cpus overflow");
472 while (*ptr && *ptr != '\n') {
475 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
478 ptr = strchr(ptr, '-') + 1;
479 } else if (sscanf(ptr, "%u", &a) == 1) {
485 while (isdigit(*ptr))
497 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
502 err = stat("/proc/self/ns/net", &st);
504 p_err("Can't stat /proc/self: %s", strerror(errno));
508 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
511 return if_indextoname(ifindex, buf);
514 static int read_sysfs_hex_int(char *path)
516 char vendor_id_buf[8];
520 fd = open(path, O_RDONLY);
522 p_err("Can't open %s: %s", path, strerror(errno));
526 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
529 p_err("Can't read %s: %s", path, strerror(errno));
532 if (len >= (int)sizeof(vendor_id_buf)) {
533 p_err("Value in %s too long", path);
537 vendor_id_buf[len] = 0;
539 return strtol(vendor_id_buf, NULL, 0);
542 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
546 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
547 devname, entry_name);
549 return read_sysfs_hex_int(full_path);
552 const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino)
554 char devname[IF_NAMESIZE];
558 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
559 p_err("Can't get net device name for ifindex %d: %s", ifindex,
564 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
566 p_err("Can't get device vendor id for %s", devname);
572 device_id = read_sysfs_netdev_hex_int(devname, "device");
573 if (device_id != 0x4000 &&
574 device_id != 0x6000 &&
576 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
579 p_err("Can't get bfd arch name for device vendor id 0x%04x",
585 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
587 char name[IF_NAMESIZE];
593 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
596 printf("ifindex %u ns_dev %llu ns_ino %llu",
597 ifindex, ns_dev, ns_inode);
600 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
602 char name[IF_NAMESIZE];
607 jsonw_name(json_wtr, "dev");
608 jsonw_start_object(json_wtr);
609 jsonw_uint_field(json_wtr, "ifindex", ifindex);
610 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
611 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
612 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
613 jsonw_string_field(json_wtr, "ifname", name);
614 jsonw_end_object(json_wtr);