NS2 (Network Simulator Version 2) Manual
NS2 (Network Simulator Version 2) Manual
Internet Engineering
2 NS2 Manual
NS2 use Tcl language for creating simulation scenario file (for
example, sample.tcl). Network topology, transmission time,
using protocol etc... are defined in scenario file. If we execute
this scenario file, the simulation results will be outputed to
out.tr and out.nam file.
1
out.tcp record the change of TCP parameters by time. Using
Gnuplot software to plot a graph, it is easy to observe the appearance of congestion control. This
file is called as TCP trace file.
3 Scenario File
3.1 Indicate a Network in NS2
To write a scenario file, we need to understand about the network inside NS2.
The node (terminal, router) in real network have 4 layer using TCP/IP model as shown in Fig. 3.
Actually, forwarding router have only 2 bottom layer.
In the otherwise, 4 layers of TCP/IP model in NS2 are shown in Fig. 4. Two bottom layers are ready
automatically when a node setting up. TCP or UDP in transport layer are shown as Agent. FTP, CBR
are in application layers. Actually, the sender will set the application for creating data while the receive
will not because it is no need to simulate the receiving data in application layer.
Realistic networks use cable for link between 2 node. One cable is available for biconnection. In NS2, a
link is use for one connection. Two lines are needed for biconnection. Each link has a queue which similar
to buffer in realistic network. Packets sent from node should be queuing in queue. When queue is empty,
it will be send to other node via link.
The following is steps to create a scenario file.
2
Step4 Setting Simulation time and schedules
Step5 Declare finish.
--------------------------------------------------
set ns [new Simulator]
set file [open out.tr w]
$ns trace-all $file
set namfile [open out.nam w]
$ns namtrace-all $namfile
set tcpfile [open out.tcp w]
Agent/TCP set trace_all_oneline_ true
--------------------------------------------------
-------------------------------------------------
set n0 [$ns node]
--------------------------------------------------
then the node which have name as n0 is ready to use. (in Tcl file it will refer to $n0). Node is numbered
from 0. Link is declared as below.
--------------------------------------------------
$ns duplex-link $n0 $n2 3Mb 5ms DropTail
$ns duplex-link-op $n0 $n2 orient right-down
--------------------------------------------------
In line 1, a biconnection link between n0 and n2 is declared. The link between two nodes n0 and n2 has
bandwidth 3Mbps and delay 5ms. DropTail is a waiting queue type, if the queue overflows then the new
entered packet will be dropped, similar to buffer of general realistic network. Line 2 sets positions of node
and link for Nam. It does not affect to the result of simulation. The length of queue is set as below
--------------------------------------------------
$ns queue-limit $n2 $n3 20
$ns duplex-link-op $n2 $n3 queuePos 0.5
--------------------------------------------------
In line 1, the length of queue on the link from n2 to n3 is 20[packets]. In line 2, the position of queue is
set for Nam, 0.5 is the angle between link and queue, it equals to (0.5π).
3
3.2.3 Step2: Setting Agent
UDP Agent
To use UDP in simulation, the sender sets the Agent as UDP Agent while the receiver sets to Null Agent.
Null Agents do nothing except receiving the packets. Setting of Agent as follows.
--------------------------------------------------
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 0
$ns color 0 blue
--------------------------------------------------
In first 4 lines, udp and null Agent are set for n0 and n3, respectively. Line 5 declares the transmission
between udp and null. Line 6 sets the number for data flow of udp. This number will be recorded to all
packet which are sent from udp. By using this number, we can easily observe the flow that packet is belong
to by looking up trace file. Similarly, in line 7, we mark the color to discrete packet for showing result on
Nam.
TCP Agent
To use TCP in simulation, the sender sets the Agent as TCP Agent while the receiver sets to TCPSink
Agent. When receiving a packet, TCPSink Agent will reply an acknowledgment packet (ACK). Setting
Agent for TCP is similar to UDP.
--------------------------------------------------
set tcp [new Agent/TCP]
$ns attach-agent $n1 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$ns color 1 red
--------------------------------------------------
Confirm that fid and color differ from in the setting of udp.
Trace file of TCP is written as below.
--------------------------------------------------
$tcp attach-trace $tcpfile
$tcp trace cwnd_
--------------------------------------------------
Column 3, 4 Position of event (Link). Number in column 3, 4 is the number of node the event is occurred
when data send from node indicated in column 3 to node in column 4.
Column 7 Flag: For first retransmit packet in retransmission control, A is indicated in flag value. Actually,
the flag isn’t shown for all retransmission packet.
Column 9 Packet sender. For example, if the value is 1.0, it means that node with ID of 1 sends data
from port 0.
Column 10 Packet receiver. For example, if the value is 3.1, it means that node with ID of 3 receives
data by port 1.
What is a port?
In realistic network, a computer have to receive data from many service (Web, Mail...). In NS2,
there are many Agent will be set. For this reason, if we only record the IDs of sender and receiver,
we can not know the packet belong to which Agent. Using different port for each Agent is a way to
solve this problem. Actually, in relistic network, individual port is used for each service.
Colum 12 The ID number of that packet. Each packet have an individual ID number.
--------------------------------------------------
+ 1.70342 2 3 tcp 1040 ------- 1 1.0 3.1 30 231
d 1.70342 2 3 tcp 1040 ------- 1 1.0 3.1 30 231
--------------------------------------------------
At time 1.70342[sec], packet enters the queue and then be dropped at the same time. It means that,
packet tries to enter to the queue and be dropped because the queue overflows.
6
6 Trace File (out.tcp)
The below is a part of TCP trace file (actually, it is one line).
----------------------------------------------------------------------------------------
time: 1.57005 saddr: 1 sport: 0 daddr: 3 dport: 1 maxseq: 2 hiack: 1 seqno: 3
cwnd: 3.000 ssthresh: 20 dupacks: 0 rtt: 0.040 srtt: 0.030 rttvar: 0.015 bkoff: 1
----------------------------------------------------------------------------------------
Each value is the value of TCP parameter when packet is sent at sender. The meaning of each parameter
is explained below.
To understand about Congestion Control of TCP, parameters seqno, cwnd, ssthresh are very important.
Pay attention to these parameter. In this lecture, we will not concern about srtt, rttvar, bkoff.
7
7 appendix : full text of sample.tcl
##### Declare Simulator
set ns [new Simulator]
8
$tcp set fid_ 1
$ns color 1 red
### Setting output file of TCP Agent
$tcp attach-trace $tcpfile
$tcp trace cwnd_