diff options
author | gVisor bot <gvisor-bot@google.com> | 2019-10-21 20:26:03 +0000 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2019-10-21 20:26:03 +0000 |
commit | 975132cced68da837aea09743d45a103be1789c7 (patch) | |
tree | cfb9f8426af5bb021d4d087a2af784d95bd06e89 /pkg/tcpip/transport/raw | |
parent | 17a63aa1580ad13c7f3c441a414d331f3c6be52f (diff) | |
parent | 12235d533ae5c8b4b03c6d7f24cae785a2d6ec22 (diff) |
Merge release-20190806.1-295-g12235d5 (automated)
Diffstat (limited to 'pkg/tcpip/transport/raw')
-rw-r--r-- | pkg/tcpip/transport/raw/endpoint.go | 30 | ||||
-rw-r--r-- | pkg/tcpip/transport/raw/endpoint_state.go | 14 | ||||
-rw-r--r-- | pkg/tcpip/transport/raw/protocol.go | 12 | ||||
-rwxr-xr-x | pkg/tcpip/transport/raw/raw_packet_list.go (renamed from pkg/tcpip/transport/raw/packet_list.go) | 88 | ||||
-rwxr-xr-x | pkg/tcpip/transport/raw/raw_state_autogen.go | 34 |
5 files changed, 92 insertions, 86 deletions
diff --git a/pkg/tcpip/transport/raw/endpoint.go b/pkg/tcpip/transport/raw/endpoint.go index b4c660859..308f10d24 100644 --- a/pkg/tcpip/transport/raw/endpoint.go +++ b/pkg/tcpip/transport/raw/endpoint.go @@ -17,8 +17,7 @@ // // * manually write and inspect transport layer headers and payloads // * receive all traffic of a given transport protocol (e.g. ICMP or UDP) -// * optionally write and inspect network layer and link layer headers for -// packets +// * optionally write and inspect network layer headers of packets // // Raw sockets don't have any notion of ports, and incoming packets are // demultiplexed solely by protocol number. Thus, a raw UDP endpoint will @@ -38,8 +37,8 @@ import ( ) // +stateify savable -type packet struct { - packetEntry +type rawPacket struct { + rawPacketEntry // data holds the actual packet data, including any headers and // payload. data buffer.VectorisedView `state:".(buffer.VectorisedView)"` @@ -72,7 +71,7 @@ type endpoint struct { // The following fields are used to manage the receive queue and are // protected by rcvMu. rcvMu sync.Mutex `state:"nosave"` - rcvList packetList + rcvList rawPacketList rcvBufSizeMax int `state:".(int)"` rcvBufSize int rcvClosed bool @@ -90,7 +89,6 @@ type endpoint struct { } // NewEndpoint returns a raw endpoint for the given protocols. -// TODO(b/129292371): IP_HDRINCL and AF_PACKET. func NewEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return newEndpoint(stack, netProto, transProto, waiterQueue, true /* associated */) } @@ -187,17 +185,17 @@ func (e *endpoint) Read(addr *tcpip.FullAddress) (buffer.View, tcpip.ControlMess return buffer.View{}, tcpip.ControlMessages{}, err } - packet := e.rcvList.Front() - e.rcvList.Remove(packet) - e.rcvBufSize -= packet.data.Size() + pkt := e.rcvList.Front() + e.rcvList.Remove(pkt) + e.rcvBufSize -= pkt.data.Size() e.rcvMu.Unlock() if addr != nil { - *addr = packet.senderAddr + *addr = pkt.senderAddr } - return packet.data.ToView(), tcpip.ControlMessages{HasTimestamp: true, Timestamp: packet.timestampNS}, nil + return pkt.data.ToView(), tcpip.ControlMessages{HasTimestamp: true, Timestamp: pkt.timestampNS}, nil } // Write implements tcpip.Endpoint.Write. @@ -602,7 +600,7 @@ func (e *endpoint) HandlePacket(route *stack.Route, netHeader buffer.View, vv bu wasEmpty := e.rcvBufSize == 0 // Push new packet into receive list and increment the buffer size. - packet := &packet{ + pkt := &rawPacket{ senderAddr: tcpip.FullAddress{ NIC: route.NICID(), Addr: route.RemoteAddress, @@ -611,11 +609,11 @@ func (e *endpoint) HandlePacket(route *stack.Route, netHeader buffer.View, vv bu combinedVV := netHeader.ToVectorisedView() combinedVV.Append(vv) - packet.data = combinedVV.Clone(packet.views[:]) - packet.timestampNS = e.stack.NowNanoseconds() + pkt.data = combinedVV.Clone(pkt.views[:]) + pkt.timestampNS = e.stack.NowNanoseconds() - e.rcvList.PushBack(packet) - e.rcvBufSize += packet.data.Size() + e.rcvList.PushBack(pkt) + e.rcvBufSize += pkt.data.Size() e.rcvMu.Unlock() e.stats.PacketsReceived.Increment() diff --git a/pkg/tcpip/transport/raw/endpoint_state.go b/pkg/tcpip/transport/raw/endpoint_state.go index a6c7cc43a..33bfb56cd 100644 --- a/pkg/tcpip/transport/raw/endpoint_state.go +++ b/pkg/tcpip/transport/raw/endpoint_state.go @@ -20,15 +20,15 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/stack" ) -// saveData saves packet.data field. -func (p *packet) saveData() buffer.VectorisedView { +// saveData saves rawPacket.data field. +func (p *rawPacket) saveData() buffer.VectorisedView { // We cannot save p.data directly as p.data.views may alias to p.views, // which is not allowed by state framework (in-struct pointer). return p.data.Clone(nil) } -// loadData loads packet.data field. -func (p *packet) loadData(data buffer.VectorisedView) { +// loadData loads rawPacket.data field. +func (p *rawPacket) loadData(data buffer.VectorisedView) { // NOTE: We cannot do the p.data = data.Clone(p.views[:]) optimization // here because data.views is not guaranteed to be loaded by now. Plus, // data.views will be allocated anyway so there really is little point @@ -86,7 +86,9 @@ func (ep *endpoint) Resume(s *stack.Stack) { } } - if err := ep.stack.RegisterRawTransportEndpoint(ep.RegisterNICID, ep.NetProto, ep.TransProto, ep); err != nil { - panic(err) + if ep.associated { + if err := ep.stack.RegisterRawTransportEndpoint(ep.RegisterNICID, ep.NetProto, ep.TransProto, ep); err != nil { + panic(err) + } } } diff --git a/pkg/tcpip/transport/raw/protocol.go b/pkg/tcpip/transport/raw/protocol.go index a2512d666..f30aa2a4a 100644 --- a/pkg/tcpip/transport/raw/protocol.go +++ b/pkg/tcpip/transport/raw/protocol.go @@ -17,13 +17,19 @@ package raw import ( "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/stack" + "gvisor.dev/gvisor/pkg/tcpip/transport/packet" "gvisor.dev/gvisor/pkg/waiter" ) -// EndpointFactory implements stack.UnassociatedEndpointFactory. +// EndpointFactory implements stack.RawFactory. type EndpointFactory struct{} -// NewUnassociatedRawEndpoint implements stack.UnassociatedEndpointFactory. -func (EndpointFactory) NewUnassociatedRawEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { +// NewUnassociatedEndpoint implements stack.RawFactory.NewUnassociatedEndpoint. +func (EndpointFactory) NewUnassociatedEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return newEndpoint(stack, netProto, transProto, waiterQueue, false /* associated */) } + +// NewPacketEndpoint implements stack.RawFactory.NewPacketEndpoint. +func (EndpointFactory) NewPacketEndpoint(stack *stack.Stack, cooked bool, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { + return packet.NewEndpoint(stack, cooked, netProto, waiterQueue) +} diff --git a/pkg/tcpip/transport/raw/packet_list.go b/pkg/tcpip/transport/raw/raw_packet_list.go index 2e9074934..12edb4334 100755 --- a/pkg/tcpip/transport/raw/packet_list.go +++ b/pkg/tcpip/transport/raw/raw_packet_list.go @@ -6,14 +6,14 @@ package raw // objects, if they are not the same. An ElementMapper is not typically // required if: Linker is left as is, Element is left as is, or Linker and // Element are the same type. -type packetElementMapper struct{} +type rawPacketElementMapper struct{} // linkerFor maps an Element to a Linker. // // This default implementation should be inlined. // //go:nosplit -func (packetElementMapper) linkerFor(elem *packet) *packet { return elem } +func (rawPacketElementMapper) linkerFor(elem *rawPacket) *rawPacket { return elem } // List is an intrusive list. Entries can be added to or removed from the list // in O(1) time and with no additional memory allocations. @@ -26,39 +26,39 @@ func (packetElementMapper) linkerFor(elem *packet) *packet { return elem } // } // // +stateify savable -type packetList struct { - head *packet - tail *packet +type rawPacketList struct { + head *rawPacket + tail *rawPacket } // Reset resets list l to the empty state. -func (l *packetList) Reset() { +func (l *rawPacketList) Reset() { l.head = nil l.tail = nil } // Empty returns true iff the list is empty. -func (l *packetList) Empty() bool { +func (l *rawPacketList) Empty() bool { return l.head == nil } // Front returns the first element of list l or nil. -func (l *packetList) Front() *packet { +func (l *rawPacketList) Front() *rawPacket { return l.head } // Back returns the last element of list l or nil. -func (l *packetList) Back() *packet { +func (l *rawPacketList) Back() *rawPacket { return l.tail } // PushFront inserts the element e at the front of list l. -func (l *packetList) PushFront(e *packet) { - packetElementMapper{}.linkerFor(e).SetNext(l.head) - packetElementMapper{}.linkerFor(e).SetPrev(nil) +func (l *rawPacketList) PushFront(e *rawPacket) { + rawPacketElementMapper{}.linkerFor(e).SetNext(l.head) + rawPacketElementMapper{}.linkerFor(e).SetPrev(nil) if l.head != nil { - packetElementMapper{}.linkerFor(l.head).SetPrev(e) + rawPacketElementMapper{}.linkerFor(l.head).SetPrev(e) } else { l.tail = e } @@ -67,12 +67,12 @@ func (l *packetList) PushFront(e *packet) { } // PushBack inserts the element e at the back of list l. -func (l *packetList) PushBack(e *packet) { - packetElementMapper{}.linkerFor(e).SetNext(nil) - packetElementMapper{}.linkerFor(e).SetPrev(l.tail) +func (l *rawPacketList) PushBack(e *rawPacket) { + rawPacketElementMapper{}.linkerFor(e).SetNext(nil) + rawPacketElementMapper{}.linkerFor(e).SetPrev(l.tail) if l.tail != nil { - packetElementMapper{}.linkerFor(l.tail).SetNext(e) + rawPacketElementMapper{}.linkerFor(l.tail).SetNext(e) } else { l.head = e } @@ -81,13 +81,13 @@ func (l *packetList) PushBack(e *packet) { } // PushBackList inserts list m at the end of list l, emptying m. -func (l *packetList) PushBackList(m *packetList) { +func (l *rawPacketList) PushBackList(m *rawPacketList) { if l.head == nil { l.head = m.head l.tail = m.tail } else if m.head != nil { - packetElementMapper{}.linkerFor(l.tail).SetNext(m.head) - packetElementMapper{}.linkerFor(m.head).SetPrev(l.tail) + rawPacketElementMapper{}.linkerFor(l.tail).SetNext(m.head) + rawPacketElementMapper{}.linkerFor(m.head).SetPrev(l.tail) l.tail = m.tail } @@ -97,46 +97,46 @@ func (l *packetList) PushBackList(m *packetList) { } // InsertAfter inserts e after b. -func (l *packetList) InsertAfter(b, e *packet) { - a := packetElementMapper{}.linkerFor(b).Next() - packetElementMapper{}.linkerFor(e).SetNext(a) - packetElementMapper{}.linkerFor(e).SetPrev(b) - packetElementMapper{}.linkerFor(b).SetNext(e) +func (l *rawPacketList) InsertAfter(b, e *rawPacket) { + a := rawPacketElementMapper{}.linkerFor(b).Next() + rawPacketElementMapper{}.linkerFor(e).SetNext(a) + rawPacketElementMapper{}.linkerFor(e).SetPrev(b) + rawPacketElementMapper{}.linkerFor(b).SetNext(e) if a != nil { - packetElementMapper{}.linkerFor(a).SetPrev(e) + rawPacketElementMapper{}.linkerFor(a).SetPrev(e) } else { l.tail = e } } // InsertBefore inserts e before a. -func (l *packetList) InsertBefore(a, e *packet) { - b := packetElementMapper{}.linkerFor(a).Prev() - packetElementMapper{}.linkerFor(e).SetNext(a) - packetElementMapper{}.linkerFor(e).SetPrev(b) - packetElementMapper{}.linkerFor(a).SetPrev(e) +func (l *rawPacketList) InsertBefore(a, e *rawPacket) { + b := rawPacketElementMapper{}.linkerFor(a).Prev() + rawPacketElementMapper{}.linkerFor(e).SetNext(a) + rawPacketElementMapper{}.linkerFor(e).SetPrev(b) + rawPacketElementMapper{}.linkerFor(a).SetPrev(e) if b != nil { - packetElementMapper{}.linkerFor(b).SetNext(e) + rawPacketElementMapper{}.linkerFor(b).SetNext(e) } else { l.head = e } } // Remove removes e from l. -func (l *packetList) Remove(e *packet) { - prev := packetElementMapper{}.linkerFor(e).Prev() - next := packetElementMapper{}.linkerFor(e).Next() +func (l *rawPacketList) Remove(e *rawPacket) { + prev := rawPacketElementMapper{}.linkerFor(e).Prev() + next := rawPacketElementMapper{}.linkerFor(e).Next() if prev != nil { - packetElementMapper{}.linkerFor(prev).SetNext(next) + rawPacketElementMapper{}.linkerFor(prev).SetNext(next) } else { l.head = next } if next != nil { - packetElementMapper{}.linkerFor(next).SetPrev(prev) + rawPacketElementMapper{}.linkerFor(next).SetPrev(prev) } else { l.tail = prev } @@ -147,27 +147,27 @@ func (l *packetList) Remove(e *packet) { // methods needed by List. // // +stateify savable -type packetEntry struct { - next *packet - prev *packet +type rawPacketEntry struct { + next *rawPacket + prev *rawPacket } // Next returns the entry that follows e in the list. -func (e *packetEntry) Next() *packet { +func (e *rawPacketEntry) Next() *rawPacket { return e.next } // Prev returns the entry that precedes e in the list. -func (e *packetEntry) Prev() *packet { +func (e *rawPacketEntry) Prev() *rawPacket { return e.prev } // SetNext assigns 'entry' as the entry that follows e in the list. -func (e *packetEntry) SetNext(elem *packet) { +func (e *rawPacketEntry) SetNext(elem *rawPacket) { e.next = elem } // SetPrev assigns 'entry' as the entry that precedes e in the list. -func (e *packetEntry) SetPrev(elem *packet) { +func (e *rawPacketEntry) SetPrev(elem *rawPacket) { e.prev = elem } diff --git a/pkg/tcpip/transport/raw/raw_state_autogen.go b/pkg/tcpip/transport/raw/raw_state_autogen.go index ceb504915..3753ccc37 100755 --- a/pkg/tcpip/transport/raw/raw_state_autogen.go +++ b/pkg/tcpip/transport/raw/raw_state_autogen.go @@ -7,19 +7,19 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/buffer" ) -func (x *packet) beforeSave() {} -func (x *packet) save(m state.Map) { +func (x *rawPacket) beforeSave() {} +func (x *rawPacket) save(m state.Map) { x.beforeSave() var data buffer.VectorisedView = x.saveData() m.SaveValue("data", data) - m.Save("packetEntry", &x.packetEntry) + m.Save("rawPacketEntry", &x.rawPacketEntry) m.Save("timestampNS", &x.timestampNS) m.Save("senderAddr", &x.senderAddr) } -func (x *packet) afterLoad() {} -func (x *packet) load(m state.Map) { - m.Load("packetEntry", &x.packetEntry) +func (x *rawPacket) afterLoad() {} +func (x *rawPacket) load(m state.Map) { + m.Load("rawPacketEntry", &x.rawPacketEntry) m.Load("timestampNS", &x.timestampNS) m.Load("senderAddr", &x.senderAddr) m.LoadValue("data", new(buffer.VectorisedView), func(y interface{}) { x.loadData(y.(buffer.VectorisedView)) }) @@ -56,35 +56,35 @@ func (x *endpoint) load(m state.Map) { m.AfterLoad(x.afterLoad) } -func (x *packetList) beforeSave() {} -func (x *packetList) save(m state.Map) { +func (x *rawPacketList) beforeSave() {} +func (x *rawPacketList) save(m state.Map) { x.beforeSave() m.Save("head", &x.head) m.Save("tail", &x.tail) } -func (x *packetList) afterLoad() {} -func (x *packetList) load(m state.Map) { +func (x *rawPacketList) afterLoad() {} +func (x *rawPacketList) load(m state.Map) { m.Load("head", &x.head) m.Load("tail", &x.tail) } -func (x *packetEntry) beforeSave() {} -func (x *packetEntry) save(m state.Map) { +func (x *rawPacketEntry) beforeSave() {} +func (x *rawPacketEntry) save(m state.Map) { x.beforeSave() m.Save("next", &x.next) m.Save("prev", &x.prev) } -func (x *packetEntry) afterLoad() {} -func (x *packetEntry) load(m state.Map) { +func (x *rawPacketEntry) afterLoad() {} +func (x *rawPacketEntry) load(m state.Map) { m.Load("next", &x.next) m.Load("prev", &x.prev) } func init() { - state.Register("raw.packet", (*packet)(nil), state.Fns{Save: (*packet).save, Load: (*packet).load}) + state.Register("raw.rawPacket", (*rawPacket)(nil), state.Fns{Save: (*rawPacket).save, Load: (*rawPacket).load}) state.Register("raw.endpoint", (*endpoint)(nil), state.Fns{Save: (*endpoint).save, Load: (*endpoint).load}) - state.Register("raw.packetList", (*packetList)(nil), state.Fns{Save: (*packetList).save, Load: (*packetList).load}) - state.Register("raw.packetEntry", (*packetEntry)(nil), state.Fns{Save: (*packetEntry).save, Load: (*packetEntry).load}) + state.Register("raw.rawPacketList", (*rawPacketList)(nil), state.Fns{Save: (*rawPacketList).save, Load: (*rawPacketList).load}) + state.Register("raw.rawPacketEntry", (*rawPacketEntry)(nil), state.Fns{Save: (*rawPacketEntry).save, Load: (*rawPacketEntry).load}) } |