| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package channel |
| 19 |
|
| 20 |
import ( |
| 21 |
"context" |
| 22 |
|
| 23 |
"inet.af/netstack/sync" |
| 24 |
"inet.af/netstack/tcpip" |
| 25 |
"inet.af/netstack/tcpip/header" |
| 26 |
"inet.af/netstack/tcpip/stack" |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
type PacketInfo struct { |
| 31 |
Pkt *stack.PacketBuffer |
| 32 |
|
| 33 |
|
| 34 |
Proto tcpip.NetworkProtocolNumber |
| 35 |
Route stack.RouteInfo |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type Notification interface { |
| 41 |
|
| 42 |
WriteNotify() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
type NotificationHandle struct { |
| 50 |
n Notification |
| 51 |
} |
| 52 |
|
| 53 |
type queue struct { |
| 54 |
|
| 55 |
c chan PacketInfo |
| 56 |
|
| 57 |
mu sync.RWMutex |
| 58 |
notify []*NotificationHandle |
| 59 |
} |
| 60 |
|
| 61 |
func (q *queue) Close() { |
| 62 |
close(q.c) |
| 63 |
} |
| 64 |
|
| 65 |
func (q *queue) Read() (PacketInfo, bool) { |
| 66 |
select { |
| 67 |
case p := <-q.c: |
| 68 |
return p, true |
| 69 |
default: |
| 70 |
return PacketInfo{}, false |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func (q *queue) ReadContext(ctx context.Context) (PacketInfo, bool) { |
| 75 |
select { |
| 76 |
case pkt := <-q.c: |
| 77 |
return pkt, true |
| 78 |
case <-ctx.Done(): |
| 79 |
return PacketInfo{}, false |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func (q *queue) Write(p PacketInfo) bool { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
p.Pkt.PreserveObject() |
| 96 |
wrote := false |
| 97 |
select { |
| 98 |
case q.c <- p: |
| 99 |
wrote = true |
| 100 |
default: |
| 101 |
} |
| 102 |
q.mu.Lock() |
| 103 |
notify := q.notify |
| 104 |
q.mu.Unlock() |
| 105 |
|
| 106 |
if wrote { |
| 107 |
|
| 108 |
for _, h := range notify { |
| 109 |
h.n.WriteNotify() |
| 110 |
} |
| 111 |
} |
| 112 |
return wrote |
| 113 |
} |
| 114 |
|
| 115 |
func (q *queue) Num() int { |
| 116 |
return len(q.c) |
| 117 |
} |
| 118 |
|
| 119 |
func (q *queue) AddNotify(notify Notification) *NotificationHandle { |
| 120 |
q.mu.Lock() |
| 121 |
defer q.mu.Unlock() |
| 122 |
h := &NotificationHandle{n: notify} |
| 123 |
q.notify = append(q.notify, h) |
| 124 |
return h |
| 125 |
} |
| 126 |
|
| 127 |
func (q *queue) RemoveNotify(handle *NotificationHandle) { |
| 128 |
q.mu.Lock() |
| 129 |
defer q.mu.Unlock() |
| 130 |
|
| 131 |
notify := make([]*NotificationHandle, 0, len(q.notify)) |
| 132 |
for _, h := range q.notify { |
| 133 |
if h != handle { |
| 134 |
notify = append(notify, h) |
| 135 |
} |
| 136 |
} |
| 137 |
q.notify = notify |
| 138 |
} |
| 139 |
|
| 140 |
var _ stack.LinkEndpoint = (*Endpoint)(nil) |
| 141 |
var _ stack.GSOEndpoint = (*Endpoint)(nil) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
type Endpoint struct { |
| 146 |
dispatcher stack.NetworkDispatcher |
| 147 |
mtu uint32 |
| 148 |
linkAddr tcpip.LinkAddress |
| 149 |
LinkEPCapabilities stack.LinkEndpointCapabilities |
| 150 |
SupportedGSOKind stack.SupportedGSO |
| 151 |
|
| 152 |
|
| 153 |
q *queue |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func New(size int, mtu uint32, linkAddr tcpip.LinkAddress) *Endpoint { |
| 158 |
return &Endpoint{ |
| 159 |
q: &queue{ |
| 160 |
c: make(chan PacketInfo, size), |
| 161 |
}, |
| 162 |
mtu: mtu, |
| 163 |
linkAddr: linkAddr, |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
func (e *Endpoint) Close() { |
| 170 |
e.q.Close() |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func (e *Endpoint) Read() (PacketInfo, bool) { |
| 175 |
return e.q.Read() |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (e *Endpoint) ReadContext(ctx context.Context) (PacketInfo, bool) { |
| 181 |
return e.q.ReadContext(ctx) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
func (e *Endpoint) Drain() int { |
| 186 |
c := 0 |
| 187 |
for { |
| 188 |
if _, ok := e.Read(); !ok { |
| 189 |
return c |
| 190 |
} |
| 191 |
c++ |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func (e *Endpoint) NumQueued() int { |
| 197 |
return e.q.Num() |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
func (e *Endpoint) InjectInbound(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { |
| 202 |
e.InjectLinkAddr(protocol, "", pkt) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func (e *Endpoint) InjectLinkAddr(protocol tcpip.NetworkProtocolNumber, remote tcpip.LinkAddress, pkt *stack.PacketBuffer) { |
| 207 |
e.dispatcher.DeliverNetworkPacket(remote, "" , protocol, pkt) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
func (e *Endpoint) Attach(dispatcher stack.NetworkDispatcher) { |
| 213 |
e.dispatcher = dispatcher |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
func (e *Endpoint) IsAttached() bool { |
| 218 |
return e.dispatcher != nil |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
func (e *Endpoint) MTU() uint32 { |
| 224 |
return e.mtu |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
func (e *Endpoint) Capabilities() stack.LinkEndpointCapabilities { |
| 229 |
return e.LinkEPCapabilities |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
func (*Endpoint) GSOMaxSize() uint32 { |
| 234 |
return 1 << 15 |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
func (e *Endpoint) SupportedGSO() stack.SupportedGSO { |
| 239 |
return e.SupportedGSOKind |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (*Endpoint) MaxHeaderLength() uint16 { |
| 245 |
return 0 |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
func (e *Endpoint) LinkAddress() tcpip.LinkAddress { |
| 250 |
return e.linkAddr |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
func (e *Endpoint) WritePacket(r stack.RouteInfo, protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) tcpip.Error { |
| 255 |
p := PacketInfo{ |
| 256 |
Pkt: pkt, |
| 257 |
Proto: protocol, |
| 258 |
Route: r, |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
_ = e.q.Write(p) |
| 265 |
|
| 266 |
return nil |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
func (e *Endpoint) WritePackets(r stack.RouteInfo, pkts stack.PacketBufferList, protocol tcpip.NetworkProtocolNumber) (int, tcpip.Error) { |
| 271 |
n := 0 |
| 272 |
for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { |
| 273 |
p := PacketInfo{ |
| 274 |
Pkt: pkt, |
| 275 |
Proto: protocol, |
| 276 |
Route: r, |
| 277 |
} |
| 278 |
|
| 279 |
if !e.q.Write(p) { |
| 280 |
break |
| 281 |
} |
| 282 |
n++ |
| 283 |
} |
| 284 |
|
| 285 |
return n, nil |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func (*Endpoint) Wait() {} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
func (e *Endpoint) AddNotify(notify Notification) *NotificationHandle { |
| 294 |
return e.q.AddNotify(notify) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func (e *Endpoint) RemoveNotify(handle *NotificationHandle) { |
| 299 |
e.q.RemoveNotify(handle) |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
func (*Endpoint) ARPHardwareType() header.ARPHardwareType { |
| 304 |
return header.ARPHardwareNone |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
func (*Endpoint) AddHeader(tcpip.LinkAddress, tcpip.LinkAddress, tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
func (e *Endpoint) WriteRawPacket(pkt *stack.PacketBuffer) tcpip.Error { |
| 313 |
p := PacketInfo{ |
| 314 |
Pkt: pkt, |
| 315 |
Proto: pkt.NetworkProtocolNumber, |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
_ = e.q.Write(p) |
| 322 |
|
| 323 |
return nil |
| 324 |
} |