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

selftests/bpf: Verify token of struct mptcp_sock

This patch verifies the struct member token of struct mptcp_sock. Add a
new member token in struct mptcp_storage to store the token value of the
msk socket got by bpf_skc_to_mptcp_sock(). Trace the kernel function
mptcp_pm_new_connection() by using bpf fentry prog to obtain the msk token
and save it in a global bpf variable. Pass the variable to verify_msk() to
verify it with the token saved in socket_storage_map.

v4:
- use ASSERT_* instead of CHECK_FAIL (Andrii)
- skip the test if 'ip mptcp monitor' is not supported (Mat)

v5:
- Drop 'ip mptcp monitor', trace mptcp_pm_new_connection instead (Martin)
- Use ASSERT_EQ (Andrii)

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
Signed-off-by: Mat Martineau <mathew.j.martineau@linux.intel.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Matthieu Baerts <matthieu.baerts@tessares.net>
Link: https://lore.kernel.org/bpf/20220519233016.105670-6-mathew.j.martineau@linux.intel.com

authored by

Geliang Tang and committed by
Andrii Nakryiko
02662234 3bc48b56

+31 -2
+2
tools/testing/selftests/bpf/bpf_tcp_helpers.h
··· 228 228 229 229 struct mptcp_sock { 230 230 struct inet_connection_sock sk; 231 + 232 + __u32 token; 231 233 } __attribute__((preserve_access_index)); 232 234 233 235 #endif
+13 -2
tools/testing/selftests/bpf/prog_tests/mptcp.c
··· 10 10 struct mptcp_storage { 11 11 __u32 invoked; 12 12 __u32 is_mptcp; 13 + __u32 token; 13 14 }; 14 15 15 16 static int verify_tsk(int map_fd, int client_fd) ··· 31 30 return err; 32 31 } 33 32 34 - static int verify_msk(int map_fd, int client_fd) 33 + static int verify_msk(int map_fd, int client_fd, __u32 token) 35 34 { 36 35 int err, cfd = client_fd; 37 36 struct mptcp_storage val; 37 + 38 + if (!ASSERT_GT(token, 0, "invalid token")) 39 + return -1; 38 40 39 41 err = bpf_map_lookup_elem(map_fd, &cfd, &val); 40 42 if (!ASSERT_OK(err, "bpf_map_lookup_elem")) ··· 47 43 err++; 48 44 49 45 if (!ASSERT_EQ(val.is_mptcp, 1, "unexpected is_mptcp")) 46 + err++; 47 + 48 + if (!ASSERT_EQ(val.token, token, "unexpected token")) 50 49 err++; 51 50 52 51 return err; ··· 63 56 sock_skel = mptcp_sock__open_and_load(); 64 57 if (!ASSERT_OK_PTR(sock_skel, "skel_open_load")) 65 58 return -EIO; 59 + 60 + err = mptcp_sock__attach(sock_skel); 61 + if (!ASSERT_OK(err, "skel_attach")) 62 + goto out; 66 63 67 64 prog_fd = bpf_program__fd(sock_skel->progs._sockops); 68 65 if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd")) { ··· 90 79 goto out; 91 80 } 92 81 93 - err += is_mptcp ? verify_msk(map_fd, client_fd) : 82 + err += is_mptcp ? verify_msk(map_fd, client_fd, sock_skel->bss->token) : 94 83 verify_tsk(map_fd, client_fd); 95 84 96 85 close(client_fd);
+16
tools/testing/selftests/bpf/progs/mptcp_sock.c
··· 7 7 #include "bpf_tcp_helpers.h" 8 8 9 9 char _license[] SEC("license") = "GPL"; 10 + __u32 token = 0; 10 11 11 12 struct mptcp_storage { 12 13 __u32 invoked; 13 14 __u32 is_mptcp; 15 + __u32 token; 14 16 }; 15 17 16 18 struct { ··· 49 47 BPF_SK_STORAGE_GET_F_CREATE); 50 48 if (!storage) 51 49 return 1; 50 + 51 + storage->token = 0; 52 52 } else { 53 53 msk = bpf_skc_to_mptcp_sock(sk); 54 54 if (!msk) ··· 60 56 BPF_SK_STORAGE_GET_F_CREATE); 61 57 if (!storage) 62 58 return 1; 59 + 60 + storage->token = msk->token; 63 61 } 64 62 storage->invoked++; 65 63 storage->is_mptcp = is_mptcp; 66 64 67 65 return 1; 66 + } 67 + 68 + SEC("fentry/mptcp_pm_new_connection") 69 + int BPF_PROG(trace_mptcp_pm_new_connection, struct mptcp_sock *msk, 70 + const struct sock *ssk, int server_side) 71 + { 72 + if (!server_side) 73 + token = msk->token; 74 + 75 + return 0; 68 76 }