Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement route access protocol in ns3

Implementing and simulating a route access protocol in ns3 we will use one of the routing protocols which supports ns3 like AODV (Ad-hoc On-Demand Distance Vector) or DSR (Dynamic Source Routing), or OLSR (Optimized Link State Routing). Here, let us dive into the interesting and detailed guide on setting up and simulating a basic network using the AODV protocol.

Steps to implement AODV in ns3

  1. Set up your environment

Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.

  1. 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

  1. 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;

 

  1. 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;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (10.0),

                                 “DeltaY”, DoubleValue (10.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                             “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

  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 (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));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“aodv-simulation.tr”));

  wifiPhy.EnablePcapAll (“aodv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

  1. Build and run the simulation

Build and run your script after writing.

./waf build

./waf –run scratch/aodv-simulation

  1. 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;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (10.0),

                                 “DeltaY”, DoubleValue (10.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

                             “Bounds”, RectangleValue (Rectangle (-50, 50, -50, 50)));

  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 (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));

  // 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 executed our implementation on Route access protocol by setting up and simulating a simple network using the AODV protocol. Also, we provide more related project coding on route access protocol.