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

How to Implement path vector routing in ns3

Path Vector routing is a type of network routing protocol which maintains the path information that gets updated dynamically. The example of a path vector routing protocol is BGP (Border Gateway Protocol). Below are steps to implement a basic Path Vector routing protocol in ns3.

Steps for implementation

  1. Set up your ns3 :
  • Make sure that ns3 is installed in the computer. If not, install it.

 

  1. Include necessary libraries :
  • In your script, include the necessary libraries.

 

  1. Define network topology :
  • For your network topology, create the nodes and links.
  1. Install Internet Stack :
  • On your nodes, install the internet stack.
  1. Implement the path vector routing protocol :
  • Create a custom routing protocol class to implement the behavior of path vector routing protocol.
  1. Integrate the Custom Routing Protocol :
  • On the ns3 stack, integrate your custom routing protocol.
  1. Set Up Applications :
  • To generate traffic and test the routing, install applications.

 

 

  1. Run the Simulation :
  • Define the simulation parameters and run it.

Example implementation in C++

 

Here is a complete example on implementing a basic path vector routing protocol in ns3:

 

Include libraries :

#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/ipv4-routing-protocol.h”

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

#include <map>

#include <vector>

 

Define network topology :

using namespace ns3;

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

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(6);

 

// Define point-to-point connections

PointToPointHelper pointToPoint;

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

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

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

NetDeviceContainer devices02 = pointToPoint.Install(nodes.Get(0), nodes.Get(2));

NetDeviceContainer devices13 = pointToPoint.Install(nodes.Get(1), nodes.Get(3));

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

NetDeviceContainer devices34 = pointToPoint.Install(nodes.Get(3), nodes.Get(4));

NetDeviceContainer devices35 = pointToPoint.Install(nodes.Get(3), nodes.Get(5));

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces01 = address.Assign(devices01);

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

Ipv4InterfaceContainer interfaces02 = address.Assign(devices02);

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

Ipv4InterfaceContainer interfaces13 = address.Assign(devices13);

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

Ipv4InterfaceContainer interfaces23 = address.Assign(devices23);

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

Ipv4InterfaceContainer interfaces34 = address.Assign(devices34);

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

Ipv4InterfaceContainer interfaces35 = address.Assign(devices35);

 

Implement the Path Vector Routing Protocol :

Create a new header file path-vector-routing.h

#ifndef PATH_VECTOR_ROUTING_H

#define PATH_VECTOR_ROUTING_H

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

#include “ns3/ipv4.h”

#include “ns3/net-device.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

#include <map>

#include <vector>

namespace ns3 {

class PathVectorRouting : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId(void);

PathVectorRouting();

virtual ~PathVectorRouting();

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

void Start();

private:

void SendPathVector();

void ReceivePathVector(Ptr<Socket> socket);

void UpdateRoutingTable(Ipv4Address source, std::vector<Ipv4Address> path);

Ptr<Ipv4> m_ipv4;

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

std::map<uint32_t, Ptr<Socket>> m_socketMap;

Time m_updateInterval;

EventId m_updateEvent;

};

}

#endif // PATH_VECTOR_ROUTING_H

Create the corresponding implementation file path-vector-routing.cc:

#include “path-vector-routing.h”

#include “ns3/log.h”

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

#include “ns3/simulator.h”

#include “ns3/inet-socket-address.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE(“PathVectorRouting”);

NS_OBJECT_ENSURE_REGISTERED(PathVectorRouting);

TypeId PathVectorRouting::GetTypeId(void) {

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

.SetParent<Ipv4RoutingProtocol>()

.SetGroupName(“Internet”)

.AddConstructor<PathVectorRouting>();

return tid;

}

PathVectorRouting::PathVectorRouting() : m_updateInterval(Seconds(5)) {

NS_LOG_FUNCTION(this);

Simulator::Schedule(Seconds(1.0), &PathVectorRouting::Start, this);

}

PathVectorRouting::~PathVectorRouting() {

NS_LOG_FUNCTION(this);

}

void PathVectorRouting::SetIpv4(Ptr<Ipv4> ipv4) {

NS_LOG_FUNCTION(this << ipv4);

m_ipv4 = ipv4;

}

void PathVectorRouting::Start() {

NS_LOG_FUNCTION(this);

SendPathVector();

m_updateEvent = Simulator::Schedule(m_updateInterval, &PathVectorRouting::Start, this);

}

void PathVectorRouting::SendPathVector() {

NS_LOG_FUNCTION(this);

Ptr<Packet> packet = Create<Packet>();

for (auto const &entry : m_routingTable) {

Ipv4Address source = entry.first;

for (auto const &path : entry.second) {

packet->AddHeader(Ipv4Header(source, path, 0, 0));

}

}

for (uint32_t i = 0; i < m_ipv4->GetNInterfaces(); ++i) {

Ptr<Socket> socket = m_socketMap[i];

socket->SendTo(packet, 0, InetSocketAddress(Ipv4Address::GetBroadcast(), 80));

}

}

void PathVectorRouting::ReceivePathVector(Ptr<Socket> socket) {

NS_LOG_FUNCTION(this);

Ptr<Packet> packet = socket->Recv();

Ipv4Header header;

packet->RemoveHeader(header);

Ipv4Address source = header.GetSource();

Ipv4Address path = header.GetDestination();

std::vector<Ipv4Address> paths;

paths.push_back(path);

UpdateRoutingTable(source, paths);

}

void PathVectorRouting::UpdateRoutingTable(Ipv4Address source, std::vector<Ipv4Address> paths) {

NS_LOG_FUNCTION(this << source);

for (auto const &path : paths) {

m_routingTable.push_back(path);

}

}

Ptr<Ipv4Route> PathVectorRouting::RouteOutput(

Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif,

Socket::SocketErrno &sockerr) {

NS_LOG_FUNCTION(this << p << header << oif << sockerr);

Ipv4Address dest = header.GetDestination();

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

sockerr = Socket::ERROR_NOROUTETOHOST;

return 0;

}

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

route->SetDestination(dest);

route->SetGateway(m_routingTable[dest].front());  // Choose the first path for simplicity

route->SetOutputDevice(oif);

return route;

}

bool PathVectorRouting::RouteInput(

Ptr<const Packet> p, const Ipv4Header &header,

Ptr<const NetDevice> idev, UnicastForwardCallback ucb,

MulticastForwardCallback mcb, LocalDeliverCallback lcb,

ErrorCallback ecb) {

NS_LOG_FUNCTION(this << p << header << idev << ucb << mcb

<< lcb << ecb);

Ipv4Address dest = header.GetDestination();

if (dest == m_ipv4->GetAddress(1, 0).GetLocal()) {

lcb(p, header, idev);

return true;

}

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

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

return false;

}

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

route->SetDestination(dest);

route->SetGateway(m_routingTable[dest].front());  // Choose the first path for simplicity

route->SetOutputDevice(idev);

ucb(route, p, header);

return true;

}

void PathVectorRouting::NotifyInterfaceUp(uint32_t interface) {

NS_LOG_FUNCTION(this << interface);

Ptr<Socket> socket = Socket::CreateSocket(GetObject<Node>(), TypeId::LookupByName(“ns3::UdpSocketFactory”));

socket->SetAllowBroadcast(true);

socket->BindToNetDevice(m_ipv4->GetNetDevice(interface));

socket->Bind(InetSocketAddress(Ipv4Address::GetAny(), 80));

socket->SetRecvCallback(MakeCallback(&PathVectorRouting::ReceivePathVector, this));

m_socketMap[interface] = socket;

}

void PathVectorRouting::NotifyInterfaceDown(uint32_t interface) {

NS_LOG_FUNCTION(this << interface);

m_socketMap[interface]->Close();

m_socketMap.erase(interface);

}

void PathVectorRouting::NotifyAddAddress(uint32_t interface,

Ipv4InterfaceAddress address) {

NS_LOG_FUNCTION(this << interface << address);

}

void PathVectorRouting::NotifyRemoveAddress(uint32_t interface,

Ipv4InterfaceAddress address) {

NS_LOG_FUNCTION(this << interface << address);

}

}

Integrate the Custom Routing Protocol :

#include “path-vector-routing.h”

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

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(6);

PointToPointHelper pointToPoint;

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

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

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

NetDeviceContainer devices02 = pointToPoint.Install(nodes.Get(0), nodes.Get(2));

NetDeviceContainer devices13 = pointToPoint.Install(nodes.Get(1), nodes.Get(3));

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

NetDeviceContainer devices34 = pointToPoint.Install(nodes.Get(3), nodes.Get(4));

NetDeviceContainer devices35 = pointToPoint.Install(nodes.Get(3), nodes.Get(5));

InternetStackHelper stack;

PathVectorRoutingHelper pathVectorRouting;

Ipv4ListRoutingHelper list;

list.Add(pathVectorRouting, 0);

stack.SetRoutingHelper(list);

stack.Install(nodes);

Ipv4AddressHelper address;

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

address.Assign(devices01);

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

address.Assign(devices02);

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

address.Assign(devices13);

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

address.Assign(devices23);

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

address.Assign(devices34);

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

address.Assign(devices35);

// Set up applications

uint16_t port = 9;

UdpEchoServerHelper server(port);

ApplicationContainer apps = server.Install(nodes.Get(5));

apps.Start(Seconds(1.0));

apps.Stop(Seconds(10.0));

UdpEchoClientHelper client(address.GetAddress(5), port);

client.SetAttribute(“MaxPackets”, UintegerValue(1));

client.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

client.SetAttribute(“PacketSize”, UintegerValue(1024));

apps = client.Install(nodes.Get(0));

apps.Start(Seconds(2.0));

apps.Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

Return

 

Explanation

  1. Network Topology :

The six nodes connected in a specific configuration, to set up the network topology.

  1. path vector routing protocol Routing Protocol :

A custom path vector routing protocol is implemented for handling route computation and distribution based on path vector principles.

  1. Route Computation :

The UpdateRoutingTable method updates the routing table based on received path vectors.

  1. Path Vector Updates :

The SendLinkPathVector method is used to send the current link-state database to neighbors, and the ReceiveLinkPathVector method is used to process incoming path vectors and update the routing table.

  1. Route Input and Output :

To handle packet routing based on the routing table, the RouteInput and RouteOutput methods are used.

  1. Integrate Custom Routing Protocol :

The custom routing protocol is integrated into the ns3 stack using the PathVector RoutingHelper.

  1. Applications :

To set up to test the routing, UdpEchoServer and UdpEchoClient applications are used.

Running the Code :

Save your script to a file, for example, path-vector-routing.cc and Compile the script using the ns3 build system

./waf configure –enable-examples

./waf build

./waf –run scratch/path-vector-routing

Overall, we had successfully learned on implementing Path Vector routing in ns3 which maintains the path information that gets updated dynamically. Carry on Path Vector routing projects with our expert support.