Skip to content

Commit

Permalink
Remove WritePacket from LinkEndpoint.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 418672809
  • Loading branch information
manninglucas authored and gvisor-bot committed Dec 28, 2021
1 parent 6838e0f commit e511fc9
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 193 deletions.
10 changes: 0 additions & 10 deletions pkg/tcpip/link/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,6 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.linkAddr
}

// WritePacket stores outbound packets into the channel.
func (e *Endpoint) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
// Write returns false if the queue is full. A full queue is not an error
// from the perspective of a LinkEndpoint so we ignore Write's return
// value and always return nil from this method.
_ = e.q.Write(pkt)

return nil
}

// WritePackets stores outbound packets into the channel.
func (e *Endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
n := 0
Expand Down
6 changes: 0 additions & 6 deletions pkg/tcpip/link/ethernet/ethernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities {
return c
}

// WritePacket implements stack.LinkEndpoint.
func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
e.AddHeader(e.LinkAddress(), r.RemoteLinkAddress, proto, pkt)
return e.Endpoint.WritePacket(r, proto, pkt)
}

// WritePackets implements stack.LinkEndpoint.
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
linkAddr := e.LinkAddress()
Expand Down
6 changes: 3 additions & 3 deletions pkg/tcpip/link/fdbased/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ func (e *endpoint) AddHeader(local, remote tcpip.LinkAddress, protocol tcpip.Net
// WriteRawPacket implements stack.LinkEndpoint.
func (*endpoint) WriteRawPacket(*stack.PacketBuffer) tcpip.Error { return &tcpip.ErrNotSupported{} }

// WritePacket writes outbound packets to the file descriptor. If it is not
// writePacket writes outbound packets to the file descriptor. If it is not
// currently writable, the packet is dropped.
func (e *endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
func (e *endpoint) writePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
if e.hdrSize > 0 {
e.AddHeader(r.LocalLinkAddress, r.RemoteLinkAddress, protocol, pkt)
}
Expand Down Expand Up @@ -641,7 +641,7 @@ func (e *endpoint) sendBatch(batchFD int, pkts []*stack.PacketBuffer) (int, tcpi
// if necessary (by using e.writevMaxIovs instead of
// rawfile.MaxIovs).
pkt := batch[0]
if err := e.WritePacket(pkt.EgressRoute, pkt.NetworkProtocolNumber, pkt); err != nil {
if err := e.writePacket(pkt.EgressRoute, pkt.NetworkProtocolNumber, pkt); err != nil {
return packets, err
}
packets++
Expand Down
16 changes: 14 additions & 2 deletions pkg/tcpip/link/fdbased/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ func testWritePacket(t *testing.T, plen int, eth bool, gsoMaxSize uint32, hash u
Data: payload.ToVectorisedView(),
})
pkt.Hash = hash
// Every PacketBuffer must have these set:
// See nic.writePacket.
pkt.EgressRoute = r
pkt.NetworkProtocolNumber = proto

// Build header.
b := pkt.NetworkHeader().Push(netHdrLen)
Expand All @@ -218,7 +222,9 @@ func testWritePacket(t *testing.T, plen int, eth bool, gsoMaxSize uint32, hash u
L3HdrLen: l3HdrLen,
}
}
if err := c.ep.WritePacket(r, proto, pkt); err != nil {
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
t.Fatalf("WritePacket failed: %v", err)
}

Expand Down Expand Up @@ -333,7 +339,13 @@ func TestPreserveSrcAddress(t *testing.T) {
ReserveHeaderBytes: header.EthernetMinimumSize,
Data: buffer.VectorisedView{},
})
if err := c.ep.WritePacket(r, proto, pkt); err != nil {
// Every PacketBuffer must have these set:
// See nic.writePacket.
pkt.NetworkProtocolNumber = proto
pkt.EgressRoute = r
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
t.Fatalf("WritePacket failed: %v", err)
}

Expand Down
6 changes: 0 additions & 6 deletions pkg/tcpip/link/loopback/loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ func (*endpoint) LinkAddress() tcpip.LinkAddress {
// Wait implements stack.LinkEndpoint.Wait.
func (*endpoint) Wait() {}

// WritePacket implements stack.LinkEndpoint.WritePacket. It delivers outbound
// packets to the network-layer dispatcher.
func (e *endpoint) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
return e.WriteRawPacket(pkt)
}

// WritePackets implements stack.LinkEndpoint.WritePackets.
func (e *endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
n := 0
Expand Down
10 changes: 0 additions & 10 deletions pkg/tcpip/link/muxed/injectable.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ func (m *InjectableEndpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBu
return endpoint.WritePackets(r, pkts, protocol)
}

// WritePacket writes outbound packets to the appropriate LinkInjectableEndpoint
// based on the RemoteAddress. HandleLocal only works if r.RemoteAddress has a
// route registered in this endpoint.
func (m *InjectableEndpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
if endpoint, ok := m.routes[r.RemoteAddress]; ok {
return endpoint.WritePacket(r, protocol, pkt)
}
return &tcpip.ErrNoRoute{}
}

// InjectOutbound writes outbound packets to the appropriate
// LinkInjectableEndpoint based on the dest address.
func (m *InjectableEndpoint) InjectOutbound(dest tcpip.Address, packet []byte) tcpip.Error {
Expand Down
13 changes: 11 additions & 2 deletions pkg/tcpip/link/muxed/injectable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func TestInjectableEndpointDispatch(t *testing.T) {
var packetRoute stack.RouteInfo
packetRoute.RemoteAddress = dstIP

endpoint.WritePacket(packetRoute, ipv4.ProtocolNumber, pkt)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := endpoint.WritePackets(packetRoute, pkts, ipv4.ProtocolNumber); err != nil {
t.Fatalf("Unable to write packets: %s", err)
}

buf := make([]byte, 6500)
bytesRead, err := sock.Read(buf)
Expand All @@ -76,7 +80,12 @@ func TestInjectableEndpointDispatchHdrOnly(t *testing.T) {
pkt.TransportHeader().Push(1)[0] = 0xFA
var packetRoute stack.RouteInfo
packetRoute.RemoteAddress = dstIP
endpoint.WritePacket(packetRoute, ipv4.ProtocolNumber, pkt)

var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := endpoint.WritePackets(packetRoute, pkts, ipv4.ProtocolNumber); err != nil {
t.Fatalf("Unable to write packets: %s", err)
}
buf := make([]byte, 6500)
bytesRead, err := sock.Read(buf)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/tcpip/link/nested/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
return e.child.LinkAddress()
}

// WritePacket implements stack.LinkEndpoint.
func (e *Endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
return e.child.WritePacket(r, protocol, pkt)
}

// WritePackets implements stack.LinkEndpoint.
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
return e.child.WritePackets(r, pkts, protocol)
Expand Down
13 changes: 4 additions & 9 deletions pkg/tcpip/link/pipe/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocol
}
}

// WritePacket implements stack.LinkEndpoint.
func (e *Endpoint) WritePacket(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
e.deliverPackets(r, proto, pkts)
return nil
}

// WritePackets implements stack.LinkEndpoint.
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
n := pkts.Len()
Expand Down Expand Up @@ -129,5 +121,8 @@ func (*Endpoint) AddHeader(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber

// WriteRawPacket implements stack.LinkEndpoint.
func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error {
return e.WritePacket(stack.RouteInfo{}, 0, pkt)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
_, err := e.WritePackets(stack.RouteInfo{}, pkts, 0)
return err
}
12 changes: 0 additions & 12 deletions pkg/tcpip/link/sharedmem/sharedmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,6 @@ func (e *endpoint) writePacketLocked(r stack.RouteInfo, protocol tcpip.NetworkPr
return nil
}

// WritePacket writes outbound packets to the file descriptor. If it is not
// currently writable, the packet is dropped.
func (e *endpoint) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
e.mu.Lock()
defer e.mu.Unlock()
if err := e.writePacketLocked(pkt.EgressRoute, pkt.NetworkProtocolNumber, pkt); err != nil {
return err
}
e.tx.notify()
return nil
}

// WritePackets implements stack.LinkEndpoint.WritePackets.
func (e *endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
n := 0
Expand Down
85 changes: 56 additions & 29 deletions pkg/tcpip/link/sharedmem/sharedmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ func TestSimpleSend(t *testing.T) {
// See nic.writePacket.
pkt.EgressRoute = r
pkt.NetworkProtocolNumber = proto
if err := c.ep.WritePacket(r, proto, pkt); err != nil {
t.Fatalf("WritePacket failed: %v", err)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
t.Fatalf("WritePackets failed: %s", err)
}

// Receive packet.
Expand Down Expand Up @@ -306,8 +308,11 @@ func TestPreserveSrcAddressInSend(t *testing.T) {
// See nic.writePacket.
pkt.EgressRoute = r
pkt.NetworkProtocolNumber = proto
if err := c.ep.WritePacket(r, proto, pkt); err != nil {
t.Fatalf("WritePacket failed: %v", err)

var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
t.Fatalf("WritePackets failed: %s", err)
}

// Receive packet.
Expand Down Expand Up @@ -362,8 +367,10 @@ func TestFillTxQueue(t *testing.T) {
Data: buf.ToVectorisedView(),
})

if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}

// Check that they have different IDs.
Expand All @@ -380,9 +387,11 @@ func TestFillTxQueue(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
_, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber)
if _, ok := err.(*tcpip.ErrWouldBlock); !ok {
t.Fatalf("got WritePacket(...) = %v, want %s", err, &tcpip.ErrWouldBlock{})
t.Fatalf("got WritePackets(...) = %s, want %s", err, &tcpip.ErrWouldBlock{})
}
}

Expand All @@ -403,13 +412,17 @@ func TestFillTxQueueAfterBadCompletion(t *testing.T) {
buf := buffer.NewView(100)

// Send two packets so that the id slice has at least two slots.
for i := 2; i > 0; i-- {
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
{
var pkts stack.PacketBufferList
for i := 2; i > 0; i-- {
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
pkts.PushBack(pkt)
}
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}
}

Expand All @@ -431,8 +444,10 @@ func TestFillTxQueueAfterBadCompletion(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}

// Check that they have different IDs.
Expand All @@ -449,9 +464,11 @@ func TestFillTxQueueAfterBadCompletion(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
_, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber)
if _, ok := err.(*tcpip.ErrWouldBlock); !ok {
t.Fatalf("got WritePacket(...) = %v, want %s", err, &tcpip.ErrWouldBlock{})
t.Fatalf("got WritePackets(...) = %s, want %s", err, &tcpip.ErrWouldBlock{})
}
}

Expand All @@ -475,8 +492,10 @@ func TestFillTxMemory(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}

// Check that they have different IDs.
Expand All @@ -494,9 +513,11 @@ func TestFillTxMemory(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
_, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber)
if _, ok := err.(*tcpip.ErrWouldBlock); !ok {
t.Fatalf("got WritePacket(...) = %v, want %s", err, &tcpip.ErrWouldBlock{})
t.Fatalf("got WritePackets(...) = %s, want %s", err, &tcpip.ErrWouldBlock{})
}
}

Expand All @@ -521,8 +542,10 @@ func TestFillTxMemoryWithMultiBuffer(t *testing.T) {
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}

// Pull the posted buffer.
Expand All @@ -532,24 +555,28 @@ func TestFillTxMemoryWithMultiBuffer(t *testing.T) {

// Attempt to write a two-buffer packet. It must fail.
{
var pkts stack.PacketBufferList
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buffer.NewView(bufferSize).ToVectorisedView(),
})
err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt)
pkts.PushBack(pkt)
_, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber)
if _, ok := err.(*tcpip.ErrWouldBlock); !ok {
t.Fatalf("got WritePacket(...) = %v, want %s", err, &tcpip.ErrWouldBlock{})
t.Fatalf("got WritePackets(...) = %s, want %s", err, &tcpip.ErrWouldBlock{})
}
}

// Attempt to write the one-buffer packet again. It must succeed.
{
var pkts stack.PacketBufferList
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
ReserveHeaderBytes: int(c.ep.MaxHeaderLength()),
Data: buf.ToVectorisedView(),
})
if err := c.ep.WritePacket(r, header.IPv4ProtocolNumber, pkt); err != nil {
t.Fatalf("WritePacket failed unexpectedly: %v", err)
pkts.PushBack(pkt)
if _, err := c.ep.WritePackets(r, pkts, header.IPv4ProtocolNumber); err != nil {
t.Fatalf("WritePackets failed unexpectedly: %s", err)
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/tcpip/link/sniffer/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,6 @@ func (e *endpoint) dumpPacket(dir direction, protocol tcpip.NetworkProtocolNumbe
}
}

// WritePacket implements the stack.LinkEndpoint interface. It is called by
// higher-level protocols to write packets; it just logs the packet and
// forwards the request to the lower endpoint.
func (e *endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
e.dumpPacket(directionSend, protocol, pkt)
return e.Endpoint.WritePacket(r, protocol, pkt)
}

// WritePackets implements the stack.LinkEndpoint interface. It is called by
// higher-level protocols to write packets; it just logs the packet and
// forwards the request to the lower endpoint.
Expand Down

0 comments on commit e511fc9

Please sign in to comment.