Professional Documents
Culture Documents
• An introduction to ns-2
• What is ns-2
• Fundamentals
• Writing ns-2 codes
• Wireless support
• Traces support and visualization
• Emulation
• Related work
What is ns-2
• History
• What does it support
Ns Goals
• Support networking research and education
• Protocol design, traffic studies, etc
• Protocol comparison
• Provide a collaborative environment
• Freely distributed, open source
• Share code, protocols, models, etc
• Allow easy comparison of similar protocols
• Increase confidence in results
• More people look at models in more situations
• Experts develop models
• Multiple levels of detail in one simulator
Multiple levels of detail in
simulations
Consider two nodes simple t=1, A enqueues pkt on LAN
on an Ethernet: queuing t=1.01, LAN dequeues pkt
model: and triggers B
Scheduler
Event
• Maintenance OTcl
• Performance (speed and memory) Tcl
• Careful planning of modularity C++
ns-2
C++ and OTcl Separation
OTcl C++
Creating a link
Octl codes [ns_ duplex-link $n0 $n1 5Mb 2ms drop-tail]
n0 n1
duplex link
Details in C++
n1
head_ entry_
enqT_ queue_ deqT_ link_ ttl_
drophead_ drpT_
tracing simplex link
24
Otcl and C++: The Duality
C++
user
C++/OTcl
simulation split objects
scripts
otcl
ns
Problem
Setup/run
simulation
with ns
Hello World - Interactive Mode
Interactive mode: Batch mode:
swallow 71% ns simple.tcl
% set ns [new Simulator] set ns [new Simulator]
_o3 $ns at 1 “puts \“Hello
% $ns at 1 “puts \“Hello World!\””
World!\”” $ns at 1.5 “exit”
1 $ns run
% $ns at 1.5 “exit”
swallow 74% ns simple.tcl
2
Hello World!
% $ns run
swallow 75%
Hello World!
swallow 72%
Let’s learn ns-2 by some
examples first
• #Create a simulator object
• set ns [new Simulator]
• #Monitor the queue for the link between node 2 and node 3
• $ns duplex-link-op $n2 $n3 queuePos 0.5
• #Schedule events for the CBR agent and the network dynamics
• $ns at 0.5 "$cbr0 start"
• $ns rtmodel-at 1.0 down $n(1) $n(2)
• $ns rtmodel-at 2.0 up $n(1) $n(2)
• $ns at 4.5 "$cbr0 stop"
• #Call the finish procedure after 5 seconds of simulation time
• $ns at 5.0 "finish"
C++/OTcl
split objects
otcl
ns
Otcl and C++: The Duality
C++
user
C++/OTcl
simulation split objects
scripts
otcl
ns
{ SRMEXT off_srm_ext_}
{ Ping off_ping_ }} {
set cl PacketHeader/[lindex $pair 0]
Necessary Changes (IV): Makefile
sessionhelper.o delaymodel.o srm-ssm.o \
srm-topo.o \
ping.o \
$(LIB_DIR)int.Vec.o $(LIB_DIR)int.RVec.o \
$(LIB_DIR)dmalloc_support.o \
OK, now how to write a tcl script
to use the new Ping Agent you
just wrote?
Ping.tcl
#Create a simulator object
set ns [new Simulator]
#Open a trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
#Create three nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#Define a 'recv' function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer
from \
$from with round-trip-time $rtt ms."
}
#Create two ping agents and attach them to the nodes n0
and n2
set p0 [new Agent/Ping]
$ns attach-agent $n0 $p0
set p1 [new Agent/Ping]
$ns attach-agent $n2 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.6 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 1.0 "finish"
#Run the simulation
$ns run
C++/OTcl Linkage
Root of ns-2 object hierarchy
bind(): link variable values between
TclObject C++ and OTcl
command(): link OTcl methods to C++
implementations
TclClass Create and initialize TclObject’s
Tcl C++ methods to access Tcl interpreter
TclCommand Standalone global commands
Agent Agent
Agent/TCP TcpAgent
_o123 *tcp
Agent/TCP OTcl Agent/TCP C++
shadow object object
TclObject::bind()
C++ space
TcpAgent::command()
Yes match No
“send”?
TcpAgent Agent/TCP
NsObject
• Other important objects:
• NsObject: has recv() method
• Connector: has target() and drop() APPLICATION
• BiConnector: uptarget() & PRESENTATIO
downtarget() N
SESSION
• Class hierarchy TRANSPORT
• NsObject
• Connector
NETWORK
• PingAgent MAC
• BiConnector
PHY
• Mac802_11
Class Tcl
• Singleton class with a handle to Tcl interpreter
• Tcl& tcl = Tcl::instance();
• Usage
• Invoke OTcl procedure
• tcl.evalc(“callbackX”);
• Obtain OTcl evaluation results
• clock_ = atof(tcl.result());
• Pass a result string to OTcl
• tcl.resultf(“%g”, clock())
• Return success/failure code to OTcl
• tcl.error(“command not found”);
Class Tcl
Tcl& tcl = Tcl::instance();
if (argc == 2) {
if (strcmp(argv[1], “now”) == 0) {
tcl.resultf(“%g”, clock());
return TCL_OK;
}
tcl.error(“command not found”);
return TCL_ERROR;
} else if (argc == 3) {
tcl.eval(argv[2]);
clock_ = atof(tcl.result());
return TCL_OK;
}
Class TclCommand
• Packet tracing:
• On all links: $ns trace-all [open out.tr w]
• On one specific link: $ns trace-queue $n0 $n1$tr
• <Event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq>
<attr>
• + 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
• Flow monitor
set fmon [$ns_ makeflowmon Fid]
$ns_ attach-fmon $sink $fmon
$fmon set pdrops_
Tracing and Monitoring III
• Visualize trace in nam
$ns namtrace-all [open test.nam w]
$ns namtrace-queue $n0 $n1
• Variable tracing in nam
Agent/TCP set nam_tracevar_ true
$tcp tracevar srtt_
$tcp tracevar cwnd_
• Monitor agent variables in nam
$ns add-agent-trace $tcp
$ns monitor-agent-trace $tcp
$srm0 tracevar cwnd_
……
$ns delete-agent-trace $tcp
Ns Internals
• Tcl commands translates into
• series of object creation
• plumbing of these objects
Creating Network
• Nodes
set n0 [$ns node]
set n1 [$ns node]
• Links and queuing
$ns <link_type> $n0 $n1 <bandwidth>
<delay> <queue_type>
• <link_type>: duplex-link, simplex-link
• <queue_type>: DropTail, RED, CBQ, FQ, SFQ,
DRR, diffserv RED queues
Components of Node
z id_
z Address of node
z Monotonically increasing by 1
z neighbor_
z A list of neighbors
z agent_
z A list of agents
z nodetype_
z A node type identifier
z A routing module
Unicast node
• Address classifier
• Distribute incoming packets to the correct link
• Port classifier
• Distribute incoming packets to the correct
agent
• First element that handles incoming
packet at the node
• unicast: entry_
• multicast: switch_
Network Topology: Node
n0 n1
Port
Classifier Unicast Multicast dmux_
multiclassifier_
n2
Address
classifier To Port
demux
Node
Level 3
entry
Level 2
Level 1
n0 n1
duplex link
n1
head_ entry_
enqT_ queue_ deqT_ link_ ttl_
drophead_ drpT_
tracing simplex link
<ll_type>: LL
<ifq_type>: Queue/DropTail,
<mac_type>: MAC/802_3
<channel_type>: Channel
Local area network
• Link layer
• Queue: interface queue
• LL: simulate a link layer protocol (e.g. ARQ)
• Mac layer
• Must simulate some MAC protocol CS/CD/CA
• Sending: follow a MAC protocol before transmitting packets to
Channel
• Receiving: pass packets to link layer
• Physical layer
• Channel
• Sending side: simulate the shared medium
• Classifier/Mac
• Receiving side: delivering and optionally replicating packets to the
receiving MAC objects
Routing module
• Routing agent
• Exchange routing packets with neighbors
• Route logic
• Use information gathered by routing agent (or
topology database) to perform route
computation
• Classifier
• Use computed routing table to perform packet
forwarding
Setup Routing
• Unicast
$ns rtproto <type>
<type>: Static, Session, DV, cost, multi-path, Manual
• Multicast
$ns multicast (right after [new Simulator])
$ns mrtproto <type>
<type>: CtrMcast, DM, ST, BST
• Other types of routing supported: source routing,
hierarchical routing
Examples
z $ns rtproto Static ; default, use the Dijkstra’s all-pairs SPF
algorithm
z $ns rtproto Session ; recompute route when topology changes
z $ns rtproto DV $n1 $n2 $n3
z $ns rtproto LS $n1 $n2
n0 n1
Port
Classifier
Addr
Classifier
n1
Node entry 0 dmux_ head_ entry
enqT_ queue_ deqT_ link_ ttl_ _
entry_ 1
classifier_ drophead_ drpT_
..Routing
n0 n1
Port Port
Classifier Classifier
Addr Addr
Classifier Classifier
0 dmux_ 1 dmux_
Link n0-n1
entry_ 1 entry_ 0
classifier_ classifier_
Link n1-n0
Class RouteLogic
• Route configuration
• rtproto{} -> configure{} -> compute-routes{}
• Query nexthop
• [$ns get-routelogic] lookup $n1 $2
• Recompute route on topology change
• [$ns get-routelogic] notify
Inserting Errors
• 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
$ns rtmodel Deterministic {<params>} $n0 $n1
$ns rtmodel-at <time> up|down $n0 $n1
• Parameter list
[<start>] <up_interval> <down_interval> [<finish>]
Creating Connection and Traffic I
• UDP • CBR
set udp [new Agent/UDP] set src [new
set null [new Agent/Null] Application/Traffic/CBR]
$ns attach-agent $n0 $udp • Exponential or Pareto
$ns attach-agent $n1 $null on-off
$ns connect $udp $null set src [new
Application/Traffic/Exponential]
set src [new
Application/Traffic/Pareto]
$src attach-agent $udp
Creating Connection and Traffic II
• TCP • FTP
set tcp [new Agent/TCP] set ftp [new Application/FTP]
set tcpsink [new $ftp attach-agent $tcp
Agent/TCPSink] • Telnet
$ns attach-agent $n0 $tcp set telnet [new
$ns attach-agent $n1 Application/Telnet]
$tcpsink $telnet attach-agent $tcp
$ns connect $tcp $tcpsink
Creating Traffic: Trace Driven
• Trace driven
set tfile [new Tracefile]
$tfile filename <file>
set src [new Application/Traffic/Trace]
$src attach-tracefile $tfile
<file>:
• inter-packet time (msec) and packet size
(byte)
Transport
n0 n1
Port Port
Classifier dst_=1.0 Classifier dst_=0.0
Addr 0 Agent/TCP Addr Agent/TCPSink
Classifier Classifier 0
agents_ agents_
0 dmux_ 1 dmux_
entry_ entry_
classifier_ classifier_
n0 n1
n0 n1
Link n1-n0
Application objects I
• Exponential
• packetSize_
• burst_time_
• idle_time_
• rate_
• CBR
• packetSize_
• rate_
• interval_
• random_
• maxpkts_
Application objects II
• Pareto
• packetSize_
• burst_time_
• idle_time_
• rate_
• shape_
• Telnet
• interval_
• FTP
• maxpkts_
• Default values in ns/tcl/lib/ns-default.tcl
• All above are virtual applications
• focus only on “size” and “time” when data are transferred
Application-Level Simulation
• Simulate applications that actually transfer their
own data
• e.g. simulate various webcaching algorithms
• Features
• Build on top of existing transport protocol
• Transmit user data, e.g., HTTP header
• Examples
• TCP: Application/HttpApp
• UDP: Agent/Message
Application-Level Simulation
za representation of a uniform application-
level data unit (ADU)
za common interface to pass data between
applications
zmechanisms to pass data between
applications and transport agents.
ADU
zfunctionality of an ADU is similar to that of
a Packet
z pack user data into an array
zthen included in the user data area of an
nspacket
zUser must derive new agents to accept
user data from applications, or use an
wrapper like TcpApp
Passing data between
applications
class Process {
public:
Process() : target_(0) {}
inline Process*& target() { return target_; }
protected:
Process* target_;
};
Compare to Real World
• More abstract (much simpler):
• No addresses, just global variables
• Connect them rather than name
lookup/bind/listen/accept
• Easy to change implementation
Set tsrc2 [new agent/TCP/Newreno]
Set tsrc3 [new agent/TCP/Vegas]
Summary: Generic Script
Structure
set ns [new Simulator]
# [Turn on tracing]
# Create topology
# Setup packet loss, link dynamics
# Create routing agents
# Create:
# - multicast groups
# - protocol agents
# - application and/or setup traffic sources
# Post-processing procs
# Start simulation
Question?
Two examples
• Multicast
• Web traffic
Example: Multicast Routing
• Dynamic group membership
time
time G1
G2 n2
1.3s
1.25s
1.35s
1.2s G2 1.5Mb, 10ms
1.5Mb, 10ms
n0 n1 G1
1.5Mb, 10ms
G2 n3
Multicast: Step 1
# Turn on multicast
$ns multicast
# Turn on Tracing
set fd [new “mcast.nam” w]
$ns namtrace-all $fd
Multicast: Step 2
• Topology
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create links
$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n3 1.5Mb 10ms DropTail
Multicast: Step 3
• Routing and group setup
# Routing protocol: let’s run distance vector
$ns mrtproto DM
8 7 2 3
10Mb, 20ms
1.5Mb, 40ms
9 1 0 4
10 11 6 5
Multiple levels of feedback
effects in Web traffic
client server
request >user
10sec
response HTTP
1~10sec
<TCP
1sec
request
response
Web
•User session
¾User arrival
¾Number of page per user user
•Page
¾Page arrival Page
¾Number of objects per page
•Object object
¾Object arrival
¾Object size
2008/10/7
Web traffic: Step 1
# Turn on Tracing
set fd [new “web.nam” w]
$ns namtrace-all $fd
Web traffic: Step 2
• Topology
# Create nodes
for {set i 0} {$i < 12} {incr i} {
set n($i) [$ns node]
}
$ns set src_ [list 2 3 4 5 6]
$ns set dst_ [list 7 8 9 10 11]
# Create links
$ns duplex-link $n(0) $n(1) 1.5Mb 40ms DropTail
$ns duplex-link $n(0) $n(2) 10Mb 20ms DropTail
$ns duplex-link $n(0) $n(3) 10Mb 20ms DropTail
Web traffic: Step 3
• Set up client and server nodes
set i 0
foreach s [$ns set src_] {
$pool set-client $i $n($s)
incr i
}
set i 0
foreach s [$ns set dst_] {
$pool set-server $i $n($s)
incr i
}
Web traffic: Step 4
• Specify the distributions for page arrival,
page size, object arrival, object size
set interPage [new RandomVariable/Exponential]
$interPage set avg_ 1
set pageSize [new RandomVariable/Constant]
$pageSize set val_ 1
set interObj [new RandomVariable/Exponential]
$interObj set avg_ 0.01
set objSize [new RandomVariable/ParetoII]
$objSize set avg_ 10
$objSize set shape_ 1.2
Web traffic: Step 5
• Schedule web sessions
# Let’s schedule two sessions
$pool set-num-session 2
# Let’s have 10 pages per session
set numPage 10
TclObject Handler
NsObject
Connector Classifier
Reno SACK JS
Packet Format
ts_
cmn header
header ptype_
ip header
data uid_
tcp header
size_
rtp header
iface_
trace header
...
New Packet Header
NsObject
Connector Classifier
Reno SACK
Writing ns-2 codes
• Extending ns
• In OTcl
• In C++
• Debugging: OTcl/C++, memory
• Pitfalls
Debugging C++ in ns
• C++/OTcl debugging
• Memory debugging
• purify
• dmalloc
C++/OTcl Debugging
• Usual technique
• Break inside command()
• Cannot examine states inside OTcl!
• Solution
• Execute tcl-debug inside gdb
C++/OTcl Debugging
(gdb) call Tcl::instance().eval(“debug 1”)
15: lappend auto_path $dbg_library
dbg15.3> w
*0: application
15: lappend auto_path $dbg_library
dbg15.4> Simulator info instances
_o1
dbg15.5> _o1 now
0
dbg15.6> # and other fun stuff
dbg15.7> c
(gdb) where
#0 0x102218 in write()
......
Memory Debugging in ns
• Purify
• Set PURIFY macro in ns Makefile
• Usually, put -colloctor=<ld_path>
• Gray Watson’s dmalloc library
• http://www.dmalloc.com
• make distclean
• ./configure --with-dmalloc=<dmalloc_path>
• Analyze results: dmalloc_summarize
dmalloc: Usage
• Turn on dmalloc
• alias dmalloc ’eval ‘\dmalloc –C \!*`’
• dmalloc -l log low
• dmalloc_summarize ns < logfile
• ns must be in current directory
• Itemize how much memory is allocated in
each function
Pitfalls
• Scalability vs flexibility
• Or, how to write scalable simulation?
• Memory conservation tips
• Memory leaks
Scalability vs Flexibility
• It’s tempting to write all-OTcl simulation
• Benefit: quick prototyping
• Cost: memory + runtime
• Solution
• Control the granularity of your split object by
migrating methods from OTcl to C++
THE Merit of OTcl
C/C++ OTcl
split objects
• An introduction to ns-2
• What is ns-2
• Fundamentals
• Writing ns-2 codes
• Traces support and visualization
• Wireless support
• Emulation
• Related work
nsÆnam Interface
• Color
• Node manipulation
• Link manipulation
• Topology layout
• Protocol state
• Misc
nam Interface: Color
• Color mapping
$ns color 40 red
$ns color 41 blue
$ns color 42 chocolate
• Color ↔ flow id association
$tcp0 set fid_ 40 ;# red packets
$tcp1 set fid_ 41 ;# blue packets
nam Interface: Nodes
• Color
$node color red
• Shape (can’t be changed after sim starts)
$node shape box ;# circle, box, hexagon
• Marks (concentric “shapes”)
$ns at 1.0 “$n0 add-mark m0 blue box”
$ns at 2.0 “$n0 delete-mark m0”
• Label (single string)
$ns at 1.1 “$n0 label \”web cache 0\””
$node label-at up
$node label-color blue
nam Interfaces: Links
• Color
$ns duplex-link-op $n0 $n1 color "green"
• Label
$ns duplex-link-op $n0 $n1 label "abced“
$ns duplex-link-op $n1 $n2 label-color blue
$ns duplex-link-op $n1 $n2 label-at down
• Queue position
$ns duplex-link-op queuePos right
• Dynamics (automatically handled)
$ns rtmodel Deterministic {2.0 0.9 0.1} $n0 $n1
• Asymmetric links not allowed
nam Interface: Topology Layout
• “Manual” layout: specify everything
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(1) $n(2) orient right
$ns duplex-link-op $n(2) $n(3) orient right
$ns duplex-link-op $n(3) $n(4) orient 60deg
• Packet color
$ns color $n blue
$agent set fid_ $n
• Annotation
• Add textual explanation to your simulation
$ns at 3.5 "$ns trace-annotate \“packet drop\"“
• Control playback
$ns at 0.0 "$ns set-animation-rate 0.1ms"
The nam user interface
Summary of nam
• Turn on nam tracing in your Tcl script
• As easy as turning on normal tracing
• $ns namtrace $file
• Specify color/shape/label of node/link
• $ns duplex-link-op $node1 $node2 orient left
• Execute nam
• exec nam $filename
A live demo Nam.exe
namgraph
• Display a graph showing when packets
are received/dropped.
• Enabling namgraph
• Run the namfilter script on your nam trace file:
exec tclsh /path/to/namfilter.tcl out.nam
namgraph
The nam editor
• Create simple scenarios graphically
• Good for those who don’t want to learn Tcl,
but only a limited subset of ns is currently
available
The nam editor
Topology generator
• Inet
• GT-ITM
• TIERS
• BRITE
Summary
http://www.isi.edu/nsnam/ns/ns-topogen.html
• Random movement
• $node random-motion 1
• $node start
A Movement File
$node_(2) set Z_ 0.000000000000
$node_(2) set Y_ 199.373306816804
$node_(2) set X_ 591.256560093833
$node_(1) set Z_ 0.000000000000
$node_(1) set Y_ 345.357731779204
$node_(1) set X_ 257.046298323157
$node_(0) set Z_ 0.000000000000
$node_(0) set Y_ 239.438009831261
$node_(0) set X_ 83.364418416244
$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743
170.519203111152 3.371785899154"
$ns_ at 51.000000000000 "$node_(1) setdest 221.826585497093
80.855495003839 14.909259208114"
$ns_ at 33.000000000000 "$node_(0) setdest 89.663708107313
283.494644426442 19.153832288917"
Scenario Generator: Traffic
• Generating traffic pattern files
• CBR/TCP traffic
ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed
seed] [-mc connections] [-rate rate]
• CBR traffic
ns cbrgen.tcl –type cbr –nn 20 –seed 1 –mc 8 -
rate 4
• TCP traffic
ns cbrgen.tcl –type tcp -nn 15 -seed 0 –mc 6
• Source: ns-2/indep-utils/cmu-scen-gen/
A Traffic Scenario
set udp_(0) [new Agent/UDP]
$ns_ attach-agent $node_(0) $udp_(0)
set null_(0) [new Agent/Null]
$ns_ attach-agent $node_(2) $null_(0)
set cbr_(0) [new Application/Traffic/CBR]
$cbr_(0) set packetSize_ 512
$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 127.93667922166023 "$cbr_(0) start"
…….
An Example – Step 6
# Define node movement model
source <movement-scenario-files>
255
Agent: Protocol Entity
addr routing
classifier defaulttarget_ agent
Node Entry
LL ARP
LL LL: Link layer object
IFQ
IFQ IFQ: Interface queue
MAC
Propagation
MAC MAC: Mac object
PHY and antenna
models
MobileNode
PHY PHY: Net interface
CHANNEL
Prop/ant Radio propagation/
antenna models
Mobile Node : Components
• Classifiers
• defaulttarget_ points to routing agent object
• 255 is the port id assigned for rtagent_
• Routing agent
• May be ad hoc routing protocol like AODV,
DSDV or directed diffusion
Mobile Node: Components
• Link Layer
• Same as LAN, but with a separate ARP module
• Sends queries to ARP
• ARP
• Resolves IP address to hardware (MAC) address
• Broadcasts ARP query
• Interface queue
• Gives priority to routing protocol packets
• Has packet filtering capacity
Mobile Node: Components
• MAC
• 802.11
• IEEE RTS/CTS/DATA/ACK for unicast
• Sends DATA directly for broadcast
• SMAC (for sensor network)
• Network interface (PHY)
• Used by mobilenode to access channel
• Stamps outgoing pkts with meta-data
• Interface with radio/antenna models
Mobile Node: Components
• Radio Propagation Model
• Friss-space model – attenuation at near
distance (1/r2)
• Two-ray ground reflection model for far distance
(1/r4)
• Shadowing model -probabilistic
• Antenna
• Omni-directional, unity-gain
Wireless Channel
• Duplicate packets to all mobile nodes
attached to the channel except the sender
• It is the receiver’s responsibility to decide if
it will accept the packet
• Collision is handled at individual receiver
• O(N2) messages Æ grid keeper, reference-
copying etc
Grid-keeper: An Optimization
Mobile Node: Misc.
• Energy consumption model for sensor
networks
• Visualization of node movement,
reachability, and energy
• Validation test suites
Wireless Trace Support
• Original cmu trace format
• A separate wireless trace format
developed later at ISI
• Current ongoing effort to have ONE format
to combine all wired and wireless formats
Ad Hoc Routing
• Four routing protocols currently supported:
• DSDV
• Contributed by CMU
• DSR
• Contributed by CMU
• AODV
• Recently updated version from univ. of cincinnati;
• TORA
• Contributed by CMU
• Examples under tcl/test/test-suite-
wireless- { lan-newnode.tcl, lan-aodv.tcl,
lan-tora.tcl }
A Brief on MobileIP Support
• Developed by Sun
• Require a different Node structure than MobileNode
• Originally implemented for wired nodes
• Wired-cmu-wireless extension
• Originally CMU wireless model only supports ad-hoc
and wireless LAN
• Base-stations, support hier-rtg
• Standard MobileIP
• Home Agent, Foreign Agent, MobileHosts
• Example
• Under tcl/test/test-suite-wireless-lan-
newnode.tcl (tests: DSDV-wired-cum-wireless and
DSDV-wireless-mip)
A Brief on Satellite Networking
• Developed by Tom Henderson (UCB)
• Supported models
• Geostationary satellites: bent-pipe and
processing-payload
• Low-Earth-Orbit satellites
• Example: tcl/ex/sat-*.tcl and
tcl/test/test-suite-satellite.tcl
Simulate sensor network with
ns-2
• Currently support
• Routing
• Direct diffusion
• MAC
• SMAC
Outline
• An introduction to ns-2
• What is ns-2
• Fundamentals
• Writing ns-2 codes
• Traces support and visualization
• Wireless support
• Emulation
• Related work
What is Emulation ?
• Ability to introduce the simulator into a live
network
• Application:
• Allows testing real-world implementations
• Allows testing simulated protocols
• Requirements:
• Scheduler support
• Packet capture and generation capabilities
Emulation in ns-2
ns
Read packets Write packets
Network
Scheduler
• ns operates in virtual time using event-
driven simulation
• Real-time scheduler
• Does not advance the virtual clock to next
event
• Dispatches event at real-time
Emulation Objects
Capture: BPF
• Interface between ns and
network traffic network to ns
• Network Objects
• Access to live network via
• BPF and raw sockets ns
• Tap Objects
• Conversion between ns ns to network
and
• network packet formats Inject: Raw socket
Modes of Operation
• Packet conversion leads to two modes of
operation
• Opaque Mode
• Network packet fields are not interpreted
• Protocol Mode
• Network packet is interpreted
• TTL values reflect hop count in simulator
• Network packet fields are generated
• Ping responder, TCP application
Opaque Mode
• Network packet is passed unmodified
through simulator
Extern
Source
Destination Source
Size Destination
Size
Ns packet contains
a pointer to the
ns
network packet
Data
Network Packet
Network
Protocol Mode
• Network packet fields are generated by
the simulator
Host A Host B
Source Source TCP Agent
Destination Destination
Size Size
TCP
The ns packet Application
header is Data
mapped onto the
network packet
header and visa
versa . Network Packet ns
Send/receive Send/receive
packets from packets from
application ns
Network
Applications
• Opaque Mode
• Cross-traffic interaction leading to drop, delay
and re-ordering of packets
• End to End application testing
• Protocol Mode
• Protocol and conformance testing
• Evaluate effect of DDoS attacks
• Wireless networks
Example: Setup
Goal: Make a ns TCP agent interact with a TCP server
A 192.168.1.1 B 192.168.1.2 port 8000
tcp_node entry_node
TCPTap RawSocket
Example Script
set bpf [new Network/Pcap/Live]
TCP Agent
set dev [$bpf open readonly eth0]
$bpf filter "src 192.168.1.2 and src port 8000 \ tcp_node
and dst 10.0.0.1 and dst port 6000“
set capture_tap [new Agent/TCPTap]
$capture_tap network $bpf
$ns attach-agent $entry_node $capture_tap
$ns simplex-connect $capture_tap $tcp
A 192.168.1.1 B 192.168.1.2
TCP Agent
TCP Server
(8000)
BPF Raw Socket
Switch
Further Information
• http://www.isi.edu/nsnam/ns/ns-emulation.html
• Scripts ~ns/emulate
• Opaque Mode:
• em.tcl
• Protocol Mode
• thrutcp.tcl
• pingdemo.tcl
• tcpemu.tcl
• Kevin Fall, Network Emulation in the Vint/NS Simulator,
ISCC July 1999
Ns-2 related work
• Other simulators
• Researching uses ns-2
• Using ns-2 for education
Other simulators
• GloMoSim
• Developed by UCLA
• Based on Parsec
• Good support for wireless simulation
• SSFNET
• Java-based (with some C++ components)
• Use DML (Domain Modeling Language) for model configuration
• OPNET and QualNet
• Commercial software
• Good support for all layers
• PDNS (parallel/distributed ns)
• Developed by Georgia Tech
• Can distribute a simulation on several 8-16 workstations
• Aim to support very large simulation (memory & CPU)
• JavaSim
• Another Java-based simulator
Research using ns-2
• Routing
• MIT Click Router
• Zone routing protocol
• Wireless
• Mobility generator
• MIPv6
• Bluetooth
• 802.11 PSM
• Cellular IP
• Hierarchical Mobile IP
• GPRS
• UMTS
Research using ns-2..
• Generator
• Web traffic generators
• MPEG
• Several topology generators
• Others
• MPLS
• WFQ, CSFQ
• RSVP
• RIO
• BLUE
• TCP westwood
• SCTP
• Multistate error model
Using ns-2 for education