How to Begin Implement Static Routing in NS3

To Implement the Static Routing in NS3 tools has contain for configure a predefined routing tables that do not variation for during replication. Static routing is straightforward and useful for networks by fixed topologies and well-defined routes. In NS-3, we can use the Ipv4StaticRouting class provided through the InternetStack component.

Here’s how you can begin implementing static routing in NS-3:

Steps to Begin Implement Static Routing in NS3

Step 1: Understand Static Routing

  1. Key Features:
    • Routes are manually setting and remain routes are static.
    • No dynamic modifications according to network variations.
    • Appropriate for minimum, fixed, and predictable networks.
  2. Routing Behavior:
    • Require the next hop for every destination manually.
    • Routes can be setting API using the Ipv4StaticRouting.

Step 2: Set Up the Network

Make a network topology using NS3 helper classes such as NodeContainer, PointToPointHelper, and Ipv4AddressHelper.

Step 3: Enable Static Routing

Fixed the routing is part of InternetStackHelper. We can use Ipv4StaticRoutingHelper to setting a static routes.

Example of Configuring Static Routes

Full Example Code

#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[]) {

// Create nodes

NodeContainer nodes;

nodes.Create (4);

// Install internet stack

InternetStackHelper internet;

internet.Install (nodes);

// Create point-to-point channels

PointToPointHelper p2p;

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

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

 

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

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

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

// Assign IP addresses

Ipv4AddressHelper ipv4;

ipv4.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces1 = ipv4.Assign (devices1);

ipv4.SetBase (“10.1.2.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces2 = ipv4.Assign (devices2);

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

Ipv4InterfaceContainer interfaces3 = ipv4.Assign (devices3);

// Configure static routing

Ipv4StaticRoutingHelper staticRoutingHelper;

// Node 0 routing

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

Ptr<Ipv4StaticRouting> staticRoutingNode0 = staticRoutingHelper.GetStaticRouting (ipv4Node0);

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

// Node 1 routing

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

Ptr<Ipv4StaticRouting> staticRoutingNode1 = staticRoutingHelper.GetStaticRouting (ipv4Node1);

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

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

// Node 2 routing

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

Ptr<Ipv4StaticRouting> staticRoutingNode2 = staticRoutingHelper.GetStaticRouting (ipv4Node2);

staticRoutingNode2->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.2.1”), 2);

staticRoutingNode2->AddNetworkRouteTo (Ipv4Address (“10.1.3.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.3.2”), 3);

// Node 3 routing

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

Ptr<Ipv4StaticRouting> staticRoutingNode3 = staticRoutingHelper.GetStaticRouting (ipv4Node3);

staticRoutingNode3->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.3.1”), 1);

// Install applications

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (3));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces3.GetAddress (1), 9);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (5));

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

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 4: Key Functions for Static Routing

  1. AddHostRouteTo:
    • Improve the route for a detailed host.

staticRoutingNode0->AddHostRouteTo(Ipv4Address(“10.1.3.1”), Ipv4Address(“10.1.1.2”), 1);

  1. AddNetworkRouteTo:
    • Enhanced the route for whole network.

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

  1. SetDefaultRoute:
    • Sets a default route for many destination not explicitly listed.

staticRoutingNode0->SetDefaultRoute(Ipv4Address(“10.1.1.2”), 1);

Step 5: Compile and Run

  1. Build the Simulation:

./waf configure

./waf build

  1. Run the Script:

./waf –run your-script

Step 6: Analyze the Results

  1. Pcap Tracing:
    • Use PcapHelper to capture and analyze packets.

p2p.EnablePcapAll(“static-routing”);

  1. Validation:
    • Validate the routes using NS3 environment logs or seizure congestion to assure the correct packet forwarding.

Step 7: Extend Static Routing

  1. Advanced Topologies:
    • Experiment through further complex network topologies and numerous paths.
  2. Fault Scenarios:
    • Launch the connection /node failures and observe the behavior.
  3. Hybrid Approaches:
    • Associate the static routing through dynamic protocols such as OLSR or AODV for hybrid setting.

These project ideas concentrate on implementing and discovering Static Routing in diverse network environments and related its performance with dynamic routing protocols using NS3. Further detailed about the static routing will be provided according to your necessities.