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

How to implement border gateway protocol in ns3

To implement the border gateway protocol (BGP) in ns3 we have to make the custom modules to manage the BGP functionalities. BGP related protocols based on your area are worked by us.  This procedure will show you how to set up a general BGP protocol in ns3.

Step-by-Step Guide to Implementing BGP in ns-3

  1. Set Up Your Environment

To make certain ns3 is installed. If not installed then download it from official website.

  1. Create a New ns-3 Module

Create a new module for BGP in the src directory of your ns-3 installation. This involves creating the necessary directory structure and files.

cd ns-3.xx

cd src

mkdir -p bgp/model

mkdir -p bgp/helper

3.      Create the BGP Protocol Header File

Create the BGP routing protocol header file bgp-routing-protocol.h in the model directory.

#ifndef BGP_ROUTING_PROTOCOL_H

#define BGP_ROUTING_PROTOCOL_H

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

#include “ns3/ipv4-address.h”

#include “ns3/timer.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 BgpRoutingProtocol : public Ipv4RoutingProtocol

{

public:

  static TypeId GetTypeId (void);

  BgpRoutingProtocol ();

  virtual ~BgpRoutingProtocol ();

  // 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;

  void SendBGPUpdate (Ipv4Address dest);

  void ProcessBGPUpdate (Ptr<Packet> packet, Ipv4Address from);

  Ptr<NetDevice> GetNetDevice (uint32_t interface);

  Ipv4Address GetNextHop (Ipv4Address dest);

};

} // namespace ns3

#endif /* BGP_ROUTING_PROTOCOL_H */

4.      Create the BGP Protocol Source File

Create the BGP routing protocol source file bgp-routing-protocol.cc in the model directory.

#include “bgp-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“BgpRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (BgpRoutingProtocol);

TypeId

BgpRoutingProtocol::GetTypeId (void)

{

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

    .SetParent<Ipv4RoutingProtocol> ()

    .SetGroupName(“Internet”)

    .AddConstructor<BgpRoutingProtocol> ();

  return tid;

}

BgpRoutingProtocol::BgpRoutingProtocol ()

{

}

BgpRoutingProtocol::~BgpRoutingProtocol ()

{

}

void

BgpRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

  m_ipv4 = ipv4;

}

Ptr<NetDevice>

BgpRoutingProtocol::GetNetDevice (uint32_t interface)

{

  return m_ipv4->GetNetDevice (interface);

}

Ipv4Address

BgpRoutingProtocol::GetNextHop (Ipv4Address dest)

{

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

    {

      return m_routingTable[dest].GetGateway();

    }

  return Ipv4Address::GetBroadcast ();

}

Ptr<Ipv4Route>

BgpRoutingProtocol::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 ())

    {

      SendBGPUpdate (dest);

      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

BgpRoutingProtocol::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 ())

    {

      SendBGPUpdate (dest);

      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

BgpRoutingProtocol::SendBGPUpdate (Ipv4Address dest)

{

  // Implementation of BGP Update packet sending

  NS_LOG_FUNCTION (this << dest);

}

void

BgpRoutingProtocol::ProcessBGPUpdate (Ptr<Packet> packet, Ipv4Address from)

{

  // Implementation of BGP Update packet processing

  NS_LOG_FUNCTION (this << from);

}

void

BgpRoutingProtocol::NotifyInterfaceUp (uint32_t interface)

{

}

void

BgpRoutingProtocol::NotifyInterfaceDown (uint32_t interface)

{

}

void

BgpRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

BgpRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

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

{

  *stream->GetStream () << “BGP 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

5.      Define BGP Helper

Create the BGP helper header file bgp-helper.h in the helper directory.

#ifndef BGP_HELPER_H

#define BGP_HELPER_H

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

#include “bgp-routing-protocol.h”

namespace ns3 {

class BgpHelper : public Ipv4RoutingHelper

{

public:

  BgpHelper ();

  virtual ~BgpHelper ();

  BgpHelper* Copy (void) const;

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

};

} // namespace ns3

#endif /* BGP_HELPER_H */

Create the BGP helper source file bgp-helper.cc in the helper directory.

#include “bgp-helper.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

BgpHelper::BgpHelper ()

{

}

 

BgpHelper::~BgpHelper ()

{

}

BgpHelper*

BgpHelper::Copy (void) const

{

  return new BgpHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

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

{

  Ptr<BgpRoutingProtocol> bgpRouting = CreateObject<BgpRoutingProtocol> ();

  node->AggregateObject (bgpRouting);

  return bgpRouting;

}

} // namespace ns3

6.      Update CMakeLists.txt

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

add_subdirectory (bgp)

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

ns3_add_library (bgp

    model/bgp-routing-protocol.cc

    helper/bgp-helper.cc

)

target_link_libraries (bgp)

7.      Set Up the Network Topology

Create a simulation script in the scratch directory to use the BGP 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 “bgp-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (4);

  // Set up point-to-point links

  PointToPointHelper pointToPoint;

  pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

  pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

  NetDeviceContainer devices;

  devices = pointToPoint.Install (nodes.Get(0), nodes.Get(1));

  devices = pointToPoint.Install (nodes.Get(1), nodes.Get(2));

  devices = pointToPoint.Install (nodes.Get(2), nodes.Get(3));

  // Install the internet stack on nodes

  InternetStackHelper stack;

  BgpHelper bgp;

  stack.SetRoutingHelper (bgp);

  stack.Install (nodes);

  // Assign IP addresses to the devices

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (Ipv4Address (“10.1.1.4”), 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;

  pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“bgp-simulation.tr”));

  pointToPoint.EnablePcapAll (“bgp-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

8.      Build and Run the Simulation

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

./waf build

./waf –run scratch/bgp-simulation

9.      Analyze the Results

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

Complete Example Script for BGP Protocol

At this point we have provided the sample script reference for BGP 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 “bgp-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (4);

  // Set up point-to-point links

  PointToPointHelper pointToPoint;  pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));  pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

  NetDeviceContainer devices;

  devices = pointToPoint.Install (nodes.Get(0), nodes.Get(1));

  devices = pointToPoint.Install (nodes.Get(1), nodes.Get(2));

  devices = pointToPoint.Install (nodes.Get(2), nodes.Get(3));

  // Install the internet stack on nodes

  InternetStackHelper stack;

  BgpHelper bgp;

  stack.SetRoutingHelper (bgp);

  stack.Install (nodes);

  // Assign IP addresses to the devices

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (Ipv4Address (“10.1.1.4”), 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;

  pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“bgp-simulation.tr”));

  pointToPoint.EnablePcapAll (“bgp-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Here we have evaluated the BGP protocol in ns3 simulation tool to verified that will run smoothly to make custom modules and also we provide the further information about how the BGP will adapt in diverse environments. We carry on implementation for all BGP projects.