Debian Codegraph @

receive_payload_queue.go

raw
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package sctp
5
6 import (
7 "fmt"
8 "math/bits"
9 "strings"
10 )
11
12 type receivePayloadQueue struct {
13 tailTSN uint32
14 chunkSize int
15 tsnBitmask []uint64
16 dupTSN []uint32
17 maxTSNOffset uint32
18
19 cumulativeTSN uint32
20 }
21
22 func newReceivePayloadQueue(maxTSNOffset uint32) *receivePayloadQueue {
23 maxTSNOffset = ((maxTSNOffset + 63) / 64) * 64
24
25 return &receivePayloadQueue{
26 tsnBitmask: make([]uint64, maxTSNOffset/64),
27 maxTSNOffset: maxTSNOffset,
28 }
29 }
30
31 func (q *receivePayloadQueue) init(cumulativeTSN uint32) {
32 q.cumulativeTSN = cumulativeTSN
33 q.tailTSN = cumulativeTSN
34 q.chunkSize = 0
35 for i := range q.tsnBitmask {
36 q.tsnBitmask[i] = 0
37 }
38 q.dupTSN = q.dupTSN[:0]
39 }
40
41 func (q *receivePayloadQueue) hasChunk(tsn uint32) bool {
42 if q.chunkSize == 0 || sna32LTE(tsn, q.cumulativeTSN) || sna32GT(tsn, q.tailTSN) {
43 return false
44 }
45
46 index, offset := int(tsn/64)%len(q.tsnBitmask), tsn%64
47
48 return q.tsnBitmask[index]&(1<<offset) != 0
49 }
50
51 func (q *receivePayloadQueue) canPush(tsn uint32) bool {
52 ok := q.hasChunk(tsn)
53 if ok || sna32LTE(tsn, q.cumulativeTSN) || sna32GT(tsn, q.cumulativeTSN+q.maxTSNOffset) {
54 return false
55 }
56
57 return true
58 }
59
60 // push pushes a payload data. If the payload data is already in our queue or
61 // older than our cumulativeTSN marker, it will be recored as duplications,
62 // which can later be retrieved using popDuplicates.
63 func (q *receivePayloadQueue) push(tsn uint32) bool {
64 if sna32GT(tsn, q.cumulativeTSN+q.maxTSNOffset) {
65 return false
66 }
67
68 if sna32LTE(tsn, q.cumulativeTSN) || q.hasChunk(tsn) {
69 // Found the packet, log in dups
70 q.dupTSN = append(q.dupTSN, tsn)
71
72 return false
73 }
74
75 index, offset := int(tsn/64)%len(q.tsnBitmask), tsn%64
76 q.tsnBitmask[index] |= (1 << offset)
77 q.chunkSize++
78 if sna32GT(tsn, q.tailTSN) {
79 q.tailTSN = tsn
80 }
81
82 return true
83 }
84
85 // pop advances cumulativeTSN and pops the oldest chunk's TSN if it matches the given TSN or force is true.
86 func (q *receivePayloadQueue) pop(force bool) bool {
87 tsn := q.cumulativeTSN + 1
88 if q.hasChunk(tsn) {
89 index, offset := int(tsn/64)%len(q.tsnBitmask), int(tsn%64)
90 q.tsnBitmask[index] &= ^uint64(1 << (offset))
91 q.chunkSize--
92 q.cumulativeTSN++
93
94 return true
95 }
96 if force {
97 q.cumulativeTSN++
98 if q.chunkSize == 0 {
99 q.tailTSN = q.cumulativeTSN
100 }
101 }
102
103 return false
104 }
105
106 // advanceCumulativeTSN moves the cumulative TSN forward and removes any queued
107 // TSNs that are now covered by the cumulative point.
108 func (q *receivePayloadQueue) advanceCumulativeTSN(cumulativeTSN uint32) {
109 if !sna32LT(q.cumulativeTSN, cumulativeTSN) {
110 return
111 }
112
113 if q.chunkSize == 0 || sna32LTE(q.tailTSN, cumulativeTSN) {
114 for i := range q.tsnBitmask {
115 q.tsnBitmask[i] = 0
116 }
117 q.chunkSize = 0
118 q.cumulativeTSN = cumulativeTSN
119 q.tailTSN = cumulativeTSN
120
121 return
122 }
123
124 q.clearTSNRange(q.cumulativeTSN+1, cumulativeTSN)
125 q.cumulativeTSN = cumulativeTSN
126 if q.chunkSize == 0 {
127 q.tailTSN = cumulativeTSN
128 }
129 }
130
131 func (q *receivePayloadQueue) clearTSNRange(startTSN, endTSN uint32) {
132 for remaining := endTSN - startTSN + 1; remaining > 0; {
133 offset := startTSN % 64
134 n := min(remaining, 64-offset)
135 mask := ^uint64(0)
136 if n < 64 {
137 mask = ((uint64(1) << n) - 1) << offset
138 }
139
140 index := int(startTSN/64) % len(q.tsnBitmask)
141 cleared := q.tsnBitmask[index] & mask
142 q.tsnBitmask[index] &^= mask
143 q.chunkSize -= bits.OnesCount64(cleared)
144
145 startTSN += n
146 remaining -= n
147 }
148 }
149
150 // popDuplicates returns an array of TSN values that were duplicated.
151 func (q *receivePayloadQueue) popDuplicates() []uint32 {
152 dups := q.dupTSN
153 q.dupTSN = []uint32{}
154
155 return dups
156 }
157
158 func (q *receivePayloadQueue) getGapAckBlocks() (gapAckBlocks []gapAckBlock) {
159 var ackBlock gapAckBlock
160
161 if q.chunkSize == 0 {
162 return nil
163 }
164
165 // RFC 9260 section 3.3.4: Gap Ack Blocks report received TSNs greater than
166 // the Cumulative TSN Ack and up to the highest TSN newly received.
167 startTSN, endTSN := q.cumulativeTSN+1, q.tailTSN
168 var findEnd bool
169 for tsn := startTSN; sna32LTE(tsn, endTSN); {
170 index, offset := int(tsn/64)%len(q.tsnBitmask), int(tsn%64)
171 if !findEnd { //nolint:nestif
172 // find first received tsn as start
173 if nonZeroBit, ok := getFirstNonZeroBit(q.tsnBitmask[index], offset, 64); ok {
174 //nolint:gosec // G115
175 ackBlock.start = uint16(tsn + uint32(nonZeroBit-offset) - q.cumulativeTSN)
176 tsn += uint32(nonZeroBit - offset) //nolint:gosec // G115
177 findEnd = true
178 } else {
179 // no result, find start bits in next uint64 bitmask
180 tsn += uint32(64 - offset) //nolint:gosec // G115
181 }
182 } else {
183 if zeroBit, ok := getFirstZeroBit(q.tsnBitmask[index], offset, 64); ok {
184 //nolint:gosec // G115
185 ackBlock.end = uint16(tsn + uint32(zeroBit-offset) - 1 - q.cumulativeTSN)
186 tsn += uint32(zeroBit - offset) //nolint:gosec // G115
187 if sna32LTE(tsn, endTSN) {
188 gapAckBlocks = append(gapAckBlocks, gapAckBlock{
189 start: ackBlock.start,
190 end: ackBlock.end,
191 })
192 }
193 findEnd = false
194 } else {
195 tsn += uint32(64 - offset) //nolint:gosec // G115
196 }
197
198 // no zero bit at the end, close and append the last gap
199 if sna32GT(tsn, endTSN) {
200 ackBlock.end = uint16(endTSN - q.cumulativeTSN) //nolint:gosec // G115
201 gapAckBlocks = append(gapAckBlocks, gapAckBlock{
202 start: ackBlock.start,
203 end: ackBlock.end,
204 })
205
206 break
207 }
208 }
209 }
210
211 return gapAckBlocks
212 }
213
214 func (q *receivePayloadQueue) getGapAckBlocksString() string {
215 gapAckBlocks := q.getGapAckBlocks()
216 var str strings.Builder
217 fmt.Fprintf(&str, "cumTSN=%d", q.cumulativeTSN)
218 for _, b := range gapAckBlocks {
219 fmt.Fprintf(&str, ",%d-%d", b.start, b.end)
220 }
221
222 return str.String()
223 }
224
225 func (q *receivePayloadQueue) getLastTSNReceived() (uint32, bool) {
226 if q.chunkSize == 0 {
227 return 0, false
228 }
229
230 return q.tailTSN, true
231 }
232
233 func (q *receivePayloadQueue) getcumulativeTSN() uint32 {
234 return q.cumulativeTSN
235 }
236
237 func (q *receivePayloadQueue) size() int {
238 return q.chunkSize
239 }
240
241 func getFirstNonZeroBit(val uint64, start, end int) (int, bool) {
242 i := bits.TrailingZeros64(val >> uint64(start)) //nolint:gosec // G115
243
244 return i + start, i+start < end
245 }
246
247 func getFirstZeroBit(val uint64, start, end int) (int, bool) {
248 return getFirstNonZeroBit(^val, start, end)
249 }