To implement the dynamic source Routing (DSR) in ns3, the routing protocols is used for wireless mesh networks and mobile ad hoc networks and it include the essential classes and approaches to support the protocol features.
Read the below listed steps that are mentioned below.
Steps to Implement DSR in ns3
- Set up Your ns3 Workspace: Make sure ns3 is installed in the computer and configure with working directory.
- Create a New Module for DSR:
- Navigate to the src directory of ns3 and create a new directory named dsr.
- Inside the dsr directory, create subdirectories: model, helper, and examples.
- Define the DSR Protocol:
- In the model directory, create .cc and .h files for the DSR protocol. Define the DsrRoutingProtocol class that inherits from the Ipv4RoutingProtocol class.
- Implement DSR Classes:
- The key elements of DSR are route discovery, route maintenance, and packet forwarding.
- Create classes for the DSR control messages: Route Request (RREQ), Route Reply (RREP), and Route Error (RERR).
- Integrate DSR with ns3 Routing System:
- Modify the RoutingHelper class to include DSR.
- Register DSR as a routing protocol in ns3.
- Create a Simulation Script:
- Set up a network topology.
- Install the Internet stack.
- To enable DSR by the help of DSR routing.
- Set up applications and run the simulation.
Example Code Structure
The given below are the sample snippets to implement the DSR in ns3 project:
- Define DSR Routing Protocol
// src/dsr/model/dsr-routing-protocol.h
#ifndef DSR_ROUTING_PROTOCOL_H
#define DSR_ROUTING_PROTOCOL_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4.h”
#include “ns3/ipv4-l3-protocol.h”
#include “ns3/ipv4-route.h”
#include “ns3/node.h”
#include “ns3/net-device.h”
#include “ns3/socket.h”
#include “ns3/timer.h”
#include <vector>
#include <map>
namespace ns3 {
class DsrRoutingProtocol : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
DsrRoutingProtocol ();
virtual ~DsrRoutingProtocol ();
// 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);
void RecvDsr (Ptr<Socket> socket);
private:
void Start ();
void Stop ();
void SendRreq (Ipv4Address dest);
void SendRrep (Ipv4Address dest, std::vector<Ipv4Address> route);
void SendRerr (Ipv4Address dest);
Ptr<Ipv4> m_ipv4;
std::map<Ptr<Socket>, Ipv4InterfaceAddress> m_socketAddresses;
std::map<Ipv4Address, std::vector<Ipv4Address>> m_routeCache;
};
} // namespace ns3
#endif // DSR_ROUTING_PROTOCOL_H
2. Implement DSR Routing Protocol
// src/dsr/model/dsr-routing-protocol.cc
#include “dsr-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-static-routing.h”
#include “ns3/ipv4-static-routing-helper.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“DsrRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (DsrRoutingProtocol);
TypeId
DsrRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::DsrRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName (“Internet”)
.AddConstructor<DsrRoutingProtocol> ();
return tid;
}
DsrRoutingProtocol::DsrRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
DsrRoutingProtocol::~DsrRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
void
DsrRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION (this << ipv4);
m_ipv4 = ipv4;
}
void
DsrRoutingProtocol::NotifyInterfaceUp (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
DsrRoutingProtocol::NotifyInterfaceDown (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
DsrRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
DsrRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
DsrRoutingProtocol::Start ()
{
NS_LOG_FUNCTION (this);
}
void
DsrRoutingProtocol::Stop ()
{
NS_LOG_FUNCTION (this);
}
void
DsrRoutingProtocol::SendRreq (Ipv4Address dest)
{
NS_LOG_FUNCTION (this << dest);
// Implement the logic to send a Route Request (RREQ)
}
void
DsrRoutingProtocol::SendRrep (Ipv4Address dest, std::vector<Ipv4Address> route)
{
NS_LOG_FUNCTION (this << dest << route);
// Implement the logic to send a Route Reply (RREP)
}
void
DsrRoutingProtocol::SendRerr (Ipv4Address dest)
{
NS_LOG_FUNCTION (this << dest);
// Implement the logic to send a Route Error (RERR)
}
Ptr<Ipv4Route>
DsrRoutingProtocol::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 ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
if (m_routeCache.find (dest) != m_routeCache.end ())
{
route->SetDestination (dest);
route->SetOutputDevice (oif);
route->SetGateway (m_routeCache[dest].back ());
return route;
}
else
{
SendRreq (dest);
sockerr = Socket::ERROR_NOROUTETOHOST;
return nullptr;
}
}
bool
DsrRoutingProtocol::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 (m_routeCache.find (dest) != m_routeCache.end ())
{
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
route->SetDestination (dest);
route->SetOutputDevice (m_ipv4->GetNetDevice (0)); // Example output device
route->SetGateway (m_routeCache[dest].back ());
ucb (route, p, header);
return true;
}
else
{
SendRreq (dest);
return false;
}
}
void
DsrRoutingProtocol::RecvDsr (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
// Implement the logic to receive DSR control messages
}
} // namespace ns3
Helper Class
// src/dsr/helper/dsr-helper.h
#ifndef DSR_HELPER_H
#define DSR_HELPER_H
#include “ns3/ipv4-routing-helper.h”
#include “ns3/object-factory.h”
#include “ns3/dsr-routing-protocol.h”
namespace ns3 {
class DsrHelper : public Ipv4RoutingHelper
{
public:
DsrHelper ();
DsrHelper (const DsrHelper &);
DsrHelper &operator= (const DsrHelper &);
virtual ~DsrHelper ();
virtual DsrHelper* Copy (void) const;
virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
};
} // namespace ns3
#endif // DSR_HELPER_H
3. Implement Helper Class
// src/dsr/helper/dsr-helper.cc
#include “dsr-helper.h”
#include “ns3/dsr-routing-protocol.h”
#include “ns3/node.h”
namespace ns3 {
DsrHelper::DsrHelper ()
{
}
DsrHelper::DsrHelper (const DsrHelper &o)
{
}
DsrHelper&
DsrHelper::operator= (const DsrHelper &o)
{
return *this;
}
DsrHelper::~DsrHelper ()
{
}
DsrHelper*
DsrHelper::Copy (void) const
{
return new DsrHelper (*this);
}
Ptr<Ipv4RoutingProtocol>
DsrHelper::Create (Ptr<Node> node) const
{
Ptr<DsrRoutingProtocol> protocol = CreateObject<DsrRoutingProtocol> ();
node->AggregateObject (protocol);
return protocol;
}
} // namespace ns3
Example Simulation Script
Here, we provide the script to complete the simulation for DSR:
// examples/dsr-simulation.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 “ns3/dsr-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“DsrSimulation”);
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (4);
// Create point-to-point links
PointToPointHelper p2p;
p2p.SetDeviceAttribute (“DataRate”, StringValue (“1Mbps”));
p2p.SetChannelAttribute (“Delay”, StringValue (“10ms”));
NetDeviceContainer devices01 = p2p.Install (nodes.Get (0), nodes.Get (1));
NetDeviceContainer devices12 = p2p.Install (nodes.Get (1), nodes.Get (2));
NetDeviceContainer devices23 = p2p.Install (nodes.Get (2), nodes.Get (3));
NetDeviceContainer devices30 = p2p.Install (nodes.Get (3), nodes.Get (0));
// Install the Internet stack
InternetStackHelper stack;
DsrHelper dsr;
stack.SetRoutingHelper (dsr);
stack.Install (nodes);
// Assign IP addresses
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 interfaces12 = address.Assign (devices12);
address.SetBase (“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);
address.SetBase (“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces30 = address.Assign (devices30);
// Create a packet sink to receive packets
uint16_t sinkPort = 8080;
Address sinkAddress (InetSocketAddress (Ipv4Address (“10.1.4.1”), sinkPort));
PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, sinkAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (3));
sinkApps.Start (Seconds (1.0));
sinkApps.Stop (Seconds (10.0));
// Create a TCP client to send packets
OnOffHelper clientHelper (“ns3::TcpSocketFactory”, sinkAddress);
clientHelper.SetAttribute (“OnTime”, StringValue (“ns3::ConstantRandomVariable[Constant=1]”));
clientHelper.SetAttribute (“OffTime”, StringValue (“ns3::ConstantRandomVariable[Constant=0]”));
clientHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
clientHelper.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = clientHelper.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream (“dsr-routing.tr”));
p2p.EnablePcapAll (“dsr-routing”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Running the Simulation
To run the simulation, compile the script and execute it:
./waf configure –enable-examples
./waf build
./waf –run dsr-simulation
Overall, we provide the detailed procedures about how to setup a network to sample snippet to execute the DSR routing protocol in ns3 projects. Are you in need of system development support then reach us out at ns3simualion.com.