To implement Optimized Link State Routing (OLSR) protocol in ns3, we have to utilize the built-in OLSR module. Let’s dive into the quick and elaborate guide on setting up a simulation that uses OLSR protocol in ns3.
Steps for implementing OLSR protocol
- Set up your environment
Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.
- Create a new ns3 script
On your ns3 installation, create a new simulation script in the scratch directory. For Example, you can create a file named olsr-simulation.cc.
cd ns-3.xx
cd scratch
touch olsr-simulation.cc
- Include the necessary header files
In the olsr-simulation.cc file, create the necessary header files 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/mobility-module.h”
#include “ns3/olsr-module.h”
using namespace ns3;
- Set the network topology
Set the network topology by creating the basic topology that contains three nodes which are connected in a line and utilizes the OLSR as its routing protocol.
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 devices;
devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get (0));
interfaces.Add (address.Assign (devices.Get (1)));
address.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get (2)));
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (2), 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;
}
- Configure the OLSR protocol
The OLSR protocol is installed and configured using the OlsrHelper class, in this example. On the nodes of the OLSR routing protocol, configure the OlsrHelper class.
- Build and run the simulation
Build and run your script after writing.
./waf build
./waf –run scratch/olsr-simulation
- Analyze the results
You can analyze the results based on your needs after running the simulation. Also, you can add logging or tracing to observe the behavior of the OLSR protocol.
Example script for layer 3 protocol
Below is the example script for your 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/olsr-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 devices;
devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));
devices.Add (pointToPoint.Install (nodes.Get (1), nodes.Get (2)));
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices.Get (0));
interfaces.Add (address.Assign (devices.Get (1)));
address.SetBase (“10.1.2.0”, “255.255.255.0”);
interfaces.Add (address.Assign (devices.Get (2)));
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (2));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (2), 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;
}
Overall, we had a performance analysis on implementing the Optimized Link State Router (OLSR) by setting up and running a simulator which uses OLSR protocol in ns3. Also, we provide more simulation help on Optimized Link State Router (OLSR).