Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Joe Stringer and committed by
Arnaldo Carvalho de Melo
d5148d85 b6989f35

+54
+53
tools/lib/bpf/libbpf.c
··· 1379 1379 return 0; 1380 1380 } 1381 1381 1382 + int 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 + 1382 1435 void bpf_object__close(struct bpf_object *obj) 1383 1436 { 1384 1437 size_t i;
+1
tools/lib/bpf/libbpf.h
··· 65 65 struct bpf_object *bpf_object__open_buffer(void *obj_buf, 66 66 size_t obj_buf_sz, 67 67 const char *name); 68 + int bpf_object__pin(struct bpf_object *object, const char *path); 68 69 void bpf_object__close(struct bpf_object *object); 69 70 70 71 /* Load/unload object into/from kernel */