How to Implement Static Routing in NS2

To implement the Static routing in NS2 has numerous steps that is a technique in which the routes are physically configured and they did not altered unless clearly modified. This type of routing is helpful in network in which the topology does not change commonly, and routes are predefined. In NS2, we can execute the static routing by physically specifying the routes among the nodes using the add-route command. The below are the following procedures to implement the static routing in NS2.

Steps to Implement Static Routing in NS2

  1. Set Up NS2 Environment
    • Make sure NS2 is installed and configured properly on the system.
  2. Understand Static Routing
    • In static routing, routes are physically configured. This means each node in the network has a predefined routing table that especially for the next hop for each destination.
    • Routes will not vary enthusiastically in response to network events like link failures, congestion.

Example OTcl Script for Static Routing in NS2

The given below are the example of how we can deploy static routing in NS2.

  1. Define the Network Topology and Static Routes

# Create a Simulator object

set ns [new Simulator]

# Open the trace file for recording data

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Define the finish procedure

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exec nam out.nam &

exit 0

}

# Create network nodes

set node1 [$ns node]

set node2 [$ns node]

set node3 [$ns node]

set node4 [$ns node]

set node5 [$ns node]

# Create links between nodes with varying delays and bandwidths

$ns duplex-link $node1 $node2 10Mb 10ms DropTail

$ns duplex-link $node2 $node3 10Mb 10ms DropTail

$ns duplex-link $node3 $node4 10Mb 10ms DropTail

$ns duplex-link $node4 $node5 10Mb 10ms DropTail

$ns duplex-link $node1 $node5 5Mb 50ms DropTail

$ns duplex-link $node2 $node4 5Mb 30ms DropTail

# Configure static routes manually

# Routing for node1

$node1 add-route $node2 1

$node1 add-route $node3 2

$node1 add-route $node4 2

$node1 add-route $node5 5  ;# Direct link (costly link)

# Routing for node2

$node2 add-route $node1 1

$node2 add-route $node3 1

$node2 add-route $node4 2

$node2 add-route $node5 2

# Routing for node3

$node3 add-route $node2 1

$node3 add-route $node4 1

$node3 add-route $node1 2

$node3 add-route $node5 2

# Routing for node4

$node4 add-route $node3 1

$node4 add-route $node2 2

$node4 add-route $node1 2

$node4 add-route $node5 1

# Routing for node5

$node5 add-route $node4 1

$node5 add-route $node3 2

$node5 add-route $node2 2

$node5 add-route $node1 5  ;# Direct link (costly link)

# Attach TCP agents for traffic generation

set tcp [new Agent/TCP]

$ns attach-agent $node1 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $node5 $sink

# Connect the TCP agent and sink

$ns connect $tcp $sink

# Create an FTP application and attach it to the TCP agent

set ftp [new Application/FTP]

$ftp attach-agent $tcp

# Start the FTP application

$ns at 1.0 “$ftp start”

# Stop the simulation at 10 seconds

$ns at 10.0 “finish”

# Run the simulation

$ns run

Explanation of the Script

  1. Network Topology: The script configures a network of five nodes with numerous links associating them. The links have diverse bandwidths and latency to mimic diverse network conditions.
  2. Manual Route Configuration: The static routes are physically set using the add-route command. Each node has a routing table with the next hop to reach every other node in the network. For instance:
    • Node1 can reach node2 directly.
    • To reach node3 and node4, node1 sends packets across node2.
    • For node5, node1 can use either the direct link (costly link) or transmit via node2 and node4 that relaying on the scenario.
  3. Traffic Generation: TCP traffic is created among node1 and node5 using an FTP application.
  4. Simulation: The simulation execute for 10 seconds, during that time data is routed from node1 to node5 based on the manually configured routes.

Running the Simulation

  1. Save the script as static_routing.tcl.
  2. Execute the simulation using NS2:

ns static_routing.tcl

  1. The simulation will create a trace file (out.tr) and a Network Animator (NAM) file (out.nam). Use NAM to visualize the simulation and monitor on the packet flows among the nodes.

Analysing the Results

  • Trace File: The trace file includes the data about packets being sent, received, and forwarded based on the static routes. we can use tools such as grep or awk to evaluate the trace file and confirm that packets follow the manually configured routes.
  • NAM Visualization: Use NAM to monitor on how packets travel via the network. We should see the traffic flow from node1 to node5 using the predefined static routes.

Enhancements

  1. Multiple Traffic Flows: Attach more traffic flows among diverse nodes to mimic a more complex network and validate on how static routing performs with multiple concurrent routes.
  2. Link Failure: Establish link failures or vary in link conditions to monitor on how static routing manages in such scenarios. While the static routing does not systematically adapts to network changes, packets be dropped if a route becomes unavailable.
  3. Routing Optimization: Validate with diverse routes to enhance the network performance such as delay, throughput, or packet loss.
  4. Comparison with Dynamic Routing: Execute a dynamic routing protocol such as OSPF or AODV and relate its performance with static routing in changing network conditions like node mobility, link failures.

In this simulation setup, we understood the concept clearly on how to install the simulation and analyse the outcomes for static routing using ns2 simulation tool. We plan to provide more information about how the static routing will perform in other simulation tool. Contact ns2project.com to connect with our talented developers who can provide expert guidance on Static Routing in NS2. We’re here to share project ideas and give you the best advice possible.