Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: selftest: Use bpf_skc_to_tcp_sock() in the sock_fields test

This test uses bpf_skc_to_tcp_sock() to get a kernel tcp_sock ptr "ktp".
Access the ktp->lsndtime and also pass ktp to bpf_sk_storage_get().

It also exercises the bpf_sk_cgroup_id() and bpf_sk_ancestor_cgroup_id()
with the "ktp". To do that, a parent cgroup and a child cgroup are
created. The bpf prog is attached to the child cgroup.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200925000446.3858975-1-kafai@fb.com

authored by

Martin KaFai Lau and committed by
Alexei Starovoitov
edc2d66a c40a565a

+55 -9
+33 -7
tools/testing/selftests/bpf/prog_tests/sock_fields.c
··· 29 29 __u32 cnt; 30 30 }; 31 31 32 - #define TEST_CGROUP "/test-bpf-sock-fields" 32 + #define PARENT_CGROUP "/test-bpf-sock-fields" 33 + #define CHILD_CGROUP "/test-bpf-sock-fields/child" 33 34 #define DATA "Hello BPF!" 34 35 #define DATA_LEN sizeof(DATA) 35 36 ··· 38 37 static int sk_pkt_out_cnt10_fd; 39 38 struct test_sock_fields *skel; 40 39 static int sk_pkt_out_cnt_fd; 40 + static __u64 parent_cg_id; 41 + static __u64 child_cg_id; 41 42 static int linum_map_fd; 42 43 static __u32 duration; 43 44 ··· 145 142 "srv_sk", "Unexpected. Check srv_sk output. egress_linum:%u\n", 146 143 egress_linum); 147 144 145 + CHECK(!skel->bss->lsndtime, "srv_tp", "Unexpected lsndtime:0\n"); 146 + 148 147 CHECK(cli_sk.state == 10 || 149 148 !cli_sk.state || 150 149 cli_sk.family != AF_INET6 || ··· 183 178 cli_tp.bytes_received < 2 * DATA_LEN, 184 179 "cli_tp", "Unexpected. Check cli_tp output. egress_linum:%u\n", 185 180 egress_linum); 181 + 182 + CHECK(skel->bss->parent_cg_id != parent_cg_id, 183 + "parent_cg_id", "%zu != %zu\n", 184 + (size_t)skel->bss->parent_cg_id, (size_t)parent_cg_id); 185 + 186 + CHECK(skel->bss->child_cg_id != child_cg_id, 187 + "child_cg_id", "%zu != %zu\n", 188 + (size_t)skel->bss->child_cg_id, (size_t)child_cg_id); 186 189 } 187 190 188 191 static void check_sk_pkt_out_cnt(int accept_fd, int cli_fd) ··· 332 319 void test_sock_fields(void) 333 320 { 334 321 struct bpf_link *egress_link = NULL, *ingress_link = NULL; 335 - int cgroup_fd; 322 + int parent_cg_fd = -1, child_cg_fd = -1; 336 323 337 324 /* Create a cgroup, get fd, and join it */ 338 - cgroup_fd = test__join_cgroup(TEST_CGROUP); 339 - if (CHECK_FAIL(cgroup_fd < 0)) 325 + parent_cg_fd = test__join_cgroup(PARENT_CGROUP); 326 + if (CHECK_FAIL(parent_cg_fd < 0)) 340 327 return; 328 + parent_cg_id = get_cgroup_id(PARENT_CGROUP); 329 + if (CHECK_FAIL(!parent_cg_id)) 330 + goto done; 331 + 332 + child_cg_fd = test__join_cgroup(CHILD_CGROUP); 333 + if (CHECK_FAIL(child_cg_fd < 0)) 334 + goto done; 335 + child_cg_id = get_cgroup_id(CHILD_CGROUP); 336 + if (CHECK_FAIL(!child_cg_id)) 337 + goto done; 341 338 342 339 skel = test_sock_fields__open_and_load(); 343 340 if (CHECK(!skel, "test_sock_fields__open_and_load", "failed\n")) 344 341 goto done; 345 342 346 343 egress_link = bpf_program__attach_cgroup(skel->progs.egress_read_sock_fields, 347 - cgroup_fd); 344 + child_cg_fd); 348 345 if (CHECK(IS_ERR(egress_link), "attach_cgroup(egress)", "err:%ld\n", 349 346 PTR_ERR(egress_link))) 350 347 goto done; 351 348 352 349 ingress_link = bpf_program__attach_cgroup(skel->progs.ingress_read_sock_fields, 353 - cgroup_fd); 350 + child_cg_fd); 354 351 if (CHECK(IS_ERR(ingress_link), "attach_cgroup(ingress)", "err:%ld\n", 355 352 PTR_ERR(ingress_link))) 356 353 goto done; ··· 375 352 bpf_link__destroy(egress_link); 376 353 bpf_link__destroy(ingress_link); 377 354 test_sock_fields__destroy(skel); 378 - close(cgroup_fd); 355 + if (child_cg_fd != -1) 356 + close(child_cg_fd); 357 + if (parent_cg_fd != -1) 358 + close(parent_cg_fd); 379 359 }
+22 -2
tools/testing/selftests/bpf/progs/test_sock_fields.c
··· 7 7 8 8 #include <bpf/bpf_helpers.h> 9 9 #include <bpf/bpf_endian.h> 10 + #include "bpf_tcp_helpers.h" 10 11 11 12 enum bpf_linum_array_idx { 12 13 EGRESS_LINUM_IDX, ··· 48 47 struct bpf_sock listen_sk = {}; 49 48 struct bpf_sock srv_sk = {}; 50 49 struct bpf_sock cli_sk = {}; 50 + __u64 parent_cg_id = 0; 51 + __u64 child_cg_id = 0; 52 + __u64 lsndtime = 0; 51 53 52 54 static bool is_loopback6(__u32 *a6) 53 55 { ··· 125 121 struct bpf_tcp_sock *tp, *tp_ret; 126 122 struct bpf_sock *sk, *sk_ret; 127 123 __u32 linum, linum_idx; 124 + struct tcp_sock *ktp; 128 125 129 126 linum_idx = EGRESS_LINUM_IDX; 130 127 ··· 170 165 tpcpy(tp_ret, tp); 171 166 172 167 if (sk_ret == &srv_sk) { 168 + ktp = bpf_skc_to_tcp_sock(sk); 169 + 170 + if (!ktp) 171 + RET_LOG(); 172 + 173 + lsndtime = ktp->lsndtime; 174 + 175 + child_cg_id = bpf_sk_cgroup_id(ktp); 176 + if (!child_cg_id) 177 + RET_LOG(); 178 + 179 + parent_cg_id = bpf_sk_ancestor_cgroup_id(ktp, 2); 180 + if (!parent_cg_id) 181 + RET_LOG(); 182 + 173 183 /* The userspace has created it for srv sk */ 174 - pkt_out_cnt = bpf_sk_storage_get(&sk_pkt_out_cnt, sk, 0, 0); 175 - pkt_out_cnt10 = bpf_sk_storage_get(&sk_pkt_out_cnt10, sk, 184 + pkt_out_cnt = bpf_sk_storage_get(&sk_pkt_out_cnt, ktp, 0, 0); 185 + pkt_out_cnt10 = bpf_sk_storage_get(&sk_pkt_out_cnt10, ktp, 176 186 0, 0); 177 187 } else { 178 188 pkt_out_cnt = bpf_sk_storage_get(&sk_pkt_out_cnt, sk,