From bcaefce4ac2fc3124384a8bb8cb72445fff8446a Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Mon, 18 Dec 2023 22:38:52 -0500 Subject: [PATCH] more types --- handshake_ix.go | 1 + handshake_manager.go | 1 + hostmap.go | 3 +-- mutex.go | 5 +++++ remote_list.go | 12 ++++++------ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/handshake_ix.go b/handshake_ix.go index f4a3106cc..1c7ff6c90 100644 --- a/handshake_ix.go +++ b/handshake_ix.go @@ -135,6 +135,7 @@ func ixHandshakeStage1(f *Interface, addr *udp.Addr, via *ViaSender, packet []by HandshakePacket: make(map[uint8][]byte, 0), lastHandshakeTime: hs.Details.Time, relayState: RelayState{ + syncRWMutex: newSyncRWMutex(mutexKey{Type: mutexKeyTypeRelayState, ID: uint32(vpnIp)}), relays: map[iputil.VpnIp]struct{}{}, relayForByIp: map[iputil.VpnIp]*Relay{}, relayForByIdx: map[uint32]*Relay{}, diff --git a/handshake_manager.go b/handshake_manager.go index 62204b31e..db7e2a564 100644 --- a/handshake_manager.go +++ b/handshake_manager.go @@ -389,6 +389,7 @@ func (hm *HandshakeManager) StartHandshake(vpnIp iputil.VpnIp, cacheCb func(*Han vpnIp: vpnIp, HandshakePacket: make(map[uint8][]byte, 0), relayState: RelayState{ + syncRWMutex: newSyncRWMutex(mutexKey{Type: mutexKeyTypeRelayState, ID: uint32(vpnIp)}), relays: map[iputil.VpnIp]struct{}{}, relayForByIp: map[iputil.VpnIp]*Relay{}, relayForByIdx: map[uint32]*Relay{}, diff --git a/hostmap.go b/hostmap.go index 1834705de..c5cbc5d28 100644 --- a/hostmap.go +++ b/hostmap.go @@ -3,7 +3,6 @@ package nebula import ( "errors" "net" - "sync" "sync/atomic" "time" @@ -67,7 +66,7 @@ type HostMap struct { // struct, make a copy of an existing value, edit the fileds in the copy, and // then store a pointer to the new copy in both realyForBy* maps. type RelayState struct { - sync.RWMutex + syncRWMutex relays map[iputil.VpnIp]struct{} // Set of VpnIp's of Hosts to use as relays to access this peer relayForByIp map[iputil.VpnIp]*Relay // Maps VpnIps of peers for which this HostInfo is a relay to some Relay info diff --git a/mutex.go b/mutex.go index bcf6d8884..21a47ae3b 100644 --- a/mutex.go +++ b/mutex.go @@ -8,8 +8,10 @@ const ( mutexKeyTypeHostMap mutexKeyType = "hostmap" mutexKeyTypeLightHouse = "lighthouse" + mutexKeyTypeRemoteList = "remote-list" mutexKeyTypeFirewallConntrack = "firewall-conntrack" mutexKeyTypeHostInfo = "hostinfo" + mutexKeyTypeRelayState = "relay-state" mutexKeyTypeHandshakeHostInfo = "handshake-hostinfo" mutexKeyTypeHandshakeManager = "handshake-manager" mutexKeyTypeConnectionStateWrite = "connection-state-write-lock" @@ -30,10 +32,13 @@ var allowedConcurrentLocks = map[mutexKeyType][]mutexKeyType{ mutexKeyTypeConnectionStateWrite: {mutexKeyTypeHostMap}, mutexKeyTypeLightHouse: {mutexKeyTypeHandshakeManager}, + mutexKeyTypeRemoteList: {mutexKeyTypeLightHouse}, mutexKeyTypeConnectionManagerIn: {mutexKeyTypeHostMap}, mutexKeyTypeConnectionManagerOut: {mutexKeyTypeConnectionStateWrite, mutexKeyTypeConnectionManagerIn}, mutexKeyTypeConnectionManagerRelayUsed: {mutexKeyTypeHandshakeHostInfo}, + + mutexKeyTypeRelayState: {mutexKeyTypeHostMap, mutexKeyTypeConnectionManagerRelayUsed}, } type mutexKey struct { diff --git a/remote_list.go b/remote_list.go index 60a1afdaf..a573e6531 100644 --- a/remote_list.go +++ b/remote_list.go @@ -7,7 +7,6 @@ import ( "net/netip" "sort" "strconv" - "sync" "sync/atomic" "time" @@ -190,7 +189,7 @@ func (hr *hostnamesResults) GetIPs() []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 + syncRWMutex // A deduplicated set of addresses. Any accessor should lock beforehand. addrs []*udp.Addr @@ -217,10 +216,11 @@ type RemoteList struct { // NewRemoteList creates a new empty RemoteList func NewRemoteList(shouldAdd func(netip.Addr) bool) *RemoteList { return &RemoteList{ - addrs: make([]*udp.Addr, 0), - relays: make([]*iputil.VpnIp, 0), - cache: make(map[iputil.VpnIp]*cache), - shouldAdd: shouldAdd, + syncRWMutex: newSyncRWMutex(mutexKey{Type: mutexKeyTypeRemoteList}), + addrs: make([]*udp.Addr, 0), + relays: make([]*iputil.VpnIp, 0), + cache: make(map[iputil.VpnIp]*cache), + shouldAdd: shouldAdd, } }