Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // Copyright (C) 2017 Facebook |
| 3 | // Author: Roman Gushchin <guro@fb.com> |
| 4 | |
| 5 | #include <fcntl.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <bpf.h> |
| 13 | |
| 14 | #include "main.h" |
| 15 | |
| 16 | #define HELP_SPEC_ATTACH_FLAGS \ |
| 17 | "ATTACH_FLAGS := { multi | override }" |
| 18 | |
Andrey Ignatov | 393de51 | 2018-04-17 10:28:44 -0700 | [diff] [blame^] | 19 | #define HELP_SPEC_ATTACH_TYPES \ |
| 20 | " ATTACH_TYPE := { ingress | egress | sock_create |\n" \ |
| 21 | " sock_ops | device | bind4 | bind6 |\n" \ |
| 22 | " post_bind4 | post_bind6 | connect4 |\n" \ |
| 23 | " connect6 }" |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 24 | |
| 25 | static const char * const attach_type_strings[] = { |
| 26 | [BPF_CGROUP_INET_INGRESS] = "ingress", |
| 27 | [BPF_CGROUP_INET_EGRESS] = "egress", |
| 28 | [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", |
| 29 | [BPF_CGROUP_SOCK_OPS] = "sock_ops", |
| 30 | [BPF_CGROUP_DEVICE] = "device", |
Andrey Ignatov | 393de51 | 2018-04-17 10:28:44 -0700 | [diff] [blame^] | 31 | [BPF_CGROUP_INET4_BIND] = "bind4", |
| 32 | [BPF_CGROUP_INET6_BIND] = "bind6", |
| 33 | [BPF_CGROUP_INET4_CONNECT] = "connect4", |
| 34 | [BPF_CGROUP_INET6_CONNECT] = "connect6", |
| 35 | [BPF_CGROUP_INET4_POST_BIND] = "post_bind4", |
| 36 | [BPF_CGROUP_INET6_POST_BIND] = "post_bind6", |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 37 | [__MAX_BPF_ATTACH_TYPE] = NULL, |
| 38 | }; |
| 39 | |
| 40 | static enum bpf_attach_type parse_attach_type(const char *str) |
| 41 | { |
| 42 | enum bpf_attach_type type; |
| 43 | |
| 44 | for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { |
| 45 | if (attach_type_strings[type] && |
| 46 | is_prefix(str, attach_type_strings[type])) |
| 47 | return type; |
| 48 | } |
| 49 | |
| 50 | return __MAX_BPF_ATTACH_TYPE; |
| 51 | } |
| 52 | |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 53 | static int show_bpf_prog(int id, const char *attach_type_str, |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 54 | const char *attach_flags_str) |
| 55 | { |
| 56 | struct bpf_prog_info info = {}; |
| 57 | __u32 info_len = sizeof(info); |
| 58 | int prog_fd; |
| 59 | |
| 60 | prog_fd = bpf_prog_get_fd_by_id(id); |
| 61 | if (prog_fd < 0) |
| 62 | return -1; |
| 63 | |
| 64 | if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len)) { |
| 65 | close(prog_fd); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | if (json_output) { |
| 70 | jsonw_start_object(json_wtr); |
| 71 | jsonw_uint_field(json_wtr, "id", info.id); |
| 72 | jsonw_string_field(json_wtr, "attach_type", |
| 73 | attach_type_str); |
| 74 | jsonw_string_field(json_wtr, "attach_flags", |
| 75 | attach_flags_str); |
| 76 | jsonw_string_field(json_wtr, "name", info.name); |
| 77 | jsonw_end_object(json_wtr); |
| 78 | } else { |
| 79 | printf("%-8u %-15s %-15s %-15s\n", info.id, |
| 80 | attach_type_str, |
| 81 | attach_flags_str, |
| 82 | info.name); |
| 83 | } |
| 84 | |
| 85 | close(prog_fd); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 89 | static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type) |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 90 | { |
| 91 | __u32 prog_ids[1024] = {0}; |
| 92 | char *attach_flags_str; |
| 93 | __u32 prog_cnt, iter; |
| 94 | __u32 attach_flags; |
| 95 | char buf[32]; |
| 96 | int ret; |
| 97 | |
| 98 | prog_cnt = ARRAY_SIZE(prog_ids); |
| 99 | ret = bpf_prog_query(cgroup_fd, type, 0, &attach_flags, prog_ids, |
| 100 | &prog_cnt); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | |
| 104 | if (prog_cnt == 0) |
| 105 | return 0; |
| 106 | |
| 107 | switch (attach_flags) { |
| 108 | case BPF_F_ALLOW_MULTI: |
| 109 | attach_flags_str = "multi"; |
| 110 | break; |
| 111 | case BPF_F_ALLOW_OVERRIDE: |
| 112 | attach_flags_str = "override"; |
| 113 | break; |
| 114 | case 0: |
| 115 | attach_flags_str = ""; |
| 116 | break; |
| 117 | default: |
| 118 | snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags); |
| 119 | attach_flags_str = buf; |
| 120 | } |
| 121 | |
| 122 | for (iter = 0; iter < prog_cnt; iter++) |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 123 | show_bpf_prog(prog_ids[iter], attach_type_strings[type], |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 124 | attach_flags_str); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 129 | static int do_show(int argc, char **argv) |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 130 | { |
| 131 | enum bpf_attach_type type; |
| 132 | int cgroup_fd; |
| 133 | int ret = -1; |
| 134 | |
| 135 | if (argc < 1) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 136 | p_err("too few parameters for cgroup show"); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 137 | goto exit; |
| 138 | } else if (argc > 1) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 139 | p_err("too many parameters for cgroup show"); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 140 | goto exit; |
| 141 | } |
| 142 | |
| 143 | cgroup_fd = open(argv[0], O_RDONLY); |
| 144 | if (cgroup_fd < 0) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 145 | p_err("can't open cgroup %s", argv[1]); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 146 | goto exit; |
| 147 | } |
| 148 | |
| 149 | if (json_output) |
| 150 | jsonw_start_array(json_wtr); |
| 151 | else |
| 152 | printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType", |
| 153 | "AttachFlags", "Name"); |
| 154 | |
| 155 | for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) { |
| 156 | /* |
| 157 | * Not all attach types may be supported, so it's expected, |
| 158 | * that some requests will fail. |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 159 | * If we were able to get the show for at least one |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 160 | * attach type, let's return 0. |
| 161 | */ |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 162 | if (show_attached_bpf_progs(cgroup_fd, type) == 0) |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 163 | ret = 0; |
| 164 | } |
| 165 | |
| 166 | if (json_output) |
| 167 | jsonw_end_array(json_wtr); |
| 168 | |
| 169 | close(cgroup_fd); |
| 170 | exit: |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static int do_attach(int argc, char **argv) |
| 175 | { |
| 176 | enum bpf_attach_type attach_type; |
| 177 | int cgroup_fd, prog_fd; |
| 178 | int attach_flags = 0; |
| 179 | int ret = -1; |
| 180 | int i; |
| 181 | |
| 182 | if (argc < 4) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 183 | p_err("too few parameters for cgroup attach"); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | cgroup_fd = open(argv[0], O_RDONLY); |
| 188 | if (cgroup_fd < 0) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 189 | p_err("can't open cgroup %s", argv[1]); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 190 | goto exit; |
| 191 | } |
| 192 | |
| 193 | attach_type = parse_attach_type(argv[1]); |
| 194 | if (attach_type == __MAX_BPF_ATTACH_TYPE) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 195 | p_err("invalid attach type"); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 196 | goto exit_cgroup; |
| 197 | } |
| 198 | |
| 199 | argc -= 2; |
| 200 | argv = &argv[2]; |
| 201 | prog_fd = prog_parse_fd(&argc, &argv); |
| 202 | if (prog_fd < 0) |
| 203 | goto exit_cgroup; |
| 204 | |
| 205 | for (i = 0; i < argc; i++) { |
| 206 | if (is_prefix(argv[i], "multi")) { |
| 207 | attach_flags |= BPF_F_ALLOW_MULTI; |
| 208 | } else if (is_prefix(argv[i], "override")) { |
| 209 | attach_flags |= BPF_F_ALLOW_OVERRIDE; |
| 210 | } else { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 211 | p_err("unknown option: %s", argv[i]); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 212 | goto exit_cgroup; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (bpf_prog_attach(prog_fd, cgroup_fd, attach_type, attach_flags)) { |
| 217 | p_err("failed to attach program"); |
| 218 | goto exit_prog; |
| 219 | } |
| 220 | |
| 221 | if (json_output) |
| 222 | jsonw_null(json_wtr); |
| 223 | |
| 224 | ret = 0; |
| 225 | |
| 226 | exit_prog: |
| 227 | close(prog_fd); |
| 228 | exit_cgroup: |
| 229 | close(cgroup_fd); |
| 230 | exit: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int do_detach(int argc, char **argv) |
| 235 | { |
| 236 | enum bpf_attach_type attach_type; |
| 237 | int prog_fd, cgroup_fd; |
| 238 | int ret = -1; |
| 239 | |
| 240 | if (argc < 4) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 241 | p_err("too few parameters for cgroup detach"); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 242 | goto exit; |
| 243 | } |
| 244 | |
| 245 | cgroup_fd = open(argv[0], O_RDONLY); |
| 246 | if (cgroup_fd < 0) { |
Jakub Kicinski | b4fac96 | 2018-01-02 14:48:37 -0800 | [diff] [blame] | 247 | p_err("can't open cgroup %s", argv[1]); |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 248 | goto exit; |
| 249 | } |
| 250 | |
| 251 | attach_type = parse_attach_type(argv[1]); |
| 252 | if (attach_type == __MAX_BPF_ATTACH_TYPE) { |
| 253 | p_err("invalid attach type"); |
| 254 | goto exit_cgroup; |
| 255 | } |
| 256 | |
| 257 | argc -= 2; |
| 258 | argv = &argv[2]; |
| 259 | prog_fd = prog_parse_fd(&argc, &argv); |
| 260 | if (prog_fd < 0) |
| 261 | goto exit_cgroup; |
| 262 | |
| 263 | if (bpf_prog_detach2(prog_fd, cgroup_fd, attach_type)) { |
| 264 | p_err("failed to detach program"); |
| 265 | goto exit_prog; |
| 266 | } |
| 267 | |
| 268 | if (json_output) |
| 269 | jsonw_null(json_wtr); |
| 270 | |
| 271 | ret = 0; |
| 272 | |
| 273 | exit_prog: |
| 274 | close(prog_fd); |
| 275 | exit_cgroup: |
| 276 | close(cgroup_fd); |
| 277 | exit: |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int do_help(int argc, char **argv) |
| 282 | { |
| 283 | if (json_output) { |
| 284 | jsonw_null(json_wtr); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | fprintf(stderr, |
Jakub Kicinski | 6ebe6db | 2018-01-02 14:48:36 -0800 | [diff] [blame] | 289 | "Usage: %s %s { show | list } CGROUP\n" |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 290 | " %s %s attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]\n" |
| 291 | " %s %s detach CGROUP ATTACH_TYPE PROG\n" |
| 292 | " %s %s help\n" |
| 293 | "\n" |
Andrey Ignatov | 393de51 | 2018-04-17 10:28:44 -0700 | [diff] [blame^] | 294 | HELP_SPEC_ATTACH_TYPES "\n" |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 295 | " " HELP_SPEC_ATTACH_FLAGS "\n" |
| 296 | " " HELP_SPEC_PROGRAM "\n" |
| 297 | " " HELP_SPEC_OPTIONS "\n" |
| 298 | "", |
| 299 | bin_name, argv[-2], bin_name, argv[-2], |
| 300 | bin_name, argv[-2], bin_name, argv[-2]); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static const struct cmd cmds[] = { |
Jakub Kicinski | 6ebe6db | 2018-01-02 14:48:36 -0800 | [diff] [blame] | 306 | { "show", do_show }, |
Jakub Kicinski | 65b875b | 2018-01-02 14:48:35 -0800 | [diff] [blame] | 307 | { "list", do_show }, |
Roman Gushchin | 5ccda64 | 2017-12-13 15:18:54 +0000 | [diff] [blame] | 308 | { "attach", do_attach }, |
| 309 | { "detach", do_detach }, |
| 310 | { "help", do_help }, |
| 311 | { 0 } |
| 312 | }; |
| 313 | |
| 314 | int do_cgroup(int argc, char **argv) |
| 315 | { |
| 316 | return cmd_select(cmds, argc, argv, do_help); |
| 317 | } |