61 Commits
Author SHA1 Message Date
gVisor bot 975eb8e83f Internal change.
PiperOrigin-RevId: 957694371
2026-08-01 12:58:40 -07:00
Xin ZhongandgVisor bot f38a547aaa Fix TSan data race in UDP Socket Tests
TSan flags concurrent socket operations on the identical host FD
as a data race. This commit uses dup() to duplicate the socket file
descriptor specifically for the background thread's connect() call,
so sendto and connect can execute concurrently from two different
threads without TSan data race on the fd struct.

PiperOrigin-RevId: 955479183
2026-07-28 14:41:55 -07:00
Xin ZhongandgVisor bot 232b98272b Fix MSan uninitialized variables in udp_socket.cc
Zero-initialize multiple char arrays and msghdr structs in udp_socket.cc to
fix MSan failures caused by uninitialized memory reading prior to send(),
sendto(), write(), and sendmsg().

PiperOrigin-RevId: 954953223
2026-07-27 18:50:14 -07:00
gVisor bot 38d62f734a Merge pull request #12852 from tanyifeng:net-ip-mtu-discover
PiperOrigin-RevId: 894240043
2026-04-03 14:06:21 -07:00
Tan Yifeng 5c258bd415 net: Support all valid IP_MTU_DISCOVER values for UDP and TCP
The UDP endpoint only accepted PMTUDiscoveryDont and the TCP endpoint
rejected PMTUDiscoveryProbe, causing QUIC libraries (quic-go) to fail
when setting IP_PMTUDISC_DO or IP_PMTUDISC_PROBE on UDP sockets.

Accept all valid PMTUD strategies (DONT/WANT/DO/PROBE) for both UDP
and TCP, since gVisor does not implement ICMP-based PMTU feedback,
`PROBE` and `DO` are functionally equivalent — both set DF but neither
updates a cached route PMTU.

Test: UdpSocketTest.SetPMTUD, TcpSocketTest.SetPMTUD

Signed-off-by: Tan Yifeng <[email protected]>
2026-04-02 16:47:35 +08:00
Nayana BidariandgVisor bot 8738a61d9f Remove TODOs
gVisor allows connecting with port zero, remove the TODOs and allow the tests
to run on gVisor.

PiperOrigin-RevId: 859194427
2026-01-21 11:43:42 -08:00
Shailend ChandandgVisor bot cfc0fde362 Deflake the hostinet variants of syscalls:udp_socket_test
The `BoundaryPreserving*` tests used to fail with hostinet because
Linux would occasionally reorder the udp pkts. `RecvErrorConnRefused`
stops flaking after adding a poll().

PiperOrigin-RevId: 859157927
2026-01-21 10:23:21 -08:00
Shailend ChandandgVisor bot 3eadc299d9 Deflake the UdpSocketTest.RecvBufLimits syscall test
PiperOrigin-RevId: 858759391
2026-01-20 14:44:00 -08:00
Nayana BidariandgVisor bot aa18b0c854 Fix reconnect of UDP socket.
The reconnect for UDP sockets was failing because we were not removing the old
registration before attempting to register again. Fix this issue by
removing the old registration and then registering the endpoint.

Updates #8998

PiperOrigin-RevId: 582007411
2023-11-13 10:24:31 -08:00
gVisor bot aaa9c4e5ce Internal change.
PiperOrigin-RevId: 567370524
2023-09-21 11:42:16 -07:00
Nayana BidariandgVisor bot 33bc37de55 Return error while calling shutdown on a bound unconnected UDP socket.
PiperOrigin-RevId: 542057418
2023-06-20 14:57:45 -07:00
Alex KonradiandgVisor bot 65a32da6ce Add a test for receiving UDP packet with src port 0
Add a test that validates that when a UDP packet is received with a source port
value of 0, it is delivered to a listening socket.

PiperOrigin-RevId: 536773375
2023-05-31 11:31:05 -07:00
Nayana BidariandgVisor bot 9d92c5daf2 Enable socket option SO_NO_CHECK tests on hostinet.
- Enables the test to set/get SO_NO_CHECK option on hostinet as this option is
already supported on hostinet.
- Enables all the other UDP hostinet tests except the flaky test to set/get
IP_RECVTOS option.

PiperOrigin-RevId: 525792961
2023-04-20 10:57:06 -07:00
Nick BrownandgVisor bot aeac52a964 Connect to bound addr before closing socket
The `ConnectAndSendNoReceiver` test tries to setup a scenario in which a socket
sends to an address with no listener, asserting on the returned ICMP error.
In the existing code, this is done by closing a socket and then
connecting/sending to its bound address with another socket. This is flaky
because sometimes the system will coincidentally bind the sender to the bound
address of the receiver, in which case we won't get the ICMP error. The fix is
to connect before the socket is closed (that way the sender will get a
different port than the boudn address).

PiperOrigin-RevId: 518612786
2023-03-22 10:48:33 -07:00
Nick BrownandgVisor bot 92885aa8c6 Defer poll timeout to Fuchsia's CI infra
Set the ICMP poll timeout to a negative value so the poll will block
indefinitely. This effectively delegates the timeout to the overall test
timeout imposed by Fuchsia's CI/CQ.

PiperOrigin-RevId: 516863038
2023-03-15 10:28:20 -07:00
Nick BrownandgVisor bot a72a5406cd Increase ICMP poll timeout on Fuchsia
We are still seeing flakes in Fuchsia's CQ. Increase the poll timeout by a
factor of 10.

PiperOrigin-RevId: 516659576
2023-03-14 16:10:08 -07:00
Nick BrownandgVisor bot 21d79d9607 Increase ICMP timeout
We are still hitting the occasional flake in Fuchsia's CQ when polling for an
ICMP error. Increase the timeout from 2s => 5s.

PiperOrigin-RevId: 509655650
2023-02-14 15:34:12 -08:00
Alex KonradiandgVisor bot 478c3f178a Connect before closing the destination socket to acquire different port
In the ConnectWriteToInvalidPort test case, the target socket is closed before
the source socket sends data to generate and then verify reception of an ICMP
error packet. If the target socket is closed before an outgoing port number is
picked for the source socket, the source socket might be assigned the same
port number for sending as its destination. The result is that the source
socket receives the packet it sends and so no error is generated. This results
in test flakiness, though with very low probability.

Fixes #8338

PiperOrigin-RevId: 499244574
2023-01-03 09:46:19 -08:00
Alex KonradiandgVisor bot b6cae48a5c Fix more UDP port number rollovers
Some of the UDP tests bind a socket with port 0, read the bound port number,
then connect using 1+bound port as the port number. This can roll over if the
originally bound port is 2^16-1, which makes for test cases that are very
infrequently flaky on systems with an ephemeral port range that includes
2^16-1. Fix this by using bound port-1, which should never underflow since the
bound port isn't 0, and should always be in a usable range, even if it is
below the ephemeral port range.

PiperOrigin-RevId: 496996446
2022-12-21 13:43:37 -08:00
Alex KonradiandgVisor bot 3f4ba8b65f Don't hardcode ports when binding udp sockets
The existing logic constructs a loopback address for binding by incrementing
the port number of an existing address. This can lead to failures in the event
that the original address had port=65535 (the maximum port). Resolve this
failure case by using the same port number instead of incrementing.

PiperOrigin-RevId: 488934780
2022-11-16 07:31:56 -08:00
Nick BrownandgVisor bot be441ba2d1 Increase timeout when polling for ICMP errors
We're seeing occasional flakes in CQ in which the ICMP error isn't received in
time. Double the timeout from 1s to 2s.

PiperOrigin-RevId: 487035507
2022-11-08 13:24:33 -08:00
Nick BrownandgVisor bot 925832e127 Don't hardcode ports when binding udp sockets
The existing logic constructs a loopback address for binding by incrementing
the port number of an existing address. This can lead to failures in the event
that the original address had port=65535 (the maximum port). Resolve this
failure case by just binding to a new loopback address and letting the system
pick the port.

Also, fix a bug in `ReceiveFromNotConnected` in which the test was asserting on
the wrong socket.

PiperOrigin-RevId: 482260609
2022-10-19 11:41:07 -07:00
Nick BrownandgVisor bot 6527b6e865 Run syscall tests against Fast UDP
Includes the following diffs:

- Use timeouts when reading immediately after sending.
- Poll for POLLIN before operations that assume data has arrived.
- Expect`SendMsgTooLarge` passes with Fast UDP (error code is correct now).
- Skip `RecvBufLimits` on Fast UDP (limits not enforced due to zircon socket).

Fuchsia Bug: 101888

PiperOrigin-RevId: 459857293
2022-07-08 16:32:59 -07:00
Ghanan GowripalanandgVisor bot 4ca36a9e60 Respect SO_SNDBUF for network datagram endpoints
Previously, SO_SNDBUF was effectively a no-op. The stack should make
sure that only SO_SNDBUF bytes are ever in-flight for any given
socket/endpoint.

Fuchsia Bug: https://fxbug.dev/99070

PiperOrigin-RevId: 446792223
2022-05-05 12:52:34 -07:00
Ghanan GowripalanandgVisor bot d2a2d74aa9 Use different flags for IPV6_RECVERR and IP_RECVERR
PiperOrigin-RevId: 446361103
2022-05-03 21:17:40 -07:00
Andrei VaginandgVisor bot ca1012c047 tests: fix compile time warnings
test/syscalls/linux/futex.cc:
In member function 'virtual void gvisor::testing::
{anonymous}::PrivateAndSharedFutexTest_PIWaiters_Test::TestBody()':

test/syscalls/linux/futex.cc:697:19:
warning: comparison of integer expressions of different signedness:
'std::__atomic_base<int>::__int_type' {aka 'int'} and 'unsigned int'
[-Wsign-compare]

  697 |   while (a.load() != (FUTEX_WAITERS | gettid())) {
      |          ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test/syscalls/linux/proc_pid_uid_gid_map.cc:207:64:
warning: comparison of integer expressions of different signedness: 'size_t'
{aka 'long unsigned int'} and 'int' [-Wsign-compare]

  207 |         TEST_PCHECK((n = write(fd, line.c_str(), line.size())) != -1);
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
test/syscalls/linux/socket_inet_loopback.cc:964:21:
warning: comparison of integer expressions of different signedness: 'int' and
'std::array<gvisor::testing::FileDescriptor, 1>::size_type' {aka 'long
unsigned int'} [-Wsign-compare]

  964 |   for (int i = 0; i < std::size(established_clients); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

test/syscalls/linux/socket_inet_loopback.cc:974:21:
warning: comparison of integer expressions of different signedness: 'int' and
'std::array<gvisor::testing::FileDescriptor, 1>::size_type' {aka 'long
unsigned int'} [-Wsign-compare]

  974 |   for (int i = 0; i < std::size(waiting_clients); i++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

test/syscalls/linux/udp_socket.cc:869:6:
warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]

  869 |if (!IsRunningWithHostinet() || GvisorPlatform() == Platform::kPtrace ||
      |   ^

test/syscalls/linux/socket_unix_unbound_abstract.cc:93:9:
warning: variable 'orig_opts' set but not used [-Wunused-but-set-variable]

   93 |     int orig_opts;
      |         ^~~~~~~~~

test/syscalls/linux/socket_unix_unbound_abstract.cc:107:9:
warning: variable 'orig_opts' set but not used [-Wunused-but-set-variable]

  107 |     int orig_opts;
      |         ^~~~~~~~~

PiperOrigin-RevId: 445545240
2022-04-29 17:59:14 -07:00
Nick BrownandgVisor bot e5ee28faa3 Poll for POLLERR before validating ICMP error
...since ICMP errors are returned asynchronously. This also matches the
behavior of existing tests.

PiperOrigin-RevId: 435484650
2022-03-17 16:50:09 -07:00
gVisor bot eedbed0ae3 Internal change.
PiperOrigin-RevId: 434836427
2022-03-15 13:13:15 -07:00
Arthur SfezandgVisor bot 4dbf587b07 Support sending TTL and HopLimit
PiperOrigin-RevId: 430554985
2022-02-23 15:40:37 -08:00
Arthur SfezandgVisor bot 1d2822917b Support receiving ttl/hoplimit control message
PiperOrigin-RevId: 429096959
2022-02-16 11:27:21 -08:00
Arthur SfezandgVisor bot 7ea73a6830 Support receiving PKTINFO on icmp endpoints
PiperOrigin-RevId: 428599075
2022-02-14 13:51:49 -08:00
Arthur SfezandgVisor bot 9a87142f27 Simplify TOS and TClass UDP tests
This adds helpers to send both of these control messages via sendmsg,
and most of the initialization is moved into the SetUp body.

The parametrization is now more meaningful. The kDualStack is actually
impactful, which means DifferentTOSAndTClass test has been removed. The
IPv4-mapped IPv6 scenario is now tested in the SetAndReceiveTOSOrTClass.

PiperOrigin-RevId: 426313619
2022-02-03 21:57:29 -08:00
Arthur SfezandgVisor bot 3077b0781b Remove unused parameter in UDP tests instantiation
The variant kDualStack is never used by any test. It just duplicates
kIpv6 case. Because the parameters are now just kIpv4 and kIpv6, use
an int to encode the address family, instead of the enum.

PiperOrigin-RevId: 426224701
2022-02-03 13:31:10 -08:00
Ghanan GowripalanandgVisor bot cd0623bfd5 [bind] Return EINVAL for under sized address
...and EAFNOSUPPORT for unexpected address family.

To comply with Linux.

Updates #6021, #6575.

PiperOrigin-RevId: 396893590
2021-09-15 12:20:51 -07:00
Ghanan GowripalanandgVisor bot 43c75a9b4d Explicitly bind endpoint to a NIC
Previously, any time a datagram-based network endpoint (e.g. UDP) was
bound, the bound NIC is always set based on the bound address (if
specified). However, we should only consider the endpoint bound to
an NIC if a NIC was explicitly bound to.

If an endpoint has been bound to an address and attempts to send packets
to an unconnected remote, the endpoint will default to sending packets
through the bound address' NIC if not explicitly bound to a NIC.

Updates #6565.

PiperOrigin-RevId: 396712415
2021-09-14 16:47:19 -07:00
Ghanan GowripalanandgVisor bot 73e087c6f4 Separate IPv4 ToS & IPv6 TClass in dgram endpoint
Setting the ToS for IPv4 packets (SOL_IP, IP_TOS) should not affect the
Traffic Class of IPv6 packets (SOL_IPV6, IPV6_TCLASS).

Also only return the ToS value XOR Traffic Class as a packet cannot be
both an IPv4 and an IPv6 packet; It is invalid to return both the IPv4
ToS and IPv6 Traffic Class control messages when reading packets.

Updates #6389.

PiperOrigin-RevId: 396399096
2021-09-13 11:15:36 -07:00
Ghanan GowripalanandgVisor bot 129cc47c63 Move socket_test_util to //test/util
...and rename the library to socket_util.

PiperOrigin-RevId: 386348306
2021-07-22 16:37:15 -07:00
Ian LewisandgVisor bot 4492679179 Trigger poll/epoll events on zero-length hostinet sendmsg
Fixes #2726

PiperOrigin-RevId: 380753516
2021-06-22 01:10:24 -07:00
Andrei VaginandgVisor bot ffc6f4bfac test: use std::vector instead of allocating memory with calloc
A memory that is allocated with calloc has to be freed.

PiperOrigin-RevId: 378001409
2021-06-07 14:11:30 -07:00
Andrei VaginandgVisor bot c75a0cf622 hostinet: parse the timeval structure from a SO_TIMESTAMP control message
PiperOrigin-RevId: 370181621
2021-04-23 16:45:03 -07:00
Nayana BidariandgVisor bot f1de34234a Move SO_RCVBUF to socketops.
Fixes #2926, #674

PiperOrigin-RevId: 369457123
2021-04-20 09:32:54 -07:00
Ting-Yu WangandgVisor bot 3984f2238e Make AsSockAddr() to replace reinterpret_cast<sockaddr*>
It's a common pattern in test code to reinterpret_cast<sockaddr*> from
sockaddr_* structs. Make AsSockAddr() for them so code looks better.

Note: Why not a wrapper type for `sockaddr_storage` and etc?
It's also a common need to have a local in-out variable of socklen_t.
Creating a wrapper type may however lead to this wrong code:
  Wrapper addr;
  socklen_t addrlen = sizeof(addr);
where sizeof(Wrapper) may not equal to sizeof(sockaddr_storage).
PiperOrigin-RevId: 368126229
2021-04-12 18:35:09 -07:00
Andrei VaginandgVisor bot 4fda88538c tests: getsockname expects that addrlen will be initialized
PiperOrigin-RevId: 357224877
2021-02-12 10:58:17 -08:00
Zeling FengandgVisor bot 7e952ebe47 Allow UDP sockets connect()ing to port 0
We previously return EINVAL when connecting to port 0, however this is not the
observed behavior on Linux. One of the observable effects after connecting to
port 0 on Linux is that getpeername() will fail with ENOTCONN.

PiperOrigin-RevId: 356413451
2021-02-08 20:13:17 -08:00
Andrei VaginandgVisor bot b3ca382c1c Don't check that msg_flags contains MSG_ERRQUEUE on gvisor platforms.
PiperOrigin-RevId: 350246333
2021-01-05 16:47:39 -08:00
Ayush RanjanandgVisor bot 4bc318854f [netstack] Implement IP(V6)_RECVERR socket option.
PiperOrigin-RevId: 348055514
2020-12-17 11:10:41 -08:00
Bhasker HariharanandgVisor bot 78a5dca134 Fix deadlock in UDP handleControlPacket path.
Fixing the sendto deadlock exposed yet another deadlock where a lock inversion
occurs on the handleControlPacket path where e.mu and demuxer.epsByNIC.mu are
acquired in reverse order from say when RegisterTransportEndpoint is called
in endpoint.Connect().

This fix sidesteps the issue by just making endpoint.state an atomic and gets rid
of the need to acquire e.mu in e.HandleControlPacket.

PiperOrigin-RevId: 344939895
2020-11-30 20:13:26 -08:00
Bhasker HariharanandgVisor bot db5a4fbd34 Fix possible deadlock in UDP.Write().
In UDP endpoint.Write() sendUDP is called with e.mu Rlocked. But if this happens
to send a datagram over loopback which ends up generating an ICMP response of
say ErrNoPortReachable, the handling of the response in HandleControlPacket also
acquires e.mu using RLock. This is mostly fine unless there is a competing
caller trying to acquire e.mu in exclusive mode using Lock(). This will deadlock
as a caller waiting in Lock() disallows an new RLocks() to ensure it can
actually acquire the Lock.

This is documented here https://golang.org/pkg/sync/#RWMutex.

This change releases the endpoint mutex before calling sendUDP to resolve the
possibility of the deadlock.

Reported-by: [email protected]
Reported-by: [email protected]
PiperOrigin-RevId: 342894148
2020-11-17 10:36:29 -08:00
Ghanan GowripalanandgVisor bot 2dd8eb0785 Do not send to the zero port
Port 0 is not meant to identify any remote port so attempting to send
a packet to it should return an error.

PiperOrigin-RevId: 341009528
2020-11-06 01:47:09 -08:00
Bhasker HariharanandgVisor bot 340bbb9e23 Wake up any waiters on an ICMP error on UDP socket.
This change wakes up any waiters when we receive an ICMP port unreachable
control packet on an UDP socket as well as sets waiter.EventErr in
the result returned by Readiness() when e.lastError is not nil.

The latter is required where an epoll()/poll() is done after the error
is already handled since we will never notify again in such cases.

PiperOrigin-RevId: 339370469
2020-10-27 18:13:46 -07:00