ns tutorial (for cmpe 252 a) by duy nguyen 10/25/2012
TRANSCRIPT
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-allinone-
2.34/otcl-1.13:/cse/grads/duy/ns-allinone-2.34/libsetenv TCL_LIBRARY
/cse/grads/duy/ns-allinone-2.34/tcl8.4.18/library
• $ cd ns-2.34; ./validate
NS-2 initialization
# create simulatorset ns [new Simulator]
# Open trace fileset 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 procedureproc 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
# Create nodesset 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 Create Nodes and Links
# Set up a TCP connection for node 0 setup tcp [new Agent/TCP]$ns attach-agent $n0 $tcp # Create a sink agent for node 1set sink [new Agent/TCPSink] $ns attach-agent $ns1 $sink
#Now connect them$ns connect $tcp $sink
# Set packet size$ns set packetSize_ 512
NS-2 Agents and Applications
# Set FTP over TCP connection# FTP uses TCP/IPset ftp [new Application/FTP]$ftp attach-agent $tcp
# Setting up UDP connectionset udp [new Agent/UDP]$ns attach-agent $n0 $udpset null [new Agent/Null]$ns attach-agent $n1 $null$ns connect $udp $null
# Set up CBR over UDP connectionSet 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
# 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
Scheduling Events
# 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
Trace File
+ 0.987333 3 5 cbr 1000 ------- 2 1.0 5.0 28 28- 0.987333 3 5 cbr 1000 ------- 2 1.0 5.0 28 28r 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 50- 0.992857 1 2 cbr 1000 ------- 2 1.0 5.0 50 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 51r 1.006667 3 5 cbr 1000 ------- 2 1.0 5.0 27 27r 1.006857 1 2 cbr 1000 ------- 2 1.0 5.0 50 50+ 1.006857 2 3 cbr 1000 ------- 2 1.0 5.0 50 50r 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 52- 1.010714 1 2 cbr 1000 ------- 2 1.0 5.0 51 52r 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 34r 1.024714 1 2 cbr 1000 ------- 2 1.0 5.0 51 52+ 1.024714 2 3 cbr 1000 ------- 2 1.0 5.0 51 52+ 1.028571 1 2 cbr 1000 ------- 2 1.0 5.0 52 53- 1.028571 1 2 cbr 1000 ------- 2 1.0 5.0 52 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.
set opt(chan) Channel/WirelessChannel; set opt(prop) Propagation/TwoRayGround; set opt(netif) Phy/WirelessPhy; set opt(mac) Mac/802_11;set opt(ifq) Queue/DropTail/PriQueue;set opt(ll) LL ;#link layerset opt(ant) Antenna/OmniAntenna;set opt(ifqlen) 50 ;# queue lengthset opt(nn) 36 ;# number of nodesset opt(flow) 3set opt(row) 6 ;#for gridset opt(column) 6#set opt(adhocRouting) DSDV ;# routing protocolset opt(adhocRouting) DumbAgent ;# disable routingset opt(cp) "" ;# connection patternset opt(sc) "" ;# node movementset opt(x) 400 ;# x coordinate of topologyset opt(y) 400 ;# y coordinate of topologyset opt(seed) 0 ;# seed for random number genset opt(stop) 50 ;# time to stop simulation
Wireless Simulation
# Create topography objectset 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 nodescreate-god [expr $opt(nn)]
# Creating mobile nodesfor {set j 0} {$j < $opt(nn)} {incr j} { set node_($j) [$ns_ node]}
# define node’s fixed positionfor {set j 0} {$j < $opt(row)} {incr j} { for {set i 0} {$i < $opt(column)} {incr i} { $node_([expr $j * $opt(column) + $i]) set X_ [expr $i * 10] $node_([expr $j * $opt(column) + $i]) set Y_ [expr $j * 10] $node_([expr $j * $opt(column) + $i]) set Z_ 0 }}
# setting up connectionfor {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 endsfor {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"
# Available in ~ns/indep-utils/cmu-scen-gen
# Formatns 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-testns cbrgen.tcl -type cbr -nn 20 -seed 1.0 -mc 8 -rate 4.0 > cbr-20-test
Generating traffic-connection
# 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/movement-file]
# 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 scriptset opt(cp) "tcl/mobility/scene/cbr-20-test" set opt(sc) "tcl/mobility/scene/scen-20-test"
Generating node-movement file
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/ns-
lib.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/book/978-0-387-71759-3
References
• NS-2 page-- http://www.isi.edu/nsnam/ns/• Detailed NS-2 tutorial--
http://www-sop.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
classNetDevice
classIpv4Protocol
• Linux corollaries– 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/ns-allinone-3.15.tar.bz2
• ./build.py --enable-examples --enable-tests – This script will build everything
• Also: ./waf clean; ./waf -d optimized --enable-examples --enable-tests configure; ./waf – Replace ‘optimized’ with ‘debug’ for enabling debug
messages
Sample • Browse examples for sample scenarios. Try simple-
global-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 simple-
global-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://www-sop.inria.fr/members/Eitan.Altman/COURS-NS/n3.pdf
• Slides from Tom Henderson’s talk UW-EE• Documentationhttp://www.nsnam.org/
documentation/ • 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