Skip to content

Commit a98bf57

Browse files
Jakub KicinskiAlexei Starovoitov
authored andcommitted
tools: bpftool: add support for reporting the effective cgroup progs
Takshak said in the original submission: With different bpf attach_flags available to attach bpf programs specially with BPF_F_ALLOW_OVERRIDE and BPF_F_ALLOW_MULTI, the list of effective bpf-programs available to any sub-cgroups really needs to be available for easy debugging. Using BPF_F_QUERY_EFFECTIVE flag, one can get the list of not only attached bpf-programs to a cgroup but also the inherited ones from parent cgroup. So a new option is introduced to use BPF_F_QUERY_EFFECTIVE query flag here to list all the effective bpf-programs available for execution at a specified cgroup. Reused modified test program test_cgroup_attach from tools/testing/selftests/bpf: # ./test_cgroup_attach With old bpftool: # bpftool cgroup show /sys/fs/cgroup/cgroup-test-work-dir/cg1/ ID AttachType AttachFlags Name 271 egress multi pkt_cntr_1 272 egress multi pkt_cntr_2 Attached new program pkt_cntr_4 in cg2 gives following: # bpftool cgroup show /sys/fs/cgroup/cgroup-test-work-dir/cg1/cg2 ID AttachType AttachFlags Name 273 egress override pkt_cntr_4 And with new "effective" option it shows all effective programs for cg2: # bpftool cgroup show /sys/fs/cgroup/cgroup-test-work-dir/cg1/cg2 effective ID AttachType AttachFlags Name 273 egress override pkt_cntr_4 271 egress override pkt_cntr_1 272 egress override pkt_cntr_2 Compared to original submission use a local flag instead of global option. We need to clear query_flags on every command, in case batch mode wants to use varying settings. v2: (Takshak) - forbid duplicated flags; - fix cgroup path freeing. Signed-off-by: Takshak Chahande <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Reviewed-by: Takshak Chahande <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent bf8ff0f commit a98bf57

File tree

3 files changed

+76
-38
lines changed

3 files changed

+76
-38
lines changed

tools/bpf/bpftool/Documentation/bpftool-cgroup.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ SYNOPSIS
2020
CGROUP COMMANDS
2121
===============
2222

23-
| **bpftool** **cgroup { show | list }** *CGROUP*
24-
| **bpftool** **cgroup tree** [*CGROUP_ROOT*]
23+
| **bpftool** **cgroup { show | list }** *CGROUP* [**effective**]
24+
| **bpftool** **cgroup tree** [*CGROUP_ROOT*] [**effective**]
2525
| **bpftool** **cgroup attach** *CGROUP* *ATTACH_TYPE* *PROG* [*ATTACH_FLAGS*]
2626
| **bpftool** **cgroup detach** *CGROUP* *ATTACH_TYPE* *PROG*
2727
| **bpftool** **cgroup help**
@@ -35,13 +35,17 @@ CGROUP COMMANDS
3535
3636
DESCRIPTION
3737
===========
38-
**bpftool cgroup { show | list }** *CGROUP*
38+
**bpftool cgroup { show | list }** *CGROUP* [**effective**]
3939
List all programs attached to the cgroup *CGROUP*.
4040

4141
Output will start with program ID followed by attach type,
4242
attach flags and program name.
4343

44-
**bpftool cgroup tree** [*CGROUP_ROOT*]
44+
If **effective** is specified retrieve effective programs that
45+
will execute for events within a cgroup. This includes
46+
inherited along with attached ones.
47+
48+
**bpftool cgroup tree** [*CGROUP_ROOT*] [**effective**]
4549
Iterate over all cgroups in *CGROUP_ROOT* and list all
4650
attached programs. If *CGROUP_ROOT* is not specified,
4751
bpftool uses cgroup v2 mountpoint.
@@ -50,6 +54,10 @@ DESCRIPTION
5054
commands: it starts with absolute cgroup path, followed by
5155
program ID, attach type, attach flags and program name.
5256

57+
If **effective** is specified retrieve effective programs that
58+
will execute for events within a cgroup. This includes
59+
inherited along with attached ones.
60+
5361
**bpftool cgroup attach** *CGROUP* *ATTACH_TYPE* *PROG* [*ATTACH_FLAGS*]
5462
Attach program *PROG* to the cgroup *CGROUP* with attach type
5563
*ATTACH_TYPE* and optional *ATTACH_FLAGS*.

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,15 @@ _bpftool()
710710
;;
711711
cgroup)
712712
case $command in
713-
show|list)
714-
_filedir
715-
return 0
716-
;;
717-
tree)
718-
_filedir
713+
show|list|tree)
714+
case $cword in
715+
3)
716+
_filedir
717+
;;
718+
4)
719+
COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
720+
;;
721+
esac
719722
return 0
720723
;;
721724
attach|detach)

tools/bpf/bpftool/cgroup.c

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
" recvmsg4 | recvmsg6 | sysctl |\n" \
3030
" getsockopt | setsockopt }"
3131

32+
static unsigned int query_flags;
33+
3234
static const char * const attach_type_strings[] = {
3335
[BPF_CGROUP_INET_INGRESS] = "ingress",
3436
[BPF_CGROUP_INET_EGRESS] = "egress",
@@ -107,7 +109,8 @@ static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
107109
__u32 prog_cnt = 0;
108110
int ret;
109111

110-
ret = bpf_prog_query(cgroup_fd, type, 0, NULL, NULL, &prog_cnt);
112+
ret = bpf_prog_query(cgroup_fd, type, query_flags, NULL,
113+
NULL, &prog_cnt);
111114
if (ret)
112115
return -1;
113116

@@ -125,8 +128,8 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
125128
int ret;
126129

127130
prog_cnt = ARRAY_SIZE(prog_ids);
128-
ret = bpf_prog_query(cgroup_fd, type, 0, &attach_flags, prog_ids,
129-
&prog_cnt);
131+
ret = bpf_prog_query(cgroup_fd, type, query_flags, &attach_flags,
132+
prog_ids, &prog_cnt);
130133
if (ret)
131134
return ret;
132135

@@ -158,20 +161,34 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
158161
static int do_show(int argc, char **argv)
159162
{
160163
enum bpf_attach_type type;
164+
const char *path;
161165
int cgroup_fd;
162166
int ret = -1;
163167

164-
if (argc < 1) {
165-
p_err("too few parameters for cgroup show");
166-
goto exit;
167-
} else if (argc > 1) {
168-
p_err("too many parameters for cgroup show");
169-
goto exit;
168+
query_flags = 0;
169+
170+
if (!REQ_ARGS(1))
171+
return -1;
172+
path = GET_ARG();
173+
174+
while (argc) {
175+
if (is_prefix(*argv, "effective")) {
176+
if (query_flags & BPF_F_QUERY_EFFECTIVE) {
177+
p_err("duplicated argument: %s", *argv);
178+
return -1;
179+
}
180+
query_flags |= BPF_F_QUERY_EFFECTIVE;
181+
NEXT_ARG();
182+
} else {
183+
p_err("expected no more arguments, 'effective', got: '%s'?",
184+
*argv);
185+
return -1;
186+
}
170187
}
171188

172-
cgroup_fd = open(argv[0], O_RDONLY);
189+
cgroup_fd = open(path, O_RDONLY);
173190
if (cgroup_fd < 0) {
174-
p_err("can't open cgroup %s", argv[0]);
191+
p_err("can't open cgroup %s", path);
175192
goto exit;
176193
}
177194

@@ -294,26 +311,37 @@ static char *find_cgroup_root(void)
294311

295312
static int do_show_tree(int argc, char **argv)
296313
{
297-
char *cgroup_root;
314+
char *cgroup_root, *cgroup_alloced = NULL;
298315
int ret;
299316

300-
switch (argc) {
301-
case 0:
302-
cgroup_root = find_cgroup_root();
303-
if (!cgroup_root) {
317+
query_flags = 0;
318+
319+
if (!argc) {
320+
cgroup_alloced = find_cgroup_root();
321+
if (!cgroup_alloced) {
304322
p_err("cgroup v2 isn't mounted");
305323
return -1;
306324
}
307-
break;
308-
case 1:
309-
cgroup_root = argv[0];
310-
break;
311-
default:
312-
p_err("too many parameters for cgroup tree");
313-
return -1;
325+
cgroup_root = cgroup_alloced;
326+
} else {
327+
cgroup_root = GET_ARG();
328+
329+
while (argc) {
330+
if (is_prefix(*argv, "effective")) {
331+
if (query_flags & BPF_F_QUERY_EFFECTIVE) {
332+
p_err("duplicated argument: %s", *argv);
333+
return -1;
334+
}
335+
query_flags |= BPF_F_QUERY_EFFECTIVE;
336+
NEXT_ARG();
337+
} else {
338+
p_err("expected no more arguments, 'effective', got: '%s'?",
339+
*argv);
340+
return -1;
341+
}
342+
}
314343
}
315344

316-
317345
if (json_output)
318346
jsonw_start_array(json_wtr);
319347
else
@@ -338,8 +366,7 @@ static int do_show_tree(int argc, char **argv)
338366
if (json_output)
339367
jsonw_end_array(json_wtr);
340368

341-
if (argc == 0)
342-
free(cgroup_root);
369+
free(cgroup_alloced);
343370

344371
return ret;
345372
}
@@ -459,8 +486,8 @@ static int do_help(int argc, char **argv)
459486
}
460487

461488
fprintf(stderr,
462-
"Usage: %s %s { show | list } CGROUP\n"
463-
" %s %s tree [CGROUP_ROOT]\n"
489+
"Usage: %s %s { show | list } CGROUP [**effective**]\n"
490+
" %s %s tree [CGROUP_ROOT] [**effective**]\n"
464491
" %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
465492
" %s %s detach CGROUP ATTACH_TYPE PROG\n"
466493
" %s %s help\n"

0 commit comments

Comments
 (0)