191 Commits
Author SHA1 Message Date
Tamir DubersteinandHanzo Dev e479442145 checklocks: guard networking test helpers
The fake clock and networking helpers protect mutable state with mutexes
without exposing those contracts to checklocks. Annotate the clock,
timers, notifications, and helper state, including the existing locked
clock and DAD initialization helpers.

Adapt the helper annotations from #7053 to current source, including
multicast forwarding state added since that proposal.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-21 12:23:26 -04:00
Parth SarthiandgVisor bot c2e9a88ab3 NIC: Added support to return kind with info.
PiperOrigin-RevId: 959288350
2026-08-04 16:25:37 -07:00
Konstantin BogomolovandgVisor bot fb292ca5fc Copy route MTU from net namespace and adjust for network header size
In the original version of this commit
https://github.com/google/gvisor/pull/11857, If r.mtu has been set we skip
calculating the proper MTU in tcpip/stack/route.go:Route.MTU() via endpoint.MTU
and use r.mtu instead. However nothing in the callstack from
tcpip/stack/stack.go:FindRoute to setting r.mtu actually adjusts the size to be
able to hold the endpoint header. Hence we need to adjust it.

Fixes #11856

PiperOrigin-RevId: 852383307
2026-01-05 11:25:45 -08:00
Nayana BidariandgVisor bot 6c7d444961 Return NetworkUnreachable instead of HostUnreachable when routes are not found
Updates #8105

PiperOrigin-RevId: 791875726
2025-08-06 15:52:32 -07:00
Jing ChenandgVisor bot 8045b3337a Implement RTM_DELROUTE in netstack.
PiperOrigin-RevId: 675711612
2024-09-17 14:33:41 -07:00
Andrei VaginandgVisor bot 57a86d3caa netstack: choose route by longest prefix
It has to return the most specific route.

PiperOrigin-RevId: 647421535
2024-06-27 13:12:45 -07:00
gVisor bot d242b5df88 netstack: support mtu in routes
PiperOrigin-RevId: 640919454
2024-06-06 09:16:11 -07:00
Konstantin BogomolovandgVisor bot a3560a6db2 Prevent using zero as NICID.
PiperOrigin-RevId: 629890469
2024-05-01 17:46:34 -07:00
Lucas ManningandgVisor bot 33510ad9a9 Add a method for inspecting assigned addresses.
This new method allows checking for the existence of assigned addresses without
taking an extra reference that needs to be DecRefed. DecRef takes exclusive
locks. Contention on the addressState lock causes performance issues when
multiple goroutines are processing IP packets simultaneously. This isn't the
case today since IP processing is single threaded, but will be eventually.

PiperOrigin-RevId: 623567408
2024-04-10 12:12:23 -07:00
Kevin KrakauerandgVisor bot 507908d9fa netstack: remove PacketBuffer.IsNil()
The change was originally motivated by ticket references (cl/450976957), which
were never implemented.

PiperOrigin-RevId: 617480960
2024-03-20 05:31:48 -07:00
Kevin KrakauerandgVisor bot f4ac73a37b netstack: remove PacketBufferPtr
PacketBufferPtr is leftover from a dead experiment. Just use *PacketBuffer.

PiperOrigin-RevId: 611531815
2024-02-29 11:10:38 -08:00
Kevin KrakauerandgVisor bot 14a0ac9549 netstack: support source hints in routes
PiperOrigin-RevId: 587841841
2023-12-04 14:30:57 -08:00
Kevin KrakauerandgVisor bot 33b8b42525 netstack: add IP source selection test that was rolled back
See cl/557941259 for the original.

PiperOrigin-RevId: 580624680
2023-11-08 12:52:08 -08:00
Andrei Vagin 43bd258fcf Fix a few typos
It is an idea of running codespell as part of our presubmit checks.
Before enabling it for new changes, let's fix what it has found.

Signed-off-by: Andrei Vagin <[email protected]>
2023-10-25 12:13:42 -07:00
Peter JohnstonandgVisor bot 1e2d234636 Prefer routes with local addr on outgoing NIC only for local traffic
Currently gVisor's routing logic always prefers routes that use a local
address that is assigned to the outgoing interface. This preference is
applied even for forwarded traffic, where the local address is left
unspecified on route lookup because the source address of the packet
belongs to some other node as opposed to the stack itself. This means
that when forwarding incoming traffic, the netstack will prefer routes
that go through a NIC with a local address endpoint over those that do
not, even if a better route exists.

Change this logic such that the preference for routes with a local
address assigned to the outgoing interface only applies for locally-
generated traffic.

PiperOrigin-RevId: 573812452
2023-10-16 08:12:10 -07:00
Lucas ManningandgVisor bot 510ed7c3af Automated rollback of changelist 557941259
PiperOrigin-RevId: 566749598
2023-09-19 14:51:59 -07:00
Lucas ManningandgVisor bot aa4e302c75 Automated rollback of changelist 558922801
PiperOrigin-RevId: 566731014
2023-09-19 13:46:56 -07:00
Kevin KrakauerandgVisor bot 64c7809b4d netstack: refactor IPv4 source address selection
This was in the wrong place. This CL just refactors it into package ipv4, just
as IPv6-specific logic is in package ipv6.

PiperOrigin-RevId: 558922801
2023-08-21 15:54:10 -07:00
Kevin KrakauerandgVisor bot e14c91efba netstack: use longest prefix match to choose IPs on a NIC
Once a NIC is chosen for a route, we currently just grab the first IP on the
NIC as the source address. We should choose the address with the longest
matching prefix instead.

This is a simple linear search of the route table. That's slow for large
tables, but large tables are exceedingly rare for use with gVisor and we can
always reimplement this with a trie if it becomes necessary.

PiperOrigin-RevId: 557941259
2023-08-17 14:45:53 -07:00
Kevin KrakauerandgVisor bot 8a2b1df3f6 have gVisor use renamed buffer package
PiperOrigin-RevId: 537209194
2023-06-01 21:27:49 -07:00
Kevin KrakauerandgVisor bot 5cdebeb73d netstack: make tcpip.Address hold a []byte
tcp_benchmark throughput increase 2-3%, but allocations go down (25% in the
download benchmark, only 2% in the upload path).

PiperOrigin-RevId: 532523146
2023-05-16 11:40:49 -07:00
Kevin KrakauerandgVisor bot ef88b30bff netstack: make tcpip.Address an opaque type
The important change here is in tcpip/tcpip.go, where tcpip.Address is defined.
The rest is updating uses of tcpip.Address.

This is preparation for netip.Addr or []byte based addresses, which should save
us a bunch of allocations. Currently, we allocate every time we want to, say,
get a tcpip.Address from a header. This is because the header is a byte slice,
but Address is a string. Strings are immutable, so Go allocates and copies.

PiperOrigin-RevId: 532284732
2023-05-15 18:07:03 -07:00
Ghanan GowripalanandgVisor bot 3ebe155d12 Don't prevent removing loopback in core netstack
Implement this check in netstack integration (in sentry) so that the
core netstack does not prevent an integrator from removing loopback.

PiperOrigin-RevId: 493992707
2022-12-08 14:23:45 -08:00
Bruno Dal BoandgVisor bot 681f42f1b3 Rename ErrNoRoute to ErrHostUnreachable
ErrNoRoute gets translated to EHOSTUNREACH which causes some code paths to
produce different errors unexpectedly. Rename the error so we can clean up some
sites to return ENETUNREACH more clearly where needed.

Updates #8105

PiperOrigin-RevId: 482355099
2022-10-19 18:29:13 -07:00
Kevin KrakauerandgVisor bot fedffe9ca6 netstack: introduce PacketBufferPtr
PiperOrigin-RevId: 479437464
2022-10-06 16:16:54 -07:00
Bruno Dal BoandgVisor bot 6e9aca5d76 Use monotonic time for neighbor entries
PiperOrigin-RevId: 470832047
2022-08-29 15:41:41 -07:00
Lucas ManningandgVisor bot 57f173fcec Refactor netstack to use bufferv2 instead of buffer.
This change has significant performance implications. bufferv2 is reference
counted and pooled, which alleviates heap/GC pressure. Below are the results
from running the iperf benchmark.

HEAD:
BenchmarkIperf/operation.Upload-16    1552  ns/op   46.6GiB total allocations
BenchmarkIperf/operation.Download-16  1114  ns/op   68.6GiB total allocations

w/ change:
BenchmarkIperf/operation.Upload-16    1139  ns/op (-27%)   1.41GiB total allocations (-97%)
BenchmarkIperf/operation.Download-16  753.2 ns/op (-33%)   706MiB  total allocations (-99%)

PiperOrigin-RevId: 462453185
2022-07-21 13:09:31 -07:00
Tony GongandgVisor bot 9e6140d108 Introduce and Implement AddressDispatcher
Introduce the AddressDispatcher interface which integrators can
provide an implementation at the time of adding an address
to receive callbacks when address properties change and when the
address is removed. Modify `NDPDispatcher`'s callback when a SLAAC
address is added to receive an implementation of `AddressDispatcher`.

Added informational preferred and valid lifetime fields to
`AddressProperties` so they can be set when adding the address; and a
way to update said lifetimes.

Added a means to disable an `AddressableEndpointState` and each
individual `addressState`, so that the `AddressDisabled` assignment
state can be reported to integrators.

Added a configurable option to `AddressableEndpointState` which
determines whether addresses of kind `PermanentDisabled` are included
in the return value of `PrimaryAddresses` and `PermanentAddresses`. This
option is set such that IPv4 addresses are returned, while IPv6
addresses are hidden, when the NIC is disabled. This is a change in
behavior for IPv6, but is consistent with behavior on Linux.

Modified tests in `ndp_test` to use the new AddressDispatcher. Fixed
some bugs along the way.

PiperOrigin-RevId: 459658009
2022-07-07 19:58:25 -07:00
Nate HurleyandgVisor bot b63c4b3348 Enable multicast forwarding per protocol.
After this change, multicast packets will only be forwarded if
multicast forwarding is enabled on the relevant NIC AND it is
enabled for the relevant protocol (IPv4 or IPv6).

Multicast forwarding at the protocol level will be tied to the lifetime
of the IPv4/IPv6RoutingTableControllers. In particular, this change
enables us to satisfy the following requirements:

https://cs.opensource.google/fuchsia/fuchsia/+/main:sdk/fidl/fuchsia.net.multicast.admin/ipv4.fidl;l=20-21;drc=acd6519f50c92e30f18cc1816bd1c400759b533c

Updates #7338.

PiperOrigin-RevId: 455155793
2022-06-15 10:00:49 -07:00
Lucas ManningandgVisor bot c8207270b3 Remove VectorisedView everywhere.
PiperOrigin-RevId: 453471156
2022-06-07 10:36:23 -07:00
Lucas ManningandgVisor bot 4b6229c992 Replace VectorisedView in network endpoints.
PiperOrigin-RevId: 451267391
2022-05-26 16:25:30 -07:00
Nate HurleyandgVisor bot ae264d7836 Implement GetMulticastRouteLastUsedTime.
Updates #7338.

PiperOrigin-RevId: 451172827
2022-05-26 08:59:42 -07:00
Nate HurleyandgVisor bot 39a0834a5e Implement remove multicast route.
Updates #7338.

PiperOrigin-RevId: 451162886
2022-05-26 08:06:06 -07:00
Nate HurleyandgVisor bot fbdfc990c9 Implement IPv4 multicast forwarding.
This change implements AddMulticastRoute and the requisite routing logic.
Subsequent changes will still be needed to:

1. Emit events for missing route or unexpected input interface
2. Implement DelRoute
3. Implement GetRouteStats

Updates #7338.

PiperOrigin-RevId: 451026594
2022-05-25 15:18:13 -07:00
Nate HurleyandgVisor bot 49812a53f9 Add option to enable multicast forwarding
Setting this newly added option to "true" will currently be a no-op. Full
support for multicast routing will be added in subsequent CLs.

Updates #7338.

PiperOrigin-RevId: 439356271
2022-04-04 10:48:26 -07:00
bemascandgVisor bot 08140fb5ad Add reference counting for packets in channel.Endpoint
This reduces GC pressure when using a TUN device or similar link.

Updates #6910

PiperOrigin-RevId: 431441991
2022-02-28 08:33:36 -08:00
Ghanan GowripalanandgVisor bot 3721f70dff Return previous config when setting NIC forwarding
...so that callers can determine if forwarding changed as a consequence
of their call.

PiperOrigin-RevId: 431069840
2022-02-25 18:05:40 -08:00
Tony GongandgVisor bot 04c8729e32 Expose address deprecation on Stack
Add a method for setting an address to be deprecated/preferred to
AddressableEndpoint, so that addresses added from outside the stack
can be deprecated/renewed respectively, e.g. by a DHCPv6 client.

PiperOrigin-RevId: 426435598
2022-02-04 10:29:52 -08:00
Tony GongandgVisor bot 788386f4ac Add temporary address property
Remove the AddressConfigSlaacTemp address configuration type
and add a boolean representing temporality. This enables support
for statically-configured temporary addresses (such as an address
configured by an out-of-stack DHCPv6 client). Like temporary addresses
generated via SLAAC, such addresses are preferred over addresses that
are not temporary during IPv6 source address selection.

Added a test case to the IPv6 source address selection tests which
asserts that an address with the static temporary config type is
preferred over a non-temporary address.

PiperOrigin-RevId: 426301838
2022-02-03 20:21:14 -08:00
Ghanan GowripalanandgVisor bot 2f180880c2 Don't pass route info and net proto to write fns
...as the packet buffer already holds that information.

Updates #3810.
Fixes #6537.

PiperOrigin-RevId: 421898143
2022-01-14 13:23:22 -08:00
Tamir DubersteinandgVisor bot 40aef5d553 Delete channel.PacketInfo
Updates #6537.

PiperOrigin-RevId: 417507096
2021-12-20 17:16:37 -08:00
Ghanan GowripalanandgVisor bot a2af8a5cf6 Always parse Transport headers
..including ICMP headers before delivering them to the
TransportDispatcher.

Updates #3810.

PiperOrigin-RevId: 404404002
2021-10-19 17:25:55 -07:00
Ayush RanjanandgVisor bot 469a4a5203 Prevent PacketData from being modified.
PacketData should not be modified and should be treated readonly because it
represents packet payload. The old DeleteFront method allowed callers to modify
the underlying buffer which should not be allowed.

Added a way to consume from the PacketData instead of deleting from it.
Updated call points to use that instead.

Reported-by: [email protected]
PiperOrigin-RevId: 399268473
2021-09-27 13:35:22 -07:00
Tony GongandgVisor bot 31f5add4b4 Pass address properties in a single struct
Replaced the current AddAddressWithOptions method with
AddAddressWithProperties which passes all address properties in
a single AddressProperties type. More properties that need to be
configured in the future are expected, so adding a type makes adding
them easier.

PiperOrigin-RevId: 396930729
2021-09-15 15:00:01 -07:00
Zeling FengandgVisor bot ccf06b3451 Support RTM_DELLINK
This change will allow us to remove the default link in a packetimpact test so
we can reduce indeterministic behaviors as required in https://fxbug.dev/78430.
This will also help with testing #1388.

Updates #578, #1388.

PiperOrigin-RevId: 387896847
2021-07-30 15:41:36 -07:00
Tamir DubersteinandgVisor bot beb1320c52 Use fake clocks in all tests
...except TCP tests and NDP tests that mutate globals. These will be
undertaken later.

Updates #5940.

PiperOrigin-RevId: 376145608
2021-05-27 05:06:04 -07:00
Tamir DubersteinandgVisor bot 40a19e38c1 Use opaque types to represent time
Introduce tcpip.MonotonicTime; replace int64 in tcpip.Clock method
returns with time.Time and MonotonicTime to improve type safety and
ensure that monotonic clock readings are never compared to wall clock
readings.

PiperOrigin-RevId: 375775907
2021-05-25 13:00:29 -07:00
Tamir DubersteinandgVisor bot 268f4262a4 Remove detritus
- Unused constants
- Unused functions
- Unused arguments
- Unkeyed literals
- Unnecessary conversions

PiperOrigin-RevId: 375253464
2021-05-22 05:49:34 -07:00
Arthur SfezandgVisor bot 2add8baf49 Add aggregated NIC stats
This change also includes miscellaneous improvements:
* UnknownProtocolRcvdPackets has been separated into two stats, to
specify at which layer the unknown protocol was found (L3 or L4)
* MalformedRcvdPacket is not aggregated across every endpoint anymore.
Doing it this way did not add useful information, and it was also error-prone
(example: ipv6 forgot to increment this aggregated stat, it only
incremented its own ipv6.MalformedPacketsReceived). It is now only incremented
 the NIC.
* Removed TestStatsString test which was outdated and had no real
utility.

PiperOrigin-RevId: 375057472
2021-05-21 04:27:41 -07:00
Ghanan GowripalanandgVisor bot a1d4a6a23d Control forwarding per NetworkEndpoint
...instead of per NetworkProtocol to better conform with linux
(https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt):

```
conf/interface/*

forwarding - BOOLEAN
	Enable IP forwarding on this interface.  This controls whether packets
	received _on_ this interface can be forwarded.
```

Fixes #5932.

PiperOrigin-RevId: 373888000
2021-05-14 16:32:16 -07:00