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

How to Implement Multi Criteria based Routing in ns3

To implement a multi-criteria based routing protocol in ns3, we need to create a routing algorithm. In that algorithm multiple factors has to be considered such as delay, bandwidth, energy consumption, and more.

The following steps will guide on how to set-up this multi criteria based routing in ns3.

Step-by-step to implement this routing in ns3:

Step 1: Set Up the ns3 Environment

  1. Install ns3: Make sure that ns3 is installed on the system.

sudo apt-get update

sudo apt-get install ns3

Create a New ns-3 Project: Create a directory for the new project within the ns3 workspace.

cd ns-3

mkdir scratch/multi-criteria-routing

Step 2: Define the Multi-Criteria Routing Protocol

  1. Create the Routing Protocol Class: Define a new routing protocol class that implements the multi-criteria routing algorithm.

// MultiCriteriaRoutingProtocol.h

#ifndef MULTI_CRITERIA_ROUTING_PROTOCOL_H

#define MULTI_CRITERIA_ROUTING_PROTOCOL_H

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

#include “ns3/ipv4.h”

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

#include “ns3/timer.h”

#include <map>

#include <vector>

namespace ns3 {

class MultiCriteriaRoutingProtocol : public Ipv4RoutingProtocol {

public:

static TypeId GetTypeId (void);

MultiCriteriaRoutingProtocol ();

virtual ~MultiCriteriaRoutingProtocol ();

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, Time::Unit unit = Time::S) const;

private:

void DiscoverRoute (Ipv4Address dst);

double CalculateRouteMetric (const Ipv4RoutingTableEntry &entry);

Ptr<Ipv4> m_ipv4;

std::map<Ipv4Address, Ipv4RoutingTableEntry> m_routes;

};

} // namespace ns3

#endif // MULTI_CRITERIA_ROUTING_PROTOCOL_H

// MultiCriteriaRoutingProtocol.cc

#include “MultiCriteriaRoutingProtocol.h”

#include “ns3/log.h”

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

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“MultiCriteriaRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (MultiCriteriaRoutingProtocol);

TypeId MultiCriteriaRoutingProtocol::GetTypeId (void) {

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

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName (“Internet”)

.AddConstructor<MultiCriteriaRoutingProtocol> ();

return tid;

}

MultiCriteriaRoutingProtocol::MultiCriteriaRoutingProtocol () {}

MultiCriteriaRoutingProtocol::~MultiCriteriaRoutingProtocol () {}

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

m_ipv4 = ipv4;

}

 

void MultiCriteriaRoutingProtocol::NotifyInterfaceUp (uint32_t interface) {}

void MultiCriteriaRoutingProtocol::NotifyInterfaceDown (uint32_t interface) {}

void MultiCriteriaRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {}

void MultiCriteriaRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {}

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

Ipv4Address dst = header.GetDestination ();

// Check if a route exists in the routing table

if (m_routes.find (dst) != m_routes.end ()) {

Ipv4RoutingTableEntry route = m_routes[dst];

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

ipv4Route->SetDestination (route.GetDest ());

ipv4Route->SetGateway (route.GetGateway ());

ipv4Route->SetOutputDevice (m_ipv4->GetNetDevice (route.GetInterface ()));

ipv4Route->SetSource (route.GetSource ());

return ipv4Route;

}

// If no route exists, discover a route

DiscoverRoute (dst);

if (m_routes.find (dst) != m_routes.end ()) {

Ipv4RoutingTableEntry route = m_routes[dst];

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

ipv4Route->SetDestination (route.GetDest ());

ipv4Route->SetGateway (route.GetGateway ());

ipv4Route->SetOutputDevice (m_ipv4->GetNetDevice (route.GetInterface ()));

ipv4Route->SetSource (route.GetSource ());

return ipv4Route;

}

sockerr = Socket::ERROR_NOROUTETOHOST;

return 0;

}

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

Ipv4Address dst = header.GetDestination ();

// Check if the packet is for this node

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

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

if (m_ipv4->GetAddress (i, j).GetLocal () == dst) {

lcb (p, header, i);

return true;

}

}

}

// Check if a route exists in the routing table

if (m_routes.find (dst) != m_routes.end ()) {

Ipv4RoutingTableEntry route = m_routes[dst];

ucb (route, p, header);

return true;

}

// If no route exists, discover a route

DiscoverRoute (dst);

if (m_routes.find (dst) != m_routes.end ()) {

Ipv4RoutingTableEntry route = m_routes[dst];

ucb (route, p, header);

return true;

}

return false;

}

void MultiCriteriaRoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit) const {

*stream->GetStream () << “Multi-Criteria Routing Table:\n”;

for (auto &route : m_routes) {

route.second.Print (stream);

}

}

void MultiCriteriaRoutingProtocol::DiscoverRoute (Ipv4Address dst) {

// Implement route discovery logic here

// Example: Send route request packets and wait for replies

}

double MultiCriteriaRoutingProtocol::CalculateRouteMetric (const Ipv4RoutingTableEntry &entry) {

// Implement the logic to calculate route metrics based on multiple criteria

// Example: Combine delay, bandwidth, and energy consumption into a single metric

double delay = 1.0;  // Example delay value

double bandwidth = 1.0;  // Example bandwidth value

double energy = 1.0;  // Example energy consumption value

return delay + bandwidth + energy;  // Example combined metric

}

} // namespace ns3

Step 3: Integrate the Routing Protocol in a Simulation

  1. Create a New Simulation Script: Create a new script in the scratch directory to use the multi-criteria routing protocol.

// multi-criteria-routing.cc

#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 “MultiCriteriaRoutingProtocol.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“MultiCriteriaRoutingExample”);

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

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (4);

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

InternetStackHelper stack;

Ptr<MultiCriteriaRoutingProtocol>multiCriteriaRouting = CreateObject<MultiCriteriaRoutingProtocol> ();

stack.SetRoutingHelper (multiCriteriaRouting);

stack.Install (nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

UdpEchoServerHelper echoServer (9);

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

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 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));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 4: Compile and Run the Simulation

  1. Compile the Script: Compile the script using the waf build system.

./waf build

Run the Simulation: Run the simulation script and observe the results.

./waf –run scratch/multi-criteria-routing

Step 5: Enable Tracing and Analyze Results

  1. Enable Tracing: Add tracing to collect data for analysis.

AsciiTraceHelper ascii;

pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“multi-criteria-routing.tr”));

Run the Simulation: Set the simulation stop time and run it.

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

Step 6: Analyze the Results

After running the simulation, analyze the generated trace files (multi-criteria-routing.tr) to study the routing behavior and performance.

On the conclusion we get to know how to implement Multi criteria based routing protocol by following the routing algorithm to evaluate multiple factors in the protocol and integrating the routing protocol, enabling tracing for analyzing the results.

We’ve been focusing on Multi Criteria based Routing in ns3simulation and would love to help you out with any questions you have. Feel free to reach out to us for more guidance on your implementation. We can also provide you with detailed project performance results.