A Discrete-Event Network Simulator
API
ipv6-forwarding-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 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/socket-factory.h"
23 #include "ns3/udp-socket-factory.h"
24 #include "ns3/simulator.h"
25 #include "ns3/simple-channel.h"
26 #include "ns3/simple-net-device.h"
27 #include "ns3/simple-net-device-helper.h"
28 #include "ns3/drop-tail-queue.h"
29 #include "ns3/socket.h"
30 #include "ns3/boolean.h"
31 
32 #include "ns3/log.h"
33 #include "ns3/node.h"
34 #include "ns3/inet6-socket-address.h"
35 
36 #include "ns3/ipv6-l3-protocol.h"
37 #include "ns3/icmpv6-l4-protocol.h"
38 #include "ns3/udp-l4-protocol.h"
39 #include "ns3/ipv6-static-routing.h"
40 #include "ns3/internet-stack-helper.h"
41 #include "ns3/ipv6-address-helper.h"
42 #include "ns3/ipv6-routing-helper.h"
43 
44 #include <string>
45 #include <limits>
46 
47 using namespace ns3;
48 
50 {
52  void DoSendData (Ptr<Socket> socket, std::string to);
53  void SendData (Ptr<Socket> socket, std::string to);
54 
55 public:
56  virtual void DoRun (void);
58 
59  void ReceivePkt (Ptr<Socket> socket);
60 };
61 
63  : TestCase ("IPv6 forwarding")
64 {
65 }
66 
68 {
69  uint32_t availableData;
70  availableData = socket->GetRxAvailable ();
72  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
73  //cast availableData to void, to suppress 'availableData' set but not used
74  //compiler warning
75  (void) availableData;
76 }
77 
78 void
80 {
81  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
82  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
83  123, "100");
84 }
85 
86 void
87 Ipv6ForwardingTest::SendData (Ptr<Socket> socket, std::string to)
88 {
89  m_receivedPacket = Create<Packet> ();
90  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
91  &Ipv6ForwardingTest::DoSendData, this, socket, to);
92  Simulator::Run ();
93 }
94 
95 void
97 {
98  // Create topology
99 
100  // Receiver Node
101  Ptr<Node> rxNode = CreateObject<Node> ();
102  // Forwarding Node
103  Ptr<Node> fwNode = CreateObject<Node> ();
104  // Sender Node
105  Ptr<Node> txNode = CreateObject<Node> ();
106 
107  NodeContainer net1nodes (rxNode, fwNode);
108  NodeContainer net2nodes (fwNode, txNode);
109  NodeContainer nodes (rxNode, fwNode, txNode);
110 
111  SimpleNetDeviceHelper helperChannel1;
112  helperChannel1.SetNetDevicePointToPointMode (true);
113  NetDeviceContainer net1 = helperChannel1.Install (net1nodes);
114 
115  SimpleNetDeviceHelper helperChannel2;
116  helperChannel2.SetNetDevicePointToPointMode (true);
117  NetDeviceContainer net2 = helperChannel2.Install (net2nodes);
118 
119  InternetStackHelper internetv6;
120  internetv6.Install (nodes);
121 
122  txNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
123  fwNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
124  rxNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
125 
126  Ipv6AddressHelper ipv6helper;
127  Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress (net1);
128  Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress (net2);
129 
130  Ptr<NetDevice> device;
131  Ptr<Ipv6> ipv6;
132  int32_t ifIndex;
133  Ipv6InterfaceAddress ipv6Addr;
134 
135  ipv6 = rxNode->GetObject<Ipv6> ();
136  device = net1.Get (0);
137  ifIndex = ipv6->GetInterfaceForDevice (device);
138  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::2"), Ipv6Prefix (64));
139  ipv6->AddAddress (ifIndex, ipv6Addr);
140 
141  ipv6 = fwNode->GetObject<Ipv6> ();
142  device = net1.Get (1);
143  ifIndex = ipv6->GetInterfaceForDevice (device);
144  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::1"), Ipv6Prefix (64));
145  ipv6->AddAddress (ifIndex, ipv6Addr);
146 
147  device = net2.Get (0);
148  ifIndex = ipv6->GetInterfaceForDevice (device);
149  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::1"), Ipv6Prefix (64));
150  ipv6->AddAddress (ifIndex, ipv6Addr);
151 
152  ipv6 = txNode->GetObject<Ipv6> ();
153  device = net2.Get (1);
154  ifIndex = ipv6->GetInterfaceForDevice (device);
155  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::2"), Ipv6Prefix (64));
156  ipv6->AddAddress (ifIndex, ipv6Addr);
157 
158  // Setup at least a route from the sender.
159  Ptr<Ipv6StaticRouting> ipv6StaticRouting = Ipv6RoutingHelper::GetRouting <Ipv6StaticRouting> (txNode->GetObject<Ipv6> ()->GetRoutingProtocol ());
160  ipv6StaticRouting->SetDefaultRoute (Ipv6Address ("2001:2::1"), ifIndex);
161 
162  // Create the UDP sockets
163  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
164  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
165  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:1::2"), 1234)), 0, "trivial");
166  rxSocket->SetRecvCallback (MakeCallback (&Ipv6ForwardingTest::ReceivePkt, this));
167 
168  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
169  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
170  txSocket->SetAllowBroadcast (true);
171 
172  // ------ Now the tests ------------
173 
174  // Unicast test
175  SendData (txSocket, "2001:1::2");
176  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv6 Forwarding off");
177 
179  m_receivedPacket = 0;
180 
181  ipv6 = fwNode->GetObject<Ipv6> ();
182  ipv6->SetAttribute("IpForward", BooleanValue (true));
183  SendData (txSocket, "2001:1::2");
184  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv6 Forwarding on");
185 
187 
188  Simulator::Destroy ();
189 
190 }
191 
192 
193 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
196 {
197 public:
198  Ipv6ForwardingTestSuite () : TestSuite ("ipv6-forwarding", UNIT)
199  {
200  AddTestCase (new Ipv6ForwardingTest, TestCase::QUICK);
201  }
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Keep track of a set of IPv6 interfaces.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
A suite of tests to run.
Definition: test.h:1333
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
IPv6 address associated with an interface.
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1147
Ptr< Packet > m_receivedPacket
This test suite implements a Unit Test.
Definition: test.h:1343
a polymophic address class
Definition: address.h:90
Ipv6ForwardingTestSuite g_ipv6forwardingTestSuite
tuple nodes
Definition: first.py:25
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
void ReceivePkt(Ptr< Socket > socket)
holds a vector of ns3::NetDevice pointers
An Inet6 address class.
An implementation of the ICMPv6 protocol.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class to auto-assign global IPv6 unicast addresses.
void SendData(Ptr< Socket > socket, std::string to)
Describes an IPv6 address.
Definition: ipv6-address.h:48
void SetDefaultRoute(Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address("::"), uint32_t metric=0)
Set the default route.
uint32_t GetId(void) const
Definition: node.cc:107
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:349
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
API to create UDP socket instances.
build a set of SimpleNetDevice objects
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
Get the interface index of the specified NetDevice.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.