github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_ip_tcp_generic.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "test/syscalls/linux/socket_ip_tcp_generic.h" 16 17 #include <fcntl.h> 18 #include <netinet/in.h> 19 #include <netinet/tcp.h> 20 #include <poll.h> 21 #include <stdio.h> 22 #include <sys/ioctl.h> 23 #include <sys/socket.h> 24 #include <sys/types.h> 25 #include <sys/un.h> 26 27 #include "gtest/gtest.h" 28 #include "absl/memory/memory.h" 29 #include "absl/time/clock.h" 30 #include "absl/time/time.h" 31 #include "test/syscalls/linux/socket_test_util.h" 32 #include "test/util/temp_path.h" 33 #include "test/util/test_util.h" 34 #include "test/util/thread_util.h" 35 36 namespace gvisor { 37 namespace testing { 38 39 using ::testing::AnyOf; 40 using ::testing::Eq; 41 42 TEST_P(TCPSocketPairTest, TcpInfoSucceeds) { 43 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 44 45 struct tcp_info opt = {}; 46 socklen_t optLen = sizeof(opt); 47 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 48 SyscallSucceeds()); 49 } 50 51 TEST_P(TCPSocketPairTest, ShortTcpInfoSucceeds) { 52 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 53 54 struct tcp_info opt = {}; 55 socklen_t optLen = 1; 56 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 57 SyscallSucceeds()); 58 } 59 60 TEST_P(TCPSocketPairTest, ZeroTcpInfoSucceeds) { 61 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 62 63 struct tcp_info opt = {}; 64 socklen_t optLen = 0; 65 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 66 SyscallSucceeds()); 67 } 68 69 // Copied from include/net/tcp.h. 70 constexpr int TCP_CA_OPEN = 0; 71 72 TEST_P(TCPSocketPairTest, CheckTcpInfoFields) { 73 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 74 75 char buf[10] = {}; 76 ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), buf, sizeof(buf), 0), 77 SyscallSucceedsWithValue(sizeof(buf))); 78 79 // Wait until second_fd sees the data and then recv it. 80 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 81 constexpr int kPollTimeoutMs = 2000; // Wait up to 2 seconds for the data. 82 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 83 SyscallSucceedsWithValue(1)); 84 85 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 86 SyscallSucceedsWithValue(sizeof(buf))); 87 88 struct tcp_info opt = {}; 89 socklen_t optLen = sizeof(opt); 90 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 91 SyscallSucceeds()); 92 ASSERT_EQ(optLen, sizeof(opt)); 93 94 // Validates the received tcp_info fields. 95 EXPECT_EQ(opt.tcpi_ca_state, TCP_CA_OPEN); 96 EXPECT_GT(opt.tcpi_snd_cwnd, 0); 97 EXPECT_GT(opt.tcpi_rto, 0); 98 } 99 100 // This test validates that an RST is sent instead of a FIN when data is 101 // unread on calls to close(2). 102 TEST_P(TCPSocketPairTest, RSTSentOnCloseWithUnreadData) { 103 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 104 105 char buf[10] = {}; 106 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 107 SyscallSucceedsWithValue(sizeof(buf))); 108 109 // Wait until t_ sees the data on its side but don't read it. 110 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 111 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 112 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 113 SyscallSucceedsWithValue(1)); 114 115 // Now close the connected without reading the data. 116 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 117 118 // Wait for the other end to receive the RST (up to 20 seconds). 119 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 120 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 121 SyscallSucceedsWithValue(1)); 122 123 // A shutdown with unread data will cause a RST to be sent instead 124 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 125 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 126 SyscallFailsWithErrno(ECONNRESET)); 127 } 128 129 // This test will validate that a RST will cause POLLHUP to trigger. 130 TEST_P(TCPSocketPairTest, RSTCausesPollHUP) { 131 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 132 133 char buf[10] = {}; 134 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 135 SyscallSucceedsWithValue(sizeof(buf))); 136 137 // Wait until second sees the data on its side but don't read it. 138 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 139 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 140 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 141 SyscallSucceedsWithValue(1)); 142 EXPECT_EQ(poll_fd.revents & POLLIN, POLLIN); 143 144 // Confirm we at least have one unread byte. 145 int bytes_available = 0; 146 ASSERT_THAT( 147 RetryEINTR(ioctl)(sockets->second_fd(), FIONREAD, &bytes_available), 148 SyscallSucceeds()); 149 EXPECT_GT(bytes_available, 0); 150 151 // Now close the connected socket without reading the data from the second, 152 // this will cause a RST and we should see that with POLLHUP. 153 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 154 155 // Wait for the other end to receive the RST (up to 20 seconds). 156 struct pollfd poll_fd3 = {sockets->first_fd(), POLLHUP, 0}; 157 ASSERT_THAT(RetryEINTR(poll)(&poll_fd3, 1, kPollTimeoutMs), 158 SyscallSucceedsWithValue(1)); 159 ASSERT_NE(poll_fd3.revents & POLLHUP, 0); 160 } 161 162 // This test validates that even if a RST is sent the other end will not 163 // get an ECONNRESET until it's read all data. 164 TEST_P(TCPSocketPairTest, RSTSentOnCloseWithUnreadDataAllowsReadBuffered) { 165 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 166 167 char buf[10] = {}; 168 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 169 SyscallSucceedsWithValue(sizeof(buf))); 170 ASSERT_THAT(RetryEINTR(write)(sockets->second_fd(), buf, sizeof(buf)), 171 SyscallSucceedsWithValue(sizeof(buf))); 172 173 // Wait until second sees the data on its side but don't read it. 174 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 175 constexpr int kPollTimeoutMs = 30000; // Wait up to 30 seconds for the data. 176 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 177 SyscallSucceedsWithValue(1)); 178 179 // Wait until first sees the data on its side but don't read it. 180 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN, 0}; 181 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 182 SyscallSucceedsWithValue(1)); 183 184 // Now close the connected socket without reading the data from the second. 185 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 186 187 // Wait for the other end to receive the RST (up to 30 seconds). 188 struct pollfd poll_fd3 = {sockets->first_fd(), POLLHUP, 0}; 189 ASSERT_THAT(RetryEINTR(poll)(&poll_fd3, 1, kPollTimeoutMs), 190 SyscallSucceedsWithValue(1)); 191 192 // Since we also have data buffered we should be able to read it before 193 // the syscall will fail with ECONNRESET. 194 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 195 SyscallSucceedsWithValue(sizeof(buf))); 196 197 // A shutdown with unread data will cause a RST to be sent instead 198 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 199 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 200 SyscallFailsWithErrno(ECONNRESET)); 201 } 202 203 // This test will verify that a clean shutdown (FIN) is preformed when there 204 // is unread data but only the write side is closed. 205 TEST_P(TCPSocketPairTest, FINSentOnShutdownWrWithUnreadData) { 206 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 207 208 char buf[10] = {}; 209 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 210 SyscallSucceedsWithValue(sizeof(buf))); 211 212 // Wait until t_ sees the data on its side but don't read it. 213 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 214 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 215 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 216 SyscallSucceedsWithValue(1)); 217 218 // Now shutdown the write end leaving the read end open. 219 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_WR), SyscallSucceeds()); 220 221 // Wait for the other end to receive the FIN (up to 20 seconds). 222 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 223 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 224 SyscallSucceedsWithValue(1)); 225 226 // Since we didn't shutdown the read end this will be a clean close. 227 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 228 SyscallSucceedsWithValue(0)); 229 } 230 231 // This test will verify that when data is received by a socket, even if it's 232 // not read SHUT_RD will not cause any packets to be generated. 233 TEST_P(TCPSocketPairTest, ShutdownRdShouldCauseNoPacketsWithUnreadData) { 234 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 235 236 char buf[10] = {}; 237 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 238 SyscallSucceedsWithValue(sizeof(buf))); 239 240 // Wait until t_ sees the data on its side but don't read it. 241 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 242 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 243 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 244 SyscallSucceedsWithValue(1)); 245 246 // Now shutdown the read end, this will generate no packets to the other end. 247 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 248 249 // We should not receive any events on the other side of the socket. 250 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 251 constexpr int kPollNoResponseTimeoutMs = 3000; 252 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollNoResponseTimeoutMs), 253 SyscallSucceedsWithValue(0)); // Timeout. 254 } 255 256 // This test will verify that a socket which has unread data will still allow 257 // the data to be read after shutting down the read side, and once there is no 258 // unread data left, then read will return an EOF. 259 TEST_P(TCPSocketPairTest, ShutdownRdAllowsReadOfReceivedDataBeforeEOF) { 260 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 261 262 char buf[10] = {}; 263 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 264 SyscallSucceedsWithValue(sizeof(buf))); 265 266 // Wait until t_ sees the data on its side but don't read it. 267 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 268 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 269 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 270 SyscallSucceedsWithValue(1)); 271 272 // Now shutdown the read end. 273 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 274 275 // Even though we did a SHUT_RD on the read end we can still read the data. 276 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 277 SyscallSucceedsWithValue(sizeof(buf))); 278 279 // After reading all of the data, reading the closed read end returns EOF. 280 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 281 SyscallSucceedsWithValue(1)); 282 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 283 SyscallSucceedsWithValue(0)); 284 } 285 286 // This test verifies that a shutdown(wr) by the server after sending 287 // data allows the client to still read() the queued data and a client 288 // close after sending response allows server to read the incoming 289 // response. 290 TEST_P(TCPSocketPairTest, ShutdownWrServerClientClose) { 291 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 292 char buf[10] = {}; 293 ScopedThread t([&]() { 294 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 295 SyscallSucceedsWithValue(sizeof(buf))); 296 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 297 SyscallSucceedsWithValue(sizeof(buf))); 298 ASSERT_THAT(close(sockets->release_first_fd()), 299 SyscallSucceedsWithValue(0)); 300 }); 301 ASSERT_THAT(RetryEINTR(write)(sockets->second_fd(), buf, sizeof(buf)), 302 SyscallSucceedsWithValue(sizeof(buf))); 303 ASSERT_THAT(RetryEINTR(shutdown)(sockets->second_fd(), SHUT_WR), 304 SyscallSucceedsWithValue(0)); 305 t.Join(); 306 307 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 308 SyscallSucceedsWithValue(sizeof(buf))); 309 } 310 311 TEST_P(TCPSocketPairTest, ClosedReadNonBlockingSocket) { 312 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 313 314 // Set the read end to O_NONBLOCK. 315 int opts = 0; 316 ASSERT_THAT(opts = fcntl(sockets->second_fd(), F_GETFL), SyscallSucceeds()); 317 ASSERT_THAT(fcntl(sockets->second_fd(), F_SETFL, opts | O_NONBLOCK), 318 SyscallSucceeds()); 319 320 char buf[10] = {}; 321 ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), buf, sizeof(buf), 0), 322 SyscallSucceedsWithValue(sizeof(buf))); 323 324 // Wait until second_fd sees the data and then recv it. 325 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 326 constexpr int kPollTimeoutMs = 2000; // Wait up to 2 seconds for the data. 327 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 328 SyscallSucceedsWithValue(1)); 329 330 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 331 SyscallSucceedsWithValue(sizeof(buf))); 332 333 // Now shutdown the write end leaving the read end open. 334 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 335 336 // Wait for close notification and recv again. 337 struct pollfd poll_fd2 = {sockets->second_fd(), POLLIN, 0}; 338 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 339 SyscallSucceedsWithValue(1)); 340 341 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 342 SyscallSucceedsWithValue(0)); 343 } 344 345 TEST_P(TCPSocketPairTest, 346 ShutdownRdUnreadDataShouldCauseNoPacketsUnlessClosed) { 347 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 348 349 char buf[10] = {}; 350 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 351 SyscallSucceedsWithValue(sizeof(buf))); 352 353 // Wait until t_ sees the data on its side but don't read it. 354 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 355 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 356 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 357 SyscallSucceedsWithValue(1)); 358 359 // Now shutdown the read end, this will generate no packets to the other end. 360 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 361 362 // We should not receive any events on the other side of the socket. 363 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 364 constexpr int kPollNoResponseTimeoutMs = 3000; 365 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollNoResponseTimeoutMs), 366 SyscallSucceedsWithValue(0)); // Timeout. 367 368 // Now since we've fully closed the connection it will generate a RST. 369 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 370 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 371 SyscallSucceedsWithValue(1)); // The other end has closed. 372 373 // A shutdown with unread data will cause a RST to be sent instead 374 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 375 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 376 SyscallFailsWithErrno(ECONNRESET)); 377 } 378 379 TEST_P(TCPSocketPairTest, TCPCorkDefault) { 380 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 381 382 int get = -1; 383 socklen_t get_len = sizeof(get); 384 EXPECT_THAT( 385 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 386 SyscallSucceedsWithValue(0)); 387 EXPECT_EQ(get_len, sizeof(get)); 388 EXPECT_EQ(get, kSockOptOff); 389 } 390 391 TEST_P(TCPSocketPairTest, SetTCPCork) { 392 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 393 394 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 395 &kSockOptOn, sizeof(kSockOptOn)), 396 SyscallSucceeds()); 397 398 int get = -1; 399 socklen_t get_len = sizeof(get); 400 EXPECT_THAT( 401 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 402 SyscallSucceedsWithValue(0)); 403 EXPECT_EQ(get_len, sizeof(get)); 404 EXPECT_EQ(get, kSockOptOn); 405 406 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 407 &kSockOptOff, sizeof(kSockOptOff)), 408 SyscallSucceeds()); 409 410 EXPECT_THAT( 411 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 412 SyscallSucceedsWithValue(0)); 413 EXPECT_EQ(get_len, sizeof(get)); 414 EXPECT_EQ(get, kSockOptOff); 415 } 416 417 TEST_P(TCPSocketPairTest, TCPCork) { 418 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 419 420 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 421 &kSockOptOn, sizeof(kSockOptOn)), 422 SyscallSucceeds()); 423 424 constexpr char kData[] = "abc"; 425 ASSERT_THAT(WriteFd(sockets->first_fd(), kData, sizeof(kData)), 426 SyscallSucceedsWithValue(sizeof(kData))); 427 428 ASSERT_NO_FATAL_FAILURE(RecvNoData(sockets->second_fd())); 429 430 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 431 &kSockOptOff, sizeof(kSockOptOff)), 432 SyscallSucceeds()); 433 434 // Create a receive buffer larger than kData. 435 char buf[(sizeof(kData) + 1) * 2] = {}; 436 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 437 SyscallSucceedsWithValue(sizeof(kData))); 438 EXPECT_EQ(absl::string_view(kData, sizeof(kData)), 439 absl::string_view(buf, sizeof(kData))); 440 } 441 442 TEST_P(TCPSocketPairTest, TCPQuickAckDefault) { 443 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 444 445 int get = -1; 446 socklen_t get_len = sizeof(get); 447 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 448 &get_len), 449 SyscallSucceedsWithValue(0)); 450 EXPECT_EQ(get_len, sizeof(get)); 451 EXPECT_EQ(get, kSockOptOn); 452 } 453 454 TEST_P(TCPSocketPairTest, SetTCPQuickAck) { 455 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 456 457 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, 458 &kSockOptOff, sizeof(kSockOptOff)), 459 SyscallSucceeds()); 460 461 int get = -1; 462 socklen_t get_len = sizeof(get); 463 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 464 &get_len), 465 SyscallSucceedsWithValue(0)); 466 EXPECT_EQ(get_len, sizeof(get)); 467 EXPECT_EQ(get, kSockOptOff); 468 469 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, 470 &kSockOptOn, sizeof(kSockOptOn)), 471 SyscallSucceeds()); 472 473 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 474 &get_len), 475 SyscallSucceedsWithValue(0)); 476 EXPECT_EQ(get_len, sizeof(get)); 477 EXPECT_EQ(get, kSockOptOn); 478 } 479 480 TEST_P(TCPSocketPairTest, SoKeepaliveDefault) { 481 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 482 483 int get = -1; 484 socklen_t get_len = sizeof(get); 485 EXPECT_THAT( 486 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 487 SyscallSucceedsWithValue(0)); 488 EXPECT_EQ(get_len, sizeof(get)); 489 EXPECT_EQ(get, kSockOptOff); 490 } 491 492 TEST_P(TCPSocketPairTest, SetSoKeepalive) { 493 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 494 495 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 496 &kSockOptOn, sizeof(kSockOptOn)), 497 SyscallSucceeds()); 498 499 int get = -1; 500 socklen_t get_len = sizeof(get); 501 EXPECT_THAT( 502 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 503 SyscallSucceedsWithValue(0)); 504 EXPECT_EQ(get_len, sizeof(get)); 505 EXPECT_EQ(get, kSockOptOn); 506 507 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 508 &kSockOptOff, sizeof(kSockOptOff)), 509 SyscallSucceeds()); 510 511 EXPECT_THAT( 512 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 513 SyscallSucceedsWithValue(0)); 514 EXPECT_EQ(get_len, sizeof(get)); 515 EXPECT_EQ(get, kSockOptOff); 516 } 517 518 TEST_P(TCPSocketPairTest, TCPKeepidleDefault) { 519 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 520 521 int get = -1; 522 socklen_t get_len = sizeof(get); 523 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &get, 524 &get_len), 525 SyscallSucceedsWithValue(0)); 526 EXPECT_EQ(get_len, sizeof(get)); 527 EXPECT_EQ(get, 2 * 60 * 60); // 2 hours. 528 } 529 530 TEST_P(TCPSocketPairTest, TCPKeepintvlDefault) { 531 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 532 533 int get = -1; 534 socklen_t get_len = sizeof(get); 535 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, &get, 536 &get_len), 537 SyscallSucceedsWithValue(0)); 538 EXPECT_EQ(get_len, sizeof(get)); 539 EXPECT_EQ(get, 75); // 75 seconds. 540 } 541 542 TEST_P(TCPSocketPairTest, SetTCPKeepidleZero) { 543 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 544 545 constexpr int kZero = 0; 546 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &kZero, 547 sizeof(kZero)), 548 SyscallFailsWithErrno(EINVAL)); 549 } 550 551 TEST_P(TCPSocketPairTest, SetTCPKeepintvlZero) { 552 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 553 554 constexpr int kZero = 0; 555 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 556 &kZero, sizeof(kZero)), 557 SyscallFailsWithErrno(EINVAL)); 558 } 559 560 // Copied from include/net/tcp.h. 561 constexpr int MAX_TCP_KEEPIDLE = 32767; 562 constexpr int MAX_TCP_KEEPINTVL = 32767; 563 constexpr int MAX_TCP_KEEPCNT = 127; 564 565 TEST_P(TCPSocketPairTest, SetTCPKeepidleAboveMax) { 566 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 567 568 constexpr int kAboveMax = MAX_TCP_KEEPIDLE + 1; 569 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, 570 &kAboveMax, sizeof(kAboveMax)), 571 SyscallFailsWithErrno(EINVAL)); 572 } 573 574 TEST_P(TCPSocketPairTest, SetTCPKeepintvlAboveMax) { 575 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 576 577 constexpr int kAboveMax = MAX_TCP_KEEPINTVL + 1; 578 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 579 &kAboveMax, sizeof(kAboveMax)), 580 SyscallFailsWithErrno(EINVAL)); 581 } 582 583 TEST_P(TCPSocketPairTest, SetTCPKeepidleToMax) { 584 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 585 586 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, 587 &MAX_TCP_KEEPIDLE, sizeof(MAX_TCP_KEEPIDLE)), 588 SyscallSucceedsWithValue(0)); 589 590 int get = -1; 591 socklen_t get_len = sizeof(get); 592 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &get, 593 &get_len), 594 SyscallSucceedsWithValue(0)); 595 EXPECT_EQ(get_len, sizeof(get)); 596 EXPECT_EQ(get, MAX_TCP_KEEPIDLE); 597 } 598 599 TEST_P(TCPSocketPairTest, SetTCPKeepintvlToMax) { 600 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 601 602 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 603 &MAX_TCP_KEEPINTVL, sizeof(MAX_TCP_KEEPINTVL)), 604 SyscallSucceedsWithValue(0)); 605 606 int get = -1; 607 socklen_t get_len = sizeof(get); 608 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, &get, 609 &get_len), 610 SyscallSucceedsWithValue(0)); 611 EXPECT_EQ(get_len, sizeof(get)); 612 EXPECT_EQ(get, MAX_TCP_KEEPINTVL); 613 } 614 615 TEST_P(TCPSocketPairTest, TCPKeepcountDefault) { 616 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 617 618 int get = -1; 619 socklen_t get_len = sizeof(get); 620 EXPECT_THAT( 621 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 622 SyscallSucceedsWithValue(0)); 623 EXPECT_EQ(get_len, sizeof(get)); 624 EXPECT_EQ(get, 9); // 9 keepalive probes. 625 } 626 627 TEST_P(TCPSocketPairTest, SetTCPKeepcountZero) { 628 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 629 630 constexpr int kZero = 0; 631 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &kZero, 632 sizeof(kZero)), 633 SyscallFailsWithErrno(EINVAL)); 634 } 635 636 TEST_P(TCPSocketPairTest, SetTCPKeepcountAboveMax) { 637 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 638 639 constexpr int kAboveMax = MAX_TCP_KEEPCNT + 1; 640 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 641 &kAboveMax, sizeof(kAboveMax)), 642 SyscallFailsWithErrno(EINVAL)); 643 } 644 645 TEST_P(TCPSocketPairTest, SetTCPKeepcountToMax) { 646 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 647 648 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 649 &MAX_TCP_KEEPCNT, sizeof(MAX_TCP_KEEPCNT)), 650 SyscallSucceedsWithValue(0)); 651 652 int get = -1; 653 socklen_t get_len = sizeof(get); 654 EXPECT_THAT( 655 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 656 SyscallSucceedsWithValue(0)); 657 EXPECT_EQ(get_len, sizeof(get)); 658 EXPECT_EQ(get, MAX_TCP_KEEPCNT); 659 } 660 661 TEST_P(TCPSocketPairTest, SetTCPKeepcountToOne) { 662 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 663 664 int keepaliveCount = 1; 665 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 666 &keepaliveCount, sizeof(keepaliveCount)), 667 SyscallSucceedsWithValue(0)); 668 669 int get = -1; 670 socklen_t get_len = sizeof(get); 671 EXPECT_THAT( 672 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 673 SyscallSucceedsWithValue(0)); 674 EXPECT_EQ(get_len, sizeof(get)); 675 EXPECT_EQ(get, keepaliveCount); 676 } 677 678 TEST_P(TCPSocketPairTest, SetTCPKeepcountToNegative) { 679 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 680 681 int keepaliveCount = -5; 682 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 683 &keepaliveCount, sizeof(keepaliveCount)), 684 SyscallFailsWithErrno(EINVAL)); 685 } 686 687 TEST_P(TCPSocketPairTest, SetOOBInline) { 688 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 689 690 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_OOBINLINE, 691 &kSockOptOn, sizeof(kSockOptOn)), 692 SyscallSucceeds()); 693 694 int get = -1; 695 socklen_t get_len = sizeof(get); 696 EXPECT_THAT( 697 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_OOBINLINE, &get, &get_len), 698 SyscallSucceedsWithValue(0)); 699 EXPECT_EQ(get_len, sizeof(get)); 700 EXPECT_EQ(get, kSockOptOn); 701 } 702 703 TEST_P(TCPSocketPairTest, MsgTruncMsgPeek) { 704 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 705 706 char sent_data[512]; 707 RandomizeBuffer(sent_data, sizeof(sent_data)); 708 ASSERT_THAT( 709 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 710 SyscallSucceedsWithValue(sizeof(sent_data))); 711 712 // Read half of the data with MSG_TRUNC | MSG_PEEK. This way there will still 713 // be some data left to read in the next step even if the data gets consumed. 714 char received_data1[sizeof(sent_data) / 2] = {}; 715 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data1, 716 sizeof(received_data1), MSG_TRUNC | MSG_PEEK), 717 SyscallSucceedsWithValue(sizeof(received_data1))); 718 719 // Check that we didn't get anything. 720 char zeros[sizeof(received_data1)] = {}; 721 EXPECT_EQ(0, memcmp(zeros, received_data1, sizeof(received_data1))); 722 723 // Check that all of the data is still there. 724 char received_data2[sizeof(sent_data)] = {}; 725 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data2, 726 sizeof(received_data2), 0), 727 SyscallSucceedsWithValue(sizeof(sent_data))); 728 729 EXPECT_EQ(0, memcmp(received_data2, sent_data, sizeof(sent_data))); 730 } 731 732 TEST_P(TCPSocketPairTest, SetCongestionControlSucceedsForSupported) { 733 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 734 const int kTcpCaNameMax = 16; 735 736 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 737 // Netstack only supports reno & cubic so we only test these two values here. 738 { 739 const char kSetCC[kTcpCaNameMax] = "reno"; 740 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 741 &kSetCC, strlen(kSetCC)), 742 SyscallSucceedsWithValue(0)); 743 744 char got_cc[kTcpCaNameMax]; 745 memset(got_cc, '1', sizeof(got_cc)); 746 socklen_t optlen = sizeof(got_cc); 747 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 748 &got_cc, &optlen), 749 SyscallSucceedsWithValue(0)); 750 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 751 } 752 { 753 const char kSetCC[kTcpCaNameMax] = "cubic"; 754 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 755 &kSetCC, strlen(kSetCC)), 756 SyscallSucceedsWithValue(0)); 757 758 char got_cc[kTcpCaNameMax]; 759 memset(got_cc, '1', sizeof(got_cc)); 760 socklen_t optlen = sizeof(got_cc); 761 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 762 &got_cc, &optlen), 763 SyscallSucceedsWithValue(0)); 764 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 765 } 766 } 767 768 TEST_P(TCPSocketPairTest, SetGetTCPCongestionShortReadBuffer) { 769 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 770 { 771 // Verify that getsockopt/setsockopt work with buffers smaller than 772 // kTcpCaNameMax. 773 const char kSetCC[] = "cubic"; 774 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 775 &kSetCC, strlen(kSetCC)), 776 SyscallSucceedsWithValue(0)); 777 778 char got_cc[sizeof(kSetCC)]; 779 socklen_t optlen = sizeof(got_cc); 780 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 781 &got_cc, &optlen), 782 SyscallSucceedsWithValue(0)); 783 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(got_cc))); 784 } 785 } 786 787 TEST_P(TCPSocketPairTest, SetGetTCPCongestionLargeReadBuffer) { 788 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 789 const int kTcpCaNameMax = 16; 790 791 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 792 { 793 // Verify that getsockopt works with buffers larger than 794 // kTcpCaNameMax. 795 const char kSetCC[] = "cubic"; 796 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 797 &kSetCC, strlen(kSetCC)), 798 SyscallSucceedsWithValue(0)); 799 800 char got_cc[kTcpCaNameMax + 5]; 801 socklen_t optlen = sizeof(got_cc); 802 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 803 &got_cc, &optlen), 804 SyscallSucceedsWithValue(0)); 805 // Linux copies the minimum of kTcpCaNameMax or the length of the passed in 806 // buffer and sets optlen to the number of bytes actually copied 807 // irrespective of the actual length of the congestion control name. 808 EXPECT_EQ(kTcpCaNameMax, optlen); 809 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 810 } 811 } 812 813 TEST_P(TCPSocketPairTest, SetCongestionControlFailsForUnsupported) { 814 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 815 const int kTcpCaNameMax = 16; 816 817 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 818 char old_cc[kTcpCaNameMax]; 819 socklen_t optlen = sizeof(old_cc); 820 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 821 &old_cc, &optlen), 822 SyscallSucceedsWithValue(0)); 823 824 const char kSetCC[] = "invalid_ca_cc"; 825 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 826 &kSetCC, strlen(kSetCC)), 827 SyscallFailsWithErrno(ENOENT)); 828 829 char got_cc[kTcpCaNameMax]; 830 optlen = sizeof(got_cc); 831 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 832 &got_cc, &optlen), 833 SyscallSucceedsWithValue(0)); 834 EXPECT_EQ(0, memcmp(got_cc, old_cc, sizeof(old_cc))); 835 } 836 837 // Linux and Netstack both default to a 60s TCP_LINGER2 timeout. 838 constexpr int kDefaultTCPLingerTimeout = 60; 839 // On Linux, the maximum linger2 timeout was changed from 60sec to 120sec. 840 constexpr int kMaxTCPLingerTimeout = 120; 841 constexpr int kOldMaxTCPLingerTimeout = 60; 842 843 TEST_P(TCPSocketPairTest, TCPLingerTimeoutDefault) { 844 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 845 846 int get = -1; 847 socklen_t get_len = sizeof(get); 848 EXPECT_THAT( 849 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 850 SyscallSucceedsWithValue(0)); 851 EXPECT_EQ(get_len, sizeof(get)); 852 EXPECT_EQ(get, kDefaultTCPLingerTimeout); 853 } 854 855 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutLessThanZero) { 856 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 857 858 constexpr int kNegative = -1234; 859 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 860 &kNegative, sizeof(kNegative)), 861 SyscallSucceedsWithValue(0)); 862 int get = INT_MAX; 863 socklen_t get_len = sizeof(get); 864 EXPECT_THAT( 865 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 866 SyscallSucceedsWithValue(0)); 867 EXPECT_EQ(get_len, sizeof(get)); 868 EXPECT_EQ(get, -1); 869 } 870 871 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutZero) { 872 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 873 874 constexpr int kZero = 0; 875 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &kZero, 876 sizeof(kZero)), 877 SyscallSucceedsWithValue(0)); 878 int get = -1; 879 socklen_t get_len = sizeof(get); 880 EXPECT_THAT( 881 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 882 SyscallSucceedsWithValue(0)); 883 EXPECT_EQ(get_len, sizeof(get)); 884 EXPECT_THAT(get, 885 AnyOf(Eq(kMaxTCPLingerTimeout), Eq(kOldMaxTCPLingerTimeout))); 886 } 887 888 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutAboveMax) { 889 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 890 891 // Values above the net.ipv4.tcp_fin_timeout are capped to tcp_fin_timeout 892 // on linux (defaults to 60 seconds on linux). 893 constexpr int kAboveDefault = kMaxTCPLingerTimeout + 1; 894 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 895 &kAboveDefault, sizeof(kAboveDefault)), 896 SyscallSucceedsWithValue(0)); 897 898 int get = -1; 899 socklen_t get_len = sizeof(get); 900 EXPECT_THAT( 901 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 902 SyscallSucceedsWithValue(0)); 903 EXPECT_EQ(get_len, sizeof(get)); 904 if (IsRunningOnGvisor()) { 905 EXPECT_EQ(get, kMaxTCPLingerTimeout); 906 } else { 907 EXPECT_THAT(get, 908 AnyOf(Eq(kMaxTCPLingerTimeout), Eq(kOldMaxTCPLingerTimeout))); 909 } 910 } 911 912 TEST_P(TCPSocketPairTest, SetTCPLingerTimeout) { 913 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 914 915 // Values above the net.ipv4.tcp_fin_timeout are capped to tcp_fin_timeout 916 // on linux (defaults to 60 seconds on linux). 917 constexpr int kTCPLingerTimeout = kDefaultTCPLingerTimeout - 1; 918 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 919 &kTCPLingerTimeout, sizeof(kTCPLingerTimeout)), 920 SyscallSucceedsWithValue(0)); 921 922 int get = -1; 923 socklen_t get_len = sizeof(get); 924 EXPECT_THAT( 925 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 926 SyscallSucceedsWithValue(0)); 927 EXPECT_EQ(get_len, sizeof(get)); 928 EXPECT_EQ(get, kTCPLingerTimeout); 929 } 930 931 TEST_P(TCPSocketPairTest, TestTCPCloseWithData) { 932 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 933 934 ScopedThread t([&]() { 935 // Close one end to trigger sending of a FIN. 936 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_WR), SyscallSucceeds()); 937 char buf[3]; 938 ASSERT_THAT(read(sockets->second_fd(), buf, 3), 939 SyscallSucceedsWithValue(3)); 940 absl::SleepFor(absl::Milliseconds(50)); 941 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 942 }); 943 944 absl::SleepFor(absl::Milliseconds(50)); 945 // Send some data then close. 946 constexpr char kStr[] = "abc"; 947 ASSERT_THAT(write(sockets->first_fd(), kStr, 3), SyscallSucceedsWithValue(3)); 948 t.Join(); 949 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 950 } 951 952 TEST_P(TCPSocketPairTest, TCPUserTimeoutDefault) { 953 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 954 955 int get = -1; 956 socklen_t get_len = sizeof(get); 957 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 958 &get, &get_len), 959 SyscallSucceeds()); 960 EXPECT_EQ(get_len, sizeof(get)); 961 EXPECT_EQ(get, 0); // 0 ms (disabled). 962 } 963 964 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutZero) { 965 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 966 967 constexpr int kZero = 0; 968 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 969 &kZero, sizeof(kZero)), 970 SyscallSucceeds()); 971 972 int get = -1; 973 socklen_t get_len = sizeof(get); 974 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 975 &get, &get_len), 976 SyscallSucceeds()); 977 EXPECT_EQ(get_len, sizeof(get)); 978 EXPECT_EQ(get, 0); // 0 ms (disabled). 979 } 980 981 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutBelowZero) { 982 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 983 984 constexpr int kNeg = -10; 985 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 986 &kNeg, sizeof(kNeg)), 987 SyscallFailsWithErrno(EINVAL)); 988 989 int get = -1; 990 socklen_t get_len = sizeof(get); 991 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 992 &get, &get_len), 993 SyscallSucceeds()); 994 EXPECT_EQ(get_len, sizeof(get)); 995 EXPECT_EQ(get, 0); // 0 ms (disabled). 996 } 997 998 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutAboveZero) { 999 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1000 1001 constexpr int kAbove = 10; 1002 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1003 &kAbove, sizeof(kAbove)), 1004 SyscallSucceeds()); 1005 1006 int get = -1; 1007 socklen_t get_len = sizeof(get); 1008 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1009 &get, &get_len), 1010 SyscallSucceeds()); 1011 EXPECT_EQ(get_len, sizeof(get)); 1012 EXPECT_EQ(get, kAbove); 1013 } 1014 1015 #ifdef __linux__ 1016 TEST_P(TCPSocketPairTest, SpliceFromPipe) { 1017 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1018 1019 int fds[2]; 1020 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 1021 FileDescriptor rfd(fds[0]); 1022 FileDescriptor wfd(fds[1]); 1023 1024 // Fill with some random data. 1025 std::vector<char> buf(kPageSize / 2); 1026 RandomizeBuffer(buf.data(), buf.size()); 1027 ASSERT_THAT(write(wfd.get(), buf.data(), buf.size()), 1028 SyscallSucceedsWithValue(buf.size())); 1029 1030 EXPECT_THAT( 1031 splice(rfd.get(), nullptr, sockets->first_fd(), nullptr, kPageSize, 0), 1032 SyscallSucceedsWithValue(buf.size())); 1033 1034 std::vector<char> rbuf(buf.size()); 1035 ASSERT_THAT(read(sockets->second_fd(), rbuf.data(), rbuf.size()), 1036 SyscallSucceedsWithValue(buf.size())); 1037 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1038 } 1039 1040 TEST_P(TCPSocketPairTest, SpliceToPipe) { 1041 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1042 1043 int fds[2]; 1044 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 1045 FileDescriptor rfd(fds[0]); 1046 FileDescriptor wfd(fds[1]); 1047 1048 // Fill with some random data. 1049 std::vector<char> buf(kPageSize / 2); 1050 RandomizeBuffer(buf.data(), buf.size()); 1051 ASSERT_THAT(write(sockets->first_fd(), buf.data(), buf.size()), 1052 SyscallSucceedsWithValue(buf.size())); 1053 shutdown(sockets->first_fd(), SHUT_WR); 1054 EXPECT_THAT( 1055 splice(sockets->second_fd(), nullptr, wfd.get(), nullptr, kPageSize, 0), 1056 SyscallSucceedsWithValue(buf.size())); 1057 1058 std::vector<char> rbuf(buf.size()); 1059 ASSERT_THAT(read(rfd.get(), rbuf.data(), rbuf.size()), 1060 SyscallSucceedsWithValue(buf.size())); 1061 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1062 } 1063 1064 #include <sys/sendfile.h> 1065 1066 TEST_P(TCPSocketPairTest, SendfileFromRegularFileSucceeds) { 1067 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1068 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 1069 const FileDescriptor in_fd = 1070 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 1071 // Fill with some random data. 1072 std::vector<char> buf(kPageSize / 2); 1073 RandomizeBuffer(buf.data(), buf.size()); 1074 ASSERT_THAT(pwrite(in_fd.get(), buf.data(), buf.size(), 0), 1075 SyscallSucceedsWithValue(buf.size())); 1076 1077 EXPECT_THAT( 1078 sendfile(sockets->first_fd(), in_fd.get(), nullptr, buf.size() + 1), 1079 SyscallSucceedsWithValue(buf.size())); 1080 std::vector<char> rbuf(buf.size() + 1); 1081 ASSERT_THAT(read(sockets->second_fd(), rbuf.data(), rbuf.size()), 1082 SyscallSucceedsWithValue(buf.size())); 1083 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1084 } 1085 #endif // __linux__ 1086 1087 TEST_P(TCPSocketPairTest, SetTCPWindowClampBelowMinRcvBufConnectedSocket) { 1088 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1089 // Discover minimum receive buf by setting a really low value 1090 // for the receive buffer. 1091 constexpr int kZero = 0; 1092 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVBUF, &kZero, 1093 sizeof(kZero)), 1094 SyscallSucceeds()); 1095 1096 // Now retrieve the minimum value for SO_RCVBUF as the set above should 1097 // have caused SO_RCVBUF for the socket to be set to the minimum. 1098 int get = -1; 1099 socklen_t get_len = sizeof(get); 1100 ASSERT_THAT( 1101 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVBUF, &get, &get_len), 1102 SyscallSucceedsWithValue(0)); 1103 EXPECT_EQ(get_len, sizeof(get)); 1104 int min_so_rcvbuf = get; 1105 1106 { 1107 // Setting TCP_WINDOW_CLAMP to zero for a connected socket is not permitted. 1108 constexpr int kZero = 0; 1109 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1110 &kZero, sizeof(kZero)), 1111 SyscallFailsWithErrno(EINVAL)); 1112 1113 // Non-zero clamp values below MIN_SO_RCVBUF/2 should result in the clamp 1114 // being set to MIN_SO_RCVBUF/2. 1115 int below_half_min_so_rcvbuf = min_so_rcvbuf / 2 - 1; 1116 EXPECT_THAT( 1117 setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1118 &below_half_min_so_rcvbuf, sizeof(below_half_min_so_rcvbuf)), 1119 SyscallSucceeds()); 1120 1121 int get = -1; 1122 socklen_t get_len = sizeof(get); 1123 1124 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1125 &get, &get_len), 1126 SyscallSucceedsWithValue(0)); 1127 EXPECT_EQ(get_len, sizeof(get)); 1128 EXPECT_EQ(min_so_rcvbuf / 2, get); 1129 } 1130 } 1131 1132 TEST_P(TCPSocketPairTest, IpMulticastTtlDefault) { 1133 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1134 1135 int get = -1; 1136 socklen_t get_len = sizeof(get); 1137 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_IP, IP_MULTICAST_TTL, 1138 &get, &get_len), 1139 SyscallSucceedsWithValue(0)); 1140 EXPECT_EQ(get_len, sizeof(get)); 1141 EXPECT_GT(get, 0); 1142 } 1143 1144 TEST_P(TCPSocketPairTest, IpMulticastLoopDefault) { 1145 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1146 1147 int get = -1; 1148 socklen_t get_len = sizeof(get); 1149 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_IP, IP_MULTICAST_LOOP, 1150 &get, &get_len), 1151 SyscallSucceedsWithValue(0)); 1152 EXPECT_EQ(get_len, sizeof(get)); 1153 EXPECT_EQ(get, 1); 1154 } 1155 1156 TEST_P(TCPSocketPairTest, TCPResetDuringClose) { 1157 DisableSave ds; // Too many syscalls. 1158 constexpr int kThreadCount = 100; 1159 std::unique_ptr<ScopedThread> instances[kThreadCount]; 1160 for (int i = 0; i < kThreadCount; i++) { 1161 instances[i] = absl::make_unique<ScopedThread>([&]() { 1162 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1163 1164 ScopedThread t([&]() { 1165 // Close one end to trigger sending of a FIN. 1166 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 1167 // Wait up to 20 seconds for the data. 1168 constexpr int kPollTimeoutMs = 20000; 1169 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 1170 SyscallSucceedsWithValue(1)); 1171 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 1172 }); 1173 1174 // Send some data then close. 1175 constexpr char kStr[] = "abc"; 1176 ASSERT_THAT(write(sockets->first_fd(), kStr, 3), 1177 SyscallSucceedsWithValue(3)); 1178 absl::SleepFor(absl::Milliseconds(10)); 1179 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1180 t.Join(); 1181 }); 1182 } 1183 for (int i = 0; i < kThreadCount; i++) { 1184 instances[i]->Join(); 1185 } 1186 } 1187 1188 // Test setsockopt and getsockopt for a socket with SO_LINGER option. 1189 TEST_P(TCPSocketPairTest, SetAndGetLingerOption) { 1190 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1191 1192 // Check getsockopt before SO_LINGER option is set. 1193 struct linger got_linger = {-1, -1}; 1194 socklen_t got_len = sizeof(got_linger); 1195 1196 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1197 &got_linger, &got_len), 1198 SyscallSucceeds()); 1199 ASSERT_THAT(got_len, sizeof(got_linger)); 1200 struct linger want_linger = {}; 1201 EXPECT_EQ(0, memcmp(&want_linger, &got_linger, got_len)); 1202 1203 // Set and get SO_LINGER with negative values. 1204 struct linger sl; 1205 sl.l_onoff = 1; 1206 sl.l_linger = -3; 1207 ASSERT_THAT( 1208 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1209 SyscallSucceeds()); 1210 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1211 &got_linger, &got_len), 1212 SyscallSucceeds()); 1213 ASSERT_EQ(got_len, sizeof(got_linger)); 1214 EXPECT_EQ(sl.l_onoff, got_linger.l_onoff); 1215 // Linux returns a different value as it uses HZ to convert the seconds to 1216 // jiffies which overflows for negative values. We want to be compatible with 1217 // linux for getsockopt return value. 1218 if (IsRunningOnGvisor()) { 1219 EXPECT_EQ(sl.l_linger, got_linger.l_linger); 1220 } 1221 1222 // Set and get SO_LINGER option with positive values. 1223 sl.l_onoff = 1; 1224 sl.l_linger = 5; 1225 ASSERT_THAT( 1226 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1227 SyscallSucceeds()); 1228 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1229 &got_linger, &got_len), 1230 SyscallSucceeds()); 1231 ASSERT_EQ(got_len, sizeof(got_linger)); 1232 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1233 } 1234 1235 // Test socket to disable SO_LINGER option. 1236 TEST_P(TCPSocketPairTest, SetOffLingerOption) { 1237 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1238 1239 // Set the SO_LINGER option. 1240 struct linger sl; 1241 sl.l_onoff = 1; 1242 sl.l_linger = 5; 1243 ASSERT_THAT( 1244 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1245 SyscallSucceeds()); 1246 1247 // Check getsockopt after SO_LINGER option is set. 1248 struct linger got_linger = {-1, -1}; 1249 socklen_t got_len = sizeof(got_linger); 1250 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1251 &got_linger, &got_len), 1252 SyscallSucceeds()); 1253 ASSERT_EQ(got_len, sizeof(got_linger)); 1254 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1255 1256 sl.l_onoff = 0; 1257 sl.l_linger = 5; 1258 ASSERT_THAT( 1259 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1260 SyscallSucceeds()); 1261 1262 // Check getsockopt after SO_LINGER option is set to zero. 1263 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1264 &got_linger, &got_len), 1265 SyscallSucceeds()); 1266 ASSERT_EQ(got_len, sizeof(got_linger)); 1267 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1268 } 1269 1270 // Test close on dup'd socket with SO_LINGER option set. 1271 TEST_P(TCPSocketPairTest, CloseWithLingerOption) { 1272 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1273 1274 // Set the SO_LINGER option. 1275 struct linger sl; 1276 sl.l_onoff = 1; 1277 sl.l_linger = 5; 1278 ASSERT_THAT( 1279 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1280 SyscallSucceeds()); 1281 1282 // Check getsockopt after SO_LINGER option is set. 1283 struct linger got_linger = {-1, -1}; 1284 socklen_t got_len = sizeof(got_linger); 1285 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1286 &got_linger, &got_len), 1287 SyscallSucceeds()); 1288 ASSERT_EQ(got_len, sizeof(got_linger)); 1289 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1290 1291 FileDescriptor dupFd = FileDescriptor(dup(sockets->first_fd())); 1292 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1293 char buf[10] = {}; 1294 // Write on dupFd should succeed as socket will not be closed until 1295 // all references are removed. 1296 ASSERT_THAT(RetryEINTR(write)(dupFd.get(), buf, sizeof(buf)), 1297 SyscallSucceedsWithValue(sizeof(buf))); 1298 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 1299 SyscallFailsWithErrno(EBADF)); 1300 1301 // Close the socket. 1302 dupFd.reset(); 1303 // Write on dupFd should fail as all references for socket are removed. 1304 ASSERT_THAT(RetryEINTR(write)(dupFd.get(), buf, sizeof(buf)), 1305 SyscallFailsWithErrno(EBADF)); 1306 } 1307 } // namespace testing 1308 } // namespace gvisor