Ns2 by khan

  • View

  • Download

Embed Size (px)


Easy way to learn ns2

Text of Ns2 by khan

  • 1. Introduction To NS-2Introduction To NS-2 By Arshad Ahmad Khan Mohammad Part Time Research Scholar (CNIS) KL University, Andhra Pradesh India, .

2. Outline Of PresentationOutline Of Presentation IntroductionIntroduction InstallationInstallation ArchitectureArchitecture Tcl & Otcl ExamplesTcl & Otcl Examples Simulation StepsSimulation Steps Examples (Wired)Examples (Wired) Examples (Wireless)Examples (Wireless) 3. IntroductionIntroduction About NS2About NS2 Advantages of NS2Advantages of NS2 NS2 Supported Protocols(Wired &NS2 Supported Protocols(Wired & Wireless)Wireless) 4. Arshad KhanArshad Khan 44 Network Simulation *Network Simulation * Motivation:Motivation: Learn fundamentalsLearn fundamentals of evaluatingof evaluating networknetwork performance viaperformance via simulationsimulation Overview:Overview: fundamentals offundamentals of discrete eventdiscrete event simulationsimulation ns-2 simulationns-2 simulation 5. Arshad KhanArshad Khan 55 Why Simulation? *Why Simulation? * real-system notreal-system not available, is complex/costly oravailable, is complex/costly or dangerousdangerous ((eg: space simulations, flighteg: space simulations, flight simulations)simulations) quickly evaluate designquickly evaluate design alternativesalternatives (eg:(eg: different system configurations)different system configurations) evaluateevaluate complex functionscomplex functions for which closedfor which closed form formulas or numerical techniques notform formulas or numerical techniques not availableavailable 6. Arshad KhanArshad Khan 66 Simulation: advantages/drawbacks*Simulation: advantages/drawbacks* advantages:advantages: sometimes cheapersometimes cheaper find bugs (in design) in advancefind bugs (in design) in advance generality:generality: over analytic/numerical techniquesover analytic/numerical techniques detail:detail: can simulate system details at arbitrarycan simulate system details at arbitrary levellevel drawbacks:drawbacks: caution: does model reflect realitycaution: does model reflect reality large scale systems: lots of resources tolarge scale systems: lots of resources to simulate (especially accurately simulate)simulate (especially accurately simulate) may be slow (computationally expensive 1may be slow (computationally expensive 1 min real time could be hours of simulated time)min real time could be hours of simulated time) art: determining right level of model complexityart: determining right level of model complexity statistical uncertainty in resultsstatistical uncertainty in results 7. OverviewOverview Ns2 is event simulator where the advanceNs2 is event simulator where the advance of time depends on the timing of eventsof time depends on the timing of events which are maintained by a scheduler.which are maintained by a scheduler. Event an object in C++ hierarchy.Event an object in C++ hierarchy. unique ID, Scheduled time,unique ID, Scheduled time, pointer to an objectpointer to an object Scheduler maintains ordered dataScheduler maintains ordered data structure with the events to be executedstructure with the events to be executed and fires them one by one, invoking theand fires them one by one, invoking the handler of the event.handler of the event. 8. Arshad KhanArshad Khan Discrete event simulatorDiscrete event simulator ns-2 is an discrete event driven simulationns-2 is an discrete event driven simulation Physical activities are translated to eventsPhysical activities are translated to events Events are queued and processed in the order of theirEvents are queued and processed in the order of their scheduled occurrencesscheduled occurrences Time progresses as the events are processedTime progresses as the events are processed 1 2 Time: 1.5 sec Time: 1.7 sec Time: 1.8 secTime: 2.0 sec 9. 99 Event Driven SimulationEvent Driven Simulation TX Pkt Event @ 1.5sec Node 1 Module Node 2 Module TX Pkt Event @ 1.5sec RX Pkt Event @ 1.7sec RX Pkt Event @ 1.7sec TX Ack Event @ 1.8sec TX Ack Event @ 1.8sec RX Ack Event @ 2.0sec RX Ack Event @ 2.0sec Event Queue Simulation Finished! 10. 1010 Discrete Event ExamplesDiscrete Event Examples Consider two nodes on an Ethernet: A B simple queuing model: t=1, A enqueues pkt on LAN t=1.01, LAN dequeues pkt and triggers B detailed CSMA/CD model: t=1.0: A sends pkt to NIC As NIC starts carrier sense t=1.005: As NIC concludes cs, starts tx t=1.006: Bs NIC begins reciving pkt t=1.01: Bs NIC concludes pkt Bs NIC passes pkt to app 11. 1111 ns-2 Environmentns-2 Environment Simulation Scenario Tcl Script C++ Implementation 1 2 set ns_ [new Simulator] set node_(0) [$ns_ node] set node_(1) [$ns_ node] class MobileNode : public Node { friend class PositionHandler; public: MobileNode(); } 12. 1212 LanguagesLanguages C++ forC++ for datadata Per-packet processing, the core ofPer-packet processing, the core of nsns Fast to run, detailed, complete controlFast to run, detailed, complete control OTcl forOTcl for controlcontrol Simulation descriptionSimulation description Periodic or triggered actionsPeriodic or triggered actions Manipulating existing C++ objectsManipulating existing C++ objects Faster to write and changeFaster to write and change (a matter of opinion)(a matter of opinion) 13. Working with NSWorking with NS NSNS an OTcl interpreter with network librariesan OTcl interpreter with network libraries InvocationInvocation Interactive modeInteractive mode nsns % (This is the ns prom)% (This is the ns prom) Batch modeBatch mode ns tclfile.tclns tclfile.tcl 14. Hello World Example (Interactive)Hello World Example (Interactive) nsns % puts "Hello World!!!"% puts "Hello World!!!" Hello World!!!Hello World!!! % puts "Hello NS newbie"% puts "Hello NS newbie" Hello NS newbieHello NS newbie %exit%exit 15. Hello World Example (Batch)Hello World Example (Batch) Hello.tclHello.tcl puts "Hello World!!!"puts "Hello World!!!" puts "Hello NS newbie"puts "Hello NS newbie" InvocationInvocation ns hello.tclns hello.tcl "Hello World!!!"Hello World!!! "Hello NS newbie""Hello NS newbie" 16. OTclOTcl NS is an interpreter of OtclNS is an interpreter of Otcl We communicate with NS by OtclWe communicate with NS by Otcl We need to know how to use OTclWe need to know how to use OTcl Its syntaxIts syntax Basic commandsBasic commands 17. OTcl BasicsOTcl Basics set: Assign a value to a variableset: Assign a value to a variable $x: The value of variable x$x: The value of variable x [ ]: Run command and return result[ ]: Run command and return result [expr ]: Calculate the value of expression[expr ]: Calculate the value of expression set x $a: Assign value of a to variable xset x $a: Assign value of a to variable x puts : print outputs : print out proc: Define a procedureproc: Define a procedure gets stdin x: Read variable x from stdingets stdin x: Read variable x from stdin Be careful about spacesBe careful about spaces 18. OTcl: Example 1OTcl: Example 1 puts "Enter 1th operand"puts "Enter 1th operand" gets stdin agets stdin a puts "Enter 2th operand"puts "Enter 2th operand" gets stdin bgets stdin b set sum [expr $a + $b]set sum [expr $a + $b] set diff [expr $a - $b]set diff [expr $a - $b] puts "$a + $b = $sum"puts "$a + $b = $sum" puts "$a - $b = $diff"puts "$a - $b = $diff" 19. 1919 Basic TclBasic Tcl # Variables:# Variables: setset x 10x 10 setset xx putsputs x is $xx is $x # Functions and expressions:# Functions and expressions: setset y [pow x 2]y [pow x 2] setset y [expr x*x]y [expr x*x] # Control flow:# Control flow: ifif {$x > 0} {{$x > 0} { returnreturn $x }$x } elseelse {{ returnreturn [expr -$x] }[expr -$x] } whilewhile { $x > 0 } {{ $x > 0 } { putsputs $x$x incrincr x 1x 1 }} forfor {{setset i 0} {$i