How to Implement Distance vector Routing in NS2

To implement Distance Vector Routing (typically known as Distance Vector Routing Protocol, or DVR) in NS2 (Network Simulator 2), you can utilize the DSDV (Destination-Sequenced Distance-Vector) routing protocol, which is a standard distance vector protocol existed in NS2.

DSDV is depends on the classic Bellman-Ford distance vector algorithm, and it occasionally updates routing tables with the distances to all other nodes in the network.

The given step-by-step approach will guide you to accomplish it in ns2:

Steps to Implement Distance Vector Routing (DSDV) in NS2

  1. Set Up NS2 Environment

Make sure to install and configure the ns2 on your computer. If you haven’t installed it yet, you can do so using the following command (on Linux):

sudo apt-get install ns2

  1. TCL Script to Simulate DSDV (Distance Vector Routing Protocol)

Here’s a basic sample of how to configure a simulation using the DSDV routing protocol in NS2:

# Create the simulator object

set ns [new Simulator]

# Open a trace file to log the simulation

set tracefile [open dsdv_trace.tr w]

$ns trace-all $tracefile

# Open a NAM file to visualize the simulation

set namfile [open dsdv_simulation.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 dsdv_simulation.nam &

exit 0

}

# Create nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

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

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

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

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

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

# Enable the DSDV routing protocol (Distance Vector Routing)

set opt(adhocRouting) DSDV

# 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 and attach it to node 4 (destination)

set null0 [new Agent/Null]

$ns attach-agent $n4 $null0

# Connect the UDP agent to the null agent (unicast communication)

$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.2

$cbr attach-agent $udp0

# Schedule the CBR traffic to start and stop

$ns at 1.0 “$cbr start”

$ns at 4.0 “$cbr stop”

# Schedule the end of the simulation at 5 seconds

$ns at 5.0 “finish”

# Run the simulation

$ns run

  1. Explanation of the Script:
  • DSDV Protocol: DSDV is fix as the routing protocol using set opt(adhocRouting) DSDV. DSDV is a distance-vector protocol that upholds routing tables that has the shortest paths (in terms of the count of hops) to all nodes in the network. These tables are sporadically updated and interchanged with neighbors.
  • Nodes and Links: The script generates five nodes (n0 to n4) and accomplishes duplex links amongst them with 2Mbps bandwidth and 10ms delay.
  • Traffic Generation: A UDP agent delivers packets from node n0 to node n4 using CBR (Constant Bit Rate) traffic. This permits you to monitor how DSDV updates the routing tables and routes packets.
  • Trace and Visualization Files: The simulation produces trace (dsdv_trace.tr) and NAM (dsdv_simulation.nam) files to visualize the network activities.
  1. Running the Simulation:
  1. Save the TCL script as dsdv_example.tcl.
  2. Execute the simulation using the following command:

ns dsdv_example.tcl

  1. This will create two output files:
    • dsdv_trace.tr: The trace file has information about the simulation containing packet transmissions, route updates, etc.
    • dsdv_simulation.nam: A file that can be visualized using NAM (Network Animator).
  2. To visualize the simulation, use the given command:

nam dsdv_simulation.nam

This will open the NAM visualizer, where you can monitor how DSDV dynamically updates the routing tables when traffic flows through the network.

  1. Analyzing the Simulation:
  • Trace File: The trace file includes information about the packet transmissions, route updates, and events in the network. You can parse this file to measure performance metrics includes packet delivery ratio, end-to-end delay, and routing overhead.
  • Visualization with NAM: In the NAM window, you can see the packet transmissions amongst nodes, along with the routes that are accomplished depends on the DSDV protocol.
  1. Adding Node Mobility (Optional):

If you’re working in a Mobile Ad Hoc Network (MANET) environment, you can launch mobility to the nodes. Here’s an instance of how to include mobility to node n1:

# Set the initial position of the node

$n1 set X_ 0

$n1 set Y_ 0

$n1 set Z_ 0

# Set the movement of the node

$ns at 2.0 “$n1 setdest 100 100 10.0”

$ns at 3.0 “$n1 setdest 200 200 20.0”

This moves node n1 from (0, 0) to (100, 100) at 10 units per second starting at 2 seconds, and then to (200, 200) at 20 units per second starting at 3 seconds. The DSDV protocol will dynamically update the routes as the topology varies because of node mobility.

In conclusion, we thoroughly concentrated on the implementation of distance vector routing protocol and how to maintain the routes based on the distance amongst the nodes in ns2. If needed, we can also provide additional information of this DVR. Contact ns2project.com! We’re here to help you with the best Distance Vector Routing implementation in NS2. Plus, we have awesome research ideas and topics that fit your project perfectly!