A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/drop-tail-queue.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 
40 #include <string>
41 #include <limits>
42 
43 using namespace ns3;
44 
45 static void
47 {
48  //IPV6
49  Ptr<Ipv6L3Protocol> ipv6 = CreateObject<Ipv6L3Protocol> ();
50  //Routing for Ipv6
51  Ptr<Ipv6StaticRouting> ipv6Routing = CreateObject<Ipv6StaticRouting> ();
52  ipv6->SetRoutingProtocol (ipv6Routing);
53  node->AggregateObject (ipv6);
54  node->AggregateObject (ipv6Routing);
55  //ICMP
56  Ptr<Icmpv6L4Protocol> icmp = CreateObject<Icmpv6L4Protocol> ();
57  node->AggregateObject (icmp);
58  //Ipv6 Extensions
59  ipv6->RegisterExtensions ();
60  ipv6->RegisterOptions ();
61  //UDP
62  Ptr<UdpL4Protocol> udp = CreateObject<UdpL4Protocol> ();
63  node->AggregateObject (udp);
64 }
65 
66 
67 
69 {
71  void DoSendData (Ptr<Socket> socket, std::string to);
72  void SendData (Ptr<Socket> socket, std::string to);
73 
74 public:
75  virtual void DoRun (void);
77 
78  void ReceivePkt (Ptr<Socket> socket);
79 };
80 
82  : TestCase ("UDP6 socket implementation")
83 {
84 }
85 
87 {
88  uint32_t availableData;
89  availableData = socket->GetRxAvailable ();
90  m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
91  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
92  //cast availableData to void, to suppress 'availableData' set but not used
93  //compiler warning
94  (void) availableData;
95 }
96 
97 void
99 {
100  Address realTo = Inet6SocketAddress (Ipv6Address (to.c_str ()), 1234);
101  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
102  123, "100");
103 }
104 
105 void
107 {
108  m_receivedPacket = Create<Packet> ();
109  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
110  &Ipv6ForwardingTest::DoSendData, this, socket, to);
111  Simulator::Run ();
112 }
113 
114 void
116 {
117  // Create topology
118 
119  // Receiver Node
120  Ptr<Node> rxNode = CreateObject<Node> ();
121  AddInternetStack6 (rxNode);
122  Ptr<SimpleNetDevice> rxDev;
123  { // first interface
124  rxDev = CreateObject<SimpleNetDevice> ();
125  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
126  rxNode->AddDevice (rxDev);
127  Ptr<Ipv6> ipv6 = rxNode->GetObject<Ipv6> ();
128  uint32_t netdev_idx = ipv6->AddInterface (rxDev);
129  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::2"), Ipv6Prefix (64));
130  ipv6->AddAddress (netdev_idx, ipv6Addr);
131  ipv6->SetUp (netdev_idx);
132  }
133 
134  // Forwarding Node
135  Ptr<Node> fwNode = CreateObject<Node> ();
136  AddInternetStack6 (fwNode);
137  Ptr<SimpleNetDevice> fwDev1, fwDev2;
138  { // first interface
139  fwDev1 = CreateObject<SimpleNetDevice> ();
140  fwDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
141  fwNode->AddDevice (fwDev1);
142  Ptr<Ipv6> ipv6 = fwNode->GetObject<Ipv6> ();
143  uint32_t netdev_idx = ipv6->AddInterface (fwDev1);
144  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:1::1"), Ipv6Prefix (64));
145  ipv6->AddAddress (netdev_idx, ipv6Addr);
146  ipv6->SetUp (netdev_idx);
147  }
148 
149  Ipv6Address nextHop;
150  { // second interface
151  fwDev2 = CreateObject<SimpleNetDevice> ();
152  fwDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
153  fwNode->AddDevice (fwDev2);
154  Ptr<Ipv6> ipv6 = fwNode->GetObject<Ipv6> ();
155  uint32_t netdev_idx = ipv6->AddInterface (fwDev2);
156  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::1"), Ipv6Prefix (64));
157  nextHop = ipv6->GetAddress(netdev_idx, 0).GetAddress();
158  ipv6->AddAddress (netdev_idx, ipv6Addr);
159  ipv6->SetUp (netdev_idx);
160  }
161 
162  // Sender Node
163  Ptr<Node> txNode = CreateObject<Node> ();
164  AddInternetStack6 (txNode);
165  Ptr<SimpleNetDevice> txDev;
166  {
167  txDev = CreateObject<SimpleNetDevice> ();
168  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
169  txNode->AddDevice (txDev);
170  Ptr<Ipv6> ipv6 = txNode->GetObject<Ipv6> ();
171  uint32_t netdev_idx = ipv6->AddInterface (txDev);
172  Ipv6InterfaceAddress ipv6Addr = Ipv6InterfaceAddress (Ipv6Address ("2001:2::2"), Ipv6Prefix (64));
173  ipv6->AddAddress (netdev_idx, ipv6Addr);
174  ipv6->SetUp (netdev_idx);
175  Ptr<Ipv6StaticRouting> ipv6StaticRouting = txNode->GetObject<Ipv6StaticRouting> ();
176  ipv6StaticRouting->SetDefaultRoute(nextHop, netdev_idx);
177  }
178 
179  // link the two nodes
180  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
181  rxDev->SetChannel (channel1);
182  fwDev1->SetChannel (channel1);
183 
184  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
185  fwDev2->SetChannel (channel2);
186  txDev->SetChannel (channel2);
187 
188  // Create the UDP sockets
189  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
190  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
191  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (Inet6SocketAddress (Ipv6Address ("2001:1::2"), 1234)), 0, "trivial");
192  rxSocket->SetRecvCallback (MakeCallback (&Ipv6ForwardingTest::ReceivePkt, this));
193 
194  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
195  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
196  txSocket->SetAllowBroadcast (true);
197 
198  // ------ Now the tests ------------
199 
200  // Unicast test
201  SendData (txSocket, "2001:1::2");
202  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv6 Forwarding off");
203 
205  m_receivedPacket = 0;
206 
207  Ptr<Ipv6> ipv6 = fwNode->GetObject<Ipv6> ();
208  ipv6->SetAttribute("IpForward", BooleanValue (true));
209  SendData (txSocket, "2001:1::2");
210  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv6 Forwarding on");
211 
213 
214  Simulator::Destroy ();
215 
216 }
217 
218 
219 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 {
223 public:
224  Ipv6ForwardingTestSuite () : TestSuite ("ipv6-forwarding", UNIT)
225  {
226  AddTestCase (new Ipv6ForwardingTest, TestCase::QUICK);
227  }
Hold a bool native type.
Definition: boolean.h:38
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
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)
A suite of tests to run.
Definition: test.h:1025
#define NS_ASSERT(condition)
Definition: assert.h:64
IPv6 address associated with an interface.
uint32_t GetSize(void) const
Definition: packet.h:650
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:849
Ptr< Packet > m_receivedPacket
a polymophic address class
Definition: address.h:86
Ipv6ForwardingTestSuite g_ipv6forwardingTestSuite
Ipv6Address GetAddress() const
Get the IPv6 address.
virtual Ipv6InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
Get IPv6 address on specified IPv6 interface.
void ReceivePkt(Ptr< Socket > socket)
#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:244
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
static void AddInternetStack6(Ptr< Node > node)
Doxygen introspection did not find any typical Config paths.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
void SendData(Ptr< Socket > socket, std::string to)
virtual void RegisterExtensions()
Register the IPv6 Extensions.
Describes an IPv6 address.
Definition: ipv6-address.h:46
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
void SetDefaultRoute(Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse=Ipv6Address("::"), uint32_t metric=0)
Set the default route.
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
uint32_t GetId(void) const
Definition: node.cc:104
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual void SetAddress(Address address)
Set the address of this interface.
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:361
virtual void RegisterOptions()
Register the IPv6 Options.
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
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.
This test suite implements a Unit Test.
Definition: test.h:1035
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.