To implement network routing in ns-3, first we need to set up a network topology which contains multiple nodes and configuring routing protocols to manage the paths that data packets travel through the network. The following step-by-step guide helps to create a simple network topology with dynamic routing using the OLSR (Optimized Link State Routing) protocol.
Here is the Step-by-Step Guide to Implement Network Routing in ns-3 :
- Install ns-3 :
Make sure that ns-3 is installed. If not, follow the installation illustrations to install ns-3.
- Create a simulation script :
Create a new script file in ns-3, e.g., network-routing-simulation.cc.
- Include necessary headers :
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/olsr-helper.h”
- Define the Main Function:
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
- Set Up the Network Topology:
- To set up the network topology, create nodes and configure network settings
NodeContainer nodes;
nodes.Create (6); // Create 6 nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
Ipv4AddressHelper address;
// Create point-to-point links between nodes
for (uint32_t i = 0; i < nodes.GetN() – 1; ++i)
{
NetDeviceContainer link = pointToPoint.Install (nodes.Get(i), nodes.Get(i+1));
devices.Add (link);
std::ostringstream subnet;
subnet << “10.1.” << i+1 << “.0”;
address.SetBase (subnet.str().c_str(), “255.255.255.0”);
address.Assign (link);
}
- Install Internet Stack and Routing Protocol:
- Install the Internet stack on all nodes which was created previously and enable OLSR routing protocol
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
Ipv4AddressHelper ipv4;
ipv4.SetBase (“10.1.0.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
- Configure Mobility (Optional):
- Set up mobility models for the nodes (optional).
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
- Install Applications:
- Set up a UDP echo server on the one node and a UDP echo client on another node
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (5));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
- Run the Simulation:
- Define the simulation stop time and start the simulator:
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
Example Complete Script (network-routing-simulation.cc):
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/mobility-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 (6); // Create 6 nodes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));
pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));
NetDeviceContainer devices;
Ipv4AddressHelper address;
// Create point-to-point links between nodes
for (uint32_t i = 0; i < nodes.GetN() – 1; ++i)
{
NetDeviceContainer link = pointToPoint.Install (nodes.Get(i), nodes.Get(i+1));
devices.Add (link);
std::ostringstream subnet;
subnet << “10.1.” << i+1 << “.0”;
address.SetBase (subnet.str().c_str(), “255.255.255.0”);
address.Assign (link);
}
// Install the Internet stack on all nodes and enable OLSR routing protocol
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
Ipv4AddressHelper ipv4;
ipv4.SetBase (“10.1.0.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);
// Configure mobility (optional)
MobilityHelper mobility;
mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);
mobility.Install (nodes);
// Install Applications
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (5));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Explanation:
Here are the explanation on setting up, implementing and running the network routing in ns-3:
- Network Configuration:
- Nodes are created, and point-to-point links are set up between the nodes.
- IP addresses are assigned to the network devices.
- Routing Protocol:
- The Internet stack is installed on all nodes, and the OLSR routing protocol is enabled.
- Mobility (Optional):
- The Constant Position Mobility Model is used to keep the nodes at fixed positions. This is an optional step. Hence, it can be modified to simulate different mobility scenarios.
- Applications:
- An UDP echo server application is installed on one node.
- An UDP echo client application is installed on another node for simulating communication.
- Running the Simulation:
- The simulation is run for a specific time as we mentioned the stopping time for the simulator, and then the simulator is destroyed to clean up.
Finally, we have successfully implemented network routing by creating a simple network topology with dynamic routing using the OLSR (Optimized Link State Routing) protocol.