Do the following using NS2 Simulator
- NS2 Simulator - Introduction
- Simulate to Find the Number of Packets Dropped
- Simulate to Find the Number of Packets Dropped by TCP/UDP
- Simulate to Find the Number of Packets Dropped due to Congestion
- Simulate to Compare Data Rate & Throughput
- Simulate to Plot Congestion for Different Source/Destination
- Simulate to Determine the Performance with respect to Transmission of Packets
NS2 Simulator :
Aim
To study the NS2 network simulator and use it to simulate packet drops, TCP/UDP traffic, congestion, data rate and throughput, and the congestion window of TCP flows.
i. NS2 Simulator – Introduction
NS2 (Network Simulator version 2) is an open-source, discrete-event simulator used for research and teaching in networking. It simulates wired and wireless networks and protocols such as TCP, UDP, routing protocols and queue-management schemes.
- The simulator is written in C++ (the protocols) and the simulation script is written in
OTcl (an object-oriented Tcl). The script file has the extension
.tcl. - Running a script produces a trace file (
.tr) that records every packet event, and an optional NAM file (.nam) that the Network Animator (NAM) plays as an animation. - The trace file is analysed with awk scripts and the results are plotted with xgraph or gnuplot.
Installation and execution (Ubuntu):
sudo apt update
sudo apt install ns2 nam xgraph gawk
# run a simulation script
ns simple.tcl
Steps of a simulation script:
- Create the simulator object:
set ns [new Simulator] - Open the trace files:
trace-allandnamtrace-all - Create the nodes:
$ns node - Create the links:
$ns duplex-link node1 node2 bandwidth delay queue-type - Create the agents (TCP, UDP, sinks) and attach them to the nodes
- Create the applications (FTP, CBR) and attach them to the agents
- Schedule events with
$ns at time "action"and call$ns run
File Name: simple.tcl
# simple.tcl : one UDP flow between two nodes
set ns [new Simulator]
set tr [open simple.tr w]
$ns trace-all $tr
set nf [open simple.nam w]
$ns namtrace-all $nf
proc finish {} {
global ns tr nf
$ns flush-trace
close $tr
close $nf
exec nam simple.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n1 $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 500
$cbr set interval_ 0.01
$ns at 0.5 "$cbr start"
$ns at 4.5 "$cbr stop"
$ns at 5.0 "finish"
$ns run
Format of a line of the trace file:
+ 0.5 0 1 cbr 500 ------- 0 0.0 1.0 0 0
r 0.514 0 1 cbr 500 ------- 0 0.0 1.0 0 0
| Field | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Meaning | event | time | from node | to node | packet type | size (bytes) | flags | flow id | source | destination | sequence no. | packet id |
Event: + = enqueued, - = dequeued, r = received, d = dropped.
Common topology and trace file for parts ii to v and vii
A TCP flow (FTP) from n0 and a UDP flow (CBR, 1 Mbps) from n1 are sent to n3 through the router n2. The link n2–n3 has a bandwidth of only 1 Mbps and a queue of 10 packets, so it is the bottleneck where packets are dropped.
File Name: congestion.tcl
# congestion.tcl : a TCP flow and a UDP flow share a 1 Mbps bottleneck link
#
# n0 (TCP source) ---\
# n2 ======== n3 (destination of both flows)
# n1 (UDP source) ---/ 1 Mbps bottleneck
#
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red
set tr [open out.tr w]
$ns trace-all $tr
set nf [open out.nam w]
$ns namtrace-all $nf
proc finish {} {
global ns tr nf
$ns flush-trace
close $tr
close $nf
exec nam out.nam &
exit 0
}
# nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# links
$ns duplex-link $n0 $n2 10Mb 5ms DropTail
$ns duplex-link $n1 $n2 10Mb 5ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns queue-limit $n2 $n3 10
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
# TCP flow n0 -> 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
$tcp set fid_ 1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# UDP flow n1 -> n3 (1000 bytes every 8 ms = 1 Mbps)
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ 1000
$cbr set interval_ 0.008
# events
$ns at 0.1 "$ftp start"
$ns at 0.1 "$cbr start"
$ns at 9.5 "$ftp stop"
$ns at 9.5 "$cbr stop"
$ns at 10.0 "finish"
$ns run
Run the script: ns congestion.tcl. The trace file out.tr is analysed in the following parts.
ii. Simulate to Find the Number of Packets Dropped
A dropped packet is recorded in the trace file with the event d. Count these lines:
awk '$1=="d" {drop++} END {print "Total packets dropped =", drop+0}' out.tr
Change $ns queue-limit $n2 $n3 10 to a larger value (for example 50) and run again to see how the queue size
affects the number of dropped packets.
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
TCP packets have the type tcp and the CBR (UDP) packets have the type cbr in the trace file.
Count the dropped packets of each type:
awk '$1=="d" && $5=="tcp" {tcp++}
$1=="d" && $5=="cbr" {udp++}
END {
print "TCP packets dropped =", tcp+0
print "UDP (CBR) packets dropped =", udp+0
}' out.tr
Observation: UDP does not slow down when packets are lost, but TCP reduces its sending rate after a loss, so the two flows are affected differently by the same bottleneck.
iv. Simulate to Find the Number of Packets Dropped due to Congestion
Congestion occurs when the offered load (TCP + UDP = more than 1 Mbps) is greater than the capacity of the bottleneck link (1 Mbps). The queue becomes full and packets are dropped. The number of drops in each second of the simulation is:
awk '$1=="d" {n[int($2)]++}
END {for (i = 0; i <= 10; i++) print i, n[i]+0}' out.tr > drops.dat
# plot drops per second
xgraph drops.dat
Repeat the simulation with a lower UDP rate ($cbr set interval_ 0.016) and compare the number of drops.
v. Simulate to Compare Data Rate & Throughput
Data rate is the rate at which the source sends data, and throughput is the rate at which the data is actually received at the destination. The CBR packets enter the network at node 1 and are received at node 3. The flow runs for 9.4 seconds (0.1 s to 9.5 s).
awk -v T=9.4 '
$1=="+" && $3==1 && $5=="cbr" {sent += $6}
$1=="r" && $4==3 && $5=="cbr" {recv += $6}
END {
printf "Data rate (sent) = %.1f kbps\n", sent*8/T/1000
printf "Throughput (recv) = %.1f kbps\n", recv*8/T/1000
}' out.tr
Observation: the throughput is lower than the data rate because some packets are dropped at the
bottleneck. For the TCP flow, use $3==0, $4==3 and $5=="tcp" in the same script.
vi. Simulate to Plot Congestion for Different Source/Destination
The congestion window (cwnd_) of a TCP source shows how TCP reacts to congestion: it grows
during slow start, grows linearly in congestion avoidance and is reduced after a packet loss. Here two TCP flows with
different sources and destinations (n0→n4 and n1→n5) share the same bottleneck and their
congestion windows are plotted together.
File Name: cwnd.tcl
# cwnd.tcl : congestion window of two TCP flows with different source/destination
#
# n0 ---\ /--- n4
# n2 ======== n3
# n1 ---/ bottleneck \--- n5
#
set ns [new Simulator]
set w0 [open win0.dat w]
set w1 [open win1.dat w]
proc finish {} {
global ns w0 w1
close $w0
close $w1
exec xgraph win0.dat win1.dat -geometry 800x400 &
exit 0
}
# write the congestion window of a TCP agent to a file every 0.1 s
proc plotWindow {tcpSource outfile} {
global ns
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $outfile "$now $cwnd"
$ns at [expr $now + 0.1] "plotWindow $tcpSource $outfile"
}
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
$ns duplex-link $n(0) $n(2) 10Mb 5ms DropTail
$ns duplex-link $n(1) $n(2) 10Mb 5ms DropTail
$ns duplex-link $n(2) $n(3) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(4) 10Mb 5ms DropTail
$ns duplex-link $n(3) $n(5) 10Mb 5ms DropTail
$ns queue-limit $n(2) $n(3) 10
# TCP flow 0 : n0 -> n4
set tcp0 [new Agent/TCP]
$tcp0 set window_ 100
$ns attach-agent $n(0) $tcp0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n(4) $sink0
$ns connect $tcp0 $sink0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
# TCP flow 1 : n1 -> n5
set tcp1 [new Agent/TCP]
$tcp1 set window_ 100
$ns attach-agent $n(1) $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n(5) $sink1
$ns connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 0.0 "plotWindow $tcp0 $w0"
$ns at 0.0 "plotWindow $tcp1 $w1"
$ns at 0.1 "$ftp0 start"
$ns at 1.0 "$ftp1 start"
$ns at 19.5 "$ftp0 stop"
$ns at 19.5 "$ftp1 stop"
$ns at 20.0 "finish"
$ns run
Run ns cwnd.tcl. The program xgraph plots win0.dat and win1.dat
(time on the X axis and congestion window on the Y axis). The saw-tooth shape of the curves shows slow start, congestion
avoidance and the reduction of the window after losses.
vii. Simulate to Determine the Performance with respect to Transmission of Packets
The performance of the TCP flow of congestion.tcl (source n0, destination n3) is measured with the
number of packets sent, received and dropped, the packet delivery ratio and the
average end-to-end delay.
awk '
BEGIN {sent = 0; recv = 0; drop = 0; delay = 0}
$1=="+" && $3==0 && $5=="tcp" {sent++; st[$12] = $2}
$1=="r" && $4==3 && $5=="tcp" {recv++; delay += $2 - st[$12]}
$1=="d" && $5=="tcp" {drop++}
END {
printf "Packets sent = %d\n", sent
printf "Packets received = %d\n", recv
printf "Packets dropped = %d\n", drop
if (sent > 0) printf "Packet delivery ratio = %.2f %%\n", recv*100/sent
if (recv > 0) printf "Average end-to-end delay = %.4f s\n", delay/recv
}' out.tr
The packet delivery ratio is (packets received / packets sent) × 100. Change the bottleneck bandwidth, the queue limit or the number of flows, run the simulation again and compare the results.
Observation
Record the scripts, the commands used and the values obtained in each part (packets dropped, drops of TCP and UDP, data rate and throughput, congestion-window plot, packet delivery ratio and delay) in the lab record.