A Discrete-Event Network Simulator
API
tcp-rate-ops.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Natale Patriciello <natale.patriciello@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 #include "tcp-rate-ops.h"
19 
20 #include "ns3/log.h"
21 #include "ns3/simulator.h"
22 
23 namespace ns3
24 {
25 
26 NS_LOG_COMPONENT_DEFINE("TcpRateOps");
27 NS_OBJECT_ENSURE_REGISTERED(TcpRateOps);
28 
29 TypeId
31 {
32  static TypeId tid = TypeId("ns3::TcpRateOps").SetParent<Object>().SetGroupName("Internet");
33  return tid;
34 }
35 
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::TcpRateLinux")
44  .SetGroupName("Internet")
45  .AddTraceSource("TcpRateUpdated",
46  "Tcp rate information has been updated",
48  "ns3::TcpRateLinux::TcpRateUpdated")
49  .AddTraceSource("TcpRateSampleUpdated",
50  "Tcp rate sample has been updated",
52  "ns3::TcpRateLinux::TcpRateSampleUpdated");
53  return tid;
54 }
55 
57 TcpRateLinux::GenerateSample(uint32_t delivered,
58  uint32_t lost,
59  bool is_sack_reneg,
60  uint32_t priorInFlight,
61  const Time& minRtt)
62 {
63  NS_LOG_FUNCTION(this << delivered << lost << is_sack_reneg);
64 
65  /* Clear app limited if bubble is acked and gone. */
67  {
68  NS_LOG_INFO("Updating Rate m_appLimited to zero");
69  m_rate.m_appLimited = 0;
70  }
71 
72  NS_LOG_INFO("Updating RateSample m_ackedSacked=" << delivered << ", m_bytesLoss=" << lost
73  << " and m_priorInFlight" << priorInFlight);
74  m_rateSample.m_ackedSacked = delivered; /* freshly ACKed or SACKed */
75  m_rateSample.m_bytesLoss = lost; /* freshly marked lost */
76  m_rateSample.m_priorInFlight = priorInFlight;
77 
78  /* Return an invalid sample if no timing information is available or
79  * in recovery from loss with SACK reneging. Rate samples taken during
80  * a SACK reneging event may overestimate bw by including packets that
81  * were SACKed before the reneg.
82  */
83  if (m_rateSample.m_priorTime == Seconds(0) || is_sack_reneg)
84  {
85  NS_LOG_INFO("PriorTime is zero, invalidating sample");
89  return m_rateSample;
90  }
91 
92  // LINUX:
93  // /* Model sending data and receiving ACKs as separate pipeline phases
94  // * for a window. Usually the ACK phase is longer, but with ACK
95  // * compression the send phase can be longer. To be safe we use the
96  // * longer phase.
97  // */
98  // auto snd_us = m_rateSample.m_interval; /* send phase */
99  // auto ack_us = Simulator::Now () - m_rateSample.m_prior_mstamp;
100  // m_rateSample.m_interval = std::max (snd_us, ack_us);
101 
104  NS_LOG_INFO("Updating sample interval=" << m_rateSample.m_interval << " and delivered data"
106 
107  /* Normally we expect m_interval >= minRtt.
108  * Note that rate may still be over-estimated when a spuriously
109  * retransmistted skb was first (s)acked because "interval_us"
110  * is under-estimated (up to an RTT). However continuously
111  * measuring the delivery rate during loss recovery is crucial
112  * for connections suffer heavy or prolonged losses.
113  */
114  if (m_rateSample.m_interval < minRtt)
115  {
116  NS_LOG_INFO("Sampling interval is invalid");
118  m_rateSample.m_priorTime = Seconds(0); // To make rate sample invalid
120  return m_rateSample;
121  }
122 
123  /* Record the last non-app-limited or the highest app-limited bw */
126  {
132  NS_LOG_INFO("Updating delivery rate=" << m_rateSample.m_deliveryRate);
133  }
134 
136  return m_rateSample;
137 }
138 
139 void
141  uint32_t in_flight,
142  uint32_t segmentSize,
143  const SequenceNumber32& tailSeq,
144  const SequenceNumber32& nextTx,
145  const uint32_t lostOut,
146  const uint32_t retransOut)
147 {
148  NS_LOG_FUNCTION(this);
149 
150  /* Missing checks from Linux:
151  * - Nothing in sending host's qdisc queues or NIC tx queue. NOT IMPLEMENTED
152  */
153  if (tailSeq - nextTx <
154  static_cast<int32_t>(segmentSize) // We have less than one packet to send.
155  && in_flight < cWnd // We are not limited by CWND.
156  && lostOut <= retransOut) // All lost packets have been retransmitted.
157  {
158  m_rate.m_appLimited = std::max<uint32_t>(m_rate.m_delivered + in_flight, 1);
160  }
161 
162  // m_appLimited will be reset once in GenerateSample, if it has to be.
163  // else
164  // {
165  // m_rate.m_appLimited = 0;
166  // }
167 }
168 
169 void
171 {
172  NS_LOG_FUNCTION(this << skb);
173 
175 
176  if (skbInfo.m_deliveredTime == Time::Max())
177  {
178  return;
179  }
180 
181  m_rate.m_delivered += skb->GetSeqSize();
183 
185  {
191 
193 
195  }
196 
197  /* Mark off the skb delivered once it's taken into account to avoid being
198  * used again when it's cumulatively acked, in case it was SACKed.
199  */
200  skbInfo.m_deliveredTime = Time::Max();
203 }
204 
205 void
206 TcpRateLinux::SkbSent(TcpTxItem* skb, bool isStartOfTransmission)
207 {
208  NS_LOG_FUNCTION(this << skb << isStartOfTransmission);
209 
211 
212  /* In general we need to start delivery rate samples from the
213  * time we received the most recent ACK, to ensure we include
214  * the full time the network needs to deliver all in-flight
215  * packets. If there are no packets in flight yet, then we
216  * know that any ACKs after now indicate that the network was
217  * able to deliver those packets completely in the sampling
218  * interval between now and the next ACK.
219  *
220  * Note that we use the entire window size instead of bytes_in_flight
221  * because the latter is a guess based on RTO and loss-marking
222  * heuristics. We don't want spurious RTOs or loss markings to cause
223  * a spuriously small time interval, causing a spuriously high
224  * bandwidth estimate.
225  */
226  if (isStartOfTransmission)
227  {
228  NS_LOG_INFO("Starting of a transmission at time " << Simulator::Now().GetSeconds());
232  }
233 
236  skbInfo.m_isAppLimited = (m_rate.m_appLimited != 0);
237  skbInfo.m_delivered = m_rate.m_delivered;
238 }
239 
240 std::ostream&
241 operator<<(std::ostream& os, const TcpRateOps::TcpRateConnection& rate)
242 {
243  os << "m_delivered = " << rate.m_delivered << std::endl;
244  os << "m_deliveredTime = " << rate.m_deliveredTime << std::endl;
245  os << "m_firstSentTime = " << rate.m_firstSentTime << std::endl;
246  os << "m_appLimited = " << rate.m_appLimited << std::endl;
247  os << "m_rateDelivered = " << rate.m_rateDelivered << std::endl;
248  os << "m_rateInterval = " << rate.m_rateInterval << std::endl;
249  os << "m_rateAppLimited = " << rate.m_rateAppLimited << std::endl;
250  os << "m_txItemDelivered = " << rate.m_txItemDelivered << std::endl;
251  return os;
252 }
253 
254 std::ostream&
255 operator<<(std::ostream& os, const TcpRateOps::TcpRateSample& sample)
256 {
257  os << "m_deliveryRate = " << sample.m_deliveryRate << std::endl;
258  os << " m_isAppLimited = " << sample.m_isAppLimited << std::endl;
259  os << " m_interval = " << sample.m_interval << std::endl;
260  os << " m_delivered = " << sample.m_delivered << std::endl;
261  os << " m_priorDelivered = " << sample.m_priorDelivered << std::endl;
262  os << " m_priorTime = " << sample.m_priorTime << std::endl;
263  os << " m_sendElapsed = " << sample.m_sendElapsed << std::endl;
264  os << " m_ackElapsed = " << sample.m_ackElapsed << std::endl;
265  os << " m_bytesLoss = " << sample.m_bytesLoss << std::endl;
266  os << " m_priorInFlight= " << sample.m_priorInFlight << std::endl;
267  os << " m_ackedSacked = " << sample.m_ackedSacked << std::endl;
268  return os;
269 }
270 
271 bool
273 {
274  return (lhs.m_deliveryRate == rhs.m_deliveryRate && lhs.m_isAppLimited == rhs.m_isAppLimited &&
275  lhs.m_interval == rhs.m_interval && lhs.m_delivered == rhs.m_delivered &&
276  lhs.m_priorDelivered == rhs.m_priorDelivered && lhs.m_priorTime == rhs.m_priorTime &&
277  lhs.m_sendElapsed == rhs.m_sendElapsed && lhs.m_ackElapsed == rhs.m_ackElapsed);
278 }
279 
280 bool
282 {
283  return (lhs.m_delivered == rhs.m_delivered && lhs.m_deliveredTime == rhs.m_deliveredTime &&
284  lhs.m_firstSentTime == rhs.m_firstSentTime && lhs.m_appLimited == rhs.m_appLimited);
285 }
286 
287 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
A base class which provides memory management and object aggregation.
Definition: object.h:89
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Linux management and generation of Rate information for TCP.
Definition: tcp-rate-ops.h:196
TracedCallback< const TcpRateConnection & > m_rateTrace
Rate trace.
Definition: tcp-rate-ops.h:253
void SkbDelivered(TcpTxItem *skb) override
Update the Rate information after an item is received.
TracedCallback< const TcpRateSample & > m_rateSampleTrace
Rate Sample trace.
Definition: tcp-rate-ops.h:254
TcpRateSample m_rateSample
Rate sample (continuously updated)
Definition: tcp-rate-ops.h:251
TcpRateConnection m_rate
Rate information.
Definition: tcp-rate-ops.h:250
void SkbSent(TcpTxItem *skb, bool isStartOfTransmission) override
Put the rate information inside the sent skb.
const TcpRateSample & GenerateSample(uint32_t delivered, uint32_t lost, bool is_sack_reneg, uint32_t priorInFlight, const Time &minRtt) override
Generate a TcpRateSample to feed a congestion avoidance algorithm.
Definition: tcp-rate-ops.cc:57
static TypeId GetTypeId()
Get the type ID.
Definition: tcp-rate-ops.cc:39
void CalculateAppLimited(uint32_t cWnd, uint32_t in_flight, uint32_t segmentSize, const SequenceNumber32 &tailSeq, const SequenceNumber32 &nextTx, const uint32_t lostOut, const uint32_t retransOut) override
If a gap is detected between sends, it means we are app-limited.
Interface for all operations that involve a Rate monitoring for TCP.
Definition: tcp-rate-ops.h:40
static TypeId GetTypeId()
Get the type ID.
Definition: tcp-rate-ops.cc:30
Item that encloses the application packet and some flags for it.
Definition: tcp-tx-item.h:33
uint32_t GetSeqSize() const
Get the size in the sequence number space.
Definition: tcp-tx-item.cc:61
const Time & GetLastSent() const
Get a reference to the time the packet was sent for the last time.
Definition: tcp-tx-item.cc:91
RateInformation & GetRateInformation()
Get (to modify) the Rate Information of this item.
Definition: tcp-tx-item.cc:97
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:297
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
uint32_t segmentSize
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Information about the connection rate.
Definition: tcp-rate-ops.h:174
uint32_t m_txItemDelivered
The value of delivered when the acked item was sent.
Definition: tcp-rate-ops.h:181
Time m_deliveredTime
Simulator time when m_delivered was last updated.
Definition: tcp-rate-ops.h:176
int32_t m_rateDelivered
The amount of data delivered considered to calculate delivery rate.
Definition: tcp-rate-ops.h:182
uint64_t m_delivered
The total amount of data in bytes delivered so far.
Definition: tcp-rate-ops.h:175
uint32_t m_appLimited
The index of the last transmitted packet marked as application-limited.
Definition: tcp-rate-ops.h:179
bool m_rateAppLimited
Was sample was taken when data is app limited?
Definition: tcp-rate-ops.h:186
Time m_firstSentTime
The send time of the packet that was most recently marked as delivered.
Definition: tcp-rate-ops.h:177
Time m_rateInterval
The value of interval considered to calculate delivery rate.
Definition: tcp-rate-ops.h:184
Rate Sample structure.
Definition: tcp-rate-ops.h:140
Time m_ackElapsed
ACK time interval calculated from the most recent packet delivered.
Definition: tcp-rate-ops.h:150
bool m_isAppLimited
Indicates whether the rate sample is application-limited.
Definition: tcp-rate-ops.h:142
uint32_t m_ackedSacked
The amount of data acked and sacked in the last received ack.
Definition: tcp-rate-ops.h:155
DataRate m_deliveryRate
The delivery rate sample.
Definition: tcp-rate-ops.h:141
uint32_t m_priorInFlight
The value if bytes in flight prior to last received ack.
Definition: tcp-rate-ops.h:154
Time m_sendElapsed
Send time interval calculated from the most recent packet delivered.
Definition: tcp-rate-ops.h:148
Time m_interval
The length of the sampling interval.
Definition: tcp-rate-ops.h:143
uint32_t m_priorDelivered
The delivered count of the most recent packet delivered.
Definition: tcp-rate-ops.h:146
int32_t m_delivered
The amount of data marked as delivered over the sampling interval.
Definition: tcp-rate-ops.h:144
uint32_t m_bytesLoss
The amount of data marked as lost from the most recent ack received.
Definition: tcp-rate-ops.h:152
Time m_priorTime
The delivered time of the most recent packet delivered.
Definition: tcp-rate-ops.h:147
Various rate-related information, can be accessed by TcpRateOps.
Definition: tcp-tx-item.h:88
bool m_isAppLimited
Connection's app limited at the time the packet was sent.
Definition: tcp-tx-item.h:94
uint64_t m_delivered
Connection's delivered data at the time the packet was sent.
Definition: tcp-tx-item.h:89
Time m_deliveredTime
Connection's delivered time at the time the packet was sent.
Definition: tcp-tx-item.h:90
Time m_firstSent
Connection's first sent time at the time the packet was sent.
Definition: tcp-tx-item.h:92