Skip to content

netstack: remove only the first matching route on RTM_DELROUTE - #15301

Merged
copybara-service[bot] merged 1 commit into
gvisor:masterfrom
adi-IL:bot/gvisor-15295
Oct 6, 2026
Merged

copybara-service[bot] merged 1 commit into
gvisor:masterfrom
adi-IL:bot/gvisor-15295

Conversation

@adi-IL

@adi-IL adi-IL commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15295

netstack.Stack.RemoveRoute passes a match function to tcpip.Stack.RemoveRoutes, which removes every route that matches. One RTM_DELROUTE request can therefore delete several routes. Linux deletes only the first matching route per request and returns ESRCH when nothing matches.

This change adds a found flag to the match function in RemoveRoute. After the first match, the function returns false for every later route. tcpip.Stack.RemoveRoutes does not change.

The first match is the oldest matching route in the route table. Linux sorts routes with the same prefix by metric, so its first match is the route with the lowest metric. gVisor cannot do that yet. tcpip.Route has no metric field and localRoute ignores RTA_PRIORITY. Metric ordering is out of scope for this PR.

Testing:

  • New TestRemoveRouteRemovesFirstMatchOnly in pkg/sentry/socket/netstack/stack_test.go. It adds two 10.0.0.0/24 routes on NIC 1 and NIC 2 and sends RTM_DELROUTE three times. The first request leaves only the NIC 2 route, the second leaves an empty table, and the third returns ESRCH. The test uses NICs to tell the routes apart because the routes carry no metric. Without the stack.go change it fails with route table after first RemoveRoute = [], want only the NIC 2 route.
  • The netstack_test target in BUILD gets the deps the new test needs.
  • //pkg/sentry/socket/netstack:netstack_test and //pkg/tcpip/stack:stack_test pass locally under bazel.
  • test/syscalls/linux/socket_netlink_route.cc gains NetlinkRouteTest.DeleteRouteRemovesFirstMatchOnly. It adds 10.0.0.0/24 on two interfaces and deletes three times with no output interface. The route count goes from 2 to 1 to 0, and the third delete returns ESRCH.
  • test/rtnetlink/linux/route_test.sh runs the same sequence with ip route del.
  • Local bazel runs of //test/syscalls:socket_netlink_route_test_runsc_systrap_directfs (71 tests, including the new one) and //test/rtnetlink:route_test_runsc_systrap_overlay_directfs pass with the fix. Both fail without the stack.go change. The same tests also pass on native Linux. ptrace runsc variants were not run.

@milantracy milantracy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add test at test/syscalls/linux/socket_netlink_route.cc and test/rtnetlink/linux/route_test.sh

please also let others know that you are interested working on an issue via commenting on the issue etc, otherwise, there might be some conflicts

@adi-IL adi-IL left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add tests in test/syscalls/linux/socket_netlink_route.cc and test/rtnetlink/linux/route_test.sh, and I left a note on #15295.

@adi-IL

adi-IL commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Added the tests in 178e872.

  • test/syscalls/linux/socket_netlink_route.cc has a new NetlinkRouteTest.DeleteRouteRemovesFirstMatchOnly. It adds 10.0.0.0/24 on two veth links and sends RTM_DELROUTE with no output interface, so both routes match. The route count goes from 2 to 1 to 0, and a third delete returns ESRCH.
  • test/rtnetlink/linux/route_test.sh runs the same sequence with ip route del on eth0 and eth1.

Both tests fail without the stack.go change and pass with it. They also pass on native Linux.

@milantracy milantracy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please squash 2 commits into 1

@adi-IL

adi-IL commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Squashed into one commit, 7577af3. The tree is the same as 178e872, so the code and tests did not change.

@milantracy milantracy added the ready to pull Causes the PR to be imported into Google. Will be removed soon. label Oct 6, 2026
copybara-service Bot pushed a commit that referenced this pull request Oct 6, 2026
Fixes #15295

`netstack.Stack.RemoveRoute` passes a match function to `tcpip.Stack.RemoveRoutes`, which removes every route that matches. One RTM_DELROUTE request can therefore delete several routes. Linux deletes only the first matching route per request and returns ESRCH when nothing matches.

This change adds a `found` flag to the match function in `RemoveRoute`. After the first match, the function returns false for every later route. `tcpip.Stack.RemoveRoutes` does not change.

The first match is the oldest matching route in the route table. Linux sorts routes with the same prefix by metric, so its first match is the route with the lowest metric. gVisor cannot do that yet. `tcpip.Route` has no metric field and `localRoute` ignores RTA_PRIORITY. Metric ordering is out of scope for this PR.

Testing:
- New `TestRemoveRouteRemovesFirstMatchOnly` in `pkg/sentry/socket/netstack/stack_test.go`. It adds two 10.0.0.0/24 routes on NIC 1 and NIC 2 and sends RTM_DELROUTE three times. The first request leaves only the NIC 2 route, the second leaves an empty table, and the third returns ESRCH. The test uses NICs to tell the routes apart because the routes carry no metric. Without the `stack.go` change it fails with `route table after first RemoveRoute = [], want only the NIC 2 route`.
- The `netstack_test` target in `BUILD` gets the deps the new test needs.
- `//pkg/sentry/socket/netstack:netstack_test` and `//pkg/tcpip/stack:stack_test` pass locally under bazel.
- `test/syscalls/linux/socket_netlink_route.cc` gains `NetlinkRouteTest.DeleteRouteRemovesFirstMatchOnly`. It adds 10.0.0.0/24 on two interfaces and deletes three times with no output interface. The route count goes from 2 to 1 to 0, and the third delete returns ESRCH.
- `test/rtnetlink/linux/route_test.sh` runs the same sequence with `ip route del`.
- Local bazel runs of `//test/syscalls:socket_netlink_route_test_runsc_systrap_directfs` (71 tests, including the new one) and `//test/rtnetlink:route_test_runsc_systrap_overlay_directfs` pass with the fix. Both fail without the `stack.go` change. The same tests also pass on native Linux. ptrace runsc variants were not run.
FUTURE_COPYBARA_INTEGRATE_REVIEW=#15301 from adi-IL:bot/gvisor-15295 7577af3
PiperOrigin-RevId: 994048898
@copybara-service
copybara-service Bot merged commit 186b5da into gvisor:master Oct 6, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull Causes the PR to be imported into Google. Will be removed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RTM_DELROUTE deletes all matching routes

2 participants