Debian Codegraph @

srtcore/core.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 02/28/2012
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53
54 #ifndef INC_SRT_CORE_H
55 #define INC_SRT_CORE_H
56
57 #include <deque>
58 #include <sstream>
59 #include "srt.h"
60 #include "common.h"
61 #include "list.h"
62 #include "buffer_snd.h"
63 #include "buffer_rcv.h"
64 #include "window.h"
65 #include "packet.h"
66 #include "channel.h"
67 #include "cache.h"
68 #include "queue.h"
69 #include "handshake.h"
70 #include "congctl.h"
71 #include "packetfilter.h"
72 #include "socketconfig.h"
73 #include "utilities.h"
74 #include "logger_defs.h"
75
76 #include "stats.h"
77
78 #include <haicrypt.h>
79
80 #ifndef SRT_ENABLE_FREQUENT_LOG_TRACE
81 #define SRT_ENABLE_FREQUENT_LOG_TRACE 0
82 #endif
83
84
85 // TODO: Utility function - to be moved to utilities.h?
86 template <class T>
87 inline T CountIIR(T base, T newval, double factor)
88 {
89 if ( base == 0.0 )
90 return newval;
91
92 T diff = newval - base;
93 return base+T(diff*factor);
94 }
95
96 // TODO: Probably a better rework for that can be done - this can be
97 // turned into a serializable structure, just like it's done for CHandShake.
98 enum AckDataItem
99 {
100 ACKD_RCVLASTACK = 0,
101 ACKD_RTT = 1,
102 ACKD_RTTVAR = 2,
103 ACKD_BUFFERLEFT = 3,
104 ACKD_TOTAL_SIZE_SMALL = 4, // Size of the Small ACK, packet length = 16.
105
106 // Extra fields for Full ACK.
107 ACKD_RCVSPEED = 4,
108 ACKD_BANDWIDTH = 5,
109 ACKD_TOTAL_SIZE_UDTBASE = 6, // Packet length = 24.
110
111 // Extra stats since SRT v1.0.1.
112 ACKD_RCVRATE = 6,
113 ACKD_TOTAL_SIZE_VER101 = 7, // Packet length = 28.
114
115 // Only in SRT v1.0.2.
116 ACKD_XMRATE_VER102_ONLY = 7,
117 ACKD_TOTAL_SIZE_VER102_ONLY = 8, // Packet length = 32.
118
119 ACKD_TOTAL_SIZE = ACKD_TOTAL_SIZE_VER102_ONLY // The maximum known ACK length is 32 bytes.
120 };
121 const size_t ACKD_FIELD_SIZE = sizeof(int32_t);
122
123 #ifdef ENABLE_MAXREXMITBW
124 static const size_t SRT_SOCKOPT_NPOST = 13;
125 #else
126 static const size_t SRT_SOCKOPT_NPOST = 12;
127 #endif
128
129 extern const SRT_SOCKOPT srt_post_opt_list [];
130
131 enum GroupDataItem
132 {
133 GRPD_GROUPID,
134 GRPD_GROUPDATA,
135
136 GRPD_E_SIZE
137 };
138
139 const size_t GRPD_MIN_SIZE = 2; // ID and GROUPTYPE as backward compat
140
141 const size_t GRPD_FIELD_SIZE = sizeof(int32_t);
142
143 // For HSv4 legacy handshake
144 #define SRT_MAX_HSRETRY 10 /* Maximum SRT handshake retry */
145
146 enum SeqPairItems
147 {
148 SEQ_BEGIN = 0, SEQ_END = 1, SEQ_SIZE = 2
149 };
150
151
152 // Extended SRT Congestion control class - only an incomplete definition required
153 class CCryptoControl;
154
155 namespace srt {
156 class CUDTUnited;
157 class CUDTSocket;
158 #if ENABLE_BONDING
159 class CUDTGroup;
160 #endif
161
162 #ifdef ENABLE_RATE_MEASUREMENT
163 struct RateMeasurement
164 {
165 typedef sync::steady_clock clock_type;
166 typedef clock_type::time_point clock_time;
167 typedef clock_type::duration clock_interval;
168
169 static const int SLICE_INTERVAL_MS = 20;
170 static const size_t MIN_SLICES = 5; // min
171 static const size_t MAX_SLICES = 10;
172
173 sync::Mutex m_lock;
174
175 size_t m_SysHeaderSize;
176
177 // Instantaneous data updated by sending packets
178 // AFFINITY: Snd:worker thread
179 // MODE: write
180 sync::atomic<int> m_nInstaPackets;
181 sync::atomic<int> m_nInstaBytes;
182
183 void dataUpdate(int packets, int bytes)
184 {
185 sync::ScopedLock lk (m_lock);
186 m_nInstaPackets = m_nInstaPackets + packets;
187 m_nInstaBytes = m_nInstaBytes + bytes;
188 }
189
190 // Cached last measurement
191 // AFFINITY: Snd:worker thread MODE: read
192 // AFFINITY: Rcv:worker thread MODE: write
193 // (not locking because one data is updated at a time).
194 sync::atomic<int64_t> m_currentPktRate;
195 sync::atomic<int64_t> m_currentByteRate;
196 int64_t rateBytes() const
197 {
198 return m_currentByteRate;
199 }
200 int64_t ratePackets() const
201 {
202 return m_currentPktRate;
203 }
204
205 // Time of the last checkpoint or initialization
206 clock_time m_beginTime;
207
208 struct Slice
209 {
210 int packets;
211 int bytes;
212 clock_time begin_time;
213
214 Slice(): packets(0), bytes(0) {} // clock_time is non-POD
215 Slice(const clock_time& t): packets(0), bytes(0), begin_time(t) {}
216
217 };
218
219 struct Summary: Slice
220 {
221 Summary(const clock_time& earliest_time): Slice(earliest_time), nsamples(0) {}
222
223 unsigned char nsamples;
224
225 void consume(const Slice& another)
226 {
227 if (is_zero(begin_time))
228 begin_time = another.begin_time;
229 packets += another.packets;
230 bytes += another.bytes;
231 nsamples++;
232 }
233
234 int64_t ratePackets(const clock_time& end_time) const
235 {
236 double packets_per_micro = double(packets) * 1000 * 1000;
237 double rate = packets_per_micro / count_microseconds(end_time - begin_time);
238 return rate;
239 }
240
241 int64_t rateBytes(const clock_time& end_time, size_t hdr_size) const
242 {
243 double data_bytes = bytes + (packets * hdr_size);
244 double bytes_per_micro = data_bytes * 1000 * 1000;
245 double rate = bytes_per_micro / count_microseconds(end_time - begin_time);
246 return rate;
247 }
248 };
249
250 std::deque<Slice> m_slices;
251
252 RateMeasurement():
253 m_SysHeaderSize(CPacket::UDP_HDR_SIZE), // XXX NOTE: IPv4 !
254 m_nInstaPackets(0),
255 m_nInstaBytes(0),
256 m_currentPktRate(0),
257 m_currentByteRate(0)
258 {
259 }
260
261 bool passedInterval(const clock_time& this_time, clock_interval& w_interval)
262 {
263 if (is_zero(m_beginTime))
264 {
265 sync::ScopedLock lk (m_lock);
266 m_beginTime = this_time;
267 w_interval = clock_interval(0);
268 return false;
269 }
270 w_interval = this_time - m_beginTime;
271 return (sync::count_milliseconds(w_interval) > SLICE_INTERVAL_MS);
272 }
273
274 // This is to be called in constructor, only once.
275 void init(const clock_time& time, size_t sys_hdr_size)
276 {
277 // Just formally.
278 sync::ScopedLock lk (m_lock);
279 m_beginTime = time;
280 m_SysHeaderSize = sys_hdr_size;
281 }
282
283 // AFFINITY: Rcv:worker thread (update thread)
284 // This function should be called in regular time periods.
285 void pickup(const clock_time& time);
286 };
287 #endif
288
289 // XXX REFACTOR: The 'CUDT' class is to be merged with 'CUDTSocket'.
290 // There's no reason for separating them, there's no case of having them
291 // anyhow managed separately. After this is done, with a small help with
292 // separating the internal abnormal path management (exceptions) from the
293 // API (return values), through CUDTUnited, this class may become in future
294 // an officially exposed C++ API.
295 class CUDT
296 {
297 friend class CUDTSocket;
298 friend class CUDTUnited;
299 friend class CCC;
300 friend struct CUDTComp;
301 friend class CCache<CInfoBlock>;
302 friend class CRendezvousQueue;
303 friend class CSndQueue;
304 friend class CRcvQueue;
305 friend class CSndUList;
306 friend class CRcvUList;
307 friend class PacketFilter;
308 friend class CUDTGroup;
309 friend class TestMockCUDT; // unit tests
310
311 typedef sync::steady_clock::time_point time_point;
312 typedef sync::steady_clock::duration duration;
313 typedef sync::AtomicClock<sync::steady_clock> atomic_time_point;
314 typedef sync::AtomicDuration<sync::steady_clock> atomic_duration;
315
316 private: // constructor and destructor
317 void construct();
318 void clearData();
319 CUDT(CUDTSocket* parent);
320 CUDT(CUDTSocket* parent, const CUDT& ancestor);
321 const CUDT& operator=(const CUDT&) {return *this;} // = delete ?
322 ~CUDT();
323
324 public: //API
325 static int startup();
326 static int cleanup();
327 static int cleanupAtFork();
328 static SRTSOCKET socket();
329 #if ENABLE_BONDING
330 static SRTSOCKET createGroup(SRT_GROUP_TYPE);
331 static SRTSOCKET getGroupOfSocket(SRTSOCKET socket);
332 static int getGroupData(SRTSOCKET groupid, SRT_SOCKGROUPDATA* pdata, size_t* psize);
333 static bool isgroup(SRTSOCKET sock) { return (sock & SRTGROUP_MASK) != 0; }
334 #endif
335 static int bind(SRTSOCKET u, const sockaddr* name, int namelen);
336 static int bind(SRTSOCKET u, UDPSOCKET udpsock);
337 static int listen(SRTSOCKET u, int backlog);
338 static SRTSOCKET accept(SRTSOCKET u, sockaddr* addr, int* addrlen);
339 static SRTSOCKET accept_bond(const SRTSOCKET listeners [], int lsize, int64_t msTimeOut);
340 static int connect(SRTSOCKET u, const sockaddr* name, int namelen, int32_t forced_isn);
341 static int connect(SRTSOCKET u, const sockaddr* name, const sockaddr* tname, int namelen);
342 #if ENABLE_BONDING
343 static int connectLinks(SRTSOCKET grp, SRT_SOCKGROUPCONFIG links [], int arraysize);
344 #endif
345 static int close(SRTSOCKET u);
346 static int getpeername(SRTSOCKET u, sockaddr* name, int* namelen);
347 static int getsockname(SRTSOCKET u, sockaddr* name, int* namelen);
348 static int getsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, void* optval, int* optlen);
349 static int setsockopt(SRTSOCKET u, int level, SRT_SOCKOPT optname, const void* optval, int optlen);
350 static int send(SRTSOCKET u, const char* buf, int len, int flags);
351 static int recv(SRTSOCKET u, char* buf, int len, int flags);
352 static int sendmsg(SRTSOCKET u, const char* buf, int len, int ttl = SRT_MSGTTL_INF, bool inorder = false, int64_t srctime = 0);
353 static int recvmsg(SRTSOCKET u, char* buf, int len, int64_t& srctime);
354 static int sendmsg2(SRTSOCKET u, const char* buf, int len, SRT_MSGCTRL& mctrl);
355 static int recvmsg2(SRTSOCKET u, char* buf, int len, SRT_MSGCTRL& w_mctrl);
356 static int64_t sendfile(SRTSOCKET u, std::fstream& ifs, int64_t& offset, int64_t size, int block = SRT_DEFAULT_SENDFILE_BLOCK);
357 static int64_t recvfile(SRTSOCKET u, std::fstream& ofs, int64_t& offset, int64_t size, int block = SRT_DEFAULT_RECVFILE_BLOCK);
358 static int select(int nfds, UDT::UDSET* readfds, UDT::UDSET* writefds, UDT::UDSET* exceptfds, const timeval* timeout);
359 static int selectEx(const std::vector<SRTSOCKET>& fds, std::vector<SRTSOCKET>* readfds, std::vector<SRTSOCKET>* writefds, std::vector<SRTSOCKET>* exceptfds, int64_t msTimeOut);
360 static int epoll_create();
361 static int epoll_clear_usocks(int eid);
362 static int epoll_add_usock(const int eid, const SRTSOCKET u, const int* events = NULL);
363 static int epoll_add_ssock(const int eid, const SYSSOCKET s, const int* events = NULL);
364 static int epoll_remove_usock(const int eid, const SRTSOCKET u);
365 static int epoll_remove_ssock(const int eid, const SYSSOCKET s);
366 static int epoll_update_usock(const int eid, const SRTSOCKET u, const int* events = NULL);
367 static int epoll_update_ssock(const int eid, const SYSSOCKET s, const int* events = NULL);
368 static int epoll_wait(const int eid, std::set<SRTSOCKET>* readfds, std::set<SRTSOCKET>* writefds,
369 int64_t msTimeOut, std::set<SYSSOCKET>* lrfds = NULL, std::set<SYSSOCKET>* wrfds = NULL);
370 static int epoll_uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut);
371 static int32_t epoll_set(const int eid, int32_t flags);
372 static int epoll_release(const int eid);
373 static CUDTException& getlasterror();
374 static int bstats(SRTSOCKET u, CBytePerfMon* perf, bool clear = true, bool instantaneous = false);
375 #if ENABLE_BONDING
376 static int groupsockbstats(SRTSOCKET u, CBytePerfMon* perf, bool clear = true);
377 #endif
378 static SRT_SOCKSTATUS getsockstate(SRTSOCKET u);
379 static bool setstreamid(SRTSOCKET u, const std::string& sid);
380 static std::string getstreamid(SRTSOCKET u);
381 static int getsndbuffer(SRTSOCKET u, size_t* blocks, size_t* bytes);
382 static int rejectReason(SRTSOCKET s);
383 static int rejectReason(SRTSOCKET s, int value);
384 static int64_t socketStartTime(SRTSOCKET s);
385
386 public: // internal API
387 // This is public so that it can be used directly in API implementation functions.
388 struct APIError
389 {
390 APIError(const CUDTException&);
391 APIError(CodeMajor, CodeMinor, int = 0);
392
393 operator int() const
394 {
395 return SRT_ERROR;
396 }
397 };
398
399 static const SRTSOCKET INVALID_SOCK = -1; // Invalid socket descriptor
400 static const int ERROR = -1; // Socket api error returned value
401
402 static const int HS_VERSION_UDT4 = 4;
403 static const int HS_VERSION_SRT1 = 5;
404
405 // Parameters
406 //
407 // NOTE: Use notation with X*1000*1000*... instead of
408 // million zeros in a row.
409 static const int COMM_RESPONSE_MAX_EXP = 16;
410 static const int SRT_TLPKTDROP_MINTHRESHOLD_MS = 1000;
411 static const uint64_t COMM_KEEPALIVE_PERIOD_US = 1*1000*1000;
412 static const int32_t COMM_SYN_INTERVAL_US = 10*1000;
413 static const int COMM_CLOSE_BROKEN_LISTENER_TIMEOUT_MS = 3000;
414 static const uint16_t MAX_WEIGHT = 32767;
415 static const size_t ACK_WND_SIZE = 1024;
416 static const int INITIAL_RTT = 10 * COMM_SYN_INTERVAL_US;
417 static const int INITIAL_RTTVAR = INITIAL_RTT / 2;
418
419 int handshakeVersion()
420 {
421 return m_ConnRes.m_iVersion;
422 }
423
424 int32_t handshakeCookie()
425 {
426 return m_ConnReq.m_iCookie;
427 }
428
429 static HandshakeSide handshakeSide(SRTSOCKET u);
430 HandshakeSide handshakeSide()
431 {
432 return m_SrtHsSide;
433 }
434
435 std::string CONID() const
436 {
437 #if ENABLE_LOGGING
438 std::ostringstream os;
439 os << "@" << m_SocketID << ": ";
440 return os.str();
441 #else
442 return "";
443 #endif
444 }
445
446 SRTSOCKET socketID() const { return m_SocketID; }
447
448 static CUDT* getUDTHandle(SRTSOCKET u);
449 static std::vector<SRTSOCKET> existingSockets();
450
451 void addressAndSend(CPacket& pkt);
452
453 SRT_ATTR_REQUIRES(m_ConnectionLock)
454 void sendSrtMsg(int cmd, uint32_t *srtdata_in = NULL, size_t srtlen_in = 0);
455
456 bool isOPT_TsbPd() const { return m_config.bTSBPD; }
457 int SRTT() const { return m_iSRTT; }
458 int RTTVar() const { return m_iRTTVar; }
459 int32_t sndSeqNo() const { return m_iSndCurrSeqNo; }
460 int32_t schedSeqNo() const { return m_iSndNextSeqNo; }
461 bool overrideSndSeqNo(int32_t seq);
462
463 #if ENABLE_BONDING
464 sync::steady_clock::time_point lastRspTime() const { return m_tsLastRspTime.load(); }
465 sync::steady_clock::time_point freshActivationStart() const { return m_tsFreshActivation; }
466 #endif
467
468 int32_t rcvSeqNo() const { return m_iRcvCurrSeqNo; }
469 SRT_ATTR_REQUIRES(m_RecvAckLock)
470 int flowWindowSize() const { return m_iFlowWindowSize; }
471 int32_t deliveryRate() const { return m_iDeliveryRate; }
472 int bandwidth() const { return m_iBandwidth; }
473 int64_t maxBandwidth() const { return m_config.llMaxBW; }
474 int MSS() const { return m_config.iMSS; }
475
476 uint32_t peerLatency_us() const { return m_iPeerTsbPdDelay_ms * 1000; }
477 int peerIdleTimeout_ms() const { return m_config.iPeerIdleTimeout_ms; }
478 size_t maxPayloadSize() const { return m_iMaxSRTPayloadSize; }
479 size_t OPT_PayloadSize() const { return m_config.zExpPayloadSize; }
480 size_t payloadSize() const
481 {
482 // If payloadsize is set, it should already be checked that
483 // it is less than the possible maximum payload size. So return it
484 // if it is set to nonzero value. In case when the connection isn't
485 // yet established, return also 0, if the value wasn't set.
486 if (m_config.zExpPayloadSize || !m_bConnected)
487 return m_config.zExpPayloadSize;
488
489 // If SRTO_PAYLOADSIZE was remaining with 0 (default for FILE mode)
490 // then return the maximum payload size per packet.
491 return m_iMaxSRTPayloadSize;
492 }
493
494 int sndLossLength() { return m_pSndLossList->getLossLength(); }
495 int32_t ISN() const { return m_iISN; }
496 int32_t peerISN() const { return m_iPeerISN; }
497 duration minNAKInterval() const { return m_tdMinNakInterval; }
498 sockaddr_any peerAddr() const { return m_PeerAddr; }
499
500 /// Returns the number of packets in flight (sent, but not yet acknowledged).
501 /// @param lastack is the sequence number of the first unacknowledged packet.
502 /// @param curseq is the sequence number of the latest original packet sent
503 ///
504 /// @note When there are no packets in flight, lastack = incseq(curseq).
505 ///
506 /// @returns The number of packets in flight belonging to the interval [0; ...)
507 static int32_t getFlightSpan(int32_t lastack, int32_t curseq)
508 {
509 // Packets sent:
510 // | 1 | 2 | 3 | 4 | 5 |
511 // ^ ^
512 // | |
513 // lastack |
514 // curseq
515 //
516 // In Flight: [lastack; curseq]
517 //
518 // Normally 'lastack' should be PAST the 'curseq',
519 // however in a case when the sending stopped and all packets were
520 // ACKed, the 'lastack' is one sequence ahead of 'curseq'.
521 // Therefore we increase 'curseq' by 1 forward and then
522 // get the distance towards the last ACK. This way this value may
523 // be only positive as seqlen() includes endpoints.
524 // Finally, we subtract 1 to exclude the increment added earlier.
525
526 return CSeqNo::seqlen(lastack, CSeqNo::incseq(curseq)) - 1;
527 }
528
529 /// Returns the number of packets in flight (sent, but not yet acknowledged).
530 /// @returns The number of packets in flight belonging to the interval [0; ...)
531 SRT_ATTR_REQUIRES(m_RecvAckLock)
532 int32_t getFlightSpan() const
533 {
534 return getFlightSpan(m_iSndLastAck, m_iSndCurrSeqNo);
535 }
536
537 int minSndSize(int len = 0) const
538 {
539 const int ps = (int) maxPayloadSize();
540 if (len == 0) // weird, can't use non-static data member as default argument!
541 len = ps;
542 return m_config.bMessageAPI ? (len+ps-1)/ps : 1;
543 }
544
545 static int32_t makeTS(const time_point& from_time, const time_point& tsStartTime)
546 {
547 // NOTE:
548 // - This calculates first the time difference towards start time.
549 // - This difference value is also CUT OFF THE SEGMENT information
550 // (a multiple of MAX_TIMESTAMP+1)
551 // So, this can be simply defined as: TS = (RTS - STS) % (MAX_TIMESTAMP+1)
552 SRT_ASSERT(from_time >= tsStartTime);
553 return (int32_t) sync::count_microseconds(from_time - tsStartTime);
554 }
555
556 static void setPacketTS(CPacket& p, const time_point& start_time, const time_point& ts)
557 {
558 p.set_timestamp(makeTS(ts, start_time));
559 }
560
561 /// @brief Set the timestamp field of the packet using the provided value (no check)
562 /// @param p the packet structure to set the timestamp on.
563 /// @param ts timestamp to use as a source for packet timestamp.
564 SRT_ATTR_EXCLUDES(m_StatsLock)
565 void setPacketTS(CPacket& p, const time_point& ts);
566
567 /// @brief Set the timestamp field of the packet according the TSBPD mode.
568 /// Also checks the connection start time (m_tsStartTime).
569 /// @param p the packet structure to set the timestamp on.
570 /// @param ts timestamp to use as a source for packet timestamp. Ignored if m_bPeerTsbPd is false.
571 SRT_ATTR_EXCLUDES(m_StatsLock)
572 void setDataPacketTS(CPacket& p, const time_point& ts);
573
574 // Utility used for closing a listening socket
575 // immediately to free the socket
576 void notListening()
577 {
578 m_bListening = false;
579 m_pRcvQueue->removeListener(this);
580 }
581
582 static int32_t generateISN()
583 {
584 using namespace sync;
585 return genRandomInt(0, CSeqNo::m_iMaxSeqNo);
586 }
587
588 static CUDTUnited& uglobal(); // UDT global management base
589
590 std::set<int>& pollset() { return m_sPollID; }
591
592 CSrtConfig m_config;
593
594 SRTU_PROPERTY_RO(SRTSOCKET, id, m_SocketID);
595 SRTU_PROPERTY_RO(bool, isClosing, m_bClosing);
596 SRTU_PROPERTY_RO(srt::CRcvBuffer*, rcvBuffer, m_pRcvBuffer);
597 SRTU_PROPERTY_RO(bool, isTLPktDrop, m_bTLPktDrop);
598 SRTU_PROPERTY_RO(bool, isSynReceiving, m_config.bSynRecving);
599 SRTU_PROPERTY_RR(sync::Condition*, recvDataCond, &m_RecvDataCond);
600 SRTU_PROPERTY_RR(sync::Condition*, recvTsbPdCond, &m_RcvTsbPdCond);
601
602 /// @brief Request a socket to be broken due to too long instability (normally by a group).
603 void breakAsUnstable() { m_bBreakAsUnstable = true; }
604
605 void ConnectSignal(ETransmissionEvent tev, EventSlot sl);
606 void DisconnectSignal(ETransmissionEvent tev);
607
608 // This is in public section so prospective overriding it can be
609 // done by directly assigning to a field.
610
611 typedef std::vector< std::pair<int32_t, int32_t> > loss_seqs_t;
612 typedef loss_seqs_t packetArrival_cb(void*, CPacket&);
613 CallbackHolder<packetArrival_cb> m_cbPacketArrival;
614
615 private:
616 /// initialize a UDT entity and bind to a local address.
617 void open();
618
619 /// Start listening to any connection request.
620 void setListenState();
621
622 /// Connect to a UDT entity listening at address "peer".
623 /// @param peer [in] The address of the listening UDT entity.
624 void startConnect(const sockaddr_any& peer, int32_t forced_isn);
625
626 /// Process the response handshake packet. Failure reasons can be:
627 /// * Socket is not in connecting state
628 /// * Response @a pkt is not a handshake control message
629 /// * Rendezvous socket has once processed a regular handshake
630 /// @param pkt [in] handshake packet.
631 /// @retval 0 Connection successful
632 /// @retval 1 Connection in progress (m_ConnReq turned into RESPONSE)
633 /// @retval -1 Connection failed
634 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
635 EConnectStatus processConnectResponse(const CPacket& pkt, CUDTException* eout) ATR_NOEXCEPT;
636
637 // This function works in case of HSv5 rendezvous. It changes the state
638 // according to the present state and received message type, as well as the
639 // INITIATOR/RESPONDER side resolved through cookieContest().
640 // The resulting data are:
641 // - rsptype: handshake message type that should be sent back to the peer (nothing if URQ_DONE)
642 // - needs_extension: the HSREQ/KMREQ or HSRSP/KMRSP extensions should be attached to the handshake message.
643 // - RETURNED VALUE: if true, it means a URQ_CONCLUSION message was received with HSRSP/KMRSP extensions and needs HSRSP/KMRSP.
644 void rendezvousSwitchState(UDTRequestType& rsptype, bool& needs_extension, bool& needs_hsrsp);
645 void cookieContest();
646
647 /// Interpret the incoming handshake packet in order to perform appropriate
648 /// rendezvous FSM state transition if needed, and craft the response, serialized
649 /// into the packet to be next sent.
650 /// @param reqpkt Packet to be written with handshake data
651 /// @param response incoming handshake response packet to be interpreted
652 /// @param serv_addr incoming packet's address
653 /// @param rst Current read status to know if the HS packet was freshly received from the peer, or this is only a periodic update (RST_AGAIN)
654 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
655 EConnectStatus processRendezvous(const CPacket* response, const sockaddr_any& serv_addr, EReadStatus, CPacket& reqpkt);
656 void sendRendezvousRejection(const sockaddr_any& serv_addr, CPacket& request);
657
658 /// Create the CryptoControl object based on the HS packet.
659 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
660 bool prepareConnectionObjects(const CHandShake &hs, HandshakeSide hsd, CUDTException* eout);
661
662 /// Allocates sender and receiver buffers and loss lists.
663 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
664 bool prepareBuffers(CUDTException* eout);
665 int getAuthTagSize() const;
666
667 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
668 EConnectStatus postConnect(const CPacket* response, bool rendezvous, CUDTException* eout) ATR_NOEXCEPT;
669
670 SRT_ATR_NODISCARD bool applyResponseSettings(const CPacket* hspkt /*[[nullable]]*/) ATR_NOEXCEPT;
671 SRT_ATR_NODISCARD EConnectStatus processAsyncConnectResponse(const CPacket& pkt) ATR_NOEXCEPT;
672 SRT_ATR_NODISCARD bool processAsyncConnectRequest(EReadStatus rst, EConnectStatus cst, const CPacket* response, const sockaddr_any& serv_addr);
673 SRT_ATR_NODISCARD EConnectStatus craftKmResponse(uint32_t* aw_kmdata, size_t& w_kmdatasize);
674
675 void checkUpdateCryptoKeyLen(const char* loghdr, int32_t typefield);
676
677 SRT_ATR_NODISCARD size_t fillSrtHandshake_HSREQ(uint32_t* srtdata, size_t srtlen, int hs_version);
678 SRT_ATR_NODISCARD size_t fillSrtHandshake_HSRSP(uint32_t* srtdata, size_t srtlen, int hs_version);
679 SRT_ATR_NODISCARD size_t fillSrtHandshake(uint32_t* srtdata, size_t srtlen, int msgtype, int hs_version);
680
681 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
682 bool createSrtHandshake(int srths_cmd, int srtkm_cmd, const uint32_t* data, size_t datalen,
683 CPacket& w_reqpkt, CHandShake& w_hs);
684
685 SRT_ATR_NODISCARD size_t fillHsExtConfigString(uint32_t *pcmdspec, int cmd, const std::string &str);
686 #if ENABLE_BONDING
687 SRT_ATR_NODISCARD size_t fillHsExtGroup(uint32_t *pcmdspec);
688 #endif
689 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
690 size_t fillHsExtKMREQ(uint32_t *pcmdspec, size_t ki);
691
692 SRT_ATR_NODISCARD size_t fillHsExtKMRSP(uint32_t *pcmdspec, const uint32_t *kmdata, size_t kmdata_wordsize);
693
694 SRT_ATR_NODISCARD size_t prepareSrtHsMsg(int cmd, uint32_t* srtdata, size_t size);
695
696 SRT_ATR_NODISCARD bool processSrtMsg(const CPacket *ctrlpkt);
697 SRT_ATR_NODISCARD int processSrtMsg_HSREQ(const uint32_t* srtdata, size_t bytelen, uint32_t ts, int hsv);
698 SRT_ATR_NODISCARD int processSrtMsg_HSRSP(const uint32_t* srtdata, size_t bytelen, uint32_t ts, int hsv);
699 SRT_ATR_NODISCARD bool interpretSrtHandshake(const CHandShake& hs, const CPacket& hspkt, uint32_t* out_data, size_t* out_len);
700 SRT_ATR_NODISCARD bool checkApplyFilterConfig(const std::string& cs);
701
702 #if ENABLE_BONDING
703 static CUDTGroup& newGroup(const int); // defined EXCEPTIONALLY in api.cpp for convenience reasons
704 // Note: This is an "interpret" function, which should treat the tp as
705 // "possibly group type" that might be out of the existing values.
706 SRT_ATR_NODISCARD bool interpretGroup(const int32_t grpdata[], size_t data_size, int hsreq_type_cmd);
707 SRT_ATR_NODISCARD SRTSOCKET makeMePeerOf(SRTSOCKET peergroup, SRT_GROUP_TYPE tp, uint32_t link_flags);
708 void synchronizeWithGroup(CUDTGroup* grp);
709 #endif
710
711 void updateAfterSrtHandshake(int hsv);
712
713 void updateSrtRcvSettings();
714 void updateSrtSndSettings();
715
716 void updateIdleLinkFrom(CUDT* source);
717
718 /// @brief Drop packets too late to be delivered if any.
719 /// @returns the number of packets actually dropped.
720 SRT_ATTR_REQUIRES2(m_RecvAckLock, m_StatsLock)
721 int sndDropTooLate();
722
723 // Returns true if there is a regular packet waiting for sending
724 // and sending regular packets has priority over retransmitted ones.
725 bool isRegularSendingPriority();
726
727 // Performs updates in the measurement variables after possible
728 // extraction of a retransmission packet. Some are for debug purposes,
729 // others for retransmission rate measurement.
730 void updateSenderMeasurements(bool can_rexmit);
731
732 /// Connect to a UDT entity as per hs request. This will update
733 /// required data in the entity, then update them also in the hs structure,
734 /// and then send the response back to the caller.
735 /// @param agent [in] The address to which the UDT entity is bound.
736 /// @param peer [in] The address of the listening UDT entity.
737 /// @param hspkt [in] The original packet that brought the handshake.
738 /// @param hs [in/out] The handshake information sent by the peer side (in), negotiated value (out).
739 void acceptAndRespond(const sockaddr_any& agent, const sockaddr_any& peer, const CPacket& hspkt, CHandShake& hs);
740
741 /// Write back to the hs structure the data after they have been
742 /// negotiated by acceptAndRespond.
743 void rewriteHandshakeData(const sockaddr_any& peer, CHandShake& w_hs);
744 bool runAcceptHook(CUDT* acore, const sockaddr* peer, const CHandShake& hs, const CPacket& hspkt);
745
746 /// Close the opened UDT entity.
747
748 bool closeInternal() ATR_NOEXCEPT;
749 bool closeAtFork() ATR_NOEXCEPT;
750 void updateBrokenConnection();
751 void completeBrokenConnectionDependencies(int errorcode);
752
753 /// Request UDT to send out a data block "data" with size of "len".
754 /// @param data [in] The address of the application data to be sent.
755 /// @param len [in] The size of the data block.
756 /// @return Actual size of data sent.
757
758 SRT_ATR_NODISCARD int send(const char* data, int len)
759 {
760 return sendmsg(data, len, SRT_MSGTTL_INF, false, 0);
761 }
762
763 /// Request UDT to receive data to a memory block "data" with size of "len".
764 /// @param data [out] data received.
765 /// @param len [in] The desired size of data to be received.
766 /// @return Actual size of data received.
767
768 SRT_ATR_NODISCARD int recv(char* data, int len);
769
770 /// send a message of a memory block "data" with size of "len".
771 /// @param data [out] data received.
772 /// @param len [in] The desired size of data to be received.
773 /// @param ttl [in] the time-to-live of the message.
774 /// @param inorder [in] if the message should be delivered in order.
775 /// @param srctime [in] Time when the data were ready to send.
776 /// @return Actual size of data sent.
777
778 SRT_ATR_NODISCARD int sendmsg(const char* data, int len, int ttl, bool inorder, int64_t srctime);
779 /// Receive a message to buffer "data".
780 /// @param data [out] data received.
781 /// @param len [in] size of the buffer.
782 /// @return Actual size of data received.
783
784 SRT_ATR_NODISCARD int sendmsg2(const char* data, int len, SRT_MSGCTRL& w_m);
785
786 SRT_ATR_NODISCARD int recvmsg(char* data, int len, int64_t& srctime);
787 SRT_ATR_NODISCARD int recvmsg2(char* data, int len, SRT_MSGCTRL& w_m);
788 SRT_ATR_NODISCARD int receiveMessage(char* data, int len, SRT_MSGCTRL& w_m, int erh = 1 /*throw exception*/);
789 SRT_ATR_NODISCARD int receiveBuffer(char* data, int len);
790
791 size_t dropMessage(int32_t seqtoskip);
792
793 /// Request UDT to send out a file described as "fd", starting from "offset", with size of "size".
794 /// @param ifs [in] The input file stream.
795 /// @param offset [in, out] From where to read and send data; output is the new offset when the call returns.
796 /// @param size [in] How many data to be sent.
797 /// @param block [in] size of block per read from disk
798 /// @return Actual size of data sent.
799
800 SRT_ATR_NODISCARD int64_t sendfile(std::fstream& ifs, int64_t& offset, int64_t size, int block = 366000);
801
802 /// Request UDT to receive data into a file described as "fd", starting from "offset", with expected size of "size".
803 /// @param ofs [out] The output file stream.
804 /// @param offset [in, out] From where to write data; output is the new offset when the call returns.
805 /// @param size [in] How many data to be received.
806 /// @param block [in] size of block per write to disk
807 /// @return Actual size of data received.
808
809 SRT_ATR_NODISCARD int64_t recvfile(std::fstream& ofs, int64_t& offset, int64_t size, int block = 7320000);
810
811 /// Configure UDT options.
812 /// @param optName [in] The enum name of a UDT option.
813 /// @param optval [in] The value to be set.
814 /// @param optlen [in] size of "optval".
815
816 void setOpt(SRT_SOCKOPT optName, const void* optval, int optlen);
817
818 /// Read UDT options.
819 /// @param optName [in] The enum name of a UDT option.
820 /// @param optval [in] The value to be returned.
821 /// @param optlen [out] size of "optval".
822
823 void getOpt(SRT_SOCKOPT optName, void* optval, int& w_optlen);
824
825 #if ENABLE_BONDING
826 /// Applies the configuration set on the socket.
827 /// Any errors in this process are reported by exception.
828 SRT_ERRNO applyMemberConfigObject(const SRT_SocketOptionObject& opt);
829 #endif
830
831 /// read the performance data with bytes counters since bstats()
832 ///
833 /// @param perf [in, out] pointer to a CPerfMon structure to record the performance data.
834 /// @param clear [in] flag to decide if the local performance trace should be cleared.
835 /// @param instantaneous [in] flag to request instantaneous data
836 /// instead of moving averages.
837 void bstats(CBytePerfMon* perf, bool clear = true, bool instantaneous = false);
838
839 /// Mark sequence contained in the given packet as not lost. This
840 /// removes the loss record from both current receiver loss list and
841 /// the receiver fresh loss list.
842 void unlose(const CPacket& oldpacket);
843 void dropFromLossLists(int32_t from, int32_t to);
844
845 SRT_ATTR_REQUIRES(m_RecvAckLock)
846 bool getFirstNoncontSequence(int32_t& w_seq, std::string& w_log_reason);
847
848 SRT_ATTR_EXCLUDES(m_ConnectionLock)
849 void checkSndTimers();
850
851 /// @brief Check and perform KM refresh if needed.
852 void checkSndKMRefresh();
853
854 void handshakeDone()
855 {
856 m_iSndHsRetryCnt = 0;
857 }
858
859 int64_t withOverhead(int64_t basebw)
860 {
861 return (basebw * (100 + m_config.iOverheadBW))/100;
862 }
863
864 static double Bps2Mbps(int64_t basebw)
865 {
866 return double(basebw) * 8.0/1000000.0;
867 }
868
869 bool stillConnected()
870 {
871 // Still connected is when:
872 // - no "broken" condition appeared (security, protocol error, response timeout)
873 return !m_bBroken
874 // - still connected (no one called srt_close())
875 && m_bConnected
876 // - isn't currently closing (srt_close() called, response timeout, shutdown)
877 && !m_bClosing;
878 }
879
880 int sndSpaceLeft()
881 {
882 return static_cast<int>(sndBuffersLeft() * maxPayloadSize());
883 }
884
885 int sndBuffersLeft()
886 {
887 return m_config.iSndBufSize - m_pSndBuffer->getCurrBufSize();
888 }
889
890 time_point socketStartTime()
891 {
892 sync::ScopedLock lk (m_StatsLock);
893 return m_stats.tsStartTime;
894 }
895
896 SRT_ATTR_EXCLUDES(m_RcvBufferLock)
897 bool isRcvBufferReady() const;
898
899 SRT_ATTR_REQUIRES(m_RcvBufferLock)
900 bool isRcvBufferReadyNoLock() const;
901
902 SRT_ATTR_EXCLUDES(m_RcvBufferLock)
903 bool isRcvBufferFull() const;
904
905 // TSBPD thread main function.
906 static void* tsbpd(void* param);
907
908 enum DropReason
909 {
910 DROP_TOO_LATE, //< Drop to keep up to the live pace (TLPKTDROP).
911 DROP_DISCARD //< Drop because another group member already provided these packets.
912 };
913
914 /// Drop too late packets (receiver side). Update loss lists and ACK positions.
915 /// The @a seqno packet itself is not dropped.
916 /// @param seqno [in] The sequence number of the first packets following those to be dropped.
917 /// @param reason A reason for dropping (see @a DropReason).
918 /// @return The number of packets dropped.
919 SRT_ATTR_EXCLUDES(m_RcvBufferLock, m_RcvLossLock)
920 int rcvDropTooLateUpTo(int seqno, DropReason reason = DROP_TOO_LATE);
921
922 static loss_seqs_t defaultPacketArrival(void* vself, CPacket& pkt);
923 static loss_seqs_t groupPacketArrival(void* vself, CPacket& pkt);
924
925 void setRateEstimator(const CRateEstimator& rate)
926 {
927 if (!m_pSndBuffer)
928 return;
929
930 m_pSndBuffer->setRateEstimator(rate);
931 updateCC(TEV_SYNC, EventVariant(0));
932 }
933
934
935 private: // Identification
936 CUDTSocket* const m_parent; // Temporary, until the CUDTSocket class is merged with CUDT
937 SRTSOCKET m_SocketID; // UDT socket number
938 SRTSOCKET m_PeerID; // Peer ID, for multiplexer
939
940 // HSv4 (legacy handshake) support)
941 time_point m_tsSndHsLastTime; // Last SRT handshake request time
942 int m_iSndHsRetryCnt; // SRT handshake retries left
943
944 #if ENABLE_BONDING
945 SRT_GROUP_TYPE m_HSGroupType; // Group type about-to-be-set in the handshake
946 #endif
947
948 private:
949 int m_iMaxSRTPayloadSize; // Maximum/regular payload size, in bytes
950 int m_iTsbPdDelay_ms; // Rx delay to absorb burst, in milliseconds
951 int m_iPeerTsbPdDelay_ms; // Tx delay that the peer uses to absorb burst, in milliseconds
952 bool m_bTLPktDrop; // Enable Too-late Packet Drop
953 SRT_ATTR_PT_GUARDED_BY(m_ConnectionLock)
954 UniquePtr<CCryptoControl> m_pCryptoControl; // Crypto control module
955 CCache<CInfoBlock>* m_pCache; // Network information cache
956
957 // Congestion control
958 std::vector<EventSlot> m_Slots[TEV_E_SIZE];
959 SrtCongestion m_CongCtl;
960
961 // Packet filtering
962 PacketFilter m_PacketFilter;
963 SRT_ARQLevel m_PktFilterRexmitLevel;
964 std::string m_sPeerPktFilterConfigString;
965
966 // Attached tool function
967 void EmitSignal(ETransmissionEvent tev, EventVariant var);
968
969 // Internal state
970 sync::atomic<bool> m_bListening; // If the UDT entity is listening to connection
971 sync::atomic<bool> m_bConnecting; // The short phase when connect() is called but not yet completed
972 sync::atomic<bool> m_bConnected; // Whether the connection is on or off
973 sync::atomic<bool> m_bClosing; // If the UDT entity is closing
974 sync::atomic<bool> m_bShutdown; // If the peer side has shutdown the connection
975 sync::atomic<bool> m_bBreaking; // The flag that declares interrupt of the connecting process
976 sync::atomic<bool> m_bBroken; // If the connection has been broken
977 sync::atomic<bool> m_bBreakAsUnstable; // A flag indicating that the socket should become broken because it has been unstable for too long.
978 sync::atomic<bool> m_bPeerHealth; // If the peer status is normal
979 sync::atomic<int> m_RejectReason;
980 bool m_bOpened; // If the UDT entity has been opened
981 // A counter (number of GC checks happening every 1s) to let the GC tag this socket as closed.
982 sync::atomic<int> m_iBrokenCounter; // If a broken socket still has data in the receiver buffer, it is not marked closed until the counter is 0.
983
984 int m_iEXPCount; // Expiration counter
985 sync::atomic<int> m_iBandwidth; // Estimated bandwidth, number of packets per second
986 sync::atomic<int> m_iSRTT; // Smoothed RTT (an exponentially-weighted moving average (EWMA)
987 // of an endpoint's RTT samples), in microseconds
988 sync::atomic<int> m_iRTTVar; // The variation in the RTT samples (RTT variance), in microseconds
989 sync::atomic<bool> m_bIsFirstRTTReceived; // True if the first RTT sample was obtained from the ACK/ACKACK pair
990 // at the receiver side or received by the sender from an ACK packet.
991 // It's used to reset the initial value of smoothed RTT (m_iSRTT)
992 // at the beginning of transmission (including the one taken from
993 // cache). False by default.
994 sync::atomic<int> m_iDeliveryRate; // Packet arrival rate at the receiver side
995 sync::atomic<int> m_iByteDeliveryRate; // Byte arrival rate at the receiver side
996
997 CHandShake m_ConnReq; // Connection request
998 CHandShake m_ConnRes; // Connection response
999 CHandShake::RendezvousState m_RdvState; // HSv5 rendezvous state
1000 HandshakeSide m_SrtHsSide; // HSv5 rendezvous handshake side resolved from cookie contest (DRAW if not yet resolved)
1001
1002 private: // Sending related data
1003 CSndBuffer* m_pSndBuffer; // Sender buffer
1004 CSndLossList* m_pSndLossList; // Sender loss list
1005 CPktTimeWindow<16, 16> m_SndTimeWindow; // Packet sending time window
1006 #ifdef ENABLE_MAXREXMITBW
1007 size_t m_zSndAveragePacketSize;
1008 size_t m_zSndMaxPacketSize;
1009 // XXX Old rate estimator for rexmit
1010 // CSndRateEstimator m_SndRexmitRate; // Retransmission rate estimation.
1011 CShaper m_SndRexmitShaper;
1012
1013 #ifdef ENABLE_RATE_MEASUREMENT
1014 RateMeasurement m_SndRegularMeasurement; // Regular rate measurement
1015 RateMeasurement m_SndRexmitMeasurement; // Retransmission rate measurement
1016 #endif
1017 #endif
1018
1019 atomic_duration m_tdSendInterval; // Inter-packet time, in CPU clock cycles
1020
1021 atomic_duration m_tdSendTimeDiff; // Aggregate difference in inter-packet sending time
1022
1023 SRT_ATTR_GUARDED_BY(m_RecvAckLock)
1024 sync::atomic<int> m_iFlowWindowSize; // Flow control window size
1025 sync::atomic<int> m_iCongestionWindow; // Congestion window size
1026
1027 private: // Timers
1028 atomic_time_point m_tsNextACKTime; // Next ACK time, in CPU clock cycles, same below
1029 atomic_time_point m_tsNextNAKTime; // Next NAK time
1030
1031 duration m_tdACKInterval; // ACK interval
1032 duration m_tdNAKInterval; // NAK interval
1033
1034 SRT_ATTR_GUARDED_BY(m_RecvAckLock)
1035 atomic_time_point m_tsLastRspTime; // Timestamp of last response from the peer
1036 time_point m_tsLastRspAckTime; // (SND) Timestamp of last ACK from the peer
1037 atomic_time_point m_tsLastSndTime; // Timestamp of last data/ctrl sent (in system ticks)
1038 time_point m_tsLastWarningTime; // Last time that a warning message is sent
1039 atomic_time_point m_tsLastReqTime; // last time when a connection request is sent
1040 time_point m_tsRcvPeerStartTime;
1041 time_point m_tsLingerExpiration; // Linger expiration time (for GC to close a socket with data in sending buffer)
1042 time_point m_tsLastAckTime; // (RCV) Timestamp of last ACK
1043 duration m_tdMinNakInterval; // NAK timeout lower bound; too small value can cause unnecessary retransmission
1044 duration m_tdMinExpInterval; // Timeout lower bound threshold: too small timeout can cause problem
1045
1046 int m_iPktCount; // Packet counter for ACK
1047 int m_iLightACKCount; // Light ACK counter
1048
1049 time_point m_tsNextSendTime; // Scheduled time of next packet sending
1050
1051 sync::atomic<int32_t> m_iSndLastFullAck; // Last full ACK received
1052 SRT_ATTR_GUARDED_BY(m_RecvAckLock)
1053 sync::atomic<int32_t> m_iSndLastAck; // Last ACK received
1054
1055 // NOTE: m_iSndLastDataAck is the value strictly bound to the CSndBufer object (m_pSndBuffer)
1056 // and this is the sequence number that refers to the block at position [0]. Upon acknowledgement,
1057 // this value is shifted to the acknowledged position, and the blocks are removed from the
1058 // m_pSndBuffer buffer up to excluding this sequence number.
1059 // XXX CONSIDER removing this field and giving up the maintenance of this sequence number
1060 // to the sending buffer. This way, extraction of an old packet for retransmission should
1061 // require only the lost sequence number, and how to find the packet with this sequence
1062 // will be up to the sending buffer.
1063 sync::atomic<int32_t> m_iSndLastDataAck; // The real last ACK that updates the sender buffer and loss list
1064 SRT_ATTR_GUARDED_BY(m_RecvAckLock)
1065 sync::atomic<int32_t> m_iSndCurrSeqNo; // The largest sequence number that HAS BEEN SENT
1066 sync::atomic<int32_t> m_iSndNextSeqNo; // The sequence number predicted to be placed at the currently scheduled packet
1067
1068 // Note important differences between Curr and Next fields:
1069 // - m_iSndCurrSeqNo: this is used by SRT:SndQ:worker thread and it's operated from CUDT::packData
1070 // function only. This value represents the sequence number that has been stamped on a packet directly
1071 // before it is sent over the network.
1072 // - m_iSndNextSeqNo: this is used by the user's thread and it's operated from CUDT::sendmsg2
1073 // function only. This value represents the sequence number that is PREDICTED to be stamped on the
1074 // first block out of the block series that will be scheduled for later sending over the network
1075 // out of the data passed in this function. For a special case when the length of the data is
1076 // short enough to be passed in one UDP packet (always the case for live mode), this value is
1077 // always increased by one in this call, otherwise it will be increased by the number of blocks
1078 // scheduled for sending.
1079
1080 int32_t m_iSndLastAck2; // Last ACK2 sent back
1081 time_point m_SndLastAck2Time; // The time when last ACK2 was sent back
1082 void setInitialSndSeq(int32_t isn)
1083 {
1084 m_iSndLastAck = isn;
1085 m_iSndLastDataAck = isn;
1086 m_iSndLastFullAck = isn;
1087 m_iSndCurrSeqNo = CSeqNo::decseq(isn);
1088 m_iSndNextSeqNo = isn;
1089 m_iSndLastAck2 = isn;
1090 }
1091
1092 void setInitialRcvSeq(int32_t isn);
1093
1094 int32_t m_iISN; // Initial Sequence Number
1095 bool m_bPeerTsbPd; // Peer accept TimeStamp-Based Rx mode
1096 bool m_bPeerTLPktDrop; // Enable sender late packet dropping
1097 bool m_bPeerNakReport; // Sender's peer (receiver) issues Periodic NAK Reports
1098 bool m_bPeerRexmitFlag; // Receiver supports rexmit flag in payload packets
1099
1100 SRT_ATTR_GUARDED_BY(m_RecvAckLock)
1101 int32_t m_iReXmitCount; // Re-Transmit Count since last ACK
1102
1103 static const size_t
1104 FREQLOGFA_ENCRYPTION_FAILURE = 0,
1105 FREQLOGFA_RCV_DROPPED = 1,
1106 FREQLOGFA_ACKACK_OUTOFORDER = 2,
1107 MAX_FREQLOGFA = 3;
1108
1109 atomic_time_point m_tsLogSlowDown[MAX_FREQLOGFA]; // The last time a log message from the "slow down" group was shown.
1110 // The "slow down" group of logs are those that can be printed too often otherwise, but can't be turned off (warnings and errors).
1111 // Currently only used by decryption failure message, therefore no mutex protection needed.
1112 sync::atomic<uint8_t> m_LogSlowDownExpired; // Can't use bitset because atomic
1113 sync::atomic<int> m_aSuppressedMsg[MAX_FREQLOGFA];
1114
1115 /// @brief Check if a frequent log can be shown.
1116 /// @param tnow current time
1117 /// @return true if it is ok to print a frequent log message.
1118 bool frequentLogAllowed(size_t logid, const time_point& tnow, std::string& why);
1119
1120 private: // Receiving related data
1121 SRT_ATTR_GUARDED_BY(m_RcvBufferLock)
1122 CRcvBuffer* m_pRcvBuffer; //< Receiver buffer
1123 SRT_ATTR_GUARDED_BY(m_RcvLossLock)
1124 CRcvLossList* m_pRcvLossList; //< Receiver loss list
1125 SRT_ATTR_GUARDED_BY(m_RcvLossLock)
1126 std::deque<CRcvFreshLoss> m_FreshLoss; //< Lost sequence already added to m_pRcvLossList, but not yet sent UMSG_LOSSREPORT for.
1127
1128 int m_iReorderTolerance; //< Current value of dynamic reorder tolerance
1129 int m_iConsecEarlyDelivery; //< Increases with every OOO packet that came <TTL-2 time, resets with every increased reorder tolerance
1130 int m_iConsecOrderedDelivery; //< Increases with every packet coming in order or retransmitted, resets with every out-of-order packet
1131
1132 CACKWindow<ACK_WND_SIZE> m_ACKWindow; // ACK history window
1133 CPktTimeWindow<16, 64> m_RcvTimeWindow; // Packet arrival time window
1134
1135 int32_t m_iRcvLastAck; // First unacknowledged packet seqno sent in the latest ACK.
1136 #ifdef ENABLE_LOGGING
1137 int32_t m_iDebugPrevLastAck;
1138 #endif
1139 int32_t m_iRcvLastAckAck; // (RCV) Latest packet seqno in a sent ACK acknowledged by ACKACK. RcvQTh (sendCtrlAck {r}, processCtrlAckAck {r}, processCtrlAck {r}, connection {w}).
1140 int32_t m_iAckSeqNo; // Last ACK sequence number
1141 sync::atomic<int32_t> m_iRcvCurrSeqNo; // (RCV) Largest received sequence number. RcvQTh, TSBPDTh.
1142 int32_t m_iRcvCurrPhySeqNo; // Same as m_iRcvCurrSeqNo, but physical only (disregarding a filter)
1143 bool m_bBufferWasFull; // Indicate that RX buffer was full last time a ack was sent
1144 int32_t m_iPeerISN; // Initial Sequence Number of the peer side
1145
1146 uint32_t m_uPeerSrtVersion;
1147 uint32_t m_uPeerSrtFlags;
1148
1149 bool m_bTsbPd; // Peer sends TimeStamp-Based Packet Delivery Packets
1150 bool m_bGroupTsbPd; // TSBPD should be used for GROUP RECEIVER instead
1151
1152 SRT_ATTR_GUARDED_BY(m_RcvTsbPdStartupLock)
1153 sync::CThread m_RcvTsbPdThread; // Rcv TsbPD Thread handle
1154 sync::Condition m_RcvTsbPdCond; // TSBPD signals if reading is ready. Use together with m_RecvLock
1155 bool m_bTsbPdNeedsWakeup; // Signal TsbPd thread to wake up on RCV buffer state change.
1156 sync::Mutex m_RcvTsbPdStartupLock; // Protects TSBPD thread creation and joining.
1157
1158 CallbackHolder<srt_listen_callback_fn> m_cbAcceptHook;
1159 CallbackHolder<srt_connect_callback_fn> m_cbConnectHook;
1160 // FORWARDER
1161 public:
1162 static int installAcceptHook(SRTSOCKET lsn, srt_listen_callback_fn* hook, void* opaq);
1163 static int installConnectHook(SRTSOCKET lsn, srt_connect_callback_fn* hook, void* opaq);
1164 static enum HandshakeSide compareCookies(int32_t req, int32_t res);
1165 static enum HandshakeSide backwardCompatibleCookieContest(int32_t req, int32_t res);
1166 private:
1167 void installAcceptHook(srt_listen_callback_fn* hook, void* opaq)
1168 {
1169 if (m_bConnected || m_bConnecting || m_bListening || m_bBroken)
1170 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
1171
1172 m_cbAcceptHook.set(opaq, hook);
1173 }
1174
1175 void installConnectHook(srt_connect_callback_fn* hook, void* opaq)
1176 {
1177 if (m_bConnected || m_bConnecting || m_bListening || m_bBroken)
1178 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
1179
1180 m_cbConnectHook.set(opaq, hook);
1181 }
1182
1183
1184 private: // synchronization: mutexes and conditions
1185 sync::Mutex m_ConnectionLock; // used to synchronize connection operation
1186
1187 sync::Condition m_SendBlockCond; // used to block "send" call
1188 sync::Mutex m_SendBlockLock; // lock associated to m_SendBlockCond
1189
1190 mutable sync::Mutex m_RcvBufferLock; // Protects the state of the m_pRcvBuffer
1191 // Protects access to m_iSndCurrSeqNo, m_iSndLastAck
1192 mutable sync::Mutex m_RecvAckLock; // Protects the state changes while processing incoming ACK (SRT_EPOLL_OUT)
1193
1194 sync::Condition m_RecvDataCond; // used to block "srt_recv*" when there is no data. Use together with m_RecvLock
1195 sync::Mutex m_RecvLock; // used to synchronize "srt_recv*" call, protects TSBPD drift updates (CRcvBuffer::isRcvDataReady())
1196
1197 sync::Mutex m_SendLock; // used to synchronize "send" call
1198 sync::Mutex m_RcvLossLock; // Protects the receiver loss list (access: CRcvQueue::worker, CUDT::tsbpd)
1199 mutable sync::Mutex m_StatsLock; // used to synchronize access to trace statistics
1200
1201 void initSynch();
1202 void destroySynch();
1203 void resetAtFork();
1204 void releaseSynch();
1205
1206 private: // Common connection Congestion Control setup
1207 // This can fail only when it failed to create a congctl
1208 // which only may happen when the congctl list is extended
1209 // with user-supplied congctl modules, not a case so far.
1210 SRT_ATR_NODISCARD
1211 SRT_REJECT_REASON setupCC();
1212
1213 // for updateCC it's ok to discard the value. This returns false only if
1214 // the congctl isn't created, and this can be prevented from.
1215 bool updateCC(ETransmissionEvent, const EventVariant arg);
1216
1217 // Failure to create the crypter means that an encrypted
1218 // connection should be rejected if ENFORCEDENCRYPTION is on.
1219 SRT_ATR_NODISCARD SRT_ATTR_REQUIRES(m_ConnectionLock)
1220 bool createCrypter(HandshakeSide side, bool bidi);
1221
1222 private: // Generation and processing of packets
1223 void sendCtrl(UDTMessageType pkttype, const int32_t* lparam = NULL, void* rparam = NULL, int size = 0);
1224
1225 /// Forms and sends ACK packet
1226 /// @note Assumes @ctrlpkt already has a timestamp.
1227 ///
1228 /// @param ctrlpkt A control packet structure to fill. It must have a timestamp already set.
1229 /// @param size Sends lite ACK if size is SEND_LITE_ACK, Full ACK otherwise
1230 ///
1231 /// @returns the number of packets sent.
1232 int sendCtrlAck(CPacket& ctrlpkt, int size);
1233 void sendLossReport(const std::vector< std::pair<int32_t, int32_t> >& losslist);
1234
1235 void processCtrl(const CPacket& ctrlpkt);
1236
1237 /// @brief Process incoming control ACK packet.
1238 /// @param ctrlpkt incoming ACK packet
1239 /// @param currtime current clock time
1240 void processCtrlAck(const CPacket& ctrlpkt, const time_point& currtime);
1241
1242 /// @brief Process incoming control ACKACK packet.
1243 /// @param ctrlpkt incoming ACKACK packet
1244 /// @param tsArrival time when packet has arrived (used to calculate RTT)
1245 void processCtrlAckAck(const CPacket& ctrlpkt, const time_point& tsArrival);
1246
1247 /// @brief Process incoming loss report (NAK) packet.
1248 /// @param ctrlpkt incoming NAK packet
1249 void processCtrlLossReport(const CPacket& ctrlpkt);
1250
1251 /// @brief Process incoming handshake control packet
1252 /// @param ctrlpkt incoming HS packet
1253 void processCtrlHS(const CPacket& ctrlpkt);
1254
1255 /// @brief Process incoming drop request control packet
1256 /// @param ctrlpkt incoming drop request packet
1257 void processCtrlDropReq(const CPacket& ctrlpkt);
1258
1259 /// @brief Process incoming shutdown control packet
1260 void processCtrlShutdown();
1261 /// @brief Process incoming user defined control packet
1262 /// @param ctrlpkt incoming user defined packet
1263 void processCtrlUserDefined(const CPacket& ctrlpkt);
1264
1265 /// @brief Update sender's loss list on an incoming acknowledgement.
1266 /// @param ackdata_seqno sequence number of a data packet being acknowledged
1267 void updateSndLossListOnACK(int32_t ackdata_seqno);
1268
1269 /// Pack a packet from a list of lost packets.
1270 /// @param packet [in, out] a packet structure to fill
1271 /// @return payload size on success, <=0 on failure
1272 int packLostData(CPacket &packet);
1273
1274 std::pair<int32_t, int> getCleanRexmitOffset();
1275 bool checkRexmitRightTime(int offset, const srt::sync::steady_clock::time_point& current_time);
1276 int extractCleanRexmitPacket(int32_t seqno, int offset, CPacket& w_packet,
1277 srt::sync::steady_clock::time_point& w_tsOrigin);
1278
1279 /// Pack a unique data packet (never sent so far) in CPacket for sending.
1280 /// @param packet [in, out] a CPacket structure to fill.
1281 ///
1282 /// @return true if a packet has been packets; false otherwise.
1283 bool packUniqueData(CPacket& packet);
1284
1285 /// Pack in CPacket the next data to be send.
1286 ///
1287 /// @param packet [out] a CPacket structure to fill
1288 /// @param nexttime [out] Time when this socket should be next time picked up for processing.
1289 /// @param src_addr [out] Source address to pass to channel's sendto
1290 ///
1291 /// @retval true A packet was extracted for sending, the socket should be rechecked at @a nexttime
1292 /// @retval false Nothing was extracted for sending, @a nexttime should be ignored
1293 bool packData(CPacket& packet, time_point& nexttime, sockaddr_any& src_addr);
1294
1295 /// Also excludes srt::CUDTUnited::m_GlobControlLock.
1296 SRT_ATTR_EXCLUDES(m_RcvTsbPdStartupLock, m_StatsLock, m_RecvLock, m_RcvLossLock, m_RcvBufferLock)
1297 int processData(CUnit* unit);
1298
1299 /// This function passes the incoming packet to the initial processing
1300 /// (like packet filter) and is about to store it effectively to the
1301 /// receiver buffer and do some postprocessing (decryption) if necessary
1302 /// and report the status thereof.
1303 ///
1304 /// @param incoming [in] The packet coming from the network medium
1305 /// @param w_new_inserted [out] Set false, if the packet already exists, otherwise true (packet added)
1306 /// @param w_was_sent_in_order [out] Set false, if the packet was belated, but had no R flag set.
1307 /// @param w_srt_loss_seqs [out] Gets inserted a loss, if this function has detected it.
1308 ///
1309 /// @return 0 The call was successful (regardless if the packet was accepted or not).
1310 /// @return -1 The call has failed: no space left in the buffer.
1311 /// @return -2 The incoming packet exceeds the expected sequence by more than a length of the buffer (irrepairable discrepancy).
1312 int handleSocketPacketReception(const std::vector<CUnit*>& incoming, bool& w_new_inserted, bool& w_was_sent_in_order, CUDT::loss_seqs_t& w_srt_loss_seqs);
1313
1314 /// Get the packet's TSBPD time -
1315 /// the time when it is passed to the reading application.
1316 /// The @a grp passed by void* is not used yet
1317 /// and shall not be used when ENABLE_BONDING=0.
1318 time_point getPktTsbPdTime(void* grp, const CPacket& packet);
1319
1320 SRT_ATTR_EXCLUDES(m_RcvTsbPdStartupLock)
1321 /// Checks and spawns the TSBPD thread if required.
1322 int checkLazySpawnTsbPdThread();
1323
1324 void processClose();
1325
1326 /// Process the request after receiving the handshake from caller.
1327 /// The @a packet param is passed here as non-const because this function
1328 /// will need to make a temporary back-and-forth endian swap; it doesn't intend to
1329 /// modify the object permanently.
1330 /// @param addr source address from where the request came
1331 /// @param packet contents of the packet
1332 /// @return URQ code, possibly containing reject reason
1333 int processConnectRequest(const sockaddr_any& addr, CPacket& packet);
1334 static void addLossRecord(std::vector<int32_t>& lossrecord, int32_t lo, int32_t hi);
1335 int32_t bake(const sockaddr_any& addr, int32_t previous_cookie = 0, int correction = 0);
1336
1337 void processKeepalive(const CPacket& ctrlpkt, const time_point& tsArrival);
1338
1339
1340 SRT_ATTR_REQUIRES(m_RcvBufferLock)
1341 /// Retrieves the available size of the receiver buffer.
1342 /// Expects that m_RcvBufferLock is locked.
1343 size_t getAvailRcvBufferSizeNoLock() const;
1344
1345 private: // Trace
1346 struct CoreStats
1347 {
1348 time_point tsStartTime; // timestamp when the UDT entity is started
1349 stats::Sender sndr; // sender statistics
1350 stats::Receiver rcvr; // receiver statistics
1351
1352 int64_t m_sndDurationTotal; // total real time for sending
1353
1354 time_point tsLastSampleTime; // last performance sample time
1355 int traceReorderDistance;
1356 double traceBelatedTime;
1357
1358 int64_t sndDuration; // real time for sending
1359 time_point sndDurationCounter; // timers to record the sending Duration
1360
1361 } m_stats;
1362
1363 public:
1364 static const int SELF_CLOCK_INTERVAL = 64; // ACK interval for self-clocking
1365 static const int SEND_LITE_ACK = sizeof(int32_t); // special size for ack containing only ack seq
1366 static const int PACKETPAIR_MASK = 0xF;
1367
1368 private: // Timers functions
1369 #if ENABLE_BONDING
1370 time_point m_tsFreshActivation; // GROUPS: time of fresh activation of the link, or 0 if past the activation phase or idle
1371 time_point m_tsUnstableSince; // GROUPS: time since unexpected ACK delay experienced, or 0 if link seems healthy
1372 time_point m_tsWarySince; // GROUPS: time since an unstable link has first some response
1373 #endif
1374
1375 static const int BECAUSE_NO_REASON = 0, // NO BITS
1376 BECAUSE_ACK = 1 << 0,
1377 BECAUSE_LITEACK = 1 << 1,
1378 BECAUSE_NAKREPORT = 1 << 2,
1379 LAST_BECAUSE_BIT = 3;
1380
1381 void checkTimers();
1382 void considerLegacySrtHandshake(const time_point &timebase);
1383 int checkACKTimer (const time_point& currtime);
1384 int checkNAKTimer(const time_point& currtime);
1385 bool checkExpTimer (const time_point& currtime, int check_reason); // returns true if the connection is expired
1386 void checkRexmitTimer(const time_point& currtime);
1387
1388
1389 private: // for UDP multiplexer
1390 CSndQueue* m_pSndQueue; // packet sending queue
1391 CRcvQueue* m_pRcvQueue; // packet receiving queue
1392 sockaddr_any m_PeerAddr; // peer address
1393 sockaddr_any m_SourceAddr; // override UDP source address with this one when sending
1394 uint32_t m_piSelfIP[4]; // local UDP IP address
1395 CSNode* m_pSNode; // node information for UDT list used in snd queue
1396 CRNode* m_pRNode; // node information for UDT list used in rcv queue
1397
1398 public: // For SrtCongestion
1399 const CSndQueue* sndQueue() { return m_pSndQueue; }
1400 const CRcvQueue* rcvQueue() { return m_pRcvQueue; }
1401
1402 private: // for epoll
1403 std::set<int> m_sPollID; // set of epoll ID to trigger
1404 void addEPoll(const int eid);
1405 void removeEPollEvents(const int eid);
1406 void removeEPollID(const int eid);
1407 };
1408
1409 // DEBUG SUPPORT
1410 HandshakeSide getHandshakeSide(SRTSOCKET s);
1411
1412 } // namespace srt
1413
1414 #endif