To implement ad hoc routing protocols in ns3 we complete using built-in modules for numerous protocols like Ad hoc on-demand distance vector (AODV), Destination sequenced distance-vector (DSDV), and optimized link state routing (OLSR). Here is a guide on how to implement an AODV, DSDV, and OLSR in ns3 environment.
Step-by-Step Guide to Implementation
- Setting Up Your Environment
To make sure we installed ns3 on the system. If we don’t install then download it from the official website and setup.
- Create a New ns-3 Script
Create a new simulation script in the scratch directory of your ns3 installation. For example, create a file named adhoc-routing-simulation.cc.
cd ns-3.xx
cd scratch
touch adhoc-routing-simulation.cc
- Include Necessary Headers
In your adhoc-routing-simulation.cc file, include the necessary headers for your simulation.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/aodv-module.h”
#include “ns3/dsdv-module.h”
#include “ns3/olsr-module.h”
using namespace ns3;
- Set Up the Network Topology
Describe the network topology. We have created a basic topology with a few nodes and use AODV, DSDV, and OLSR as the routing protocols.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (5.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// 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);
InternetStackHelper stack;
// Uncomment the routing protocol you want to use
// AODV
// AodvHelper aodv;
// stack.SetRoutingHelper (aodv);
// DSDV
// DsdvHelper dsdv;
// stack.SetRoutingHelper (dsdv);
// OLSR
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
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 (10.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 (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Building and Running the Simulation
After writing your script, build and run it.
./waf build
./waf –run scratch/adhoc-routing-simulation
- Analysing the Results
After running the simulation, we can evaluate the consequences based on our needs. We can add logging or tracing to detect the behaviour of the certain ad hoc routing protocol.
Complete Example Scripts
Here, we provide the sample script to complete the AODV, DSDV and OLSR in ns3 environment:
AODV Example Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (5.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// 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);
InternetStackHelper stack;
AodvHelper aodv;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
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 (10.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 (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
DSDV Example Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/dsdv-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (5.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// 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);
InternetStackHelper stack;
DsdvHelper dsdv;
stack.SetRoutingHelper (dsdv);
stack.Install (nodes);
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 (10.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 (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
OLSR Example Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/olsr-module.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
// Set up mobility model
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (5.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// 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);
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
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 (10.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 (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
As we discussed about how we implemented the ad hoc protocol in ns3 environment and we also help further data about how the ad hoc protocols will adjust in diverse scenarios with implementation support on all concepts.