Populate ethernet header fields from packet buffer

...instead of the arguments provided in the call to WritePackets.

QDisc always passes 0 for the protocol for calls to WritePackets as
each packet may have a different protocol number.

A later change will remove the unnecessary arguments from WritePackets.

PiperOrigin-RevId: 418513699
This commit is contained in:
Ghanan Gowripalan
2021-12-27 11:25:59 -08:00
committed by gVisor bot
parent 76776aad8b
commit 9c9fdfa075
2 changed files with 47 additions and 1 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList,
linkAddr := e.LinkAddress()
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
e.AddHeader(linkAddr, r.RemoteLinkAddress, proto, pkt)
e.AddHeader(linkAddr, pkt.EgressRoute.RemoteLinkAddress, pkt.NetworkProtocolNumber, pkt)
}
return e.Endpoint.WritePackets(r, pkts, proto)
+46
View File
@@ -119,3 +119,49 @@ func TestMTU(t *testing.T) {
})
}
}
func TestWritePacketsAddHeader(t *testing.T) {
const (
localLinkAddr = tcpip.LinkAddress("\x02\x02\x03\x04\x05\x06")
remoteLinkAddr = tcpip.LinkAddress("\x02\x02\x03\x04\x05\x07")
netProto = 55
)
c := channel.New(1, header.EthernetMinimumSize, localLinkAddr)
e := ethernet.New(c)
{
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: int(e.MaxHeaderLength()),
})
pkt.NetworkProtocolNumber = netProto
pkt.EgressRoute.RemoteLinkAddress = remoteLinkAddr
var pkts stack.PacketBufferList
pkts.PushFront(pkt)
if n, err := e.WritePackets(stack.RouteInfo{}, pkts, 0 /* protocol */); err != nil {
t.Fatalf("e.WritePackets({}, _, 0): %s", err)
} else if n != 1 {
t.Fatalf("got e.WritePackets({}, _, 0) = %d, want = 1", n)
}
}
{
pkt := c.Read()
if pkt == nil {
t.Fatal("expected to read a packet")
}
eth := header.Ethernet(pkt.LinkHeader().View())
if got := eth.SourceAddress(); got != localLinkAddr {
t.Errorf("got eth.SourceAddress() = %s, want = %s", got, localLinkAddr)
}
if got := eth.DestinationAddress(); got != remoteLinkAddr {
t.Errorf("got eth.DestinationAddress() = %s, want = %s", got, remoteLinkAddr)
}
if got := eth.Type(); got != netProto {
t.Errorf("got eth.Type() = %d, want = %d", got, netProto)
}
}
}