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

How to Implement ABR protocol in ns3

To implement the associativity-based routing (ABR) protocol in ns-3 we consist to manage the ABR functionalities for make a custom module. specific kind of implementation support are provided as per your concept. Here, we are going to see how to implement a general ABR protocol in ns-3 environment.

Step-by-Step Guide to Implementing ABR in ns3

  1. Set up Your Environment

To make sure we have installed ns3 on the system.

  1. Create a New ns-3 Module

Create a new module for the ABR protocol in the src directory of your ns3 installation. This contains to make a necessary directory structures and files.

cd ns-3.xx

cd src

mkdir -p abr/model

mkdir -p abr/helper

  1. Create the ABR Protocol Header File

Create the ABR routing protocol header file abr-routing-protocol.h in the model directory.

#ifndef ABR_ROUTING_PROTOCOL_H

#define ABR_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 AbrRoutingProtocol : public Ipv4RoutingProtocol

{

public:

  static TypeId GetTypeId (void);

  AbrRoutingProtocol ();

  virtual ~AbrRoutingProtocol ();

  // 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, Ipv4RoutingTableEntry> m_routingTable;

  std::map<Ipv4Address, uint32_t> m_associativityTable;

  Ptr<NetDevice> GetNetDevice (uint32_t interface);

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

  Ipv4Address GetNextHop (Ipv4Address dest);

  void UpdateAssociativityTable ();

};

} // namespace ns3

#endif /* ABR_ROUTING_PROTOCOL_H */

  1. Create the ABR Protocol Source File

Create the ABR routing protocol source file abr-routing-protocol.cc in the model directory.

#include “abr-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“AbrRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (AbrRoutingProtocol);

TypeId

AbrRoutingProtocol::GetTypeId (void)

{

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

    .SetParent<Ipv4RoutingProtocol> ()

    .SetGroupName(“Internet”)

    .AddConstructor<AbrRoutingProtocol> ();

  return tid;

}

AbrRoutingProtocol::AbrRoutingProtocol ()

{

}

AbrRoutingProtocol::~AbrRoutingProtocol ()

{

}

void

AbrRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

  m_ipv4 = ipv4;

  // Start the periodic update for associativity table

  Simulator::Schedule(Seconds(1.0), &AbrRoutingProtocol::UpdateAssociativityTable, this);

}

void

AbrRoutingProtocol::UpdateAssociativityTable ()

{

  NS_LOG_FUNCTION (this);

  // Update the associativity table periodically

  for (auto &entry : m_associativityTable)

    {

      entry.second++;

    }

  // Schedule the next update

  Simulator::Schedule(Seconds(1.0), &AbrRoutingProtocol::UpdateAssociativityTable, this);

}

Ptr<NetDevice>

AbrRoutingProtocol::GetNetDevice (uint32_t interface)

{

  return m_ipv4->GetNetDevice (interface);

}

Ptr<MobilityModel>

AbrRoutingProtocol::GetMobilityModel (Ptr<NetDevice> netDevice)

{

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

}

Ipv4Address

AbrRoutingProtocol::GetNextHop (Ipv4Address dest)

{

  // Simplified example of next hop selection based on highest associativity

  Ipv4Address nextHop = Ipv4Address::GetBroadcast ();

  uint32_t maxAssociativity = 0;

  for (auto const &entry : m_routingTable)

    {

      if(entry.first==dest&&m_associativityTable[entry.second.GetGateway()]> maxAssociativity)

        {

          nextHop = entry.second.GetGateway();

          maxAssociativity = m_associativityTable[nextHop];

        }

    }

  return nextHop;

}

Ptr<Ipv4Route>

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

{

  Ipv4Address dest = header.GetDestination ();

  Ipv4Address nextHop = GetNextHop (dest);

  if (nextHop == Ipv4Address::GetBroadcast ())

    {

      sockerr = Socket::ERROR_NOROUTETOHOST;

      return nullptr;

    }

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

  route->SetDestination (dest);

  route->SetGateway (nextHop);

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

  return route;

}

bool

AbrRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice>idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)

{

  Ipv4Address dest = header.GetDestination ();

  Ipv4Address nextHop = GetNextHop (dest);

  if (nextHop == Ipv4Address::GetBroadcast ())

    {

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

      return false;

    }

  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

AbrRoutingProtocol::NotifyInterfaceUp (uint32_t interface)

{

}

void

AbrRoutingProtocol::NotifyInterfaceDown (uint32_t interface)

{

}

void

AbrRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

AbrRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

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

{

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

  for (auto const &entry : m_routingTable)

    {

      *stream->GetStream () << entry.first << ” -> ” << entry.second.GetGateway () << ” via ” << entry.second.GetInterface () << std::endl;

    }

}

} // namespace ns3

  1. Define ABR Helper

Create the ABR helper header file abr-helper.h in the helper directory.

#ifndef ABR_HELPER_H

#define ABR_HELPER_H

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

#include “abr-routing-protocol.h”

namespace ns3 {

class AbrHelper : public Ipv4RoutingHelper

{

public:

  AbrHelper ();

  virtual ~AbrHelper ();

  AbrHelper* Copy (void) const;

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

};

} // namespace ns3

#endif /* ABR_HELPER_H */

Create the ABR helper source file abr-helper.cc in the helper directory.

#include “abr-helper.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

AbrHelper::AbrHelper ()

{

}

AbrHelper::~AbrHelper ()

{

}

AbrHelper*

AbrHelper::Copy (void) const

{

  return new AbrHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

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

{

  Ptr<AbrRoutingProtocol> abrRouting = CreateObject<AbrRoutingProtocol> ();

  node->AggregateObject (abrRouting);

  return abrRouting;

}

} // namespace ns3

  1. Update CMakeLists.txt

Add the new ABR module to the ns-3 build system. Edit src/CMakeLists.txt and add the following line:

add_subdirectory (abr)

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

ns3_add_library (abr

    model/abr-routing-protocol.cc

    helper/abr-helper.cc

)

target_link_libraries (abr)

  1. Set Up the Network Topology

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

#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 “abr-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;

  AbrHelper abr;

  stack.SetRoutingHelper (abr);

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

  wifiPhy.EnablePcapAll (“abr-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

  1. Build and Run the Simulation

We need to build and run after writing the script.

./waf build

./waf –run scratch/abr-simulation

  1. Analyze the Results

After running the simulation, we can see the outcomes by generated trace files (abr-simulation.tr and abr-simulation-0-0.pcap).

Complete Example Script for ABR Protocol

Here we provide the sample script for the reference to complete the ABR protocol in ns3:

#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 “abr-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;

  AbrHelper abr;

  stack.SetRoutingHelper (abr);

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

  wifiPhy.EnablePcapAll (“abr-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Finally, we provide steps on  how to implementing the ABR protocol outcomes in ns-3 environment in detailed manner and also we provide related information for ABR based protocols that adjust in other environments with programming results.