Skip to content

Commit

Permalink
S/R iptables
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 716114923
  • Loading branch information
nybidari authored and gvisor-bot committed Jan 16, 2025
1 parent 25b1d71 commit 54c6f26
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/tcpip/stack/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ type ConnTrack struct {

// clock provides timing used to determine conntrack reapings.
clock tcpip.Clock
// TODO(b/341946753): Restore when netstack is savable.

rand *rand.Rand `state:"nosave"`

mu connTrackRWMutex `state:"nosave"`
Expand Down
19 changes: 11 additions & 8 deletions pkg/tcpip/stack/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package stack

import (
"context"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -606,15 +605,19 @@ func (it *IPTables) check(table Table, hook Hook, pkt *PacketBuffer, r *Route, a

// beforeSave is invoked by stateify.
func (it *IPTables) beforeSave() {
// Ensure the reaper exits cleanly.
it.reaper.Stop()
// Prevent others from modifying the connection table.
it.connections.mu.Lock()
if it.reaper != nil {
// Ensure the reaper exits cleanly.
it.reaper.Stop()
}
}

// afterLoad is invoked by stateify.
func (it *IPTables) afterLoad(context.Context) {
it.startReaper(reaperDelay)
func (it *IPTables) Resume() {
it.mu.Lock()
defer it.mu.Unlock()

if it.modified {
it.startReaper(reaperDelay)
}
}

// startReaper periodically reaps timed out connections.
Expand Down
2 changes: 1 addition & 1 deletion pkg/tcpip/stack/iptables_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const (
type IPTables struct {
connections ConnTrack

reaper tcpip.Timer
reaper tcpip.Timer `state:"nosave"`

mu ipTablesRWMutex `state:"nosave"`
// v4Tables and v6tables map tableIDs to tables. They hold builtin
Expand Down
1 change: 1 addition & 0 deletions pkg/tcpip/stack/save_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ import (
func (s *Stack) afterLoad(context.Context) {
s.insecureRNG = rand.New(rand.NewSource(time.Now().UnixNano()))
s.secureRNG = cryptorand.RNGFrom(cryptorand.Reader)
s.tables.connections.rand = s.insecureRNG
}
5 changes: 3 additions & 2 deletions pkg/tcpip/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ type Stack struct {
handleLocal bool

// tables are the iptables packet filtering and manipulation rules.
// TODO(gvisor.dev/issue/4595): S/R this field.
tables *IPTables `state:"nosave"`
tables *IPTables

// restoredEndpoints is a list of endpoints that need to be restored if the
// stack is being restored.
Expand Down Expand Up @@ -2005,6 +2004,7 @@ func (s *Stack) Restore() {
eps := s.restoredEndpoints
s.restoredEndpoints = nil
saveRestoreEnabled := s.saveRestoreEnabled
s.tables.Resume()
s.mu.Unlock()
for _, e := range eps {
e.Restore(s)
Expand All @@ -2024,6 +2024,7 @@ func (s *Stack) Resume() {
s.mu.Lock()
eps := s.resumableEndpoints
s.resumableEndpoints = nil
s.tables.Resume()
s.mu.Unlock()
for _, e := range eps {
e.Resume()
Expand Down

0 comments on commit 54c6f26

Please sign in to comment.