Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement non adaptive routing in ns3

To implement a non-adaptive routing protocol in ns3, we have to set up a fixed routing table which do not change on the basis of network state or traffic conditions. This means that we should manually specify the routes in advance, and these routes remain static throughout entire the simulation.

Step-by-step guide on implementing non-adaptive routing in ns3

  1. Set up your ns3 :
  • Make sure that ns3 is installed in the computer. If not, install it.

 

  1. Create simulation script:
  • Set up a network topology.
  • Install the Internet stack.
  • Use the non-adaptive routing helper to enable non-adaptive routing .
  • Set up applications and run the simulation.

Example simulation script

Below is example to set up non-adaptive (static) routing in ns3.

#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/ipv4-static-routing-helper.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“StaticRoutingExample”);

int main (int argc, char *argv[])

{

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create (4);

// Create point-to-point links

PointToPointHelper p2p;

p2p.SetDeviceAttribute (“DataRate”, StringValue (“1Mbps”));

p2p.SetChannelAttribute (“Delay”, StringValue (“10ms”));

NetDeviceContainer devices01 = p2p.Install (nodes.Get (0), nodes.Get (1));

NetDeviceContainer devices12 = p2p.Install (nodes.Get (1), nodes.Get (2));

NetDeviceContainer devices23 = p2p.Install (nodes.Get (2), nodes.Get (3));

NetDeviceContainer devices30 = p2p.Install (nodes.Get (3), nodes.Get (0));

// Install the Internet stack

InternetStackHelper stack;

stack.Install (nodes);

// Assign IP addresses

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);

address.SetBase (“10.1.3.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);

address.SetBase (“10.1.4.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces30 = address.Assign (devices30);

// Enable static routing

Ipv4StaticRoutingHelper staticRoutingHelper;

Ptr<Ipv4StaticRouting> staticRouting0 = staticRoutingHelper.GetStaticRouting (nodes.Get (0)->GetObject<Ipv4> ());

Ptr<Ipv4StaticRouting> staticRouting1 = staticRoutingHelper.GetStaticRouting (nodes.Get (1)->GetObject<Ipv4> ());

Ptr<Ipv4StaticRouting> staticRouting2 = staticRoutingHelper.GetStaticRouting (nodes.Get (2)->GetObject<Ipv4> ());

Ptr<Ipv4StaticRouting> staticRouting3 = staticRoutingHelper.GetStaticRouting (nodes.Get (3)->GetObject<Ipv4> ());

// Manually configure static routes

staticRouting0->AddNetworkRouteTo (Ipv4Address (“10.1.2.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.2”), 1);

staticRouting0->AddNetworkRouteTo (Ipv4Address (“10.1.3.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.2”), 1);

staticRouting0->AddNetworkRouteTo (Ipv4Address (“10.1.4.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.2”), 1);

staticRouting1->AddNetworkRouteTo (Ipv4Address (“10.1.3.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.2.2”), 2);

staticRouting1->AddNetworkRouteTo (Ipv4Address (“10.1.4.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.2.2”), 2);

staticRouting1->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.1”), 1);

staticRouting2->AddNetworkRouteTo (Ipv4Address (“10.1.4.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.3.2”), 2);

staticRouting2->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.2.1”), 1);

staticRouting2->AddNetworkRouteTo (Ipv4Address (“10.1.2.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.2.1”), 1);

staticRouting3->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.4.2”), 2);

staticRouting3->AddNetworkRouteTo (Ipv4Address (“10.1.2.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.3.1”), 1);

staticRouting3->AddNetworkRouteTo (Ipv4Address (“10.1.3.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.3.1”), 1);

// Create a packet sink to receive packets

uint16_t sinkPort = 8080;

Address sinkAddress (InetSocketAddress (interfaces23.GetAddress (1), sinkPort));

PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, sinkAddress);

ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (3));

sinkApps.Start (Seconds (1.0));

sinkApps.Stop (Seconds (10.0));

// Create a TCP client to send packets

OnOffHelper clientHelper (“ns3::TcpSocketFactory”, sinkAddress);

clientHelper.SetAttribute (“OnTime”, StringValue (“ns3::ConstantRandomVariable[Constant=1]”));

clientHelper.SetAttribute (“OffTime”, StringValue (“ns3::ConstantRandomVariable[Constant=0]”));

clientHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));

clientHelper.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApps = clientHelper.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

// Enable tracing

AsciiTraceHelper ascii;

p2p.EnableAsciiAll (ascii.CreateFileStream (“static-routing.tr”));

p2p.EnablePcapAll (“static-routing”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Explanation of the script

In this set up, we had implemented a non-adaptive routing using ns3 features. Let’s have a detailed explanation on the script below:

  1. Create nodes : Create four nodes and connects them with point-to-point links.
  2. Install internet stack : Using InternetStackHelper, install the internet stack on all the nodes.
  3. Assign IP addresses : Using Ipv4AddressHelper, assign IP addresses to the devices.
  4. Enable Static Routing: Use the Ipv4StaticRoutingHelper to obtain static routing objects for each node.
  5. Configure Static Routes: To the routing tables, manually add static routes. The AddNetworkRouteTo function specifies the destination network, the network mask, the next-hop address, and the output interface index.
  6. Create applications : To receive packets, a packet sink application is installed on node. An OnOff application is installed on node 0 to send packets to node 3.
  7. Enable tracking : Capture packet traces using pcap tracing for analysis.
  8. Run the simulator : Compile and execute to run the simulator.

Running the Simulation

To run the simulation, compile the script and execute it:

./waf configure –enable-examples

./waf build

./waf –run static-routing

So, we had successfully learned on implementing non-adaptive routing protocol in ns3 by setting up a fixed routing table that doesn’t change based on the network state or traffic conditions. Get best guidance on non-adaptive routing protocol in ns3 with implementation support.