|
7 | 7 | #include "bpf_iter_task.skel.h"
|
8 | 8 | #include "bpf_iter_task_stack.skel.h"
|
9 | 9 | #include "bpf_iter_task_file.skel.h"
|
| 10 | +#include "bpf_iter_task_btf.skel.h" |
10 | 11 | #include "bpf_iter_tcp4.skel.h"
|
11 | 12 | #include "bpf_iter_tcp6.skel.h"
|
12 | 13 | #include "bpf_iter_udp4.skel.h"
|
@@ -167,6 +168,69 @@ static void test_task_file(void)
|
167 | 168 | bpf_iter_task_file__destroy(skel);
|
168 | 169 | }
|
169 | 170 |
|
| 171 | +#define FSBUFSZ 8192 |
| 172 | + |
| 173 | +static char fsbuf[FSBUFSZ]; |
| 174 | + |
| 175 | +static void do_btf_read(struct bpf_program *prog) |
| 176 | +{ |
| 177 | + int iter_fd = -1, len = 0, bufleft = FSBUFSZ; |
| 178 | + struct bpf_link *link; |
| 179 | + char *buf = fsbuf; |
| 180 | + |
| 181 | + link = bpf_program__attach_iter(prog, NULL); |
| 182 | + if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) |
| 183 | + return; |
| 184 | + |
| 185 | + iter_fd = bpf_iter_create(bpf_link__fd(link)); |
| 186 | + if (CHECK(iter_fd < 0, "create_iter", "create_iter failed\n")) |
| 187 | + goto free_link; |
| 188 | + |
| 189 | + do { |
| 190 | + len = read(iter_fd, buf, bufleft); |
| 191 | + if (len > 0) { |
| 192 | + buf += len; |
| 193 | + bufleft -= len; |
| 194 | + } |
| 195 | + } while (len > 0); |
| 196 | + |
| 197 | + if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) |
| 198 | + goto free_link; |
| 199 | + |
| 200 | + CHECK(strstr(fsbuf, "(struct fs_struct)") == NULL, |
| 201 | + "check for btf representation of fs_struct in iter data", |
| 202 | + "struct fs_struct not found"); |
| 203 | +free_link: |
| 204 | + if (iter_fd > 0) |
| 205 | + close(iter_fd); |
| 206 | + bpf_link__destroy(link); |
| 207 | +} |
| 208 | + |
| 209 | +static void test_task_btf(void) |
| 210 | +{ |
| 211 | + struct bpf_iter_task_btf__bss *bss; |
| 212 | + struct bpf_iter_task_btf *skel; |
| 213 | + |
| 214 | + skel = bpf_iter_task_btf__open_and_load(); |
| 215 | + if (CHECK(!skel, "bpf_iter_task_btf__open_and_load", |
| 216 | + "skeleton open_and_load failed\n")) |
| 217 | + return; |
| 218 | + |
| 219 | + bss = skel->bss; |
| 220 | + |
| 221 | + do_btf_read(skel->progs.dump_task_fs_struct); |
| 222 | + |
| 223 | + if (CHECK(bss->tasks == 0, "check if iterated over tasks", |
| 224 | + "no task iteration, did BPF program run?\n")) |
| 225 | + goto cleanup; |
| 226 | + |
| 227 | + CHECK(bss->seq_err != 0, "check for unexpected err", |
| 228 | + "bpf_seq_printf_btf returned %ld", bss->seq_err); |
| 229 | + |
| 230 | +cleanup: |
| 231 | + bpf_iter_task_btf__destroy(skel); |
| 232 | +} |
| 233 | + |
170 | 234 | static void test_tcp4(void)
|
171 | 235 | {
|
172 | 236 | struct bpf_iter_tcp4 *skel;
|
@@ -957,6 +1021,8 @@ void test_bpf_iter(void)
|
957 | 1021 | test_task_stack();
|
958 | 1022 | if (test__start_subtest("task_file"))
|
959 | 1023 | test_task_file();
|
| 1024 | + if (test__start_subtest("task_btf")) |
| 1025 | + test_task_btf(); |
960 | 1026 | if (test__start_subtest("tcp4"))
|
961 | 1027 | test_tcp4();
|
962 | 1028 | if (test__start_subtest("tcp6"))
|
|
0 commit comments