A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
global-routing-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #include <vector>
18 #include "ns3/boolean.h"
19 #include "ns3/config.h"
20 #include "ns3/csma-helper.h"
21 #include "ns3/flow-monitor.h"
22 #include "ns3/flow-monitor-helper.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/internet-stack-helper.h"
25 #include "ns3/ipv4-address-helper.h"
26 #include "ns3/ipv4-global-routing-helper.h"
27 #include "ns3/ipv4-static-routing-helper.h"
28 #include "ns3/node.h"
29 #include "ns3/node-container.h"
30 #include "ns3/on-off-helper.h"
31 #include "ns3/packet.h"
32 #include "ns3/packet-sink-helper.h"
33 #include "ns3/packet-sink.h"
34 #include "ns3/packet-socket-helper.h"
35 #include "ns3/packet-socket-address.h"
36 #include "ns3/csma-net-device.h"
37 #include "ns3/point-to-point-helper.h"
38 #include "ns3/pointer.h"
39 #include "ns3/simple-channel.h"
40 #include "ns3/simulator.h"
41 #include "ns3/string.h"
42 #include "ns3/test.h"
43 #include "ns3/uinteger.h"
44 #include "ns3/ipv4-packet-info-tag.h"
45 
46 using namespace ns3;
47 
49 {
50 public:
52  virtual ~DynamicGlobalRoutingTestCase ();
53 
54 private:
55  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
56  void HandleRead (Ptr<Socket>);
57  virtual void DoRun (void);
58  int m_count;
59  std::vector<uint8_t> m_firstInterface;
60  std::vector<uint8_t> m_secondInterface;
61 };
62 
63 // Add some help text to this case to describe what it is intended to test
65  : TestCase ("Dynamic global routing example"), m_count (0)
66 {
67  m_firstInterface.resize (16);
68  m_secondInterface.resize (16);
69 }
70 
72 {
73 }
74 
75 void
77 {
79  bool found;
80  found = p->PeekPacketTag (tag);
81  uint8_t now = static_cast<uint8_t> (Simulator::Now ().GetSeconds ());
82  if (found)
83  {
84  ;
85  }
86  m_firstInterface[now]++;
87  m_count++;
88 }
89 
90 void
92 {
93  Ptr<Packet> packet;
94  Address from;
95  while ((packet = socket->RecvFrom (from)))
96  {
97  if (packet->GetSize () == 0)
98  { //EOF
99  break;
100  }
101  Ipv4PacketInfoTag tag;
102  bool found;
103  found = packet->PeekPacketTag (tag);
104  uint8_t now = static_cast<uint8_t> (Simulator::Now ().GetSeconds ());
105  if (found)
106  {
107  if (tag.GetRecvIf () == 1)
108  {
109  m_firstInterface[now]++;
110  }
111  if (tag.GetRecvIf () == 2)
112  {
113  m_secondInterface[now]++;
114  }
115  m_count++;
116  }
117  }
118 }
119 
120 // Test derived from examples/routing/dynamic-global-routing.cc
121 //
122 // Network topology
123 //
124 // n0
125 // \ p-p
126 // \ (shared csma/cd)
127 // n2 -------------------------n3
128 // / | |
129 // / p-p n4 n5 ---------- n6
130 // n1 p-p
131 // | |
132 // ----------------------------------------
133 // p-p
134 //
135 // Test that for node n6, the interface facing n5 receives packets at
136 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
137 // facing n1 receives packets at times (2-4), (6-8), (12-13)
138 //
139 void
141 {
142  // The below value configures the default behavior of global routing.
143  // By default, it is disabled. To respond to interface events, set to true
144  Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
145 
146  NodeContainer c;
147  c.Create (7);
148  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
149  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
150  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
151  NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
152  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
153 
154  InternetStackHelper internet;
155  internet.Install (c);
156 
157  // We create the channels first without any IP addressing information
158  PointToPointHelper p2p;
159  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
160  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
161  NetDeviceContainer d0d2 = p2p.Install (n0n2);
162  NetDeviceContainer d1d6 = p2p.Install (n1n6);
163 
164  NetDeviceContainer d1d2 = p2p.Install (n1n2);
165 
166  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
167  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
168  NetDeviceContainer d5d6 = p2p.Install (n5n6);
169 
170  // We create the channels first without any IP addressing information
171  CsmaHelper csma;
172  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
173  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
174  NetDeviceContainer d2345 = csma.Install (n2345);
175 
176  // Later, we add IP addresses.
177  Ipv4AddressHelper ipv4;
178  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
179  ipv4.Assign (d0d2);
180 
181  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
182  ipv4.Assign (d1d2);
183 
184  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
185  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
186 
187  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
188  ipv4.Assign (d2345);
189 
190  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
191  Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
192 
193  // Create router nodes, initialize routing database and set up the routing
194  // tables in the nodes.
195  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
196 
197  // Create the OnOff application to send UDP datagrams of size
198  // 210 bytes at a rate of 448 Kb/s
199  uint16_t port = 9; // Discard port (RFC 863)
200  OnOffHelper onoff ("ns3::UdpSocketFactory",
201  InetSocketAddress (i5i6.GetAddress (1), port));
202  onoff.SetConstantRate (DataRate ("2kbps"));
203  onoff.SetAttribute ("PacketSize", UintegerValue (50));
204 
205  ApplicationContainer apps = onoff.Install (c.Get (1));
206  apps.Start (Seconds (1.0));
207  apps.Stop (Seconds (10.0));
208 
209  // Create a second OnOff application to send UDP datagrams of size
210  // 210 bytes at a rate of 448 Kb/s
211  OnOffHelper onoff2 ("ns3::UdpSocketFactory",
212  InetSocketAddress (i1i6.GetAddress (1), port));
213  onoff2.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
214  onoff2.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
215  onoff2.SetAttribute ("DataRate", StringValue ("2kbps"));
216  onoff2.SetAttribute ("PacketSize", UintegerValue (50));
217 
218  ApplicationContainer apps2 = onoff2.Install (c.Get (1));
219  apps2.Start (Seconds (11.0));
220  apps2.Stop (Seconds (16.0));
221 
222  // Create an optional packet sink to receive these packets
223  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
224  Ptr<Socket> sink2 = Socket::CreateSocket (c.Get (6), tid);
225  sink2->Bind (Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
226  sink2->Listen ();
227  sink2->ShutdownSend ();
228 
229  sink2->SetRecvPktInfo (true);
231 
232  Ptr<Node> n1 = c.Get (1);
233  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
234  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
235  // then the next p2p is numbered 2
236  uint32_t ipv4ifIndex1 = 2;
237 
238  // Trace receptions
239  Config::Connect ("/NodeList/6/ApplicationList/*/$ns3::PacketSink/Rx",
241 
242  Simulator::Schedule (Seconds (2),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
243  Simulator::Schedule (Seconds (4),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
244 
245  Ptr<Node> n6 = c.Get (6);
246  Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4> ();
247  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
248  // then the next p2p is numbered 2
249  uint32_t ipv4ifIndex6 = 2;
250  Simulator::Schedule (Seconds (6),&Ipv4::SetDown,ipv46, ipv4ifIndex6);
251  Simulator::Schedule (Seconds (8),&Ipv4::SetUp,ipv46, ipv4ifIndex6);
252 
253  Simulator::Schedule (Seconds (12),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
254  Simulator::Schedule (Seconds (14),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
255 
256  Simulator::Run ();
257 
258  NS_TEST_ASSERT_MSG_EQ (m_count, 68, "Dynamic global routing did not deliver all packets");
259 // Test that for node n6, the interface facing n5 receives packets at
260 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
261 // facing n1 receives packets at times (2-4), (6-8), (12-13)
262  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[1], 4, "Dynamic global routing did not deliver all packets");
263  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[2], 5, "Dynamic global routing did not deliver all packets");
264  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[3], 5, "Dynamic global routing did not deliver all packets");
265  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[4], 5, "Dynamic global routing did not deliver all packets");
266  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[5], 5, "Dynamic global routing did not deliver all packets");
267  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[6], 5, "Dynamic global routing did not deliver all packets");
268  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[7], 5, "Dynamic global routing did not deliver all packets");
269  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[8], 5, "Dynamic global routing did not deliver all packets");
270  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[9], 5, "Dynamic global routing did not deliver all packets");
271  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[10], 0, "Dynamic global routing did not deliver all packets");
272  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[11], 4, "Dynamic global routing did not deliver all packets");
273  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[12], 5, "Dynamic global routing did not deliver all packets");
274  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[13], 5, "Dynamic global routing did not deliver all packets");
275  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[14], 5, "Dynamic global routing did not deliver all packets");
276  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[15], 5, "Dynamic global routing did not deliver all packets");
277  Simulator::Destroy ();
278 }
279 
281 {
282 public:
285 
286 private:
287  virtual void DoRun (void);
288 };
289 
290 // Add some help text to this case to describe what it is intended to test
292  : TestCase ("Slash 32 global routing example")
293 {
294 }
295 
297 {
298 }
299 
300 // Test program for this 3-router scenario, using global routing
301 //
302 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
303 //
304 void
306 {
307  Ptr<Node> nA = CreateObject<Node> ();
308  Ptr<Node> nB = CreateObject<Node> ();
309  Ptr<Node> nC = CreateObject<Node> ();
310 
311  NodeContainer c = NodeContainer (nA, nB, nC);
312 
313  InternetStackHelper internet;
314  internet.Install (c);
315 
316  // Point-to-point links
317  NodeContainer nAnB = NodeContainer (nA, nB);
318  NodeContainer nBnC = NodeContainer (nB, nC);
319 
320  // We create the channels first without any IP addressing information
321  PointToPointHelper p2p;
322  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
323  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
324  NetDeviceContainer dAdB = p2p.Install (nAnB);
325 
326  NetDeviceContainer dBdC = p2p.Install (nBnC);;
327 
328  Ptr<CsmaNetDevice> deviceA = CreateObject<CsmaNetDevice> ();
329  deviceA->SetAddress (Mac48Address::Allocate ());
330  nA->AddDevice (deviceA);
331 
332  Ptr<CsmaNetDevice> deviceC = CreateObject<CsmaNetDevice> ();
333  deviceC->SetAddress (Mac48Address::Allocate ());
334  nC->AddDevice (deviceC);
335 
336  // Later, we add IP addresses.
337  Ipv4AddressHelper ipv4;
338  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
339  Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);
340 
341  ipv4.SetBase ("10.1.1.4", "255.255.255.252");
342  Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);
343 
344  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();
345  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();
346 
347  int32_t ifIndexA = ipv4A->AddInterface (deviceA);
348  int32_t ifIndexC = ipv4C->AddInterface (deviceC);
349 
350  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("255.255.255.255"));
351  ipv4A->AddAddress (ifIndexA, ifInAddrA);
352  ipv4A->SetMetric (ifIndexA, 1);
353  ipv4A->SetUp (ifIndexA);
354 
355  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("255.255.255.255"));
356  ipv4C->AddAddress (ifIndexC, ifInAddrC);
357  ipv4C->SetMetric (ifIndexC, 1);
358  ipv4C->SetUp (ifIndexC);
359 
360  // Create router nodes, initialize routing database and set up the routing
361  // tables in the nodes.
362  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
363 
364  // Create the OnOff application to send UDP datagrams of size
365  // 210 bytes at a rate of 448 Kb/s
366  uint16_t port = 9; // Discard port (RFC 863)
367  OnOffHelper onoff ("ns3::UdpSocketFactory",
368  Address (InetSocketAddress (ifInAddrC.GetLocal (), port)));
369  onoff.SetConstantRate (DataRate (6000));
370  ApplicationContainer apps = onoff.Install (nA);
371  apps.Start (Seconds (1.0));
372  apps.Stop (Seconds (10.0));
373 
374  // Create a packet sink to receive these packets
375  PacketSinkHelper sink ("ns3::UdpSocketFactory",
376  Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
377  apps = sink.Install (nC);
378  apps.Start (Seconds (1.0));
379  apps.Stop (Seconds (10.0));
380 
381  Simulator::Run ();
382  // Check that we received 13 * 512 = 6656 bytes
383  Ptr<PacketSink> sinkPtr = DynamicCast <PacketSink> (apps.Get (0));
384  NS_TEST_ASSERT_MSG_EQ (sinkPtr->GetTotalRx (), 6656, "Static routing with /32 did not deliver all packets");
385  Simulator::Destroy ();
386 }
387 
388 
390 {
391 public:
393 };
394 
396  : TestSuite ("global-routing", UNIT)
397 {
398  AddTestCase (new DynamicGlobalRoutingTestCase, TestCase::QUICK);
399  AddTestCase (new GlobalRoutingSlash32TestCase, TestCase::QUICK);
400 }
401 
402 // Do not forget to allocate an instance of this TestSuite
holds a vector of ns3::Application pointers.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Hold a bool native type.
Definition: boolean.h:38
NodeContainer n1n2
Definition: red-tests.cc:63
holds a vector of std::pair of Ptr and interface index.
Ipv4Address GetLocal(void) const
Get the local address.
hold variables of type string
Definition: string.h:18
NetDeviceContainer Install(NodeContainer c)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
virtual int ShutdownSend(void)=0
A suite of tests to run.
Definition: test.h:1105
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:929
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
void SetUp(char *deviceName)
virtual int Listen(void)=0
Listen for incoming connections.
Class for representing data rates.
Definition: data-rate.h:71
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:357
double GetSeconds(void) const
Definition: nstime.h:272
std::vector< uint8_t > m_secondInterface
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:863
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void SetUp(uint32_t interface)=0
uint32_t GetRecvIf(void) const
Get the tag's receiving interface.
Hold an unsigned integer type.
Definition: uinteger.h:46
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:148
holds a vector of ns3::NetDevice pointers
virtual void DoRun(void)
Implementation to actually run this TestCase.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
NodeContainer n0n2
Definition: red-tests.cc:62
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
uint32_t GetTotalRx() const
Definition: packet-sink.cc:72
static GlobalRoutingTestSuite globalRoutingTestSuite
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:120
a class to store IPv4 address information on an interface
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual void SetAddress(Address address)
Set the address of this interface.
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Ptr< T > GetObject(void) const
Definition: object.h:362
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
a unique identifier for an interface.
Definition: type-id.h:49
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const