Debian Codegraph @

srtcore/queue.cpp

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 05/05/2011
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #include "platform_sys.h"
54
55 #include <cstring>
56
57 #include "common.h"
58 #include "api.h"
59 #include "netinet_any.h"
60 #include "threadname.h"
61 #include "logging.h"
62 #include "queue.h"
63
64 using namespace std;
65 using namespace srt::sync;
66 using namespace srt_logging;
67
68 srt::CUnitQueue::CUnitQueue(int initNumUnits, int mss)
69 : m_iNumTaken(0)
70 , m_iMSS(mss)
71 , m_iBlockSize(initNumUnits)
72 {
73 CQEntry* tempq = allocateEntry(m_iBlockSize, m_iMSS);
74
75 if (tempq == NULL)
76 throw CUDTException(MJ_SYSTEMRES, MN_MEMORY);
77
78 m_pQEntry = m_pCurrQueue = m_pLastQueue = tempq;
79 m_pQEntry->m_pNext = m_pQEntry;
80
81 m_pAvailUnit = m_pCurrQueue->m_pUnit;
82
83 m_iSize = m_iBlockSize;
84 }
85
86 srt::CUnitQueue::~CUnitQueue()
87 {
88 CQEntry* p = m_pQEntry;
89
90 while (p != NULL)
91 {
92 delete[] p->m_pUnit;
93 delete[] p->m_pBuffer;
94
95 CQEntry* q = p;
96 if (p == m_pLastQueue)
97 p = NULL;
98 else
99 p = p->m_pNext;
100 delete q;
101 }
102 }
103
104 srt::CUnitQueue::CQEntry* srt::CUnitQueue::allocateEntry(const int iNumUnits, const int mss)
105 {
106 CQEntry* tempq = NULL;
107 CUnit* tempu = NULL;
108 char* tempb = NULL;
109
110 try
111 {
112 tempq = new CQEntry;
113 tempu = new CUnit[iNumUnits];
114 tempb = new char[iNumUnits * mss];
115 }
116 catch (...)
117 {
118 delete tempq;
119 delete[] tempu;
120 delete[] tempb;
121
122 LOGC(rslog.Error, log << "CUnitQueue: failed to allocate " << iNumUnits << " units.");
123 return NULL;
124 }
125
126 for (int i = 0; i < iNumUnits; ++i)
127 {
128 tempu[i].m_bTaken = false;
129 tempu[i].m_Packet.m_pcData = tempb + i * mss;
130 }
131
132 tempq->m_pUnit = tempu;
133 tempq->m_pBuffer = tempb;
134 tempq->m_iSize = iNumUnits;
135
136 return tempq;
137 }
138
139 int srt::CUnitQueue::increase_()
140 {
141 const int numUnits = m_iBlockSize;
142 HLOGC(qrlog.Debug, log << "CUnitQueue::increase: Capacity" << capacity() << " + " << numUnits << " new units, " << m_iNumTaken << " in use.");
143
144 CQEntry* tempq = allocateEntry(numUnits, m_iMSS);
145 if (tempq == NULL)
146 return -1;
147
148 m_pLastQueue->m_pNext = tempq;
149 m_pLastQueue = tempq;
150 m_pLastQueue->m_pNext = m_pQEntry;
151
152 m_iSize += numUnits;
153
154 return 0;
155 }
156
157 srt::CUnit* srt::CUnitQueue::getNextAvailUnit()
158 {
159 const int iNumUnitsTotal = capacity();
160 if (m_iNumTaken * 10 > iNumUnitsTotal * 9) // 90% or more are in use.
161 increase_();
162
163 if (m_iNumTaken >= capacity())
164 {
165 LOGC(qrlog.Error, log << "CUnitQueue: No free units to take. Capacity" << capacity() << ".");
166 return NULL;
167 }
168
169 int units_checked = 0;
170 do
171 {
172 const CUnit* end = m_pCurrQueue->m_pUnit + m_pCurrQueue->m_iSize;
173 for (; m_pAvailUnit != end; ++m_pAvailUnit, ++units_checked)
174 {
175 if (!m_pAvailUnit->m_bTaken)
176 {
177 return m_pAvailUnit;
178 }
179 }
180
181 m_pCurrQueue = m_pCurrQueue->m_pNext;
182 m_pAvailUnit = m_pCurrQueue->m_pUnit;
183 } while (units_checked < m_iSize);
184
185 return NULL;
186 }
187
188 void srt::CUnitQueue::makeUnitFree(CUnit* unit)
189 {
190 SRT_ASSERT(unit != NULL);
191 SRT_ASSERT(unit->m_bTaken);
192 unit->m_bTaken.store(false);
193
194 --m_iNumTaken;
195 }
196
197 void srt::CUnitQueue::makeUnitTaken(CUnit* unit)
198 {
199 ++m_iNumTaken;
200
201 SRT_ASSERT(unit != NULL);
202 SRT_ASSERT(!unit->m_bTaken);
203 unit->m_bTaken.store(true);
204 }
205
206 srt::CSndUList::CSndUList(sync::CTimer* pTimer)
207 : m_pHeap(NULL)
208 , m_iArrayLength(512)
209 , m_iLastEntry(-1)
210 , m_ListLock()
211 , m_pTimer(pTimer)
212 {
213 setupCond(m_ListCond, "CSndUListCond");
214 m_pHeap = new CSNode*[m_iArrayLength];
215 }
216
217 srt::CSndUList::~CSndUList()
218 {
219 releaseCond(m_ListCond);
220 delete[] m_pHeap;
221 }
222
223 void srt::CSndUList::resetAtFork()
224 {
225 resetCond(m_ListCond);
226 }
227
228 void srt::CSndUList::update(const CUDT* u, EReschedule reschedule, sync::steady_clock::time_point ts)
229 {
230 ScopedLock listguard(m_ListLock);
231
232 CSNode* n = u->m_pSNode;
233
234 if (n->m_iHeapLoc >= 0)
235 {
236 if (reschedule == DONT_RESCHEDULE)
237 return;
238
239 if (n->m_tsTimeStamp <= ts)
240 return;
241
242 if (n->m_iHeapLoc == 0)
243 {
244 n->m_tsTimeStamp = ts;
245 m_pTimer->interrupt();
246 return;
247 }
248
249 remove_(u);
250 insert_norealloc_(ts, u);
251 return;
252 }
253
254 insert_(ts, u);
255 }
256
257 srt::CUDT* srt::CSndUList::pop()
258 {
259 ScopedLock listguard(m_ListLock);
260
261 if (-1 == m_iLastEntry)
262 return NULL;
263
264 // no pop until the next scheduled time
265 if (m_pHeap[0]->m_tsTimeStamp > steady_clock::now())
266 return NULL;
267
268 CUDT* u = m_pHeap[0]->m_pUDT;
269 remove_(u);
270 return u;
271 }
272
273 void srt::CSndUList::remove(const CUDT* u)
274 {
275 ScopedLock listguard(m_ListLock);
276 remove_(u);
277 }
278
279 steady_clock::time_point srt::CSndUList::getNextProcTime()
280 {
281 ScopedLock listguard(m_ListLock);
282
283 if (-1 == m_iLastEntry)
284 return steady_clock::time_point();
285
286 return m_pHeap[0]->m_tsTimeStamp;
287 }
288
289 void srt::CSndUList::waitNonEmpty() const
290 {
291 UniqueLock listguard(m_ListLock);
292 if (m_iLastEntry >= 0)
293 return;
294
295 m_ListCond.wait(listguard);
296 }
297
298 void srt::CSndUList::signalInterrupt() const
299 {
300 ScopedLock listguard(m_ListLock);
301 m_ListCond.notify_one();
302 }
303
304 void srt::CSndUList::realloc_()
305 {
306 CSNode** temp = NULL;
307
308 try
309 {
310 temp = new CSNode*[2 * m_iArrayLength];
311 }
312 catch (...)
313 {
314 throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0);
315 }
316
317 memcpy((temp), m_pHeap, sizeof(CSNode*) * m_iArrayLength);
318 m_iArrayLength *= 2;
319 delete[] m_pHeap;
320 m_pHeap = temp;
321 }
322
323 void srt::CSndUList::insert_(const steady_clock::time_point& ts, const CUDT* u)
324 {
325 // increase the heap array size if necessary
326 if (m_iLastEntry == m_iArrayLength - 1)
327 realloc_();
328
329 insert_norealloc_(ts, u);
330 }
331
332 void srt::CSndUList::insert_norealloc_(const steady_clock::time_point& ts, const CUDT* u)
333 {
334 CSNode* n = u->m_pSNode;
335
336 // do not insert repeated node
337 if (n->m_iHeapLoc >= 0)
338 return;
339
340 SRT_ASSERT(m_iLastEntry < m_iArrayLength);
341
342 m_iLastEntry++;
343 m_pHeap[m_iLastEntry] = n;
344 n->m_tsTimeStamp = ts;
345
346 int q = m_iLastEntry;
347 int p = q;
348 while (p != 0)
349 {
350 p = (q - 1) >> 1;
351 if (m_pHeap[p]->m_tsTimeStamp <= m_pHeap[q]->m_tsTimeStamp)
352 break;
353
354 swap(m_pHeap[p], m_pHeap[q]);
355 m_pHeap[q]->m_iHeapLoc = q;
356 q = p;
357 }
358
359 n->m_iHeapLoc = q;
360
361 // an earlier event has been inserted, wake up sending worker
362 if (n->m_iHeapLoc == 0)
363 m_pTimer->interrupt();
364
365 // first entry, activate the sending queue
366 if (0 == m_iLastEntry)
367 {
368 // m_ListLock is assumed to be locked.
369 m_ListCond.notify_one();
370 }
371 }
372
373 void srt::CSndUList::remove_(const CUDT* u)
374 {
375 CSNode* n = u->m_pSNode;
376
377 if (n->m_iHeapLoc >= 0)
378 {
379 // remove the node from heap
380 m_pHeap[n->m_iHeapLoc] = m_pHeap[m_iLastEntry];
381 m_iLastEntry--;
382 m_pHeap[n->m_iHeapLoc]->m_iHeapLoc = n->m_iHeapLoc.load();
383
384 int q = n->m_iHeapLoc;
385 int p = q * 2 + 1;
386 while (p <= m_iLastEntry)
387 {
388 if ((p + 1 <= m_iLastEntry) && (m_pHeap[p]->m_tsTimeStamp > m_pHeap[p + 1]->m_tsTimeStamp))
389 p++;
390
391 if (m_pHeap[q]->m_tsTimeStamp > m_pHeap[p]->m_tsTimeStamp)
392 {
393 swap(m_pHeap[p], m_pHeap[q]);
394 m_pHeap[p]->m_iHeapLoc = p;
395 m_pHeap[q]->m_iHeapLoc = q;
396
397 q = p;
398 p = q * 2 + 1;
399 }
400 else
401 break;
402 }
403
404 n->m_iHeapLoc = -1;
405 }
406
407 // the only event has been deleted, wake up immediately
408 if (0 == m_iLastEntry)
409 m_pTimer->interrupt();
410 }
411
412 //
413 srt::CSndQueue::CSndQueue()
414 : m_pSndUList(NULL)
415 , m_pChannel(NULL)
416 , m_pTimer(NULL)
417 , m_bClosing(false)
418 {
419 }
420
421 srt::CSndQueue::~CSndQueue()
422 {
423 delete m_pSndUList;
424 }
425
426 void srt::CSndQueue::resetAtFork()
427 {
428 resetThread(&m_WorkerThread);
429 m_pSndUList->resetAtFork();
430 }
431
432 void srt::CSndQueue::stop()
433 {
434 m_bClosing = true;
435
436 if (m_pTimer != NULL)
437 {
438 m_pTimer->interrupt();
439 }
440
441 // Unblock CSndQueue worker thread if it is waiting.
442 m_pSndUList->signalInterrupt();
443
444 if (m_WorkerThread.joinable())
445 {
446 HLOGC(rslog.Debug, log << "SndQueue: EXIT");
447 m_WorkerThread.join();
448 }
449 }
450
451 int srt::CSndQueue::ioctlQuery(int type) const
452 {
453 return m_pChannel->ioctlQuery(type);
454 }
455 int srt::CSndQueue::sockoptQuery(int level, int type) const
456 {
457 return m_pChannel->sockoptQuery(level, type);
458 }
459
460 #if ENABLE_LOGGING
461 srt::sync::atomic<int> srt::CSndQueue::m_counter(0);
462 #endif
463
464 void srt::CSndQueue::init(CChannel* c, CTimer* t)
465 {
466 m_pChannel = c;
467 m_pTimer = t;
468 m_pSndUList = new CSndUList(t);
469
470 #if ENABLE_LOGGING
471 ++m_counter;
472 const std::string thrname = "SRT:SndQ:w" + Sprint(m_counter);
473 const char* thname = thrname.c_str();
474 #else
475 const char* thname = "SRT:SndQ";
476 #endif
477 if (!StartThread(m_WorkerThread, CSndQueue::worker, this, thname))
478 throw CUDTException(MJ_SYSTEMRES, MN_THREAD);
479 }
480
481 int srt::CSndQueue::getIpTTL() const
482 {
483 return m_pChannel ? m_pChannel->getIpTTL() : -1;
484 }
485
486 int srt::CSndQueue::getIpToS() const
487 {
488 return m_pChannel ? m_pChannel->getIpToS() : -1;
489 }
490
491 #ifdef SRT_ENABLE_BINDTODEVICE
492 bool srt::CSndQueue::getBind(char* dst, size_t len) const
493 {
494 return m_pChannel ? m_pChannel->getBind(dst, len) : false;
495 }
496 #endif
497
498 #if defined(SRT_DEBUG_SNDQ_HIGHRATE)
499 static void CSndQueueDebugHighratePrint(const srt::CSndQueue* self, const steady_clock::time_point currtime)
500 {
501 if (self->m_DbgTime <= currtime)
502 {
503 fprintf(stdout,
504 "SndQueue %lu slt:%lu nrp:%lu snt:%lu nrt:%lu ctw:%lu\n",
505 self->m_WorkerStats.lIteration,
506 self->m_WorkerStats.lSleepTo,
507 self->m_WorkerStats.lNotReadyPop,
508 self->m_WorkerStats.lSendTo,
509 self->m_WorkerStats.lNotReadyTs,
510 self->m_WorkerStats.lCondWait);
511 memset(&self->m_WorkerStats, 0, sizeof(self->m_WorkerStats));
512 self->m_DbgTime = currtime + self->m_DbgPeriod;
513 }
514 }
515 #endif
516
517 void* srt::CSndQueue::worker(void* param)
518 {
519 CSndQueue* self = (CSndQueue*)param;
520
521 std::string thname;
522 ThreadName::get(thname);
523 THREAD_STATE_INIT(thname.c_str());
524
525 #if defined(SRT_DEBUG_SNDQ_HIGHRATE)
526 #define IF_DEBUG_HIGHRATE(statement) statement
527 self->m_DbgTime = sync::steady_clock::now();
528 self->m_DbgPeriod = sync::microseconds_from(5000000);
529 self->m_DbgTime += self->m_DbgPeriod;
530 #else
531 #define IF_DEBUG_HIGHRATE(statement) (void)0
532 #endif /* SRT_DEBUG_SNDQ_HIGHRATE */
533
534 while (!self->m_bClosing)
535 {
536 const steady_clock::time_point next_time = self->m_pSndUList->getNextProcTime();
537
538 INCREMENT_THREAD_ITERATIONS();
539
540 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lIteration++);
541
542 if (is_zero(next_time))
543 {
544 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lNotReadyTs++);
545
546 // wait here if there is no sockets with data to be sent
547 THREAD_PAUSED();
548 if (!self->m_bClosing)
549 {
550 self->m_pSndUList->waitNonEmpty();
551 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lCondWait++);
552 }
553 THREAD_RESUMED();
554
555 continue;
556 }
557
558 // wait until next processing time of the first socket on the list
559 const steady_clock::time_point currtime = steady_clock::now();
560
561 IF_DEBUG_HIGHRATE(CSndQueueDebugHighratePrint(self, currtime));
562 if (currtime < next_time)
563 {
564 THREAD_PAUSED();
565 self->m_pTimer->sleep_until(next_time);
566 THREAD_RESUMED();
567 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lSleepTo++);
568 }
569
570 // Get a socket with a send request if any.
571 CUDT* u = self->m_pSndUList->pop();
572 if (u == NULL)
573 {
574 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lNotReadyPop++);
575 continue;
576 }
577
578 #define UST(field) ((u->m_b##field) ? "+" : "-") << #field << " "
579 HLOGC(qslog.Debug,
580 log << "CSndQueue: requesting packet from @" << u->socketID() << " STATUS: " << UST(Listening)
581 << UST(Connecting) << UST(Connected) << UST(Closing) << UST(Shutdown) << UST(Broken) << UST(PeerHealth)
582 << UST(Opened));
583 #undef UST
584
585 if (!u->m_bConnected || u->m_bBroken)
586 {
587 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lNotReadyPop++);
588 continue;
589 }
590
591 CUDTUnited::SocketKeeper sk (CUDT::uglobal(), u->id());
592 if (!sk.socket)
593 {
594 HLOGC(qslog.Debug, log << "Socket to be processed was deleted in the meantime, not packing");
595 continue;
596 }
597
598 // pack a packet from the socket
599 CPacket pkt;
600 steady_clock::time_point next_send_time;
601 sockaddr_any source_addr;
602 const bool res = u->packData((pkt), (next_send_time), (source_addr));
603
604 // Check if extracted anything to send
605 if (res == false)
606 {
607 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lNotReadyPop++);
608 continue;
609 }
610
611 const sockaddr_any addr = u->m_PeerAddr;
612 if (!is_zero(next_send_time))
613 self->m_pSndUList->update(u, CSndUList::DO_RESCHEDULE, next_send_time);
614
615 HLOGC(qslog.Debug, log << self->CONID() << "chn:SENDING: " << pkt.Info());
616 self->m_pChannel->sendto(addr, pkt, source_addr);
617
618 IF_DEBUG_HIGHRATE(self->m_WorkerStats.lSendTo++);
619 }
620
621 THREAD_EXIT();
622 return NULL;
623 }
624
625 int srt::CSndQueue::sendto(const sockaddr_any& addr, CPacket& w_packet, const sockaddr_any& src)
626 {
627 // send out the packet immediately (high priority), this is a control packet
628 // NOTE: w_packet is passed by mutable reference because this function will do
629 // a modification in place and then it will revert it. After returning this object
630 // should look unmodified, hence it is here passed without a reference marker.
631 m_pChannel->sendto(addr, w_packet, src);
632 return (int)w_packet.getLength();
633 }
634
635 //
636 srt::CRcvUList::CRcvUList()
637 : m_pUList(NULL)
638 , m_pLast(NULL)
639 {
640 }
641
642 srt::CRcvUList::~CRcvUList() {}
643
644 void srt::CRcvUList::insert(const CUDT* u)
645 {
646 CRNode* n = u->m_pRNode;
647 n->m_tsTimeStamp = steady_clock::now();
648
649 if (NULL == m_pUList)
650 {
651 // empty list, insert as the single node
652 n->m_pPrev = n->m_pNext = NULL;
653 m_pLast = m_pUList = n;
654
655 return;
656 }
657
658 // always insert at the end for RcvUList
659 n->m_pPrev = m_pLast;
660 n->m_pNext = NULL;
661 m_pLast->m_pNext = n;
662 m_pLast = n;
663 }
664
665 void srt::CRcvUList::remove(const CUDT* u)
666 {
667 CRNode* n = u->m_pRNode;
668
669 if (!n->m_bOnList)
670 return;
671
672 if (NULL == n->m_pPrev)
673 {
674 // n is the first node
675 m_pUList = n->m_pNext;
676 if (NULL == m_pUList)
677 m_pLast = NULL;
678 else
679 m_pUList->m_pPrev = NULL;
680 }
681 else
682 {
683 n->m_pPrev->m_pNext = n->m_pNext;
684 if (NULL == n->m_pNext)
685 {
686 // n is the last node
687 m_pLast = n->m_pPrev;
688 }
689 else
690 n->m_pNext->m_pPrev = n->m_pPrev;
691 }
692
693 n->m_pNext = n->m_pPrev = NULL;
694 }
695
696 void srt::CRcvUList::update(const CUDT* u)
697 {
698 CRNode* n = u->m_pRNode;
699
700 if (!n->m_bOnList)
701 return;
702
703 n->m_tsTimeStamp = steady_clock::now();
704
705 // if n is the last node, do not need to change
706 if (NULL == n->m_pNext)
707 return;
708
709 if (NULL == n->m_pPrev)
710 {
711 m_pUList = n->m_pNext;
712 m_pUList->m_pPrev = NULL;
713 }
714 else
715 {
716 n->m_pPrev->m_pNext = n->m_pNext;
717 n->m_pNext->m_pPrev = n->m_pPrev;
718 }
719
720 n->m_pPrev = m_pLast;
721 n->m_pNext = NULL;
722 m_pLast->m_pNext = n;
723 m_pLast = n;
724 }
725
726 //
727 srt::CHash::CHash()
728 : m_pBucket(NULL)
729 , m_iHashSize(0)
730 {
731 }
732
733 srt::CHash::~CHash()
734 {
735 for (int i = 0; i < m_iHashSize; ++i)
736 {
737 CBucket* b = m_pBucket[i];
738 while (NULL != b)
739 {
740 CBucket* n = b->m_pNext;
741 delete b;
742 b = n;
743 }
744 }
745
746 delete[] m_pBucket;
747 }
748
749 void srt::CHash::init(int size)
750 {
751 m_pBucket = new CBucket*[size];
752
753 for (int i = 0; i < size; ++i)
754 m_pBucket[i] = NULL;
755
756 m_iHashSize = size;
757 }
758
759 srt::CUDT* srt::CHash::lookup(int32_t id)
760 {
761 // simple hash function (% hash table size); suitable for socket descriptors
762 CBucket* b = m_pBucket[id % m_iHashSize];
763
764 while (NULL != b)
765 {
766 if (id == b->m_iID)
767 return b->m_pUDT;
768 b = b->m_pNext;
769 }
770
771 return NULL;
772 }
773
774 void srt::CHash::insert(int32_t id, CUDT* u)
775 {
776 CBucket* b = m_pBucket[id % m_iHashSize];
777
778 CBucket* n = new CBucket;
779 n->m_iID = id;
780 n->m_pUDT = u;
781 n->m_pNext = b;
782
783 m_pBucket[id % m_iHashSize] = n;
784 }
785
786 void srt::CHash::remove(int32_t id)
787 {
788 CBucket* b = m_pBucket[id % m_iHashSize];
789 CBucket* p = NULL;
790
791 while (NULL != b)
792 {
793 if (id == b->m_iID)
794 {
795 if (NULL == p)
796 m_pBucket[id % m_iHashSize] = b->m_pNext;
797 else
798 p->m_pNext = b->m_pNext;
799
800 delete b;
801
802 return;
803 }
804
805 p = b;
806 b = b->m_pNext;
807 }
808 }
809
810 //
811 srt::CRendezvousQueue::CRendezvousQueue()
812 : m_lRendezvousID()
813 , m_RIDListLock()
814 {
815 }
816
817 srt::CRendezvousQueue::~CRendezvousQueue()
818 {
819 m_lRendezvousID.clear();
820 }
821
822 void srt::CRendezvousQueue::insert(const SRTSOCKET& id,
823 CUDT* u,
824 const sockaddr_any& addr,
825 const steady_clock::time_point& ttl)
826 {
827 ScopedLock vg(m_RIDListLock);
828
829 CRL r;
830 r.m_iID = id;
831 r.m_pUDT = u;
832 r.m_PeerAddr = addr;
833 r.m_tsTTL = ttl;
834
835 m_lRendezvousID.push_back(r);
836 HLOGC(cnlog.Debug,
837 log << "RID: adding socket @" << id << " for address: " << addr.str() << " expires: " << FormatTime(ttl)
838 << " (total connectors: " << m_lRendezvousID.size() << ")");
839 }
840
841 void srt::CRendezvousQueue::remove(const SRTSOCKET& id)
842 {
843 ScopedLock lkv(m_RIDListLock);
844
845 for (list<CRL>::iterator i = m_lRendezvousID.begin(); i != m_lRendezvousID.end(); ++i)
846 {
847 if (i->m_iID == id)
848 {
849 m_lRendezvousID.erase(i);
850 break;
851 }
852 }
853 }
854
855 srt::CUDT* srt::CRendezvousQueue::retrieve(const sockaddr_any& addr, SRTSOCKET& w_id) const
856 {
857 ScopedLock vg(m_RIDListLock);
858
859 IF_HEAVY_LOGGING(const char* const id_type = w_id ? "THIS ID" : "A NEW CONNECTION");
860
861 // TODO: optimize search
862 for (list<CRL>::const_iterator i = m_lRendezvousID.begin(); i != m_lRendezvousID.end(); ++i)
863 {
864 if (i->m_PeerAddr == addr && ((w_id == 0) || (w_id == i->m_iID)))
865 {
866 // This procedure doesn't exactly respond to the original UDT idea.
867 // As the "rendezvous queue" is used for both handling rendezvous and
868 // the caller sockets in the non-blocking mode (for blocking mode the
869 // entire handshake procedure is handled in a loop-style in CUDT::startConnect),
870 // the RID list should give up a socket entity in the following cases:
871 // 1. For THE SAME id as passed in w_id, respond always, as per a caller
872 // socket that is currently trying to connect and is managed with
873 // HS roundtrips in an event-style. Same for rendezvous.
874 // 2. For the "connection request" ID=0 the found socket should be given up
875 // ONLY IF it is rendezvous. Normally ID=0 is only for listener as a
876 // connection request. But if there was a listener, then this function
877 // wouldn't even be called, as this case would be handled before trying
878 // to call this function.
879 //
880 // This means: if an incoming ID is 0, then this search should succeed ONLY
881 // IF THE FOUND SOCKET WAS RENDEZVOUS.
882
883 if (!w_id && !i->m_pUDT->m_config.bRendezvous)
884 {
885 HLOGC(cnlog.Debug,
886 log << "RID: found id @" << i->m_iID << " while looking for "
887 << id_type << " FROM " << i->m_PeerAddr.str()
888 << ", but it's NOT RENDEZVOUS, skipping");
889 continue;
890 }
891
892 HLOGC(cnlog.Debug,
893 log << "RID: found id @" << i->m_iID << " while looking for "
894 << id_type << " FROM " << i->m_PeerAddr.str());
895 w_id = i->m_iID;
896 return i->m_pUDT;
897 }
898 }
899
900 #if ENABLE_HEAVY_LOGGING
901 std::ostringstream spec;
902 if (w_id == 0)
903 spec << "A NEW CONNECTION REQUEST";
904 else
905 spec << " AGENT @" << w_id;
906 HLOGC(cnlog.Debug,
907 log << "RID: NO CONNECTOR FOR ADR:" << addr.str() << " while looking for " << spec.str() << " ("
908 << m_lRendezvousID.size() << " connectors total)");
909 #endif
910
911 return NULL;
912 }
913
914 void srt::CRendezvousQueue::updateConnStatus(EReadStatus rst, EConnectStatus cst, CUnit* unit)
915 {
916 vector<LinkStatusInfo> toRemove, toProcess;
917
918 const CPacket* pkt = unit ? &unit->m_Packet : NULL;
919
920 // Need a stub value for a case when there's no unit provided ("storage depleted" case).
921 // It should be normally NOT IN USE because in case of "storage depleted", rst != RST_OK.
922 const SRTSOCKET dest_id = pkt ? pkt->id() : 0;
923
924 // If no socket were qualified for further handling, finish here.
925 // Otherwise toRemove and toProcess contain items to handle.
926 if (!qualifyToHandle(rst, cst, dest_id, (toRemove), (toProcess)))
927 return;
928
929 HLOGC(cnlog.Debug,
930 log << "updateConnStatus: collected " << toProcess.size() << " for processing, " << toRemove.size()
931 << " to close");
932
933 // Repeat (resend) connection request.
934 for (vector<LinkStatusInfo>::iterator i = toProcess.begin(); i != toProcess.end(); ++i)
935 {
936 // IMPORTANT INFORMATION concerning changes towards UDT legacy.
937 // In the UDT code there was no attempt to interpret any incoming data.
938 // All data from the incoming packet were considered to be already deployed into
939 // m_ConnRes field, and m_ConnReq field was considered at this time accordingly updated.
940 // Therefore this procedure did only one thing: craft a new handshake packet and send it.
941 // In SRT this may also interpret extra data (extensions in case when Agent is Responder)
942 // and the `pktIn` packet may sometimes contain no data. Therefore the passed `rst`
943 // must be checked to distinguish the call by periodic update (RST_AGAIN) from a call
944 // due to have received the packet (RST_OK).
945 //
946 // In the below call, only the underlying `processRendezvous` function will be attempting
947 // to interpret these data (for caller-listener this was already done by `processConnectRequest`
948 // before calling this function), and it checks for the data presence.
949
950 EReadStatus read_st = rst;
951 EConnectStatus conn_st = cst;
952
953 CUDTUnited::SocketKeeper sk (CUDT::uglobal(), i->id);
954 if (!sk.socket)
955 {
956 // Socket deleted already, so stop this and proceed to the next loop.
957 LOGC(cnlog.Error, log << "updateConnStatus: IPE: socket @" << i->id << " already closed, proceed to only removal from lists");
958 toRemove.push_back(*i);
959 continue;
960 }
961
962
963 if (cst != CONN_RENDEZVOUS && dest_id != 0)
964 {
965 if (i->id != dest_id)
966 {
967 HLOGC(cnlog.Debug, log << "updateConnStatus: cst=" << ConnectStatusStr(cst) << " but for RID @" << i->id
968 << " dest_id=@" << dest_id << " - resetting to AGAIN");
969
970 read_st = RST_AGAIN;
971 conn_st = CONN_AGAIN;
972 }
973 else
974 {
975 HLOGC(cnlog.Debug, log << "updateConnStatus: cst=" << ConnectStatusStr(cst) << " for @"
976 << i->id);
977 }
978 }
979 else
980 {
981 HLOGC(cnlog.Debug, log << "updateConnStatus: cst=" << ConnectStatusStr(cst) << " and dest_id=@" << dest_id
982 << " - NOT checking against RID @" << i->id);
983 }
984
985 HLOGC(cnlog.Debug,
986 log << "updateConnStatus: processing async conn for @" << i->id << " FROM " << i->peeraddr.str());
987
988 if (!i->u->processAsyncConnectRequest(read_st, conn_st, pkt, i->peeraddr))
989 {
990 // cst == CONN_REJECT can only be result of worker_ProcessAddressedPacket and
991 // its already set in this case.
992 LinkStatusInfo fi = *i;
993 fi.errorcode = SRT_ECONNREJ;
994 toRemove.push_back(fi);
995 i->u->sendCtrl(UMSG_SHUTDOWN);
996 }
997 }
998
999 // NOTE: it is "believed" here that all CUDT objects will not be
1000 // deleted in the meantime. This is based on a statement that at worst
1001 // they have been "just" declared failed and it will pass at least 1s until
1002 // they are moved to ClosedSockets and it is believed that this function will
1003 // not be held on mutexes that long.
1004
1005 for (vector<LinkStatusInfo>::iterator i = toRemove.begin(); i != toRemove.end(); ++i)
1006 {
1007 HLOGC(cnlog.Debug, log << "updateConnStatus: COMPLETING dep objects update on failed @" << i->id);
1008 remove(i->id);
1009
1010 CUDTUnited::SocketKeeper sk (CUDT::uglobal(), i->id);
1011 if (!sk.socket)
1012 {
1013 // This actually shall never happen, so it's a kind of paranoid check.
1014 LOGC(cnlog.Error, log << "updateConnStatus: IPE: socket @" << i->id << " already closed, NOT ACCESSING its contents");
1015 continue;
1016 }
1017
1018 // Setting m_bConnecting to false, and need to remove the socket from the rendezvous queue
1019 // because the next CUDT::close will not remove it from the queue when m_bConnecting = false,
1020 // and may crash on next pass.
1021 //
1022 // TODO: maybe lock i->u->m_ConnectionLock?
1023 i->u->m_bConnecting = false;
1024
1025 // DO NOT close the socket here because in this case it might be
1026 // unable to get status from at the right moment. Also only member
1027 // sockets should be taken care of internally - single sockets should
1028 // be normally closed by the application, after it is done with them.
1029
1030 // app can call any UDT API to learn the connection_broken error
1031 CUDT::uglobal().m_EPoll.update_events(
1032 i->u->m_SocketID, i->u->m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR, true);
1033
1034 // Make sure that the socket wasn't deleted in the meantime.
1035 // Skip this part if it was. Note also that if the socket was
1036 // decided to be deleted, it's already moved to m_ClosedSockets
1037 // and should have been therefore already processed for deletion.
1038
1039 i->u->completeBrokenConnectionDependencies(i->errorcode);
1040 }
1041
1042 {
1043 // Now, additionally for every failed link reset the TTL so that
1044 // they are set expired right now.
1045 ScopedLock vg(m_RIDListLock);
1046 for (list<CRL>::iterator i = m_lRendezvousID.begin(); i != m_lRendezvousID.end(); ++i)
1047 {
1048 if (find_if(toRemove.begin(), toRemove.end(), LinkStatusInfo::HasID(i->m_iID)) != toRemove.end())
1049 {
1050 LOGC(cnlog.Error,
1051 log << "updateConnStatus: processAsyncConnectRequest FAILED on @" << i->m_iID
1052 << ". Setting TTL as EXPIRED.");
1053 i->m_tsTTL =
1054 steady_clock::time_point(); // Make it expire right now, will be picked up at the next iteration
1055 }
1056 }
1057 }
1058 }
1059
1060 bool srt::CRendezvousQueue::qualifyToHandle(EReadStatus rst,
1061 EConnectStatus cst SRT_ATR_UNUSED,
1062 int iDstSockID,
1063 vector<LinkStatusInfo>& toRemove,
1064 vector<LinkStatusInfo>& toProcess)
1065 {
1066 ScopedLock vg(m_RIDListLock);
1067
1068 if (m_lRendezvousID.empty())
1069 return false; // nothing to process.
1070
1071 HLOGC(cnlog.Debug,
1072 log << "updateConnStatus: updating after getting pkt with DST socket ID @" << iDstSockID
1073 << " status: " << ConnectStatusStr(cst));
1074
1075 for (list<CRL>::iterator i = m_lRendezvousID.begin(), i_next = i; i != m_lRendezvousID.end(); i = i_next)
1076 {
1077 // Safe iterator to the next element. If the current element is erased, the iterator is updated again.
1078 ++i_next;
1079
1080 const steady_clock::time_point tsNow = steady_clock::now();
1081
1082 if (tsNow >= i->m_tsTTL)
1083 {
1084 HLOGC(cnlog.Debug,
1085 log << "RID: socket @" << i->m_iID
1086 << " removed - EXPIRED ("
1087 // The "enforced on FAILURE" is below when processAsyncConnectRequest failed.
1088 << (is_zero(i->m_tsTTL) ? "enforced on FAILURE" : "passed TTL") << "). WILL REMOVE from queue.");
1089
1090 // Set appropriate error information, but do not update yet.
1091 // Exit the lock first. Collect objects to update them later.
1092 int ccerror = SRT_ECONNREJ;
1093 if (i->m_pUDT->m_RejectReason == SRT_REJ_UNKNOWN)
1094 {
1095 if (!is_zero(i->m_tsTTL))
1096 {
1097 // Timer expired, set TIMEOUT forcefully
1098 i->m_pUDT->m_RejectReason = SRT_REJ_TIMEOUT;
1099 ccerror = SRT_ENOSERVER;
1100 }
1101 else
1102 {
1103 // In case of unknown reason, rejection should at least
1104 // suggest error on the peer
1105 i->m_pUDT->m_RejectReason = SRT_REJ_PEER;
1106 }
1107 }
1108
1109 // The call to completeBrokenConnectionDependencies() cannot happen here
1110 // under the lock of m_RIDListLock as it risks a deadlock.
1111 // Collect in 'toRemove' to update later.
1112 LinkStatusInfo fi = {i->m_pUDT, i->m_iID, ccerror, i->m_PeerAddr, -1};
1113 toRemove.push_back(fi);
1114
1115 // i_next was preincremented, but this is guaranteed to point to
1116 // the element next to erased one.
1117 i_next = m_lRendezvousID.erase(i);
1118 continue;
1119 }
1120 else
1121 {
1122 HLOGC(cnlog.Debug,
1123 log << "RID: socket @" << i->m_iID << " still active (remaining " << std::fixed
1124 << (count_microseconds(i->m_tsTTL - tsNow) / 1000000.0) << "s of TTL)...");
1125 }
1126
1127 const steady_clock::time_point tsLastReq = i->m_pUDT->m_tsLastReqTime;
1128 const steady_clock::time_point tsRepeat =
1129 tsLastReq + milliseconds_from(250); // Repeat connection request (send HS).
1130
1131 // A connection request is repeated every 250 ms if there was no response from the peer:
1132 // - RST_AGAIN means no packet was received over UDP.
1133 // - a packet was received, but not for THIS socket.
1134 if ((rst == RST_AGAIN || i->m_iID != iDstSockID) && tsNow <= tsRepeat)
1135 {
1136 HLOGC(cnlog.Debug,
1137 log << "RID:@" << i->m_iID << " " << FormatDuration<DUNIT_MS>(tsNow - tsLastReq)
1138 << " passed since last connection request.");
1139
1140 continue;
1141 }
1142
1143 HLOGC(cnlog.Debug,
1144 log << "RID:@" << i->m_iID << " cst=" << ConnectStatusStr(cst) << " -- repeating connection request.");
1145
1146 // This queue is used only in case of Async mode (rendezvous or caller-listener).
1147 // Synchronous connection requests are handled in startConnect() completely.
1148 if (!i->m_pUDT->m_config.bSynRecving)
1149 {
1150 // Collect them so that they can be updated out of m_RIDListLock.
1151 LinkStatusInfo fi = {i->m_pUDT, i->m_iID, SRT_SUCCESS, i->m_PeerAddr, -1};
1152 toProcess.push_back(fi);
1153 }
1154 else
1155 {
1156 HLOGC(cnlog.Debug, log << "RID: socket @" << i->m_iID << " is SYNCHRONOUS, NOT UPDATING");
1157 }
1158 }
1159
1160 return !toRemove.empty() || !toProcess.empty();
1161 }
1162
1163 //
1164 srt::CRcvQueue::CRcvQueue()
1165 : m_WorkerThread()
1166 , m_pUnitQueue(NULL)
1167 , m_pRcvUList(NULL)
1168 , m_pHash(NULL)
1169 , m_pChannel(NULL)
1170 , m_pTimer(NULL)
1171 , m_iIPversion()
1172 , m_szPayloadSize()
1173 , m_bClosing(false)
1174 , m_pRendezvousQueue(NULL)
1175 , m_vNewEntry()
1176 , m_IDLock()
1177 , m_mBuffer()
1178 , m_BufferCond()
1179 {
1180 setupCond(m_BufferCond, "QueueBuffer");
1181 }
1182
1183 srt::CRcvQueue::~CRcvQueue()
1184 {
1185 delete m_pUnitQueue;
1186 delete m_pRcvUList;
1187 delete m_pHash;
1188 delete m_pRendezvousQueue;
1189
1190 // remove all queued messages
1191 for (map<int32_t, std::queue<CPacket*> >::iterator i = m_mBuffer.begin(); i != m_mBuffer.end(); ++i)
1192 {
1193 while (!i->second.empty())
1194 {
1195 CPacket* pkt = i->second.front();
1196 delete pkt;
1197 i->second.pop();
1198 }
1199 }
1200 }
1201
1202 void srt::CRcvQueue::resetAtFork()
1203 {
1204 resetThread(&m_WorkerThread);
1205 }
1206
1207 void srt::CRcvQueue::stop()
1208 {
1209 m_bClosing = true;
1210
1211 if (m_WorkerThread.joinable())
1212 {
1213 HLOGC(rslog.Debug, log << "RcvQueue: EXIT");
1214 m_WorkerThread.join();
1215 }
1216 releaseCond(m_BufferCond);
1217 }
1218
1219
1220 #if ENABLE_LOGGING
1221 srt::sync::atomic<int> srt::CRcvQueue::m_counter(0);
1222 #endif
1223
1224 void srt::CRcvQueue::init(int qsize, size_t payload, int version, int hsize, CChannel* cc, CTimer* t)
1225 {
1226 m_iIPversion = version;
1227 m_szPayloadSize = payload;
1228
1229 SRT_ASSERT(m_pUnitQueue == NULL);
1230 m_pUnitQueue = new CUnitQueue(qsize, (int)payload);
1231
1232 m_pHash = new CHash;
1233 m_pHash->init(hsize);
1234
1235 m_pChannel = cc;
1236 m_pTimer = t;
1237
1238 m_pRcvUList = new CRcvUList;
1239 m_pRendezvousQueue = new CRendezvousQueue;
1240
1241 #if ENABLE_LOGGING
1242 const int cnt = ++m_counter;
1243 const std::string thrname = "SRT:RcvQ:w" + Sprint(cnt);
1244 #else
1245 const std::string thrname = "SRT:RcvQ:w";
1246 #endif
1247
1248 if (!StartThread(m_WorkerThread, CRcvQueue::worker, this, thrname.c_str()))
1249 {
1250 throw CUDTException(MJ_SYSTEMRES, MN_THREAD);
1251 }
1252 }
1253
1254 void* srt::CRcvQueue::worker(void* param)
1255 {
1256 CRcvQueue* self = (CRcvQueue*)param;
1257 sockaddr_any sa(self->getIPversion());
1258 int32_t id = 0;
1259
1260 std::string thname;
1261 ThreadName::get(thname);
1262 THREAD_STATE_INIT(thname.c_str());
1263
1264 CUnit* unit = 0;
1265 EConnectStatus cst = CONN_AGAIN;
1266 while (!self->m_bClosing)
1267 {
1268 bool have_received = false;
1269 EReadStatus rst = self->worker_RetrieveUnit((id), (unit), (sa));
1270
1271 INCREMENT_THREAD_ITERATIONS();
1272 if (rst == RST_OK)
1273 {
1274 if (id < 0)
1275 {
1276 // User error on peer. May log something, but generally can only ignore it.
1277 // XXX Think maybe about sending some "connection rejection response".
1278 HLOGC(qrlog.Debug,
1279 log << self->CONID() << "RECEIVED negative socket id '" << id
1280 << "', rejecting (POSSIBLE ATTACK)");
1281 continue;
1282 }
1283
1284 // NOTE: cst state is being changed here.
1285 // This state should be maintained through any next failed calls to worker_RetrieveUnit.
1286 // Any error switches this to rejection, just for a case.
1287
1288 // Note to rendezvous connection. This can accept:
1289 // - ID == 0 - take the first waiting rendezvous socket
1290 // - ID > 0 - find the rendezvous socket that has this ID.
1291 if (id == 0)
1292 {
1293 // ID 0 is for connection request, which should be passed to the listening socket or rendezvous sockets
1294 cst = self->worker_ProcessConnectionRequest(unit, sa);
1295 }
1296 else
1297 {
1298 // Otherwise ID is expected to be associated with:
1299 // - an enqueued rendezvous socket
1300 // - a socket connected to a peer
1301 cst = self->worker_ProcessAddressedPacket(id, unit, sa);
1302 // CAN RETURN CONN_REJECT, but m_RejectReason is already set
1303 }
1304 HLOGC(qrlog.Debug, log << self->CONID() << "worker: result for the unit: " << ConnectStatusStr(cst));
1305 if (cst == CONN_AGAIN)
1306 {
1307 HLOGC(qrlog.Debug, log << self->CONID() << "worker: packet not dispatched, continuing reading.");
1308 continue;
1309 }
1310 have_received = true;
1311 }
1312 else if (rst == RST_ERROR)
1313 {
1314 // According to the description by CChannel::recvfrom, this can be either of:
1315 // - IPE: all errors except EBADF
1316 // - socket was closed in the meantime by another thread: EBADF
1317 // If EBADF, then it's expected that the "closing" state is also set.
1318 // Check that just to report possible errors, but interrupt the loop anyway.
1319 if (self->m_bClosing)
1320 {
1321 HLOGC(qrlog.Debug,
1322 log << self->CONID() << "CChannel reported error, but Queue is closing - INTERRUPTING worker.");
1323 }
1324 else
1325 {
1326 LOGC(qrlog.Fatal,
1327 log << self->CONID()
1328 << "CChannel reported ERROR DURING TRANSMISSION - IPE. INTERRUPTING worker anyway.");
1329 }
1330 cst = CONN_REJECT;
1331 break;
1332 }
1333 // OTHERWISE: this is an "AGAIN" situation. No data was read, but the process should continue.
1334
1335 // take care of the timing event for all UDT sockets
1336 const steady_clock::time_point curtime_minus_syn =
1337 steady_clock::now() - microseconds_from(CUDT::COMM_SYN_INTERVAL_US);
1338
1339 CRNode* ul = self->m_pRcvUList->m_pUList;
1340 while ((NULL != ul) && (ul->m_tsTimeStamp < curtime_minus_syn))
1341 {
1342 CUDT* u = ul->m_pUDT;
1343
1344 if (u->m_bConnected && !u->m_bBroken && !u->m_bClosing)
1345 {
1346 u->checkTimers();
1347 self->m_pRcvUList->update(u);
1348 }
1349 else
1350 {
1351 HLOGC(qrlog.Debug,
1352 log << CUDTUnited::CONID(u->m_SocketID) << " SOCKET broken, REMOVING FROM RCV QUEUE/MAP.");
1353 // the socket must be removed from Hash table first, then RcvUList
1354 self->m_pHash->remove(u->m_SocketID);
1355 self->m_pRcvUList->remove(u);
1356 u->m_pRNode->m_bOnList = false;
1357 }
1358
1359 ul = self->m_pRcvUList->m_pUList;
1360 }
1361
1362 if (have_received)
1363 {
1364 HLOGC(qrlog.Debug,
1365 log << "worker: RECEIVED PACKET --> updateConnStatus. cst=" << ConnectStatusStr(cst) << " id=" << id
1366 << " pkt-payload-size=" << unit->m_Packet.getLength());
1367 }
1368
1369 // Check connection requests status for all sockets in the RendezvousQueue.
1370 // Pass the connection status from the last call of:
1371 // worker_ProcessAddressedPacket --->
1372 // worker_TryAsyncRend_OrStore --->
1373 // CUDT::processAsyncConnectResponse --->
1374 // CUDT::processConnectResponse
1375 self->m_pRendezvousQueue->updateConnStatus(rst, cst, unit);
1376
1377 // XXX updateConnStatus may have removed the connector from the list,
1378 // however there's still m_mBuffer in CRcvQueue for that socket to care about.
1379 }
1380
1381 HLOGC(qrlog.Debug, log << "worker: EXIT");
1382
1383 THREAD_EXIT();
1384 return NULL;
1385 }
1386
1387 srt::EReadStatus srt::CRcvQueue::worker_RetrieveUnit(int32_t& w_id, CUnit*& w_unit, sockaddr_any& w_addr)
1388 {
1389 #if !USE_BUSY_WAITING
1390 // This might be not really necessary, and probably
1391 // not good for extensive bidirectional communication.
1392 m_pTimer->tick();
1393 #endif
1394
1395 // check waiting list, if new socket, insert it to the list
1396 while (ifNewEntry())
1397 {
1398 CUDT* ne = getNewEntry();
1399 if (ne)
1400 {
1401 HLOGC(qrlog.Debug,
1402 log << CUDTUnited::CONID(ne->m_SocketID)
1403 << " SOCKET pending for connection - ADDING TO RCV QUEUE/MAP");
1404 m_pRcvUList->insert(ne);
1405 m_pHash->insert(ne->m_SocketID, ne);
1406 }
1407 }
1408 // find next available slot for incoming packet
1409 w_unit = m_pUnitQueue->getNextAvailUnit();
1410 if (!w_unit)
1411 {
1412 // no space, skip this packet
1413 CPacket temp;
1414 temp.allocate(m_szPayloadSize);
1415 THREAD_PAUSED();
1416 EReadStatus rst = m_pChannel->recvfrom((w_addr), (temp));
1417 THREAD_RESUMED();
1418 // Note: this will print nothing about the packet details unless heavy logging is on.
1419 LOGC(qrlog.Error, log << CONID() << "LOCAL STORAGE DEPLETED. Dropping 1 packet: " << temp.Info());
1420
1421 // Be transparent for RST_ERROR, but ignore the correct
1422 // data read and fake that the packet was dropped.
1423 return rst == RST_ERROR ? RST_ERROR : RST_AGAIN;
1424 }
1425
1426 w_unit->m_Packet.setLength(m_szPayloadSize);
1427
1428 // reading next incoming packet, recvfrom returns -1 is nothing has been received
1429 THREAD_PAUSED();
1430 EReadStatus rst = m_pChannel->recvfrom((w_addr), (w_unit->m_Packet));
1431 THREAD_RESUMED();
1432
1433 if (rst == RST_OK)
1434 {
1435 w_id = w_unit->m_Packet.id();
1436 HLOGC(qrlog.Debug,
1437 log << "INCOMING PACKET: FROM=" << w_addr.str() << " BOUND=" << m_pChannel->bindAddressAny().str() << " "
1438 << w_unit->m_Packet.Info());
1439 }
1440 return rst;
1441 }
1442
1443 srt::EConnectStatus srt::CRcvQueue::worker_ProcessConnectionRequest(CUnit* unit, const sockaddr_any& addr)
1444 {
1445 HLOGC(cnlog.Debug,
1446 log << "Got sockID=0 from " << addr.str() << " - trying to resolve it as a connection request...");
1447 // Introduced protection because it may potentially happen
1448 // that another thread could have closed the socket at
1449 // the same time and inject a bug between checking the
1450 // pointer for NULL and using it.
1451 int listener_ret = SRT_REJ_UNKNOWN;
1452 bool have_listener = false;
1453 {
1454 SharedLock shl(m_pListener);
1455 CUDT* pListener = m_pListener.get_locked(shl);
1456
1457 if (pListener)
1458 {
1459 LOGC(cnlog.Debug, log << "PASSING request from: " << addr.str() << " to listener:" << pListener->socketID());
1460 listener_ret = pListener->processConnectRequest(addr, unit->m_Packet);
1461
1462 // This function does return a code, but it's hard to say as to whether
1463 // anything can be done about it. In case when it's stated possible, the
1464 // listener will try to send some rejection response to the caller, but
1465 // that's already done inside this function. So it's only used for
1466 // displaying the error in logs.
1467
1468 have_listener = true;
1469 }
1470 }
1471
1472 // NOTE: Rendezvous sockets do bind(), but not listen(). It means that the socket is
1473 // ready to accept connection requests, but they are not being redirected to the listener
1474 // socket, as this is not a listener socket at all. This goes then HERE.
1475
1476 if (have_listener) // That is, the above block with m_pListener->processConnectRequest was executed
1477 {
1478 LOGC(cnlog.Debug,
1479 log << CONID() << "Listener got the connection request from: " << addr.str()
1480 << " result:" << RequestTypeStr(UDTRequestType(listener_ret)));
1481 return listener_ret == SRT_REJ_UNKNOWN ? CONN_CONTINUE : CONN_REJECT;
1482 }
1483
1484 // If there's no listener waiting for the packet, just store it into the queue.
1485 return worker_TryAsyncRend_OrStore(0, unit, addr); // 0 id because the packet came in with that very ID.
1486 }
1487
1488 srt::EConnectStatus srt::CRcvQueue::worker_ProcessAddressedPacket(int32_t id, CUnit* unit, const sockaddr_any& addr)
1489 {
1490 CUDT* u = m_pHash->lookup(id);
1491 if (!u)
1492 {
1493 // Pass this to either async rendezvous connection,
1494 // or store the packet in the queue.
1495 HLOGC(cnlog.Debug, log << "worker_ProcessAddressedPacket: resending to QUEUED socket @" << id);
1496 return worker_TryAsyncRend_OrStore(id, unit, addr);
1497 }
1498 // Although we don“t have an exclusive passing here,
1499 // we can count on that when the socket was once present in the hash,
1500 // it will not be deleted for at least one GC cycle. But we still need
1501 // to maintain the object existence until it's in use.
1502 // Note that here we are out of any locks, so m_GlobControlLock can be locked.
1503 CUDTUnited::SocketKeeper sk (CUDT::uglobal(), u->m_parent);
1504
1505 // Found associated CUDT - process this as control or data packet
1506 // addressed to an associated socket.
1507 if (addr != u->m_PeerAddr)
1508 {
1509 HLOGC(cnlog.Debug,
1510 log << CONID() << "Packet for SID=" << id << " asoc with " << u->m_PeerAddr.str() << " received from "
1511 << addr.str() << " (CONSIDERED ATTACK ATTEMPT)");
1512 // This came not from the address that is the peer associated
1513 // with the socket. Ignore it.
1514 return CONN_AGAIN;
1515 }
1516
1517 if (!u->m_bConnected || u->m_bBroken || u->m_bClosing)
1518 {
1519 u->m_RejectReason = SRT_REJ_CLOSE;
1520 // The socket is currently in the process of being disconnected
1521 // or destroyed. Ignore.
1522 // XXX send UMSG_SHUTDOWN in this case?
1523 // XXX May it require mutex protection?
1524 return CONN_REJECT;
1525 }
1526
1527 if (unit->m_Packet.isControl())
1528 u->processCtrl(unit->m_Packet);
1529 else
1530 u->processData(unit);
1531
1532 u->checkTimers();
1533 m_pRcvUList->update(u);
1534
1535 return CONN_RUNNING;
1536 }
1537
1538 // This function responds to the fact that a packet has come
1539 // for a socket that does not expect to receive a normal connection
1540 // request. This can be then:
1541 // - a normal packet of whatever kind, just to be processed by the message loop
1542 // - a rendezvous connection
1543 // This function then tries to manage the packet as a rendezvous connection
1544 // request in ASYNC mode; when this is not applicable, it stores the packet
1545 // in the "receiving queue" so that it will be picked up in the "main" thread.
1546 srt::EConnectStatus srt::CRcvQueue::worker_TryAsyncRend_OrStore(int32_t id, CUnit* unit, const sockaddr_any& addr)
1547 {
1548 // This 'retrieve' requires that 'id' be either one of those
1549 // stored in the rendezvous queue (see CRcvQueue::registerConnector)
1550 // or simply 0, but then at least the address must match one of these.
1551 // If the id was 0, it will be set to the actual socket ID of the returned CUDT.
1552 CUDT* u = m_pRendezvousQueue->retrieve(addr, (id));
1553 if (!u)
1554 {
1555 // this socket is then completely unknown to the system.
1556 // Note that this situation may also happen at a very unfortunate
1557 // coincidence that the socket is already bound, but the registerConnector()
1558 // has not yet started. In case of rendezvous this may mean that the other
1559 // side just started sending its handshake packets, the local side has already
1560 // run the CRcvQueue::worker thread, and this worker thread is trying to dispatch
1561 // the handshake packet too early, before the dispatcher has a chance to see
1562 // this socket registered in the RendezvousQueue, which causes the packet unable
1563 // to be dispatched. Therefore simply treat every "out of band" packet (with socket
1564 // not belonging to the connection and not registered as rendezvous) as "possible
1565 // attack" and ignore it. This also should better protect the rendezvous socket
1566 // against a rogue connector.
1567 if (id == 0)
1568 {
1569 HLOGC(cnlog.Debug,
1570 log << CONID() << "AsyncOrRND: no sockets expect connection from " << addr.str()
1571 << " - POSSIBLE ATTACK, ignore packet");
1572 }
1573 else
1574 {
1575 HLOGC(cnlog.Debug,
1576 log << CONID() << "AsyncOrRND: no sockets expect socket " << id << " from " << addr.str()
1577 << " - POSSIBLE ATTACK, ignore packet");
1578 }
1579 return CONN_AGAIN; // This means that the packet should be ignored.
1580 }
1581
1582 // asynchronous connect: call connect here
1583 // otherwise wait for the UDT socket to retrieve this packet
1584 if (!u->m_config.bSynRecving)
1585 {
1586 HLOGC(cnlog.Debug, log << "AsyncOrRND: packet RESOLVED TO @" << id << " -- continuing as ASYNC CONNECT");
1587 // This is practically same as processConnectResponse, just this applies
1588 // appropriate mutex lock - which can't be done here because it's intentionally private.
1589 // OTOH it can't be applied to processConnectResponse because the synchronous
1590 // call to this method applies the lock by itself, and same-thread-double-locking is nonportable (crashable).
1591 EConnectStatus cst = u->processAsyncConnectResponse(unit->m_Packet);
1592
1593 if (cst == CONN_CONFUSED)
1594 {
1595 LOGC(cnlog.Warn, log << "AsyncOrRND: PACKET NOT HANDSHAKE - re-requesting handshake from peer");
1596 storePktClone(id, unit->m_Packet);
1597 if (!u->processAsyncConnectRequest(RST_AGAIN, CONN_CONTINUE, &unit->m_Packet, u->m_PeerAddr))
1598 {
1599 // Reuse previous behavior to reject a packet
1600 cst = CONN_REJECT;
1601 }
1602 else
1603 {
1604 cst = CONN_CONTINUE;
1605 }
1606 }
1607
1608 // It might be that this is a data packet, which has turned the connection
1609 // into "connected" state, removed the connector (so since now every next packet
1610 // will land directly in the queue), but this data packet shall still be delivered.
1611 if (cst == CONN_ACCEPT && !unit->m_Packet.isControl())
1612 {
1613 // The process as called through processAsyncConnectResponse() should have put the
1614 // socket into the pending queue for pending connection (don't ask me, this is so).
1615 // This pending queue is being purged every time in the beginning of this loop, so
1616 // currently the socket is in the pending queue, but not yet in the connection queue.
1617 // It will be done at the next iteration of the reading loop, but it will be too late,
1618 // we have a pending data packet now and we must either dispatch it to an already connected
1619 // socket or disregard it, and rather prefer the former. So do this transformation now
1620 // that we KNOW (by the cst == CONN_ACCEPT result) that the socket should be inserted
1621 // into the pending anteroom.
1622
1623 CUDT* ne = getNewEntry(); // This function actually removes the entry and returns it.
1624 // This **should** now always return a non-null value, but check it first
1625 // because if this accidentally isn't true, the call to worker_ProcessAddressedPacket will
1626 // result in redirecting it to here and so on until the call stack overflow. In case of
1627 // this "accident" simply disregard the packet from any further processing, it will be later
1628 // loss-recovered.
1629 // XXX (Probably the old contents of UDT's CRcvQueue::worker should be shaped a little bit
1630 // differently throughout the functions).
1631 if (ne)
1632 {
1633 HLOGC(cnlog.Debug,
1634 log << CUDTUnited::CONID(ne->m_SocketID)
1635 << " SOCKET pending for connection - ADDING TO RCV QUEUE/MAP");
1636 m_pRcvUList->insert(ne);
1637 m_pHash->insert(ne->m_SocketID, ne);
1638
1639 // The current situation is that this has passed processAsyncConnectResponse, but actually
1640 // this packet *SHOULD HAVE BEEN* handled by worker_ProcessAddressedPacket, however the
1641 // connection state wasn't completed at the moment when dispatching this packet. This has
1642 // been now completed inside the call to processAsyncConnectResponse, but this is still a
1643 // data packet that should have expected the connection to be already established. Therefore
1644 // redirect it once again into worker_ProcessAddressedPacket here.
1645
1646 HLOGC(cnlog.Debug,
1647 log << "AsyncOrRND: packet SWITCHED TO CONNECTED with ID=" << id
1648 << " -- passing to worker_ProcessAddressedPacket");
1649
1650 // Theoretically we should check if m_pHash->lookup(ne->m_SocketID) returns 'ne', but this
1651 // has been just added to m_pHash, so the check would be extremely paranoid here.
1652 cst = worker_ProcessAddressedPacket(id, unit, addr);
1653 if (cst == CONN_REJECT)
1654 return cst;
1655 return CONN_ACCEPT; // this function usually will return CONN_CONTINUE, which doesn't represent current
1656 // situation.
1657 }
1658 else
1659 {
1660 LOGC(cnlog.Error,
1661 log << "IPE: AsyncOrRND: packet SWITCHED TO CONNECTED, but ID=" << id
1662 << " is still not present in the socket ID dispatch hash - DISREGARDING");
1663 }
1664 }
1665 return cst;
1666 }
1667 HLOGC(cnlog.Debug,
1668 log << "AsyncOrRND: packet RESOLVED TO ID=" << id << " -- continuing through CENTRAL PACKET QUEUE");
1669 // This is where also the packets for rendezvous connection will be landing,
1670 // in case of a synchronous connection.
1671 storePktClone(id, unit->m_Packet);
1672
1673 return CONN_CONTINUE;
1674 }
1675
1676 void srt::CRcvQueue::stopWorker()
1677 {
1678 // We use the decent way, so we say to the thread "please exit".
1679 m_bClosing = true;
1680
1681 // Sanity check of the function's affinity.
1682 if (srt::sync::this_thread::get_id() == m_WorkerThread.get_id())
1683 {
1684 LOGC(rslog.Error, log << "IPE: RcvQ:WORKER TRIES TO CLOSE ITSELF!");
1685 return; // do nothing else, this would cause a hangup or crash.
1686 }
1687
1688 HLOGC(rslog.Debug, log << "RcvQueue: EXIT (forced)");
1689 // And we trust the thread that it does.
1690 m_WorkerThread.join();
1691 }
1692
1693 int srt::CRcvQueue::recvfrom(int32_t id, CPacket& w_packet)
1694 {
1695 CUniqueSync buffercond(m_BufferLock, m_BufferCond);
1696
1697 map<int32_t, std::queue<CPacket*> >::iterator i = m_mBuffer.find(id);
1698
1699 if (i == m_mBuffer.end())
1700 {
1701 THREAD_PAUSED();
1702 buffercond.wait_for(seconds_from(1));
1703 THREAD_RESUMED();
1704
1705 i = m_mBuffer.find(id);
1706 if (i == m_mBuffer.end())
1707 {
1708 w_packet.setLength(-1);
1709 return -1;
1710 }
1711 }
1712
1713 // retrieve the earliest packet
1714 CPacket* newpkt = i->second.front();
1715
1716 if (w_packet.getLength() < newpkt->getLength())
1717 {
1718 w_packet.setLength(-1);
1719 return -1;
1720 }
1721
1722 // copy packet content
1723 // XXX Check if this wouldn't be better done by providing
1724 // copy constructor for DynamicStruct.
1725 // XXX Another thing: this looks wasteful. This expects an already
1726 // allocated memory on the packet, this thing gets the packet,
1727 // copies it into the passed packet and then the source packet
1728 // gets deleted. Why not simply return the originally stored packet,
1729 // without copying, allocation and deallocation?
1730 memcpy((w_packet.m_nHeader), newpkt->m_nHeader, CPacket::HDR_SIZE);
1731 memcpy((w_packet.m_pcData), newpkt->m_pcData, newpkt->getLength());
1732 w_packet.setLength(newpkt->getLength());
1733 w_packet.m_DestAddr = newpkt->m_DestAddr;
1734
1735 delete newpkt;
1736
1737 // remove this message from queue,
1738 // if no more messages left for this socket, release its data structure
1739 i->second.pop();
1740 if (i->second.empty())
1741 m_mBuffer.erase(i);
1742
1743 return (int)w_packet.getLength();
1744 }
1745
1746 bool srt::CRcvQueue::setListener(CUDT* u)
1747 {
1748 return m_pListener.compare_exchange(NULL, u);
1749 }
1750
1751 srt::CUDT* srt::CRcvQueue::getListener()
1752 {
1753 SharedLock lkl (m_pListener);
1754 return m_pListener.get_locked(lkl);
1755 }
1756
1757 // XXX NOTE: TSan reports here false positive against the call
1758 // to locateSocket in CUDTUnited::newConnection. This here will apply
1759 // exclusive lock on m_pListener, while keeping shared lock on
1760 // CUDTUnited::m_GlobControlLock in CUDTUnited::closeAllSockets.
1761 // As the other thread locks both as shared, this is no deadlock risk.
1762 bool srt::CRcvQueue::removeListener(CUDT* u)
1763 {
1764 return m_pListener.compare_exchange(u, NULL);
1765 }
1766
1767 void srt::CRcvQueue::registerConnector(const SRTSOCKET& id,
1768 CUDT* u,
1769 const sockaddr_any& addr,
1770 const steady_clock::time_point& ttl)
1771 {
1772 HLOGC(cnlog.Debug,
1773 log << "registerConnector: adding @" << id << " addr=" << addr.str() << " TTL=" << FormatTime(ttl));
1774 m_pRendezvousQueue->insert(id, u, addr, ttl);
1775 }
1776
1777 void srt::CRcvQueue::removeConnector(const SRTSOCKET& id)
1778 {
1779 HLOGC(cnlog.Debug, log << "removeConnector: removing @" << id);
1780 m_pRendezvousQueue->remove(id);
1781
1782 ScopedLock bufferlock(m_BufferLock);
1783
1784 map<int32_t, std::queue<CPacket*> >::iterator i = m_mBuffer.find(id);
1785 if (i != m_mBuffer.end())
1786 {
1787 HLOGC(cnlog.Debug,
1788 log << "removeConnector: ... and its packet queue with " << i->second.size() << " packets collected");
1789 while (!i->second.empty())
1790 {
1791 delete i->second.front();
1792 i->second.pop();
1793 }
1794 m_mBuffer.erase(i);
1795 }
1796 }
1797
1798 void srt::CRcvQueue::setNewEntry(CUDT* u)
1799 {
1800 HLOGC(cnlog.Debug, log << CUDTUnited::CONID(u->m_SocketID) << "setting socket PENDING FOR CONNECTION");
1801 ScopedLock listguard(m_IDLock);
1802 m_vNewEntry.push_back(u);
1803 }
1804
1805 bool srt::CRcvQueue::ifNewEntry()
1806 {
1807 ScopedLock listguard(m_IDLock);
1808 return !(m_vNewEntry.empty());
1809 }
1810
1811 srt::CUDT* srt::CRcvQueue::getNewEntry()
1812 {
1813 ScopedLock listguard(m_IDLock);
1814
1815 if (m_vNewEntry.empty())
1816 return NULL;
1817
1818 CUDT* u = (CUDT*)*(m_vNewEntry.begin());
1819 m_vNewEntry.erase(m_vNewEntry.begin());
1820
1821 return u;
1822 }
1823
1824 void srt::CRcvQueue::storePktClone(int32_t id, const CPacket& pkt)
1825 {
1826 CUniqueSync passcond(m_BufferLock, m_BufferCond);
1827
1828 map<int32_t, std::queue<CPacket*> >::iterator i = m_mBuffer.find(id);
1829
1830 if (i == m_mBuffer.end())
1831 {
1832 m_mBuffer[id].push(pkt.clone());
1833 passcond.notify_one();
1834 }
1835 else
1836 {
1837 // Avoid storing too many packets, in case of malfunction or attack.
1838 if (i->second.size() > 16)
1839 return;
1840
1841 i->second.push(pkt.clone());
1842 }
1843 }
1844
1845 void srt::CMultiplexer::resetAtFork()
1846 {
1847 if (m_pRcvQueue != NULL)
1848 m_pRcvQueue->resetAtFork();
1849 if (m_pSndQueue != NULL)
1850 m_pSndQueue->resetAtFork();
1851 }
1852
1853 void srt::CMultiplexer::close()
1854 {
1855 if (m_pChannel)
1856 {
1857 m_pChannel->close();
1858 delete m_pChannel;
1859 m_pChannel = NULL;
1860 }
1861 }
1862
1863 void srt::CMultiplexer::stop()
1864 {
1865 if (m_pRcvQueue != NULL)
1866 m_pRcvQueue->stop();
1867 if (m_pSndQueue != NULL)
1868 m_pSndQueue->stop();
1869 }
1870
1871 void srt::CMultiplexer::destroy()
1872 {
1873 // Reverse order of the assigned.
1874 stop();
1875 close();
1876 }