Network Simulator ns-2
Download
Report
Transcript Network Simulator ns-2
Network Simulator ns-2
Agenda
Introduction
Interface
Simulator
Tcl and OTcl
TclCL
Wired network
Wireless network
Program Assignment
2
Introduction
NS-2: network simulator version 2
Discrete event simulator
Packet level simulation
Features
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
NS-2: Paradigm
Object-oriented programming
Protocol layering
Large scale simulation
Modularity and extensibility
Maintenance and reusability
Split-language programming
Scripting language (Tcl)
System programming language (C)
4
NS-2: Split Languages
Tcl scripts (Tcl/OTcl)
C codes (C/C++)
Interpreted (interactive)
Setup and configuration
Compiled (efficient)
Algorithms and protocols
TclCL (OTcl/C++)
Link Tcl/OTcl scripts and C/C++ codes
Provide a layer of C++ glue over OTcl
5
NS-2: Split Objects
OTcl/C++ split objects
Pure C++
objects
Pure OTcl
objects
OTcl
TclCL linkage
C++
NS-2
6
NS-2: A Tcl Script Example
#!/home/hsieh/ns-allinone-2.27/bin/ns
set
set
$ns
for
ns [new Simulator]
nf [open out.tr w]
trace-all $nf
{set i 0} {$i<2} {incr i} {
set n($i) [$ns node]
;# create the nodes
}
$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
/home>ns abc.tcl
/home>abc.tcl
7
NS-2: A C++ Code Example
static class UdpAgentClass : public TclClass {
public:
UdpAgentClass() : TclClass("Agent/UDP") {}
TclObject* create(int, const char*const*) {
return (new UdpAgent());
}
} class_udp_agent;
UdpAgent::UdpAgent() : Agent(PT_UDP), seqno_(-1)
{
bind("packetSize_", &size_);
}
void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp::access(p)->seqno() = ++seqno_;
p->setdata(data);
target_->recv(p);
}
8
TclCL: Class TclObject
Base class in NS-2 for split objects
Usage
Mirrored in both C++ (TclObject) and OTcl
(SplitObject)
Instantiation, bind and command
Example
set tcp [new Agent/TCP]
$tcp set window_ 30
$tcp advanceby 5000
9
Class TclObject: Hierarchy
SplitObject OTcl class
hierarchy
C++ class
hierarchy
TclObject
Connector
Agent
Agent
Agent/TCP
TcpAgent
_o123
Agent/TCP
OTcl object
*tcp
Agent/TCP
C++ object
10
Class TclObject: Binding
Bi-directional variable bindings
Link C++ member variables (compiled) to OTcl
instance variables (interpreted)
Initialization through the closest OTcl class
variable
Agent/TCP set window_ 50
Do all initialization of bound variables in
~ns/tcl/lib/ns-default.tcl
Otherwise a warning will be issued when the
shadow compiled object is created
11
Class TclObject: Binding
C++
TcpAgent::TcpAgent() {
bind(“window_”, &wnd_);
… …
}
bind(), bind_time(), bind_bool(), bind_bw()
OTcl
Agent/TCP set window_ 50
set tcp [new Agent/TCP]
$tcp set window_ 100
12
Class TclObject: Command
Invoke C++ compiled functions through OTcl
interpreted methods
Hook point
A way of implementing OTcl methods in C++
Tcl method unknown{}
OTcl method cmd{}
Send all arguments after cmd{} call to
TclObject::command()
Use Tcl::resultf() in C++ to pass back
results
13
Class TclObject: Command
$tcp send
no such
SplitObject::unknown{}
procedure
OTcl space
$tcp cmd send
C++ space
TcpAgent::command()
Yes
process and return
match
“send”?
No
Invoke parent:
return Agent::command()
14
Class TclObject: Command
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
Class TclClass
Unified interpreted (OTcl) and compiled (C++)
class hierarchies
Seamless access (procedure call and variable
access) between OTcl and C++
Mechanism that makes TclObject work
Class Tcl
Primitives to access OTcl interpreter
16
NS-2: Directory Structure
ns-allinone (ftp://ftp.isi.edu/nsnam/ns-allinone-2.30.tar.gz)
Tcl8
TK8
OTcl
TclCL
...
tcl
ex
examples
test
validation tests
ns-2
lib
nam-1
...
C++ code
mcast
...
OTcl code
17
Network Simulator ns-2
Part II: Wired Network
Class Hierarchy
TclObject
recv()
Scheduler
NsObject
Node
Connector
Queue
Delay Agent
DropTail RED
TCP
Reno
target_
Classifier
Trace
Enq Deq Drop
SACK
Vegas
Process
Application
FTP
AddrClassifier
PortClassifier
19
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
Create event scheduler
Schedule events (OTcl)
OTcl: $ns at <time> <TCL_command>
C++: Scheduler::schedule(h,e, delay)
Obtain simulation time
set ns [new Simulator]
OTcl: $ns now
C++: Scheduler::clock()
Start scheduler
$ns run
The last line of your OTcl script
21
Trace
Trace packets on all links
$ns trace-all [open nstr.out w]
<event>
+
r
<pkt> <size> -- <fid> <src> <dst> <seq> <uid>
cbr
210 ------- 0
0.0
3.1
0
0
cbr
210 ------- 0
0.0
3.1
0
0
cbr
210 ------- 0
0.0
3.1
0
0
$ns namtrace-all [open namtr.out w]
Turn on tracing on specific links
<t> <from> <to>
1
0
2
1
0
2
1.00234 0
2
$ns trace-queue $n0 $n1
$ns namtrace-queue $n0 $n1
Output trace to /dev/null if not desired
22
Network Topology
Nodes
set n0 [$ns node]
set n1 [$ns node]
Links and queues
$ns duplex-link $n0 $n1 \
<bandwidth> <delay> <queue>
queue: DropTail, RED, CBQ, FQ, …
Link delay = f (bandwidth, delay)
= packet transmission time + propagation delay
23
Network Topology: Node
n0
n1
Port
Classifier
dmux_
Addr
Classifier
Node entry
classifier_
dmux_
entry_
Node entry
entry_
classifier_
Unicast Node
multiclassifier_
Multicast
Classifier
Multicast Node
24
Network Topology: Link
n0
n1
duplex link
head_
enqT_
tracing
queue_
drophead_
deqT_
drpT_
link_
ttl_
n1
entry_
simplex link
25
Routing
Unicast routing
Default static routing
$ns rtproto <type> <nodes>
type: Static (default), Session, DV, LS, Manual
nodes: default entire topology
Dijkstra’s all-pairs shortest path first algorithm
Route calculation is done before simulation starts
Link cost
$ns cost $n0 $n1 <cost>
default link cost = 1
26
Routing
n0
n1
Port
Classifier
Addr
Classifier
Node entry
entry_
0
1
dmux_
classifier_
head_
enqT_
queue_
drophead_
deqT_
link_
ttl_
n1
entry_
drpT_
27
Routing
n0
n1
Port
Classifier
Port
Classifier
Addr
Classifier
Addr
Classifier
entry_
0
1
dmux_
Link n0-n1
classifier_
entry_
1
0
dmux_
classifier_
Link n1-n0
28
Transport: TCP
TCP
set
set
$ns
$ns
$ns
Use
tcp [new Agent/TCP]
tcpsink [new Agent/TCPSink]
attach-agent $n0 $tcp
attach-agent $n1 $tcpsink
connect $tcp $tcpsink
create-connection{}
Customization
$agent set fid <fid>
$agent set packetSize_ <size>
29
Transport
n0
n1
Port
Classifier
Addr
Classifier
entry_
0
1
0
Port
Classifier
dst_= 1.0
Agent/TCP
agents_
dmux_
Link n0-n1
classifier_
entry_
Addr
Classifier
0
1
0
dmux_
dst_= 0.0
Agent/TCPSink
agents_
classifier_
Link n1-n0
30
Application
FTP
CBR
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
Start traffic generation
$ns at <time> “$app start”
31
Application
n0
n1
Application/FTP
Port
Classifier
entry_
Addr
Classifier
0
0
1
dmux_
Port
Classifier
dst_=1.0
Agent/TCP
agents_
Link n0-n1
classifier_
entry_
Addr
Classifier
0
1
0
dmux_
dst_=0.0
Agent/TCPSink
agents_
classifier_
Link n1-n0
32
Packet
Packets are events
Packets contain header section and data
Header section is a cascade of all in-use headers
Each packet contains a common header
Can be scheduled to “arrive”
packet size (used to compute transmission time)
packet type
timestamp, uid, …
All in-use headers are included when
simulation starts
Change packet size to reflect header cascading
33
Packet Header
cmn header
header
data
ip header
tcp header
rtp header
Example: Get the pointer to
the common header:
p->access(hdr_cmn::offset_)
trace header
ts_
ptype_
uid_
size_
iface_
...
or, HDR_CMN(p)
34
Packet Flow
n0
n1
Port
Classifier
Addr
Classifier
entry_
0
1
0
Application/FTP
Port
Classifier
dst_=1.0
Addr
Classifier
Agent/TCP
Link n0-n1
entry_
0
dst_=0.0
Agent/TCPSink
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
Network Simulator ns-2
Part III: Wireless Network
Wireless Network
Wireless network
Nodes can move
No explicit “links” used to connect nodes
Wireless network extension
Mobile node
Wireless channel and propagation model
Packet headers
Topology and movement
Routing and forwarding
38
Class Hierarchy
TclObject
NsObject
Node
Channel
Propagation
Connector
MobileNode
WirelessChannel
TwoRayGround
BiConnector
uptarget_
downtarget_
Phy
MAC
WirelessPhy
802.11
Delay
LL
39
Mobile Node: Portrait
Node
port
classifier
Classifier: Forwarding
protocol
agent
Node Entry
255
addr
classifier
defaulttarget_
LL
routing
agent
ARP
IFQ
MAC
PHY
MobileNode
Propagation
and antenna
models
Agent: Protocol entity
LL
LL: Link layer object
IFQ
IFQ: Interface queue
MAC
MAC: MAC object
PHY
PHY: Network interface
CHANNEL
40
Example: Ad Hoc Network
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
# ns trace and nam trace
$ns trace-all [open ns.tr w]
$ns namtrace-all-wireless [open ns.nam w] 670 670
42
An Example – Step 2
# Create God
set god [create-god 3]
God: General Operations Director
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
Node is energy-aware
EnergyModel \
100.0 \
0.6 \
0.2
Node status: on / off / sleep
Pt_ and Pt_consume_
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
}
# Define movement model (if applicable)
source movement-scenario-files
# Define traffic model (if applicable)
source traffic-scenario-files
46
Scenario: Movement
Mobile movement generator
~ns/indep-utils/cmu-scen-gen/setdest/setdest
setdest -n <numNodes>
-p <pauseTime> -s <maxSpeed>
-t <simTime> -x <maxX> -y <maxY>
Random movement
$node random-motion 1
$node start
Change POSITION_UPDATE_INTERVAL and
MAX_SPEED internally
47
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
Traffic pattern generator
CBR (UDP) or FTP (TCP) traffic
~ns/indep-utils/cmu-scen-gen/cbrgen.tcl
ns cbrgen.tcl [-type cbr|tcp]
[-nn nodes] [-seed seed]
[-mc connections] [-rate rate]
Specify in the OTcl script
Same as the wired network scenario
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
}
# Tell ns/nam the simulation stop time
$ns at 100.0 “$ns nam-end-wireless 100.0”
$ns at 100.0 “$ns halt”
# Start your simulation
$ns run
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
Test the following script
Change pareto to exponential distribution.
53
M/M/1 Queueing System
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.
TCP
TCP
S
S
1Mbps
1Mbps
BN
1Mbps
D
1Mbps
TCP
S
55
TCP over DSDV
3 node ad-hoc simulation
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
Draw TCP window size
56
NS2 Installation Guidelines
Downloading
NS2 is available from http://www.isi.edu/nsnam/ns/ns-build.html
Download the ns-allinone package, ns-allinone-2.30.tar.gz
Installing NS2
Go to directory of downloaded ns-allinone.tar.gz
Unpack the file using:
tar -xzvf ns-allinone-2.30.tar.gz
Go to the ns-allinone directory:
cd ns-allinone-2.30
Install it with the command
./install
How to run ns2
Type
./ns [filename].tcl
57
NS2 Useful Links
Network Simulator NS-2
NS-2 Homepage (http://www.isi.edu/nsnam/ns/)
NS-2 Manual (http://www.isi.edu/nsnam/ns/nsdocumentation.html)
Getting started with NS-2
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,” AddisonWesley, 1994
59