Skip to content

Commit 196baa6

Browse files
ghananigansgvisor-bot
authored andcommitted
Don't pass route info and net proto to write fns
...as the packet buffer already holds that information. Updates google#3810. Fixes google#6537. PiperOrigin-RevId: 421898143
1 parent c6de0ac commit 196baa6

35 files changed

+212
-189
lines changed

pkg/tcpip/link/channel/channel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
240240
}
241241

242242
// WritePackets stores outbound packets into the channel.
243-
func (e *Endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
243+
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
244244
n := 0
245245
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
246246
if !e.q.Write(pkt) {

pkg/tcpip/link/ethernet/ethernet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities {
8181
}
8282

8383
// WritePackets implements stack.LinkEndpoint.
84-
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
84+
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
8585
linkAddr := e.LinkAddress()
8686

8787
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() {
8888
e.AddHeader(linkAddr, pkt.EgressRoute.RemoteLinkAddress, pkt.NetworkProtocolNumber, pkt)
8989
}
9090

91-
return e.Endpoint.WritePackets(r, pkts, proto)
91+
return e.Endpoint.WritePackets(pkts)
9292
}
9393

9494
// MaxHeaderLength implements stack.LinkEndpoint.

pkg/tcpip/link/ethernet/ethernet_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ func TestWritePacketsAddHeader(t *testing.T) {
140140

141141
var pkts stack.PacketBufferList
142142
pkts.PushFront(pkt)
143-
if n, err := e.WritePackets(stack.RouteInfo{}, pkts, 0 /* protocol */); err != nil {
144-
t.Fatalf("e.WritePackets({}, _, 0): %s", err)
143+
if n, err := e.WritePackets(pkts); err != nil {
144+
t.Fatalf("e.WritePackets(_): %s", err)
145145
} else if n != 1 {
146-
t.Fatalf("got e.WritePackets({}, _, 0) = %d, want = 1", n)
146+
t.Fatalf("got e.WritePackets(_) = %d, want = 1", n)
147147
}
148148
}
149149

pkg/tcpip/link/fdbased/endpoint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func (e *endpoint) sendBatch(batchFD int, pkts []*stack.PacketBuffer) (int, tcpi
668668
// - pkt.EgressRoute
669669
// - pkt.GSOOptions
670670
// - pkt.NetworkProtocolNumber
671-
func (e *endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
671+
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
672672
// Preallocate to avoid repeated reallocation as we append to batch.
673673
// batchSz is 47 because when SWGSO is in use then a single 65KB TCP
674674
// segment can get split into 46 segments of 1420 bytes and a single 216

pkg/tcpip/link/fdbased/endpoint_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func testWritePacket(t *testing.T, plen int, eth bool, gsoMaxSize uint32, hash u
224224
}
225225
var pkts stack.PacketBufferList
226226
pkts.PushBack(pkt)
227-
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
228-
t.Fatalf("WritePacket failed: %v", err)
227+
if _, err := c.ep.WritePackets(pkts); err != nil {
228+
t.Fatalf("WritePackets failed: %s", err)
229229
}
230230

231231
// Read from the corresponding FD, then compare with what we wrote.
@@ -345,8 +345,8 @@ func TestPreserveSrcAddress(t *testing.T) {
345345
pkt.EgressRoute = r
346346
var pkts stack.PacketBufferList
347347
pkts.PushBack(pkt)
348-
if _, err := c.ep.WritePackets(r, pkts, proto); err != nil {
349-
t.Fatalf("WritePacket failed: %v", err)
348+
if _, err := c.ep.WritePackets(pkts); err != nil {
349+
t.Fatalf("WritePackets failed: %s", err)
350350
}
351351

352352
// Read from the FD, then compare with what we wrote.

pkg/tcpip/link/loopback/loopback.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (*endpoint) LinkAddress() tcpip.LinkAddress {
7575
func (*endpoint) Wait() {}
7676

7777
// WritePackets implements stack.LinkEndpoint.WritePackets.
78-
func (e *endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
78+
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
7979
n := 0
8080
for p := pkts.Front(); p != nil; p = p.Next() {
8181
if err := e.WriteRawPacket(p); err != nil {

pkg/tcpip/link/muxed/injectable.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,30 @@ func (m *InjectableEndpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber,
8686

8787
// WritePackets writes outbound packets to the appropriate
8888
// LinkInjectableEndpoint based on the RemoteAddress. HandleLocal only works if
89-
// r.RemoteAddress has a route registered in this endpoint.
90-
func (m *InjectableEndpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
91-
endpoint, ok := m.routes[r.RemoteAddress]
92-
if !ok {
93-
return 0, &tcpip.ErrNoRoute{}
89+
// pkt.EgressRoute.RemoteAddress has a route registered in this endpoint.
90+
func (m *InjectableEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
91+
i := 0
92+
for pkt := pkts.Front(); pkt != nil; {
93+
nextPkt := pkt.Next()
94+
95+
endpoint, ok := m.routes[pkt.EgressRoute.RemoteAddress]
96+
if !ok {
97+
return i, &tcpip.ErrNoRoute{}
98+
}
99+
100+
var tmpPkts stack.PacketBufferList
101+
tmpPkts.PushFront(pkt)
102+
103+
n, err := endpoint.WritePackets(tmpPkts)
104+
if err != nil {
105+
return i, err
106+
}
107+
108+
i += n
109+
pkt = nextPkt
94110
}
95-
return endpoint.WritePackets(r, pkts, protocol)
111+
112+
return i, nil
96113
}
97114

98115
// InjectOutbound writes outbound packets to the appropriate

pkg/tcpip/link/muxed/injectable_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func TestInjectableEndpointDispatch(t *testing.T) {
5151
Data: buffer.NewViewFromBytes([]byte{0xFB}).ToVectorisedView(),
5252
})
5353
pkt.TransportHeader().Push(1)[0] = 0xFA
54-
var packetRoute stack.RouteInfo
55-
packetRoute.RemoteAddress = dstIP
54+
pkt.EgressRoute.RemoteAddress = dstIP
55+
pkt.NetworkProtocolNumber = ipv4.ProtocolNumber
5656

5757
var pkts stack.PacketBufferList
5858
pkts.PushBack(pkt)
59-
if _, err := endpoint.WritePackets(packetRoute, pkts, ipv4.ProtocolNumber); err != nil {
59+
if _, err := endpoint.WritePackets(pkts); err != nil {
6060
t.Fatalf("Unable to write packets: %s", err)
6161
}
6262

@@ -78,12 +78,12 @@ func TestInjectableEndpointDispatchHdrOnly(t *testing.T) {
7878
Data: buffer.NewView(0).ToVectorisedView(),
7979
})
8080
pkt.TransportHeader().Push(1)[0] = 0xFA
81-
var packetRoute stack.RouteInfo
82-
packetRoute.RemoteAddress = dstIP
81+
pkt.EgressRoute.RemoteAddress = dstIP
82+
pkt.NetworkProtocolNumber = ipv4.ProtocolNumber
8383

8484
var pkts stack.PacketBufferList
8585
pkts.PushBack(pkt)
86-
if _, err := endpoint.WritePackets(packetRoute, pkts, ipv4.ProtocolNumber); err != nil {
86+
if _, err := endpoint.WritePackets(pkts); err != nil {
8787
t.Fatalf("Unable to write packets: %s", err)
8888
}
8989
buf := make([]byte, 6500)

pkg/tcpip/link/nested/nested.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
103103
}
104104

105105
// WritePackets implements stack.LinkEndpoint.
106-
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
107-
return e.child.WritePackets(r, pkts, protocol)
106+
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
107+
return e.child.WritePackets(pkts)
108108
}
109109

110110
// Wait implements stack.LinkEndpoint.

pkg/tcpip/link/pipe/pipe.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Endpoint struct {
4848
mtu uint32
4949
}
5050

51-
func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocolNumber, pkts stack.PacketBufferList) {
51+
func (e *Endpoint) deliverPackets(pkts stack.PacketBufferList) {
5252
if !e.linked.IsAttached() {
5353
return
5454
}
@@ -65,15 +65,16 @@ func (e *Endpoint) deliverPackets(r stack.RouteInfo, proto tcpip.NetworkProtocol
6565
newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
6666
Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()),
6767
})
68-
e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, proto, newPkt)
68+
r := pkt.EgressRoute
69+
e.linked.dispatcher.DeliverNetworkPacket(r.LocalLinkAddress /* remote */, r.RemoteLinkAddress /* local */, pkt.NetworkProtocolNumber, newPkt)
6970
newPkt.DecRef()
7071
}
7172
}
7273

7374
// WritePackets implements stack.LinkEndpoint.
74-
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, proto tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
75+
func (e *Endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
7576
n := pkts.Len()
76-
e.deliverPackets(r, proto, pkts)
77+
e.deliverPackets(pkts)
7778
return n, nil
7879
}
7980

@@ -123,6 +124,6 @@ func (*Endpoint) AddHeader(_, _ tcpip.LinkAddress, _ tcpip.NetworkProtocolNumber
123124
func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error {
124125
var pkts stack.PacketBufferList
125126
pkts.PushBack(pkt)
126-
_, err := e.WritePackets(stack.RouteInfo{}, pkts, 0)
127+
_, err := e.WritePackets(pkts)
127128
return err
128129
}

pkg/tcpip/link/qdisc/fifo/fifo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,14 @@ func (qd *queueDispatcher) dispatchLoop() {
9696
if pkt == nil {
9797
break
9898
}
99+
99100
qd.queue.Remove(pkt)
100101
qd.used--
101102
batch.PushBack(pkt)
102103
}
103104
qd.mu.Unlock()
104105

105-
// We pass a protocol of zero here because each packet carries its
106-
// NetworkProtocol.
107-
_, _ = qd.lower.WritePackets(stack.RouteInfo{}, batch, 0 /* protocol */)
106+
_, _ = qd.lower.WritePackets(batch)
108107
batch.DecRef()
109108
batch.Reset()
110109
}
@@ -116,7 +115,7 @@ func (qd *queueDispatcher) dispatchLoop() {
116115
// - pkt.EgressRoute
117116
// - pkt.GSOOptions
118117
// - pkt.NetworkProtocolNumber
119-
func (d *discipline) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error {
118+
func (d *discipline) WritePacket(pkt *stack.PacketBuffer) tcpip.Error {
120119
qd := &d.dispatchers[int(pkt.Hash)%len(d.dispatchers)]
121120
qd.mu.Lock()
122121
haveSpace := qd.used < qd.limit

pkg/tcpip/link/qdisc/fifo/qdisc_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var _ stack.LinkWriter = (*discardWriter)(nil)
3131
type discardWriter struct {
3232
}
3333

34-
func (*discardWriter) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, _ tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
34+
func (*discardWriter) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
3535
return pkts.Len(), nil
3636
}
3737

@@ -43,9 +43,6 @@ func TestFastSimultaneousWrites(t *testing.T) {
4343

4444
v := make(buffer.View, 1)
4545

46-
prot := tcpip.NetworkProtocolNumber(0)
47-
r := stack.RouteInfo{}
48-
4946
// Simulate many simultaneous writes from various goroutines, similar to TCP's sendTCPBatch().
5047
nWriters := 100
5148
nWrites := 100
@@ -60,7 +57,7 @@ func TestFastSimultaneousWrites(t *testing.T) {
6057
Data: v.ToVectorisedView(),
6158
})
6259
pkt.Hash = rand.Uint32()
63-
linkEP.WritePacket(r, prot, pkt)
60+
linkEP.WritePacket(pkt)
6461
pkt.DecRef()
6562
}
6663
}()

pkg/tcpip/link/sharedmem/sharedmem.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (e *endpoint) writePacketLocked(r stack.RouteInfo, protocol tcpip.NetworkPr
364364
}
365365

366366
// WritePackets implements stack.LinkEndpoint.WritePackets.
367-
func (e *endpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
367+
func (e *endpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
368368
n := 0
369369
var err tcpip.Error
370370
e.mu.Lock()

pkg/tcpip/link/sharedmem/sharedmem_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (e *serverEndpoint) WritePacket(_ stack.RouteInfo, _ tcpip.NetworkProtocolN
274274
}
275275

276276
// WritePackets implements stack.LinkEndpoint.WritePackets.
277-
func (e *serverEndpoint) WritePackets(_ stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) {
277+
func (e *serverEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
278278
n := 0
279279
var err tcpip.Error
280280
e.mu.Lock()

0 commit comments

Comments
 (0)