| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package stack |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
|
| 20 |
"inet.af/netstack/sync" |
| 21 |
"inet.af/netstack/tcpip" |
| 22 |
) |
| 23 |
|
| 24 |
const ( |
| 25 |
|
| 26 |
|
| 27 |
maxPendingResolutions = 64 |
| 28 |
maxPendingPacketsPerResolution = 256 |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type pendingPacketBuffer interface { |
| 36 |
len() int |
| 37 |
} |
| 38 |
|
| 39 |
func (*PacketBuffer) len() int { |
| 40 |
return 1 |
| 41 |
} |
| 42 |
|
| 43 |
func (p *PacketBufferList) len() int { |
| 44 |
return p.Len() |
| 45 |
} |
| 46 |
|
| 47 |
type pendingPacket struct { |
| 48 |
routeInfo RouteInfo |
| 49 |
proto tcpip.NetworkProtocolNumber |
| 50 |
pkt pendingPacketBuffer |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
type packetsPendingLinkResolution struct { |
| 57 |
nic *nic |
| 58 |
|
| 59 |
mu struct { |
| 60 |
sync.Mutex |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
packets map[<-chan struct{}][]pendingPacket |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
cancelChans []<-chan struct{} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func (f *packetsPendingLinkResolution) incrementOutgoingPacketErrors(proto tcpip.NetworkProtocolNumber, pkt pendingPacketBuffer) { |
| 76 |
n := uint64(pkt.len()) |
| 77 |
f.nic.stack.stats.IP.OutgoingPacketErrors.IncrementBy(n) |
| 78 |
|
| 79 |
if ipEndpointStats, ok := f.nic.getNetworkEndpoint(proto).Stats().(IPNetworkEndpointStats); ok { |
| 80 |
ipEndpointStats.IPStats().OutgoingPacketErrors.IncrementBy(n) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (f *packetsPendingLinkResolution) init(nic *nic) { |
| 85 |
f.mu.Lock() |
| 86 |
defer f.mu.Unlock() |
| 87 |
f.nic = nic |
| 88 |
f.mu.packets = make(map[<-chan struct{}][]pendingPacket) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
func (f *packetsPendingLinkResolution) dequeue(ch <-chan struct{}, linkAddr tcpip.LinkAddress, err tcpip.Error) { |
| 96 |
f.mu.Lock() |
| 97 |
packets, ok := f.mu.packets[ch] |
| 98 |
delete(f.mu.packets, ch) |
| 99 |
|
| 100 |
if ok { |
| 101 |
for i, cancelChan := range f.mu.cancelChans { |
| 102 |
if cancelChan == ch { |
| 103 |
f.mu.cancelChans = append(f.mu.cancelChans[:i], f.mu.cancelChans[i+1:]...) |
| 104 |
break |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
f.mu.Unlock() |
| 110 |
|
| 111 |
if ok { |
| 112 |
f.dequeuePackets(packets, linkAddr, err) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func (f *packetsPendingLinkResolution) enqueue(r *Route, proto tcpip.NetworkProtocolNumber, pkt pendingPacketBuffer) (int, tcpip.Error) { |
| 122 |
f.mu.Lock() |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
routeInfo, ch, err := r.resolvedFields(nil) |
| 134 |
switch err.(type) { |
| 135 |
case nil: |
| 136 |
|
| 137 |
|
| 138 |
f.mu.Unlock() |
| 139 |
return f.nic.writePacketBuffer(routeInfo, proto, pkt) |
| 140 |
case *tcpip.ErrWouldBlock: |
| 141 |
|
| 142 |
default: |
| 143 |
f.mu.Unlock() |
| 144 |
return 0, err |
| 145 |
} |
| 146 |
|
| 147 |
defer f.mu.Unlock() |
| 148 |
|
| 149 |
packets, ok := f.mu.packets[ch] |
| 150 |
packets = append(packets, pendingPacket{ |
| 151 |
routeInfo: routeInfo, |
| 152 |
proto: proto, |
| 153 |
pkt: pkt, |
| 154 |
}) |
| 155 |
switch pkt := pkt.(type) { |
| 156 |
case *PacketBuffer: |
| 157 |
pkt.IncRef() |
| 158 |
case *PacketBufferList: |
| 159 |
pkt.IncRef() |
| 160 |
} |
| 161 |
|
| 162 |
if len(packets) > maxPendingPacketsPerResolution { |
| 163 |
f.incrementOutgoingPacketErrors(packets[0].proto, packets[0].pkt) |
| 164 |
packets[0] = pendingPacket{} |
| 165 |
packets = packets[1:] |
| 166 |
|
| 167 |
if numPackets := len(packets); numPackets != maxPendingPacketsPerResolution { |
| 168 |
panic(fmt.Sprintf("holding more queued packets than expected; got = %d, want <= %d", numPackets, maxPendingPacketsPerResolution)) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
f.mu.packets[ch] = packets |
| 173 |
|
| 174 |
if ok { |
| 175 |
return pkt.len(), nil |
| 176 |
} |
| 177 |
|
| 178 |
cancelledPackets := f.newCancelChannelLocked(ch) |
| 179 |
|
| 180 |
if len(cancelledPackets) != 0 { |
| 181 |
|
| 182 |
|
| 183 |
go f.dequeuePackets(cancelledPackets, "" , &tcpip.ErrAborted{}) |
| 184 |
} |
| 185 |
|
| 186 |
return pkt.len(), nil |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func (f *packetsPendingLinkResolution) newCancelChannelLocked(newCH <-chan struct{}) []pendingPacket { |
| 193 |
f.mu.cancelChans = append(f.mu.cancelChans, newCH) |
| 194 |
if len(f.mu.cancelChans) <= maxPendingResolutions { |
| 195 |
return nil |
| 196 |
} |
| 197 |
|
| 198 |
ch := f.mu.cancelChans[0] |
| 199 |
f.mu.cancelChans[0] = nil |
| 200 |
f.mu.cancelChans = f.mu.cancelChans[1:] |
| 201 |
if l := len(f.mu.cancelChans); l > maxPendingResolutions { |
| 202 |
panic(fmt.Sprintf("max pending resolutions reached; got %d active resolutions, max = %d", l, maxPendingResolutions)) |
| 203 |
} |
| 204 |
|
| 205 |
packets, ok := f.mu.packets[ch] |
| 206 |
if !ok { |
| 207 |
panic("must have a packet queue for an uncancelled channel") |
| 208 |
} |
| 209 |
delete(f.mu.packets, ch) |
| 210 |
|
| 211 |
return packets |
| 212 |
} |
| 213 |
|
| 214 |
func (f *packetsPendingLinkResolution) dequeuePackets(packets []pendingPacket, linkAddr tcpip.LinkAddress, err tcpip.Error) { |
| 215 |
for _, p := range packets { |
| 216 |
if err == nil { |
| 217 |
p.routeInfo.RemoteLinkAddress = linkAddr |
| 218 |
_, _ = f.nic.writePacketBuffer(p.routeInfo, p.proto, p.pkt) |
| 219 |
} else { |
| 220 |
f.incrementOutgoingPacketErrors(p.proto, p.pkt) |
| 221 |
|
| 222 |
if linkResolvableEP, ok := f.nic.getNetworkEndpoint(p.proto).(LinkResolvableNetworkEndpoint); ok { |
| 223 |
switch pkt := p.pkt.(type) { |
| 224 |
case *PacketBuffer: |
| 225 |
linkResolvableEP.HandleLinkResolutionFailure(pkt) |
| 226 |
case *PacketBufferList: |
| 227 |
for pb := pkt.Front(); pb != nil; pb = pb.Next() { |
| 228 |
linkResolvableEP.HandleLinkResolutionFailure(pb) |
| 229 |
} |
| 230 |
default: |
| 231 |
panic(fmt.Sprintf("unrecognized pending packet buffer type = %T", p.pkt)) |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
switch pkt := p.pkt.(type) { |
| 236 |
case *PacketBuffer: |
| 237 |
pkt.DecRef() |
| 238 |
case *PacketBufferList: |
| 239 |
pkt.DecRef() |
| 240 |
} |
| 241 |
} |
| 242 |
} |