To implement a routing interface protocol in ns3, we have to create a new routing protocol class, that defines its behavior, and integrates it with ns3’s network stack.
Below are the steps to implement routing interface protocol.
Steps to implement routing interface protocol
- Set up your environment
Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website. Navigate to the src directory where you want to add your protocol.
- Create a new protocol class
Add two files named my-routing-protocol.h and my-routing-protocol.cc in the new directory of your protocol (eg., src/my-routing-protocol/).
my-routing-protocol.h
#ifndef MY_ROUTING_PROTOCOL_H
#define MY_ROUTING_PROTOCOL_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4.h”
#include “ns3/node.h”
#include “ns3/ipv4-route.h”
#include “ns3/net-device.h”
namespace ns3 {
class MyRoutingProtocol : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId (void);
MyRoutingProtocol ();
virtual ~MyRoutingProtocol ();
// Required by Ipv4RoutingProtocol
Ptr<Ipv4Route> RouteOutput (Ptr<Packet> packet, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
bool RouteInput (Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb);
void AddHostRouteTo (Ipv4Address dest, Ipv4Address nextHop, uint32_t interface);
void AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask,
Ipv4Address nextHop, uint32_t interface);
void SetIpv4 (Ptr<Ipv4> ipv4);
private:
Ptr<Ipv4> m_ipv4;
};
} // namespace ns3
#endif // MY_ROUTING_PROTOCOL_H
my-routing-protocol.cc
#include “my-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-routing-table-entry.h”
#include “ns3/ipv4-route.h”
#include “ns3/simulator.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“MyRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (MyRoutingProtocol);
TypeId
MyRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::MyRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName (“Internet”)
.AddConstructor<MyRoutingProtocol> ();
return tid;
}
MyRoutingProtocol::MyRoutingProtocol () {
NS_LOG_FUNCTION (this);
}
MyRoutingProtocol::~MyRoutingProtocol () {
NS_LOG_FUNCTION (this);
}
Ptr<Ipv4Route>
MyRoutingProtocol::RouteOutput (Ptr<Packet> packet, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) {
// Implement your routing logic here
NS_LOG_FUNCTION (this << packet << header << oif);
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
// Fill route details based on your protocol logic
return route;
}
bool
MyRoutingProtocol::RouteInput (Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) {
// Implement your routing logic here
NS_LOG_FUNCTION (this << packet << header << idev);
return false;
}
void
MyRoutingProtocol::AddHostRouteTo (Ipv4Address dest, Ipv4Address nextHop, uint32_t interface) {
NS_LOG_FUNCTION (this << dest << nextHop << interface);
// Implement your routing logic here
}
void
MyRoutingProtocol::AddNetworkRouteTo (Ipv4Address network, Ipv4Mask networkMask,
Ipv4Address nextHop, uint32_t interface) {
NS_LOG_FUNCTION (this << network << networkMask << nextHop << interface);
// Implement your routing logic here
}
void
MyRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4) {
NS_LOG_FUNCTION (this << ipv4);
m_ipv4 = ipv4;
}
} // namespace ns3
- Integrate with ns3
To include your new protocol, modify the wscript in the src directory and to ensure that your new files are compiled, add the following lines :
wscript
def build(bld):
…
bld.ns3_add_library(‘my-routing-protocol’,
[‘my-routing-protocol.cc’])
…
- Testing and validation
In the scratch directory, create a simulation script to test your new protocol :
scratch/test-my-routing-protocol.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/my-routing-protocol.h”
using namespace ns3;
int main (int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
Ptr<MyRoutingProtocol> myRoutingProtocol = CreateObject<MyRoutingProtocol> ();
stack.SetRoutingHelper (myRoutingProtocol);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
- Build and run the simulation
Build and run your script after writing.
./waf configure
./waf build
./waf –run scratch/ test-my-routing-protocol
Explanation of the code
- Header file (my-routing-protocol.h) :
The MyRoutingProtocol class is defined by inheriting from Application.
For starting and stopping the application, sending packets, scheduling transmissions, and handling received packets, it declares methods.
- Implementation file (my-routing-protocol.cc) :
The constructor and destructor are implemented.
For type identification, implements the GetTypeId method.
For starting and stopping the application, sending packets, scheduling transmissions, and handling received packets, it declares methods.
- Testing script (test–my-routing-protocol.cc) :
Sets up a basic network with two nodes connected by a point-to-point link.
Internet stacks are installed.
To the interfaces, IP addresses are assigned.
The custom protocol are configured as both a server and a client on different nodes and schedules the simulation to run for 10 seconds.
Overall, we had a performance analysis on routing interface protocol by implementing the routing protocol. Also, we provide more related implementation sustenance on routing interface protocol for your project.