aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorJoe Stringer <joe@ovn.org>2017-01-26 16:19:58 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2017-01-31 14:20:06 -0500
commitd5148d8554d08f03b3e34ecc286ab1729c35c24c (patch)
tree748317494d2db58cc6750d143c0d0fa872c0106e /tools/lib/bpf/libbpf.c
parentb6989f35e80bf830f8dc97b74128d619faef0273 (diff)
tools lib bpf: Add bpf_object__pin()
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> Cc: Alexei Starovoitov <ast@fb.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Wang Nan <wangnan0@huawei.com> Cc: netdev@vger.kernel.org Link: http://lkml.kernel.org/r/20170126212001.14103-4-joe@ovn.org [ Check snprintf >= for truncation, as snprintf(bf, size, ...) == size also means truncation ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r--tools/lib/bpf/libbpf.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6a8c8beeb291..ac6eb863b2a4 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1379,6 +1379,59 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
1379 return 0; 1379 return 0;
1380} 1380}
1381 1381
1382int bpf_object__pin(struct bpf_object *obj, const char *path)
1383{
1384 struct bpf_program *prog;
1385 struct bpf_map *map;
1386 int err;
1387
1388 if (!obj)
1389 return -ENOENT;
1390
1391 if (!obj->loaded) {
1392 pr_warning("object not yet loaded; load it first\n");
1393 return -ENOENT;
1394 }
1395
1396 err = make_dir(path);
1397 if (err)
1398 return err;
1399
1400 bpf_map__for_each(map, obj) {
1401 char buf[PATH_MAX];
1402 int len;
1403
1404 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1405 bpf_map__name(map));
1406 if (len < 0)
1407 return -EINVAL;
1408 else if (len >= PATH_MAX)
1409 return -ENAMETOOLONG;
1410
1411 err = bpf_map__pin(map, buf);
1412 if (err)
1413 return err;
1414 }
1415
1416 bpf_object__for_each_program(prog, obj) {
1417 char buf[PATH_MAX];
1418 int len;
1419
1420 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1421 prog->section_name);
1422 if (len < 0)
1423 return -EINVAL;
1424 else if (len >= PATH_MAX)
1425 return -ENAMETOOLONG;
1426
1427 err = bpf_program__pin(prog, buf);
1428 if (err)
1429 return err;
1430 }
1431
1432 return 0;
1433}
1434
1382void bpf_object__close(struct bpf_object *obj) 1435void bpf_object__close(struct bpf_object *obj)
1383{ 1436{
1384 size_t i; 1437 size_t i;