[PATCH bpf-next 4/5] bpftool: support optional 'task main_thread_only' argument
From: Yonghong Song <hidden>
Date: 2020-08-27 00:06:37
Also in:
bpf
Subsystem:
bpf [general] (safe dynamic programs and tools), bpf [tooling] (bpftool), the rest · Maintainers:
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Quentin Monnet, Linus Torvalds
For task and task_file bpf iterators, optional 'task main_thread_only'
can signal the kernel to only iterate through main threads of each
process. link_query will also print out main_thread_only value for
task/task_file iterators.
This patch also fixed the issue where if the additional arguments
are not supported, bpftool will print an error message and exit.
$ ./bpftool iter pin ./bpf_iter_task.o /sys/fs/bpf/p1 task main_thread_only
$ ./bpftool iter pin ./bpf_iter_task_file.o /sys/fs/bpf/p2 task main_thread_only
$ ./bpftool iter pin ./bpf_iter_task_file.o /sys/fs/bpf/p3
$ ./bpftool link show
1: iter prog 6 target_name bpf_map
2: iter prog 7 target_name bpf_prog
3: iter prog 12 target_name task main_thread_only 1
5: iter prog 23 target_name task_file main_thread_only 1
6: iter prog 28 target_name task_file main_thread_only 0
$ cat /sys/fs/bpf/p2
tgid gid fd file
...
1716 1716 255 ffffffffa2e95ec0
1756 1756 0 ffffffffa2e95ec0
1756 1756 1 ffffffffa2e95ec0
1756 1756 2 ffffffffa2e95ec0
1756 1756 3 ffffffffa2e20a80
1756 1756 4 ffffffffa2e19ba0
1756 1756 5 ffffffffa2e16460
1756 1756 6 ffffffffa2e16460
1756 1756 7 ffffffffa2e16460
1756 1756 8 ffffffffa2e16260
1761 1761 0 ffffffffa2e95ec0
...
$ ls /proc/1756/task/
1756 1757 1758 1759 1760
In the above task_file iterator, the process with id 1756 has 5 threads and
only the thread with pid = 1756 is processed by the bpf program.
Signed-off-by: Yonghong Song <redacted>
---
.../bpftool/Documentation/bpftool-iter.rst | 17 +++++++++--
tools/bpf/bpftool/bash-completion/bpftool | 9 +++++-
tools/bpf/bpftool/iter.c | 28 ++++++++++++++++---
tools/bpf/bpftool/link.c | 12 ++++++++
4 files changed, 59 insertions(+), 7 deletions(-)
diff --git a/tools/bpf/bpftool/Documentation/bpftool-iter.rst b/tools/bpf/bpftool/Documentation/bpftool-iter.rst
index 070ffacb42b5..d9aac12c76da 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-iter.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-iter.rst@@ -17,15 +17,16 @@ SYNOPSIS ITER COMMANDS =================== -| **bpftool** **iter pin** *OBJ* *PATH* [**map** *MAP*] +| **bpftool** **iter pin** *OBJ* *PATH* [**map** *MAP* | **task** *TASK_OPT*] | **bpftool** **iter help** | | *OBJ* := /a/file/of/bpf_iter_target.o | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } +| *TASK_OPT* := { **main_thread_only** } DESCRIPTION =========== - **bpftool iter pin** *OBJ* *PATH* [**map** *MAP*] + **bpftool iter pin** *OBJ* *PATH* [**map** *MAP* | **task** *TASK_OPT*] 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
@@ -44,6 +45,11 @@ DESCRIPTION with each map element, do checking, filtering, aggregation, etc. without copying data to user space. + The task or task_file bpf iterator can have an optional + parameter *TASK_OPT*. The current supported value is + **main_thread_only** which supports to iterate only main + threads of each process. + User can then *cat PATH* to see the bpf iterator output. **bpftool iter help**
@@ -78,6 +84,13 @@ EXAMPLES Create a file-based bpf iterator from bpf_iter_hashmap.o and map with id 20, and pin it to /sys/fs/bpf/my_hashmap +**# bpftool iter pin bpf_iter_task.o /sys/fs/bpf/my_task task main_thread_only** + +:: + + Create a file-based bpf iterator from bpf_iter_task.o which iterates main + threads of processes only, and pin it to /sys/fs/bpf/my_hashmap + SEE ALSO ======== **bpf**\ (2),
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index 7b68e3c0a5fb..84d538de71e1 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool@@ -613,6 +613,7 @@ _bpftool() esac ;; iter) + local TARGET_TYPE='map task' case $command in pin) case $prev in
@@ -628,9 +629,15 @@ _bpftool() pinned) _filedir ;; - *) + task) + _bpftool_one_of_list 'main_thread_only' + ;; + map) _bpftool_one_of_list $MAP_TYPE ;; + *) + _bpftool_one_of_list $TARGET_TYPE + ;; esac return 0 ;;
diff --git a/tools/bpf/bpftool/iter.c b/tools/bpf/bpftool/iter.c
index 3b1aad7535dd..a4c789ea43f1 100644
--- a/tools/bpf/bpftool/iter.c
+++ b/tools/bpf/bpftool/iter.c@@ -26,6 +26,7 @@ static int do_pin(int argc, char **argv) /* optional arguments */ if (argc) { + memset(&linfo, 0, sizeof(linfo)); if (is_prefix(*argv, "map")) { NEXT_ARG();
@@ -38,11 +39,29 @@ static int do_pin(int argc, char **argv) if (map_fd < 0) return -1; - memset(&linfo, 0, sizeof(linfo)); linfo.map.map_fd = map_fd; - iter_opts.link_info = &linfo; - iter_opts.link_info_len = sizeof(linfo); + } else if (is_prefix(*argv, "task")) { + NEXT_ARG(); + + if (!REQ_ARGS(1)) { + p_err("incorrect task spec"); + return -1; + } + + if (strcmp(*argv, "main_thread_only") != 0) { + p_err("incorrect task spec"); + return -1; + } + + linfo.task.main_thread_only = true; + } else { + p_err("expected no more arguments, 'map' or 'task', got: '%s'?", + *argv); + return -1; } + + iter_opts.link_info = &linfo; + iter_opts.link_info_len = sizeof(linfo); } obj = bpf_object__open(objfile);
@@ -95,9 +114,10 @@ static int do_pin(int argc, char **argv) static int do_help(int argc, char **argv) { fprintf(stderr, - "Usage: %1$s %2$s pin OBJ PATH [map MAP]\n" + "Usage: %1$s %2$s pin OBJ PATH [map MAP | task TASK_OPT]\n" " %1$s %2$s help\n" " " HELP_SPEC_MAP "\n" + " TASK_OPT := { main_thread_only }\n" "", bin_name, "iter");
diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index e77e1525d20a..a159d5680c74 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c@@ -83,6 +83,12 @@ static bool is_iter_map_target(const char *target_name) strcmp(target_name, "bpf_sk_storage_map") == 0; } +static bool is_iter_task_target(const char *target_name) +{ + return strcmp(target_name, "task") == 0 || + strcmp(target_name, "task_file") == 0; +} + static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) { const char *target_name = u64_to_ptr(info->iter.target_name);
@@ -91,6 +97,9 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) if (is_iter_map_target(target_name)) jsonw_uint_field(wtr, "map_id", info->iter.map.map_id); + else if (is_iter_task_target(target_name)) + jsonw_uint_field(wtr, "main_thread_only", + info->iter.task.main_thread_only); } static int get_prog_info(int prog_id, struct bpf_prog_info *info)
@@ -202,6 +211,9 @@ static void show_iter_plain(struct bpf_link_info *info) if (is_iter_map_target(target_name)) printf("map_id %u ", info->iter.map.map_id); + else if (is_iter_task_target(target_name)) + printf("main_thread_only %u ", + info->iter.task.main_thread_only); } static int show_link_close_plain(int fd, struct bpf_link_info *info)
--
2.24.1