From 2a2b6424ed5bf7b2e97464115736ce75175159de Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Wed, 2 Apr 2025 11:57:02 -0400 Subject: [PATCH] add new locks added to master --- config/config.go | 9 +++++---- connection_manager.go | 2 +- dns_server.go | 5 +++-- firewall.go | 5 +++-- handshake_manager.go | 2 +- remote_list.go | 5 +++-- sshd/server.go | 5 +++-- timeout.go | 8 +++++--- udp/udp_rio_windows.go | 10 +++++++--- 9 files changed, 31 insertions(+), 20 deletions(-) diff --git a/config/config.go b/config/config.go index b1531e9..2dfe1c5 100644 --- a/config/config.go +++ b/config/config.go @@ -11,12 +11,12 @@ import ( "sort" "strconv" "strings" - "sync" "syscall" "time" "dario.cat/mergo" "github.com/sirupsen/logrus" + "github.com/wadey/synctrace" "gopkg.in/yaml.v3" ) @@ -27,13 +27,14 @@ type C struct { oldSettings map[string]any callbacks []func(*C) l *logrus.Logger - reloadLock sync.Mutex + reloadLock synctrace.Mutex } func NewC(l *logrus.Logger) *C { return &C{ - Settings: make(map[string]any), - l: l, + Settings: make(map[string]any), + l: l, + reloadLock: synctrace.NewMutex("config-reload"), } } diff --git a/connection_manager.go b/connection_manager.go index 2e24973..d7d071e 100644 --- a/connection_manager.go +++ b/connection_manager.go @@ -65,7 +65,7 @@ func newConnectionManager(ctx context.Context, l *logrus.Logger, intf *Interface outLock: synctrace.NewRWMutex("connection-manager-out"), relayUsed: make(map[uint32]struct{}), relayUsedLock: synctrace.NewRWMutex("connection-manager-relay-used"), - trafficTimer: NewLockingTimerWheel[uint32](time.Millisecond*500, max), + trafficTimer: NewLockingTimerWheel[uint32]("traffic-timer", time.Millisecond*500, max), intf: intf, pendingDeletion: make(map[uint32]struct{}), checkInterval: checkInterval, diff --git a/dns_server.go b/dns_server.go index 710f6ed..813cd8c 100644 --- a/dns_server.go +++ b/dns_server.go @@ -6,12 +6,12 @@ import ( "net/netip" "strconv" "strings" - "sync" "github.com/gaissmai/bart" "github.com/miekg/dns" "github.com/sirupsen/logrus" "github.com/slackhq/nebula/config" + "github.com/wadey/synctrace" ) // This whole thing should be rewritten to use context @@ -21,7 +21,7 @@ var dnsServer *dns.Server var dnsAddr string type dnsRecords struct { - sync.RWMutex + synctrace.RWMutex l *logrus.Logger dnsMap4 map[string]netip.Addr dnsMap6 map[string]netip.Addr @@ -31,6 +31,7 @@ type dnsRecords struct { func newDnsRecords(l *logrus.Logger, cs *CertState, hostMap *HostMap) *dnsRecords { return &dnsRecords{ + RWMutex: synctrace.NewRWMutex("dns-records"), l: l, dnsMap4: make(map[string]netip.Addr), dnsMap6: make(map[string]netip.Addr), diff --git a/firewall.go b/firewall.go index e730114..82b2736 100644 --- a/firewall.go +++ b/firewall.go @@ -10,7 +10,6 @@ import ( "reflect" "strconv" "strings" - "sync" "time" "github.com/gaissmai/bart" @@ -19,6 +18,7 @@ import ( "github.com/slackhq/nebula/cert" "github.com/slackhq/nebula/config" "github.com/slackhq/nebula/firewall" + "github.com/wadey/synctrace" ) type FirewallInterface interface { @@ -76,7 +76,7 @@ type firewallMetrics struct { } type FirewallConntrack struct { - sync.Mutex + synctrace.Mutex Conns map[firewall.Packet]*conn TimerWheel *TimerWheel[firewall.Packet] @@ -164,6 +164,7 @@ func NewFirewall(l *logrus.Logger, tcpTimeout, UDPTimeout, defaultTimeout time.D return &Firewall{ Conntrack: &FirewallConntrack{ + Mutex: synctrace.NewMutex("firewall-conntrack"), Conns: make(map[firewall.Packet]*conn), TimerWheel: NewTimerWheel[firewall.Packet](tmin, tmax), }, diff --git a/handshake_manager.go b/handshake_manager.go index c520293..7defbbf 100644 --- a/handshake_manager.go +++ b/handshake_manager.go @@ -112,7 +112,7 @@ func NewHandshakeManager(l *logrus.Logger, mainHostMap *HostMap, lightHouse *Lig outside: outside, config: config, trigger: make(chan netip.Addr, config.triggerBuffer), - OutboundHandshakeTimer: NewLockingTimerWheel[netip.Addr](config.tryInterval, hsTimeout(config.retries, config.tryInterval)), + OutboundHandshakeTimer: NewLockingTimerWheel[netip.Addr]("outbound-handshake-timer", config.tryInterval, hsTimeout(config.retries, config.tryInterval)), messageMetrics: config.messageMetrics, metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil), metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil), diff --git a/remote_list.go b/remote_list.go index 6baed29..871b568 100644 --- a/remote_list.go +++ b/remote_list.go @@ -7,11 +7,11 @@ import ( "slices" "sort" "strconv" - "sync" "sync/atomic" "time" "github.com/sirupsen/logrus" + "github.com/wadey/synctrace" ) // forEachFunc is used to benefit folks that want to do work inside the lock @@ -185,7 +185,7 @@ func (hr *hostnamesResults) GetAddrs() []netip.AddrPort { // It serves as a local cache of query replies, host update notifications, and locally learned addresses type RemoteList struct { // Every interaction with internals requires a lock! - sync.RWMutex + synctrace.RWMutex // The full list of vpn addresses assigned to this host vpnAddrs []netip.Addr @@ -215,6 +215,7 @@ type RemoteList struct { // NewRemoteList creates a new empty RemoteList func NewRemoteList(vpnAddrs []netip.Addr, shouldAdd func(netip.Addr) bool) *RemoteList { r := &RemoteList{ + RWMutex: synctrace.NewRWMutex("remote-list"), vpnAddrs: make([]netip.Addr, len(vpnAddrs)), addrs: make([]netip.AddrPort, 0), relays: make([]netip.Addr, 0), diff --git a/sshd/server.go b/sshd/server.go index a8b60ba..50d9379 100644 --- a/sshd/server.go +++ b/sshd/server.go @@ -5,10 +5,10 @@ import ( "errors" "fmt" "net" - "sync" "github.com/armon/go-radix" "github.com/sirupsen/logrus" + "github.com/wadey/synctrace" "golang.org/x/crypto/ssh" ) @@ -28,7 +28,7 @@ type SSHServer struct { listener net.Listener // Locks the conns/counter to avoid concurrent map access - connsLock sync.Mutex + connsLock synctrace.Mutex conns map[int]*session counter int } @@ -41,6 +41,7 @@ func NewSSHServer(l *logrus.Entry) (*SSHServer, error) { l: l, commands: radix.New(), conns: make(map[int]*session), + connsLock: synctrace.NewMutex("ssh-server-conns"), } cc := ssh.CertChecker{ diff --git a/timeout.go b/timeout.go index c1b4c39..5b09161 100644 --- a/timeout.go +++ b/timeout.go @@ -1,8 +1,9 @@ package nebula import ( - "sync" "time" + + "github.com/wadey/synctrace" ) // How many timer objects should be cached @@ -34,7 +35,7 @@ type TimerWheel[T any] struct { } type LockingTimerWheel[T any] struct { - m sync.Mutex + m synctrace.Mutex t *TimerWheel[T] } @@ -81,8 +82,9 @@ func NewTimerWheel[T any](min, max time.Duration) *TimerWheel[T] { } // NewLockingTimerWheel is version of TimerWheel that is safe for concurrent use with a small performance penalty -func NewLockingTimerWheel[T any](min, max time.Duration) *LockingTimerWheel[T] { +func NewLockingTimerWheel[T any](name string, min, max time.Duration) *LockingTimerWheel[T] { return &LockingTimerWheel[T]{ + m: synctrace.NewMutex(name), t: NewTimerWheel[T](min, max), } } diff --git a/udp/udp_rio_windows.go b/udp/udp_rio_windows.go index 585b642..e3f836c 100644 --- a/udp/udp_rio_windows.go +++ b/udp/udp_rio_windows.go @@ -11,13 +11,13 @@ import ( "io" "net" "net/netip" - "sync" "sync/atomic" "syscall" "unsafe" "github.com/sirupsen/logrus" "github.com/slackhq/nebula/config" + "github.com/wadey/synctrace" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/conn/winrio" ) @@ -46,7 +46,7 @@ type ringBuffer struct { iocp windows.Handle isFull bool cq winrio.Cq - mu sync.Mutex + mu synctrace.Mutex overlapped windows.Overlapped } @@ -64,7 +64,11 @@ func NewRIOListener(l *logrus.Logger, addr netip.Addr, port int) (*RIOConn, erro return nil, errors.New("could not initialize winrio") } - u := &RIOConn{l: l} + u := &RIOConn{ + l: l, + rx: ringBuffer{mu: synctrace.NewMutex("rio-rx")}, + tx: ringBuffer{mu: synctrace.NewMutex("rio-tx")}, + } err := u.bind(&windows.SockaddrInet6{Addr: addr.As16(), Port: port}) if err != nil {