A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-timestamp-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Natale Patriciello <natale.patriciello@gmail.com>
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  */
19 
20 #include "ns3/test.h"
21 #include "ns3/socket-factory.h"
22 #include "ns3/tcp-socket-factory.h"
23 #include "ns3/simulator.h"
24 #include "ns3/simple-channel.h"
25 #include "ns3/simple-net-device.h"
26 #include "ns3/drop-tail-queue.h"
27 #include "ns3/config.h"
28 #include "ns3/ipv4-static-routing.h"
29 #include "ns3/ipv4-list-routing.h"
30 #include "ns3/ipv6-static-routing.h"
31 #include "ns3/ipv6-list-routing.h"
32 #include "ns3/node.h"
33 #include "ns3/inet-socket-address.h"
34 #include "ns3/inet6-socket-address.h"
35 #include "ns3/uinteger.h"
36 #include "ns3/log.h"
37 #include "ns3/tcp-socket-base.h"
38 
39 #include "ns3/ipv4-end-point.h"
40 #include "ns3/arp-l3-protocol.h"
41 #include "ns3/ipv4-l3-protocol.h"
42 #include "ns3/ipv6-l3-protocol.h"
43 #include "ns3/icmpv4-l4-protocol.h"
44 #include "ns3/icmpv6-l4-protocol.h"
45 #include "ns3/udp-l4-protocol.h"
46 #include "ns3/tcp-l4-protocol.h"
47 
48 #include "../src/internet/model/tcp-option-ts.h"
49 
50 namespace ns3 {
51 
52 NS_LOG_COMPONENT_DEFINE ("TimestampTestSuite");
53 
55 {
56 public:
58  {
63  };
64 
66 
67 private:
68  virtual void DoRun (void);
69  virtual void DoTeardown (void);
70 
71  void SetupDefaultSim (void);
72  //void SetupDefaultSim6 (void);
73 
76  Ptr<SimpleNetDevice> AddSimpleNetDevice (Ptr<Node> node, const char* ipaddr, const char* netmask);
79  void ServerHandleRecv (Ptr<Socket> sock);
80  void ServerHandleSend (Ptr<Socket> sock, uint32_t sendB);
81  void SourceHandleSend (Ptr<Socket> sock, uint32_t available);
82  void SourceHandleRecv (Ptr<Socket> sock);
83 
85 
86  uint32_t m_totalBytes;
88  uint32_t m_sourceReadSize;
90  uint32_t m_serverReadSize;
95 };
96 
98  : TestCase ("Testing the TCP Timestamp option")
99 {
100  m_configuration = conf;
101  m_totalBytes = 2000;
102  m_sourceWriteSize = 500;
103  m_sourceReadSize = 500;
104  m_serverWriteSize = 500;
105  m_serverReadSize = 500;
106 }
107 
108 void
110 {
115 
116 
117 // if (m_useIpv6 == true)
118 // {
119 // SetupDefaultSim6 ();
120 // }
121 // else
122 // {
123  SetupDefaultSim ();
124 // }
125 
126  Simulator::Run ();
127 
128  NS_TEST_EXPECT_MSG_EQ (m_currentSourceTxBytes, m_totalBytes, "Source sent all bytes");
129  NS_TEST_EXPECT_MSG_EQ (m_currentServerRxBytes, m_totalBytes, "Server received all bytes");
130  NS_TEST_EXPECT_MSG_EQ (m_currentSourceRxBytes, m_totalBytes, "Source received all bytes");
131 }
132 
133 void
135 {
137 }
138 
139 void
141 {
144 }
145 
146 void
148 {
149  while (sock->GetRxAvailable () > 0)
150  {
151  uint32_t toRead = std::min (m_serverReadSize, sock->GetRxAvailable ());
152  Ptr<Packet> p = sock->Recv (toRead, 0);
153 
154  if (p == 0 && sock->GetErrno () != Socket::ERROR_NOTERROR)
155  {
156  NS_FATAL_ERROR ("Server could not read stream at byte " << m_currentServerRxBytes);
157  }
158 
159  m_currentServerRxBytes += p->GetSize ();
160 
161  ServerHandleSend (sock, sock->GetTxAvailable ());
162  }
163 }
164 
165 void
167 {
169  {
171  uint32_t toSend = std::min (left, sock->GetTxAvailable ());
172  toSend = std::min (toSend, m_serverWriteSize);
173 
174  Ptr<Packet> p = Create<Packet> (toSend);
175 
176  int sent = sock->Send (p);
177 
178  NS_TEST_EXPECT_MSG_EQ ((sent != -1), true, "Server error during send ?");
179 
180  m_currentServerTxBytes += sent;
181  }
182 
184  {
185  sock->Close ();
186  }
187 }
188 
189 void
191 {
192  while (sock->GetTxAvailable () > 0 && m_currentSourceTxBytes < m_totalBytes)
193  {
194  uint32_t left = m_totalBytes - m_currentSourceTxBytes;
195  uint32_t toSend = std::min (left, sock->GetTxAvailable ());
196  toSend = std::min (toSend, m_sourceWriteSize);
197 
198  Ptr<Packet> p = Create<Packet> (toSend);
199 
200  int sent = sock->Send (p);
201  NS_TEST_EXPECT_MSG_EQ ((sent != -1), true, "Error during send ?");
202  m_currentSourceTxBytes += sent;
203  }
204 }
205 
206 void
208 {
209  while (sock->GetRxAvailable () > 0 && m_currentSourceRxBytes < m_totalBytes)
210  {
211  uint32_t toRead = std::min (m_sourceReadSize, sock->GetRxAvailable ());
212  Ptr<Packet> p = sock->Recv (toRead, 0);
213 
214  if (p == 0 && sock->GetErrno () != Socket::ERROR_NOTERROR)
215  {
216  NS_FATAL_ERROR ("Source could not read stream at byte " << m_currentSourceRxBytes);
217  }
218 
219  m_currentSourceRxBytes += p->GetSize ();
220  }
221 
223  {
224  sock->Close ();
225  }
226 }
227 
228 Ptr<Node>
230 {
231  Ptr<Node> node = CreateObject<Node> ();
232  //ARP
233  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
234  node->AggregateObject (arp);
235  //IPV4
236  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
237  //Routing for Ipv4
238  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
239  ipv4->SetRoutingProtocol (ipv4Routing);
240  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
241  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
242  node->AggregateObject (ipv4);
243  //ICMP
244  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
245  node->AggregateObject (icmp);
246  //UDP
247  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
248  node->AggregateObject (udp);
249  //TCP
250  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
251  node->AggregateObject (tcp);
252  return node;
253 }
254 
256 TimestampTestCase::AddSimpleNetDevice (Ptr<Node> node, const char* ipaddr, const char* netmask)
257 {
258  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
259  dev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
260  node->AddDevice (dev);
261  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
262  uint32_t ndid = ipv4->AddInterface (dev);
263  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address (ipaddr), Ipv4Mask (netmask));
264  ipv4->AddAddress (ndid, ipv4Addr);
265  ipv4->SetUp (ndid);
266  return dev;
267 }
268 
269 void
271 {
272  const char* netmask = "255.255.255.0";
273  const char* ipaddr0 = "192.168.1.1";
274  const char* ipaddr1 = "192.168.1.2";
275  Ptr<Node> node0 = CreateInternetNode ();
276  Ptr<Node> node1 = CreateInternetNode ();
277  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice (node0, ipaddr0, netmask);
278  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice (node1, ipaddr1, netmask);
279 
280  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
281  dev0->SetChannel (channel);
282  dev1->SetChannel (channel);
283 
284  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
285  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
286 
287  Ptr<TcpSocketBase> server = DynamicCast<TcpSocketBase> (sockFactory0->CreateSocket ());
288  Ptr<TcpSocketBase> source = DynamicCast<TcpSocketBase> (sockFactory1->CreateSocket ());
289 
290  NS_ASSERT (server != 0);
291  NS_ASSERT (source != 0);
292 
293  switch (m_configuration)
294  {
295  case DISABLED:
296  server->SetAttribute ("Timestamp", BooleanValue (false));
297  source->SetAttribute ("Timestamp", BooleanValue (false));
298  break;
299 
300  case ENABLED_CLIENT:
301  server->SetAttribute ("Timestamp", BooleanValue (false));
302  source->SetAttribute ("Timestamp", BooleanValue (true));
303  break;
304 
305  case ENABLED_SERVER:
306  server->SetAttribute ("Timestamp", BooleanValue (true));
307  source->SetAttribute ("Timestamp", BooleanValue (false));
308  break;
309 
310  case ENABLED:
311  server->SetAttribute ("Timestamp", BooleanValue (true));
312  source->SetAttribute ("Timestamp", BooleanValue (true));
313  break;
314  }
315 
316  uint16_t port = 50000;
317  InetSocketAddress serverlocaladdr (Ipv4Address::GetAny (), port);
318  InetSocketAddress serverremoteaddr (Ipv4Address (ipaddr0), port);
319 
320  server->Bind (serverlocaladdr);
321  server->Listen ();
322  server->SetAcceptCallback (MakeNullCallback<bool, Ptr< Socket >, const Address &> (),
324 
325  source->SetRecvCallback (MakeCallback (&TimestampTestCase::SourceHandleRecv, this));
326  source->SetSendCallback (MakeCallback (&TimestampTestCase::SourceHandleSend, this));
327 
328  source->Connect (serverremoteaddr);
329 }
330 
332 {
333 public:
334  TimestampValueTestCase (double startTime, double timeToWait,
335  std::string name);
336 
337 private:
338  virtual void DoRun (void);
339  virtual void DoTeardown (void);
340 
341  void Check ();
342  void Init ();
343 
344  double m_startTime;
345  double m_timeToWait;
346  double m_initValue;
347 };
348 
350  double timeToWait,
351  std::string name)
352  : TestCase (name)
353 {
355  m_timeToWait = timeToWait;
356 }
357 
358 void
360 {
365 
366  Simulator::Run ();
367 }
368 
369 void
371 {
373 }
374 
375 void
377 {
379 }
380 
381 void
383 {
384  uint32_t lastValue = TcpOptionTS::NowToTsValue ();
385 
387  MilliSeconds (1), "Different TS values");
388 
390  MilliSeconds (1), "Estimating Wrong RTT");
391 }
392 
393 static class TcpTimestampTestSuite : public TestSuite
394 {
395 public:
397  : TestSuite ("tcp-timestamp", UNIT)
398  {
403  AddTestCase (new TimestampValueTestCase (0.0, 0.01, "Value Check"), TestCase::QUICK);
404  AddTestCase (new TimestampValueTestCase (3.0, 0.5, "Value Check"), TestCase::QUICK);
405  AddTestCase (new TimestampValueTestCase (5.5, 1.0, "Value Check"), TestCase::QUICK);
406  AddTestCase (new TimestampValueTestCase (6.0, 2.0, "Value Check"), TestCase::QUICK);
407  AddTestCase (new TimestampValueTestCase (2.4, 0.7, "Value Check"), TestCase::QUICK);
408 
409  }
410 
412 
413 } // namespace ns3
an Inet address class
static Ipv4Address GetAny(void)
Hold a bool native type.
Definition: boolean.h:38
void ServerHandleConnectionCreated(Ptr< Socket > s, const Address &addr)
static Time ElapsedTimeFromTsValue(uint32_t echoTime)
Estimate the Time elapsed from a TS echo value.
API to create TCP socket instances.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
A suite of tests to run.
Definition: test.h:1289
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:853
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:265
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
Callback< R > MakeNullCallback(void)
Definition: callback.h:1429
encapsulates test code
Definition: test.h:1113
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
Ptr< Node > CreateInternetNode6(void)
ns3::TcpTimestampTestSuite g_tcpTimestampTestSuite
virtual enum Socket::SocketErrno GetErrno(void) const =0
Get last error number.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
TimestampTestCase(TimestampTestCase::Configuration conf)
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
double startTime
TimestampValueTestCase(double startTime, double timeToWait, std::string name)
Ptr< SampleEmitter > s
Ptr< SimpleNetDevice > AddSimpleNetDevice(Ptr< Node > node, const char *ipaddr, const char *netmask)
void ServerHandleSend(Ptr< Socket > sock, uint32_t sendB)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1283
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:355
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SourceHandleRecv(Ptr< Socket > sock)
static Mac48Address ConvertFrom(const Address &address)
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Ptr< Node > CreateInternetNode(void)
void SourceHandleSend(Ptr< Socket > sock, uint32_t available)
void ServerHandleRecv(Ptr< Socket > sock)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition: socket.cc:120
static uint32_t NowToTsValue()
Return an uint32_t value which represent "now".
Fast test.
Definition: test.h:1121
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:120
a class to store IPv4 address information on an interface
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Ptr< SimpleNetDevice > AddSimpleNetDevice6(Ptr< Node > node, Ipv6Address ipaddr, Ipv6Prefix prefix)
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
virtual void DoRun(void)
Implementation to actually run this TestCase.
This test suite implements a Unit Test.
Definition: test.h:1299
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
Ptr< T > GetObject(void) const
Definition: object.h:362
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.