ns2 Basics Manual1
ns2 Basics Manual1
Time Required
Around 3.00 hours
Learn the basic idea about open source network simulator NS2 and how to download, install
and work with NS2 using TCL programming.
Defining the different agents and their applications like TCP, FTP over TCP, UDP, CBR
and CBR over UDP etc.
Identifying and solving the installation error of NS2.
Introduction
The network simulator is discrete event packet level simulator.The network simulator covers a
very large number of application of different kind of protocols of different network types
consisting of different network elements and traffic models.Network simulator is a package of
tools that simulates behavior of networks such as creating network topologies, log events that
happen under any load,analyze the events and understand the network. Well the main aim of our
first experiment is to learn how to use network simulator and to get acquainted with the
simulated objects and understand the operations of network simulation and we also need to
analyze the behavior of the simulation object using network simulation.
Backend Environment of Network Simulator
Network Simulator is mainly based on two languages.They are C++ and OTcl. OTcl is the
object oriented version of Tool Command language.The network simulator is a bank of of
different network and protocol objects. C++ helps in the following way:
Initialization
From the above command we get that a variable ns is being initialized by using the set command.
Here the code [new Simulator] is a instantiation of the class Simulator which uses the reserved
word 'new'. So we can call all the methods present inside the class simulator by using the
variable ns.
In the above we create a output trace file out.tr and a nam visualization file out.nam. But in the
Tcl script they are not called by their names declared,while they are called by the pointers
initialized for them such as tracefile1 and namfile1 respectively.The line which starts with '#' are
commented.The next line opens the file 'out.tr' which is used for writing is declared 'w'.The next
line uses a simulator method trace-all by which we will trace all the events in a particular format.
In the above the word 'proc' is used to declare a procedure called 'finish'.The word 'global' is
used to tell what variables are being used outside the procedure.
'flush-trace' is a simulator method that dumps the traces on the respective files.the command
'close' is used to close the trace files and the command 'exec' is used to execute the nam
visualization.The command 'exit' closes the application and returns 0 as zero(0) is default for
clean exit.
Thus the entire operation ends at 125 seconds.To begin the simulation we will use the command
1 #start the the simulation process
2 $ns run
Defining nodes,links,queues and topology
In the above we created a node that is pointed by a variable n0.While referring the node in the
script we use $n0. Similarly we create another node n2.Now we will set a link between the two
nodes.
So we are creating a bi-directional link between n0 and n2 with a capacity of 10Mb/sec and a
propagation delay of 10ms.
In NS an output queue of a node is implemented as a part of a link whose input is that node to
handle the overflow at the queue.But if the buffer capacity of the output queue is exceeded then
the last packet arrived is dropped and here we will use a 'DropTail' option.Many other options
such as RED(Random Early Discard) mechanism, FQ(Fair Queuing), DRR(Deficit Round
Robin), SFQ(Stochastic Fair Queuing) are available.
So now we will define the buffer capacity of the queue related to the above link
TCP is a dynamic reliable congestion protocol which is used to provide reliable transport of
packets from one host to another host by sending acknowledgements on proper transfer or loss of
packets.Thus TCP requires bi-directional links in order for acknowledgements to return to the
source.
Now we will show how to set up tcp connection between two nodes
The command 'set tcp [new Agent/TCP]' gives a pointer called 'tcp' which indicates the tcp
agent which is a object of ns.Then the command '$ns attach-agent $n0 $tcp' defines the source
node of tcp connection. Next the command 'set sink [new Agent/TCPSink]' defines the
destination of tcp by a pointer called sink. The next command '$ns attach-agent $n4 $sink'
defines the destination node as n4.Next, the command '$ns connect $tcp $sink' makes the TCP
connection between the source and the destination.i.e n0 and n4.When we have several flows
such as TCP, UDP etc in a network. So, to identify these flows we mark these flows by using the
command '$tcp set fid_1'. In the last line we set the packet size of tcp as 552 while the default
packet size of tcp is 1000.
File Transfer Protocol(FTP) is a standard mechanism provided by the Internet for transferring
files from one host to another. Well this is the most common task expected from a networking or
a inter networking . FTP differs from other client server applications in that it establishes
between the client and the server. One connection is used for data transfer and other one is used
for providing control information. FTP uses the services of the TCP. It needs two connections.
The well Known port 21 is used for control connections and the other port 20 is used for data
transfer.
Well here we will learn in how to run a FTP connection over a TCP
In above,the command 'set ftp [new Application/FTP]' gives a pointer called 'ftp' which
indicates the FTP application.Next, we attach the ftp application with tcp agent as FTP uses the
services of TCP.
UDP
The User datagram Protocol is one of the main protocols of the Internet protocol suite.UDP
helps the host to send send messages in the form of datagrams to another host which is present in
a Internet protocol network without any kind of requirement for channel transmission setup.
UDP provides a unreliable service and the datagrams may arrive out of order,appear duplicated,
or go missing without notice. UDP assumes that error checking and correction is either not
necessary or performed in the application, avoiding the overhead of such processing at the
network interface level. Time-sensitive applications often use UDP because dropping packets is
preferable to waiting for delayed packets, which may not be an option in a real-time system.
Similarly,the command 'set udp [new Agent/UDP]' gives a pointer called 'udp' which indicates
the udp agent which is a object of ns.Then the command '$ns attach-agent $n1 $udp' defines the
source node of udp connection. Next the command 'set null [new Agent/Null]' defines the
destination of udp by a pointer called null. The next command '$ns attach-agent $n5 $null'
defines the destination node as n5.Next, the command '$ns connect $udp $null' makes the UDP
connection between the source and the destination.i.e n1 and n5.When we have several flows
such as TCP,UDP etc in a network. So, to identify these flows we mark these flows by using the
command '$udp set fid_2
In the above we define a CBR connection over a UDP one. Well we have already defined the
UDP source and UDP agent as same as TCP. Instead of defining the rate we define the time
interval between the transmission of packets in the command '$cbr set rate_0.01Mb'. Next, with
the help of the command '$cbr set random _false' we can set random noise in cbr traffic.we can
keep the noise by setting it to 'false' or we can set the noise on by the command '$cbr set random
_1'. We can set by packet size by using the command '$cbr set packetSize_(packetsize).We can
set the packet size up to sum value in bytes.
Scheduling Events
In ns the tcl script defines how to schedule the events or in other words at what time which event
will occur and stop. This can be done using the command
$ns at .
2
When we will run the above program in ns then we can can visualize the network in the NAM.
But instead of giving random positions to the nodes, we can give suitable initial positions to the
nodes and can form a suitable topology. So, in our program we can give positions to the nodes in
NAM in the following way
We can also define the color of cbr and tcp packets for identification in NAM.For this we use
the following command
To view the network animator we need to type the command: nam
Tracing
Tracing Objects
NS simulation can produce visualization trace as well as ASCII file corresponding to the events
that are registered at the network. While tracing ns inserts four objects: EnqT,DeqT,RecvT &
DrpT. EnqT registers information regarding the arrival of packet and is queued at the input queue
of the link. When overflow of a packet occurs, then the information of thye dropped packet is
registered in DrpT.DeqT holds the information abut the packet that is dequeued instantly.RecvT
hold the information about the packet that has been received instantly.
cat out.tr
Steps for conducting the experiment
General Instructions
Follow are the steps to be followed in general to perform the experiments in Advanced Network
Technologies Virtual Lab.
3. Take the self evaluation to judge your understanding (optional, but recommended)
In the theory part we have learned how to work with NS2. In this section we now learn how to
make your system ready so that you can work with Network Simulator 2.NS2 is a open source
software. It can be downloaded from Internet and installed.
Basic Requirements:
ns-2.34 package
gcc-4.4.3
make tools
The following instructions for downloading and installing ns2 are for a system with:
gcc: 4.4.3
Some of the known problems that have been faced during installation as well as their solutions
have been discussed in one of the sections below.
The steps for installation should ideally be applicable for other version and/or configuration of
Linux also. Any other problem that might arise would require further troubleshooting.
Downloading ns-2.34
1. First go to http://www.isi.edu/nsnam/ns/ns-build.html.
2. Then download Tcl and Tk from http://www.tcl.tk/software/tcltk/downloadnow84.tml [note:
the versions of Tcl and Tk must be same.]
Note: there are some other few things to be downloaded but that depends upon your
requirement.For example, if some error occurs for absence of any package,then you need to
detect the error and download the required package.
Installation
Here we will install the the packages separately
All the files will be zip format.So at first you need to unzip all the files. the command to unzip
the files:
for e.g if we want to unzip the Tcl package the type: tar -xzvf tcl8.4.19
cd tcl8.4.19
ls
cd unix
./configure
make
sudo make install
2. Install Tk:
cd tk8.4.19
ls
cd unix
./configure
make
sudo make install
3. Install OTcl:
cd otcl-1.13
/configure --with-tcl=../tcl8.4.19 #note-while configuring we need to specify the path of
tcl
make
sudo make install
4. Install Tclcl-1.19:
cd tclcl-1.19
./configure --with-tcl=../tcl8.4.19 #note-while configuring we need to specify the path of
tcl
make
sudo make install
5. Install ns-2.34:
cd ns-2.34
/configure --with-tcl=../tcl8.4.19
make
sudo make install
6. Install NAM:
cd nam-1.14
./configure --with-tcl=../tcl8.4.19
make
sudo make install
7. Install xgraph:
cd xgraph-12.1
./configure
make
sudo make install
Probable problems that could appear while installing the packages and their solution
After installing tk8.4.19 try to run the script tk8.4.19/unix/wish from the terminal.A small
window will open,close it. If no error messages appears in the terminal then Tk installation is
successful and Tk is working properly. This can also be verified after installing nam and then
trying to run nam. The error message would be something like:
nam:
[code omitted because of length]
: no event type or button # or keysym
while executing
"bind Listbox <MouseWheel> {
%W yview scroll [expr {- (%D / 120) * 4}] units
}"
invoked from within
"if {[tk windowingsystem] eq "classic" || [tk windowingsystem] eq "aqua"} {
bind Listbox <MouseWheel> {
%W yview scroll [expr {- (%D)}] units
}
bind Li..."
Solution:
If you get error messages then download the patch files tk-8.4.18-tkBind.patch and tk-8.4-
lastevent.patch from http://bugs.gentoo.org/show_bug.cgi?id=225999.
then copy those files into Tk directory.Now apply the patches by using the following command:
If fail to apply the patches then open the patch, check the name of the file to be patched, and
make the relevant modifications to that file accordingly.
Note: the contents of the original file are shown with a minus(-) sign at the beginning.The
modified contents do begin with a plus(+) sign. The contents of the two patch files are shown
below for easy reference:
--- tk8.4.18-orig/generic/tkBind.c 2006-07-21 08:26:54.000000000 +0200
+++ tk8.4.18/generic/tkBind.c 2008-07-05 12:17:10.000000000 +0200
@@ -586,6 +586,9 @@
/* ColormapNotify */ COLORMAP,
/* ClientMessage */ 0,
/* MappingNotify */ 0,
+#ifdef GenericEvent
+ /* GenericEvent */ 0,
+#endif
/* VirtualEvent */ VIRTUAL,
/* Activate */ ACTIVATE,
/* Deactivate */ ACTIVATE,
And
/*After doing all these stuffs, do install Tk from beginning again and also verify whether 'wish'
runs properly(as indicated above).
Solution:
SHLIB_LD="ld -shared"
SHLIB_LD="gcc -shared"
3. Problem wile running ' sudo make install' for ns-2.34
ns: error while loading shared libraries: libotcl.so: cannot open shared object file: No such file or
directory
Solution:
We need to set the following environment variables, and store them in the ~/.bashrc file.
1 OTCL_LIB=/your/path/ns-2.34/otcl-1.13
2 NS2_LIB=/your/path/ns-2.34/lib
3 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB
Now open a new terminal, and type ns. This should now work without any error.
xwd.c:87:29: error: X11/Xmu/WinUtil.h: No such file or directory make: *** [xwd.o] Error 1
Solution:
./configure
make clean
make
sudo make install
Solution:
http://archive.ubuntu.com/ubuntu/pool/universe/x/xgraph/xgraph_12.1-12.diff.gz
Note: copy and unzip the above patch file into xgraph-12.1
view source
print?
1 patch < xgraph_12.1-12.diff
After applying the patch if you see in get any problem with the configure.in file like
then goto configure.in file and add 'AC_LOCAL' in the first line.
QUESTIONS
o Packet size
o Packet id
o $ns nam
5. Sink can
o only recieve packets
o send packets
o 552
o 785