115-744: Computer Networkingns-2 TutorialBased on slides from John Heidemann, Polly Huang, and Amit ManjhiWhat is ns?• Network simulator• a discrete event simulator• focused on modeling network protocols• wired, wireless, satellite• TCP, UDP, multicast, unicast• Web, telnet, ftp• Ad hoc routing; sensor networks• Infrastructure: stats, tracing, error models etc. ns --- what is it good for?• Evaluate performance of existing network protocols.• Prototyping and evaluation of new protocols.• Large-scale simulations not possible in real experiments.Used to:ns• Event-driven simulator• Model world as events• Simulator has list of events• Process: take next one, run it, until done• Each event happens in instant of virtual time, but takes arbitrary real time• Single thread of control• Packet levelHow does it work:2Ns models• Traffic/applications• CBR, FTP, telnet, web• Routing/Queuing• Drop-tail, FQ, SFQ, RED, DRR• Wired routing, adhoc routing etc• Transport• TCP (variants), UDP, multicast (SRM)ns - software structure• Object oriented (C++, OTcl) – code reuse• Scalability + Extensibility• Control/”data” separation• Split C++/OTcl object• C++ for packet-processing (fast to run)• OTcl for control - (fast to write)• Simulation setup and configurationotcl and C++: The Duality Your ns-scriptOutline• Overview••Tcl, OTcl basicsTcl, OTcl basics• ns basics• Extending ns• ns internals3Tcl basicsproc fact {x} {set ret 1if {$x > 2} {for {set i 1} {$i <= $x} {incr i} {set ret [expr $i * $ret]}}puts “factorial of $x is $ret”}fact 5 factorial of 5 is 120Tcl basicsproc fact {x} {set ret 1if {$x > 2} {for {set i 1} {$i <= $x} {incr i} {set ret [expr $i * $ret]}}puts “factorial of $x is $ret”}fact 5 factorial of 5 is 120• $ for de-referencing• Spaces - important•{} defines a block • set, puts • proc definition:proc name args bodyBasic otclClass mommom instproc init {age} {$self instvar age_set age_ $age}mom instproc greet {} {$self instvar age_puts “$age_ years old mom: How are you doing?”}set a [new mom 45]$a greet• instead of single class declaration multiple definitions• instproc adds class methods• instvar adds instance variable, and brings them to the local scope• $self : this in Java, C++• all methods virtual (as in Java)Basic otcl - inheritanceClass kid -superclass momkid instproc greet {} {$self instvar age_puts “$age_ years old kid: What’s up, dude?”}set b [new kid 15]$b greet4Outline• Overview• Tcl, OTcl basics••ns basicsns basics• Extending ns• ns internalsBasic structure of ns-scripts• Creating the event scheduler• [Tracing]• Creating network topology• Creating Transport Layer - Agents• Creating Applications - Applications• Events!Creating Event Scheduler• Create scheduler• set ns [new Simulator]• Schedule event• $ns at <time> <event>• <event>: any legitimate ns/tcl commands• Start scheduler• $ns run“Hello World” in nssimple.tclset ns [new Simulator]$ns at 1 “puts \“Hello World!\””$ns at 1.5 “exit”$ns runbovik@gs19% ns simple.tclHello World!bovik@gs19%5Creating Network• Nodes• set n0 [$ns node]• set n1 [$ns node]• Links & Queuing• $ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>• Queue type: DropTail, RED, CBQ, FQ, SFQ, DRRRouting + traffic• Unicast• $ns rtproto <type> • <type>: Static, Session, DV• Multicast support also.• Traffic• Simple two layers: transport and application.• Transport: TCP, UDP etc.• Applications: web, ftp, telnet etc. Transport LayerClass AgentAgent/UDP Agent/TCP (=Tahoe)…Other TCP flavorsThe transport layer: 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 $null6The transport layer: 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 $tcpsinkTransport LayerClass AgentAgent/UDP Agent/TCP (=Tahoe)…Other TCP flavorsAgent/TCP/FullTCPApplication LayerClass Application{Simulated Applications}(on top of TCP){Traffic generators}(on top of UDP)Creating Traffic: On Top of TCPFTP• set ftp [new Application/FTP]• $ftp attach-agent $tcp• $ns at <time> “$ftp start”Telnet• set telnet [new Application/Telnet]• $telnet attach-agent $tcp7Creating 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]• Trace driven traffic• Inter-packet time and packet-sizeAttaching a traffic source• set cbr [new Application/Traffic/CBR]• $cbr attach-agent $udp• $ns at <time> “$cbr start”TracingTrace packets on all links:• set f[open out.tr w]• $ns trace-all $f• $ns flush-trace• close $f <event><time><from><to><type><size>--<flags>--<flow id><src><dst><seqno> <pckt id>+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0 More Tracing• Tracing specific links• $ns trace-queue $n0 $n1 $f• Tracing variables • set cwnd_chan_ [open all.cwnd w]• $tcp trace cwnd_• $tcp attach $cwnd_chan_8Controlling object parameters• Almost all ns objects have parameters• ex. Application/Traffic/Exponential has rate and packetSize• set parameters in OTcl• set etraf [new Application/Traffic/Exponential]• $etraf set rate_ 1Mb• $etraf set packetSize_ 1024Putting it all togetherset ns [new Simulator]set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail set tcp [$ns create-connection TCP $n0 TCPSink $n1 0] set ftp [new Application/FTP]$ftp attach-agent $tcp $ns trace-queue $n0 $n1 $f $ns at 0.2 "$ftp start“$ns at 1.2 ”exit“$ns runnam – the network animatorn0 n1set nf [open out.nam w]$ns namtrace-all $nfexec nam
View Full Document