Browse code

libnetwork/osl: Namespace: make mutex private

Make the mutex internal to the Namespace; locking/unlocking should not
be done externally, and this makes it easier to see where it's used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/08/21 17:18:39
Showing 4 changed files
... ...
@@ -115,8 +115,8 @@ func (i *Interface) Statistics() (*types.InterfaceStatistics, error) {
115 115
 }
116 116
 
117 117
 func (n *Namespace) findDst(srcName string, isBridge bool) string {
118
-	n.Lock()
119
-	defer n.Unlock()
118
+	n.mu.Lock()
119
+	defer n.mu.Unlock()
120 120
 
121 121
 	for _, i := range n.iFaces {
122 122
 		// The master should match the srcname of the interface and the
... ...
@@ -152,7 +152,7 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
152 152
 		}
153 153
 	}
154 154
 
155
-	n.Lock()
155
+	n.mu.Lock()
156 156
 	if n.isDefault {
157 157
 		i.dstName = i.srcName
158 158
 	} else {
... ...
@@ -164,7 +164,7 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
164 164
 	isDefault := n.isDefault
165 165
 	nlh := n.nlHandle
166 166
 	nlhHost := ns.NlHandle()
167
-	n.Unlock()
167
+	n.mu.Unlock()
168 168
 
169 169
 	// If it is a bridge interface we have to create the bridge inside
170 170
 	// the namespace so don't try to lookup the interface using srcName
... ...
@@ -240,9 +240,9 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
240 240
 		return fmt.Errorf("error setting interface %q routes to %q: %v", iface.Attrs().Name, i.Routes(), err)
241 241
 	}
242 242
 
243
-	n.Lock()
243
+	n.mu.Lock()
244 244
 	n.iFaces = append(n.iFaces, i)
245
-	n.Unlock()
245
+	n.mu.Unlock()
246 246
 
247 247
 	n.checkLoV6()
248 248
 
... ...
@@ -252,10 +252,10 @@ func (n *Namespace) AddInterface(srcName, dstPrefix string, options ...IfaceOpti
252 252
 // RemoveInterface removes an interface from the namespace by renaming to
253 253
 // original name and moving it out of the sandbox.
254 254
 func (n *Namespace) RemoveInterface(i *Interface) error {
255
-	n.Lock()
255
+	n.mu.Lock()
256 256
 	isDefault := n.isDefault
257 257
 	nlh := n.nlHandle
258
-	n.Unlock()
258
+	n.mu.Unlock()
259 259
 
260 260
 	// Find the network interface identified by the DstName attribute.
261 261
 	iface, err := nlh.LinkByName(i.DstName())
... ...
@@ -287,14 +287,14 @@ func (n *Namespace) RemoveInterface(i *Interface) error {
287 287
 		}
288 288
 	}
289 289
 
290
-	n.Lock()
290
+	n.mu.Lock()
291 291
 	for index, intf := range i.ns.iFaces {
292 292
 		if intf == i {
293 293
 			i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
294 294
 			break
295 295
 		}
296 296
 	}
297
-	n.Unlock()
297
+	n.mu.Unlock()
298 298
 
299 299
 	n.checkLoV6()
300 300
 	return nil
... ...
@@ -328,7 +328,7 @@ type Namespace struct {
328 328
 	isDefault    bool
329 329
 	nlHandle     *netlink.Handle
330 330
 	loV6Enabled  bool
331
-	sync.Mutex
331
+	mu           sync.Mutex
332 332
 }
333 333
 
334 334
 // Interfaces returns the collection of Interface previously added with the AddInterface
... ...
@@ -450,8 +450,8 @@ func (n *Namespace) InvokeFunc(f func()) error {
450 450
 }
451 451
 
452 452
 func (n *Namespace) nsPath() string {
453
-	n.Lock()
454
-	defer n.Unlock()
453
+	n.mu.Lock()
454
+	defer n.mu.Unlock()
455 455
 
456 456
 	return n.path
457 457
 }
... ...
@@ -547,33 +547,33 @@ func (n *Namespace) Restore(ifsopt map[Iface][]IfaceOption, routes []*types.Stat
547 547
 				}
548 548
 			}
549 549
 			index++
550
-			n.Lock()
550
+			n.mu.Lock()
551 551
 			if index > n.nextIfIndex[name.DstPrefix] {
552 552
 				n.nextIfIndex[name.DstPrefix] = index
553 553
 			}
554 554
 			n.iFaces = append(n.iFaces, i)
555
-			n.Unlock()
555
+			n.mu.Unlock()
556 556
 		}
557 557
 	}
558 558
 
559 559
 	// restore routes
560 560
 	for _, r := range routes {
561
-		n.Lock()
561
+		n.mu.Lock()
562 562
 		n.staticRoutes = append(n.staticRoutes, r)
563
-		n.Unlock()
563
+		n.mu.Unlock()
564 564
 	}
565 565
 
566 566
 	// restore gateway
567 567
 	if len(gw) > 0 {
568
-		n.Lock()
568
+		n.mu.Lock()
569 569
 		n.gw = gw
570
-		n.Unlock()
570
+		n.mu.Unlock()
571 571
 	}
572 572
 
573 573
 	if len(gw6) > 0 {
574
-		n.Lock()
574
+		n.mu.Lock()
575 575
 		n.gwv6 = gw6
576
-		n.Unlock()
576
+		n.mu.Unlock()
577 577
 	}
578 578
 
579 579
 	return nil
... ...
@@ -586,7 +586,7 @@ func (n *Namespace) checkLoV6() {
586 586
 		action = "disable"
587 587
 	)
588 588
 
589
-	n.Lock()
589
+	n.mu.Lock()
590 590
 	for _, iface := range n.iFaces {
591 591
 		if iface.AddressIPv6() != nil {
592 592
 			enable = true
... ...
@@ -594,7 +594,7 @@ func (n *Namespace) checkLoV6() {
594 594
 			break
595 595
 		}
596 596
 	}
597
-	n.Unlock()
597
+	n.mu.Unlock()
598 598
 
599 599
 	if n.loV6Enabled == enable {
600 600
 		return
... ...
@@ -32,8 +32,8 @@ type neigh struct {
32 32
 }
33 33
 
34 34
 func (n *Namespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
35
-	n.Lock()
36
-	defer n.Unlock()
35
+	n.mu.Lock()
36
+	defer n.mu.Unlock()
37 37
 
38 38
 	for _, nh := range n.neighbors {
39 39
 		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
... ...
@@ -51,9 +51,9 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
51 51
 		return NeighborSearchError{dstIP, dstMac, false}
52 52
 	}
53 53
 
54
-	n.Lock()
54
+	n.mu.Lock()
55 55
 	nlh := n.nlHandle
56
-	n.Unlock()
56
+	n.mu.Unlock()
57 57
 
58 58
 	var linkIndex int
59 59
 	if nh.linkDst != "" {
... ...
@@ -96,14 +96,14 @@ func (n *Namespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr) error
96 96
 		}
97 97
 	}
98 98
 
99
-	n.Lock()
99
+	n.mu.Lock()
100 100
 	for i, neighbor := range n.neighbors {
101 101
 		if neighbor.dstIP.Equal(dstIP) && bytes.Equal(neighbor.dstMac, dstMac) {
102 102
 			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
103 103
 			break
104 104
 		}
105 105
 	}
106
-	n.Unlock()
106
+	n.mu.Unlock()
107 107
 	log.G(context.TODO()).Debugf("Neighbor entry deleted for IP %v, mac %v", dstIP, dstMac)
108 108
 
109 109
 	return nil
... ...
@@ -142,9 +142,9 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force boo
142 142
 		}
143 143
 	}
144 144
 
145
-	n.Lock()
145
+	n.mu.Lock()
146 146
 	nlh := n.nlHandle
147
-	n.Unlock()
147
+	n.mu.Unlock()
148 148
 
149 149
 	if nh.linkDst != "" {
150 150
 		iface, err = nlh.LinkByName(nh.linkDst)
... ...
@@ -176,9 +176,9 @@ func (n *Namespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force boo
176 176
 		return nil
177 177
 	}
178 178
 
179
-	n.Lock()
179
+	n.mu.Lock()
180 180
 	n.neighbors = append(n.neighbors, nh)
181
-	n.Unlock()
181
+	n.mu.Unlock()
182 182
 	log.G(context.TODO()).Debugf("Neighbor entry added for IP:%v, mac:%v on ifc:%s", dstIP, dstMac, nh.linkName)
183 183
 
184 184
 	return nil
... ...
@@ -10,16 +10,16 @@ import (
10 10
 
11 11
 // Gateway returns the IPv4 gateway for the sandbox.
12 12
 func (n *Namespace) Gateway() net.IP {
13
-	n.Lock()
14
-	defer n.Unlock()
13
+	n.mu.Lock()
14
+	defer n.mu.Unlock()
15 15
 
16 16
 	return n.gw
17 17
 }
18 18
 
19 19
 // GatewayIPv6 returns the IPv6 gateway for the sandbox.
20 20
 func (n *Namespace) GatewayIPv6() net.IP {
21
-	n.Lock()
22
-	defer n.Unlock()
21
+	n.mu.Lock()
22
+	defer n.mu.Unlock()
23 23
 
24 24
 	return n.gwv6
25 25
 }
... ...
@@ -28,8 +28,8 @@ func (n *Namespace) GatewayIPv6() net.IP {
28 28
 // directly connected routes are stored on the particular interface they
29 29
 // refer to.
30 30
 func (n *Namespace) StaticRoutes() []*types.StaticRoute {
31
-	n.Lock()
32
-	defer n.Unlock()
31
+	n.mu.Lock()
32
+	defer n.mu.Unlock()
33 33
 
34 34
 	routes := make([]*types.StaticRoute, len(n.staticRoutes))
35 35
 	for i, route := range n.staticRoutes {
... ...
@@ -41,15 +41,15 @@ func (n *Namespace) StaticRoutes() []*types.StaticRoute {
41 41
 }
42 42
 
43 43
 func (n *Namespace) setGateway(gw net.IP) {
44
-	n.Lock()
44
+	n.mu.Lock()
45 45
 	n.gw = gw
46
-	n.Unlock()
46
+	n.mu.Unlock()
47 47
 }
48 48
 
49 49
 func (n *Namespace) setGatewayIPv6(gwv6 net.IP) {
50
-	n.Lock()
50
+	n.mu.Lock()
51 51
 	n.gwv6 = gwv6
52
-	n.Unlock()
52
+	n.mu.Unlock()
53 53
 }
54 54
 
55 55
 // SetGateway sets the default IPv4 gateway for the sandbox.
... ...
@@ -173,9 +173,9 @@ func (n *Namespace) UnsetGatewayIPv6() error {
173 173
 
174 174
 	err := n.programGateway(gwv6, false)
175 175
 	if err == nil {
176
-		n.Lock()
176
+		n.mu.Lock()
177 177
 		n.gwv6 = net.IP{}
178
-		n.Unlock()
178
+		n.mu.Unlock()
179 179
 	}
180 180
 
181 181
 	return err
... ...
@@ -185,9 +185,9 @@ func (n *Namespace) UnsetGatewayIPv6() error {
185 185
 func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
186 186
 	err := n.programRoute(n.nsPath(), r.Destination, r.NextHop)
187 187
 	if err == nil {
188
-		n.Lock()
188
+		n.mu.Lock()
189 189
 		n.staticRoutes = append(n.staticRoutes, r)
190
-		n.Unlock()
190
+		n.mu.Unlock()
191 191
 	}
192 192
 	return err
193 193
 }
... ...
@@ -196,7 +196,7 @@ func (n *Namespace) AddStaticRoute(r *types.StaticRoute) error {
196 196
 func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
197 197
 	err := n.removeRoute(n.nsPath(), r.Destination, r.NextHop)
198 198
 	if err == nil {
199
-		n.Lock()
199
+		n.mu.Lock()
200 200
 		lastIndex := len(n.staticRoutes) - 1
201 201
 		for i, v := range n.staticRoutes {
202 202
 			if v == r {
... ...
@@ -207,7 +207,7 @@ func (n *Namespace) RemoveStaticRoute(r *types.StaticRoute) error {
207 207
 				break
208 208
 			}
209 209
 		}
210
-		n.Unlock()
210
+		n.mu.Unlock()
211 211
 	}
212 212
 	return err
213 213
 }