Ns 2
Ns 2
Ns 2
Agenda
Introduction Interface
Simulator
Program Assignment
2
Introduction
Discrete event simulator Packet level simulation Open source Scheduling, routing and congestion control Wired networks: P2P links, LAN Wireless networks: terrestrial (ad-hoc, cellular; GPRS, UMTS, WLAN, Bluetooth), satellite Emulation and trace
3
Features
NS-2: Paradigm
Object-oriented programming
Protocol layering
Split-language programming
Interpreted (interactive) Setup and configuration Compiled (efficient) Algorithms and protocols Link Tcl/OTcl scripts and C/C++ codes Provide a layer of C++ glue over OTcl
C codes (C/C++)
TclCL (OTcl/C++)
OTcl
C++
} $ns duplex-link $n(0) $n(1) 1Mb 10ms DropTail # Create a UDP agent set udp(src) [new Agent/UDP] $udp(src) set packetSize_ 500 $ns attach-agent $n(0) $udp(src) proc finish {} { global ns nf $ns flush-trace; close $nf } $ns at 5.0 "finish" $ns run
Usage
Example
set tcp [new Agent/TCP] $tcp set window_ 30 $tcp advanceby 5000
Otherwise a warning will be issued when the shadow compiled object is created
11
C++
TcpAgent::TcpAgent() { bind(window_, &wnd_); }
OTcl
Agent/TCP set window_ 50 set tcp [new Agent/TCP] $tcp set window_ 100
12
Hook point
Yes
match send?
No
OTcl
set tcp [new Agent/TCP] $tcp advance 100
C++
int TcpAgent::command(int argc, const char*const* argv) { if (argc == 3) { if (strcmp(argv[1], advance) == 0) { int newseq = atoi(argv[2]); return TCL_OK; } } return (Agent::command(argc, argv); }
15
TclCL: Summary
Class TclObject
Unified interpreted (OTcl) and compiled (C++) class hierarchies Seamless access (procedure call and variable access) between OTcl and C++
Mechanism that makes TclObject work Primitives to access OTcl interpreter
Class TclClass
Class Tcl
16
C++ code
ex
examples
test
validation tests
mcast
OTcl code
...
17
Class Hierarchy
recv()
Scheduler NsObject Connector Queue Delay Agent TCP Reno
TclObject
Node Process
target_
Trace
Classifier
Application
FTP AddrClassifier PortClassifier
19
DropTail RED
SACK
Simulation Elements
Create the event scheduler (simulator) [Setup tracing] Create network topology [Setup routing] [Insert error modules/network dynamics] Create connection (transport) Create traffic (application) Start the scheduler Post-process data
20
Event Scheduler
Start scheduler
Trace
Network Topology
Nodes
set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 \ <bandwidth> <delay> <queue> queue: DropTail, RED, CBQ, FQ, Link delay = f (bandwidth, delay)
= packet transmission time + propagation delay
23
n1
dmux_ classifier_
Addr Classifier
Node entry entry_ classifier_ dmux_ Node entry entry_
Unicast Node
Multicast Node
24
head_
enqT_ tracing
queue_
drophead_
deqT_
drpT_
link_
n1 entry_
25
Routing
Unicast routing
$ns rtproto <type> <nodes> type: Static (default), Session, DV, LS, Manual nodes: default entire topology
Dijkstras all-pairs shortest path first algorithm Route calculation is done before simulation starts
Link cost
26
Routing
n0
Port Classifier
n1
Addr Classifier
Node entry
entry_
0 1
dmux_
n1 entry_
classifier_
drpT_
27
Routing
n0
Port Classifier
n1
Port Classifier Addr Classifier
Addr Classifier
entry_
0 1
classifier_
1 0
classifier_
dmux_
Link n1-n0
28
Transport: TCP
TCP
tcp [new Agent/TCP] tcpsink [new Agent/TCPSink] attach-agent $n0 $tcp attach-agent $n1 $tcpsink connect $tcp $tcpsink create-connection{}
Customization
Transport
n0
Port Classifier Addr Classifier
n1
Port Classifier
0
dmux_
Addr Classifier
0
dmux_
entry_
0 1
classifier_
entry_
1 0
classifier_
Link n1-n0
30
Application
FTP
set ftp [new Application/FTP] $ftp attach-agent $tcp $tcp attach-app FTP
set cbr [new Application/Traffic/CBR] $cbr set packetSize_ 1000 $cbr set rate_ 16000 $ns at <time> $app start
CBR
31
Application
n0
Port Classifier Application/FTP dst_=1.0 Agent/TCP agents_ Link n0-n1
n1
Port Classifier
Addr Classifier
0
dmux_
Addr Classifier
0
dmux_
entry_
0 1
classifier_
entry_
1 0
classifier_
Link n1-n0
32
Packet
Packet Header
cmn header
header ip header ts_ ptype_ uid_ size_ iface_
data
tcp header
rtp header
Example: Get the pointer to the common header: p->access(hdr_cmn::offset_) or, HDR_CMN(p)
trace header
...
34
Packet Flow
n0
Port Classifier Application/FTP dst_=1.0 Agent/TCP
n1
Port Classifier
dst_=0.0 Agent/TCPSink
Addr Classifier
Addr Classifier
entry_
0 1
Link n0-n1
entry_
1 0
Link n1-n0
35
Recap
NsObject: generic receive method recv() for packet reception Connector: one neighbor target_ Node: collection of classifiers and agents Link: encapsulation of queue and delay Classifier: packet demultiplexer (routing) Agent: protocol endpoint or implementation of routing protocol Application: traffic generation
36
Wireless Network
Wireless network
Nodes can move No explicit links used to connect nodes Mobile node Wireless channel and propagation model Packet headers Topology and movement Routing and forwarding
38
Class Hierarchy
TclObject
NsObject Connector BiConnector Phy WirelessPhy Node MobileNode
uptarget_ downtarget_
Channel
WirelessChannel
Propagation
TwoRayGround
Delay LL
MAC 802.11
39
Classifier: Forwarding
Node Entry
routing agent
LL
IFQ MAC PHY
ARP
LL: Link layer object IFQ: Interface queue MAC: MAC object PHY: Network interface
IFQ
MobileNode
CHANNEL
40
Scenario
3 mobile nodes Move within a 670m*670m flat topology DSR ad hoc routing protocol Random waypoint mobility model UDP and CBR traffic
41
An Example Step 1
# Create simulator
set ns [new Simulator] # Create a topology in a 670m x 670m area set topo [new Topography] $topo load_flatgrid 670 670
42
An Example Step 2
# Create God
set god [create-god 3]
Keep the number of nodes in the network Called by 802.11 MAC to keep a sequence number cache of all nodes Store an array of the smallest number of hops required to reach one node to another Used for setdest operation
$ns at 100.00 $god set-dist 2 3 1
43
An Example Step 3
# Define how to create a mobile node
$ns node-config \ -adhocRouting DSR \ -llType LL \ -macType Mac/802_11 \ -ifqLen 50 \ -ifqType Queue/DropTail/PriQueue \ -phyType Phy/WirelessPhy \ -antType Antenna/OmniAntenna \ -propType Propagation/TwoRayGround \ -channel [new Channel/WirelessChannel] \ -topoInstance $topo -agentTrace ON \ -routerTrace OFF \ -macTrace OFF \ -movementTrace OFF
44
Energy Parameters
$ns node-config \ energyModel -initialEnergy -txPower -rxPower EnergyModel \ 100.0 \ 0.6 \ 0.2
Node is energy-aware
45
An Example Step 4
# Create mobile nodes
for {set i 0} {$i<3} {incr i} { set node($i) [$ns node] # disable random motion for static network $node($i) random-motion 0 }
46
Scenario: Movement
Random movement
$node random-motion 1 $node start
A Movement File
$node_(0) set X_ 83.4 $node_(0) set Y_ 239.4 $node_(0) set Z_ 0.0 $node_(1) set X_ 257.1 $node_(1) set Y_ 345.4 $node_(1) set Z_ 0.0 $node_(2) set X_ 591.3 $node_(2) set Y_ 199.4 $node_(2) set Z_ 0.0 $ns_ at 33.0 "$node_(0) setdest 89.7 283.5 19.2 $ns_ at 51.0 "$node_(1) setdest 221.8 80.9 14.9" $ns_ at 50.0 "$node_(2) setdest 369.5 170.5 3.4"
48
Scenario: Traffic
49
A Traffic Scenario
set udp_(0) [new $ns_ attach-agent set null_(0) [new $ns_ attach-agent Agent/UDP] $node_(0) $udp_(0) Agent/Null] $node_(2) $null_(0)
set cbr_(0) [new Application/Traffic/CBR] $cbr_(0) set packetSize_ 1000 $cbr_(0) set interval_ 4.0 $cbr_(0) set random_ 1 $cbr_(0) set maxpkts_ 10000 $cbr_(0) attach-agent $udp_(0) $ns_ connect $udp_(0) $null_(0) $ns_ at 20.0 "$cbr_(0) start"
50
An Example Step 5
# Define node initial position in nam
for {set i 0} {$i < 3} {incr i} { $ns initial_node_position $node($i) 20 }
51
Summary
NS-2 is an open source, discrete event, and packet level network simulator NS-2 is written in C++ with OTcl interpreter as a front end TclCL provides linkage for class hierarchy, object instantiation, variable binding and command dispatching NS-2 provides abundant implementations of protocols used in wired and wireless networks
52
Random Variable
53
Arrival: Poisson arrival with rate Service: Packet size is exponential distribution (average tx duration is ) = / Average number of packet in the queue is /(1- ). Compare the theoretical value and simulation results.
54
Many TCP
Many TCP (Agent/TCP/Newreno) sessions sharing a bottleneck link and single destination (D)
Draw queue size of node BN every 0.1s. Draw throughput of each TCP sessions every 0.1s. S
TCP
1Mbps 1Mbps
1Mbps
TCP
TCP
S
S
BN
1Mbps
55
Channel: wireless channel Propagation: Tworayground Mac: 802.11 Queue: priority queue Routing protocol: DSDV Topology: 400mx500m Position of nodes: n0(5,5), n1(490,285), n2(150,240) TCP sender at n0 and receiver at n2
56
Downloading
Installing NS2
Type
./ns [filename].tcl
57
NS-2 Homepage (http://www.isi.edu/nsnam/ns/) NS-2 Manual (http://www.isi.edu/nsnam/ns/nsdocumentation.html) Marc Greis's tutorial (http://www.isi.edu/nsnam/ns/tutorial/index.html) NS by example (http://nile.wpi.edu/NS/) NS2 for beginners (http://www-sop.inria.fr/maestro/personnel/Eitan.Altman/COURS-NS/n3.pdf) OTcl (ftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/tutorial.html)
58
References
The ns Manual, January 2002 IEC ns workshop slides, June 2000 First ns workshop slides, September 1997 Wetherall and Lindblad, Extending Tcl for Dynamic Object-Oriented Programming, Proceedings of the Tcl/Tk Workshop, 1995 Welch, Practical Programming in Tcl and Tk, Prentice-Hall, 1995 Ousterhout, Tcl and the Tk Toolkit, Addison-Wesley, 1994
59