To implement a reactive routing protocol in ns3, we use any one of the reactive routing protocol’s built-in like AODV (Ad-hoc On-Demand Distance Vector) or DSR (Dynamic Source Routing). Here, we will learn on setting up a simulation which uses AODV protocol in ns3
Steps for implementing AODV in ns3
- Set up your environment
Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.
- Create a new ns3 script
On your ns3 installation, create a new simulation script in the scratch directory. For Example, you can create a file named aodv-simulation.cc.
cd ns-3.xx
cd scratch
touch aodv-simulation.cc
- Include the necessary header files
In the aodv-simulation.cc file, create the necessary header files for your simulation.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/aodv-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
- Set the network topology
Set the network topology by creating the basic topology that contains several nodes which utilizes the mobility model and the AODV routing protocol.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up WiFi
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Set up mobility model
MobilityHelper mobility;
Ptr<UniformRandomVariable> random = CreateObject<UniformRandomVariable> ();
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“MaxX”, DoubleValue (100.0),
“MaxY”, DoubleValue (100.0));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::ConstantRandomVariable[Constant=20.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=0.0]”),
“PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator[MinX=0.0|MinY=0.0|MaxX=100.0|MaxY=100.0]”));
mobility.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (20.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (20.0));
// Enable tracing
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“aodv-simulation.tr”));
wifiPhy.EnablePcapAll (“aodv-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Build and run the simulation
Build and run your script after writing.
./waf build
./waf –run scratch/aodv-simulation
- Analyze the results
You can analyze the results after running the simulation, by using the generated trace files (aodv-simulation.tr and aodv-simulation-0-0.pcap).
Example script for AODV
Below is the example script for your reference:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/aodv-module.h”
#include “ns3/applications-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up WiFi
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Set up mobility model
MobilityHelper mobility;
Ptr<UniformRandomVariable> random = CreateObject<UniformRandomVariable> ();
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“MaxX”, DoubleValue (100.0),
“MaxY”, DoubleValue (100.0));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::ConstantRandomVariable[Constant=20.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=0.0]”),
“PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator[MinX=0.0|MinY=0.0|MaxX=100.0|MaxY=100.0]”));
mobility.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (20.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (20.0));
// Enable tracing
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“aodv-simulation.tr”));
wifiPhy.EnablePcapAll (“aodv-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Overall, we had successfully learned a reactive routing protocol in ns-3 by analyzing the simulation results of AODV (Ad hoc On-Demand Distance Vector). Also, we offer more programming and implementation support on reactive routing protocols.