A Discrete-Event Network Simulator
API
bug780-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
14  * USA
15  *
16  */
17 
18 // OLSR was observed to not converge in simple 3-nodes varying topology.
19 // https://www.nsnam.org/bugzilla/show_bug.cgi?id=780
20 // tcpdump -r bug780-0-0.pcap -nn -tt icmp | wc
21 // should show about 395 packets; there is a ping outage from time
22 // 123-127 due to the mobility.
23 
24 #include "ns3/test.h"
25 #include "ns3/olsr-helper.h"
26 #include "ns3/ipv4-list-routing-helper.h"
27 #include "ns3/olsr-routing-protocol.h"
28 #include "ns3/internet-stack-helper.h"
29 #include "ns3/log.h"
30 #include "ns3/double.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/string.h"
33 #include "ns3/boolean.h"
34 #include "ns3/ipv4-address-helper.h"
35 #include "ns3/ipv4-interface-container.h"
36 #include "ns3/internet-stack-helper.h"
37 #include "ns3/rng-seed-manager.h"
38 #include "ns3/simple-net-device-helper.h"
39 #include "ns3/simple-net-device.h"
40 #include "ns3/icmpv4.h"
41 #include "bug780-test.h"
42 
43 namespace ns3 {
44 namespace olsr {
45 
47  TestCase ("Test OLSR bug 780"),
48  m_time (Seconds (200.0)), m_seq (0), m_recvCount (0)
49 {
50 }
51 
53 {
54 }
55 
56 void
58 {
60  RngSeedManager::SetRun (12345);
61  CreateNodes ();
62 
64  Simulator::Run ();
65 
66  NS_TEST_EXPECT_MSG_EQ (m_recvCount, 192, "192 out of 200 ping received.");
67 
69 }
70 
71 void
73 {
74  NodeContainer c;
75  c.Create (3);
76 
77  // install TCP/IP & OLSR
79  InternetStackHelper internet;
80  internet.SetRoutingHelper (olsr);
81  internet.Install (c);
82  int64_t streamsUsed = olsr.AssignStreams (c, 0);
83  NS_TEST_EXPECT_MSG_EQ (streamsUsed, 3, "Should have assigned 3 streams");
84 
85  // create channel & devices
86  SimpleNetDeviceHelper simpleNetHelper;
87  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
88  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
89  NetDeviceContainer nd = simpleNetHelper.Install (c);
90 
91  Ipv4AddressHelper addressAdhoc;
92  addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0");
93  Ipv4InterfaceContainer adhocInterfaces;
94  adhocInterfaces = addressAdhoc.Assign (nd);
95 
96  // Blacklist some devices (equivalent to Wireless out of range)
97  Ptr<SimpleNetDevice> nd0 = DynamicCast<SimpleNetDevice> (nd.Get (0));
98  Ptr<SimpleNetDevice> nd2 = DynamicCast<SimpleNetDevice> (nd.Get (2));
99  Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel> (nd.Get (0)->GetChannel ());
100 
101  Simulator::Schedule (Seconds (100.0), &SimpleChannel::BlackList, ch, nd0, nd2);
102  Simulator::Schedule (Seconds (100.0), &SimpleChannel::BlackList, ch, nd2, nd0);
103 
104  // 3. Setup ping
105  m_socket = Socket::CreateSocket (c.Get (0), TypeId::LookupByName ("ns3::Ipv4RawSocketFactory"));
106  m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp
109  m_socket->Bind (src);
110  InetSocketAddress dst = InetSocketAddress (adhocInterfaces.GetAddress (2), 0);
111  m_socket->Connect (dst);
112 
113  SendPing ();
114 }
115 
116 void
118 {
119  if (Simulator::Now () >= m_time)
120  {
121  return;
122  }
123 
124  Ptr<Packet> p = Create<Packet> ();
125  Icmpv4Echo echo;
126  echo.SetSequenceNumber (m_seq);
127  m_seq++;
128  echo.SetIdentifier (0);
129 
130  Ptr<Packet> dataPacket = Create<Packet> (56);
131  echo.SetData (dataPacket);
132  p->AddHeader (echo);
133  Icmpv4Header header;
134  header.SetType (Icmpv4Header::ECHO);
135  header.SetCode (0);
136  if (Node::ChecksumEnabled ())
137  {
138  header.EnableChecksum ();
139  }
140  p->AddHeader (header);
141  m_socket->Send (p, 0);
143 }
144 
145 void
147 {
148  while (m_socket->GetRxAvailable () > 0)
149  {
150  Address from;
151  Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from);
152 
155  NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp.
156  Ipv4Header ipv4;
157  p->RemoveHeader (ipv4);
158  NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp.
159  Icmpv4Header icmp;
160  p->RemoveHeader (icmp);
161  if (icmp.GetType () == Icmpv4Header::ECHO_REPLY)
162  {
163  m_recvCount++;
164  }
165  }
166 }
167 
168 } // namespace olsr
169 } // namespace ns3
170 
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:267
void SetType(uint8_t type)
Set ICMP type.
Definition: icmpv4.cc:109
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
holds a vector of std::pair of Ptr and interface index.
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
static bool ChecksumEnabled(void)
Definition: node.cc:276
#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
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
void Receive(Ptr< Socket > socket)
Receive echo reply.
Definition: bug780-test.cc:146
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: olsr-helper.cc:87
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
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:40
encapsulates test code
Definition: test.h:1155
Base class for all the ICMP packet headers.
Definition: icmpv4.h:40
static void SetRun(uint64_t run)
Set the run number of simulation.
a polymophic address class
Definition: address.h:90
void SetCode(uint8_t code)
Set ICMP code.
Definition: icmpv4.cc:115
const Time m_time
Total simulation time.
Definition: bug780-test.h:42
Packet header for IPv4.
Definition: ipv4-header.h:33
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
void EnableChecksum(void)
Enables ICMP Checksum calculation.
Definition: icmpv4.cc:57
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
keep track of a set of node pointers.
ICMP Echo header.
Definition: icmpv4.h:107
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: olsr.py:1
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
uint16_t m_seq
Sequence number.
Definition: bug780-test.h:56
static void SetSeed(uint32_t seed)
Set the seed.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void SendPing()
Send one ping.
Definition: bug780-test.cc:117
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void SetSequenceNumber(uint16_t seq)
Set the Echo sequence number.
Definition: icmpv4.cc:146
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
uint16_t m_recvCount
Received ECHO Reply counter.
Definition: bug780-test.h:58
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
uint16_t GetPort(void) const
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Socket > m_socket
Socket.
Definition: bug780-test.h:54
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.
build a set of SimpleNetDevice objects
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void DoRun()
Implementation to actually run this TestCase.
Definition: bug780-test.cc:57
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
static bool IsMatchingType(const Address &address)
virtual void BlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Blocks the communications from a NetDevice to another NetDevice.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void CreateNodes()
Create & configure test network.
Definition: bug780-test.cc:72
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:823
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.