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/rng-seed-manager.h"
37 #include "ns3/simple-net-device-helper.h"
38 #include "ns3/simple-net-device.h"
39 #include "ns3/icmpv4.h"
40 #include "bug780-test.h"
41 
42 namespace ns3 {
43 namespace olsr {
44 
46  TestCase ("Test OLSR bug 780"),
47  m_time (Seconds (200.0)), m_seq (0), m_recvCount (0)
48 {
49 }
50 
52 {
53 }
54 
55 void
57 {
59  RngSeedManager::SetRun (12345);
60  CreateNodes ();
61 
63  Simulator::Run ();
64 
65  NS_TEST_EXPECT_MSG_EQ (m_recvCount, 192, "192 out of 200 ping received.");
66 
68 }
69 
70 void
72 {
73  NodeContainer c;
74  c.Create (3);
75 
76  // install TCP/IP & OLSR
78  InternetStackHelper internet;
79  internet.SetRoutingHelper (olsr);
80  internet.Install (c);
81  int64_t streamsUsed = olsr.AssignStreams (c, 0);
82  NS_TEST_EXPECT_MSG_EQ (streamsUsed, 3, "Should have assigned 3 streams");
83 
84  // create channel & devices
85  SimpleNetDeviceHelper simpleNetHelper;
86  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
87  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
88  NetDeviceContainer nd = simpleNetHelper.Install (c);
89 
90  Ipv4AddressHelper addressAdhoc;
91  addressAdhoc.SetBase ("10.1.1.0", "255.255.255.0");
92  Ipv4InterfaceContainer adhocInterfaces;
93  adhocInterfaces = addressAdhoc.Assign (nd);
94 
95  // Blacklist some devices (equivalent to Wireless out of range)
96  Ptr<SimpleNetDevice> nd0 = DynamicCast<SimpleNetDevice> (nd.Get (0));
97  Ptr<SimpleNetDevice> nd2 = DynamicCast<SimpleNetDevice> (nd.Get (2));
98  Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel> (nd.Get (0)->GetChannel ());
99 
100  Simulator::Schedule (Seconds (100.0), &SimpleChannel::BlackList, ch, nd0, nd2);
101  Simulator::Schedule (Seconds (100.0), &SimpleChannel::BlackList, ch, nd2, nd0);
102 
103  // 3. Setup ping
104  m_socket = Socket::CreateSocket (c.Get (0), TypeId::LookupByName ("ns3::Ipv4RawSocketFactory"));
105  m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp
108  m_socket->Bind (src);
109  InetSocketAddress dst = InetSocketAddress (adhocInterfaces.GetAddress (2), 0);
110  m_socket->Connect (dst);
111 
112  SendPing ();
113 }
114 
115 void
117 {
118  if (Simulator::Now () >= m_time)
119  {
120  return;
121  }
122 
123  Ptr<Packet> p = Create<Packet> ();
124  Icmpv4Echo echo;
125  echo.SetSequenceNumber (m_seq);
126  m_seq++;
127  echo.SetIdentifier (0);
128 
129  Ptr<Packet> dataPacket = Create<Packet> (56);
130  echo.SetData (dataPacket);
131  p->AddHeader (echo);
132  Icmpv4Header header;
134  header.SetCode (0);
135  if (Node::ChecksumEnabled ())
136  {
137  header.EnableChecksum ();
138  }
139  p->AddHeader (header);
140  m_socket->Send (p, 0);
142 }
143 
144 void
146 {
147  while (m_socket->GetRxAvailable () > 0)
148  {
149  Address from;
150  Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from);
151 
154  NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp.
155  Ipv4Header ipv4;
156  p->RemoveHeader (ipv4);
157  NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp.
158  Icmpv4Header icmp;
159  p->RemoveHeader (icmp);
161  {
162  m_recvCount++;
163  }
164  }
165 }
166 
167 } // namespace olsr
168 } // namespace ns3
169 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::Ipv4Header
Packet header for IPv4.
Definition: ipv4-header.h:34
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
NS_ASSERT
#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
bug780-test.h
ns3::InetSocketAddress::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition: inet-socket-address.cc:103
ns3::Icmpv4Echo::SetIdentifier
void SetIdentifier(uint16_t id)
Set the Echo identifier.
Definition: icmpv4.cc:140
ns3::Socket::Bind
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
ns3::olsr::Bug780Test::m_seq
uint16_t m_seq
Sequence number.
Definition: bug780-test.h:56
ns3::Icmpv4Header::EnableChecksum
void EnableChecksum(void)
Enables ICMP Checksum calculation.
Definition: icmpv4.cc:57
ns3::Icmpv4Header::SetType
void SetType(uint8_t type)
Set ICMP type.
Definition: icmpv4.cc:109
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
ns3::InetSocketAddress::GetPort
uint16_t GetPort(void) const
Definition: inet-socket-address.cc:65
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::Icmpv4Echo::SetSequenceNumber
void SetSequenceNumber(uint16_t seq)
Set the Echo sequence number.
Definition: icmpv4.cc:146
ns3::olsr::Bug780Test::Bug780Test
Bug780Test()
Definition: bug780-test.cc:45
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::InetSocketAddress::ConvertFrom
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Definition: inet-socket-address.cc:126
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::olsr::Bug780Test::m_socket
Ptr< Socket > m_socket
Socket.
Definition: bug780-test.h:54
ns3::RngSeedManager::SetRun
static void SetRun(uint64_t run)
Set the run number of simulation.
Definition: rng-seed-manager.cc:87
ns3::TestCase
encapsulates test code
Definition: test.h:1154
ns3::SimpleNetDeviceHelper::SetChannelAttribute
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: simple-net-device-helper.cc:88
ns3::Ipv4Header::GetProtocol
uint8_t GetProtocol(void) const
Definition: ipv4-header.cc:272
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::SimpleNetDeviceHelper::SetDeviceAttribute
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: simple-net-device-helper.cc:82
ns3::olsr::Bug780Test::Receive
void Receive(Ptr< Socket > socket)
Receive echo reply.
Definition: bug780-test.cc:145
ns3::Socket::Send
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
ns3::Socket::SetRecvCallback
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
ns3::OlsrHelper
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::SimpleChannel::BlackList
virtual void BlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Blocks the communications from a NetDevice to another NetDevice.
Definition: simple-channel.cc:102
ns3::InternetStackHelper::Install
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: internet-stack-helper.cc:366
ns3::Socket::RecvFrom
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.
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
ns3::Node::ChecksumEnabled
static bool ChecksumEnabled(void)
Definition: node.cc:278
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
ns3::SimpleNetDeviceHelper
build a set of SimpleNetDevice objects
Definition: simple-net-device-helper.h:37
NS_TEST_EXPECT_MSG_EQ
#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:283
ns3::RngSeedManager::SetSeed
static void SetSeed(uint32_t seed)
Set the seed.
Definition: rng-seed-manager.cc:81
ns3::SimpleNetDeviceHelper::Install
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Definition: simple-net-device-helper.cc:100
ns3::Icmpv4Header::ICMPV4_ECHO
@ ICMPV4_ECHO
Definition: icmpv4.h:50
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::olsr::Bug780Test::m_recvCount
uint16_t m_recvCount
Received ECHO Reply counter.
Definition: bug780-test.h:58
ns3::olsr::Bug780Test::SendPing
void SendPing()
Send one ping.
Definition: bug780-test.cc:116
ns3::Icmpv4Echo
ICMP Echo header.
Definition: icmpv4.h:108
ns3::Socket::Connect
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::InternetStackHelper::SetRoutingHelper
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
Definition: internet-stack-helper.cc:175
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::Icmpv4Header::ICMPV4_ECHO_REPLY
@ ICMPV4_ECHO_REPLY
Definition: icmpv4.h:48
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::olsr::Bug780Test::DoRun
void DoRun()
Implementation to actually run this TestCase.
Definition: bug780-test.cc:56
olsr
Definition: olsr.py:1
ns3::Icmpv4Header::SetCode
void SetCode(uint8_t code)
Set ICMP code.
Definition: icmpv4.cc:115
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::Icmpv4Header
Base class for all the ICMP packet headers.
Definition: icmpv4.h:41
ns3::olsr::Bug780Test::~Bug780Test
~Bug780Test()
Definition: bug780-test.cc:51
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::Socket::CreateSocket
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
ns3::Socket::GetRxAvailable
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
ns3::olsr::Bug780Test::m_time
const Time m_time
Total simulation time.
Definition: bug780-test.h:42
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
ns3::NetDeviceContainer::Get
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Definition: net-device-container.cc:62
ns3::Icmpv4Header::GetType
uint8_t GetType(void) const
Get ICMP type.
Definition: icmpv4.cc:121
ns3::Icmpv4Echo::SetData
void SetData(Ptr< const Packet > data)
Set the Echo data.
Definition: icmpv4.cc:152
ns3::olsr::Bug780Test::CreateNodes
void CreateNodes()
Create & configure test network.
Definition: bug780-test.cc:71