COMM3F Network Simulation
(Ian Fletcher & John Tindle).

Ns2 Tutorial
Paul Guy 3rd December 2003

5. How to build a simple model for ns2

The sub sections will cover the main elements including snippets of code for creating a simple model. Contained at appendix C is a complete commented script that when run will invoke the nam and Xgraph. Two screen shots are shown below of the result.

[ Fig-7 Xgraph.]

[ Fig-8 Ns Simulation]

5.1. How to create a network object

To enable the simulation to run we need to create an instance of the simulator class. In the example the instance is given the variable name ns.
#Create a simulator object
set ns [new Simulator]

Open trace files for use with Xgraph and out.nam for nam. Multiple copies can be created.
#Open the trace files for Xgraph and out.nam for nam
set f0 [open w]
set f1 [open w]
set f2 [open w]
set nf [open out.nam w]
$ns namtrace-all $nf

A finish procedure needs to be created to close the output files, execute Xgraph and nam and exit.
#Define a 'finish' procedure
proc finish {} {
global ns nf f0 f1 f2
$ns flush-trace
#Close the NAM trace file
close $nf
#Close the output files
close $f0
close $f1
close $f2
#Execute xgraph to display the results
exec xgraph -geometry 600x400 &
#Execute NAM on the trace file
exec nam out.nam &
exit 0

5.2. How to create network nodes

Create instances of node. In the example the instances are given the variable names n0 and n1.
#Create Six nodes
set n0 [$ns node]
set n1 [$ns node]

5.3. How to link nodes together to make a network

The code specifies what nodes are to be linked together including the link type and parameters.
#Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail

5.4. How to add a protocol to a network

A TCP agent is created with the variable name tcp and attached to node 0 (n0).
#Setup a TCP1 connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp

Next we create a sink node with the variable name sink and attach it to node 5 (n5). Finally we connect the tcp agent to the sink.
set sink [new Agent/TCPSink]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink

5.5. How to add a traffic source to the model

A FTP traffic source (application) is created with a variable name ftp and attached to the agent tcp
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

5.6. Plot a graph showing the output results from a simulation

In order to use Xgraph we need to create output files to store the results in. Each set of data is stored in one trace file.
#Open the trace files for Xgraph
set f0 [open w]
set f1 [open w]
set f2 [open w]

In the finish procedure we need to close the output files and execute Xgraph specifying what output files to use and the size of the window.
#Close the output files
close $f0
close $f1
close $f2
#Execute xgraph to display the results
exec xgraph -geometry 600x400 &

In the example script procedures are used. In the first procedure UDP agents are attached to a node. By the nature of the procedure multiple agents can be added by calling the procedure with the appropriate parameters.
#Create a UDP agent and attach it to the node
set source [new Agent/UDP]
$ns attach-agent $node $source

Secondly we add an exponential traffic source defining the parameters that will be used. Again multiple traffic sources can be added by calling the procedure with the correct parameters.
#Create an Exponential traffic agent and set its properties
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst

Next we attach the traffic source to the agent. The second procedure counts how many bytes the sink has received and calculates the bandwidth used in Mbit/s and writes it to the specified output file in this case f0.
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f0 "$now [expr $bw0/$time*8/1000000]"

A Loss monitor agent is created and attached to a node

#Create five traffic sinks and attach them to the nodes n2 n3
set sinkl1 [new Agent/LossMonitor]
$ns attach-agent $n2 $sinkl1

Variables are created which uses an expression to pass parameters back to the procedure expo-traffic, which creates Traffic sources with the appropriate parameters.

#Create three traffic sources
set source0 [expo-traffic $n2 $sinkl1 200 2s 1s 100k]

Finally we call the record procedure to gather the data for output. The traffic sources are told when to start and stop and define the duration of the simulation before running it.

#Start record procedure
$ns at 0.0 "record"
#Start the traffic sources
$ns at 0.1 "$source0 start"
#Stop the traffic sources
$ns at 5.0 "$source0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.2 "finish"
#Run the simulation
$ns run