| .. | .. |
|---|
| 18 | 18 | #define CG_PATH "/foo" |
|---|
| 19 | 19 | #define SOCKET_COOKIE_PROG "./socket_cookie_prog.o" |
|---|
| 20 | 20 | |
|---|
| 21 | +struct socket_cookie { |
|---|
| 22 | + __u64 cookie_key; |
|---|
| 23 | + __u32 cookie_value; |
|---|
| 24 | +}; |
|---|
| 25 | + |
|---|
| 21 | 26 | static int start_server(void) |
|---|
| 22 | 27 | { |
|---|
| 23 | 28 | struct sockaddr_in6 addr; |
|---|
| .. | .. |
|---|
| 89 | 94 | __u32 cookie_expected_value; |
|---|
| 90 | 95 | struct sockaddr_in6 addr; |
|---|
| 91 | 96 | socklen_t len = sizeof(addr); |
|---|
| 92 | | - __u32 cookie_value; |
|---|
| 93 | | - __u64 cookie_key; |
|---|
| 97 | + struct socket_cookie val; |
|---|
| 94 | 98 | int err = 0; |
|---|
| 95 | 99 | int map_fd; |
|---|
| 96 | 100 | |
|---|
| .. | .. |
|---|
| 101 | 105 | |
|---|
| 102 | 106 | map_fd = bpf_map__fd(map); |
|---|
| 103 | 107 | |
|---|
| 104 | | - err = bpf_map_get_next_key(map_fd, NULL, &cookie_key); |
|---|
| 105 | | - if (err) { |
|---|
| 106 | | - log_err("Can't get cookie key from map"); |
|---|
| 107 | | - goto out; |
|---|
| 108 | | - } |
|---|
| 109 | | - |
|---|
| 110 | | - err = bpf_map_lookup_elem(map_fd, &cookie_key, &cookie_value); |
|---|
| 111 | | - if (err) { |
|---|
| 112 | | - log_err("Can't get cookie value from map"); |
|---|
| 113 | | - goto out; |
|---|
| 114 | | - } |
|---|
| 108 | + err = bpf_map_lookup_elem(map_fd, &client_fd, &val); |
|---|
| 115 | 109 | |
|---|
| 116 | 110 | err = getsockname(client_fd, (struct sockaddr *)&addr, &len); |
|---|
| 117 | 111 | if (err) { |
|---|
| .. | .. |
|---|
| 120 | 114 | } |
|---|
| 121 | 115 | |
|---|
| 122 | 116 | cookie_expected_value = (ntohs(addr.sin6_port) << 8) | 0xFF; |
|---|
| 123 | | - if (cookie_value != cookie_expected_value) { |
|---|
| 124 | | - log_err("Unexpected value in map: %x != %x", cookie_value, |
|---|
| 117 | + if (val.cookie_value != cookie_expected_value) { |
|---|
| 118 | + log_err("Unexpected value in map: %x != %x", val.cookie_value, |
|---|
| 125 | 119 | cookie_expected_value); |
|---|
| 126 | 120 | goto err; |
|---|
| 127 | 121 | } |
|---|
| .. | .. |
|---|
| 148 | 142 | memset(&attr, 0, sizeof(attr)); |
|---|
| 149 | 143 | attr.file = SOCKET_COOKIE_PROG; |
|---|
| 150 | 144 | attr.prog_type = BPF_PROG_TYPE_UNSPEC; |
|---|
| 145 | + attr.prog_flags = BPF_F_TEST_RND_HI32; |
|---|
| 151 | 146 | |
|---|
| 152 | 147 | err = bpf_prog_load_xattr(&attr, &pobj, &prog_fd); |
|---|
| 153 | 148 | if (err) { |
|---|
| .. | .. |
|---|
| 156 | 151 | } |
|---|
| 157 | 152 | |
|---|
| 158 | 153 | bpf_object__for_each_program(prog, pobj) { |
|---|
| 159 | | - prog_name = bpf_program__title(prog, /*needs_copy*/ false); |
|---|
| 154 | + prog_name = bpf_program__section_name(prog); |
|---|
| 160 | 155 | |
|---|
| 161 | | - if (strcmp(prog_name, "cgroup/connect6") == 0) { |
|---|
| 162 | | - attach_type = BPF_CGROUP_INET6_CONNECT; |
|---|
| 163 | | - } else if (strcmp(prog_name, "sockops") == 0) { |
|---|
| 164 | | - attach_type = BPF_CGROUP_SOCK_OPS; |
|---|
| 165 | | - } else { |
|---|
| 166 | | - log_err("Unexpected prog: %s", prog_name); |
|---|
| 156 | + if (libbpf_attach_type_by_name(prog_name, &attach_type)) |
|---|
| 167 | 157 | goto err; |
|---|
| 168 | | - } |
|---|
| 169 | 158 | |
|---|
| 170 | 159 | err = bpf_prog_attach(bpf_program__fd(prog), cgfd, attach_type, |
|---|
| 171 | 160 | BPF_F_ALLOW_OVERRIDE); |
|---|
| .. | .. |
|---|
| 202 | 191 | int cgfd = -1; |
|---|
| 203 | 192 | int err = 0; |
|---|
| 204 | 193 | |
|---|
| 205 | | - if (setup_cgroup_environment()) |
|---|
| 206 | | - goto err; |
|---|
| 207 | | - |
|---|
| 208 | | - cgfd = create_and_get_cgroup(CG_PATH); |
|---|
| 209 | | - if (!cgfd) |
|---|
| 210 | | - goto err; |
|---|
| 211 | | - |
|---|
| 212 | | - if (join_cgroup(CG_PATH)) |
|---|
| 194 | + cgfd = cgroup_setup_and_join(CG_PATH); |
|---|
| 195 | + if (cgfd < 0) |
|---|
| 213 | 196 | goto err; |
|---|
| 214 | 197 | |
|---|
| 215 | 198 | if (run_test(cgfd)) |
|---|