NS-2 Tutorial (For CMPE 252 A)

Download Report

Transcript NS-2 Tutorial (For CMPE 252 A)

NS Tutorial
(For CMPE 252 A)
By Duy Nguyen
10/25/2012
What is NS-2?
• A discrete event simulator targeted at
networking research
• Written in C++ and OTcl(OO of Tcl)
• OTcl usage:
– Configuration and setup
– Manipulating existing C++ objects
• C++ usage:
– Anything that requires processing each packet of a
flow
– Change behavior of existing C++ class
Getting NS-2
• Go to http://www.isi.edu/nsnam/ns/
• Download an ns-allinone package
• $ tar –zxf ns-allinone-2.34.tar.gz
• $ cd ns-allinone-2.34 && ./install
• Follow the instructions for setting up the environments:
setenv LD_LIBRARY_PATH /cse/grads/duy/ns-allinone2.34/otcl-1.13:/cse/grads/duy/ns-allinone-2.34/lib
setenv TCL_LIBRARY /cse/grads/duy/ns-allinone2.34/tcl8.4.18/library
• $ cd ns-2.34; ./validate
NS-2 initialization
# create simulator
set ns [new Simulator]
# Open trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
# Open trace file
$set namefile [open out.nam w]
$ns nametrace-all $namefile
# define a finish procedure
proc finish {} {
global ns tracefile namefile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# calling finish procedure at 125
$ns at 125.0 “finish”
# Run simulation
$ns run
NS-2 Create Nodes and Links
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
#available queues: RED, FQ, DRR, SFO,CBQ.
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
#a unidirectional link
$ns simplex-link $n0 $n1 10Mb 10m
# Set queue size limit on the link to 50
$ns queue-limit $n0 $n2 50
NS-2 Agents and Applications
# Set up a TCP connection for node 0
setup tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
# Create a sink agent for node 1
set sink [new Agent/TCPSink]
$ns attach-agent $ns1 $sink
#Now connect them
$ns connect $tcp $sink
# Set packet size
$ns set packetSize_ 512
# Set FTP over TCP connection
# FTP uses TCP/IP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
# Setting up UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n1 $null
$ns connect $udp $null
# Set up CBR over UDP connection
Set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set rate_ .01Mb
# introduce noise
$cbr set random_ true
# interval between transmission of packets
$cbr interval_ .005
# set packet Size
$cbr set packetSize_ 512
Scheduling Events
# Format
$ns at <time> <event>
# Examples
$ns at 1.0 “$cbr start”
$ns at 3.0 “$ftp start”
$ns at 95.0 “$cbr stop”
$ns at 100.0 “$ftp stop”
#a unidirectional link
$ns simplex-link $n0 $n1 10Mb 10m
Trace File
# Format
<event> <time> <from> <to> <pktype> <size>
------<fid> <src> <dest> <seq> <pktid>
#Please see
http://nsnam.isi.edu/nsnam/index.php/NS-2_Trace_Formats
+
r
+
+
+
r
r
+
r
+
+
r
+
r
+
+
-
0.987333 3 5 cbr 1000 ------- 2 1.0 5.0 28
0.987333 3 5 cbr 1000 ------- 2 1.0 5.0 28
0.989 1 2 cbr 1000 ------- 2 1.0 5.0 49 49
0.989 2 3 cbr 1000 ------- 2 1.0 5.0 49 49
0.992857 1 2 cbr 1000 ------- 2 1.0 5.0 50
0.992857 1 2 cbr 1000 ------- 2 1.0 5.0 50
0.994 2 3 cbr 1000 ------- 2 1.0 5.0 33 33
1 0 2 tcp 40 ------- 1 0.0 4.0 0 51
1 0 2 tcp 40 ------- 1 0.0 4.0 0 51
1.006667 3 5 cbr 1000 ------- 2 1.0 5.0 27
1.006857 1 2 cbr 1000 ------- 2 1.0 5.0 50
1.006857 2 3 cbr 1000 ------- 2 1.0 5.0 50
1.01016 0 2 tcp 40 ------- 1 0.0 4.0 0 51
1.01016 2 3 tcp 40 ------- 1 0.0 4.0 0 51
1.010714 1 2 cbr 1000 ------- 2 1.0 5.0 51
1.010714 1 2 cbr 1000 ------- 2 1.0 5.0 51
1.014 2 3 cbr 1000 ------- 2 1.0 5.0 29 29
1.014 3 5 cbr 1000 ------- 2 1.0 5.0 29 29
1.014 3 5 cbr 1000 ------- 2 1.0 5.0 29 29
1.020667 2 3 cbr 1000 ------- 2 1.0 5.0 34
1.024714 1 2 cbr 1000 ------- 2 1.0 5.0 51
1.024714 2 3 cbr 1000 ------- 2 1.0 5.0 51
1.028571 1 2 cbr 1000 ------- 2 1.0 5.0 52
1.028571 1 2 cbr 1000 ------- 2 1.0 5.0 52
28
28
50
50
27
50
50
52
52
34
52
52
53
53
Comments on the tracefile
• There are current old and new trace format, select one
which is most suitable.
• Need to write your own script for processing data from
the trace file i.e. throughput, delay etc
• Awk, perl, grep are some of the tools
• Need to specify how the throughput and delay are
obtained. For example:
– Throughput is the number of successful packets received
multiply by the packet length and divided by the total simulation
time
– Delay is difference between the time a packet arrives at the
queue and the time a packet gets transmitted successfully
Plotting graphs
• Matlab, Xgraph, and many other tools
• I personally use an awk script to extract
throughput, delay, etc information and use
Microsoft Excel to graph them.
Wireless Simulation
set
set
set
set
set
set
set
set
set
set
set
set
opt(chan)
opt(prop)
opt(netif)
opt(mac)
opt(ifq)
opt(ll)
opt(ant)
opt(ifqlen)
opt(nn)
opt(flow)
opt(row)
opt(column)
#set opt(adhocRouting)
set
set
set
set
set
set
set
opt(adhocRouting)
opt(cp) ""
opt(sc) ""
opt(x)
400
opt(y)
400
opt(seed)
0
opt(stop)
50
Channel/WirelessChannel;
Propagation/TwoRayGround;
Phy/WirelessPhy;
Mac/802_11;
Queue/DropTail/PriQueue;
LL
;#link layer
Antenna/OmniAntenna;
50
;# queue length
36
;# number of nodes
3
6
;#for grid
6
DSDV
;# routing protocol
DumbAgent ;# disable routing
;# connection pattern
;# node movement
;# x coordinate of topology
;# y coordinate of topology
;# seed for random number gen
;# time to stop simulation
# Create topography object
set topo
[new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
# configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting)
-llType $opt(ll)
-macType $opt(mac)
-ifqType $opt(ifq)
-ifqLen $opt(ifqlen)
-antType $opt(ant)
-propType $opt(prop)
-phyType $opt(netif)
-channelType $opt(chan)
-topoInstance $topo
-agentTrace ON
-routerTrace OFF
-macTrace ON
-movementTrace OFF
# create God for monitoring all nodes
create-god [expr $opt(nn)]
# Creating mobile nodes
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [$ns_ node]
}
# define node’s fixed position
for {set j 0} {$j <
for {set i 0} {$i
$node_([expr $j
$node_([expr $j
$node_([expr $j
}
}
$opt(row)} {incr j} {
< $opt(column)} {incr
* $opt(column) + $i])
* $opt(column) + $i])
* $opt(column) + $i])
i} {
set X_ [expr $i * 10]
set Y_ [expr $j * 10]
set Z_ 0
# setting up connection
for {set k 1} {$k <= $opt(flow)} {incr k} {
set udp_($k) [new Agent/UDP]
$ns_ attach-agent $node_([expr $l + 0]) $udp_($k)
set null_($k) [new Agent/Null]
$ns_ attach-agent $node_([expr $l + 1]) $null_($k)
set cbr_($k) [new Application/Traffic/CBR]
$cbr_($k) set packetSize_ $pktsize
$cbr_($k) set interval_ $inter
$cbr_($k) set random_ 1
$cbr_($k) attach-agent $udp_($k)
$ns_ connect $udp_($k) $null_($k)
$ns_ at 9.8 "$cbr_($k) start"
$ns_ at 30.0 "$cbr_($k) stop"
set l
}
[expr $l + 2]
# Tell all nodes when the simulation ends
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0004 "stop"
$ns_ at $opt(stop).0005 "puts \"NS EXITING...\" ; $ns_
halt"
Generating traffic-connection
# Available in
~ns/indep-utils/cmu-scen-gen
#
Format
ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed]
[-mc connections] [-rate rate]
# CBR 20 nodes, max 8 connections, seed value 1.0 and a
rate of 4.0, output to cbr-20-test
ns cbrgen.tcl -type cbr -nn 20 -seed 1.0 -mc 8 -rate 4.0
> cbr-20-test
Generating node-movement file
# Available in
~ns/indep-utils/cmu-scen-gen/setdest and consists of
setdest{.cc,.h} and Makefile
# Format
./setdest [-n num_of_nodes] [-p pausetime] [-s maxspeed]
[-t simtime] [-x maxx] [-y maxy] > [outdir/movementfile]
# 20 nodes, maximum speed of 10.0m/s, an average pause
between movement being 2s.
./setdest -n 20 -p 2.0 -s 10.0 -t 200 -x 500 -y 500 >
scen-20-test
#Specify traffic-connection and node-movement in script
set opt(cp)
"tcl/mobility/scene/cbr-20-test"
set opt(sc)
"tcl/mobility/scene/scen-20-test"
New protocols
• See Chapter 3.1-3.3 of NS Manual for better
understanding of C++ and Otcl
• Linkage between the C++ code and Tcl code(if
you modify an already existing protocol, no
need)
• Add new protocols
Make changes to makefile(.cc) or ~ns/tcl/lib/nslib.tcl(.tcl)
$make clean && ./configure --enable-debug &&
make
Other features
• Emulation (nice complement with your
simulation)
• NS-Click (Modular router)
• Sensor-nets
• Satellite
• …
More Help
• Use Nam visual tool to aid in visualizing the
networks (not really essential)
• Check out other tutorials in reference section
• Sign up for NS user mailing list
– http://www.isi.edu/nsnam/ns/ns-lists.html
• Documentation(most comprehensive source):
– http://www.isi.edu/nsnam/ns/ns-documentation.html
• Very detailed tutorial:
http://www.springer.com/engineering/signals/boo
k/978-0-387-71759-3
References
• NS-2 page-- http://www.isi.edu/nsnam/ns/
• Detailed NS-2 tutorial--http://wwwsop.inria.fr/members/Eitan.Altman/COURS
-NS/n3.pdf
• Marc Greis’ tutorial
http://www.isi.edu/nsnam/ns/tutorial/index.
html
• NS-2 by examples-http://nile.wpi.edu/NS/E
NS-3
• An open source project building a new
network simulator to replace ns-2
ns-3 is a new simulator, without
backward compatibility
Similarities to ns-2:
• C++ software core
• GNU GPLv2 licensing
• ported ns-2 models: random variables, error models,
OLSR, Calendar Queue scheduler, (more planned)
Differences:
• Python scripting (or C++ programs) replaces OTcl
• most of the core rewritten
• also based on the yans and GTNetS simulators
• new animators, configuration tools, etc. are in work
ns-3 follows a Linux architecture
• Linux corollaries
class
Ipv4Protocol
class
NetDevice
– each NetDevice is modelled
like struct net_device
– each layer-3 protocol has a
struct in_device
component for each
NetDevice
– this interface (transmit side)
is aligned with Linux
dev_queue_xmit()
– on receive side, demux is
modelled after Linux protocol
handlers
Software Integration
Do not reimplement models and tools for which
open-source implementations abound
• ns-3 conforms to standard input/output formats
so that other tools can be reused.
– e.g., pcap trace output, ns-2 mobility scripts
• ns-3 is adding support for running
implementation code
– Network Simulation Cradle integration has met with
success: Linux TCP code
– ns-3 “process” API
Instructions
• Go to http://www.nsnam.org/ and download
current ns-3
• $ wget https://www.nsnam.org/release/nsallinone-3.15.tar.bz2
• ./build.py --enable-examples --enable-tests
– This script will build everything
• Also: ./waf clean; ./waf -d optimized --enableexamples --enable-tests configure; ./waf
– Replace ‘optimized’ with ‘debug’ for enabling debug
messages
Sample
• Browse examples for sample scenarios. Try simpleglobal-routing
• // n0
• // \ 5 Mb/s, 2ms
• //
\
1.5Mb/s, 10ms
• //
n2 -------------------------n3
• //
/
• // / 5 Mb/s, 2ms
• // n1
• CBR/UDP flows from n0 to n3, and from n3 to n1
• FTP/TCP flow from n0 to n3, starting at time 1.2 to time
1.35 sec.
• UDP packet size of 210 bytes, with per-packet interval
0.00375 sec.
Sample
• To run: ./waf --run simple-global-routing
• Output: pcap files and trace .tr files
• To view pcap files:
– tcpdump -nn -tt -r filename.pcap
Sample
• duy@ddn:~/ns-allinone-3.15/ns-3.15$ tcpdump -nn -tt -r simpleglobal-routing-1-1.pcap
• reading from file simple-global-routing-1-1.pcap, link-type PPP
(PPP)
• 1.124900 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.134043 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.143186 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.152329 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.161472 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.170615 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.179757 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.188900 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
• 1.198043 IP 10.1.3.1.49153 > 10.1.2.1.9: UDP, length 512
Final comments
• Since ns-3 is a new network simulator, it
does not have as many network models as
ns-2.
• For wireless simulations: use ns-3
– Wireless Routing, Rate Adaptation
• For wired simulations: use ns-2
– Distance Vector (RIP): routing/rtProtoDV.cc
– Link State: linkstate/
References
• NS-3 page-- http://www.nsnam.org/
• Detailed NS-2 tutorial--http://wwwsop.inria.fr/members/Eitan.Altman/COURSNS/n3.pdf
• Slides from Tom Henderson’s talk UW-EE
• Documentationhttp://www.nsnam.org/documenta
tion/
• Tutorial:
http://www.nsnam.org/docs/release/3.15/tutorial/
singlehtml/index.html
• Contact: Duy Nguyen duy att soe.ucsc.edu
Qualnet RIP Tutorials links
http://www.cs.binghamton.edu/~vinkolar/qualnet/QualNetTutorial.pdf
http://www.cs.binghamton.edu/~vinkolar/qualnet/qualnet-tut1.pdf
http://www.cs.binghamton.edu/~vinkolar/qualnet/qualnet-tut2.pdf