diff --git a/pkg/tcpip/network/BUILD b/pkg/tcpip/network/BUILD index 0a8d3b987..2c5732c9c 100644 --- a/pkg/tcpip/network/BUILD +++ b/pkg/tcpip/network/BUILD @@ -11,6 +11,7 @@ go_test( "multicast_group_test.go", ], deps = [ + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/sync", diff --git a/pkg/tcpip/network/arp/BUILD b/pkg/tcpip/network/arp/BUILD index 7df369548..8b30a0446 100644 --- a/pkg/tcpip/network/arp/BUILD +++ b/pkg/tcpip/network/arp/BUILD @@ -27,10 +27,10 @@ go_test( srcs = ["arp_test.go"], deps = [ ":arp", + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/faketime", "//pkg/tcpip/header", "//pkg/tcpip/link/channel", diff --git a/pkg/tcpip/network/arp/arp_test.go b/pkg/tcpip/network/arp/arp_test.go index c074285f8..24ba5e782 100644 --- a/pkg/tcpip/network/arp/arp_test.go +++ b/pkg/tcpip/network/arp/arp_test.go @@ -21,10 +21,10 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refs" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/link/channel" @@ -182,9 +182,8 @@ func TestMalformedPacket(t *testing.T) { c := makeTestContext(t, 0, 0) defer c.cleanup() - v := make(buffer.View, header.ARPSize) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: v.ToVectorisedView(), + Payload: buffer.NewWithData(make([]byte, header.ARPSize)), }) c.linkEP.InjectInbound(arp.ProtocolNumber, pkt) @@ -208,9 +207,8 @@ func TestDisabledEndpoint(t *testing.T) { } ep.Disable() - v := make(buffer.View, header.ARPSize) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: v.ToVectorisedView(), + Payload: buffer.NewWithData(make([]byte, header.ARPSize)), }) c.linkEP.InjectInbound(arp.ProtocolNumber, pkt) @@ -231,7 +229,7 @@ func TestDirectReply(t *testing.T) { const senderMAC = "\x01\x02\x03\x04\x05\x06" const senderIPv4 = "\x0a\x00\x00\x02" - v := make(buffer.View, header.ARPSize) + v := make([]byte, header.ARPSize) h := header.ARP(v) h.SetIPv4OverEthernet() h.SetOp(header.ARPReply) @@ -242,7 +240,7 @@ func TestDirectReply(t *testing.T) { copy(h.ProtocolAddressTarget(), stackAddr) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: v.ToVectorisedView(), + Payload: buffer.NewWithData(v), }) c.linkEP.InjectInbound(arp.ProtocolNumber, pkt) @@ -298,7 +296,7 @@ func TestDirectRequest(t *testing.T) { outgoingReplies := c.s.Stats().ARP.OutgoingRepliesSent.Value() // Inject an incoming ARP request. - v := make(buffer.View, header.ARPSize) + v := make([]byte, header.ARPSize) h := header.ARP(v) h.SetIPv4OverEthernet() h.SetOp(header.ARPRequest) @@ -306,7 +304,7 @@ func TestDirectRequest(t *testing.T) { copy(h.ProtocolAddressSender(), test.senderAddr) copy(h.ProtocolAddressTarget(), test.targetAddr) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: v.ToVectorisedView(), + Payload: buffer.NewWithData(v), }) c.linkEP.InjectInbound(arp.ProtocolNumber, pkt) pkt.DecRef() diff --git a/pkg/tcpip/network/internal/fragmentation/BUILD b/pkg/tcpip/network/internal/fragmentation/BUILD index c0291d803..4d1caff18 100644 --- a/pkg/tcpip/network/internal/fragmentation/BUILD +++ b/pkg/tcpip/network/internal/fragmentation/BUILD @@ -27,6 +27,7 @@ go_library( "//pkg/tcpip/network/ipv6:__pkg__", ], deps = [ + "//pkg/buffer", "//pkg/log", "//pkg/sync", "//pkg/tcpip", @@ -46,9 +47,9 @@ go_test( ], library = ":fragmentation", deps = [ + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", - "//pkg/tcpip/buffer", "//pkg/tcpip/faketime", "//pkg/tcpip/network/internal/testutil", "//pkg/tcpip/stack", diff --git a/pkg/tcpip/network/internal/fragmentation/fragmentation.go b/pkg/tcpip/network/internal/fragmentation/fragmentation.go index e0b64a6b7..5b7a90eea 100644 --- a/pkg/tcpip/network/internal/fragmentation/fragmentation.go +++ b/pkg/tcpip/network/internal/fragmentation/fragmentation.go @@ -21,10 +21,10 @@ import ( "fmt" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/log" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/stack" ) @@ -286,8 +286,8 @@ func (f *Fragmentation) releaseReassemblersLocked() { // PacketFragmenter is the book-keeping struct for packet fragmentation. type PacketFragmenter struct { - transportHeader buffer.View - data buffer.VectorisedView + transportHeader []byte + data buffer.Buffer reserve int fragmentPayloadLen int fragmentCount int @@ -312,9 +312,9 @@ func MakePacketFragmenter(pkt *stack.PacketBuffer, fragmentPayloadLen uint32, re // TODO(gvisor.dev/issue/3912): Once Authentication or ESP Headers are // supported for outbound packets, the fragmentable data should not include // these headers. - var fragmentableData buffer.VectorisedView - fragmentableData.AppendView(pkt.TransportHeader().View()) - fragmentableData.Append(pkt.Data().ExtractVV()) + fragmentableData := buffer.NewWithData(pkt.TransportHeader().View()) + pktBuf := pkt.Data().AsBuffer() + fragmentableData.Merge(&pktBuf) fragmentCount := (uint32(fragmentableData.Size()) + fragmentPayloadLen - 1) / fragmentPayloadLen return PacketFragmenter{ @@ -344,7 +344,7 @@ func (pf *PacketFragmenter) BuildNextFragment() (*stack.PacketBuffer, int, int, }) // Copy data for the fragment. - copied := fragPkt.Data().ReadFromVV(&pf.data, pf.fragmentPayloadLen) + copied := fragPkt.Data().ReadFromBuffer(&pf.data, pf.fragmentPayloadLen) offset := pf.fragmentOffset pf.fragmentOffset += copied diff --git a/pkg/tcpip/network/internal/fragmentation/fragmentation_test.go b/pkg/tcpip/network/internal/fragmentation/fragmentation_test.go index 18dbfe311..48a697229 100644 --- a/pkg/tcpip/network/internal/fragmentation/fragmentation_test.go +++ b/pkg/tcpip/network/internal/fragmentation/fragmentation_test.go @@ -20,7 +20,7 @@ import ( "time" "github.com/google/go-cmp/cmp" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/network/internal/testutil" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -30,19 +30,19 @@ import ( // advances. const reassembleTimeout = 1 -// vv is a helper to build VectorisedView from different strings. -func vv(size int, pieces ...string) buffer.VectorisedView { - views := make([]buffer.View, len(pieces)) - for i, p := range pieces { - views[i] = []byte(p) +// buf is a helper to build a Buffer from different strings. +func buf(size int, pieces ...string) buffer.Buffer { + buf := buffer.Buffer{} + for _, p := range pieces { + buf.Append([]byte(p)) } - return buffer.NewVectorisedView(size, views) + return buf } func pkt(size int, pieces ...string) *stack.PacketBuffer { return stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv(size, pieces...), + Payload: buf(size, pieces...), }) } @@ -56,7 +56,7 @@ type processInput struct { } type processOutput struct { - vv buffer.VectorisedView + buf buffer.Buffer proto uint8 done bool } @@ -74,8 +74,8 @@ func TestFragmentationProcess(t *testing.T) { {id: FragmentID{ID: 0}, first: 2, last: 3, more: false, pkt: pkt(2, "23")}, }, out: []processOutput{ - {vv: buffer.VectorisedView{}, done: false}, - {vv: vv(4, "01", "23"), done: true}, + {buf: buffer.Buffer{}, done: false}, + {buf: buf(4, "01", "23"), done: true}, }, }, { @@ -85,8 +85,8 @@ func TestFragmentationProcess(t *testing.T) { {id: FragmentID{ID: 0}, first: 2, last: 3, more: false, proto: 17, pkt: pkt(2, "23")}, }, out: []processOutput{ - {vv: buffer.VectorisedView{}, done: false}, - {vv: vv(4, "01", "23"), proto: 6, done: true}, + {buf: buffer.Buffer{}, done: false}, + {buf: buf(4, "01", "23"), proto: 6, done: true}, }, }, { @@ -98,10 +98,10 @@ func TestFragmentationProcess(t *testing.T) { {id: FragmentID{ID: 0}, first: 2, last: 3, more: false, pkt: pkt(2, "23")}, }, out: []processOutput{ - {vv: buffer.VectorisedView{}, done: false}, - {vv: buffer.VectorisedView{}, done: false}, - {vv: vv(4, "ab", "cd"), done: true}, - {vv: vv(4, "01", "23"), done: true}, + {buf: buffer.Buffer{}, done: false}, + {buf: buffer.Buffer{}, done: false}, + {buf: buf(4, "ab", "cd"), done: true}, + {buf: buf(4, "01", "23"), done: true}, }, }, } @@ -124,7 +124,7 @@ func TestFragmentationProcess(t *testing.T) { in.id, in.first, in.last, in.more, in.proto, done, c.out[i].done) } if c.out[i].done { - if diff := cmp.Diff(c.out[i].vv.ToOwnedView(), resPkt.Data().AsRange().ToOwnedView()); diff != "" { + if diff := cmp.Diff(c.out[i].buf.Flatten(), resPkt.Data().AsRange().ToOwnedView()); diff != "" { t.Errorf("got Process(%+v, %d, %d, %t, %d, %#v) result mismatch (-want, +got):\n%s", in.id, in.first, in.last, in.more, in.proto, in.pkt, diff) } @@ -525,8 +525,8 @@ func TestPacketFragmenter(t *testing.T) { t.Run(test.name, func(t *testing.T) { pkt := testutil.MakeRandPkt(test.transportHeaderLen, reserve, []int{test.payloadSize}, proto) defer pkt.DecRef() - originalPayload := stack.PayloadSince(pkt.TransportHeader()) - var reassembledPayload buffer.VectorisedView + originalPayload := []byte(stack.PayloadSince(pkt.TransportHeader())) + var reassembledPayload buffer.Buffer pf := MakePacketFragmenter(pkt, test.fragmentPayloadLen, reserve) for i := 0; ; i++ { fragPkt, offset, copied, more := pf.BuildNextFragment() @@ -553,7 +553,8 @@ func TestPacketFragmenter(t *testing.T) { if got := fragPkt.TransportHeader().View().Size(); got != 0 { t.Errorf("(fragment #%d) got fragPkt.TransportHeader().View().Size() = %d, want = 0", i, got) } - reassembledPayload.AppendViews(fragPkt.Data().Views()) + fragBuf := fragPkt.Data().AsBuffer() + reassembledPayload.Merge(&fragBuf) if !more { if i != len(test.wantFragments)-1 { t.Errorf("got fragment count = %d, want = %d", i, len(test.wantFragments)-1) @@ -561,7 +562,7 @@ func TestPacketFragmenter(t *testing.T) { break } } - if diff := cmp.Diff(reassembledPayload.ToView(), originalPayload); diff != "" { + if diff := cmp.Diff(reassembledPayload.Flatten(), originalPayload); diff != "" { t.Errorf("reassembledPayload mismatch (-want +got):\n%s", diff) } }) diff --git a/pkg/tcpip/network/internal/fragmentation/reassembler_test.go b/pkg/tcpip/network/internal/fragmentation/reassembler_test.go index 83a9b56c6..ccd7ac6a5 100644 --- a/pkg/tcpip/network/internal/fragmentation/reassembler_test.go +++ b/pkg/tcpip/network/internal/fragmentation/reassembler_test.go @@ -20,7 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/stack" ) @@ -37,8 +37,8 @@ type processParams struct { func TestReassemblerProcess(t *testing.T) { const proto = 99 - v := func(size int) buffer.View { - payload := buffer.NewView(size) + v := func(size int) []byte { + payload := make([]byte, size) for i := 1; i < size; i++ { payload[i] = uint8(i) * 3 } @@ -46,12 +46,12 @@ func TestReassemblerProcess(t *testing.T) { } pkt := func(sizes ...int) *stack.PacketBuffer { - var vv buffer.VectorisedView + var buf buffer.Buffer for _, size := range sizes { - vv.AppendView(v(size)) + buf.Append(v(size)) } return stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) } diff --git a/pkg/tcpip/network/internal/multicast/BUILD b/pkg/tcpip/network/internal/multicast/BUILD index 682091a94..d0d43d6d6 100644 --- a/pkg/tcpip/network/internal/multicast/BUILD +++ b/pkg/tcpip/network/internal/multicast/BUILD @@ -20,10 +20,10 @@ go_test( srcs = ["route_table_test.go"], library = ":multicast", deps = [ + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/faketime", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", @@ -38,10 +38,10 @@ go_test( srcs = ["example_test.go"], deps = [ ":multicast", + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/faketime", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", diff --git a/pkg/tcpip/network/internal/multicast/example_test.go b/pkg/tcpip/network/internal/multicast/example_test.go index 912676fbc..2b0174287 100644 --- a/pkg/tcpip/network/internal/multicast/example_test.go +++ b/pkg/tcpip/network/internal/multicast/example_test.go @@ -20,10 +20,10 @@ import ( "testing" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refs" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/network/internal/multicast" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -129,7 +129,7 @@ func deliverPktLocally(*stack.PacketBuffer) { func newPacketBuffer(body string) *stack.PacketBuffer { return stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.View(body).ToVectorisedView(), + Payload: buffer.NewWithData([]byte(body)), }) } diff --git a/pkg/tcpip/network/internal/multicast/route_table_test.go b/pkg/tcpip/network/internal/multicast/route_table_test.go index d1f697fdd..ae1d1fd91 100644 --- a/pkg/tcpip/network/internal/multicast/route_table_test.go +++ b/pkg/tcpip/network/internal/multicast/route_table_test.go @@ -21,10 +21,10 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refs" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" @@ -47,7 +47,7 @@ var ( func newPacketBuffer(body string) *stack.PacketBuffer { return stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.View(body).ToVectorisedView(), + Payload: buffer.NewWithData([]byte(body)), }) } @@ -258,10 +258,10 @@ func TestAddInstalledRouteWithPending(t *testing.T) { defer pkt.DecRef() cmpOpts := []cmp.Option{ - cmp.Transformer("AsViews", func(pkt *stack.PacketBuffer) []buffer.View { - return pkt.Views() + cmp.Transformer("AsSlices", func(pkt *stack.PacketBuffer) [][]byte { + return pkt.Slices() }), - cmp.Comparer(func(a []buffer.View, b []buffer.View) bool { + cmp.Comparer(func(a [][]byte, b [][]byte) bool { return cmp.Equal(a, b) }), } diff --git a/pkg/tcpip/network/internal/testutil/BUILD b/pkg/tcpip/network/internal/testutil/BUILD index a180e5c75..22474e085 100644 --- a/pkg/tcpip/network/internal/testutil/BUILD +++ b/pkg/tcpip/network/internal/testutil/BUILD @@ -13,8 +13,8 @@ go_library( "//pkg/tcpip/tests/integration:__pkg__", ], deps = [ + "//pkg/buffer", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/header", "//pkg/tcpip/stack", ], diff --git a/pkg/tcpip/network/internal/testutil/testutil.go b/pkg/tcpip/network/internal/testutil/testutil.go index f9e91a9bb..aa4febc21 100644 --- a/pkg/tcpip/network/internal/testutil/testutil.go +++ b/pkg/tcpip/network/internal/testutil/testutil.go @@ -20,8 +20,8 @@ import ( "fmt" "math/rand" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" ) @@ -104,19 +104,19 @@ func (ep *MockLinkEndpoint) Close() { // the other headers. The payload is made from Views of the sizes listed in // viewSizes. func MakeRandPkt(transportHeaderLength int, extraHeaderReserveLength int, viewSizes []int, proto tcpip.NetworkProtocolNumber) *stack.PacketBuffer { - var views buffer.VectorisedView + var buffer buffer.Buffer for _, s := range viewSizes { - newView := buffer.NewView(s) + newView := make([]byte, s) if _, err := rand.Read(newView); err != nil { panic(fmt.Sprintf("rand.Read: %s", err)) } - views.AppendView(newView) + buffer.Append(newView) } pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: transportHeaderLength + extraHeaderReserveLength, - Data: views, + Payload: buffer, }) pkt.NetworkProtocolNumber = proto if _, err := rand.Read(pkt.TransportHeader().Push(transportHeaderLength)); err != nil { diff --git a/pkg/tcpip/network/ip_test.go b/pkg/tcpip/network/ip_test.go index 29db0a86c..fff63cf3d 100644 --- a/pkg/tcpip/network/ip_test.go +++ b/pkg/tcpip/network/ip_test.go @@ -21,10 +21,11 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/link/channel" @@ -98,7 +99,7 @@ type testObject struct { // checkValues verifies that the transport protocol, data contents, src & dst // addresses of a packet match what's expected. If any field doesn't match, the // test fails. -func (t *testObject) checkValues(protocol tcpip.TransportProtocolNumber, v buffer.View, srcAddr, dstAddr tcpip.Address) { +func (t *testObject) checkValues(protocol tcpip.TransportProtocolNumber, v []byte, srcAddr, dstAddr tcpip.Address) { if protocol != t.protocol { t.t.Errorf("protocol = %v, want %v", protocol, t.protocol) } @@ -371,7 +372,7 @@ func (*testInterface) CheckLocalAddress(tcpip.NetworkProtocolNumber, tcpip.Addre func TestSourceAddressValidation(t *testing.T) { rxIPv4ICMP := func(e *channel.Endpoint, src tcpip.Address) { totalLen := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) pkt.SetType(header.ICMPv4Echo) pkt.SetCode(0) @@ -388,7 +389,7 @@ func TestSourceAddressValidation(t *testing.T) { ip.SetChecksum(^ip.CalculateChecksum()) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv4ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -396,7 +397,7 @@ func TestSourceAddressValidation(t *testing.T) { rxIPv6ICMP := func(e *channel.Endpoint, src tcpip.Address) { totalLen := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetCode(0) @@ -415,7 +416,7 @@ func TestSourceAddressValidation(t *testing.T) { DstAddr: localIPv6Addr, }) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv6ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -609,7 +610,7 @@ func TestIPv4Send(t *testing.T) { defer ep.Close() // Allocate and initialize the payload view. - payload := buffer.NewView(100) + payload := make([]byte, 100) for i := 0; i < len(payload); i++ { payload[i] = uint8(i) } @@ -617,7 +618,7 @@ func TestIPv4Send(t *testing.T) { // Setup the packet buffer. pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(ep.MaxHeaderLength()), - Data: payload.ToVectorisedView(), + Payload: buffer.NewWithData(payload), }) defer pkt.DecRef() @@ -658,7 +659,7 @@ func TestReceive(t *testing.T) { handlePacket: func(t *testing.T, ep stack.NetworkEndpoint, nic *testInterface) { const totalLen = header.IPv4MinimumSize + 30 /* payload length */ - view := buffer.NewView(totalLen) + view := make([]byte, totalLen) ip := header.IPv4(view) ip.Encode(&header.IPv4Fields{ TotalLength: totalLen, @@ -681,7 +682,7 @@ func TestReceive(t *testing.T) { nic.testObject.contents = view[header.IPv4MinimumSize:totalLen] pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: view.ToVectorisedView(), + Payload: buffer.NewWithData(view), }) ep.HandlePacket(pkt) pkt.DecRef() @@ -695,7 +696,7 @@ func TestReceive(t *testing.T) { epAddr: localIPv6Addr.WithPrefix(), handlePacket: func(t *testing.T, ep stack.NetworkEndpoint, nic *testInterface) { const payloadLen = 30 - view := buffer.NewView(header.IPv6MinimumSize + payloadLen) + view := make([]byte, header.IPv6MinimumSize+payloadLen) ip := header.IPv6(view) ip.Encode(&header.IPv6Fields{ PayloadLength: payloadLen, @@ -717,7 +718,7 @@ func TestReceive(t *testing.T) { nic.testObject.contents = view[header.IPv6MinimumSize:][:payloadLen] pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: view.ToVectorisedView(), + Payload: buffer.NewWithData(view), }) ep.HandlePacket(pkt) pkt.DecRef() @@ -873,7 +874,7 @@ func TestIPv4ReceiveControl(t *testing.T) { } const dataOffset = header.IPv4MinimumSize*2 + header.ICMPv4MinimumSize - view := buffer.NewView(dataOffset + dataLen) + view := make([]byte, dataOffset+dataLen) // Create the outer IPv4 header. ip := header.IPv4(view) @@ -964,7 +965,7 @@ func TestIPv4FragmentationReceive(t *testing.T) { totalLen := header.IPv4MinimumSize + 24 - frag1 := buffer.NewView(totalLen) + frag1 := make([]byte, totalLen) ip1 := header.IPv4(frag1) ip1.Encode(&header.IPv4Fields{ TotalLength: uint16(totalLen), @@ -982,7 +983,7 @@ func TestIPv4FragmentationReceive(t *testing.T) { frag1[i] = uint8(i) } - frag2 := buffer.NewView(totalLen) + frag2 := make([]byte, totalLen) ip2 := header.IPv4(frag2) ip2.Encode(&header.IPv4Fields{ TotalLength: uint16(totalLen), @@ -1018,7 +1019,7 @@ func TestIPv4FragmentationReceive(t *testing.T) { // Send first segment. pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: frag1.ToVectorisedView(), + Payload: buffer.NewWithData(frag1), }) ep.HandlePacket(pkt) pkt.DecRef() @@ -1032,7 +1033,7 @@ func TestIPv4FragmentationReceive(t *testing.T) { // Send second segment. pkt = stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: frag2.ToVectorisedView(), + Payload: buffer.NewWithData(frag2), }) ep.HandlePacket(pkt) pkt.DecRef() @@ -1064,7 +1065,7 @@ func TestIPv6Send(t *testing.T) { } // Allocate and initialize the payload view. - payload := buffer.NewView(100) + payload := make([]byte, 100) for i := 0; i < len(payload); i++ { payload[i] = uint8(i) } @@ -1072,7 +1073,7 @@ func TestIPv6Send(t *testing.T) { // Setup the packet buffer. pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(ep.MaxHeaderLength()), - Data: payload.ToVectorisedView(), + Payload: buffer.NewWithData(payload), }) defer pkt.DecRef() // Issue the write. @@ -1225,7 +1226,7 @@ func TestIPv6ReceiveControl(t *testing.T) { if c.fragmentOffset != nil { dataOffset += header.IPv6FragmentHeaderSize } - view := buffer.NewView(dataOffset + dataLen) + view := make([]byte, dataOffset+dataLen) // Create the outer IPv6 header. ip := header.IPv6(view) @@ -1309,10 +1310,10 @@ func TestIPv6ReceiveControl(t *testing.T) { // after truncation, is large enough to hold a network header, it makes part of // view the packet's NetworkHeader and the rest its Data. Otherwise all of view // becomes Data. -func truncatedPacket(view buffer.View, trunc, netHdrLen int) *stack.PacketBuffer { +func truncatedPacket(view []byte, trunc, netHdrLen int) *stack.PacketBuffer { v := view[:len(view)-trunc] pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: v.ToVectorisedView(), + Payload: buffer.NewWithData(v), }) return pkt } @@ -1360,7 +1361,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum tcpip.NetworkProtocolNumber nicAddr tcpip.AddressWithPrefix remoteAddr tcpip.Address - pktGen func(*testing.T, tcpip.Address) buffer.VectorisedView + pktGen func(*testing.T, tcpip.Address) buffer.Buffer checker func(*testing.T, *stack.PacketBuffer, tcpip.Address) expectedErr tcpip.Error }{ @@ -1370,9 +1371,9 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv4MinimumSize + len(data) - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1383,7 +1384,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv4Addr, }) - return hdr.View().ToVectorisedView() + return buffer.NewWithData(hdr.View()) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv4Any { @@ -1411,9 +1412,9 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv4MinimumSize + len(data) - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1425,7 +1426,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { DstAddr: remoteIPv4Addr, }) ip.SetHeaderLength(header.IPv4MinimumSize - 1) - return hdr.View().ToVectorisedView() + return buffer.NewWithData(hdr.View()) }, expectedErr: &tcpip.ErrMalformedHeader{}, }, @@ -1435,7 +1436,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ip := header.IPv4(make([]byte, header.IPv4MinimumSize)) ip.Encode(&header.IPv4Fields{ Protocol: transportProto, @@ -1443,7 +1444,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv4Addr, }) - return buffer.View(ip[:len(ip)-1]).ToVectorisedView() + return buffer.NewWithData(ip[:len(ip)-1]) }, expectedErr: &tcpip.ErrMalformedHeader{}, }, @@ -1453,7 +1454,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ip := header.IPv4(make([]byte, header.IPv4MinimumSize)) ip.Encode(&header.IPv4Fields{ Protocol: transportProto, @@ -1461,7 +1462,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv4Addr, }) - return buffer.View(ip).ToVectorisedView() + return buffer.NewWithData(ip) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv4Any { @@ -1489,10 +1490,10 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ipHdrLen := int(header.IPv4MinimumSize + ipv4Options.Length()) totalLen := ipHdrLen + len(data) - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1504,7 +1505,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { DstAddr: remoteIPv4Addr, Options: ipv4Options, }) - return hdr.View().ToVectorisedView() + return buffer.NewWithData(hdr.View()) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv4Any { @@ -1534,7 +1535,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv4.ProtocolNumber, nicAddr: localIPv4AddrWithPrefix, remoteAddr: remoteIPv4Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ip := header.IPv4(make([]byte, header.IPv4MinimumSize+ipv4Options.Length())) ip.Encode(&header.IPv4Fields{ Protocol: transportProto, @@ -1543,9 +1544,9 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { DstAddr: remoteIPv4Addr, Options: ipv4Options, }) - vv := buffer.View(ip).ToVectorisedView() - vv.AppendView(data) - return vv + buf := buffer.NewWithData(ip) + buf.Append(data) + return buf }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv4Any { @@ -1575,9 +1576,9 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv6.ProtocolNumber, nicAddr: localIPv6AddrWithPrefix, remoteAddr: remoteIPv6Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv6MinimumSize + len(data) - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1588,7 +1589,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv6Addr, }) - return hdr.View().ToVectorisedView() + return buffer.NewWithData(hdr.View()) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv6Any { @@ -1615,9 +1616,9 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv6.ProtocolNumber, nicAddr: localIPv6AddrWithPrefix, remoteAddr: remoteIPv6Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv6MinimumSize + len(ipv6FragmentExtHdr) + len(data) - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1633,7 +1634,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv6Addr, }) - return hdr.View().ToVectorisedView() + return buffer.NewWithData(hdr.View()) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv6Any { @@ -1660,7 +1661,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv6.ProtocolNumber, nicAddr: localIPv6AddrWithPrefix, remoteAddr: remoteIPv6Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ip := header.IPv6(make([]byte, header.IPv6MinimumSize)) ip.Encode(&header.IPv6Fields{ TransportProtocol: transportProto, @@ -1668,7 +1669,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv6Addr, }) - return buffer.View(ip).ToVectorisedView() + return buffer.NewWithData(ip) }, checker: func(t *testing.T, pkt *stack.PacketBuffer, src tcpip.Address) { if src == header.IPv6Any { @@ -1695,7 +1696,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { protoNum: ipv6.ProtocolNumber, nicAddr: localIPv6AddrWithPrefix, remoteAddr: remoteIPv6Addr, - pktGen: func(t *testing.T, src tcpip.Address) buffer.VectorisedView { + pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ip := header.IPv6(make([]byte, header.IPv6MinimumSize)) ip.Encode(&header.IPv6Fields{ TransportProtocol: transportProto, @@ -1703,7 +1704,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { SrcAddr: src, DstAddr: remoteIPv4Addr, }) - return buffer.View(ip[:len(ip)-1]).ToVectorisedView() + return buffer.NewWithData(ip[:len(ip)-1]) }, expectedErr: &tcpip.ErrMalformedHeader{}, }, @@ -1758,7 +1759,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: test.pktGen(t, subTest.srcAddr), + Payload: test.pktGen(t, subTest.srcAddr), }) err := r.WriteHeaderIncludedPacket(pkt) pkt.DecRef() @@ -1798,9 +1799,9 @@ func TestICMPInclusionSize(t *testing.T) { // IPv4 function to create a IP packet and send it to the stack. // The packet should generate an error response. We can do that by using an // unknown transport protocol (254). - rxIPv4Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) buffer.View { + rxIPv4Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) []byte { totalLen := header.IPv4MinimumSize + len(payload) - hdr := buffer.NewPrependable(header.IPv4MinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize) ip := header.IPv4(hdr.Prepend(header.IPv4MinimumSize)) ip.Encode(&header.IPv4Fields{ TotalLength: uint16(totalLen), @@ -1810,13 +1811,13 @@ func TestICMPInclusionSize(t *testing.T) { DstAddr: localIPv4Addr, }) ip.SetChecksum(^ip.CalculateChecksum()) - vv := hdr.View().ToVectorisedView() - vv.AppendView(buffer.View(payload)) + buf := buffer.NewWithData(hdr.View()) + buf.Append(payload) // Take a copy before InjectInbound takes ownership of vv // as vv may be changed during the call. - v := vv.ToView() + v := buf.Flatten() pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) e.InjectInbound(header.IPv4ProtocolNumber, pkt) pkt.DecRef() @@ -1828,8 +1829,8 @@ func TestICMPInclusionSize(t *testing.T) { // ICMP error response and have enough data to allow the testing of the // inclusion of the errant packet. Use `unknown next header' to generate // the error. - rxIPv6Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) buffer.View { - hdr := buffer.NewPrependable(header.IPv6MinimumSize) + rxIPv6Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) []byte { + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize)) ip.Encode(&header.IPv6Fields{ PayloadLength: uint16(len(payload)), @@ -1838,21 +1839,21 @@ func TestICMPInclusionSize(t *testing.T) { SrcAddr: src, DstAddr: localIPv6Addr, }) - vv := hdr.View().ToVectorisedView() - vv.AppendView(buffer.View(payload)) + buf := buffer.NewWithData(hdr.View()) + buf.Append(payload) // Take a copy before InjectInbound takes ownership of vv // as vv may be changed during the call. - v := vv.ToView() + v := buf.Flatten() pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) e.InjectInbound(header.IPv6ProtocolNumber, pkt) pkt.DecRef() return v } - v4Checker := func(t *testing.T, pkt *stack.PacketBuffer, payload buffer.View) { + v4Checker := func(t *testing.T, pkt *stack.PacketBuffer, payload []byte) { // We already know the entire packet is the right size so we can use its // length to calculate the right payload size to check. expectedPayloadLength := pkt.Size() - header.IPv4MinimumSize - header.ICMPv4MinimumSize @@ -1870,7 +1871,7 @@ func TestICMPInclusionSize(t *testing.T) { ) } - v6Checker := func(t *testing.T, pkt *stack.PacketBuffer, payload buffer.View) { + v6Checker := func(t *testing.T, pkt *stack.PacketBuffer, payload []byte) { // We already know the entire packet is the right size so we can use its // length to calculate the right payload size to check. expectedPayloadLength := pkt.Size() - header.IPv6MinimumSize - header.ICMPv6MinimumSize @@ -1888,8 +1889,8 @@ func TestICMPInclusionSize(t *testing.T) { tests := []struct { name string srcAddress tcpip.Address - injector func(*channel.Endpoint, tcpip.Address, []byte) buffer.View - checker func(*testing.T, *stack.PacketBuffer, buffer.View) + injector func(*channel.Endpoint, tcpip.Address, []byte) []byte + checker func(*testing.T, *stack.PacketBuffer, []byte) payloadLength int // Not including IP header. linkMTU uint32 // Largest IP packet that the link can send as payload. replyLength int // Total size of IP/ICMP packet expected back. @@ -2006,7 +2007,7 @@ func TestICMPInclusionSize(t *testing.T) { e := addLinkEndpointToStackWithMTU(t, s, test.linkMTU) defer e.Close() // Allocate and initialize the payload view. - payload := buffer.NewView(test.payloadLength) + payload := make([]byte, test.payloadLength) for i := 0; i < len(payload); i++ { payload[i] = uint8(i) } diff --git a/pkg/tcpip/network/ipv4/BUILD b/pkg/tcpip/network/ipv4/BUILD index 97eeb782c..f4527ddb9 100644 --- a/pkg/tcpip/network/ipv4/BUILD +++ b/pkg/tcpip/network/ipv4/BUILD @@ -13,6 +13,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/atomicbitops", + "//pkg/buffer", "//pkg/sync", "//pkg/tcpip", "//pkg/tcpip/buffer", @@ -35,6 +36,7 @@ go_test( "main_test.go", ], deps = [ + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/sync", diff --git a/pkg/tcpip/network/ipv4/icmp.go b/pkg/tcpip/network/ipv4/icmp.go index 3885fcc07..e6ae12ed7 100644 --- a/pkg/tcpip/network/ipv4/icmp.go +++ b/pkg/tcpip/network/ipv4/icmp.go @@ -17,8 +17,8 @@ package ipv4 import ( "fmt" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/header/parse" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -324,11 +324,11 @@ func (e *endpoint) handleICMP(pkt *stack.PacketBuffer) { replyICMPHdr.SetChecksum(0) replyICMPHdr.SetChecksum(^header.Checksum(replyData, 0)) - replyVV := buffer.View(replyIPHdr).ToVectorisedView() - replyVV.AppendView(replyData) + replyBuf := buffer.NewWithData(replyIPHdr) + replyBuf.AppendOwned(replyData) replyPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(r.MaxHeaderLength()), - Data: replyVV, + Payload: replyBuf, }) defer replyPkt.DecRef() // Populate the network/transport headers in the packet buffer so the @@ -641,18 +641,20 @@ func (p *protocol) returnError(reason icmpReason, pkt *stack.PacketBuffer, deliv // view with the entire incoming IP packet reassembled and truncated as // required. This is now the payload of the new ICMP packet and no longer // considered a packet in its own right. - newHeader := append(buffer.View(nil), origIPHdr...) + + var newHeader []byte + newHeader = append(newHeader, origIPHdr...) newHeader = append(newHeader, transportHeader...) - payload := newHeader.ToVectorisedView() - if dataCap := payloadLen - payload.Size(); dataCap > 0 { - payload.AppendView(pkt.Data().AsRange().Capped(dataCap).ToOwnedView()) + payload := buffer.NewWithData(newHeader) + if dataCap := payloadLen - int(payload.Size()); dataCap > 0 { + payload.AppendOwned(pkt.Data().AsRange().Capped(dataCap).ToOwnedView()) } else { - payload.CapLength(payloadLen) + payload.Truncate(int64(payloadLen)) } icmpPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(route.MaxHeaderLength()) + header.ICMPv4MinimumSize, - Data: payload, + Payload: payload, }) defer icmpPkt.DecRef() diff --git a/pkg/tcpip/network/ipv4/igmp.go b/pkg/tcpip/network/ipv4/igmp.go index d9e231335..b90e831ef 100644 --- a/pkg/tcpip/network/ipv4/igmp.go +++ b/pkg/tcpip/network/ipv4/igmp.go @@ -19,8 +19,8 @@ import ( "time" "gvisor.dev/gvisor/pkg/atomicbitops" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/network/internal/ip" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -312,14 +312,14 @@ func (igmp *igmpState) handleMembershipReport(groupAddress tcpip.Address) { // // +checklocksread:igmp.ep.mu func (igmp *igmpState) writePacket(destAddress tcpip.Address, groupAddress tcpip.Address, igmpType header.IGMPType) (bool, tcpip.Error) { - igmpData := header.IGMP(buffer.NewView(header.IGMPReportMinimumSize)) + igmpData := header.IGMP(make([]byte, header.IGMPReportMinimumSize)) igmpData.SetType(igmpType) igmpData.SetGroupAddress(groupAddress) igmpData.SetChecksum(header.IGMPCalculateChecksum(igmpData)) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(igmp.ep.MaxHeaderLength()), - Data: buffer.View(igmpData).ToVectorisedView(), + Payload: buffer.NewWithData(igmpData), }) defer pkt.DecRef() diff --git a/pkg/tcpip/network/ipv4/igmp_test.go b/pkg/tcpip/network/ipv4/igmp_test.go index de627e3a7..959303a06 100644 --- a/pkg/tcpip/network/ipv4/igmp_test.go +++ b/pkg/tcpip/network/ipv4/igmp_test.go @@ -18,9 +18,9 @@ import ( "testing" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -110,7 +110,7 @@ func createAndInjectIGMPPacket(e *channel.Endpoint, igmpType header.IGMPType, ma &header.IPv4SerializableRouterAlertOption{}, } } - buf := buffer.NewView(header.IPv4MinimumSize + int(options.Length()) + header.IGMPQueryMinimumSize) + buf := make([]byte, header.IPv4MinimumSize+int(options.Length())+header.IGMPQueryMinimumSize) ip := header.IPv4(buf) ip.Encode(&header.IPv4Fields{ @@ -129,7 +129,7 @@ func createAndInjectIGMPPacket(e *channel.Endpoint, igmpType header.IGMPType, ma igmp.SetGroupAddress(groupAddress) igmp.SetChecksum(header.IGMPCalculateChecksum(igmp)) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buf.ToVectorisedView(), + Payload: buffer.NewWithData(buf), }) e.InjectInbound(ipv4.ProtocolNumber, pkt) pkt.DecRef() diff --git a/pkg/tcpip/network/ipv4/ipv4.go b/pkg/tcpip/network/ipv4/ipv4.go index f00c9fdbf..c176350b4 100644 --- a/pkg/tcpip/network/ipv4/ipv4.go +++ b/pkg/tcpip/network/ipv4/ipv4.go @@ -124,7 +124,7 @@ func (e *endpoint) HandleLinkResolutionFailure(pkt *stack.PacketBuffer) { // handleControl expects the entire offending packet to be in the packet // buffer's data field. pkt = stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), + Payload: pkt.Buffer(), }) defer pkt.DecRef() pkt.NICID = e.nic.ID() @@ -1423,6 +1423,8 @@ func (p *protocol) MinimumPacketSize() int { } // ParseAddresses implements stack.NetworkProtocol. +// TODO(b/230896518): Remove buffer.View once stack.NetworkProtocol is changed +// to use pkg/buffer.Buffer. func (*protocol) ParseAddresses(v buffer.View) (src, dst tcpip.Address) { h := header.IPv4(v) return h.SourceAddress(), h.DestinationAddress() diff --git a/pkg/tcpip/network/ipv4/ipv4_test.go b/pkg/tcpip/network/ipv4/ipv4_test.go index 282802fb0..db1f46dd6 100644 --- a/pkg/tcpip/network/ipv4/ipv4_test.go +++ b/pkg/tcpip/network/ipv4/ipv4_test.go @@ -26,10 +26,11 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -246,7 +247,7 @@ func newICMPEchoPacket(t *testing.T, srcAddr, dstAddr tcpip.Address, ttl uint8, t.Fatalf("ipHeaderLength = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize) } totalLength := ipHeaderLength + header.ICMPv4MinimumSize + options.payloadLength - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) hdr.Prepend(options.payloadLength) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) icmpH.SetIdent(randomIdent) @@ -1739,7 +1740,9 @@ func TestIPv4Sanity(t *testing.T) { t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize) } totalLen := uint16(ipHeaderLength + header.ICMPv4MinimumSize) - hdr := buffer.NewPrependable(int(totalLen)) + // TODO(b/230896518): tcpipbuffer is only needed for Prependable. Move + // Prependable to outside pkg/tcpip/buffer. + hdr := tcpipbuffer.NewPrependable(int(totalLen)) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) // Specify ident/seq to make sure we get the same in the response. @@ -1780,7 +1783,7 @@ func TestIPv4Sanity(t *testing.T) { } ip.SetChecksum(^ipHeaderChecksum) requestPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) defer requestPkt.DecRef() e.InjectInbound(header.IPv4ProtocolNumber, requestPkt) @@ -1908,29 +1911,29 @@ func TestIPv4Sanity(t *testing.T) { func compareFragments(packets []*stack.PacketBuffer, sourcePacket *stack.PacketBuffer, mtu uint32, wantFragments []fragmentInfo, proto tcpip.TransportProtocolNumber, withIPHeader bool, expectedAvailableHeaderBytes int) error { // Make a complete array of the sourcePacket packet. var source header.IPv4 - vv := buffer.NewVectorisedView(sourcePacket.Size(), sourcePacket.Views()) + buf := sourcePacket.Buffer() // If the packet to be fragmented contains an IPv4 header, use that header for // validating fragment headers. Else, use the header of the first fragment. if withIPHeader { - source = header.IPv4(vv.ToView()) + source = header.IPv4(buf.Flatten()) } else { source = header.IPv4(packets[0].NetworkHeader().View()) - source = append(source, vv.ToView()...) + source = append(source, buf.Flatten()...) } // Make a copy of the IP header, which will be modified in some fields to make // an expected header. - sourceCopy := header.IPv4(append(buffer.View(nil), source[:source.HeaderLength()]...)) + sourceCopy := header.IPv4(append([]byte{}, source[:source.HeaderLength()]...)) sourceCopy.SetChecksum(0) sourceCopy.SetFlagsFragmentOffset(0, 0) sourceCopy.SetTotalLength(0) // Build up an array of the bytes sent. - var reassembledPayload buffer.VectorisedView + var reassembledPayload buffer.Buffer for i, packet := range packets { // Confirm that the packet is valid. - allBytes := buffer.NewVectorisedView(packet.Size(), packet.Views()) - fragmentIPHeader := header.IPv4(allBytes.ToView()) + allBytes := packet.Buffer() + fragmentIPHeader := header.IPv4(allBytes.Flatten()) if !fragmentIPHeader.IsValid(len(fragmentIPHeader)) { return fmt.Errorf("fragment #%d: IP packet is invalid:\n%s", i, hex.Dump(fragmentIPHeader)) } @@ -1954,8 +1957,8 @@ func compareFragments(packets []*stack.PacketBuffer, sourcePacket *stack.PacketB } else { sourceCopy.SetFlagsFragmentOffset(sourceCopy.Flags()&^header.IPv4FlagMoreFragments, wantFragments[i].offset) } - reassembledPayload.AppendView(packet.TransportHeader().View()) - reassembledPayload.AppendView(packet.Data().AsRange().ToOwnedView()) + reassembledPayload.Append(packet.TransportHeader().View()) + reassembledPayload.Append(packet.Data().AsRange().ToOwnedView()) // Clear out the checksum and length from the ip because we can't compare // it. sourceCopy.SetTotalLength(wantFragments[i].payloadSize + header.IPv4MinimumSize) @@ -1974,8 +1977,8 @@ func compareFragments(packets []*stack.PacketBuffer, sourcePacket *stack.PacketB } } - expected := buffer.View(source[source.HeaderLength():]) - if diff := cmp.Diff(expected, reassembledPayload.ToView()); diff != "" { + expected := []byte(source[source.HeaderLength():]) + if diff := cmp.Diff(expected, reassembledPayload.Flatten()); diff != "" { return fmt.Errorf("reassembledPayload mismatch (-want +got):\n%s", diff) } @@ -2486,7 +2489,7 @@ func TestInvalidFragments(t *testing.T) { for _, f := range test.fragments { pktSize := header.IPv4MinimumSize + len(f.payload) - hdr := buffer.NewPrependable(pktSize) + hdr := tcpipbuffer.NewPrependable(pktSize) ip := header.IPv4(hdr.Prepend(pktSize)) ip.Encode(&f.ipv4fields) @@ -2509,9 +2512,8 @@ func TestInvalidFragments(t *testing.T) { ip.SetChecksum(^ip.CalculateChecksum()) } - vv := hdr.View().ToVectorisedView() pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv4ProtocolNumber, pkt) pkt.DecRef() @@ -2718,10 +2720,10 @@ func TestFragmentReassemblyTimeout(t *testing.T) { NIC: nicID, }}) - var firstFragmentSent buffer.View + var firstFragmentSent buffer.Buffer for _, f := range test.fragments { pktSize := header.IPv4MinimumSize - hdr := buffer.NewPrependable(pktSize) + hdr := tcpipbuffer.NewPrependable(pktSize) ip := header.IPv4(hdr.Prepend(pktSize)) ip.Encode(&f.ipv4fields) @@ -2729,15 +2731,15 @@ func TestFragmentReassemblyTimeout(t *testing.T) { ip.SetChecksum(0) ip.SetChecksum(^ip.CalculateChecksum()) - vv := hdr.View().ToVectorisedView() - vv.AppendView(f.payload) + buf := buffer.NewWithData(hdr.View()) + buf.Append(f.payload) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) - if firstFragmentSent == nil && ip.FragmentOffset() == 0 { - firstFragmentSent = stack.PayloadSince(pkt.NetworkHeader()) + if firstFragmentSent.Size() == 0 && ip.FragmentOffset() == 0 { + firstFragmentSent = buffer.NewWithData(stack.PayloadSince(pkt.NetworkHeader())) } e.InjectInbound(header.IPv4ProtocolNumber, pkt) @@ -2756,7 +2758,7 @@ func TestFragmentReassemblyTimeout(t *testing.T) { if reply == nil { t.Fatal("expected ICMP error message missing") } - if firstFragmentSent == nil { + if firstFragmentSent.Size() == 0 { t.Fatalf("unexpected ICMP error message received: %#v", reply) } @@ -2769,7 +2771,7 @@ func TestFragmentReassemblyTimeout(t *testing.T) { checker.ICMPv4Type(header.ICMPv4TimeExceeded), checker.ICMPv4Code(header.ICMPv4ReassemblyTimeout), checker.ICMPv4Checksum(), - checker.ICMPv4Payload(firstFragmentSent), + checker.ICMPv4Payload(firstFragmentSent.Flatten()), ), ) reply.DecRef() @@ -2789,15 +2791,15 @@ func TestReceiveFragments(t *testing.T) { ) // Build and return a UDP header containing payload. - udpGen := func(payloadLen int, multiplier uint8, src, dst tcpip.Address) buffer.View { - payload := buffer.NewView(payloadLen) + udpGen := func(payloadLen int, multiplier uint8, src, dst tcpip.Address) []byte { + payload := make([]byte, payloadLen) for i := 0; i < len(payload); i++ { payload[i] = uint8(i) * multiplier } udpLength := header.UDPMinimumSize + len(payload) - hdr := buffer.NewPrependable(udpLength) + hdr := tcpipbuffer.NewPrependable(udpLength) u := header.UDP(hdr.Prepend(udpLength)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -2834,7 +2836,7 @@ func TestReceiveFragments(t *testing.T) { id uint16 flags uint8 fragmentOffset uint16 - payload buffer.View + payload []byte } tests := []struct { @@ -3210,7 +3212,7 @@ func TestReceiveFragments(t *testing.T) { // Prepare and send the fragments. for _, frag := range test.fragments { - hdr := buffer.NewPrependable(header.IPv4MinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize) // Serialize IPv4 fixed header. ip := header.IPv4(hdr.Prepend(header.IPv4MinimumSize)) @@ -3226,10 +3228,10 @@ func TestReceiveFragments(t *testing.T) { }) ip.SetChecksum(^ip.CalculateChecksum()) - vv := hdr.View().ToVectorisedView() - vv.AppendView(frag.payload) + buf := buffer.NewWithData(hdr.View()) + buf.Append(frag.payload) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) e.InjectInbound(header.IPv4ProtocolNumber, pkt) pkt.DecRef() @@ -3398,7 +3400,7 @@ func TestWriteStats(t *testing.T) { for i := 0; i < nPackets; i++ { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: header.UDPMinimumSize + int(rt.MaxHeaderLength()), - Data: buffer.NewView(0).ToVectorisedView(), + Payload: buffer.Buffer{}, }) defer pkt.DecRef() pkt.TransportHeader().Push(header.UDPMinimumSize) @@ -3509,7 +3511,7 @@ func TestPacketQueuing(t *testing.T) { { name: "ICMP Error", rxPkt: func(e *channel.Endpoint) { - hdr := buffer.NewPrependable(header.IPv4MinimumSize + header.UDPMinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -3529,7 +3531,7 @@ func TestPacketQueuing(t *testing.T) { }) ip.SetChecksum(^ip.CalculateChecksum()) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) defer pkt.DecRef() e.InjectInbound(ipv4.ProtocolNumber, pkt) @@ -3559,7 +3561,7 @@ func TestPacketQueuing(t *testing.T) { name: "Ping", rxPkt: func(e *channel.Endpoint) { totalLen := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) pkt.SetType(header.ICMPv4Echo) pkt.SetCode(0) @@ -3575,7 +3577,7 @@ func TestPacketQueuing(t *testing.T) { }) ip.SetChecksum(^ip.CalculateChecksum()) echoPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) defer echoPkt.DecRef() e.InjectInbound(header.IPv4ProtocolNumber, echoPkt) @@ -3661,7 +3663,7 @@ func TestPacketQueuing(t *testing.T) { // Send an ARP reply to complete link address resolution. { - hdr := buffer.View(make([]byte, header.ARPSize)) + hdr := make([]byte, header.ARPSize) packet := header.ARP(hdr) packet.SetIPv4OverEthernet() packet.SetOp(header.ARPReply) @@ -3670,7 +3672,7 @@ func TestPacketQueuing(t *testing.T) { copy(packet.HardwareAddressTarget(), host1NICLinkAddr) copy(packet.ProtocolAddressTarget(), host1IPv4Addr.AddressWithPrefix.Address) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.ToVectorisedView(), + Payload: buffer.NewWithData(hdr), }) e.InjectInbound(arp.ProtocolNumber, pkt) pkt.DecRef() @@ -3851,14 +3853,14 @@ func TestIcmpRateLimit(t *testing.T) { }) tests := []struct { name string - createPacket func() buffer.View + createPacket func() []byte check func(*testing.T, *channel.Endpoint, int) }{ { name: "echo", - createPacket: func() buffer.View { + createPacket: func() []byte { totalLength := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) icmpH.SetIdent(1) icmpH.SetSequence(1) @@ -3896,9 +3898,9 @@ func TestIcmpRateLimit(t *testing.T) { }, { name: "dst unreachable", - createPacket: func() buffer.View { + createPacket: func() []byte { totalLength := header.IPv4MinimumSize + header.UDPMinimumSize - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) udpH := header.UDP(hdr.Prepend(header.UDPMinimumSize)) udpH.Encode(&header.UDPFields{ SrcPort: 100, @@ -3942,7 +3944,7 @@ func TestIcmpRateLimit(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { for round := 0; round < icmpBurst+1; round++ { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: testCase.createPacket().ToVectorisedView(), + Payload: buffer.NewWithData(testCase.createPacket()), }) e.InjectInbound(header.IPv4ProtocolNumber, pkt) pkt.DecRef() diff --git a/pkg/tcpip/network/ipv6/BUILD b/pkg/tcpip/network/ipv6/BUILD index 1c1159659..71cab1d08 100644 --- a/pkg/tcpip/network/ipv6/BUILD +++ b/pkg/tcpip/network/ipv6/BUILD @@ -15,6 +15,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/atomicbitops", + "//pkg/buffer", "//pkg/sync", "//pkg/tcpip", "//pkg/tcpip/buffer", @@ -39,6 +40,7 @@ go_test( ], library = ":ipv6", deps = [ + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", @@ -66,10 +68,10 @@ go_test( srcs = ["mld_test.go"], deps = [ ":ipv6", + "//pkg/buffer", "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/checker", "//pkg/tcpip/faketime", "//pkg/tcpip/header", diff --git a/pkg/tcpip/network/ipv6/icmp.go b/pkg/tcpip/network/ipv6/icmp.go index 280055f05..529e834cc 100644 --- a/pkg/tcpip/network/ipv6/icmp.go +++ b/pkg/tcpip/network/ipv6/icmp.go @@ -17,6 +17,7 @@ package ipv6 import ( "fmt" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -674,7 +675,7 @@ func (e *endpoint) handleICMP(pkt *stack.PacketBuffer, hasFragmentHeader bool, r replyPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(r.MaxHeaderLength()) + header.ICMPv6EchoMinimumSize, - Data: pkt.Data().ExtractVV(), + Payload: pkt.Data().AsBuffer(), }) defer replyPkt.DecRef() icmp := header.ICMPv6(replyPkt.TransportHeader().Push(header.ICMPv6EchoMinimumSize)) @@ -1164,14 +1165,15 @@ func (p *protocol) returnError(reason icmpReason, pkt *stack.PacketBuffer, deliv if payloadLen > available { payloadLen = available } - payload := network.ToVectorisedView() - payload.AppendView(transport) - payload.Append(pkt.Data().ExtractVV()) - payload.CapLength(payloadLen) + payload := buffer.NewWithData(network) + payload.AppendOwned(transport) + dataBuf := pkt.Data().AsBuffer() + payload.Merge(&dataBuf) + payload.Truncate(int64(payloadLen)) newPkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(route.MaxHeaderLength()) + header.ICMPv6ErrorHeaderSize, - Data: payload, + Payload: payload, }) defer newPkt.DecRef() newPkt.TransportProtocolNumber = header.ICMPv6ProtocolNumber diff --git a/pkg/tcpip/network/ipv6/icmp_test.go b/pkg/tcpip/network/ipv6/icmp_test.go index a3b28cd11..ca815a8bc 100644 --- a/pkg/tcpip/network/ipv6/icmp_test.go +++ b/pkg/tcpip/network/ipv6/icmp_test.go @@ -23,9 +23,10 @@ import ( "github.com/google/go-cmp/cmp" "golang.org/x/time/rate" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -179,7 +180,7 @@ func handleICMPInIPv6(ep stack.NetworkEndpoint, src, dst tcpip.Address, icmp hea }, } } - ip := buffer.NewView(header.IPv6MinimumSize + extensionHeaders.Length()) + ip := make([]byte, header.IPv6MinimumSize+extensionHeaders.Length()) header.IPv6(ip).Encode(&header.IPv6Fields{ PayloadLength: uint16(len(icmp)), TransportProtocol: header.ICMPv6ProtocolNumber, @@ -189,10 +190,10 @@ func handleICMPInIPv6(ep stack.NetworkEndpoint, src, dst tcpip.Address, icmp hea ExtensionHeaders: extensionHeaders, }) - vv := ip.ToVectorisedView() - vv.AppendView(buffer.View(icmp)) + buf := buffer.NewWithData(ip) + buf.Append(icmp) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) ep.HandlePacket(pkt) pkt.DecRef() @@ -359,7 +360,7 @@ func TestICMPCounts(t *testing.T) { } for _, typ := range types { - icmp := header.ICMPv6(buffer.NewView(typ.size + len(typ.extraData))) + icmp := header.ICMPv6(make([]byte, typ.size+len(typ.extraData))) copy(icmp[typ.size:], typ.extraData) icmp.SetType(typ.typ) icmp.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ @@ -374,7 +375,7 @@ func TestICMPCounts(t *testing.T) { // Construct an empty ICMP packet so that // Stats().ICMP.ICMPv6ReceivedPacketStats.Invalid is incremented. - handleICMPInIPv6(ep, lladdr1, lladdr0, header.ICMPv6(buffer.NewView(header.IPv6MinimumSize)), arbitraryHopLimit, false) + handleICMPInIPv6(ep, lladdr1, lladdr0, header.ICMPv6(make([]byte, header.IPv6MinimumSize)), arbitraryHopLimit, false) icmpv6Stats := s.Stats().ICMP.V6.PacketsReceived visitStats(reflect.ValueOf(&icmpv6Stats).Elem(), func(name string, s *tcpip.StatCounter) { @@ -526,7 +527,7 @@ func routeICMPv6Packet(t *testing.T, clock *faketime.ManualClock, args routeArgs { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.NewVectorisedView(pi.Size(), pi.Views()), + Payload: pi.Buffer(), }) args.dst.InjectInbound(pi.NetworkProtocolNumber, pkt) pkt.DecRef() @@ -568,7 +569,7 @@ func TestLinkResolution(t *testing.T) { } defer r.Release() - hdr := buffer.NewPrependable(int(r.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6EchoMinimumSize) + hdr := tcpipbuffer.NewPrependable(int(r.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6EchoMinimumSize) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6EchoMinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ @@ -765,7 +766,7 @@ func TestICMPChecksumValidationSimple(t *testing.T) { } handleIPv6Payload := func(checksum bool) { - icmp := header.ICMPv6(buffer.NewView(typ.size + len(typ.extraData))) + icmp := header.ICMPv6(make([]byte, typ.size+len(typ.extraData))) copy(icmp[typ.size:], typ.extraData) icmp.SetType(typ.typ) if checksum { @@ -775,7 +776,7 @@ func TestICMPChecksumValidationSimple(t *testing.T) { Dst: lladdr0, })) } - ip := header.IPv6(buffer.NewView(header.IPv6MinimumSize)) + ip := header.IPv6(make([]byte, header.IPv6MinimumSize)) ip.Encode(&header.IPv6Fields{ PayloadLength: uint16(len(icmp)), TransportProtocol: header.ICMPv6ProtocolNumber, @@ -783,8 +784,9 @@ func TestICMPChecksumValidationSimple(t *testing.T) { SrcAddr: lladdr1, DstAddr: lladdr0, }) + buf := buffer.NewWithData(append(ip, icmp...)) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.NewVectorisedView(len(ip)+len(icmp), []buffer.View{buffer.View(ip), buffer.View(icmp)}), + Payload: buf, }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -843,14 +845,14 @@ func TestICMPChecksumValidationSimple(t *testing.T) { func TestICMPChecksumValidationWithPayload(t *testing.T) { const simpleBodySize = 64 - simpleBody := func(view buffer.View) { + simpleBody := func(view []byte) { for i := 0; i < simpleBodySize; i++ { view[i] = uint8(i) } } const errorICMPBodySize = header.IPv6MinimumSize + simpleBodySize - errorICMPBody := func(view buffer.View) { + errorICMPBody := func(view []byte) { ip := header.IPv6(view) ip.Encode(&header.IPv6Fields{ PayloadLength: simpleBodySize, @@ -868,7 +870,7 @@ func TestICMPChecksumValidationWithPayload(t *testing.T) { size int statCounter func(tcpip.ICMPv6ReceivedPacketStats) *tcpip.StatCounter payloadSize int - payload func(buffer.View) + payload func([]byte) }{ { "DstUnreachable", @@ -964,9 +966,9 @@ func TestICMPChecksumValidationWithPayload(t *testing.T) { ) } - handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func(buffer.View), checksum bool) { + handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func([]byte), checksum bool) { icmpSize := size + payloadSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + icmpSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + icmpSize) icmpHdr := header.ICMPv6(hdr.Prepend(icmpSize)) icmpHdr.SetType(typ) payloadFn(icmpHdr.Payload()) @@ -988,7 +990,7 @@ func TestICMPChecksumValidationWithPayload(t *testing.T) { DstAddr: lladdr0, }) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -1032,14 +1034,14 @@ func TestICMPChecksumValidationWithPayload(t *testing.T) { func TestICMPChecksumValidationWithPayloadMultipleViews(t *testing.T) { const simpleBodySize = 64 - simpleBody := func(view buffer.View) { + simpleBody := func(view []byte) { for i := 0; i < simpleBodySize; i++ { view[i] = uint8(i) } } const errorICMPBodySize = header.IPv6MinimumSize + simpleBodySize - errorICMPBody := func(view buffer.View) { + errorICMPBody := func(view []byte) { ip := header.IPv6(view) ip.Encode(&header.IPv6Fields{ PayloadLength: simpleBodySize, @@ -1057,7 +1059,7 @@ func TestICMPChecksumValidationWithPayloadMultipleViews(t *testing.T) { size int statCounter func(tcpip.ICMPv6ReceivedPacketStats) *tcpip.StatCounter payloadSize int - payload func(buffer.View) + payload func([]byte) }{ { "DstUnreachable", @@ -1153,12 +1155,12 @@ func TestICMPChecksumValidationWithPayloadMultipleViews(t *testing.T) { ) } - handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func(buffer.View), checksum bool) { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + size) + handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func([]byte), checksum bool) { + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + size) icmpHdr := header.ICMPv6(hdr.Prepend(size)) icmpHdr.SetType(typ) - payload := buffer.NewView(payloadSize) + payload := make([]byte, payloadSize) payloadFn(payload) if checksum { @@ -1179,8 +1181,9 @@ func TestICMPChecksumValidationWithPayloadMultipleViews(t *testing.T) { SrcAddr: lladdr1, DstAddr: lladdr0, }) + buf := buffer.NewWithData(append(hdr.View(), payload...)) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.NewVectorisedView(header.IPv6MinimumSize+size+payloadSize, []buffer.View{hdr.View(), payload}), + Payload: buf, }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -1389,7 +1392,7 @@ func TestPacketQueing(t *testing.T) { { name: "ICMP Error", rxPkt: func(e *channel.Endpoint) { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -1409,7 +1412,7 @@ func TestPacketQueing(t *testing.T) { DstAddr: host1IPv6Addr.AddressWithPrefix.Address, }) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -1439,7 +1442,7 @@ func TestPacketQueing(t *testing.T) { name: "Ping", rxPkt: func(e *channel.Endpoint) { totalLen := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := tcpipbuffer.NewPrependable(totalLen) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetCode(0) @@ -1458,7 +1461,7 @@ func TestPacketQueing(t *testing.T) { DstAddr: host1IPv6Addr.AddressWithPrefix.Address, }) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv6ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -1542,7 +1545,7 @@ func TestPacketQueing(t *testing.T) { // Send a neighbor advertisement to complete link address resolution. { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + naSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + naSize) pkt := header.ICMPv6(hdr.Prepend(naSize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -1567,7 +1570,7 @@ func TestPacketQueing(t *testing.T) { DstAddr: host1IPv6Addr.AddressWithPrefix.Address, }) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -1599,7 +1602,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Unicast Neighbor Solicitation without source link-layer address option", createPacket: func() header.ICMPv6 { nsSize := header.ICMPv6NeighborSolicitMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(nsSize)) + icmp := header.ICMPv6(make([]byte, nsSize)) icmp.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(icmp.MessageBody()) ns.SetTargetAddress(lladdr0) @@ -1619,7 +1622,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Unicast Neighbor Solicitation with source link-layer address option", createPacket: func() header.ICMPv6 { nsSize := header.ICMPv6NeighborSolicitMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(nsSize)) + icmp := header.ICMPv6(make([]byte, nsSize)) icmp.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(icmp.MessageBody()) ns.SetTargetAddress(lladdr0) @@ -1636,7 +1639,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Multicast Neighbor Solicitation without source link-layer address option", createPacket: func() header.ICMPv6 { nsSize := header.ICMPv6NeighborSolicitMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(nsSize)) + icmp := header.ICMPv6(make([]byte, nsSize)) icmp.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(icmp.MessageBody()) ns.SetTargetAddress(lladdr0) @@ -1652,7 +1655,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Multicast Neighbor Solicitation with source link-layer address option", createPacket: func() header.ICMPv6 { nsSize := header.ICMPv6NeighborSolicitMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(nsSize)) + icmp := header.ICMPv6(make([]byte, nsSize)) icmp.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(icmp.MessageBody()) ns.SetTargetAddress(lladdr0) @@ -1669,7 +1672,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Unicast Neighbor Advertisement without target link-layer address option", createPacket: func() header.ICMPv6 { naSize := header.ICMPv6NeighborAdvertMinimumSize - icmp := header.ICMPv6(buffer.NewView(naSize)) + icmp := header.ICMPv6(make([]byte, naSize)) icmp.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(icmp.MessageBody()) na.SetSolicitedFlag(true) @@ -1690,7 +1693,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Unicast Neighbor Advertisement with target link-layer address option", createPacket: func() header.ICMPv6 { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(naSize)) + icmp := header.ICMPv6(make([]byte, naSize)) icmp.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(icmp.MessageBody()) na.SetSolicitedFlag(true) @@ -1709,7 +1712,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Multicast Neighbor Advertisement without target link-layer address option", createPacket: func() header.ICMPv6 { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(naSize)) + icmp := header.ICMPv6(make([]byte, naSize)) icmp.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(icmp.MessageBody()) na.SetSolicitedFlag(false) @@ -1729,7 +1732,7 @@ func TestCallsToNeighborCache(t *testing.T) { name: "Multicast Neighbor Advertisement with target link-layer address option", createPacket: func() header.ICMPv6 { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - icmp := header.ICMPv6(buffer.NewView(naSize)) + icmp := header.ICMPv6(make([]byte, naSize)) icmp.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(icmp.MessageBody()) na.SetSolicitedFlag(false) diff --git a/pkg/tcpip/network/ipv6/ipv6.go b/pkg/tcpip/network/ipv6/ipv6.go index 2a95c1280..7a3cf35dc 100644 --- a/pkg/tcpip/network/ipv6/ipv6.go +++ b/pkg/tcpip/network/ipv6/ipv6.go @@ -299,7 +299,7 @@ func (e *endpoint) HandleLinkResolutionFailure(pkt *stack.PacketBuffer) { // handleControl expects the entire offending packet to be in the packet // buffer's data field. pkt = stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.NewVectorisedView(pkt.Size(), pkt.Views()), + Payload: pkt.Buffer(), }) defer pkt.DecRef() pkt.NICID = e.nic.ID() @@ -1594,6 +1594,8 @@ func (e *endpoint) processExtensionHeaders(h header.IPv6, pkt *stack.PacketBuffe // 8200 section 4.5. We also use the NextHeader value from the first // fragment. data := pkt.Data() + // TODO(b/230896518): Create a pkg/buffer once MakeIPv6PayloadIterator + // API has been changed. dataVV := buffer.NewVectorisedView(data.Size(), data.Views()) it = header.MakeIPv6PayloadIterator(header.IPv6ExtensionHeaderIdentifier(proto), dataVV) } diff --git a/pkg/tcpip/network/ipv6/ipv6_test.go b/pkg/tcpip/network/ipv6/ipv6_test.go index 208bdef14..e0c33ba3a 100644 --- a/pkg/tcpip/network/ipv6/ipv6_test.go +++ b/pkg/tcpip/network/ipv6/ipv6_test.go @@ -25,8 +25,9 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/link/channel" @@ -64,7 +65,7 @@ func testReceiveICMP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst t.Helper() // Receive ICMP packet. - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.ICMPv6NeighborAdvertMinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.ICMPv6NeighborAdvertMinimumSize) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6NeighborAdvertMinimumSize)) pkt.SetType(header.ICMPv6NeighborAdvert) pkt.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ @@ -83,7 +84,7 @@ func testReceiveICMP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst }) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -117,7 +118,7 @@ func testReceiveUDP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst } // Receive UDP Packet. - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -143,7 +144,7 @@ func testReceiveUDP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst }) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -160,14 +161,14 @@ func compareFragments(packets []*stack.PacketBuffer, sourcePacket *stack.PacketB // from the first fragment. source := header.IPv6(packets[0].NetworkHeader().View()) sourceIPHeadersLen := len(source) - vv := buffer.NewVectorisedView(sourcePacket.Size(), sourcePacket.Views()) - source = append(source, vv.ToView()...) + buf := sourcePacket.Buffer() + source = append(source, buf.Flatten()...) - var reassembledPayload buffer.VectorisedView + var reassembledPayload buffer.Buffer for i, fragment := range packets { // Confirm that the packet is valid. - allBytes := buffer.NewVectorisedView(fragment.Size(), fragment.Views()) - fragmentIPHeaders := header.IPv6(allBytes.ToView()) + allBytes := fragment.Buffer() + fragmentIPHeaders := header.IPv6(allBytes.Flatten()) if !fragmentIPHeaders.IsValid(len(fragmentIPHeaders)) { return fmt.Errorf("fragment #%d: IP packet is invalid:\n%s", i, hex.Dump(fragmentIPHeaders)) } @@ -222,11 +223,11 @@ func compareFragments(packets []*stack.PacketBuffer, sourcePacket *stack.PacketB // Store the reassembled payload as we parse each fragment. The payload // includes the Transport header and everything after. - reassembledPayload.AppendView(fragment.TransportHeader().View()) - reassembledPayload.AppendView(fragment.Data().AsRange().ToOwnedView()) + reassembledPayload.Append(fragment.TransportHeader().View()) + reassembledPayload.Append(fragment.Data().AsRange().ToOwnedView()) } - if diff := cmp.Diff(buffer.View(source[sourceIPHeadersLen:]), reassembledPayload.ToView()); diff != "" { + if diff := cmp.Diff([]byte(source[sourceIPHeadersLen:]), reassembledPayload.Flatten()); diff != "" { return fmt.Errorf("reassembledPayload mismatch (-want +got):\n%s", diff) } @@ -950,7 +951,7 @@ func TestReceiveIPv6ExtHdrs(t *testing.T) { udpLength := header.UDPMinimumSize + len(udpPayload) extHdrBytes, ipv6NextHdr := test.extHdr(uint8(header.UDPProtocolNumber)) extHdrLen := len(extHdrBytes) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + extHdrLen + udpLength) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + extHdrLen + udpLength) // Serialize UDP message. u := header.UDP(hdr.Prepend(udpLength)) @@ -1001,7 +1002,7 @@ func TestReceiveIPv6ExtHdrs(t *testing.T) { } pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -1032,9 +1033,9 @@ func TestReceiveIPv6ExtHdrs(t *testing.T) { // Pack the output packet into a single buffer.View as the checkers // assume that. - vv := buffer.NewVectorisedView(p.Size(), p.Views()) + buf := p.Buffer() p.DecRef() - pkt := vv.ToView() + pkt := buf.Flatten() if got, want := len(pkt), header.IPv6FixedHeaderSize+header.ICMPv6MinimumSize+hdr.UsedLength(); got != want { t.Fatalf("got an ICMP packet of size = %d, want = %d", got, want) } @@ -1053,7 +1054,7 @@ func TestReceiveIPv6ExtHdrs(t *testing.T) { if got, want := icm.TypeSpecific(), test.pointer; got != want { t.Errorf("unexpected ICMPv6 pointer, got = %d, want = %d\n", got, want) } - if diff := cmp.Diff(hdr.View(), buffer.View(originalPacket)); diff != "" { + if diff := cmp.Diff([]byte(hdr.View()), originalPacket); diff != "" { t.Errorf("ICMPv6 payload mismatch (-want +got):\n%s", diff) } return @@ -1092,7 +1093,7 @@ type fragmentData struct { srcAddr tcpip.Address dstAddr tcpip.Address nextHdr uint8 - data buffer.VectorisedView + data buffer.Buffer } func TestReceiveIPv6Fragments(t *testing.T) { @@ -1110,7 +1111,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { routingExtHdrLen = 8 ) - udpGen := func(payload []byte, multiplier uint8, src, dst tcpip.Address) buffer.View { + udpGen := func(payload []byte, multiplier uint8, src, dst tcpip.Address) []byte { payloadLen := len(payload) for i := 0; i < payloadLen; i++ { payload[i] = uint8(i) * multiplier @@ -1118,7 +1119,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { udpLength := header.UDPMinimumSize + payloadLen - hdr := buffer.NewPrependable(udpLength) + hdr := tcpipbuffer.NewPrependable(udpLength) u := header.UDP(hdr.Prepend(udpLength)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -1165,7 +1166,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: uint8(header.UDPProtocolNumber), - data: ipv6Payload1Addr1ToAddr2.ToVectorisedView(), + data: buffer.NewWithData(ipv6Payload1Addr1ToAddr2), }, }, expectedPayloads: [][]byte{udpPayload1Addr1ToAddr2}, @@ -1177,14 +1178,12 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2), - []buffer.View{ - // Fragment extension header. + data: buffer.NewWithData( + // Fragment extension header. + append( []byte{uint8(header.UDPProtocolNumber), 0, 0, 0, 0, 0, 0, 0}, - - ipv6Payload1Addr1ToAddr2, - }, + ipv6Payload1Addr1ToAddr2..., + ), ), }, }, @@ -1197,14 +1196,12 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload3Addr1ToAddr2), - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. []byte{uint8(header.UDPProtocolNumber), 0, 0, 0, 0, 0, 0, 0}, - - ipv6Payload3Addr1ToAddr2, - }, + ipv6Payload3Addr1ToAddr2..., + ), ), }, }, @@ -1217,32 +1214,28 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1255,32 +1248,28 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, }, @@ -1293,34 +1282,30 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 // NextHeader value is different than the one in the first fragment, so // this NextHeader should be ignored. []byte{uint8(header.IPv6NoNextHeaderIdentifier), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1333,32 +1318,28 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload3Addr1ToAddr2[:64], - }, + ipv6Payload3Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload3Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload3Addr1ToAddr2[64:], - }, + ipv6Payload3Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1371,32 +1352,28 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+63, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload3Addr1ToAddr2[:63], - }, + ipv6Payload3Addr1ToAddr2[:63]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload3Addr1ToAddr2)-63, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload3Addr1ToAddr2[63:], - }, + ipv6Payload3Addr1ToAddr2[63:]..., + ), ), }, }, @@ -1409,32 +1386,28 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 2 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 2}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1447,25 +1420,22 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+udpMaximumSizeMinus15, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload4Addr1ToAddr2[:udpMaximumSizeMinus15], - }, + ipv6Payload4Addr1ToAddr2[:udpMaximumSizeMinus15]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload4Addr1ToAddr2)-udpMaximumSizeMinus15, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = udpMaximumSizeMinus15/8, More = false, ID = 1 @@ -1473,9 +1443,8 @@ func TestReceiveIPv6Fragments(t *testing.T) { udpMaximumSizeMinus15 >> 8, udpMaximumSizeMinus15 & 0xff, 0, 0, 0, 1}, - - ipv6Payload4Addr1ToAddr2[udpMaximumSizeMinus15:], - }, + ipv6Payload4Addr1ToAddr2[udpMaximumSizeMinus15:]..., + ), ), }, }, @@ -1488,25 +1457,22 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+udpMaximumSizeMinus15, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload4Addr1ToAddr2[:udpMaximumSizeMinus15], - }, + ipv6Payload4Addr1ToAddr2[:udpMaximumSizeMinus15]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload4Addr1ToAddr2)-udpMaximumSizeMinus15, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = udpMaximumSizeMinus15/8, More = true, ID = 1 @@ -1515,8 +1481,8 @@ func TestReceiveIPv6Fragments(t *testing.T) { (udpMaximumSizeMinus15 & 0xff) + 1, 0, 0, 0, 1}, - ipv6Payload4Addr1ToAddr2[udpMaximumSizeMinus15:], - }, + ipv6Payload4Addr1ToAddr2[udpMaximumSizeMinus15:]..., + ), ), }, }, @@ -1529,42 +1495,40 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: routingExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Routing extension header. // // Segments left = 0. []byte{fragmentExtHdrID, 0, 1, 0, 2, 3, 4, 5}, - - // Fragment extension header. - // - // Fragment offset = 0, More = true, ID = 1 - []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + append( + // Fragment extension header. + // + // Fragment offset = 0, More = true, ID = 1 + []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, + ipv6Payload1Addr1ToAddr2[:64]..., + )..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: routingExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Routing extension header. // // Segments left = 0. []byte{fragmentExtHdrID, 0, 1, 0, 2, 3, 4, 5}, - - // Fragment extension header. - // - // Fragment offset = 8, More = false, ID = 1 - []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + append( + // Fragment extension header. + // + // Fragment offset = 8, More = false, ID = 1 + []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, + ipv6Payload1Addr1ToAddr2[64:]..., + )..., + ), ), }, }, @@ -1577,42 +1541,41 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: routingExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Routing extension header. // // Segments left = 1. []byte{fragmentExtHdrID, 0, 1, 1, 2, 3, 4, 5}, - - // Fragment extension header. - // - // Fragment offset = 0, More = true, ID = 1 - []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + append( + // Fragment extension header. + // + // Fragment offset = 0, More = true, ID = 1 + []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, + ipv6Payload1Addr1ToAddr2[:64]..., + )..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: routingExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Routing extension header. // // Segments left = 1. []byte{fragmentExtHdrID, 0, 1, 1, 2, 3, 4, 5}, - // Fragment extension header. - // - // Fragment offset = 9, More = false, ID = 1 - []byte{uint8(header.UDPProtocolNumber), 0, 0, 72, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + append( + // Fragment extension header. + // + // Fragment offset = 9, More = false, ID = 1 + []byte{uint8(header.UDPProtocolNumber), 0, 0, 72, 0, 0, 0, 1}, + ipv6Payload1Addr1ToAddr2[64:]..., + )..., + ), ), }, }, @@ -1625,37 +1588,35 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{routingExtHdrID, 0, 0, 1, 0, 0, 0, 1}, - - // Routing extension header. - // - // Segments left = 0. - []byte{uint8(header.UDPProtocolNumber), 0, 1, 0, 2, 3, 4, 5}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + append( + // Routing extension header. + // + // Segments left = 0. + []byte{uint8(header.UDPProtocolNumber), 0, 1, 0, 2, 3, 4, 5}, + ipv6Payload1Addr1ToAddr2[:64]..., + )..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 9, More = false, ID = 1 []byte{routingExtHdrID, 0, 0, 72, 0, 0, 0, 1}, - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1668,37 +1629,34 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - routingExtHdrLen+fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{routingExtHdrID, 0, 0, 1, 0, 0, 0, 1}, - - // Routing extension header. - // - // Segments left = 1. - []byte{uint8(header.UDPProtocolNumber), 0, 1, 1, 2, 3, 4, 5}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + append( + // Routing extension header. + // + // Segments left = 1. + []byte{uint8(header.UDPProtocolNumber), 0, 1, 1, 2, 3, 4, 5}, + ipv6Payload1Addr1ToAddr2[:64]..., + )..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 9, More = false, ID = 1 []byte{routingExtHdrID, 0, 0, 72, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1711,44 +1669,33 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - // The length of this payload is fragmentExtHdrLen+8 because the - // first 8 bytes of the 16 byte routing extension header is in - // this fragment. - fragmentExtHdrLen+8, - []buffer.View{ - // Fragment extension header. - // + data: buffer.NewWithData( + append( // Fragment offset = 0, More = true, ID = 1 []byte{routingExtHdrID, 0, 0, 1, 0, 0, 0, 1}, - // Routing extension header (part 1) // // Segments left = 0. - []byte{uint8(header.UDPProtocolNumber), 1, 1, 0, 2, 3, 4, 5}, - }, + []byte{uint8(header.UDPProtocolNumber), 1, 1, 0, 2, 3, 4, 5}..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - // The length of this payload is - // fragmentExtHdrLen+8+len(ipv6Payload1Addr1ToAddr2) because the last 8 bytes of - // the 16 byte routing extension header is in this fagment. - fragmentExtHdrLen+8+len(ipv6Payload1Addr1ToAddr2), - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 1, More = false, ID = 1 []byte{routingExtHdrID, 0, 0, 8, 0, 0, 0, 1}, - - // Routing extension header (part 2) - []byte{6, 7, 8, 9, 10, 11, 12, 13}, - - ipv6Payload1Addr1ToAddr2, - }, + append( + // Routing extension header (part 2) + []byte{6, 7, 8, 9, 10, 11, 12, 13}, + ipv6Payload1Addr1ToAddr2..., + )..., + ), ), }, }, @@ -1761,12 +1708,8 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - // The length of this payload is fragmentExtHdrLen+8 because the - // first 8 bytes of the 16 byte routing extension header is in - // this fragment. - fragmentExtHdrLen+8, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 @@ -1775,30 +1718,26 @@ func TestReceiveIPv6Fragments(t *testing.T) { // Routing extension header (part 1) // // Segments left = 1. - []byte{uint8(header.UDPProtocolNumber), 1, 1, 1, 2, 3, 4, 5}, - }, + []byte{uint8(header.UDPProtocolNumber), 1, 1, 1, 2, 3, 4, 5}..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - // The length of this payload is - // fragmentExtHdrLen+8+len(ipv6Payload1Addr1ToAddr2) because the last 8 bytes of - // the 16 byte routing extension header is in this fagment. - fragmentExtHdrLen+8+len(ipv6Payload1Addr1ToAddr2), - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 1, More = false, ID = 1 []byte{routingExtHdrID, 0, 0, 8, 0, 0, 0, 1}, - - // Routing extension header (part 2) - []byte{6, 7, 8, 9, 10, 11, 12, 13}, - - ipv6Payload1Addr1ToAddr2, - }, + append( + // Routing extension header (part 2) + []byte{6, 7, 8, 9, 10, 11, 12, 13}, + ipv6Payload1Addr1ToAddr2..., + )..., + ), ), }, }, @@ -1813,16 +1752,14 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, // This fragment has the same ID as the other fragments but is an atomic @@ -1831,32 +1768,29 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload2Addr1ToAddr2), - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 0, 0, 0, 0, 1}, - - ipv6Payload2Addr1ToAddr2, - }, + ipv6Payload2Addr1ToAddr2..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, }, @@ -1869,64 +1803,56 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+32, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 2 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 2}, - - ipv6Payload2Addr1ToAddr2[:32], - }, + ipv6Payload2Addr1ToAddr2[:32]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload2Addr1ToAddr2)-32, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 4, More = false, ID = 2 []byte{uint8(header.UDPProtocolNumber), 0, 0, 32, 0, 0, 0, 2}, - - ipv6Payload2Addr1ToAddr2[32:], - }, + ipv6Payload2Addr1ToAddr2[32:]..., + ), ), }, }, @@ -1939,64 +1865,57 @@ func TestReceiveIPv6Fragments(t *testing.T) { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - ipv6Payload1Addr1ToAddr2[:64], - }, + ipv6Payload1Addr1ToAddr2[:64]..., + ), ), }, { srcAddr: addr3, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+32, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 0, More = true, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 1, 0, 0, 0, 1}, - - ipv6Payload1Addr3ToAddr2[:32], - }, + ipv6Payload1Addr3ToAddr2[:32]..., + ), ), }, { srcAddr: addr1, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-64, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 8, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 64, 0, 0, 0, 1}, - - ipv6Payload1Addr1ToAddr2[64:], - }, + ipv6Payload1Addr1ToAddr2[64:]..., + ), ), }, { srcAddr: addr3, dstAddr: addr2, nextHdr: fragmentExtHdrID, - data: buffer.NewVectorisedView( - fragmentExtHdrLen+len(ipv6Payload1Addr1ToAddr2)-32, - []buffer.View{ + data: buffer.NewWithData( + append( // Fragment extension header. // // Fragment offset = 4, More = false, ID = 1 []byte{uint8(header.UDPProtocolNumber), 0, 0, 32, 0, 0, 0, 1}, - - ipv6Payload1Addr3ToAddr2[32:], - }, + ipv6Payload1Addr3ToAddr2[32:]..., + ), ), }, }, @@ -2040,7 +1959,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { } for _, f := range test.fragments { - hdr := buffer.NewPrependable(header.IPv6MinimumSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize) // Serialize IPv6 fixed header. ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize)) @@ -2054,10 +1973,10 @@ func TestReceiveIPv6Fragments(t *testing.T) { DstAddr: f.dstAddr, }) - vv := hdr.View().ToVectorisedView() - vv.Append(f.data) + buf := buffer.NewWithData(hdr.View()) + buf.Append(f.data.Flatten()) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) e.InjectInbound(ProtocolNumber, pkt) pkt.DecRef() @@ -2190,20 +2109,20 @@ func TestInvalidIPv6Fragments(t *testing.T) { NIC: nicID, }}) - var expectICMPPayload buffer.View + var expectICMPPayload []byte for _, f := range test.fragments { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize)) encodeArgs := f.ipv6Fields encodeArgs.ExtensionHeaders = append(encodeArgs.ExtensionHeaders, &f.ipv6FragmentFields) ip.Encode(&encodeArgs) - vv := hdr.View().ToVectorisedView() - vv.AppendView(f.payload) + buf := buffer.NewWithData(hdr.View()) + buf.Append(f.payload) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) if test.expectICMP { @@ -2235,7 +2154,7 @@ func TestInvalidIPv6Fragments(t *testing.T) { checker.IPv6(t, stack.PayloadSince(reply.NetworkHeader()), checker.SrcAddr(addr2), checker.DstAddr(addr1), - checker.IPFullLength(uint16(header.IPv6MinimumSize+header.ICMPv6MinimumSize+expectICMPPayload.Size())), + checker.IPFullLength(uint16(header.IPv6MinimumSize+header.ICMPv6MinimumSize+len(expectICMPPayload))), checker.ICMPv6( checker.ICMPv6Type(test.expectICMPType), checker.ICMPv6Code(test.expectICMPCode), @@ -2445,9 +2364,9 @@ func TestFragmentReassemblyTimeout(t *testing.T) { NIC: nicID, }}) - var firstFragmentSent buffer.View + var firstFragmentSent []byte for _, f := range test.fragments { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize)) encodeArgs := f.ipv6Fields @@ -2456,11 +2375,11 @@ func TestFragmentReassemblyTimeout(t *testing.T) { fragHDR := header.IPv6Fragment(hdr.View()[header.IPv6MinimumSize:]) - vv := hdr.View().ToVectorisedView() - vv.AppendView(f.payload) + buf := buffer.NewWithData(hdr.View()) + buf.Append(f.payload) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) if firstFragmentSent == nil && fragHDR.FragmentOffset() == 0 { @@ -2490,7 +2409,7 @@ func TestFragmentReassemblyTimeout(t *testing.T) { checker.IPv6(t, stack.PayloadSince(reply.NetworkHeader()), checker.SrcAddr(addr2), checker.DstAddr(addr1), - checker.IPFullLength(uint16(header.IPv6MinimumSize+header.ICMPv6MinimumSize+firstFragmentSent.Size())), + checker.IPFullLength(uint16(header.IPv6MinimumSize+header.ICMPv6MinimumSize+len(firstFragmentSent))), checker.ICMPv6( checker.ICMPv6Type(header.ICMPv6TimeExceeded), checker.ICMPv6Code(header.ICMPv6ReassemblyTimeout), @@ -2619,7 +2538,7 @@ func TestWriteStats(t *testing.T) { for i := 0; i < nPackets; i++ { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: header.UDPMinimumSize + int(rt.MaxHeaderLength()), - Data: buffer.NewView(0).ToVectorisedView(), + Payload: buffer.Buffer{}, }) defer pkt.DecRef() pkt.TransportHeader().Push(header.UDPMinimumSize) @@ -3255,7 +3174,7 @@ func TestForwarding(t *testing.T) { icmpHeaderLength := header.ICMPv6MinimumSize payloadLength := icmpHeaderLength + test.payloadLength + extHdrLen totalLength := ipHeaderLength + payloadLength - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) hdr.Prepend(test.payloadLength) icmpH := header.ICMPv6(hdr.Prepend(icmpHeaderLength)) @@ -3279,7 +3198,7 @@ func TestForwarding(t *testing.T) { DstAddr: test.dstAddr, }) request := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) incomingEndpoint, ok := endpoints[incomingNICID] @@ -3578,7 +3497,7 @@ func TestMulticastForwarding(t *testing.T) { icmpHeaderLength := header.ICMPv6MinimumSize payloadLength := icmpHeaderLength + test.payloadLength + extHdrLen totalLength := ipHeaderLength + payloadLength - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) hdr.Prepend(test.payloadLength) icmpH := header.ICMPv6(hdr.Prepend(icmpHeaderLength)) @@ -3766,14 +3685,14 @@ func TestIcmpRateLimit(t *testing.T) { }) tests := []struct { name string - createPacket func() buffer.View + createPacket func() []byte check func(*testing.T, *channel.Endpoint, int) }{ { name: "echo", - createPacket: func() buffer.View { + createPacket: func() []byte { totalLength := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) icmpH := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) icmpH.SetIdent(1) icmpH.SetSequence(1) @@ -3815,9 +3734,9 @@ func TestIcmpRateLimit(t *testing.T) { }, { name: "dst unreachable", - createPacket: func() buffer.View { + createPacket: func() []byte { totalLength := header.IPv6MinimumSize + header.UDPMinimumSize - hdr := buffer.NewPrependable(totalLength) + hdr := tcpipbuffer.NewPrependable(totalLength) udpH := header.UDP(hdr.Prepend(header.UDPMinimumSize)) udpH.Encode(&header.UDPFields{ SrcPort: 100, @@ -3867,7 +3786,7 @@ func TestIcmpRateLimit(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { for round := 0; round < icmpBurst+1; round++ { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: testCase.createPacket().ToVectorisedView(), + Payload: buffer.NewWithData(testCase.createPacket()), }) e.InjectInbound(header.IPv6ProtocolNumber, pkt) pkt.DecRef() diff --git a/pkg/tcpip/network/ipv6/mld.go b/pkg/tcpip/network/ipv6/mld.go index 7238053c8..4c8d66043 100644 --- a/pkg/tcpip/network/ipv6/mld.go +++ b/pkg/tcpip/network/ipv6/mld.go @@ -18,8 +18,8 @@ import ( "fmt" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/network/internal/ip" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -196,7 +196,7 @@ func (mld *mldState) writePacket(destAddress, groupAddress tcpip.Address, mldTyp panic(fmt.Sprintf("unrecognized mld type = %d", mldType)) } - icmp := header.ICMPv6(buffer.NewView(header.ICMPv6HeaderSize + header.MLDMinimumSize)) + icmp := header.ICMPv6(make([]byte, header.ICMPv6HeaderSize+header.MLDMinimumSize)) icmp.SetType(mldType) header.MLD(icmp.MessageBody()).SetMulticastAddress(groupAddress) // As per RFC 2710 section 3, @@ -268,7 +268,7 @@ func (mld *mldState) writePacket(destAddress, groupAddress tcpip.Address, mldTyp pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(mld.ep.MaxHeaderLength()) + extensionHeaders.Length(), - Data: buffer.View(icmp).ToVectorisedView(), + Payload: buffer.NewWithData(icmp), }) defer pkt.DecRef() diff --git a/pkg/tcpip/network/ipv6/mld_test.go b/pkg/tcpip/network/ipv6/mld_test.go index 9baa63f82..79a5d7fb7 100644 --- a/pkg/tcpip/network/ipv6/mld_test.go +++ b/pkg/tcpip/network/ipv6/mld_test.go @@ -21,10 +21,10 @@ import ( "testing" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refs" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -43,7 +43,7 @@ var ( globalAddrSNMC = header.SolicitedNodeAddr(globalAddr) ) -func validateMLDPacket(t *testing.T, p buffer.View, localAddress, remoteAddress tcpip.Address, mldType header.ICMPv6Type, groupAddress tcpip.Address) { +func validateMLDPacket(t *testing.T, p []byte, localAddress, remoteAddress tcpip.Address, mldType header.ICMPv6Type, groupAddress tcpip.Address) { t.Helper() checker.IPv6WithExtHdr(t, p, @@ -376,7 +376,7 @@ func createAndInjectMLDPacket(e *channel.Endpoint, mldType header.ICMPv6Type, ho extensionHeadersLength := extensionHeaders.Length() payloadLength := extensionHeadersLength + header.ICMPv6HeaderSize + header.MLDMinimumSize - buf := buffer.NewView(header.IPv6MinimumSize + payloadLength) + buf := make([]byte, header.IPv6MinimumSize+payloadLength) ip := header.IPv6(buf) ip.Encode(&header.IPv6Fields{ @@ -400,7 +400,7 @@ func createAndInjectMLDPacket(e *channel.Endpoint, mldType header.ICMPv6Type, ho })) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buf.ToVectorisedView(), + Payload: buffer.NewWithData(buf), }) e.InjectInbound(ipv6.ProtocolNumber, pkt) pkt.DecRef() diff --git a/pkg/tcpip/network/ipv6/ndp.go b/pkg/tcpip/network/ipv6/ndp.go index 9d79f76e5..09cf3672a 100644 --- a/pkg/tcpip/network/ipv6/ndp.go +++ b/pkg/tcpip/network/ipv6/ndp.go @@ -18,9 +18,9 @@ import ( "fmt" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/network/internal/ip" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -1793,7 +1793,7 @@ func (ndp *ndpState) startSolicitingRouters() { } } payloadSize := header.ICMPv6HeaderSize + header.NDPRSMinimumSize + optsSerializer.Length() - icmpData := header.ICMPv6(buffer.NewView(payloadSize)) + icmpData := header.ICMPv6(make([]byte, payloadSize)) icmpData.SetType(header.ICMPv6RouterSolicit) rs := header.NDPRouterSolicit(icmpData.MessageBody()) rs.Options().Serialize(optsSerializer) @@ -1805,7 +1805,7 @@ func (ndp *ndpState) startSolicitingRouters() { pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(ndp.ep.MaxHeaderLength()), - Data: buffer.View(icmpData).ToVectorisedView(), + Payload: buffer.NewWithData(icmpData), }) defer pkt.DecRef() @@ -1910,7 +1910,7 @@ func (ndp *ndpState) SendDADMessage(addr tcpip.Address, nonce []byte) tcpip.Erro } func (e *endpoint) sendNDPNS(srcAddr, dstAddr, targetAddr tcpip.Address, remoteLinkAddr tcpip.LinkAddress, opts header.NDPOptionsSerializer) tcpip.Error { - icmp := header.ICMPv6(buffer.NewView(header.ICMPv6NeighborSolicitMinimumSize + opts.Length())) + icmp := header.ICMPv6(make([]byte, header.ICMPv6NeighborSolicitMinimumSize+opts.Length())) icmp.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(icmp.MessageBody()) ns.SetTargetAddress(targetAddr) @@ -1923,7 +1923,7 @@ func (e *endpoint) sendNDPNS(srcAddr, dstAddr, targetAddr tcpip.Address, remoteL pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: int(e.MaxHeaderLength()), - Data: buffer.View(icmp).ToVectorisedView(), + Payload: buffer.NewWithData(icmp), }) defer pkt.DecRef() diff --git a/pkg/tcpip/network/ipv6/ndp_test.go b/pkg/tcpip/network/ipv6/ndp_test.go index cdd1939a8..c344a3d2b 100644 --- a/pkg/tcpip/network/ipv6/ndp_test.go +++ b/pkg/tcpip/network/ipv6/ndp_test.go @@ -22,8 +22,9 @@ import ( "time" "github.com/google/go-cmp/cmp" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -156,7 +157,7 @@ func TestNeighborSolicitationWithSourceLinkLayerOption(t *testing.T) { } ndpNSSize := header.ICMPv6NeighborSolicitMinimumSize + len(test.optsBuf) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) pkt := header.ICMPv6(hdr.Prepend(ndpNSSize)) pkt.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(pkt.MessageBody()) @@ -186,7 +187,7 @@ func TestNeighborSolicitationWithSourceLinkLayerOption(t *testing.T) { } pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -418,7 +419,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { }) ndpNSSize := header.ICMPv6NeighborSolicitMinimumSize + test.nsOpts.Length() - hdr := buffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) pkt := header.ICMPv6(hdr.Prepend(ndpNSSize)) pkt.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(pkt.MessageBody()) @@ -448,7 +449,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { } pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -502,7 +503,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { header.NDPTargetLinkLayerAddressOption(linkAddr1), } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize + ser.Length() - hdr := buffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -525,7 +526,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { DstAddr: nicAddr, }) pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -620,7 +621,7 @@ func TestNeighborAdvertisementWithTargetLinkLayerOption(t *testing.T) { } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize + len(test.optsBuf) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) ns := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -649,7 +650,7 @@ func TestNeighborAdvertisementWithTargetLinkLayerOption(t *testing.T) { t.Fatalf("got invalid = %d, want = 0", got) } pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -692,14 +693,14 @@ func TestNeighborAdvertisementWithTargetLinkLayerOption(t *testing.T) { func TestNDPValidation(t *testing.T) { const nicID = 1 - handleIPv6Payload := func(payload buffer.View, hopLimit uint8, atomicFragment bool, ep stack.NetworkEndpoint) { + handleIPv6Payload := func(payload []byte, hopLimit uint8, atomicFragment bool, ep stack.NetworkEndpoint) { var extHdrs header.IPv6ExtHdrSerializer if atomicFragment { extHdrs = append(extHdrs, &header.IPv6SerializableFragmentExtHdr{}) } extHdrsLen := extHdrs.Length() - ip := buffer.NewView(header.IPv6MinimumSize + extHdrsLen) + ip := make([]byte, header.IPv6MinimumSize+extHdrsLen) header.IPv6(ip).Encode(&header.IPv6Fields{ PayloadLength: uint16(len(payload) + extHdrsLen), TransportProtocol: header.ICMPv6ProtocolNumber, @@ -708,10 +709,10 @@ func TestNDPValidation(t *testing.T) { DstAddr: lladdr0, ExtensionHeaders: extHdrs, }) - vv := ip.ToVectorisedView() - vv.AppendView(payload) + buf := buffer.NewWithData(ip) + buf.Append(payload) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: vv, + Payload: buf, }) ep.HandlePacket(pkt) pkt.DecRef() @@ -869,7 +870,7 @@ func TestNDPValidation(t *testing.T) { routerOnly := stats.RouterOnlyPacketsDroppedByHost typStat := typ.statCounter(stats) - icmpH := header.ICMPv6(buffer.NewView(typ.size + len(typ.extraData))) + icmpH := header.ICMPv6(make([]byte, typ.size+len(typ.extraData))) copy(icmpH[typ.size:], typ.extraData) icmpH.SetType(typ.typ) icmpH.SetCode(test.code) @@ -900,7 +901,7 @@ func TestNDPValidation(t *testing.T) { t.FailNow() } - handleIPv6Payload(buffer.View(icmpH), test.hopLimit, test.atomicFragment, ep) + handleIPv6Payload(icmpH, test.hopLimit, test.atomicFragment, ep) // Rx count of the NDP packet should have increased. if got := typStat.Value(); got != 1 { @@ -992,7 +993,7 @@ func TestNeighborAdvertisementValidation(t *testing.T) { } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -1025,7 +1026,7 @@ func TestNeighborAdvertisementValidation(t *testing.T) { } pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv6ProtocolNumber, pktBuf) pktBuf.DecRef() @@ -1194,7 +1195,7 @@ func TestRouterAdvertValidation(t *testing.T) { } icmpSize := header.ICMPv6HeaderSize + len(test.ndpPayload) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + icmpSize) + hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + icmpSize) pkt := header.ICMPv6(hdr.Prepend(icmpSize)) pkt.SetType(header.ICMPv6RouterAdvert) pkt.SetCode(test.code) @@ -1226,7 +1227,7 @@ func TestRouterAdvertValidation(t *testing.T) { } pktBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) e.InjectInbound(header.IPv6ProtocolNumber, pktBuf) pktBuf.DecRef() diff --git a/pkg/tcpip/network/multicast_group_test.go b/pkg/tcpip/network/multicast_group_test.go index 263c2bb77..a4d7b1f6c 100644 --- a/pkg/tcpip/network/multicast_group_test.go +++ b/pkg/tcpip/network/multicast_group_test.go @@ -20,9 +20,10 @@ import ( "testing" "time" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/checker" "gvisor.dev/gvisor/pkg/tcpip/faketime" "gvisor.dev/gvisor/pkg/tcpip/header" @@ -243,7 +244,7 @@ func createAndInjectIGMPPacket(e *channel.Endpoint, igmpType byte, maxRespTime b options := header.IPv4OptionsSerializer{ &header.IPv4SerializableRouterAlertOption{}, } - buf := buffer.NewView(header.IPv4MinimumSize + int(options.Length()) + header.IGMPQueryMinimumSize) + buf := make([]byte, header.IPv4MinimumSize+int(options.Length())+header.IGMPQueryMinimumSize) ip := header.IPv4(buf) ip.Encode(&header.IPv4Fields{ TotalLength: uint16(len(buf)), @@ -262,7 +263,7 @@ func createAndInjectIGMPPacket(e *channel.Endpoint, igmpType byte, maxRespTime b igmp.SetChecksum(header.IGMPCalculateChecksum(igmp)) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buf.ToVectorisedView(), + Payload: buffer.NewWithData(buf), }) e.InjectInbound(ipv4.ProtocolNumber, pkt) pkt.DecRef() @@ -279,7 +280,7 @@ func createAndInjectMLDPacket(e *channel.Endpoint, mldType uint8, maxRespDelay b extensionHeadersLength := extensionHeaders.Length() payloadLength := extensionHeadersLength + header.ICMPv6HeaderSize + header.MLDMinimumSize - buf := buffer.NewView(header.IPv6MinimumSize + payloadLength) + buf := make([]byte, header.IPv6MinimumSize+payloadLength) ip := header.IPv6(buf) ip.Encode(&header.IPv6Fields{ @@ -303,7 +304,7 @@ func createAndInjectMLDPacket(e *channel.Endpoint, mldType uint8, maxRespDelay b })) pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buf.ToVectorisedView(), + Payload: buffer.NewWithData(buf), }) e.InjectInbound(ipv6.ProtocolNumber, pkt) pkt.DecRef() @@ -1078,7 +1079,7 @@ func TestMGPWithNICLifecycle(t *testing.T) { ipv6HeaderIter := header.MakeIPv6PayloadIterator( header.IPv6ExtensionHeaderIdentifier(ipv6.NextHeader()), - buffer.View(ipv6.Payload()).ToVectorisedView(), + tcpipbuffer.View(ipv6.Payload()).ToVectorisedView(), ) var transport header.IPv6RawPayloadHeader diff --git a/pkg/tcpip/stack/BUILD b/pkg/tcpip/stack/BUILD index 0af5bca0c..19aefd085 100644 --- a/pkg/tcpip/stack/BUILD +++ b/pkg/tcpip/stack/BUILD @@ -146,6 +146,7 @@ go_test( library = ":stack", deps = [ "//pkg/atomicbitops", + "//pkg/buffer", "//pkg/sync", "//pkg/tcpip", "//pkg/tcpip/buffer", diff --git a/pkg/tcpip/stack/packet_buffer.go b/pkg/tcpip/stack/packet_buffer.go index ccf65bc57..ae56f90b4 100644 --- a/pkg/tcpip/stack/packet_buffer.go +++ b/pkg/tcpip/stack/packet_buffer.go @@ -614,6 +614,20 @@ func (d PacketData) ReadFromVV(srcVV *tcpipbuffer.VectorisedView, count int) int return done } +// ReadFromBuffer moves at most count bytes from the beginning of src to the end +// of d and returns the number of bytes moved. +func (d PacketData) ReadFromBuffer(src *buffer.Buffer, count int) int { + toRead := int64(count) + if toRead > src.Size() { + toRead = src.Size() + } + clone := src.Clone() + clone.Truncate(toRead) + d.pk.buf.Merge(&clone) + src.TrimFront(toRead) + return int(toRead) +} + // AppendRange appends and takes ownership of the data in r. func (d PacketData) AppendRange(r Range) { r.iterate(func(b []byte) { @@ -749,7 +763,7 @@ func (r Range) AsView() tcpipbuffer.View { } // ToOwnedView returns a owned copy of data in r. -func (r Range) ToOwnedView() tcpipbuffer.View { +func (r Range) ToOwnedView() []byte { if r.length == 0 { return nil } diff --git a/pkg/tcpip/stack/packet_buffer_test.go b/pkg/tcpip/stack/packet_buffer_test.go index 935d07fc1..a142bf96b 100644 --- a/pkg/tcpip/stack/packet_buffer_test.go +++ b/pkg/tcpip/stack/packet_buffer_test.go @@ -18,7 +18,8 @@ import ( "fmt" "testing" - "gvisor.dev/gvisor/pkg/tcpip/buffer" + "gvisor.dev/gvisor/pkg/buffer" + tcpipbuffer "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" ) @@ -77,7 +78,7 @@ func TestPacketHeaderPush(t *testing.T) { ReserveHeaderBytes: test.reserved, // Make a copy of data to make sure our truth data won't be taint by // PacketBuffer. - Data: buffer.NewViewFromBytes(test.data).ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(test.data).ToVectorisedView(), }) allHdrSize := len(test.link) + len(test.network) + len(test.transport) @@ -85,7 +86,7 @@ func TestPacketHeaderPush(t *testing.T) { // Check the initial values for packet. checkInitialPacketBuffer(t, pk, PacketBufferOptions{ ReserveHeaderBytes: test.reserved, - Data: buffer.View(test.data).ToVectorisedView(), + Data: tcpipbuffer.View(test.data).ToVectorisedView(), }) // Push headers. @@ -149,12 +150,12 @@ func TestPacketHeaderConsume(t *testing.T) { pk := NewPacketBuffer(PacketBufferOptions{ // Make a copy of data to make sure our truth data won't be taint by // PacketBuffer. - Data: buffer.NewViewFromBytes(test.data).ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(test.data).ToVectorisedView(), }) // Check the initial values for packet. checkInitialPacketBuffer(t, pk, PacketBufferOptions{ - Data: buffer.View(test.data).ToVectorisedView(), + Data: tcpipbuffer.View(test.data).ToVectorisedView(), }) // Consume headers. @@ -206,7 +207,7 @@ func TestPacketHeaderConsumeDataTooShort(t *testing.T) { pk := NewPacketBuffer(PacketBufferOptions{ // Make a copy of data to make sure our truth data won't be taint by // PacketBuffer. - Data: buffer.NewViewFromBytes(data).ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(data).ToVectorisedView(), }) // Consume should fail if pkt.Data is too short. @@ -222,7 +223,7 @@ func TestPacketHeaderConsumeDataTooShort(t *testing.T) { // Check packet should look the same as initial packet. checkInitialPacketBuffer(t, pk, PacketBufferOptions{ - Data: buffer.View(data).ToVectorisedView(), + Data: tcpipbuffer.View(data).ToVectorisedView(), }) } @@ -239,7 +240,7 @@ func TestPacketHeaderPushConsumeMixed(t *testing.T) { initData = append(initData, data...) pk := NewPacketBuffer(PacketBufferOptions{ ReserveHeaderBytes: len(link), - Data: buffer.NewViewFromBytes(initData).ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(initData).ToVectorisedView(), }) // 1. Consume network header @@ -267,7 +268,7 @@ func TestPacketHeaderPushConsumeMixedTooLong(t *testing.T) { initData := concatViews(network, data) pk := NewPacketBuffer(PacketBufferOptions{ ReserveHeaderBytes: len(link), - Data: buffer.NewViewFromBytes(initData).ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(initData).ToVectorisedView(), }) // 1. Push link header @@ -494,7 +495,7 @@ func TestPacketBufferData(t *testing.T) { s := "APPEND" pkt := tc.makePkt(t) - pkt.Data().AppendView(buffer.View(s)) + pkt.Data().AppendView(tcpipbuffer.View(s)) checkData(t, pkt, []byte(tc.data+s)) }) @@ -582,15 +583,32 @@ func TestPacketBufferData(t *testing.T) { }) } }) + + // ReadFromBuffer + for _, n := range []int{0, 1, 2, 7, 10, 14, 20} { + t.Run(fmt.Sprintf("ReadFromBuffer%d", n), func(t *testing.T) { + s := "TO READ" + s += s + srcBuf := buffer.NewWithData([]byte(s)) + + pkt := tc.makePkt(t) + pkt.Data().ReadFromBuffer(&srcBuf, n) + + if n < len(s) { + s = s[:n] + } + checkData(t, pkt, []byte(tc.data+s)) + }) + } }) } } type packetContents struct { - link buffer.View - network buffer.View - transport buffer.View - data buffer.View + link tcpipbuffer.View + network tcpipbuffer.View + transport tcpipbuffer.View + data tcpipbuffer.View } func checkPacketContents(t *testing.T, prefix string, pk *PacketBuffer, want packetContents) { @@ -643,7 +661,7 @@ func checkPacketHeader(t *testing.T, name string, h PacketHeader, want []byte) { checkViewEqual(t, name+".View()", h.View(), want) } -func checkViewEqual(t *testing.T, what string, got, want buffer.View) { +func checkViewEqual(t *testing.T, what string, got, want tcpipbuffer.View) { t.Helper() if !bytes.Equal(got, want) { t.Errorf("%s = %x, want %x", what, got, want) @@ -703,24 +721,24 @@ func checkRange(t *testing.T, r Range, data []byte) { } } -func vv(pieces ...string) buffer.VectorisedView { - var views []buffer.View +func vv(pieces ...string) tcpipbuffer.VectorisedView { + var views []tcpipbuffer.View var size int for _, p := range pieces { - v := buffer.View([]byte(p)) + v := tcpipbuffer.View([]byte(p)) size += len(v) views = append(views, v) } - return buffer.NewVectorisedView(size, views) + return tcpipbuffer.NewVectorisedView(size, views) } -func makeView(size int) buffer.View { +func makeView(size int) tcpipbuffer.View { b := byte(size) return bytes.Repeat([]byte{b}, size) } -func concatViews(views ...buffer.View) buffer.View { - var all buffer.View +func concatViews(views ...tcpipbuffer.View) tcpipbuffer.View { + var all tcpipbuffer.View for _, v := range views { all = append(all, v...) } diff --git a/pkg/tcpip/stack/stack_test.go b/pkg/tcpip/stack/stack_test.go index 9a1cac181..bf7acbffa 100644 --- a/pkg/tcpip/stack/stack_test.go +++ b/pkg/tcpip/stack/stack_test.go @@ -5038,7 +5038,7 @@ func TestWritePacketToRemote(t *testing.T) { if pkt.EgressRoute.RemoteLinkAddress != linkAddr2 { t.Fatalf("pkt.EgressRoute.RemoteAddress = %s, want %s", pkt.EgressRoute.RemoteLinkAddress, linkAddr2) } - if diff := cmp.Diff(pkt.Data().AsRange().ToOwnedView(), buffer.View(test.payload)); diff != "" { + if diff := cmp.Diff(pkt.Data().AsRange().ToOwnedView(), test.payload); diff != "" { t.Errorf("pkt.Data mismatch (-want +got):\n%s", diff) } })