Network Simulator 2(NS2) Yingyue Xu 4/7/2015 Overview: The Network simulator (NS): discrete event simulator for networks. supports wired, wireless, and satellite networks. various routing and multicast protocols. written in C++ and object-oriented tool command language (OTcl). NS output is a file(s) which contains all packet information. It can be viewed using network animator (NAM), xgraph, or processed with custom scripts (Perl, Tcl or Awk ). User view of NS: Why two languages: C++ Fast in running. Slow and hard to modify (need to recompile the whole NS ). Used to implement detailed protocols and algorithms, to manipulate bytes since it can run over large number of data OTcl Slow in running Can be quickly changed (no need to recompile). Used to implement the simulation configuration like # of nodes, simulation topology and the dimensions of test area. That’s why NS object called split object! NS via Tclcl (Tcl with classes) provides glue to make objects and variables appear on both languages. Ns functionalities Wired world Wireless Routing DV, LS, PIM-SM Transportation: TCP and UDP Traffic sources:web, ftp, telnet, cbr, stochastic Queuing disciplines:drop-tail, RED, FQ, SFQ, DRR QoS: IntServ and Diffserv Emulation Ad hoc routing and mobile IP Directed diffusion, sensor-MAC Tracing, visualization, various utilities Ns Models Traffic models and applications: Transport protocols: unicast: TCP (Reno, Vegas, etc.), UDP Multicast: SRM Routing and queueing: Web, FTP, telnet, constant-bit rate, real audio Wired routing, ad hoc rtg and directed diffusion queueing protocols: RED, drop-tail, etc Physical media: Wired (point-to-point, LANs), wireless (multiple propagation models), satellite NS installation: NS can work on most UNIX platforms, and also on windows platform. Detailed info for downloading and building NS: http://www.isi.edu/nsnam/ns/ns-build.html Easier way to download all in one package and build NS (this will not work on Windows), this package contains Tcl, Tclcl, OTcl, Tk, and NS source codes. You need to run install from UNIX shell (Bash or C shell). Basic Tcl variables: set x 10 puts “x is $x” functions and expressions: set y [pow x 2] set y [expr x*x] control flow: if {$x > 0} { return $x } else { return [expr -$x] } while { $x > 0 } { puts $x incr x –1 } procedures: proc pow {x n} { if {$n == 1} { return $x } set part [pow x [expr $n-1]] return [expr $x*$part] } Also lists, associative arrays, etc. => can use a real programming language to build network topologies, traffic models, etc. Basic otcl Class Person # constructor: Person instproc init {age} { $self instvar age_ set age_ $age } # method: Person instproc greet {} { $self instvar age_ puts “$age_ years old: How are you doing?” } # subclass: Class Kid -superclass Person Kid instproc greet {} { $self instvar age_ puts “$age_ years old kid: What’s up, dude?” } set a [new Person 45] set b [new Kid 15] $a greet $b greet => can easily make variations of existing things (TCP, TCP/Reno) Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data Creating Event Scheduler Create event scheduler set ns [new Simulator] Schedule events $ns at <time> <event> <event>: any legitimate ns/tcl commands $ns at 5.0 “finish” Start scheduler $ns run Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data Tracing and Monitoring 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 Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data 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 Creating Network: LAN $ns make-lan <node_list> <bandwidth> <delay> <ll_type> <ifq_type> <mac_type> <channel_type> <ll_type>: LL <ifq_type>: Queue/DropTail, <mac_type>: MAC/802_3 <channel_type>: Channel Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data Setup Routing Unicast $ns rtproto <type> <type>: Static, Session, DV, cost, multi-path Multicast $ns multicast (right after [new Simulator]) $ns mrtproto <type> <type>: CtrMcast, DM, ST, BST Other types of routing supported: source routing, hierarchical routing Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data Inserting 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 Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Creating Connection and Traffic UDP CBR set udp [new Agent/UDP] set src [new set null [new Agent/Null] Application/Traffic/CBR] $ns attach-agent $n0 $udp $ns attach-agent $n1 $null $ns connect $udp $null Creating Connection and Traffic II 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 FTP set ftp [new Application/FTP] $ftp attach-agent $tcp Telnet set telnet [new Application/Telnet] $telnet attach-agent $tcp 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 Plumbing: Packet Flow n0 n1 Port Classifier Addr Classifier entry_ 0 1 0 Application/FTP dst_=1.0 Port Classifier Addr Classifier Agent/TCP Link n0-n1 entry_ Link n1-n0 1 0 0 dst_=0.0 Agent/TCPSink Getting started with nam Turn on nam tracing in your Tcl script As easy as turning on normal tracing $ns namtrace $file Specify link orientation (or node position for wireless) $ns duplex-link-op $node1 $node2 orient left Execute nam exec nam $filename Advanced nam capabilities Node options — color, shape, label $node $node $node $node $node color red shape hexagon label “my text” label-color blue label-at up Link options $ns $ns $ns $ns $ns duplex-link-op duplex-link-op duplex-link-op duplex-link-op duplex-link-op $n1 $n2 color green queuePos right $n1 $n2 label “my text” $n1 $n2 label-color blue $n1 $n2 label-at down Advanced nam capabilities Packet colors $ns color $n blue $agent set fid_ $n Annotation $ns at $time “$ns trace-annotate $text” Control playback $ns set-animation-rate 3ms The nam user interface 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 available The nam editor Wireless model Mobilenode at core of mobility model Mobilenodes can move in a given topology, receive/transmit signals from/to wireless channels Wireless network stack consists of LL, ARP, MAC, IFQ etc Allows simulations of multi-hop ad hoc networks, wireless LANs, sensor networks etc An Example – Step 1 # Define Global Variables # create simulator set ns [new Simulator] # create a flat topology in a 670m x 670m area set topo [new Topography] $topo load_flatgrid 670 670 An Example – Step 2 # Define standard ns/nam trace # ns trace set tracefd [open demo.tr w] $ns trace-all $tracefd # nam trace set namtrace [open demo.nam w] $ns namtrace-all-wireless $namtrace 670 670 GOD (General Operations Director) Stores smallest number of hops from one node to another Optimal case to compare routing protocol performance Automatically generated by scenario file set god [create-god <no of mnodes>] $god set-dist <from> <to> <#hops> Example –Step 3 Create God set god [create-god 3] $ns at 900.00 “$god setdist 2 3 1” # An Example – Step 4 Define how a mobile node is configured $ns node-config \ -adhocRouting DSDV \ -llType LL \ -macType Mac/802_11 \ -ifqLen 50 \ -ifqType Queue/DropTail/PriQueue \ -antType Antenna/OmniAntenna \ -propType Propagation/TwoRayGround \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \ -topoInstance $topo -agentTrace ON \ -routerTrace OFF \ -macTrace OFF An Example – Step 5 # Next create a mobile node, attach it to the channel set node(0) [$ns node] # disable random motion $node(0) random-motion 0 # Use “for” loop to create 3 nodes: for {set i < 0} {$i < 3} {incr i} { set node($i) [$ns node] $node($i) random-motion 0 } Mobilenode Movement Node position defined in a 3-D model However z axis not used $node set X_ <x1> $node set Y_ <y1> $node set Z_ <z1> $node at $time setdest <x2> <y2> <speed> Node movement may be logged Scenario Generator: Movement Mobile Movement Generator setdest -n <num_of_nodes> -p pausetime -s <maxspeed> -t <simtime> -x <maxx> -y <maxy> Source: ns-2/indep-utils/cmu-scengen/setdest/ 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 traffic ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections] [-rate rate] TCP traffic ns tcpgen.tcl [-nn nodes] [-seed seed] Source: ns-2/indep-utils/cmu-scengen/ 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> # Define traffic model source <traffic-scenario-files> An Example – Step 7 # Tell ns/nam the simulation stop time $ns at 200.0 “$ns nam-end-wireless 200.0” $ns at 200.0 “$ns halt” # Start your simulation $ns run nam Visualization Replace $ns namtrace-all $fd with $ns namtrace-all-wireless $fd At the end of simulation, do $ns nam-end-wireless [$ns now] 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 Useful links: Tutorials: http://www.isi.edu/nsnam/ns/tutorial/index.html http://nile.wpi.edu/NS/ NS2-Manual http://www.isi.edu/nsnam/ns/ns-man.html Network Animator (NAM ): http://www.isi.edu/nsnam/nam/ OTcl Tutorial: http://bmrc.berkeley.edu/research/cmt/cmtdoc/otcl Help and Resources Ns and nam build questions http://www.isi.edu/nsnam/ns/ns-build.html Ns mailing list: ns-users@isi.edu TCL: http://dev.scriptics.com/scripting Otcl tutorial (in distribution): ftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/tutorial.html