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

selftests/bpf: Verify ca_name of struct mptcp_sock

This patch verifies another member of struct mptcp_sock, ca_name. Add a
new function get_msk_ca_name() to read the sysctl tcp_congestion_control
and verify it in verify_msk().

v3: Access the sysctl through the filesystem to avoid compatibility
issues with the busybox sysctl command.

v4: use ASSERT_* instead of CHECK_FAIL (Andrii)

v5: use ASSERT_STRNEQ() instead of strncmp() (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-7-mathew.j.martineau@linux.intel.com

authored by

Geliang Tang and committed by
Andrii Nakryiko
ccc090f4 02662234

+39
+5
tools/testing/selftests/bpf/bpf_tcp_helpers.h
··· 16 16 #define SOL_TCP 6 17 17 #endif 18 18 19 + #ifndef TCP_CA_NAME_MAX 20 + #define TCP_CA_NAME_MAX 16 21 + #endif 22 + 19 23 #define tcp_jiffies32 ((__u32)bpf_jiffies64()) 20 24 21 25 struct sock_common { ··· 234 230 struct inet_connection_sock sk; 235 231 236 232 __u32 token; 233 + char ca_name[TCP_CA_NAME_MAX]; 237 234 } __attribute__((preserve_access_index)); 238 235 239 236 #endif
+31
tools/testing/selftests/bpf/prog_tests/mptcp.c
··· 7 7 #include "network_helpers.h" 8 8 #include "mptcp_sock.skel.h" 9 9 10 + #ifndef TCP_CA_NAME_MAX 11 + #define TCP_CA_NAME_MAX 16 12 + #endif 13 + 10 14 struct mptcp_storage { 11 15 __u32 invoked; 12 16 __u32 is_mptcp; 13 17 __u32 token; 18 + char ca_name[TCP_CA_NAME_MAX]; 14 19 }; 15 20 16 21 static int verify_tsk(int map_fd, int client_fd) ··· 36 31 return err; 37 32 } 38 33 34 + static void get_msk_ca_name(char ca_name[]) 35 + { 36 + size_t len; 37 + int fd; 38 + 39 + fd = open("/proc/sys/net/ipv4/tcp_congestion_control", O_RDONLY); 40 + if (!ASSERT_GE(fd, 0, "failed to open tcp_congestion_control")) 41 + return; 42 + 43 + len = read(fd, ca_name, TCP_CA_NAME_MAX); 44 + if (!ASSERT_GT(len, 0, "failed to read ca_name")) 45 + goto err; 46 + 47 + if (len > 0 && ca_name[len - 1] == '\n') 48 + ca_name[len - 1] = '\0'; 49 + 50 + err: 51 + close(fd); 52 + } 53 + 39 54 static int verify_msk(int map_fd, int client_fd, __u32 token) 40 55 { 56 + char ca_name[TCP_CA_NAME_MAX]; 41 57 int err, cfd = client_fd; 42 58 struct mptcp_storage val; 43 59 44 60 if (!ASSERT_GT(token, 0, "invalid token")) 45 61 return -1; 62 + 63 + get_msk_ca_name(ca_name); 46 64 47 65 err = bpf_map_lookup_elem(map_fd, &cfd, &val); 48 66 if (!ASSERT_OK(err, "bpf_map_lookup_elem")) ··· 78 50 err++; 79 51 80 52 if (!ASSERT_EQ(val.token, token, "unexpected token")) 53 + err++; 54 + 55 + if (!ASSERT_STRNEQ(val.ca_name, ca_name, TCP_CA_NAME_MAX, "unexpected ca_name")) 81 56 err++; 82 57 83 58 return err;
+3
tools/testing/selftests/bpf/progs/mptcp_sock.c
··· 13 13 __u32 invoked; 14 14 __u32 is_mptcp; 15 15 __u32 token; 16 + char ca_name[TCP_CA_NAME_MAX]; 16 17 }; 17 18 18 19 struct { ··· 52 51 return 1; 53 52 54 53 storage->token = 0; 54 + __builtin_memset(storage->ca_name, 0, TCP_CA_NAME_MAX); 55 55 } else { 56 56 msk = bpf_skc_to_mptcp_sock(sk); 57 57 if (!msk) ··· 64 62 return 1; 65 63 66 64 storage->token = msk->token; 65 + __builtin_memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX); 67 66 } 68 67 storage->invoked++; 69 68 storage->is_mptcp = is_mptcp;