0% found this document useful (0 votes)
28 views

CN Simulation

The document discusses simulation of computer networks using the NS2 simulator. It provides an introduction to NS2, describing it as an event-driven simulation tool useful for studying wired and wireless network protocols and functions. It explains that NS2 allows specifying network protocols and simulating their behavior. It also provides information on using Tcl scripting with NS2, the basics of Tcl syntax, and components of wired Tcl scripts for NS2 including creating nodes, links, traffic, and terminating the simulation.

Uploaded by

vvce21cse0119
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

CN Simulation

The document discusses simulation of computer networks using the NS2 simulator. It provides an introduction to NS2, describing it as an event-driven simulation tool useful for studying wired and wireless network protocols and functions. It explains that NS2 allows specifying network protocols and simulating their behavior. It also provides information on using Tcl scripting with NS2, the basics of Tcl syntax, and components of wired Tcl scripts for NS2 including creating nodes, links, traffic, and terminating the simulation.

Uploaded by

vvce21cse0119
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Networks Laboratory

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} {
......} }

Chayashree G,ISE,VVCE, MysuruPage 1


Computer Networks Laboratory

Wired TCL Script Components


 Create the event scheduler
 Open new files & turn on the tracing
 Create the nodes
 Setup the links
 Configure the traffic type (e.g., TCP, UDP, etc)
 Set the time of traffic generation (e.g., CBR, FTP)
 Terminate the simulation

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.

Initialization and Termination of TCL Script in NS-2


An ns simulation starts with the command: set ns [new Simulator] Which is thus the first
line in the tcl script? This line declares a new variable as using the set command, you can call
this variable as you wish, In general people declares it as ns because it is an instance of the
Simulator class, so an object the code[new Simulator] is indeed the installation of the class
Simulator using the reserved word new.

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ǁ.

Chayashree G,ISE,VVCE, MysuruPage 2


Computer Networks Laboratory

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

#setup a CBR over UDP connection

Chayashree G,ISE,VVCE, MysuruPage 3


Computer Networks Laboratory

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.

set ns [new Simulator] /* Letter S is capital */ int a=0

set nf [open lab1.nam w] /* open a nam trace file in write mode */


$ns namtrace-all $nf /* nf – nam file */

set tf [open lab1.tr w] /* tf- trace file */


$ns trace-all $tf

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
}

set n0 [$ns node] /* creates 4 nodes */


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$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

Chayashree G,ISE,VVCE, MysuruPage 4


Computer Networks Laboratory

$ns queue-limit $n0 $n2 10


$ns queue-limit $n1 $n2 10

set udp0 [new Agent/UDP] /* Letters A,U,D and P are capital */


$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR] /* A,T,C,B and R are capital*/


$cbr0 set packetSize 500 /*S is capital, space after underscore*/
$cbr0 set interval 0.005
$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

set udp2 [new Agent/UDP]


$ns attach-agent $n2 $udp2
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2

set null0 [new Agent/Null] /* A and N are capital */


$ns attach-agent $n3 $null0

$ns connect $udp0 $null0


$ns connect $udp1 $null0

$ns at 0.1 "$cbr0 start"


$ns at 0.2 "$cbr1 start"
$ns at 1.0 "finish"
$ns run

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);
}

Chayashree G,ISE,VVCE, MysuruPage 5


Computer Networks Laboratory

}
/*immediately after END should open braces „{„
END{
printf("The number of packets dropped =%d\n",c);
}

Steps for execution


1. Open vi editor and type program. Program name should have the extension ― .tcl ǁ
[root@localhost ~]# vi lab1.tcl
2. Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
3. Open vi editor and type awk program. Program name should have the extension―.awk ǁ
[root@localhost ~]# vi lab1.awk
4. Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
5. Run the simulation program
[root@localhost~]# ns lab1.tcl
i. Here “ns” indicates network simulator. We get the topology shown in the
snapshot.
ii. Now press the play button in the simulation window and the simulation will
begins.
6. After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab1.awk lab1.tr
7. To see the trace file contents open the file as ,
[root@localhost~]# vi lab1.tr

Chayashree G,ISE,VVCE, MysuruPage 6


Computer Networks Laboratory

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.

 PING: Packet Internet Gropper


 It is a tool used to test whether a particular host is reachable across an IP network
 It measures the time it takes for packets to be sent from the local host to a destination
computer and back
 It measures and records the RRT of the packet and any issues along the way.

set ns [ new Simulator ]

set nf [ open lab2.nam w ]


$ns namtrace-all $nf
set tf [ open lab2.tr w ]
$ns trace-all $tf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$n4 shape box

$ns duplex-link $n0 $n4 1005Mb 1ms DropTail


$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p1 [new Agent/Ping]


$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001

set p2 [new Agent/Ping]


$ns attach-agent $n1 $p2

set p3 [new Agent/Ping]

Chayashree G,ISE,VVCE, MysuruPage 7


Computer Networks Laboratory

$ns attach-agent $n2 $p3

$p3 set packetSize_ 30000


$p3 set interval_ 0.00001

set p4 [new Agent/Ping]


$ns attach-agent $n3 $p4

set p5 [new Agent/Ping]


$ns attach-agent $n5 $p5

$ns queue-limit $n0 $n4 5


$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
}
# please provide space between $node_ and id. No space between $ and from. No
#space between and $ and rtt */
$ns connect $p1 $p5
$ns connect $p3 $p4

proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
}

$ns at 0.1 "$p1 send"


$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"

Chayashree G,ISE,VVCE, MysuruPage 8


Computer Networks Laboratory

$ns at 1.4 "$p1 send"


$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
$ns at 2.1 "$p1 send"
$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"

$ns at 0.1 "$p3 send"


$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"
$ns at 1.0 "$p3 send"
$ns at 1.1 "$p3 send"
$ns at 1.2 "$p3 send"
$ns at 1.3 "$p3 send"
$ns at 1.4 "$p3 send"
$ns at 1.5 "$p3 send"
$ns at 1.6 "$p3 send"
$ns at 1.7 "$p3 send"
$ns at 1.8 "$p3 send"
$ns at 1.9 "$p3 send"
$ns at 2.0 "$p3 send"
$ns at 2.1 "$p3 send"
$ns at 2.2 "$p3 send"
$ns at 2.3 "$p3 send"
$ns at 2.4 "$p3 send"
$ns at 2.5 "$p3 send"
$ns at 2.6 "$p3 send"
$ns at 2.7 "$p3 send"
$ns at 2.8 "$p3 send"
$ns at 2.9 "$p3 send"

Chayashree G,ISE,VVCE, MysuruPage 9


Computer Networks Laboratory

$ns at 3.0 "finish"


$ns run

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);
}

Steps for execution


1. Open vi editor and type program. Program name should have the extension ― .tcl ǁ
[root@localhost ~]# vi lab2.tcl
2. Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
3. Open vi editor and type awk program. Program name should have the extension ―.awk ǁ
4. [root@localhost ~]# vi lab2.awk
1) Save the program by pressing “ESC key” first, followed by “Shift and
:” keys
5. simultaneously and type “wq” and press Enter key.
2) Run the simulation program
6. [root@localhost~]# ns lab2.tcl
a. Here “ns” indicates network simulator. We get the topology shown in the
snapshot.
b. Now press the play button in the simulation window and the simulation will
begins.
7. After simulation is completed run awk file to see the output ,[root@localhost~]# awk –f
lab2.awk lab2.tr
8. To see the trace file contents open the file as ,
[root@localhost~]# vi lab2.tr

Chayashree G,ISE,VVCE, MysuruPage 10


Computer Networks Laboratory

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

set ns [new Simulator]

set tf [open lab3.tr w]


$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf

set n0 [$ns node]


$n0 color "magenta"
$n0 label "src1"

set n1 [$ns node]

set n2 [$ns node]


$n2 color "magenta"
$n2 label "src2"

set n3 [$ns node]


$n3 color "blue"
$n3 label "dest2"

set n4 [$ns node]

set n5 [$ns node]


$n5 color "blue"
$n5 label "dest1"

$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
/* should come in single line */

Chayashree G,ISE,VVCE, MysuruPage 11


Computer Networks Laboratory

$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001

set sink5 [new Agent/TCPSink]


$ns attach-agent $n5 $sink5
$ns connect $tcp0 $sink5

set tcp2 [new Agent/TCP]


$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600
$ftp2 set interval_ 0.001

set sink3 [new Agent/TCPSink]


$ns attach-agent $n3 $sink3

$ns connect $tcp2 $sink3

set file1 [open file1.tr w]


$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2

$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 0.1 "$ftp0 start"


$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"

Chayashree G,ISE,VVCE, MysuruPage 12


Computer Networks Laboratory

$ns at 0.2 "$ftp2 start"

$ns at 8 "$ftp2 stop"


$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"

$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 {
}

Steps for execution


1) Open vi editor and type program. Program name should have the extension ― .tcl ǁ
[root@localhost ~]# vi lab3.tcl
2) Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
3) Open vi editor and type awk program. Program name should have the extension
―.awk ǁ
[root@localhost ~]# vi lab3.awk
4) Save the program by pressing “ESC key” first, followed by “Shift and :” keys
simultaneously and type “wq” and press Enter key.
5) Run the simulation program
[root@localhost~]# ns lab3.tcl
6) After simulation is completed run awk file to see the output ,
i. [root@localhost~]# awk –f lab3.awk file1.tr > a1
ii. [root@localhost~]# awk –f lab3.awk file2.tr > a2
iii. [root@localhost~]# xgraph a1 a2
7) Here we are using the congestion window trace files i.e. file1.tr and file2.tr and we
are redirecting the contents of those files to new files say a1 and a2 using output
redirection operator (>).
8) To see the trace file contents open the file as ,
[root@localhost~]# vi lab3.tr

Chayashree G,ISE,VVCE, MysuruPage 13

You might also like