Simulation of Congestion Control Algorithms Using NS-2
Aim
To study the Network Simulator (NS-2) and simulate TCP
congestion control algorithms, namely TCP Reno and TCP Tahoe, and
observe their congestion-window behavior.
Steps to execute
1.
Install NS-2 in Google Colab
a.
Open Google Colab and create a new notebook
b.
Check the Linux environment
o
!uname -a
c.
Update the package list
o
!apt-get update -qq
d.
Install NS-2
o
!apt-get install -y ns2
e.
Check the NS-2 installation and verify
o
!which ns
2. TCP Reno congestion-control algorithm.
a.
Create the Reno TCL file
b.
Run the TCL program
c.
Check the generated files
d.
Plot the TCP Reno congestion window
3.
TCP Tahoe congestion-control algorithm
a.
Create the Tahoe TCL file
b.
Run the TCL program
c.
Check the generated files
d.
Plot the TCP Tahoe congestion window
TCP Reno congestion-control algorithm.
a.
Create the Reno TCL file
%%writefile
reno_cwnd.tcl
# Create
simulator
set ns [new
Simulator]
# Create trace
file
set tracefile
[open reno.tr w]
$ns trace-all
$tracefile
# Create
congestion window file
set cwndfile
[open reno_cwnd.tr w]
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create links
$ns duplex-link
$n0 $n2 10Mb 10ms DropTail
$ns duplex-link
$n1 $n2 10Mb 10ms DropTail
$ns duplex-link
$n2 $n3 1Mb 20ms DropTail
# Queue size
$ns queue-limit
$n2 $n3 10
# Create TCP Reno
set tcp [new
Agent/TCP/Reno]
# Attach TCP to
sender
$ns attach-agent
$n0 $tcp
# Create TCP sink
set sink [new
Agent/TCPSink]
# Attach sink to
receiver
$ns attach-agent
$n3 $sink
# Connect TCP and
sink
$ns connect $tcp
$sink
# Create FTP
application
set ftp [new
Application/FTP]
# Attach FTP to
TCP
$ftp attach-agent
$tcp
# Procedure to
record congestion window
proc record {} {
global ns tcp cwndfile
set now [$ns now]
set cwnd [$tcp set cwnd_]
puts $cwndfile "$now $cwnd"
$ns at [expr $now + 0.01]
"record"
}
# Start recording
$ns at 0.0
"record"
# Start FTP
$ns at 0.5
"$ftp start"
# Stop FTP
$ns at 9.5
"$ftp stop"
# Finish
simulation
$ns at 10.0
"finish"
proc finish {} {
global ns tracefile cwndfile
$ns flush-trace
close $tracefile
close $cwndfile
exit 0
}
# Run simulation
$ns run
b.
Run the TCL program
!ns reno_cwnd.tcl
c.
Check the generated files
!ls -lh reno*
d.
Plot the TCP Reno congestion window
import
matplotlib.pyplot as plt
time = []
cwnd = []
with
open("reno_cwnd.tr", "r") as f:
for line in f:
t, c = line.split()
time.append(float(t))
cwnd.append(float(c))
plt.figure(figsize=(10,5))
plt.plot(time,
cwnd)
plt.xlabel("Time
(seconds)")
plt.ylabel("Congestion
Window (packets)")
plt.title("TCP
Reno Congestion Window")
plt.grid()
plt.show()
TCP Tahoe congestion-control algorithm
a.
Create the Tahoe TCL file
%%writefile
tahoe_cwnd.tcl
# Create
simulator
set ns [new
Simulator]
# Create trace
file
set tracefile
[open tahoe.tr w]
$ns trace-all
$tracefile
# Create
congestion window file
set cwndfile
[open tahoe_cwnd.tr w]
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create links
$ns duplex-link
$n0 $n2 10Mb 10ms DropTail
$ns duplex-link
$n1 $n2 10Mb 10ms DropTail
$ns duplex-link
$n2 $n3 1Mb 20ms DropTail
# Queue size
$ns queue-limit
$n2 $n3 10
# Create TCP
Tahoe
set tcp [new
Agent/TCP]
# Attach TCP to
sender
$ns attach-agent
$n0 $tcp
# Create TCP sink
set sink [new
Agent/TCPSink]
# Attach sink to
receiver
$ns attach-agent
$n3 $sink
# Connect TCP and
sink
$ns connect $tcp
$sink
# Create FTP
application
set ftp [new
Application/FTP]
# Attach FTP to
TCP
$ftp attach-agent
$tcp
# Procedure to
record congestion window
proc record {} {
global ns tcp cwndfile
set now [$ns now]
set cwnd [$tcp set cwnd_]
puts $cwndfile "$now $cwnd"
$ns at [expr $now + 0.01]
"record"
}
# Start recording
$ns at 0.0
"record"
# Start FTP
$ns at 0.5
"$ftp start"
# Stop FTP
$ns at 9.5
"$ftp stop"
# Finish
simulation
$ns at 10.0
"finish"
proc finish {} {
global ns tracefile cwndfile
$ns flush-trace
close $tracefile
close $cwndfile
exit 0
}
# Run simulation
$ns run
b.
Run the TCL program
!ns
tahoe_cwnd.tcl
c.
Check the generated files
!ls -lh tahoe*
d.
Plot the TCP Tahoe congestion window
import
matplotlib.pyplot as plt
time = []
cwnd = []
with
open("tahoe_cwnd.tr", "r") as f:
for line in f:
t, c = line.split()
time.append(float(t))
cwnd.append(float(c))
plt.figure(figsize=(10,5))
plt.plot(time,
cwnd)
plt.xlabel("Time
(seconds)")
plt.ylabel("Congestion
Window (packets)")
plt.title("TCP
Tahoe Congestion Window")
plt.grid()
plt.show()
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu