github.com/google/netstack@v0.0.0-20191123085552-55fcc16cd0eb/tcpip/packet_buffer.go (about) 1 // Copyright 2019 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package tcpip 15 16 import "github.com/google/netstack/tcpip/buffer" 17 18 // A PacketBuffer contains all the data of a network packet. 19 // 20 // As a PacketBuffer traverses up the stack, it may be necessary to pass it to 21 // multiple endpoints. Clone() should be called in such cases so that 22 // modifications to the Data field do not affect other copies. 23 // 24 // +stateify savable 25 type PacketBuffer struct { 26 // Data holds the payload of the packet. For inbound packets, it also 27 // holds the headers, which are consumed as the packet moves up the 28 // stack. Headers are guaranteed not to be split across views. 29 // 30 // The bytes backing Data are immutable, but Data itself may be trimmed 31 // or otherwise modified. 32 Data buffer.VectorisedView 33 34 // Header holds the headers of outbound packets. As a packet is passed 35 // down the stack, each layer adds to Header. 36 Header buffer.Prependable 37 38 // These fields are used by both inbound and outbound packets. They 39 // typically overlap with the Data and Header fields. 40 // 41 // The bytes backing these views are immutable. Each field may be nil 42 // if either it has not been set yet or no such header exists (e.g. 43 // packets sent via loopback may not have a link header). 44 // 45 // These fields may be Views into other slices (either Data or Header). 46 // SR dosen't support this, so deep copies are necessary in some cases. 47 LinkHeader buffer.View 48 NetworkHeader buffer.View 49 TransportHeader buffer.View 50 } 51 52 // Clone makes a copy of pk. It clones the Data field, which creates a new 53 // VectorisedView but does not deep copy the underlying bytes. 54 // 55 // Clone also does not deep copy any of its other fields. 56 func (pk PacketBuffer) Clone() PacketBuffer { 57 pk.Data = pk.Data.Clone(nil) 58 return pk 59 }