From: Joe Stringer <joe@ovn.org>
To: linux-kernel@vger.kernel.org
Cc: wangnan0@huawei.com, ast@fb.com, daniel@iogearbox.net, acme@kernel.org
Subject: [PATCH perf/core 6/6] tools lib bpf: Add bpf_object__pin()
Date: Wed, 18 Jan 2017 15:57:24 -0800 [thread overview]
Message-ID: <20170118235724.26103-7-joe@ovn.org> (raw)
In-Reply-To: <20170118235724.26103-1-joe@ovn.org>
Add a new API to pin a BPF object to the filesystem. The user can
specify a subdirectory under the BPF filesystem to pin these programs.
For example, with the subdirectory 'foo', programs and maps are pinned:
/sys/fs/bpf/foo/progs/PROG_NAME
/sys/fs/bpf/foo/maps/MAP_NAME
If the user has specified an alternative BPF filesystem mountpoint via
/proc/mounts, that will be read and used instead.
Signed-off-by: Joe Stringer <joe@ovn.org>
---
tools/lib/bpf/libbpf.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++
tools/lib/bpf/libbpf.h | 1 +
2 files changed, 137 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6b651c19870d..181dca0bdacb 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4,6 +4,7 @@
* Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
* Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
* Copyright (C) 2015 Huawei Inc.
+ * Copyright (C) 2016 Nicira, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -31,7 +32,12 @@
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/bpf.h>
+#include <linux/magic.h>
#include <linux/list.h>
+#include <linux/limits.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/vfs.h>
#include <libelf.h>
#include <gelf.h>
@@ -1231,6 +1237,136 @@ int bpf_object__load(struct bpf_object *obj)
return err;
}
+#define stringize(x) #x
+
+static int mount_bpf(char *path)
+{
+ const char *mounts_fmt = "%*s %"stringize(PATH_MAX)"s %#"stringize(NAME_MAX)"s %*s\n";
+ struct statfs st_fs;
+ char type[NAME_MAX];
+ int err = 0;
+ FILE *fp;
+
+ /* Populate 'path'. */
+ fp = fopen("/proc/mounts", "r");
+ if (fp) {
+ while (fscanf(fp, mounts_fmt, path, type) == 2) {
+ if (!strcmp(type, "bpf"))
+ break;
+ }
+ if (fclose(fp)) {
+ err = -errno;
+ pr_warning("failed to close /proc/mounts: %s\n",
+ strerror(errno));
+ }
+ if (strcmp(type, "bpf")) {
+ err = -errno;
+ pr_debug("failed to find bpf mount\n");
+ }
+ } else {
+ err = -errno;
+ pr_warning("cannot open /proc/mounts: %s\n", strerror(errno));
+ }
+ if (err) {
+ pr_debug("using /sys/fs/bpf for BPF filesystem mountpoint\n");
+ strcpy(path, "/sys/fs/bpf");
+ }
+
+ if (!statfs(path, &st_fs) && st_fs.f_type == BPF_FS_MAGIC)
+ return 0;
+
+ if (mount("bpf", path, "bpf", 0, NULL)) {
+ pr_warning("failed to mount bpf: %s\n", strerror(errno));
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int make_dirs(const char *path, const char *subdir, char *buf,
+ size_t len)
+{
+ snprintf(buf, len, "%s/%s/", path, subdir);
+ if (mkdir(buf, 0700) && errno != EEXIST) {
+ pr_warning("failed to mkdir %s: %s\n", subdir, strerror(errno));
+ return -errno;
+ }
+
+ snprintf(buf, len, "%s/%s/maps/", path, subdir);
+ if (mkdir(buf, 0700) && errno != EEXIST) {
+ pr_warning("failed to mkdir map: %s\n", strerror(errno));
+ return -errno;
+ }
+
+ snprintf(buf, len, "%s/%s/progs/", path, subdir);
+ if (mkdir(buf, 0700) && errno != EEXIST) {
+ pr_warning("failed to mkdir prog: %s\n", strerror(errno));
+ return -errno;
+ }
+
+ return 0;
+}
+
+int bpf_object__pin(struct bpf_object *obj, const char *subdir)
+{
+ const size_t len = 255;
+ char path[PATH_MAX];
+ char buf[len];
+ size_t i, j;
+ int err = 0;
+
+ if (!obj)
+ return -ENOENT;
+
+ if (!obj->loaded) {
+ pr_warning("object not yet loaded; load it first\n");
+ return -ENOENT;
+ }
+
+ err = mount_bpf(path);
+ if (err)
+ return err;
+
+ err = make_dirs(path, subdir, buf, len);
+ if (err)
+ return err;
+
+ for (i = 0; i < obj->nr_maps; i++) {
+ struct bpf_map *map = &obj->maps[i];
+
+ snprintf(buf, len, "%s/%s/maps/%s", path, subdir,
+ bpf_map__name(map));
+ if (bpf_obj_pin(map->fd, buf)) {
+ err = -errno;
+ pr_warning("failed to pin map: %s\n", strerror(errno));
+ goto out;
+ }
+ pr_debug("Loaded map '%s' at '%s'\n", bpf_map__name(map), buf);
+ }
+
+ if (obj->programs && obj->nr_programs) {
+ for (i = 0; i < obj->nr_programs; i++) {
+ struct bpf_program *prog = &obj->programs[i];
+
+ for (j = 0; j < prog->instances.nr; j++) {
+ snprintf(buf, len, "%s/%s/progs/%s_%zu", path,
+ subdir, prog->section_name, j);
+ if (bpf_obj_pin(prog->instances.fds[j], buf)) {
+ err = -errno;
+ pr_warning("failed to pin prog: %s\n",
+ strerror(errno));
+ goto out;
+ }
+ pr_debug("Loaded prog '%s' at '%s'\n",
+ prog->section_name, buf);
+ }
+ }
+ }
+
+out:
+ return err;
+}
+
void bpf_object__close(struct bpf_object *obj)
{
size_t i;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 4014d1ba5e3d..920699a437be 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -65,6 +65,7 @@ struct bpf_object *bpf_object__open(const char *path);
struct bpf_object *bpf_object__open_buffer(void *obj_buf,
size_t obj_buf_sz,
const char *name);
+int bpf_object__pin(struct bpf_object *object, const char *subdir);
void bpf_object__close(struct bpf_object *object);
/* Load/unload object into/from kernel */
--
2.11.0
next prev parent reply other threads:[~2017-01-19 0:00 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-18 23:57 [PATCH perf/core 0/6] Libbpf improvements Joe Stringer
2017-01-18 23:57 ` [PATCH perf/core 1/6] tools lib bpf: Fix map offsets in relocation Joe Stringer
2017-01-19 6:07 ` Wangnan (F)
2017-01-19 9:53 ` [PATCH -improve] " Wang Nan
2017-01-18 23:57 ` [PATCH perf/core 2/6] tools lib bpf: Fix grammar in map_idx warning Joe Stringer
2017-01-19 10:10 ` Wangnan (F)
2017-01-18 23:57 ` [PATCH perf/core 3/6] tools lib bpf: Define prog_type fns with macro Joe Stringer
2017-01-18 23:57 ` [PATCH perf/core 4/6] tools lib bpf: Add set/is helpers for all prog types Joe Stringer
2017-01-18 23:57 ` [PATCH perf/core 5/6] tools lib bpf: Add libbpf_get_error() Joe Stringer
2017-01-18 23:57 ` Joe Stringer [this message]
2017-01-19 10:22 ` [PATCH perf/core 6/6] tools lib bpf: Add bpf_object__pin() Wangnan (F)
2017-01-19 23:56 ` Joe Stringer
2017-01-19 10:24 ` [PATCH perf/core 0/6] Libbpf improvements Wangnan (F)
2017-01-23 1:12 ` Joe Stringer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170118235724.26103-7-joe@ovn.org \
--to=joe@ovn.org \
--cc=acme@kernel.org \
--cc=ast@fb.com \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=wangnan0@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®