How to Implement Network Optimal Routing in NS2

To implement Network Optimal Routing in NS2, we mimic a routing in such a way that the network selects the most efficient path among source and destination nodes according to a certain parameters like shortest path, least delay, or least congestion. Executing optimal routing usually includes using a routing algorithm that regulates the best path based on the chosen parameters.

In NS2, we can use built-in routing protocols such as AODV (Ad hoc On-Demand Distance Vector), DSDV (Destination-Sequenced Distance-Vector Routing), or customize an existing protocol to attain optimal routing according to the chosen parameters.

The below is the procedure to implement the Network Optimal Routing in ns2:

Steps to Implement Network Optimal Routing in NS2:

  1. Set up the Simulation Environment

Initially, describe the network nodes and links. The network topology consists of source and destination nodes associated by intermediate nodes (routers or switches). The goal is to regulate the optimal path among the source and destination.

Example TCL Script for Basic Setup:

# Create a simulator instance

set ns [new Simulator]

# Define link parameters (bandwidth and delay)

set bw 10Mb          ;# Bandwidth for each link

set delay 10ms       ;# Delay for each link

# Create network nodes

set node1 [$ns node]   ;# Source node

set node2 [$ns node]   ;# Intermediate node

set node3 [$ns node]   ;# Intermediate node

set node4 [$ns node]   ;# Destination node

# Create links between the nodes

$ns duplex-link $node1 $node2 $bw $delay DropTail

$ns duplex-link $node2 $node3 $bw $delay DropTail

$ns duplex-link $node3 $node4 $bw $delay DropTail

$ns duplex-link $node1 $node3 $bw $delay DropTail  ;# Additional link to create multiple paths

This generates a basic network topology in which Node1 (source) can reach Node4 (destination) through either Node2 or directly through Node3.

  1. Configure Traffic between Source and Destination

After that, configure the traffic to mimic data transmission from the source to the destination. This can be completed using UDP or TCP agents with CBR (Constant Bit Rate) or other traffic patterns.

Example of Configuring Traffic:

# Create a UDP agent at Node1 (source node)

set udp_src [new Agent/UDP]

$ns attach-agent $node1 $udp_src

# Create a CBR traffic generator to simulate constant packet flow

set cbr_src [new Application/Traffic/CBR]

$cbr_src set packet_size_ 500      ;# Packet size in bytes

$cbr_src set rate_ 2Mb             ;# Data rate (e.g., 2 Mbps)

$cbr_src attach-agent $udp_src

# Create a UDP sink at Node4 (destination node)

set null_sink [new Agent/Null]

$ns attach-agent $node4 $null_sink

# Connect source to destination via intermediate nodes (optimal routing will decide the best path)

$ns connect $udp_src $null_sink

# Start and stop the traffic

$ns at 1.0 “$cbr_src start”

$ns at 10.0 “$cbr_src stop”

  1. Choose a Routing Protocol

Option 1: Using AODV (Ad hoc On-Demand Distance Vector)

AODV is a dynamic routing protocol that determines routes on-demand and chooses the shortest path based on the number of hops. This technique is usually used to attain optimal routing in terms of path length.

# Enable AODV routing protocol

set val(rp) AODV

# Attach AODV routing to all nodes

for {set i 1} {$i <= 4} {incr i} {

set node [set node($i)]

$ns at 0.0 “$node set ragent [new Agent/AODV]”

}

AODV will systematically discover and use the optimal path among Node1 and Node4 based on the number of hops.

Option 2: Using DSDV (Destination-Sequenced Distance-Vector Routing)

DSDV is a proactive routing protocol that maintains reliable routing tables at each node. It occasionally updates routes and chooses the shortest path according to the number of hops or other metrics.

# Enable DSDV routing protocol

set val(rp) DSDV

# Attach DSDV routing to all nodes

for {set i 1} {$i <= 4} {incr i} {

set node [set node($i)]

$ns at 0.0 “$node set ragent [new Agent/DSDV]”

}

  1. Configure a Custom Routing Metric (Optional)

If we want to customize the routing process to optimize based on different parameters like delay or available bandwidth, we can adjust the routing protocols by customizing the metrics used for path selection.

Example: Modifying AODV for Minimum Delay

In NS2, we can adjust the AODV implementation to factor in link delays when choosing routes. This needs to changes to the AODV agent code (which is written in C++).

Otherwise, in the TCL script, we can adapt link delays and monitor on how the routing protocol reacts to those delays.

# Modify the delay for one of the links to represent a less optimal path

$ns duplex-link $node1 $node2 $bw 10Mb 100ms DropTail   ;# Longer delay link

$ns duplex-link $node1 $node3 $bw 10Mb 5ms DropTail     ;# Shorter delay link (optimal path)

# The routing protocol will now likely choose the shorter delay link for routing

  1. Enable Tracing and Monitor Routing Decisions

NS2 deliver tracing capabilities that enable them to monitor how packets are transmitted via the network, which routes are selected, and whether the routing is optimal based on the chosen parameters.

Enable Trace Files:

# Enable tracing for the simulation

set tracefile [open “optimal_routing_trace.tr” w]

$ns trace-all $tracefile

# Define a finish procedure to end the simulation and close the trace file

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Set the simulation end time

$ns at 20.0 “finish”

The trace file will capture packet transmissions, routing decisions, and any route changes, enable them to evaluate the optimal routing behaviour.

  1. Run the Simulation

Execute the simulation to monitor on how the routing protocol prioritize the optimal path for packet forwarding according to the chosen parameters such as shortest path, least delay.

# Run the simulation

$ns run

Example Complete TCL Script for Network Optimal Routing Simulation

# Create a simulator instance

set ns [new Simulator]

# Define link parameters (bandwidth and delay)

set bw 10Mb

set delay 10ms

# Create network nodes

set node1 [$ns node]   ;# Source node

set node2 [$ns node]   ;# Intermediate node

set node3 [$ns node]   ;# Intermediate node

set node4 [$ns node]   ;# Destination node

# Create links between the nodes

$ns duplex-link $node1 $node2 $bw $delay DropTail

$ns duplex-link $node2 $node3 $bw $delay DropTail

$ns duplex-link $node3 $node4 $bw $delay DropTail

$ns duplex-link $node1 $node3 $bw 10Mb 5ms DropTail  ;# Shorter delay link (optimal path)

# Create UDP agent for traffic from Node1 (source) to Node4 (destination)

set udp_src [new Agent/UDP]

$ns attach-agent $node1 $udp_src

# Create CBR traffic generator for UDP source

set cbr_src [new Application/Traffic/CBR]

$cbr_src set packet_size_ 500

$cbr_src set rate_ 2Mb

$cbr_src attach-agent $udp_src

# Create UDP sink at Node4 (destination)

set null_sink [new Agent/Null]

$ns attach-agent $node4 $null_sink

# Connect source to destination via intermediate nodes (optimal routing will decide the best path)

$ns connect $udp_src $null_sink

# Start and stop the traffic

$ns at 1.0 “$cbr_src start”

$ns at 10.0 “$cbr_src stop”

# Enable AODV routing protocol for optimal path selection

set val(rp) AODV

for {set i 1} {$i <= 4} {incr i} {

set node [set node($i)]

$ns at 0.0 “$node set ragent [new Agent/AODV]”

}

# Enable tracing

set tracefile [open “optimal_routing_trace.tr” w]

$ns trace-all $tracefile

# End simulation

$ns at 20.0 “finish”

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Run the simulation

$ns run

Here’s we provide, how you can replicate the basic optimal routing in ns2 simulation environment that leveraging one of the routing protocols or by configuring the network for optimal path selection. If you have any query regarding the above implementation process we will provide it.

Our team focuses on built-in routing protocols like AODV (Ad hoc On-Demand Distance Vector) and DSDV (Destination-Sequenced Distance-Vector Routing). We can also customize existing protocols to achieve the best routing for your projects. For excellent results, collaborate with ns2project.com. Keep in contact with us for innovative guidance on Network Optimal Routing in NS2. We provide great project ideas and implementation support.