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>
22 static const int cgroup_attach_types
[] = {
23 BPF_CGROUP_INET_INGRESS
,
24 BPF_CGROUP_INET_EGRESS
,
25 BPF_CGROUP_INET_SOCK_CREATE
,
26 BPF_CGROUP_INET_SOCK_RELEASE
,
27 BPF_CGROUP_INET4_BIND
,
28 BPF_CGROUP_INET6_BIND
,
29 BPF_CGROUP_INET4_POST_BIND
,
30 BPF_CGROUP_INET6_POST_BIND
,
31 BPF_CGROUP_INET4_CONNECT
,
32 BPF_CGROUP_INET6_CONNECT
,
33 BPF_CGROUP_UNIX_CONNECT
,
34 BPF_CGROUP_INET4_GETPEERNAME
,
35 BPF_CGROUP_INET6_GETPEERNAME
,
36 BPF_CGROUP_UNIX_GETPEERNAME
,
37 BPF_CGROUP_INET4_GETSOCKNAME
,
38 BPF_CGROUP_INET6_GETSOCKNAME
,
39 BPF_CGROUP_UNIX_GETSOCKNAME
,
40 BPF_CGROUP_UDP4_SENDMSG
,
41 BPF_CGROUP_UDP6_SENDMSG
,
42 BPF_CGROUP_UNIX_SENDMSG
,
43 BPF_CGROUP_UDP4_RECVMSG
,
44 BPF_CGROUP_UDP6_RECVMSG
,
45 BPF_CGROUP_UNIX_RECVMSG
,
49 BPF_CGROUP_GETSOCKOPT
,
50 BPF_CGROUP_SETSOCKOPT
,
54 #define HELP_SPEC_ATTACH_FLAGS \
55 "ATTACH_FLAGS := { multi | override }"
57 #define HELP_SPEC_ATTACH_TYPES \
58 " ATTACH_TYPE := { cgroup_inet_ingress | cgroup_inet_egress |\n" \
59 " cgroup_inet_sock_create | cgroup_sock_ops |\n" \
60 " cgroup_device | cgroup_inet4_bind |\n" \
61 " cgroup_inet6_bind | cgroup_inet4_post_bind |\n" \
62 " cgroup_inet6_post_bind | cgroup_inet4_connect |\n" \
63 " cgroup_inet6_connect | cgroup_unix_connect |\n" \
64 " cgroup_inet4_getpeername | cgroup_inet6_getpeername |\n" \
65 " cgroup_unix_getpeername | cgroup_inet4_getsockname |\n" \
66 " cgroup_inet6_getsockname | cgroup_unix_getsockname |\n" \
67 " cgroup_udp4_sendmsg | cgroup_udp6_sendmsg |\n" \
68 " cgroup_unix_sendmsg | cgroup_udp4_recvmsg |\n" \
69 " cgroup_udp6_recvmsg | cgroup_unix_recvmsg |\n" \
70 " cgroup_sysctl | cgroup_getsockopt |\n" \
71 " cgroup_setsockopt | cgroup_inet_sock_release }"
73 static unsigned int query_flags
;
74 static struct btf
*btf_vmlinux
;
75 static __u32 btf_vmlinux_id
;
77 static enum bpf_attach_type
parse_attach_type(const char *str
)
79 const char *attach_type_str
;
80 enum bpf_attach_type type
;
82 for (type
= 0; ; type
++) {
83 attach_type_str
= libbpf_bpf_attach_type_str(type
);
86 if (!strcmp(str
, attach_type_str
))
90 /* Also check traditionally used attach type strings. For these we keep
91 * allowing prefixed usage.
93 for (type
= 0; ; type
++) {
94 attach_type_str
= bpf_attach_type_input_str(type
);
97 if (is_prefix(str
, attach_type_str
))
101 return __MAX_BPF_ATTACH_TYPE
;
104 static void guess_vmlinux_btf_id(__u32 attach_btf_obj_id
)
106 struct bpf_btf_info btf_info
= {};
107 __u32 btf_len
= sizeof(btf_info
);
112 btf_info
.name
= ptr_to_u64(name
);
113 btf_info
.name_len
= sizeof(name
);
115 fd
= bpf_btf_get_fd_by_id(attach_btf_obj_id
);
119 err
= bpf_btf_get_info_by_fd(fd
, &btf_info
, &btf_len
);
123 if (btf_info
.kernel_btf
&& strncmp(name
, "vmlinux", sizeof(name
)) == 0)
124 btf_vmlinux_id
= btf_info
.id
;
130 static int show_bpf_prog(int id
, enum bpf_attach_type attach_type
,
131 const char *attach_flags_str
,
134 char prog_name
[MAX_PROG_FULL_NAME
];
135 const char *attach_btf_name
= NULL
;
136 struct bpf_prog_info info
= {};
137 const char *attach_type_str
;
138 __u32 info_len
= sizeof(info
);
141 prog_fd
= bpf_prog_get_fd_by_id(id
);
145 if (bpf_prog_get_info_by_fd(prog_fd
, &info
, &info_len
)) {
150 attach_type_str
= libbpf_bpf_attach_type_str(attach_type
);
154 guess_vmlinux_btf_id(info
.attach_btf_obj_id
);
156 if (btf_vmlinux_id
== info
.attach_btf_obj_id
&&
157 info
.attach_btf_id
< btf__type_cnt(btf_vmlinux
)) {
158 const struct btf_type
*t
=
159 btf__type_by_id(btf_vmlinux
, info
.attach_btf_id
);
161 btf__name_by_offset(btf_vmlinux
, t
->name_off
);
165 get_prog_full_name(&info
, prog_fd
, prog_name
, sizeof(prog_name
));
167 jsonw_start_object(json_wtr
);
168 jsonw_uint_field(json_wtr
, "id", info
.id
);
170 jsonw_string_field(json_wtr
, "attach_type", attach_type_str
);
172 jsonw_uint_field(json_wtr
, "attach_type", attach_type
);
173 if (!(query_flags
& BPF_F_QUERY_EFFECTIVE
))
174 jsonw_string_field(json_wtr
, "attach_flags", attach_flags_str
);
175 jsonw_string_field(json_wtr
, "name", prog_name
);
177 jsonw_string_field(json_wtr
, "attach_btf_name", attach_btf_name
);
178 jsonw_uint_field(json_wtr
, "attach_btf_obj_id", info
.attach_btf_obj_id
);
179 jsonw_uint_field(json_wtr
, "attach_btf_id", info
.attach_btf_id
);
180 jsonw_end_object(json_wtr
);
182 printf("%s%-8u ", level
? " " : "", info
.id
);
184 printf("%-15s", attach_type_str
);
186 printf("type %-10u", attach_type
);
187 if (query_flags
& BPF_F_QUERY_EFFECTIVE
)
188 printf(" %-15s", prog_name
);
190 printf(" %-15s %-15s", attach_flags_str
, prog_name
);
192 printf(" %-15s", attach_btf_name
);
193 else if (info
.attach_btf_id
)
194 printf(" attach_btf_obj_id=%d attach_btf_id=%d",
195 info
.attach_btf_obj_id
, info
.attach_btf_id
);
203 static int count_attached_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
)
208 ret
= bpf_prog_query(cgroup_fd
, type
, query_flags
, NULL
,
216 static int cgroup_has_attached_progs(int cgroup_fd
)
221 for (i
= 0; i
< ARRAY_SIZE(cgroup_attach_types
); i
++) {
222 int count
= count_attached_bpf_progs(cgroup_fd
, cgroup_attach_types
[i
]);
233 return no_prog
? 0 : 1;
236 static int show_effective_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
,
239 LIBBPF_OPTS(bpf_prog_query_opts
, p
);
240 __u32 prog_ids
[1024] = {0};
244 p
.query_flags
= query_flags
;
245 p
.prog_cnt
= ARRAY_SIZE(prog_ids
);
246 p
.prog_ids
= prog_ids
;
248 ret
= bpf_prog_query_opts(cgroup_fd
, type
, &p
);
255 for (iter
= 0; iter
< p
.prog_cnt
; iter
++)
256 show_bpf_prog(prog_ids
[iter
], type
, NULL
, level
);
261 static int show_attached_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
,
264 LIBBPF_OPTS(bpf_prog_query_opts
, p
);
265 __u32 prog_attach_flags
[1024] = {0};
266 const char *attach_flags_str
;
267 __u32 prog_ids
[1024] = {0};
272 p
.query_flags
= query_flags
;
273 p
.prog_cnt
= ARRAY_SIZE(prog_ids
);
274 p
.prog_ids
= prog_ids
;
275 p
.prog_attach_flags
= prog_attach_flags
;
277 ret
= bpf_prog_query_opts(cgroup_fd
, type
, &p
);
284 for (iter
= 0; iter
< p
.prog_cnt
; iter
++) {
287 attach_flags
= prog_attach_flags
[iter
] ?: p
.attach_flags
;
289 switch (attach_flags
) {
290 case BPF_F_ALLOW_MULTI
:
291 attach_flags_str
= "multi";
293 case BPF_F_ALLOW_OVERRIDE
:
294 attach_flags_str
= "override";
297 attach_flags_str
= "";
300 snprintf(buf
, sizeof(buf
), "unknown(%x)", attach_flags
);
301 attach_flags_str
= buf
;
304 show_bpf_prog(prog_ids
[iter
], type
,
305 attach_flags_str
, level
);
311 static int show_bpf_progs(int cgroup_fd
, enum bpf_attach_type type
,
314 return query_flags
& BPF_F_QUERY_EFFECTIVE
?
315 show_effective_bpf_progs(cgroup_fd
, type
, level
) :
316 show_attached_bpf_progs(cgroup_fd
, type
, level
);
319 static int do_show(int argc
, char **argv
)
321 enum bpf_attach_type type
;
322 int has_attached_progs
;
334 if (is_prefix(*argv
, "effective")) {
335 if (query_flags
& BPF_F_QUERY_EFFECTIVE
) {
336 p_err("duplicated argument: %s", *argv
);
339 query_flags
|= BPF_F_QUERY_EFFECTIVE
;
342 p_err("expected no more arguments, 'effective', got: '%s'?",
348 cgroup_fd
= open(path
, O_RDONLY
);
350 p_err("can't open cgroup %s", path
);
354 has_attached_progs
= cgroup_has_attached_progs(cgroup_fd
);
355 if (has_attached_progs
< 0) {
356 p_err("can't query bpf programs attached to %s: %s",
357 path
, strerror(errno
));
359 } else if (!has_attached_progs
) {
365 jsonw_start_array(json_wtr
);
366 else if (query_flags
& BPF_F_QUERY_EFFECTIVE
)
367 printf("%-8s %-15s %-15s\n", "ID", "AttachType", "Name");
369 printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
370 "AttachFlags", "Name");
372 btf_vmlinux
= libbpf_find_kernel_btf();
373 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++) {
375 * Not all attach types may be supported, so it's expected,
376 * that some requests will fail.
377 * If we were able to get the show for at least one
378 * attach type, let's return 0.
380 if (show_bpf_progs(cgroup_fd
, type
, 0) == 0)
385 jsonw_end_array(json_wtr
);
394 * To distinguish nftw() errors and do_show_tree_fn() errors
395 * and avoid duplicating error messages, let's return -2
396 * from do_show_tree_fn() in case of error.
399 #define SHOW_TREE_FN_ERR -2
400 static int do_show_tree_fn(const char *fpath
, const struct stat
*sb
,
401 int typeflag
, struct FTW
*ftw
)
403 enum bpf_attach_type type
;
404 int has_attached_progs
;
407 if (typeflag
!= FTW_D
)
410 cgroup_fd
= open(fpath
, O_RDONLY
);
412 p_err("can't open cgroup %s: %s", fpath
, strerror(errno
));
413 return SHOW_TREE_FN_ERR
;
416 has_attached_progs
= cgroup_has_attached_progs(cgroup_fd
);
417 if (has_attached_progs
< 0) {
418 p_err("can't query bpf programs attached to %s: %s",
419 fpath
, strerror(errno
));
421 return SHOW_TREE_FN_ERR
;
422 } else if (!has_attached_progs
) {
428 jsonw_start_object(json_wtr
);
429 jsonw_string_field(json_wtr
, "cgroup", fpath
);
430 jsonw_name(json_wtr
, "programs");
431 jsonw_start_array(json_wtr
);
433 printf("%s\n", fpath
);
436 btf_vmlinux
= libbpf_find_kernel_btf();
437 for (type
= 0; type
< __MAX_BPF_ATTACH_TYPE
; type
++)
438 show_bpf_progs(cgroup_fd
, type
, ftw
->level
);
441 /* Last attach type does not support query.
442 * Do not report an error for this, especially because batch
443 * mode would stop processing commands.
448 jsonw_end_array(json_wtr
);
449 jsonw_end_object(json_wtr
);
457 static char *find_cgroup_root(void)
462 f
= fopen("/proc/mounts", "r");
466 while ((mnt
= getmntent(f
))) {
467 if (strcmp(mnt
->mnt_type
, "cgroup2") == 0) {
469 return strdup(mnt
->mnt_dir
);
477 static int do_show_tree(int argc
, char **argv
)
479 char *cgroup_root
, *cgroup_alloced
= NULL
;
485 cgroup_alloced
= find_cgroup_root();
486 if (!cgroup_alloced
) {
487 p_err("cgroup v2 isn't mounted");
490 cgroup_root
= cgroup_alloced
;
492 cgroup_root
= GET_ARG();
495 if (is_prefix(*argv
, "effective")) {
496 if (query_flags
& BPF_F_QUERY_EFFECTIVE
) {
497 p_err("duplicated argument: %s", *argv
);
500 query_flags
|= BPF_F_QUERY_EFFECTIVE
;
503 p_err("expected no more arguments, 'effective', got: '%s'?",
511 jsonw_start_array(json_wtr
);
512 else if (query_flags
& BPF_F_QUERY_EFFECTIVE
)
514 "%-8s %-15s %-15s\n",
516 "ID", "AttachType", "Name");
519 "%-8s %-15s %-15s %-15s\n",
521 "ID", "AttachType", "AttachFlags", "Name");
523 switch (nftw(cgroup_root
, do_show_tree_fn
, 1024, FTW_MOUNT
)) {
525 p_err("can't iterate over %s: %s", cgroup_root
,
529 case SHOW_TREE_FN_ERR
:
537 jsonw_end_array(json_wtr
);
539 free(cgroup_alloced
);
544 static int do_attach(int argc
, char **argv
)
546 enum bpf_attach_type attach_type
;
547 int cgroup_fd
, prog_fd
;
548 int attach_flags
= 0;
553 p_err("too few parameters for cgroup attach");
557 cgroup_fd
= open(argv
[0], O_RDONLY
);
559 p_err("can't open cgroup %s", argv
[0]);
563 attach_type
= parse_attach_type(argv
[1]);
564 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
565 p_err("invalid attach type");
571 prog_fd
= prog_parse_fd(&argc
, &argv
);
575 for (i
= 0; i
< argc
; i
++) {
576 if (is_prefix(argv
[i
], "multi")) {
577 attach_flags
|= BPF_F_ALLOW_MULTI
;
578 } else if (is_prefix(argv
[i
], "override")) {
579 attach_flags
|= BPF_F_ALLOW_OVERRIDE
;
581 p_err("unknown option: %s", argv
[i
]);
586 if (bpf_prog_attach(prog_fd
, cgroup_fd
, attach_type
, attach_flags
)) {
587 p_err("failed to attach program");
592 jsonw_null(json_wtr
);
604 static int do_detach(int argc
, char **argv
)
606 enum bpf_attach_type attach_type
;
607 int prog_fd
, cgroup_fd
;
611 p_err("too few parameters for cgroup detach");
615 cgroup_fd
= open(argv
[0], O_RDONLY
);
617 p_err("can't open cgroup %s", argv
[0]);
621 attach_type
= parse_attach_type(argv
[1]);
622 if (attach_type
== __MAX_BPF_ATTACH_TYPE
) {
623 p_err("invalid attach type");
629 prog_fd
= prog_parse_fd(&argc
, &argv
);
633 if (bpf_prog_detach2(prog_fd
, cgroup_fd
, attach_type
)) {
634 p_err("failed to detach program");
639 jsonw_null(json_wtr
);
651 static int do_help(int argc
, char **argv
)
654 jsonw_null(json_wtr
);
659 "Usage: %1$s %2$s { show | list } CGROUP [**effective**]\n"
660 " %1$s %2$s tree [CGROUP_ROOT] [**effective**]\n"
661 " %1$s %2$s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
662 " %1$s %2$s detach CGROUP ATTACH_TYPE PROG\n"
665 HELP_SPEC_ATTACH_TYPES
"\n"
666 " " HELP_SPEC_ATTACH_FLAGS
"\n"
667 " " HELP_SPEC_PROGRAM
"\n"
668 " " HELP_SPEC_OPTIONS
" |\n"
676 static const struct cmd cmds
[] = {
679 { "tree", do_show_tree
},
680 { "attach", do_attach
},
681 { "detach", do_detach
},
686 int do_cgroup(int argc
, char **argv
)
688 return cmd_select(cmds
, argc
, argv
, do_help
);