IT++ Logo
tcp.h
Go to the documentation of this file.
1 
32 #ifndef TCP_H
33 #define TCP_H
34 
35 #include <itpp/base/vec.h>
36 
37 #if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB) && !(defined(itpp_EXPORTS) || defined(itpp_debug_EXPORTS)))
38 
39 #ifndef ITPP_PROTOCOL_EXCLUDED
40 #define ITPP_PROTOCOL_EXCLUDED
41 #pragma message( "PROTOCOL definitions are not available for MSVC shared builds" )
42 #endif
43 
44 #else
45 
46 #include <itpp/base/converters.h>
47 #include <itpp/protocol/packet.h>
48 #include <itpp/protocol/events.h>
49 
50 
51 namespace itpp
52 {
53 
55 
56 
72 {
73 public:
75  Sequence_Number() : seq(0) { }
79  Sequence_Number &operator=(const Sequence_Number &n) { seq = n.seq; return *this; }
81  Sequence_Number &operator=(const int &rep) { seq = rep; return *this; }
82 
83  //relational operators
85  bool operator==(const Sequence_Number &n) const { return seq == n.seq; }
87  bool operator!=(const Sequence_Number &n) const { return seq != n.seq; }
89  bool operator>(const Sequence_Number &n) const { return (seq - n.seq) > 0; }
91  bool operator>=(const Sequence_Number &n) const { return (seq - n.seq) >= 0; }
93  bool operator<(const Sequence_Number &n) const { return (seq - n.seq) < 0; }
95  bool operator<=(const Sequence_Number &n) const { return (seq - n.seq) <= 0; }
96 
97  //addition and subtraction
99  Sequence_Number operator+(const int n) const { return Sequence_Number(seq + n); }
101  Sequence_Number &operator+=(const int n) { seq += n; return *this; }
103  Sequence_Number operator-(const int n) const { return Sequence_Number(seq - n); }
105  Sequence_Number &operator-=(const int n) { seq -= n; return *this; }
107  int operator-(const Sequence_Number &n) const { return seq - n.seq; }
108 
110  int value() const { return seq; }
111 
113  friend Sequence_Number operator+(const int n1, const Sequence_Number &n2) { return Sequence_Number(n1 + n2.seq); }
115  friend std::ostream &operator<<(std::ostream &os, const Sequence_Number &n) { os << n.seq; return os; }
116 
117 protected:
119  Sequence_Number(int n) : seq(n) {}
121  int seq;
122 };
123 
125 inline const Sequence_Number & min(const Sequence_Number &n1, const Sequence_Number &n2) { return (n1 < n2) ? n1 : n2; }
127 inline const Sequence_Number & max(const Sequence_Number &n1, const Sequence_Number &n2) { return (n1 > n2) ? n1 : n2; }
128 
129 
130 
146 {
147 public:
149  TCP_Segment();
151  TCP_Segment(const Sequence_Number &sn_begin, const Sequence_Number &sn_end);
153  TCP_Segment(const TCP_Segment &segment);
154 
155  //modification
157  TCP_Segment &operator=(const TCP_Segment &segment);
159  void set_begin(const Sequence_Number &sn);
161  void set_end(const Sequence_Number &sn);
163  void combine(const TCP_Segment &segment);
164 
165  //query
167  bool operator==(const TCP_Segment &segment) const;
169  bool operator!=(const TCP_Segment &segment) const;
171  bool can_be_combined(const TCP_Segment &segment) const;
173  bool is_contained(const TCP_Segment &segment) const;
175  unsigned length() const;
177  Sequence_Number begin() const { return seq_begin; }
179  Sequence_Number end() const { return seq_end; }
180 
182  friend std::ostream & operator<<(std::ostream &os, const TCP_Segment &segment);
183 
184 protected:
187 };
188 
189 
211 class TCP_Packet : public itpp::Packet
212 {
213 public:
215  TCP_Packet();
217  TCP_Packet(const TCP_Packet &packet);
219  virtual ~TCP_Packet();
221  virtual TCP_Packet &clone() const;
222 
223  //TCP window mechanism
225  void set_segment(const TCP_Segment &seg) { fSegment = seg; }
227  TCP_Segment get_segment() const { return fSegment; }
229  void set_wnd(unsigned val) { fWnd = val; }
231  unsigned get_wnd() const { return fWnd; }
233  void set_ACK(Sequence_Number val) { fACK = val; }
235  Sequence_Number get_ACK() const { return fACK; }
236 
237  //session control
239  void set_session_id(int val) { fSessionId = val; }
241  int get_session_id() const { return fSessionId; }
242 
243  //debugging support
245  void set_destination_port(unsigned val) { fDestinationPort = val; }
247  unsigned get_destination_port() const { return fDestinationPort; }
249  void set_source_port(unsigned val) { fSourcePort = val; }
251  unsigned get_source_port() const { return fSourcePort; }
253  void set_info(unsigned ssThresh, unsigned recWnd, unsigned cWnd, double estRTT, Sequence_Number sndUna, Sequence_Number sndNxt, bool isRtx);
255  virtual void print_header(std::ostream &) const;
256 
257 protected:
261  unsigned fSourcePort;
262 
265  unsigned fWnd;
268  //Tracing
269 
271  struct TDebugInfo {
272  unsigned fSSThresh;
273  unsigned fRecWnd;
274  unsigned fCWnd;
275  double fRTTEstimate;
278  bool fRtxFlag;
279  };
280 
283 
285  friend std::ostream & operator<<(std::ostream &, TCP_Packet &);
286 };
287 
288 
324 {
325 public:
327  TCP_Sender(int label);
328 
330  virtual ~TCP_Sender();
331 
332  //connection control
334  virtual void setup();
336  virtual void release(std::string trace_filename = "");
337 
339  virtual void print_item(std::ostream &, const std::string &);
340 
342  virtual void set_debug(const bool enable_debug = true);
344  virtual void set_debug(bool enable_debug, bool enable_signal_debug);
346  virtual void set_trace(const bool enable_trace = true);
348  virtual void save_trace(std::string filename);
349 
358 
359 
360  //Signal<itpp::Packet*> TcpSendSignal;
361  //Slot<TCP_Sender, itpp::Packet*> TcpRecvAckSlot;
362  //Slot<TCP_Sender, itpp::Packet*> SocketWriteSlot;
363  //Slot<TCP_Sender, string> ReleaseSlot;
364 
365 private:
366  std::queue<itpp::Packet*> SocketWriteQueue;
367 
368  virtual void InitStatistics();
369  virtual void HandleACK(TCP_Packet &);
370  virtual void SendNewData(bool skipSWSA = false);
371  virtual void UnaRetransmit();
372  virtual void FinishFastRecovery();
373  virtual void ReduceSSThresh();
374  virtual void SendMsg(TCP_Packet & msg);
375  virtual void HandleRtxTimeout(Ttype);
376  virtual void IdleCheck();
377  virtual void HandleSWSATimeout(Ttype);
378  virtual unsigned GetNextSegmentSize(const Sequence_Number & begin);
379  virtual unsigned SendWindow() const;
380  virtual double CalcRTOValue() const;
381  virtual void SetRtxTimer();
382  virtual void UpdateRTTVariables(double sampleRTT);
383  virtual void TraceCWnd();
384  virtual void TraceSentSeqNo(const Sequence_Number sn);
385  virtual void TraceACKedSeqNo(const Sequence_Number sn);
386  virtual void TraceRTTVariables(double sampleRTT);
387  virtual void TraceSSThresh();
388  virtual std::string GenerateFilename();
389 
390  void StopTransientPhase();
392  enum eTCPVersion {kTahoe, kReno, kNewReno};
393 
394  virtual void set_label(int label);
395 
396  //socket variables
397  unsigned fLabel; // end point identification also used at receiver
398 
399  //message handling
400  virtual void HandleUserMessageIndication(itpp::Packet *user_data);
401  virtual void ReceiveMessageFromNet(itpp::Packet *msg);
402 
403  //parameters parsed from input file
404  eTCPVersion fTCPVersion; // one of Reno, Tahoe, NewReno
405  unsigned fMSS; // maximum segment size
406  unsigned fTCPIPHeaderLength;
407  double fInitialRTT;
408  unsigned fInitialCWnd;
409  unsigned fInitialSSThresh;
410  unsigned fMaxCWnd;
411  unsigned fDupACKThreshold;
412  double fTimerGranularity;
413  double fMaxRTO; // max value of retransmission TO
414  unsigned fMaxBackoff;
415  bool fImmediateBackoffReset;
416  bool fKarn;
417  bool fGoBackN;
418  bool fFlightSizeRecovery;// use flight size on fast rec. exit
419  bool fRenoConservation;
420  bool fCarefulSSThreshReduction;
421  bool fIgnoreDupACKOnTORecovery;
422  bool fCarefulMulFastRtxAvoidance;
423  bool fNagle;
424  double fSWSATimerValue;
425  bool fRestartAfterIdle;
426  bool fTraceCWnd; // print CWnd trace to cout
427  bool fTraceSentSeqNo;
428  bool fTraceACKedSeqNo;
429  bool fDebug; // print additional information to cout
430  bool fTrace; // store trace info in vectors
431 
432  //session identification
433  int fSessionId;
435  //TCP flow control (RFC 793)
436  Sequence_Number fSndUna; // lowest unacknowledged sn
437  Sequence_Number fSndNxt; // next byte to be sent
438  Sequence_Number fSndMax;
439  unsigned fRecWnd; // receiver advertised window
440  unsigned fMaxRecWnd;
441  Sequence_Number fUserNxt; // next byte to be received from user
442 
443  //TCP congestion avoidance (RFC 2581, RFC 2001, RFC 2582)
444  unsigned fCWnd; // congestion window
445  unsigned fSSThresh;
446  unsigned fDupACKCnt;
447  Sequence_Number fRecoveryDupACK;
448  Sequence_Number fRecoveryTO;
450  //TCP timers
451  TTimer<TCP_Sender> fRtxTimer;
452  Sequence_Number fTimUna;
453  TTimer<TCP_Sender> fSWSATimer;
454  int fBackoff;
455  bool fPendingBackoffReset;
456  Ttype fLastSendTime;
458  //round trip time measurement (RTTM)
459  double fSRTT;
460  double fRTTVar;
461  double fRTTEstimate;
462  Sequence_Number fRTTMByte;
463  bool fRTTMPending;
464  double fRTTMStartTime;
466  //statistic counters
467  unsigned long fNumberOfTimeouts;
468  unsigned long fNumberOfFastRetransmits;
469  unsigned long fNumberOfRTTMeasurements;
470  unsigned long fNumberOfReceivedACKs;
471  unsigned long fNumberOfIdleTimeouts;
472 
473  vec CWnd_val;
474  vec CWnd_time;
475  int CWnd_index;
476 
477  vec SSThresh_val;
478  vec SSThresh_time;
479  int SSThresh_index;
480 
481  ivec sent_seq_num_val;
482  vec sent_seq_num_time;
483  int sent_seq_num_index;
484 
485  ivec sender_recv_ack_seq_num_val;
486  vec sender_recv_ack_seq_num_time;
487  int sender_recv_ack_seq_num_index;
488 
489  vec RTTEstimate_val;
490  vec RTTEstimate_time;
491  int RTTEstimate_index;
492 
493  vec RTTsample_val;
494  vec RTTsample_time;
495  int RTTsample_index;
496 };
497 
498 
520 {
521 public:
528 
529  void reset();
531  void write(TCP_Segment newBlock);
532  void read(unsigned noOfBytes);
534  unsigned first_block_size() const;
535  Sequence_Number first_byte() const;
536  Sequence_Number last_byte() const;
538 
539  unsigned window() const;
540 
541  std::ostream &info(std::ostream &os, int detail = 0) const;
543 protected:
546 
547  std::list <TCP_Segment> fBufList;
548 };
549 
550 
551 
552 
583 {
584 public:
585 
587  TCP_Receiver(int label);
589  virtual ~TCP_Receiver();
590 
591  //name connection control
593  virtual void setup();
595  virtual void release(std::string trace_filename = "");
596 
597  //message handling
601 
602  virtual void set_debug(const bool enable_debug = true);
604  virtual void set_debug(bool enable_debug, bool enable_signal_debug);
606  virtual void set_trace(const bool enable_trace = true);
608  virtual void save_trace(std::string filename);
609 
615 
617 
618 private:
619  void IndicateUserMessage();
620  virtual void ReceiveMessageFromNet(itpp::Packet* msg);
622  virtual void ReceiveDataPacket(TCP_Packet & packet);
623  virtual void SendACK(bool);
624  virtual void ScheduleACKMessage();
625  virtual void SendACKMessage(Ttype);
626  virtual void DelayedACKHandler(Ttype);
627  virtual void PeriodicACKHandler(Ttype);
628  virtual void HandleEndOfProcessing(Ttype);
629  virtual void TraceReceivedSeqNo(const Sequence_Number &sn);
630  virtual std::string GenerateFilename();
631 
632  //basic variables
633  TCP_Receiver_Buffer fReceiverBuffer;
634  unsigned fLabel;
635 
636  //parameters read by the parser
637  unsigned fTCPIPHeaderLength;
638  unsigned fMSS;
639  unsigned fBufferSize;
640  bool fDelayedACK;
641  Ttype fACKDelayTime;
642  bool fSendPeriodicACKs;
643  bool fStrictPeriodicACKs;
644  Ttype fPeriodicACKInterval;// interval after which an ACK is repeated
645  Ttype fACKSchedulingDelay;
646  bool fACKOnBufferWrite;
647  bool fACKOnBufferRead;
648  unsigned fMaxUserBlockSize;
649  unsigned fMinUserBlockSize;
650  double fUserBlockProcDelay;
651  bool fTrace;
652  bool fDebug;
654  //specific TCP variables
655  Sequence_Number fAdvRcvNxt;
656  unsigned fAdvRcvWnd;
658  //session management
659  int fSessionId;
661  //ACK timers
662  TTimer<TCP_Receiver> fDelayedACKTimer;
663  TTimer<TCP_Receiver> fPeriodicACKTimer;
664  TTimer<TCP_Receiver> fACKSchedulingTimer;
665  TCP_Packet * fWaitingACKMsg;
666 
667  //user data delivery
668  Packet * fUserMessage;
669  TTimer<TCP_Receiver> fUserBlockProcTimer;
670 
671 
672  //statistic counters
673  ivec received_seq_num_val;
674  vec received_seq_num_time;
675  int received_seq_num_index;
676 
677 };
678 
679 
680 
681 // ------------------------------- Inline definitions ---------------------------------------------
682 
683 
684 
686 {
687  return fFirstByte;
688 }
689 
690 
692 {
693  if (fBufList.empty()) {
694  return fFirstByte;
695  }
696  else {
697  return fBufList.back().end();
698  }
699 }
700 
701 
703 {
704  return fFirstByte + first_block_size();
705 }
706 
707 
709 {
710  seq_begin = sn;
711 
712  it_assert(seq_begin <= seq_end, "TCP_Segment::begin, end byte " + to_str(seq_end.value()) + " < begin byte " + to_str(seq_begin.value()));
713 }
714 
715 
716 inline void TCP_Segment::set_end(const Sequence_Number &sn)
717 {
718  seq_end = sn;
719 
720  it_assert(seq_begin <= seq_end, "TCP_Segment::set_begin, end byte " + to_str(seq_end.value()) + " < begin byte " + to_str(seq_begin.value()));
721 }
722 
723 
724 inline bool TCP_Segment::operator==(const TCP_Segment &segment) const
725 {
726  return (this->seq_begin == segment.seq_begin) && (this->seq_end == segment.seq_end);
727 }
728 
729 
730 inline bool TCP_Segment::operator!=(const TCP_Segment &segment) const
731 {
732  return (this->seq_begin != segment.seq_begin) || (this->seq_end != segment.seq_end);
733 }
734 
735 
736 inline bool TCP_Segment::can_be_combined(const TCP_Segment &segment) const
737 {
738  return (this->seq_begin <= segment.seq_end) && (segment.seq_begin <= this->seq_end);
739 }
740 
741 
742 inline bool TCP_Segment::is_contained(const TCP_Segment &segment) const
743 {
744  return (segment.seq_begin <= this->seq_begin) && (this->seq_end <= segment.seq_end);
745 }
746 
747 
748 inline unsigned TCP_Segment::length() const
749 {
750  return seq_end - seq_begin;
751 }
752 
754 
755 
756 } // namespace itpp
757 
758 #endif
759 
760 #endif // #ifndef TCP_H
761 
SourceForge Logo

Generated on Sat May 25 2013 16:32:23 for IT++ by Doxygen 1.8.2