To implement a tutorials point in ns3, by creating a simple and easy-to-understand routing protocol for educational purposes which has the stylish way of explaining the concepts that helps the students to understand the concept easily.
The steps given below will explain about the implementation process of basic custom routing protocol in ns3.
Step-by-Step Guide to Implement tutorials point routing in ns3:
- Set Up ns-3 Environment: Make sure ns3 is installed on the system.
- Include Necessary Libraries: Include the required ns-3 libraries in the script.
- Define Network Topology: For network topology we need to create the nodes and links.
- Install Internet Stack: Install the Internet stack on the nodes.
- Implement the Custom Routing Protocol: Create a custom routing protocol class that demonstrates basic routing principles.
- Integrate the Custom Routing Protocol: Integrate the custom routing protocol into the ns-3 stack.
- Set Up Applications: Install applications to generate traffic and test the routing.
- Run the Simulation: Configure the simulation parameters and run it.
Example Implementation in C++
Here’s a detailed example to implement a simple custom routing protocol in ns-3:
- 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”
- 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, devices12, devices23, devices30;
devices01 = pointToPoint.Install(nodes.Get(0), nodes.Get(1)));
devices12 = pointToPoint.Install(nodes.Get(1), nodes.Get(2)));
devices23 = pointToPoint.Install(nodes.Get(2), nodes.Get(3)));
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 Custom Routing Protocol:
- Create a new header file tutorial-routing.h:
#ifndef TUTORIAL_ROUTING_H
#define TUTORIAL_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>
namespace ns3 {
class TutorialRouting : public Ipv4RoutingProtocol {
public:
static TypeId GetTypeId(void);
TutorialRouting();
virtual ~TutorialRouting();
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);
private:
Ptr<Ipv4> m_ipv4;
std::map<Ipv4Address, Ipv4Address> m_routingTable;
};
}
#endif // TUTORIAL_ROUTING_H
Create the corresponding implementation file tutorial-routing.cc:
#include “tutorial-routing.h”
#include “ns3/log.h”
#include “ns3/ipv4-routing-table-entry.h”
#include “ns3/simulator.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE(“TutorialRouting”);
NS_OBJECT_ENSURE_REGISTERED(TutorialRouting);
TypeId TutorialRouting::GetTypeId(void) {
static TypeId tid = TypeId(“ns3::TutorialRouting”)
.SetParent<Ipv4RoutingProtocol>()
.SetGroupName(“Internet”)
.AddConstructor<TutorialRouting>();
return tid;
}
TutorialRouting::TutorialRouting() {
NS_LOG_FUNCTION(this);
}
TutorialRouting::~TutorialRouting() {
NS_LOG_FUNCTION(this);
}
void TutorialRouting::SetIpv4(Ptr<Ipv4> ipv4) {
NS_LOG_FUNCTION(this << ipv4);
m_ipv4 = ipv4;
}
Ptr<Ipv4Route> TutorialRouting::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_routingTable.find(dest) == m_routingTable.end()) {
sockerr = Socket::ERROR_NOROUTETOHOST;
return 0;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_routingTable[dest]);
route->SetOutputDevice(oif);
return route;
}
bool TutorialRouting::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_routingTable.find(dest) == m_routingTable.end()) {
ecb(p, header, Socket::ERROR_NOROUTETOHOST);
return false;
}
Ptr<Ipv4Route> route = Create<Ipv4Route>();
route->SetDestination(dest);
route->SetGateway(m_routingTable[dest]);
route->SetOutputDevice(idev);
ucb(route, p, header);
return true;
}
void TutorialRouting::NotifyInterfaceUp(uint32_t interface) {
NS_LOG_FUNCTION(this << interface);
}
void TutorialRouting::NotifyInterfaceDown(uint32_t interface) {
NS_LOG_FUNCTION(this << interface);
}
void TutorialRouting::NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
void TutorialRouting::NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address) {
NS_LOG_FUNCTION(this << interface << address);
}
}
- Integrate the Custom Routing Protocol:
#include “tutorial-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, devices12, devices23, devices30;
devices01 = pointToPoint.Install(nodes.Get(0), nodes.Get(1)));
devices12 = pointToPoint.Install(nodes.Get(1), nodes.Get(2)));
devices23 = pointToPoint.Install(nodes.Get(2), nodes.Get(3)));
devices30 = pointToPoint.Install(nodes.Get(3), nodes.Get(0)));
InternetStackHelper stack;
TutorialRoutingHelper tutorialRouting;
Ipv4ListRoutingHelper list;
list.Add(tutorialRouting, 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
- Network Topology: The code sets up a network topology with four nodes connected in a ring.
- Custom Routing Protocol: A custom routing protocol class (TutorialRouting) is implemented that demonstrates basic routing principles.
- Routing Table Management: The routing table is managed using a simple map that stores the next hop for each destination address.
- 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 TutorialRoutingHelper.
- Applications: UdpEchoServer and UdpEchoClient applications are set up to test the routing.
- Running the Code
- Save the script to a file, for example, tutorial-routing.cc.
- Compile the script using the ns-3 build system:
./waf configure –enable-examples
./waf build
./waf –run scratch/tutorial-routing
The process of implementing tutorials point routing using a custom routing protocol was successfully done and the scripts were compiled using the ns3 build system. tutorials point in ns3 with best routing and implementation support can be got from ns3simulation.com