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

How to Implement static routing in ns3

To implement static routing in ns3, we need to do the manual configuration of the routing tables for each node present in the network. Static routing is a great example for simpler routing which does not change based on the network conditions like dynamic routing.

The given step-by-step guide will give the instructions to implement static routing in ns3.

Step-by-Step guide to Implement static routing in ns3:

  1. Set Up ns-3 Environment: Make sure that ns3 is installed on the system.
  2. Include Necessary Libraries: The required ns3 libraries need to be included in the script.
  3. Define Network Topology: Create the nodes and links for the network topology.
  4. Install Internet Stack: Install the Internet stack on the nodes.
  5. Assign IP Addresses: Assign IP addresses to the interfaces.
  6. Configure Static Routes: Manually configure the routing tables for each node.
  7. Set Up Applications: Install applications to generate traffic and test the routing.
  8. Run the Simulation: Configure the simulation parameters and run it.

Example Implementation in C++

Here’s a detailed example to implement static routing in ns-3:

  1. Include Libraries:

#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”

  1. Define Network Topology:

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

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

NetDeviceContainer devices12;

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

NetDeviceContainer devices23;

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

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

 

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

Ipv4InterfaceContainer interfaces23 = address.Assign(devices23);

  1. Configure Static Routes:

Ipv4StaticRoutingHelper staticRoutingHelper;

Ptr<Ipv4> ipv4_0 = nodes.Get(0)->GetObject<Ipv4>();

Ptr<Ipv4StaticRouting> staticRouting_0 = staticRoutingHelper.GetStaticRouting(ipv4_0);

staticRouting_0->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”),     Ipv4Mask(“255.255.255.0”), interfaces01.GetAddress(1), 1);

Ptr<Ipv4> ipv4_1 = nodes.Get(1)->GetObject<Ipv4>();

Ptr<Ipv4StaticRouting> staticRouting_1 = staticRoutingHelper.GetStaticRouting(ipv4_1);

staticRouting_1->AddNetworkRouteTo(Ipv4Address(“10.1.1.0”), Ipv4Mask(“255.255.255.0”), interfaces01.GetAddress(0), 1);

staticRouting_1->AddNetworkRouteTo(Ipv4Address(“10.1.3.0”), Ipv4Mask(“255.255.255.0”), interfaces12.GetAddress(1), 2);

Ptr<Ipv4> ipv4_2 = nodes.Get(2)->GetObject<Ipv4>();

Ptr<Ipv4StaticRouting> staticRouting_2 = staticRoutingHelper.GetStaticRouting(ipv4_2);

staticRouting_2->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.0”), interfaces12.GetAddress(0), 1);

staticRouting_2->AddNetworkRouteTo(Ipv4Address(“10.1.1.0”), Ipv4Mask(“255.255.255.0”), interfaces12.GetAddress(0), 1);

staticRouting_2->AddNetworkRouteTo(Ipv4Address(“10.1.3.0”), Ipv4Mask(“255.255.255.0”), interfaces23.GetAddress(1), 2);

Ptr<Ipv4> ipv4_3 = nodes.Get(3)->GetObject<Ipv4>();

Ptr<Ipv4StaticRouting> staticRouting_3 = staticRoutingHelper.GetStaticRouting(ipv4_3);

staticRouting_3->AddNetworkRouteTo(Ipv4Address(“10.1.2.0”), Ipv4Mask(“255.255.255.0”), interfaces23.GetAddress(0), 1);

staticRouting_3->AddNetworkRouteTo(Ipv4Address(“10.1.1.0”), Ipv4Mask(“255.255.255.0”), interfaces23.GetAddress(0), 1);

  1. Set Up Applications:

uint16_t port = 9;

UdpEchoServerHelper server(port);

ApplicationContainer apps = server.Install(nodes.Get(3));

apps.Start(Seconds(1.0));

apps.Stop(Seconds(10.0));

UdpEchoClientHelper client(interfaces23.GetAddress(1), port);

client.SetAttribute(“MaxPackets”, UintegerValue(1));

client.SetAttribute(“Interval”, TimeValue(Seconds(1.0)));

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

apps = client.Install(nodes.Get(0));

apps.Start(Seconds(2.0));

apps.Stop(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

 

 

Explanation

  1. Network Topology: The code sets up a network topology with four nodes connected in a line.
  2. Static Routing Protocol: The Ipv4StaticRoutingHelper is used to configure static routes manually for each node.
  3. Routes Configuration: Static routes are added to each node’s routing table. For example, Node 0 has a route to the network 10.1.2.0/24 via the interface connected to Node 1.
  4. Applications: UdpEchoServer and UdpEchoClient applications are set up to test the routing. The server is installed on Node 3, and the client is installed on Node 0 to send packets to Node 3.

Running the Code

  1. Save your script to a file, for example, static-routing.cc.
  2. Compile the script using the ns-3 build system:

./waf configure –enable-examples

./waf build

./waf –run scratch/static-routing

The static routing protocol is implemented by assigning IP addresses to the interface and manually configuring the routing tables for each node in the network for the simulation process. For all your implementation details you can contact ns3simulation.com.