diff mbox series

[bpf-next,v1] bpftool: Add support for querying cgroup_iter link

Message ID 20220829231828.1016835-1-haoluo@google.com (mailing list archive)
State Accepted
Commit 6f95de6d713130c953af0a40b13c1da519f91c4e
Delegated to: BPF
Headers show
Series [bpf-next,v1] bpftool: Add support for querying cgroup_iter link | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-PR fail merge-conflict
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc

Commit Message

Hao Luo Aug. 29, 2022, 11:18 p.m. UTC
Support dumping info of a cgroup_iter link. This includes
showing the cgroup's id and the order for walking the cgroup
hierarchy. Example output is as follows:

> bpftool link show
1: iter  prog 2  target_name bpf_map
2: iter  prog 3  target_name bpf_prog
3: iter  prog 12  target_name cgroup  cgroup_id 72  order self_only

> bpftool -p link show
[{
        "id": 1,
        "type": "iter",
        "prog_id": 2,
        "target_name": "bpf_map"
    },{
        "id": 2,
        "type": "iter",
        "prog_id": 3,
        "target_name": "bpf_prog"
    },{
        "id": 3,
        "type": "iter",
        "prog_id": 12,
        "target_name": "cgroup",
        "cgroup_id": 72,
        "order": "self_only"
    }
]

Signed-off-by: Hao Luo <haoluo@google.com>
---
 tools/bpf/bpftool/link.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

Comments

Quentin Monnet Aug. 30, 2022, 1:33 p.m. UTC | #1
On 30/08/2022 00:18, Hao Luo wrote:
> Support dumping info of a cgroup_iter link. This includes
> showing the cgroup's id and the order for walking the cgroup
> hierarchy. Example output is as follows:
> 
>> bpftool link show
> 1: iter  prog 2  target_name bpf_map
> 2: iter  prog 3  target_name bpf_prog
> 3: iter  prog 12  target_name cgroup  cgroup_id 72  order self_only
> 
>> bpftool -p link show
> [{
>         "id": 1,
>         "type": "iter",
>         "prog_id": 2,
>         "target_name": "bpf_map"
>     },{
>         "id": 2,
>         "type": "iter",
>         "prog_id": 3,
>         "target_name": "bpf_prog"
>     },{
>         "id": 3,
>         "type": "iter",
>         "prog_id": 12,
>         "target_name": "cgroup",
>         "cgroup_id": 72,
>         "order": "self_only"
>     }
> ]
> 
> Signed-off-by: Hao Luo <haoluo@google.com>
> ---
>  tools/bpf/bpftool/link.c | 35 +++++++++++++++++++++++++++++++++++
>  1 file changed, 35 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
> index 7a20931c3250..9e8d14d0114d 100644
> --- a/tools/bpf/bpftool/link.c
> +++ b/tools/bpf/bpftool/link.c
> @@ -83,6 +83,29 @@ static bool is_iter_map_target(const char *target_name)
>  	       strcmp(target_name, "bpf_sk_storage_map") == 0;
>  }
>  
> +static bool is_iter_cgroup_target(const char *target_name)
> +{
> +	return strcmp(target_name, "cgroup") == 0;
> +}
> +
> +static const char *cgroup_order_string(__u32 order)
> +{
> +	switch (order) {
> +	case BPF_CGROUP_ITER_ORDER_UNSPEC:
> +		return "order_unspec";
> +	case BPF_CGROUP_ITER_SELF_ONLY:
> +		return "self_only";
> +	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
> +		return "descendants_pre";
> +	case BPF_CGROUP_ITER_DESCENDANTS_POST:
> +		return "descendants_post";
> +	case BPF_CGROUP_ITER_ANCESTORS_UP:
> +		return "ancestors_up";
> +	default: /* won't happen */
> +		return "";

I wonder if that one should be "unknown", in case another option is
added in the future, so we can spot it and address it?

> +	}
> +}
> +
>  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 +114,12 @@ 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);
> +
> +	if (is_iter_cgroup_target(target_name)) {
> +		jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
> +		jsonw_string_field(wtr, "order",
> +				   cgroup_order_string(info->iter.cgroup.order));
> +	}
>  }
>  
>  static int get_prog_info(int prog_id, struct bpf_prog_info *info)
> @@ -208,6 +237,12 @@ 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);
> +
> +	if (is_iter_cgroup_target(target_name)) {
> +		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
> +		printf("order %s  ",
> +		       cgroup_order_string(info->iter.cgroup.order));
> +	}
>  }
>  
>  static int show_link_close_plain(int fd, struct bpf_link_info *info)

Looks good to me, thank you!

Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Yonghong Song Aug. 30, 2022, 4:41 p.m. UTC | #2
On 8/29/22 4:18 PM, Hao Luo wrote:
> Support dumping info of a cgroup_iter link. This includes
> showing the cgroup's id and the order for walking the cgroup
> hierarchy. Example output is as follows:
> 
>> bpftool link show
> 1: iter  prog 2  target_name bpf_map
> 2: iter  prog 3  target_name bpf_prog
> 3: iter  prog 12  target_name cgroup  cgroup_id 72  order self_only
> 
>> bpftool -p link show
> [{
>          "id": 1,
>          "type": "iter",
>          "prog_id": 2,
>          "target_name": "bpf_map"
>      },{
>          "id": 2,
>          "type": "iter",
>          "prog_id": 3,
>          "target_name": "bpf_prog"
>      },{
>          "id": 3,
>          "type": "iter",
>          "prog_id": 12,
>          "target_name": "cgroup",
>          "cgroup_id": 72,
>          "order": "self_only"
>      }
> ]
> 
> Signed-off-by: Hao Luo <haoluo@google.com>

Acked-by: Yonghong Song <yhs@fb.com>
Martin KaFai Lau Aug. 30, 2022, 6:06 p.m. UTC | #3
On Tue, Aug 30, 2022 at 02:33:47PM +0100, Quentin Monnet wrote:
> > +static const char *cgroup_order_string(__u32 order)
> > +{
> > +	switch (order) {
> > +	case BPF_CGROUP_ITER_ORDER_UNSPEC:
> > +		return "order_unspec";
> > +	case BPF_CGROUP_ITER_SELF_ONLY:
> > +		return "self_only";
> > +	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
> > +		return "descendants_pre";
> > +	case BPF_CGROUP_ITER_DESCENDANTS_POST:
> > +		return "descendants_post";
> > +	case BPF_CGROUP_ITER_ANCESTORS_UP:
> > +		return "ancestors_up";
> > +	default: /* won't happen */
> > +		return "";
> 
> I wonder if that one should be "unknown", in case another option is
> added in the future, so we can spot it and address it?
I added "unknown" and applied.
patchwork-bot+netdevbpf@kernel.org Aug. 30, 2022, 6:10 p.m. UTC | #4
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Martin KaFai Lau <martin.lau@linux.dev>:

On Mon, 29 Aug 2022 16:18:28 -0700 you wrote:
> Support dumping info of a cgroup_iter link. This includes
> showing the cgroup's id and the order for walking the cgroup
> hierarchy. Example output is as follows:
> 
> > bpftool link show
> 1: iter  prog 2  target_name bpf_map
> 2: iter  prog 3  target_name bpf_prog
> 3: iter  prog 12  target_name cgroup  cgroup_id 72  order self_only
> 
> [...]

Here is the summary with links:
  - [bpf-next,v1] bpftool: Add support for querying cgroup_iter link
    https://git.kernel.org/bpf/bpf-next/c/6f95de6d7131

You are awesome, thank you!
Hao Luo Aug. 30, 2022, 6:19 p.m. UTC | #5
On Tue, Aug 30, 2022 at 11:06 AM Martin KaFai Lau <kafai@fb.com> wrote:
>
> On Tue, Aug 30, 2022 at 02:33:47PM +0100, Quentin Monnet wrote:
> > > +static const char *cgroup_order_string(__u32 order)
> > > +{
> > > +   switch (order) {
> > > +   case BPF_CGROUP_ITER_ORDER_UNSPEC:
> > > +           return "order_unspec";
> > > +   case BPF_CGROUP_ITER_SELF_ONLY:
> > > +           return "self_only";
> > > +   case BPF_CGROUP_ITER_DESCENDANTS_PRE:
> > > +           return "descendants_pre";
> > > +   case BPF_CGROUP_ITER_DESCENDANTS_POST:
> > > +           return "descendants_post";
> > > +   case BPF_CGROUP_ITER_ANCESTORS_UP:
> > > +           return "ancestors_up";
> > > +   default: /* won't happen */
> > > +           return "";
> >
> > I wonder if that one should be "unknown", in case another option is
> > added in the future, so we can spot it and address it?
> I added "unknown" and applied.

Thanks Martin and Quentin, "unknown" is better than an empty string.
Hao Luo Aug. 30, 2022, 6:20 p.m. UTC | #6
On Tue, Aug 30, 2022 at 9:41 AM Yonghong Song <yhs@fb.com> wrote:
>
> On 8/29/22 4:18 PM, Hao Luo wrote:
[...]
> >
> > Signed-off-by: Hao Luo <haoluo@google.com>
>
> Acked-by: Yonghong Song <yhs@fb.com>

Thanks Yonghong.
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index 7a20931c3250..9e8d14d0114d 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -83,6 +83,29 @@  static bool is_iter_map_target(const char *target_name)
 	       strcmp(target_name, "bpf_sk_storage_map") == 0;
 }
 
+static bool is_iter_cgroup_target(const char *target_name)
+{
+	return strcmp(target_name, "cgroup") == 0;
+}
+
+static const char *cgroup_order_string(__u32 order)
+{
+	switch (order) {
+	case BPF_CGROUP_ITER_ORDER_UNSPEC:
+		return "order_unspec";
+	case BPF_CGROUP_ITER_SELF_ONLY:
+		return "self_only";
+	case BPF_CGROUP_ITER_DESCENDANTS_PRE:
+		return "descendants_pre";
+	case BPF_CGROUP_ITER_DESCENDANTS_POST:
+		return "descendants_post";
+	case BPF_CGROUP_ITER_ANCESTORS_UP:
+		return "ancestors_up";
+	default: /* won't happen */
+		return "";
+	}
+}
+
 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 +114,12 @@  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);
+
+	if (is_iter_cgroup_target(target_name)) {
+		jsonw_lluint_field(wtr, "cgroup_id", info->iter.cgroup.cgroup_id);
+		jsonw_string_field(wtr, "order",
+				   cgroup_order_string(info->iter.cgroup.order));
+	}
 }
 
 static int get_prog_info(int prog_id, struct bpf_prog_info *info)
@@ -208,6 +237,12 @@  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);
+
+	if (is_iter_cgroup_target(target_name)) {
+		printf("cgroup_id %llu  ", info->iter.cgroup.cgroup_id);
+		printf("order %s  ",
+		       cgroup_order_string(info->iter.cgroup.order));
+	}
 }
 
 static int show_link_close_plain(int fd, struct bpf_link_info *info)