ns-2 Introduction: DR Osman Ghazali
ns-2 Introduction: DR Osman Ghazali
Dr Osman Ghazali
Credits
Some of this material is based on,
variously:
ns-2 documentation
NS2 Online Tutorial
Outline
Background
Usage
Simulation
Fundamentals
Infrastructure
Debugging
Outline
Background
Links. Purpose. History. Components.
Status.
Usage
Simulation
Fundamentals
Infrastructure
Debugging
4
Useful links
http://www.isi.edu/nsnam/ns/
http://www.isi.edu/nsnam/nam/
ns-2 workshops
http://www.isi.edu/nsnam/ns/ns-tutorial/index.html
ns-2 manual
http://www.isi.edu/nsnam/ns/ns-documentation.html
detail
expense, scale
understanding
limited detail
detail within niche
niches, limited reuse
History
1989: REAL by Keshav
1995: ns by Floyd, McCanne at LBL
1997: ns-2 by the VINT project
(Virtual InterNetwork Testbed) at
LBL, Xerox PARC, UCB, USC/ISI
Now: ns-2.29 maintained at USC/ISI
ns-2.30 pending release
7
Components
ns-2.29
tcl
ex
examples
tcl8.4.11
doc
test
test cases
tk8.4.11
ns-tutorial
lib
core code
models
otcl-1.11
tclcl-1.17
C++
OTcl
nam-1.11
and, of course,
your simulation script!
Components
ns-2, the simulator itself
Specify simulation, generate traces
Depends on Tcl/Tk, OTcl, TclCL
Pre-processing
Traffic, topology generation
Post-processing
Analyse trace output with awk, etc
9
Status
ns-2.34 (Pending), October 2008
~330 kLOC C/C++, ~250 kLOC Tcl/OTcl
~100 test suites and 100+ examples
~400 pages of ns manual
Platform support
FreeBSD, Linux, Solaris, Windows, Mac
User base
>1k institutes (50 countries), >10k users
~300 posts/month to [email protected]
[email protected]
[email protected]
subscribe ns-users yourname@address
10
Outline
Background
Usage
Process. Split object model. Languages.
Tcl/OTcl. Linkage.
Simulation
Fundamentals
Infrastructure
Debugging
11
Using ns
Define
problem
Simulation
model
Extend
simulator
Post-process
results
12
Execute
simulation
Using ns
Create simulation
Describe network, protocols, sources, sinks
Interface via OTcl which controls C++
Execute simulation
Simulator maintains event list (packet list), executes
next event (packet), repeats until done
Events happen instantly in virtual time but could take
arbitrarily long real time
Single thread of control, no locking, races, etc
Post-process results
Scripts (awk, perl, python) to process text output
No standard library but some available on web
13
Languages
C++ for data
Per-packet processing, the core of ns
Fast to run, detailed, complete control
Basic Tcl
# Variables:
set x 10
set x
puts x is $x
# Procedures:
proc pow {x n} {
if {$n == 1} { return $x }
set part [pow x [expr $n-1]]
Basic OTcl
Class Person
# constructor:
Person instproc init {age} {
$self instvar age_
set age_ $age
}
# subclass:
Class Child -superclass Person
Child instproc greet {} {
$self instvar age_
puts age $age_ kid: Wassup!
}
# method:
Person instproc greet {} {
$self instvar age_
puts age $age_: Hello!
}
16
C++/OTcl Linkage
OTcl creates objects in the simulator
Evaluate strings in
interpreter
Objects are shared between OTcl and C++
by default
Access the OTcl interpreter via class Tcl
Tcl &tcl = Tcl::instance();
tcl.eval(); tcl.evalc(); tcl.evalf(,);
tcl.result(); res = tcl.result();
17
C++/OTcl Linkage
Commands and methods
For all TclObject, ns creates cmd{} instance
procedure to access compiled methods
Consider $o distance? <agentaddr>
Invokes distance?{} instance procedure of $o
18
C++/OTcl Linkage
struct hdr_ping {
char ret; double send_time; double rcv_time; int seq;
Header management
Constructor,
with bound variable
19
Outline
Background
Usage
Simulation
Model. Class hierarchy.
Fundamentals
Infrastructure
Debugging
20
App2
App1
Application,
Agent & Node
App2
App1
App2
Agent1 Agent2
Agent1 Agent2
Agent1 Agent2
Node
Node
Node
Link
Link
Node
Link
Link
Node
Agent1 Agent2
App1
21
App2
Link
Link
Node
Link
Node
Node
Link
Link
Node
Node
Agent1 Agent2
Agent1 Agent2
App1
App2
App1
App2
NsObject
Connector
Queue
Classifier
Delay
Agent
Trace
AddressClassifier
DropTail
Deq
Drop
Table of n RED
slots each TCP
pointing to aEnq
TclObject
classify() identifies destination slot for packet
AddressClassifier, PortClassifier found within Nodes
Reno
22
SACK
Outline
Background
Usage
Simulation
Fundamentals
Applications. Agents. Nodes. Links.
Packets. A simple topology.
Infrastructure
Debugging
23
Components
Four major types
Application
Communication instigator
Agent
Packet generator/consumer
Node
Addressable entity
Link
Set of queues
24
25
Applications
Application
Assumes attached to a TCPAgent
start(), stop(), send(), recv()
E.g. class TelnetApp
[ns-2.20/apps/telnet.h]
Schedule agent_->sendmsg() calls based on
exponential interarrival timer
TrafficGenerator
Assumes attached to a UDPAgent
init(), next_interval()
E.g. class POO_Traffic
[ns-2.29/tools/pareto.cc]
Schedule bursts of packets (Pareto on-off source)
26
Agents
TCP/UDP stacks
timeout(), send(), etc
Allocate and schedule packets
recv(), etc
Callback to app_ to notify of data
Subtype of Connector
class Agent : public Connector
[ns-2.29/common/agent.h,
/tcl/lib/ns-agent.tcl]
class TcpAgent : public Agent
class FullTcpAgent : public TcpAgent
Nodes
Addressable entity
built from classifiers
dmux_
entry_
28
classifier_
Distributes incoming
data to agents
Distributes outgoing
data to links
agents_
Agent
Agent
Agent
Link
Link
head_
enqT_
queue_
drophead_
deqT_
drpT_
Tracing
29
link_
ttl_
rcvT_
Links
n0
n1
head_
enqT_
queue_
drophead_
deqT_
drpT_
Tracing
30
link_
ttl_
rcvT_
Packets
next_
next_
hdrlen_
hdrlen_
data_
bits_
accessdata()
userdata()
access(int)
bits()
data_
IP header
TCP header
RTP header
Packet
31
bits_
Packets
Derived from base class Event
Other derived class is at-event (OTcl)
32
33
foreach cl [ PacketHeader
info subclass ] { puts "$cl
[$cl set hdrlen_]" }
PacketHeader/LRWPAN 216
PacketHeader/Encap 4
PacketHeader/XCP 64
PacketHeader/HttpInval 4
PacketHeader/Lms 56
PacketHeader/MFTP 64
PacketHeader/PGM 16
PacketHeader/SRMEXT 8
PacketHeader/PGM_SPM 8
PacketHeader/SRM 16
PacketHeader/PGM_NAK 16
PacketHeader/aSRM 8
PacketHeader/Pushback 4
PacketHeader/mcastCtrl 20
PacketHeader/NV 8
PacketHeader/CtrMcast 12
PacketHeader/LDP 40
PacketHeader/rtProtoDV 4
PacketHeader/MPLS 20
PacketHeader/GAF 8
PacketHeader/rtProtoLS 8
PacketHeader/Snoop 24
PacketHeader/Ping 32
PacketHeader/SCTP 8
PacketHeader/TFRC 56
PacketHeader/TCPA 16
PacketHeader/TFRC_ACK 64
PacketHeader/TCP 80
PacketHeader/Diffusion 192
PacketHeader/IVS 32
PacketHeader/RAP 24
PacketHeader/RTP 12
PacketHeader/AODV 808
PacketHeader/Message 64
PacketHeader/SR 720
PacketHeader/Resv 16
PacketHeader/TORA 32
PacketHeader/TCP_QS 12
PacketHeader/IMEP 512
PacketHeader/UMP 16
PacketHeader/ARP 32
PacketHeader/Src_rt 76
PacketHeader/MIP 32
PacketHeader/IP 28
PacketHeader/IPinIP 4
PacketHeader/Common 104
PacketHeader/LL 32
PacketHeader/Flags 9
PacketHeader/Mac 40
A Simple Topology
Application
n0
n1
Agent
Agent
0
1
0
0
1
head_
enqT_
queue_
drophead_
deqT_
drpT_
Link
n1 n0
34
link_
ttl_
rcvT_
Outline
Background
Usage
Simulation
Fundamentals
Infrastructure
Addressing. Routing. Dynamics. Maths.
Tracing.
Debugging
35
Addressing
Two modes: default and hierarchical
[ns-2.29/tcl/lib/ns-address.tcl]
Session
Simple Dijkstra, computed at each topology change
Routing
For dynamic (session) routing, need
a failure model: there are four
$ns rtmodel Trace <config_file> $n0 $n1
$ns rtmodel Exponential {<params>} $n0 $n1
$ns rtmodel Deterministic {<params>} $n0 $n1
$ns rtmodel-at <time> up|down $n0 $n1
38
Maths
Support classes [ns-2.29/tools/]
class Integrator
Simple linear interpolation integrator
class Samples
Tracks set of sample points (count, sum, sum2, mean, var)
class RNG
Implementation of pseudo-random number generator with a
period of 231-2
MRG32k3a proposed in P. LEcuyer, Good parameters and
implementations for combined multiple recursive
random number generators, Operations Research,
47(1):159164, 1999.
39
Monitoring
Record counters of interest for all
packets or on a per-flow basis
Tracing
ns and nam trace file formats
ns also supports show_tcphdr_ variable controlling
display of TCP flags
ns format
op
ts
prevhop
nexthop
type
size
flags
flowid
src
dst
seqno
pktid
1.102
tcp
40
0.0
1.0
Outline
Background
Usage
Simulation
Fundamentals
Infrastructure
Debugging
42
Debugging
Split object model is a PITA
Use Don Libs Tcl Debugger and gdb
See the ns-2 documentation for details
Use call Tcl::instance().eval() to
bounce between C++ and OTcl
$ns gen-map{}, prints all objects
Debugging
OTcl leaks: it does not garbage collect
Conservation
Avoid $ns trace-all $f
44
Debugging
OTcl leaks: it does not garbage collect
Conservation
Avoid $ns trace-all $f
Remove unnecessary packet headers
Debugging
OTcl leaks: it does not garbage collect
Conservation
Avoid $ns trace-all $f
Remove unnecessary packet headers
Use arrays for sequences
vs.
Saves ~40B/variable
Debugging
OTcl leaks: it does not garbage collect
Conservation
Avoid $ns trace-all $f
Remove unnecessary packet headers
Use arrays for sequences
Avoid naming objects unnecessarily
Saves ~80B/variable
Debugging
OTcl leaks: it does not garbage collect
Conservation
48
Summary
Background
Links. Purpose. History. Components. Status.
Usage
Process. Split object model. Languages. Tcl/OTcl.
Linkage.
Simulation
Model. Class hierarchy.
Fundamentals
Applications. Agents. Nodes. Links. Packets. A simple
topology.
Infrastructure
Addressing. Routing. Dynamics. Maths. Tracing.
Debugging
49
Thank You
50