How to Implement DSR Routing in NS2

To implement the DSR (Dynamic Source Routing) in Network Simulator 2 (NS2), we have to understand how the ns2 functions with the routing protocols and how DSR performs inside the network. Nevertheless, NS2 already have a built-in execution of DSR, so we don’t need to design it from the beginning. Yet, we have to tailor or replicate it by altering the available protocol or typically using the pre-defined DSR implementation for simulation purposes.

Contact ns2project.com for best implementation guidance in DSR Routing in NS2. Get best project guidance from us.

In the following below have delivered the instructions to implement it in ns2:

Steps to Implement DSR Routing in NS2:

  1. Use the Built-in DSR Protocol

NS2 has a built-in implementation of DSR. State the DSR as a routing protocol to replicate it using TCL script.

  1. TCL Script to Simulate DSR

To simulate DSR in NS2, you need to configure a network topology, set up nodes to use the DSR protocol, and execute the simulation. Below is an example of a TCL script that sets up a basic wireless ad-hoc network using DSR:

# Define Simulator

set ns [new Simulator]

# Define the trace file

set tracefile [open “dsr_trace.tr” w]

$ns trace-all $tracefile

# Define the nam file

set namfile [open “dsr.nam” w]

$ns namtrace-all $namfile

# Set up the nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

# Configure wireless nodes with DSR protocol

$ns node-config -adhocRouting DSR \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON

# Create wireless links (for visualization)

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

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

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

# Create TCP agents and applications

set tcp0 [new Agent/TCP]

set sink [new Agent/TCPSink]

$ns attach-agent $n0 $tcp0

$ns attach-agent $n3 $sink

$ns connect $tcp0 $sink

# Create a CBR traffic generator and attach it to the TCP agent

set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 512

$cbr set interval_ 0.2

$cbr attach-agent $tcp0

# Schedule traffic

$ns at 0.1 “$cbr start”

$ns at 4.5 “$cbr stop”

# Define simulation end

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam dsr.nam &

exit 0

}

# Schedule end of simulation

$ns at 5.0 “finish”

# Run simulation

$ns run

Key Elements in the TCL Script:

  • Wireless node configuration: The node-config command design wireless nodes to utilize DSR with other configuration parameters like MAC type, antenna type, and interface queue type.
  • Agent configuration: The TCP agent (Agent/TCP) and TCPSink are used to replicate traffic generation and reception.
  • Traffic generation: A CBR (Constant Bit Rate) traffic generator is included to the TCP agent, simulating data transmit amongst nodes.
  • Tracing and visualization: The trace and nam files are used for logging the simulation and use NAM (Network Animator) to visualize it.
  1. Run the Simulation
  1. Save the TCL script to a file (e.g., dsr_simulation.tcl).
  2. Run the simulation in NS2 using the below command:

ns dsr_simulation.tcl

  1. View the output:
    • The trace file (e.g., dsr_trace.tr) has entire information of the packet flow, which you can assess.
    • We have to open the NAM file (dsr.nam) to visualize the simulation using below command:

nam dsr.nam

  1. Customizing DSR Behavior (Optional)

If you want to alter or extend the DSR routing protocol itself, you’ll need to work with the C++ source files:

  • DSR-related source files:
    • dsragent.cc: Includes the core DSR agent logic.
    • dsragent.h: Header file for DSR protocol.

To make changes:

  1. Edit the relevant files in the ~/ns-allinone-2.35/ns-2.35/dsr/ directory.
  2. After making modifications, recompile NS2:

./configure

make clean

make

You can modify how DSR discovers routes, manages route maintenance, or caches route information by editing the logic in these files.

  1. Performance Evaluation

Analyze the different performance by extending the simulation such as:

  • Packet Delivery Ratio (PDR)
  • End-to-End Delay
  • Throughput

These metrics can be extracted from the trace file using parsing scripts (written in AWK, Perl, or Python).

Through this set up, we offered example with snippet codes of DSR protocols which is executed in ns2 environment. We also provide the analyzing steps, node setups and agent configuration of it to help you understand the process.