Chapter 10: Introduction To Network Simulator (NS2) : CDA6530: Performance Models of Computers and Networks
Chapter 10: Introduction To Network Simulator (NS2) : CDA6530: Performance Models of Computers and Networks
2
Where to Run NS2
Our department unix server - eustis.eecs.ucf.edu has
installed ns2
Connect it using SSH, out-of-campus machine needs to setup
VPN first to campus.
First, you need to change default configuration
Modify the hidden file .profile under home directory
After login, type ‘pico .profile’ to use Pico editor
It lists what commands to be used at the bottom
Ctrl+O: write file Ctrl+X: exit
Add the following configuration
export PATH=$PATH:/usr/local/ns2/bin:/usr/local/ns2/tcl8.4.18/unix:/usr/local/ns2/tk8.4.18/unix
export LD_LIBRARY_PATH=/usr/local/ns2/otcl-1.13:/usr/local/ns2/lib
export TCL_LIBRARY=/usr/local/ns2/tcl8.4.18/library
3
Then, Run ns2:
czou@eustis:~$ ns
4
ns2- Network Simulator
One of the most popular simulator among networking
researchers
Open source, free
Discrete event, Packet level simulator
Events like ‘received an ack packet’, ‘enqueued a data
packet’
Network protocol stack written in C++
Tcl (Tool Command Language) used for specifying
scenarios and events.
You can think that Tcl is used to write the high-level
programming, while C++ code is doing the actual
simulation for speed consideration
Simulates both wired and wireless networks.
5
Goal of this tutorial
Understand how to write Tcl scripts to
simulate simple network topologies and
traffic patterns.
6
“Ns” Components
Ns, the simulator itself
Nam, the network animator
Visualize ns (or other) output
Nam editor: GUI interface to generate ns scripts
Since we only run ns2 in remote Unix server, we will not introduce
Nam usage in this class
It is not essential to simulation and analysis
Pre-processing:
Traffic and topology generators (use Tcl to write)
Post-processing:
Simple trace analysis, often in Awk, Perl, or Tcl
You can also use grep (under linux), or C/java
7
C++ and OTcl Separation
“data” / control separation
C++ for “data”:
per packet processing, core of ns
fast to run, detailed, complete control
8
Basic Tcl
variables: procedures:
set x 10
set z x+10 # string ‘x+10’ to z
proc pow {x n} {
set y [expr $x+10] if {$n == 1} { return $x }
puts “x is $x” set part [pow x [expr $n-1]]
return [expr $x*$part]
functions and expressions: }
set y [expr pow($x, 2)]
9
Simple two node wired network
n0 n1
10
Simple two node wired network
n0 n1
11
Simple two node wired network
#Create a simulator object
set ns [new Simulator]
#Open trace files
set f [open out.tr w]
$ns trace-all $f
#Define a 'finish' procedure
proc finish {} {
global ns f
$ns flush-trace
close $f
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation But we have no traffic!
$ns run 12
Adding traffic to the link
n0 n1
udp
udp
13
Adding traffic to the link
n0 n1
udp
udp
CBR: constant bit rate
cbr
cbr
14
Adding traffic to the link
n0 n1
udp
udp null
null
cbr
cbr
udp
udp null
null
cbr
cbr
17
Simulate a simple topology – UDP Traffic
sender
n0
n2 n3
router receiver
n1
sender SFQ: Stochastic Fair queuing
sender
n0
n2 n3
router receiver
n1
sender
sender
n0
n2 n3
router receiver
n1
sender
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
Simulate a simple topology – UDP Traffic
sender
n0
n2 n3
router receiver
n1
sender
#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1
Simulate a simple topology – UDP Traffic
sender
n0
n2 n3
router receiver
n1
sender
# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
Simulate a simple topology – UDP Traffic
sender
n0
n2 n3
router receiver
n1
sender
#Create a Null agent (a traffic sink) and attach it to
node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
Simulate a simple topology – UDP Traffic
sender
n0
n2 n3
router receiver
n1
sender
#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0
Simulate a simple topology – UDP Traffic
35
Post-processing: Basic usage of Grep
Command-line text-search program in Linux
Some useful usage:
Grep ‘word’ filename # find lines with ‘word’
Grep –v ‘word’ filename # find lines without ‘word’
Grep ‘^word’ filename # find lines beginning with ‘word’
Grep ‘word’ filename > file2 # output lines with ‘word’ to file2
ls -l | grep rwxrwxrwx # list files that have ‘rwxrwxrwx’ feature
grep '^[0-4]‘ filename # find lines beginning with any of the numbers
from 0-4
Grep –c ‘word’ filename # find lines with ‘word’ and print out the
number of these lines
Grep –i ‘word’ filename # find lines with ‘word’ regardless of case
36
Complex topology and link failure
sender
0
6 1
5 2
4 3 receiver
Complex topology and link failure
41
Inserting Errors
Creating Error Module
set loss_module [new ErrorModel]
$loss_module set rate_ 0.01
$loss_module unit pkt
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
Inserting Error Module
$ns lossmodel $loss_module $n0 $n1
42
Setup Routing
Unicast
$ns rtproto <type>
<type>: Static, Session, DV, cost, multi-path
Multicast
$ns multicast (right after [new Simulator])
$ns mrtproto <type>
<type>: CtrMcast, DM, ST, BST
Other types of routing supported: source routing,
hierarchical routing
43
Network Dynamics
Link failures
Hooks in routing module to reflect routing
changes
Four models
$ns rtmodel Trace <config_file> $n0 $n1
$ns rtmodel Exponential {<params>} $n0 $n1
#Exponential on/off model
$ns rtmodel Deterministic {<params>} $n0 $n1
$ns rtmodel-at <time> up|down $n0 $n1
Parameter list
[<start>] <up_interval> <down_interval> [<finish>]
See details at:
http://www.isi.edu/nsnam/ns/doc/node362.html
44
Wireless Network Simulation
This section is mainly based on Marc Greis'
Tutorial for the UCB/LBNL/VINT Network Simulator "ns“
http://www.isi.edu/nsnam/ns/tutorial/index.html
Others:
http://www.cs.binghamton.edu/~kliu/research/ns2code/
45
Simple 2 Nodes Simulation
Simulate a very simple 2-node wireless scenario
The topology consists of two mobilenodes
The mobilenodes move about within 500mX500m area
A TCP connection is setup between the two
mobilenodes.
Packets are exchanged between the nodes as they come within
hearing range of one another.
As they move away, packets start getting dropped.
46
Define options:
# Define options #
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ll) LL ;# Link layer type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) 2 ;# number of mobilenodes
47
Define NS simulator
set ns_ [new Simulator]
Define trace file
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
Create topology object
set topo [new Topography]
Topography object with (x=500, y=500)
$topo load_flatgrid 500 500
48
God (General Operations Director) Object
node to another
w
49
Define how a mobile node should be created
$ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
50
Manual Create Node Motion
Create two nodes
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0 ;# disable random motion
}
Provide node position and movement(speed & direction)
51
Produce some node movements
# Node_(1) starts to move towards node_(0)
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0“
52
Setup traffic flow between the two nodes:
# TCP connections between node_(0) and node_(1)
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
a
53
# Tell nodes when the simulation ends
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\“ ; $ns_ halt"
proc stop {} {
global ns_ tracefd
close $tracefd
}
54
Wireless Trace File Analysis
ACTION: [s|r|D]: s -- sent, r -- received, D -- dropped
WHEN: the time when the action happened
WHERE: the node where the action happened
LAYER: AGT -- application,
RTR -- routing,
LL -- link layer (ARP is done here)
IFQ -- outgoing packet queue (between link and mac layer)
MAC -- mac,
PHY -- physical
flags:
SEQNO: the sequence number of the packet
TYPE: the packet type
cbr -- CBR data stream packet
DSR -- DSR routing packet (control packet generated by routing)
RTS -- RTS packet generated by MAC 802.11
ARP -- link layer ARP packet
SIZE: the size of packet at current layer, when packet goes down, size increases, goes up size decreases
[a b c d]: a -- the packet duration in mac layer header
b -- the mac address of destination
c -- the mac address of source
d -- the mac type of the packet body
flags:
[......]: [ source node ip : port_number
destination node ip (-1 means broadcast) : port_number
ip header ttl
ip of next hop (0 means node 0 or broadcast)
]
55
Example of Trace Intepretation
s 76.000000000 _98_ AGT --- 1812 cbr 32 [0 0 0 0] ------- [98:0 0:0 32 0]
Application 0 (port number) on node 98 sent a CBR packet whose ID
is 1812 and size is 32 bytes, at time 76.0 second, to application 0 on
node 0 with TTL is 32 hops. The next hop is not decided yet.
r 0.010176954 _9_ RTR --- 1 gpsr 29 [0 ffffffff 8 800] ------- [8:255 -1:255 32 0]
The routing agent on node 9 received a GPSR broadcast (mac
address 0xff, and ip address is -1, either of them means broadcast)
routing packet whose ID is 1 and size is 29 bytes, at time 0.010176954
second, from node 8 (both mac and ip addresses are 8), port 255
(routing agent).
56
Trace beginning:
57
Using node-movement/traffic-pattern files
58
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(x) 670 ;# X dimension of the topography
set val(y) 670 ;# Y dimension of the topography
set val(ifqlen) 50 ;# max packet in ifq
set val(seed) 0.0
set val(adhocRouting) DSR
set val(nn) 3 ;# how many nodes are simulated
set val(cp) "../mobility/scene/cbr-3-test"
set val(sc) "../mobility/scene/scen-3-test"
set val(stop) 2000.0 ;# simulation time
59
“Source” node-movement and connection pattern files
#
# Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)
#
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)
60
Creating random traffic-pattern for
wireless scenarios
ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-
mc connections] [-rate rate]
Cbrgen.tcl is a traffic generator script to generate TCP or CBR
traffic
1/rate is the average interval time between CBR packets
Connections is the maximum # of connections
The start times for the TCP/CBR connections are randomly
generated with a maximum value set at 180.0s
Example: ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc
8 -rate 4.0 > cbr-10-test
create a CBR connection file between 10 nodes, having
maximum of 8 connections, with a seed value of 1.0 and a rate
of 4.0.
61
Example: ns cbrgen.tcl -type tcp -nn 25 -seed
0.0 -mc 8 > tcp-25-test
Create a maximum 8 TCP connections (FTP traffic)
between 25 nodes.
62
Creating node-movements for wireless
scenarios
Setdest is the program under ~ns/indep-utils/cmu-
scen-gen/setdest
63
Example: ./setdest -n 20 -p 2.0 -M 10.0 -t
200 -x 500 -y 500 > scen-20-test
an average pause between movement being
2s. Simulation stops after 200s and the
topology boundary is defined as 500 X 500.
64
Line in the file:
$ns_ at 2.000000000000 "$node_(0) setdest
90.441179033457 44.896095544010
1.373556960010”
node_(0) at time 2.0s starts to move toward
destination (90.44, 44.89) at a speed of 1.37m/s.
$ns_ at 899.642 "$god_ set-dist 23 46 2”
shortest path between node 23 and node 46
changed to 2 hops at time 899.642.
65