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/socket.h"
29 #include "ns3/boolean.h"
30 
31 #include "ns3/log.h"
32 #include "ns3/node.h"
33 #include "ns3/inet6-socket-address.h"
34 
35 #include "ns3/ipv6-l3-protocol.h"
36 #include "ns3/icmpv6-l4-protocol.h"
37 #include "ns3/udp-l4-protocol.h"
38 #include "ns3/ipv6-static-routing.h"
39 #include "ns3/internet-stack-helper.h"
40 #include "ns3/ipv6-address-helper.h"
41 #include "ns3/ipv6-routing-helper.h"
42 
43 #include <string>
44 #include <limits>
45 
46 using namespace ns3;
47 
55 {
57 
63  void DoSendData (Ptr<Socket> socket, std::string to);
69  void SendData (Ptr<Socket> socket, std::string to);
70 
71 public:
72  virtual void DoRun (void);
74 
79  void ReceivePkt (Ptr<Socket> socket);
80 };
81 
83  : TestCase ("IPv6 forwarding")
84 {
85 }
86 
88 {
89  uint32_t availableData;
90  availableData = socket->GetRxAvailable ();
92  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
93  //cast availableData to void, to suppress 'availableData' set but not used
94  //compiler warning
95  (void) availableData;
96 }
97 
98 void
100 {
101  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
102  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
103  123, "100");
104 }
105 
106 void
108 {
109  m_receivedPacket = Create<Packet> ();
110  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
111  &Ipv6ForwardingTest::DoSendData, this, socket, to);
112  Simulator::Run ();
113 }
114 
115 void
117 {
118  // Create topology
119 
120  // Receiver Node
121  Ptr<Node> rxNode = CreateObject<Node> ();
122  // Forwarding Node
123  Ptr<Node> fwNode = CreateObject<Node> ();
124  // Sender Node
125  Ptr<Node> txNode = CreateObject<Node> ();
126 
127  NodeContainer net1nodes (rxNode, fwNode);
128  NodeContainer net2nodes (fwNode, txNode);
129  NodeContainer nodes (rxNode, fwNode, txNode);
130 
131  SimpleNetDeviceHelper helperChannel1;
132  helperChannel1.SetNetDevicePointToPointMode (true);
133  NetDeviceContainer net1 = helperChannel1.Install (net1nodes);
134 
135  SimpleNetDeviceHelper helperChannel2;
136  helperChannel2.SetNetDevicePointToPointMode (true);
137  NetDeviceContainer net2 = helperChannel2.Install (net2nodes);
138 
139  InternetStackHelper internetv6;
140  internetv6.Install (nodes);
141 
142  txNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
143  fwNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
144  rxNode->GetObject<Icmpv6L4Protocol> ()->SetAttribute ("DAD", BooleanValue (false));
145 
146  Ipv6AddressHelper ipv6helper;
147  Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress (net1);
148  Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress (net2);
149 
150  Ptr<NetDevice> device;
151  Ptr<Ipv6> ipv6;
152  int32_t ifIndex;
153  Ipv6InterfaceAddress ipv6Addr;
154 
155  ipv6 = rxNode->GetObject<Ipv6> ();
156  device = net1.Get (0);
157  ifIndex = ipv6->GetInterfaceForDevice (device);
158  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::2"), Ipv6Prefix (64));
159  ipv6->AddAddress (ifIndex, ipv6Addr);
160 
161  ipv6 = fwNode->GetObject<Ipv6> ();
162  device = net1.Get (1);
163  ifIndex = ipv6->GetInterfaceForDevice (device);
164  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::1"), Ipv6Prefix (64));
165  ipv6->AddAddress (ifIndex, ipv6Addr);
166 
167  device = net2.Get (0);
168  ifIndex = ipv6->GetInterfaceForDevice (device);
169  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::1"), Ipv6Prefix (64));
170  ipv6->AddAddress (ifIndex, ipv6Addr);
171 
172  ipv6 = txNode->GetObject<Ipv6> ();
173  device = net2.Get (1);
174  ifIndex = ipv6->GetInterfaceForDevice (device);
175  ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::2"), Ipv6Prefix (64));
176  ipv6->AddAddress (ifIndex, ipv6Addr);
177 
178  // Setup at least a route from the sender.
179  Ptr<Ipv6StaticRouting> ipv6StaticRouting = Ipv6RoutingHelper::GetRouting <Ipv6StaticRouting> (txNode->GetObject<Ipv6> ()->GetRoutingProtocol ());
180  ipv6StaticRouting->SetDefaultRoute (Ipv6Address ("2001:2::1"), ifIndex);
181 
182  // Create the UDP sockets
183  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
184  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
185  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:1::2"), 1234)), 0, "trivial");
186  rxSocket->SetRecvCallback (MakeCallback (&Ipv6ForwardingTest::ReceivePkt, this));
187 
188  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
189  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
190  txSocket->SetAllowBroadcast (true);
191 
192  // ------ Now the tests ------------
193 
194  // Unicast test
195  SendData (txSocket, "2001:1::2");
196  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv6 Forwarding off");
197 
199  m_receivedPacket = 0;
200 
201  ipv6 = fwNode->GetObject<Ipv6> ();
202  ipv6->SetAttribute("IpForward", BooleanValue (true));
203  SendData (txSocket, "2001:1::2");
204  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv6 Forwarding on");
205 
207 
208  Simulator::Destroy ();
209 
210 }
211 
212 
220 {
221 public:
222  Ipv6ForwardingTestSuite () : TestSuite ("ipv6-forwarding", UNIT)
223  {
224  AddTestCase (new Ipv6ForwardingTest, TestCase::QUICK);
225  }
226 };
227 
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
uint32_t GetId(void) const
Definition: node.cc:107
Keep track of a set of IPv6 interfaces.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
A suite of tests to run.
Definition: test.h:1342
#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:285
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1155
Ptr< Packet > m_receivedPacket
Received packet.
a polymophic address class
Definition: address.h:90
nodes
Definition: first.py:25
#define max(a, b)
Definition: 80211b.c:43
IPv6 Forwarding TestSuite.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
void ReceivePkt(Ptr< Socket > socket)
Receive data.
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
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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.
Helper class to auto-assign global IPv6 unicast addresses.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SendData(Ptr< Socket > socket, std::string to)
Send data.
Describes an IPv6 address.
Definition: ipv6-address.h:49
void SetDefaultRoute(Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address("::"), uint32_t metric=0)
Set the default route.
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:1014
static Ipv6ForwardingTestSuite g_ipv6forwardingTestSuite
Static variable for test initialization.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
IPv6 Forwarding Test.
Describes an IPv6 prefix.
Definition: ipv6-address.h:428
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
This test suite implements a Unit Test.
Definition: test.h:1352
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
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.