Skip to content

Commit 0b666fe

Browse files
committed
netstack/seamless: granular locking
1 parent fcb8daa commit 0b666fe

1 file changed

Lines changed: 55 additions & 36 deletions

File tree

intra/netstack/seamless.go

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,113 +104,132 @@ func PcapModes() string {
104104

105105
// Swap implements FdSwapper.
106106
func (l *linkSwap) Swap(fd int) error {
107-
l.Lock()
108-
defer l.Unlock()
109-
110107
if l.FdSwapper == nil {
111108
return errNoFdSwapper
112109
}
113110

114111
err := l.FdSwapper.Swap(fd)
115112
if errors.Is(err, errNeedsNewEndpoint) {
116-
umtu := uint32(l.e.MTU())
113+
umtu := uint32(l.MTU())
117114
opt := Options{
118115
FDs: []int{fd},
119116
MTU: umtu,
120117
}
121-
core.Go("linkFdSwap."+strconv.Itoa(fd), l.e.Close)
122-
l.e, err = newFdbasedInjectableEndpoint(&opt)
118+
if ep, err := newFdbasedInjectableEndpoint(&opt); err == nil {
119+
l.Lock()
120+
core.Go("linkFdSwap."+strconv.Itoa(fd), l.e.Close)
121+
l.e = ep
122+
l.Unlock()
123+
} else {
124+
log.E("netstack: linkFdSwap(%d); err %v", fd, err)
125+
return err
126+
}
123127
}
124128

125129
return err
126130
}
127131

128132
func (l *linkSwap) MTU() uint32 {
129133
l.Lock()
130-
defer l.Unlock()
131-
return l.e.MTU()
134+
e := l.e
135+
l.Unlock()
136+
return e.MTU()
132137
}
133138

134139
func (l *linkSwap) SetMTU(mtu uint32) {
135140
l.Lock()
136-
defer l.Unlock()
137-
l.e.SetMTU(mtu)
141+
e := l.e
142+
l.Unlock()
143+
e.SetMTU(mtu)
138144
}
139145

140146
func (l *linkSwap) MaxHeaderLength() uint16 {
141147
l.Lock()
142-
defer l.Unlock()
143-
return l.e.MaxHeaderLength()
148+
e := l.e
149+
l.Unlock()
150+
return e.MaxHeaderLength()
144151
}
145152

146153
func (l *linkSwap) LinkAddress() tcpip.LinkAddress {
147154
l.Lock()
148-
defer l.Unlock()
149-
return l.e.LinkAddress()
155+
e := l.e
156+
l.Unlock()
157+
return e.LinkAddress()
150158
}
151159

152160
func (l *linkSwap) SetLinkAddress(addr tcpip.LinkAddress) {
153161
l.Lock()
154-
defer l.Unlock()
155-
l.e.SetLinkAddress(addr)
162+
e := l.e
163+
l.Unlock()
164+
e.SetLinkAddress(addr)
156165
}
157166

158167
func (l *linkSwap) Capabilities() stack.LinkEndpointCapabilities {
159168
l.Lock()
160-
defer l.Unlock()
161-
return l.e.Capabilities()
169+
e := l.e
170+
l.Unlock()
171+
return e.Capabilities()
162172
}
163173

164174
func (l *linkSwap) Attach(dispatcher stack.NetworkDispatcher) {
165175
l.Lock()
166-
defer l.Unlock()
167-
l.e.Attach(dispatcher)
176+
e := l.e
177+
l.Unlock()
178+
e.Attach(dispatcher)
168179
}
169180

170181
func (l *linkSwap) IsAttached() bool {
171182
l.Lock()
172-
defer l.Unlock()
173-
return l.e.IsAttached()
183+
e := l.e
184+
l.Unlock()
185+
return e.IsAttached()
174186
}
175187

176188
func (l *linkSwap) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
177189
l.Lock()
178-
defer l.Unlock()
179-
return l.e.WritePackets(pkts)
190+
e := l.e
191+
l.Unlock()
192+
return e.WritePackets(pkts)
180193
}
181194

182195
func (l *linkSwap) Wait() {
183196
l.Lock()
184-
defer l.Unlock()
185-
l.e.Wait()
197+
e := l.e
198+
l.Unlock()
199+
e.Wait()
186200
}
187201

188202
func (l *linkSwap) ARPHardwareType() header.ARPHardwareType {
189203
l.Lock()
190-
defer l.Unlock()
191-
return l.e.ARPHardwareType()
204+
e := l.e
205+
l.Unlock()
206+
return e.ARPHardwareType()
192207
}
193208

194209
func (l *linkSwap) AddHeader(pkt *stack.PacketBuffer) {
195210
l.Lock()
196-
defer l.Unlock()
197-
l.e.AddHeader(pkt)
211+
e := l.e
212+
l.Unlock()
213+
e.AddHeader(pkt)
198214
}
199215

200216
func (l *linkSwap) ParseHeader(pkt *stack.PacketBuffer) bool {
201217
l.Lock()
202-
defer l.Unlock()
203-
return l.e.ParseHeader(pkt)
218+
e := l.e
219+
l.Unlock()
220+
return e.ParseHeader(pkt)
204221
}
205222

206223
func (l *linkSwap) Close() {
207224
l.Lock()
208-
defer l.Unlock()
209-
l.e.Close()
225+
e := l.e
226+
l.Unlock()
227+
e.Close()
210228
}
211229

212230
func (l *linkSwap) SetOnCloseAction(f func()) {
213231
l.Lock()
214-
defer l.Unlock()
215-
l.e.SetOnCloseAction(f)
232+
e := l.e
233+
l.Unlock()
234+
e.SetOnCloseAction(f)
216235
}

0 commit comments

Comments
 (0)