In lab Assignment 4

This lab will be based on ns. First we will explain how to use commands of ns and create tcl files. Then you will be responsible for building a simple scenerio and make some post simulation analysis.

The following lines describe the basic functions that you can use in a .tcl file to build a scenerio and generate traffic on it.

Now that the basic network setup is done, the next thing to do is to setup traffic agents such as TCP and UDP, traffic sources such as FTP and CBR, and attach them to nodes and agents respectively.

Assuming that all the network configuration is done, the next thing to do is write a simulation scenario (i.e. simulation scheduling). The Simulator object has many scheduling member functions. However, the one that is mostly used is the following:

After all network configuration, scheduling and post-simulation procedure specifications are done, the only thing left is to run the simulation. This is done by $ns run.

The above example and information come from the Worcester Polytechnic Institute and from the site Ns by Example. There you can find many examples for Ns Simulator and additional resources.

Trace Analysis Example

This section shows a trace analysis example. Trace_Example is the same OTcl script as the one in the "Simple Simulation Example" section with a few lines added to open a trace file and write traces to it. For the network topology it generates the simulation scenario, refer to last weeks figure. To run this script download "ns-simple-trace.tcl" and type "ns ns-simple-trace.tcl" at your shell prompt.

Trace_Example. Trace Enabled Simple NS Simulation Script (modified from Example ns-simple.tcl)

Running the above script generates a NAM trace file that is going to be used as an input to NAM and a trace file called "" that will be used for our simulation analysis. Following figure shows the trace format and example trace data from "".

Trace Format Example

Each trace line starts with an event (+, -, d, r) descriptor followed by the simulation time (in seconds) of that event, and from and to node, which identify the link on which the event occurred. The next information in the line before flags (appeared as "------" since no flag is set) is packet type and size (in Bytes). Currently, NS implements only the Explicit Congestion Notification (ECN) bit, and the remaining bits are not used. The next field is flow id (fid) of IPv6 that a user can set for each flow at the input OTcl script. Even though fid field may not used in a simulation, users can use this field for analysis purposes. The fid field is also used when specifying stream color for the NAM display. The next two fields are source and destination address in forms of "node.port". The next field shows the network layer protocol's packet sequence number. Note that even though UDP implementations do not use sequence number, NS keeps track of UDP packet sequence number for analysis purposes. The last field shows the unique id of the packet.

Having simulation trace data at hand, all one has to do is to transform a subset of the data of interest into a comprehensible information and analyze it.

Programming Assignment

Part 1
Build a simple network that consists of 4 nodes connected in a linear topology. The detailed specifications of the network are: The simulation will run for 5 seconds. At time 0.1 seconds a Poisson traffic generator will begin generating packets in Node0 with destination Node3. The Transport protocol that will be used for the communication of Node0 - Node3 is UDP. The Poisson will generate packets with a rate of 10000k and packet size 2000 (Tip: Set the Burst time equal to zero and the idle time to 10ms).

Plot the size of the queue in Node1 and Node2 as a function of time (Tip: Compute the size of the queue in intervals of 0.1 seconds).


                The focus of this tutorial is to provide you with one-line, clear statements to do what is necessary to extract relevant information for analysis from the trace file dumps generated by NS2. We proceed in a manner in unison with what needs to be achieved in this lab, mainly parsing through the trace file to extract information about the queue status.

Assuming the above trace file example, is stored in a file called nstrace, we would first need to extract those lines which correspond to events describing packet arrivals and departures to and from node 2 (Say). To extract all such lines you would need to type in awk '{if(($9>=2.0)&&($9<3.0)) elsif(($10>=2.0)&&($10<3.0))) print $0 }' nstrace > temp

What this statement does is check the 9th column of every line to see if the value is between 2.0 and 3.0 or not, the same for the 10th column, if this so, we select that line for further analysis.

To calculate the number of packets in the buffer in a time frame we must be able to "count" the number of packets be queued and dequeued, and get the difference. Say we want to measure the number of packets in the queue of node 2 every 1 second. We need to write awk 'BEGIN{count=0} /+/{if($2<1) count+=1} END{print count}' temp      to count the number of packets coming into the node in the first second.

The BEGIN defines the initialization of a var called count, and /+/ means we want awk to search thru the file for all lines that have a + in them.

Similarly, to compute number of packets leaving/ being dequeued from node 2 we write   awk 'BEGIN{count=0} /-/{if($2<1) count+=1} END{print count}' temp  Now that you have the number of packets coming in, and going out in 1 second you can compute the number of packets inside the queue of node 2  by taking the difference.

Since you will need to do this again and again, you need to write a small shell script which will automate the process for you. What you should do is take the output from the above awk commands into a shell variable like in=(`awk 'BEGIN{count=0} /+/{if($2<1) count+=1} END{print count}' temp `) similarly define a var called out. put this calculation in a for loop, like this.

for((counter=0; counter=MAX; counter++)) #where MAX is the time the last even recorded, say 5 sec, divided by the granularity of measurements for the queue, in this case,1


        in=(`awk 'BEGIN{count=0} /+/{if($2<1) count+=1} END{print count}' temp `)

        out=(`awk 'BEGIN{count=0} /-/{if($2<1) count+=1} END{print count}' temp`)


        echo $queue


Remember, this is skeleton code, you will need to write a complete shell script. Simply copying and pasting this will not be enough. Have a look at and for basic shell scripting. For awk you may refer to,