rogue
Loading...
Searching...
No Matches
Controller.cpp
Go to the documentation of this file.
1
18
19#include <inttypes.h>
20#include <sys/time.h>
21#include <unistd.h>
22
23#include <cmath>
24#include <cstdlib>
25#include <cstring>
26#include <map>
27#include <memory>
28#include <utility>
29#include <vector>
30
31#include "rogue/GeneralError.h"
32#include "rogue/GilRelease.h"
33#include "rogue/Helpers.h"
34#include "rogue/Logging.h"
35#include "rogue/ScopedGil.h"
42
43namespace rpr = rogue::protocols::rssi;
45
47rpr::ControllerPtr rpr::Controller::create(uint32_t segSize,
48 rpr::TransportPtr tran,
49 rpr::ApplicationPtr app,
50 bool server) {
51 rpr::ControllerPtr r = std::make_shared<rpr::Controller>(segSize, tran, app, server);
52 return (r);
53}
54
56rpr::Controller::Controller(uint32_t segSize, rpr::TransportPtr tran, rpr::ApplicationPtr app, bool server) {
57 app_ = app;
58 tran_ = tran;
59 server_ = server;
60
61 locTryPeriod_ = 100;
62
63 // Busy after two entries
64 appQueue_.setThold(2);
65
66 dropCount_ = 0;
67 nextSeqRx_ = 0;
68 lastAckRx_ = 0;
69 locBusy_ = false;
70 remBusy_ = false;
71
72 lastSeqRx_ = 0;
73 ackSeqRx_ = 0;
74
75 state_ = StClosed;
76 gettimeofday(&stTime_, NULL);
77 downCount_ = 0;
78 retranCount_ = 0;
79
80 txListCount_ = 0;
81 lastAckTx_ = 0;
82 locSequence_ = 100;
83 gettimeofday(&txTime_, NULL);
84
85 locMaxBuffers_ = 32; // MAX_NUM_OUTS_SEG_G in FW
86 locMaxSegment_ = segSize;
87 locCumAckTout_ = 5; // ACK_TOUT_G in FW, 5mS
88 locRetranTout_ = 20; // RETRANS_TOUT_G in FW, 2hmS
89 locNullTout_ = 1000; // NULL_TOUT_G in FW, 1S
90 locMaxRetran_ = 15; // MAX_RETRANS_CNT_G in FW
91 locMaxCumAck_ = 2; // MAX_CUM_ACK_CNT_G in FW
92
93 curMaxBuffers_ = 32; // MAX_NUM_OUTS_SEG_G in FW
94 curMaxSegment_ = segSize;
95 curCumAckTout_ = 5; // ACK_TOUT_G in FW, 5mS
96 curRetranTout_ = 20; // RETRANS_TOUT_G in FW, 2hmS
97 curNullTout_ = 1000; // NULL_TOUT_G in FW, 1S
98 curMaxRetran_ = 15; // MAX_RETRANS_CNT_G in FW
99 curMaxCumAck_ = 2; // MAX_CUM_ACK_CNT_G in FW
100
101 locConnId_ = 0x12345678;
102 remConnId_ = 0;
103
104 convTime(tryPeriodD1_, locTryPeriod_);
105 convTime(tryPeriodD4_, locTryPeriod_ / 4);
106 convTime(retranToutD1_, curRetranTout_);
107 convTime(nullToutD3_, curNullTout_ / 3);
108 convTime(cumAckToutD1_, curCumAckTout_);
109 convTime(cumAckToutD2_, curCumAckTout_ / 2);
110
111 memset(&zeroTme_, 0, sizeof(struct timeval));
112
113 rogue::defaultTimeout(timeout_);
114
115 locBusyCnt_ = 0;
116 remBusyCnt_ = 0;
117
118 log_ = rogue::Logging::create("rssi.controller");
119
120 thread_ = NULL;
121}
122
124rpr::Controller::~Controller() {
125 stop();
126}
127
129void rpr::Controller::stopQueue() {
130 appQueue_.stop();
131}
132
134void rpr::Controller::stop() {
135 if (thread_ != NULL) {
136 rogue::GilRelease noGil;
137 threadEn_ = false;
138 thread_->join();
139 delete thread_;
140 thread_ = NULL;
141 state_ = StClosed;
142 }
143}
144
146void rpr::Controller::start() {
147 if (thread_ == NULL) {
148 state_ = StClosed;
149 threadEn_ = true;
150 thread_ = new std::thread(&rpr::Controller::runThread, this);
151
152 // Set a thread name
153#ifndef __MACH__
154 pthread_setname_np(thread_->native_handle(), "RssiControler");
155#endif
156 }
157}
158
160ris::FramePtr rpr::Controller::reqFrame(uint32_t size) {
161 ris::FramePtr frame;
162 ris::BufferPtr buffer;
163 uint32_t nSize;
164
165 // Request only single buffer frames.
166 // Frame size returned is never greater than remote max size
167 // or local segment size
168 nSize = size + rpr::Header::HeaderSize;
169 if (nSize > curMaxSegment_ && curMaxSegment_ > 0) nSize = curMaxSegment_;
170 if (nSize > locMaxSegment_) nSize = locMaxSegment_;
171
172 // Forward frame request to transport slave
173 frame = tran_->reqFrame(nSize, false);
174 buffer = *(frame->beginBuffer());
175
176 // Make sure there is enough room the buffer for our header
177 if (buffer->getAvailable() < rpr::Header::HeaderSize)
178 throw(rogue::GeneralError::create("rssi::Controller::reqFrame",
179 "Buffer size %" PRIu32 " is less than min header size %" PRIu32,
180 buffer->getAvailable(),
181 rpr::Header::HeaderSize));
182
183 // Update buffer to include our header space.
184 buffer->adjustHeader(rpr::Header::HeaderSize);
185
186 // Recreate frame to ensure outbound only has a single buffer
187 frame = ris::Frame::create();
188 frame->appendBuffer(buffer);
189
190 // Return frame
191 return (frame);
192}
193
195void rpr::Controller::transportRx(ris::FramePtr frame) {
196 std::map<uint8_t, rpr::HeaderPtr>::iterator it;
197
198 rpr::HeaderPtr head = rpr::Header::create(frame);
199
200 rogue::GilRelease noGil;
201 ris::FrameLockPtr flock = frame->lock();
202
203 if (frame->getError() || frame->isEmpty() || !head->verify()) {
204 log_->warning("Dumping bad frame state=%" PRIu32 " server=%d", state_, server_);
205 dropCount_++;
206 return;
207 }
208
209 log_->debug("RX frame: state=%" PRIu32 " server=%d size=%" PRIu32 " syn=%d ack=%d"
210 " nul=%d, bsy=%d, rst=%d, ack#=%" PRIu8 " seq=%" PRIu8 ", nxt=%" PRIu8,
211 state_,
212 server_,
213 frame->getPayload(),
214 head->syn,
215 head->ack,
216 head->nul,
217 head->busy,
218 head->rst,
219 head->acknowledge,
220 head->sequence,
221 nextSeqRx_);
222
223 // Ack set
224 if (head->ack && (head->acknowledge != lastAckRx_)) {
225 // Moved out under txMtx_, released after the lock drops. See releaseHeaders().
226 std::vector<rpr::HeaderPtr> acked;
227
228 {
229 std::unique_lock<std::mutex> lock(txMtx_);
230
231 // Sequences in (lastAckRx_, locSequence_) have been sent and not yet
232 // acknowledged. Only an acknowledgement inside that window is valid,
233 // computed modulo 256. A stale, reordered or corrupted ack outside it
234 // would otherwise walk txList_ past the outstanding frames, dropping
235 // them from retransmission without ever being acknowledged.
236 uint8_t pending = static_cast<uint8_t>(locSequence_ - lastAckRx_ - 1);
237 uint8_t delta = static_cast<uint8_t>(head->acknowledge - lastAckRx_);
238
239 if (delta <= pending) {
240 do {
241 acked.push_back(std::move(txList_[++lastAckRx_]));
242 if (txListCount_ != 0) txListCount_--;
243 } while (lastAckRx_ != head->acknowledge);
244 } else {
245 log_->warning("Ignoring out of window ack=%" PRIu8 " lastAckRx=%" PRIu8 " locSeq=%" PRIu8 " server=%d",
246 head->acknowledge,
247 lastAckRx_,
248 locSequence_,
249 server_);
250 }
251 }
252
253 releaseHeaders(acked);
254 }
255
256 // Check for busy state transition
257 if (!remBusy_ && head->busy) remBusyCnt_++;
258
259 // Update busy bit
260 remBusy_ = head->busy;
261
262 // Reset
263 if (head->rst) {
264 if (state_ == StOpen || state_ == StWaitSyn) {
265 stQueue_.push(head);
266 }
267
268 // Syn frame goes to state machine if state = open
269 // or we are waiting for ack replay
270 } else if (head->syn) {
271 if (state_ == StOpen || state_ == StWaitSyn) {
272 lastSeqRx_ = head->sequence;
273 nextSeqRx_ = lastSeqRx_ + 1;
274 stQueue_.push(head);
275 }
276
277 // Data or NULL in the correct sequence go to application
278 } else if (state_ == StOpen && (head->nul || frame->getPayload() > rpr::Header::HeaderSize)) {
279 if (head->sequence == nextSeqRx_) {
280 // log_->warning("Data or NULL in the correct sequence go to application: nextSeqRx_=0x%" PRIx8,
281 // nextSeqRx_);
282
283 lastSeqRx_ = nextSeqRx_;
284 nextSeqRx_ = nextSeqRx_ + 1;
285 appQueue_.push(head);
286
287 // There are elements in ooo (out-of-order) queue
288 if (!oooQueue_.empty()) {
289 // First remove received sequence number from queue to avoid duplicates
290 if ((it = oooQueue_.find(head->sequence)) != oooQueue_.end()) {
291 log_->warning("Removed duplicate frame. server=%d, head->sequence=%" PRIu8
292 ", next sequence=%" PRIu8,
293 server_,
294 head->sequence,
295 nextSeqRx_);
296 dropCount_++;
297 oooQueue_.erase(it);
298 }
299
300 // Get next entries from ooo (out-of-order) queue if they exist
301 // This works because max outstanding will never be the full range of ids
302 // otherwise this could be stale data from previous ids
303 while ((it = oooQueue_.find(nextSeqRx_)) != oooQueue_.end()) {
304 lastSeqRx_ = nextSeqRx_;
305 nextSeqRx_ = nextSeqRx_ + 1;
306
307 appQueue_.push(it->second);
308 log_->info("Using frame from ooo queue. server=%d, head->sequence=%" PRIu8,
309 server_,
310 (it->second)->sequence);
311 oooQueue_.erase(it);
312 }
313 }
314
315 // Notify after the last sequence update
316 stCond_.notify_all();
317
318 // Check if received frame is already in out of order queue
319 } else if ((it = oooQueue_.find(head->sequence)) != oooQueue_.end()) {
320 log_->warning("Dropped duplicate frame. server=%d, head->sequence=%" PRIu8
321 ", next sequence=%" PRIu8,
322 server_,
323 head->sequence,
324 nextSeqRx_);
325 dropCount_++;
326
327 // Add to out of order queue in case things arrive out of order
328 // Make sure received sequence is in window. There may be a better way
329 // to do this while handling the 8 bit rollover
330 } else {
331 uint8_t x = nextSeqRx_;
332 uint8_t windowEnd = (nextSeqRx_ + curMaxBuffers_ + 1);
333
334 while (++x != windowEnd) {
335 if (head->sequence == x) {
336 oooQueue_.insert(std::make_pair(head->sequence, head));
337 log_->info("Adding frame to ooo queue. server=%d, head->sequence=%" PRIu8
338 ", nextSeqRx_=%" PRIu8 ", windowsEnd=%" PRIu8,
339 server_,
340 head->sequence,
341 nextSeqRx_,
342 windowEnd);
343 break;
344 }
345 }
346
347 if (x == windowEnd) {
348 log_->warning("Dropping out of window frame. server=%d, head->sequence=%" PRIu8
349 ", nextSeqRx_=%" PRIu8 ", windowsEnd=%" PRIu8,
350 server_,
351 head->sequence,
352 nextSeqRx_,
353 windowEnd);
354 dropCount_++;
355 }
356 }
357 }
358}
359
361// Called by application class thread
362ris::FramePtr rpr::Controller::applicationTx() {
363 ris::FramePtr frame;
364 rpr::HeaderPtr head;
365
366 rogue::GilRelease noGil;
367
368 do {
369 if ((head = appQueue_.pop()) == NULL) return (frame);
370 stCond_.notify_all();
371
372 frame = head->getFrame();
373 ris::FrameLockPtr flock = frame->lock();
374
375 ackSeqRx_ = head->sequence;
376
377 // Drop NULL frames
378 if (head->nul) {
379 head.reset();
380 frame.reset();
381 } else {
382 (*(frame->beginBuffer()))->adjustHeader(rpr::Header::HeaderSize);
383 }
384 } while (!frame);
385
386 return (frame);
387}
388
390void rpr::Controller::applicationRx(ris::FramePtr frame) {
391 ris::FramePtr tranFrame;
392 struct timeval startTime;
393
394 gettimeofday(&startTime, NULL);
395
396 rogue::GilRelease noGil;
397 ris::FrameLockPtr flock = frame->lock();
398
399 if (frame->isEmpty()) {
400 log_->warning("Dropping empty application frame");
401 return;
402 }
403
404 if (frame->getError()) {
405 log_->warning("Dropping errored application frame");
406 return;
407 }
408
409 // Adjust header in first buffer
410 (*(frame->beginBuffer()))->adjustHeader(-rpr::Header::HeaderSize);
411
412 // Map to RSSI
413 rpr::HeaderPtr head = rpr::Header::create(frame);
414 head->ack = true;
415 flock->unlock();
416
417 // Connection is closed
418 if (state_ != StOpen) {
419 return;
420 }
421
422 // Wait while busy either by flow control or buffer starvation
423 while (txListCount_ >= curMaxBuffers_) {
424 usleep(10);
425 if (timePassed(startTime, timeout_)) {
426 gettimeofday(&startTime, NULL);
427 log_->critical("Controller::applicationRx: Timeout waiting for outbound queue after %" PRIu32 ".%" PRIu32
428 " seconds! May be caused by outbound backpressure.",
429 timeout_.tv_sec,
430 timeout_.tv_usec);
431 }
432 }
433
434 // Transmit
435 transportTx(head, true, false);
436 stCond_.notify_all();
437}
438
440bool rpr::Controller::getOpen() {
441 return (state_ == StOpen);
442}
443
445uint32_t rpr::Controller::getDownCount() {
446 return (downCount_);
447}
448
450uint32_t rpr::Controller::getDropCount() {
451 return (dropCount_);
452}
453
455uint32_t rpr::Controller::getRetranCount() {
456 return (retranCount_);
457}
458
460bool rpr::Controller::getLocBusy() {
461 bool queueBusy = appQueue_.busy();
462 if (!locBusy_ && queueBusy) locBusyCnt_++;
463 locBusy_ = queueBusy;
464 return (locBusy_);
465}
466
468uint32_t rpr::Controller::getLocBusyCnt() {
469 return (locBusyCnt_);
470}
471
473bool rpr::Controller::getRemBusy() {
474 return (remBusy_);
475}
476
478uint32_t rpr::Controller::getRemBusyCnt() {
479 return (remBusyCnt_);
480}
481
482void rpr::Controller::setLocTryPeriod(uint32_t val) {
483 if (val == 0)
484 throw rogue::GeneralError::create("Rssi::Controller::setLocTryPeriod",
485 "Invalid LocTryPeriod Value = %" PRIu32,
486 val);
487 locTryPeriod_ = val;
488 convTime(tryPeriodD1_, locTryPeriod_);
489 convTime(tryPeriodD4_, locTryPeriod_ / 4);
490}
491
492uint32_t rpr::Controller::getLocTryPeriod() {
493 return locTryPeriod_;
494}
495
496void rpr::Controller::setLocMaxBuffers(uint8_t val) {
497 if (val == 0)
498 throw rogue::GeneralError::create("Rssi::Controller::setLocMaxBuffers",
499 "Invalid LocMaxBuffers Value = %" PRIu8,
500 val);
501
502 locMaxBuffers_ = val;
503}
504
505uint8_t rpr::Controller::getLocMaxBuffers() {
506 return locMaxBuffers_;
507}
508
509void rpr::Controller::setLocMaxSegment(uint16_t val) {
510 if (val == 0)
511 throw rogue::GeneralError::create("Rssi::Controller::setLocMaxSegment",
512 "Invalid LocMaxSegment Value = %" PRIu16,
513 val);
514 locMaxSegment_ = val;
515}
516
517uint16_t rpr::Controller::getLocMaxSegment() {
518 return locMaxSegment_;
519}
520
521void rpr::Controller::setLocCumAckTout(uint16_t val) {
522 if (val == 0)
523 throw rogue::GeneralError::create("Rssi::Controller::setLocCumAckTout",
524 "Invalid LocCumAckTout Value = %" PRIu16,
525 val);
526 locCumAckTout_ = val;
527}
528
529uint16_t rpr::Controller::getLocCumAckTout() {
530 return locCumAckTout_;
531}
532
533void rpr::Controller::setLocRetranTout(uint16_t val) {
534 if (val == 0)
535 throw rogue::GeneralError::create("Rssi::Controller::setLocRetranTout",
536 "Invalid LocRetranTout Value = %" PRIu16,
537 val);
538 locRetranTout_ = val;
539}
540
541uint16_t rpr::Controller::getLocRetranTout() {
542 return locRetranTout_;
543}
544
545void rpr::Controller::setLocNullTout(uint16_t val) {
546 if (val == 0)
547 throw rogue::GeneralError::create("Rssi::Controller::setLocNullTout",
548 "Invalid LocNullTout Value = %" PRIu16,
549 val);
550 locNullTout_ = val;
551}
552
553uint16_t rpr::Controller::getLocNullTout() {
554 return locNullTout_;
555}
556
557void rpr::Controller::setLocMaxRetran(uint8_t val) {
558 if (val == 0)
559 throw rogue::GeneralError::create("Rssi::Controller::setLocMaxRetran",
560 "Invalid LocMaxRetran Value = %" PRIu8,
561 val);
562 locMaxRetran_ = val;
563}
564
565uint8_t rpr::Controller::getLocMaxRetran() {
566 return locMaxRetran_;
567}
568
569void rpr::Controller::setLocMaxCumAck(uint8_t val) {
570 if (val == 0)
571 throw rogue::GeneralError::create("Rssi::Controller::setLocMaxAck", "Invalid LocMaxAck Value = %" PRIu8, val);
572 locMaxCumAck_ = val;
573}
574
575uint8_t rpr::Controller::getLocMaxCumAck() {
576 return locMaxCumAck_;
577}
578
579uint8_t rpr::Controller::curMaxBuffers() {
580 return curMaxBuffers_;
581}
582
583uint16_t rpr::Controller::curMaxSegment() {
584 return curMaxSegment_;
585}
586
587uint16_t rpr::Controller::curCumAckTout() {
588 return curCumAckTout_;
589}
590
591uint16_t rpr::Controller::curRetranTout() {
592 return curRetranTout_;
593}
594
595uint16_t rpr::Controller::curNullTout() {
596 return curNullTout_;
597}
598
599uint8_t rpr::Controller::curMaxRetran() {
600 return curMaxRetran_;
601}
602
603uint8_t rpr::Controller::curMaxCumAck() {
604 return curMaxCumAck_;
605}
606
607void rpr::Controller::resetCounters() {
608 dropCount_ = 0;
609 downCount_ = 0;
610 retranCount_ = 0;
611 locBusyCnt_ = 0;
612 remBusyCnt_ = 0;
613}
614
615// Release retransmit headers, taking the GIL when Python is running.
616//
617// Each Header owns the frame applicationRx() wrapped uncopied, so a Python-owned
618// frame carries a Boost.Python deleter that calls Py_DECREF without the GIL,
619// which is fatal on a transport thread. Call with txMtx_ released so the GIL is
620// never acquired while holding it.
621void rpr::Controller::releaseHeaders(std::vector<rpr::HeaderPtr>& heads) {
622#ifndef NO_PYTHON
623 if (Py_IsInitialized()) {
625 heads.clear();
626 return;
627 }
628#endif
629 heads.clear();
630}
631
632// Method to transit a frame with proper updates
633void rpr::Controller::transportTx(rpr::HeaderPtr head, bool seqUpdate, bool txReset) {
634 std::vector<rpr::HeaderPtr> acked;
635 std::unique_lock<std::mutex> lock(txMtx_);
636
637 head->sequence = locSequence_;
638
639 // Update sequence numbers
640 if (seqUpdate) {
641 txList_[locSequence_] = head;
642 txListCount_++;
643 locSequence_++;
644 }
645
646 // Reset tx list. Headers are released after txMtx_ drops, via releaseHeaders().
647 if (txReset) {
648 for (uint32_t x = 0; x < 256; x++) {
649 if (txList_[x]) acked.push_back(std::move(txList_[x]));
650 }
651 txListCount_ = 0;
652 }
653
654 if (getLocBusy()) {
655 head->acknowledge = lastAckTx_;
656 head->busy = true;
657 } else {
658 head->acknowledge = ackSeqRx_;
659 lastAckTx_ = ackSeqRx_;
660 head->busy = false;
661 }
662
663 // Track last tx time
664 gettimeofday(&txTime_, NULL);
665
666 ris::FrameLockPtr flock = head->getFrame()->lock();
667 head->update();
668
669 log_->log(rogue::Logging::Debug,
670 "TX frame: state=%" PRIu32 " server=%d size=%" PRIu32 " syn=%d ack=%d nul=%d"
671 ", bsy=%d, rst=%d, ack#=%" PRIu8 ", seq=%" PRIu8 ", recount=%" PRIu32 ", ptr=%p",
672 state_,
673 server_,
674 head->getFrame()->getPayload(),
675 head->syn,
676 head->ack,
677 head->nul,
678 head->busy,
679 head->rst,
680 head->acknowledge,
681 head->sequence,
682 retranCount_.load(),
683 static_cast<void*>(head->getFrame().get()));
684
685 flock->unlock();
686 lock.unlock();
687
688 // Release any headers the txReset above moved out, now that txMtx_ is dropped.
689 releaseHeaders(acked);
690
691 // Send frame
692 tran_->sendFrame(head->getFrame());
693}
694
695// Method to retransmit a frame
696int8_t rpr::Controller::retransmit(uint8_t id) {
697 std::unique_lock<std::mutex> lock(txMtx_);
698
699 rpr::HeaderPtr head = txList_[id];
700 if (head == NULL) return 0;
701
702 // retransmit timer has not expired
703 if (!timePassed(head->getTime(), retranToutD1_)) return 0;
704
705 // max retransmission count has been reached
706 if (head->count() >= curMaxRetran_) return -1;
707
708 retranCount_++;
709
710 if (getLocBusy()) {
711 head->acknowledge = lastAckTx_;
712 head->busy = true;
713 } else {
714 head->acknowledge = ackSeqRx_;
715 lastAckTx_ = ackSeqRx_;
716 head->busy = false;
717 }
718
719 // Track last tx time
720 gettimeofday(&txTime_, NULL);
721
722 log_->log(rogue::Logging::Warning,
723 "Retran frame: state=%" PRIu32 " server=%d size=%" PRIu32 " syn=%d ack=%d"
724 " nul=%d, rst=%d, ack#=%" PRIu8 ", seq=%" PRIu8 ", recount=%" PRIu32 ", ptr=%p",
725 state_,
726 server_,
727 head->getFrame()->getPayload(),
728 head->syn,
729 head->ack,
730 head->nul,
731 head->rst,
732 head->acknowledge,
733 head->sequence,
734 retranCount_.load(),
735 static_cast<void*>(head->getFrame().get()));
736
737 ris::FrameLockPtr flock = head->getFrame()->lock();
738 head->update();
739 flock->unlock();
740 lock.unlock();
741
742 // Send frame
743 tran_->sendFrame(head->getFrame());
744 return 1;
745}
746
748void rpr::Controller::convTime(struct timeval& tme, uint32_t rssiTime) {
749 float units = std::pow(10, -TimeoutUnit);
750 float value = units * static_cast<float>(rssiTime);
751
752 uint32_t usec = static_cast<uint32_t>(value / 1e-6);
753
754 div_t divResult = div(usec, 1000000);
755 tme.tv_sec = divResult.quot;
756 tme.tv_usec = divResult.rem;
757}
758
760bool rpr::Controller::timePassed(struct timeval& lastTime, struct timeval& tme) {
761 struct timeval endTime;
762 struct timeval currTime;
763
764 gettimeofday(&currTime, NULL);
765 timeradd(&lastTime, &tme, &endTime);
766 return (timercmp(&currTime, &endTime, >=));
767}
768
770void rpr::Controller::runThread() {
771 struct timeval wait;
772
773 log_->logThreadId();
774
775 wait = zeroTme_;
776
777 while (threadEn_) {
778 // Lock context
779 if (wait.tv_sec != 0 || wait.tv_usec != 0) {
780 // Wait on condition or timeout
781 std::unique_lock<std::mutex> lock(stMtx_);
782
783 // Adjustable wait
784 stCond_.wait_for(lock, std::chrono::microseconds(wait.tv_usec) + std::chrono::seconds(wait.tv_sec));
785 }
786
787 switch (state_) {
788 case StClosed:
789 case StWaitSyn:
790 wait = stateClosedWait();
791 break;
792
793 case StSendSynAck:
794 wait = stateSendSynAck();
795 break;
796
797 case StSendSeqAck:
798 wait = stateSendSeqAck();
799 break;
800
801 case StOpen:
802 wait = stateOpen();
803 break;
804
805 case StError:
806 wait = stateError();
807 break;
808 default:
809 break;
810 }
811 }
812
813 // Send reset on exit
814 stateError();
815}
816
818struct timeval& rpr::Controller::stateClosedWait() {
819 rpr::HeaderPtr head;
820
821 // got syn or reset
822 if (!stQueue_.empty()) {
823 head = stQueue_.pop();
824
825 // Reset
826 if (head->rst) {
827 state_ = StClosed;
828 log_->warning("Closing link after reset request. Server=%d", server_);
829
830 // Syn ack
831 } else if (head->syn && (head->ack || server_)) {
832 curMaxBuffers_ = head->maxOutstandingSegments;
833 curMaxSegment_ = head->maxSegmentSize;
834 curCumAckTout_ = head->cumulativeAckTimeout;
835 curRetranTout_ = head->retransmissionTimeout;
836 curNullTout_ = head->nullTimeout;
837 curMaxRetran_ = head->maxRetransmissions;
838 curMaxCumAck_ = head->maxCumulativeAck;
839 lastAckRx_ = head->acknowledge;
840
841 // Convert times
842 convTime(retranToutD1_, curRetranTout_);
843 convTime(cumAckToutD1_, curCumAckTout_);
844 convTime(cumAckToutD2_, curCumAckTout_ / 2);
845 convTime(nullToutD3_, curNullTout_ / 3);
846
847 if (server_) {
848 state_ = StSendSynAck;
849 return (zeroTme_);
850 } else {
851 state_ = StSendSeqAck;
852 }
853 gettimeofday(&stTime_, NULL);
854
855 // reset counters
856 } else {
857 curMaxBuffers_ = locMaxBuffers_;
858 curMaxSegment_ = locMaxSegment_;
859 curCumAckTout_ = locCumAckTout_;
860 curRetranTout_ = locRetranTout_;
861 curNullTout_ = locNullTout_;
862 curMaxRetran_ = locMaxRetran_;
863 curMaxCumAck_ = locMaxCumAck_;
864 }
865
866 // Generate syn after try period passes
867 } else if ((!server_) && timePassed(stTime_, tryPeriodD1_)) {
868 // Allocate frame
869 head = rpr::Header::create(tran_->reqFrame(rpr::Header::SynSize, false));
870
871 // Set frame
872 head->syn = true;
873 head->version = Version;
874 head->chk = true;
875 head->maxOutstandingSegments = locMaxBuffers_;
876 head->maxSegmentSize = locMaxSegment_;
877 head->retransmissionTimeout = locRetranTout_;
878 head->cumulativeAckTimeout = locCumAckTout_;
879 head->nullTimeout = locNullTout_;
880 head->maxRetransmissions = locMaxRetran_;
881 head->maxCumulativeAck = locMaxCumAck_;
882 head->timeoutUnit = TimeoutUnit;
883 head->connectionId = locConnId_;
884
885 transportTx(head, true, false);
886
887 // Update state
888 gettimeofday(&stTime_, NULL);
889 state_ = StWaitSyn;
890 } else if (server_) {
891 state_ = StWaitSyn;
892 }
893
894 return (tryPeriodD4_);
895}
896
898struct timeval& rpr::Controller::stateSendSynAck() {
899 // Allocate frame
900 rpr::HeaderPtr head = rpr::Header::create(tran_->reqFrame(rpr::Header::SynSize, false));
901
902 // Set frame
903 head->syn = true;
904 head->ack = true;
905 head->version = Version;
906 head->chk = true;
907 head->maxOutstandingSegments = curMaxBuffers_;
908 head->maxSegmentSize = curMaxSegment_;
909 head->retransmissionTimeout = curRetranTout_;
910 head->cumulativeAckTimeout = curCumAckTout_;
911 head->nullTimeout = curNullTout_;
912 head->maxRetransmissions = curMaxRetran_;
913 head->maxCumulativeAck = curMaxCumAck_;
914 head->timeoutUnit = TimeoutUnit;
915 head->connectionId = locConnId_;
916
917 transportTx(head, true, true);
918
919 // Update state
920 log_->info("Link state is open. Server=%d", server_);
921 state_ = StOpen;
922 return (cumAckToutD2_);
923}
924
926struct timeval& rpr::Controller::stateSendSeqAck() {
927 // Allocate frame
928 rpr::HeaderPtr ack = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize, false));
929
930 // Setup frame
931 ack->ack = true;
932 ack->nul = false;
933 ackSeqRx_ = lastSeqRx_;
934
935 transportTx(ack, false, true);
936
937 // Update state
938 state_ = StOpen;
939 log_->info("Link state is open. Server=%d", server_);
940 return (cumAckToutD2_);
941}
942
944struct timeval& rpr::Controller::stateOpen() {
945 rpr::HeaderPtr head;
946 uint8_t idx;
947 bool doNull;
948 uint8_t ackPend;
949 struct timeval locTime;
950
951 // Pending frame may be reset
952 while (!stQueue_.empty()) {
953 head = stQueue_.pop();
954
955 // Reset or syn without ack is an error
956 if ((head->rst) || (head->syn && (!head->ack))) {
957 state_ = StError;
958 gettimeofday(&stTime_, NULL);
959 return (zeroTme_);
960 }
961 }
962
963 // Sample transmit time and compute pending ack count under lock
964 {
965 std::unique_lock<std::mutex> lock(txMtx_);
966 locTime = txTime_;
967 ackPend = ackSeqRx_ - lastAckTx_;
968 }
969
970 // NULL required
971 if (timePassed(locTime, nullToutD3_))
972 doNull = true;
973 else
974 doNull = false;
975
976 // Outbound frame required
977 if ((doNull || ((!getLocBusy()) && ackPend >= curMaxCumAck_) ||
978 ((ackPend > 0 || getLocBusy()) && timePassed(locTime, cumAckToutD1_)))) {
979 head = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize, false));
980 head->ack = true;
981 head->nul = doNull;
982 transportTx(head, doNull, false);
983 }
984
985 // Retransmission processing, don't process when busy
986 idx = lastAckRx_;
987 while ((!remBusy_) && (idx != locSequence_)) {
988 if (retransmit(idx++) < 0) {
989 state_ = StError;
990 gettimeofday(&stTime_, NULL);
991 return (zeroTme_);
992 }
993 }
994
995 return (cumAckToutD2_);
996}
997
999struct timeval& rpr::Controller::stateError() {
1000 rpr::HeaderPtr rst;
1001
1002 log_->warning("Entering reset state. Server=%d", server_);
1003
1004 rst = rpr::Header::create(tran_->reqFrame(rpr::Header::HeaderSize, false));
1005 rst->rst = true;
1006
1007 transportTx(rst, true, true);
1008
1009 downCount_++;
1010 log_->warning("Entering closed state after reset. Server=%d", server_);
1011 state_ = StClosed;
1012
1013 // Reset queues. These hold Headers wrapping uncopied frames, so drain them
1014 // under the GIL. See releaseHeaders().
1015#ifndef NO_PYTHON
1016 if (Py_IsInitialized()) {
1017 rogue::ScopedGil gil;
1018 appQueue_.reset();
1019 oooQueue_.clear();
1020 stQueue_.reset();
1021 } else {
1022 appQueue_.reset();
1023 oooQueue_.clear();
1024 stQueue_.reset();
1025 }
1026#else
1027 appQueue_.reset();
1028 oooQueue_.clear();
1029 stQueue_.reset();
1030#endif
1031
1032 gettimeofday(&stTime_, NULL);
1033 return (tryPeriodD1_);
1034}
1035
1037void rpr::Controller::setTimeout(uint32_t timeout) {
1038 div_t divResult = div(timeout, 1000000);
1039 timeout_.tv_sec = divResult.quot;
1040 timeout_.tv_usec = divResult.rem;
1041}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
static const uint32_t Debug
Debug severity level constant.
Definition Logging.h:100
static const uint32_t Warning
Warning severity level constant.
Definition Logging.h:96
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
std::shared_ptr< rogue::interfaces::stream::Buffer > BufferPtr
Shared pointer alias for Buffer.
Definition Buffer.h:270
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110
void defaultTimeout(struct timeval &tout)
Returns Rogue default timeout as a timeval.
Definition Helpers.h:49