Network Simulator: ns-2
Antonio Cianfrani
Dipartimento DIET
Università “Sapienza” di Roma
Introduction
• Network simulator provides a powerful support to research in networking area
– Design of new protocols, traffic characterization, etc – Comparison among different protocol versions
• It is an open source software
– The code is shared among users and can be modified so that to support new features, models, etc..
– In this way software reliability is improved
• It is the unique simulator implementing the whole TCP/IP stack
Features of ns-2
• Wired networks
– Routing DV, LS, PIM-SM – Trasport: TCP, UDP, SCTP
– Traffic generator: web, ftp, telnet, cbr, stocastiche
– Queuing scheduling policies :drop-tail, RED, FQ, SFQ, DRR – QoS: IntServ and Diffserv
• Wireless networks
– Ad hoc routing and mobile IP – Directed diffusion, sensor-MAC – Satellitar networks
ns-2 components
• ns, the simulator
• Nam, the network animator tool
• Pre-processing:
– Traffic and topology generators
• Post-processing:
Discrete event simulator
• NS-2 discrete event simulator used to study packet based networks
• Two programming languages are used – C++ : simulation engine
– OTcl (Object-oriented tool commande language): to manage the user/simulator interface
OTcl Interpreter
C++ NS simulator:- Event scheduler - Network components
The C++ simulator
• The C++ simulator is the engine of ns-2; it implements the supported protocols:
– Network protocols (MAC, routing, transport) – Traffic generators(CBR, FTP, On/Off, … ) – Queue scheduling policies (FIFO, RED, …) – Wireless protocols
The OTcl Interpreter
• Used by the user to describe the simulation scenario, to configure the network topology and to schedule the events
User role
• The user must describe the simulation scenario by means of an OTcl script; then results evaluation can
The OTcl language
• The OTcl language is an extension of Tcl scripting language: it is an Object Oriented language
• Il Tcl language makes use of the Tk (Tool Kit) library, that allows the creation of symple graphical interfaces • Information about OTcl available at
OTcl commands (1/4)
• The command to assign a value to a variable is set: set a 5
• The variable is not declared before. All variables are of string type: if needed the interpreter performs numerical convertion • The value of a variabile is identified with the symbol $:
set b $a
• The command to perform a arithmetic operation is expr : set c [ expr $a * $b /2] (c=a*b/2)
OTcl commands (2/4)
• To send an output to the screesn the puts command is used:
puts $a puts “ Ciao”
• To write into a file:
– opening: set file [ open “filename” w ]
– writing: puts $file $a
OTcl commands (3/4)
• FOR
for {set i 0} {$i < 100} {incr i} { ”istruzioni” } • WHILE while {$i < 100} { ”istruzioni” incr i } • IF if {$i < 10} { “istruzioni“ }
OTcl commands (4/4)
• A procedure is a function that can be called within the script. The proc command is used:
proc positivo { numero } { if {$numero < 0} { return $numero } return 0
}
• The command to call the procedure is: set valore [ positivo num ]
How to create a simulation
scenario
The OTcl script
• The first step is the OTcl script:
• - to describe the network topology; • - to schedule the events list.
• NS-2 provides classes.
• The most used classes are: – Simulator
– Node – Link – Agent
The OTcl classes
• Network scenario: – Simulator – Node – Link – Agent – Application • Results collecting: − Trace − MonitorSimulator
• It is the basic NS-2 class
• Must be defined at the beginning of every script
set ns [ new Simulator ]
• Within the simulator (ns) it will be possible to:
– create a network scenario
– monitor the simulation evolution (trace)
– manage the events scheduler
• At the end of a script the following command must be inserted: $ns run
Simulator class: the
scheduler
• The scheduler allows to introduce events in specific time instants.
• Commands:
– at t : at instant t
– after p : after p seconds • Example
set ns [ new Simulator ]
$ns at 2,5 “generate an IP packet” $ns after 3 “exit 0”
The Node class
• It represents an IP node, implementing all network layer functionalities:
– Addressing – Routing
• To create a node:
set n1 [ $ns node]
• To create N different nodes:
for {set i 0} {$i < N} {incr i} { set n($i) [$ns node] }
The Link class (1/2)
• It represent the IP link among two nodes. • Set of parameters:
– Link type
– Interconnected nodes
– Features: delay, bandwidth – Queing policy $ns duplex-link $n0 $n1 1Mb 10ms DropTail Bi-directional link Nodes Link Capacity Propagation delay Queuing policy
The Link class (2/2)
• A link is implemented by means of an ingress
queue
• If the link is bidirectional there will be two different
buffers
• It is possible to set the maximum queue size
$ns queue-limit $n1 $n2 queue-limit
The Agent class (1/3)
• It represents the transport layer of an IP node:
Nodo1
Nodo2
The Agent class (2/3)
• The transport protocol must be specified: TCP or
UDP.
• The Agent role:
– transmitterset agent_tcp [new Agent/TCP] set agent_udp [new Agent/UDP] – receiver
set null [new Agent/Null] (drop)
set tcpsink [new Agent/TCPSink] (reply with ack) set sink0 [new Agent/LossMonitor] (collect statistics)
The Agent class (3/3)
• The agent must be associated to a node
$ns attach-agent $node $agent
• The transmitter and the receiver agents must be
connected:
$ns connect $agent1 $agent2
• The first command must be always executed
before the second one.
The Application class
• It represents an application : FTP, Telnet, HTTP, traffic generator, ..
set application1 [new Application / ″TYPE″]
• The application must be associated to an Agent
$application1 attach-agent $agent1
Nodo1
Nodo2
Agent1
Agent2
Traffic generator (1/4)
• Traffic generator definition:
set traffic [new Application / Traffic / “Type”]
• EXPOO_Traffic: Exponential On/Off distribution. Packets
are sent at a fixed rate during on periods, and no packets are sent during off periods. On and off periods are taken from an exponential distribution. Packets are constant size.
– packetSize_ the constant size of the packets generated – burst_time_ the average “on” time for the generator
– idle_time_ the average “off” time for the generator – rate_ the sending rate during “on” times
Traffic generator (2/4)
• POO_Traffic: generates traffic according to a Pareto
On/Off distribution. This is identical to the exponential
On/Off distribution, except the on and off periods are taken from a Pareto distribution.
– packetSize_ the constant size of the packets generated – burst_time_ the average "on" time for the generator
– idle_time_ the average "off" time for the generator – rate_ the sending rate during "on" times
– shape_ the "shape" parameter used by the pareto distribution
Traffic generator (3/4)
• CBR_Traffic: generates traffic according to a deterministic
rate. Packets are constant size. – rate_ the sending rate
– interval_ (Optional) interval between packets
– packetSize_ the constant size of the packets generated – random_ flag indicating whether or not to introduce
random “noise” in the scheduled departure times (default is off)
– maxpkts_ the maximum number of packets to send (default is 228)
Traffic generator (4/4)
• TrafficTrace: generates traffic according to a trace file.
Each record in the trace file consists of 2 fields. The first contains the inter-arrival time (in microseconds), the second contains the length (in bytes).
set tfile [new Tracefile]
CBR Traffic
• CBR definition: parameters
set cbr [new Application / Traffic / CBR] $cbr set packetSize_ 500 (byte)
$cbr set interval_ 0.005 (seconds)
($cbr set rate_ 800kb ) $cbr attach-agent $agent
• Traffic scheduling
$ns at 0.5 "$cbr start" $ns at 4.5 "$cbr stop"
The ERROR MODEL Class
• It is possible to introduce network failures in the simulation
• Inserting/removing a link:
$ns rtmodel-at <time> up|down $n0 $n1
• Example:
$ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2)
The Trace class
• The Trace class generates a report (trace) with the whole events list.
• The report can also be related to a single link (queue). • A trace must be always associated to a file (to be
opened when starting the simulation).
The Trace class: commands
set traccia [ open traccia.tr w]
$ns trace-all $traccia
($ns trace-queue $S1 $S2 $traccia)
………
close $traccia
The Trace class: how to
monitor a queue
• To focus on a single link (from n1 to n2):
$ns trace-queue $n1 $n2 $traccia
• Also specific events can be selected
$ns create-trace type fileID $n1 $n2
• where type field can be:
Enqueue Dequeue Drops
The Monitor class (1/2)
• The Monitor class is able to monitor specific
simulation variables.
• The QueueMonitor element
set coda1_2 [ $ns monitor-queue $n1 $n2 [
$ns get-ns-traceall]]
The Monitor class (2/2)
• QueueMonitor fields:
size_ queue length (bytes) at t
pkts_ number of packets in queue at t parrivals_ total number of received packets barrivals_ total number of received bytes pdepartures_ total number of sended packets bdepartures_ total number of sended bytes
pdrops_ total number of dropped packets bdrops_ total number of dropped bytes
• A specific procedure (record) can be used to exploits QueueMonitor features.
The
record
procedure
proc record { } {
global ns f1 f2 coda1_2 set ns [Simulator instance] set time 0.1
set lung_coda [$coda1_2 set pkts_ ] set drops [$coda1_2 set pdrops_ ] set now [$ns now]
puts $f1 "$now $lung_coda" puts $f2 "$now $drops"
$ns at [expr $now + $time] "record“ }