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.
- set ns [new Simulator]: generates an NS
simulator object instance, and assigns it to variable ns (italics
is used for variables and values in this section). What this line does is
the following:
- Initialize the packet format (ignore this for now)
- Create a scheduler (default is calendar scheduler)
- Select the default address format (ignore this for now)
The "Simulator" object has member functions that do the following:
- Create compound objects such as nodes and links (described later)
- Connect network component objects created (ex. attach-agent)
- Set network component parameters (mostly for compound objects)
- Create connections between agents (ex. make connection between a "tcp"
and "sink")
- Specify NAM display options
- Etc.
Most of member functions are for simulation setup (referred to as plumbing
functions) and scheduling, however some of them are for the NAM display.
The "Simulator" object member function implementations are located in the
"ns-2/tcl/lib/ns-lib.tcl" file.
- $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
on the actual simulation.
- $ns namtrace-all file-descriptor:
This member function tells the simulator to record simulation traces in
NAM input format. It also gives the file name that the trace will be written
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 is compound object
made of address and port classifiers (described in a later section). Users
can create a node by separately creating an address and a port classifier
objects and connecting them together. However, this member function of Simulator
object makes the job easier. To see how a node is created, look at the files:
"ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-node.tcl".
- $ns duplex-link node1 node2 bandwidth delay
queue-type: creates two simplex links of specified bandwidth
and delay, and connects the two specified nodes. In NS, the output queue
of a node is implemented as a part of a link, therefore users should specify
the queue-type when creating links. In the above simulation script, DropTail
queue is used. If the reader wants to use a RED queue, simply replace the
word DropTail with RED. The NS implementation of a link is shown in a later
section. Like a node, a link is a compound object, and users can create
its sub-objects and connect them and the nodes. Link source codes can be
found in "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl" files. One thing to note is that
you can insert error modules in a link component to simulate a lossy link
(actually users can make and insert 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 two simplex links that connect node1
and node2 to the number specified. At this point, the authors do not know
how many of these kinds of member functions of Simulator objects are available
and what they are. Please take a look at "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", or NS documentation for more information.
- $ns duplex-link-op node1 node2 ...:
The next couple of lines are used for the NAM display. To see the effects
of these lines, users can comment these lines out and try the simulation.
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.
- set tcp [new Agent/TCP]: This line
shows how to create a TCP agent. But in general, users can create any agent or
traffic sources in this way. Agents and traffic sources are in fact basic
objects (not compound objects), mostly implemented in C++ and linked to OTcl.
Therefore, there are no specific Simulator object member functions that create
these object instances. To create agents or traffic sources, a user should
know the class names these objects (Agent/TCP, Agnet/TCPSink, Application/FTP
and so on). This information can be found in the NS documentation or partly in
this documentation. But one shortcut is to look at the "ns-2/tcl/libs/ns-default.tcl" file. This file contains the
default configurable parameter value settings for available network objects.
Therefore, it works as a good indicator of what kind of network objects are
available in NS and what are the configurable parameters.
- $ns attach-agent node agent: The
attach-agent member function attaches an agent object
created to a node object. Actually, what this function does is call the attach member function of specified node, which attaches
the given agent to itself. Therefore, a user can do the same thing by, for
example, $n0 attach $tcp. Similarly, each agent
object has a member function attach-agent that
attaches a traffic source object to itself.
- $ns connect agent1 agent2: After
two agents that will communicate with each other are created, the next thing
is to establish a logical network connection between them. This line
establishes a network connection by setting the destination address to each
others' network and port address pair.
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:
- $ns at time "string": This member
function of a Simulator object makes the scheduler (scheduler_ is the variable
that points the scheduler object created by [new Scheduler] command at the
beginning of the script) to schedule the execution of the specified string at
given simulation time. For example, $ns at 0.1
"$cbr start" will make the scheduler call a start member function of the CBR traffic source object,
which starts the CBR to transmit data. In NS, usually a traffic source does
not transmit actual data, but it notifies the underlying agent that it has
some amount of data to transmit, and the agent, just knowing how much of the
data to transfer, creates packets and sends them.
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 "out.tr" that will be
used for our simulation analysis. Following figure shows the trace format
and example trace data from "out.tr".

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:
- Node0 will be connected with Node1 with a duplex link. The link has 1.5
Mbps of bandwidth and 20 ms of delay.
- Node1 is connected with Node2 with a duplex link of 1 Mbps bandwidth and 20 ms of delay.
- Node2 will be connected with Node3 with a duplex link. The link has 1.5
Mbps of bandwidth and 20 ms of delay.
- The queue size of the links Node1-Node2 and Node2-Node3 is 150
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).
Appendix
-
For help on how to create a poisson traffic generator check the official site of
NS-2
- A Brief Tutorial on awk and grep
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
do
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`)
queue=$in-$out
echo $queue
done
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
http://www.freeos.com/guides/lsst/
and
http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html for basic shell
scripting. For awk you may refer to,
http://www.vectorsite.net/tsawk.html
- If you want to use another way to manipulate the trace file (e.g. C code) you are free to do so but writting shell scripts can be useful in general