Skip to content

Commit 8166076

Browse files
committed
ipn/wg: use conn age for status calc
1 parent 1cf26af commit 8166076

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

intra/backend/ipn_proxies.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ type RouterStats struct {
414414
LastOK int64
415415
// last refresh time in millis
416416
LastRefresh int64
417+
// last re-connection opened in unix millis
418+
LastOpen int64
417419
// uptime in millis
418420
Since int64
419421
// Current proxy status

intra/ipn/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func healthy(p Proxy) error {
724724

725725
stat := p.Router().Stat()
726726
now := now()
727-
age := now - stat.Since
727+
age := now - stat.LastOpen
728728

729729
oldEnough := age > ageThreshold.Milliseconds()
730730
lastOK := stat.LastOK

intra/ipn/wg/wgconn.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ again:
358358
tries++
359359
goto again
360360
}
361-
if err != nil && !no6 {
361+
if err != nil && !no6 { // err and supports v6
362362
clos(ipv4)
363363
return nil, 0, err
364364
}
@@ -522,9 +522,9 @@ func (s *StdNetBind) quit() {
522522
}
523523

524524
func (s *StdNetBind) makeReceiveFn(fd int, uc net.PacketConn) conn.ReceiveFunc {
525+
who := strconv.Itoa(fd)
525526
// github.com/WireGuard/wireguard-go/blob/469159ecf/device/device.go#L531
526527
return func(bufs [][]byte, sizes []int, eps []conn.Endpoint) (n int, err error) {
527-
who := strconv.Itoa(fd)
528528
defer core.Recover(core.Exit11, "wgconn.recv."+who+"."+s.id)
529529

530530
anyProcessed := false // true when numMsgs > 0 (ex: no error)
@@ -619,9 +619,10 @@ func (s *StdNetBind) Send(buf [][]byte, peer conn.Endpoint) (err error) {
619619
hasAmnezia := amnezia.Set()
620620

621621
for _, data := range buf {
622-
bufok := len(data) > 0
622+
datalen := len(data) // grab the length before amnezia overwrites it
623+
bufok := datalen > 0
623624

624-
if false && log.Debug {
625+
if false && log.Verbose {
625626
log.VV("wg: bind: send: %s (%d) addr(%v) floodwg? %t, blackhole? %t; noconn? %t; hasbuf? %t",
626627
s.id, fd, dstIpp, floodWg, blackhole, noconn, bufok)
627628
}
@@ -636,8 +637,6 @@ func (s *StdNetBind) Send(buf [][]byte, peer conn.Endpoint) (err error) {
636637
anyProcessed = true
637638
anyTransportTyp = anyTransportTyp || transportType(data)
638639

639-
datalen := len(data) // grab the length before we overwrite it
640-
641640
overwritten = amnezia.send(&data)
642641

643642
if !flooded && (floodWg || hasAmnezia) {

intra/ipn/wgproxy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ type wgtun struct {
150150
// TODO: move status to a state-machine for all proxies
151151
status *core.Volatile[int] // status of this interface
152152
statusReason core.Volatile[string] // last state transition reason
153+
latestOpen atomic.Int64 // last open time in unix millis
153154
latestRefresh atomic.Int64 // last refresh time in unix millis
154155
latestPing atomic.Int64 // last ping time in unix millis
155156
latestErr core.Volatile[error] // last open/dial err
@@ -1265,6 +1266,7 @@ func (w *wgproxy) Stat() (out *x.RouterStats) {
12651266
out.LastGoodRx = w.latestGoodRx.Load()
12661267
out.LastGoodTx = w.latestGoodTx.Load()
12671268
out.LastRefresh = w.latestRefresh.Load()
1269+
out.LastOpen = w.latestOpen.Load()
12681270
out.Since = w.since
12691271
out.Status = pxstatus(w.status.Load()).String()
12701272
out.StatusReason = w.statusReason.Load()
@@ -1720,10 +1722,12 @@ func (h *wgtun) listener(op wg.PktDir, err error) (ended bool) {
17201722
why = "TNT: closed; prev: " + pxstatus(s).String()
17211723
s = TNT
17221724
return
1725+
} else if op == wg.Opn {
1726+
h.latestOpen.Store(now())
17231727
}
17241728

17251729
now := now()
1726-
age := now - h.since
1730+
age := now - h.latestOpen.Load()
17271731
if err != nil { // failing
17281732
s = TKO
17291733
why = "TKO: " + err.Error()
@@ -1738,6 +1742,8 @@ func (h *wgtun) listener(op wg.PktDir, err error) (ended bool) {
17381742
// on net.ErrClosed, wg stops recieving routine for all peers; this among
17391743
// other things mean that the wg.Device is effectively down and would not
17401744
// recieve any incoming messages (nor outgoing as those use the same socket)
1745+
// Note that, there could be multiple receive functions (not just one) and
1746+
// the other ones (one each per ip family) may be running just fine.
17411747
s = TNT
17421748
why = "TNT: closed " + string(op)
17431749
}
@@ -1791,7 +1797,7 @@ func (h *wgtun) listener(op wg.PktDir, err error) (ended bool) {
17911797
hasNewReads := lastRead > age
17921798

17931799
// too much time since last good write and good reads
1794-
readWriteDeviation := (hasNewReads || hasNewWrites) && deviationMs > markTNTAfterMillis
1800+
readWriteDeviation := (hasNewReads || hasNewWrites) && deviationMs > 2*markTNTAfterMillis
17951801
// too much time since last attempted read was good
17961802
readThres := hasNewReads && readElapsedMs > markTNTAfterMillis
17971803
// too much time since last attempted write was good

0 commit comments

Comments
 (0)