0% found this document useful (0 votes)
90 views20 pages

Study of Network Simulator Package-2: R.Ram Kumar 411614104018

The document provides an overview of the network simulator NS-2. It discusses that NS-2 is a discrete event simulator used for networking research. It can simulate protocols like TCP, UDP, and wireless networks. NS-2 uses C++ for performance critical code and OTcl for configuration and runtime control. The document also provides examples of creating network topologies and configuring simulations in NS-2.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views20 pages

Study of Network Simulator Package-2: R.Ram Kumar 411614104018

The document provides an overview of the network simulator NS-2. It discusses that NS-2 is a discrete event simulator used for networking research. It can simulate protocols like TCP, UDP, and wireless networks. NS-2 uses C++ for performance critical code and OTcl for configuration and runtime control. The document also provides examples of creating network topologies and configuring simulations in NS-2.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

R.

Ram Kumar
411614104018

STUDY OF NETWORK SIMULATOR PACKAGE-2

AIM:
To make a descriptive study of the network simulator Ns-2.
OVERVIEW:
Ns-2 stands for Network Simulator version 2.Network simulator is a
discrete event simulator for networking research and work at packet level. It also
provides substantial support to simulate bunch of protocols like TCP, UDP, FTP,
HTTP and DSR. It can also able to simulate wired and wireless network.
It is primarily UNIX based and uses TCL as its scripting language.
Ns-2 is a standard experiment environment in research community. It uses
two languages Object-oriented support with otcl and C++ with otcl linkage. It is a
discrete event scheduler. An event in NS is a packet ID that is unique for a packet
with scheduled time and the pointer to an object that handles the event.
In NS an events in the event queue scheduled for the current time invoked by
appropriate network components, which usually are the ones who issued the
events, and let them do the appropriate action with packet pointed by the event.
C++ is protocol simulations require systems programming language
byte manipulation, packet processing, and algorithm implementation Run time
speed is important. Turnaround time (run simulation, find bug, fix bug, recompile,
re-run) is slower. Tcl: Simulation of slightly varying parameters or configurations
quickly exploring a number of scenarios iteration time (change the model and re
-run) is more important.

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

Architectural view of NS

C++ and OTcl: The Duality

NS is written not only in OTcl but in C++ also. For efficiency reasoning
separates the data path implementation from control path implementations. In order
to reduce packet and event scheduler and the basic network component object in
the data path are written and compiled using C++.these compiled objects are made
available to the OTcl interpreter through an OTcl linkage that creates a matching
OTcl objects for each of the C++ objects and make control function. The object in
C++ that do not be controlled by simulation. An object can be entire;y
implemented in OTcl.

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

Basic operation in NS-2

Create a new simulator, [turn on tracing]

Create network (physical layer)

Create link and queue (data link layer)

Define routing protocol

Create transport connection (transport layer)

Create traffic (application layer)

Creating simulator instance (set ns [new simulator])

Usually the first non comment statement in Ns-2 script is to initialize the packet
format and create a scheduler (defaults a calendar scheduler). Turning on a trace
file by opening Own trace, nam trace, ns trace files.

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

#Open the NAM trace file


set nf [open out.nam w]
$ns namtrace-all $nf
#Open the NS trace file
set ns[new simulator]
set nf[open out.nam w]

#Open your own trace file

Set my_nf[open my_out.tr w]

Creating a network

To create a network between the collections of nodes and to transfer the


content from one node to another if creates a network.

set n0 [$ns node]


set n1 [$ns node]
Creating a link, queue
To create a link between two nodes based on transmission mode and the types
of connection. The data rate for transmission can also identified.

#create links between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
Simple simulation of nodes with types of connection

#Create a simulator object


set ns [new Simulator]
#Define different colors for data flows (for NAM)

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

$ns color 1 Blue


$ns color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

$ns queue-limit $n2 $n3 10


#Give node position (for NAM)
$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
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connectionRun
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

$udp set fid_ 2


#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 500
$cbr set rate_ 0.5mb
$cbr set random_ false
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
USES OF NETWORK SIMULATOR:

Network simulator serves a variety of needs. Compared to cost and time


involved in setting up an entire bed containing multiple networked computers,
routers and data links, network simulators are relatively fat and expensive. They
allow engineers to test scenarios that might be particularly difficult or expensive to

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

emulate using real hardware for instance, simulation effect of a sudden burst in
traffic or a DOS attack on a network service.

As the name suggested as Network Simulator are used by researcher to


design various kind of networks, simulate and analyze the effect of various
parameters on the network performance.

NS-2
Ns-2 is a discrete event simulator targeted at networking research. Ns provides
substantial
support for simulation of TCP, routing, and multicast protocols over wired and
wireless (local and satellite) networks.

NS-2 ARCHITECTURE
Downloading/Installing ns&nam
One can build ns either from the various packages (Tcl/Tk, otcl, etc.), or can
download an 'all-in-one'package. web page: http://www.isi.edu/nsnam/ns/ns-
build.html

Starting ns
Start ns with the command 'ns <tclscript>', where '<tclscript>' is the name of a Tcl
script file
which defines the simulation scenario (i.e. the topology and the events). Just start
ns without any arguments and enter the Tcl commands in the Tcl shell, but that is

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

definitely less comfortable.Everything else depends on the Tcl script. The script
might create some output on stdout, it might write a trace file or it might start nam
to visualize the simulation.
Starting nam
One can either start nam with the command 'nam <nam-file>' where '<nam-file>' is
the name of a nam trace file that was generated by ns, or one can execute it directly
out of the Tcl simulation script for the simulation which you want to visualize.

First TCL Script


Now we are going to write a 'template' that you can use for all of the first Tcl
scripts. You can
write your Tcl scripts in any text editor like joe or emacs. I suggest that you call
this first example
'example1.tcl'.
First of all, you need to create a simulator object. This is done with the command
set ns [new Simulator]
Now we open a file for writing that is going to be used for the nam trace data.
set nf [open out.nam w]
$ns namtrace-all $nf

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In
the second line we tell the simulator object that we created above to write all
simulation data that is going to be relevant for nam into this file.
The next step is to add a 'finish' procedure that closes the trace file and starts nam.
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
You don't really have to understand all of the above code yet. It will get clearer
to you once you see what the code does.The next line tells the simulator object
to execute the 'finish' procedure after 5.0 seconds of simulation time.
$ns at 5.0 "finish"
You probably understand what this line does just by looking at it. ns provides you
with a very simple way to schedule events with the 'at' command.
The last line finally starts the simulation.
$ns run
Ns-2 MAIN CONSOLE WITH NETWORK ANIMATOR

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

PROGRAM:

FTPTCP LS.tcl

set ns [new Simulator]

$ns color 1 Blue

$ns color 2 Red

set nf [open out.nam w ]

$ns namtrace-all $nf

set tf [open out.tr w]

$ns trace-all $tf

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

$ns rtproto LS

set n0 [ $ns node ]

set n1 [ $ns node ]

set n2 [ $ns node ]

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

set n3 [ $ns node ]

set n4 [ $ns node ]

set n5 [ $ns node ]

$ns duplex-link $n0 $n1 0.3MB 10ms DropTail

$ns duplex-link $n1 $n2 0.3MB 10ms DropTail

$ns duplex-link $n2 $n3 0.3MB 100ms DropTail

$ns duplex-link $n1 $n4 0.3MB 10ms DropTail

$ns duplex-link $n3 $n5 0.5MB 10ms DropTail

$ns duplex-link $n4 $n5 0.5MB 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right

$ns duplex-link-op $n1 $n2 orient right

$ns duplex-link-op $n2 $n3 orient up

$ns simplex-link-op $n1 $n4 orient up-left

$ns duplex-link-op $n3 $n5 orient left-up

$ns duplex-link-op $n4 $n5 orient right-up

set tcp [ new Agent/TCP/Newreno ]

$ns attach-agent $n0 $tcp

set sink [ new Agent/TCPSink/DelAck ]

$ns attach-agent $n5 $sink

$ns connect $tcp $sink

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

$tcp set fid_ 2

set ftp [ new Application/FTP ]

$ftp attach-agent $tcp

$ftp set type_ FTP

$ns rtmodel-at 1.0 down $n1 $n4

$ns rtmodel-at 4.5 up $n1 $n4

$ns at 0.1 "$ftp start"

$ns at 6.0 "finish"

$ns run

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

OUTPUT:

administrator@ubuntu:~$ set ns[new simulator]

administrator@ubuntu:~$ set nf[open out.nam w]

administrator@ubuntu:~$ ns FTPTCPLS.tcl

administrator@ubuntu:~$

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

PROGRAM:

FTPTFPDV.tcl

set ns [new Simulator]

$ns color 1 Blue

$ns color 2 Red

set nf [open out1.nam w ]

$ns namtrace-all $nf

set tf [open out1.tr w]

$ns trace-all $tf

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

$ns rtproto DV

set n0 [ $ns node ]

set n1 [ $ns node ]

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

set n2 [ $ns node ]

set n3 [ $ns node ]

set n4 [ $ns node ]

set n5 [ $ns node ]

$ns duplex-link $n0 $n1 0.3MB 10ms DropTail

$ns duplex-link $n1 $n2 0.3MB 10ms DropTail

$ns duplex-link $n2 $n3 0.3MB 100ms DropTail

$ns duplex-link $n1 $n4 0.3MB 10ms DropTail

$ns duplex-link $n3 $n5 0.5MB 10ms DropTail

$ns duplex-link $n4 $n5 0.5MB 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right

$ns duplex-link-op $n1 $n2 orient right

$ns duplex-link-op $n2 $n3 orient up

$ns simplex-link-op $n1 $n4 orient up-left

$ns duplex-link-op $n3 $n5 orient left-up

$ns duplex-link-op $n4 $n5 orient right-up

set tcp [ new Agent/TCP/Newreno ]

$ns attach-agent $n0 $tcp

set sink [ new Agent/TCPSink ]

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

$ns attach-agent $n5 $sink

$ns connect $tcp $sink

$tcp set fid_ 2

set ftp [ new Application/FTP ]

$ftp attach-agent $tcp

$ftp set type_ FTP

$ns rtmodel-at 1.0 down $n1 $n4

$ns rtmodel-at 4.5 up $n1 $n4

$ns at 0.1 "$ftp start"

$ns at 6.0 "finish"

$ns run

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

OUTPUT:

administrator@ubuntu:~$ set ns[new simulator]

administrator@ubuntu:~$ set nf[open out.nam w]

administrator@ubuntu:~$ ns FTPTCPDV.tcl

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

administrator@ubuntu:~$ cat out.tr

administrator@ubuntu:~$ raw2xg a TCPLSout.tr > TCPLSout.xg

administrator@ubuntu:~$ raw2xg a TCPDVout1.tr > TCPDVout1.xg

administrator@ubuntu:~$ xgraph t Performance of TCP TCPLSout.xg


TCPDVout1.xg geometry 800*400

CS6411-NETWORKS LABORATORY
R.Ram Kumar
411614104018

CS6411-NETWORKS LABORATORY

You might also like