Skip to contentSkip to conversation

bpftool: Add support for querying cgroup_iter link

PATCH Applied Hao Luo 5 participants 7 messages Aug 29, 2022 – Aug 30, 2022

Subsystems (2)
lore
Share & tools
lore

Hao Luo

author
#0 lore raw

PATCH 1/1 bpftool: Add support for querying cgroup_iter link

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:

Quoted 1 line

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
Quoted 1 line

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"
    }

]

---
 tools/bpf/bpftool/link.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/bpf/bpftool/link.c +35 -0
index 7a20931c3250..9e8d14d0114d 100644
@@ -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)
Signature · 1 line
2.37.2.672.g94769d06f0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 53 lines · Hao Luo

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:

Quoted 1 line

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
Quoted 1 line

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"
    }

]

---
 tools/bpf/bpftool/link.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/bpf/bpftool/link.c +10 -0
index 7a20931c3250..9e8d14d0114d 100644
@@ -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";

tools/bpf/bpftool/link.c static bool is_iter_map_target(const char *target_name) · line 105

Quoted 10 lines
tools/bpf/bpftool/link.c +10 -0
+	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?

Quoted 22 lines
tools/bpf/bpftool/link.c +9 -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 +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))

tools/bpf/bpftool/link.c static void show_iter_plain(struct bpf_link_info *info) · line 248

Quoted 10 lines
tools/bpf/bpftool/link.c +6 -0
 		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!

#2 lore raw
Quoted 10 lines · Quentin Monnet
Quoted 10 lines
tools/bpf/bpftool/link.c +10 -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:
Quoted 8 lines

tools/bpf/bpftool/link.c static bool is_iter_map_target(const char *target_name) · line 105

Quoted 5 lines
tools/bpf/bpftool/link.c +5 -0
+		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.

author
#3 lore raw
Quoted 20 lines · Martin KaFai Lau
Quoted 10 lines · Quentin Monnet
Quoted 10 lines
tools/bpf/bpftool/link.c +10 -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:
Quoted 8 lines

tools/bpf/bpftool/link.c static bool is_iter_map_target(const char *target_name) · line 105

Quoted 5 lines
tools/bpf/bpftool/link.c +5 -0
+           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?

Quoted 1 line

I added "unknown" and applied.

Thanks Martin and Quentin, "unknown" is better than an empty string.

Acked-by on v1 1/1
#4 lore raw
In reply to Hao Luo
Quoted 28 lines · Hao Luo

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:

Quoted 1 line

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
Quoted 1 line

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"
     }
Quoted 3 lines

]

#6 lore raw
In reply to Hao Luo
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:

Quoted 10 lines · 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:

Quoted 1 line

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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html