Don't pass link addresses in rx path

...as they are not used in all cases expect in the packet endpoint
which can get the link address directly from the link header.

PiperOrigin-RevId: 424427195
This commit is contained in:
Ghanan Gowripalan
2022-01-26 13:24:35 -08:00
committed by gVisor bot
parent c1fa5be7ac
commit abd993f608
27 changed files with 77 additions and 175 deletions
+1 -6
View File
@@ -188,12 +188,7 @@ func (e *Endpoint) NumQueued() int {
// InjectInbound injects an inbound packet.
func (e *Endpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
e.InjectLinkAddr(protocol, "", pkt)
}
// InjectLinkAddr injects an inbound packet with a remote link address.
func (e *Endpoint) InjectLinkAddr(protocol tcpip.NetworkProtocolNumber, remote tcpip.LinkAddress, pkt *stack.PacketBuffer) {
e.dispatcher.DeliverNetworkPacket(remote, "" /* local */, protocol, pkt)
e.dispatcher.DeliverNetworkPacket(protocol, pkt)
}
// Attach saves the stack network-layer dispatcher for use later when packets
+2 -3
View File
@@ -59,7 +59,7 @@ func (e *Endpoint) MTU() uint32 {
}
// DeliverNetworkPacket implements stack.NetworkDispatcher.
func (e *Endpoint) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (e *Endpoint) DeliverNetworkPacket(_ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
if !ok {
return
@@ -67,8 +67,7 @@ func (e *Endpoint) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkP
// Note, there is no need to check the destination link address here since
// the ethernet hardware filters frames based on their destination addresses.
eth := header.Ethernet(hdr)
e.Endpoint.DeliverNetworkPacket(eth.SourceAddress() /* remote */, eth.DestinationAddress() /* local */, eth.Type() /* protocol */, pkt)
e.Endpoint.DeliverNetworkPacket(header.Ethernet(hdr).Type() /* protocol */, pkt)
}
// Capabilities implements stack.LinkEndpoint.
+2 -5
View File
@@ -35,13 +35,10 @@ type testNetworkDispatcher struct {
networkPackets int
}
func (t *testNetworkDispatcher) DeliverNetworkPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) {
func (t *testNetworkDispatcher) DeliverNetworkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
t.networkPackets++
}
func (*testNetworkDispatcher) DeliverOutboundPacket(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber, _ *stack.PacketBuffer) {
}
func TestDeliverNetworkPacket(t *testing.T) {
const (
linkAddr = tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06")
@@ -68,7 +65,7 @@ func TestDeliverNetworkPacket(t *testing.T) {
})
p := stack.NewPacketBuffer(stack.PacketBufferOptions{Data: eth.ToVectorisedView()})
defer p.DecRef()
e.DeliverNetworkPacket("", "", 0, p)
e.DeliverNetworkPacket(0, p)
if networkDispatcher.networkPackets != 1 {
t.Fatalf("got networkDispatcher.networkPackets = %d, want = 1", networkDispatcher.networkPackets)
}
+1 -1
View File
@@ -764,7 +764,7 @@ func (e *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
// InjectInbound injects an inbound packet.
func (e *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
e.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, protocol, pkt)
e.dispatcher.DeliverNetworkPacket(protocol, pkt)
}
// NewInjectable creates a new fd-based InjectableEndpoint.
+3 -14
View File
@@ -47,7 +47,6 @@ const (
)
type packetInfo struct {
Raddr tcpip.LinkAddress
Proto tcpip.NetworkProtocolNumber
Contents *stack.PacketBuffer
}
@@ -134,12 +133,8 @@ func (c *context) cleanup() {
}
}
func (c *context) DeliverNetworkPacket(remote tcpip.LinkAddress, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
c.ch <- packetInfo{remote, protocol, pkt}
}
func (c *context) DeliverOutboundPacket(remote tcpip.LinkAddress, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
panic("unimplemented")
func (c *context) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
c.ch <- packetInfo{protocol, pkt}
}
func TestNoEthernetProperties(t *testing.T) {
@@ -410,13 +405,11 @@ func TestDeliverPacket(t *testing.T) {
select {
case pi := <-c.ch:
want := packetInfo{
Raddr: raddr,
Proto: proto,
Contents: wantPkt,
}
if !eth {
want.Proto = header.IPv4ProtocolNumber
want.Raddr = ""
}
checkPacketInfoEqual(t, pi, want)
case <-time.After(10 * time.Second):
@@ -569,14 +562,10 @@ type fakeNetworkDispatcher struct {
pkts []*stack.PacketBuffer
}
func (d *fakeNetworkDispatcher) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (d *fakeNetworkDispatcher) DeliverNetworkPacket(_ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
d.pkts = append(d.pkts, pkt)
}
func (d *fakeNetworkDispatcher) DeliverOutboundPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
panic("unimplemented")
}
func TestDispatchPacketFormat(t *testing.T) {
for _, test := range []struct {
name string
+3 -9
View File
@@ -169,15 +169,9 @@ func (d *packetMMapDispatcher) dispatch() (bool, tcpip.Error) {
if err != nil || stopped {
return false, err
}
var (
p tcpip.NetworkProtocolNumber
remote, local tcpip.LinkAddress
)
var p tcpip.NetworkProtocolNumber
if d.e.hdrSize > 0 {
eth := header.Ethernet(pkt)
p = eth.Type()
remote = eth.SourceAddress()
local = eth.DestinationAddress()
p = header.Ethernet(pkt).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
@@ -200,6 +194,6 @@ func (d *packetMMapDispatcher) dispatch() (bool, tcpip.Error) {
panic(fmt.Sprintf("LinkHeader().Consume(%d) must succeed", d.e.hdrSize))
}
}
d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pbuf)
d.e.dispatcher.DeliverNetworkPacket(p, pbuf)
return true, nil
}
+6 -18
View File
@@ -183,19 +183,13 @@ func (d *readVDispatcher) dispatch() (bool, tcpip.Error) {
})
defer pkt.DecRef()
var (
p tcpip.NetworkProtocolNumber
remote, local tcpip.LinkAddress
)
var p tcpip.NetworkProtocolNumber
if d.e.hdrSize > 0 {
hdr, ok := pkt.LinkHeader().Consume(d.e.hdrSize)
if !ok {
return false, nil
}
eth := header.Ethernet(hdr)
p = eth.Type()
remote = eth.SourceAddress()
local = eth.DestinationAddress()
p = header.Ethernet(hdr).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
@@ -214,7 +208,7 @@ func (d *readVDispatcher) dispatch() (bool, tcpip.Error) {
}
}
d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pkt)
d.e.dispatcher.DeliverNetworkPacket(p, pkt)
return true, nil
}
@@ -298,19 +292,13 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) {
// Mark that this iovec has been processed.
d.msgHdrs[k].Msg.Iovlen = 0
var (
p tcpip.NetworkProtocolNumber
remote, local tcpip.LinkAddress
)
var p tcpip.NetworkProtocolNumber
if d.e.hdrSize > 0 {
hdr, ok := pkt.LinkHeader().Consume(d.e.hdrSize)
if !ok {
return false, nil
}
eth := header.Ethernet(hdr)
p = eth.Type()
remote = eth.SourceAddress()
local = eth.DestinationAddress()
p = header.Ethernet(hdr).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
@@ -331,7 +319,7 @@ func (d *recvMMsgDispatcher) dispatch() (bool, tcpip.Error) {
}
}
d.e.dispatcher.DeliverNetworkPacket(remote, local, p, pkt)
d.e.dispatcher.DeliverNetworkPacket(p, pkt)
}
return true, nil
+1 -1
View File
@@ -106,7 +106,7 @@ func (e *endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error {
Data: data,
})
defer newPkt.DecRef()
e.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, pkt.NetworkProtocolNumber, newPkt)
e.dispatcher.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt)
return nil
}
+1 -1
View File
@@ -81,7 +81,7 @@ func (m *InjectableEndpoint) IsAttached() bool {
// InjectInbound implements stack.InjectableLinkEndpoint.
func (m *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
m.dispatcher.DeliverNetworkPacket("" /* remote */, "" /* local */, protocol, pkt)
m.dispatcher.DeliverNetworkPacket(protocol, pkt)
}
// WritePackets writes outbound packets to the appropriate
+2 -2
View File
@@ -51,12 +51,12 @@ func (e *Endpoint) Init(child stack.LinkEndpoint, embedder stack.NetworkDispatch
}
// DeliverNetworkPacket implements stack.NetworkDispatcher.
func (e *Endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
e.mu.RLock()
d := e.dispatcher
e.mu.RUnlock()
if d != nil {
d.DeliverNetworkPacket(remote, local, protocol, pkt)
d.DeliverNetworkPacket(protocol, pkt)
}
}
+3 -9
View File
@@ -54,17 +54,11 @@ type counterDispatcher struct {
var _ stack.NetworkDispatcher = (*counterDispatcher)(nil)
func (d *counterDispatcher) DeliverNetworkPacket(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
func (d *counterDispatcher) DeliverNetworkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
d.count++
}
func (d *counterDispatcher) DeliverOutboundPacket(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) {
panic("unimplemented")
}
func TestNestedLinkEndpoint(t *testing.T) {
const emptyAddress = tcpip.LinkAddress("")
var (
childEP childEndpoint
nestedEP parentEndpoint
@@ -92,7 +86,7 @@ func TestNestedLinkEndpoint(t *testing.T) {
{
p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
nestedEP.DeliverNetworkPacket(emptyAddress, emptyAddress, header.IPv4ProtocolNumber, p)
nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p)
p.DecRef()
if disp.count != 1 {
t.Errorf("After first packet with dispatcher attached, got disp.count = %d, want = 1", disp.count)
@@ -110,7 +104,7 @@ func TestNestedLinkEndpoint(t *testing.T) {
{
disp.count = 0
p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
nestedEP.DeliverNetworkPacket(emptyAddress, emptyAddress, header.IPv4ProtocolNumber, p)
nestedEP.DeliverNetworkPacket(header.IPv4ProtocolNumber, p)
p.DecRef()
if disp.count != 0 {
t.Errorf("After second packet with dispatcher detached, got disp.count = %d, want = 0", disp.count)
+1 -10
View File
@@ -53,20 +53,11 @@ func (e *Endpoint) deliverPackets(pkts stack.PacketBufferList) {
return
}
// Note that the local address from the perspective of this endpoint is the
// remote address from the perspective of the other end of the pipe
// (e.linked). Similarly, the remote address from the perspective of this
// endpoint is the local address on the other end.
//
// Deliver the packet in a new goroutine to escape this goroutine's stack and
// avoid a deadlock when a packet triggers a response which leads the stack to
// try and take a lock it already holds.
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()),
})
r := pkt.EgressRoute
e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, pkt.NetworkProtocolNumber, newPkt)
e.linked.dispatcher.DeliverNetworkPacket(pkt.NetworkProtocolNumber, newPkt)
newPkt.DecRef()
}
}
+2 -6
View File
@@ -432,7 +432,6 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) {
}
}
var src, dst tcpip.LinkAddress
var proto tcpip.NetworkProtocolNumber
if e.addr != "" {
hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
@@ -440,10 +439,7 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) {
pkt.DecRef()
continue
}
eth := header.Ethernet(hdr)
src = eth.SourceAddress()
dst = eth.DestinationAddress()
proto = eth.Type()
proto = header.Ethernet(hdr).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
@@ -465,7 +461,7 @@ func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) {
}
// Send packet up the stack.
d.DeliverNetworkPacket(src, dst, proto, pkt)
d.DeliverNetworkPacket(proto, pkt)
pkt.DecRef()
}
+2 -6
View File
@@ -326,7 +326,6 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) {
continue
}
}
var src, dst tcpip.LinkAddress
var proto tcpip.NetworkProtocolNumber
if e.addr != "" {
hdr, ok := pkt.LinkHeader().Consume(header.EthernetMinimumSize)
@@ -334,10 +333,7 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) {
pkt.DecRef()
continue
}
eth := header.Ethernet(hdr)
src = eth.SourceAddress()
dst = eth.DestinationAddress()
proto = eth.Type()
proto = header.Ethernet(hdr).Type()
} else {
// We don't get any indication of what the packet is, so try to guess
// if it's an IPv4 or IPv6 packet.
@@ -358,7 +354,7 @@ func (e *serverEndpoint) dispatchLoop(d stack.NetworkDispatcher) {
}
}
// Send packet up the stack.
d.DeliverNetworkPacket(src, dst, proto, pkt)
d.DeliverNetworkPacket(proto, pkt)
pkt.DecRef()
}
+1 -7
View File
@@ -80,7 +80,6 @@ func (q *queueBuffers) cleanup() {
}
type packetInfo struct {
addr tcpip.LinkAddress
proto tcpip.NetworkProtocolNumber
data buffer.View
linkHeader buffer.View
@@ -145,10 +144,9 @@ func newTestContext(t *testing.T, mtu, bufferSize uint32, addr tcpip.LinkAddress
return c
}
func (c *testContext) DeliverNetworkPacket(remoteLinkAddr, localLinkAddr tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (c *testContext) DeliverNetworkPacket(proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
c.mu.Lock()
c.packets = append(c.packets, packetInfo{
addr: remoteLinkAddr,
proto: proto,
data: pkt.Data().AsRange().ToOwnedView(),
})
@@ -157,10 +155,6 @@ func (c *testContext) DeliverNetworkPacket(remoteLinkAddr, localLinkAddr tcpip.L
c.packetCh <- struct{}{}
}
func (c *testContext) DeliverOutboundPacket(remoteLinkAddr, localLinkAddr tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
panic("unimplemented")
}
func (c *testContext) cleanup() {
c.ep.Close()
closeFDs(c.txCfg)
+2 -2
View File
@@ -135,9 +135,9 @@ func NewWithWriter(lower stack.LinkEndpoint, writer io.Writer, snapLen uint32) (
// DeliverNetworkPacket implements the stack.NetworkDispatcher interface. It is
// called by the link-layer endpoint being wrapped when a packet arrives, and
// logs the packet before forwarding to the actual dispatcher.
func (e *endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (e *endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
e.dumpPacket(directionRecv, protocol, pkt)
e.Endpoint.DeliverNetworkPacket(remote, local, protocol, pkt)
e.Endpoint.DeliverNetworkPacket(protocol, pkt)
}
func (e *endpoint) dumpPacket(dir direction, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
+1 -10
View File
@@ -219,22 +219,13 @@ func (d *Device) Write(data []byte) (int64, error) {
}
}
// Try to determine remote link address, default zero.
var remote tcpip.LinkAddress
switch {
case ethHdr != nil:
remote = ethHdr.SourceAddress()
default:
remote = tcpip.LinkAddress(zeroMAC[:])
}
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: len(ethHdr),
Data: buffer.View(data).ToVectorisedView(),
})
defer pkt.DecRef()
copy(pkt.LinkHeader().Push(len(ethHdr)), ethHdr)
endpoint.InjectLinkAddr(protocol, remote, pkt)
endpoint.InjectInbound(protocol, pkt)
return dataLen, nil
}
+2 -2
View File
@@ -50,12 +50,12 @@ func New(lower stack.LinkEndpoint) *Endpoint {
// It is called by the link-layer endpoint being wrapped when a packet arrives,
// and only forwards to the actual dispatcher if Wait or WaitDispatch haven't
// been called.
func (e *Endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (e *Endpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
if !e.dispatchGate.Enter() {
return
}
e.dispatcher.DeliverNetworkPacket(remote, local, protocol, pkt)
e.dispatcher.DeliverNetworkPacket(protocol, pkt)
e.dispatchGate.Leave()
}
+4 -8
View File
@@ -40,14 +40,10 @@ type countedEndpoint struct {
dispatcher stack.NetworkDispatcher
}
func (e *countedEndpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
func (e *countedEndpoint) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
e.dispatchCount++
}
func (e *countedEndpoint) DeliverOutboundPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
panic("unimplemented")
}
func (e *countedEndpoint) Attach(dispatcher stack.NetworkDispatcher) {
e.attachCount++
e.dispatcher = dispatcher
@@ -161,7 +157,7 @@ func TestWaitDispatch(t *testing.T) {
// Dispatch and check that it goes through.
{
p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
ep.dispatcher.DeliverNetworkPacket("", "", 0, p)
ep.dispatcher.DeliverNetworkPacket(0, p)
if want := 1; ep.dispatchCount != want {
t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want)
}
@@ -172,7 +168,7 @@ func TestWaitDispatch(t *testing.T) {
{
wep.WaitWrite()
p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
ep.dispatcher.DeliverNetworkPacket("", "", 0, p)
ep.dispatcher.DeliverNetworkPacket(0, p)
if want := 2; ep.dispatchCount != want {
t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want)
}
@@ -183,7 +179,7 @@ func TestWaitDispatch(t *testing.T) {
{
wep.WaitDispatch()
p := stack.NewPacketBuffer(stack.PacketBufferOptions{})
ep.dispatcher.DeliverNetworkPacket("", "", 0, p)
ep.dispatcher.DeliverNetworkPacket(0, p)
if want := 2; ep.dispatchCount != want {
t.Fatalf("Unexpected dispatchCount: got=%v, want=%v", ep.dispatchCount, want)
}
+1 -1
View File
@@ -486,7 +486,7 @@ func routeICMPv6Packet(t *testing.T, clock *faketime.ManualClock, args routeArgs
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
Data: buffer.NewVectorisedView(pi.Size(), pi.Views()),
})
args.dst.InjectLinkAddr(pi.NetworkProtocolNumber, args.dst.LinkAddress(), pkt)
args.dst.InjectInbound(pi.NetworkProtocolNumber, pkt)
}
if pi.NetworkProtocolNumber != ProtocolNumber {

Some files were not shown because too many files have changed in this diff Show More