How to Implement Network Routing in NS2

To implement network routing in Network Simulator 2 (NS2) has needs to follow several steps that has includes to utilize the numerous built-in routing protocols like AODV, DSDV, DSR, and others. These protocols are used to handle how packets are transmitted among nodes in both wired and wireless topologies. Here is the procedure on how to perform the network routing in ns2:

Steps to Implement Network Routing in NS2

  1. Set up NS2

Make sure that NS2 is installed on the system. If it’s not installed, we can install it using:

sudo apt-get update

sudo apt-get install ns2

  1. Choosing a Routing Protocol

In NS2, we can use numerous routing protocols relaying on the network type (wired or wireless). Some of the most frequently used protocols in NS2 involve:

  • AODV (Ad-hoc On-Demand Distance Vector): A reactive routing protocol usually utilize in wireless ad-hoc networks.
  • DSDV (Destination-Sequenced Distance-Vector): A proactive routing protocol used in wireless networks.
  • DSR (Dynamic Source Routing): A source routing protocol used in wireless ad-hoc networks.
  • OSPF (Open Shortest Path First): A routing protocol used for wired IP networks.

Example TCL Script for Routing in NS2

The following is a sample TCL script to replicate a wireless network using AODV routing in NS2. The script generates a basic topology and creates traffic among nodes using the AODV routing protocol.

  1. Wireless Network with AODV Routing

# Define the simulator

set ns [new Simulator]

# Open trace file

set tracefile [open routing_out.tr w]

$ns trace-all $tracefile

# Define the topology for wireless nodes

set topo [new Topography]

$topo load_flatgrid 500 500

# Create a wireless channel

set chan_1_ [new Channel/WirelessChannel]

# Define radio propagation model, physical and MAC layers

Phy/WirelessPhy set CPThresh_ 10.0

Phy/WirelessPhy set CSThresh_ 1.0e-10

Phy/WirelessPhy set RXThresh_ 1.0e-10

Phy/WirelessPhy set Rb_ 2Mb

Phy/WirelessPhy set Pt_ 0.2818

Phy/WirelessPhy set freq_ 2.472e9

# Create a global node configuration for wireless

$ns node-config -adhocRouting AODV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType $chan_1_ \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON

# Create 4 wireless nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

# Set the positions of the nodes

$n0 set X_ 100.0

$n0 set Y_ 200.0

$n0 set Z_ 0.0

$n1 set X_ 150.0

$n1 set Y_ 300.0

$n1 set Z_ 0.0

$n2 set X_ 300.0

$n2 set Y_ 300.0

$n2 set Z_ 0.0

$n3 set X_ 350.0

$n3 set Y_ 100.0

$n3 set Z_ 0.0

# Define a mobility model (nodes move during the simulation)

$ns at 1.0 “$n0 setdest 200.0 250.0 5.0”

$ns at 1.5 “$n1 setdest 250.0 350.0 5.0”

$ns at 2.0 “$n2 setdest 350.0 350.0 5.0”

$ns at 2.5 “$n3 setdest 400.0 200.0 5.0”

# Set up traffic flow (UDP over CBR) from n0 to n3

set udp0 [new Agent/UDP]

set null0 [new Agent/Null]

$ns attach-agent $n0 $udp0

$ns attach-agent $n3 $null0

$ns connect $udp0 $null0

# Create CBR (Constant Bit Rate) traffic

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 512

$cbr0 set interval_ 0.05

$cbr0 attach-agent $udp0

$ns at 1.0 “$cbr0 start”

# Schedule simulation end

$ns at 10.0 “finish”

# Finish procedure to close the simulation and generate output

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exec nam routing_out.nam &

exit 0

}

# Run the simulation

$ns run

Explanation of the Script:

  1. Node Configuration with AODV Routing:
    • The script configures wireless nodes to use AODV routing, quantified by -adhocRouting AODV.
    • Other wireless metrics like the MAC layer (802.11), physical layer (WirelessPhy), and the antenna type are also configured.
  2. Node Mobility:
    • Nodes are set to move during the simulation using the setdest command. This movement mimic dynamic network topology that is corporate in wireless ad-hoc networks.
  3. Traffic Flow:
    • UDP traffic is created from node n0 to node n3 using CBR (Constant Bit Rate) traffic. This replicates a continuous stream of packets being sent among the nodes.
  4. Trace and NAM Output:
    • The simulation makes trace files (routing_out.tr) and NAM visualization files (routing_out.nam) that can be utilized to evaluate the routing features and packet flow in the network.
  1. Run the Simulation

Save the script as routing_simulation.tcl and executed it using the following command in NS2:

ns routing_simulation.tcl

To visualize the network using NAM, run:

nam routing_out.nam

  1. Analysing the Routing Behavior

After running the mimic, we can evaluate the trace file (routing_out.tr) to investigate the routing behaviour, such as:

  • Route discovery in AODV will see rreq, rrep, and rerr messages in the trace file.
  • Packet delivery and packet drops.
  • Routing overhead triggered by route discovery processes.

We can extract useful information from the trace file such as:

  • Throughput: By counting the number of received packets and estimating the rate at which they are received.
  • End-to-end delay: By estimating the time difference among packet sending and receiving.
  • Routing overhead: By counting the number of routing control messages like AODV’s route requests and replies.
  1. Switching to Other Routing Protocols

We can simply switch to other routing protocols in NS2 by varying the -adhocRouting option in the node-config section:

  • For DSDV:

$ns node-config -adhocRouting DSDV

  • For DSR:

$ns node-config -adhocRouting DSR

  • For OSPF (for wired networks):

# For OSPF in a wired network, use suitable node configuration without specifying wireless channel.

We explicitly aggregated the significant information regarding the network routing that has generates a basic topology and creates traffic among nodes using the AODV routing protocol that execute in ns2 simulation. We plan to provide additional information regarding the network routing performances in other simulation settings.

We help with network routing in NS2 implementation. We offer great project ideas and topic support. Share your project details with us, and we will assist you in performance analysis. Get our help for implementation.