To implement the distance vector routing in ns3, we make the custom routing protocol for manage the table of each nodes that each node sustains the minimum distance to each and every node then it updates this table based on information established from its neighbours. This was stimulated by classic algorithm like Routing Information Protocol (RIP).
The given below is the detailed procedure to implement the distance vector routing protocol in ns3:
Step-by-Step Implementation
- Set Up ns3 Environment: To make sure ns3 is installed in the system.
- Include Necessary Libraries: To contains the needed ns3 libraries in the script.
- Define Network Topology: To build network topology by making nodes and the links.
- Install Internet Stack: download the Internet stack on your nodes.
- Implement the Distance Vector Routing Protocol: To simulate distance vector routing principles by making the custom routing protocol class. .
- Integrate the Custom Routing Protocol: Integrate your custom routing protocol into the ns3 stack.
- Set up Applications: download applications to generate traffic and test the routing.
- Run the Simulation: Configure the simulation parameters and run it.
Example Implementation in C++
Below are the samples that are required to simulate the distance 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(4);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices01 = pointToPoint.Install(nodes.Get(0), nodes.Get(1));
NetDeviceContainer devices12 = pointToPoint.Install(nodes.Get(1), nodes.Get(2));
NetDeviceContainer devices23 = pointToPoint.Install(nodes.Get(2), nodes.Get(3));
NetDeviceContainer devices30 = pointToPoint.Install(nodes.Get(3), nodes.Get(0));
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 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);
- Implement the Distance Vector Routing Protocol:
- Create a new header file distance-vector-routing.h:
#ifndef DISTANCE_VECTOR_ROUTING_H
#define DISTANCE_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 DistanceVectorRouting : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId(void);
DistanceVectorRouting();
virtual ~DistanceVectorRouting();
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 SendDistanceVector();
void ReceiveDistanceVector(Ptr<Socket> socket);
void UpdateRoutingTable(Ipv4Address source, std::map<Ipv4Address, uint32_t> distances);
Ptr<Ipv4> m_ipv4;
std::map<Ipv4Address, std::map<Ipv4Address, uint32_t>> m_distanceTable; // Distance vector table
std::map<Ipv4Address, Ipv4Address> m_nextHop; // Next hop table
std::map<uint32_t, Ptr<Socket>> m_socketMap;
Time m_updateInterval;
EventId m_updateEvent;
};
}
#endif // DISTANCE_VECTOR_ROUTING_H
Create the corresponding implementation file distance-vector-routing.cc:
#include “distance-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(“DistanceVectorRouting”);
NS_OBJECT_ENSURE_REGISTERED(DistanceVectorRouting);
TypeId DistanceVectorRouting::GetTypeId(void) {
static TypeId tid = TypeId(“ns3::DistanceVectorRouting”)
.SetParent<Ipv4RoutingProtocol>()
.SetGroupName(“Internet”)
.AddConstructor<DistanceVectorRouting>();
return tid;
}
DistanceVectorRouting::DistanceVectorRouting() : m_updateInterval(Seconds(5)) {
NS_LOG_FUNCTION(this);
Simulator::Schedule(Seconds(1.0), &DistanceVectorRouting::Start, this);
}
DistanceVectorRouting::~DistanceVectorRouting() {
NS_LOG_FUNCTION(this);
}
void DistanceVectorRouting::SetIpv4(Ptr<Ipv4> ipv4) {
NS_LOG_FUNCTION(this << ipv4);
m_ipv4 = ipv4;
}
void DistanceVectorRouting::Start() {
NS_LOG_FUNCTION(this);
SendDistanceVector();
m_updateEvent=Simulator::Schedule(m_updateInterval,&DistanceVectorRouting::Start, this);
}
void DistanceVectorRouting::SendDistanceVector() {
NS_LOG_FUNCTION(this);
Ptr<Packet> packet = Create<Packet>();
for (auto const &entry : m_distanceTable) {
Ipv4Address source = entry.first;
for (auto const &distanceEntry : entry.second) {
Ipv4Address dest = distanceEntry.first;
uint32_t distance = distanceEntry.second;
packet->AddHeader(Ipv4Header(source, dest, distance, 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 DistanceVectorRouting::ReceiveDistanceVector(Ptr<Socket> socket) {
NS_LOG_FUNCTION(this);
Ptr<Packet> packet = socket->Recv();
Ipv4Header header;
packet->RemoveHeader(header);
Ipv4Address source = header.GetSource();
Ipv4Address dest = header.GetDestination();
uint32_t distance = header.GetIdentification();
std::map<Ipv4Address, uint32_t> distances;
distances[dest] = distance;
UpdateRoutingTable(source, distances);
}
voidDistanceVectorRouting::UpdateRoutingTable(Ipv4Addresssource,std::map<Ipv4Address, uint32_t> distances) {
NS_LOG_FUNCTION(this << source);
for (auto const &entry : distances) {
Ipv4Address dest = entry.first;
uint32_t distance = entry.second;
if (m_distanceTable[dest] > distance + 1) {
m_distanceTable[dest] = distance + 1;
m_nextHop[dest] = source;
}
}
}
Ptr<Ipv4Route> DistanceVectorRouting::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_nextHop.find(dest) == m_nextHop.end()) {
sockerr = Socket::ERROR_NOROUTETOHOST;
return 0;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_nextHop[dest]);
route->SetOutputDevice(oif);
return route;
}
bool DistanceVectorRouting::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_nextHop.find(dest) == m_nextHop.end()) {
ecb(p, header, Socket::ERROR_NOROUTETOHOST);
return false;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_nextHop[dest]);
route->SetOutputDevice(idev);
ucb(route, p, header);
return true;
}
void DistanceVectorRouting::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(&DistanceVectorRouting::ReceiveDistanceVector, this));
m_socketMap[interface] = socket;
}
void DistanceVectorRouting::NotifyInterfaceDown(uint32_t interface) {
NS_LOG_FUNCTION(this << interface);
m_socketMap[interface]->Close();
m_socketMap.erase(interface);
}
void DistanceVectorRouting::NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
voidDistanceVectorRouting::NotifyRemoveAddress(uint32_tinterface,Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
}
- Integrate the Custom Routing Protocol:
#include “distance-vector-routing.h”
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 devices01 = pointToPoint.Install(nodes.Get(0), nodes.Get(1));
NetDeviceContainer devices12 = pointToPoint.Install(nodes.Get(1), nodes.Get(2));
NetDeviceContainer devices23 = pointToPoint.Install(nodes.Get(2), nodes.Get(3));
NetDeviceContainer devices30 = pointToPoint.Install(nodes.Get(3), nodes.Get(0));
InternetStackHelper stack;
DistanceVectorRoutingHelper distanceVectorRouting;
Ipv4ListRoutingHelper list;
list.Add(distanceVectorRouting, 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(devices12);
address.SetBase(“10.1.3.0”, “255.255.255.0”);
address.Assign(devices23);
address.SetBase(“10.1.4.0”, “255.255.255.0”);
address.Assign(devices30);
// Set up applications
uint16_t port = 9;
UdpEchoServerHelper server(port);
ApplicationContainer apps = server.Install(nodes.Get(3));
apps.Start(Seconds(1.0));
apps.Stop(Seconds(10.0));
UdpEchoClientHelper client(address.GetAddress(3), 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 0;
}
Explanation
At here, we describe the process of distance vector routing protocol in ns3:
- Network Topology: The code sets up a network topology with four nodes connected in a ring.
- Distance Vector Routing Protocol: A custom distance vector routing protocol class (DistanceVectorRouting) is implemented. This class handles route computation and distribution based on distance vector routing principles.
- Route Computation: The ComputeRoutes method computes initial static routes for the network.
- Distance Vector Exchange: The SendDistanceVector method sends the current distance vector table to neighbors, and the ReceiveDistanceVector method processes incoming distance vectors and updates the routing table.
- Route Input and Output: The RouteInput and RouteOutput methods handle packet routing based on the routing table.
- Integrate Custom Routing Protocol: The custom routing protocol is integrated into the ns-3 stack using the DistanceVectorRoutingHelper.
- Applications: UdpEchoServer and UdpEchoClient applications are set up to test the routing.
Running the Code
- Save your script to a file, for example, distance-vector-routing.cc.
- Compile the script using the ns-3 build system
./waf configure –enable-examples
./waf build
./waf –run scratch/distance-vector-routing
Overall, in this project we had learned about how the distance vector maintains the table by using the ns3 simulation tool. We provide the related support about distance vector routing protocol. Simulation of classic algorithm like Routing Information Protocol (RIP) can be got by ns3simulation.com.