Ns2 lab final

  • View

  • Download

Embed Size (px)



Text of Ns2 lab final

  • 1. Lab -10 Understanding Network Simulator 2Introduction :Network Simulator (Version 2), widely known as NS2, is simply an event-driven simulation tool thathas proved useful in studying the dynamic nature of communication networks. Simulation of wiredas well as wireless network functions and protocols (e.g., routing algorithms, TCP, UDP) can bedone using NS2. In general, NS2 provides users with a way of specifying such network protocolsand simulating their corresponding behaviours .Basic Architecture :Figure shows the basic architecture of NS2. NS2 provides users with an executable command nswhich takes one input argument, the name of a Tcl simulation scripting file . In most cases, asimulation trace file is created and is used to plot graph and/or to create animation.NS2 consists of two key languages: C++ and Object-oriented Tool Command Language (OTcl).While the C++ denes the internal mechanism (i.e., a backend) of the simulation, the OTcl sets upsimulation by assembling and configuring the objects as well as scheduling discrete events (i.e., afrontend). The C++ and the OTcl are linked together using TclCL. Mapped to a C++ object, variablesin the OTcl domains are sometimes referred to as handles. Conceptually, a handle is just a string(e.g., _o10) in the OTcl domain and does not contain any functionality. Instead, the functionality(e.g., receiving a packet) is defined in the mapped C++ object (e.g., of class Connector). In the OTcldomain, a handle acts as a frontend which interacts with users and other OTcl objects. It maydene its own procedures and variables to facilitate the interaction. Note that the memberprocedures and variables in the OTcl domain are called instance procedures (instprocs) andinstance variables (instvars), respectively. NS2 provides a largenumber of built-in C++ classes. It is advisable to use these C++ classes to set up a simulation via aTcl simulation script. However, advance users may nd these objects insufcient. They need todevelop their own C++ classes and use a OTcl configuration interface to put together objectsinstantiated from these class.
  • 2. Directory StructureSuppose that NS2 is installed in Directory nsallinone-2.35. Figure shows the directory structureunder directory nsallinone-2.35. Here, the directory nsallinone-2.35is on the Level 1. On the Level2, the directory tclcl-1.20 contains classes in TclCL (e.g., Tcl, TclObject, TclClass). All NS2 simulationmodules are in the directory ns-2.35 on the Level 2. Hereafter, we will refer to directories ns-2.35and tclcl-1.20 as ns/ and tclcl/, respectively.On Level 3, the modules in the interpreted hierarchy are under the directory tcl. Among thesemodules, the frequently used ones (e.g., ns-lib.tcl, ns-node.tcl, ns-link.tcl) are stored under thedirectory lib on Level 4. Simulation modules in the compiled hierarchy are classied in directorieson Level 3. For example, directory tools contain various helper classes such as random variablegenerators. Directory common contains basic modules related to packet forwarding such as thesimulator, the scheduler, connector, packet. Directories queue, tcp, and trace contain modules forqueue, TCP (Transmission Control Protocol), and tracing, respectively.
  • 3. Running NS2 Simulation :A Simulation ExampleNote : this example is taken from this linkhttp://nile.wpi.edu/NS/ 1.) Copy this file to myfirstTcl.tcl#Create a simulator objectset ns [new Simulator]#Define different colors for data flows (for NAM)$ns color 1 Blue$ns color 2 Red#Open the NAM trace fileset nf [open out.nam w]$ns namtrace-all $nf#Define a finish procedureproc 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 nodesset 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$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
  • 4. $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 connectionset tcp [new Agent/TCP]$tcp set class_ 2$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sink$tcp set fid_ 1#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#Setup a UDP connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb$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"#Detach tcp and sink agents (not really necessary)$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"#Print CBR packet size and interval
  • 5. puts "CBR packet size = [$cbr set packet_size_]"puts "CBR interval = [$cbr set interval_]"#Run the simulation$ns run 2.) Run the above simulation by typing #ns myfirstTcl.tcl 3.) Nam Emulator will appear .set ns [new Simulator]: generates an NS simulator object instance, and assigns it tovariable ns (italics is used for variables and values in this section). What this line does is thefollowing: o Initialize the packet format (ignore this for now) o Create a scheduler (default is calendar scheduler) o Select the default address format (ignore this for now) The "Simulator" object has member functions that do the following:
  • 6. o Create compound objects such as nodes and links (described later)o Connect network component objects created (ex. attach-agent)o Set network component parameters (mostly for compound objects)o Create connections between agents (ex. make connection between a "tcp" and "sink")o Specify NAM display optionso Etc. $ns color fid color: is to set color of the packets for a flow specified by the flow id (fid).This member function of "Simulator" object is for the NAM display, and has no effect onthe actual simulation. $ns namtrace-all file-descriptor: This member function tells the simulator to recordsimulation traces in NAM input format. It also gives the file name that the trace will bewritten to later by the command $ns flush-trace. Similarly, the member function trace-all is for recording the simulation trace in a general format. proc finish {}: is called after this simulation is over by the command $ns at 5.0 "finish".In this function, post-simulation processes are specified. set n0 [$ns node]: The member function node creates a node. A node in NS iscompound object made of address and port classifiers. Users can create a node byseparately creating an address and a port classifier objects and connecting themtogether. However, this member function of Simulator object makes the job easier. Tosee how a node is created, look at the files: " ns-2.35/tcl/lib/ns-lib.tcl" and "ns-2.35/tcl/lib/ns-node.tcl". $ns duplex-link node1 node2 bandwidth delay queue-type: creates two simplex linksof specified bandwidth and delay, and connects the two specified nodes. In NS, theoutput queue of a node is implemented as a part of a link, therefore users shouldspecify the queue-type when creating links. In the above simulation script, DropTailqueue is used. If the reader wants to use a RED queue, simply replace the word DropTailwith RED. Like a node, a link is a compound object, and users can create its sub-objectsand connect them and the nodes. Link source codes can be found in "ns-2.35/tcl/lib/ns-lib.tcl" and "ns-2.35/tcl/lib/ns-link.tcl" files. One thing to note is that you can inserterror modules in a link component to simulate a lossy link (actually users can make andinsert any network objects). Refer to the NS documentation to find out how to do this. $ns queue-limit node1 node2 number: This line sets the queue limit of the twosimplex links that connect node1 and node2 to the number specified. Please take a lookat "ns-2.35/tcl/lib/ns- lib.tcl" and "ns-2.35/tcl/lib/ns-link.tcl", or NS documentation formore information. $ns duplex-link-op node1 node2 ...: The next couple of lines are used for the NAMdisplay. To see the effects of these lines, users can comment these lines out and try thesimulation. Now that the basic network setup is done, the next thing to do is to setuptraffic agents such as TCP and UDP, traffic sources such as FTP and CBR, and attach themto nodes and agents respectively.