IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #include <itpp/base/converters.h>
37 #include <itpp/protocol/packet.h>
38 #include <itpp/protocol/events.h>
39 
40 
41 namespace itpp
42 {
43 
45 
46 
62 {
63 public:
65  Sequence_Number() : seq(0) { }
69  Sequence_Number &operator=(const Sequence_Number &n) { seq = n.seq; return *this; }
71  Sequence_Number &operator=(const int &rep) { seq = rep; return *this; }
72 
73  //relational operators
75  bool operator==(const Sequence_Number &n) const { return seq == n.seq; }
77  bool operator!=(const Sequence_Number &n) const { return seq != n.seq; }
79  bool operator>(const Sequence_Number &n) const { return (seq - n.seq) > 0; }
81  bool operator>=(const Sequence_Number &n) const { return (seq - n.seq) >= 0; }
83  bool operator<(const Sequence_Number &n) const { return (seq - n.seq) < 0; }
85  bool operator<=(const Sequence_Number &n) const { return (seq - n.seq) <= 0; }
86 
87  //addition and subtraction
89  Sequence_Number operator+(const int n) const { return Sequence_Number(seq + n); }
91  Sequence_Number &operator+=(const int n) { seq += n; return *this; }
93  Sequence_Number operator-(const int n) const { return Sequence_Number(seq - n); }
95  Sequence_Number &operator-=(const int n) { seq -= n; return *this; }
97  int operator-(const Sequence_Number &n) const { return seq - n.seq; }
98 
100  int value() const { return seq; }
101 
103  friend Sequence_Number operator+(const int n1, const Sequence_Number &n2) { return Sequence_Number(n1 + n2.seq); }
105  friend std::ostream &operator<<(std::ostream &os, const Sequence_Number &n) { os << n.seq; return os; }
106 
107 protected:
109  Sequence_Number(int n) : seq(n) {}
111  int seq;
112 };
113 
115 inline const Sequence_Number & min(const Sequence_Number &n1, const Sequence_Number &n2) { return (n1 < n2) ? n1 : n2; }
117 inline const Sequence_Number & max(const Sequence_Number &n1, const Sequence_Number &n2) { return (n1 > n2) ? n1 : n2; }
118 
119 
120 
136 {
137 public:
139  TCP_Segment();
141  TCP_Segment(const Sequence_Number &sn_begin, const Sequence_Number &sn_end);
143  TCP_Segment(const TCP_Segment &segment);
144 
145  //modification
147  TCP_Segment &operator=(const TCP_Segment &segment);
149  void set_begin(const Sequence_Number &sn);
151  void set_end(const Sequence_Number &sn);
153  void combine(const TCP_Segment &segment);
154 
155  //query
157  bool operator==(const TCP_Segment &segment) const;
159  bool operator!=(const TCP_Segment &segment) const;
161  bool can_be_combined(const TCP_Segment &segment) const;
163  bool is_contained(const TCP_Segment &segment) const;
165  unsigned length() const;
167  Sequence_Number begin() const { return seq_begin; }
169  Sequence_Number end() const { return seq_end; }
170 
172  friend std::ostream & operator<<(std::ostream &os, const TCP_Segment &segment);
173 
174 protected:
177 };
178 
179 
201 class TCP_Packet : public itpp::Packet
202 {
203 public:
205  TCP_Packet();
207  TCP_Packet(const TCP_Packet &packet);
209  virtual ~TCP_Packet();
211  virtual TCP_Packet &clone() const;
212 
213  //TCP window mechanism
215  void set_segment(const TCP_Segment &seg) { fSegment = seg; }
217  TCP_Segment get_segment() const { return fSegment; }
219  void set_wnd(unsigned val) { fWnd = val; }
221  unsigned get_wnd() const { return fWnd; }
223  void set_ACK(Sequence_Number val) { fACK = val; }
225  Sequence_Number get_ACK() const { return fACK; }
226 
227  //session control
229  void set_session_id(int val) { fSessionId = val; }
231  int get_session_id() const { return fSessionId; }
232 
233  //debugging support
235  void set_destination_port(unsigned val) { fDestinationPort = val; }
237  unsigned get_destination_port() const { return fDestinationPort; }
239  void set_source_port(unsigned val) { fSourcePort = val; }
241  unsigned get_source_port() const { return fSourcePort; }
243  void set_info(unsigned ssThresh, unsigned recWnd, unsigned cWnd, double estRTT, Sequence_Number sndUna, Sequence_Number sndNxt, bool isRtx);
245  virtual void print_header(std::ostream &) const;
246 
247 protected:
251  unsigned fSourcePort;
252 
255  unsigned fWnd;
258  //Tracing
259 
261  struct TDebugInfo {
262  unsigned fSSThresh;
263  unsigned fRecWnd;
264  unsigned fCWnd;
265  double fRTTEstimate;
268  bool fRtxFlag;
269  };
270 
273 
275  friend std::ostream & operator<<(std::ostream &, TCP_Packet &);
276 };
277 
278 
314 {
315 public:
317  TCP_Sender(int label);
318 
320  virtual ~TCP_Sender();
321 
322  //connection control
324  virtual void setup();
326  virtual void release(std::string trace_filename = "");
327 
329  virtual void print_item(std::ostream &, const std::string &);
330 
332  virtual void set_debug(const bool enable_debug = true);
334  virtual void set_debug(bool enable_debug, bool enable_signal_debug);
336  virtual void set_trace(const bool enable_trace = true);
338  virtual void save_trace(std::string filename);
339 
348 
349 
350  //Signal<itpp::Packet*> TcpSendSignal;
351  //Slot<TCP_Sender, itpp::Packet*> TcpRecvAckSlot;
352  //Slot<TCP_Sender, itpp::Packet*> SocketWriteSlot;
353  //Slot<TCP_Sender, string> ReleaseSlot;
354 
355 private:
356  std::queue<itpp::Packet*> SocketWriteQueue;
357 
358  virtual void InitStatistics();
359  virtual void HandleACK(TCP_Packet &);
360  virtual void SendNewData(bool skipSWSA = false);
361  virtual void UnaRetransmit();
362  virtual void FinishFastRecovery();
363  virtual void ReduceSSThresh();
364  virtual void SendMsg(TCP_Packet & msg);
365  virtual void HandleRtxTimeout(Ttype);
366  virtual void IdleCheck();
367  virtual void HandleSWSATimeout(Ttype);
368  virtual unsigned GetNextSegmentSize(const Sequence_Number & begin);
369  virtual unsigned SendWindow() const;
370  virtual double CalcRTOValue() const;
371  virtual void SetRtxTimer();
372  virtual void UpdateRTTVariables(double sampleRTT);
373  virtual void TraceCWnd();
374  virtual void TraceSentSeqNo(const Sequence_Number sn);
375  virtual void TraceACKedSeqNo(const Sequence_Number sn);
376  virtual void TraceRTTVariables(double sampleRTT);
377  virtual void TraceSSThresh();
378  virtual std::string GenerateFilename();
379 
380  void StopTransientPhase();
382  enum eTCPVersion {kTahoe, kReno, kNewReno};
383 
384  virtual void set_label(int label);
385 
386  //socket variables
387  unsigned fLabel; // end point identification also used at receiver
388 
389  //message handling
390  virtual void HandleUserMessageIndication(itpp::Packet *user_data);
391  virtual void ReceiveMessageFromNet(itpp::Packet *msg);
392 
393  //parameters parsed from input file
394  eTCPVersion fTCPVersion; // one of Reno, Tahoe, NewReno
395  unsigned fMSS; // maximum segment size
396  unsigned fTCPIPHeaderLength;
397  double fInitialRTT;
398  unsigned fInitialCWnd;
399  unsigned fInitialSSThresh;
400  unsigned fMaxCWnd;
401  unsigned fDupACKThreshold;
402  double fTimerGranularity;
403  double fMaxRTO; // max value of retransmission TO
404  unsigned fMaxBackoff;
405  bool fImmediateBackoffReset;
406  bool fKarn;
407  bool fGoBackN;
408  bool fFlightSizeRecovery;// use flight size on fast rec. exit
409  bool fRenoConservation;
410  bool fCarefulSSThreshReduction;
411  bool fIgnoreDupACKOnTORecovery;
412  bool fCarefulMulFastRtxAvoidance;
413  bool fNagle;
414  double fSWSATimerValue;
415  bool fRestartAfterIdle;
416  bool fTraceCWnd; // print CWnd trace to cout
417  bool fTraceSentSeqNo;
418  bool fTraceACKedSeqNo;
419  bool fDebug; // print additional information to cout
420  bool fTrace; // store trace info in vectors
421 
422  //session identification
423  int fSessionId;
425  //TCP flow control (RFC 793)
426  Sequence_Number fSndUna; // lowest unacknowledged sn
427  Sequence_Number fSndNxt; // next byte to be sent
428  Sequence_Number fSndMax;
429  unsigned fRecWnd; // receiver advertised window
430  unsigned fMaxRecWnd;
431  Sequence_Number fUserNxt; // next byte to be received from user
432 
433  //TCP congestion avoidance (RFC 2581, RFC 2001, RFC 2582)
434  unsigned fCWnd; // congestion window
435  unsigned fSSThresh;
436  unsigned fDupACKCnt;
437  Sequence_Number fRecoveryDupACK;
438  Sequence_Number fRecoveryTO;
440  //TCP timers
441  TTimer<TCP_Sender> fRtxTimer;
442  Sequence_Number fTimUna;
443  TTimer<TCP_Sender> fSWSATimer;
444  int fBackoff;
445  bool fPendingBackoffReset;
446  Ttype fLastSendTime;
448  //round trip time measurement (RTTM)
449  double fSRTT;
450  double fRTTVar;
451  double fRTTEstimate;
452  Sequence_Number fRTTMByte;
453  bool fRTTMPending;
454  double fRTTMStartTime;
456  //statistic counters
457  unsigned long fNumberOfTimeouts;
458  unsigned long fNumberOfFastRetransmits;
459  unsigned long fNumberOfRTTMeasurements;
460  unsigned long fNumberOfReceivedACKs;
461  unsigned long fNumberOfIdleTimeouts;
462 
463  vec CWnd_val;
464  vec CWnd_time;
465  int CWnd_index;
466 
467  vec SSThresh_val;
468  vec SSThresh_time;
469  int SSThresh_index;
470 
471  ivec sent_seq_num_val;
472  vec sent_seq_num_time;
473  int sent_seq_num_index;
474 
475  ivec sender_recv_ack_seq_num_val;
476  vec sender_recv_ack_seq_num_time;
477  int sender_recv_ack_seq_num_index;
478 
479  vec RTTEstimate_val;
480  vec RTTEstimate_time;
481  int RTTEstimate_index;
482 
483  vec RTTsample_val;
484  vec RTTsample_time;
485  int RTTsample_index;
486 };
487 
488 
510 {
511 public:
518 
519  void reset();
521  void write(TCP_Segment newBlock);
522  void read(unsigned noOfBytes);
524  unsigned first_block_size() const;
525  Sequence_Number first_byte() const;
526  Sequence_Number last_byte() const;
528 
529  unsigned window() const;
530 
531  std::ostream &info(std::ostream &os, int detail = 0) const;
533 protected:
536 
537  std::list <TCP_Segment> fBufList;
538 };
539 
540 
541 
542 
573 {
574 public:
575 
577  TCP_Receiver(int label);
579  virtual ~TCP_Receiver();
580 
581  //name connection control
583  virtual void setup();
585  virtual void release(std::string trace_filename = "");
586 
587  //message handling
591 
592  virtual void set_debug(const bool enable_debug = true);
594  virtual void set_debug(bool enable_debug, bool enable_signal_debug);
596  virtual void set_trace(const bool enable_trace = true);
598  virtual void save_trace(std::string filename);
599 
605 
607 
608 private:
609  void IndicateUserMessage();
610  virtual void ReceiveMessageFromNet(itpp::Packet* msg);
612  virtual void ReceiveDataPacket(TCP_Packet & packet);
613  virtual void SendACK(bool);
614  virtual void ScheduleACKMessage();
615  virtual void SendACKMessage(Ttype);
616  virtual void DelayedACKHandler(Ttype);
617  virtual void PeriodicACKHandler(Ttype);
618  virtual void HandleEndOfProcessing(Ttype);
619  virtual void TraceReceivedSeqNo(const Sequence_Number &sn);
620  virtual std::string GenerateFilename();
621 
622  //basic variables
623  TCP_Receiver_Buffer fReceiverBuffer;
624  unsigned fLabel;
625 
626  //parameters read by the parser
627  unsigned fTCPIPHeaderLength;
628  unsigned fMSS;
629  unsigned fBufferSize;
630  bool fDelayedACK;
631  Ttype fACKDelayTime;
632  bool fSendPeriodicACKs;
633  bool fStrictPeriodicACKs;
634  Ttype fPeriodicACKInterval;// interval after which an ACK is repeated
635  Ttype fACKSchedulingDelay;
636  bool fACKOnBufferWrite;
637  bool fACKOnBufferRead;
638  unsigned fMaxUserBlockSize;
639  unsigned fMinUserBlockSize;
640  double fUserBlockProcDelay;
641  bool fTrace;
642  bool fDebug;
644  //specific TCP variables
645  Sequence_Number fAdvRcvNxt;
646  unsigned fAdvRcvWnd;
648  //session management
649  int fSessionId;
651  //ACK timers
652  TTimer<TCP_Receiver> fDelayedACKTimer;
653  TTimer<TCP_Receiver> fPeriodicACKTimer;
654  TTimer<TCP_Receiver> fACKSchedulingTimer;
655  TCP_Packet * fWaitingACKMsg;
656 
657  //user data delivery
658  Packet * fUserMessage;
659  TTimer<TCP_Receiver> fUserBlockProcTimer;
660 
661 
662  //statistic counters
663  ivec received_seq_num_val;
664  vec received_seq_num_time;
665  int received_seq_num_index;
666 
667 };
668 
669 
670 
671 // ------------------------------- Inline definitions ---------------------------------------------
672 
673 
674 
676 {
677  return fFirstByte;
678 }
679 
680 
682 {
683  if (fBufList.empty()) {
684  return fFirstByte;
685  }
686  else {
687  return fBufList.back().end();
688  }
689 }
690 
691 
693 {
694  return fFirstByte + first_block_size();
695 }
696 
697 
699 {
700  seq_begin = sn;
701 
702  it_assert(seq_begin <= seq_end, "TCP_Segment::begin, end byte " + to_str(seq_end.value()) + " < begin byte " + to_str(seq_begin.value()));
703 }
704 
705 
706 inline void TCP_Segment::set_end(const Sequence_Number &sn)
707 {
708  seq_end = sn;
709 
710  it_assert(seq_begin <= seq_end, "TCP_Segment::set_begin, end byte " + to_str(seq_end.value()) + " < begin byte " + to_str(seq_begin.value()));
711 }
712 
713 
714 inline bool TCP_Segment::operator==(const TCP_Segment &segment) const
715 {
716  return (this->seq_begin == segment.seq_begin) && (this->seq_end == segment.seq_end);
717 }
718 
719 
720 inline bool TCP_Segment::operator!=(const TCP_Segment &segment) const
721 {
722  return (this->seq_begin != segment.seq_begin) || (this->seq_end != segment.seq_end);
723 }
724 
725 
726 inline bool TCP_Segment::can_be_combined(const TCP_Segment &segment) const
727 {
728  return (this->seq_begin <= segment.seq_end) && (segment.seq_begin <= this->seq_end);
729 }
730 
731 
732 inline bool TCP_Segment::is_contained(const TCP_Segment &segment) const
733 {
734  return (segment.seq_begin <= this->seq_begin) && (this->seq_end <= segment.seq_end);
735 }
736 
737 
738 inline unsigned TCP_Segment::length() const
739 {
740  return seq_end - seq_begin;
741 }
742 
744 
745 
746 } // namespace itpp
747 
748 #endif // #ifndef TCP_H
749 
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:14 for IT++ by Doxygen 1.8.1.2