To implement an internal routing protocols (Interior Gateway Protocols, IGP) like RIP (Routing Information Protocol), OSPF (Open Shortest Path First), and other custom internal protocols in ns-3, first we need to set-up a network topology, then configure the desired protocol, and also have to run the applications that uses the desired routing.
The given below steps will guide on setting up the internal routing protocols using RIP in ns3:
Step-by-Step Guide to Implementing RIP in ns3:
- Set Up Your Environment
Ensure that ns3 is installed.
- Create a New ns-3 Script
Create a new simulation script in the scratch directory of the ns3 installation. For example, create a file named rip-simulation.cc.
cd ns3.xx
cd scratch
touch rip-simulation.cc
3. Include Necessary Headers
In the rip-simulation.cc file, include the necessary headers for the simulation process.
#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/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/rip-helper.h”
using namespace ns3;
4. Set Up the Network Topology
Define the network topology. Let’s create a basic topology with a few nodes and use RIP as the routing protocol.
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (4);
// Set up point-to-point links
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get(0), nodes.Get(1));
devices.Add (pointToPoint.Install (nodes.Get(1), nodes.Get(2)));
devices.Add (pointToPoint.Install (nodes.Get(2), nodes.Get(3)));
// Install the internet stack on nodes
InternetStackHelper stack;
RipHelper ripRouting;
Ipv4ListRoutingHelper list;
list.Add (ripRouting, 0);
stack.SetRoutingHelper (list);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get(0), devices.Get(1));
address.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(1), devices.Get(2)));
address.SetBase (“10.1.3.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(2), devices.Get(3)));
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“rip-simulation.tr”));
pointToPoint.EnablePcapAll (“rip-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
5. Build and Run the Simulation
After writing the script, we need to build and run it.
./waf build
./waf –run scratch/rip-simulation
6. Analyze the Results
After running the simulation, we can analyze the results using the generated trace files (rip-simulation.tr and rip-simulation-0-0.pcap).
Complete Example Script for RIP
Here’s a complete example script for reference:
#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/mobility-module.h”
#include “ns3/wifi-module.h”
#include “ns3/rip-helper.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (4);
// Set up point-to-point links
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes.Get(0), nodes.Get(1));
devices.Add (pointToPoint.Install (nodes.Get(1), nodes.Get(2)));
devices.Add (pointToPoint.Install (nodes.Get(2), nodes.Get(3)));
// Install the internet stack on nodes
InternetStackHelper stack;
RipHelper ripRouting;
Ipv4ListRoutingHelper list;
list.Add (ripRouting, 0);
stack.SetRoutingHelper (list);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get(0), devices.Get(1));
address.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(1), devices.Get(2)));
address.SetBase (“10.1.3.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(2), devices.Get(3)));
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (3), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“rip-simulation.tr”));
pointToPoint.EnablePcapAll (“rip-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Implementing OSPF in ns-3
Ns3 does not directly support OSPF, so we have to create a custom module or external library for implementation. Here’s a highly advanced guide for implementing custom OSPF-like protocols which are not directly supported by ns3.
1. Create a Custom OSPF Protocol
There are some steps to follow to create a custom protocol as we discussed earlier, but OSPF logic should be implemented in the ospf-protocol.h and ospf-protocol.cc.
2. Include Necessary Headers and Implement OSPF Logic
Created classes and methods in OSPF’s hello packets, link-state advertisements (LSAs) and SPF algorithm are required while implementing the custom OSPF.
3. Set Up the Network Topology and Use the Custom Protocol
After the simulation process the script will look similarly as the RIP example, here we need to add the custom OSPF protocol.
Now, we had learnt all the steps for implementing the internal routing protocol- RIP (Routing Information protocol) in ns3. And get our implementation support for OSPF we create the custom module in ns3 environment.