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

How to implement AOMDV protocol in ns3

To implement the Ad hoc On-Demand Multipath Distance Vector (AOMDV) protocol in ns3 we need to make a custom module for AOMDV and this is not obtainable to default ns3 distribution. Get a Custom module for AOMDV related to your project concept from us. AOMDV is an extension to AODV that offers multiple loop free and link disjoint paths. Here are the procedures on how to implement the AOMDV in ns3 environment.

Step-by-Step Guide to Implementing AOMDV in ns3

  1. Set Up Your Environment

Make certain we have installed ns3. If you haven’t installed it yet, download and set it up.

  1. Create a New ns3 Module

Create a new module for AOMDV in the src directory of your ns3 installation. This involves creating the necessary directory structure and files.

cd ns-3.xx

cd src

mkdir -p aomdv/model

mkdir -p aomdv/helper

  1. Create the AOMDV Header File

Create the AOMDV routing protocol header file aomdv-routing-protocol.h in the model directory.

#ifndef AOMDV_ROUTING_PROTOCOL_H

#define AOMDV_ROUTING_PROTOCOL_H

#include “ns3/ipv4-routing-protocol.h”

#include “ns3/ipv4-address.h”

#include “ns3/timer.h”

#include “ns3/mobility-model.h”

#include “ns3/node.h”

#include “ns3/net-device.h”

#include “ns3/ipv4.h”

#include “ns3/ipv4-routing-table-entry.h”

#include <map>

#include <vector>

namespace ns3 {

class AomdvRoutingProtocol : public Ipv4RoutingProtocol

{

public:

  static TypeId GetTypeId (void);

  AomdvRoutingProtocol ();

  virtual ~AomdvRoutingProtocol ();

  // Inherited from Ipv4RoutingProtocol

  virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);

  virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,

                           UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb);

  virtual void NotifyInterfaceUp (uint32_t interface);

  virtual void NotifyInterfaceDown (uint32_t interface);

  virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);

  virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);

  virtual void SetIpv4 (Ptr<Ipv4> ipv4);

  virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;

private:

  Ptr<Ipv4> m_ipv4;

  std::map<Ipv4Address, std::vector<Ipv4RoutingTableEntry>> m_routingTable;

  Ptr<NetDevice> GetNetDevice (uint32_t interface);

  Ptr<MobilityModel> GetMobilityModel (Ptr<NetDevice> netDevice);

  std::vector<Ipv4Address> GetNextHops (Ipv4Address dest);

};

} // namespace ns3

#endif /* AOMDV_ROUTING_PROTOCOL_H */

  1. Create the AOMDV Source File

Create the AOMDV routing protocol source file aomdv-routing-protocol.cc in the model directory.

#include “aomdv-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

#include <algorithm>

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“AomdvRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (AomdvRoutingProtocol);

TypeId

AomdvRoutingProtocol::GetTypeId (void)

{

  static TypeId tid = TypeId (“ns3::AomdvRoutingProtocol”)

    .SetParent<Ipv4RoutingProtocol> ()

    .SetGroupName(“Internet”)

    .AddConstructor<AomdvRoutingProtocol> ();

  return tid;

}

AomdvRoutingProtocol::AomdvRoutingProtocol ()

{

}

AomdvRoutingProtocol::~AomdvRoutingProtocol ()

{

}

void

AomdvRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

  m_ipv4 = ipv4;

}

Ptr<NetDevice>

AomdvRoutingProtocol::GetNetDevice (uint32_t interface)

{

  return m_ipv4->GetNetDevice (interface);

}

Ptr<MobilityModel>

AomdvRoutingProtocol::GetMobilityModel (Ptr<NetDevice> netDevice)

{

  return netDevice->GetNode ()->GetObject<MobilityModel> ();

}

std::vector<Ipv4Address>

AomdvRoutingProtocol::GetNextHops (Ipv4Address dest)

{

  // Simplified example of next hop selection based on AOMDV

  std::vector<Ipv4Address> nextHops;

  if (m_routingTable.find(dest) != m_routingTable.end())

    {

      for (const auto &entry : m_routingTable[dest])

        {

          nextHops.push_back(entry.GetGateway());

        }

    }

  return nextHops;

}

Ptr<Ipv4Route>

AomdvRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)

{

  Ipv4Address dest = header.GetDestination ();

  std::vector<Ipv4Address> nextHops = GetNextHops (dest);

  if (nextHops.empty())

    {

      sockerr = Socket::ERROR_NOROUTETOHOST;

      return nullptr;

    }

  // Select the first available next hop for simplicity

  Ipv4Address nextHop = nextHops[0];

  Ptr<Ipv4Route> route = Create<Ipv4Route> ();

  route->SetDestination (dest);

  route->SetGateway (nextHop);

  route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));

  return route;

}

bool

AomdvRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,

UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)

{

  Ipv4Address dest = header.GetDestination ();

  std::vector<Ipv4Address> nextHops = GetNextHops (dest);

  if (nextHops.empty())

    {

      ecb (p, header, Socket::ERROR_NOROUTETOHOST);

      return false;

    }

 

  // Select the first available next hop for simplicity

  Ipv4Address nextHop = nextHops[0];

  Ptr<Ipv4Route> route = Create<Ipv4Route> ();

  route->SetDestination (dest);

  route->SetGateway (nextHop);

  route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));

  ucb (route, p, header);

  return true;

}

void

AomdvRoutingProtocol::NotifyInterfaceUp (uint32_t interface)

{

}

void

AomdvRoutingProtocol::NotifyInterfaceDown (uint32_t interface)

{

}

void

AomdvRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

AomdvRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

AomdvRoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const

{

  *stream->GetStream () << “AOMDV Routing Table” << std::endl;

  for (auto const &entry : m_routingTable)

    {

      *stream->GetStream () << entry.first << ” -> “;

      for (const auto &routeEntry : entry.second)

        {

          *stream->GetStream () << routeEntry.GetGateway () << ” via ” << routeEntry.GetInterface () << “, “;

        }

      *stream->GetStream () << std::endl;

    }

}

 

} // namespace ns3

  1. Define AOMDV Helper

Create the AOMDV helper header file aomdv-helper.h in the helper directory.

#ifndef AOMDV_HELPER_H

#define AOMDV_HELPER_H

#include “ns3/ipv4-routing-helper.h”

#include “aomdv-routing-protocol.h”

namespace ns3 {

class AomdvHelper : public Ipv4RoutingHelper

{

public:

  AomdvHelper ();

  virtual ~AomdvHelper ();

  AomdvHelper* Copy (void) const;

  virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;

};

} // namespace ns3

#endif /* AOMDV_HELPER_H */

Create the AOMDV helper source file aomdv-helper.cc in the helper directory.

#include “aomdv-helper.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

AomdvHelper::AomdvHelper ()

{

}

AomdvHelper::~AomdvHelper ()

{

}

AomdvHelper*

AomdvHelper::Copy (void) const

{

  return new AomdvHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

AomdvHelper::Create (Ptr<Node> node) const

{

  Ptr<AomdvRoutingProtocol> aomdvRouting = CreateObject<AomdvRoutingProtocol> ();

  node->AggregateObject (aomdvRouting);

  return aomdvRouting;

}

} // namespace ns3

  1. Update CMakeLists.txt

Add the new AOMDV module to the ns3 build system. Edit src/CMakeLists.txt and add the following line:

add_subdirectory (aomdv)

Create src/aomdv/CMakeLists.txt with the following content:

ns3_add_library (aomdv

    model/aomdv-routing-protocol.cc

    helper/aomdv-helper.cc

)

target_link_libraries (aomdv)

  1. Set Up the Network Topology

Create a simulation script in the scratch directory to use the AOMDV protocol.

#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 “aomdv-helper.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;

  AomdvHelper aomdv;

  stack.SetRoutingHelper (aomdv);

  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 (“aomdv-simulation.tr”));

  wifiPhy.EnablePcapAll (“aomdv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

  1. Build and Run the Simulation

After writing your script, you need to build and run it.

./waf build

./waf –run scratch/aomdv-simulation

  1. Analyze the Results

After running the simulation, you can analyze the results using the generated trace files (aomdv-simulation.tr and aomdv-simulation-0-0.pcap).

Complete Example Script for AOMDV

Here is a sample script to complete the AOMDV protocol in ns3 environment:

#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/applications-module.h”

#include “aomdv-helper.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;

  AomdvHelper aomdv;

  stack.SetRoutingHelper (aomdv);

  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 (“aomdv-simulation.tr”));

  wifiPhy.EnablePcapAll (“aomdv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

As we discussed earlier about how the AOMDV will perform in ns3 environment and we help to deliver further information about how the AOMDV will adapt in diverse environments. For more programming assistance in AOMDV stay in touch with us.