Tuesday, October 12, 2010

Theorize the Network Simulator (ns-2 tutorial)


Experiments using Network Simulator(NS2)
Introduction:
Network Simulator 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.
Desing: ns is an object oriented simulator, written in C++, with an OTcl(MIT [Massachusetts Institute of Technology in Cambrige] Object Tcl) interpreter as a frontend.
History: Modified from REAL network simulator and then Developed through VINT project at UCB and then the NS1 and  NS2 came into existence.
Main Class: Simulator class
Tool Command Language(Tcl):
Need: The Network Simulator uses Tcl scripts to simulate the communication among the network by using set of protocols(which said to be as set of rules).
History: The Tcl programming language was created in the spring of 1988 by John Ousterhout while working at the University of California, Berkeley.
Commands
Equivalents
set
= (equal to)
$variable1
Shows the value of the variable1
           
Explanation by sequence from the Start:
            Don’t forget that the Tcl Scripts are Object Oriented, so each and every line script is either setting values of the variable or calling methods using objects. The object variables are declared and defined using set method. These Object variables can used to call a method of the class it referes.
Output:This network simulator generates two output one is .nam file (for animated simulation of the scenario) and .tr – trace file: for analyzation purpose.
Structure of the Program:
·        Initializing Simulation
·        Opening Nam file
·        Generating Trace File (optional)
·        Finish Procedure
·        Creation of nodes
·        Generating link between Nodes
·        Sending data connections
·        Receiving data connections
·        Establishing Connection between two Nodes
·        Setting the Routing Protocol
·        Scheduling Events
·        Starting the Execution


Initializing Simulation:
1..set ns [new Simulator] :
    §  This Statement 1st creates a object for the Simulator class.
    §  This Object’s address is assigned to the ns object variable using set method in Tcl.


Opening Nam file:
  Need: The NAM(Network AniMator) file consists of data need to simulate the scenario described by the tcl script. These data are generated by using method namtrace-all method available in Simulator class.
   2..set nf [open out.nam w]                                                                                                                                                                          3..$ns namtrace-all $nf :
   §  The 2nd line creates a out.nam file and opens in write mode whose pointer (pointing to the file out.nam) is given ‘nf’ variable.
   §  This pointer addr. is sent as parameter to the method namtrace-all available in Simulator class.
   §  Use of namtrace-all method: this generates information need to simulate the animator NAM window.

Generating Trace File:
   need: This generates information need for analysis purpose by providing various information like below,
Tracing File Contents:
Each lines consists of:
Event Descriptor (+, -, d, r)
Simulation time (in seconds) of that event
From Node & To Node, which identify the link on which the event occurred
Packet type
Packet size
Flags (appeared as "------" since no flag is set). Currently, NS implements only
the Explicit Congestion Notification (ECN) bit, and the remaining bits are not
used.
Flow id (fid)
Source and destination address in forms of "node.port".
The network layer protocol's packet sequence number. What about UDP?
The last field shows the unique id of the packet.
How to manipulate it: These files can be used to display various information from the above list using a GREP command.
 4..set nt [open trace.tr w]                                                                                                                                                                    5..$ns trace-all $nt :
    §  The 4th line creates a trace.tr file and opens in write mode whose pointer (pointing to the file trace.tr) is given ‘nt’ variable.
    §  This pointer addr. is sent as parameter to the method namtrace-all available in Simulator class.
    §  Use of trace-all method: this generates information need for analysization. Various data in that file such as No. Of Packet sent, No. Of Packet Received, No. Of Packet is dropped, etc..

Finish Procedure:
  need: This is user defined procedure which contains code to flush the trace buffer and start the nam file for viewing purpose. This procedure is can be used for calling it in a particular time using timer.

 6 ..proc finish {} {
 7 ..global ns nf
 8 ..$ns flush-trace
 9 ..close $nf
 10..exec nam out.nam &
 11..exit 0
 12..}
   §  global keyword used to indicate that the object at outside is needed inside.
   §  flush-trace method used to flush the namtrace buffer used for writing data into the file.
   §  close keyword used to close the nam file.
   §  exec keyword used to execute the application available. Eg: As like above exec nam out.nam   & exit 0- executes the nam application which shows the animation using nam file out.nam and then exits the program execution by normally exit 0.

Creation of nodes:
  node: method availabe in Simulator class used to creates a node and intializes various parameters and returns a address.
 13..set n0 [$ns node]
 14..set n1 [$ns node]:
     §  [$ns node] method creates a node and returns pointer to that node.
     §  set n0 keyword sets the value of the n0 to be returned address.

Generating link between Nodes:
  need: the links are established to make the communication between the nodes. It can be either a duplex or simplex link. Simplex are one-way transmission and duplex are two way transmission.
 15..$ns duplex-link $n0 $n1 1Mb 10ms DropTail:
    §  duplex-link method needs 5 parameters for creating link between nodes.
    §  1. $no- one end node(source).
    §  2. $n1- other end node(destination).
    §  3. 1Mb or 1Kb- speed of the transfer on that link.
    §  4. 10ms – time need for a packet travel from n0 to n1 nodes(distance of nodes).
    §  5. DropTail – Queue Type. Other type SFQ(stochastic fair queueing) queue.
Sending data:
 how:We know that all data’s are exchanged through a port no. which was maintained by the Transport layer. The Transport layer either be a TCP or UDP protocol. We can attach these protocols to the nodes using the attach-agent method.
 What is Traffic Source: It is nothing but application which needs data transfer between another application which was executing in another node in the network. Various Traffic Source such as Telnet, FTP(File Transfer Protocol), CBR(Contant Bit Rate).
Attaching node with a Transport layer agent:
 16..set udp0 [new Agent/UDP]
 17..$ns attach-agent $n0 $udp0
 
    §  [new Agent/UDP] creates a pointer to the UDP class containing various parameters about the UDP type.
    §  attach-agent method needs 2 parameter – attaches transport layer agent with the node.
    §  1. $n0 – node which is need the Transport layer agent.
    §  2. $udp0 – pointer to the UDP object.

Creating application and attaching to a Transport layer agent(TCP/UDP):
  18..set cbr0 [new Application/Traffic/CBR]
  19..$cbr0 set packetSize_ 500
  20..$cbr0 set interval_ 0.005
  21..$cbr0 attach-agent $udp0
   §  [new Application/Traffic/CBR] creates new CBR object and assigns to the cbr0 variable.
    §  $cbr0 set packetSize_ 500 here packetSize_ is a field of the class CBR. Indicates the size of the packet to be sent from the node.
   §  $cbr0 set interval_ 0.005 here interval_ is also a field of the CBR object. Indicates the interval between two packets sent. There other fields available i wish you to analyze.
   §  $cbr0 attach-agent $udp0 here CBR contains method attach-agent to attach a Transport port layer agent to the application cbr0 object.

Receiving data:
  how: We all know that that, every transmission of data packet will end at the destination called receiver. There are various agents to receive the data they are, Null and Sink agent. Null used when UDP is used in sender side and Sink agent is used when TCP is used in sender side.
Attaching Receiver agent to the receiving node:
 22.. set null0 [new Agent/Null]
 23..$ns attach-agent $n1 $null0
    §  We can perform this action using Null class object to the null0.
    §  Then attach receiver agent null0 to the receiver node $n1 using attach-agent method.

Establising Connection between two Node:
 Why connection needed apart from linking: Even though the two Nodes physically linked they need some handshaking information between those since each node can’t contain all the information about the network in which the node is connected. So those nodes which are need to connect makes some handshaking sequence which exchanges information about the nodes buffer rate, processing speed, Data transfer speed from sender and receiving rate, etc.
 24..$ns connect $udp0 $null0
    §  connect: Method available in the Simulator class.
   §  $ns connect $udp0 $null0 makes Virtual connection between application UDP and NULL agents. 

    Setting the Routing Protocol:
 Why it is needed: This kind of protocol used for to determine easiest path to the destination. 
Various Routing Protocol DV(The dynamic routing protocol, developped in ns2, is centralized,
and the DV is distributed).
Setting Routing Protocol:
 25..$ns rtproto DV
    §  rtproto (routing protocol) is a method of Simulator class invoked with parameter DV type.
Making a scenario to fial the link and recovering it at paticular sec:
26..$ns rtmodel-at 2.0 down $n0 $n1
27..$ns rtmodel-at 3.0 up $n0 $n1
     §  rtmodel-at method used to make some link to fail and function according to the time.
     §  $ns rtmodel-at 2.0 down $n0 $n1—shows the link n0 to n1 is going to fail between n0 and n1 at time 2.0 sec.
     §  $ns rtmodel-at 3.0 up $n0 $n1—shows the link n0 to n1 is going refunction back to normal and transmission takes place.

Scheduling Events:
 Why it is needed: Since all packet transfers are done within a time. So we consider time a heavy part of the simulation which provides flexibility to schedule the events which are need to occur on a particular time. Currently we use Calendar Queue scheduler for scheduling the events. Other schedulers List Scheduler, Heap Scheduler, Real Time scheduler.
   §  Here at is a method of Simulator receving two parameters – specially used for scheduling purposes.
   §  1. Time, 2. Procedure to be executed.
 Starting the application to communicate:
 28..$ns at 0.5 “$cbr0 start”
 29..$ns at 4.5 “$cbr0 stop”
    §   start is the method of CBR class used start the transfer data packets.
    §  stop is the method of CBR class used end the transfer of data packets.
    §  cbr is scheduled to send packets at 0.5 sec. and stop sending the packet at 4.5

 30..$ns at 5.0 "finish"
    §  finish procedure which is defined by us is available within this document on the 6th line code is scheduled to be called(invoket) at time 5.0 sec.

Starting the Execution:
  Why it is needed: Upto now we just invoked some methods and set some parameters. So upto now the simulation is not yet started. So, the Simulation of our scenario will be started using the method run in Simulator.
 31..$ns run
   §  Here ns invokes run procedure to execute the simulation.

  Summarize: The numbered code is the connection between two node transferring packets at 0.005 sec. 
Sample code for you:

#Create a simulator object
set ns [new Simulator]

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

#Open the tr trace file
set tf [open out.tr w]
$ns trace-all $tf

#Define a 'finish' procedure
proc finish {} {
global ns nf
            $ns flush-trace
           
#Close the trace file
            close $nf
           
            #Execute nam on the trace file
            exec nam out.nam &
            exit 0
}

#Create six nodes

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n4 1Mb 50ms DropTail
$ns duplex-link $n1 $n4 1Mb 50ms DropTail
$ns duplex-link $n2 $n5 1Mb 1ms DropTail
$ns duplex-link $n3 $n5 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 50ms DropTail

#Create a TCP agent and attach it to node n0 as source
set source1 [new Agent/TCP]
$ns attach-agent $n0 $source1

#create a destination and attach it to the node n2
set dest [new Agent/TCPSink]
$ns attach-agent $n2 $dest

#Connect the source and the destination
$ns connect $source1 $dest

# Create a CBR traffic source and attach it to n0
set appl [new Application/FTP]
$appl attach-agent $source1

#Schedule events for the CBR agents
$ns at 0.0 "$appl start"
$ns at 4.5 "$appl stop"

#Call the finish procedure to finish the execution of the program
$ns at 5.0  "finish"

#Run the simulation
$ns run
           

No comments:

Post a Comment