| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package fifo |
| 19 |
|
| 20 |
import ( |
| 21 |
"inet.af/netstack/sleep" |
| 22 |
"inet.af/netstack/sync" |
| 23 |
"inet.af/netstack/tcpip" |
| 24 |
"inet.af/netstack/tcpip/header" |
| 25 |
"inet.af/netstack/tcpip/stack" |
| 26 |
) |
| 27 |
|
| 28 |
var _ stack.LinkEndpoint = (*endpoint)(nil) |
| 29 |
var _ stack.GSOEndpoint = (*endpoint)(nil) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
type endpoint struct { |
| 37 |
dispatcher stack.NetworkDispatcher |
| 38 |
lower stack.LinkEndpoint |
| 39 |
wg sync.WaitGroup |
| 40 |
dispatchers []*queueDispatcher |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type queueDispatcher struct { |
| 47 |
lower stack.LinkEndpoint |
| 48 |
q *packetBufferQueue |
| 49 |
newPacketWaker sleep.Waker |
| 50 |
closeWaker sleep.Waker |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func New(lower stack.LinkEndpoint, n int, queueLen int) stack.LinkEndpoint { |
| 56 |
e := &endpoint{ |
| 57 |
lower: lower, |
| 58 |
} |
| 59 |
|
| 60 |
for i := 0; i < n; i++ { |
| 61 |
qd := &queueDispatcher{ |
| 62 |
q: &packetBufferQueue{limit: queueLen}, |
| 63 |
lower: lower, |
| 64 |
} |
| 65 |
e.dispatchers = append(e.dispatchers, qd) |
| 66 |
e.wg.Add(1) |
| 67 |
go func() { |
| 68 |
defer e.wg.Done() |
| 69 |
qd.dispatchLoop() |
| 70 |
}() |
| 71 |
} |
| 72 |
return e |
| 73 |
} |
| 74 |
|
| 75 |
func (q *queueDispatcher) dispatchLoop() { |
| 76 |
s := sleep.Sleeper{} |
| 77 |
s.AddWaker(&q.newPacketWaker) |
| 78 |
s.AddWaker(&q.closeWaker) |
| 79 |
defer s.Done() |
| 80 |
|
| 81 |
const batchSize = 32 |
| 82 |
var batch stack.PacketBufferList |
| 83 |
for { |
| 84 |
w := s.Fetch(true) |
| 85 |
if w == &q.closeWaker { |
| 86 |
return |
| 87 |
} |
| 88 |
|
| 89 |
for pkt := q.q.dequeue(); pkt != nil; pkt = q.q.dequeue() { |
| 90 |
batch.PushBack(pkt) |
| 91 |
if batch.Len() < batchSize && !q.q.empty() { |
| 92 |
continue |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
q.lower.WritePackets(stack.RouteInfo{}, batch, 0 ) |
| 97 |
batch.DecRef() |
| 98 |
batch.Reset() |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func (e *endpoint) DeliverNetworkPacket(remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 105 |
e.dispatcher.DeliverNetworkPacket(remote, local, protocol, pkt) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 110 |
|
| 111 |
if dispatcher == nil { |
| 112 |
e.lower.Attach(nil) |
| 113 |
e.Wait() |
| 114 |
e.dispatcher = nil |
| 115 |
return |
| 116 |
} |
| 117 |
e.dispatcher = dispatcher |
| 118 |
e.lower.Attach(e) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
func (e *endpoint) IsAttached() bool { |
| 123 |
return e.dispatcher != nil |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func (e *endpoint) MTU() uint32 { |
| 128 |
return e.lower.MTU() |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (e *endpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 133 |
return e.lower.Capabilities() |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
func (e *endpoint) MaxHeaderLength() uint16 { |
| 138 |
return e.lower.MaxHeaderLength() |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (e *endpoint) LinkAddress() tcpip.LinkAddress { |
| 143 |
return e.lower.LinkAddress() |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func (e *endpoint) GSOMaxSize() uint32 { |
| 148 |
if gso, ok := e.lower.(stack.GSOEndpoint); ok { |
| 149 |
return gso.GSOMaxSize() |
| 150 |
} |
| 151 |
return 0 |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func (e *endpoint) SupportedGSO() stack.SupportedGSO { |
| 156 |
if gso, ok := e.lower.(stack.GSOEndpoint); ok { |
| 157 |
return gso.SupportedGSO() |
| 158 |
} |
| 159 |
return stack.GSONotSupported |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func (e *endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 169 |
d := e.dispatchers[int(pkt.Hash)%len(e.dispatchers)] |
| 170 |
if !d.q.enqueue(pkt) { |
| 171 |
return &tcpip.ErrNoBufferSpace{} |
| 172 |
} |
| 173 |
d.newPacketWaker.Assert() |
| 174 |
return nil |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
func (e *endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) { |
| 185 |
enqueued := 0 |
| 186 |
for pkt := pkts.Front(); pkt != nil; { |
| 187 |
d := e.dispatchers[int(pkt.Hash)%len(e.dispatchers)] |
| 188 |
nxt := pkt.Next() |
| 189 |
if !d.q.enqueue(pkt) { |
| 190 |
if enqueued > 0 { |
| 191 |
d.newPacketWaker.Assert() |
| 192 |
} |
| 193 |
return enqueued, &tcpip.ErrNoBufferSpace{} |
| 194 |
} |
| 195 |
pkt = nxt |
| 196 |
enqueued++ |
| 197 |
d.newPacketWaker.Assert() |
| 198 |
} |
| 199 |
return enqueued, nil |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
func (e *endpoint) Wait() { |
| 204 |
e.lower.Wait() |
| 205 |
|
| 206 |
|
| 207 |
for i := range e.dispatchers { |
| 208 |
e.dispatchers[i].closeWaker.Assert() |
| 209 |
} |
| 210 |
|
| 211 |
e.wg.Wait() |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
func (e *endpoint) ARPHardwareType() header.ARPHardwareType { |
| 216 |
return e.lower.ARPHardwareType() |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
func (e *endpoint) AddHeader(local, remote tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 221 |
e.lower.AddHeader(local, remote, protocol, pkt) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func (e *endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error { |
| 226 |
return e.lower.WriteRawPacket(pkt) |
| 227 |
} |