| 1 | // Copyright 2018 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 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package waitable provides the implementation of data-link layer endpoints |
| 16 | // that wrap other endpoints, and can wait for inflight calls to WritePacket or |
| 17 | // DeliverNetworkPacket to finish (and new ones to be prevented). |
| 18 | // |
| 19 | // Waitable endpoints can be used in the networking stack by calling New(eID) to |
| 20 | // create a new endpoint, where eID is the ID of the endpoint being wrapped, |
| 21 | // and then passing it as an argument to Stack.CreateNIC(). |
| 22 | package waitable |
| 23 | |
| 24 | import ( |
| 25 | "inet.af/netstack/sync" |
| 26 | "inet.af/netstack/tcpip" |
| 27 | "inet.af/netstack/tcpip/header" |
| 28 | "inet.af/netstack/tcpip/stack" |
| 29 | ) |
| 30 | |
| 31 | // Endpoint is a waitable link-layer endpoint. |
| 32 | type Endpoint struct { |
| 33 | dispatchGate sync.Gate |
| 34 | dispatcher stack.NetworkDispatcher |
| 35 | |
| 36 | writeGate sync.Gate |
| 37 | lower stack.LinkEndpoint |
| 38 | } |
| 39 | |
| 40 | // New creates a new waitable link-layer endpoint. It wraps around another |
| 41 | // endpoint and allows the caller to block new write/dispatch calls and wait for |
| 42 | // the inflight ones to finish before returning. |
| 43 | func New(lower stack.LinkEndpoint) *Endpoint { |
| 44 | return &Endpoint{ |
| 45 | lower: lower, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // DeliverNetworkPacket implements stack.NetworkDispatcher.DeliverNetworkPacket. |
| 50 | // It is called by the link-layer endpoint being wrapped when a packet arrives, |
| 51 | // and only forwards to the actual dispatcher if Wait or WaitDispatch haven't |
| 52 | // been called. |
| 53 | func (e *Endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 54 | if !e.dispatchGate.Enter() { |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | e.dispatcher.DeliverNetworkPacket(remote, local, protocol, pkt) |
| 59 | e.dispatchGate.Leave() |
| 60 | } |
| 61 | |
| 62 | // Attach implements stack.LinkEndpoint.Attach. It saves the dispatcher and |
| 63 | // registers with the lower endpoint as its dispatcher so that "e" is called |
| 64 | // for inbound packets. |
| 65 | func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 66 | e.dispatcher = dispatcher |
| 67 | e.lower.Attach(e) |
| 68 | } |
| 69 | |
| 70 | // IsAttached implements stack.LinkEndpoint.IsAttached. |
| 71 | func (e *Endpoint) IsAttached() bool { |
| 72 | return e.dispatcher != nil |
| 73 | } |
| 74 | |
| 75 | // MTU implements stack.LinkEndpoint.MTU. It just forwards the request to the |
| 76 | // lower endpoint. |
| 77 | func (e *Endpoint) MTU() uint32 { |
| 78 | return e.lower.MTU() |
| 79 | } |
| 80 | |
| 81 | // Capabilities implements stack.LinkEndpoint.Capabilities. It just forwards the |
| 82 | // request to the lower endpoint. |
| 83 | func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 84 | return e.lower.Capabilities() |
| 85 | } |
| 86 | |
| 87 | // MaxHeaderLength implements stack.LinkEndpoint.MaxHeaderLength. It just |
| 88 | // forwards the request to the lower endpoint. |
| 89 | func (e *Endpoint) MaxHeaderLength() uint16 { |
| 90 | return e.lower.MaxHeaderLength() |
| 91 | } |
| 92 | |
| 93 | // LinkAddress implements stack.LinkEndpoint.LinkAddress. It just forwards the |
| 94 | // request to the lower endpoint. |
| 95 | func (e *Endpoint) LinkAddress() tcpip.LinkAddress { |
| 96 | return e.lower.LinkAddress() |
| 97 | } |
| 98 | |
| 99 | // WritePacket implements stack.LinkEndpoint.WritePacket. It is called by |
| 100 | // higher-level protocols to write packets. It only forwards packets to the |
| 101 | // lower endpoint if Wait or WaitWrite haven't been called. |
| 102 | func (e *Endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 103 | if !e.writeGate.Enter() { |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | err := e.lower.WritePacket(r, protocol, pkt) |
| 108 | e.writeGate.Leave() |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | // WritePackets implements stack.LinkEndpoint.WritePackets. It is called by |
| 113 | // higher-level protocols to write packets. It only forwards packets to the |
| 114 | // lower endpoint if Wait or WaitWrite haven't been called. |
| 115 | func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) { |
| 116 | if !e.writeGate.Enter() { |
| 117 | return pkts.Len(), nil |
| 118 | } |
| 119 | |
| 120 | n, err := e.lower.WritePackets(r, pkts, protocol) |
| 121 | e.writeGate.Leave() |
| 122 | return n, err |
| 123 | } |
| 124 | |
| 125 | // WaitWrite prevents new calls to WritePacket from reaching the lower endpoint, |
| 126 | // and waits for inflight ones to finish before returning. |
| 127 | func (e *Endpoint) WaitWrite() { |
| 128 | e.writeGate.Close() |
| 129 | } |
| 130 | |
| 131 | // WaitDispatch prevents new calls to DeliverNetworkPacket from reaching the |
| 132 | // actual dispatcher, and waits for inflight ones to finish before returning. |
| 133 | func (e *Endpoint) WaitDispatch() { |
| 134 | e.dispatchGate.Close() |
| 135 | } |
| 136 | |
| 137 | // Wait implements stack.LinkEndpoint.Wait. |
| 138 | func (e *Endpoint) Wait() {} |
| 139 | |
| 140 | // ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType. |
| 141 | func (e *Endpoint) ARPHardwareType() header.ARPHardwareType { |
| 142 | return e.lower.ARPHardwareType() |
| 143 | } |
| 144 | |
| 145 | // AddHeader implements stack.LinkEndpoint.AddHeader. |
| 146 | func (e *Endpoint) AddHeader(local, remote tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 147 | e.lower.AddHeader(local, remote, protocol, pkt) |
| 148 | } |
| 149 | |
| 150 | // WriteRawPacket implements stack.LinkEndpoint. |
| 151 | func (*Endpoint) WriteRawPacket(*stack.PacketBuffer) tcpip.Error { return &tcpip.ErrNotSupported{} } |