Remove echoReplier

Mirror the ICMPv6 echo implementation in ICMPv4 echo. This removes
unnecessary asynchrony, reduces copying, and reduces complexity.

PiperOrigin-RevId: 240394525
Change-Id: If8f53254154f86772f5e51159765aa23b3b328b8
This commit is contained in:
Tamir Duberstein
2019-03-26 11:45:01 -07:00
committed by Shentubot
parent 23a5306b5c
commit 9cd2b66f10
3 changed files with 11 additions and 42 deletions
+8 -34
View File
@@ -17,7 +17,6 @@ package ipv4
import (
"encoding/binary"
"gvisor.googlesource.com/gvisor/pkg/tcpip"
"gvisor.googlesource.com/gvisor/pkg/tcpip/buffer"
"gvisor.googlesource.com/gvisor/pkg/tcpip/header"
"gvisor.googlesource.com/gvisor/pkg/tcpip/stack"
@@ -67,17 +66,17 @@ func (e *endpoint) handleICMP(r *stack.Route, netHeader buffer.View, vv buffer.V
if len(v) < header.ICMPv4EchoMinimumSize {
return
}
echoPayload := vv.ToView()
echoPayload.TrimFront(header.ICMPv4MinimumSize)
req := echoRequest{r: r.Clone(), v: echoPayload}
select {
case e.echoRequests <- req:
default:
req.r.Release()
}
// It's possible that a raw socket expects to receive this.
e.dispatcher.DeliverTransportPacket(r, header.ICMPv4ProtocolNumber, netHeader, vv)
vv.TrimFront(header.ICMPv4EchoMinimumSize)
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.ICMPv4EchoMinimumSize)
pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4EchoMinimumSize))
copy(pkt, h)
pkt.SetType(header.ICMPv4EchoReply)
pkt.SetChecksum(^header.Checksum(pkt, header.ChecksumVV(vv, 0)))
r.WritePacket(hdr, vv, header.ICMPv4ProtocolNumber, r.DefaultTTL())
case header.ICMPv4EchoReply:
if len(v) < header.ICMPv4EchoMinimumSize {
return
@@ -100,28 +99,3 @@ func (e *endpoint) handleICMP(r *stack.Route, netHeader buffer.View, vv buffer.V
}
// TODO: Handle other ICMP types.
}
type echoRequest struct {
r stack.Route
v buffer.View
}
func (e *endpoint) echoReplier() {
for req := range e.echoRequests {
sendPing4(&req.r, 0, req.v)
req.r.Release()
}
}
func sendPing4(r *stack.Route, code byte, data buffer.View) *tcpip.Error {
hdr := buffer.NewPrependable(header.ICMPv4EchoMinimumSize + int(r.MaxHeaderLength()))
icmpv4 := header.ICMPv4(hdr.Prepend(header.ICMPv4EchoMinimumSize))
icmpv4.SetType(header.ICMPv4EchoReply)
icmpv4.SetCode(code)
copy(icmpv4[header.ICMPv4MinimumSize:], data)
data = data[header.ICMPv4EchoMinimumSize-header.ICMPv4MinimumSize:]
icmpv4.SetChecksum(^header.Checksum(icmpv4, header.Checksum(data, 0)))
return r.WritePacket(hdr, data.ToVectorisedView(), header.ICMPv4ProtocolNumber, r.DefaultTTL())
}
+1 -7
View File
@@ -51,7 +51,6 @@ type endpoint struct {
id stack.NetworkEndpointID
linkEP stack.LinkEndpoint
dispatcher stack.TransportDispatcher
echoRequests chan echoRequest
fragmentation *fragmentation.Fragmentation
}
@@ -62,12 +61,9 @@ func (p *protocol) NewEndpoint(nicid tcpip.NICID, addr tcpip.Address, linkAddrCa
id: stack.NetworkEndpointID{LocalAddress: addr},
linkEP: linkEP,
dispatcher: dispatcher,
echoRequests: make(chan echoRequest, 10),
fragmentation: fragmentation.NewFragmentation(fragmentation.HighFragThreshold, fragmentation.LowFragThreshold, fragmentation.DefaultReassembleTimeout),
}
go e.echoReplier()
return e, nil
}
@@ -174,9 +170,7 @@ func (e *endpoint) HandlePacket(r *stack.Route, vv buffer.VectorisedView) {
}
// Close cleans up resources associated with the endpoint.
func (e *endpoint) Close() {
close(e.echoRequests)
}
func (e *endpoint) Close() {}
type protocol struct{}
+2 -1
View File
@@ -136,8 +136,9 @@ func (e *endpoint) handleICMP(r *stack.Route, netHeader buffer.View, vv buffer.V
if len(v) < header.ICMPv6EchoMinimumSize {
return
}
vv.TrimFront(header.ICMPv6EchoMinimumSize)
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6EchoMinimumSize)
hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.ICMPv6EchoMinimumSize)
pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6EchoMinimumSize))
copy(pkt, h)
pkt.SetType(header.ICMPv6EchoReply)