CN Simulation
CN Simulation
INTRODUCTION TO NS2
Widely known as NS2, is simply an event driven simulation tool.
Useful in studying the dynamic nature of communication networks.
Simulation of wired as well as wireless network functions and protocols (e.g., routing
algorithms, TCP, UDP) can be done using NS2.
In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviours.
Tcl scripting
• Tcl is a general purpose scripting language. [Interpreter]
• Tcl runs on most of the platforms such as Unix, Windows, and Mac.
• The strength of Tcl is its simplicity.
• It is not necessary to declare a data type for variable prior to the usage.
Basics of TCL
Syntax: command arg1 arg2 arg3
Hello World!
puts stdout{Hello, World!}
Hello, World!
Variables Command Substitution
set a 5 set len [string length foobar]
set b $a set len [expr [string length foobar] + 9]
Simple Arithmetic
expr 7.2 / 4
Procedures
proc Diag {a b} {
set c [expr sqrt($a * $a + $b * $b)]
return $c }
puts ―Diagonal of a 3, 4 right triangle is [Diag 3 4]ǁ
Output: Diagonal of a 3, 4 right triangle is 5.0
Loops
while{$i < $n} { for {set i 0} {$i < $n} {incr i} {
......} }
NS Simulator Preliminaries.
1. Initialization and termination aspects of the ns simulator.
2. Definition of network nodes, links, queues and topology.
3. Definition of agents and of applications.
4. The nam visualization tool.
5. Tracing and random variables.
In order to have output files with data on the simulation (trace files) or files used for
visualization (nam files), we need to create the files using ―openǁ command:
#Open the Trace file
set tracefile1 [open out.tr w]
$ns trace-all $tracefile1
#Open the NAM trace file
set namfile [open out.nam w]
$ns namtrace-all $namfile
The above creates a data trace file called ―out.trǁ and a nam visualization trace file called
―out.namǁ. Within the tcl script, these files are not called explicitly by their names, but instead
by pointers that are declared above and called ―tracefile1ǁ and ―namfileǁ respectively. Remark
that they begins with a # symbol. The second line open the file ―out.trǁ to be used for writing,
declared with the letter ―wǁ. The third line uses a simulator method called trace-all that have as
parameter the name of the file where the traces will go.
The last line tells the simulator to record all simulation traces in NAM input format. It also gives
the file name that the trace will be written to later by the command $ns flush-trace. In our case,
this will be the file pointed at by the pointer ―$namfileǁ,i.e the file ―out.trǁ.
If the buffer capacity of the output queue is exceeded then the last packet to arrive is dropped.
Many alternative options exist, such as the RED (Random Early Discard) mechanism, the FQ
(Fair Queuing), the DRR (Deficit Round Robin), the stochastic Fair Queuing (SFQ) and the CBQ
(which including a priority and a round-robin scheduler).
In ns, an output queue of a node is implemented as a part of each link whose input is that
node. We should also define the buffer capacity of the queue related to each link. An example
would be:
#set Queue Size of link (n0-n2) to 20
$ns queue-limit $n0 $n2 20
Agents and Applications: We need to define routing (sources, destinations) the agents
(protocols) the application that use them.
FTP over TCP: TCP is a dynamic reliable congestion control protocol. It uses
Acknowledgements created by the destination to know whether packets are well received. There
are number variants of the TCP protocol, such as Tahoe, Reno, NewReno, Vegas. The type of
agent appears in the first line:
set tcp [new Agent/TCP]
The command $ns attach-agent $n0 $tcp defines the source node of the tcp connection.
The command
set sink [new Agent /TCPSink]
Defines the behaviour of the destination node of TCP and assigns to it a pointer called sink.
#Setup a UDP connection
Program 1:
Implement three nodes point – to – point network with duplex links between them. Set
the queue size, vary the bandwidth and find the number of packets dropped.
proc finish { } { /* provide space b/w proc and finish and all are in small case */
global ns nf tf
$ns flush-trace /* clears trace file contents */
close $nf
close $tf
exec nam lab1.nam &
exit 0
}
$ns duplex-link $n0 $n2 200Mb 10ms DropTail /*Letter M is capital Mb*/
$ns duplex-link $n1 $n2 100Mb 5ms DropTail /*D and T are capital*/
$ns duplex-link $n2 $n3 1Mb 1000ms DropTail
AWK file (Open a new editor using “vi command” and write awk file and save with
“.awk” extension)
/*immediately after BEGIN should open braces „{„
BEGIN {
c=0;
}
{
If ($1= ="d")
{
c++;
printf("%s\t%s\n",$5,$11);
}
}
/*immediately after END should open braces „{„
END{
printf("The number of packets dropped =%d\n",c);
}
Program 2.
Implement transmission of ping messages/trace route over a network topology
consisting of 6 nodes and find the number of packets dropped due to congestion.
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
}
AWK file (Open a new editor using “vi command” and write awk file and save
with “.awk” extension)
BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}
Program 3
Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
local Area Network (LAN) is a data communication network connecting various
terminals or computers within a building or limited geographical area.
Ethernet is the most widely used LAN technology.
Ethernet generally uses Bus Topology.
Ethernet operates in two layers of the OSI model, Physical Layer, and Data Link Layer.
It is having 2 sublayers: Medium access (Mac) Layer: 802.3 & Logical link layer
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
/* should come in single line */
$tcp0 trace cwnd_ /* must put underscore ( _ ) after cwnd and no space between
them*/
$tcp2 trace cwnd_
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab3.nam &
exit 0
}
$ns at 16 "finish"
$ns run
AWK file (Open a new editor using “vi command” and write awk file and save with
“.awk” extension)
cwnd:- means congestion window
BEGIN {
}
{
if($6= ="cwnd_") /* don‘t leave space after writing cwnd_ */
printf("%f\t%f\t\n",$1,$7); /* you must put \n in printf */
}
END {
}