From 56998a010d0581379204a1b8b36987649d2e4c4e Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 30 Mar 2023 16:09:46 +0200 Subject: [PATCH] Improve lookup tries for network state --- network/state/lookup.go | 14 ++++++-------- network/state/system_linux.go | 6 ++++-- network/state/tables.go | 1 - 3 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 network/state/tables.go diff --git a/network/state/lookup.go b/network/state/lookup.go index a764e522..479bcf94 100644 --- a/network/state/lookup.go +++ b/network/state/lookup.go @@ -2,7 +2,6 @@ package state import ( "errors" - "time" "github.com/safing/portmaster/network/netutils" "github.com/safing/portmaster/network/packet" @@ -30,9 +29,8 @@ var ( ) var ( - baseWaitTime = 3 * time.Millisecond - lookupRetries = 7 * 2 // Every retry takes two full passes. - fastLookupRetries = 2 * 2 + lookupTries = 15 // With a max wait of 5ms, this amounts to up to 75ms. + fastLookupTries = 2 ) // Lookup looks for the given connection in the system state tables and returns the PID of the associated process and whether the connection is inbound. @@ -81,7 +79,7 @@ func (table *tcpTable) lookup(pktInfo *packet.Info, fast bool) ( ) // Search for the socket until found. - for i := 1; i <= lookupRetries; i++ { + for i := 1; i <= lookupTries; i++ { // Get or update tables. if i == 1 { connections, listeners, updateIter = table.getCurrentTables() @@ -120,7 +118,7 @@ func (table *tcpTable) lookup(pktInfo *packet.Info, fast bool) ( } // Search less if we want to be fast. - if fast && i < fastLookupRetries { + if fast && i >= fastLookupTries { break } } @@ -184,7 +182,7 @@ func (table *udpTable) lookup(pktInfo *packet.Info, fast bool) ( ) // Search for the socket until found. - for i := 1; i <= lookupRetries; i++ { + for i := 1; i <= lookupTries; i++ { // Get or update tables. if i == 1 { binds, updateIter = table.getCurrentTables() @@ -245,7 +243,7 @@ func (table *udpTable) lookup(pktInfo *packet.Info, fast bool) ( } // Search less if we want to be fast. - if fast && i < fastLookupRetries { + if fast && i >= fastLookupTries { break } } diff --git a/network/state/system_linux.go b/network/state/system_linux.go index abef01c1..4f7c4138 100644 --- a/network/state/system_linux.go +++ b/network/state/system_linux.go @@ -14,8 +14,10 @@ var ( getUDP6Table = proc.GetUDP6Table ) +var baseWaitTime = 3 * time.Millisecond + func checkPID(socketInfo socket.Info, connInbound bool) (pid int, inbound bool, err error) { - for i := 0; i <= lookupRetries; i++ { + for i := 1; i <= lookupTries; i++ { // look for PID pid = proc.GetPID(socketInfo) if pid != socket.UndefinedProcessID { @@ -24,7 +26,7 @@ func checkPID(socketInfo socket.Info, connInbound bool) (pid int, inbound bool, } // every time, except for the last iteration - if i < lookupRetries { + if i < lookupTries { // we found no PID, we could have been too fast, give the kernel some time to think // back off timer: with 3ms baseWaitTime: 3, 6, 9, 12, 15, 18, 21ms - 84ms in total time.Sleep(time.Duration(i+1) * baseWaitTime) diff --git a/network/state/tables.go b/network/state/tables.go deleted file mode 100644 index 7bf2df5b..00000000 --- a/network/state/tables.go +++ /dev/null @@ -1 +0,0 @@ -package state