Skip to content

Commit 74713e1

Browse files
committed
libnetwork/d/overlay: un-embed mutexes
It is easier to find all references when they are struct fields rather than embedded structs. Signed-off-by: Cory Snider <[email protected]>
1 parent 674e401 commit 74713e1

5 files changed

Lines changed: 56 additions & 56 deletions

File tree

libnetwork/drivers/overlay/encryption.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ type encrNode struct {
9898

9999
type encrMap struct {
100100
nodes map[netip.Addr]encrNode
101-
sync.Mutex
101+
mu sync.Mutex
102102
}
103103

104104
func (e *encrMap) String() string {
105-
e.Lock()
106-
defer e.Unlock()
105+
e.mu.Lock()
106+
defer e.mu.Unlock()
107107
b := new(bytes.Buffer)
108108
for k, v := range e.nodes {
109109
b.WriteString("\n")
@@ -153,12 +153,12 @@ func (d *driver) setupEncryption(remoteIP netip.Addr) error {
153153
}
154154
}
155155

156-
d.secMap.Lock()
156+
d.secMap.mu.Lock()
157157
node := d.secMap.nodes[remoteIP]
158158
node.spi = indices
159159
node.count++
160160
d.secMap.nodes[remoteIP] = node
161-
d.secMap.Unlock()
161+
d.secMap.mu.Unlock()
162162

163163
return nil
164164
}
@@ -167,8 +167,8 @@ func (d *driver) removeEncryption(remoteIP netip.Addr) error {
167167
log.G(context.TODO()).Debugf("removeEncryption(%s)", remoteIP)
168168

169169
spi := func() []spi {
170-
d.secMap.Lock()
171-
defer d.secMap.Unlock()
170+
d.secMap.mu.Lock()
171+
defer d.secMap.mu.Unlock()
172172
node := d.secMap.nodes[remoteIP]
173173
if node.count == 1 {
174174
delete(d.secMap.nodes, remoteIP)
@@ -452,7 +452,7 @@ func buildAeadAlgo(k *key, s int) *netlink.XfrmStateAlgo {
452452
}
453453

454454
func (d *driver) secMapWalk(f func(netip.Addr, []spi) ([]spi, bool)) error {
455-
d.secMap.Lock()
455+
d.secMap.mu.Lock()
456456
for rIP, node := range d.secMap.nodes {
457457
idxs, stop := f(rIP, node.spi)
458458
if idxs != nil {
@@ -462,18 +462,18 @@ func (d *driver) secMapWalk(f func(netip.Addr, []spi) ([]spi, bool)) error {
462462
break
463463
}
464464
}
465-
d.secMap.Unlock()
465+
d.secMap.mu.Unlock()
466466
return nil
467467
}
468468

469469
func (d *driver) setKeys(keys []*key) error {
470470
// Remove any stale policy, state
471471
clearEncryptionStates()
472472
// Accept the encryption keys and clear any stale encryption map
473-
d.Lock()
473+
d.mu.Lock()
474474
d.keys = keys
475475
d.secMap = &encrMap{nodes: map[netip.Addr]encrNode{}}
476-
d.Unlock()
476+
d.mu.Unlock()
477477
log.G(context.TODO()).Debugf("Initial encryption keys: %v", keys)
478478
return nil
479479
}
@@ -493,8 +493,8 @@ func (d *driver) updateKeys(newKey, primary, pruneKey *key) error {
493493
aIP = d.advertiseAddress
494494
)
495495

496-
d.Lock()
497-
defer d.Unlock()
496+
d.mu.Lock()
497+
defer d.mu.Unlock()
498498

499499
// add new
500500
if newKey != nil {

libnetwork/drivers/overlay/ov_endpoint.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ type endpoint struct {
2727
}
2828

2929
func (n *network) endpoint(eid string) *endpoint {
30-
n.Lock()
31-
defer n.Unlock()
30+
n.mu.Lock()
31+
defer n.mu.Unlock()
3232

3333
return n.endpoints[eid]
3434
}
3535

3636
func (n *network) addEndpoint(ep *endpoint) {
37-
n.Lock()
37+
n.mu.Lock()
3838
n.endpoints[ep.id] = ep
39-
n.Unlock()
39+
n.mu.Unlock()
4040
}
4141

4242
func (n *network) deleteEndpoint(eid string) {
43-
n.Lock()
43+
n.mu.Lock()
4444
delete(n.endpoints, eid)
45-
n.Unlock()
45+
n.mu.Unlock()
4646
}
4747

4848
func (d *driver) CreateEndpoint(_ context.Context, nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {

libnetwork/drivers/overlay/ov_network.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type network struct {
6363
subnets []*subnet
6464
secure bool
6565
mtu int
66-
sync.Mutex
66+
mu sync.Mutex
6767
}
6868

6969
func init() {
@@ -151,8 +151,8 @@ func (d *driver) CreateNetwork(ctx context.Context, id string, option map[string
151151
n.subnets = append(n.subnets, s)
152152
}
153153

154-
d.Lock()
155-
defer d.Unlock()
154+
d.mu.Lock()
155+
defer d.mu.Unlock()
156156
if d.networks[n.id] != nil {
157157
return fmt.Errorf("attempt to create overlay network %v that already exists", n.id)
158158
}
@@ -187,12 +187,12 @@ func (d *driver) DeleteNetwork(nid string) error {
187187
return err
188188
}
189189

190-
d.Lock()
190+
d.mu.Lock()
191191
// Only perform a peer flush operation (if required) AFTER unlocking
192192
// the driver lock to avoid deadlocking w/ the peerDB.
193193
var doPeerFlush bool
194194
defer func() {
195-
d.Unlock()
195+
d.mu.Unlock()
196196
if doPeerFlush {
197197
d.peerFlush(nid)
198198
}
@@ -245,14 +245,14 @@ func (n *network) joinSandbox(s *subnet, incJoinCount bool) error {
245245
// the other will wait.
246246
networkOnce.Do(populateVNITbl)
247247

248-
n.Lock()
248+
n.mu.Lock()
249249
// If initialization was successful then tell the peerDB to initialize the
250250
// sandbox with all the peers previously received from networkdb. But only
251251
// do this after unlocking the network. Otherwise we could deadlock with
252252
// on the peerDB channel while peerDB is waiting for the network lock.
253253
var doInitPeerDB bool
254254
defer func() {
255-
n.Unlock()
255+
n.mu.Unlock()
256256
if doInitPeerDB {
257257
go n.driver.initSandboxPeerDB(n.id)
258258
}
@@ -290,8 +290,8 @@ func (n *network) joinSandbox(s *subnet, incJoinCount bool) error {
290290
}
291291

292292
func (n *network) leaveSandbox() {
293-
n.Lock()
294-
defer n.Unlock()
293+
n.mu.Lock()
294+
defer n.mu.Unlock()
295295
n.joinCnt--
296296
if n.joinCnt != 0 {
297297
return
@@ -597,16 +597,16 @@ func (n *network) initSandbox() error {
597597
}
598598

599599
func (d *driver) network(nid string) *network {
600-
d.Lock()
600+
d.mu.Lock()
601601
n := d.networks[nid]
602-
d.Unlock()
602+
d.mu.Unlock()
603603

604604
return n
605605
}
606606

607607
func (n *network) sandbox() *osl.Namespace {
608-
n.Lock()
609-
defer n.Unlock()
608+
n.mu.Lock()
609+
defer n.mu.Unlock()
610610
return n.sbox
611611
}
612612

libnetwork/drivers/overlay/overlay.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type driver struct {
3838
initOS sync.Once
3939
keys []*key
4040
peerOpMu sync.Mutex
41-
sync.Mutex
41+
mu sync.Mutex
4242
}
4343

4444
// Register registers a new instance of the overlay driver.
@@ -91,10 +91,10 @@ func (d *driver) nodeJoin(data discoverapi.NodeDiscoveryData) error {
9191
if !advAddr.IsValid() {
9292
return errors.New("invalid discovery data")
9393
}
94-
d.Lock()
94+
d.mu.Lock()
9595
d.advertiseAddress = advAddr
9696
d.bindAddress = bindAddr
97-
d.Unlock()
97+
d.mu.Unlock()
9898
}
9999
return nil
100100
}

libnetwork/drivers/overlay/peerdb.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@ func (p *peerEntry) isLocal() bool {
3131

3232
type peerMap struct {
3333
mp setmatrix.SetMatrix[netip.Prefix, peerEntry]
34-
sync.Mutex
34+
mu sync.Mutex
3535
}
3636

3737
type peerNetworkMap struct {
3838
// map with key peerKey
3939
mp map[string]*peerMap
40-
sync.Mutex
40+
mu sync.Mutex
4141
}
4242

4343
func (d *driver) peerDbNetworkWalk(nid string, f func(netip.Prefix, peerEntry) bool) {
44-
d.peerDb.Lock()
44+
d.peerDb.mu.Lock()
4545
pMap, ok := d.peerDb.mp[nid]
46-
d.peerDb.Unlock()
46+
d.peerDb.mu.Unlock()
4747

4848
if !ok {
4949
return
5050
}
5151

5252
mp := map[netip.Prefix]peerEntry{}
53-
pMap.Lock()
53+
pMap.mu.Lock()
5454
for _, pKey := range pMap.mp.Keys() {
5555
entryDBList, ok := pMap.mp.Get(pKey)
5656
if ok {
5757
mp[pKey] = entryDBList[0]
5858
}
5959
}
60-
pMap.Unlock()
60+
pMap.mu.Unlock()
6161

6262
for k, v := range mp {
6363
if f(k, v) {
@@ -67,15 +67,15 @@ func (d *driver) peerDbNetworkWalk(nid string, f func(netip.Prefix, peerEntry) b
6767
}
6868

6969
func (d *driver) peerDbGet(nid string, peerIP netip.Prefix) (peerEntry, bool) {
70-
d.peerDb.Lock()
70+
d.peerDb.mu.Lock()
7171
pMap, ok := d.peerDb.mp[nid]
72-
d.peerDb.Unlock()
72+
d.peerDb.mu.Unlock()
7373
if !ok {
7474
return peerEntry{}, false
7575
}
7676

77-
pMap.Lock()
78-
defer pMap.Unlock()
77+
pMap.mu.Lock()
78+
defer pMap.mu.Unlock()
7979
c, _ := pMap.mp.Get(peerIP)
8080
if len(c) == 0 {
8181
return peerEntry{}, false
@@ -84,22 +84,22 @@ func (d *driver) peerDbGet(nid string, peerIP netip.Prefix) (peerEntry, bool) {
8484
}
8585

8686
func (d *driver) peerDbAdd(nid, eid string, peerIP netip.Prefix, peerMac net.HardwareAddr, vtep netip.Addr) (bool, int) {
87-
d.peerDb.Lock()
87+
d.peerDb.mu.Lock()
8888
pMap, ok := d.peerDb.mp[nid]
8989
if !ok {
9090
pMap = &peerMap{}
9191
d.peerDb.mp[nid] = pMap
9292
}
93-
d.peerDb.Unlock()
93+
d.peerDb.mu.Unlock()
9494

9595
pEntry := peerEntry{
9696
eid: eid,
9797
mac: macAddrOf(peerMac),
9898
vtep: vtep,
9999
}
100100

101-
pMap.Lock()
102-
defer pMap.Unlock()
101+
pMap.mu.Lock()
102+
defer pMap.mu.Unlock()
103103
b, i := pMap.mp.Insert(peerIP, pEntry)
104104
if i != 1 {
105105
// Transient case, there is more than one endpoint that is using the same IP
@@ -111,22 +111,22 @@ func (d *driver) peerDbAdd(nid, eid string, peerIP netip.Prefix, peerMac net.Har
111111
}
112112

113113
func (d *driver) peerDbDelete(nid, eid string, peerIP netip.Prefix, peerMac net.HardwareAddr, vtep netip.Addr) (bool, int) {
114-
d.peerDb.Lock()
114+
d.peerDb.mu.Lock()
115115
pMap, ok := d.peerDb.mp[nid]
116116
if !ok {
117-
d.peerDb.Unlock()
117+
d.peerDb.mu.Unlock()
118118
return false, 0
119119
}
120-
d.peerDb.Unlock()
120+
d.peerDb.mu.Unlock()
121121

122122
pEntry := peerEntry{
123123
eid: eid,
124124
mac: macAddrOf(peerMac),
125125
vtep: vtep,
126126
}
127127

128-
pMap.Lock()
129-
defer pMap.Unlock()
128+
pMap.mu.Lock()
129+
defer pMap.mu.Unlock()
130130
b, i := pMap.mp.Remove(peerIP, pEntry)
131131
if i != 0 {
132132
// Transient case, there is more than one endpoint that is using the same IP
@@ -335,8 +335,8 @@ func (d *driver) deleteNeighbor(nid string, peerIP netip.Prefix, peerMac net.Har
335335
func (d *driver) peerFlush(nid string) {
336336
d.peerOpMu.Lock()
337337
defer d.peerOpMu.Unlock()
338-
d.peerDb.Lock()
339-
defer d.peerDb.Unlock()
338+
d.peerDb.mu.Lock()
339+
defer d.peerDb.mu.Unlock()
340340
_, ok := d.peerDb.mp[nid]
341341
if !ok {
342342
log.G(context.TODO()).Warnf("Peer flush operation failed: unable to find the peerDB for nid:%s", nid)

0 commit comments

Comments
 (0)