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 |\n" \
29 " recvmsg4 | recvmsg6 | sysctl |\n" \
30 " getsockopt | setsockopt }"
32 static unsigned int query_flags;
34 static enum bpf_attach_type parse_attach_type(const char *str)
36 enum bpf_attach_type type;
38 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
39 if (attach_type_name[type] &&
40 is_prefix(str, attach_type_name[type]))
44 return __MAX_BPF_ATTACH_TYPE;
47 static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
48 const char *attach_flags_str,
51 struct bpf_prog_info info = {};
52 __u32 info_len = sizeof(info);
55 prog_fd = bpf_prog_get_fd_by_id(id);
59 if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) {
65 jsonw_start_object(json_wtr);
66 jsonw_uint_field(json_wtr, "id", info.id);
67 if (attach_type < ARRAY_SIZE(attach_type_name))
68 jsonw_string_field(json_wtr, "attach_type",
69 attach_type_name[attach_type]);
71 jsonw_uint_field(json_wtr, "attach_type", attach_type);
72 jsonw_string_field(json_wtr, "attach_flags",
74 jsonw_string_field(json_wtr, "name", info.name);
75 jsonw_end_object(json_wtr);
77 printf("%s%-8u ", level ? " " : "", info.id);
78 if (attach_type < ARRAY_SIZE(attach_type_name))
79 printf("%-15s", attach_type_name[attach_type]);
81 printf("type %-10u", attach_type);
82 printf(" %-15s %-15s\n", attach_flags_str, info.name);
89 static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
94 ret = bpf_prog_query(cgroup_fd, type, query_flags, NULL,
102 static int cgroup_has_attached_progs(int cgroup_fd)
104 enum bpf_attach_type type;
107 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
108 int count = count_attached_bpf_progs(cgroup_fd, type);
110 if (count < 0 && errno != EINVAL)
119 return no_prog ? 0 : 1;
121 static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
124 const char *attach_flags_str;
125 __u32 prog_ids[1024] = {0};
126 __u32 prog_cnt, iter;
131 prog_cnt = ARRAY_SIZE(prog_ids);
132 ret = bpf_prog_query(cgroup_fd, type, query_flags, &attach_flags,
133 prog_ids, &prog_cnt);
140 switch (attach_flags) {
141 case BPF_F_ALLOW_MULTI:
142 attach_flags_str = "multi";
144 case BPF_F_ALLOW_OVERRIDE:
145 attach_flags_str = "override";
148 attach_flags_str = "";
151 snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
152 attach_flags_str = buf;
155 for (iter = 0; iter < prog_cnt; iter++)
156 show_bpf_prog(prog_ids[iter], type,
157 attach_flags_str, level);
162 static int do_show(int argc, char **argv)
164 enum bpf_attach_type type;
165 int has_attached_progs;
177 if (is_prefix(*argv, "effective")) {
178 if (query_flags & BPF_F_QUERY_EFFECTIVE) {
179 p_err("duplicated argument: %s", *argv);
182 query_flags |= BPF_F_QUERY_EFFECTIVE;
185 p_err("expected no more arguments, 'effective', got: '%s'?",
191 cgroup_fd = open(path, O_RDONLY);
193 p_err("can't open cgroup %s", path);
197 has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
198 if (has_attached_progs < 0) {
199 p_err("can't query bpf programs attached to %s: %s",
200 path, strerror(errno));
202 } else if (!has_attached_progs) {
208 jsonw_start_array(json_wtr);
210 printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
211 "AttachFlags", "Name");
213 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
215 * Not all attach types may be supported, so it's expected,
216 * that some requests will fail.
217 * If we were able to get the show for at least one
218 * attach type, let's return 0.
220 if (show_attached_bpf_progs(cgroup_fd, type, 0) == 0)
225 jsonw_end_array(json_wtr);
234 * To distinguish nftw() errors and do_show_tree_fn() errors
235 * and avoid duplicating error messages, let's return -2
236 * from do_show_tree_fn() in case of error.
239 #define SHOW_TREE_FN_ERR -2
240 static int do_show_tree_fn(const char *fpath, const struct stat *sb,
241 int typeflag, struct FTW *ftw)
243 enum bpf_attach_type type;
244 int has_attached_progs;
247 if (typeflag != FTW_D)
250 cgroup_fd = open(fpath, O_RDONLY);
252 p_err("can't open cgroup %s: %s", fpath, strerror(errno));
253 return SHOW_TREE_FN_ERR;
256 has_attached_progs = cgroup_has_attached_progs(cgroup_fd);
257 if (has_attached_progs < 0) {
258 p_err("can't query bpf programs attached to %s: %s",
259 fpath, strerror(errno));
261 return SHOW_TREE_FN_ERR;
262 } else if (!has_attached_progs) {
268 jsonw_start_object(json_wtr);
269 jsonw_string_field(json_wtr, "cgroup", fpath);
270 jsonw_name(json_wtr, "programs");
271 jsonw_start_array(json_wtr);
273 printf("%s\n", fpath);
276 for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++)
277 show_attached_bpf_progs(cgroup_fd, type, ftw->level);
280 /* Last attach type does not support query.
281 * Do not report an error for this, especially because batch
282 * mode would stop processing commands.
287 jsonw_end_array(json_wtr);
288 jsonw_end_object(json_wtr);
296 static char *find_cgroup_root(void)
301 f = fopen("/proc/mounts", "r");
305 while ((mnt = getmntent(f))) {
306 if (strcmp(mnt->mnt_type, "cgroup2") == 0) {
308 return strdup(mnt->mnt_dir);
316 static int do_show_tree(int argc, char **argv)
318 char *cgroup_root, *cgroup_alloced = NULL;
324 cgroup_alloced = find_cgroup_root();
325 if (!cgroup_alloced) {
326 p_err("cgroup v2 isn't mounted");
329 cgroup_root = cgroup_alloced;
331 cgroup_root = GET_ARG();
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'?",
350 jsonw_start_array(json_wtr);
353 "%-8s %-15s %-15s %-15s\n",
355 "ID", "AttachType", "AttachFlags", "Name");
357 switch (nftw(cgroup_root, do_show_tree_fn, 1024, FTW_MOUNT)) {
359 p_err("can't iterate over %s: %s", cgroup_root,
363 case SHOW_TREE_FN_ERR:
371 jsonw_end_array(json_wtr);
373 free(cgroup_alloced);
378 static int do_attach(int argc, char **argv)
380 enum bpf_attach_type attach_type;
381 int cgroup_fd, prog_fd;
382 int attach_flags = 0;
387 p_err("too few parameters for cgroup attach");
391 cgroup_fd = open(argv[0], O_RDONLY);
393 p_err("can't open cgroup %s", argv[0]);
397 attach_type = parse_attach_type(argv[1]);
398 if (attach_type == __MAX_BPF_ATTACH_TYPE) {
399 p_err("invalid attach type");
405 prog_fd = prog_parse_fd(&argc, &argv);
409 for (i = 0; i < argc; i++) {
410 if (is_prefix(argv[i], "multi")) {
411 attach_flags |= BPF_F_ALLOW_MULTI;
412 } else if (is_prefix(argv[i], "override")) {
413 attach_flags |= BPF_F_ALLOW_OVERRIDE;
415 p_err("unknown option: %s", argv[i]);
420 if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) {
421 p_err("failed to attach program");
426 jsonw_null(json_wtr);
438 static int do_detach(int argc, char **argv)
440 enum bpf_attach_type attach_type;
441 int prog_fd, cgroup_fd;
445 p_err("too few parameters for cgroup detach");
449 cgroup_fd = open(argv[0], O_RDONLY);
451 p_err("can't open cgroup %s", argv[0]);
455 attach_type = parse_attach_type(argv[1]);
456 if (attach_type == __MAX_BPF_ATTACH_TYPE) {
457 p_err("invalid attach type");
463 prog_fd = prog_parse_fd(&argc, &argv);
467 if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) {
468 p_err("failed to detach program");
473 jsonw_null(json_wtr);
485 static int do_help(int argc, char **argv)
488 jsonw_null(json_wtr);
493 "Usage: %s %s { show | list } CGROUP [**effective**]\n"
494 " %s %s tree [CGROUP_ROOT] [**effective**]\n"
495 " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n"
496 " %s %s detach CGROUP ATTACH_TYPE PROG\n"
499 HELP_SPEC_ATTACH_TYPES "\n"
500 " " HELP_SPEC_ATTACH_FLAGS "\n"
501 " " HELP_SPEC_PROGRAM "\n"
502 " " HELP_SPEC_OPTIONS "\n"
505 bin_name, argv[-2], bin_name, argv[-2],
506 bin_name, argv[-2], bin_name, argv[-2]);
511 static const struct cmd cmds[] = {
514 { "tree", do_show_tree },
515 { "attach", do_attach },
516 { "detach", do_detach },
521 int do_cgroup(int argc, char **argv)
523 return cmd_select(cmds, argc, argv, do_help);