Skip to content

Commit e142bd0

Browse files
committed
netstack/icmp: mv icmp responder to processors
1 parent 6ab1666 commit e142bd0

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

intra/netstack/dispatchers.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ type readVDispatcher struct {
178178
closed atomic.Bool // closed is set to true when fd is closed.
179179
once sync.Once // Ensures stop() is called only once.
180180
mgr *supervisor
181-
icmp icmpResponder
182181
}
183182

184183
var _ linkDispatcher = (*readVDispatcher)(nil)
@@ -187,10 +186,9 @@ var _ linkDispatcher = (*readVDispatcher)(nil)
187186
// fd and dispatches them to endpoint e. It assumes ownership of fd but not of e.
188187
func newReadVDispatcher(f *fds, e *endpoint) (linkDispatcher, error) {
189188
d := &readVDispatcher{
190-
e: e,
191-
buf: newIovecBuffer(bufcfg),
192-
mgr: newSupervisor(e, f.tun()),
193-
icmp: newICMPResponder(e),
189+
e: e,
190+
buf: newIovecBuffer(bufcfg),
191+
mgr: newSupervisor(e, icmp, f.tun()),
194192
}
195193
d.mgr.start()
196194

@@ -319,12 +317,6 @@ func (d *readVDispatcher) io(fds *fds) (bool, tcpip.Error) {
319317
})
320318
defer pkt.DecRef()
321319

322-
if d.icmp.ok() {
323-
if d.icmp.respond(pkt) {
324-
return cont, nil
325-
}
326-
}
327-
328320
var iseth = d.e.hdrSize > 0 // hdrSize always zero; unused
329321
if iseth {
330322
if !d.e.parseHeader(pkt) {

intra/netstack/forwarders.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ type processor struct {
134134
pkts stack.PacketBufferList
135135

136136
e stack.InjectableLinkEndpoint
137+
icmp *icmpResponder
137138
sleeper sleep.Sleeper
138139
packetWaker sleep.Waker
139140
closeWaker sleep.Waker
@@ -176,6 +177,9 @@ func (p *processor) deliverPackets() {
176177
pkt := p.pkts.PopFront()
177178
p.mu.Unlock()
178179
if pkt != nil {
180+
if p.icmp.respond(pkt) {
181+
return
182+
}
179183
p.e.InjectInbound(pkt.NetworkProtocolNumber, pkt)
180184
pkt.DecRef()
181185
}
@@ -191,6 +195,7 @@ func (p *processor) deliverPackets() {
191195
// goroutines.
192196
type supervisor struct {
193197
processors []processor
198+
icmp *icmpResponder
194199
seed uint32
195200
wg sync.WaitGroup
196201
sid *core.Volatile[int] // tun fd for diagnostics
@@ -199,21 +204,24 @@ type supervisor struct {
199204

200205
// newSupervisor creates a new supervisor for the processors of endpoint e.
201206
func newSupervisor(e stack.InjectableLinkEndpoint, sid int) *supervisor {
207+
icmp := newICMPResponder(e)
208+
202209
m := &supervisor{
203210
seed: rand.Uint32(),
204211
sid: core.NewVolatile(sid),
205212
ready: make([]bool, maxForwarders),
206213
processors: make([]processor, maxForwarders),
214+
icmp: &icmp,
207215
wg: sync.WaitGroup{},
208216
}
209217

210218
m.wg.Add(maxForwarders)
211-
212219
for i := range m.processors {
213220
p := &m.processors[i]
214221
p.sleeper.AddWaker(&p.packetWaker)
215222
p.sleeper.AddWaker(&p.closeWaker)
216223
p.e = e
224+
p.icmp = &icmp
217225
}
218226

219227
return m
@@ -314,6 +322,7 @@ func (m *supervisor) queuePacket(pkt *stack.PacketBuffer, hasEthHeader bool) {
314322

315323
// stop stops all processor goroutines.
316324
func (m *supervisor) stop() {
325+
m.icmp.stop()
317326
sid := m.tunid()
318327
start := time.Now()
319328
log.D("ns: tun(%d): forwarder: stopping %d procs", sid, len(m.processors))

intra/netstack/icmp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func OutboundICMP(id string, s *stack.Stack, hdl GICMPHandler) {
4444
forwarder := newIcmpForwarder(id, s, hdl)
4545
s.SetTransportProtocolHandler(icmp.ProtocolNumber4, forwarder.reply4)
4646
s.SetTransportProtocolHandler(icmp.ProtocolNumber6, forwarder.reply6)
47+
// TODO: the handler must only be set for the "main" netstack
4748
setICMPEchoHandler(forwarder)
4849
}
4950

intra/netstack/icmpecho.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ func setICMPEchoHandler(h *icmpForwarder) {
3636
// ICMP implementation) to forward the ping and then injects the response back
3737
// into the TUN device.
3838
type icmpResponder struct {
39-
ep *endpoint
39+
ep stack.LinkEndpoint
4040
open atomic.Bool
4141
}
4242

4343
func (r *icmpResponder) stop() {
44-
r.ep = nil
4544
r.open.Store(false)
4645
}
4746

48-
func newICMPResponder(ep *endpoint) (r icmpResponder) {
49-
if ep == nil {
47+
func newICMPResponder(ep stack.LinkEndpoint) (r icmpResponder) {
48+
if ep == nil || core.IsNil(ep) {
5049
return
5150
}
5251
r.ep = ep
@@ -55,7 +54,7 @@ func newICMPResponder(ep *endpoint) (r icmpResponder) {
5554
}
5655

5756
func (r *icmpResponder) ok() bool {
58-
return r != nil && r.open.Load() && r.ep != nil
57+
return r != nil && r.open.Load()
5958
}
6059

6160
func (r *icmpResponder) respond(pkt *stack.PacketBuffer) (handled bool) {

0 commit comments

Comments
 (0)