diff --git a/pkg/tcpip/buffer/BUILD b/pkg/tcpip/buffer/BUILD index 23aa0ad05..ce4062a81 100644 --- a/pkg/tcpip/buffer/BUILD +++ b/pkg/tcpip/buffer/BUILD @@ -5,7 +5,6 @@ package(licenses = ["notice"]) go_library( name = "buffer", srcs = [ - "prependable.go", "view.go", "view_unsafe.go", ], diff --git a/pkg/tcpip/header/BUILD b/pkg/tcpip/header/BUILD index 5b7c07aa7..1b2985459 100644 --- a/pkg/tcpip/header/BUILD +++ b/pkg/tcpip/header/BUILD @@ -53,6 +53,7 @@ go_test( "//pkg/rand", "//pkg/tcpip", "//pkg/tcpip/buffer", + "//pkg/tcpip/prependable", "//pkg/tcpip/testutil", "@com_github_google_go_cmp//cmp:go_default_library", ], diff --git a/pkg/tcpip/header/ipv4_test.go b/pkg/tcpip/header/ipv4_test.go index c02fe898b..178ec978f 100644 --- a/pkg/tcpip/header/ipv4_test.go +++ b/pkg/tcpip/header/ipv4_test.go @@ -19,8 +19,8 @@ import ( "github.com/google/go-cmp/cmp" "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" "gvisor.dev/gvisor/pkg/tcpip/header" + "gvisor.dev/gvisor/pkg/tcpip/prependable" ) func TestIPv4OptionsSerializer(t *testing.T) { @@ -150,7 +150,7 @@ func TestIPv4EncodeOptions(t *testing.T) { t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize) } totalLen := uint16(ipHeaderLength) - hdr := buffer.NewPrependable(int(totalLen)) + hdr := prependable.New(int(totalLen)) ip := header.IPv4(hdr.Prepend(ipHeaderLength)) // To check the padding works, poison the last byte of the options space. if paddedOptionLength != serializeOpts.Length() { diff --git a/pkg/tcpip/network/BUILD b/pkg/tcpip/network/BUILD index 2c5732c9c..6efb9ca66 100644 --- a/pkg/tcpip/network/BUILD +++ b/pkg/tcpip/network/BUILD @@ -24,6 +24,7 @@ go_test( "//pkg/tcpip/link/loopback", "//pkg/tcpip/network/ipv4", "//pkg/tcpip/network/ipv6", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", "//pkg/tcpip/transport/icmp", diff --git a/pkg/tcpip/network/ip_test.go b/pkg/tcpip/network/ip_test.go index fff63cf3d..bc137adf9 100644 --- a/pkg/tcpip/network/ip_test.go +++ b/pkg/tcpip/network/ip_test.go @@ -25,13 +25,13 @@ import ( "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - 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" "gvisor.dev/gvisor/pkg/tcpip/link/loopback" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" @@ -372,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 := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) pkt.SetType(header.ICMPv4Echo) pkt.SetCode(0) @@ -397,7 +397,7 @@ func TestSourceAddressValidation(t *testing.T) { rxIPv6ICMP := func(e *channel.Endpoint, src tcpip.Address) { totalLen := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetCode(0) @@ -1373,7 +1373,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { remoteAddr: remoteIPv4Addr, pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv4MinimumSize + len(data) - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1414,7 +1414,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { remoteAddr: remoteIPv4Addr, pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv4MinimumSize + len(data) - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1493,7 +1493,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { ipHdrLen := int(header.IPv4MinimumSize + ipv4Options.Length()) totalLen := ipHdrLen + len(data) - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1578,7 +1578,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { remoteAddr: remoteIPv6Addr, pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv6MinimumSize + len(data) - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1618,7 +1618,7 @@ func TestWriteHeaderIncludedPacket(t *testing.T) { remoteAddr: remoteIPv6Addr, pktGen: func(t *testing.T, src tcpip.Address) buffer.Buffer { totalLen := header.IPv6MinimumSize + len(ipv6FragmentExtHdr) + len(data) - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) if n := copy(hdr.Prepend(len(data)), data); n != len(data) { t.Fatalf("copied %d bytes, expected %d bytes", n, len(data)) } @@ -1801,7 +1801,7 @@ func TestICMPInclusionSize(t *testing.T) { // unknown transport protocol (254). rxIPv4Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) []byte { totalLen := header.IPv4MinimumSize + len(payload) - hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize) + hdr := prependable.New(header.IPv4MinimumSize) ip := header.IPv4(hdr.Prepend(header.IPv4MinimumSize)) ip.Encode(&header.IPv4Fields{ TotalLength: uint16(totalLen), @@ -1830,7 +1830,7 @@ func TestICMPInclusionSize(t *testing.T) { // inclusion of the errant packet. Use `unknown next header' to generate // the error. rxIPv6Bad := func(e *channel.Endpoint, src tcpip.Address, payload []byte) []byte { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize) + hdr := prependable.New(header.IPv6MinimumSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize)) ip.Encode(&header.IPv6Fields{ PayloadLength: uint16(len(payload)), diff --git a/pkg/tcpip/network/ipv4/BUILD b/pkg/tcpip/network/ipv4/BUILD index f4527ddb9..83a1c2a6f 100644 --- a/pkg/tcpip/network/ipv4/BUILD +++ b/pkg/tcpip/network/ipv4/BUILD @@ -41,7 +41,6 @@ go_test( "//pkg/refsvfs2", "//pkg/sync", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/checker", "//pkg/tcpip/faketime", "//pkg/tcpip/header", @@ -50,6 +49,7 @@ go_test( "//pkg/tcpip/network/arp", "//pkg/tcpip/network/internal/testutil", "//pkg/tcpip/network/ipv4", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", "//pkg/tcpip/transport/icmp", diff --git a/pkg/tcpip/network/ipv4/ipv4_test.go b/pkg/tcpip/network/ipv4/ipv4_test.go index db1f46dd6..a476d184e 100644 --- a/pkg/tcpip/network/ipv4/ipv4_test.go +++ b/pkg/tcpip/network/ipv4/ipv4_test.go @@ -30,7 +30,6 @@ import ( "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/tcpip" - 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" @@ -39,6 +38,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/network/arp" iptestutil "gvisor.dev/gvisor/pkg/tcpip/network/internal/testutil" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" @@ -247,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 := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) hdr.Prepend(options.payloadLength) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) icmpH.SetIdent(randomIdent) @@ -286,7 +286,7 @@ func newICMPEchoPacket(t *testing.T, srcAddr, dstAddr tcpip.Address, ttl uint8, } pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) pkt.NetworkProtocolNumber = header.IPv4ProtocolNumber @@ -1740,9 +1740,7 @@ func TestIPv4Sanity(t *testing.T) { t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize) } totalLen := uint16(ipHeaderLength + header.ICMPv4MinimumSize) - // TODO(b/230896518): tcpipbuffer is only needed for Prependable. Move - // Prependable to outside pkg/tcpip/buffer. - hdr := tcpipbuffer.NewPrependable(int(totalLen)) + hdr := prependable.New(int(totalLen)) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) // Specify ident/seq to make sure we get the same in the response. @@ -2489,7 +2487,7 @@ func TestInvalidFragments(t *testing.T) { for _, f := range test.fragments { pktSize := header.IPv4MinimumSize + len(f.payload) - hdr := tcpipbuffer.NewPrependable(pktSize) + hdr := prependable.New(pktSize) ip := header.IPv4(hdr.Prepend(pktSize)) ip.Encode(&f.ipv4fields) @@ -2723,7 +2721,7 @@ func TestFragmentReassemblyTimeout(t *testing.T) { var firstFragmentSent buffer.Buffer for _, f := range test.fragments { pktSize := header.IPv4MinimumSize - hdr := tcpipbuffer.NewPrependable(pktSize) + hdr := prependable.New(pktSize) ip := header.IPv4(hdr.Prepend(pktSize)) ip.Encode(&f.ipv4fields) @@ -2799,7 +2797,7 @@ func TestReceiveFragments(t *testing.T) { udpLength := header.UDPMinimumSize + len(payload) - hdr := tcpipbuffer.NewPrependable(udpLength) + hdr := prependable.New(udpLength) u := header.UDP(hdr.Prepend(udpLength)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -3212,7 +3210,7 @@ func TestReceiveFragments(t *testing.T) { // Prepare and send the fragments. for _, frag := range test.fragments { - hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize) + hdr := prependable.New(header.IPv4MinimumSize) // Serialize IPv4 fixed header. ip := header.IPv4(hdr.Prepend(header.IPv4MinimumSize)) @@ -3511,7 +3509,7 @@ func TestPacketQueuing(t *testing.T) { { name: "ICMP Error", rxPkt: func(e *channel.Endpoint) { - hdr := tcpipbuffer.NewPrependable(header.IPv4MinimumSize + header.UDPMinimumSize) + hdr := prependable.New(header.IPv4MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -3561,7 +3559,7 @@ func TestPacketQueuing(t *testing.T) { name: "Ping", rxPkt: func(e *channel.Endpoint) { totalLen := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) pkt.SetType(header.ICMPv4Echo) pkt.SetCode(0) @@ -3860,7 +3858,7 @@ func TestIcmpRateLimit(t *testing.T) { name: "echo", createPacket: func() []byte { totalLength := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) icmpH := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) icmpH.SetIdent(1) icmpH.SetSequence(1) @@ -3900,7 +3898,7 @@ func TestIcmpRateLimit(t *testing.T) { name: "dst unreachable", createPacket: func() []byte { totalLength := header.IPv4MinimumSize + header.UDPMinimumSize - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) udpH := header.UDP(hdr.Prepend(header.UDPMinimumSize)) udpH.Encode(&header.UDPFields{ SrcPort: 100, diff --git a/pkg/tcpip/network/ipv6/BUILD b/pkg/tcpip/network/ipv6/BUILD index 71cab1d08..1de8698b3 100644 --- a/pkg/tcpip/network/ipv6/BUILD +++ b/pkg/tcpip/network/ipv6/BUILD @@ -44,13 +44,13 @@ go_test( "//pkg/refs", "//pkg/refsvfs2", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/checker", "//pkg/tcpip/faketime", "//pkg/tcpip/header", "//pkg/tcpip/link/channel", "//pkg/tcpip/link/sniffer", "//pkg/tcpip/network/internal/testutil", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", "//pkg/tcpip/transport/icmp", diff --git a/pkg/tcpip/network/ipv6/icmp_test.go b/pkg/tcpip/network/ipv6/icmp_test.go index 58c359d3c..43cad28ce 100644 --- a/pkg/tcpip/network/ipv6/icmp_test.go +++ b/pkg/tcpip/network/ipv6/icmp_test.go @@ -26,12 +26,12 @@ import ( "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/refsvfs2" "gvisor.dev/gvisor/pkg/tcpip" - 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" "gvisor.dev/gvisor/pkg/tcpip/link/channel" "gvisor.dev/gvisor/pkg/tcpip/link/sniffer" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" "gvisor.dev/gvisor/pkg/tcpip/transport/udp" @@ -578,7 +578,7 @@ func TestLinkResolution(t *testing.T) { } defer r.Release() - hdr := tcpipbuffer.NewPrependable(int(r.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6EchoMinimumSize) + hdr := prependable.New(int(r.MaxHeaderLength()) + header.IPv6MinimumSize + header.ICMPv6EchoMinimumSize) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6EchoMinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ @@ -977,7 +977,7 @@ func TestICMPChecksumValidationWithPayload(t *testing.T) { handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func([]byte), checksum bool) { icmpSize := size + payloadSize - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + icmpSize) + hdr := prependable.New(header.IPv6MinimumSize + icmpSize) icmpHdr := header.ICMPv6(hdr.Prepend(icmpSize)) icmpHdr.SetType(typ) payloadFn(icmpHdr.Payload()) @@ -1165,7 +1165,7 @@ func TestICMPChecksumValidationWithPayloadMultipleViews(t *testing.T) { } handleIPv6Payload := func(typ header.ICMPv6Type, size, payloadSize int, payloadFn func([]byte), checksum bool) { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + size) + hdr := prependable.New(header.IPv6MinimumSize + size) icmpHdr := header.ICMPv6(hdr.Prepend(size)) icmpHdr.SetType(typ) @@ -1401,7 +1401,7 @@ func TestPacketQueing(t *testing.T) { { name: "ICMP Error", rxPkt: func(e *channel.Endpoint) { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) + hdr := prependable.New(header.IPv6MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -1451,7 +1451,7 @@ func TestPacketQueing(t *testing.T) { name: "Ping", rxPkt: func(e *channel.Endpoint) { totalLen := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := tcpipbuffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) pkt.SetType(header.ICMPv6EchoRequest) pkt.SetCode(0) @@ -1554,7 +1554,7 @@ func TestPacketQueing(t *testing.T) { // Send a neighbor advertisement to complete link address resolution. { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + naSize) + hdr := prependable.New(header.IPv6MinimumSize + naSize) pkt := header.ICMPv6(hdr.Prepend(naSize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) diff --git a/pkg/tcpip/network/ipv6/ipv6_test.go b/pkg/tcpip/network/ipv6/ipv6_test.go index e0c33ba3a..17900cff6 100644 --- a/pkg/tcpip/network/ipv6/ipv6_test.go +++ b/pkg/tcpip/network/ipv6/ipv6_test.go @@ -27,11 +27,11 @@ import ( "github.com/google/go-cmp/cmp" "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - 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" iptestutil "gvisor.dev/gvisor/pkg/tcpip/network/internal/testutil" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" @@ -65,7 +65,7 @@ func testReceiveICMP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst t.Helper() // Receive ICMP packet. - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.ICMPv6NeighborAdvertMinimumSize) + hdr := prependable.New(header.IPv6MinimumSize + header.ICMPv6NeighborAdvertMinimumSize) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6NeighborAdvertMinimumSize)) pkt.SetType(header.ICMPv6NeighborAdvert) pkt.SetChecksum(header.ICMPv6Checksum(header.ICMPv6ChecksumParams{ @@ -118,7 +118,7 @@ func testReceiveUDP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst } // Receive UDP Packet. - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.UDPMinimumSize) + hdr := prependable.New(header.IPv6MinimumSize + header.UDPMinimumSize) u := header.UDP(hdr.Prepend(header.UDPMinimumSize)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -951,7 +951,7 @@ func TestReceiveIPv6ExtHdrs(t *testing.T) { udpLength := header.UDPMinimumSize + len(udpPayload) extHdrBytes, ipv6NextHdr := test.extHdr(uint8(header.UDPProtocolNumber)) extHdrLen := len(extHdrBytes) - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + extHdrLen + udpLength) + hdr := prependable.New(header.IPv6MinimumSize + extHdrLen + udpLength) // Serialize UDP message. u := header.UDP(hdr.Prepend(udpLength)) @@ -1119,7 +1119,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { udpLength := header.UDPMinimumSize + payloadLen - hdr := tcpipbuffer.NewPrependable(udpLength) + hdr := prependable.New(udpLength) u := header.UDP(hdr.Prepend(udpLength)) u.Encode(&header.UDPFields{ SrcPort: 5555, @@ -1959,7 +1959,7 @@ func TestReceiveIPv6Fragments(t *testing.T) { } for _, f := range test.fragments { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize) + hdr := prependable.New(header.IPv6MinimumSize) // Serialize IPv6 fixed header. ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize)) @@ -2111,7 +2111,7 @@ func TestInvalidIPv6Fragments(t *testing.T) { var expectICMPPayload []byte for _, f := range test.fragments { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) + hdr := prependable.New(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize)) encodeArgs := f.ipv6Fields @@ -2366,7 +2366,7 @@ func TestFragmentReassemblyTimeout(t *testing.T) { var firstFragmentSent []byte for _, f := range test.fragments { - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) + hdr := prependable.New(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize) ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize + header.IPv6FragmentHeaderSize)) encodeArgs := f.ipv6Fields @@ -3174,7 +3174,7 @@ func TestForwarding(t *testing.T) { icmpHeaderLength := header.ICMPv6MinimumSize payloadLength := icmpHeaderLength + test.payloadLength + extHdrLen totalLength := ipHeaderLength + payloadLength - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) hdr.Prepend(test.payloadLength) icmpH := header.ICMPv6(hdr.Prepend(icmpHeaderLength)) @@ -3497,7 +3497,7 @@ func TestMulticastForwarding(t *testing.T) { icmpHeaderLength := header.ICMPv6MinimumSize payloadLength := icmpHeaderLength + test.payloadLength + extHdrLen totalLength := ipHeaderLength + payloadLength - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) hdr.Prepend(test.payloadLength) icmpH := header.ICMPv6(hdr.Prepend(icmpHeaderLength)) @@ -3521,7 +3521,7 @@ func TestMulticastForwarding(t *testing.T) { DstAddr: dstAddr, }) request := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), }) incomingEndpoint, ok := endpoints[incomingNICID] @@ -3692,7 +3692,7 @@ func TestIcmpRateLimit(t *testing.T) { name: "echo", createPacket: func() []byte { totalLength := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) icmpH := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) icmpH.SetIdent(1) icmpH.SetSequence(1) @@ -3736,7 +3736,7 @@ func TestIcmpRateLimit(t *testing.T) { name: "dst unreachable", createPacket: func() []byte { totalLength := header.IPv6MinimumSize + header.UDPMinimumSize - hdr := tcpipbuffer.NewPrependable(totalLength) + hdr := prependable.New(totalLength) udpH := header.UDP(hdr.Prepend(header.UDPMinimumSize)) udpH.Encode(&header.UDPFields{ SrcPort: 100, diff --git a/pkg/tcpip/network/ipv6/ndp_test.go b/pkg/tcpip/network/ipv6/ndp_test.go index c344a3d2b..93ad732ed 100644 --- a/pkg/tcpip/network/ipv6/ndp_test.go +++ b/pkg/tcpip/network/ipv6/ndp_test.go @@ -24,11 +24,11 @@ import ( "github.com/google/go-cmp/cmp" "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - 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" "gvisor.dev/gvisor/pkg/tcpip/link/channel" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" ) @@ -157,7 +157,7 @@ func TestNeighborSolicitationWithSourceLinkLayerOption(t *testing.T) { } ndpNSSize := header.ICMPv6NeighborSolicitMinimumSize + len(test.optsBuf) - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) + hdr := prependable.New(header.IPv6MinimumSize + ndpNSSize) pkt := header.ICMPv6(hdr.Prepend(ndpNSSize)) pkt.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(pkt.MessageBody()) @@ -419,7 +419,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { }) ndpNSSize := header.ICMPv6NeighborSolicitMinimumSize + test.nsOpts.Length() - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNSSize) + hdr := prependable.New(header.IPv6MinimumSize + ndpNSSize) pkt := header.ICMPv6(hdr.Prepend(ndpNSSize)) pkt.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(pkt.MessageBody()) @@ -503,7 +503,7 @@ func TestNeighborSolicitationResponse(t *testing.T) { header.NDPTargetLinkLayerAddressOption(linkAddr1), } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize + ser.Length() - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := prependable.New(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -621,7 +621,7 @@ func TestNeighborAdvertisementWithTargetLinkLayerOption(t *testing.T) { } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize + len(test.optsBuf) - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := prependable.New(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) ns := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -993,7 +993,7 @@ func TestNeighborAdvertisementValidation(t *testing.T) { } ndpNASize := header.ICMPv6NeighborAdvertMinimumSize - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + ndpNASize) + hdr := prependable.New(header.IPv6MinimumSize + ndpNASize) pkt := header.ICMPv6(hdr.Prepend(ndpNASize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -1195,7 +1195,7 @@ func TestRouterAdvertValidation(t *testing.T) { } icmpSize := header.ICMPv6HeaderSize + len(test.ndpPayload) - hdr := tcpipbuffer.NewPrependable(header.IPv6MinimumSize + icmpSize) + hdr := prependable.New(header.IPv6MinimumSize + icmpSize) pkt := header.ICMPv6(hdr.Prepend(icmpSize)) pkt.SetType(header.ICMPv6RouterAdvert) pkt.SetCode(test.code) diff --git a/pkg/tcpip/prependable/BUILD b/pkg/tcpip/prependable/BUILD new file mode 100644 index 000000000..9bcb9368e --- /dev/null +++ b/pkg/tcpip/prependable/BUILD @@ -0,0 +1,9 @@ +load("//tools:defs.bzl", "go_library") + +package(licenses = ["notice"]) + +go_library( + name = "prependable", + srcs = ["prependable.go"], + visibility = ["//visibility:public"], +) diff --git a/pkg/tcpip/buffer/prependable.go b/pkg/tcpip/prependable/prependable.go similarity index 74% rename from pkg/tcpip/buffer/prependable.go rename to pkg/tcpip/prependable/prependable.go index ba21f4eca..d6b40c921 100644 --- a/pkg/tcpip/buffer/prependable.go +++ b/pkg/tcpip/prependable/prependable.go @@ -1,4 +1,4 @@ -// Copyright 2018 The gVisor Authors. +// Copyright 2022 The gVisor Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -package buffer +// Package prependable defines a buffer that grows backwards. +package prependable // Prependable is a buffer that grows backwards, that is, more data can be // prepended to it. It is useful when building networking packets, where each @@ -21,34 +22,34 @@ package buffer // then IP would prepend its own, then ethernet. type Prependable struct { // Buf is the buffer backing the prependable buffer. - buf View + buf []byte // usedIdx is the index where the used part of the buffer begins. usedIdx int } -// NewPrependable allocates a new prependable buffer with the given size. -func NewPrependable(size int) Prependable { - return Prependable{buf: NewView(size), usedIdx: size} +// New allocates a new prependable buffer with the given size. +func New(size int) Prependable { + return Prependable{buf: make([]byte, size), usedIdx: size} } -// NewPrependableFromView creates an entirely-used Prependable from a View. +// NewFromSlice creates an entirely-used Prependable from a slice. // -// NewPrependableFromView takes ownership of v. Note that since the entire +// NewFromSlice takes ownership of v. Note that since the entire // prependable is used, further attempts to call Prepend will note that size > // p.usedIdx and return nil. -func NewPrependableFromView(v View) Prependable { +func NewFromSlice(v []byte) Prependable { return Prependable{buf: v, usedIdx: 0} } -// NewEmptyPrependableFromView creates a new prependable buffer from a View. -func NewEmptyPrependableFromView(v View) Prependable { +// NewEmptyFromSlice creates a new prependable buffer from a slice. +func NewEmptyFromSlice(v []byte) Prependable { return Prependable{buf: v, usedIdx: len(v)} } -// View returns a View of the backing buffer that contains all prepended +// View returns a slice of the backing buffer that contains all prepended // data so far. -func (p Prependable) View() View { +func (p Prependable) View() []byte { return p.buf[p.usedIdx:] } @@ -80,6 +81,6 @@ func (p *Prependable) Prepend(size int) []byte { // DeepCopy copies p and the bytes backing it. func (p Prependable) DeepCopy() Prependable { - p.buf = append(View(nil), p.buf...) + p.buf = append([]byte{}, p.buf...) return p } diff --git a/pkg/tcpip/stack/BUILD b/pkg/tcpip/stack/BUILD index 19aefd085..1519155bb 100644 --- a/pkg/tcpip/stack/BUILD +++ b/pkg/tcpip/stack/BUILD @@ -122,6 +122,7 @@ go_test( "//pkg/tcpip/network/ipv4", "//pkg/tcpip/network/ipv6", "//pkg/tcpip/ports", + "//pkg/tcpip/prependable", "//pkg/tcpip/testutil", "//pkg/tcpip/transport/icmp", "//pkg/tcpip/transport/udp", diff --git a/pkg/tcpip/stack/ndp_test.go b/pkg/tcpip/stack/ndp_test.go index ee86baa75..7feb49f8e 100644 --- a/pkg/tcpip/stack/ndp_test.go +++ b/pkg/tcpip/stack/ndp_test.go @@ -25,13 +25,14 @@ import ( "github.com/google/go-cmp/cmp" cryptorand "gvisor.dev/gvisor/pkg/rand" "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" "gvisor.dev/gvisor/pkg/tcpip/link/channel" "gvisor.dev/gvisor/pkg/tcpip/link/loopback" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" @@ -650,7 +651,7 @@ func TestDADResolve(t *testing.T) { } func rxNDPSolicit(e *channel.Endpoint, tgt tcpip.Address) { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.ICMPv6NeighborSolicitMinimumSize) + hdr := prependable.New(header.IPv6MinimumSize + header.ICMPv6NeighborSolicitMinimumSize) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6NeighborSolicitMinimumSize)) pkt.SetType(header.ICMPv6NeighborSolicit) ns := header.NDPNeighborSolicit(pkt.MessageBody()) @@ -670,7 +671,7 @@ func rxNDPSolicit(e *channel.Endpoint, tgt tcpip.Address) { SrcAddr: header.IPv6Any, DstAddr: snmc, }) - e.InjectInbound(header.IPv6ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{Data: hdr.View().ToVectorisedView()})) + e.InjectInbound(header.IPv6ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{Data: tcpipbuffer.NewViewFromBytes(hdr.View()).ToVectorisedView()})) } // TestDADFail tests to make sure that the DAD process fails if another node is @@ -698,7 +699,7 @@ func TestDADFail(t *testing.T) { name: "RxAdvert", rxPkt: func(e *channel.Endpoint, tgt tcpip.Address) { naSize := header.ICMPv6NeighborAdvertMinimumSize + header.NDPLinkLayerAddressSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + naSize) + hdr := prependable.New(header.IPv6MinimumSize + naSize) pkt := header.ICMPv6(hdr.Prepend(naSize)) pkt.SetType(header.ICMPv6NeighborAdvert) na := header.NDPNeighborAdvert(pkt.MessageBody()) @@ -722,7 +723,7 @@ func TestDADFail(t *testing.T) { SrcAddr: tgt, DstAddr: header.IPv6AllNodesMulticastAddress, }) - e.InjectInbound(header.IPv6ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{Data: hdr.View().ToVectorisedView()})) + e.InjectInbound(header.IPv6ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{Data: tcpipbuffer.NewViewFromBytes(hdr.View()).ToVectorisedView()})) }, getStat: func(s tcpip.ICMPv6ReceivedPacketStats) *tcpip.StatCounter { return s.NeighborAdvert @@ -1071,7 +1072,7 @@ func raBuf(ip tcpip.Address, rl uint16, managedAddress, otherConfigurations bool const routerLifetimeOffset = 2 icmpSize := header.ICMPv6HeaderSize + header.NDPRAMinimumSize + optSer.Length() - hdr := buffer.NewPrependable(header.IPv6MinimumSize + icmpSize) + hdr := prependable.New(header.IPv6MinimumSize + icmpSize) pkt := header.ICMPv6(hdr.Prepend(icmpSize)) pkt.SetType(header.ICMPv6RouterAdvert) pkt.SetCode(0) @@ -1109,7 +1110,7 @@ func raBuf(ip tcpip.Address, rl uint16, managedAddress, otherConfigurations bool }) return stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Data: tcpipbuffer.NewViewFromBytes(hdr.View()).ToVectorisedView(), }) } diff --git a/pkg/tcpip/tests/integration/BUILD b/pkg/tcpip/tests/integration/BUILD index 05c1a531d..f7f8c6086 100644 --- a/pkg/tcpip/tests/integration/BUILD +++ b/pkg/tcpip/tests/integration/BUILD @@ -38,6 +38,7 @@ go_test( "//pkg/tcpip/network/arp", "//pkg/tcpip/network/ipv4", "//pkg/tcpip/network/ipv6", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/tests/utils", "//pkg/tcpip/testutil", @@ -105,14 +106,15 @@ go_test( size = "small", srcs = ["multicast_broadcast_test.go"], deps = [ + "//pkg/buffer", "//pkg/tcpip", - "//pkg/tcpip/buffer", "//pkg/tcpip/checker", "//pkg/tcpip/header", "//pkg/tcpip/link/channel", "//pkg/tcpip/link/loopback", "//pkg/tcpip/network/ipv4", "//pkg/tcpip/network/ipv6", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/tests/utils", "//pkg/tcpip/testutil", diff --git a/pkg/tcpip/tests/integration/iptables_test.go b/pkg/tcpip/tests/integration/iptables_test.go index 50ace42b1..c384a78f9 100644 --- a/pkg/tcpip/tests/integration/iptables_test.go +++ b/pkg/tcpip/tests/integration/iptables_test.go @@ -30,6 +30,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/network/arp" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/tests/utils" "gvisor.dev/gvisor/pkg/tcpip/testutil" @@ -107,7 +108,7 @@ func genStackV4(t *testing.T) (*stack.Stack, *channel.Endpoint) { func genPacketV6() *stack.PacketBuffer { pktSize := header.IPv6MinimumSize + payloadSize - hdr := buffer.NewPrependable(pktSize) + hdr := prependable.New(pktSize) ip := header.IPv6(hdr.Prepend(pktSize)) ip.Encode(&header.IPv6Fields{ PayloadLength: payloadSize, @@ -116,13 +117,13 @@ func genPacketV6() *stack.PacketBuffer { SrcAddr: srcAddrV6, DstAddr: dstAddrV6, }) - vv := hdr.View().ToVectorisedView() + vv := buffer.NewViewFromBytes(hdr.View()).ToVectorisedView() return stack.NewPacketBuffer(stack.PacketBufferOptions{Data: vv}) } func genPacketV4() *stack.PacketBuffer { pktSize := header.IPv4MinimumSize + payloadSize - hdr := buffer.NewPrependable(pktSize) + hdr := prependable.New(pktSize) ip := header.IPv4(hdr.Prepend(pktSize)) ip.Encode(&header.IPv4Fields{ TOS: 0, @@ -137,7 +138,7 @@ func genPacketV4() *stack.PacketBuffer { }) ip.SetChecksum(0) ip.SetChecksum(^ip.CalculateChecksum()) - vv := hdr.View().ToVectorisedView() + vv := buffer.NewViewFromBytes(hdr.View()).ToVectorisedView() return stack.NewPacketBuffer(stack.PacketBufferOptions{Data: vv}) } @@ -2031,7 +2032,7 @@ func encodeIPv6Header(v buffer.View, payloadLen int, transProto tcpip.TransportP func udpv4Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSize int) buffer.View { udpSize := header.UDPMinimumSize + dataSize - hdr := buffer.NewPrependable(header.IPv4MinimumSize + udpSize) + hdr := prependable.New(header.IPv4MinimumSize + udpSize) udp := header.UDP(hdr.Prepend(udpSize)) udp.SetSourcePort(srcPort) udp.SetDestinationPort(dstPort) @@ -2054,7 +2055,7 @@ func udpv4Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSi func tcpv4Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSize int) buffer.View { tcpSize := header.TCPMinimumSize + dataSize - hdr := buffer.NewPrependable(header.IPv4MinimumSize + tcpSize) + hdr := prependable.New(header.IPv4MinimumSize + tcpSize) tcp := header.TCP(hdr.Prepend(tcpSize)) tcp.SetSourcePort(srcPort) tcp.SetDestinationPort(dstPort) @@ -2077,7 +2078,7 @@ func tcpv4Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSi } func icmpv4Packet(srcAddr, dstAddr tcpip.Address, icmpType header.ICMPv4Type, ident uint16) buffer.View { - hdr := buffer.NewPrependable(header.IPv4MinimumSize + header.ICMPv4MinimumSize) + hdr := prependable.New(header.IPv4MinimumSize + header.ICMPv4MinimumSize) icmp := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) icmp.SetType(icmpType) icmp.SetIdent(ident) @@ -2095,7 +2096,7 @@ func icmpv4Packet(srcAddr, dstAddr tcpip.Address, icmpType header.ICMPv4Type, id func udpv6Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSize int) buffer.View { udpSize := header.UDPMinimumSize + dataSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + udpSize) + hdr := prependable.New(header.IPv6MinimumSize + udpSize) udp := header.UDP(hdr.Prepend(udpSize)) udp.SetSourcePort(srcPort) udp.SetDestinationPort(dstPort) @@ -2118,7 +2119,7 @@ func udpv6Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSi func tcpv6Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSize int) buffer.View { tcpSize := header.TCPMinimumSize + dataSize - hdr := buffer.NewPrependable(header.IPv6MinimumSize + tcpSize) + hdr := prependable.New(header.IPv6MinimumSize + tcpSize) tcp := header.TCP(hdr.Prepend(tcpSize)) tcp.SetSourcePort(srcPort) tcp.SetDestinationPort(dstPort) @@ -2141,7 +2142,7 @@ func tcpv6Packet(srcAddr, dstAddr tcpip.Address, srcPort, dstPort uint16, dataSi } func icmpv6Packet(srcAddr, dstAddr tcpip.Address, icmpType header.ICMPv6Type, ident uint16) buffer.View { - hdr := buffer.NewPrependable(header.IPv6MinimumSize + header.ICMPv6MinimumSize) + hdr := prependable.New(header.IPv6MinimumSize + header.ICMPv6MinimumSize) icmp := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) icmp.SetType(icmpType) icmp.SetIdent(ident) @@ -2197,7 +2198,7 @@ func TestNATICMPError(t *testing.T) { netProto: ipv4.ProtocolNumber, host1Addr: utils.Host1IPv4Addr.AddressWithPrefix.Address, icmpError: func(t *testing.T, original buffer.View, icmpType uint8) buffer.View { - hdr := buffer.NewPrependable(header.IPv4MinimumSize + header.ICMPv4MinimumSize + len(original)) + hdr := prependable.New(header.IPv4MinimumSize + header.ICMPv4MinimumSize + len(original)) if n := copy(hdr.Prepend(len(original)), original); n != len(original) { t.Fatalf("got copy(...) = %d, want = %d", n, len(original)) } @@ -2301,7 +2302,7 @@ func TestNATICMPError(t *testing.T) { host1Addr: utils.Host1IPv6Addr.AddressWithPrefix.Address, icmpError: func(t *testing.T, original buffer.View, icmpType uint8) buffer.View { payloadLen := header.ICMPv6MinimumSize + len(original) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + payloadLen) + hdr := prependable.New(header.IPv6MinimumSize + payloadLen) icmp := header.ICMPv6(hdr.Prepend(payloadLen)) icmp.SetType(header.ICMPv6Type(icmpType)) if n := copy(icmp.Payload(), original); n != len(original) { diff --git a/pkg/tcpip/tests/integration/multicast_broadcast_test.go b/pkg/tcpip/tests/integration/multicast_broadcast_test.go index e6f80e667..d327ce5dd 100644 --- a/pkg/tcpip/tests/integration/multicast_broadcast_test.go +++ b/pkg/tcpip/tests/integration/multicast_broadcast_test.go @@ -19,14 +19,15 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "gvisor.dev/gvisor/pkg/buffer" "gvisor.dev/gvisor/pkg/tcpip" - "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" "gvisor.dev/gvisor/pkg/tcpip/link/loopback" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/tests/utils" "gvisor.dev/gvisor/pkg/tcpip/testutil" @@ -174,7 +175,7 @@ func TestPingMulticastBroadcast(t *testing.T) { func rxIPv4UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) { payloadLen := header.UDPMinimumSize + len(data) totalLen := header.IPv4MinimumSize + payloadLen - hdr := buffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) u := header.UDP(hdr.Prepend(payloadLen)) u.Encode(&header.UDPFields{ SrcPort: utils.RemotePort, @@ -197,13 +198,13 @@ func rxIPv4UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) { ip.SetChecksum(^ip.CalculateChecksum()) e.InjectInbound(header.IPv4ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), })) } func rxIPv6UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) { payloadLen := header.UDPMinimumSize + len(data) - hdr := buffer.NewPrependable(header.IPv6MinimumSize + payloadLen) + hdr := prependable.New(header.IPv6MinimumSize + payloadLen) u := header.UDP(hdr.Prepend(payloadLen)) u.Encode(&header.UDPFields{ SrcPort: utils.RemotePort, @@ -225,7 +226,7 @@ func rxIPv6UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) { }) e.InjectInbound(header.IPv6ProtocolNumber, stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: hdr.View().ToVectorisedView(), + Payload: buffer.NewWithData(hdr.View()), })) } diff --git a/pkg/tcpip/tests/utils/BUILD b/pkg/tcpip/tests/utils/BUILD index b556aa975..0bdf5c6f2 100644 --- a/pkg/tcpip/tests/utils/BUILD +++ b/pkg/tcpip/tests/utils/BUILD @@ -17,6 +17,7 @@ go_library( "//pkg/tcpip/link/pipe", "//pkg/tcpip/network/ipv4", "//pkg/tcpip/network/ipv6", + "//pkg/tcpip/prependable", "//pkg/tcpip/stack", "//pkg/tcpip/testutil", "//pkg/tcpip/transport/icmp", diff --git a/pkg/tcpip/tests/utils/utils.go b/pkg/tcpip/tests/utils/utils.go index 849c55177..1dffdbe1c 100644 --- a/pkg/tcpip/tests/utils/utils.go +++ b/pkg/tcpip/tests/utils/utils.go @@ -27,6 +27,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/link/pipe" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" + "gvisor.dev/gvisor/pkg/tcpip/prependable" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/testutil" "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" @@ -356,7 +357,7 @@ func SetupRoutedStacks(t *testing.T, host1Stack, routerStack, host2Stack *stack. // ICMPv4Echo returns an ICMPv4 echo packet. func ICMPv4Echo(src, dst tcpip.Address, ttl uint8, ty header.ICMPv4Type) buffer.View { totalLen := header.IPv4MinimumSize + header.ICMPv4MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv4(hdr.Prepend(header.ICMPv4MinimumSize)) pkt.SetType(ty) pkt.SetCode(header.ICMPv4UnusedCode) @@ -397,7 +398,7 @@ func RxICMPv4EchoReply(e *channel.Endpoint, src, dst tcpip.Address, ttl uint8) { // ICMPv6Echo returns an ICMPv6 echo packet. func ICMPv6Echo(src, dst tcpip.Address, ttl uint8, ty header.ICMPv6Type) buffer.View { totalLen := header.IPv6MinimumSize + header.ICMPv6MinimumSize - hdr := buffer.NewPrependable(totalLen) + hdr := prependable.New(totalLen) pkt := header.ICMPv6(hdr.Prepend(header.ICMPv6MinimumSize)) pkt.SetType(ty) pkt.SetCode(header.ICMPv6UnusedCode)