linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Stringer <joe@ovn.org>
To: acme@kernel.org
Cc: wangnan0@huawei.com, ast@fb.com, daniel@iogearbox.net,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org
Subject: [PATCHv3 perf/core 3/6] tools lib bpf: Add bpf_object__pin()
Date: Thu, 26 Jan 2017 13:19:58 -0800	[thread overview]
Message-ID: <20170126212001.14103-4-joe@ovn.org> (raw)
In-Reply-To: <20170126212001.14103-1-joe@ovn.org>

Add a new API to pin a BPF object to the filesystem. The user can
specify the path within a BPF filesystem to pin the object.
Programs will be pinned under a subdirectory named the same as the
program, with each instance appearing as a numbered file under that
directory, and maps will be pinned under the path using the name of
the map as the file basename.

For example, with the directory '/sys/fs/bpf/foo' and a BPF object which
contains two instances of a program named 'bar', and a map named 'baz':
/sys/fs/bpf/foo/bar/0
/sys/fs/bpf/foo/bar/1
/sys/fs/bpf/foo/baz

Signed-off-by: Joe Stringer <joe@ovn.org>
---
v3: Mount to PATH/MAPNAME, PATH/PROGNAME/N (N = instance number)
v2: Don't automount BPF filesystem
    Split program, map, object pinning into separate APIs and separate
    patches.
---
 tools/lib/bpf/libbpf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |  1 +
 2 files changed, 55 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ce987c02363e..703cfa986b34 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -36,6 +36,7 @@
 #include <linux/magic.h>
 #include <linux/list.h>
 #include <linux/limits.h>
+#include <sys/stat.h>
 #include <sys/vfs.h>
 #include <libelf.h>
 #include <gelf.h>
@@ -1371,6 +1372,59 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
 	return 0;
 }
 
+int bpf_object__pin(struct bpf_object *obj, const char *path)
+{
+	struct bpf_program *prog;
+	struct bpf_map *map;
+	int err;
+
+	if (!obj)
+		return -ENOENT;
+
+	if (!obj->loaded) {
+		pr_warning("object not yet loaded; load it first\n");
+		return -ENOENT;
+	}
+
+	err = make_dir(path);
+	if (err)
+		return err;
+
+	bpf_map__for_each(map, obj) {
+		char buf[PATH_MAX];
+		int len;
+
+		len = snprintf(buf, PATH_MAX, "%s/%s", path,
+			       bpf_map__name(map));
+		if (len < 0)
+			return -EINVAL;
+		else if (len > PATH_MAX)
+			return -ENAMETOOLONG;
+
+		err = bpf_map__pin(map, buf);
+		if (err)
+			return err;
+	}
+
+	bpf_object__for_each_program(prog, obj) {
+		char buf[PATH_MAX];
+		int len;
+
+		len = snprintf(buf, PATH_MAX, "%s/%s", path,
+			       prog->section_name);
+		if (len < 0)
+			return -EINVAL;
+		else if (len > PATH_MAX)
+			return -ENAMETOOLONG;
+
+		err = bpf_program__pin(prog, buf);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 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 2addf9d5b13c..b30394f9947a 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 *path);
 void bpf_object__close(struct bpf_object *object);
 
 /* Load/unload object into/from kernel */
-- 
2.11.0

  parent reply	other threads:[~2017-01-26 21:21 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-26 21:19 [PATCHv3 perf/core 0/6] Libbpf object pinning Joe Stringer
2017-01-26 21:19 ` [PATCHv3 perf/core 1/6] tools lib bpf: Add BPF program pinning APIs Joe Stringer
2017-01-30 20:25   ` Arnaldo Carvalho de Melo
2017-01-30 20:28     ` Arnaldo Carvalho de Melo
2017-01-30 21:16       ` Joe Stringer
2017-01-31  0:58         ` Arnaldo Carvalho de Melo
2017-01-31 16:08           ` Arnaldo Carvalho de Melo
2017-01-31 16:13             ` Arnaldo Carvalho de Melo
2017-01-31 16:42               ` Arnaldo Carvalho de Melo
2017-02-01 14:40   ` [tip:perf/core] " tip-bot for Joe Stringer
2017-01-26 21:19 ` [PATCHv3 perf/core 2/6] tools lib bpf: Add bpf_map__pin() Joe Stringer
2017-02-01 14:40   ` [tip:perf/core] " tip-bot for Joe Stringer
2017-01-26 21:19 ` Joe Stringer [this message]
2017-02-01 14:41   ` [tip:perf/core] tools lib bpf: Add bpf_object__pin() tip-bot for Joe Stringer
2017-01-26 21:19 ` [PATCHv3 perf/core 4/6] tools perf util: Make rm_rf(path) argument const Joe Stringer
2017-02-01 14:41   ` [tip:perf/core] " tip-bot for Joe Stringer
2017-01-26 21:20 ` [PATCHv3 perf/core 5/6] tools lib api fs: Add bpf_fs filesystem detector Joe Stringer
2017-02-01 14:42   ` [tip:perf/core] " tip-bot for Joe Stringer
2017-01-26 21:20 ` [PATCHv3 perf/core 6/6] perf test: Add libbpf pinning test Joe Stringer
2017-02-01 14:42   ` [tip:perf/core] " tip-bot for Joe Stringer
2017-01-30 20:37 ` [PATCHv3 perf/core 0/6] Libbpf object pinning Arnaldo Carvalho de Melo

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=20170126212001.14103-4-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=netdev@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;
as well as URLs for NNTP newsgroup(s).