-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/bpftool: Add bpf_iter support for bptool
Currently, only one command is supported bpftool iter pin <bpf_prog.o> <path> It will pin the trace/iter bpf program in the object file <bpf_prog.o> to the <path> where <path> should be on a bpffs mount. For example, $ bpftool iter pin ./bpf_iter_ipv6_route.o \ /sys/fs/bpf/my_route User can then do a `cat` to print out the results: $ cat /sys/fs/bpf/my_route fe800000000000000000000000000000 40 00000000000000000000000000000000 ... 00000000000000000000000000000000 00 00000000000000000000000000000000 ... 00000000000000000000000000000001 80 00000000000000000000000000000000 ... fe800000000000008c0162fffebdfd57 80 00000000000000000000000000000000 ... ff000000000000000000000000000000 08 00000000000000000000000000000000 ... 00000000000000000000000000000000 00 00000000000000000000000000000000 ... The implementation for ipv6_route iterator is in one of subsequent patches. This patch also added BPF_LINK_TYPE_ITER to link query. In the future, we may add additional parameters to pin command by parameterizing the bpf iterator. For example, a map_id or pid may be added to let bpf program only traverses a single map or task, similar to kernel seq_file single_open(). We may also add introspection command for targets/iterators by leveraging the bpf_iter itself. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200509175920.2477247-1-yhs@fb.com
- Loading branch information
Yonghong Song
authored and
Alexei Starovoitov
committed
May 10, 2020
1 parent
5fbc220
commit 9406b48
Showing
6 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
============ | ||
bpftool-iter | ||
============ | ||
------------------------------------------------------------------------------- | ||
tool to create BPF iterators | ||
------------------------------------------------------------------------------- | ||
|
||
:Manual section: 8 | ||
|
||
SYNOPSIS | ||
======== | ||
|
||
**bpftool** [*OPTIONS*] **iter** *COMMAND* | ||
|
||
*COMMANDS* := { **pin** | **help** } | ||
|
||
ITER COMMANDS | ||
=================== | ||
|
||
| **bpftool** **iter pin** *OBJ* *PATH* | ||
| **bpftool** **iter help** | ||
| | ||
| *OBJ* := /a/file/of/bpf_iter_target.o | ||
|
||
DESCRIPTION | ||
=========== | ||
**bpftool iter pin** *OBJ* *PATH* | ||
A bpf iterator combines a kernel iterating of | ||
particular kernel data (e.g., tasks, bpf_maps, etc.) | ||
and a bpf program called for each kernel data object | ||
(e.g., one task, one bpf_map, etc.). User space can | ||
*read* kernel iterator output through *read()* syscall. | ||
|
||
The *pin* command creates a bpf iterator from *OBJ*, | ||
and pin it to *PATH*. The *PATH* should be located | ||
in *bpffs* mount. It must not contain a dot | ||
character ('.'), which is reserved for future extensions | ||
of *bpffs*. | ||
|
||
User can then *cat PATH* to see the bpf iterator output. | ||
|
||
**bpftool iter help** | ||
Print short help message. | ||
|
||
OPTIONS | ||
======= | ||
-h, --help | ||
Print short generic help message (similar to **bpftool help**). | ||
|
||
-V, --version | ||
Print version number (similar to **bpftool version**). | ||
|
||
-d, --debug | ||
Print all logs available, even debug-level information. This | ||
includes logs from libbpf as well as from the verifier, when | ||
attempting to load programs. | ||
|
||
EXAMPLES | ||
======== | ||
**# bpftool iter pin bpf_iter_netlink.o /sys/fs/bpf/my_netlink** | ||
|
||
:: | ||
|
||
Create a file-based bpf iterator from bpf_iter_netlink.o and pin it | ||
to /sys/fs/bpf/my_netlink | ||
|
||
|
||
SEE ALSO | ||
======== | ||
**bpf**\ (2), | ||
**bpf-helpers**\ (7), | ||
**bpftool**\ (8), | ||
**bpftool-prog**\ (8), | ||
**bpftool-map**\ (8), | ||
**bpftool-link**\ (8), | ||
**bpftool-cgroup**\ (8), | ||
**bpftool-feature**\ (8), | ||
**bpftool-net**\ (8), | ||
**bpftool-perf**\ (8), | ||
**bpftool-btf**\ (8) | ||
**bpftool-gen**\ (8) | ||
**bpftool-struct_ops**\ (8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
// Copyright (C) 2020 Facebook | ||
|
||
#define _GNU_SOURCE | ||
#include <linux/err.h> | ||
#include <bpf/libbpf.h> | ||
|
||
#include "main.h" | ||
|
||
static int do_pin(int argc, char **argv) | ||
{ | ||
const char *objfile, *path; | ||
struct bpf_program *prog; | ||
struct bpf_object *obj; | ||
struct bpf_link *link; | ||
int err; | ||
|
||
if (!REQ_ARGS(2)) | ||
usage(); | ||
|
||
objfile = GET_ARG(); | ||
path = GET_ARG(); | ||
|
||
obj = bpf_object__open(objfile); | ||
if (IS_ERR(obj)) { | ||
p_err("can't open objfile %s", objfile); | ||
return -1; | ||
} | ||
|
||
err = bpf_object__load(obj); | ||
if (err) { | ||
p_err("can't load objfile %s", objfile); | ||
goto close_obj; | ||
} | ||
|
||
prog = bpf_program__next(NULL, obj); | ||
if (!prog) { | ||
p_err("can't find bpf program in objfile %s", objfile); | ||
goto close_obj; | ||
} | ||
|
||
link = bpf_program__attach_iter(prog, NULL); | ||
if (IS_ERR(link)) { | ||
err = PTR_ERR(link); | ||
p_err("attach_iter failed for program %s", | ||
bpf_program__name(prog)); | ||
goto close_obj; | ||
} | ||
|
||
err = mount_bpffs_for_pin(path); | ||
if (err) | ||
goto close_link; | ||
|
||
err = bpf_link__pin(link, path); | ||
if (err) { | ||
p_err("pin_iter failed for program %s to path %s", | ||
bpf_program__name(prog), path); | ||
goto close_link; | ||
} | ||
|
||
close_link: | ||
bpf_link__destroy(link); | ||
close_obj: | ||
bpf_object__close(obj); | ||
return err; | ||
} | ||
|
||
static int do_help(int argc, char **argv) | ||
{ | ||
fprintf(stderr, | ||
"Usage: %s %s pin OBJ PATH\n" | ||
" %s %s help\n" | ||
"\n", | ||
bin_name, argv[-2], bin_name, argv[-2]); | ||
|
||
return 0; | ||
} | ||
|
||
static const struct cmd cmds[] = { | ||
{ "help", do_help }, | ||
{ "pin", do_pin }, | ||
{ 0 } | ||
}; | ||
|
||
int do_iter(int argc, char **argv) | ||
{ | ||
return cmd_select(cmds, argc, argv, do_help); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters