1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020, Tessares SA. */ 3 /* Copyright (c) 2022, SUSE. */ 4 5 #include <linux/const.h> 6 #include <netinet/in.h> 7 #include <test_progs.h> 8 #include <unistd.h> 9 #include "cgroup_helpers.h" 10 #include "network_helpers.h" 11 #include "mptcp_sock.skel.h" 12 #include "mptcpify.skel.h" 13 #include "mptcp_subflow.skel.h" 14 15 #define NS_TEST "mptcp_ns" 16 #define ADDR_1 "10.0.1.1" 17 #define ADDR_2 "10.0.1.2" 18 #define PORT_1 10001 19 20 #ifndef IPPROTO_MPTCP 21 #define IPPROTO_MPTCP 262 22 #endif 23 24 #ifndef SOL_MPTCP 25 #define SOL_MPTCP 284 26 #endif 27 #ifndef MPTCP_INFO 28 #define MPTCP_INFO 1 29 #endif 30 #ifndef MPTCP_INFO_FLAG_FALLBACK 31 #define MPTCP_INFO_FLAG_FALLBACK _BITUL(0) 32 #endif 33 #ifndef MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED 34 #define MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED _BITUL(1) 35 #endif 36 37 #ifndef TCP_CA_NAME_MAX 38 #define TCP_CA_NAME_MAX 16 39 #endif 40 41 struct __mptcp_info { 42 __u8 mptcpi_subflows; 43 __u8 mptcpi_add_addr_signal; 44 __u8 mptcpi_add_addr_accepted; 45 __u8 mptcpi_subflows_max; 46 __u8 mptcpi_add_addr_signal_max; 47 __u8 mptcpi_add_addr_accepted_max; 48 __u32 mptcpi_flags; 49 __u32 mptcpi_token; 50 __u64 mptcpi_write_seq; 51 __u64 mptcpi_snd_una; 52 __u64 mptcpi_rcv_nxt; 53 __u8 mptcpi_local_addr_used; 54 __u8 mptcpi_local_addr_max; 55 __u8 mptcpi_csum_enabled; 56 __u32 mptcpi_retransmits; 57 __u64 mptcpi_bytes_retrans; 58 __u64 mptcpi_bytes_sent; 59 __u64 mptcpi_bytes_received; 60 __u64 mptcpi_bytes_acked; 61 }; 62 63 struct mptcp_storage { 64 __u32 invoked; 65 __u32 is_mptcp; 66 struct sock *sk; 67 __u32 token; 68 struct sock *first; 69 char ca_name[TCP_CA_NAME_MAX]; 70 }; 71 72 static int start_mptcp_server(int family, const char *addr_str, __u16 port, 73 int timeout_ms) 74 { 75 struct network_helper_opts opts = { 76 .timeout_ms = timeout_ms, 77 .proto = IPPROTO_MPTCP, 78 }; 79 80 return start_server_str(family, SOCK_STREAM, addr_str, port, &opts); 81 } 82 83 static int verify_tsk(int map_fd, int client_fd) 84 { 85 int err, cfd = client_fd; 86 struct mptcp_storage val; 87 88 err = bpf_map_lookup_elem(map_fd, &cfd, &val); 89 if (!ASSERT_OK(err, "bpf_map_lookup_elem")) 90 return err; 91 92 if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count")) 93 err++; 94 95 if (!ASSERT_EQ(val.is_mptcp, 0, "unexpected is_mptcp")) 96 err++; 97 98 return err; 99 } 100 101 static void get_msk_ca_name(char ca_name[]) 102 { 103 size_t len; 104 int fd; 105 106 fd = open("/proc/sys/net/ipv4/tcp_congestion_control", O_RDONLY); 107 if (!ASSERT_GE(fd, 0, "failed to open tcp_congestion_control")) 108 return; 109 110 len = read(fd, ca_name, TCP_CA_NAME_MAX); 111 if (!ASSERT_GT(len, 0, "failed to read ca_name")) 112 goto err; 113 114 if (len > 0 && ca_name[len - 1] == '\n') 115 ca_name[len - 1] = '\0'; 116 117 err: 118 close(fd); 119 } 120 121 static int verify_msk(int map_fd, int client_fd, __u32 token) 122 { 123 char ca_name[TCP_CA_NAME_MAX]; 124 int err, cfd = client_fd; 125 struct mptcp_storage val; 126 127 if (!ASSERT_GT(token, 0, "invalid token")) 128 return -1; 129 130 get_msk_ca_name(ca_name); 131 132 err = bpf_map_lookup_elem(map_fd, &cfd, &val); 133 if (!ASSERT_OK(err, "bpf_map_lookup_elem")) 134 return err; 135 136 if (!ASSERT_EQ(val.invoked, 1, "unexpected invoked count")) 137 err++; 138 139 if (!ASSERT_EQ(val.is_mptcp, 1, "unexpected is_mptcp")) 140 err++; 141 142 if (!ASSERT_EQ(val.token, token, "unexpected token")) 143 err++; 144 145 if (!ASSERT_EQ(val.first, val.sk, "unexpected first")) 146 err++; 147 148 if (!ASSERT_STRNEQ(val.ca_name, ca_name, TCP_CA_NAME_MAX, "unexpected ca_name")) 149 err++; 150 151 return err; 152 } 153 154 static int run_test(int cgroup_fd, int server_fd, bool is_mptcp) 155 { 156 int client_fd, prog_fd, map_fd, err; 157 struct mptcp_sock *sock_skel; 158 159 sock_skel = mptcp_sock__open_and_load(); 160 if (!ASSERT_OK_PTR(sock_skel, "skel_open_load")) 161 return libbpf_get_error(sock_skel); 162 163 err = mptcp_sock__attach(sock_skel); 164 if (!ASSERT_OK(err, "skel_attach")) 165 goto out; 166 167 prog_fd = bpf_program__fd(sock_skel->progs._sockops); 168 map_fd = bpf_map__fd(sock_skel->maps.socket_storage_map); 169 err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0); 170 if (!ASSERT_OK(err, "bpf_prog_attach")) 171 goto out; 172 173 client_fd = connect_to_fd(server_fd, 0); 174 if (!ASSERT_GE(client_fd, 0, "connect to fd")) { 175 err = -EIO; 176 goto out; 177 } 178 179 err += is_mptcp ? verify_msk(map_fd, client_fd, sock_skel->bss->token) : 180 verify_tsk(map_fd, client_fd); 181 182 close(client_fd); 183 184 out: 185 mptcp_sock__destroy(sock_skel); 186 return err; 187 } 188 189 static void test_base(void) 190 { 191 struct netns_obj *netns = NULL; 192 int server_fd, cgroup_fd; 193 194 cgroup_fd = test__join_cgroup("/mptcp"); 195 if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) 196 return; 197 198 netns = netns_new(NS_TEST, true); 199 if (!ASSERT_OK_PTR(netns, "netns_new")) 200 goto fail; 201 202 /* without MPTCP */ 203 server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0); 204 if (!ASSERT_GE(server_fd, 0, "start_server")) 205 goto with_mptcp; 206 207 ASSERT_OK(run_test(cgroup_fd, server_fd, false), "run_test tcp"); 208 209 close(server_fd); 210 211 with_mptcp: 212 /* with MPTCP */ 213 server_fd = start_mptcp_server(AF_INET, NULL, 0, 0); 214 if (!ASSERT_GE(server_fd, 0, "start_mptcp_server")) 215 goto fail; 216 217 ASSERT_OK(run_test(cgroup_fd, server_fd, true), "run_test mptcp"); 218 219 close(server_fd); 220 221 fail: 222 netns_free(netns); 223 close(cgroup_fd); 224 } 225 226 static void send_byte(int fd) 227 { 228 char b = 0x55; 229 230 ASSERT_EQ(write(fd, &b, sizeof(b)), 1, "send single byte"); 231 } 232 233 static int verify_mptcpify(int server_fd, int client_fd) 234 { 235 struct __mptcp_info info; 236 socklen_t optlen; 237 int protocol; 238 int err = 0; 239 240 optlen = sizeof(protocol); 241 if (!ASSERT_OK(getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen), 242 "getsockopt(SOL_PROTOCOL)")) 243 return -1; 244 245 if (!ASSERT_EQ(protocol, IPPROTO_MPTCP, "protocol isn't MPTCP")) 246 err++; 247 248 optlen = sizeof(info); 249 if (!ASSERT_OK(getsockopt(client_fd, SOL_MPTCP, MPTCP_INFO, &info, &optlen), 250 "getsockopt(MPTCP_INFO)")) 251 return -1; 252 253 if (!ASSERT_GE(info.mptcpi_flags, 0, "unexpected mptcpi_flags")) 254 err++; 255 if (!ASSERT_FALSE(info.mptcpi_flags & MPTCP_INFO_FLAG_FALLBACK, 256 "MPTCP fallback")) 257 err++; 258 if (!ASSERT_TRUE(info.mptcpi_flags & MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED, 259 "no remote key received")) 260 err++; 261 262 return err; 263 } 264 265 static int run_mptcpify(int cgroup_fd) 266 { 267 int server_fd, client_fd, err = 0; 268 struct mptcpify *mptcpify_skel; 269 270 mptcpify_skel = mptcpify__open_and_load(); 271 if (!ASSERT_OK_PTR(mptcpify_skel, "skel_open_load")) 272 return libbpf_get_error(mptcpify_skel); 273 274 mptcpify_skel->bss->pid = getpid(); 275 276 err = mptcpify__attach(mptcpify_skel); 277 if (!ASSERT_OK(err, "skel_attach")) 278 goto out; 279 280 /* without MPTCP */ 281 server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0); 282 if (!ASSERT_GE(server_fd, 0, "start_server")) { 283 err = -EIO; 284 goto out; 285 } 286 287 client_fd = connect_to_fd(server_fd, 0); 288 if (!ASSERT_GE(client_fd, 0, "connect to fd")) { 289 err = -EIO; 290 goto close_server; 291 } 292 293 send_byte(client_fd); 294 295 err = verify_mptcpify(server_fd, client_fd); 296 297 close(client_fd); 298 close_server: 299 close(server_fd); 300 out: 301 mptcpify__destroy(mptcpify_skel); 302 return err; 303 } 304 305 static void test_mptcpify(void) 306 { 307 struct netns_obj *netns = NULL; 308 int cgroup_fd; 309 310 cgroup_fd = test__join_cgroup("/mptcpify"); 311 if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup")) 312 return; 313 314 netns = netns_new(NS_TEST, true); 315 if (!ASSERT_OK_PTR(netns, "netns_new")) 316 goto fail; 317 318 ASSERT_OK(run_mptcpify(cgroup_fd), "run_mptcpify"); 319 320 fail: 321 netns_free(netns); 322 close(cgroup_fd); 323 } 324 325 static int endpoint_init(char *flags) 326 { 327 SYS(fail, "ip -net %s link add veth1 type veth peer name veth2", NS_TEST); 328 SYS(fail, "ip -net %s addr add %s/24 dev veth1", NS_TEST, ADDR_1); 329 SYS(fail, "ip -net %s link set dev veth1 up", NS_TEST); 330 SYS(fail, "ip -net %s addr add %s/24 dev veth2", NS_TEST, ADDR_2); 331 SYS(fail, "ip -net %s link set dev veth2 up", NS_TEST); 332 if (SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", NS_TEST, ADDR_2, flags)) { 333 printf("'ip mptcp' not supported, skip this test.\n"); 334 test__skip(); 335 goto fail; 336 } 337 338 return 0; 339 fail: 340 return -1; 341 } 342 343 static void wait_for_new_subflows(int fd) 344 { 345 socklen_t len; 346 u8 subflows; 347 int err, i; 348 349 len = sizeof(subflows); 350 /* Wait max 5 sec for new subflows to be created */ 351 for (i = 0; i < 50; i++) { 352 err = getsockopt(fd, SOL_MPTCP, MPTCP_INFO, &subflows, &len); 353 if (!err && subflows > 0) 354 break; 355 356 usleep(100000); /* 0.1s */ 357 } 358 } 359 360 static void run_subflow(void) 361 { 362 int server_fd, client_fd, err; 363 char new[TCP_CA_NAME_MAX]; 364 char cc[TCP_CA_NAME_MAX]; 365 unsigned int mark; 366 socklen_t len; 367 368 server_fd = start_mptcp_server(AF_INET, ADDR_1, PORT_1, 0); 369 if (!ASSERT_OK_FD(server_fd, "start_mptcp_server")) 370 return; 371 372 client_fd = connect_to_fd(server_fd, 0); 373 if (!ASSERT_OK_FD(client_fd, "connect_to_fd")) 374 goto close_server; 375 376 send_byte(client_fd); 377 wait_for_new_subflows(client_fd); 378 379 len = sizeof(mark); 380 err = getsockopt(client_fd, SOL_SOCKET, SO_MARK, &mark, &len); 381 if (ASSERT_OK(err, "getsockopt(client_fd, SO_MARK)")) 382 ASSERT_EQ(mark, 0, "mark"); 383 384 len = sizeof(new); 385 err = getsockopt(client_fd, SOL_TCP, TCP_CONGESTION, new, &len); 386 if (ASSERT_OK(err, "getsockopt(client_fd, TCP_CONGESTION)")) { 387 get_msk_ca_name(cc); 388 ASSERT_STREQ(new, cc, "cc"); 389 } 390 391 close(client_fd); 392 close_server: 393 close(server_fd); 394 } 395 396 static void test_subflow(void) 397 { 398 struct mptcp_subflow *skel; 399 struct netns_obj *netns; 400 int cgroup_fd; 401 402 cgroup_fd = test__join_cgroup("/mptcp_subflow"); 403 if (!ASSERT_OK_FD(cgroup_fd, "join_cgroup: mptcp_subflow")) 404 return; 405 406 skel = mptcp_subflow__open_and_load(); 407 if (!ASSERT_OK_PTR(skel, "skel_open_load: mptcp_subflow")) 408 goto close_cgroup; 409 410 skel->bss->pid = getpid(); 411 412 skel->links.mptcp_subflow = 413 bpf_program__attach_cgroup(skel->progs.mptcp_subflow, cgroup_fd); 414 if (!ASSERT_OK_PTR(skel->links.mptcp_subflow, "attach mptcp_subflow")) 415 goto skel_destroy; 416 417 skel->links._getsockopt_subflow = 418 bpf_program__attach_cgroup(skel->progs._getsockopt_subflow, cgroup_fd); 419 if (!ASSERT_OK_PTR(skel->links._getsockopt_subflow, "attach _getsockopt_subflow")) 420 goto skel_destroy; 421 422 netns = netns_new(NS_TEST, true); 423 if (!ASSERT_OK_PTR(netns, "netns_new: mptcp_subflow")) 424 goto skel_destroy; 425 426 if (endpoint_init("subflow") < 0) 427 goto close_netns; 428 429 run_subflow(); 430 431 close_netns: 432 netns_free(netns); 433 skel_destroy: 434 mptcp_subflow__destroy(skel); 435 close_cgroup: 436 close(cgroup_fd); 437 } 438 439 void test_mptcp(void) 440 { 441 if (test__start_subtest("base")) 442 test_base(); 443 if (test__start_subtest("mptcpify")) 444 test_mptcpify(); 445 if (test__start_subtest("subflow")) 446 test_subflow(); 447 } 448