1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2018 NITK Surathkal
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Viyom Mittal <viyommittal@gmail.com>
19  *         Vivek Jain <jain.vivek.anand@gmail.com>
20  *         Mohit P. Tahiliani <tahiliani@nitk.edu.in>
21  *
22  */
23 
24 #include "tcp-prr-recovery.h"
25 #include "tcp-socket-state.h"
26 
27 #include "ns3/log.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("TcpPrrRecovery");
32 NS_OBJECT_ENSURE_REGISTERED (TcpPrrRecovery);
33 
34 TypeId
GetTypeId(void)35 TcpPrrRecovery::GetTypeId (void)
36 {
37   static TypeId tid = TypeId ("ns3::TcpPrrRecovery")
38     .SetParent<TcpClassicRecovery> ()
39     .AddConstructor<TcpPrrRecovery> ()
40     .SetGroupName ("Internet")
41     .AddAttribute ("ReductionBound", "Type of Reduction Bound",
42                    EnumValue (SSRB),
43                    MakeEnumAccessor (&TcpPrrRecovery::m_reductionBoundMode),
44                    MakeEnumChecker (CRB, "CRB",
45                                     SSRB, "SSRB"))
46   ;
47   return tid;
48 }
49 
TcpPrrRecovery(void)50 TcpPrrRecovery::TcpPrrRecovery (void)
51   : TcpClassicRecovery ()
52 {
53   NS_LOG_FUNCTION (this);
54 }
55 
TcpPrrRecovery(const TcpPrrRecovery & recovery)56 TcpPrrRecovery::TcpPrrRecovery (const TcpPrrRecovery& recovery)
57   : TcpClassicRecovery (recovery),
58     m_prrDelivered (recovery.m_prrDelivered),
59     m_prrOut (recovery.m_prrOut),
60     m_recoveryFlightSize (recovery.m_recoveryFlightSize),
61     m_reductionBoundMode (recovery.m_reductionBoundMode)
62 {
63   NS_LOG_FUNCTION (this);
64 }
65 
~TcpPrrRecovery(void)66 TcpPrrRecovery::~TcpPrrRecovery (void)
67 {
68   NS_LOG_FUNCTION (this);
69 }
70 
71 void
EnterRecovery(Ptr<TcpSocketState> tcb,uint32_t dupAckCount,uint32_t unAckDataCount,uint32_t deliveredBytes)72 TcpPrrRecovery::EnterRecovery (Ptr<TcpSocketState> tcb, uint32_t dupAckCount,
73                                uint32_t unAckDataCount, uint32_t deliveredBytes)
74 {
75   NS_LOG_FUNCTION (this << tcb << dupAckCount << unAckDataCount);
76   NS_UNUSED (dupAckCount);
77 
78   m_prrOut = 0;
79   m_prrDelivered = 0;
80   m_recoveryFlightSize = unAckDataCount;
81 
82   DoRecovery (tcb, deliveredBytes);
83 }
84 
85 void
DoRecovery(Ptr<TcpSocketState> tcb,uint32_t deliveredBytes)86 TcpPrrRecovery::DoRecovery (Ptr<TcpSocketState> tcb, uint32_t deliveredBytes)
87 {
88   NS_LOG_FUNCTION (this << tcb << deliveredBytes);
89   m_prrDelivered += deliveredBytes;
90 
91   int sendCount;
92   if (tcb->m_bytesInFlight > tcb->m_ssThresh)
93     {
94       sendCount = std::ceil (m_prrDelivered * tcb->m_ssThresh * 1.0 / m_recoveryFlightSize) - m_prrOut;
95     }
96   else
97     {
98       int limit = static_cast<int> (tcb->m_ssThresh - tcb->m_bytesInFlight);
99       if (m_reductionBoundMode == CRB)
100         {
101           limit = m_prrDelivered - m_prrOut;
102         }
103       else if (m_reductionBoundMode == SSRB)
104         {
105           if (tcb->m_isRetransDataAcked)
106             {
107               limit = std::max (m_prrDelivered - m_prrOut, deliveredBytes) + tcb->m_segmentSize;
108             }
109           else
110             {
111               limit = deliveredBytes;
112             }
113         }
114       sendCount = std::min (limit, static_cast<int> (tcb->m_ssThresh - tcb->m_bytesInFlight));
115     }
116 
117   /* Force a fast retransmit upon entering fast recovery */
118   sendCount = std::max (sendCount, static_cast<int> (m_prrOut > 0 ? 0 : tcb->m_segmentSize));
119   tcb->m_cWnd = tcb->m_bytesInFlight + sendCount;
120   tcb->m_cWndInfl = tcb->m_cWnd;
121 }
122 
123 void
ExitRecovery(Ptr<TcpSocketState> tcb)124 TcpPrrRecovery::ExitRecovery (Ptr<TcpSocketState> tcb)
125 {
126   NS_LOG_FUNCTION (this << tcb);
127   tcb->m_cWndInfl = tcb->m_cWnd;
128 }
129 
130 void
UpdateBytesSent(uint32_t bytesSent)131 TcpPrrRecovery::UpdateBytesSent (uint32_t bytesSent)
132 {
133   NS_LOG_FUNCTION (this << bytesSent);
134   m_prrOut += bytesSent;
135 }
136 
137 Ptr<TcpRecoveryOps>
Fork(void)138 TcpPrrRecovery::Fork (void)
139 {
140   return CopyObject<TcpPrrRecovery> (this);
141 }
142 
143 std::string
GetName() const144 TcpPrrRecovery::GetName () const
145 {
146   return "PrrRecovery";
147 }
148 
149 } // namespace ns3
150