Debian Codegraph @

srtcore/epoll.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above
20 copyright notice, this list of conditions and the
21 following disclaimer.
22
23 * Redistributions in binary form must reproduce the
24 above copyright notice, this list of conditions
25 and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 * Neither the name of the University of Illinois
29 nor the names of its contributors may be used to
30 endorse or promote products derived from this
31 software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45
46 /*****************************************************************************
47 written by
48 Yunhong Gu, last updated 01/01/2011
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #define SRT_IMPORT_EVENT
54 #include "platform_sys.h"
55
56 #include <algorithm>
57 #include <cerrno>
58 #include <cstring>
59 #include <iterator>
60
61 #if defined(__FreeBSD_kernel__)
62 #include <sys/event.h>
63 #endif
64
65 #include "common.h"
66 #include "epoll.h"
67 #include "logging.h"
68 #include "udt.h"
69 #include "utilities.h"
70
71 using namespace std;
72 using namespace srt::sync;
73
74 namespace srt_logging
75 {
76 extern Logger eilog, ealog;
77 }
78
79 using namespace srt_logging;
80
81 #if ENABLE_HEAVY_LOGGING
82 #define IF_DIRNAME(tested, flag, name) (tested & flag ? name : "")
83 #endif
84
85 srt::CEPoll::CEPoll():
86 m_iIDSeed(0)
87 {
88 // Exception -> CUDTUnited ctor.
89 setupMutex(m_EPollLock, "EPoll");
90 }
91
92 srt::CEPoll::~CEPoll()
93 {
94 releaseMutex(m_EPollLock);
95 }
96
97 int srt::CEPoll::create(CEPollDesc** pout)
98 {
99 ScopedLock pg(m_EPollLock);
100
101 if (++ m_iIDSeed >= 0x7FFFFFFF)
102 m_iIDSeed = 0;
103
104 // Check if an item already exists. Should not ever happen.
105 if (m_mPolls.find(m_iIDSeed) != m_mPolls.end())
106 throw CUDTException(MJ_SETUP, MN_NONE);
107
108 int localid = 0;
109
110 #ifdef LINUX
111
112 // NOTE: epoll_create1() and EPOLL_CLOEXEC were introduced in GLIBC-2.9.
113 // So earlier versions of GLIBC, must use epoll_create() and set
114 // FD_CLOEXEC on the file descriptor returned by it after the fact.
115 #if defined(EPOLL_CLOEXEC)
116 int flags = 0;
117 #if ENABLE_SOCK_CLOEXEC
118 flags |= EPOLL_CLOEXEC;
119 #endif
120 localid = epoll_create1(flags);
121 #else
122 localid = epoll_create(1);
123 #if ENABLE_SOCK_CLOEXEC
124 if (localid != -1)
125 {
126 int fdFlags = fcntl(localid, F_GETFD);
127 if (fdFlags != -1)
128 {
129 fdFlags |= FD_CLOEXEC;
130 fcntl(localid, F_SETFD, fdFlags);
131 }
132 }
133 #endif
134 #endif
135
136 /* Possible reasons of -1 error:
137 EMFILE: The per-user limit on the number of epoll instances imposed by /proc/sys/fs/epoll/max_user_instances was encountered.
138 ENFILE: The system limit on the total number of open files has been reached.
139 ENOMEM: There was insufficient memory to create the kernel object.
140 */
141 if (localid < 0)
142 throw CUDTException(MJ_SETUP, MN_NONE, errno);
143 #elif defined(BSD) || TARGET_OS_MAC
144 localid = kqueue();
145 if (localid < 0)
146 throw CUDTException(MJ_SETUP, MN_NONE, errno);
147 #else
148 // TODO: Solaris, use port_getn()
149 // https://docs.oracle.com/cd/E86824_01/html/E54766/port-get-3c.html
150 // on Windows, select
151 #endif
152
153 pair<map<int, CEPollDesc>::iterator, bool> res = m_mPolls.insert(make_pair(m_iIDSeed, CEPollDesc(m_iIDSeed, localid)));
154 if (!res.second) // Insertion failed (no memory?)
155 throw CUDTException(MJ_SETUP, MN_NONE);
156 if (pout)
157 *pout = &res.first->second;
158
159 return m_iIDSeed;
160 }
161
162 int srt::CEPoll::clear_usocks(int eid)
163 {
164 // This should remove all SRT sockets from given eid.
165 ScopedLock pg (m_EPollLock);
166
167 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
168 if (p == m_mPolls.end())
169 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
170
171 CEPollDesc& d = p->second;
172
173 d.clearAll();
174
175 return 0;
176 }
177
178
179 void srt::CEPoll::clear_ready_usocks(CEPollDesc& d, int direction)
180 {
181 if ((direction & ~SRT_EPOLL_EVENTTYPES) != 0)
182 {
183 // This is internal function, so simply report an IPE on incorrect usage.
184 LOGC(eilog.Error, log << "CEPoll::clear_ready_usocks: IPE, event flags exceed event types: " << direction);
185 return;
186 }
187 ScopedLock pg (m_EPollLock);
188
189 vector<SRTSOCKET> cleared;
190
191 CEPollDesc::enotice_t::iterator i = d.enotice_begin();
192 while (i != d.enotice_end())
193 {
194 IF_HEAVY_LOGGING(SRTSOCKET subsock = i->fd);
195 SRTSOCKET rs = d.clearEventSub(i++, direction);
196 // This function returns:
197 // - a valid socket - if there are no other subscription after 'direction' was cleared
198 // - SRT_INVALID_SOCK otherwise
199 // Valid sockets should be collected as sockets that no longer
200 // have a subscribed event should be deleted from subscriptions.
201 if (rs != SRT_INVALID_SOCK)
202 {
203 HLOGC(eilog.Debug, log << "CEPoll::clear_ready_usocks: @" << rs << " got all subscription cleared");
204 cleared.push_back(rs);
205 }
206 else
207 {
208 HLOGC(eilog.Debug, log << "CEPoll::clear_ready_usocks: @" << subsock << " is still subscribed");
209 }
210 }
211
212 for (size_t j = 0; j < cleared.size(); ++j)
213 d.removeSubscription(cleared[j]);
214 }
215
216 int srt::CEPoll::add_ssock(const int eid, const SYSSOCKET& s, const int* events)
217 {
218 ScopedLock pg(m_EPollLock);
219
220 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
221 if (p == m_mPolls.end())
222 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
223
224 #ifdef LINUX
225 epoll_event ev;
226 memset(&ev, 0, sizeof(epoll_event));
227
228 if (NULL == events)
229 ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
230 else
231 {
232 ev.events = 0;
233 if (*events & SRT_EPOLL_IN)
234 ev.events |= EPOLLIN;
235 if (*events & SRT_EPOLL_OUT)
236 ev.events |= EPOLLOUT;
237 if (*events & SRT_EPOLL_ERR)
238 ev.events |= EPOLLERR;
239 }
240
241 ev.data.fd = s;
242 if (::epoll_ctl(p->second.m_iLocalID, EPOLL_CTL_ADD, s, &ev) < 0)
243 throw CUDTException();
244 #elif defined(BSD) || TARGET_OS_MAC
245 struct kevent ke[2];
246 int num = 0;
247
248 if (NULL == events)
249 {
250 EV_SET(&ke[num++], s, EVFILT_READ, EV_ADD, 0, 0, NULL);
251 EV_SET(&ke[num++], s, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
252 }
253 else
254 {
255 if (*events & SRT_EPOLL_IN)
256 {
257 EV_SET(&ke[num++], s, EVFILT_READ, EV_ADD, 0, 0, NULL);
258 }
259 if (*events & SRT_EPOLL_OUT)
260 {
261 EV_SET(&ke[num++], s, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
262 }
263 }
264 if (kevent(p->second.m_iLocalID, ke, num, NULL, 0, NULL) < 0)
265 throw CUDTException();
266 #else
267
268 // fake use 'events' to prevent warning. Remove when implemented.
269 (void)events;
270 (void)s;
271
272 #ifdef _MSC_VER
273 // Microsoft Visual Studio doesn't support the #warning directive - nonstandard anyway.
274 // Use #pragma message with the same text.
275 // All other compilers should be ok :)
276 #pragma message("WARNING: Unsupported system for epoll. The epoll_add_ssock() API call won't work on this platform.")
277 #else
278 #warning "Unsupported system for epoll. The epoll_add_ssock() API call won't work on this platform."
279 #endif
280
281 #endif
282
283 p->second.m_sLocals.insert(s);
284
285 return 0;
286 }
287
288 int srt::CEPoll::remove_ssock(const int eid, const SYSSOCKET& s)
289 {
290 ScopedLock pg(m_EPollLock);
291
292 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
293 if (p == m_mPolls.end())
294 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
295
296 #ifdef LINUX
297 epoll_event ev; // ev is ignored, for compatibility with old Linux kernel only.
298 if (::epoll_ctl(p->second.m_iLocalID, EPOLL_CTL_DEL, s, &ev) < 0)
299 throw CUDTException();
300 #elif defined(BSD) || TARGET_OS_MAC
301 struct kevent ke;
302
303 //
304 // Since I don't know what was set before
305 // Just clear out both read and write
306 //
307 EV_SET(&ke, s, EVFILT_READ, EV_DELETE, 0, 0, NULL);
308 kevent(p->second.m_iLocalID, &ke, 1, NULL, 0, NULL);
309 EV_SET(&ke, s, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
310 kevent(p->second.m_iLocalID, &ke, 1, NULL, 0, NULL);
311 #endif
312
313 p->second.m_sLocals.erase(s);
314
315 return 0;
316 }
317
318 // Need this to atomically modify polled events (ex: remove write/keep read)
319 int srt::CEPoll::update_usock(const int eid, const SRTSOCKET& u, const int* events)
320 {
321 ScopedLock pg(m_EPollLock);
322 IF_HEAVY_LOGGING(ostringstream evd);
323
324 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
325 if (p == m_mPolls.end())
326 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
327
328 CEPollDesc& d = p->second;
329
330 int32_t evts = events ? *events : uint32_t(SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR);
331 bool edgeTriggered = evts & SRT_EPOLL_ET;
332 evts &= ~SRT_EPOLL_ET;
333
334 // et_evts = all events, if SRT_EPOLL_ET, or only those that are always ET otherwise.
335 int32_t et_evts = edgeTriggered ? evts : evts & SRT_EPOLL_ETONLY;
336 if (evts)
337 {
338 pair<CEPollDesc::ewatch_t::iterator, bool> iter_new = d.addWatch(u, evts, et_evts);
339 CEPollDesc::Wait& wait = iter_new.first->second;
340 if (!iter_new.second)
341 {
342 // The object exists. We only are certain about the `u`
343 // parameter, but others are probably unchanged. Change them
344 // forcefully and take out notices that are no longer valid.
345 const int removable = wait.watch & ~evts;
346 IF_HEAVY_LOGGING(PrintEpollEvent(evd, evts & (~wait.watch)));
347
348 // Check if there are any events that would be removed.
349 // If there are no removed events watched (for example, when
350 // only new events are being added to existing socket),
351 // there's nothing to remove, but might be something to update.
352 if (removable)
353 {
354 d.removeExcessEvents(wait, evts);
355 }
356
357 // Update the watch configuration, including edge
358 wait.watch = evts;
359 wait.edge = et_evts;
360
361 // Now it should look exactly like newly added
362 // and the state is also updated
363 HLOGC(ealog.Debug, log << "srt_epoll_update_usock: UPDATED E" << eid << " for @" << u << " +" << evd.str());
364 }
365 else
366 {
367 IF_HEAVY_LOGGING(PrintEpollEvent(evd, evts));
368 HLOGC(ealog.Debug, log << "srt_epoll_update_usock: ADDED E" << eid << " for @" << u << " " << evd.str());
369 }
370
371 const int newstate = wait.watch & wait.state;
372 if (newstate)
373 {
374 d.addEventNotice(wait, u, newstate);
375 }
376 }
377 else if (edgeTriggered)
378 {
379 LOGC(ealog.Error, log << "srt_epoll_update_usock: Specified only SRT_EPOLL_ET flag, but no event flag. Error.");
380 throw CUDTException(MJ_NOTSUP, MN_INVAL);
381 }
382 else
383 {
384 // Update with no events means to remove subscription
385 HLOGC(ealog.Debug, log << "srt_epoll_update_usock: REMOVED E" << eid << " socket @" << u);
386 d.removeSubscription(u);
387 }
388 return 0;
389 }
390
391 int srt::CEPoll::update_ssock(const int eid, const SYSSOCKET& s, const int* events)
392 {
393 ScopedLock pg(m_EPollLock);
394
395 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
396 if (p == m_mPolls.end())
397 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
398
399 #ifdef LINUX
400 epoll_event ev;
401 memset(&ev, 0, sizeof(epoll_event));
402
403 if (NULL == events)
404 ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
405 else
406 {
407 ev.events = 0;
408 if (*events & SRT_EPOLL_IN)
409 ev.events |= EPOLLIN;
410 if (*events & SRT_EPOLL_OUT)
411 ev.events |= EPOLLOUT;
412 if (*events & SRT_EPOLL_ERR)
413 ev.events |= EPOLLERR;
414 }
415
416 ev.data.fd = s;
417 if (::epoll_ctl(p->second.m_iLocalID, EPOLL_CTL_MOD, s, &ev) < 0)
418 throw CUDTException();
419 #elif defined(BSD) || TARGET_OS_MAC
420 struct kevent ke[2];
421 int num = 0;
422
423 //
424 // Since I don't know what was set before
425 // Just clear out both read and write
426 //
427 EV_SET(&ke[0], s, EVFILT_READ, EV_DELETE, 0, 0, NULL);
428 kevent(p->second.m_iLocalID, ke, 1, NULL, 0, NULL);
429 EV_SET(&ke[0], s, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
430 kevent(p->second.m_iLocalID, ke, 1, NULL, 0, NULL);
431 if (NULL == events)
432 {
433 EV_SET(&ke[num++], s, EVFILT_READ, EV_ADD, 0, 0, NULL);
434 EV_SET(&ke[num++], s, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
435 }
436 else
437 {
438 if (*events & SRT_EPOLL_IN)
439 {
440 EV_SET(&ke[num++], s, EVFILT_READ, EV_ADD, 0, 0, NULL);
441 }
442 if (*events & SRT_EPOLL_OUT)
443 {
444 EV_SET(&ke[num++], s, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
445 }
446 }
447 if (kevent(p->second.m_iLocalID, ke, num, NULL, 0, NULL) < 0)
448 throw CUDTException();
449 #else
450
451 // fake use 'events' to prevent warning. Remove when implemented.
452 (void)events;
453 (void)s;
454
455 #endif
456 // Assuming add is used if not inserted
457 // p->second.m_sLocals.insert(s);
458
459 return 0;
460 }
461
462 int srt::CEPoll::setflags(const int eid, int32_t flags)
463 {
464 ScopedLock pg(m_EPollLock);
465 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
466 if (p == m_mPolls.end())
467 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
468 CEPollDesc& ed = p->second;
469
470 int32_t oflags = ed.flags();
471
472 if (flags == -1)
473 return oflags;
474
475 if (flags == 0)
476 {
477 ed.clr_flags(~int32_t());
478 }
479 else
480 {
481 ed.set_flags(flags);
482 }
483
484 return oflags;
485 }
486
487 int srt::CEPoll::uwait(const int eid, SRT_EPOLL_EVENT* fdsSet, int fdsSize, int64_t msTimeOut)
488 {
489 // It is allowed to call this function with fdsSize == 0
490 // and therefore also NULL fdsSet. This will then only report
491 // the number of ready sockets, just without information which.
492 if (fdsSize < 0 || (fdsSize > 0 && !fdsSet))
493 throw CUDTException(MJ_NOTSUP, MN_INVAL);
494
495 steady_clock::time_point entertime = steady_clock::now();
496
497 while (true)
498 {
499 {
500 ScopedLock pg(m_EPollLock);
501 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
502 if (p == m_mPolls.end())
503 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
504 CEPollDesc& ed = p->second;
505
506 if (!ed.flags(SRT_EPOLL_ENABLE_EMPTY) && ed.watch_empty())
507 {
508 // Empty EID is not allowed, report error.
509 throw CUDTException(MJ_NOTSUP, MN_EEMPTY);
510 }
511
512 if (ed.flags(SRT_EPOLL_ENABLE_OUTPUTCHECK) && (fdsSet == NULL || fdsSize == 0))
513 {
514 // Empty EID is not allowed, report error.
515 throw CUDTException(MJ_NOTSUP, MN_INVAL);
516 }
517
518 if (!ed.m_sLocals.empty())
519 {
520 // XXX Add error log
521 // uwait should not be used with EIDs subscribed to system sockets
522 throw CUDTException(MJ_NOTSUP, MN_INVAL);
523 }
524
525 CEPollDesc::enotice_t::iterator i = ed.enotice_begin(), inext;
526 int pos = 0; // This is a list, so count it during iteration
527 for (inext = i ; i != ed.enotice_end() && pos < fdsSize ; ++pos, i = inext)
528 {
529 ++inext; // deletion-safe list loop
530
531 fdsSet[pos] = *i;
532 ed.checkEdge(i); // NOTE: potentially deletes `i`
533 }
534 if (pos) // pos is increased by 1 towards the last used position
535 return pos;
536 }
537
538 if ((msTimeOut >= 0) && (count_microseconds(srt::sync::steady_clock::now() - entertime) >= msTimeOut * int64_t(1000)))
539 break; // official wait does: throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
540
541 CGlobEvent::waitForEvent();
542 }
543
544 return 0;
545 }
546
547 int srt::CEPoll::wait(const int eid, set<SRTSOCKET>* readfds, set<SRTSOCKET>* writefds, int64_t msTimeOut, set<SYSSOCKET>* lrfds, set<SYSSOCKET>* lwfds)
548 {
549 // if all fields is NULL and waiting time is infinite, then this would be a deadlock
550 if (!readfds && !writefds && !lrfds && !lwfds && (msTimeOut < 0))
551 throw CUDTException(MJ_NOTSUP, MN_INVAL, 0);
552
553 // Clear these sets in case the app forget to do it.
554 if (readfds) readfds->clear();
555 if (writefds) writefds->clear();
556 if (lrfds) lrfds->clear();
557 if (lwfds) lwfds->clear();
558
559 int total = 0;
560
561 srt::sync::steady_clock::time_point entertime = srt::sync::steady_clock::now();
562 while (true)
563 {
564 {
565 ScopedLock epollock(m_EPollLock);
566
567 map<int, CEPollDesc>::iterator p = m_mPolls.find(eid);
568 if (p == m_mPolls.end())
569 {
570 LOGC(ealog.Error, log << "EID:" << eid << " INVALID.");
571 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
572 }
573
574 CEPollDesc& ed = p->second;
575
576 if (!ed.flags(SRT_EPOLL_ENABLE_EMPTY) && ed.watch_empty() && ed.m_sLocals.empty())
577 {
578 // Empty EID is not allowed, report error.
579 //throw CUDTException(MJ_NOTSUP, MN_INVAL);
580 LOGC(ealog.Error, log << "EID:" << eid << " no sockets to check, this would deadlock");
581 throw CUDTException(MJ_NOTSUP, MN_EEMPTY, 0);
582 }
583
584 if (ed.flags(SRT_EPOLL_ENABLE_OUTPUTCHECK))
585 {
586 // Empty report is not allowed, report error.
587 if (!ed.m_sLocals.empty() && (!lrfds || !lwfds))
588 throw CUDTException(MJ_NOTSUP, MN_INVAL);
589
590 if (!ed.watch_empty() && (!readfds || !writefds))
591 throw CUDTException(MJ_NOTSUP, MN_INVAL);
592 }
593
594 IF_HEAVY_LOGGING(int total_noticed = 0);
595 IF_HEAVY_LOGGING(ostringstream debug_sockets);
596 // Sockets with exceptions are returned to both read and write sets.
597 for (CEPollDesc::enotice_t::iterator it = ed.enotice_begin(), it_next = it; it != ed.enotice_end(); it = it_next)
598 {
599 ++it_next;
600 IF_HEAVY_LOGGING(++total_noticed);
601 if (readfds && ((it->events & SRT_EPOLL_IN) || (it->events & SRT_EPOLL_ERR)))
602 {
603 if (readfds->insert(it->fd).second)
604 ++total;
605 }
606
607 if (writefds && ((it->events & SRT_EPOLL_OUT) || (it->events & SRT_EPOLL_ERR)))
608 {
609 if (writefds->insert(it->fd).second)
610 ++total;
611 }
612
613 IF_HEAVY_LOGGING(debug_sockets << " " << it->fd << ":"
614 << IF_DIRNAME(it->events, SRT_EPOLL_IN, "R")
615 << IF_DIRNAME(it->events, SRT_EPOLL_OUT, "W")
616 << IF_DIRNAME(it->events, SRT_EPOLL_ERR, "E"));
617
618 if (ed.checkEdge(it)) // NOTE: potentially erases 'it'.
619 {
620 IF_HEAVY_LOGGING(debug_sockets << "!");
621 }
622 }
623
624 HLOGC(ealog.Debug, log << "CEPoll::wait: REPORTED " << total << "/" << total_noticed
625 << debug_sockets.str());
626
627 if ((lrfds || lwfds) && !ed.m_sLocals.empty())
628 {
629 #ifdef LINUX
630 const int max_events = ed.m_sLocals.size();
631 SRT_ASSERT(max_events > 0);
632 srt::FixedArray<epoll_event> ev(max_events);
633 int nfds = ::epoll_wait(ed.m_iLocalID, ev.data(), ev.size(), 0);
634
635 IF_HEAVY_LOGGING(const int prev_total = total);
636 for (int i = 0; i < nfds; ++ i)
637 {
638 if ((NULL != lrfds) && (ev[i].events & EPOLLIN))
639 {
640 lrfds->insert(ev[i].data.fd);
641 ++ total;
642 }
643 if ((NULL != lwfds) && (ev[i].events & EPOLLOUT))
644 {
645 lwfds->insert(ev[i].data.fd);
646 ++ total;
647 }
648 }
649 HLOGC(ealog.Debug, log << "CEPoll::wait: LINUX: picking up " << (total - prev_total) << " ready fds.");
650
651 #elif defined(BSD) || TARGET_OS_MAC
652 struct timespec tmout = {0, 0};
653 const int max_events = (int)ed.m_sLocals.size();
654 SRT_ASSERT(max_events > 0);
655 srt::FixedArray<struct kevent> ke(max_events);
656
657 int nfds = kevent(ed.m_iLocalID, NULL, 0, ke.data(), (int)ke.size(), &tmout);
658 IF_HEAVY_LOGGING(const int prev_total = total);
659
660 for (int i = 0; i < nfds; ++ i)
661 {
662 if ((NULL != lrfds) && (ke[i].filter == EVFILT_READ))
663 {
664 lrfds->insert((int)ke[i].ident);
665 ++ total;
666 }
667 if ((NULL != lwfds) && (ke[i].filter == EVFILT_WRITE))
668 {
669 lwfds->insert((int)ke[i].ident);
670 ++ total;
671 }
672 }
673
674 HLOGC(ealog.Debug, log << "CEPoll::wait: Darwin/BSD: picking up " << (total - prev_total) << " ready fds.");
675
676 #else
677 //currently "select" is used for all non-Linux platforms.
678 //faster approaches can be applied for specific systems in the future.
679
680 //"select" has a limitation on the number of sockets
681 int max_fd = 0;
682
683 fd_set rqreadfds;
684 fd_set rqwritefds;
685 FD_ZERO(&rqreadfds);
686 FD_ZERO(&rqwritefds);
687
688 for (set<SYSSOCKET>::const_iterator i = ed.m_sLocals.begin(); i != ed.m_sLocals.end(); ++ i)
689 {
690 if (lrfds)
691 FD_SET(*i, &rqreadfds);
692 if (lwfds)
693 FD_SET(*i, &rqwritefds);
694 if ((int)*i > max_fd)
695 max_fd = (int)*i;
696 }
697
698 IF_HEAVY_LOGGING(const int prev_total = total);
699 timeval tv;
700 tv.tv_sec = 0;
701 tv.tv_usec = 0;
702 if (::select(max_fd + 1, &rqreadfds, &rqwritefds, NULL, &tv) > 0)
703 {
704 for (set<SYSSOCKET>::const_iterator i = ed.m_sLocals.begin(); i != ed.m_sLocals.end(); ++ i)
705 {
706 if (lrfds && FD_ISSET(*i, &rqreadfds))
707 {
708 lrfds->insert(*i);
709 ++ total;
710 }
711 if (lwfds && FD_ISSET(*i, &rqwritefds))
712 {
713 lwfds->insert(*i);
714 ++ total;
715 }
716 }
717 }
718
719 HLOGC(ealog.Debug, log << "CEPoll::wait: select(otherSYS): picking up " << (total - prev_total) << " ready fds.");
720 #endif
721 }
722
723 } // END-LOCK: m_EPollLock
724
725 HLOGC(ealog.Debug, log << "CEPoll::wait: Total of " << total << " READY SOCKETS");
726
727 if (total > 0)
728 return total;
729
730 if ((msTimeOut >= 0) && (count_microseconds(srt::sync::steady_clock::now() - entertime) >= msTimeOut * int64_t(1000)))
731 {
732 HLOGC(ealog.Debug, log << "EID:" << eid << ": TIMEOUT.");
733 throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
734 }
735
736 const bool wait_signaled SRT_ATR_UNUSED = CGlobEvent::waitForEvent();
737 HLOGC(ealog.Debug, log << "CEPoll::wait: EVENT WAITING: "
738 << (wait_signaled ? "TRIGGERED" : "CHECKPOINT"));
739 }
740
741 return 0;
742 }
743
744 int srt::CEPoll::swait(CEPollDesc& d, map<SRTSOCKET, int>& st, int64_t msTimeOut, bool report_by_exception)
745 {
746 {
747 ScopedLock lg (m_EPollLock);
748 if (!d.flags(SRT_EPOLL_ENABLE_EMPTY) && d.watch_empty() && msTimeOut < 0)
749 {
750 // no socket is being monitored, this may be a deadlock
751 LOGC(ealog.Error, log << "EID:" << d.m_iID << " no sockets to check, this would deadlock");
752 if (report_by_exception)
753 throw CUDTException(MJ_NOTSUP, MN_EEMPTY, 0);
754 return -1;
755 }
756 }
757
758 st.clear();
759
760 steady_clock::time_point entertime = steady_clock::now();
761 while (true)
762 {
763 {
764 // Not extracting separately because this function is
765 // for internal use only and we state that the eid could
766 // not be deleted or changed the target CEPollDesc in the
767 // meantime.
768
769 // Here we only prevent the pollset be updated simultaneously
770 // with unstable reading.
771 ScopedLock lg (m_EPollLock);
772
773 if (!d.flags(SRT_EPOLL_ENABLE_EMPTY) && d.watch_empty())
774 {
775 // Empty EID is not allowed, report error.
776 throw CUDTException(MJ_NOTSUP, MN_EEMPTY);
777 }
778
779 if (!d.m_sLocals.empty())
780 {
781 // XXX Add error log
782 // uwait should not be used with EIDs subscribed to system sockets
783 throw CUDTException(MJ_NOTSUP, MN_INVAL);
784 }
785
786 bool empty = d.enotice_empty();
787
788 if (!empty || msTimeOut == 0)
789 {
790 IF_HEAVY_LOGGING(ostringstream singles);
791 // If msTimeOut == 0, it means that we need the information
792 // immediately, we don't want to wait. Therefore in this case
793 // report also when none is ready.
794 int total = 0; // This is a list, so count it during iteration
795 CEPollDesc::enotice_t::iterator i = d.enotice_begin();
796 while (i != d.enotice_end())
797 {
798 ++total;
799 st[i->fd] = i->events;
800 IF_HEAVY_LOGGING(singles << "@" << i->fd << ":");
801 IF_HEAVY_LOGGING(PrintEpollEvent(singles, i->events, i->parent->edgeOnly()));
802 const bool edged SRT_ATR_UNUSED = d.checkEdge(i++); // NOTE: potentially deletes `i`
803 IF_HEAVY_LOGGING(singles << (edged ? "<^> " : " "));
804 }
805
806 // Logging into 'singles' because it notifies as to whether
807 // the edge-triggered event has been cleared
808 HLOGC(ealog.Debug, log << "E" << d.m_iID << " rdy=" << total << ": "
809 << singles.str()
810 << " TRACKED: " << d.DisplayEpollWatch());
811 return total;
812 }
813 // Don't report any updates because this check happens
814 // extremely often.
815 }
816
817 if ((msTimeOut >= 0) && ((steady_clock::now() - entertime) >= microseconds_from(msTimeOut * int64_t(1000))))
818 {
819 HLOGC(ealog.Debug, log << "EID:" << d.m_iID << ": TIMEOUT.");
820 if (report_by_exception)
821 throw CUDTException(MJ_AGAIN, MN_XMTIMEOUT, 0);
822 return 0; // meaning "none is ready"
823 }
824
825 CGlobEvent::waitForEvent();
826 }
827
828 return 0;
829 }
830
831 bool srt::CEPoll::empty(const CEPollDesc& d) const
832 {
833 ScopedLock lg (m_EPollLock);
834 return d.watch_empty();
835 }
836
837 int srt::CEPoll::release(const int eid)
838 {
839 ScopedLock pg(m_EPollLock);
840
841 map<int, CEPollDesc>::iterator i = m_mPolls.find(eid);
842 if (i == m_mPolls.end())
843 throw CUDTException(MJ_NOTSUP, MN_EIDINVAL);
844
845 #ifdef LINUX
846 // release local/system epoll descriptor
847 ::close(i->second.m_iLocalID);
848 #elif defined(BSD) || TARGET_OS_MAC
849 ::close(i->second.m_iLocalID);
850 #endif
851
852 m_mPolls.erase(i);
853
854 return 0;
855 }
856
857
858 int srt::CEPoll::update_events(const SRTSOCKET& uid, std::set<int>& eids, const int events, const bool enable)
859 {
860 // As event flags no longer contain only event types, check now.
861 if ((events & ~SRT_EPOLL_EVENTTYPES) != 0)
862 {
863 LOGC(eilog.Fatal, log << "epoll/update: IPE: 'events' parameter shall not contain special flags!");
864 return -1; // still, ignored.
865 }
866
867 int nupdated = 0;
868 vector<int> lost;
869
870 IF_HEAVY_LOGGING(ostringstream debug);
871 IF_HEAVY_LOGGING(debug << "epoll/update: @" << uid << " " << (enable ? "+" : "-"));
872 IF_HEAVY_LOGGING(PrintEpollEvent(debug, events));
873
874 ScopedLock pg (m_EPollLock);
875 for (set<int>::iterator i = eids.begin(); i != eids.end(); ++ i)
876 {
877 map<int, CEPollDesc>::iterator p = m_mPolls.find(*i);
878 if (p == m_mPolls.end())
879 {
880 HLOGC(eilog.Note, log << "epoll/update: E" << *i << " was deleted in the meantime");
881 // EID invalid, though still present in the socket's subscriber list
882 // (dangling in the socket). Postpone to fix the subscruption and continue.
883 lost.push_back(*i);
884 continue;
885 }
886
887 CEPollDesc& ed = p->second;
888
889 // Check if this EID is subscribed for this socket.
890 CEPollDesc::Wait* pwait = ed.watch_find(uid);
891 if (!pwait)
892 {
893 // As this is mapped in the socket's data, it should be impossible.
894 LOGC(eilog.Error, log << "epoll/update: IPE: update struck E"
895 << (*i) << " which is NOT SUBSCRIBED to @" << uid);
896 continue;
897 }
898
899 IF_HEAVY_LOGGING(string tracking = " TRACKING: " + ed.DisplayEpollWatch());
900 // compute new states
901
902 // New state to be set into the permanent state
903 const int newstate = enable ? pwait->state | events // SET event bits if enable
904 : pwait->state & (~events); // CLEAR event bits
905
906 // compute states changes!
907 int changes = pwait->state ^ newstate; // oldState XOR newState
908 if (!changes)
909 {
910 HLOGC(eilog.Debug, log << debug.str() << ": E" << (*i)
911 << tracking << " NOT updated: no changes");
912 continue; // no changes!
913 }
914 // assign new state
915 pwait->state = newstate;
916 // filter change relating what is watching
917 changes &= pwait->watch;
918 if (!changes)
919 {
920 HLOGC(eilog.Debug, log << debug.str() << ": E" << (*i)
921 << tracking << " NOT updated: not subscribed");
922 continue; // no change watching
923 }
924 // set events changes!
925
926 // This function will update the notice object associated with
927 // the given events, that is:
928 // - if enable, it will set event flags, possibly in a new notice object
929 // - if !enable, it will clear event flags, possibly remove notice if resulted in 0
930 ed.updateEventNotice(*pwait, uid, events, enable);
931 ++nupdated;
932
933 HLOGC(eilog.Debug, log << debug.str() << ": E" << (*i)
934 << " TRACKING: " << ed.DisplayEpollWatch());
935 }
936
937 for (vector<int>::iterator i = lost.begin(); i != lost.end(); ++ i)
938 eids.erase(*i);
939
940 return nupdated;
941 }
942
943 // Debug use only.
944 #if ENABLE_HEAVY_LOGGING
945 namespace srt
946 {
947
948 string DisplayEpollResults(const std::map<SRTSOCKET, int>& sockset)
949 {
950 typedef map<SRTSOCKET, int> fmap_t;
951 ostringstream os;
952 for (fmap_t::const_iterator i = sockset.begin(); i != sockset.end(); ++i)
953 {
954 os << "@" << i->first << ":";
955 PrintEpollEvent(os, i->second);
956 os << " ";
957 }
958
959 return os.str();
960 }
961
962 string CEPollDesc::DisplayEpollWatch()
963 {
964 ostringstream os;
965 for (ewatch_t::const_iterator i = m_USockWatchState.begin(); i != m_USockWatchState.end(); ++i)
966 {
967 os << "@" << i->first << ":";
968 PrintEpollEvent(os, i->second.watch, i->second.edge);
969 os << " ";
970 }
971
972 return os.str();
973 }
974
975 } // namespace srt
976
977 #endif