Use the stack clock in iptables reaper

Updates #5939.

PiperOrigin-RevId: 418707213
This commit is contained in:
Tamir Duberstein
2021-12-28 18:39:16 -08:00
committed by gVisor bot
parent e511fc9092
commit 1444ce97b4
3 changed files with 12 additions and 20 deletions
+7 -16
View File
@@ -180,7 +180,6 @@ func DefaultTables(clock tcpip.Clock, rand *rand.Rand) *IPTables {
clock: clock,
rand: rand,
},
reaperDone: make(chan struct{}, 1),
}
}
@@ -545,7 +544,7 @@ 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.reaperDone <- struct{}{}
it.reaper.Stop()
// Prevent others from modifying the connection table.
it.connections.mu.Lock()
}
@@ -555,21 +554,13 @@ func (it *IPTables) afterLoad() {
it.startReaper(reaperDelay)
}
// startReaper starts a goroutine that wakes up periodically to reap timed out
// connections.
// startReaper periodically reaps timed out connections.
func (it *IPTables) startReaper(interval time.Duration) {
go func() { // S/R-SAFE: reaperDone is signalled when iptables is saved.
bucket := 0
for {
select {
case <-it.reaperDone:
return
// TODO(gvisor.dev/issue/5939): do not use the ambient clock.
case <-time.After(interval):
bucket, interval = it.connections.reapUnused(bucket, interval)
}
}
}()
bucket := 0
it.reaper = it.connections.clock.AfterFunc(interval, func() {
bucket, interval = it.connections.reapUnused(bucket, interval)
it.reaper.Reset(interval)
})
}
// Preconditions:
+4 -2
View File
@@ -121,7 +121,9 @@ func TestNATedConnectionReap(t *testing.T) {
// Stop the reaper if it is running so we can reap manually as it is started
// on the first change to IPTables.
iptables.reaperDone <- struct{}{}
if !iptables.reaper.Stop() {
t.Fatal("failed to stop reaper")
}
pkt := v6PacketBuffer()
@@ -224,7 +226,7 @@ func TestNATedConnectionReap(t *testing.T) {
bkt.mu.RLock()
defer bkt.mu.RUnlock()
for tuple := bkt.tuples.Front(); tuple != nil; tuple = tuple.Next() {
if tuple.id() == originalTID {
if tuple.id() == tid {
t.Errorf("unexpectedly found tuple with ID = %#v; reply = %t", tid, reply)
}
}
+1 -2
View File
@@ -83,8 +83,7 @@ const (
type IPTables struct {
connections ConnTrack
// reaperDone can be signaled to stop the reaper goroutine.
reaperDone chan struct{}
reaper tcpip.Timer
mu sync.RWMutex
// v4Tables and v6tables map tableIDs to tables. They hold builtin