To Implement intra-domain routing protocols, like RIP, OSPF, or EIGRP, in ns-3, it can be done by general step of setting up the network topology and then configuring the routing protocol, which is used for running the applications. Here we have given he complete instructions for setting up the RIP protocol in ns3, which supports directly.
Step-by-Step Guide to Implementing RIP in ns-3
- Set Up Your Environment
Ensure that ns-3 installed.
- Create a New ns-3 Script
Create a new simulation script in the scratch directory of your 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 your rip-simulation.cc file, include the necessary headers for your simulation
#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/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 your script, you 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/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 and EIGRP in ns-3
We can use the external tools such as Quagga or FRRouting with the Direct Code Execution (DCE) module, for implementing the protocols which are not directly support in ns3 like OSPF and EIGRP.
Here we have given the simplified script of OSPF using Quagga through DCE.
Implementing OSPF Using Quagga and DCE
- Install DCE: Follow the instructions to set up DCE from the DCE website.
- Create a new script for OSPF simulation:
#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/dce-module.h”
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 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)));
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.0.0.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get(0), devices.Get(1));
address.SetBase (“10.0.1.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(1), devices.Get(2)));
address.SetBase (“10.0.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get(2), devices.Get(3)));
DceManagerHelper dceManager;
dceManager.SetTaskManagerAttribute (“FiberManagerType”, StringValue (“UcontextFiberManager”));
dceManager.Install (nodes);
QuaggaHelper quagga;
quagga.EnableRadvd (nodes);
quagga.Install (nodes);
quagga.Config (nodes.Get (0), “zebra”, “log stdout\n”
“interface *\n”
“ip forwarding\n”
“router ospf\n”
” network 10.0.0.0/24 area 0\n”
” network 10.0.1.0/24 area 0\n”);
quagga.Config (nodes.Get (1), “zebra”, “log stdout\n”
“interface *\n”
“ip forwarding\n”
“router ospf\n”
” network 10.0.0.0/24 area 0\n”
” network 10.0.1.0/24 area 0\n”);
quagga.Config (nodes.Get (2), “zebra”, “log stdout\n”
“interface *\n”
“ip forwarding\n”
“router ospf\n”
” network 10.0.1.0/24 area 0\n”
” network 10.0.2.0/24 area 0\n”);
quagga.Config (nodes.Get (3), “zebra”, “log stdout\n”
“interface *\n”
“ip forwarding\n”
“router ospf\n”
” network 10.0.2.0/24 area 0\n”);
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream (“ospf-simulation.tr”));
pointToPoint.EnablePcapAll (“ospf-simulation”);
Simulator::Stop (Seconds (20.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Build and Run the Simulation:
./waf build
./waf –run scratch/ospf-simulation
Overall, we had learnt to implement the intra-domain protocols like RIP, OSPF or EIGRP in ns3. RIP can be supported directly in ns3 so it follows the general process for implementation, but OSPF and EIGRP does not support natively in ns3. So, here we have used the external tool Quagga for OSPF through Direct code Extension (DCE) module for implementation. Direct code Extension (DCE) comparative analysis are carried on by our developers for your project.