How to Simulate Network Routing in ns2
To implement the Network Routing in ns2, we have to configure a network topology, setting up routing protocol and stating traffic patterns amongst nodes. Follow the implementation guide of network routing in ns2:
Step-by-Step Implementation:
Step 1: Choose a Routing Protocol
ns2 backs several routing protocols, such as:
- AODV (Ad hoc On-Demand Distance Vector)
- DSDV (Destination-Sequenced Distance Vector)
- DSR (Dynamic Source Routing)
- TORA (Temporally-Ordered Routing Algorithm)
In this procedure, I’ll show you how to execute routing using the AODV protocol, which is often used in ad hoc networks.
Step 2: Create the Tcl Script
You will generate a Tcl script that configures the simulation environment, sets up the routing protocol, and states the network traffic.
Example Tcl Script for AODV Routing
# Create a simulator object
set ns [new Simulator]
# Define the topography object
set topo [new Topography]
$topo load_flatgrid 500 500
# Create the General Operations Director (GOD) for wireless simulations
create-god 10 # Number of nodes in the network
# Configure node settings and specify the routing protocol (AODV)
$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 Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace OFF
# Open trace and NAM files for recording the simulation
set tracefile [open routing_out.tr w]
$ns trace-all $tracefile
set namfile [open routing_out.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define a finish procedure to close files and end the simulation
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam routing_out.nam &
exit 0
}
# Create nodes
for {set i 0} {$i < 10} {incr i} {
set node_($i) [$ns node]
}
# Set initial positions for the nodes
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 100.0
$node_(1) set Y_ 100.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 150.0
$node_(2) set Y_ 150.0
$node_(2) set Z_ 0.0
#… (set positions for other nodes similarly)
# Define a TCP connection from node 0 to node 9
set tcp0 [new Agent/TCP]
$ns attach-agent $node_(0) $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $node_(9) $sink0
$ns connect $tcp0 $sink0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 start
# Define another UDP connection from node 1 to node 8
set udp1 [new Agent/UDP]
$ns attach-agent $node_(1) $udp1
set null1 [new Agent/Null]
$ns attach-agent $node_(8) $null1
$ns connect $udp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set packetSize_ 512
$cbr1 set rate_ 1Mb
$cbr1 start 2.0
# Schedule the end of the simulation
$ns at 20.0 “finish”
# Run the simulation
$ns run
Step 3: Run the Tcl Script
Save the Tcl script with a .tcl extension (like routing_simulation.tcl). Open a terminal and execute the script using the below command:
ns routing_simulation.tcl
This will produce a trace file (routing_out.tr) and a NAM file (routing_out.nam).
Step 4: Visualize the Routing Process
Open the NAM file to visualize the routing and network activity with the following command:
nam routing_out.nam
NAM will show the network topology, viewing how the AODV protocol realizes routes and how packets are forwarded amongst nodes.
Script Explanation
- Simulator Object: $ns [new Simulator] generate the simulator instance.
- Topography: The topography object ($topo) states the simulation area size, which is set to 500×500.
- Node Configuration: $ns node-config fix the wireless node parameters as well as the routing protocol (AODV), MAC type (802.11), and antenna type (OmniAntenna).
- Node Placement: Nodes are placed at particular (X, Y, Z) coordinates using the set X_, set Y_, and set Z_ commands.
- Traffic Definition: TCP and UDP connections are described amongst various nodes to simulate network traffic. FTP traffic is sent over TCP, and CBR (Constant Bit Rate) traffic is sent over UDP.
- Finish Procedure: After the simulation is completed, this procedure cleans up and closes files.
Customization
- Change Routing Protocol: Swap AODV with other routing protocols like DSDV, DSR, or TORA by altering the -adhocRouting option.
- Add More Nodes: Maximize the count of nodes and state extra traffic patterns to generate a more challenging network.
- Modify Traffic: Replicate various network environments by altering the packet sizes, rates, and start times of the traffic.
- Experiment with Mobility: Execute node mobility by dynamically apprising node locations during the simulation.
Advanced Features
- Routing Metrics: Evaluate routing metrics like packet delivery ratio, end-to-end delay, and routing overhead by parsing the trace file.
- Routing Protocol Comparison: Simulate various routing protocols and compare their performance in different network conditions.
- Multicast Routing: Accomplish multicast routing protocols if needed for certain network situations.
This procedure offered a step-by-step guide on how to implement and simulate network routing in ns2. Start by picking a routing protocol like in this manual, we choose AODV (Ad hoc On-Demand Distance Vector) and write the Tcl script for it and we also provide their customization approach and how to use their advance features.
For the most effective support and implementation strategies regarding Network Routing in ns2, we encourage you to contact ns2project.com to ensure optimal results. Our expertise lies in routing protocols and analyzing traffic patterns among nodes relevant to your projects. Please share the specifics of your project with us, and we will provide you with the most efficient guidance available.