Browse code

libnetwork/osl: nwIface: remove mutex altogether

The mutex is only used on reads, but there's nothing protecting writes,
and it looks like nothing is mutating fields after creation, so let's
remove this altogether.

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

Sebastiaan van Stijn authored on 2023/08/03 08:45:45
Showing 1 changed files
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"context"
5 5
 	"fmt"
6 6
 	"net"
7
-	"sync"
8 7
 	"syscall"
9 8
 	"time"
10 9
 
... ...
@@ -27,107 +26,66 @@ type nwIface struct {
27 27
 	routes      []*net.IPNet
28 28
 	bridge      bool
29 29
 	ns          *networkNamespace
30
-	mu          sync.Mutex
31 30
 }
32 31
 
33 32
 func (i *nwIface) SrcName() string {
34
-	i.mu.Lock()
35
-	defer i.mu.Unlock()
36
-
37 33
 	return i.srcName
38 34
 }
39 35
 
40 36
 func (i *nwIface) DstName() string {
41
-	i.mu.Lock()
42
-	defer i.mu.Unlock()
43
-
44 37
 	return i.dstName
45 38
 }
46 39
 
47 40
 func (i *nwIface) DstMaster() string {
48
-	i.mu.Lock()
49
-	defer i.mu.Unlock()
50
-
51 41
 	return i.dstMaster
52 42
 }
53 43
 
54 44
 func (i *nwIface) Bridge() bool {
55
-	i.mu.Lock()
56
-	defer i.mu.Unlock()
57
-
58 45
 	return i.bridge
59 46
 }
60 47
 
61 48
 func (i *nwIface) Master() string {
62
-	i.mu.Lock()
63
-	defer i.mu.Unlock()
64
-
65 49
 	return i.master
66 50
 }
67 51
 
68 52
 func (i *nwIface) MacAddress() net.HardwareAddr {
69
-	i.mu.Lock()
70
-	defer i.mu.Unlock()
71
-
72 53
 	return types.GetMacCopy(i.mac)
73 54
 }
74 55
 
75 56
 func (i *nwIface) Address() *net.IPNet {
76
-	i.mu.Lock()
77
-	defer i.mu.Unlock()
78
-
79 57
 	return types.GetIPNetCopy(i.address)
80 58
 }
81 59
 
82 60
 func (i *nwIface) AddressIPv6() *net.IPNet {
83
-	i.mu.Lock()
84
-	defer i.mu.Unlock()
85
-
86 61
 	return types.GetIPNetCopy(i.addressIPv6)
87 62
 }
88 63
 
89 64
 func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
90
-	i.mu.Lock()
91
-	defer i.mu.Unlock()
92
-
93 65
 	return i.llAddrs
94 66
 }
95 67
 
96 68
 func (i *nwIface) Routes() []*net.IPNet {
97
-	i.mu.Lock()
98
-	defer i.mu.Unlock()
99
-
100 69
 	routes := make([]*net.IPNet, len(i.routes))
101 70
 	for index, route := range i.routes {
102
-		r := types.GetIPNetCopy(route)
103
-		routes[index] = r
71
+		routes[index] = types.GetIPNetCopy(route)
104 72
 	}
105 73
 
106 74
 	return routes
107 75
 }
108 76
 
109 77
 func (n *networkNamespace) Interfaces() []Interface {
110
-	n.Lock()
111
-	defer n.Unlock()
112
-
113 78
 	ifaces := make([]Interface, len(n.iFaces))
114
-
115 79
 	for i, iface := range n.iFaces {
116 80
 		ifaces[i] = iface
117 81
 	}
118
-
119 82
 	return ifaces
120 83
 }
121 84
 
122 85
 func (i *nwIface) Remove() error {
123
-	i.mu.Lock()
124
-	n := i.ns
125
-	i.mu.Unlock()
126
-
127
-	n.Lock()
128
-	isDefault := n.isDefault
129
-	nlh := n.nlHandle
130
-	n.Unlock()
86
+	i.ns.Lock()
87
+	isDefault := i.ns.isDefault
88
+	nlh := i.ns.nlHandle
89
+	i.ns.Unlock()
131 90
 
132 91
 	// Find the network interface identified by the DstName attribute.
133 92
 	iface, err := nlh.LinkByName(i.DstName())
... ...
@@ -159,29 +117,25 @@ func (i *nwIface) Remove() error {
159 159
 		}
160 160
 	}
161 161
 
162
-	n.Lock()
163
-	for index, intf := range n.iFaces {
162
+	i.ns.Lock()
163
+	for index, intf := range i.ns.iFaces {
164 164
 		if intf == i {
165
-			n.iFaces = append(n.iFaces[:index], n.iFaces[index+1:]...)
165
+			i.ns.iFaces = append(i.ns.iFaces[:index], i.ns.iFaces[index+1:]...)
166 166
 			break
167 167
 		}
168 168
 	}
169
-	n.Unlock()
169
+	i.ns.Unlock()
170 170
 
171
-	n.checkLoV6()
171
+	i.ns.checkLoV6()
172 172
 
173 173
 	return nil
174 174
 }
175 175
 
176 176
 // Returns the sandbox's side veth interface statistics
177 177
 func (i *nwIface) Statistics() (*types.InterfaceStatistics, error) {
178
-	i.mu.Lock()
179
-	n := i.ns
180
-	i.mu.Unlock()
181
-
182
-	l, err := n.nlHandle.LinkByName(i.DstName())
178
+	l, err := i.ns.nlHandle.LinkByName(i.DstName())
183 179
 	if err != nil {
184
-		return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), n.path, err)
180
+		return nil, fmt.Errorf("failed to retrieve the statistics for %s in netns %s: %v", i.DstName(), i.ns.path, err)
185 181
 	}
186 182
 
187 183
 	stats := l.Attrs().Statistics