Debian Codegraph @

tcpip/stack/pending_packets.go

raw symbols SCIP
1 // Copyright 2020 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 stack
16
17 import (
18 "fmt"
19
20 "inet.af/netstack/sync"
21 "inet.af/netstack/tcpip"
22 )
23
24 const (
25 // maxPendingResolutions is the maximum number of pending link-address
26 // resolutions.
27 maxPendingResolutions = 64
28 maxPendingPacketsPerResolution = 256
29 )
30
31 // pendingPacketBuffer is a pending packet buffer.
32 //
33 // TODO(gvisor.dev/issue/5331): Drop this when we drop WritePacket and only use
34 // WritePackets so we can use a PacketBufferList everywhere.
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 // packetsPendingLinkResolution is a queue of packets pending link resolution.
54 //
55 // Once link resolution completes successfully, the packets will be written.
56 type packetsPendingLinkResolution struct {
57 nic *nic
58
59 mu struct {
60 sync.Mutex
61
62 // The packets to send once the resolver completes.
63 //
64 // The link resolution channel is used as the key for this map.
65 packets map[<-chan struct{}][]pendingPacket
66
67 // FIFO of channels used to cancel the oldest goroutine waiting for
68 // link-address resolution.
69 //
70 // cancelChans holds the same channels that are used as keys to packets.
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 // dequeue any pending packets associated with ch.
92 //
93 // If err is nil, packets will be written and sent to the given remote link
94 // address.
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 // enqueue a packet to be sent once link resolution completes.
117 //
118 // If the maximum number of pending resolutions is reached, the packets
119 // associated with the oldest link resolution will be dequeued as if they failed
120 // link resolution.
121 func (f *packetsPendingLinkResolution) enqueue(r *Route, proto tcpip.NetworkProtocolNumber, pkt pendingPacketBuffer) (int, tcpip.Error) {
122 f.mu.Lock()
123 // Make sure we attempt resolution while holding f's lock so that we avoid
124 // a race where link resolution completes before we enqueue the packets.
125 //
126 // A @ T1: Call ResolvedFields (get link resolution channel)
127 // B @ T2: Complete link resolution, dequeue pending packets
128 // C @ T1: Enqueue packet that already completed link resolution (which will
129 // never dequeue)
130 //
131 // To make sure B does not interleave with A and C, we make sure A and C are
132 // done while holding the lock.
133 routeInfo, ch, err := r.resolvedFields(nil)
134 switch err.(type) {
135 case nil:
136 // The route resolved immediately, so we don't need to wait for link
137 // resolution to send the packet.
138 f.mu.Unlock()
139 return f.nic.writePacketBuffer(routeInfo, proto, pkt)
140 case *tcpip.ErrWouldBlock:
141 // We need to wait for link resolution to complete.
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 // Dequeue the pending packets in a new goroutine to not hold up the current
182 // goroutine as handing link resolution failures may be a costly operation.
183 go f.dequeuePackets(cancelledPackets, "" /* linkAddr */, &tcpip.ErrAborted{})
184 }
185
186 return pkt.len(), nil
187 }
188
189 // newCancelChannelLocked appends the link resolution channel to a FIFO. If the
190 // maximum number of pending resolutions is reached, the oldest channel will be
191 // removed and its associated pending packets will be returned.
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 }