Debian Codegraph @

srtcore/window.h

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above
20 copyright notice, this list of conditions and the
21 following disclaimer.
22
23 * Redistributions in binary form must reproduce the
24 above copyright notice, this list of conditions
25 and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 * Neither the name of the University of Illinois
29 nor the names of its contributors may be used to
30 endorse or promote products derived from this
31 software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45
46 /*****************************************************************************
47 written by
48 Yunhong Gu, last updated 01/22/2011
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #ifndef INC_SRT_WINDOW_H
54 #define INC_SRT_WINDOW_H
55
56
57 #ifndef _WIN32
58 #include <sys/time.h>
59 #include <time.h>
60 #endif
61 #include "packet.h"
62 #include "udt.h"
63
64 namespace srt
65 {
66
67 namespace ACKWindowTools
68 {
69 struct Seq
70 {
71 int32_t iACKSeqNo; // Seq. No. of the ACK packet
72 int32_t iACK; // Data packet Seq. No. carried by the ACK packet
73 sync::steady_clock::time_point tsTimeStamp; // The timestamp when the ACK was sent
74 };
75
76 void store(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t ack);
77 int acknowledge(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t& r_ack, const sync::steady_clock::time_point& currtime);
78 }
79
80 template <size_t SIZE>
81 class CACKWindow
82 {
83 public:
84 CACKWindow() :
85 m_aSeq(),
86 m_iHead(0),
87 m_iTail(0)
88 {
89 m_aSeq[0].iACKSeqNo = SRT_SEQNO_NONE;
90 }
91
92 ~CACKWindow() {}
93
94 /// Write an ACK record into the window.
95 /// @param [in] seq Seq. No. of the ACK packet
96 /// @param [in] ack Data packet Seq. No. carried by the ACK packet
97
98 void store(int32_t seq, int32_t ack)
99 {
100 return ACKWindowTools::store(m_aSeq, SIZE, m_iHead, m_iTail, seq, ack);
101 }
102
103 /// Search the ACKACK "seq" in the window, find out the data packet "ack"
104 /// and calculate RTT estimate based on the ACK/ACKACK pair
105 /// @param [in] seq Seq. No. of the ACK packet carried within ACKACK
106 /// @param [out] ack Acknowledged data packet Seq. No. from the ACK packet that matches the ACKACK
107 /// @param [in] currtime The timestamp of ACKACK packet reception by the receiver
108 /// @return RTT
109
110 int acknowledge(int32_t seq, int32_t& r_ack, const sync::steady_clock::time_point& currtime)
111 {
112 return ACKWindowTools::acknowledge(m_aSeq, SIZE, m_iHead, m_iTail, seq, r_ack, currtime);
113 }
114
115 private:
116
117 typedef ACKWindowTools::Seq Seq;
118
119 Seq m_aSeq[SIZE];
120 int m_iHead; // Pointer to the latest ACK record
121 int m_iTail; // Pointer to the oldest ACK record
122
123 private:
124 CACKWindow(const CACKWindow&);
125 CACKWindow& operator=(const CACKWindow&);
126 };
127
128 ////////////////////////////////////////////////////////////////////////////////
129
130 class CPktTimeWindowTools
131 {
132 public:
133 static int getPktRcvSpeed_in(const int* window, int* replica, const int* bytes, size_t asize, size_t hsize, int& bytesps);
134 static int getBandwidth_in(const int* window, int* replica, size_t psize);
135
136 static void initializeWindowArrays(int* r_pktWindow, int* r_probeWindow, int* r_bytesWindow, size_t asize, size_t psize, size_t max_payload_size);
137
138 static int ceilPerMega(double value, double count);
139 };
140
141 template <size_t ASIZE = 16, size_t PSIZE = 16>
142 class CPktTimeWindow: CPktTimeWindowTools
143 {
144 public:
145 CPktTimeWindow():
146 m_aPktWindow(),
147 m_aBytesWindow(),
148 m_iPktWindowPtr(0),
149 m_aProbeWindow(),
150 m_iProbeWindowPtr(0),
151 m_iLastSentTime(0),
152 m_iMinPktSndInt(1000000),
153 m_tsLastArrTime(sync::steady_clock::now()),
154 m_tsCurrArrTime(),
155 m_tsProbeTime(),
156 m_Probe1Sequence(SRT_SEQNO_NONE),
157 m_zPayloadSize(0),
158 m_zHeaderSize(0)
159 {
160 // Exception: up to CUDT ctor
161 sync::setupMutex(m_lockPktWindow, "PktWindow");
162 sync::setupMutex(m_lockProbeWindow, "ProbeWindow");
163 }
164
165 ~CPktTimeWindow()
166 {
167 }
168
169 public:
170 /// read the minimum packet sending interval.
171 /// @return minimum packet sending interval (microseconds).
172
173 int getMinPktSndInt() const { return m_iMinPktSndInt; }
174
175 /// Calculate the packets arrival speed.
176 /// @return Packet arrival speed (packets per second).
177
178 int getPktRcvSpeed(int& w_bytesps) const
179 {
180 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
181 // Lock access to the packet Window
182 sync::ScopedLock cg(m_lockPktWindow);
183
184 int pktReplica[ASIZE]; // packet information window (inter-packet time)
185 return getPktRcvSpeed_in(m_aPktWindow, pktReplica, m_aBytesWindow, ASIZE, m_zHeaderSize, (w_bytesps));
186 }
187
188 int getPktRcvSpeed() const
189 {
190 int bytesps;
191 return getPktRcvSpeed((bytesps));
192 }
193
194 /// Estimate the bandwidth.
195 /// @return Estimated bandwidth (packets per second).
196
197 int getBandwidth() const
198 {
199 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
200 // Lock access to the packet Window
201 sync::ScopedLock cg(m_lockProbeWindow);
202
203 int probeReplica[PSIZE];
204 return getBandwidth_in(m_aProbeWindow, probeReplica, PSIZE);
205 }
206
207 /// Record time information of a packet sending.
208 /// @param currtime timestamp of the packet sending.
209
210 void onPktSent(int currtime)
211 {
212 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
213 int interval = currtime - m_iLastSentTime;
214
215 if ((interval < m_iMinPktSndInt) && (interval > 0))
216 m_iMinPktSndInt = interval;
217
218 m_iLastSentTime = currtime;
219 }
220
221 /// Record time information of an arrived packet.
222
223 void onPktArrival(int pktsz = 0)
224 {
225 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
226 sync::ScopedLock cg(m_lockPktWindow);
227
228 m_tsCurrArrTime = sync::steady_clock::now();
229
230 // record the packet interval between the current and the last one
231 m_aPktWindow[m_iPktWindowPtr] = (int) sync::count_microseconds(m_tsCurrArrTime - m_tsLastArrTime);
232 m_aBytesWindow[m_iPktWindowPtr] = pktsz;
233
234 // the window is logically circular
235 ++ m_iPktWindowPtr;
236 if (m_iPktWindowPtr == ASIZE)
237 m_iPktWindowPtr = 0;
238
239 // remember last packet arrival time
240 m_tsLastArrTime = m_tsCurrArrTime;
241 }
242
243 /// Shortcut to test a packet for possible probe 1 or 2
244 void probeArrival(const CPacket& pkt, bool unordered)
245 {
246 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
247 const int inorder16 = pkt.seqno() & PUMASK_SEQNO_PROBE;
248
249 // for probe1, we want 16th packet
250 if (inorder16 == 0)
251 {
252 probe1Arrival(pkt, unordered);
253 }
254
255 if (unordered)
256 return;
257
258 // for probe2, we want 17th packet
259 if (inorder16 == 1)
260 {
261 probe2Arrival(pkt);
262 }
263 }
264
265 /// Record the arrival time of the first probing packet.
266 void probe1Arrival(const CPacket& pkt, bool unordered)
267 {
268 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
269 if (unordered && pkt.seqno() == m_Probe1Sequence)
270 {
271 // Reset the starting probe into "undefined", when
272 // a packet has come as retransmitted before the
273 // measurement at arrival of 17th could be taken.
274 m_Probe1Sequence = SRT_SEQNO_NONE;
275 return;
276 }
277
278 m_tsProbeTime = sync::steady_clock::now();
279 m_Probe1Sequence = pkt.seqno(); // Record the sequence where 16th packet probe was taken
280 }
281
282 /// Record the arrival time of the second probing packet and the interval between packet pairs.
283
284 void probe2Arrival(const CPacket& pkt)
285 {
286 SRT_ASSERT(m_zHeaderSize != 0 && m_zPayloadSize != 0);
287 // Reject probes that don't refer to the very next packet
288 // towards the one that was lately notified by probe1Arrival.
289 // Otherwise the result can be stupid.
290
291 // Simply, in case when this wasn't called exactly for the
292 // expected packet pair, behave as if the 17th packet was lost.
293
294 // no start point yet (or was reset) OR not very next packet
295 if (m_Probe1Sequence == SRT_SEQNO_NONE || CSeqNo::incseq(m_Probe1Sequence) != pkt.seqno())
296 return;
297
298 // Grab the current time before trying to acquire
299 // a mutex. This might add extra delay and therefore
300 // screw up the measurement.
301 const sync::steady_clock::time_point now = sync::steady_clock::now();
302
303 // Lock access to the packet Window
304 sync::ScopedLock cg(m_lockProbeWindow);
305
306 m_tsCurrArrTime = now;
307
308 // Reset the starting probe to prevent checking if the
309 // measurement was already taken.
310 m_Probe1Sequence = SRT_SEQNO_NONE;
311
312 // record the probing packets interval
313 // Adjust the time for what a complete packet would have take
314 const int64_t timediff = sync::count_microseconds(m_tsCurrArrTime - m_tsProbeTime);
315 const int64_t timediff_times_pl_size = timediff * m_zPayloadSize;
316
317 // Let's take it simpler than it is coded here:
318 // (stating that a packet has never zero size)
319 //
320 // probe_case = (now - previous_packet_time) * SRT_MAX_PAYLOAD_SIZE / pktsz;
321 //
322 // Meaning: if the packet is fully packed, probe_case = timediff.
323 // Otherwise the timediff will be "converted" to a time that a fully packed packet "would take",
324 // provided the arrival time is proportional to the payload size and skipping
325 // the ETH+IP+UDP+SRT header part eliminates the constant packet delivery time influence.
326 //
327 const size_t pktsz = pkt.getLength();
328 m_aProbeWindow[m_iProbeWindowPtr] = pktsz ? int(timediff_times_pl_size / pktsz) : int(timediff);
329
330 // OLD CODE BEFORE BSTATS:
331 // record the probing packets interval
332 // m_aProbeWindow[m_iProbeWindowPtr] = int(m_tsCurrArrTime - m_tsProbeTime);
333
334 // the window is logically circular
335 ++ m_iProbeWindowPtr;
336 if (m_iProbeWindowPtr == PSIZE)
337 m_iProbeWindowPtr = 0;
338 }
339
340 // Late initialization because these sizes aren't known at construction time of the parent CUDT class.
341 void initialize(size_t h, size_t s)
342 {
343 m_zHeaderSize = h;
344 m_zPayloadSize = s;
345 CPktTimeWindowTools::initializeWindowArrays(m_aPktWindow, m_aProbeWindow, m_aBytesWindow, ASIZE, PSIZE, s);
346 }
347
348 private:
349 int m_aPktWindow[ASIZE]; // Packet information window (inter-packet time)
350 int m_aBytesWindow[ASIZE];
351 int m_iPktWindowPtr; // Position pointer of the packet info. window
352 mutable sync::Mutex m_lockPktWindow; // Used to synchronize access to the packet window
353
354 int m_aProbeWindow[PSIZE]; // Record inter-packet time for probing packet pairs
355 int m_iProbeWindowPtr; // Position pointer to the probing window
356 mutable sync::Mutex m_lockProbeWindow; // Used to synchronize access to the probe window
357
358 int m_iLastSentTime; // Last packet sending time
359 int m_iMinPktSndInt; // Minimum packet sending interval
360
361 sync::steady_clock::time_point m_tsLastArrTime; // Last packet arrival time
362 sync::steady_clock::time_point m_tsCurrArrTime; // Current packet arrival time
363 sync::steady_clock::time_point m_tsProbeTime; // Arrival time of the first probing packet
364 int32_t m_Probe1Sequence; // Sequence number for which the arrival time was notified
365
366 size_t m_zPayloadSize;
367 size_t m_zHeaderSize;
368
369 private:
370 CPktTimeWindow(const CPktTimeWindow&);
371 CPktTimeWindow &operator=(const CPktTimeWindow&);
372 };
373
374 } // namespace srt
375
376 #endif