1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2017 Facebook
3 // Author: Roman Gushchin <guro@fb.com>
5 #define _XOPEN_SOURCE 500
14 #include <sys/types.h>
21 #define HELP_SPEC_ATTACH_FLAGS \
22 "ATTACH_FLAGS := { multi | override }"
24 #define HELP_SPEC_ATTACH_TYPES \
25 " ATTACH_TYPE := { ingress | egress | sock_create |\n" \
26 " sock_ops | device | bind4 | bind6 |\n" \
27 " post_bind4 | post_bind6 | connect4 |\n" \
28 " connect6 | sendmsg4 | sendmsg6 }"
30 static const char * const attach_type_strings
[] = {
31 [BPF_CGROUP_INET_INGRESS
] = "ingress",
32 [BPF_CGROUP_INET_EGRESS
] = "egress",
33 [BPF_CGROUP_INET_SOCK_CREATE
] = "sock_create",
34 [BPF_CGROUP_SOCK_OPS
] = "sock_ops",
35 [BPF_CGROUP_DEVICE
] = "device",
36 [BPF_CGROUP_INET4_BIND
] = "bind4",
37 [BPF_CGROUP_INET6_BIND
] = "bind6",
38 [BPF_CGROUP_INET4_CONNECT
] = "connect4",
39 [BPF_CGROUP_INET6_CONNECT
] = "connect6",
40 [BPF_CGROUP_INET4_POST_BIND
] = "post_bind4",
41 [BPF_CGROUP_INET6_POST_BIND
] = "post_bind6",
42 [BPF_CGROUP_UDP4_SENDMSG
] = "sendmsg4",
43 [BPF_CGROUP_UDP6_SENDMSG
] = "sendmsg6",
44 [__MAX_BPF_ATTACH_TYPE
] = NULL
,
47 static enum bpf_attach_type
parse_attach_type(const char *str
)
49 enum bpf_attach_type type
;
51 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++) {
52 if (attach_type_strings
[type
] &&
53 is_prefix(str
, attach_type_strings
[type
]))
57 return __MAX_BPF_ATTACH_TYPE
;
60 static int show_bpf_prog(int id
, const char *attach_type_str
,
61 const char *attach_flags_str
,
64 struct bpf_prog_info info
= {};
65 __u32 info_len
= sizeof(info
);
68 prog_fd
= bpf_prog_get_fd_by_id(id
);
72 if (bpf_obj_get_info_by_fd(prog_fd
, &info
, &info_len
)) {
78 jsonw_start_object(json_wtr
);
79 jsonw_uint_field(json_wtr
, "id", info
.id
);
80 jsonw_string_field(json_wtr
, "attach_type",
82 jsonw_string_field(json_wtr
, "attach_flags",
84 jsonw_string_field(json_wtr
, "name", info
.name
);
85 jsonw_end_object(json_wtr
);
87 printf("%s%-8u %-15s %-15s %-15s\n", level
? " " : "",
98 static int count_attached_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
)
103 ret
= bpf_prog_query(cgroup_fd
, type
, 0, NULL
, NULL
, &prog_cnt
);
110 static int show_attached_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
,
113 __u32 prog_ids
[1024] = {0};
114 char *attach_flags_str
;
115 __u32 prog_cnt
, iter
;
120 prog_cnt
= ARRAY_SIZE(prog_ids
);
121 ret
= bpf_prog_query(cgroup_fd
, type
, 0, &attach_flags
, prog_ids
,
129 switch (attach_flags
) {
130 case BPF_F_ALLOW_MULTI
:
131 attach_flags_str
= "multi";
133 case BPF_F_ALLOW_OVERRIDE
:
134 attach_flags_str
= "override";
137 attach_flags_str
= "";
140 snprintf(buf
, sizeof(buf
), "unknown(%x)", attach_flags
);
141 attach_flags_str
= buf
;
144 for (iter
= 0; iter
< prog_cnt
; iter
++)
145 show_bpf_prog(prog_ids
[iter
], attach_type_strings
[type
],
146 attach_flags_str
, level
);
151 static int do_show(int argc
, char **argv
)
153 enum bpf_attach_type type
;
158 p_err("too few parameters for cgroup show");
160 } else if (argc
> 1) {
161 p_err("too many parameters for cgroup show");
165 cgroup_fd
= open(argv
[0], O_RDONLY
);
167 p_err("can't open cgroup %s", argv
[1]);
172 jsonw_start_array(json_wtr
);
174 printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
175 "AttachFlags", "Name");
177 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++) {
179 * Not all attach types may be supported, so it's expected,
180 * that some requests will fail.
181 * If we were able to get the show for at least one
182 * attach type, let's return 0.
184 if (show_attached_bpf_progs(cgroup_fd
, type
, 0) == 0)
189 jsonw_end_array(json_wtr
);
197 * To distinguish nftw() errors and do_show_tree_fn() errors
198 * and avoid duplicating error messages, let's return -2
199 * from do_show_tree_fn() in case of error.
202 #define SHOW_TREE_FN_ERR -2
203 static int do_show_tree_fn(const char *fpath
, const struct stat
*sb
,
204 int typeflag
, struct FTW
*ftw
)
206 enum bpf_attach_type type
;
210 if (typeflag
!= FTW_D
)
213 cgroup_fd
= open(fpath
, O_RDONLY
);
215 p_err("can't open cgroup %s: %s", fpath
, strerror(errno
));
216 return SHOW_TREE_FN_ERR
;
219 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++) {
220 int count
= count_attached_bpf_progs(cgroup_fd
, type
);
222 if (count
< 0 && errno
!= EINVAL
) {
223 p_err("can't query bpf programs attached to %s: %s",
224 fpath
, strerror(errno
));
226 return SHOW_TREE_FN_ERR
;
240 jsonw_start_object(json_wtr
);
241 jsonw_string_field(json_wtr
, "cgroup", fpath
);
242 jsonw_name(json_wtr
, "programs");
243 jsonw_start_array(json_wtr
);
245 printf("%s\n", fpath
);
248 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++)
249 show_attached_bpf_progs(cgroup_fd
, type
, ftw
->level
);
252 jsonw_end_array(json_wtr
);
253 jsonw_end_object(json_wtr
);
261 static char *find_cgroup_root(void)
266 f
= fopen("/proc/mounts", "r");
270 while ((mnt
= getmntent(f
))) {
271 if (strcmp(mnt
->mnt_type
, "cgroup2") == 0) {
273 return strdup(mnt
->mnt_dir
);
281 static int do_show_tree(int argc
, char **argv
)
288 cgroup_root
= find_cgroup_root();
290 p_err("cgroup v2 isn't mounted");
295 cgroup_root
= argv
[0];
298 p_err("too many parameters for cgroup tree");
304 jsonw_start_array(json_wtr
);
307 "%-8s %-15s %-15s %-15s\n",
309 "ID", "AttachType", "AttachFlags", "Name");
311 switch (nftw(cgroup_root
, do_show_tree_fn
, 1024, FTW_MOUNT
)) {
313 p_err("can't iterate over %s: %s", cgroup_root
,
317 case SHOW_TREE_FN_ERR
:
325 jsonw_end_array(json_wtr
);
333 static int do_attach(int argc
, char **argv
)
335 enum bpf_attach_type attach_type
;
336 int cgroup_fd
, prog_fd
;
337 int attach_flags
= 0;
342 p_err("too few parameters for cgroup attach");
346 cgroup_fd
= open(argv
[0], O_RDONLY
);
348 p_err("can't open cgroup %s", argv
[1]);
352 attach_type
= parse_attach_type(argv
[1]);
353 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
354 p_err("invalid attach type");
360 prog_fd
= prog_parse_fd(&argc
, &argv
);
364 for (i
= 0; i
< argc
; i
++) {
365 if (is_prefix(argv
[i
], "multi")) {
366 attach_flags
|= BPF_F_ALLOW_MULTI
;
367 } else if (is_prefix(argv
[i
], "override")) {
368 attach_flags
|= BPF_F_ALLOW_OVERRIDE
;
370 p_err("unknown option: %s", argv
[i
]);
375 if (bpf_prog_attach(prog_fd
, cgroup_fd
, attach_type
, attach_flags
)) {
376 p_err("failed to attach program");
381 jsonw_null(json_wtr
);
393 static int do_detach(int argc
, char **argv
)
395 enum bpf_attach_type attach_type
;
396 int prog_fd
, cgroup_fd
;
400 p_err("too few parameters for cgroup detach");
404 cgroup_fd
= open(argv
[0], O_RDONLY
);
406 p_err("can't open cgroup %s", argv
[1]);
410 attach_type
= parse_attach_type(argv
[1]);
411 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
412 p_err("invalid attach type");
418 prog_fd
= prog_parse_fd(&argc
, &argv
);
422 if (bpf_prog_detach2(prog_fd
, cgroup_fd
, attach_type
)) {
423 p_err("failed to detach program");
428 jsonw_null(json_wtr
);
440 static int do_help(int argc
, char **argv
)
443 jsonw_null(json_wtr
);
448 "Usage: %s %s { show | list } CGROUP\n"
449 " %s %s tree [CGROUP_ROOT]\n"
450 " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
451 " %s %s detach CGROUP ATTACH_TYPE PROG\n"
454 HELP_SPEC_ATTACH_TYPES
"\n"
455 " " HELP_SPEC_ATTACH_FLAGS
"\n"
456 " " HELP_SPEC_PROGRAM
"\n"
457 " " HELP_SPEC_OPTIONS
"\n"
460 bin_name
, argv
[-2], bin_name
, argv
[-2],
461 bin_name
, argv
[-2], bin_name
, argv
[-2]);
466 static const struct cmd cmds
[] = {
469 { "tree", do_show_tree
},
470 { "attach", do_attach
},
471 { "detach", do_detach
},
476 int do_cgroup(int argc
, char **argv
)
478 return cmd_select(cmds
, argc
, argv
, do_help
);