How to Implement Network Topology in NS2
To implement a network topology in NS2 (Network Simulator 2) will be breakdown into several steps which are generating a TCL script to describe nodes, links, and traffic patterns. This simple configuration can be additional customized relay on the type of network we need to simulate such as wireless, wired, or hybrid. The below is the structured procedure to implement the network topology in ns2:
Steps to Implement a Network Topology in NS2
- Install NS2
Make sure NS2 is installed on system.
- Create a TCL Script
In NS2, network topology is specified using TCL (Tool Command Language) scripts. We will generate a script that describes the nodes, links, and traffic patterns.
Example: Simple Wired Network Topology
Below is a step-by-step sample of executing a simple wired network topology with 4 nodes:
- Define Nodes and Links
# Define simulator object
set ns [new Simulator]
# Define trace and nam file for output (trace and animation)
set tracefile [open out.tr w]
set namfile [open out.nam w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Define a ‘finish’ procedure to close trace files
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create duplex links between nodes (Link between n0-n1, n1-n2, n2-n3)
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
# Add a traffic source (TCP connection from n0 to n3)
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
# Create an FTP application over TCP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp start
# Schedule simulation end
$ns at 5.0 “finish”
# Run the simulation
$ns run
- Explanation of the Script
- Simulator Object ($ns): This generated an example of the simulator to handle events, nodes, and links.
- Nodes: Nodes are generated using $ns node. In this sample, four nodes (n0, n1, n2, n3) are generated.
- Duplex Links: Duplex links among nodes are generated with the duplex-link command, especially bandwidth like 10Mb and latency such as 10ms. A DropTail queue management policy is used here.
- Traffic (TCP/FTP): A TCP agent is attached to n0, and a TCPSink is attached to n3. An FTP application is used to create traffic across the TCP connection.
- Finish Procedure: This protocol describes what happen when the simulation termination that has closing the trace and NAM (Network Animator) files.
- Run the Simulation
- Save the script with a .tcl extension like simple_topology.tcl.
- Execute the script in NS2 using the command:
ns simple_topology.tcl
- View the Output
- The simulation creates a trace file (out.tr) and a NAM animation file (out.nam).
- To envision the network, open the NAM file:
nam out.nam
- Customize the Topology
We can expand this simple network topology by:
- Adding more nodes and links.
- Varying the type of traffic such as UDP, CBR.
- Adapt bandwidth, delay, or queue sizes for different links.
- Executing diverse routing protocols.
Example: Complex Network Topology
For a more complex topology, we can generate more nodes and associating those using links with diverse configurations:
# Create more nodes
set n4 [$ns node]
set n5 [$ns node]
# Create duplex links with different parameters
$ns duplex-link $n3 $n4 5Mb 20ms DropTail
$ns duplex-link $n4 $n5 2Mb 50ms DropTail
# Adding UDP traffic
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp
$ns attach-agent $n5 $null
$ns connect $udp $null
# Create CBR traffic over UDP
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 500
$cbr set interval_ 0.005
$cbr start
This script adds more nodes and UDP traffic using CBR (Constant Bit Rate) over the newly generated links.
- Wireless Network Topology (Optional)
If we need to generate a wireless network, we will need to setup mobile nodes and configure wireless communication parameters. Here’s a quick sample for wireless:
# Set up wireless environment
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(ant) Antenna/OmniAntenna
set val(ll) LL
set val(ifq) Queue/DropTail/PriQueue
set val(mac) Mac/802_11
set val(netif) Phy/WirelessPhy
set val(rp) DSDV
set val(x) 500
set val(y) 500
# Define a simulator object
set ns [new Simulator]
# Define the topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Create a node
set n0 [$ns node]
$ns node-config -adhocRouting $val(rp) -llType $val(ll) -macType $val(mac) \
-ifqType $val(ifq) -ifqLen 50 -antType $val(ant) \
-propType $val(prop) -phyType $val(netif) \
-channelType $val(chan)
- Analyse Results
After the simulation, evaluate the trace files created by NS2. we can extract parameters such as throughput, packet delivery ratio, end-to-end delay, and more using tools such as AWK, Perl, or Python to process the trace file (*.tr).
We know how to execute and validate the outcomes for network topology by using the ns2 tool that effectively mimic the wireless, wired and hybrid networks. We will plan to extend the information how the network topology performs in other simulation tool.
We help you with setting up Network Topology using the NS2 tool, and you can count on us as your reliable partner. We assist you in simulating wireless, wired, or hybrid networks.