Title: ns2 tutorial for EE566
1Introduction to Ns-2
Zhibin WU WINLAB, ECE Dept. Rutgers
U. zhibinwu_at_winlab.rutgers.edu
1
2Goals
- Understanding NS-2
- Hands-on Experience with NS2
- Ns-2 by Example
- Write your own scripts
- Extending NS2
- Implementing new functionality
2
3Schedule
- Presentation (60 min)
- Group Assignments (10 min)
- Practices (1 Hr)
- Q A session (15 min)
3
4Talk Overview
- What is ns-2? (the evolution)
- Architecture
- Basic Tcl/Otcl commands
- Elements of an ns-2 simulation
- Example
- Online Resources Documentation
4
5What is ns?
- A discrete event, packet-level simulator
- Targeted at networking research
- Supports the simulation of
- intserv/diffserv, Multicast, Transport,
Applications, Wireless (fixed, mobile, satellite) - REAL variant (1989)?DARPA (LBL, Xerox PARC, UCB,
and USC/ISI) (1995) - Ns-3 Project (Ongoing)
5
6Status
- ns-2
- 100K lines of C
- 70K lines of OTcl
- 50K lines of test suite, examples, docs
- Platforms
- Most UNIX and UNIX-like systems (FreeBSD, Linux,
Sun Solaris) - Window 95/98/NT with Cygwin
- (Emulation only for FreeBSD for now)
6
7Remember!
- A simulator model of a real-world system is
necessarily a simplification. For example, in
simulating TCP - No SYN/FIN
- Equal size segments
- No variable window advertisement
- Bugs Users of ns are responsible for verifying
for themselves that their simulations are not
invalidated by bugs.
7
8Architecture Object-Oriented
- C for data
- Per packet action
- OTcl for control
- Periodic or triggered action
- Modularity (), re-usability(), scalability()
- Speed(-), memory(-)
8
9OTcl and C The Duality
Pure OTcl objects
Pure C objects
C/OTcl split objects
C
OTcl
ns
9
10Script in interactive mode
linux21 ns set ns new Simulator _o3 ns at
1 puts \Hello World!\ 1 ns at 1.5
exit 2 ns run Hello World! linux21
10
11A script in batch mode
simple.tcl set ns new Simulator ns at 1 puts
\Hello World!\ ns at 1.5 exit ns
run linux21 ns simple.tcl Hello World! linux21
11
12Basic Tcl
set a 43 set b 27 proc test a b set c
expr a b set d expr expr a - b
c for set k 0 k lt 10 incr k if
k lt 5 puts k lt 5, pow expr pow(d,
k) else puts k gt 5, mod
expr d k test 43 27
12
13Basic OTcl
Class Mom Mom instproc greet self instvar
age_ puts age_ years old mom How are you
doing? Class Kid -superclass Mom Kid instproc
greet self instvar age_ puts age_ years
old kid Whats up, dude?
set mom new Mom mom set age_ 45 set kid new
Kid kid set age_ 15 mom greet kid greet
13
14SIMULATE WIRED NETWORK
14
15Elements of ns-2 Simulation
- Create the event scheduler
- Turn on tracing
- Create network
- Setup routing
- Insert errors
- Create transport connection
- Create traffic
- Transmit application-level data
15
16Creating Event Scheduler
- Create event scheduler
- set ns new Simulator
- Schedule events
- ns at lttimegt lteventgt
- lteventgt any legitimate ns/tcl commands
- Start scheduler
- ns run
16
17Tracing
- Trace packets on all links
- ns trace-all open test.out w
- lteventgt lttimegt ltfromgt lttogt ltpktgt ltsizegt -- ltfidgt
ltsrcgt ltdstgt ltseqgt ltattrgt - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- Trace packets on all links in nam format
- ns namtrace-all open test.nam w
- Must appear immediately after creating scheduler
17
18Tracing
- Turn on tracing on specific links
- ns trace-queue n0 n1
- ns namtrace-queue n0 n1
18
19Creating Network
- Nodes
- set n0 ns node
- set n1 ns node
- Links and queuing
- ns duplex-link n0 n1 ltbandwidthgt ltdelaygt
ltqueue_typegt - ltqueue_typegt DropTail, RED, CBQ, FQ, SFQ, DRR
19
20Creating Network LAN
- LAN
- ns make-lan ltnode_listgt ltbandwidthgt ltdelaygt
ltll_typegt ltifq_typegt ltmac_typegt ltchannel_typegt - ltll_typegt LL
- ltifq_typegt Queue/DropTail,
- ltmac_typegt MAC/802_3
- ltchannel_typegt Channel
20
21Inserting Packet 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
21
22Network Dynamics
- Link failures
- Hooks in routing module to reflect routing
changes - Four models
- ns rtmodel-at lttimegt updown n0 n1
- ns rtmodel Trace ltconfig_filegt n0 n1
- ns rtmodel Exponential ltparamsgt n0 n1
- ns rtmodel Deterministic ltparamsgt n0 n1
- Parameter list
- ltstartgt ltup_intervalgt ltdown_intervalgt
ltfinishgt
22
23Setup Routing
- Unicast
- ns rtproto lttypegt
- lttypegt Static, Session, DV, cost, multi-path
- Multicast
- ns multicast (right after new Simulator)
- or set ns new Simulator multicast on
- ns mrtproto lttypegt
- lttypegt CtrMcast, DM, ST, BST (centralized,dense
mode, shared tree
23
24Creating Connection UDP
- UDP
- set udp new Agent/UDP
- set null new Agent/Null
- ns attach-agent n0 udp
- ns attach-agent n1 null
- ns connect udp null
24
25Creating Traffic On Top of UDP
- CBR
- set src new Application/Traffic/CBR
- Exponential or Pareto on-off
- set src new Application/Traffic/Exponential
- set src new Application/Traffic/Pareto
25
26Creating Connection TCP
- TCP
- set tcp new Agent/TCP
- set tcpsink new Agent/TCPSink
- ns attach-agent n0 tcp
- ns attach-agent n1 tcpsink
- ns connect tcp tcpsink
26
27Creating Traffic On Top of TCP
- FTP
- set ftp new Application/FTP
- ftp attach-agent tcp
- Telnet
- set telnet new Application/Telnet
- telnet attach-agent tcp
27
28Creating Traffic Trace Driven
- Trace driven
- set tfile new Tracefile
- tfile filename ltfilegt
- set src new Application/Traffic/Trace
- src attach-tracefile tfile
- ltfilegt
- Binary format (native!)
- inter-packet time (msec) and packet size (byte)
28
29Application-Level Simulation
- Features
- Build on top of existing transport protocol
- Transmit user data, e.g., HTTP header
- Two different solutions
- TCP Application/TcpApp
- UDP Agent/Message
29
30Script Structure for Wired Scenario
parameters and options set ns new Simulator
Turn on tracing Create topology Setup
packet loss, link dynamics Create routing
agents Create - protocol agents -
application and/or setup traffic sources
Post-processing procs Start simulation
30
31SIMULATE WIRELESS NETWORK
31
32 Script Structure Wireless
parameters and options set ns new Simulator
Turn on tracing create MobileNode object (PHY
to layer 3 configured) Create topology
create mobility Create Layer 4 and above -
UDP/TCP agents - application and/or setup
traffic sources Post-processing procedures
Start simulation
32
33Example Wireless Scenario
11
240m
14
13
11
8
4
7
3
2
33
34Example Step 1
- Define Parameters
- set cbr_size 500set cbr_interval 0.002set
num_row 4set time_duration 100
34
35Example Step 2
- Protocol Options
- set val(chan) Channel/WirelessChannel channel
typeset val(prop) Propagation/TwoRayGround
radio-propagation modelset val(netif)
Phy/WirelessPhy network interface typeset
val(mac) Mac/802_11 MAC typeset val(ifq)
Queue/DropTail/PriQueue interface queue
typeset val(ll) LL link layer typeset
val(ant) Antenna/OmniAntenna antenna modelset
val(ifqlen) 50 max packet in ifqset val(rp)
DSDV routing protocol
35
36Example Step 3
- Scheduler, Trace, Topo, God
- Initialize nsset ns_ new Simulatorset
tracefd open simple.tr wns_ trace-all
tracefd - set up topography object
- set topo new Topography
- topo load_flatgrid 1000 1000
- create-god expr num_row num_row
36
37Example Step 4
- Create Node Object with protocols
- 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) \
- -channel chan1 -topoInstance topo \
- -agentTrace ON -routerTrace OFF\
- -macTrace ON \
- -movementTrace OFF
-
- for set i 0 i lt expr num_rownum_row
incr i - set node_(i) ns_ node
-
37
38Example Step 5
- Create Topology
- set k 0
- while k lt num_row
- for set i 0 i lt num_row incr i
- set m expr iknum_row
- node_(m) set X_ expr i240
- node_(m) set Y_ expr k24020.0
- node_(m) set Z_ 0.0
-
- incr k
-
38
39Example Step 6
- Create Mobility
- Move node 11 from its original place to
top-right corner - ns_ at 60.0 "node_(11) setdest 990.0 990.0
15.0
40Example Step 7
- Set up transport layer (UDP)
- for set i 0 i lt num_row incr i
- set udp_(i) new Agent/UDP
- set null_(i) new Agent/Null
-
- ns_ attach-agent node_(8) udp_(0)
- ns_ attach-agent node_(4) udp_(1)
- ns_ attach-agent node_(13) udp_(2)
- ns_ attach-agent node_(14) udp_(3)
- ns_ attach-agent node_(11) null_(0)
- ns_ attach-agent node_(7) null_(1)
- ns_ attach-agent node_(2) null_(2)
- ns_ attach-agent node_(3) null_(3)
- for set i 0 i lt num_row incr i
- ns_ connect udp_(i) null_(i)
-
40
41Example Step 8
- Define Traffic Scenario
- for set i 0 i lt num_row incr i
- set cbr_(i) new Application/Traffic/CBRcb
r_(i) set packetSize_ cbr_sizecbr_(i) set
interval_ 0.5cbr_(i) attach-agent udp_(i) -
- ns_ at 11.0234 "cbr_(0) start"ns_ at 10.4578
"cbr_(1) start" ns_ at 12.7184 "cbr_(2)
start"ns_ at 12.2456 "cbr_(3) start"
41
42Example Step 9
- End-of-simulation wrapper (as usual)
- Tell nodes when the simulation endsfor
set i 0 i lt expr num_rownum_row incr
i ns_ at expr time_duration 10.0
"node_(i) reset"ns_ at expr
time_duration 10.0 "finish"ns_ at expr
time_duration 10.01 "puts \"NS Exiting...\"
ns_ halt"proc finish global ns_
tracefdns_ flush-traceclose tracefdputs
"Starting Simulation..."ns_ run
42
43Auxiliary Tools
- setdest
- used to generate the positions of nodes and their
moving speed and moving directions.setdest -v 1
-n 50 -p 0 -M 20 -t 900 -x 1500 -y 300 - cbrgen.tclns cbrgen.tcl -type cbrtcp -nn
nodes -seed seed -mc connections -rate
rate - Use source ltfilenamegt.tcl to add the generated
file to scipts -
43
44Resources
- http//www.isi.edu/nsnam/ns
- http//www.winlab.rutgers.edu/zhibinwu
- Tutorials
- Marc Greiss Tutorial (http//www.isi.edu/nsnam/ns
/tutorial/index.html) - Ns by example (http//nile.wpi.edu/NS/)
- Wireless Tutorial (http//www.isi.edu/nsnam/ns/ns-
tutorial/wireless.ppt )
44
45Documentation
- Tcl (Tool Command Language)
- http//dev.scriptics.com/scripting
- OTcl (MIT Object Tcl)
- otcl/doc/tutorial.html (in distribution)
- ns manual
- Included in distribution ns/doc
- http//www.isi.edu/salehi/ns_doc.ps.gz
45
46Advanced Topics
- Trace analysis
- Architecture of Mobilenode Object
- Extending NS-2 with new protocols and algorithms
- More complex changes
- Hybrid Node
- A node with multiple interfaces
46
47Group Assignments
- Download simple.tcl
- http//www.winlab.rutgers.edu/zhibinwu/simple.tcl
- Modifications
- Node-Configure changes
- Random topology generation 40-node within
1000x1000 area - Random Node Mobility
- Changeprotocol from UDP to TCP
- Dynamic Traffic change
47