How to Implement Dynamic Routing in NS2
To implement the dynamic routing in NS2 which means the potential of the network to update its routing table and alter routes in real-time depends on the network condition like jamming, link failures or changing network loads. This simulation support in-built dynamic routing protocols like Link State (LS), Distance Vector (DV) and ad-hoc On-Demand Distance Vector (AODV) however you can tailor or replicate other dynamic routing activities.If you face any implementation issue then you can be in touch with us, we share with you best results.
You can follow the given demonstration to accomplish the routing in ns2:
Steps to Implement Dynamic Routing in NS2
- Set Up NS2 Environment
- Make sure that NS2 is installed and properly configured on your system.
- Understand Dynamic Routing
- Dynamic routing protocols update routing tables in real-time based on alterations in the network topology or traffic conditions. Routes can vary because of the link failures, blockage, or other factors.
- Typical dynamic routing protocols such as Link-State (LS), Distance Vector (DV), and AODV (used in ad-hoc networks).
- Simulate Dynamic Routing in NS2
- Use the built-in routing protocols of ns2 to simulate the dynamic routing. Additionally, you can simulate dynamic events like link failures or congestion to examine how the routing protocol adapts.
Example 1: Using Built-In Dynamic Routing Protocol (Link State)
In this instance, we will use NS2’s built-in Link-State (LS) protocol to replicate dynamic routing.
OTcl Script for Dynamic Routing Using Link State
# Define the simulation environment
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 different bandwidths and delays
$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
# Enable dynamic routing protocol (Link-State)
$ns rtproto LS
# Attach TCP agents for traffic generation
set tcp1 [new Agent/TCP]
$ns attach-agent $node1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $node5 $sink1
# Connect TCP agent and sink
$ns connect $tcp1 $sink1
# Create an FTP application over TCP and start it
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 “$ftp1 start”
# Simulate a link failure between node2 and node3 at time 3.0 seconds
$ns rtmodel-at 3.0 down $node2 $node3
# Simulate recovery (bring the link back up) at time 7.0 seconds
$ns rtmodel-at 7.0 up $node2 $node3
# Stop the simulation at 10.0 seconds
$ns at 10.0 “finish”
# Run the simulation
$ns run
Explanation:
- Network Topology: This script builds a five-node network with different links of various delays and bandwidths.
- Dynamic Routing Protocol: The line $ns rtproto LS allows NS2’s built-in Link-State (LS) routing protocol. LS allow the nodes to uphold a full view of the network topology and dynamically modify routes.
- Traffic Generation: TCP traffic is produced among node1 and node5 using an FTP application.
- Dynamic Event (Link Failure): At 3 seconds, the link amidst node2 and node3 fails, and the routing protocol adapts to this modifications. The link is restored at 7 seconds.
- Simulation Duration: The simulation executes for 10 seconds.
Running the Simulation:
- Store the script to a file (e.g., dynamic_routing_ls.tcl).
- Execute the script using NS2:
ns dynamic_routing_ls.tcl
- The simulation will develop a trace file (out.tr) and a Network Animator (NAM) file (out.nam). You can use NAM to visualize the simulation and see how the routing fine-tunes after the link failure and recovery.
Example 2: Using AODV for Dynamic Routing (Ad-hoc Networks)
You can use the AODV protocol to execute dynamic routing. AODV adapts routing on demand when nodes move or as network topology alters, for mobile ad-hoc networks,.
OTcl Script for Dynamic Routing Using AODV
# Define the simulation environment
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 (wireless scenario)
$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
# Enable AODV dynamic routing protocol (suitable for ad-hoc networks)
$ns rtproto AODV
# Attach TCP agents for traffic generation
set tcp1 [new Agent/TCP]
$ns attach-agent $node1 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $node5 $sink1
# Connect TCP agent and sink
$ns connect $tcp1 $sink1
# Create an FTP application over TCP and start it
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 “$ftp1 start”
# Simulate node mobility (movement between nodes)
$ns at 3.0 “$node2 setdest 100 100 10”
$ns at 5.0 “$node3 setdest 200 200 15”
# Stop the simulation at 10.0 seconds
$ns at 10.0 “finish”
# Run the simulation
$ns run
Explanation:
- Network Topology: This script designs a five-node network and assumes node mobility.
- Dynamic Routing Protocol (AODV): The line $ns rtproto AODV permit NS2’s built-in AODV protocol. AODV is built for ad-hoc networks and dynamically fine-tunes routes when nodes travel or network topology adjusts.
- Traffic Generation: TCP traffic is generated amidst node1 and node5 using an FTP application.
- Node Mobility: At 3 seconds and 5 seconds, nodes move to various locations, causing the network topology to vary dynamically.
- Simulation Duration: The simulation executes for 10 seconds.
Running the Simulation:
- Log the script to a file (e.g., dynamic_routing_aodv.tcl).
- Execute the script using NS2:
ns dynamic_routing_aodv.tcl
- The simulation will create a trace file and a NAM file. Visualize the simulation and monitor how routes vary dynamically as nodes move by using NAM.
Analyze the Results
- Trace File: Examine the trace file to verify how routing tables are updated dynamically during the simulation.
- NAM Visualization: Use NAM to see how packets are forwarded and how the routing protocol adapts to network changes includes link failures, node movement, or blockings.
Enhancements
- Custom Routing Metrics: Use custom metrics like bandwidth, delay, or congestion to choose the best routes dynamically by modifying the routing protocol.
- Mobility Models: For AODV and other ad-hoc routing protocols, execute different mobility models (like Random Waypoint, Gauss-Markov) to replicate realistic node movements.
- Congestion and Load Balancing: Launch traffic jamming and execute adaptive load balancing to examine how dynamic routing reacts to network load variations.
Through this set up, we entirely focused and provided two techniques and its information on how to implement the dynamic routing using link state or AODV in ns2 tool. You can also enhance the simulation by referring the given details and get further details about the dynamic routing, if needed.