A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-wscaling-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 
22 #define private public
23 #define protected public
24 
25 #include "ns3/socket-factory.h"
26 #include "ns3/tcp-socket-factory.h"
27 #include "ns3/simulator.h"
28 #include "ns3/simple-channel.h"
29 #include "ns3/simple-net-device.h"
30 #include "ns3/drop-tail-queue.h"
31 #include "ns3/config.h"
32 #include "ns3/ipv4-static-routing.h"
33 #include "ns3/ipv4-list-routing.h"
34 #include "ns3/ipv6-static-routing.h"
35 #include "ns3/ipv6-list-routing.h"
36 #include "ns3/node.h"
37 #include "ns3/inet-socket-address.h"
38 #include "ns3/inet6-socket-address.h"
39 #include "ns3/uinteger.h"
40 #include "ns3/log.h"
41 
42 #include "ns3/ipv4-end-point.h"
43 #include "ns3/arp-l3-protocol.h"
44 #include "ns3/ipv4-l3-protocol.h"
45 #include "ns3/ipv6-l3-protocol.h"
46 #include "ns3/icmpv4-l4-protocol.h"
47 #include "ns3/icmpv6-l4-protocol.h"
48 #include "ns3/udp-l4-protocol.h"
49 #include "ns3/tcp-l4-protocol.h"
50 
51 #include "ns3/tcp-socket-base.h"
52 
53 namespace ns3 {
54 
55 NS_LOG_COMPONENT_DEFINE ("WScalingTestSuite");
56 
57 class WScalingTestCase : public TestCase
58 {
59 public:
61  {
66  };
67 
69  uint32_t maxServerBufferSize,
70  uint32_t maxSourceBufferSize, std::string name);
71 
72 private:
73  virtual void DoRun (void);
74  virtual void DoTeardown (void);
75 
76  void SetupDefaultSim (void);
77  //void SetupDefaultSim6 (void);
78 
81 
82  Ptr<SimpleNetDevice> AddSimpleNetDevice (Ptr<Node> node, const char* ipaddr, const char* netmask);
84 
87  void ServerHandleRecv (Ptr<Socket> sock);
88  void ServerHandleSend (Ptr<Socket> sock, uint32_t sendB);
89  void SourceHandleSend (Ptr<Socket> sock, uint32_t available);
90  void SourceHandleRecv (Ptr<Socket> sock);
91 
93 
94  uint32_t m_totalBytes;
96  uint32_t m_sourceReadSize;
98  uint32_t m_serverReadSize;
103 
106 
109 };
110 
112  uint32_t maxServerBufferSize,
113  uint32_t maxSourceBufferSize, std::string name)
114  : TestCase (name)
115 {
116  m_configuration = conf;
117  m_totalBytes = 2000;
118  m_sourceWriteSize = 500;
119  m_sourceReadSize = 500;
120  m_serverWriteSize = 500;
121  m_serverReadSize = 500;
122 
123  m_maxServerBufferSize = maxServerBufferSize;
124  m_maxSourceBufferSize = maxSourceBufferSize;
125 }
126 
127 void
129 {
134 
135 
136 // if (m_useIpv6 == true)
137 // {
138 // SetupDefaultSim6 ();
139 // }
140 // else
141 // {
142  SetupDefaultSim ();
143 // }
144 
145  Simulator::Run ();
146 
148  m_totalBytes,
149  "Source sent all bytes");
150 
152  m_totalBytes,
153  "Server received all bytes");
154 
156  m_totalBytes,
157  "Source received all bytes");
158 }
159 
160 void
162 {
164 }
165 
166 void
168  const Address & addr)
169 {
172 
173  Ptr<TcpSocketBase> b = DynamicCast <TcpSocketBase> (s);
174 
175  if (m_configuration == ENABLED)
176  {
177  NS_TEST_EXPECT_MSG_EQ ((b->m_rWnd.Get ()), m_maxSourceBufferSize,
178  "Miscalculating source window");
179 
180  NS_TEST_EXPECT_MSG_LT_OR_EQ ((b->m_rWnd.Get () >> b->m_rcvScaleFactor),
181  b->m_maxWinSize, "Violating maximum adv window");
182 
183  NS_TEST_EXPECT_MSG_LT_OR_EQ (b->m_rcvScaleFactor, 14,
184  "Violating RFC for max value of the scale factor");
185  }
186  else
187  {
188  NS_TEST_EXPECT_MSG_EQ (b->m_rcvScaleFactor, 0, "Using Window scale while disabled");
189  NS_TEST_EXPECT_MSG_EQ (b->m_sndScaleFactor, 0, "Using Window scale while disabled");
190  NS_TEST_EXPECT_MSG_LT_OR_EQ (b->m_rWnd.Get (), m_source->m_maxWinSize, "Violating max adv window");
191  }
192 }
193 
194 void
196 {
197  while (sock->GetRxAvailable () > 0)
198  {
199  uint32_t toRead = std::min (m_serverReadSize, sock->GetRxAvailable ());
200  Ptr<Packet> p = sock->Recv (toRead, 0);
201 
202  if (p == 0 && sock->GetErrno () != Socket::ERROR_NOTERROR)
203  {
204  NS_FATAL_ERROR ("Server could not read stream at byte " << m_currentServerRxBytes);
205  }
206 
207  m_currentServerRxBytes += p->GetSize ();
208 
209  ServerHandleSend (sock, sock->GetTxAvailable ());
210  }
211 }
212 
213 void
215 {
216  Ptr<TcpSocketBase> b = DynamicCast <TcpSocketBase> (sock);
217 
218  if (m_configuration == ENABLED)
219  {
220  NS_TEST_EXPECT_MSG_EQ ((b->m_rWnd.Get ()), m_maxServerBufferSize,
221  "Miscalculating server window " << (int)b->m_rcvScaleFactor);
222 
223  NS_TEST_EXPECT_MSG_LT_OR_EQ ((b->m_rWnd.Get () >> b->m_rcvScaleFactor),
224  b->m_maxWinSize, "Violating maximum adv window");
225 
226  NS_TEST_EXPECT_MSG_LT_OR_EQ (b->m_rcvScaleFactor, 14,
227  "Violating RFC for max value of the scale factor");
228  }
229  else
230  {
231  NS_TEST_EXPECT_MSG_EQ (b->m_rcvScaleFactor, 0, "Using Window scale while disabled");
232  NS_TEST_EXPECT_MSG_EQ (b->m_sndScaleFactor, 0, "Using Window scale while disabled");
233  NS_TEST_EXPECT_MSG_LT_OR_EQ (b->m_rWnd.Get (), m_server->m_maxWinSize, "Violating max adv window");
234  }
235 }
236 
237 void
239 {
241  {
243  uint32_t toSend = std::min (left, sock->GetTxAvailable ());
244  toSend = std::min (toSend, m_serverWriteSize);
245 
246  Ptr<Packet> p = Create<Packet> (toSend);
247 
248  int sent = sock->Send (p);
249 
250  NS_TEST_EXPECT_MSG_EQ ((sent != -1), true, "Server error during send ?");
251 
252  m_currentServerTxBytes += sent;
253  }
254 
256  {
257  sock->Close ();
258  }
259 }
260 
261 void
263 {
264  while (sock->GetTxAvailable () > 0 && m_currentSourceTxBytes < m_totalBytes)
265  {
266  uint32_t left = m_totalBytes - m_currentSourceTxBytes;
267  uint32_t toSend = std::min (left, sock->GetTxAvailable ());
268  toSend = std::min (toSend, m_sourceWriteSize);
269 
270  Ptr<Packet> p = Create<Packet> (toSend);
271 
272  int sent = sock->Send (p);
273  NS_TEST_EXPECT_MSG_EQ ((sent != -1), true, "Error during send ?");
274  m_currentSourceTxBytes += sent;
275  }
276 }
277 
278 void
280 {
281  while (sock->GetRxAvailable () > 0 && m_currentSourceRxBytes < m_totalBytes)
282  {
283  uint32_t toRead = std::min (m_sourceReadSize, sock->GetRxAvailable ());
284  Ptr<Packet> p = sock->Recv (toRead, 0);
285 
286  if (p == 0 && sock->GetErrno () != Socket::ERROR_NOTERROR)
287  {
288  NS_FATAL_ERROR ("Source could not read stream at byte " << m_currentSourceRxBytes);
289  }
290 
291  m_currentSourceRxBytes += p->GetSize ();
292  }
293 
295  {
296  sock->Close ();
297  }
298 }
299 
300 Ptr<Node>
302 {
303  Ptr<Node> node = CreateObject<Node> ();
304  //ARP
305  Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> ();
306  node->AggregateObject (arp);
307  //IPV4
308  Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> ();
309  //Routing for Ipv4
310  Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> ();
311  ipv4->SetRoutingProtocol (ipv4Routing);
312  Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> ();
313  ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
314  node->AggregateObject (ipv4);
315  //ICMP
316  Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> ();
317  node->AggregateObject (icmp);
318  //UDP
319  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
320  node->AggregateObject (udp);
321  //TCP
322  Ptr<TcpL4Protocol> tcp = CreateObject<TcpL4Protocol> ();
323  node->AggregateObject (tcp);
324  return node;
325 }
326 
328 WScalingTestCase::AddSimpleNetDevice (Ptr<Node> node, const char* ipaddr, const char* netmask)
329 {
330  Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
331  dev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
332  node->AddDevice (dev);
333  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
334  uint32_t ndid = ipv4->AddInterface (dev);
335  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address (ipaddr), Ipv4Mask (netmask));
336  ipv4->AddAddress (ndid, ipv4Addr);
337  ipv4->SetUp (ndid);
338  return dev;
339 }
340 
341 void
343 {
344  const char* netmask = "255.255.255.0";
345  const char* ipaddr0 = "192.168.1.1";
346  const char* ipaddr1 = "192.168.1.2";
347  Ptr<Node> node0 = CreateInternetNode ();
348  Ptr<Node> node1 = CreateInternetNode ();
349  Ptr<SimpleNetDevice> dev0 = AddSimpleNetDevice (node0, ipaddr0, netmask);
350  Ptr<SimpleNetDevice> dev1 = AddSimpleNetDevice (node1, ipaddr1, netmask);
351 
352  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
353  dev0->SetChannel (channel);
354  dev1->SetChannel (channel);
355 
356  Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
357  Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
358 
359  m_server = DynamicCast<TcpSocketBase> (sockFactory0->CreateSocket ());
360  m_source = DynamicCast<TcpSocketBase> (sockFactory1->CreateSocket ());
361 
362  NS_ASSERT (m_server != 0);
363  NS_ASSERT (m_source != 0);
364 
365  switch (m_configuration)
366  {
367  case DISABLED:
368  m_server->SetAttribute ("WindowScaling", BooleanValue (false));
369  m_source->SetAttribute ("WindowScaling", BooleanValue (false));
370  break;
371 
372  case ENABLED_CLIENT:
373  m_server->SetAttribute ("WindowScaling", BooleanValue (false));
374  m_source->SetAttribute ("WindowScaling", BooleanValue (true));
375  break;
376 
377  case ENABLED_SERVER:
378  m_server->SetAttribute ("WindowScaling", BooleanValue (true));
379  m_source->SetAttribute ("WindowScaling", BooleanValue (false));
380  break;
381 
382  case ENABLED:
383  m_server->SetAttribute ("WindowScaling", BooleanValue (true));
384  m_source->SetAttribute ("WindowScaling", BooleanValue (true));
385  break;
386  }
387 
388  m_server->m_rxBuffer.SetMaxBufferSize (m_maxServerBufferSize);
389  m_source->m_rxBuffer.SetMaxBufferSize (m_maxSourceBufferSize);
390 
391  uint16_t port = 50000;
392  InetSocketAddress serverlocaladdr (Ipv4Address::GetAny (), port);
393  InetSocketAddress serverremoteaddr (Ipv4Address (ipaddr0), port);
394 
395  m_server->Bind (serverlocaladdr);
396  m_server->Listen ();
397  m_server->SetAcceptCallback (MakeNullCallback<bool, Ptr< Socket >, const Address &> (),
399 
400  m_source->SetRecvCallback (MakeCallback (&WScalingTestCase::SourceHandleRecv, this));
401  m_source->SetSendCallback (MakeCallback (&WScalingTestCase::SourceHandleSend, this));
402  m_source->SetConnectCallback (MakeCallback (&WScalingTestCase::SourceHandleConnectionCreated, this),
403  MakeNullCallback<void, Ptr<Socket> > ());
404 
405  m_source->Connect (serverremoteaddr);
406 }
407 
408 static class TcpWScalingTestSuite : public TestSuite
409 {
410 public:
412  : TestSuite ("tcp-wscaling", UNIT)
413  {
414  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED, 200000, 65535, "WS only server"), TestCase::QUICK);
415  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED, 65535, 65535, "Window scaling not used, all enabled"), TestCase::QUICK);
416  AddTestCase (new WScalingTestCase (WScalingTestCase::DISABLED, 65535, 65535, "WS disabled"), TestCase::QUICK);
417  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED_CLIENT, 65535, 65535, "WS enabled client"), TestCase::QUICK);
418  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED_SERVER, 65535, 65535, "WS disabled client"), TestCase::QUICK);
419 
420  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED, 65535, 200000, "WS only client"), TestCase::QUICK);
421  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED, 131072, 65535, "WS only server"), TestCase::QUICK);
422  AddTestCase (new WScalingTestCase (WScalingTestCase::ENABLED, 65535, 131072, "WS only client"), TestCase::QUICK);
423  }
424 
426 
427 } // namespace ns3
428 
Ptr< TcpSocketBase > m_server
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)
API to create TCP socket instances.
void SourceHandleSend(Ptr< Socket > sock, uint32_t available)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
A suite of tests to run.
Definition: test.h:1289
Ptr< SimpleNetDevice > AddSimpleNetDevice6(Ptr< Node > node, Ipv6Address ipaddr, Ipv6Prefix prefix)
#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
ns3::TcpWScalingTestSuite g_tcpWScalingTestSuite
#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
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
Configuration m_configuration
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
Ptr< SampleEmitter > s
Ptr< Node > CreateInternetNode6(void)
Ptr< Node > CreateInternetNode(void)
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
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
void SourceHandleConnectionCreated(Ptr< Socket > sock)
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
static Mac48Address ConvertFrom(const Address &address)
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< TcpSocketBase > m_source
Ptr< SimpleNetDevice > AddSimpleNetDevice(Ptr< Node > node, const char *ipaddr, const char *netmask)
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void ServerHandleSend(Ptr< Socket > sock, uint32_t sendB)
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
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 DoRun(void)
Implementation to actually run this TestCase.
WScalingTestCase(WScalingTestCase::Configuration conf, uint32_t maxServerBufferSize, uint32_t maxSourceBufferSize, std::string name)
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
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 uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
Ptr< T > GetObject(void) const
Definition: object.h:362
void SourceHandleRecv(Ptr< Socket > sock)
#define NS_TEST_EXPECT_MSG_LT_OR_EQ(actual, limit, msg)
Test that an actual value is less than or equal to a limit and report if not.
Definition: test.h:890
void ServerHandleRecv(Ptr< Socket > sock)
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.