A Discrete-Event Network Simulator
API
ipv4-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/socket.h"
28 #include "ns3/boolean.h"
29 
30 #include "ns3/log.h"
31 #include "ns3/node.h"
32 #include "ns3/inet-socket-address.h"
33 
34 #include "ns3/arp-l3-protocol.h"
35 #include "ns3/ipv4-l3-protocol.h"
36 #include "ns3/icmpv4-l4-protocol.h"
37 #include "ns3/udp-l4-protocol.h"
38 #include "ns3/ipv4-static-routing.h"
39 #include "ns3/internet-stack-helper.h"
40 #include "ns3/ipv4-routing-helper.h"
41 
42 #include "ns3/traffic-control-layer.h"
43 
44 #include <string>
45 #include <limits>
46 
47 using namespace ns3;
48 
49 
57 {
59 
65  void DoSendData (Ptr<Socket> socket, std::string to);
71  void SendData (Ptr<Socket> socket, std::string to);
72 
73 public:
74  virtual void DoRun (void);
76 
81  void ReceivePkt (Ptr<Socket> socket);
82 };
83 
85  : TestCase ("UDP socket implementation")
86 {
87 }
88 
90 {
91  uint32_t availableData;
92  availableData = socket->GetRxAvailable ();
94  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
95 }
96 
97 void
99 {
100  Address realTo = InetSocketAddress (Ipv4Address (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  &Ipv4ForwardingTest::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 
122  InternetStackHelper internet;
123  internet.SetIpv6StackInstall (false);
124 
125  internet.Install (rxNode);
126  Ptr<SimpleNetDevice> rxDev;
127  { // first interface
128  rxDev = CreateObject<SimpleNetDevice> ();
129  rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
130  rxNode->AddDevice (rxDev);
131  Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
132  uint32_t netdev_idx = ipv4->AddInterface (rxDev);
133  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
134  ipv4->AddAddress (netdev_idx, ipv4Addr);
135  ipv4->SetUp (netdev_idx);
136  }
137 
138  // Forwarding Node
139  Ptr<Node> fwNode = CreateObject<Node> ();
140 
141  internet.Install (fwNode);
142  Ptr<SimpleNetDevice> fwDev1, fwDev2;
143  { // first interface
144  fwDev1 = CreateObject<SimpleNetDevice> ();
145  fwDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
146  fwNode->AddDevice (fwDev1);
147  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
148  uint32_t netdev_idx = ipv4->AddInterface (fwDev1);
149  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
150  ipv4->AddAddress (netdev_idx, ipv4Addr);
151  ipv4->SetUp (netdev_idx);
152  }
153 
154  { // second interface
155  fwDev2 = CreateObject<SimpleNetDevice> ();
156  fwDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
157  fwNode->AddDevice (fwDev2);
158  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
159  uint32_t netdev_idx = ipv4->AddInterface (fwDev2);
160  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.1"), Ipv4Mask (0xffff0000U));
161  ipv4->AddAddress (netdev_idx, ipv4Addr);
162  ipv4->SetUp (netdev_idx);
163  }
164 
165  // Sender Node
166  Ptr<Node> txNode = CreateObject<Node> ();
167 
168  internet.Install (txNode);
169  Ptr<SimpleNetDevice> txDev;
170  {
171  txDev = CreateObject<SimpleNetDevice> ();
172  txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
173  txNode->AddDevice (txDev);
174  Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
175  uint32_t netdev_idx = ipv4->AddInterface (txDev);
176  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.2"), Ipv4Mask (0xffff0000U));
177  ipv4->AddAddress (netdev_idx, ipv4Addr);
178  ipv4->SetUp (netdev_idx);
179  Ptr<Ipv4StaticRouting> ipv4StaticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (txNode->GetObject<Ipv4> ()->GetRoutingProtocol ());
180  ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
181  }
182 
183  // link the two nodes
184  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
185  rxDev->SetChannel (channel1);
186  fwDev1->SetChannel (channel1);
187 
188  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
189  fwDev2->SetChannel (channel2);
190  txDev->SetChannel (channel2);
191 
192  // Create the UDP sockets
193  Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
194  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
195  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
196  rxSocket->SetRecvCallback (MakeCallback (&Ipv4ForwardingTest::ReceivePkt, this));
197 
198  Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
199  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
200  txSocket->SetAllowBroadcast (true);
201 
202  // ------ Now the tests ------------
203 
204  // Unicast test
205  SendData (txSocket, "10.0.0.2");
206  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv4 Forwarding on");
207 
209  m_receivedPacket = 0;
210 
211  Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
212  ipv4->SetAttribute("IpForward", BooleanValue (false));
213  SendData (txSocket, "10.0.0.2");
214  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv4 Forwarding off");
215 
216  Simulator::Destroy ();
217 
218 }
219 
220 
228 {
229 public:
231 private:
232 };
233 
235  : TestSuite ("ipv4-forwarding", UNIT)
236 {
237  AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
238 }
239 
241 
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
uint32_t GetId(void) const
Definition: node.cc:107
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Ptr< Packet > m_receivedPacket
Received packet.
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
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
virtual void DoRun(void)
Implementation to actually run this TestCase.
a polymophic address class
Definition: address.h:90
void SendData(Ptr< Socket > socket, std::string to)
Send data.
#define max(a, b)
Definition: 80211b.c:43
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual void SetUp(uint32_t interface)=0
static Ipv4ForwardingTestSuite g_ipv4forwardingTestSuite
Static variable for test initialization.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
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.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
IPv4 Forwarding Test.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ReceivePkt(Ptr< Socket > socket)
Receive data.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
a class to store IPv4 address information on an interface
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.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
IPv4 Forwarding TestSuite.
API to create UDP socket instances.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.