To implement the network routing in ns3 has includes numerous steps that are initially setup the network topology, download the internet stack and the routing protocols and then configure the routing features. The given below is the complete procedure on how to implement the network routing in ns3:
Step-by-step implementation:
Step 1: Set Up the Simulation Environment
- Make certain ns3 is installed on the system.
Step 2: Create the Network Topology
- Generate the simple network topology using ns3. The given below is the sample script to setup the basic network with three nodes connected by point-to-point links.
#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”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices01;
devices01 = pointToPoint.Install (nodes.Get(0), nodes.Get(1));
NetDeviceContainer devices12;
devices12 = pointToPoint.Install (nodes.Get(1), nodes.Get(2));
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);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces12.GetAddress (1), 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));
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 3: Enable and Configure Routing Protocols
- In this instance, the Ipv4GlobalRoutingHelper is used to allow static routing. However, ns3 supports several dynamic routing protocols such as OLSR, AODV, and DSDV.
Using OLSR (Optimized Link State Routing)
To use OLSR, include the necessary header and configure the routing helper:
#include “ns3/olsr-helper.h”
// Inside the main function, after installing the Internet stack
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting, 0);
list.Add (olsr, 10);
InternetStackHelper stack;
stack.SetRoutingHelper (list);
stack.Install (nodes);
Using AODV (Ad hoc On-Demand Distance Vector)
- To use AODV, include the necessary header and configure the routing helper:
#include “ns3/aodv-helper.h”
// Inside the main function, after installing the Internet stack
AodvHelper aodv;
InternetStackHelper stack;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
Using DSDV (Destination-Sequenced Distance-Vector)
- To use DSDV, include the necessary header and configure the routing helper:
#include “ns3/dsdv-helper.h”
// Inside the main function, after installing the Internet stack
DsdvHelper dsdv;
InternetStackHelper stack;
stack.SetRoutingHelper (dsdv);
stack.Install (nodes);
Step 4: Populate Routing Tables
- For static routing, you need to populate the routing tables manually:
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
This step is usually not required for dynamic routing protocols as they manage the routing table that updates automatically.
Step 5: Run the Simulation
- Compile and run your ns3 script.
./waf configure
./waf build
./waf –run <your_script_name>
Step 6: Analyze the Results
- After running the simulation, we can evaluate the routing behaviour and network performance by inspecting the output or using tracing tools provided by ns3.
Full Example with OLSR
Here’s a complete example using the OLSR routing protocol:
#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/olsr-helper.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (3);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices01;
devices01 = pointToPoint.Install (nodes.Get(0), nodes.Get(1));
NetDeviceContainer devices12;
devices12 = pointToPoint.Install (nodes.Get(1), nodes.Get(2));
OlsrHelper olsr;
InternetStackHelper stack;
stack.SetRoutingHelper (olsr);
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);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces12.GetAddress (1), 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));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
In the conclusion, we clearly understand about the implementation procedures for network routing protocols that simulated in ns3 tool. We give the outline for further insights that related to network routing protocols.
Incase if you encounter difficulties in implementing network routing in ns3 for your project, despite reviewing the aforementioned suggestions, we encourage you to contact ns3simulation.com. We are here to assist you with performance analysis to achieve optimal results.