1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2017 Facebook
5 #define _XOPEN_SOURCE 500
14 #include <sys/types.h>
22 #define HELP_SPEC_ATTACH_FLAGS \
23 "ATTACH_FLAGS := { multi | override }"
25 #define HELP_SPEC_ATTACH_TYPES \
26 " ATTACH_TYPE := { cgroup_inet_ingress | cgroup_inet_egress |\n" \
27 " cgroup_inet_sock_create | cgroup_sock_ops |\n" \
28 " cgroup_device | cgroup_inet4_bind |\n" \
29 " cgroup_inet6_bind | cgroup_inet4_post_bind |\n" \
30 " cgroup_inet6_post_bind | cgroup_inet4_connect |\n" \
31 " cgroup_inet6_connect | cgroup_inet4_getpeername |\n" \
32 " cgroup_inet6_getpeername | cgroup_inet4_getsockname |\n" \
33 " cgroup_inet6_getsockname | cgroup_udp4_sendmsg |\n" \
34 " cgroup_udp6_sendmsg | cgroup_udp4_recvmsg |\n" \
35 " cgroup_udp6_recvmsg | cgroup_sysctl |\n" \
36 " cgroup_getsockopt | cgroup_setsockopt |\n" \
37 " cgroup_inet_sock_release }"
39 static unsigned int query_flags;
40 static struct btf *btf_vmlinux;
41 static __u32 btf_vmlinux_id;
43 static enum bpf_attach_type parse_attach_type(const char *str)
45 const char *attach_type_str;
46 enum bpf_attach_type type;
48 for (type = 0; ; type++) {
49 attach_type_str = libbpf_bpf_attach_type_str(type);
52 if (!strcmp(str, attach_type_str))
56 /* Also check traditionally used attach type strings. For these we keep
57 * allowing prefixed usage.
59 for (type = 0; ; type++) {
60 attach_type_str = bpf_attach_type_input_str(type);
63 if (is_prefix(str, attach_type_str))
67 return __MAX_BPF_ATTACH_TYPE;
70 static void guess_vmlinux_btf_id(__u32 attach_btf_obj_id)
72 struct bpf_btf_info btf_info = {};
73 __u32 btf_len = sizeof(btf_info);
78 btf_info.name = ptr_to_u64(name);
79 btf_info.name_len = sizeof(name);
81 fd = bpf_btf_get_fd_by_id(attach_btf_obj_id);
85 err = bpf_btf_get_info_by_fd(fd, &btf_info, &btf_len);
89 if (btf_info.kernel_btf && strncmp(name, "vmlinux", sizeof(name)) == 0)
90 btf_vmlinux_id = btf_info.id;
96 static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
97 const char *attach_flags_str,
100 char prog_name[MAX_PROG_FULL_NAME];
101 const char *attach_btf_name = NULL;
102 struct bpf_prog_info info = {};
103 const char *attach_type_str;
104 __u32 info_len = sizeof(info);
107 prog_fd = bpf_prog_get_fd_by_id(id);
111 if (bpf_prog_get_info_by_fd(prog_fd, &info, &info_len)) {
116 attach_type_str = libbpf_bpf_attach_type_str(attach_type);
120 guess_vmlinux_btf_id(info.attach_btf_obj_id);
122 if (btf_vmlinux_id == info.attach_btf_obj_id &&
123 info.attach_btf_id < btf__type_cnt(btf_vmlinux)) {
124 const struct btf_type *t =
125 btf__type_by_id(btf_vmlinux, info.attach_btf_id);
127 btf__name_by_offset(btf_vmlinux, t->name_off);
131 get_prog_full_name(&info, prog_fd, prog_name, sizeof(prog_name));
133 jsonw_start_object(json_wtr);
134 jsonw_uint_field(json_wtr, "id", info.id);
136 jsonw_string_field(json_wtr, "attach_type", attach_type_str);
138 jsonw_uint_field(json_wtr, "attach_type", attach_type);
139 if (!(query_flags & BPF_F_QUERY_EFFECTIVE))
140 jsonw_string_field(json_wtr, "attach_flags", attach_flags_str);
141 jsonw_string_field(json_wtr, "name", prog_name);
143 jsonw_string_field(json_wtr, "attach_btf_name", attach_btf_name);
144 jsonw_uint_field(json_wtr, "attach_btf_obj_id", info.attach_btf_obj_id);
145 jsonw_uint_field(json_wtr, "attach_btf_id", info.attach_btf_id);
146 jsonw_end_object(json_wtr);
148 printf("%s%-8u ", level ? " " : "", info.id);
150 printf("%-15s", attach_type_str);
152 printf("type %-10u", attach_type);
153 if (query_flags & BPF_F_QUERY_EFFECTIVE)
154 printf(" %-15s", prog_name);
156 printf(" %-15s %-15s", attach_flags_str, prog_name);
158 printf(" %-15s", attach_btf_name);
159 else if (info.attach_btf_id)
160 printf(" attach_btf_obj_id=%d attach_btf_id=%d",
161 info.attach_btf_obj_id, info.attach_btf_id);
169 static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
174 ret = bpf_prog_query(cgroup_fd, type, query_flags, NULL,
182 static int cgroup_has_attached_progs(int cgroup_fd)
184 enum bpf_attach_type type;
187 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
188 int count = count_attached_bpf_progs(cgroup_fd, type);
190 if (count < 0 && errno != EINVAL)
199 return no_prog ? 0 : 1;
202 static int show_effective_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
205 LIBBPF_OPTS(bpf_prog_query_opts, p);
206 __u32 prog_ids[1024] = {0};
210 p.query_flags = query_flags;
211 p.prog_cnt = ARRAY_SIZE(prog_ids);
212 p.prog_ids = prog_ids;
214 ret = bpf_prog_query_opts(cgroup_fd, type, &p);
221 for (iter = 0; iter < p.prog_cnt; iter++)
222 show_bpf_prog(prog_ids[iter], type, NULL, level);
227 static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
230 LIBBPF_OPTS(bpf_prog_query_opts, p);
231 __u32 prog_attach_flags[1024] = {0};
232 const char *attach_flags_str;
233 __u32 prog_ids[1024] = {0};
238 p.query_flags = query_flags;
239 p.prog_cnt = ARRAY_SIZE(prog_ids);
240 p.prog_ids = prog_ids;
241 p.prog_attach_flags = prog_attach_flags;
243 ret = bpf_prog_query_opts(cgroup_fd, type, &p);
250 for (iter = 0; iter < p.prog_cnt; iter++) {
253 attach_flags = prog_attach_flags[iter] ?: p.attach_flags;
255 switch (attach_flags) {
256 case BPF_F_ALLOW_MULTI:
257 attach_flags_str = "multi";
259 case BPF_F_ALLOW_OVERRIDE:
260 attach_flags_str = "override";
263 attach_flags_str = "";
266 snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
267 attach_flags_str = buf;
270 show_bpf_prog(prog_ids[iter], type,
271 attach_flags_str, level);
277 static int show_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
280 return query_flags & BPF_F_QUERY_EFFECTIVE ?
281 show_effective_bpf_progs(cgroup_fd, type, level) :
282 show_attached_bpf_progs(cgroup_fd, type, level);
285 static int do_show(int argc, char **argv)
287 enum bpf_attach_type type;
288 int has_attached_progs;
300 if (is_prefix(*argv, "effective")) {
301 if (query_flags & BPF_F_QUERY_EFFECTIVE) {
302 p_err("duplicated argument: %s", *argv);
305 query_flags |= BPF_F_QUERY_EFFECTIVE;
308 p_err("expected no more arguments, 'effective', got: '%s'?",
314 cgroup_fd = open(path, O_RDONLY);
316 p_err("can't open cgroup %s", path);
320 has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
321 if (has_attached_progs < 0) {
322 p_err("can't query bpf programs attached to %s: %s",
323 path, strerror(errno));
325 } else if (!has_attached_progs) {
331 jsonw_start_array(json_wtr);
332 else if (query_flags & BPF_F_QUERY_EFFECTIVE)
333 printf("%-8s %-15s %-15s\n", "ID", "AttachType", "Name");
335 printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
336 "AttachFlags", "Name");
338 btf_vmlinux = libbpf_find_kernel_btf();
339 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
341 * Not all attach types may be supported, so it's expected,
342 * that some requests will fail.
343 * If we were able to get the show for at least one
344 * attach type, let's return 0.
346 if (show_bpf_progs(cgroup_fd, type, 0) == 0)
351 jsonw_end_array(json_wtr);
360 * To distinguish nftw() errors and do_show_tree_fn() errors
361 * and avoid duplicating error messages, let's return -2
362 * from do_show_tree_fn() in case of error.
365 #define SHOW_TREE_FN_ERR -2
366 static int do_show_tree_fn(const char *fpath, const struct stat *sb,
367 int typeflag, struct FTW *ftw)
369 enum bpf_attach_type type;
370 int has_attached_progs;
373 if (typeflag != FTW_D)
376 cgroup_fd = open(fpath, O_RDONLY);
378 p_err("can't open cgroup %s: %s", fpath, strerror(errno));
379 return SHOW_TREE_FN_ERR;
382 has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
383 if (has_attached_progs < 0) {
384 p_err("can't query bpf programs attached to %s: %s",
385 fpath, strerror(errno));
387 return SHOW_TREE_FN_ERR;
388 } else if (!has_attached_progs) {
394 jsonw_start_object(json_wtr);
395 jsonw_string_field(json_wtr, "cgroup", fpath);
396 jsonw_name(json_wtr, "programs");
397 jsonw_start_array(json_wtr);
399 printf("%s\n", fpath);
402 btf_vmlinux = libbpf_find_kernel_btf();
403 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++)
404 show_bpf_progs(cgroup_fd, type, ftw->level);
407 /* Last attach type does not support query.
408 * Do not report an error for this, especially because batch
409 * mode would stop processing commands.
414 jsonw_end_array(json_wtr);
415 jsonw_end_object(json_wtr);
423 static char *find_cgroup_root(void)
428 f = fopen("/proc/mounts", "r");
432 while ((mnt = getmntent(f))) {
433 if (strcmp(mnt->mnt_type, "cgroup2") == 0) {
435 return strdup(mnt->mnt_dir);
443 static int do_show_tree(int argc, char **argv)
445 char *cgroup_root, *cgroup_alloced = NULL;
451 cgroup_alloced = find_cgroup_root();
452 if (!cgroup_alloced) {
453 p_err("cgroup v2 isn't mounted");
456 cgroup_root = cgroup_alloced;
458 cgroup_root = GET_ARG();
461 if (is_prefix(*argv, "effective")) {
462 if (query_flags & BPF_F_QUERY_EFFECTIVE) {
463 p_err("duplicated argument: %s", *argv);
466 query_flags |= BPF_F_QUERY_EFFECTIVE;
469 p_err("expected no more arguments, 'effective', got: '%s'?",
477 jsonw_start_array(json_wtr);
478 else if (query_flags & BPF_F_QUERY_EFFECTIVE)
480 "%-8s %-15s %-15s\n",
482 "ID", "AttachType", "Name");
485 "%-8s %-15s %-15s %-15s\n",
487 "ID", "AttachType", "AttachFlags", "Name");
489 switch (nftw(cgroup_root, do_show_tree_fn, 1024, FTW_MOUNT)) {
491 p_err("can't iterate over %s: %s", cgroup_root,
495 case SHOW_TREE_FN_ERR:
503 jsonw_end_array(json_wtr);
505 free(cgroup_alloced);
510 static int do_attach(int argc, char **argv)
512 enum bpf_attach_type attach_type;
513 int cgroup_fd, prog_fd;
514 int attach_flags = 0;
519 p_err("too few parameters for cgroup attach");
523 cgroup_fd = open(argv[0], O_RDONLY);
525 p_err("can't open cgroup %s", argv[0]);
529 attach_type = parse_attach_type(argv[1]);
530 if (attach_type == __MAX_BPF_ATTACH_TYPE) {
531 p_err("invalid attach type");
537 prog_fd = prog_parse_fd(&argc, &argv);
541 for (i = 0; i < argc; i++) {
542 if (is_prefix(argv[i], "multi")) {
543 attach_flags |= BPF_F_ALLOW_MULTI;
544 } else if (is_prefix(argv[i], "override")) {
545 attach_flags |= BPF_F_ALLOW_OVERRIDE;
547 p_err("unknown option: %s", argv[i]);
552 if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) {
553 p_err("failed to attach program");
558 jsonw_null(json_wtr);
570 static int do_detach(int argc, char **argv)
572 enum bpf_attach_type attach_type;
573 int prog_fd, cgroup_fd;
577 p_err("too few parameters for cgroup detach");
581 cgroup_fd = open(argv[0], O_RDONLY);
583 p_err("can't open cgroup %s", argv[0]);
587 attach_type = parse_attach_type(argv[1]);
588 if (attach_type == __MAX_BPF_ATTACH_TYPE) {
589 p_err("invalid attach type");
595 prog_fd = prog_parse_fd(&argc, &argv);
599 if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) {
600 p_err("failed to detach program");
605 jsonw_null(json_wtr);
617 static int do_help(int argc, char **argv)
620 jsonw_null(json_wtr);
625 "Usage: %1$s %2$s { show | list } CGROUP [**effective**]\n"
626 " %1$s %2$s tree [CGROUP_ROOT] [**effective**]\n"
627 " %1$s %2$s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
628 " %1$s %2$s detach CGROUP ATTACH_TYPE PROG\n"
631 HELP_SPEC_ATTACH_TYPES "\n"
632 " " HELP_SPEC_ATTACH_FLAGS "\n"
633 " " HELP_SPEC_PROGRAM "\n"
634 " " HELP_SPEC_OPTIONS " |\n"
642 static const struct cmd cmds[] = {
645 { "tree", do_show_tree },
646 { "attach", do_attach },
647 { "detach", do_detach },
652 int do_cgroup(int argc, char **argv)
654 return cmd_select(cmds, argc, argv, do_help);