60 Commits
Author SHA1 Message Date
Shayon MukherjeeandgVisor bot f9d6366dfa hostinet: support checkpoint/restore
Host networking did not have checkpoint/restore support. A sandbox running with `--network=host` could not be checkpointed because `hostinet.Stack` was not savable, and restore did not define what should happen to open host sockets.

This change makes the hostinet stack savable and defines that restore contract. Host socket fds cannot cross a checkpoint boundary, so `Socket.fd` is tagged `state:"nosave"` and save does not touch host sockets. With checkpoint `--leave-running`, the original sandbox keeps serving traffic on its existing connections. If save fails, the sandbox is left unharmed.

Without `--leave-running`, the sandbox exits after save and the host kernel closes its sockets. That sends `FIN` or `RST` so remote peers do not hang on half-open connections.

In the restored sandbox, `afterLoad` sets each socket fd to `-1`. Host syscalls then fail with `EBADF`. `Readiness` reports hangup and error so `epoll` waiters wake immediately, and tasks that were blocked in I/O see `EBADF` when their syscall restarts. `State()` reports no TCP state for these sockets rather than logging a warning on every `/proc/net/tcp` read. Applications are expected to reconnect after restore.

The stack's host-derived state is not saved. This includes `/proc/net/dev`, `/proc/net/snmp`, TCP buffer sizes, and allowed socket types. During restore, runsc configures a fresh hostinet stack before seccomp filters are installed. `ReplaceConfig` then copies the destination host configuration into the deserialized stack and transfers ownership of the fresh proc net handles. The restored sandbox therefore uses the destination host configuration and limits.

A hostinet checkpoint contains no netstack state and a netstack checkpoint contains no hostinet state, so the checkpoint now records the network type in its metadata. Restore fails with a clear error when the network stack kind differs between checkpoint and restore, instead of panicking inside `ReplaceConfig` during kernel load. Sandbox and none networking both use netstack and remain interchangeable. Checkpoints taken before the network type was recorded skip this check.

The hostinet `save_resume` syscall tests are enabled because save does not touch host sockets. The save and restore syscall tests remain excluded because they expect sockets to keep working after restore.

Container tests cover restored socket behavior for TCP, UDP, `epoll`, and a blocked accept. They also cover remote peer close after checkpoint, continued service after checkpoint `--leave-running`, and the error when restoring a host networking checkpoint with sandbox networking. The focused hostinet unit tests and the hostinet `save_resume` syscall tests pass on `x86_64`. The checkpoint/restore user guide documents the restored socket behavior.

Fixes #6243

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/gvisor/pull/13404 from shayonj:hostinet-save-restore-v2 734480ee348d774346ec024b6483b89ea0a91adb
PiperOrigin-RevId: 950854464
2026-07-20 08:29:43 -07:00
Nayana BidariandgVisor bot 3846731234 Refactor network configuration during restore and defer link creation in runsc
This change refactors the network configuration flow during sandbox restore
and defers the creation of network links and routes in runsc to run after
seccomp filters are installed.

- Replaced ReplaceConfig with ResetConfig in inet.Stack. Introduced
inet.NetworkArgs to configure the stack during restore, updating k.LoadFrom
to use it.
- Renamed CreateLinksAndRoutes URPC to SetNetworkArgs, which now configures
host sockets before seccomp is installed, deferring link/route creation to run
after seccomp.
- Fixed S/R support by saving and restoring limit/burst state.
- Minor seccomp filter adjustments and test updates.

PiperOrigin-RevId: 945995430
2026-07-10 18:52:31 -07:00
Nayana BidariandgVisor bot 7e09cf7be6 Automated rollback of changelist 941330876
PiperOrigin-RevId: 944161934
2026-07-07 16:47:40 -07:00
Nayana BidariandgVisor bot 997db57cd5 Refactor setting up network during restore.
Previously, during sandbox restore the network configuration was restored by
creating a new network stack (which has all the config scraped from the host)
and the replacing this new config such as NICs, routes and IP addresses from
the new stack to the loaded/restored stack.

This change refactors the process by introducing a new `inet.NetworkArgs`
interface that abstracts how the network stack is configured. The `Loader` now
implements this interface and configures the loaded stack directly using the
configuration scraped from the host during restore. `Kernel.LoadFrom` accepts
this interface (instead of stack), allowing it to configure the restored stack
using either the Loader's configuration.

PiperOrigin-RevId: 941330876
2026-07-01 15:37:56 -07:00
Jing ChenandgVisor bot 5d3cd9b7bd Implement RTM_NEWROUTE and RTM_DELROUTE for hostinet.
assisted-by: jetski
PiperOrigin-RevId: 922661202
2026-05-28 03:13:29 -07:00
gVisor bot fda893428f Merge pull request #12949 from tanyifeng:net-interface-order
PiperOrigin-RevId: 904671911
2026-04-23 15:44:00 -07:00
Tan Yifeng e2bd312913 net: Ensure deterministic interface enumeration order
Go map iteration randomness causes network interface enumeration
(getifaddrs, RTM_GETADDR, RTM_GETLINK, /proc/net/*, SIOCGIFCONF)
to return nondeterministic order, breaking applications like
asciidoctor that expect stable ordering across repeated calls.

Since gVisor allocates NIC IDs monotonically (like Linux ifindex),
sorting by NICID is equivalent to registration order. Add
InterfaceIDs() to inet.Stack so all consumer paths iterate in
deterministic ascending-ID order without ad-hoc sorting at each
call site.

Signed-off-by: Tan Yifeng <[email protected]>
2026-04-23 10:16:47 +08:00
Nayana BidariandgVisor bot 1e6759d3d9 Remove usages of save-restore-netstack flag.
This flag has been true by default for a while, remove its usages and
deprecate the flag.

PiperOrigin-RevId: 903909652
2026-04-22 10:15:08 -07:00
zsx 51ac12d858 Fix metrics collection failure in network=host mode
When using hostinet (network=host), /proc/net/dev and /proc/net/snmp
may not be available inside the sandbox. Previously Statistics() returned
an error when these files were not opened, which propagated up and caused
the entire event/metrics collection to fail, breaking kubectl top pod.

Return zero-valued stats instead of an error when the proc files are
unavailable, as this is expected in hostinet mode. Real errors like
parse failures still propagate.

Fixes #12723
2026-04-07 14:56:17 +08:00
Parth SarthiandgVisor bot 9192957ac5 hostinet: Replacing Seek with ReadAt.
Using ReadAt will use `pread` instead of calling `lseek`; issue:
  https://github.com/tailscale/tailscale/issues/16966

PiperOrigin-RevId: 871511165
2026-02-17 14:58:39 -08:00
Nayana BidariandgVisor bot a41a6099f9 Terminate connections only for restore (not for resume).
Previously, external connections were terminated during both restore and
resume operations, causing a reconnection after every resume. By limiting
termination to the restore only, connections now remain active during resumes
as intended.

Added tests to verify that resume does not terminate the connection.

PiperOrigin-RevId: 868830577
2026-02-12 14:35:26 -08:00
Shailend ChandandgVisor bot b30c659b42 Add RTNLGRP_LINK netlink multicast support for recv
Only netstack supports sending link events, requests to join multicast
groups for netlink sockets continue to be denied when hostinet is in use.

connect() and sendmsg() to RTMGRP_LINK still fails.

PiperOrigin-RevId: 831688843
2025-11-12 22:04:10 -08:00
Nayana BidariandgVisor bot c52821b3e0 Remove old NICs and routes during save/restore.
During restore there will be new NICs and routes created based on the network
config. Close these old NICs and routes in beforeSave() only for save/restore.
For save/resume, do not close them.

As new link endpoints are created during restore, there is no need to start
the associated old processor goroutines in afterLoad. Start them only for
save/resume.

PiperOrigin-RevId: 804994281
2025-09-09 11:33:49 -07:00
Nayana BidariandgVisor bot cbd03df7b0 Changes to support netstack save restore.
- Added a new Stats() method in inet.Stack to get the saved stats
during restore.
- Mark stack.nic, tcpip.Route and stack.addressState structs as "nosave".
These fields should not be saved because the IP addresses and routes can
change during restore and new configuration of routes and IP addresses will be
extracted from the restore spec and initialized in the saved stack.
- Changes in Restore() method in icmp, udp, tcp, packet and raw endpoint files
to support save restore of these endpoints. These changes are flag guarded by
the TESTONLY-save-restore-netstack flag.

PiperOrigin-RevId: 707639274
2024-12-18 12:52:22 -08:00
Nayana BidariandgVisor bot 1bc1bb3b81 Restore listening connections when netstack s/r is enabled.
This CL restores the listening connections when netstack s/r is enabled.
The changes include:
- New method as a workaround to replace the new routes and nics to the loaded
stack after restore.
- New Restore() for transport layer protocols to restore the protocol level
background workers.
- Adds afterLoad() method for fdbased processors.
- Adds a test to verify listening connection is restored after checkpointing
with netstack s/r enabled.
- Few other changes to save restore fields to enable netstack s/r.

PiperOrigin-RevId: 698453124
2024-11-20 11:13:57 -08:00
Koichi Shiraishi 5677ce4285 all: remove use io/ioutil deprecated package & fix some deprecated thing
Signed-off-by: Koichi Shiraishi <[email protected]>
2024-10-10 20:36:24 +09:00
Jing ChenandgVisor bot 8045b3337a Implement RTM_DELROUTE in netstack.
PiperOrigin-RevId: 675711612
2024-09-17 14:33:41 -07:00
Nayana BidariandgVisor bot 2836a4a651 Mark netstack as save and use it only in tests
- Adds a new flag which will enable netstack s/r. When the flag is not enabled,
there is no change in the existing behavior. The flag will be enabled only in
tests to verify the s/r functionality of netstack.
- Some additional fields in netstack were causing panic when netstack is
save/restored. Such fields are marked as 'save'/'nosave' accordingly to resolve
the panic.

PiperOrigin-RevId: 668566657
2024-08-28 12:49:43 -07:00
Jing ChenandgVisor bot ee22eb3a99 Implement RTM_NEWROUTE in netstack to create/replace a route.
PiperOrigin-RevId: 650814137
2024-07-09 18:04:19 -07:00
Andrei VaginandgVisor bot 4826035472 Pass NEWLINK netlink commands to network stacks
The NEWLINK commands contains many properties and here is no reason to have
another abstract interface between the Sentry and network stacks.

PiperOrigin-RevId: 627916629
2024-04-24 18:42:44 -07:00
Kevin KrakauerandgVisor bot c4ae01e52e netstack: remove GRO from ingress flow
GRO is getting moved and updated. This removes it in preparation for a
follow-up CL.

PiperOrigin-RevId: 621984030
2024-04-04 15:17:08 -07:00
Nayana BidariandgVisor bot 2b1b92635f Add Resume() method in netstack.
Resume method resumes the endpoints after save which is required for
save/resume. This method resumes the endpoint states which were frozen during
save.

PiperOrigin-RevId: 615467468
2024-03-13 10:36:29 -07:00
Nayana BidariandgVisor bot 54b0ba2706 Rename Resume() to Restore() in netstack.
The existing Resume method in netstack is doing the work of Restore. This
method does not resume endpoints as the resumable endpoints are only stored
during the Restore of the sandbox, rename the method appropriately.

PiperOrigin-RevId: 613320887
2024-03-06 13:25:31 -08:00
Nicolas LacasseandgVisor bot df930b374f Enable socket_stress_test with hostinet.
The min/max port ranges were broken.

PiperOrigin-RevId: 526137330
2023-04-21 14:35:51 -07:00
Nicolas LacasseandgVisor bot c07dc2673f Implement hostinet.Stack methods that manipulate interfaces and addresses.
And enable all of the netlink tests that depend on this functionality.

Specifially, this CL implements hostinet.stack:
* .RemoveInterface()
* .AddInterfaceAddr()
* .RemoveInterfaceAddr()

Applications use netlink messages to call into these methods. For hostinet, we
use netlink messages to the host to implement these methods.

Note that the netlink messages are not simple passed from the application to
the host -- they are parsed first and only carefully crafted messages are sent
to the host.

This CL required a lot of refactoring to the existing netlink code in the
hostinet package, as well as a bunch of new functionality. All of that code has
been collected in hostinet/netlink.go

I also changed how we handle CAP_NET_ADMIN and CAP_NET_RAW in tests, since we
must drop those capabilities inside the sandbox if we don't have them on the
host. The new solution is cleaner and does not rely on google-specific env
variables.

PiperOrigin-RevId: 522685544
2023-04-07 14:33:21 -07:00
Nicolas LacasseandgVisor bot e4f4a8bc8a Make hostinet get the interfaces/addr/routes dynamicallys.
Previously we were initializing these at startup and they could never change.
Now they are read dynamically every time they are requested. This is a
pre-requisite for implementing methods that mutate the interfaces
(stack.AddInterface()).

PiperOrigin-RevId: 522109098
2023-04-05 11:35:39 -07:00
Nicolas LacasseandgVisor bot 92d0427c98 Clean up netlink code in hostinet.
No functional changes. Refactored some methods into smaller chunks for easy
reuse. Got rid of some needlessly exported methods.

PiperOrigin-RevId: 520748526
2023-03-30 14:51:24 -07:00
Nicolas LacasseandgVisor bot 704b8d9ee8 hostinet: Support ping and raw sockets.
PiperOrigin-RevId: 513902015
2023-03-04 00:40:01 -08:00
Kevin KrakauerandgVisor bot 60b0f76f7f netstack: add gro_flush_timeout
Makes a per-interface file available to configure the GRO timeout, e.g.
/sys/class/net/eth0/gro_flush_timeout

PiperOrigin-RevId: 487082821
2022-11-08 16:34:55 -08:00
Kevin KrakauerandgVisor bot 3a76efe831 convert uses of interface{} to any
Done via:
  find . -name "*.go" | xargs sed -i -E 's/interface\{\}/any/g'

PiperOrigin-RevId: 487033228
2022-11-08 13:14:06 -08:00
Andrei VaginandgVisor bot 89c8e320fc Don't leak network namespaces
PiperOrigin-RevId: 454707336
2022-06-13 15:05:21 -07:00
Bhasker HariharanandgVisor bot b3a5701e19 Remove TCP endpoint goroutines.
This change removes all endpoint goroutines and all TCP processing is now done
inline in the TCP processor loop. TCP timers directly invoke handlers as
required rather than assert a waker.

UnlockUser is also simplified to just queue the endpoint to the processor
instead of trying to process segments inline. This allows us to centralize logic
for TCP state handling in the processor. This potentially could involve an extra
wakeup but now that endpoint goroutines do not exist this is not such a big
concern as in case of busy servers the processor goroutines will already be
running anyway.

This change also allows us to clean up S/R as now restoring a TCP endpoint does
not require restarting a goroutine and moving it to the right logical point but
only requires that we restart any timers that may have been running when the
save was done and restore any port bindings as required.

Endpoint.Release is now removed in favor of Endpoint.Abort by using Abort in
places where we use Endpoint.Release.

Updates #231

PiperOrigin-RevId: 442673015
2022-04-18 17:35:36 -07:00
Bhasker HariharanandgVisor bot 4664e71ebb Support custom socket options in hostinet.
PiperOrigin-RevId: 416625574
2021-12-15 12:51:54 -08:00
Ayush RanjanandgVisor bot c7f074fc3b Make {Un}Marshal{Bytes/Unsafe} return remaining buffer.
Change marshal.Marshallable method signatures to return the remaining buffer.
This makes it easier to implement these method manually. Without this, we would
have to manually do buffer shifting which is error prone.

tools/go_marshal/test:benchmark test does not show change in performance.
Additionally fixed some marshalling bugs in fsimpl/fuse.

Updated multiple callpoints to get rid of redundant slice indexing work and
simplified code using this new signature.

Updates #6450

PiperOrigin-RevId: 407857019
2021-11-05 10:43:49 -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
Zach KoopmansandgVisor bot 895c2eaa09 [syserror] Update syserror to linuxerr for EACCES, EBADF, and EPERM.
Update all instances of the above errors to the faster linuxerr implementation.
With the temporary linuxerr.Equals(), no logical changes are made.

PiperOrigin-RevId: 382306655
2021-06-30 08:18:59 -07:00
Ghanan GowripalanandgVisor bot c41e331204 Don't read forwarding from netstack in sentry
https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt:
  /proc/sys/net/ipv4/* Variables:

  ip_forward - BOOLEAN
    0 - disabled (default)
    not 0 - enabled

    Forward Packets between interfaces.

    This variable is special, its change resets all configuration
    parameters to their default state (RFC1122 for hosts, RFC1812
    for routers)

/proc/sys/net/ipv4/ip_forward only does work when its value is changed
and always returns the last written value. The last written value may
not reflect the current state of the netstack (e.g. when `ip_forward`
was written a value of "1" then disable forwarding on an interface)
so there is no need for sentry to probe netstack to get the current
forwarding state of interfaces.

```
~$ cat /proc/sys/net/ipv4/ip_forward
0
~$ sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
~$ cat /proc/sys/net/ipv4/ip_forward
1
~$ sudo sysctl -a | grep ipv4 | grep forward
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.eno1.forwarding = 1
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.wlp1s0.forwarding = 1
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
~$ sudo sysctl -w net.ipv4.conf.wlp1s0.forwarding=0
net.ipv4.conf.wlp1s0.forwarding = 0
~$ sudo sysctl -a | grep ipv4 | grep forward
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.eno1.forwarding = 1
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.wlp1s0.forwarding = 0
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
~$ cat /proc/sys/net/ipv4/ip_forward
1
~$ sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
~$ sudo sysctl -a | grep ipv4 | grep forward
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.eno1.forwarding = 1
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.wlp1s0.forwarding = 0
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
~$ sudo bash -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
~$ sudo sysctl -a | grep ipv4 | grep forward
sysctl: unable to open directory "/proc/sys/fs/binfmt_misc/"
net.ipv4.conf.all.forwarding = 0
net.ipv4.conf.default.forwarding = 0
net.ipv4.conf.eno1.forwarding = 0
net.ipv4.conf.lo.forwarding = 0
net.ipv4.conf.wlp1s0.forwarding = 0
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
~$ cat /proc/sys/net/ipv4/ip_forward
0
```

In the above example we can see that writing "1" to
/proc/sys/net/ipv4/ip_forward configures the stack to be a router (all
interfaces are configured to enable forwarding). However, if we manually
update an interace (`wlp1s0`) to not forward packets,
/proc/sys/net/ipv4/ip_forward continues to return the last written value
of "1", even though not all interfaces will forward packets.

Also note that writing the same value twice has no effect; work is
performed iff the value changes.

This change also removes the 'unset' state from sentry's ip forwarding
data structures as an 'unset' ip forwarding value is the same as leaving
forwarding disabled as the stack is always brought up with forwarding
initially disabled; disabling forwarding on a newly created stack is a
no-op.

PiperOrigin-RevId: 373853106
2021-05-14 13:22:58 -07:00
Rahat MahmoodandgVisor bot 3246444f6f Remove uses of the binary package from networking code.
Co-Author: ayushranjan
PiperOrigin-RevId: 370785009
2021-04-27 16:20:01 -07:00
Zach KoopmansandgVisor bot d8f34027b3 [syserror] Split usermem package
Split usermem package to help remove syserror dependency in go_marshal.
New hostarch package contains code not dependent on syserror.

PiperOrigin-RevId: 365651233
2021-03-29 13:30:21 -07:00
Kevin KrakauerandgVisor bot a01f5186c1 Implement /proc/sys/net/ipv4/ip_local_port_range
Speeds up the socket stress tests by a couple orders of magnitude.

PiperOrigin-RevId: 361721050
2021-03-08 20:40:34 -08:00
Ayush RanjanandgVisor bot c3ff9f93bc [op] Replace syscall package usage with golang.org/x/sys/unix in pkg/.
The syscall package has been deprecated in favor of golang.org/x/sys.

Note that syscall is still used in the following places:
- pkg/sentry/socket/hostinet/stack.go: some netlink related functionalities
  are not yet available in golang.org/x/sys.
- syscall.Stat_t is still used in some places because os.FileInfo.Sys() still
  returns it and not unix.Stat_t.

Updates #214

PiperOrigin-RevId: 360701387
2021-03-03 10:25:58 -08:00
Ian LewisandgVisor bot 675637c514 Add basic address deletion to netlink
Updates #3921

PiperOrigin-RevId: 339195417
2020-10-27 00:18:10 -07:00
Ting-Yu WangandgVisor bot d99406cbd8 Fix nogo tests in //pkg/sentry/socket/...
PiperOrigin-RevId: 338784921
2020-10-23 19:24:09 -07:00
gVisor bot 68f426dd8f Merge pull request #3651 from ianlewis:ip-forwarding
PiperOrigin-RevId: 332760843
2020-09-20 18:17:20 -07:00
Ian Lewis 2029379971 Merge branch 'master' into ip-forwarding
- Merges aleksej-paschenko's with HEAD
- Adds vfs2 support for ip_forward
2020-08-17 21:44:31 -04:00
Nayana BidariandgVisor bot 7e262bf6b6 Add loss recovery option for TCP.
/proc/sys/net/ipv4/tcp_recovery is used to enable RACK loss
recovery in TCP.

PiperOrigin-RevId: 325157807
2020-08-05 20:50:06 -07:00
Ting-Yu WangandgVisor bot e18fd8348d Support RTM_NEWADDR and RTM_GETLINK in (rt)netlink.
PiperOrigin-RevId: 293271055
2020-02-04 18:05:03 -08:00
Adin ScannellandgVisor bot 4368058004 Update package locations.
Because the abi will depend on the core types for marshalling (usermem,
context, safemem, safecopy), these need to be flattened from the sentry
directory. These packages contain no sentry-specific details.

PiperOrigin-RevId: 291811289
2020-01-27 15:31:32 -08:00
Ian GudgerandgVisor bot 6adde009e1 Add endpoint tracking to the stack.
In the future this will replace DanglingEndpoints. DanglingEndpoints must be
kept for now due to issues with save/restore.

This is arguably a cleaner design and allows the stack to know which transport
endpoints might still be using its link endpoints.

Updates #837

PiperOrigin-RevId: 277386633
2019-10-29 16:14:51 -07:00
aleksej 1b66c3ed52 Add /proc/sys/net/ipv4/ip_forward 2019-10-27 15:28:15 +03:00