How to Implement Unicast Routing in NS2

To implement a Unicast routing in Network Simulator 2 (NS2) has numerous steps to follow and it defined to the process of sending packets from a single source node to a single destination node and this is the most frequent form of routing, in which the packets are sent directly to a particular recipient using numerous routing protocols and also there is several ways to deploy the unicast routing in ns2 that relaying on whether we want to use static routes (manual) or dynamic routes (using routing protocols such as AODV, DSR, DSDV, etc.).  The below are the two common techniques that are static unicast routing and dynamic unicast routing using a routing protocol.

Step-by-Step Implementation:

  1. Static Unicast Routing in NS2

In this technique, we manually setup the routes among the nodes. This technique is appropriate for networks with a fixed topology in which we know the paths in advance.

Example TCL Script for Static Unicast Routing:

# Create the simulator object

set ns [new Simulator]

# Open a trace file to log simulation events

set tracefile [open unicast_trace.tr w]

$ns trace-all $tracefile

# Open a nam file for visualization

set namfile [open unicast_routing.nam w]

$ns namtrace-all $namfile

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam unicast_routing.nam &

exit 0

}

# Create 4 nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

# Define the links between the nodes (bandwidth, delay, and queuing mechanism)

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

# Manually define the routes for unicast communication (static routing)

$n0 add-route $n3 $n1

$n1 add-route $n3 $n2

$n2 add-route $n3 $n3

# Create a UDP agent and attach it to node 0 (source)

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Create a null agent (sink) and attach it to node 3 (destination)

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

# Connect the UDP agent with the null agent (sink)

$ns connect $udp0 $null0

# Create CBR (Constant Bit Rate) traffic over the UDP connection

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set interval_ 0.05

$cbr attach-agent $udp0

# Schedule the CBR traffic start and stop

$ns at 0.5 “$cbr start”

$ns at 4.5 “$cbr stop”

# Schedule the simulation finish at 5 seconds

$ns at 5.0 “finish”

# Run the simulation

$ns run

Explanation of the Script:

  • Static Routes: The add-route command is used to physically describe the unicast routes among nodes. In this case, packets from n0 to n3 will go through nodes n1 and n2.
  • Traffic Setup: A UDP agent is used to transfer packets from n0 to n3, with a CBR traffic generator devoted to the UDP agent.
  • Simulation Visualization: The trace and NAM files are used to evaluate and visualize the packet transmission in NAM.
  1. Dynamic Unicast Routing Using a Routing Protocol (AODV)

If we need to execute dynamic unicast routing, in which the routes are enthusiastically explored according to network topology, we can use routing protocols like Ad-hoc On-demand Distance Vector (AODV), Dynamic Source Routing (DSR), or Destination-Sequenced Distance Vector (DSDV) and these protocols are intended for unicast routing and enthusiastically update routes as the topology changes.

Example TCL Script for Unicast Routing Using AODV:

# Create the simulator object

set ns [new Simulator]

# Open a trace file to record simulation events

set tracefile [open dynamic_unicast_trace.tr w]

$ns trace-all $tracefile

# Open a nam file for network visualization

set namfile [open dynamic_unicast_routing.nam w]

$ns namtrace-all $namfile

# Define the finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam dynamic_unicast_routing.nam &

exit 0

}

# Create 5 nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

# Define the links between nodes (bandwidth, delay)

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

$ns duplex-link $n0 $n3 1Mb 15ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

# Enable AODV as the dynamic routing protocol

set opt(adhocRouting) AODV

# Create UDP agent and attach it to node 0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Create a Null agent and attach it to node 4 (sink)

set null0 [new Agent/Null]

$ns attach-agent $n4 $null0

# Connect the UDP agent to the Null agent (unicast connection)

$ns connect $udp0 $null0

# Create CBR traffic over the UDP connection

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set interval_ 0.05

$cbr attach-agent $udp0

# Schedule traffic start and stop

$ns at 0.5 “$cbr start”

$ns at 4.5 “$cbr stop”

# Schedule end of the simulation

$ns at 5.0 “finish”

# Run the simulation

$ns run

Explanation of the Script:

  • Routing Protocol (AODV): The AODV protocol is permitted using the opt(adhocRouting) parameter. AODV enthusiastically identify the best route among the source and the destination according to the current network topology.
  • Traffic Setup: A UDP agent is configure to send packets from n0 to n4, and the AODV protocol will enthusiastically introduce the route.
  • Visualization: Like the static routing instance, the trace and NAM files are used to evaluate and visualize the routing in action.
  1. Running the Simulation

Once we save either of the scripts (static_unicast.tcl or dynamic_unicast.tcl), we can execute the simulation using the following command in the terminal:

ns your_script.tcl

This will create the trace and NAM files. To visualize the network in NAM, use the following command:

nam your_script.nam

  1. Analysis of the Simulation
  • Trace File Analysis: The trace file (unicast_trace.tr or dynamic_unicast_trace.tr) will encompass information about packet transmission, such as:
    • Packet transmission times
    • Routes taken by the packets
    • Packet losses or delays
  • Visualization with NAM: We need to visualize the entire process of packet transmission, route discovery (for dynamic routing), and packet delivery in the NAM window.

Summary:

  • Static Unicast Routing: Manually describe routes in the network using the add-route command. This is valuable for small or fixed topologies.
  • Dynamic Unicast Routing (AODV): Utilize a dynamic routing protocol such as AODV to systematically identify the best routes according to the current network topology.

According to this procedure we have seen and aggregated the essential information regarding the example based on Unicast routing that includes the simulation process in the ns2 tool. We will also offer more information about the Unicast routing. If you require any implementation support then you can approach us for best results.