How to Begin Implement DSR Routing in NS3

To implement Dynamic Source Routing (DSR) using NS3 which is a reactive routing protocol used for mobile ad hoc networks (MANETs). It functions by determining the routes only if required, sustaining source routes within packets, and assisting the route caching for effectiveness of routing.

NS3 supports to offer a solid foundation to execute the routing protocols such as DSR. Below is a comprehensive instruction for implementing the DSR Routing using NS3:

Steps to Begin Implement DSR Routing in NS3

Step 1: Understand DSR

  1. Key Features:
    • Route Discovery: Introduces a route request once a source node requires transmitting the information to a destination for which it doesn’t discover the route.
    • Route Maintenance: It observes the connections and renovates split routes.
    • Source Routing: Data packets effectively transmit the entire route to the destination.
  2. Core Messages:
    • Route Request (RREQ): Request overflowed within the network for determining the routes.
    • Route Reply (RREP): It transmits again to the source when a route is discovered.
    • Route Error (RERR): Broadcasted once a link is identified failure.

Step 2: Set Up NS3 Environment

  1. Create a Protocol Directory:
    • Make a protocol directory in src/, for example src/dsr-routing/.
  2. Add Necessary Files:
    • dsr-routing-protocol.cc: Execution file.
    • dsr-routing-protocol.h: Header file for DSR.
    • dsr-routing-helper.h and dsr-routing-helper.cc: It is a helper classes.
  3. Update Build System:
    • Fine-tune wscript in src/ with DSR component for updating the system.

Step 3: Design DSR Protocol

Define the Protocol Class

Execute the functionality of DSR for prolonging the Ipv4RoutingProtocol class.

Header File (dsr-routing-protocol.h)

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

#include “ns3/socket.h”

#include <map>

#include <vector>

class DsrRoutingProtocol : public ns3::Ipv4RoutingProtocol {

public:

static ns3::TypeId GetTypeId (void);

DsrRoutingProtocol ();

virtual ~DsrRoutingProtocol ();

// Override Ipv4RoutingProtocol methods

virtual ns3::Ptr<ns3::Ipv4Route> RouteOutput (

ns3::Ptr<const ns3::Packet> packet,

const ns3::Ipv4Header &header,

ns3::Ptr<ns3::NetDevice> oif,

ns3::Socket::SocketErrno &sockerr);

virtual bool RouteInput (

ns3::Ptr<const ns3::Packet> packet,

const ns3::Ipv4Header &header,

ns3::Ptr<const ns3::NetDevice> idev,

ns3::UnicastForwardCallback ucb,

ns3::MulticastForwardCallback mcb,

ns3::LocalDeliverCallback lcb,

ns3::ErrorCallback ecb);

void SendRouteRequest (ns3::Ipv4Address destination);

void SendRouteReply (ns3::Ipv4Address destination, std::vector<ns3::Ipv4Address> path);

void HandleRouteRequest (ns3::Ptr<const ns3::Packet> packet);

void HandleRouteReply (ns3::Ptr<const ns3::Packet> packet);

void HandleRouteError (ns3::Ptr<const ns3::Packet> packet);

private:

struct RouteCacheEntry {

std::vector<ns3::Ipv4Address> path; // Full path to the destination

};

std::map<ns3::Ipv4Address, RouteCacheEntry> m_routeCache; // Route cache

ns3::Ptr<ns3::Socket> m_socket;                           // Communication socket

};

Implement Core Functions

Route Discovery

Transmit and manage the route discovery messages such as RREQ and RREP.

void DsrRoutingProtocol::SendRouteRequest (ns3::Ipv4Address destination) {

ns3::Ptr<ns3::Packet> rreqPacket = ns3::Create<ns3::Packet> ();

// Serialize RREQ message

// …

m_socket->SendTo(rreqPacket, 0, ns3::InetSocketAddress(ns3::Ipv4Address::GetBroadcast(), 9999));

}

void DsrRoutingProtocol::HandleRouteRequest (ns3::Ptr<const ns3::Packet> packet) {

// Deserialize RREQ message

ns3::Ipv4Address destination = …; // Extract destination

if (destination == m_selfAddress) {

// Send RREP to the source

std::vector<ns3::Ipv4Address> path = …; // Extract path from RREQ

SendRouteReply(packet->GetSource(), path);

} else {

// Forward RREQ

// Add self to the path and rebroadcast

}

}

void DsrRoutingProtocol::SendRouteReply (ns3::Ipv4Address destination, std::vector<ns3::Ipv4Address> path) {

ns3::Ptr<ns3::Packet> rrepPacket = ns3::Create<ns3::Packet> ();

// Serialize RREP message

// …

m_socket->SendTo(rrepPacket, 0, ns3::InetSocketAddress(destination, 9999));

}

void DsrRoutingProtocol::HandleRouteReply (ns3::Ptr<const ns3::Packet> packet) {

// Deserialize RREP message

ns3::Ipv4Address destination = …; // Extract destination

std::vector<ns3::Ipv4Address> path = …; // Extract path

// Cache the route

m_routeCache[destination] = {path};

}

Route Maintenance

Manage the route errors and renovate the failure paths.

void DsrRoutingProtocol::HandleRouteError (ns3::Ptr<const ns3::Packet> packet) {

// Deserialize RERR message

ns3::Ipv4Address brokenLink = …; // Extract broken link

// Remove all routes containing the broken link from the cache

for (auto it = m_routeCache.begin(); it != m_routeCache.end(); ) {

if (std::find(it->second.path.begin(), it->second.path.end(), brokenLink) != it->second.path.end()) {

it = m_routeCache.erase(it);

} else {

++it;

}

}

}

Packet Forwarding

To transmit the packets, we can utilize the route cache.

ns3::Ptr<ns3::Ipv4Route> DsrRoutingProtocol::RouteOutput (

ns3::Ptr<const ns3::Packet> packet,

const ns3::Ipv4Header &header,

ns3::Ptr<ns3::NetDevice> oif,

ns3::Socket::SocketErrno &sockerr) {

auto it = m_routeCache.find(header.GetDestination());

if (it != m_routeCache.end()) {

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

route->SetGateway(it->second.path[1]); // Next hop

route->SetOutputDevice(oif);

return route;

}

sockerr = ns3::Socket::ERROR_NOROUTETOHOST;

return nullptr;

}

Step 4: Write a Helper Class

Enable incorporation to NS3 simulations for creating the helper class.

#include “dsr-routing-protocol.h”

class DsrRoutingHelper {

public:

void Install (ns3::NodeContainer nodes) {

for (auto it = nodes.Begin(); it != nodes.End(); ++it) {

ns3::Ptr<DsrRoutingProtocol> protocol = ns3::CreateObject<DsrRoutingProtocol>();

(*it)->AggregateObject(protocol);

}

}

};

Step 5: Write a Simulation Script

Example Simulation Script

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “dsr-routing-helper.h”

using namespace ns3;

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

NodeContainer nodes;

nodes.Create (5);

InternetStackHelper stack;

stack.Install (nodes);

DsrRoutingHelper dsrHelper;

dsrHelper.Install(nodes);

PointToPointHelper p2p;

p2p.SetDeviceAttribute (“DataRate”, StringValue (“10Mbps”));

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

p2p.Install (nodes.Get (0), nodes.Get (1));

p2p.Install (nodes.Get (1), nodes.Get (2));

p2p.Install (nodes.Get (2), nodes.Get (3));

p2p.Install (nodes.Get (3), nodes.Get (4));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 6: Compile and Run

  1. Build the Protocol:

./waf configure

./waf build

  1. Run the Simulation:

./waf –run your-script

Step 7: Analyze and Extend

  1. Tracing:
    • Examine RREQ, RREP, and RERR messages with the support of AsciiTraceHelper or PcapHelper.
  2. Enhancements:
    • Integrate route enhancements such as route shortening.
    • Include mobility scenarios to analyze the dynamic behavior of DSR routing.

In this manual, step-by-step implementation instructions have been provided for executing and analyzing the DSR Routing in NS3 environment. If further insights or additional explanations are required, we are ready to assist.