To simulate Network Routing projects in NS2 has includes to configure a network topology and set up a routing protocols like AODV, DSDV, DSR, OSPF, and others to evaluate their performance in routing data among nodes. NS2 deliver numerous routing protocols for both wired and wireless scenarios, and we can replicate multiple routing techniques and relate their effectiveness based on packet delivery, throughput, latency, etc.
Here is a step-by-step guide on how to simulate network routing projects using NS2.
Steps to Simulate Network Routing Projects in NS2
- Install NS2
Ensure that we have NS2 installed on the system. We can download NS2 from the NS2 official website. Follow the installation instructions according to operating system (Linux is recommended).
- Basic Concepts in Network Routing
- Routing Protocols: NS2 supports both proactive (table-driven) and reactive (on-demand) routing protocols.
- Proactive Protocols: DSDV, OSPF, RIP.
- Reactive Protocols: AODV, DSR, TORA.
- Traffic Agents: Data is routed using traffic agents such as TCP or UDP. Routing protocols choose how packets are forwarded over the network.
- Routing Metrics: parameters such as packet delivery ratio, throughput, end-to-end delay, and routing overhead are utilized to measure the effectiveness of routing protocols.
- Set up a Basic Network Routing Simulation
Example: Wired Network Routing Using DSDV (Proactive Routing)
This sample configures a simple wired network using the DSDV routing protocol. The goal is to route packets among two nodes in the network.
# Create the NS2 simulator instance
set ns [new Simulator]
# Define a trace file to store the output
set tracefile [open dsdv_trace.tr w]
$ns trace-all $tracefile
# Define the network nodes
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
# Create a duplex link between node1, node2, and node3 (10Mb bandwidth, 10ms delay)
$ns duplex-link $node1 $node2 10Mb 10ms DropTail
$ns duplex-link $node2 $node3 10Mb 10ms DropTail
# Enable DSDV routing protocol on all nodes
$ns rtproto DSDV
# Set up a TCP agent on node1 and a sink (receiver) on node3
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node1 $tcp
$ns attach-agent $node3 $sink
# Connect the TCP agent to the sink
$ns connect $tcp $sink
# Set up FTP traffic over TCP from node1 to node3
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# Schedule FTP traffic to start at time 1.0s and stop at time 4.5s
$ns at 1.0 “$ftp start”
$ns at 4.5 “$ftp stop”
# Define a finish procedure to stop the simulation
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Schedule the simulation to stop after 6 seconds
$ns at 6.0 “finish”
# Run the simulation
$ns run
Explanation:
- Nodes: Three nodes (node1, node2, and node3) are generated and associated through duplex links.
- DSDV Routing Protocol: The DSDV routing protocol is permit on all nodes using the command $ns rtproto DSDV.
- TCP Communication: A TCP agent is attached to node1 (source), and a TCP sink is attached to node3 (destination).
- FTP Traffic: An FTP application is utilized to create TCP traffic from node1 to node3.
- Simulating Wireless Network Routing Using AODV (Reactive Routing)
Wireless ad-hoc networks depend on on-demand routing protocols such as AODV or DSR. Here’s an instance using AODV in a wireless network:
Example: Wireless Network Routing Using AODV (Reactive Routing)
# Create NS2 simulator instance
set ns [new Simulator]
# Set up a wireless channel
set chan [new Channel/WirelessChannel]
# Configure wireless nodes
$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
# Create four wireless nodes
set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
# Set initial positions of nodes
$node1 set X_ 50.0
$node1 set Y_ 100.0
$node2 set X_ 150.0
$node2 set Y_ 100.0
$node3 set X_ 250.0
$node3 set Y_ 100.0
$node4 set X_ 350.0
$node4 set Y_ 100.0
# Attach UDP agents to simulate communication
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $node1 $udp
$ns attach-agent $node4 $null
$ns connect $udp $null
# Create CBR traffic over UDP from node1 to node4
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 512
$cbr set interval_ 0.5
# Schedule CBR traffic to start at time 1.0s and stop at time 4.0s
$ns at 1.0 “$cbr start”
$ns at 4.0 “$cbr stop”
# Enable tracing
set tracefile [open aodv_trace.tr w]
$ns trace-all $tracefile
# Finish procedure to stop the simulation
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Run the simulation for 5 seconds
$ns at 5.0 “finish”
$ns run
Explanation:
- Wireless Nodes: Four wireless nodes are generated and positioned physically using X and Y coordinates.
- AODV Routing: AODV is allowed for ad-hoc routing using the -adhocRouting AODV option.
- UDP Communication: A UDP agent is attached to node1 (source), and a Null agent (sink) is attached to node4 (destination).
- CBR Traffic: Constant Bit Rate (CBR) traffic is created over UDP from node1 to node4.
- More Complex Topologies and Routing Scenarios
5.1. Multi-Hop Routing
In multi-hop routing, packets are forwarded via intermediate nodes before reaching their destination. The example below replicates multi-hop routing in which the data from a source node is forwarded via intermediate nodes to the destination.
# Set up a multi-hop wireless topology
$ns duplex-link $node1 $node2 10Mb 10ms DropTail
$ns duplex-link $node2 $node3 10Mb 10ms DropTail
$ns duplex-link $node3 $node4 10Mb 10ms DropTail
5.2. Mobility in Wireless Networks
We can replicate mobility in wireless ad-hoc networks using NS2’s mobility models. For instance, here’s how we can move nodes dynamically in the course of the simulation:
# Set initial and destination positions to simulate node mobility
$node1 setdest 200.0 300.0 10.0 ;# Move to (200, 300) at 10 m/s
- Analysing Routing Performance Metrics
After executing the simulation, we need to evaluate routing performance according to parameters like:
- Throughput: The rate at which data is successfully delivered to the destination.
- Packet Delivery Ratio (PDR): The percentage of successfully received packets.
- End-to-End Delay: The time taken for data to travel from the source to the destination.
- Routing Overhead: The number of control packets (like route requests) created by the routing protocol.
Example: Analysing Trace File for Packet Delivery
We can compose a simple AWK or Python script to evaluate the trace file (trace.tr) and estimate the parameters such as throughput, packet loss, or delay.
# AWK script to count the number of successfully received packets
BEGIN { received = 0 }
{
if ($1 == “r” && $4 == “AGT” && $7 == “tcp”) {
received++
}
}
END { print “Packets received: “, received }
- Visualizing the Simulation in NAM
To envision the network routing and packet flow in NAM (Network Animator), add the following to the script:
# Enable NAM trace file
set namfile [open simulation.nam w]
$ns namtrace-all $namfile
Execute the simulation and open NAM to envision the network:
nam simulation.nam
- Advanced Routing Scenarios
8.1. QoS-Aware Routing
We can replicate Quality of Service (QoS) routing by setting priorities or constraints on specific traffic types. For instance, we can configure different queues (e.g., RED, DropTail) and set up the network to select specific packets.
8.2. Comparison of Routing Protocols
We can simulate multiple routing protocols (such as compare AODV, DSR, DSDV) in the same topology and relate their performance using key parameters like throughput, delay, and overhead.
In this presented manual, we all understood the basic concepts of network routing project and also we offered the brief demonstration on how to simulate and evaluate the routing data among the nodes using the tool of ns2. If you did like to know more details regarding this process will provide it in upcoming manual. We provide more information about the Network Routing Projects. Please share your details with us, and we will assist you in getting the best simulation results.