15-744: Computer Networking ns-2 TutorialWhat is ns?ns --- what is it good for?nsNs modelsns - software structureotcl and C++: The DualityOutlineTcl basicsSlide 10Basic otclBasic otcl - inheritanceSlide 13Basic structure of ns-scriptsCreating Event Scheduler“Hello World” in nsCreating NetworkRouting + trafficTransport LayerThe transport layer: UDPThe transport layer: TCPSlide 22Application LayerCreating Traffic: On Top of TCPCreating Traffic: On Top of UDPAttaching a traffic sourceTracingMore TracingControlling object parametersPutting it all togethernam – the network animatorns “components”Network Dynamics: Link failuresIssues in SimulationsAnother practical issue: MemorySlide 36Making changes to ns – where???ns directory structureNew component purely in OtclNew component in C++Slide 41How does linkage work?TclObject: Hierarchy and ShadowingTclObjectSlide 45TclObject::bind()TclObject::command()Slide 48Slide 49Slide 50TclObject: Creation and DeletionTclClassClass TclSlide 54Class TclCommandSummaryUseful Resources15-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:Ns 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 DualityOTclC++Pure OTclobjectsPure C++objectsC++/OTcl split objectsnsYour ns-scriptOutline•Overview•Tcl, OTcl basicsTcl, OTcl basics•ns basics•Extending ns•ns internalsTcl 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 greetOutline•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%Creating 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 $nullThe 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 $tcpCreating 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 0Is tracing all links always the best thing to do?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_Controlling object parameters•Almost all ns objects have parameters•ex. Application/Traffic/Exponential has rate and packetSize•set
View Full Document