2 * Copyright (C) 2017 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>
47 #include <sys/mount.h>
48 #include <sys/types.h>
55 void p_err(const char *fmt, ...)
61 jsonw_start_object(json_wtr);
62 jsonw_name(json_wtr, "error");
63 jsonw_vprintf_enquote(json_wtr, fmt, ap);
64 jsonw_end_object(json_wtr);
66 fprintf(stderr, "Error: ");
67 vfprintf(stderr, fmt, ap);
68 fprintf(stderr, "\n");
73 void p_info(const char *fmt, ...)
81 vfprintf(stderr, fmt, ap);
82 fprintf(stderr, "\n");
86 static bool is_bpffs(char *path)
90 if (statfs(path, &st_fs) < 0)
93 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
96 static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
98 bool bind_done = false;
100 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
101 if (errno != EINVAL || bind_done) {
102 snprintf(buff, bufflen,
103 "mount --make-private %s failed: %s",
104 target, strerror(errno));
108 if (mount(target, target, "none", MS_BIND, NULL)) {
109 snprintf(buff, bufflen,
110 "mount --bind %s %s failed: %s",
111 target, target, strerror(errno));
118 if (mount("bpf", target, "bpf", 0, "mode=0700")) {
119 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
120 target, strerror(errno));
127 int open_obj_pinned(char *path)
131 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);
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 do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
168 char err_str[ERR_MAX_LEN];
176 if (!is_prefix(*argv, "id")) {
177 p_err("expected 'id' got %s", *argv);
182 id = strtoul(*argv, &endptr, 0);
184 p_err("can't parse %s as ID", *argv);
192 fd = get_fd_by_id(id);
194 p_err("can't get prog by id (%u): %s", id, strerror(errno));
198 err = bpf_obj_pin(fd, *argv);
202 file = malloc(strlen(*argv) + 1);
206 if (errno != EPERM || is_bpffs(dir)) {
207 p_err("can't pin the object (%s): %s", *argv, strerror(errno));
211 /* Attempt to mount bpffs, then retry pinning. */
212 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
214 err = bpf_obj_pin(fd, *argv);
216 p_err("can't pin the object (%s): %s", *argv,
219 err_str[ERR_MAX_LEN - 1] = '\0';
220 p_err("can't mount BPF file system to pin the object (%s): %s",
231 const char *get_fd_type_name(enum bpf_obj_type type)
233 static const char * const names[] = {
234 [BPF_OBJ_UNKNOWN] = "unknown",
235 [BPF_OBJ_PROG] = "prog",
236 [BPF_OBJ_MAP] = "map",
239 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
240 return names[BPF_OBJ_UNKNOWN];
245 int get_fd_type(int fd)
251 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
253 n = readlink(path, buf, sizeof(buf));
255 p_err("can't read link type: %s", strerror(errno));
258 if (n == sizeof(path)) {
259 p_err("can't read link type: path too long!");
263 if (strstr(buf, "bpf-map"))
265 else if (strstr(buf, "bpf-prog"))
268 return BPF_OBJ_UNKNOWN;
271 char *get_fdinfo(int fd, const char *key)
279 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
281 fdi = fopen(path, "r");
283 p_err("can't open fdinfo: %s", strerror(errno));
287 while ((n = getline(&line, &line_n, fdi))) {
291 if (!strstr(line, key))
296 value = strchr(line, '\t');
297 if (!value || !value[1]) {
298 p_err("malformed fdinfo!?");
305 memmove(line, value, len);
306 line[len - 1] = '\0';
311 p_err("key '%s' not found in fdinfo", key);
317 void print_hex_data_json(uint8_t *data, size_t len)
321 jsonw_start_array(json_wtr);
322 for (i = 0; i < len; i++)
323 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
324 jsonw_end_array(json_wtr);
327 int build_pinned_obj_table(struct pinned_obj_table *tab,
328 enum bpf_obj_type type)
330 struct bpf_prog_info pinned_info = {};
331 struct pinned_obj *obj_node = NULL;
332 __u32 len = sizeof(pinned_info);
333 struct mntent *mntent = NULL;
334 enum bpf_obj_type objtype;
335 FILE *mntfile = NULL;
340 mntfile = setmntent("/proc/mounts", "r");
344 while ((mntent = getmntent(mntfile))) {
345 char *path[] = { mntent->mnt_dir, NULL };
347 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
350 fts = fts_open(path, 0, NULL);
354 while ((ftse = fts_read(fts))) {
355 if (!(ftse->fts_info & FTS_F))
357 fd = open_obj_pinned(ftse->fts_path);
361 objtype = get_fd_type(fd);
362 if (objtype != type) {
366 memset(&pinned_info, 0, sizeof(pinned_info));
367 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
373 obj_node = malloc(sizeof(*obj_node));
381 memset(obj_node, 0, sizeof(*obj_node));
382 obj_node->id = pinned_info.id;
383 obj_node->path = strdup(ftse->fts_path);
384 hash_add(tab->table, &obj_node->hash, obj_node->id);
394 void delete_pinned_obj_table(struct pinned_obj_table *tab)
396 struct pinned_obj *obj;
397 struct hlist_node *tmp;
400 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
401 hash_del(&obj->hash);