Skip to content

Commit 145d1fe

Browse files
committed
ipn/wg: m log tag refactor
1 parent fd9d2f5 commit 145d1fe

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

intra/ipn/wgproxy.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (w *wgproxy) onNotOK() (didRefresh, allok bool) {
313313
didRefresh = true
314314
allok = err == nil
315315
} else {
316-
allok, err = w.refreshBa.DoIt(w.tag(), func() (bool, error) {
316+
allok, err = w.refreshBa.DoIt(w.who(), func() (bool, error) {
317317
rerr := w.Refresh()
318318
didRefresh = true
319319
return rerr == nil, rerr
@@ -383,7 +383,7 @@ func (w *wgproxy) Refresh() (err error) {
383383
// re-call ParseEndpoint, so peers would keep sending handshakes to stale IPs.
384384
if cfg := w.wgtun.uapicfg.Load(); len(cfg) > 0 {
385385
if ipcerr := w.Device.IpcSet(cfg); ipcerr != nil {
386-
log.W("proxy: wg: %s: refresh: re-apply ipcset failed; err %v", w.wgtun.tag(), ipcerr)
386+
log.W("proxy: wg: %s: refresh: re-apply ipcset failed; err %v", w.tag(), ipcerr)
387387
}
388388
}
389389
}
@@ -416,45 +416,45 @@ func (w *wgproxy) update(id, txt string) bool {
416416
const anew = false // cannot update in-place; create new tunnel
417417
status := w.status.Load()
418418
if status == END {
419-
log.W("proxy: wg: update(%s<>%s): END; status(%s)", id, w.tag(), pxstatus(status))
419+
log.W("proxy: wg: update(%s<>%s): END; status(%s)", id, w.who(), pxstatus(status))
420420
return anew
421421
}
422422
if status == TNT {
423-
log.W("proxy: wg: update(%s<>%s): TNT; status(%s) - marking session as un-updatable", id, w.tag(), pxstatus(status))
423+
log.W("proxy: wg: update(%s<>%s): TNT; status(%s) - marking session as un-updatable", id, w.who(), pxstatus(status))
424424
return anew
425425
}
426426

427427
incomingPrefersOffload := preferOffload(id)
428428
if incomingPrefersOffload != w.preferOffload {
429-
log.W("proxy: wg: update(%s<>%s): failed; preferOffload() %t != %t", id, w.tag(), incomingPrefersOffload, w.preferOffload)
429+
log.W("proxy: wg: update(%s<>%s): failed; preferOffload() %t != %t", id, w.who(), incomingPrefersOffload, w.preferOffload)
430430
return anew
431431
}
432432

433433
// str copy: go.dev/play/p/eO814kGGNtO
434434
cptxt := txt
435435
opts, err := wgIfConfigOf(w.id, &cptxt)
436436
if err != nil {
437-
log.W("proxy: wg: update(%s<>%s): err: %v", id, w.tag(), err)
437+
log.W("proxy: wg: update(%s<>%s): err: %v", id, w.who(), err)
438438
return anew
439439
}
440440

441441
if opts.willreplacepeers {
442-
log.W("proxy: wg: update(%s<>%s): cannot proceed; peers will be replaced", id, w.tag())
442+
log.W("proxy: wg: update(%s<>%s): cannot proceed; peers will be replaced", id, w.who())
443443
return anew
444444
}
445445

446446
if err := w.setRoutes(opts.ifaddrs); err != nil {
447-
log.W("proxy: wg: update(%s<>%s): failed; setRoutes: %v", id, w.tag(), err)
447+
log.W("proxy: wg: update(%s<>%s): failed; setRoutes: %v", id, w.who(), err)
448448
return anew
449449
}
450450

451451
if settings.Debug {
452452
if !w.amnezia.Load().Same(opts.amnezia) {
453453
log.D("proxy: wg: update(%s<>%s): failed; amnezia %v != %v",
454-
id, w.tag(), opts.amnezia, w.amnezia.Load())
454+
id, w.who(), opts.amnezia, w.amnezia.Load())
455455
}
456456
if opts.dns != nil && !opts.dns.EqualAddrs(w.dns.Load()) {
457-
log.D("proxy: wg: update(%s<>%s): failed; new/mismatched dns", id, w.tag())
457+
log.D("proxy: wg: update(%s<>%s): failed; new/mismatched dns", id, w.who())
458458
} // nb: client code MUST re-add wg DNS, not our responsibility
459459
}
460460

@@ -463,7 +463,7 @@ func (w *wgproxy) update(id, txt string) bool {
463463
// reusing existing tunnel (interface config unchanged)
464464
// but peer config may have changed!
465465
log.I("proxy: wg: update: (%s<>%s): reuse; mtu: %d=>%d, allowed: %d=>%d; peers: %d=>%d; dns: %d=>%d; endpoint: %d=>%d",
466-
id, w.tag(), w.ep.MTU(), maybeNewMtu, w.rt.Len(), len(opts.allowed), len(w.peers.Load()), len(opts.peers), w.dns.Load().Len(), opts.dns.Len(),
466+
id, w.who(), w.ep.MTU(), maybeNewMtu, w.rt.Len(), len(opts.allowed), len(w.peers.Load()), len(opts.peers), w.dns.Load().Len(), opts.dns.Len(),
467467
w.remote.Load().Len() /*remote.Load may return nil*/, opts.eps.Len())
468468

469469
w.peers.Store(opts.peers) // re-assignment is okay (map entry modification is not)
@@ -477,7 +477,7 @@ func (w *wgproxy) update(id, txt string) bool {
477477

478478
ipcerr := w.Device.IpcSet(cptxt)
479479
if ipcerr != nil {
480-
log.W("proxy: updating wg(%s<>%s) ipcset; err %v", id, w.tag(), ipcerr)
480+
log.W("proxy: updating wg(%s<>%s) ipcset; err %v", id, w.who(), ipcerr)
481481
return anew
482482
}
483483
// w.Device is assumed to be Up
@@ -716,9 +716,9 @@ func NewWgProxy(id string, ctl protect.Controller, px ProxyProvider, lp LinkProp
716716

717717
var wgep wgconn
718718
if wgtun.preferOffload {
719-
wgep = wg.NewEndpoint2(wgtun.tag(), wgtun.serve, wgtun.remote, wgtun.listener, wgtun.amnezia)
719+
wgep = wg.NewEndpoint2(wgtun.who(), wgtun.serve, wgtun.remote, wgtun.listener, wgtun.amnezia)
720720
} else {
721-
wgep = wg.NewEndpoint(wgtun.tag(), wgtun.serve, wgtun.remote, wgtun.listener, wgtun.amnezia)
721+
wgep = wg.NewEndpoint(wgtun.who(), wgtun.serve, wgtun.remote, wgtun.listener, wgtun.amnezia)
722722
}
723723

724724
wgdev, err := newdevice(wgtun, wgep, uapicfg)
@@ -788,11 +788,11 @@ func (t *wgtun) setupReverserIfNeeded(set bool) (didSet bool) {
788788
}
789789

790790
func (w *wgtun) swapVia(new Proxy) (old Proxy) {
791-
return swapVia(w.tag(), new, w.viaID, w.via)
791+
return swapVia(w.who(), new, w.viaID, w.via)
792792
}
793793

794794
func (w *wgtun) viafor() *Proxy {
795-
return viafor(w.tag(), w.viaID.Load(), w.px)
795+
return viafor(w.who(), w.viaID.Load(), w.px)
796796
}
797797

798798
func (w *wgtun) getVia() (v Proxy) {
@@ -812,9 +812,13 @@ func (w *wgtun) getViaIfDialed() Proxy {
812812
return nil
813813
}
814814

815-
// tag concats id of this proxy & status of its via.
815+
// who concats id of this proxy & status of its via.
816+
func (w *wgtun) who() string {
817+
return w.id + ":" + core.LocStr(w)
818+
}
819+
816820
func (w *wgtun) tag() string {
817-
return w.id + ":" + core.LocStr(w) + " (" + w.viaStatus() + ")"
821+
return w.who() + " (" + w.viaStatus() + ")"
818822
}
819823

820824
func (w *wgtun) viaStatus() (s string) {
@@ -915,7 +919,7 @@ func makeWgTun(id, cfg string, ctl protect.Controller, px ProxyProvider, lp Link
915919
if err := s.CreateNIC(wgnic, ep); err != nil {
916920
done()
917921
ep.Close()
918-
return nil, fmt.Errorf("wg: %s create nic: %v", t.tag(), err)
922+
return nil, fmt.Errorf("wg: %s create nic: %v", t.who(), err)
919923
}
920924

921925
settings.ExperimentalWireGuard.On(ctx, func(yn bool) {
@@ -1151,8 +1155,8 @@ func (tun *wgtun) Close() error {
11511155
// TODO: use wgtun as a receiver for Stats()
11521156
// Never returns nil.
11531157
func (w *wgproxy) Stat() (out *x.RouterStats) {
1154-
log.VV("proxy: wg: %s stats: start", w.tag())
1155-
defer log.VV("proxy: wg: %s stats: end", w.tag())
1158+
start := time.Now()
1159+
defer log.VV("proxy: wg: %s stats: end (duration: %s)", w.tag(), core.FmtTimeAsPeriod(start))
11561160

11571161
out = new(x.RouterStats)
11581162

@@ -1520,7 +1524,7 @@ func (h *wgtun) serve(network, local string) (pc net.PacketConn, err error) {
15201524
}
15211525

15221526
// todo: dial into both direct & via if via cannot handle all routes?
1523-
who := h.tag()
1527+
who := h.who()
15241528
var v Proxy // may be nil
15251529
hasvia, usingvia := false, false
15261530
if hasvia = usevia(h.viaID); hasvia {

0 commit comments

Comments
 (0)