Ns 2 Network Simulator An Introduction

19,532 views

Published on

Ns 2 Network Simulator An Introduction

Published in: Education, Technology
1 Comment
9 Likes
Statistics
Notes
  • Nice !! Download 100 % Free Ebooks, PPts, Study Notes, Novels, etc @ https://www.ThesisScientist.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
19,532
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
988
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

Ns 2 Network Simulator An Introduction

  1. 1. Ns-2 Network Simulator An Introduction
  2. 2. Introduction[1] <ul><li>Ns-2 is a discrete event simulator targeted at network research </li></ul><ul><li>Focused on modeling network protocols </li></ul><ul><ul><li>wired, wireless, satellite </li></ul></ul><ul><ul><li>TCP, UDP, multicast, unicast </li></ul></ul><ul><ul><li>web, telnet, ftp </li></ul></ul><ul><ul><li>ad hoc routing, sensor networks </li></ul></ul>
  3. 3. <ul><li>Ns-2 is a discrete event driven simulation </li></ul><ul><ul><li>Physical activities are translated to events </li></ul></ul><ul><ul><li>Events are queued and processed in the order of their scheduled occurrences </li></ul></ul><ul><ul><li>Time progresses as the events are processed </li></ul></ul>Introduction[2] 1 2 Time: 1.5 sec Time: 1.7 sec Time: 1.8 sec Time: 2.0 sec
  4. 4. Components of ns <ul><li>ns, the simulator itself </li></ul><ul><li>nam, the Network Animator </li></ul><ul><ul><li>visualize ns (or other) output </li></ul></ul><ul><ul><li>GUI input simple ns scenarios </li></ul></ul><ul><li>Pre-processing: </li></ul><ul><ul><li>traffic and topology generators </li></ul></ul><ul><li>Post-processing: </li></ul><ul><ul><li>simple trace analysis, often in Awk, Perl, or Tcl </li></ul></ul>
  5. 5. ns Software Structure: C++ and Otcl <ul><li>Uses two languages </li></ul><ul><li>C++ for packet-processing </li></ul><ul><ul><li>fast to run, detailed, complete control </li></ul></ul><ul><li>OTcl for control </li></ul><ul><ul><li>simulation setup, configuration, occasional actions </li></ul></ul><ul><ul><li>fast to write and change </li></ul></ul><ul><li>pros: trade-off running vs. writing speed, powerful/documented config language </li></ul><ul><li>cons: two languages to learn and debug in </li></ul>
  6. 6. Event Driven Simulation Event Queue Simulation Finished! 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
  7. 7. Simplified User’s View of NS-2
  8. 8. NS-2 programming <ul><li>Create the event scheduler </li></ul><ul><li>Turn on tracing </li></ul><ul><li>Creating network </li></ul><ul><ul><li>Computing routes and Setup routing - rtproto </li></ul></ul><ul><ul><li>Creating transport connections – Agents </li></ul></ul><ul><ul><li>Creating traffic – Applications </li></ul></ul><ul><ul><ul><li>Transmit application-level data </li></ul></ul></ul><ul><li>Monitoring </li></ul>
  9. 9. Creating Event Scheduler <ul><li>Create event scheduler </li></ul><ul><ul><li>set ns [new Simulator] </li></ul></ul><ul><li>Schedule events </li></ul><ul><ul><li>$ns at <time> “<event>” </li></ul></ul><ul><ul><li><event>: any legitimate ns/tcl commands </li></ul></ul><ul><li>Start scheduler </li></ul><ul><ul><li>$ns run </li></ul></ul>
  10. 10. Tracing <ul><li>Trace packets on all links </li></ul><ul><li>#Open the NAM trace file #Open the Trace file </li></ul><ul><ul><li>set nf [open out.nam w] set tf [open out.tr w] </li></ul></ul><ul><ul><li>$ns namtrace-all $nf $ns trace-all $tf </li></ul></ul><ul><li>Must appear immediately after creating scheduler </li></ul><ul><li>Turn on tracing on specific links </li></ul><ul><ul><li>$ns trace-queue $n0 $n1 </li></ul></ul><ul><ul><li>$ns namtrace-queue $n0 $n1 </li></ul></ul>
  11. 11. Creating Network <ul><li>Nodes </li></ul><ul><ul><li>set n0 [$ns node] </li></ul></ul><ul><ul><li>set n1 [$ns node] </li></ul></ul>n0 n1
  12. 12. Creating Network <ul><li>Links and queuing </li></ul><ul><ul><li>$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type> </li></ul></ul><ul><ul><li><bandwidth>: 1000b, 1kb, 0.001Mb, … </li></ul></ul><ul><ul><li><delay>: 1ms, 0.001s, … </li></ul></ul><ul><ul><li><queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR. </li></ul></ul>n0 n1
  13. 13. Creating Connection – Agents <ul><li>UDP </li></ul><ul><ul><li>set src [new Agent/UDP] </li></ul></ul><ul><ul><li>set rcv [new Agent/Null] </li></ul></ul><ul><ul><li>$ns connect $src $rcv </li></ul></ul><ul><li>TCP </li></ul><ul><ul><li>set tcp [new Agent/TCP] </li></ul></ul><ul><ul><li>set tcpsink [new Agent/TCPSink] </li></ul></ul><ul><ul><li>$ns connect $tcp $tcpsink </li></ul></ul>
  14. 14. Creating Connection – Agents (Contd) <ul><li>TCP  TCPsink </li></ul><ul><ul><li>one-way Tahoe implementation </li></ul></ul><ul><ul><li>other flavors: TCP/Reno, TCP/NewReno, TCP/Sack1, </li></ul></ul><ul><ul><li>TCP/Vegas </li></ul></ul><ul><ul><li>two-way: use FullTCP at both sides </li></ul></ul><ul><li>TCP-friendly connectionless protocols: </li></ul><ul><ul><li>RAP <-> RAP Rate Adaptation Protocol (rate-based AIMD + Slow Start) </li></ul></ul><ul><ul><li>TFRC <-> TFRCSink </li></ul></ul><ul><ul><li>TCP Friendly Rate Control protocol (based on TCP throughput-equation) </li></ul></ul>
  15. 15. Creating Traffic – Applications <ul><li>Use app on Top of agent: </li></ul><ul><li>$app start </li></ul><ul><li>$app stop </li></ul><ul><li>FTP </li></ul><ul><ul><li>set ftp [new Application/FTP] </li></ul></ul><ul><ul><li>$ftp attach-agent $tcp </li></ul></ul><ul><li>Telnet </li></ul><ul><ul><li>set telnet [new Application/Telnet] </li></ul></ul><ul><li>Web </li></ul><ul><ul><li>set session [new httpSession $ns <numPages> <clientNode>] </li></ul></ul><ul><li>Traffic generator </li></ul><ul><ul><li>set cbr [new Application/traffic/cbr] </li></ul></ul>
  16. 16. Computing Routes <ul><li>Unicast </li></ul><ul><ul><li>$ns rtproto <type> </li></ul></ul><ul><ul><li><type>: Static, Session, DV, cost, multipath </li></ul></ul><ul><li>Multicast </li></ul><ul><ul><li>Simulator set EnableMcast_ 1 </li></ul></ul><ul><ul><li>Simulator set NumberInterfaces_ 1 </li></ul></ul><ul><ul><li>$ns mrtproto <type> </li></ul></ul><ul><ul><li><type>: CtrMcast, DM, dynamicDM, pimDM </li></ul></ul>
  17. 17. Example 1: TCP (FTP from n0 to n1) set ns [new Simulator] #Create 2 nodes set n0 [$ns node] set n1 [$ns node] set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 0.2 &quot;$ftp start&quot; $ns at 1.2 ”exit&quot; $ns run $ns duplex-link $n0 $n1 1Mb 10ms DropTail set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns attach-agent $n0 $tcp $ns attach-agent $n1 $tcpsink $ns connect $tcp $tcpsink n0 n1
  18. 18. Generic Script Structure <ul><li>set ns [new Simulator] </li></ul><ul><li># [Turn on tracing] </li></ul><ul><li># Create topology </li></ul><ul><li># Setup packet loss, link dynamics </li></ul><ul><li># Create routing agents </li></ul><ul><li># Create: </li></ul><ul><li># - multicast groups </li></ul><ul><li># - protocol agents </li></ul><ul><li># - application and/or setup traffic sources </li></ul><ul><li># Post-processing procs </li></ul><ul><li># Start simulation </li></ul>
  19. 19. Screenshots
  20. 20. Tcl Basics <ul><li>Variable substitution </li></ul><ul><ul><li>i: the character ‘i’. </li></ul></ul><ul><ul><li>$i: the variable i. </li></ul></ul><ul><li>Arithmetic Expressions </li></ul><ul><ul><li>set value [ expr $v1 + $v2] </li></ul></ul><ul><li>Operation substitution </li></ul><ul><ul><li>set i [expr 5 + 6] 11 </li></ul></ul><ul><li>Printing </li></ul><ul><ul><li>puts $filename “string” (default is stdout) </li></ul></ul><ul><li>Control Structures </li></ul><ul><ul><li>if {condition} then {…….} </li></ul></ul><ul><ul><li>for {set i 0} {$i < 10} {incr i 2} {……} </li></ul></ul><ul><li>Procedures </li></ul><ul><ul><li>proc proc_name {arg1 arg2…} { ……} </li></ul></ul>
  21. 21. NS Models <ul><li>Traffic models and Applications: </li></ul><ul><ul><li>Web, FTP, Telnet, constant-bit rate(CBR), real audio </li></ul></ul><ul><li>Transport protocols: </li></ul><ul><ul><li>unicast: TCP, UDP </li></ul></ul><ul><ul><li>Multicast: SRM, CtrMcast, DM, dynamicDM, pimDM </li></ul></ul><ul><li>Routing and queuing: </li></ul><ul><ul><li>Wired routing, ad hoc routing and directed diffusion </li></ul></ul><ul><ul><li>queuing protocols: RED, drop-tail, etc </li></ul></ul><ul><li>Physical media: </li></ul><ul><ul><li>Wired (point-to-point, LANs), </li></ul></ul><ul><ul><li>wireless (multiple propagation models), </li></ul></ul><ul><ul><li>satellite </li></ul></ul>
  22. 22. Example 1 (contd.) – Basic tracing #Create a simulator object set ns [new Simulator] #Open the nam trace file set nf [open out.nam w] # Start tracing $ns namtrace-all $nf #Open the general trace file set tf [open out.tr w] # Start tracing $ns trace-all $tf #Define a 'finish' procedure proc finish {} { global ns nf tf $ns flush-trace #Close the trace files close $nf close $tf #Execute nam on the trace file exec nam out.nam & exit 0 } # Say when to stop simulation $ns at 5.0 &quot;finish“ #Run the simulation $ns run
  23. 23. Example 2 – Routing (UDP) <ul><li>#Create a simulator object </li></ul><ul><li>#Define different colors for data flows </li></ul><ul><li>$ns color 1 Blue </li></ul><ul><li>$ns color 2 Red </li></ul><ul><li>#Open the nam trace file </li></ul><ul><li># as in example 1 </li></ul><ul><li>#Define a 'finish' procedure </li></ul><ul><li># as in example 1 </li></ul><ul><li>#Create four nodes </li></ul><ul><li>set n0 [$ns node] </li></ul><ul><li>… … </li></ul><ul><li>set n3 [$ns node] </li></ul><ul><li>#Create links between the nodes </li></ul><ul><li># only one sample line as in ex 1 </li></ul><ul><li># Set orientation of the links </li></ul><ul><li>$ns duplex-link-op $n0 $n2 orient right-down </li></ul><ul><li>… </li></ul><ul><li>#Monitor (Visualize) the queue for the </li></ul><ul><li># link between node 2 and node 3 </li></ul><ul><li>$ns duplex-link-op $n2 $n3 queuePos 0.5 </li></ul>n0 n1 n2 n3 cbr0 cbr1 null0 UDP UDP UDP
  24. 24. Example 2 – Routing (Contd) <ul><li>#Create a UDP agent and attach it to node n1 </li></ul><ul><li>set udp0 [new Agent/UDP] </li></ul><ul><li>$udp0 set class_ 1 </li></ul><ul><li>$ns attach-agent $n0 $udp0 </li></ul><ul><li># Create a CBR traffic source and attach it to udp0 </li></ul><ul><li>set cbr0 [new Application/Traffic/CBR] </li></ul><ul><li>$cbr0 set packetSize_ 500 </li></ul><ul><li>$cbr0 set interval_ 0.005 </li></ul><ul><li>$cbr0 attach-agent $udp0 </li></ul><ul><li># Create a CBR traffic source cbr1 and udp1 and attach cbr1 to udp1 </li></ul><ul><li>…… </li></ul><ul><li>#Create a Null agent (a traffic sink) and attach it to node n3 </li></ul><ul><li>set null0 [new Agent/Null] </li></ul><ul><li>$ns attach-agent $n3 $null0 </li></ul><ul><li>#Connect the traffic sources with the traffic sink </li></ul><ul><li>$ns connect $udp0 $null0 </li></ul><ul><li>$ns connect $udp1 $null0 </li></ul><ul><li>#Schedule events for the CBR agents </li></ul><ul><li>$ns at 0.5 &quot;$cbr0 start&quot; </li></ul><ul><li>$ns at 1.0 &quot;$cbr1 start&quot; </li></ul><ul><li>$ns at 4.0 &quot;$cbr1 stop&quot; </li></ul><ul><li>$ns at 4.5 &quot;$cbr0 stop&quot; </li></ul><ul><li>#Call the finish procedure after 5 seconds of simulation time </li></ul><ul><li>$ns at 5.0 &quot;finish&quot; </li></ul><ul><li>#Run the simulation </li></ul><ul><li>$ns run </li></ul>
  25. 25. Example 3: Ring topology with link failure <ul><li>#Create a simulator object </li></ul><ul><li>#Tell the simulator to use dynamic routing </li></ul><ul><li>$ns rtproto DV </li></ul><ul><li>#Open the nam trace file </li></ul><ul><li># as in example 1 </li></ul><ul><li>#Define a 'finish' procedure </li></ul><ul><li># as in example 1 </li></ul><ul><li>#Create seven nodes in a loop </li></ul><ul><li>for {set i 0} {$i < 7} {incr i} { </li></ul><ul><li>set n($i) [$ns node] </li></ul><ul><li>} </li></ul><ul><li>#Create links between the nodes </li></ul><ul><li>for {set i 0} {$i < 7} {incr i} { </li></ul><ul><li>$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail </li></ul><ul><li>} </li></ul><ul><li>#Create a UDP agent udp0 and attach it to node n(0) </li></ul><ul><li>set udp0 [new Agent/UDP] </li></ul><ul><li>$ns attach-agent $n(0) $udp0 </li></ul>
  26. 26. Example 3 (Contd.) – link failure <ul><li># Create a CBR traffic source and attach it to udp0 </li></ul><ul><li>…… </li></ul><ul><li>#Create a Null agent (a traffic sink) and attach it to node n(3) </li></ul><ul><li>#Connect the traffic source with the traffic sink </li></ul><ul><li>#Schedule events for the CBR agent and the network dynamics </li></ul><ul><li>$ns at 0.5 &quot;$cbr0 start“ </li></ul>$ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2) $ns at 4.5 &quot;$cbr0 stop“ #Call the finish procedure after 5 #seconds of simulation time $ns at 5.0 &quot;finish&quot; #Run the simulation $ns run
  27. 27. How to Start ns? <ul><li>1. Open terminal window in linux </li></ul><ul><li>2. To run ns type </li></ul><ul><ul><li>$ ns file1.tcl </li></ul></ul><ul><li>Ns2 can be accessed from 1101 IPC Lab. </li></ul><ul><li>Note: Wednesday 14/10/08 at 5:00 PM a practice lab session will be conducted in IPC @ 1101 </li></ul>

×