Debian Codegraph @

srtcore/group.cpp

raw
1 #include "platform_sys.h"
2
3 #include <iterator>
4
5 #include "api.h"
6 #include "group.h"
7
8 using namespace std;
9 using namespace srt::sync;
10 using namespace srt::groups;
11 using namespace srt_logging;
12
13 // The SRT_DEF_VERSION is defined in core.cpp.
14 extern const int32_t SRT_DEF_VERSION;
15
16 namespace srt {
17
18 int32_t CUDTGroup::s_tokenGen = 0;
19
20 // [[using locked(this->m_GroupLock)]];
21 bool CUDTGroup::getBufferTimeBase(CUDT* forthesakeof,
22 steady_clock::time_point& w_tb,
23 bool& w_wp,
24 steady_clock::duration& w_dr)
25 {
26 CUDT* master = 0;
27 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
28 {
29 CUDT* u = &gi->ps->core();
30 if (gi->laststatus != SRTS_CONNECTED)
31 {
32 HLOGC(gmlog.Debug,
33 log << "getBufferTimeBase: skipping @" << u->m_SocketID
34 << ": not connected, state=" << SockStatusStr(gi->laststatus));
35 continue;
36 }
37
38 if (u == forthesakeof)
39 continue; // skip the member if it's the target itself
40
41 if (!u->m_pRcvBuffer)
42 continue; // Not initialized yet
43
44 master = u;
45 break; // found
46 }
47
48 // We don't have any sockets in the group, so can't get
49 // the buffer timebase. This should be then initialized
50 // the usual way.
51 if (!master)
52 return false;
53
54 master->m_pRcvBuffer->getInternalTimeBase((w_tb), (w_wp), (w_dr));
55
56 // Sanity check
57 if (is_zero(w_tb))
58 {
59 LOGC(gmlog.Error, log << "IPE: existing previously socket has no time base set yet!");
60 return false; // this will enforce initializing the time base normal way
61 }
62 return true;
63 }
64
65 // [[using locked(this->m_GroupLock)]];
66 bool CUDTGroup::applyGroupSequences(SRTSOCKET target, int32_t& w_snd_isn, int32_t& w_rcv_isn)
67 {
68 if (m_bConnected) // You are the first one, no need to change.
69 {
70 IF_HEAVY_LOGGING(string update_reason = "what?");
71 // Find a socket that is declared connected and is not
72 // the socket that caused the call.
73 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
74 {
75 if (gi->id == target)
76 continue;
77
78 CUDT& se = gi->ps->core();
79 if (!se.m_bConnected)
80 continue;
81
82 // Found it. Get the following sequences:
83 // For sending, the sequence that is about to be sent next.
84 // For receiving, the sequence of the latest received packet.
85
86 // SndCurrSeqNo is initially set to ISN-1, this next one is
87 // the sequence that is about to be stamped on the next sent packet
88 // over that socket. Using this field is safer because it is atomic
89 // and its affinity is to the same thread as the sending function.
90
91 // NOTE: the groupwise scheduling sequence might have been set
92 // already. If so, it means that it was set by either:
93 // - the call of this function on the very first connected socket (see below)
94 // - the call to `sendBroadcast` or `sendBackup`
95 // In both cases, we want THIS EXACTLY value to be reported
96 if (m_iLastSchedSeqNo != -1)
97 {
98 w_snd_isn = m_iLastSchedSeqNo;
99 IF_HEAVY_LOGGING(update_reason = "GROUPWISE snd-seq");
100 }
101 else
102 {
103 w_snd_isn = se.m_iSndNextSeqNo;
104
105 // Write it back to the groupwise scheduling sequence so that
106 // any next connected socket will take this value as well.
107 m_iLastSchedSeqNo = w_snd_isn;
108 IF_HEAVY_LOGGING(update_reason = "existing socket not yet sending");
109 }
110
111 // RcvCurrSeqNo is increased by one because it happens that at the
112 // synchronization moment it's already past reading and delivery.
113 // This is redundancy, so the redundant socket is connected at the moment
114 // when the other one is already transmitting, so skipping one packet
115 // even if later transmitted is less troublesome than requesting a
116 // "mistakenly seen as lost" packet.
117 w_rcv_isn = CSeqNo::incseq(se.m_iRcvCurrSeqNo);
118
119 HLOGC(gmlog.Debug,
120 log << "applyGroupSequences: @" << target << " gets seq from @" << gi->id << " rcv %" << (w_rcv_isn)
121 << " snd %" << (w_snd_isn) << " as " << update_reason);
122 return false;
123 }
124 }
125
126 // If the GROUP (!) is not connected, or no running/pending socket has been found.
127 // // That is, given socket is the first one.
128 // The group data should be set up with its own data. They should already be passed here
129 // in the variables.
130 //
131 // Override the schedule sequence of the group in this case because whatever is set now,
132 // it's not valid.
133
134 HLOGC(gmlog.Debug,
135 log << "applyGroupSequences: no socket found connected and transmitting, @" << target
136 << " not changing sequences, storing snd-seq %" << (w_snd_isn));
137
138 set_currentSchedSequence(w_snd_isn);
139
140 return true;
141 }
142
143 // NOTE: This function is now for DEBUG PURPOSES ONLY.
144 // Except for presenting the extracted data in the logs, there's no use of it now.
145 void CUDTGroup::debugMasterData(SRTSOCKET slave)
146 {
147 // Find at least one connection, which is running. Note that this function is called
148 // from within a handshake process, so the socket that undergoes this process is at best
149 // currently in SRT_GST_PENDING state and it's going to be in SRT_GST_IDLE state at the
150 // time when the connection process is done, until the first reading/writing happens.
151 ScopedLock cg(m_GroupLock);
152
153 IF_LOGGING(SRTSOCKET mpeer = SRT_INVALID_SOCK);
154 IF_LOGGING(steady_clock::time_point start_time);
155
156 bool found = false;
157
158 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
159 {
160 if (gi->sndstate == SRT_GST_RUNNING)
161 {
162 // Found it. Get the socket's peer's ID and this socket's
163 // Start Time. Once it's delivered, this can be used to calculate
164 // the Master-to-Slave start time difference.
165 IF_LOGGING(mpeer = gi->ps->m_PeerID);
166 IF_LOGGING(start_time = gi->ps->core().socketStartTime());
167 HLOGC(gmlog.Debug,
168 log << "getMasterData: found RUNNING master @" << gi->id << " - reporting master's peer $" << mpeer
169 << " starting at " << FormatTime(start_time));
170 found = true;
171 break;
172 }
173 }
174
175 if (!found)
176 {
177 // If no running one found, then take the first socket in any other
178 // state than broken, except the slave. This is for a case when a user
179 // has prepared one link already, but hasn't sent anything through it yet.
180 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
181 {
182 if (gi->sndstate == SRT_GST_BROKEN)
183 continue;
184
185 if (gi->id == slave)
186 continue;
187
188 // Found it. Get the socket's peer's ID and this socket's
189 // Start Time. Once it's delivered, this can be used to calculate
190 // the Master-to-Slave start time difference.
191 IF_LOGGING(mpeer = gi->ps->core().m_PeerID);
192 IF_LOGGING(start_time = gi->ps->core().socketStartTime());
193 HLOGC(gmlog.Debug,
194 log << "getMasterData: found IDLE/PENDING master @" << gi->id << " - reporting master's peer $" << mpeer
195 << " starting at " << FormatTime(start_time));
196 found = true;
197 break;
198 }
199 }
200
201 if (!found)
202 {
203 LOGC(cnlog.Debug, log << CONID() << "NO GROUP MASTER LINK found for group: $" << id());
204 }
205 else
206 {
207 // The returned master_st is the master's start time. Calculate the
208 // difference time.
209 IF_LOGGING(steady_clock::duration master_tdiff = m_tsStartTime - start_time);
210 LOGC(cnlog.Debug, log << CONID() << "FOUND GROUP MASTER LINK: peer=$" << mpeer
211 << " - start time diff: " << FormatDuration<DUNIT_S>(master_tdiff));
212 }
213 }
214
215 // GROUP
216
217 CUDTGroup::SocketData* CUDTGroup::add(SocketData data)
218 {
219 ScopedLock g(m_GroupLock);
220
221 // Change the snd/rcv state of the group member to PENDING.
222 // Default for SocketData after creation is BROKEN, which just
223 // after releasing the m_GroupLock could be read and interpreted
224 // as broken connection and removed before the handshake process
225 // is done.
226 data.sndstate = SRT_GST_PENDING;
227 data.rcvstate = SRT_GST_PENDING;
228
229 LOGC(gmlog.Note, log << "group/add: adding member @" << data.id << " into group $" << id());
230 m_Group.push_back(data);
231 gli_t end = m_Group.end();
232 if (m_iMaxPayloadSize == -1)
233 {
234 int plsize = (int)data.ps->core().OPT_PayloadSize();
235 HLOGC(gmlog.Debug,
236 log << "CUDTGroup::add: taking MAX payload size from socket @" << data.ps->m_SocketID << ": " << plsize
237 << " " << (plsize ? "(explicit)" : "(unspecified = fallback to 1456)"));
238 if (plsize == 0)
239 plsize = SRT_LIVE_MAX_PLSIZE;
240 // It is stated that the payload size
241 // is taken from first, and every next one
242 // will get the same.
243 m_iMaxPayloadSize = plsize;
244 }
245
246 --end;
247 return &*end;
248 }
249
250 CUDTGroup::CUDTGroup(SRT_GROUP_TYPE gtype)
251 : m_Global(CUDT::uglobal())
252 , m_GroupID(-1)
253 , m_PeerGroupID(-1)
254 , m_type(gtype)
255 , m_listener()
256 , m_iBusy()
257 , m_iSndOldestMsgNo(SRT_MSGNO_NONE)
258 , m_iSndAckedMsgNo(SRT_MSGNO_NONE)
259 , m_uOPT_MinStabilityTimeout_us(1000 * CSrtConfig::COMM_DEF_MIN_STABILITY_TIMEOUT_MS)
260 // -1 = "undefined"; will become defined with first added socket
261 , m_iMaxPayloadSize(-1)
262 , m_iAvgPayloadSize(-1)
263 , m_bSynRecving(true)
264 , m_bSynSending(true)
265 , m_bTsbPd(true)
266 , m_bTLPktDrop(true)
267 , m_iTsbPdDelay_us(0)
268 // m_*EID and m_*Epolld fields will be initialized
269 // in the constructor body.
270 , m_iSndTimeOut(-1)
271 , m_iRcvTimeOut(-1)
272 , m_tsStartTime()
273 , m_tsRcvPeerStartTime()
274 , m_RcvBaseSeqNo(SRT_SEQNO_NONE)
275 , m_bOpened(false)
276 , m_bConnected(false)
277 , m_bClosing(false)
278 , m_iLastSchedSeqNo(SRT_SEQNO_NONE)
279 , m_iLastSchedMsgNo(SRT_MSGNO_NONE)
280 {
281 setupMutex(m_GroupLock, "Group");
282 setupMutex(m_RcvDataLock, "G/RcvData");
283 setupCond(m_RcvDataCond, "G/RcvData");
284 m_RcvEID = m_Global.m_EPoll.create(&m_RcvEpolld);
285 m_SndEID = m_Global.m_EPoll.create(&m_SndEpolld);
286
287 m_stats.init();
288
289 // Set this data immediately during creation before
290 // two or more sockets start arguing about it.
291 m_iLastSchedSeqNo = CUDT::generateISN();
292 }
293
294 CUDTGroup::~CUDTGroup()
295 {
296 srt_epoll_release(m_RcvEID);
297 srt_epoll_release(m_SndEID);
298 releaseMutex(m_GroupLock);
299 releaseMutex(m_RcvDataLock);
300 releaseCond(m_RcvDataCond);
301 }
302
303 void CUDTGroup::GroupContainer::erase(CUDTGroup::gli_t it)
304 {
305 if (it == m_LastActiveLink)
306 {
307 if (m_List.empty())
308 {
309 LOGC(gmlog.Error, log << "IPE: GroupContainer is empty and 'erase' is called on it.");
310 m_LastActiveLink = m_List.end();
311 return; // this avoids any misunderstandings in iterator checks
312 }
313
314 gli_t bb = m_List.begin();
315 ++bb;
316 if (bb == m_List.end()) // means: m_List.size() == 1
317 {
318 // One element, this one being deleted, nothing to point to.
319 m_LastActiveLink = m_List.end();
320 }
321 else
322 {
323 // Set the link to the previous element IN THE RING.
324 // We have the position pointer.
325 // Reverse iterator is automatically decremented.
326 std::reverse_iterator<gli_t> rt(m_LastActiveLink);
327 if (rt == m_List.rend())
328 rt = m_List.rbegin();
329
330 m_LastActiveLink = rt.base();
331
332 // This operation is safe because we know that:
333 // - the size of the container is at least 2 (0 and 1 cases are handled above)
334 // - if m_LastActiveLink == m_List.begin(), `rt` is shifted to the opposite end.
335 --m_LastActiveLink;
336 }
337 }
338 m_List.erase(it);
339 --m_SizeCache;
340 }
341
342 void CUDTGroup::setOpt(SRT_SOCKOPT optName, const void* optval, int optlen)
343 {
344 HLOGC(gmlog.Debug,
345 log << "GROUP $" << id() << " OPTION: #" << optName
346 << " value:" << FormatBinaryString((uint8_t*)optval, optlen));
347
348 switch (optName)
349 {
350 // First go options that are NOT ALLOWED to be modified on the group.
351 // (socket-only), or are read-only.
352
353 case SRTO_ISN: // read-only
354 case SRTO_STATE: // read-only
355 case SRTO_EVENT: // read-only
356 case SRTO_SNDDATA: // read-only
357 case SRTO_RCVDATA: // read-only
358 case SRTO_KMSTATE: // read-only
359 case SRTO_VERSION: // read-only
360 case SRTO_PEERVERSION: // read-only
361 case SRTO_SNDKMSTATE: // read-only
362 case SRTO_RCVKMSTATE: // read-only
363 case SRTO_GROUPTYPE: // read-only
364 LOGC(gmlog.Error, log << "group option setter: this option ("<< int(optName) << ") is read-only");
365 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
366
367 case SRTO_SENDER: // deprecated (1.2.0 version legacy)
368 case SRTO_IPV6ONLY: // link-type specific
369 case SRTO_RENDEZVOUS: // socket-only
370 case SRTO_BINDTODEVICE: // socket-specific
371 case SRTO_GROUPCONNECT: // listener-specific
372 LOGC(gmlog.Error, log << "group option setter: this option ("<< int(optName) << ") is socket- or link-specific");
373 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
374
375 case SRTO_TRANSTYPE:
376 case SRTO_TSBPDMODE:
377 case SRTO_CONGESTION:
378 LOGC(gmlog.Error, log << "group option setter: this option (" << int(optName) << ") removes live mode, which is the only supported for groups");
379 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
380 case SRTO_RCVSYN:
381 m_bSynRecving = cast_optval<bool>(optval, optlen);
382 return;
383
384 case SRTO_SNDSYN:
385 m_bSynSending = cast_optval<bool>(optval, optlen);
386 return;
387
388 case SRTO_SNDTIMEO:
389 m_iSndTimeOut = cast_optval<int>(optval, optlen);
390 break;
391
392 case SRTO_RCVTIMEO:
393 m_iRcvTimeOut = cast_optval<int>(optval, optlen);
394 break;
395
396 case SRTO_GROUPMINSTABLETIMEO:
397 {
398 const int val_ms = cast_optval<int>(optval, optlen);
399 const int min_timeo_ms = (int) CSrtConfig::COMM_DEF_MIN_STABILITY_TIMEOUT_MS;
400 if (val_ms < min_timeo_ms)
401 {
402 LOGC(gmlog.Error,
403 log << "group option: SRTO_GROUPMINSTABLETIMEO min allowed value is " << min_timeo_ms << " ms.");
404 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
405 }
406
407 // Search if you already have SRTO_PEERIDLETIMEO set
408 int idletmo = CSrtConfig::COMM_RESPONSE_TIMEOUT_MS;
409 vector<ConfigItem>::iterator f =
410 find_if(m_config.begin(), m_config.end(), ConfigItem::OfType(SRTO_PEERIDLETIMEO));
411 if (f != m_config.end())
412 {
413 f->get(idletmo); // worst case, it will leave it unchanged.
414 }
415
416 if (val_ms > idletmo)
417 {
418 LOGC(gmlog.Error,
419 log << "group option: SRTO_GROUPMINSTABLETIMEO=" << val_ms << " exceeds SRTO_PEERIDLETIMEO=" << idletmo);
420 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
421 }
422
423 m_uOPT_MinStabilityTimeout_us = 1000 * val_ms;
424 }
425
426 break;
427
428 default:
429 break;
430 }
431
432 // All others must be simply stored for setting on a socket.
433 // If the group is already open and any post-option is about
434 // to be modified, it must be allowed and applied on all sockets.
435 if (m_bOpened)
436 {
437 // There's at least one socket in the group, so only
438 // post-options are allowed.
439 if (!binary_search(srt_post_opt_list, srt_post_opt_list + SRT_SOCKOPT_NPOST, optName))
440 {
441 LOGC(gmlog.Error, log << "setsockopt(group): Group is connected, this option can't be altered");
442 throw CUDTException(MJ_NOTSUP, MN_ISCONNECTED, 0);
443 }
444
445 HLOGC(gmlog.Debug, log << "... SPREADING to existing sockets.");
446 // This means that there are sockets already, so apply
447 // this option on them.
448 std::vector<CUDTSocket*> ps_vec;
449 {
450 // Do copy to avoid deadlock. CUDT::setOpt() cannot be called directly inside this loop, because
451 // CUDT::setOpt() will lock m_ConnectionLock, which should be locked before m_GroupLock.
452 ScopedLock gg(m_GroupLock);
453 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
454 {
455 ps_vec.push_back(gi->ps);
456 }
457 }
458 for (std::vector<CUDTSocket*>::iterator it = ps_vec.begin(); it != ps_vec.end(); ++it)
459 {
460 (*it)->core().setOpt(optName, optval, optlen);
461 }
462 }
463
464 // Before possibly storing the option, check if it is settable on a socket.
465 CSrtConfig testconfig;
466
467 // Note: this call throws CUDTException by itself.
468 int result = testconfig.set(optName, optval, optlen);
469 if (result == -1) // returned in case of unknown option
470 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
471
472 // Store the option regardless if pre or post. This will apply
473 m_config.push_back(ConfigItem(optName, optval, optlen));
474 }
475
476 static bool getOptDefault(SRT_SOCKOPT optname, void* optval, int& w_optlen);
477
478 // unfortunately this is required to properly handle th 'default_opt != opt'
479 // operation in the below importOption. Not required simultaneously operator==.
480 static bool operator!=(const struct linger& l1, const struct linger& l2)
481 {
482 return l1.l_onoff != l2.l_onoff || l1.l_linger != l2.l_linger;
483 }
484
485 /// A function template to import socket option of a trivial type.
486 /// This includes linger, bool, int8_t, int32_t, int64_t, double, etc.
487 /// Potentially can be extended to trivially_copyable if needed.
488 template <class ValueType>
489 static void importTrivialOption(vector<CUDTGroup::ConfigItem>& storage, SRT_SOCKOPT optname, const ValueType& optval, const int optsize = sizeof(ValueType))
490 {
491 SRT_STATIC_ASSERT(std::is_trivial<ValueType>::value, "ValueType must be a trivial type.");
492 ValueType optval_dflt = ValueType();
493 int optsize_dflt = sizeof(ValueType);
494 if (!getOptDefault(optname, (&optval_dflt), (optsize_dflt)) || optval_dflt != optval)
495 {
496 SRT_ASSERT(optsize == sizeof(ValueType));
497 // Store the option when:
498 // - no default for this option is found
499 // - the option value retrieved from the field is different from the default one
500 #if HAVE_FULL_CXX11
501 storage.emplace_back(optname, &optval, optsize);
502 #else
503 storage.push_back(CUDTGroup::ConfigItem(optname, &optval, optsize));
504 #endif
505 }
506 }
507
508 /// A function template to import a StringStorage option.
509 template <size_t N>
510 static void importStringOption(vector<CUDTGroup::ConfigItem>& storage, SRT_SOCKOPT optname, const StringStorage<N>& optval)
511 {
512 if (optval.empty())
513 return;
514
515 // Store the option when:
516 // - option has a value (default is empty).
517 #if HAVE_FULL_CXX11
518 storage.emplace_back(optname, optval.c_str(),(int) optval.size());
519 #else
520 storage.push_back(CUDTGroup::ConfigItem(optname, optval.c_str(), (int) optval.size()));
521 #endif
522 }
523
524 // This function is called by the same premises as the CUDT::CUDT(const CUDT&) (copy constructor).
525 // The intention is to rewrite the part that comprises settings from the socket
526 // into the group. Note that some of the settings concern group, some others concern
527 // only target socket, and there are also options that can't be set on a socket.
528 void CUDTGroup::deriveSettings(CUDT* u)
529 {
530 // !!! IMPORTANT !!!
531 //
532 // This function shall ONLY be called on a newly created group
533 // for the sake of the newly accepted socket from the group-enabled listener,
534 // which is lazy-created for the first ever accepted socket.
535 // Once the group is created, it should stay with the options
536 // state as initialized here, and be changeable only in case when
537 // the option is altered on the group.
538
539 // SRTO_RCVSYN
540 m_bSynRecving = u->m_config.bSynRecving;
541
542 // SRTO_SNDSYN
543 m_bSynSending = u->m_config.bSynSending;
544
545 // SRTO_RCVTIMEO
546 m_iRcvTimeOut = u->m_config.iRcvTimeOut;
547
548 // SRTO_SNDTIMEO
549 m_iSndTimeOut = u->m_config.iSndTimeOut;
550
551 // SRTO_GROUPMINSTABLETIMEO
552 m_uOPT_MinStabilityTimeout_us = 1000 * u->m_config.uMinStabilityTimeout_ms;
553
554 // Ok, this really is disgusting, but there's only one way
555 // to properly do it. Would be nice to have some more universal
556 // connection between an option symbolic name and the internals
557 // in CUDT class, but until this is done, since now every new
558 // option will have to be handled both in the CUDT::setOpt/getOpt
559 // functions, and here as well.
560
561 // This is about moving options from listener to the group,
562 // to be potentially replicated on the socket. So both pre
563 // and post options apply.
564
565 #define IM(option, field) importTrivialOption(m_config, option, u->m_config.field)
566 #define IMF(option, field) importTrivialOption(m_config, option, u->field)
567
568 IM(SRTO_MSS, iMSS);
569 IM(SRTO_FC, iFlightFlagSize);
570
571 // Nonstandard
572 importTrivialOption(m_config, SRTO_SNDBUF, u->m_config.iSndBufSize * (u->m_config.iMSS - CPacket::UDP_HDR_SIZE));
573 importTrivialOption(m_config, SRTO_RCVBUF, u->m_config.iRcvBufSize * (u->m_config.iMSS - CPacket::UDP_HDR_SIZE));
574
575 IM(SRTO_LINGER, Linger);
576
577 IM(SRTO_UDP_SNDBUF, iUDPSndBufSize);
578 IM(SRTO_UDP_RCVBUF, iUDPRcvBufSize);
579 // SRTO_RENDEZVOUS: impossible to have it set on a listener socket.
580 // SRTO_SNDTIMEO/RCVTIMEO: groupwise setting
581
582 // SRTO_CONNTIMEO requires a special handling, because API stores the value in integer milliseconds,
583 // but the type of the variable is srt::sync::duration.
584 importTrivialOption(m_config, SRTO_CONNTIMEO, (int) count_milliseconds(u->m_config.tdConnTimeOut));
585
586 IM(SRTO_DRIFTTRACER, bDriftTracer);
587 // Reuseaddr: true by default and should only be true.
588 IM(SRTO_MAXBW, llMaxBW);
589 IM(SRTO_INPUTBW, llInputBW);
590 IM(SRTO_MININPUTBW, llMinInputBW);
591 IM(SRTO_OHEADBW, iOverheadBW);
592 IM(SRTO_IPTOS, iIpToS);
593 IM(SRTO_IPTTL, iIpTTL);
594 IM(SRTO_TSBPDMODE, bTSBPD);
595 IM(SRTO_RCVLATENCY, iRcvLatency);
596 IM(SRTO_PEERLATENCY, iPeerLatency);
597 IM(SRTO_SNDDROPDELAY, iSndDropDelay);
598 // Special handling of SRTO_PAYLOADSIZE because API stores the value as int32_t,
599 // while the config structure stores it as size_t.
600 importTrivialOption(m_config, SRTO_PAYLOADSIZE, (int)u->m_config.zExpPayloadSize);
601 IMF(SRTO_TLPKTDROP, m_bTLPktDrop);
602
603 importStringOption(m_config, SRTO_STREAMID, u->m_config.sStreamName);
604
605 IM(SRTO_MESSAGEAPI, bMessageAPI);
606 IM(SRTO_NAKREPORT, bRcvNakReport);
607 IM(SRTO_MINVERSION, uMinimumPeerSrtVersion);
608 IM(SRTO_ENFORCEDENCRYPTION, bEnforcedEnc);
609 IM(SRTO_IPV6ONLY, iIpV6Only);
610 IM(SRTO_PEERIDLETIMEO, iPeerIdleTimeout_ms);
611
612 importStringOption(m_config, SRTO_PACKETFILTER, u->m_config.sPacketFilterConfig);
613
614 importTrivialOption(m_config, SRTO_PBKEYLEN, (int) u->m_pCryptoControl->KeyLen());
615
616 // Passphrase is empty by default. Decipher the passphrase and
617 // store as passphrase option
618 if (u->m_config.CryptoSecret.len)
619 {
620 const StringStorage<HAICRYPT_SECRET_MAX_SZ> password((const char*)u->m_config.CryptoSecret.str, u->m_config.CryptoSecret.len);
621 importStringOption(m_config, SRTO_PASSPHRASE, password);
622 }
623
624 IM(SRTO_KMREFRESHRATE, uKmRefreshRatePkt);
625 IM(SRTO_KMPREANNOUNCE, uKmPreAnnouncePkt);
626
627 const string cc = u->m_CongCtl.selected_name();
628 if (cc != "live")
629 {
630 m_config.push_back(ConfigItem(SRTO_CONGESTION, cc.c_str(), (int)cc.size()));
631 }
632
633 // NOTE: This is based on information extracted from the "semi-copy-constructor" of CUDT class.
634 // Here should be handled all things that are options that modify the socket, but not all options
635 // are assigned to configurable items.
636
637 #undef IM
638 #undef IMF
639 }
640
641 // XXX This function is likely of no use now.
642 bool CUDTGroup::applyFlags(uint32_t flags, HandshakeSide)
643 {
644 const bool synconmsg = IsSet(flags, SRT_GFLAG_SYNCONMSG);
645 if (synconmsg)
646 {
647 LOGP(gmlog.Error, "GROUP: requested sync on msgno - not supported.");
648 return false;
649 }
650
651 return true;
652 }
653
654 template <class Type>
655 struct Value
656 {
657 static int fill(void* optval, int len, const Type& value)
658 {
659 if (size_t(len) < sizeof(Type))
660 return 0;
661
662 *(Type*)optval = value;
663 return sizeof(Type);
664 }
665 };
666
667 template <>
668 inline int Value<std::string>::fill(void* optval, int len, const std::string& value)
669 {
670 if (size_t(len) < value.size())
671 return 0;
672 memcpy(optval, value.c_str(), value.size());
673 return (int) value.size();
674 }
675
676 template <class V>
677 inline int fillValue(void* optval, int len, V value)
678 {
679 return Value<V>::fill(optval, len, value);
680 }
681
682 static bool getOptDefault(SRT_SOCKOPT optname, void* pw_optval, int& w_optlen)
683 {
684 static const linger def_linger = {1, CSrtConfig::DEF_LINGER_S};
685 switch (optname)
686 {
687 default:
688 return false;
689
690 #define RD(value) \
691 w_optlen = fillValue((pw_optval), w_optlen, value); \
692 break
693
694 case SRTO_KMSTATE:
695 case SRTO_SNDKMSTATE:
696 case SRTO_RCVKMSTATE:
697 RD(SRT_KM_S_UNSECURED);
698 case SRTO_PBKEYLEN:
699 RD(16);
700
701 case SRTO_MSS:
702 RD(CSrtConfig::DEF_MSS);
703
704 case SRTO_SNDSYN:
705 RD(true);
706 case SRTO_RCVSYN:
707 RD(true);
708 case SRTO_ISN:
709 RD(SRT_SEQNO_NONE);
710 case SRTO_FC:
711 RD(CSrtConfig::DEF_FLIGHT_SIZE);
712
713 case SRTO_SNDBUF:
714 case SRTO_RCVBUF:
715 w_optlen = fillValue<int>((pw_optval), w_optlen, CSrtConfig::DEF_BUFFER_SIZE * (CSrtConfig::DEF_MSS - CPacket::UDP_HDR_SIZE));
716 break;
717
718 case SRTO_LINGER:
719 RD(def_linger);
720 case SRTO_UDP_SNDBUF:
721 case SRTO_UDP_RCVBUF:
722 RD(CSrtConfig::DEF_UDP_BUFFER_SIZE);
723 case SRTO_RENDEZVOUS:
724 RD(false);
725 case SRTO_SNDTIMEO:
726 RD(-1);
727 case SRTO_RCVTIMEO:
728 RD(-1);
729 case SRTO_REUSEADDR:
730 RD(true);
731 case SRTO_MAXBW:
732 RD(int64_t(-1));
733 #ifdef ENABLE_MAXREXMITBW
734 case SRTO_MAXREXMITBW:
735 RD(int64_t(-1));
736 #endif
737 case SRTO_INPUTBW:
738 RD(int64_t(-1));
739 case SRTO_MININPUTBW:
740 RD(int64_t(0));
741 case SRTO_OHEADBW:
742 RD(SRT_OHEAD_DEFAULT_P100);
743 case SRTO_STATE:
744 RD(SRTS_INIT);
745 case SRTO_EVENT:
746 RD(0);
747 case SRTO_SNDDATA:
748 RD(0);
749 case SRTO_RCVDATA:
750 RD(0);
751
752 case SRTO_IPTTL:
753 RD(0);
754 case SRTO_IPTOS:
755 RD(0);
756
757 case SRTO_SENDER:
758 RD(false);
759 case SRTO_TSBPDMODE:
760 RD(false);
761 case SRTO_LATENCY:
762 case SRTO_RCVLATENCY:
763 RD(SRT_LIVE_DEF_LATENCY_MS);
764 case SRTO_PEERLATENCY:
765 RD(0);
766 case SRTO_TLPKTDROP:
767 RD(true);
768 case SRTO_SNDDROPDELAY:
769 RD(-1);
770 case SRTO_NAKREPORT:
771 RD(true);
772 case SRTO_VERSION:
773 RD(SRT_DEF_VERSION);
774 case SRTO_PEERVERSION:
775 RD(0);
776 case SRTO_PEERIDLETIMEO:
777 RD(CSrtConfig::COMM_RESPONSE_TIMEOUT_MS);
778 case SRTO_CONNTIMEO:
779 RD(CSrtConfig::DEF_CONNTIMEO_S * 1000); // required milliseconds
780 case SRTO_DRIFTTRACER:
781 RD(true);
782
783 case SRTO_MINVERSION:
784 RD(SRT_VERSION_MAJ1);
785 case SRTO_STREAMID:
786 RD(std::string());
787 case SRTO_CONGESTION:
788 RD(std::string());
789 case SRTO_MESSAGEAPI:
790 RD(true);
791 case SRTO_PAYLOADSIZE:
792 RD(0);
793 case SRTO_GROUPMINSTABLETIMEO:
794 RD(CSrtConfig::COMM_DEF_MIN_STABILITY_TIMEOUT_MS);
795 case SRTO_LOSSMAXTTL:
796 RD(0);
797 case SRTO_RETRANSMITALGO:
798 RD(1);
799 }
800
801 #undef RD
802 return true;
803 }
804
805 struct FOptionValue
806 {
807 SRT_SOCKOPT expected;
808 FOptionValue(SRT_SOCKOPT v): expected(v) {}
809 bool operator()(const CUDTGroup::ConfigItem& i) const
810 {
811 return i.so == expected;
812 }
813 };
814
815 void CUDTGroup::getOpt(SRT_SOCKOPT optname, void* pw_optval, int& w_optlen)
816 {
817 // Options handled in group
818 switch (optname)
819 {
820 case SRTO_RCVSYN:
821 *(bool*)pw_optval = m_bSynRecving;
822 w_optlen = sizeof(bool);
823 return;
824
825 case SRTO_SNDSYN:
826 *(bool*)pw_optval = m_bSynSending;
827 w_optlen = sizeof(bool);
828 return;
829
830 case SRTO_SNDTIMEO:
831 *(int*)pw_optval = m_iSndTimeOut;
832 w_optlen = sizeof(int);
833 return;
834
835 case SRTO_RCVTIMEO:
836 *(int*)pw_optval = m_iRcvTimeOut;
837 w_optlen = sizeof(int);
838 return;
839
840 case SRTO_GROUPMINSTABLETIMEO:
841 *(uint32_t*)pw_optval = m_uOPT_MinStabilityTimeout_us / 1000;
842 w_optlen = sizeof(uint32_t);
843 return;
844
845 case SRTO_KMSTATE:
846 *(uint32_t*)pw_optval = getGroupEncryptionState();
847 w_optlen = sizeof(uint32_t);
848 return;
849
850 // Write-only options for security reasons or
851 // options that refer to a socket state, that
852 // makes no sense for a group.
853 case SRTO_PASSPHRASE:
854 case SRTO_KMPREANNOUNCE:
855 case SRTO_KMREFRESHRATE:
856 case SRTO_BINDTODEVICE:
857 case SRTO_GROUPCONNECT:
858 case SRTO_STATE:
859 case SRTO_EVENT:
860 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
861
862 default:; // pass on
863 }
864
865 bool is_set_on_socket = false;
866 {
867 // Can't have m_GroupLock locked while calling getOpt on a member socket
868 // because the call will acquire m_ControlLock leading to a lock-order-inversion.
869 SRTSOCKET firstsocket = SRT_INVALID_SOCK;
870 enterCS(m_GroupLock);
871 gli_t gi = m_Group.begin();
872 if (gi != m_Group.end())
873 firstsocket = gi->ps->core().id();
874 leaveCS(m_GroupLock);
875 // CUDTUnited::m_GlobControlLock can't be acquired with m_GroupLock either.
876 // We have also no guarantee that after leaving m_GroupLock the socket isn't
877 // going to be deleted. Hence use the safest method by extracting through the id.
878 if (firstsocket != SRT_INVALID_SOCK)
879 {
880 CUDTUnited::SocketKeeper sk(CUDT::uglobal(), firstsocket);
881 if (sk.socket)
882 {
883 // Return the value from the first member socket, if any is present
884 // Note: Will throw exception if the request is wrong.
885 sk.socket->core().getOpt(optname, (pw_optval), (w_optlen));
886 is_set_on_socket = true;
887 }
888 }
889 }
890
891 // Check if the option is in the storage, which means that
892 // it was modified on the group.
893 vector<ConfigItem>::const_iterator i = find_if(m_config.begin(), m_config.end(), FOptionValue(optname));
894
895 if (i == m_config.end() || i->value.empty())
896 {
897 // Already written to the target variable.
898 if (is_set_on_socket)
899 return;
900
901 // Not found, see the defaults
902 if (!getOptDefault(optname, (pw_optval), (w_optlen)))
903 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
904
905 return;
906 }
907
908 // Found a value set on or derived by a group. Prefer returning it over the one taken from a member socket.
909 // Check the size first.
910 if (w_optlen < int(i->value.size()))
911 throw CUDTException(MJ_NOTSUP, MN_XSIZE, 0);
912
913 SRT_ASSERT(!i->value.empty());
914 w_optlen = (int)i->value.size();
915 memcpy((pw_optval), &i->value[0], i->value.size());
916 }
917
918 SRT_KM_STATE CUDTGroup::getGroupEncryptionState()
919 {
920 multiset<SRT_KM_STATE> kmstates;
921 {
922 ScopedLock lk (m_GroupLock);
923
924 // First check the container. If empty, return UNSECURED
925 if (m_Group.empty())
926 return SRT_KM_S_UNSECURED;
927
928 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
929 {
930 CCryptoControl* cc = gi->ps->core().m_pCryptoControl.get();
931 if (!cc)
932 continue;
933 SRT_KM_STATE gst = cc->m_RcvKmState;
934 // A fix to NOSECRET is because this is the state when agent has set
935 // no password, but peer did, and ENFORCEDENCRYPTION=false allowed
936 // this connection to be established. UNSECURED can't be taken in this
937 // case because this would suggest that BOTH are unsecured, that is,
938 // we have established an unsecured connection (which is not true).
939 if (gst == SRT_KM_S_UNSECURED && cc->m_SndKmState == SRT_KM_S_NOSECRET)
940 gst = SRT_KM_S_NOSECRET;
941 kmstates.insert(gst);
942 }
943 }
944
945 // Criteria are:
946 // 1. UNSECURED, if no member sockets, or at least one UNSECURED found.
947 // 2. SECURED, if at least one SECURED found (cut off the previous criteria).
948 // 3. BADSECRET otherwise, although return NOSECRET if no BADSECRET is found.
949
950 if (kmstates.count(SRT_KM_S_UNSECURED))
951 return SRT_KM_S_UNSECURED;
952
953 // Now we have UNSECURED ruled out. Remaining may be NOSECRET, BADSECRET or SECURED.
954 // NOTE: SECURING is an intermediate state for HSv4 and can't occur in groups.
955 if (kmstates.count(SRT_KM_S_SECURED))
956 return SRT_KM_S_SECURED;
957
958 if (kmstates.count(SRT_KM_S_BADSECRET))
959 return SRT_KM_S_BADSECRET;
960
961 return SRT_KM_S_NOSECRET;
962 }
963
964 SRT_SOCKSTATUS CUDTGroup::getStatus()
965 {
966 typedef vector<pair<SRTSOCKET, SRT_SOCKSTATUS> > states_t;
967 states_t states;
968
969 {
970 ScopedLock cg(m_GroupLock);
971 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
972 {
973 switch (gi->sndstate)
974 {
975 // Check only sndstate. If this machine is ONLY receiving,
976 // then rcvstate will turn into SRT_GST_RUNNING, while
977 // sndstate will remain SRT_GST_IDLE, but still this may only
978 // happen if the socket is connected.
979 case SRT_GST_IDLE:
980 case SRT_GST_RUNNING:
981 states.push_back(make_pair(gi->id, SRTS_CONNECTED));
982 break;
983
984 case SRT_GST_BROKEN:
985 states.push_back(make_pair(gi->id, SRTS_BROKEN));
986 break;
987
988 default: // (pending, or whatever will be added in future)
989 {
990 // TEMPORARY make a node to note a socket to be checked afterwards
991 states.push_back(make_pair(gi->id, SRTS_NONEXIST));
992 }
993 }
994 }
995 }
996
997 SRT_SOCKSTATUS pending_state = SRTS_NONEXIST;
998
999 for (states_t::iterator i = states.begin(); i != states.end(); ++i)
1000 {
1001 // If at least one socket is connected, the state is connected.
1002 if (i->second == SRTS_CONNECTED)
1003 return SRTS_CONNECTED;
1004
1005 // Second level - pick up the state
1006 if (i->second == SRTS_NONEXIST)
1007 {
1008 // Otherwise find at least one socket, which's state isn't broken.
1009 i->second = m_Global.getStatus(i->first);
1010 if (pending_state == SRTS_NONEXIST)
1011 pending_state = i->second;
1012 }
1013 }
1014
1015 // Return that state as group state
1016 if (pending_state != SRTS_NONEXIST) // did call getStatus at least once and it didn't return NOEXIST
1017 return pending_state;
1018
1019 // If none found, return SRTS_BROKEN.
1020 return SRTS_BROKEN;
1021 }
1022
1023 // [[using locked(m_GroupLock)]];
1024 void CUDTGroup::syncWithSocket(const CUDT& core, const HandshakeSide side)
1025 {
1026 if (side == HSD_RESPONDER)
1027 {
1028 // On the listener side you should synchronize ISN with the incoming
1029 // socket, which is done immediately after creating the socket and
1030 // adding it to the group. On the caller side the ISN is defined in
1031 // the group directly, before any member socket is created.
1032 set_currentSchedSequence(core.ISN());
1033 }
1034
1035 // Only set if was not initialized to avoid problems on a running connection.
1036 if (m_RcvBaseSeqNo == SRT_SEQNO_NONE)
1037 m_RcvBaseSeqNo = CSeqNo::decseq(core.m_iPeerISN);
1038
1039 // Get the latency (possibly fixed against the opposite side)
1040 // from the first socket (core.m_iTsbPdDelay_ms),
1041 // and set it on the current socket.
1042 set_latency_us(core.m_iTsbPdDelay_ms * int64_t(1000));
1043 }
1044
1045 void CUDTGroup::close()
1046 {
1047 // Close all descriptors, then delete the group.
1048 vector<SRTSOCKET> ids;
1049
1050 {
1051 ExclusiveLock glob(CUDT::uglobal().m_GlobControlLock);
1052 ScopedLock g(m_GroupLock);
1053
1054 m_bClosing = true;
1055
1056 // Copy the list of IDs into the array.
1057 for (gli_t ig = m_Group.begin(); ig != m_Group.end(); ++ig)
1058 {
1059 ids.push_back(ig->id);
1060 // Immediately cut ties to this group.
1061 // Just for a case, redispatch the socket, to stay safe.
1062 CUDTSocket* s = CUDT::uglobal().locateSocket_LOCKED(ig->id);
1063 if (!s)
1064 {
1065 HLOGC(smlog.Debug, log << "group/close: IPE(NF): group member @" << ig->id << " already deleted");
1066 continue;
1067 }
1068
1069 // Make the socket closing BEFORE withdrawing its group membership
1070 // because a socket created as a group member cannot be valid
1071 // without the group.
1072 // This is not true in case of non-managed groups, which
1073 // only collect sockets, but also non-managed groups should not
1074 // use common group buffering and tsbpd. Also currently there are
1075 // no other groups than managed one.
1076 s->setClosing();
1077
1078 s->m_GroupOf = NULL;
1079 s->m_GroupMemberData = NULL;
1080 HLOGC(smlog.Debug, log << "group/close: CUTTING OFF @" << ig->id << " (found as @" << s->m_SocketID << ") from the group");
1081 }
1082
1083 // After all sockets that were group members have their ties cut,
1084 // the container can be cleared. Note that sockets won't be now
1085 // removing themselves from the group when closing because they
1086 // are unaware of being group members.
1087 m_Group.clear();
1088 m_PeerGroupID = -1;
1089
1090 set<int> epollid;
1091 {
1092 // Global EPOLL lock must be applied to access any socket's epoll set.
1093 // This is a set of all epoll ids subscribed to it.
1094 ScopedLock elock (CUDT::uglobal().m_EPoll.m_EPollLock);
1095 epollid = m_sPollID; // use move() in C++11
1096 m_sPollID.clear();
1097 }
1098
1099 int no_events = 0;
1100 for (set<int>::iterator i = epollid.begin(); i != epollid.end(); ++i)
1101 {
1102 HLOGC(smlog.Debug, log << "close: CLEARING subscription on E" << (*i) << " of $" << id());
1103 try
1104 {
1105 CUDT::uglobal().m_EPoll.update_usock(*i, id(), &no_events);
1106 }
1107 catch (...)
1108 {
1109 // May catch an API exception, but this isn't an API call to be interrupted.
1110 }
1111 HLOGC(smlog.Debug, log << "close: removing E" << (*i) << " from back-subscribers of $" << id());
1112 }
1113
1114 // NOW, the m_GroupLock is released, then m_GlobControlLock.
1115 // The below code should work with no locks and execute socket
1116 // closing.
1117 }
1118
1119 HLOGC(gmlog.Debug, log << "grp/close: closing $" << m_GroupID << ", closing first " << ids.size() << " sockets:");
1120 // Close all sockets with unlocked GroupLock
1121 for (vector<SRTSOCKET>::iterator i = ids.begin(); i != ids.end(); ++i)
1122 {
1123 try
1124 {
1125 CUDT::uglobal().close(*i);
1126 }
1127 catch (CUDTException&)
1128 {
1129 HLOGC(gmlog.Debug, log << "grp/close: socket @" << *i << " is likely closed already, ignoring");
1130 }
1131 }
1132
1133 HLOGC(gmlog.Debug, log << "grp/close: closing $" << m_GroupID << ": sockets closed, clearing the group:");
1134
1135 // Lock the group again to clear the group data
1136 {
1137 ScopedLock g(m_GroupLock);
1138
1139 if (!m_Group.empty())
1140 {
1141 LOGC(gmlog.Error, log << "grp/close: IPE - after requesting to close all members, still " << m_Group.size()
1142 << " lingering members!");
1143 m_Group.clear();
1144 }
1145
1146 // This takes care of the internal part.
1147 // The external part will be done in Global (CUDTUnited)
1148 }
1149
1150 // Release blocked clients
1151 // XXX This looks like a dead code. Group receiver functions
1152 // do not use any lock on m_RcvDataLock, it is likely a remainder
1153 // of the old, internal implementation.
1154 // CSync::lock_notify_one(m_RcvDataCond, m_RcvDataLock);
1155 }
1156
1157 // [[using locked(m_Global->m_GlobControlLock)]]
1158 // [[using locked(m_GroupLock)]]
1159 void CUDTGroup::send_CheckValidSockets()
1160 {
1161 vector<gli_t> toremove;
1162
1163 for (gli_t d = m_Group.begin(), d_next = d; d != m_Group.end(); d = d_next)
1164 {
1165 ++d_next; // it's now safe to erase d
1166 CUDTSocket* revps = m_Global.locateSocket_LOCKED(d->id);
1167 if (revps != d->ps)
1168 {
1169 // Note: the socket might STILL EXIST, just in the trash, so
1170 // it can't be found by locateSocket. But it can still be bound
1171 // to the group. Just mark it broken from upside so that the
1172 // internal sending procedures will skip it. Removal from the
1173 // group will happen in GC, which will both remove from
1174 // group container and cut backward links to the group.
1175
1176 HLOGC(gmlog.Debug, log << "group/send_CheckValidSockets: socket @" << d->id << " is no longer valid, setting BROKEN in $" << id());
1177 d->sndstate = SRT_GST_BROKEN;
1178 d->rcvstate = SRT_GST_BROKEN;
1179 }
1180 }
1181 }
1182
1183 int CUDTGroup::send(const char* buf, int len, SRT_MSGCTRL& w_mc)
1184 {
1185 switch (m_type)
1186 {
1187 default:
1188 LOGC(gslog.Error, log << "CUDTGroup::send: not implemented for type #" << m_type);
1189 throw CUDTException(MJ_SETUP, MN_INVAL, 0);
1190
1191 case SRT_GTYPE_BROADCAST:
1192 return sendBroadcast(buf, len, (w_mc));
1193
1194 case SRT_GTYPE_BACKUP:
1195 return sendBackup(buf, len, (w_mc));
1196
1197 /* to be implemented
1198
1199 case SRT_GTYPE_BALANCING:
1200 return sendBalancing(buf, len, (w_mc));
1201
1202 case SRT_GTYPE_MULTICAST:
1203 return sendMulticast(buf, len, (w_mc));
1204 */
1205 }
1206 }
1207
1208 int CUDTGroup::sendBroadcast(const char* buf, int len, SRT_MSGCTRL& w_mc)
1209 {
1210 // Avoid stupid errors in the beginning.
1211 if (len <= 0)
1212 {
1213 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
1214 }
1215
1216 // NOTE: This is a "vector of list iterators". Every element here
1217 // is an iterator to another container.
1218 // Note that "list" is THE ONLY container in standard C++ library,
1219 // for which NO ITERATORS ARE INVALIDATED after a node at particular
1220 // iterator has been removed, except for that iterator itself.
1221 vector<SRTSOCKET> wipeme;
1222 vector<gli_t> idleLinks;
1223 vector<SRTSOCKET> pendingSockets; // need sock ids as it will be checked out of lock
1224
1225 int32_t curseq = SRT_SEQNO_NONE; // The seqno of the first packet of this message.
1226 int32_t nextseq = SRT_SEQNO_NONE; // The seqno of the first packet of next message.
1227
1228 int rstat = -1;
1229
1230 int stat = 0;
1231 SRT_ATR_UNUSED CUDTException cx(MJ_SUCCESS, MN_NONE, 0);
1232
1233 vector<gli_t> activeLinks;
1234
1235 // First, acquire GlobControlLock to make sure all member sockets still exist
1236 enterCS(m_Global.m_GlobControlLock);
1237 ScopedLock guard(m_GroupLock);
1238
1239 if (m_bClosing)
1240 {
1241 leaveCS(m_Global.m_GlobControlLock);
1242 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1243 }
1244
1245 // Now, still under lock, check if all sockets still can be dispatched
1246
1247 // LOCKED: GlobControlLock, GroupLock (RIGHT ORDER!)
1248 send_CheckValidSockets();
1249 leaveCS(m_Global.m_GlobControlLock);
1250 // LOCKED: GroupLock (only)
1251 // Since this moment GlobControlLock may only be locked if GroupLock is unlocked first.
1252
1253 if (m_bClosing)
1254 {
1255 // No temporary locks here. The group lock is scoped.
1256 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1257 }
1258
1259 // This simply requires the payload to be sent through every socket in the group
1260 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d)
1261 {
1262 if (d->sndstate != SRT_GST_BROKEN)
1263 {
1264 // Check the socket state prematurely in order not to uselessly
1265 // send over a socket that is broken.
1266 CUDT* const pu = (d->ps)
1267 ? &d->ps->core()
1268 : NULL;
1269
1270 if (!pu || pu->m_bBroken)
1271 {
1272 HLOGC(gslog.Debug,
1273 log << "grp/sendBroadcast: socket @" << d->id << " detected +Broken - transit to BROKEN");
1274 d->sndstate = SRT_GST_BROKEN;
1275 d->rcvstate = SRT_GST_BROKEN;
1276 }
1277 }
1278
1279 // Check socket sndstate before sending
1280 if (d->sndstate == SRT_GST_BROKEN)
1281 {
1282 HLOGC(gslog.Debug,
1283 log << "grp/sendBroadcast: socket in BROKEN state: @" << d->id
1284 << ", sockstatus=" << SockStatusStr(d->ps ? d->ps->getStatus() : SRTS_NONEXIST));
1285 wipeme.push_back(d->id);
1286 continue;
1287 }
1288
1289 if (d->sndstate == SRT_GST_IDLE)
1290 {
1291 SRT_SOCKSTATUS st = SRTS_NONEXIST;
1292 if (d->ps)
1293 st = d->ps->getStatus();
1294 // If the socket is already broken, move it to broken.
1295 if (int(st) >= int(SRTS_BROKEN))
1296 {
1297 HLOGC(gslog.Debug,
1298 log << "CUDTGroup::send.$" << id() << ": @" << d->id << " became " << SockStatusStr(st)
1299 << ", WILL BE CLOSED.");
1300 wipeme.push_back(d->id);
1301 continue;
1302 }
1303
1304 if (st != SRTS_CONNECTED)
1305 {
1306 HLOGC(gslog.Debug,
1307 log << "CUDTGroup::send. @" << d->id << " is still " << SockStatusStr(st) << ", skipping.");
1308 pendingSockets.push_back(d->id);
1309 continue;
1310 }
1311
1312 HLOGC(gslog.Debug, log << "grp/sendBroadcast: socket in IDLE state: @" << d->id << " - will activate it");
1313 // This is idle, we'll take care of them next time
1314 // Might be that:
1315 // - this socket is idle, while some NEXT socket is running
1316 // - we need at least one running socket to work BEFORE activating the idle one.
1317 // - if ALL SOCKETS ARE IDLE, then we simply activate the first from the list,
1318 // and all others will be activated using the ISN from the first one.
1319 idleLinks.push_back(d);
1320 continue;
1321 }
1322
1323 if (d->sndstate == SRT_GST_RUNNING)
1324 {
1325 HLOGC(gslog.Debug,
1326 log << "grp/sendBroadcast: socket in RUNNING state: @" << d->id << " - will send a payload");
1327 activeLinks.push_back(d);
1328 continue;
1329 }
1330
1331 HLOGC(gslog.Debug,
1332 log << "grp/sendBroadcast: socket @" << d->id << " not ready, state: " << StateStr(d->sndstate) << "("
1333 << int(d->sndstate) << ") - NOT sending, SET AS PENDING");
1334
1335 pendingSockets.push_back(d->id);
1336 }
1337
1338 vector<Sendstate> sendstates;
1339 if (w_mc.srctime == 0)
1340 w_mc.srctime = count_microseconds(steady_clock::now().time_since_epoch());
1341
1342 for (vector<gli_t>::iterator snd = activeLinks.begin(); snd != activeLinks.end(); ++snd)
1343 {
1344 gli_t d = *snd;
1345 int erc = 0; // success
1346 // Remaining sndstate is SRT_GST_RUNNING. Send a payload through it.
1347 try
1348 {
1349 // This must be wrapped in try-catch because on error it throws an exception.
1350 // Possible return values are only 0, in case when len was passed 0, or a positive
1351 // >0 value that defines the size of the data that it has sent, that is, in case
1352 // of Live mode, equal to 'len'.
1353 stat = d->ps->core().sendmsg2(buf, len, (w_mc));
1354 }
1355 catch (CUDTException& e)
1356 {
1357 cx = e;
1358 stat = -1;
1359 erc = e.getErrorCode();
1360 }
1361
1362 if (stat != -1)
1363 {
1364 curseq = w_mc.pktseq;
1365 nextseq = d->ps->core().schedSeqNo();
1366 }
1367
1368 const Sendstate cstate = {d->id, &*d, stat, erc};
1369 sendstates.push_back(cstate);
1370 d->sndresult = stat;
1371 d->laststatus = d->ps->getStatus();
1372 }
1373
1374 // Ok, we have attempted to send a payload over all links
1375 // that are currently in the RUNNING state. We know that at
1376 // least one is successful if we have non-default curseq value.
1377
1378 // Here we need to activate all links that are found as IDLE.
1379 // Some portion of logical exclusions:
1380 //
1381 // - sockets that were broken in the beginning are already wiped out
1382 // - broken sockets are checked first, so they can't be simultaneously idle
1383 // - idle sockets can't get broken because there's no operation done on them
1384 // - running sockets are the only one that could change sndstate here
1385 // - running sockets can either remain running or turn to broken
1386 // In short: Running and Broken sockets can't become idle,
1387 // although Running sockets can become Broken.
1388
1389 // There's no certainty here as to whether at least one link was
1390 // running and it has successfully performed the operation.
1391 // Might have even happened that we had 2 running links that
1392 // got broken and 3 other links so far in idle sndstate that just connected
1393 // at that very moment. In this case we have 3 idle links to activate,
1394 // but there is no sequence base to overwrite their ISN with. If this
1395 // happens, then the first link that should be activated goes with
1396 // whatever ISN it has, whereas every next idle link should use that
1397 // exactly ISN.
1398 //
1399 // If it has additionally happened that the first link got broken at
1400 // that very moment of sending, the second one has a chance to succeed
1401 // and therefore take over the leading role in setting the ISN. If the
1402 // second one fails, too, then the only remaining idle link will simply
1403 // go with its own original sequence.
1404
1405 // On the opposite side, if the first packet arriving looks like a jump over,
1406 // the corresponding LOSSREPORT is sent. For packets that are truly lost,
1407 // the sender retransmits them, for packets that before ISN, DROPREQ is sent.
1408
1409 // Now we can go to the idle links and attempt to send the payload
1410 // also over them.
1411
1412 // TODO: { sendBroadcast_ActivateIdleLinks
1413 for (vector<gli_t>::iterator i = idleLinks.begin(); i != idleLinks.end(); ++i)
1414 {
1415 gli_t d = *i;
1416 if (!d->ps->m_GroupOf)
1417 continue;
1418
1419 int erc = 0;
1420 int lastseq = d->ps->core().schedSeqNo();
1421 if (curseq != SRT_SEQNO_NONE && curseq != lastseq)
1422 {
1423 HLOGC(gslog.Debug,
1424 log << "grp/sendBroadcast: socket @" << d->id << ": override snd sequence %" << lastseq << " with %"
1425 << curseq << " (diff by " << CSeqNo::seqcmp(curseq, lastseq)
1426 << "); SENDING PAYLOAD: " << BufferStamp(buf, len));
1427 d->ps->core().overrideSndSeqNo(curseq);
1428 }
1429 else
1430 {
1431 HLOGC(gslog.Debug,
1432 log << "grp/sendBroadcast: socket @" << d->id << ": sequence remains with original value: %"
1433 << lastseq << "; SENDING PAYLOAD " << BufferStamp(buf, len));
1434 }
1435
1436 // Now send and check the status
1437 // The link could have got broken
1438
1439 try
1440 {
1441 stat = d->ps->core().sendmsg2(buf, len, (w_mc));
1442 }
1443 catch (CUDTException& e)
1444 {
1445 cx = e;
1446 stat = -1;
1447 erc = e.getErrorCode();
1448 }
1449
1450 if (stat != -1)
1451 {
1452 d->sndstate = SRT_GST_RUNNING;
1453
1454 // Note: this will override the sequence number
1455 // for all next iterations in this loop.
1456 curseq = w_mc.pktseq;
1457 nextseq = d->ps->core().schedSeqNo();
1458 HLOGC(gslog.Debug,
1459 log << "@" << d->id << ":... sending SUCCESSFUL %" << curseq << " MEMBER STATUS: RUNNING");
1460 }
1461
1462 d->sndresult = stat;
1463 d->laststatus = d->ps->getStatus();
1464
1465 const Sendstate cstate = {d->id, &*d, stat, erc};
1466 sendstates.push_back(cstate);
1467 }
1468
1469 if (nextseq != SRT_SEQNO_NONE)
1470 {
1471 HLOGC(gslog.Debug,
1472 log << "grp/sendBroadcast: $" << id() << ": updating current scheduling sequence %" << nextseq);
1473 m_iLastSchedSeqNo = nextseq;
1474 }
1475
1476 // }
1477
1478 // { send_CheckBrokenSockets()
1479
1480 // Make an extra loop check to see if we could be
1481 // in a condition of "all sockets either blocked or pending"
1482
1483 int nsuccessful = 0; // number of successfully connected sockets
1484 int nblocked = 0; // number of sockets blocked in connection
1485 bool is_pending_blocked = false;
1486 for (vector<Sendstate>::iterator is = sendstates.begin(); is != sendstates.end(); ++is)
1487 {
1488 if (is->stat != -1)
1489 {
1490 nsuccessful++;
1491 }
1492 // is->stat == -1
1493 else if (is->code == SRT_EASYNCSND)
1494 {
1495 ++nblocked;
1496 }
1497 }
1498
1499 if (!pendingSockets.empty() || nblocked)
1500 {
1501 HLOGC(gslog.Debug, log << "grp/sendBroadcast: found pending sockets (blocked: " << nblocked << "), polling them.");
1502
1503 // These sockets if they are in pending state, they should be added to m_SndEID
1504 // at the connecting stage.
1505 CEPoll::fmap_t sready;
1506
1507 if (m_Global.m_EPoll.empty(*m_SndEpolld))
1508 {
1509 // Sanity check - weird pending reported.
1510 LOGC(gslog.Error,
1511 log << "grp/sendBroadcast: IPE: reported pending sockets, but EID is empty - wiping pending!");
1512 copy(pendingSockets.begin(), pendingSockets.end(), back_inserter(wipeme));
1513 }
1514 else
1515 {
1516 int swait_timeout = 0;
1517
1518 // There's also a hidden condition here that is the upper if condition.
1519 is_pending_blocked = (nsuccessful == 0);
1520
1521 // If this is the case when
1522 if (m_bSynSending && is_pending_blocked)
1523 {
1524 HLOGC(gslog.Debug, log << "grp/sendBroadcast: will block for " << m_iSndTimeOut << " - waiting for any writable in blocking mode");
1525 swait_timeout = m_iSndTimeOut;
1526 }
1527
1528 {
1529 InvertedLock ug(m_GroupLock);
1530
1531 THREAD_PAUSED();
1532 m_Global.m_EPoll.swait(
1533 *m_SndEpolld, (sready), swait_timeout, false /*report by retval*/); // Just check if anything happened
1534 THREAD_RESUMED();
1535 }
1536
1537 if (m_bClosing)
1538 {
1539 // No temporary locks here. The group lock is scoped.
1540 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1541 }
1542
1543 HLOGC(gslog.Debug, log << "grp/sendBroadcast: RDY: " << DisplayEpollResults(sready));
1544
1545 // sockets in EX: should be moved to wipeme.
1546 // IMPORTANT: we check only PENDING sockets (not blocked) because only
1547 // pending sockets might report ERR epoll without being explicitly broken.
1548 // Sockets that did connect and just have buffer full will be always broken,
1549 // if they're going to report ERR in epoll.
1550 for (vector<SRTSOCKET>::iterator i = pendingSockets.begin(); i != pendingSockets.end(); ++i)
1551 {
1552 if (CEPoll::isready(sready, *i, SRT_EPOLL_ERR))
1553 {
1554 HLOGC(gslog.Debug,
1555 log << "grp/sendBroadcast: Socket @" << (*i) << " reported FAILURE - moved to wiped.");
1556 // Failed socket. Move d to wipeme. Remove from eid.
1557 wipeme.push_back(*i);
1558 int no_events = 0;
1559 m_Global.m_EPoll.update_usock(m_SndEID, *i, &no_events);
1560 }
1561
1562 if (CEPoll::isready(sready, *i, SRT_EPOLL_OUT))
1563 is_pending_blocked = false;
1564 }
1565
1566 // After that, all sockets that have been reported
1567 // as ready to write should be removed from EID. This
1568 // will also remove those sockets that have been added
1569 // as redundant links at the connecting stage and became
1570 // writable (connected) before this function had a chance
1571 // to check them.
1572 m_Global.m_EPoll.clear_ready_usocks(*m_SndEpolld, SRT_EPOLL_CONNECT);
1573 }
1574 }
1575
1576 // Re-check after the waiting lock has been reacquired
1577 if (m_bClosing)
1578 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1579
1580 // Just for a case, when a socket that was blocked or pending
1581 // had switched to write-enabled,
1582
1583 send_CloseBrokenSockets((wipeme)); // wipeme will be cleared by this function
1584
1585 // Re-check after the waiting lock has been reacquired
1586 if (m_bClosing)
1587 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1588
1589 // }
1590
1591 // { sendBroadcast_CheckBlockedLinks()
1592
1593 // Alright, we've made an attempt to send a packet over every link.
1594 // Every operation was done through a non-blocking attempt, so
1595 // links where sending was blocked have SRT_EASYNCSND error.
1596 // Links that were successful, have the len value in state.
1597
1598 // First thing then, find out if at least one link was successful.
1599 // The first successful link sets the sequence value,
1600 // the following links derive it. This might be also the first idle
1601 // link with its random-generated ISN, if there were no active links.
1602
1603 vector<SocketData*> successful, blocked;
1604
1605 // This iteration of the state will simply
1606 // qualify the remaining sockets into three categories:
1607 //
1608 // - successful (we only need to know if at least one did)
1609 // - blocked - if none succeeded, but some blocked, POLL & RETRY.
1610 // - wipeme - sending failed by any other reason than blocking, remove.
1611
1612 // Now - sendstates contain directly sockets.
1613 // In order to update members, you need to have locked:
1614 // - GlobControlLock to prevent sockets from disappearing or being closed
1615 // - then GroupLock to latch the validity of m_GroupMemberData field.
1616
1617 {
1618 {
1619 InvertedLock ung (m_GroupLock);
1620 enterCS(CUDT::uglobal().m_GlobControlLock);
1621 HLOGC(gslog.Debug, log << "grp/sendBroadcast: Locked GlobControlLock, locking back GroupLock");
1622 }
1623
1624 // Under this condition, as an unlock-lock cycle was done on m_GroupLock,
1625 // the Sendstate::it field shall not be used here!
1626 for (vector<Sendstate>::iterator is = sendstates.begin(); is != sendstates.end(); ++is)
1627 {
1628 CUDTSocket* ps = CUDT::uglobal().locateSocket_LOCKED(is->id);
1629
1630 // Is the socket valid? If not, simply SKIP IT. Nothing to be done with it,
1631 // it's already deleted.
1632 if (!ps)
1633 continue;
1634
1635 // Is the socket still group member? If not, SKIP IT. It could only be taken ownership
1636 // by being explicitly closed and so it's deleted from the container.
1637 if (!ps->m_GroupOf)
1638 continue;
1639
1640 // Now we are certain that m_GroupMemberData is valid.
1641 SocketData* d = ps->m_GroupMemberData;
1642
1643 if (is->stat == len)
1644 {
1645 HLOGC(gslog.Debug,
1646 log << "SEND STATE link [" << (is - sendstates.begin()) << "]: SUCCESSFULLY sent " << len
1647 << " bytes");
1648 // Successful.
1649 successful.push_back(d);
1650 rstat = is->stat;
1651 continue;
1652 }
1653
1654 // Remaining are only failed. Check if again.
1655 if (is->code == SRT_EASYNCSND)
1656 {
1657 blocked.push_back(d);
1658 continue;
1659 }
1660
1661 #if ENABLE_HEAVY_LOGGING
1662 string errmsg = cx.getErrorString();
1663 LOGC(gslog.Debug,
1664 log << "SEND STATE link [" << (is - sendstates.begin()) << "]: FAILURE (result:" << is->stat
1665 << "): " << errmsg << ". Setting this socket broken status.");
1666 #endif
1667 // Turn this link broken
1668 d->sndstate = SRT_GST_BROKEN;
1669 }
1670
1671 // Now you can leave GlobControlLock, while GroupLock is still locked.
1672 leaveCS(CUDT::uglobal().m_GlobControlLock);
1673 }
1674
1675 // Re-check after the waiting lock has been reacquired
1676 if (m_bClosing)
1677 {
1678 HLOGC(gslog.Debug, log << "grp/sendBroadcast: GROUP CLOSED, ABANDONING");
1679 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1680 }
1681
1682 // Good, now let's realize the situation.
1683 // First, check the most optimistic scenario: at least one link succeeded.
1684
1685 bool was_blocked = false;
1686 bool none_succeeded = false;
1687
1688 if (!successful.empty())
1689 {
1690 // Good. All blocked links are now qualified as broken.
1691 // You had your chance, but I can't leave you here,
1692 // there will be no further chance to reattempt sending.
1693 for (vector<SocketData*>::iterator b = blocked.begin(); b != blocked.end(); ++b)
1694 {
1695 (*b)->sndstate = SRT_GST_BROKEN;
1696 }
1697 blocked.clear();
1698 }
1699 else
1700 {
1701 none_succeeded = true;
1702 was_blocked = !blocked.empty();
1703 }
1704
1705 int ercode = 0;
1706
1707 // This block causes waiting for any socket to accept the payload.
1708 // This should be done only in blocking mode and only if no other socket
1709 // accepted the payload.
1710 if (was_blocked && none_succeeded && m_bSynSending)
1711 {
1712 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
1713 if (!m_bSynSending)
1714 {
1715 throw CUDTException(MJ_AGAIN, MN_WRAVAIL, 0);
1716 }
1717
1718 HLOGC(gslog.Debug, log << "grp/sendBroadcast: all blocked, trying to common-block on epoll...");
1719
1720 // XXX TO BE REMOVED. Sockets should be subscribed in m_SndEID at connecting time
1721 // (both srt_connect and srt_accept).
1722
1723 // None was successful, but some were blocked. It means that we
1724 // haven't sent the payload over any link so far, so we still have
1725 // a chance to retry.
1726 int modes = SRT_EPOLL_OUT | SRT_EPOLL_ERR;
1727 for (vector<SocketData*>::iterator b = blocked.begin(); b != blocked.end(); ++b)
1728 {
1729 HLOGC(gslog.Debug,
1730 log << "Will block on blocked socket @" << (*b)->id << " as only blocked socket remained");
1731 CUDT::uglobal().epoll_add_usock_INTERNAL(m_SndEID, (*b)->ps, &modes);
1732 }
1733
1734 int blst = 0;
1735 CEPoll::fmap_t sready;
1736
1737 {
1738 // Lift the group lock for a while, to avoid possible deadlocks.
1739 InvertedLock ug(m_GroupLock);
1740 HLOGC(gslog.Debug, log << "grp/sendBroadcast: blocking on any of blocked sockets to allow sending");
1741
1742 // m_iSndTimeOut is -1 by default, which matches the meaning of waiting forever
1743 THREAD_PAUSED();
1744 blst = m_Global.m_EPoll.swait(*m_SndEpolld, sready, m_iSndTimeOut);
1745 THREAD_RESUMED();
1746
1747 // NOTE EXCEPTIONS:
1748 // - EEMPTY: won't happen, we have explicitly added sockets to EID here.
1749 // - XTIMEOUT: will be propagated as this what should be reported to API
1750 // This is the only reason why here the errors are allowed to be handled
1751 // by exceptions.
1752 }
1753
1754 // Re-check after the waiting lock has been reacquired
1755 if (m_bClosing)
1756 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
1757
1758 if (blst == -1)
1759 {
1760 int rno;
1761 ercode = srt_getlasterror(&rno);
1762 }
1763 else
1764 {
1765 activeLinks.clear();
1766 sendstates.clear();
1767 // Extract gli's from the whole group that have id found in the array.
1768
1769 // LOCKING INFO:
1770 // For the moment of lifting m_GroupLock, some sockets could have been closed.
1771 // But then, we believe they have been also removed from the group container,
1772 // and this requires locking on GroupLock. We can then stafely state that the
1773 // group container contains only existing sockets, at worst broken.
1774
1775 for (gli_t dd = m_Group.begin(); dd != m_Group.end(); ++dd)
1776 {
1777 int rdev = CEPoll::ready(sready, dd->id);
1778 if (rdev & SRT_EPOLL_ERR)
1779 {
1780 dd->sndstate = SRT_GST_BROKEN;
1781 }
1782 else if (rdev & SRT_EPOLL_OUT)
1783 activeLinks.push_back(dd);
1784 }
1785
1786 for (vector<gli_t>::iterator snd = activeLinks.begin(); snd != activeLinks.end(); ++snd)
1787 {
1788 gli_t d = *snd;
1789
1790 int erc = 0; // success
1791 // Remaining sndstate is SRT_GST_RUNNING. Send a payload through it.
1792 try
1793 {
1794 // This must be wrapped in try-catch because on error it throws an exception.
1795 // Possible return values are only 0, in case when len was passed 0, or a positive
1796 // >0 value that defines the size of the data that it has sent, that is, in case
1797 // of Live mode, equal to 'len'.
1798 stat = d->ps->core().sendmsg2(buf, len, (w_mc));
1799 }
1800 catch (CUDTException& e)
1801 {
1802 cx = e;
1803 stat = -1;
1804 erc = e.getErrorCode();
1805 }
1806 if (stat != -1)
1807 curseq = w_mc.pktseq;
1808
1809 const Sendstate cstate = {d->id, &*d, stat, erc};
1810 sendstates.push_back(cstate);
1811 d->sndresult = stat;
1812 d->laststatus = d->ps->getStatus();
1813 }
1814
1815 // This time only check if any were successful.
1816 // All others are wipeme.
1817 // NOTE: m_GroupLock is continuously locked - you can safely use Sendstate::it field.
1818 for (vector<Sendstate>::iterator is = sendstates.begin(); is != sendstates.end(); ++is)
1819 {
1820 if (is->stat == len)
1821 {
1822 // Successful.
1823 successful.push_back(is->mb);
1824 rstat = is->stat;
1825 was_blocked = false;
1826 none_succeeded = false;
1827 continue;
1828 }
1829 #if ENABLE_HEAVY_LOGGING
1830 string errmsg = cx.getErrorString();
1831 HLOGC(gslog.Debug,
1832 log << "... (repeat-waited) sending FAILED (" << errmsg
1833 << "). Setting this socket broken status.");
1834 #endif
1835 // Turn this link broken
1836 is->mb->sndstate = SRT_GST_BROKEN;
1837 }
1838 }
1839 }
1840
1841 // }
1842
1843 if (none_succeeded)
1844 {
1845 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
1846 if (!m_bSynSending && (is_pending_blocked || was_blocked))
1847 {
1848 HLOGC(gslog.Debug, log << "grp/sendBroadcast: no links are ready for sending");
1849 ercode = SRT_EASYNCSND;
1850 }
1851 else
1852 {
1853 HLOGC(gslog.Debug, log << "grp/sendBroadcast: all links broken (none succeeded to send a payload)");
1854 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_ERR, true);
1855 }
1856
1857 // Reparse error code, if set.
1858 // It might be set, if the last operation was failed.
1859 // If any operation succeeded, this will not be executed anyway.
1860 CodeMajor major = CodeMajor(ercode ? ercode / 1000 : MJ_CONNECTION);
1861 CodeMinor minor = CodeMinor(ercode ? ercode % 1000 : MN_CONNLOST);
1862
1863 throw CUDTException(major, minor, 0);
1864 }
1865
1866 for (vector<Sendstate>::iterator is = sendstates.begin(); is != sendstates.end(); ++is)
1867 {
1868 // Here we have a situation that at least 1 link successfully sent a packet.
1869 // All links for which sending has failed must be closed.
1870 if (is->stat == -1)
1871 {
1872 // This only sets the state to the socket; the GC process should
1873 // pick it up at the next time.
1874 HLOGC(gslog.Debug, log << "grp/sendBroadcast: per PARTIAL SUCCESS, closing failed @" << is->id);
1875 is->mb->ps->setBrokenClosed();
1876 }
1877 }
1878
1879 // Now that at least one link has succeeded, update sending stats.
1880 m_stats.sent.count(len);
1881
1882 // Pity that the blocking mode only determines as to whether this function should
1883 // block or not, but the epoll flags must be updated regardless of the mode.
1884
1885 // Now fill in the socket table. Check if the size is enough, if not,
1886 // then set the pointer to NULL and set the correct size.
1887
1888 // Note that list::size() is linear time, however this shouldn't matter,
1889 // as with the increased number of links in the redundancy group the
1890 // impossibility of using that many of them grows exponentally.
1891 size_t grpsize = m_Group.size();
1892
1893 if (w_mc.grpdata_size < grpsize)
1894 {
1895 w_mc.grpdata = NULL;
1896 }
1897
1898 size_t i = 0;
1899
1900 bool ready_again = false;
1901 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d, ++i)
1902 {
1903 if (w_mc.grpdata)
1904 {
1905 // Enough space to fill
1906 copyGroupData(*d, (w_mc.grpdata[i]));
1907 }
1908
1909 // We perform this loop anyway because we still need to check if any
1910 // socket is writable. Note that the group lock will hold any write ready
1911 // updates that are performed just after a single socket update for the
1912 // group, so if any socket is actually ready at the moment when this
1913 // is performed, and this one will result in none-write-ready, this will
1914 // be fixed just after returning from this function.
1915
1916 ready_again = ready_again || d->ps->writeReady();
1917 }
1918 w_mc.grpdata_size = i;
1919
1920 if (!ready_again)
1921 {
1922 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
1923 }
1924
1925 return rstat;
1926 }
1927
1928 int CUDTGroup::getGroupData(SRT_SOCKGROUPDATA* pdata, size_t* psize)
1929 {
1930 if (!psize)
1931 return CUDT::APIError(MJ_NOTSUP, MN_INVAL);
1932
1933 ScopedLock gl(m_GroupLock);
1934
1935 return getGroupData_LOCKED(pdata, psize);
1936 }
1937
1938 // [[using locked(this->m_GroupLock)]]
1939 int CUDTGroup::getGroupData_LOCKED(SRT_SOCKGROUPDATA* pdata, size_t* psize)
1940 {
1941 SRT_ASSERT(psize != NULL);
1942 const size_t size = *psize;
1943 // Rewrite correct size
1944 *psize = m_Group.size();
1945
1946 if (!pdata)
1947 {
1948 return 0;
1949 }
1950
1951 if (m_Group.size() > size)
1952 {
1953 // Not enough space to retrieve the data.
1954 return CUDT::APIError(MJ_NOTSUP, MN_XSIZE);
1955 }
1956
1957 size_t i = 0;
1958 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d, ++i)
1959 {
1960 copyGroupData(*d, (pdata[i]));
1961 }
1962
1963 return (int)m_Group.size();
1964 }
1965
1966 // [[using locked(this->m_GroupLock)]]
1967 void CUDTGroup::copyGroupData(const CUDTGroup::SocketData& source, SRT_SOCKGROUPDATA& w_target)
1968 {
1969 w_target.id = source.id;
1970 memcpy((&w_target.peeraddr), &source.peer, source.peer.size());
1971
1972 w_target.sockstate = source.laststatus;
1973 w_target.token = source.token;
1974
1975 // In the internal structure the member state
1976 // is one per direction. From the user perspective
1977 // however it is used either in one direction only,
1978 // in which case the one direction that is active
1979 // matters, or in both directions, in which case
1980 // it will be always either both active or both idle.
1981
1982 if (source.sndstate == SRT_GST_RUNNING || source.rcvstate == SRT_GST_RUNNING)
1983 {
1984 w_target.result = 0;
1985 w_target.memberstate = SRT_GST_RUNNING;
1986 }
1987 // Stats can differ per direction only
1988 // when at least in one direction it's ACTIVE.
1989 else if (source.sndstate == SRT_GST_BROKEN || source.rcvstate == SRT_GST_BROKEN)
1990 {
1991 w_target.result = -1;
1992 w_target.memberstate = SRT_GST_BROKEN;
1993 }
1994 else
1995 {
1996 // IDLE or PENDING
1997 w_target.result = 0;
1998 w_target.memberstate = source.sndstate;
1999 }
2000
2001 w_target.weight = source.weight;
2002 }
2003
2004 void CUDTGroup::getGroupCount(size_t& w_size, bool& w_still_alive)
2005 {
2006 ScopedLock gg(m_GroupLock);
2007
2008 // Note: linear time, but no way to avoid it.
2009 // Fortunately the size of the redundancy group is even
2010 // in the craziest possible implementation at worst 4 members long.
2011 size_t group_list_size = 0;
2012
2013 // In managed group, if all sockets made a failure, all
2014 // were removed, so the loop won't even run once. In
2015 // non-managed, simply no socket found here would have a
2016 // connected status.
2017 bool still_alive = false;
2018
2019 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
2020 {
2021 if (gi->laststatus == SRTS_CONNECTED)
2022 {
2023 still_alive = true;
2024 }
2025 ++group_list_size;
2026 }
2027
2028 // If no socket is found connected, don't update any status.
2029 w_size = group_list_size;
2030 w_still_alive = still_alive;
2031 }
2032
2033 // [[using locked(m_GroupLock)]]
2034 void CUDTGroup::fillGroupData(SRT_MSGCTRL& w_out, // MSGCTRL to be written
2035 const SRT_MSGCTRL& in // MSGCTRL read from the data-providing socket
2036 )
2037 {
2038 // Preserve the data that will be overwritten by assignment
2039 SRT_SOCKGROUPDATA* grpdata = w_out.grpdata;
2040 size_t grpdata_size = w_out.grpdata_size;
2041
2042 w_out = in; // NOTE: This will write NULL to grpdata and 0 to grpdata_size!
2043
2044 w_out.grpdata = NULL; // Make sure it's done, for any case
2045 w_out.grpdata_size = 0;
2046
2047 // User did not wish to read the group data at all.
2048 if (!grpdata)
2049 {
2050 return;
2051 }
2052
2053 int st = getGroupData_LOCKED((grpdata), (&grpdata_size));
2054
2055 // Always write back the size, no matter if the data were filled.
2056 w_out.grpdata_size = grpdata_size;
2057
2058 if (st == SRT_ERROR)
2059 {
2060 // Keep NULL in grpdata
2061 return;
2062 }
2063
2064 // Write back original data
2065 w_out.grpdata = grpdata;
2066 }
2067
2068 // [[using locked(CUDT::uglobal()->m_GlobControLock)]]
2069 // [[using locked(m_GroupLock)]]
2070 struct FLookupSocketWithEvent_LOCKED
2071 {
2072 CUDTUnited* glob;
2073 int evtype;
2074 FLookupSocketWithEvent_LOCKED(CUDTUnited* g, int event_type)
2075 : glob(g)
2076 , evtype(event_type)
2077 {
2078 }
2079
2080 typedef CUDTSocket* result_type;
2081
2082 pair<CUDTSocket*, bool> operator()(const pair<SRTSOCKET, int>& es)
2083 {
2084 CUDTSocket* so = NULL;
2085 if ((es.second & evtype) == 0)
2086 return make_pair(so, false);
2087
2088 so = glob->locateSocket_LOCKED(es.first);
2089 return make_pair(so, !!so);
2090 }
2091 };
2092
2093 void CUDTGroup::recv_CollectAliveAndBroken(vector<CUDTSocket*>& alive, set<CUDTSocket*>& broken)
2094 {
2095 #if ENABLE_HEAVY_LOGGING
2096 std::ostringstream ds;
2097 ds << "E(" << m_RcvEID << ") ";
2098 #define HCLOG(expr) expr
2099 #else
2100 #define HCLOG(x) if (false) {}
2101 #endif
2102
2103 alive.reserve(m_Group.size());
2104
2105 HLOGC(grlog.Debug, log << "group/recv: Reviewing member sockets for polling");
2106 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
2107 {
2108 if (gi->laststatus == SRTS_CONNECTING)
2109 {
2110 HCLOG(ds << "@" << gi->id << "<pending> ");
2111 continue; // don't read over a failed or pending socket
2112 }
2113
2114 if (gi->laststatus >= SRTS_BROKEN)
2115 {
2116 broken.insert(gi->ps);
2117 }
2118
2119 if (broken.count(gi->ps))
2120 {
2121 HCLOG(ds << "@" << gi->id << "<broken> ");
2122 continue;
2123 }
2124
2125 if (gi->laststatus != SRTS_CONNECTED)
2126 {
2127 HCLOG(ds << "@" << gi->id << "<unstable:" << SockStatusStr(gi->laststatus) << "> ");
2128 // Sockets in this state are ignored. We are waiting until it
2129 // achieves CONNECTING state, then it's added to write.
2130 // Or gets broken and closed in the next step.
2131 continue;
2132 }
2133
2134 // Don't skip packets that are ahead because if we have a situation
2135 // that all links are either "elephants" (do not report read readiness)
2136 // and "kangaroos" (have already delivered an ahead packet) then
2137 // omitting kangaroos will result in only elephants to be polled for
2138 // reading. Due to the strict timing requirements and ensurance that
2139 // TSBPD on every link will result in exactly the same delivery time
2140 // for a packet of given sequence, having an elephant and kangaroo in
2141 // one cage means that the elephant is simply a broken or half-broken
2142 // link (the data are not delivered, but it will get repaired soon,
2143 // enough for SRT to maintain the connection, but it will still drop
2144 // packets that didn't arrive in time), in both cases it may
2145 // potentially block the reading for an indefinite time, while
2146 // simultaneously a kangaroo might be a link that got some packets
2147 // dropped, but then it's still capable to deliver packets on time.
2148
2149 // Note that gi->id might be a socket that was previously being polled
2150 // on write, when it's attempting to connect, but now it's connected.
2151 // This will update the socket with the new event set.
2152
2153 alive.push_back(gi->ps);
2154 HCLOG(ds << "@" << gi->id << "[READ] ");
2155 }
2156
2157 HLOGC(grlog.Debug, log << "group/recv: " << ds.str() << " --> EPOLL/SWAIT");
2158 #undef HCLOG
2159 }
2160
2161 vector<CUDTSocket*> CUDTGroup::recv_WaitForReadReady(const vector<CUDTSocket*>& aliveMembers, set<CUDTSocket*>& w_broken)
2162 {
2163 if (aliveMembers.empty())
2164 {
2165 LOGC(grlog.Error, log << "group/recv: all links broken");
2166 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
2167 }
2168
2169 for (vector<CUDTSocket*>::const_iterator i = aliveMembers.begin(); i != aliveMembers.end(); ++i)
2170 {
2171 // NOT using the official srt_epoll_add_usock because this will do socket dispatching,
2172 // which requires lock on m_GlobControlLock, while this lock cannot be applied without
2173 // first unlocking m_GroupLock.
2174 const int read_modes = SRT_EPOLL_IN | SRT_EPOLL_ERR;
2175 CUDT::uglobal().epoll_add_usock_INTERNAL(m_RcvEID, *i, &read_modes);
2176 }
2177
2178 // Here we need to make an additional check.
2179 // There might be a possibility that all sockets that
2180 // were added to the reader group, are ahead. At least
2181 // surely we don't have a situation that any link contains
2182 // an ahead-read subsequent packet, because GroupCheckPacketAhead
2183 // already handled that case.
2184 //
2185 // What we can have is that every link has:
2186 // - no known seq position yet (is not registered in the position map yet)
2187 // - the position equal to the latest delivered sequence
2188 // - the ahead position
2189
2190 // Now the situation is that we don't have any packets
2191 // waiting for delivery so we need to wait for any to report one.
2192
2193 // The non-blocking mode would need to simply check the readiness
2194 // with only immediate report, and read-readiness would have to
2195 // be done in background.
2196
2197 // In blocking mode, use m_iRcvTimeOut, which's default value -1
2198 // means to block indefinitely, also in swait().
2199 // In non-blocking mode use 0, which means to always return immediately.
2200 int timeout = m_bSynRecving ? m_iRcvTimeOut : 0;
2201 int nready = 0;
2202 // Poll on this descriptor until reading is available, indefinitely.
2203 CEPoll::fmap_t sready;
2204
2205 // GlobControlLock is required for dispatching the sockets.
2206 // Therefore it must be applied only when GroupLock is off.
2207 {
2208 // This call may wait indefinite time, so GroupLock must be unlocked.
2209 InvertedLock ung (m_GroupLock);
2210 THREAD_PAUSED();
2211 nready = m_Global.m_EPoll.swait(*m_RcvEpolld, sready, timeout, false /*report by retval*/);
2212 THREAD_RESUMED();
2213
2214 // HERE GlobControlLock is locked first, then GroupLock is applied back
2215 enterCS(CUDT::uglobal().m_GlobControlLock);
2216 }
2217 // BOTH m_GlobControlLock AND m_GroupLock are locked here.
2218
2219 HLOGC(grlog.Debug, log << "group/recv: " << nready << " RDY: " << DisplayEpollResults(sready));
2220
2221 if (nready == 0)
2222 {
2223 // GlobControlLock is applied manually, so unlock manually.
2224 // GroupLock will be unlocked as per scope.
2225 leaveCS(CUDT::uglobal().m_GlobControlLock);
2226 // This can only happen when 0 is passed as timeout and none is ready.
2227 // And 0 is passed only in non-blocking mode. So this is none ready in
2228 // non-blocking mode.
2229 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, false);
2230 throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0);
2231 }
2232
2233 // Handle sockets of pending connection and with errors.
2234
2235 // Nice to have something like:
2236
2237 // broken = FilterIf(sready, [] (auto s)
2238 // { return s.second == SRT_EPOLL_ERR && (auto cs = g->locateSocket(s.first, ERH_RETURN))
2239 // ? {cs, true}
2240 // : {nullptr, false}
2241 // });
2242
2243 FilterIf(
2244 /*FROM*/ sready.begin(),
2245 sready.end(),
2246 /*TO*/ std::inserter(w_broken, w_broken.begin()),
2247 /*VIA*/ FLookupSocketWithEvent_LOCKED(&m_Global, SRT_EPOLL_ERR));
2248
2249
2250 // If this set is empty, it won't roll even once, therefore output
2251 // will be surely empty. This will be checked then same way as when
2252 // reading from every socket resulted in error.
2253 vector<CUDTSocket*> readReady;
2254 readReady.reserve(aliveMembers.size());
2255 for (vector<CUDTSocket*>::const_iterator sockiter = aliveMembers.begin(); sockiter != aliveMembers.end(); ++sockiter)
2256 {
2257 CUDTSocket* sock = *sockiter;
2258 const CEPoll::fmap_t::const_iterator ready_iter = sready.find(sock->m_SocketID);
2259 if (ready_iter != sready.end())
2260 {
2261 if (ready_iter->second & SRT_EPOLL_ERR)
2262 continue; // broken already
2263
2264 if ((ready_iter->second & SRT_EPOLL_IN) == 0)
2265 continue; // not ready for reading
2266
2267 readReady.push_back(*sockiter);
2268 }
2269 else
2270 {
2271 // No read-readiness reported by epoll, but can be missed or not yet handled
2272 // while the receiver buffer is in fact read-ready.
2273 ScopedLock lg(sock->core().m_RcvBufferLock);
2274 if (!sock->core().m_pRcvBuffer)
2275 continue;
2276 // Checking for the next packet in the RCV buffer is safer that isReadReady(tnow).
2277 const CRcvBuffer::PacketInfo info = sock->core().m_pRcvBuffer->getFirstValidPacketInfo();
2278 if (info.seqno != SRT_SEQNO_NONE && !info.seq_gap)
2279 readReady.push_back(sock);
2280 }
2281 }
2282
2283 leaveCS(CUDT::uglobal().m_GlobControlLock);
2284
2285 return readReady;
2286 }
2287
2288 void CUDTGroup::updateReadState(SRTSOCKET /* not sure if needed */, int32_t sequence)
2289 {
2290 bool ready = false;
2291 ScopedLock lg(m_GroupLock);
2292 int seqdiff = 0;
2293
2294 if (m_RcvBaseSeqNo == SRT_SEQNO_NONE)
2295 {
2296 // One socket reported readiness, while no reading operation
2297 // has ever been done. Whatever the sequence number is, it will
2298 // be taken as a good deal and reading will be accepted.
2299 ready = true;
2300 }
2301 else if ((seqdiff = CSeqNo::seqcmp(sequence, m_RcvBaseSeqNo)) > 0)
2302 {
2303 // Case diff == 1: The very next. Surely read-ready.
2304
2305 // Case diff > 1:
2306 // We have an ahead packet. There's one strict condition in which
2307 // we may believe it needs to be delivered - when KANGAROO->HORSE
2308 // transition is allowed. Stating that the time calculation is done
2309 // exactly the same way on every link in the redundancy group, when
2310 // it came to a situation that a packet from one link is ready for
2311 // extraction while it has jumped over some packet, it has surely
2312 // happened due to TLPKTDROP, and if it happened on at least one link,
2313 // we surely don't have this packet ready on any other link.
2314
2315 // This might prove not exactly true, especially when at the moment
2316 // when this happens another link may surprisinly receive this lacking
2317 // packet, so the situation gets suddenly repaired after this function
2318 // is called, the only result of it would be that it will really get
2319 // the very next sequence, even though this function doesn't know it
2320 // yet, but surely in both cases the situation is the same: the medium
2321 // is ready for reading, no matter what packet will turn out to be
2322 // returned when reading is done.
2323
2324 ready = true;
2325 }
2326
2327 // When the sequence number is behind the current one,
2328 // stating that the readines wasn't checked otherwise, the reading
2329 // function will not retrieve anything ready to read just by this premise.
2330 // Even though this packet would have to be eventually extracted (and discarded).
2331
2332 if (ready)
2333 {
2334 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, true);
2335 }
2336 }
2337
2338 int32_t CUDTGroup::getRcvBaseSeqNo()
2339 {
2340 ScopedLock lg(m_GroupLock);
2341 return m_RcvBaseSeqNo;
2342 }
2343
2344 void CUDTGroup::updateWriteState()
2345 {
2346 ScopedLock lg(m_GroupLock);
2347 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, true);
2348 }
2349
2350 /// Validate iPktSeqno is in range
2351 /// (iBaseSeqno - m_iSeqNoTH/2; iBaseSeqno + m_iSeqNoTH).
2352 ///
2353 /// EXPECT_EQ(isValidSeqno(125, 124), true); // behind
2354 /// EXPECT_EQ(isValidSeqno(125, 125), true); // behind
2355 /// EXPECT_EQ(isValidSeqno(125, 126), true); // the next in order
2356 ///
2357 /// EXPECT_EQ(isValidSeqno(0, 0x3FFFFFFF - 2), true); // ahead, but ok.
2358 /// EXPECT_EQ(isValidSeqno(0, 0x3FFFFFFF - 1), false); // too far ahead.
2359 /// EXPECT_EQ(isValidSeqno(0x3FFFFFFF + 2, 0x7FFFFFFF), false); // too far ahead.
2360 /// EXPECT_EQ(isValidSeqno(0x3FFFFFFF + 3, 0x7FFFFFFF), true); // ahead, but ok.
2361 /// EXPECT_EQ(isValidSeqno(0x3FFFFFFF, 0x1FFFFFFF + 2), false); // too far (behind)
2362 /// EXPECT_EQ(isValidSeqno(0x3FFFFFFF, 0x1FFFFFFF + 3), true); // behind, but ok
2363 /// EXPECT_EQ(isValidSeqno(0x70000000, 0x0FFFFFFF), true); // ahead, but ok
2364 /// EXPECT_EQ(isValidSeqno(0x70000000, 0x30000000 - 2), false); // too far ahead.
2365 /// EXPECT_EQ(isValidSeqno(0x70000000, 0x30000000 - 3), true); // ahead, but ok
2366 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0), true);
2367 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0x7FFFFFFF), true);
2368 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0x70000000), false);
2369 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0x70000001), false);
2370 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0x70000002), true); // behind by 536870910
2371 /// EXPECT_EQ(isValidSeqno(0x0FFFFFFF, 0x70000003), true);
2372 ///
2373 /// @return false if @a iPktSeqno is not inside the valid range; otherwise true.
2374 static bool isValidSeqno(int32_t iBaseSeqno, int32_t iPktSeqno)
2375 {
2376 const int32_t iLenAhead = CSeqNo::seqlen(iBaseSeqno, iPktSeqno);
2377 if (iLenAhead >= 0 && iLenAhead < CSeqNo::m_iSeqNoTH)
2378 return true;
2379
2380 const int32_t iLenBehind = CSeqNo::seqlen(iPktSeqno, iBaseSeqno);
2381 if (iLenBehind >= 0 && iLenBehind < CSeqNo::m_iSeqNoTH / 2)
2382 return true;
2383
2384 return false;
2385 }
2386
2387 int CUDTGroup::recv(char* buf, int len, SRT_MSGCTRL& w_mc)
2388 {
2389 // First, acquire GlobControlLock to make sure all member sockets still exist
2390 enterCS(m_Global.m_GlobControlLock);
2391 ScopedLock guard(m_GroupLock);
2392
2393 if (m_bClosing)
2394 {
2395 // The group could be set closing in the meantime, but if
2396 // this is only about to be set by another thread, this thread
2397 // must fist wait for being able to acquire this lock.
2398 // The group will not be deleted now because it is added usage counter
2399 // by this call, but will be released once it exits.
2400 leaveCS(m_Global.m_GlobControlLock);
2401 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
2402 }
2403
2404 // Now, still under lock, check if all sockets still can be dispatched
2405 send_CheckValidSockets();
2406 leaveCS(m_Global.m_GlobControlLock);
2407
2408 if (m_bClosing)
2409 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
2410
2411 // Later iteration over it might be less efficient than
2412 // by vector, but we'll also often try to check a single id
2413 // if it was ever seen broken, so that it's skipped.
2414 set<CUDTSocket*> broken;
2415
2416 for (;;)
2417 {
2418 if (!m_bOpened || !m_bConnected)
2419 {
2420 LOGC(grlog.Error,
2421 log << boolalpha << "grp/recv: $" << id() << ": ABANDONING: opened=" << m_bOpened
2422 << " connected=" << m_bConnected);
2423 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
2424 }
2425
2426 vector<CUDTSocket*> aliveMembers;
2427 recv_CollectAliveAndBroken(aliveMembers, broken);
2428 if (aliveMembers.empty())
2429 {
2430 LOGC(grlog.Error, log << "grp/recv: ALL LINKS BROKEN, ABANDONING.");
2431 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, false);
2432 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
2433 }
2434
2435 vector<CUDTSocket*> readySockets;
2436 if (m_bSynRecving)
2437 readySockets = recv_WaitForReadReady(aliveMembers, broken);
2438 else
2439 readySockets = aliveMembers;
2440
2441 if (m_bClosing)
2442 {
2443 HLOGC(grlog.Debug, log << "grp/recv: $" << id() << ": GROUP CLOSED, ABANDONING.");
2444 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
2445 }
2446
2447 // Find the first readable packet among all member sockets.
2448 steady_clock::time_point tnow = steady_clock::now();
2449 CUDTSocket* socketToRead = NULL;
2450 CRcvBuffer::PacketInfo infoToRead = {-1, false, time_point()};
2451 for (vector<CUDTSocket*>::const_iterator si = readySockets.begin(); si != readySockets.end(); ++si)
2452 {
2453 CUDTSocket* ps = *si;
2454
2455 ScopedLock lg(ps->core().m_RcvBufferLock);
2456 if (m_RcvBaseSeqNo != SRT_SEQNO_NONE)
2457 {
2458 // Drop here to make sure the getFirstReadablePacketInfo() below return fresher packet.
2459 int cnt = ps->core().rcvDropTooLateUpTo(CSeqNo::incseq(m_RcvBaseSeqNo));
2460 if (cnt > 0)
2461 {
2462 HLOGC(grlog.Debug,
2463 log << "grp/recv: $" << id() << ": @" << ps->m_SocketID << ": dropped " << cnt
2464 << " packets before reading: m_RcvBaseSeqNo=" << m_RcvBaseSeqNo);
2465 }
2466 }
2467
2468 const CRcvBuffer::PacketInfo info =
2469 ps->core().m_pRcvBuffer->getFirstReadablePacketInfo(tnow);
2470 if (info.seqno == SRT_SEQNO_NONE)
2471 {
2472 HLOGC(grlog.Debug, log << "grp/recv: $" << id() << ": @" << ps->m_SocketID << ": Nothing to read.");
2473 continue;
2474 }
2475 // We need to qualify the sequence, just for a case.
2476 if (m_RcvBaseSeqNo != SRT_SEQNO_NONE && !isValidSeqno(m_RcvBaseSeqNo, info.seqno))
2477 {
2478 LOGC(grlog.Error,
2479 log << "grp/recv: $" << id() << ": @" << ps->m_SocketID << ": SEQUENCE DISCREPANCY: base=%"
2480 << m_RcvBaseSeqNo << " vs pkt=%" << info.seqno << ", setting ESECFAIL");
2481 ps->core().m_bBroken = true;
2482 broken.insert(ps);
2483 continue;
2484 }
2485 if (socketToRead == NULL || CSeqNo::seqcmp(info.seqno, infoToRead.seqno) < 0)
2486 {
2487 socketToRead = ps;
2488 infoToRead = info;
2489
2490 if (m_RcvBaseSeqNo != SRT_SEQNO_NONE && ((CSeqNo(w_mc.pktseq) - CSeqNo(m_RcvBaseSeqNo)) == 1))
2491 {
2492 // We have the next packet. No need to check other read-ready sockets.
2493 break;
2494 }
2495 }
2496 }
2497
2498 if (socketToRead == NULL)
2499 {
2500 if (m_bSynRecving)
2501 {
2502 HLOGC(grlog.Debug,
2503 log << "grp/recv: $" << id() << ": No links reported any fresher packet, re-polling.");
2504 continue;
2505 }
2506 else
2507 {
2508 HLOGC(grlog.Debug,
2509 log << "grp/recv: $" << id() << ": No links reported any fresher packet, clearing readiness.");
2510 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, false);
2511 throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0);
2512 }
2513 }
2514 else
2515 {
2516 HLOGC(grlog.Debug,
2517 log << "grp/recv: $" << id() << ": Found first readable packet from @" << socketToRead->m_SocketID
2518 << ": seq=" << infoToRead.seqno << " gap=" << infoToRead.seq_gap
2519 << " time=" << FormatTime(infoToRead.tsbpd_time));
2520 }
2521
2522 const int res = socketToRead->core().receiveMessage((buf), len, (w_mc), CUDTUnited::ERH_RETURN);
2523 HLOGC(grlog.Debug,
2524 log << "grp/recv: $" << id() << ": @" << socketToRead->m_SocketID << ": Extracted data with %"
2525 << w_mc.pktseq << " #" << w_mc.msgno << ": " << (res <= 0 ? "(NOTHING)" : BufferStamp(buf, res)));
2526 if (res == 0)
2527 {
2528 LOGC(grlog.Warn,
2529 log << "grp/recv: $" << id() << ": @" << socketToRead->m_SocketID << ": Retrying next socket...");
2530 // This socket will not be socketToRead in the next turn because receiveMessage() return 0 here.
2531 continue;
2532 }
2533 if (res == SRT_ERROR)
2534 {
2535 LOGC(grlog.Warn,
2536 log << "grp/recv: $" << id() << ": @" << socketToRead->m_SocketID << ": " << srt_getlasterror_str()
2537 << ". Retrying next socket...");
2538 broken.insert(socketToRead);
2539 continue;
2540 }
2541 fillGroupData((w_mc), w_mc);
2542
2543 // m_RcvBaseSeqNo is expected to be set to the PeerISN with the first connected member,
2544 // so a packet drop at the start should also be detected by this condition.
2545 if (m_RcvBaseSeqNo != SRT_SEQNO_NONE)
2546 {
2547 const int32_t iNumDropped = (CSeqNo(w_mc.pktseq) - CSeqNo(m_RcvBaseSeqNo)) - 1;
2548 if (iNumDropped > 0)
2549 {
2550 m_stats.recvDrop.count(stats::BytesPackets(iNumDropped * static_cast<uint64_t>(avgRcvPacketSize()), iNumDropped));
2551 LOGC(grlog.Warn,
2552 log << "@" << m_GroupID << " GROUP RCV-DROPPED " << iNumDropped << " packet(s): seqno %"
2553 << CSeqNo::incseq(m_RcvBaseSeqNo) << " to %" << CSeqNo::decseq(w_mc.pktseq));
2554 }
2555 }
2556
2557 HLOGC(grlog.Debug,
2558 log << "grp/recv: $" << id() << ": Update m_RcvBaseSeqNo: %" << m_RcvBaseSeqNo << " -> %" << w_mc.pktseq);
2559 m_RcvBaseSeqNo = w_mc.pktseq;
2560
2561 // Update stats as per delivery
2562 m_stats.recv.count(res);
2563 updateAvgPayloadSize(res);
2564
2565 bool canReadFurther = false;
2566 for (vector<CUDTSocket*>::const_iterator si = aliveMembers.begin(); si != aliveMembers.end(); ++si)
2567 {
2568 CUDTSocket* ps = *si;
2569 ScopedLock lg(ps->core().m_RcvBufferLock);
2570 if (m_RcvBaseSeqNo != SRT_SEQNO_NONE)
2571 {
2572 const int cnt = ps->core().rcvDropTooLateUpTo(CSeqNo::incseq(m_RcvBaseSeqNo), CUDT::DROP_DISCARD);
2573 if (cnt > 0)
2574 {
2575 HLOGC(grlog.Debug,
2576 log << "grp/recv: $" << id() << ": @" << ps->m_SocketID << ": dropped " << cnt
2577 << " packets after reading: m_RcvBaseSeqNo=" << m_RcvBaseSeqNo);
2578 }
2579 }
2580
2581 if (!ps->core().isRcvBufferReadyNoLock())
2582 m_Global.m_EPoll.update_events(ps->m_SocketID, ps->core().m_sPollID, SRT_EPOLL_IN, false);
2583 else
2584 canReadFurther = true;
2585 }
2586
2587 if (!canReadFurther)
2588 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, false);
2589
2590 return res;
2591 }
2592 LOGC(grlog.Error, log << "grp/recv: UNEXPECTED RUN PATH, ABANDONING.");
2593 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, false);
2594 throw CUDTException(MJ_AGAIN, MN_RDAVAIL, 0);
2595 }
2596
2597 const char* CUDTGroup::StateStr(CUDTGroup::GroupState st)
2598 {
2599 static const char* const states[] = {"PENDING", "IDLE", "RUNNING", "BROKEN"};
2600 static const size_t size = Size(states);
2601 static const char* const unknown = "UNKNOWN";
2602 if (size_t(st) < size)
2603 return states[st];
2604 return unknown;
2605 }
2606
2607 void CUDTGroup::synchronizeDrift(const srt::CUDT* srcMember)
2608 {
2609 SRT_ASSERT(srcMember != NULL);
2610 ScopedLock glock(m_GroupLock);
2611 if (m_Group.size() <= 1)
2612 {
2613 HLOGC(grlog.Debug, log << "GROUP: synch uDRIFT NOT DONE, no other links");
2614 return;
2615 }
2616
2617 steady_clock::time_point timebase;
2618 steady_clock::duration udrift(0);
2619 bool wrap_period = false;
2620 srcMember->m_pRcvBuffer->getInternalTimeBase((timebase), (wrap_period), (udrift));
2621
2622 HLOGC(grlog.Debug,
2623 log << "GROUP: synch uDRIFT=" << FormatDuration(udrift) << " TB=" << FormatTime(timebase) << "("
2624 << (wrap_period ? "" : "NO ") << "wrap period)");
2625
2626 // Now that we have the minimum timebase and drift calculated, apply this to every link,
2627 // INCLUDING THE REPORTER.
2628
2629 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
2630 {
2631 // Skip non-connected; these will be synchronized when ready
2632 if (gi->laststatus != SRTS_CONNECTED)
2633 continue;
2634 CUDT& member = gi->ps->core();
2635 if (srcMember == &member)
2636 continue;
2637
2638 member.m_pRcvBuffer->applyGroupDrift(timebase, wrap_period, udrift);
2639 }
2640 }
2641
2642 void CUDTGroup::bstatsSocket(CBytePerfMon* perf, bool clear)
2643 {
2644 if (!m_bConnected)
2645 throw CUDTException(MJ_CONNECTION, MN_NOCONN, 0);
2646 if (m_bClosing)
2647 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
2648
2649 const steady_clock::time_point currtime = steady_clock::now();
2650
2651 // NOTE: Potentially in the group we might be using both IPv4 and IPv6
2652 // links and sending a single packet over these two links could be different.
2653 // These stats then don't make much sense in this form, this has to be
2654 // redesigned. We use the header size as per IPv4, as it was everywhere.
2655 const int pktHdrSize = CPacket::HDR_SIZE + CPacket::UDP_HDR_SIZE;
2656
2657 memset(perf, 0, sizeof *perf);
2658
2659 ScopedLock gg(m_GroupLock);
2660
2661 perf->msTimeStamp = count_milliseconds(currtime - m_tsStartTime);
2662
2663 perf->pktSentUnique = m_stats.sent.trace.count();
2664 perf->pktRecvUnique = m_stats.recv.trace.count();
2665 perf->pktRcvDrop = m_stats.recvDrop.trace.count();
2666
2667 perf->byteSentUnique = m_stats.sent.trace.bytesWithHdr(pktHdrSize);
2668 perf->byteRecvUnique = m_stats.recv.trace.bytesWithHdr(pktHdrSize);
2669 perf->byteRcvDrop = m_stats.recvDrop.trace.bytesWithHdr(pktHdrSize);
2670
2671 perf->pktSentUniqueTotal = m_stats.sent.total.count();
2672 perf->pktRecvUniqueTotal = m_stats.recv.total.count();
2673 perf->pktRcvDropTotal = m_stats.recvDrop.total.count();
2674
2675 perf->byteSentUniqueTotal = m_stats.sent.total.bytesWithHdr(pktHdrSize);
2676 perf->byteRecvUniqueTotal = m_stats.recv.total.bytesWithHdr(pktHdrSize);
2677 perf->byteRcvDropTotal = m_stats.recvDrop.total.bytesWithHdr(pktHdrSize);
2678
2679 const double interval = static_cast<double>(count_microseconds(currtime - m_stats.tsLastSampleTime));
2680 perf->mbpsSendRate = double(perf->byteSent) * 8.0 / interval;
2681 perf->mbpsRecvRate = double(perf->byteRecv) * 8.0 / interval;
2682
2683 if (clear)
2684 {
2685 m_stats.reset();
2686 }
2687 }
2688
2689 /// @brief Compares group members by their weight (higher weight comes first).
2690 struct FCompareByWeight
2691 {
2692 typedef CUDTGroup::gli_t gli_t;
2693
2694 /// @returns true if the first argument is less than (i.e. is ordered before) the second.
2695 bool operator()(const gli_t preceding, const gli_t succeeding)
2696 {
2697 return preceding->weight > succeeding->weight;
2698 }
2699 };
2700
2701 // [[using maybe_locked(this->m_GroupLock)]]
2702 BackupMemberState CUDTGroup::sendBackup_QualifyIfStandBy(const gli_t d)
2703 {
2704 if (!d->ps)
2705 return BKUPST_BROKEN;
2706
2707 const SRT_SOCKSTATUS st = d->ps->getStatus();
2708 // If the socket is already broken, move it to broken.
2709 if (int(st) >= int(SRTS_BROKEN))
2710 {
2711 HLOGC(gslog.Debug,
2712 log << "CUDTGroup::send.$" << id() << ": @" << d->id << " became " << SockStatusStr(st)
2713 << ", WILL BE CLOSED.");
2714 return BKUPST_BROKEN;
2715 }
2716
2717 if (st != SRTS_CONNECTED)
2718 {
2719 HLOGC(gslog.Debug, log << "CUDTGroup::send. @" << d->id << " is still " << SockStatusStr(st) << ", skipping.");
2720 return BKUPST_PENDING;
2721 }
2722
2723 return BKUPST_STANDBY;
2724 }
2725
2726 // [[using maybe_locked(this->m_GroupLock)]]
2727 bool CUDTGroup::send_CheckIdle(const gli_t d, vector<SRTSOCKET>& w_wipeme, vector<SRTSOCKET>& w_pendingSockets)
2728 {
2729 SRT_SOCKSTATUS st = SRTS_NONEXIST;
2730 if (d->ps)
2731 st = d->ps->getStatus();
2732 // If the socket is already broken, move it to broken.
2733 if (int(st) >= int(SRTS_BROKEN))
2734 {
2735 HLOGC(gslog.Debug,
2736 log << "CUDTGroup::send.$" << id() << ": @" << d->id << " became " << SockStatusStr(st)
2737 << ", WILL BE CLOSED.");
2738 w_wipeme.push_back(d->id);
2739 return false;
2740 }
2741
2742 if (st != SRTS_CONNECTED)
2743 {
2744 HLOGC(gslog.Debug, log << "CUDTGroup::send. @" << d->id << " is still " << SockStatusStr(st) << ", skipping.");
2745 w_pendingSockets.push_back(d->id);
2746 return false;
2747 }
2748
2749 return true;
2750 }
2751
2752
2753 #if SRT_DEBUG_BONDING_STATES
2754 class StabilityTracer
2755 {
2756 public:
2757 StabilityTracer()
2758 {
2759 }
2760
2761 ~StabilityTracer()
2762 {
2763 srt::sync::ScopedLock lck(m_mtx);
2764 m_fout.close();
2765 }
2766
2767 void trace(const CUDT& u, const srt::sync::steady_clock::time_point& currtime, uint32_t activation_period_us,
2768 int64_t stability_tmo_us, const std::string& state, uint16_t weight)
2769 {
2770 srt::sync::ScopedLock lck(m_mtx);
2771 create_file();
2772
2773 m_fout << srt::sync::FormatTime(currtime) << ",";
2774 m_fout << u.id() << ",";
2775 m_fout << weight << ",";
2776 m_fout << u.peerLatency_us() << ",";
2777 m_fout << u.SRTT() << ",";
2778 m_fout << u.RTTVar() << ",";
2779 m_fout << stability_tmo_us << ",";
2780 m_fout << count_microseconds(currtime - u.lastRspTime()) << ",";
2781 m_fout << state << ",";
2782 m_fout << (srt::sync::is_zero(u.freshActivationStart()) ? -1 : (count_microseconds(currtime - u.freshActivationStart()))) << ",";
2783 m_fout << activation_period_us << "\n";
2784 m_fout.flush();
2785 }
2786
2787 private:
2788 void print_header()
2789 {
2790 //srt::sync::ScopedLock lck(m_mtx);
2791 m_fout << "Timepoint,SocketID,weight,usLatency,usRTT,usRTTVar,usStabilityTimeout,usSinceLastResp,State,usSinceActivation,usActivationPeriod\n";
2792 }
2793
2794 void create_file()
2795 {
2796 if (m_fout.is_open())
2797 return;
2798
2799 std::string str_tnow = srt::sync::FormatTimeSys(srt::sync::steady_clock::now());
2800 str_tnow.resize(str_tnow.size() - 7); // remove trailing ' [SYST]' part
2801 while (str_tnow.find(':') != std::string::npos) {
2802 str_tnow.replace(str_tnow.find(':'), 1, 1, '_');
2803 }
2804 const std::string fname = "stability_trace_" + str_tnow + ".csv";
2805 m_fout.open(fname.c_str(), std::ofstream::out);
2806 if (!m_fout)
2807 std::cerr << "IPE: Failed to open " << fname << "!!!\n";
2808
2809 print_header();
2810 }
2811
2812 private:
2813 srt::sync::Mutex m_mtx;
2814 std::ofstream m_fout;
2815 };
2816
2817 StabilityTracer s_stab_trace;
2818 #endif
2819
2820 void CUDTGroup::sendBackup_QualifyMemberStates(SendBackupCtx& w_sendBackupCtx, const steady_clock::time_point& currtime)
2821 {
2822 // First, check status of every link - no matter if idle or active.
2823 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d)
2824 {
2825 if (d->sndstate != SRT_GST_BROKEN)
2826 {
2827 // Check the socket state prematurely in order not to uselessly
2828 // send over a socket that is broken.
2829 CUDT* const pu = (d->ps)
2830 ? &d->ps->core()
2831 : NULL;
2832
2833 if (!pu || pu->m_bBroken)
2834 {
2835 HLOGC(gslog.Debug, log << "grp/sendBackup: socket @" << d->id << " detected +Broken - transit to BROKEN");
2836 d->sndstate = SRT_GST_BROKEN;
2837 d->rcvstate = SRT_GST_BROKEN;
2838 }
2839 }
2840
2841 // Check socket sndstate before sending
2842 if (d->sndstate == SRT_GST_BROKEN)
2843 {
2844 HLOGC(gslog.Debug,
2845 log << "grp/sendBackup: socket in BROKEN state: @" << d->id
2846 << ", sockstatus=" << SockStatusStr(d->ps ? d->ps->getStatus() : SRTS_NONEXIST));
2847 sendBackup_AssignBackupState(d->ps->core(), BKUPST_BROKEN, currtime);
2848 w_sendBackupCtx.recordMemberState(&(*d), BKUPST_BROKEN);
2849 #if SRT_DEBUG_BONDING_STATES
2850 s_stab_trace.trace(d->ps->core(), currtime, 0, 0, stateToStr(BKUPST_BROKEN), d->weight);
2851 #endif
2852 continue;
2853 }
2854
2855 if (d->sndstate == SRT_GST_IDLE)
2856 {
2857 const BackupMemberState idle_state = sendBackup_QualifyIfStandBy(d);
2858 sendBackup_AssignBackupState(d->ps->core(), idle_state, currtime);
2859 w_sendBackupCtx.recordMemberState(&(*d), idle_state);
2860
2861 if (idle_state == BKUPST_STANDBY)
2862 {
2863 // TODO: Check if this is some abandoned logic.
2864 sendBackup_CheckIdleTime(d);
2865 }
2866 #if SRT_DEBUG_BONDING_STATES
2867 s_stab_trace.trace(d->ps->core(), currtime, 0, 0, stateToStr(idle_state), d->weight);
2868 #endif
2869 continue;
2870 }
2871
2872 if (d->sndstate == SRT_GST_RUNNING)
2873 {
2874 const BackupMemberState active_state = sendBackup_QualifyActiveState(d, currtime);
2875 sendBackup_AssignBackupState(d->ps->core(), active_state, currtime);
2876 w_sendBackupCtx.recordMemberState(&(*d), active_state);
2877 #if SRT_DEBUG_BONDING_STATES
2878 s_stab_trace.trace(d->ps->core(), currtime, 0, 0, stateToStr(active_state), d->weight);
2879 #endif
2880 continue;
2881 }
2882
2883 HLOGC(gslog.Debug,
2884 log << "grp/sendBackup: socket @" << d->id << " not ready, state: " << StateStr(d->sndstate) << "("
2885 << int(d->sndstate) << ") - NOT sending, SET AS PENDING");
2886
2887 // Otherwise connection pending
2888 sendBackup_AssignBackupState(d->ps->core(), BKUPST_PENDING, currtime);
2889 w_sendBackupCtx.recordMemberState(&(*d), BKUPST_PENDING);
2890 #if SRT_DEBUG_BONDING_STATES
2891 s_stab_trace.trace(d->ps->core(), currtime, 0, 0, stateToStr(BKUPST_PENDING), d->weight);
2892 #endif
2893 }
2894 }
2895
2896
2897 void CUDTGroup::sendBackup_AssignBackupState(CUDT& sock, BackupMemberState state, const steady_clock::time_point& currtime)
2898 {
2899 switch (state)
2900 {
2901 case BKUPST_PENDING:
2902 case BKUPST_STANDBY:
2903 case BKUPST_BROKEN:
2904 sock.m_tsFreshActivation = steady_clock::time_point();
2905 sock.m_tsUnstableSince = steady_clock::time_point();
2906 sock.m_tsWarySince = steady_clock::time_point();
2907 break;
2908 case BKUPST_ACTIVE_FRESH:
2909 if (is_zero(sock.freshActivationStart()))
2910 {
2911 sock.m_tsFreshActivation = currtime;
2912 }
2913 sock.m_tsUnstableSince = steady_clock::time_point();
2914 sock.m_tsWarySince = steady_clock::time_point();;
2915 break;
2916 case BKUPST_ACTIVE_STABLE:
2917 sock.m_tsFreshActivation = steady_clock::time_point();
2918 sock.m_tsUnstableSince = steady_clock::time_point();
2919 sock.m_tsWarySince = steady_clock::time_point();
2920 break;
2921 case BKUPST_ACTIVE_UNSTABLE:
2922 if (is_zero(sock.m_tsUnstableSince))
2923 {
2924 sock.m_tsUnstableSince = currtime;
2925 }
2926 sock.m_tsFreshActivation = steady_clock::time_point();
2927 sock.m_tsWarySince = steady_clock::time_point();
2928 break;
2929 case BKUPST_ACTIVE_UNSTABLE_WARY:
2930 if (is_zero(sock.m_tsWarySince))
2931 {
2932 sock.m_tsWarySince = currtime;
2933 }
2934 break;
2935 default:
2936 break;
2937 }
2938 }
2939
2940 // [[using locked(this->m_GroupLock)]]
2941 void CUDTGroup::sendBackup_CheckIdleTime(gli_t w_d)
2942 {
2943 // Check if it was fresh set as idle, we had to wait until its sender
2944 // buffer gets empty so that we can make sure that KEEPALIVE will be the
2945 // really last sent for longer time.
2946 CUDT& u = w_d->ps->core();
2947 if (is_zero(u.m_tsFreshActivation)) // TODO: Check if this condition is ever false
2948 return;
2949
2950 CSndBuffer* b = u.m_pSndBuffer;
2951 if (b && b->getCurrBufSize() == 0)
2952 {
2953 HLOGC(gslog.Debug,
2954 log << "grp/sendBackup: FRESH IDLE LINK reached empty buffer - setting permanent and KEEPALIVE");
2955 u.m_tsFreshActivation = steady_clock::time_point();
2956
2957 // Send first immediate keepalive. The link is to be turn to IDLE
2958 // now so nothing will be sent to it over time and it will start
2959 // getting KEEPALIVES since now. Send the first one now to increase
2960 // probability that the link will be recognized as IDLE on the
2961 // reception side ASAP.
2962 int32_t arg = 1;
2963 w_d->ps->core().sendCtrl(UMSG_KEEPALIVE, &arg);
2964 }
2965 }
2966
2967 // [[using locked(this->m_GroupLock)]]
2968 CUDTGroup::BackupMemberState CUDTGroup::sendBackup_QualifyActiveState(const gli_t d, const time_point currtime)
2969 {
2970 const CUDT& u = d->ps->core();
2971
2972 const uint32_t latency_us = u.peerLatency_us();
2973
2974 const int32_t min_stability_us = m_uOPT_MinStabilityTimeout_us;
2975 const int64_t initial_stabtout_us = max<int64_t>(min_stability_us, latency_us);
2976 const int64_t probing_period_us = initial_stabtout_us + 5 * CUDT::COMM_SYN_INTERVAL_US;
2977
2978 // RTT and RTTVar values are still being refined during the probing period,
2979 // therefore the dynamic timeout should not be used during the probing period.
2980 const bool is_activation_phase = !is_zero(u.freshActivationStart())
2981 && (count_microseconds(currtime - u.freshActivationStart()) <= probing_period_us);
2982
2983 // Initial stability timeout is used only in activation phase.
2984 // Otherwise runtime stability is used, including the WARY state.
2985 const int64_t stability_tout_us = is_activation_phase
2986 ? initial_stabtout_us // activation phase
2987 : min<int64_t>(max<int64_t>(min_stability_us, 2 * u.SRTT() + 4 * u.RTTVar()), latency_us);
2988
2989 const steady_clock::time_point last_rsp = max(u.freshActivationStart(), u.lastRspTime());
2990 const steady_clock::duration td_response = currtime - last_rsp;
2991
2992 // No response for a long time
2993 if (count_microseconds(td_response) > stability_tout_us)
2994 {
2995 return BKUPST_ACTIVE_UNSTABLE;
2996 }
2997
2998 enterCS(u.m_StatsLock);
2999 const int64_t drop_total = u.m_stats.sndr.dropped.total.count();
3000 leaveCS(u.m_StatsLock);
3001
3002 const bool have_new_drops = d->pktSndDropTotal != drop_total;
3003 if (have_new_drops)
3004 {
3005 d->pktSndDropTotal = drop_total;
3006 if (!is_activation_phase)
3007 return BKUPST_ACTIVE_UNSTABLE;
3008 }
3009
3010 // Responsive: either stable, wary or still fresh activated.
3011 if (is_activation_phase)
3012 return BKUPST_ACTIVE_FRESH;
3013
3014 const bool is_wary = !is_zero(u.m_tsWarySince);
3015 const bool is_wary_probing = is_wary
3016 && (count_microseconds(currtime - u.m_tsWarySince) <= 4 * u.peerLatency_us());
3017
3018 const bool is_unstable = !is_zero(u.m_tsUnstableSince);
3019
3020 // If unstable and not in wary, become wary.
3021 if (is_unstable && !is_wary)
3022 return BKUPST_ACTIVE_UNSTABLE_WARY;
3023
3024 // Still probing for stability.
3025 if (is_wary_probing)
3026 return BKUPST_ACTIVE_UNSTABLE_WARY;
3027
3028 if (is_wary)
3029 {
3030 LOGC(gslog.Debug,
3031 log << "grp/sendBackup: @" << u.id() << " wary->stable after " << count_milliseconds(currtime - u.m_tsWarySince) << " ms");
3032 }
3033
3034 return BKUPST_ACTIVE_STABLE;
3035 }
3036
3037 // [[using locked(this->m_GroupLock)]]
3038 bool CUDTGroup::sendBackup_CheckSendStatus(const steady_clock::time_point& currtime SRT_ATR_UNUSED,
3039 const int send_status,
3040 const int32_t lastseq,
3041 const int32_t pktseq,
3042 CUDT& w_u,
3043 int32_t& w_curseq,
3044 int& w_final_stat)
3045 {
3046 if (send_status == -1)
3047 return false; // Sending failed.
3048
3049
3050 bool send_succeeded = false;
3051 if (w_curseq == SRT_SEQNO_NONE)
3052 {
3053 w_curseq = pktseq;
3054 }
3055 else if (w_curseq != lastseq)
3056 {
3057 // We believe that all active links use the same seq.
3058 // But we can do some sanity check.
3059 LOGC(gslog.Error,
3060 log << "grp/sendBackup: @" << w_u.m_SocketID << ": IPE: another running link seq discrepancy: %"
3061 << lastseq << " vs. previous %" << w_curseq << " - fixing");
3062
3063 // Override must be done with a sequence number greater by one.
3064
3065 // Example:
3066 //
3067 // Link 1 before sending: curr=1114, next=1115
3068 // After sending it reports pktseq=1115
3069 //
3070 // Link 2 before sending: curr=1110, next=1111 (->lastseq before sending)
3071 // THIS CHECK done after sending:
3072 // -- w_curseq(1115) != lastseq(1111)
3073 //
3074 // NOW: Link 1 after sending is:
3075 // curr=1115, next=1116
3076 //
3077 // The value of w_curseq here = 1115, while overrideSndSeqNo
3078 // calls setInitialSndSeq(seq), which sets:
3079 // - curr = seq - 1
3080 // - next = seq
3081 //
3082 // So, in order to set curr=1115, next=1116
3083 // this must set to 1115+1.
3084
3085 w_u.overrideSndSeqNo(CSeqNo::incseq(w_curseq));
3086 }
3087
3088 // State it as succeeded, though. We don't know if the link
3089 // is broken until we get the connection broken confirmation,
3090 // and the instability state may wear off next time.
3091 send_succeeded = true;
3092 w_final_stat = send_status;
3093
3094 return send_succeeded;
3095 }
3096
3097 // [[using locked(this->m_GroupLock)]]
3098 void CUDTGroup::sendBackup_Buffering(const char* buf, const int len, int32_t& w_curseq, SRT_MSGCTRL& w_mc)
3099 {
3100 // This is required to rewrite into currentSchedSequence() property
3101 // as this value will be used as ISN when a new link is connected.
3102 int32_t oldest_buffer_seq = SRT_SEQNO_NONE;
3103
3104 if (w_curseq != SRT_SEQNO_NONE)
3105 {
3106 HLOGC(gslog.Debug, log << "grp/sendBackup: successfully sent over running link, ADDING TO BUFFER.");
3107
3108 // Note: the sequence number that was used to send this packet should be
3109 // recorded here.
3110 oldest_buffer_seq = addMessageToBuffer(buf, len, (w_mc));
3111 }
3112 else
3113 {
3114 // We have to predict, which sequence number would have
3115 // to be placed on the packet about to be sent now. To
3116 // maintain consistency:
3117
3118 // 1. If there are any packets in the sender buffer,
3119 // get the sequence of the last packet, increase it.
3120 // This must be done even if this contradicts the ISN
3121 // of all idle links because otherwise packets will get
3122 // discrepancy.
3123 if (!m_SenderBuffer.empty())
3124 {
3125 BufferedMessage& m = m_SenderBuffer.back();
3126 w_curseq = CSeqNo::incseq(m.mc.pktseq);
3127
3128 // Set also this sequence to the current w_mc
3129 w_mc.pktseq = w_curseq;
3130
3131 // XXX may need tighter revision when message mode is allowed
3132 w_mc.msgno = ++MsgNo(m.mc.msgno);
3133 oldest_buffer_seq = addMessageToBuffer(buf, len, (w_mc));
3134 }
3135
3136 // Note that if buffer is empty and w_curseq is (still) SRT_SEQNO_NONE,
3137 // it will have to try to send first in order to extract the data.
3138
3139 // Note that if w_curseq is still SRT_SEQNO_NONE at this point, it means
3140 // that we have the case of the very first packet sending.
3141 // Otherwise there would be something in the buffer already.
3142 }
3143
3144 if (oldest_buffer_seq != SRT_SEQNO_NONE)
3145 m_iLastSchedSeqNo = oldest_buffer_seq;
3146 }
3147
3148 size_t CUDTGroup::sendBackup_TryActivateStandbyIfNeeded(
3149 const char* buf,
3150 const int len,
3151 bool& w_none_succeeded,
3152 SRT_MSGCTRL& w_mc,
3153 int32_t& w_curseq,
3154 int32_t& w_final_stat,
3155 SendBackupCtx& w_sendBackupCtx,
3156 CUDTException& w_cx,
3157 const steady_clock::time_point& currtime)
3158 {
3159 const unsigned num_standby = w_sendBackupCtx.countMembersByState(BKUPST_STANDBY);
3160 if (num_standby == 0)
3161 {
3162 return 0;
3163 }
3164
3165 const unsigned num_stable = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_STABLE);
3166 const unsigned num_fresh = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_FRESH);
3167
3168 if (num_stable + num_fresh == 0)
3169 {
3170 LOGC(gslog.Warn,
3171 log << "grp/sendBackup: trying to activate a stand-by link (" << num_standby << " available). "
3172 << "Reason: no stable links"
3173 );
3174 }
3175 else if (w_sendBackupCtx.maxActiveWeight() < w_sendBackupCtx.maxStandbyWeight())
3176 {
3177 LOGC(gslog.Warn,
3178 log << "grp/sendBackup: trying to activate a stand-by link (" << num_standby << " available). "
3179 << "Reason: max active weight " << w_sendBackupCtx.maxActiveWeight()
3180 << ", max stand by weight " << w_sendBackupCtx.maxStandbyWeight()
3181 );
3182 }
3183 else
3184 {
3185 /*LOGC(gslog.Warn,
3186 log << "grp/sendBackup: no need to activate (" << num_standby << " available). "
3187 << "Max active weight " << w_sendBackupCtx.maxActiveWeight()
3188 << ", max stand by weight " << w_sendBackupCtx.maxStandbyWeight()
3189 );*/
3190 return 0;
3191 }
3192
3193 int stat = -1;
3194
3195 size_t num_activated = 0;
3196
3197 w_sendBackupCtx.sortByWeightAndState();
3198 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3199 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3200 {
3201 if (member->state != BKUPST_STANDBY)
3202 continue;
3203
3204 int erc = 0;
3205 SocketData* d = member->pSocketData;
3206 // Now send and check the status
3207 // The link could have got broken
3208
3209 try
3210 {
3211 CUDT& cudt = d->ps->core();
3212 // Take source rate estimation from an active member (needed for the input rate estimation mode).
3213 cudt.setRateEstimator(w_sendBackupCtx.getRateEstimate());
3214
3215 // TODO: At this point all packets that could be sent
3216 // are located in m_SenderBuffer. So maybe just use sendBackupRexmit()?
3217 if (w_curseq == SRT_SEQNO_NONE)
3218 {
3219 // This marks the fact that the given here packet
3220 // could not be sent over any link. This includes the
3221 // situation of sending the very first packet after connection.
3222
3223 HLOGC(gslog.Debug,
3224 log << "grp/sendBackup: ... trying @" << d->id << " - sending the VERY FIRST message");
3225
3226 stat = cudt.sendmsg2(buf, len, (w_mc));
3227 if (stat != -1)
3228 {
3229 // This will be no longer used, but let it stay here.
3230 // It's because if this is successful, no other links
3231 // will be tried.
3232 w_curseq = w_mc.pktseq;
3233 addMessageToBuffer(buf, len, (w_mc));
3234 }
3235 }
3236 else
3237 {
3238 HLOGC(gslog.Debug,
3239 log << "grp/sendBackup: ... trying @" << d->id << " - resending " << m_SenderBuffer.size()
3240 << " collected messages...");
3241 // Note: this will set the currently required packet
3242 // because it has been just freshly added to the sender buffer
3243 stat = sendBackupRexmit(cudt, (w_mc));
3244 }
3245 ++num_activated;
3246 }
3247 catch (CUDTException& e)
3248 {
3249 // This will be propagated from internal sendmsg2 call,
3250 // but that's ok - we want this sending interrupted even in half.
3251 w_cx = e;
3252 stat = -1;
3253 erc = e.getErrorCode();
3254 }
3255
3256 d->sndresult = stat;
3257 d->laststatus = d->ps->getStatus();
3258
3259 if (stat != -1)
3260 {
3261 d->sndstate = SRT_GST_RUNNING;
3262 sendBackup_AssignBackupState(d->ps->core(), BKUPST_ACTIVE_FRESH, currtime);
3263 w_sendBackupCtx.updateMemberState(d, BKUPST_ACTIVE_FRESH);
3264 // Note: this will override the sequence number
3265 // for all next iterations in this loop.
3266 w_none_succeeded = false;
3267 w_final_stat = stat;
3268
3269 LOGC(gslog.Warn,
3270 log << "@" << d->id << " FRESH-ACTIVATED");
3271
3272 // We've activated the link, so that's enough.
3273 break;
3274 }
3275
3276 // Failure - move to broken those that could not be activated
3277 bool isblocked SRT_ATR_UNUSED = true;
3278 if (erc != SRT_EASYNCSND)
3279 {
3280 isblocked = false;
3281 sendBackup_AssignBackupState(d->ps->core(), BKUPST_BROKEN, currtime);
3282 w_sendBackupCtx.updateMemberState(d, BKUPST_BROKEN);
3283 }
3284
3285 // If we found a blocked link, leave it alone, however
3286 // still try to send something over another link
3287
3288 LOGC(gslog.Warn,
3289 log << "@" << d->id << " FAILED (" << (isblocked ? "blocked" : "ERROR")
3290 << "), trying to activate another link.");
3291 }
3292
3293 return num_activated;
3294 }
3295
3296 // [[using locked(this->m_GroupLock)]]
3297 void CUDTGroup::sendBackup_CheckPendingSockets(SendBackupCtx& w_sendBackupCtx, const steady_clock::time_point& currtime)
3298 {
3299 if (w_sendBackupCtx.countMembersByState(BKUPST_PENDING) == 0)
3300 return;
3301
3302 HLOGC(gslog.Debug, log << "grp/send*: checking pending sockets.");
3303
3304 // These sockets if they are in pending state, should be added to m_SndEID
3305 // at the connecting stage.
3306 CEPoll::fmap_t sready;
3307
3308 if (m_Global.m_EPoll.empty(*m_SndEpolld))
3309 {
3310 // Sanity check - weird pending reported.
3311 LOGC(gslog.Error, log << "grp/send*: IPE: reported pending sockets, but EID is empty - wiping pending!");
3312 return;
3313 }
3314
3315 {
3316 InvertedLock ug(m_GroupLock);
3317 m_Global.m_EPoll.swait(
3318 *m_SndEpolld, sready, 0, false /*report by retval*/); // Just check if anything has happened
3319 }
3320
3321 if (m_bClosing)
3322 {
3323 HLOGC(gslog.Debug, log << "grp/send...: GROUP CLOSED, ABANDONING");
3324 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3325 }
3326
3327 // Some sockets could have been closed in the meantime.
3328 if (m_Global.m_EPoll.empty(*m_SndEpolld))
3329 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3330
3331 HLOGC(gslog.Debug, log << "grp/send*: RDY: " << DisplayEpollResults(sready));
3332
3333 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3334 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3335 {
3336 if (member->state != BKUPST_PENDING)
3337 continue;
3338
3339 const SRTSOCKET sockid = member->pSocketData->id;
3340 if (!CEPoll::isready(sready, sockid, SRT_EPOLL_ERR))
3341 continue;
3342
3343 HLOGC(gslog.Debug, log << "grp/send*: Socket @" << sockid << " reported FAILURE - qualifying as broken.");
3344 w_sendBackupCtx.updateMemberState(member->pSocketData, BKUPST_BROKEN);
3345 if (member->pSocketData->ps)
3346 sendBackup_AssignBackupState(member->pSocketData->ps->core(), BKUPST_BROKEN, currtime);
3347
3348 const int no_events = 0;
3349 m_Global.m_EPoll.update_usock(m_SndEID, sockid, &no_events);
3350 }
3351
3352 // After that, all sockets that have been reported
3353 // as ready to write should be removed from EID. This
3354 // will also remove those sockets that have been added
3355 // as redundant links at the connecting stage and became
3356 // writable (connected) before this function had a chance
3357 // to check them.
3358 m_Global.m_EPoll.clear_ready_usocks(*m_SndEpolld, SRT_EPOLL_OUT);
3359 }
3360
3361 // [[using locked(this->m_GroupLock)]]
3362 void CUDTGroup::sendBackup_CheckUnstableSockets(SendBackupCtx& w_sendBackupCtx, const steady_clock::time_point& currtime)
3363 {
3364 const unsigned num_stable = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_STABLE);
3365 if (num_stable == 0)
3366 return;
3367
3368 const unsigned num_unstable = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_UNSTABLE);
3369 const unsigned num_wary = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_UNSTABLE_WARY);
3370 if (num_unstable + num_wary == 0)
3371 return;
3372
3373 HLOGC(gslog.Debug, log << "grp/send*: checking unstable sockets.");
3374
3375
3376 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3377 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3378 {
3379 if (member->state != BKUPST_ACTIVE_UNSTABLE && member->state != BKUPST_ACTIVE_UNSTABLE_WARY)
3380 continue;
3381
3382 CUDT& sock = member->pSocketData->ps->core();
3383
3384 if (is_zero(sock.m_tsUnstableSince))
3385 {
3386 LOGC(gslog.Error, log << "grp/send* IPE: Socket @" << member->socketID
3387 << " is qualified as unstable, but does not have the 'unstable since' timestamp. Still marking for closure.");
3388 }
3389
3390 const int unstable_for_ms = (int)count_milliseconds(currtime - sock.m_tsUnstableSince);
3391 if (unstable_for_ms < sock.peerIdleTimeout_ms())
3392 continue;
3393
3394 // Requesting this socket to be broken with the next CUDT::checkExpTimer() call.
3395 sock.breakAsUnstable();
3396
3397 LOGC(gslog.Warn, log << "grp/send*: Socket @" << member->socketID << " is unstable for " << unstable_for_ms
3398 << "ms - requesting breakage.");
3399
3400 //w_sendBackupCtx.updateMemberState(member->pSocketData, BKUPST_BROKEN);
3401 //if (member->pSocketData->ps)
3402 // sendBackup_AssignBackupState(member->pSocketData->ps->core(), BKUPST_BROKEN, currtime);
3403 }
3404 }
3405
3406 // [[using locked(this->m_GroupLock)]]
3407 void CUDTGroup::send_CloseBrokenSockets(vector<SRTSOCKET>& w_wipeme)
3408 {
3409 if (!w_wipeme.empty())
3410 {
3411 InvertedLock ug(m_GroupLock);
3412
3413 // With unlocked GroupLock, we can now lock GlobControlLock.
3414 // This is needed to prevent any of them deleted from the container
3415 // at the same time.
3416 SharedLock globlock(CUDT::uglobal().m_GlobControlLock);
3417
3418 for (vector<SRTSOCKET>::iterator p = w_wipeme.begin(); p != w_wipeme.end(); ++p)
3419 {
3420 CUDTSocket* s = CUDT::uglobal().locateSocket_LOCKED(*p);
3421
3422 // If the socket has been just moved to ClosedSockets, it means that
3423 // the object still exists, but it will be no longer findable.
3424 if (!s)
3425 continue;
3426
3427 HLOGC(gslog.Debug,
3428 log << "grp/send...: BROKEN SOCKET @" << (*p) << " - CLOSING, to be removed from group.");
3429
3430 // As per sending, make it also broken so that scheduled
3431 // packets will be also abandoned.
3432 s->setClosed();
3433 }
3434 }
3435
3436 HLOGC(gslog.Debug, log << "grp/send...: - wiped " << w_wipeme.size() << " broken sockets");
3437
3438 // We'll need you again.
3439 w_wipeme.clear();
3440 }
3441
3442 // [[using locked(this->m_GroupLock)]]
3443 void CUDTGroup::sendBackup_CloseBrokenSockets(SendBackupCtx& w_sendBackupCtx)
3444 {
3445 if (w_sendBackupCtx.countMembersByState(BKUPST_BROKEN) == 0)
3446 return;
3447
3448 InvertedLock ug(m_GroupLock);
3449
3450 // With unlocked GroupLock, we can now lock GlobControlLock.
3451 // This is needed prevent any of them be deleted from the container
3452 // at the same time.
3453 SharedLock globlock(CUDT::uglobal().m_GlobControlLock);
3454
3455 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3456 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3457 {
3458 if (member->state != BKUPST_BROKEN)
3459 continue;
3460
3461 // m_GroupLock is unlocked, therefore member->pSocketData can't be used.
3462 const SRTSOCKET sockid = member->socketID;
3463 CUDTSocket* s = CUDT::uglobal().locateSocket_LOCKED(sockid);
3464
3465 // If the socket has been just moved to ClosedSockets, it means that
3466 // the object still exists, but it will be no longer findable.
3467 if (!s)
3468 continue;
3469
3470 LOGC(gslog.Debug,
3471 log << "grp/send...: BROKEN SOCKET @" << sockid << " - CLOSING, to be removed from group.");
3472
3473 // As per sending, make it also broken so that scheduled
3474 // packets will be also abandoned.
3475 s->setBrokenClosed();
3476 }
3477
3478 // TODO: all broken members are to be removed from the context now???
3479 }
3480
3481 // [[using locked(this->m_GroupLock)]]
3482 void CUDTGroup::sendBackup_RetryWaitBlocked(SendBackupCtx& w_sendBackupCtx,
3483 int& w_final_stat,
3484 bool& w_none_succeeded,
3485 SRT_MSGCTRL& w_mc,
3486 CUDTException& w_cx)
3487 {
3488 // In contradiction to broadcast sending, backup sending must check
3489 // the blocking state in total first. We need this information through
3490 // epoll because we didn't use all sockets to send the data hence the
3491 // blocked socket information would not be complete.
3492
3493 // Don't do this check if sending has succeeded over at least one
3494 // stable link. This procedure is to wait for at least one write-ready
3495 // link.
3496 //
3497 // If sending succeeded also over at least one unstable link (you only have
3498 // unstable links and none other or others just got broken), continue sending
3499 // anyway.
3500
3501
3502 // This procedure is for a case when the packet could not be sent
3503 // over any link (hence "none succeeded"), but there are some unstable
3504 // links and no parallel links. We need to WAIT for any of the links
3505 // to become available for sending.
3506
3507 // Note: A link is added in unstableLinks if sending has failed with SRT_ESYNCSND.
3508 const unsigned num_unstable = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_UNSTABLE);
3509 const unsigned num_wary = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_UNSTABLE_WARY);
3510 const unsigned num_pending = w_sendBackupCtx.countMembersByState(BKUPST_PENDING);
3511 if ((num_unstable + num_wary + num_pending == 0) || !w_none_succeeded)
3512 return;
3513
3514 HLOGC(gslog.Debug, log << "grp/sendBackup: no successful sending: "
3515 << (num_unstable + num_wary) << " unstable links, "
3516 << num_pending << " pending - waiting to retry sending...");
3517
3518 // Note: GroupLock is set already, skip locks and checks
3519 getGroupData_LOCKED((w_mc.grpdata), (&w_mc.grpdata_size));
3520 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
3521 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_ERR, true);
3522
3523 if (m_Global.m_EPoll.empty(*m_SndEpolld))
3524 {
3525 // wipeme wiped, pending sockets checked, it can only mean that
3526 // all sockets are broken.
3527 HLOGC(gslog.Debug, log << "grp/sendBackup: epolld empty - all sockets broken?");
3528 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3529 }
3530
3531 if (!m_bSynSending)
3532 {
3533 HLOGC(gslog.Debug, log << "grp/sendBackup: non-blocking mode - exit with no-write-ready");
3534 throw CUDTException(MJ_AGAIN, MN_WRAVAIL, 0);
3535 }
3536 // Here is the situation that the only links left here are:
3537 // - those that failed to send (already closed and wiped out)
3538 // - those that got blockade on sending
3539
3540 // At least, there was so far no socket through which we could
3541 // successfully send anything.
3542
3543 // As a last resort in this situation, try to wait for any links
3544 // remaining in the group to become ready to write.
3545
3546 CEPoll::fmap_t sready;
3547 int brdy;
3548
3549 // This keeps the number of links that existed at the entry.
3550 // Simply notify all dead links, regardless as to whether the number
3551 // of group members decreases below. If the number of corpses reaches
3552 // this number, consider the group connection broken.
3553 const size_t nlinks = m_Group.size();
3554 size_t ndead = 0;
3555
3556 RetryWaitBlocked:
3557 {
3558 // Some sockets could have been closed in the meantime.
3559 if (m_Global.m_EPoll.empty(*m_SndEpolld))
3560 {
3561 HLOGC(gslog.Debug, log << "grp/sendBackup: no more sockets available for sending - group broken");
3562 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3563 }
3564
3565 InvertedLock ug(m_GroupLock);
3566 HLOGC(gslog.Debug,
3567 log << "grp/sendBackup: swait call to get at least one link alive up to " << m_iSndTimeOut << "us");
3568 THREAD_PAUSED();
3569 brdy = m_Global.m_EPoll.swait(*m_SndEpolld, (sready), m_iSndTimeOut);
3570 THREAD_RESUMED();
3571
3572 if (brdy == 0) // SND timeout exceeded
3573 {
3574 throw CUDTException(MJ_AGAIN, MN_WRAVAIL, 0);
3575 }
3576
3577 HLOGC(gslog.Debug, log << "grp/sendBackup: swait exited with " << brdy << " ready sockets:");
3578
3579 // Check if there's anything in the "error" section.
3580 // This must be cleared here before the lock on group is set again.
3581 // (This loop will not fire neither once if no failed sockets found).
3582 for (CEPoll::fmap_t::const_iterator i = sready.begin(); i != sready.end(); ++i)
3583 {
3584 if (i->second & SRT_EPOLL_ERR)
3585 {
3586 SRTSOCKET id = i->first;
3587 CUDTSocket* s = m_Global.locateSocket(id, CUDTUnited::ERH_RETURN); // << LOCKS m_GlobControlLock!
3588 if (s)
3589 {
3590 HLOGC(gslog.Debug,
3591 log << "grp/sendBackup: swait/ex on @" << (id)
3592 << " while waiting for any writable socket - CLOSING");
3593 CUDT::uglobal().close(s); // << LOCKS m_GlobControlLock, then GroupLock!
3594 }
3595 else
3596 {
3597 HLOGC(gslog.Debug, log << "grp/sendBackup: swait/ex on @" << (id) << " - WAS DELETED IN THE MEANTIME");
3598 }
3599
3600 ++ndead;
3601 }
3602 }
3603 HLOGC(gslog.Debug, log << "grp/sendBackup: swait/?close done, re-acquiring GroupLock");
3604 }
3605
3606 // GroupLock is locked back
3607
3608 // Re-check after the waiting lock has been reacquired
3609 if (m_bClosing)
3610 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3611
3612 if (brdy == -1 || ndead >= nlinks)
3613 {
3614 LOGC(gslog.Error,
3615 log << "grp/sendBackup: swait=>" << brdy << " nlinks=" << nlinks << " ndead=" << ndead
3616 << " - looxlike all links broken");
3617 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
3618 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_ERR, true);
3619 // You can safely throw here - nothing to fill in when all sockets down.
3620 // (timeout was reported by exception in the swait call).
3621 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3622 }
3623
3624 // Ok, now check if we have at least one write-ready.
3625 // Note that the procedure of activation of a new link in case of
3626 // no stable links found embraces also rexmit-sending and status
3627 // check as well, including blocked status.
3628
3629 // Find which one it was. This is so rare case that we can
3630 // suffer linear search.
3631
3632 int nwaiting = 0;
3633 int nactivated SRT_ATR_UNUSED = 0;
3634 int stat = -1;
3635 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d)
3636 {
3637 // We are waiting only for active members
3638 if (d->sndstate != SRT_GST_RUNNING)
3639 {
3640 HLOGC(gslog.Debug,
3641 log << "grp/sendBackup: member @" << d->id << " state is not RUNNING - SKIPPING from retry/waiting");
3642 continue;
3643 }
3644 // Skip if not writable in this run
3645 if (!CEPoll::isready(sready, d->id, SRT_EPOLL_OUT))
3646 {
3647 ++nwaiting;
3648 HLOGC(gslog.Debug, log << "grp/sendBackup: @" << d->id << " NOT ready:OUT, added as waiting");
3649 continue;
3650 }
3651
3652 try
3653 {
3654 // Note: this will set the currently required packet
3655 // because it has been just freshly added to the sender buffer
3656 stat = sendBackupRexmit(d->ps->core(), (w_mc));
3657 ++nactivated;
3658 }
3659 catch (CUDTException& e)
3660 {
3661 // This will be propagated from internal sendmsg2 call,
3662 // but that's ok - we want this sending interrupted even in half.
3663 w_cx = e;
3664 stat = -1;
3665 }
3666
3667 d->sndresult = stat;
3668 d->laststatus = d->ps->getStatus();
3669
3670 if (stat == -1)
3671 {
3672 // This link is no longer waiting.
3673 continue;
3674 }
3675
3676 w_final_stat = stat;
3677 d->sndstate = SRT_GST_RUNNING;
3678 w_none_succeeded = false;
3679 const steady_clock::time_point currtime = steady_clock::now();
3680 sendBackup_AssignBackupState(d->ps->core(), BKUPST_ACTIVE_UNSTABLE_WARY, currtime);
3681 w_sendBackupCtx.updateMemberState(&(*d), BKUPST_ACTIVE_UNSTABLE_WARY);
3682 HLOGC(gslog.Debug, log << "grp/sendBackup: after waiting, ACTIVATED link @" << d->id);
3683
3684 break;
3685 }
3686
3687 // If we have no links successfully activated, but at least
3688 // one link "not ready for writing", continue waiting for at
3689 // least one link ready.
3690 if (stat == -1 && nwaiting > 0)
3691 {
3692 HLOGC(gslog.Debug, log << "grp/sendBackup: still have " << nwaiting << " waiting and none succeeded, REPEAT");
3693 goto RetryWaitBlocked;
3694 }
3695 }
3696
3697 // [[using locked(this->m_GroupLock)]]
3698 void CUDTGroup::sendBackup_SilenceRedundantLinks(SendBackupCtx& w_sendBackupCtx, const steady_clock::time_point& currtime)
3699 {
3700 // The most important principle is to keep the data being sent constantly,
3701 // even if it means temporarily full redundancy.
3702 // A member can be silenced only if there is at least one stable member.
3703 const unsigned num_stable = w_sendBackupCtx.countMembersByState(BKUPST_ACTIVE_STABLE);
3704 if (num_stable == 0)
3705 return;
3706
3707 // INPUT NEEDED:
3708 // - stable member with maximum weight
3709
3710 uint16_t max_weight_stable = 0;
3711 SRTSOCKET stableSocketId = SRT_INVALID_SOCK; // SocketID of a stable link with higher weight
3712
3713 w_sendBackupCtx.sortByWeightAndState();
3714 //LOGC(gslog.Debug, log << "grp/silenceRedundant: links after sort: " << w_sendBackupCtx.printMembers());
3715 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3716 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3717 {
3718 if (!isStateActive(member->state))
3719 continue;
3720
3721 const bool haveHigherWeightStable = stableSocketId != SRT_INVALID_SOCK;
3722 const uint16_t weight = member->pSocketData->weight;
3723
3724 if (member->state == BKUPST_ACTIVE_STABLE)
3725 {
3726 // silence stable link if it is not the first stable
3727 if (!haveHigherWeightStable)
3728 {
3729 max_weight_stable = (int) weight;
3730 stableSocketId = member->socketID;
3731 continue;
3732 }
3733 else
3734 {
3735 LOGC(gslog.Note, log << "grp/sendBackup: silencing stable member @" << member->socketID << " (weight " << weight
3736 << ") in favor of @" << stableSocketId << " (weight " << max_weight_stable << ")");
3737 }
3738 }
3739 else if (haveHigherWeightStable && weight <= max_weight_stable)
3740 {
3741 LOGC(gslog.Note, log << "grp/sendBackup: silencing member @" << member->socketID << " (weight " << weight
3742 << " " << stateToStr(member->state)
3743 << ") in favor of @" << stableSocketId << " (weight " << max_weight_stable << ")");
3744 }
3745 else
3746 {
3747 continue;
3748 }
3749
3750 // TODO: Move to a separate function sendBackup_SilenceMember
3751 SocketData* d = member->pSocketData;
3752 CUDT& u = d->ps->core();
3753
3754 sendBackup_AssignBackupState(u, BKUPST_STANDBY, currtime);
3755 w_sendBackupCtx.updateMemberState(d, BKUPST_STANDBY);
3756
3757 if (d->sndstate != SRT_GST_RUNNING)
3758 {
3759 LOGC(gslog.Error,
3760 log << "grp/sendBackup: IPE: misidentified a non-running link @" << d->id << " as active");
3761 continue;
3762 }
3763
3764 d->sndstate = SRT_GST_IDLE;
3765 }
3766 }
3767
3768 int CUDTGroup::sendBackup(const char* buf, int len, SRT_MSGCTRL& w_mc)
3769 {
3770 if (len <= 0)
3771 {
3772 LOGC(gslog.Error, log << "grp/send(backup): negative length: " << len);
3773 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
3774 }
3775
3776 // Only live streaming is supported
3777 if (len > SRT_LIVE_MAX_PLSIZE)
3778 {
3779 LOGC(gslog.Error, log << "grp/send(backup): buffer size=" << len << " exceeds maximum allowed in live mode");
3780 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
3781 }
3782
3783 // [[using assert(this->m_pSndBuffer != nullptr)]];
3784
3785 // First, acquire GlobControlLock to make sure all member sockets still exist
3786 enterCS(m_Global.m_GlobControlLock);
3787 ScopedLock guard(m_GroupLock);
3788
3789 if (m_bClosing)
3790 {
3791 leaveCS(m_Global.m_GlobControlLock);
3792 LOGC(gslog.Error, log << "grp/send(backup): Cannot send, connection lost!");
3793 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3794 }
3795
3796 // Now, still under lock, check if all sockets still can be dispatched
3797 send_CheckValidSockets();
3798 leaveCS(m_Global.m_GlobControlLock);
3799
3800 steady_clock::time_point currtime = steady_clock::now();
3801
3802 SendBackupCtx sendBackupCtx; // default initialized as empty
3803 // TODO: reserve? sendBackupCtx.memberStates.reserve(m_Group.size());
3804
3805 sendBackup_QualifyMemberStates((sendBackupCtx), currtime);
3806
3807 int32_t curseq = SRT_SEQNO_NONE;
3808 size_t nsuccessful = 0;
3809
3810 SRT_ATR_UNUSED CUDTException cx(MJ_SUCCESS, MN_NONE, 0); // TODO: Delete then?
3811 uint16_t maxActiveWeight = 0; // Maximum weight of active links.
3812 // The number of bytes sent or -1 for error will be stored in group_send_result
3813 int group_send_result = sendBackup_SendOverActive(buf, len, w_mc, currtime, (curseq), (nsuccessful), (maxActiveWeight), (sendBackupCtx), (cx));
3814 bool none_succeeded = (nsuccessful == 0);
3815
3816 // Save current payload in group's sender buffer.
3817 sendBackup_Buffering(buf, len, (curseq), (w_mc));
3818
3819 sendBackup_TryActivateStandbyIfNeeded(buf, len, (none_succeeded),
3820 (w_mc),
3821 (curseq),
3822 (group_send_result),
3823 (sendBackupCtx),
3824 (cx), currtime);
3825
3826 sendBackup_CheckPendingSockets((sendBackupCtx), currtime);
3827 sendBackup_CheckUnstableSockets((sendBackupCtx), currtime);
3828
3829 //LOGC(gslog.Debug, log << "grp/sendBackup: links after all checks: " << sendBackupCtx.printMembers());
3830
3831 // Re-check after the waiting lock has been reacquired
3832 if (m_bClosing)
3833 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3834
3835 sendBackup_CloseBrokenSockets((sendBackupCtx));
3836
3837 // Re-check after the waiting lock has been reacquired
3838 if (m_bClosing)
3839 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3840
3841 // If all links out of the unstable-running links are blocked (SRT_EASYNCSND),
3842 // perform epoll wait on them. In this situation we know that
3843 // there are no idle blocked links because IDLE LINK CAN'T BE BLOCKED,
3844 // no matter what. It's because the link may only be blocked if
3845 // the sender buffer of this socket is full, and it can't be
3846 // full if it wasn't used so far.
3847 //
3848 // This means that in case when we have no stable links, we
3849 // need to try out any link that can accept the rexmit-load.
3850 // We'll check link stability at the next sending attempt.
3851 sendBackup_RetryWaitBlocked((sendBackupCtx), (group_send_result), (none_succeeded), (w_mc), (cx));
3852
3853 sendBackup_SilenceRedundantLinks((sendBackupCtx), currtime);
3854 // (closing condition checked inside this call)
3855
3856 if (none_succeeded)
3857 {
3858 HLOGC(gslog.Debug, log << "grp/sendBackup: all links broken (none succeeded to send a payload)");
3859 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
3860 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_ERR, true);
3861 // Reparse error code, if set.
3862 // It might be set, if the last operation was failed.
3863 // If any operation succeeded, this will not be executed anyway.
3864
3865 throw CUDTException(MJ_CONNECTION, MN_CONNLOST, 0);
3866 }
3867
3868 // At least one link has succeeded, update sending stats.
3869 m_stats.sent.count(len);
3870
3871 // Now fill in the socket table. Check if the size is enough, if not,
3872 // then set the pointer to NULL and set the correct size.
3873
3874 // Note that list::size() is linear time, however this shouldn't matter,
3875 // as with the increased number of links in the redundancy group the
3876 // impossibility of using that many of them grows exponentally.
3877 const size_t grpsize = m_Group.size();
3878
3879 if (w_mc.grpdata_size < grpsize)
3880 {
3881 w_mc.grpdata = NULL;
3882 }
3883
3884 size_t i = 0;
3885
3886 bool ready_again = false;
3887
3888 HLOGC(gslog.Debug, log << "grp/sendBackup: copying group data");
3889 for (gli_t d = m_Group.begin(); d != m_Group.end(); ++d, ++i)
3890 {
3891 if (w_mc.grpdata)
3892 {
3893 // Enough space to fill
3894 copyGroupData(*d, (w_mc.grpdata[i]));
3895 }
3896
3897 // We perform this loop anyway because we still need to check if any
3898 // socket is writable. Note that the group lock will hold any write ready
3899 // updates that are performed just after a single socket update for the
3900 // group, so if any socket is actually ready at the moment when this
3901 // is performed, and this one will result in none-write-ready, this will
3902 // be fixed just after returning from this function.
3903
3904 ready_again = ready_again || d->ps->writeReady();
3905 }
3906 w_mc.grpdata_size = i;
3907
3908 if (!ready_again)
3909 {
3910 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, false);
3911 }
3912
3913 HLOGC(gslog.Debug,
3914 log << "grp/sendBackup: successfully sent " << group_send_result << " bytes, "
3915 << (ready_again ? "READY for next" : "NOT READY to send next"));
3916 return group_send_result;
3917 }
3918
3919 // [[using locked(this->m_GroupLock)]]
3920 int32_t CUDTGroup::addMessageToBuffer(const char* buf, size_t len, SRT_MSGCTRL& w_mc)
3921 {
3922 if (m_iSndAckedMsgNo == SRT_MSGNO_NONE)
3923 {
3924 // Very first packet, just set the msgno.
3925 m_iSndAckedMsgNo = w_mc.msgno;
3926 m_iSndOldestMsgNo = w_mc.msgno;
3927 HLOGC(gslog.Debug, log << "addMessageToBuffer: initial message no #" << w_mc.msgno);
3928 }
3929 else if (m_iSndOldestMsgNo != m_iSndAckedMsgNo)
3930 {
3931 int offset = MsgNo(m_iSndAckedMsgNo) - MsgNo(m_iSndOldestMsgNo);
3932 HLOGC(gslog.Debug,
3933 log << "addMessageToBuffer: new ACK-ed messages: #(" << m_iSndOldestMsgNo << "-" << m_iSndAckedMsgNo
3934 << ") - going to remove");
3935
3936 if (offset > int(m_SenderBuffer.size()))
3937 {
3938 LOGC(gslog.Error,
3939 log << "addMessageToBuffer: IPE: offset=" << offset << " exceeds buffer size=" << m_SenderBuffer.size()
3940 << " - CLEARING");
3941 m_SenderBuffer.clear();
3942 }
3943 else
3944 {
3945 HLOGC(gslog.Debug,
3946 log << "addMessageToBuffer: erasing " << offset << "/" << m_SenderBuffer.size()
3947 << " group-senderbuffer ACKED messages for #" << m_iSndOldestMsgNo << " - #" << m_iSndAckedMsgNo);
3948 m_SenderBuffer.erase(m_SenderBuffer.begin(), m_SenderBuffer.begin() + offset);
3949 }
3950
3951 // Position at offset is not included
3952 m_iSndOldestMsgNo = m_iSndAckedMsgNo;
3953 HLOGC(gslog.Debug,
3954 log << "addMessageToBuffer: ... after: oldest #" << m_iSndOldestMsgNo);
3955 }
3956
3957 m_SenderBuffer.resize(m_SenderBuffer.size() + 1);
3958 BufferedMessage& bm = m_SenderBuffer.back();
3959 bm.mc = w_mc;
3960 bm.copy(buf, len);
3961
3962 HLOGC(gslog.Debug,
3963 log << "addMessageToBuffer: #" << w_mc.msgno << " size=" << len << " !" << BufferStamp(buf, len));
3964
3965 return m_SenderBuffer.front().mc.pktseq;
3966 }
3967
3968 int CUDTGroup::sendBackup_SendOverActive(const char* buf, int len, SRT_MSGCTRL& w_mc, const steady_clock::time_point& currtime, int32_t& w_curseq,
3969 size_t& w_nsuccessful, uint16_t& w_maxActiveWeight, SendBackupCtx& w_sendBackupCtx, CUDTException& w_cx)
3970 {
3971 if (w_mc.srctime == 0)
3972 w_mc.srctime = count_microseconds(currtime.time_since_epoch());
3973
3974 SRT_ASSERT(w_nsuccessful == 0);
3975 SRT_ASSERT(w_maxActiveWeight == 0);
3976
3977 int group_send_result = SRT_ERROR;
3978
3979 // TODO: implement iterator over active links
3980 typedef vector<BackupMemberStateEntry>::const_iterator const_iter_t;
3981 for (const_iter_t member = w_sendBackupCtx.memberStates().begin(); member != w_sendBackupCtx.memberStates().end(); ++member)
3982 {
3983 if (!isStateActive(member->state))
3984 continue;
3985
3986 SocketData* d = member->pSocketData;
3987 int erc = SRT_SUCCESS;
3988 // Remaining sndstate is SRT_GST_RUNNING. Send a payload through it.
3989 CUDT& u = d->ps->core();
3990 const int32_t lastseq = u.schedSeqNo();
3991 int sndresult = SRT_ERROR;
3992 try
3993 {
3994 // This must be wrapped in try-catch because on error it throws an exception.
3995 // Possible return values are only 0, in case when len was passed 0, or a positive
3996 // >0 value that defines the size of the data that it has sent, that is, in case
3997 // of Live mode, equal to 'len'.
3998 sndresult = u.sendmsg2(buf, len, (w_mc));
3999 }
4000 catch (CUDTException& e)
4001 {
4002 w_cx = e;
4003 erc = e.getErrorCode();
4004 sndresult = SRT_ERROR;
4005 }
4006
4007 const bool send_succeeded = sendBackup_CheckSendStatus(
4008 currtime,
4009 sndresult,
4010 lastseq,
4011 w_mc.pktseq,
4012 (u),
4013 (w_curseq),
4014 (group_send_result));
4015
4016 if (send_succeeded)
4017 {
4018 ++w_nsuccessful;
4019 w_maxActiveWeight = max(w_maxActiveWeight, d->weight);
4020
4021 if (u.m_pSndBuffer)
4022 w_sendBackupCtx.setRateEstimate(u.m_pSndBuffer->getRateEstimator());
4023 }
4024 else if (erc == SRT_EASYNCSND)
4025 {
4026 sendBackup_AssignBackupState(u, BKUPST_ACTIVE_UNSTABLE, currtime);
4027 w_sendBackupCtx.updateMemberState(d, BKUPST_ACTIVE_UNSTABLE);
4028 }
4029
4030 d->sndresult = sndresult;
4031 d->laststatus = d->ps->getStatus();
4032 }
4033
4034 return group_send_result;
4035 }
4036
4037 // [[using locked(this->m_GroupLock)]]
4038 int CUDTGroup::sendBackupRexmit(CUDT& core, SRT_MSGCTRL& w_mc)
4039 {
4040 // This should resend all packets
4041 if (m_SenderBuffer.empty())
4042 {
4043 LOGC(gslog.Fatal, log << "IPE: sendBackupRexmit: sender buffer empty");
4044
4045 // Although act as if it was successful, otherwise you'll get connection break
4046 return 0;
4047 }
4048
4049 // using [[assert !m_SenderBuffer.empty()]];
4050
4051 // Send everything you currently have in the sender buffer.
4052 // The receiver will reject packets that it currently has.
4053 // Start from the oldest.
4054
4055 CPacket packet;
4056
4057 set<int> results;
4058 int stat = -1;
4059
4060 // Make sure that the link has correctly synchronized sequence numbers.
4061 // Note that sequence numbers should be recorded in mc.
4062 int32_t curseq = m_SenderBuffer[0].mc.pktseq;
4063 size_t skip_initial = 0;
4064 if (curseq != core.schedSeqNo())
4065 {
4066 const int distance = CSeqNo::seqoff(core.schedSeqNo(), curseq);
4067 if (distance < 0)
4068 {
4069 // This may happen in case when the link to be activated is already running.
4070 // Getting sequences backwards is not allowed, as sending them makes no
4071 // sense - they are already ACK-ed or are behind the ISN. Instead, skip all
4072 // packets that are in the past towards the scheduling sequence.
4073 skip_initial = -distance;
4074 LOGC(gslog.Warn,
4075 log << "sendBackupRexmit: OVERRIDE attempt. Link seqno %" << core.schedSeqNo() << ", trying to send from seqno %" << curseq
4076 << " - DENIED; skip " << skip_initial << " pkts, " << m_SenderBuffer.size() << " pkts in buffer");
4077 }
4078 else
4079 {
4080 // In case when the next planned sequence on this link is behind
4081 // the firstmost sequence in the backup buffer, synchronize the
4082 // sequence with it first so that they go hand-in-hand with
4083 // sequences already used by the link from which packets were
4084 // copied to the backup buffer.
4085 IF_HEAVY_LOGGING(int32_t old = core.schedSeqNo());
4086 const bool su SRT_ATR_UNUSED = core.overrideSndSeqNo(curseq);
4087 HLOGC(gslog.Debug,
4088 log << "sendBackupRexmit: OVERRIDING seq %" << old << " with %" << curseq
4089 << (su ? " - succeeded" : " - FAILED!"));
4090 }
4091 }
4092
4093
4094 if (skip_initial >= m_SenderBuffer.size())
4095 {
4096 LOGC(gslog.Warn,
4097 log << "sendBackupRexmit: All packets were skipped. Nothing to send %" << core.schedSeqNo() << ", trying to send from seqno %" << curseq
4098 << " - DENIED; skip " << skip_initial << " packets");
4099 return 0; // can't return any other state, nothing was sent
4100 }
4101
4102 senderBuffer_t::iterator i = m_SenderBuffer.begin() + skip_initial;
4103
4104 // Send everything - including the packet freshly added to the buffer
4105 for (; i != m_SenderBuffer.end(); ++i)
4106 {
4107 // NOTE: an exception from here will interrupt the loop
4108 // and will be caught in the upper level.
4109 stat = core.sendmsg2(i->data, (int)i->size, (i->mc));
4110 if (stat == -1)
4111 {
4112 // Stop sending if one sending ended up with error
4113 LOGC(gslog.Warn,
4114 log << "sendBackupRexmit: sending from buffer stopped at %" << core.schedSeqNo() << " and FAILED");
4115 return -1;
4116 }
4117 }
4118
4119 // Copy the contents of the last item being updated.
4120 w_mc = m_SenderBuffer.back().mc;
4121 HLOGC(gslog.Debug, log << "sendBackupRexmit: pre-sent collected %" << curseq << " - %" << w_mc.pktseq);
4122 return stat;
4123 }
4124
4125 // [[using locked(CUDTGroup::m_GroupLock)]];
4126 void CUDTGroup::ackMessage(int32_t msgno)
4127 {
4128 // The message id could not be identified, skip.
4129 if (msgno == SRT_MSGNO_CONTROL)
4130 {
4131 HLOGC(gslog.Debug, log << "ackMessage: msgno not found in ACK-ed sequence");
4132 return;
4133 }
4134
4135 // It's impossible to get the exact message position as the
4136 // message is allowed also to span for multiple packets.
4137 // Search since the oldest packet until you hit the first
4138 // packet with this message number.
4139
4140 // First, you need to decrease the message number by 1. It's
4141 // because the sequence number being ACK-ed can be in the middle
4142 // of the message, while it doesn't acknowledge that the whole
4143 // message has been received. Decrease the message number so that
4144 // partial-message-acknowledgement does not swipe the whole message,
4145 // part of which may need to be retransmitted over a backup link.
4146
4147 int offset = MsgNo(msgno) - MsgNo(m_iSndAckedMsgNo);
4148 if (offset <= 0)
4149 {
4150 HLOGC(gslog.Debug, log << "ackMessage: already acked up to msgno=" << msgno);
4151 return;
4152 }
4153
4154 HLOGC(gslog.Debug, log << "ackMessage: updated to #" << msgno);
4155
4156 // Update last acked. Will be picked up when adding next message.
4157 m_iSndAckedMsgNo = msgno;
4158 }
4159
4160 void CUDTGroup::processKeepalive(CUDTGroup::SocketData* gli)
4161 {
4162 // received keepalive for that group member
4163 // In backup group it means that the link went IDLE.
4164 if (m_type == SRT_GTYPE_BACKUP)
4165 {
4166 if (gli->rcvstate == SRT_GST_RUNNING)
4167 {
4168 gli->rcvstate = SRT_GST_IDLE;
4169 HLOGC(gslog.Debug, log << "GROUP: received KEEPALIVE in @" << gli->id << " - link turning rcv=IDLE");
4170 }
4171
4172 // When received KEEPALIVE, the sending state should be also
4173 // turned IDLE, if the link isn't temporarily activated. The
4174 // temporarily activated link will not be measured stability anyway,
4175 // while this should clear out the problem when the transmission is
4176 // stopped and restarted after a while. This will simply set the current
4177 // link as IDLE on the sender when the peer sends a keepalive because the
4178 // data stopped coming in and it can't send ACKs therefore.
4179 //
4180 // This also shouldn't be done for the temporary activated links because
4181 // stability timeout could be exceeded for them by a reason that, for example,
4182 // the packets come with the past sequences (as they are being synchronized
4183 // the sequence per being IDLE and empty buffer), so a large portion of initial
4184 // series of packets may come with past sequence, delaying this way with ACK,
4185 // which may result not only with exceeded stability timeout (which fortunately
4186 // isn't being measured in this case), but also with receiving keepalive
4187 // (therefore we also don't reset the link to IDLE in the temporary activation period).
4188 if (gli->sndstate == SRT_GST_RUNNING && is_zero(gli->ps->core().m_tsFreshActivation))
4189 {
4190 gli->sndstate = SRT_GST_IDLE;
4191 HLOGC(gslog.Debug,
4192 log << "GROUP: received KEEPALIVE in @" << gli->id << " active=PAST - link turning snd=IDLE");
4193 }
4194 }
4195 }
4196
4197 void CUDTGroup::internalKeepalive(SocketData* gli)
4198 {
4199 // This is in response to AGENT SENDING keepalive. This means that there's
4200 // no transmission in either direction, but the KEEPALIVE packet from the
4201 // other party could have been missed. This is to ensure that the IDLE state
4202 // is recognized early enough, before any sequence discrepancy can happen.
4203
4204 if (m_type == SRT_GTYPE_BACKUP && gli->rcvstate == SRT_GST_RUNNING)
4205 {
4206 gli->rcvstate = SRT_GST_IDLE;
4207 // Prevent sending KEEPALIVE again in group-sending
4208 gli->ps->core().m_tsFreshActivation = steady_clock::time_point();
4209 HLOGC(gslog.Debug, log << "GROUP: EXP-requested KEEPALIVE in @" << gli->id << " - link turning IDLE");
4210 }
4211 }
4212
4213 CUDTGroup::BufferedMessageStorage CUDTGroup::BufferedMessage::storage(SRT_LIVE_MAX_PLSIZE /*, 1000*/);
4214
4215 // Forwarder needed due to class definition order
4216 int32_t CUDTGroup::generateISN()
4217 {
4218 return CUDT::generateISN();
4219 }
4220
4221 void CUDTGroup::setGroupConnected()
4222 {
4223 if (!m_bConnected)
4224 {
4225 // Switch to connected state and give appropriate signal
4226 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_CONNECT, true);
4227 m_bConnected = true;
4228 }
4229 }
4230
4231 void CUDTGroup::updateLatestRcv(CUDTSocket* s)
4232 {
4233 // Currently only Backup groups use connected idle links.
4234 if (m_type != SRT_GTYPE_BACKUP)
4235 return;
4236
4237 HLOGC(grlog.Debug,
4238 log << "updateLatestRcv: BACKUP group, updating from active link @" << s->m_SocketID << " with %"
4239 << s->core().m_iRcvLastAck);
4240
4241 CUDT* source = &s->core();
4242 vector<CUDT*> targets;
4243
4244 UniqueLock lg(m_GroupLock);
4245 // Sanity check for a case when getting a deleted socket
4246 if (!s->m_GroupOf)
4247 return;
4248
4249 // Under a group lock, we block execution of removal of the socket
4250 // from the group, so if m_GroupOf is not NULL, we are granted
4251 // that m_GroupMemberData is valid.
4252 SocketData* current = s->m_GroupMemberData;
4253
4254 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
4255 {
4256 // Skip the socket that has reported packet reception
4257 if (&*gi == current)
4258 {
4259 HLOGC(grlog.Debug, log << "grp: NOT updating rcv-seq on self @" << gi->id);
4260 continue;
4261 }
4262
4263 // Don't update the state if the link is:
4264 // - PENDING - because it's not in the connected state, wait for it.
4265 // - RUNNING - because in this case it should have its own line of sequences
4266 // - BROKEN - because it doesn't make sense anymore, about to be removed
4267 if (gi->rcvstate != SRT_GST_IDLE)
4268 {
4269 HLOGC(grlog.Debug,
4270 log << "grp: NOT updating rcv-seq on @" << gi->id
4271 << " - link state:" << srt_log_grp_state[gi->rcvstate]);
4272 continue;
4273 }
4274
4275 // Sanity check
4276 if (!gi->ps->core().m_bConnected)
4277 {
4278 HLOGC(grlog.Debug, log << "grp: IPE: NOT updating rcv-seq on @" << gi->id << " - IDLE BUT NOT CONNECTED");
4279 continue;
4280 }
4281
4282 targets.push_back(&gi->ps->core());
4283 }
4284
4285 lg.unlock();
4286
4287 // Do this on the unlocked group because this
4288 // operation will need receiver lock, so it might
4289 // risk a deadlock.
4290
4291 for (size_t i = 0; i < targets.size(); ++i)
4292 {
4293 targets[i]->updateIdleLinkFrom(source);
4294 }
4295 }
4296
4297 void CUDTGroup::activateUpdateEvent(bool still_have_items)
4298 {
4299 // This function actually reacts on the fact that a socket
4300 // was deleted from the group. This might make the group empty.
4301 if (!still_have_items) // empty, or removal of unknown socket attempted - set error on group
4302 {
4303 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR, true);
4304 }
4305 else
4306 {
4307 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_UPDATE, true);
4308 }
4309 }
4310
4311 void CUDTGroup::addEPoll(int eid)
4312 {
4313 enterCS(m_Global.m_EPoll.m_EPollLock);
4314 m_sPollID.insert(eid);
4315 leaveCS(m_Global.m_EPoll.m_EPollLock);
4316
4317 bool any_read = false;
4318 bool any_write = false;
4319 bool any_broken = false;
4320 bool any_pending = false;
4321
4322 {
4323 // Check all member sockets
4324 ScopedLock gl(m_GroupLock);
4325
4326 // We only need to know if there is any socket that is
4327 // ready to get a payload and ready to receive from.
4328
4329 for (gli_t i = m_Group.begin(); i != m_Group.end(); ++i)
4330 {
4331 if (i->sndstate == SRT_GST_IDLE || i->sndstate == SRT_GST_RUNNING)
4332 {
4333 any_write |= i->ps->writeReady();
4334 }
4335
4336 if (i->rcvstate == SRT_GST_IDLE || i->rcvstate == SRT_GST_RUNNING)
4337 {
4338 any_read |= i->ps->readReady();
4339 }
4340
4341 if (i->ps->broken())
4342 any_broken |= true;
4343 else
4344 any_pending |= true;
4345 }
4346 }
4347
4348 // This is stupid, but we don't have any other interface to epoll
4349 // internals. Actually we don't have to check if id() is in m_sPollID
4350 // because we know it is, as we just added it. But it's not performance
4351 // critical, sockets are not being often added during transmission.
4352 if (any_read)
4353 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN, true);
4354
4355 if (any_write)
4356 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_OUT, true);
4357
4358 // Set broken if none is non-broken (pending, read-ready or write-ready)
4359 if (any_broken && !any_pending)
4360 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_ERR, true);
4361 }
4362
4363 void CUDTGroup::removeEPollEvents(const int eid)
4364 {
4365 // clear IO events notifications;
4366 // since this happens after the epoll ID has been removed, they cannot be set again
4367 set<int> remove;
4368 remove.insert(eid);
4369 m_Global.m_EPoll.update_events(id(), remove, SRT_EPOLL_IN | SRT_EPOLL_OUT, false);
4370 }
4371
4372 void CUDTGroup::removeEPollID(const int eid)
4373 {
4374 enterCS(m_Global.m_EPoll.m_EPollLock);
4375 m_sPollID.erase(eid);
4376 leaveCS(m_Global.m_EPoll.m_EPollLock);
4377 }
4378
4379 void CUDTGroup::updateFailedLink()
4380 {
4381 ScopedLock lg(m_GroupLock);
4382
4383 // Check all members if they are in the pending
4384 // or connected state.
4385
4386 int nhealthy = 0;
4387
4388 for (gli_t i = m_Group.begin(); i != m_Group.end(); ++i)
4389 {
4390 if (i->sndstate < SRT_GST_BROKEN)
4391 nhealthy++;
4392 }
4393
4394 if (!nhealthy)
4395 {
4396 // No healthy links, set ERR on epoll.
4397 HLOGC(gmlog.Debug, log << "group/updateFailedLink: All sockets broken");
4398 m_Global.m_EPoll.update_events(id(), m_sPollID, SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR, true);
4399 }
4400 else
4401 {
4402 HLOGC(gmlog.Debug, log << "group/updateFailedLink: Still " << nhealthy << " links in the group");
4403 }
4404 }
4405
4406 #if ENABLE_HEAVY_LOGGING
4407 // [[using maybe_locked(CUDT::uglobal()->m_GlobControlLock)]]
4408 void CUDTGroup::debugGroup()
4409 {
4410 ScopedLock gg(m_GroupLock);
4411
4412 HLOGC(gmlog.Debug, log << "GROUP MEMBER STATUS - $" << id());
4413
4414 for (gli_t gi = m_Group.begin(); gi != m_Group.end(); ++gi)
4415 {
4416 HLOGC(gmlog.Debug,
4417 log << " ... id { agent=@" << gi->id << " peer=@" << gi->ps->m_PeerID
4418 << " } address { agent=" << gi->agent.str() << " peer=" << gi->peer.str() << "} "
4419 << " state {snd=" << StateStr(gi->sndstate) << " rcv=" << StateStr(gi->rcvstate) << "}");
4420 }
4421 }
4422 #endif
4423
4424 } // namespace srt