!"! #$ %& ' %&
traffic source agent node link n0 ftp tcp sink 2mbps 10 ms n2 1.7mbps, 20 ms n3 cbr udp n1 2mbps 10 ms null pktsize 1KB, rate 1mbps
#Create a simulator object set ns [new Simulator] $ $ #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf #Define a 'finish' procedure proc finish {} { global ns nf $ns flush-trace #Close the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 }
set ns [new Simulator]" %& $ " ( (%& %! "! " # #! $ $ $ $# % & '
$ $ n0 #Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] 2mbps 10 ms n1 2mbps 10 ms n2 1.7mbps, 20 ms n3 #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10 #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right #Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuepos 0.5
$ns_ simplex-link <node1> <node2> <bw> <delay> <qtype> <args> %!% )*+, $ #!-#. #!! -.$ $ns_ duplex-link <node1> <node2> <bw> <delay> <qtype> <args> %% $ $ns_ simplex-link-op <n1> <n2> <op> <args> %!/$ %% %# $ $ns_ duplex-link-op <n1> <n2> <op> <args> %)! %#,!/$
$ $ #Setup a TCP connection set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP n0 ftp tcp n3 sink
#Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n3 $null $ns connect $udp $null $udp set fid_ 2 $ $ #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false cbr udp n1 pktsize 1KB, rate 1mbps n3 null
$ $ #Schedule events for the CBR and FTP agents $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" #Detach tcp and sink agents (not really necessary) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run
$ns trace-all $fd + 1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610-1.84375 0 2 cbr 210 ------- 0 0.0 3.1 225 610 r 1.84471 2 1 cbr 210 ------- 1 3.0 1.0 195 600 r 1.84566 2 0 ack 40 ------- 2 3.2 0.1 82 602 + 1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611-1.84566 0 2 tcp 1000 ------- 2 0.1 3.2 102 611 r 1.84609 0 2 cbr 210 ------- 0 0.0 3.1 225 610 + 1.84609 2 3 cbr 210 ------- 0 0.0 3.1 225 610 d 1.84609 2 3 cbr 210 ------- 0 0.0 3.1 225 610
" 0 %" " #( $ns_ use-scheduler Heap ) # " * # / / / /1!! 2
!!!!3 %!!!!!! " ( +!,! * -!, (, +! *! p(x)=k*(x m ) k /(x) k+1 (, * ( +. /. " " #!!
4*5!!! %&" 0 0 # 0 0 1 2 #! 0! 4*5!!% 6!" set e [new Application/Traffic/CBR] $e set packetsize_ 48 $e set rate_ 64Kb $e set random_ 1!4*5%&788 /$
9/!!!%&" 0 0 0 12 0 0 12 0 12 9/:!!!!% 6!" set e [new Application/Traffic/Exponential] $e set packetsize_ 210 $e set burst_time_ 500ms $e set idle_time_ 500ms $e set rate_ 100k 9/:!!%! % % %%88 ; %8$
<= <=%&%!4= %&$ $ftp start $ftp produce <n> $ftp stop $ftp attach agent $ftp producemore <count> 4!=" maxpkts
! $ %6!! $ 3 4, ), ( Agent/UDP set packetsize_ 1000 ;# max segment size
4= &!4=" " senders receivers (sink) " # " & 5, " 1 2 5, & 5, 6 " 16 2 5, & 5, % " 6 & 5, 7 " 5, 6 8 5. 9 7 : # & 5, ; " 5, ; & 5, 8 " 6 5, 1 2
4= " & 5, " 5, & 5 < & 5, 4 & " 5, & 5 < & 5, 7 " & 5 < 6 8 5. 9 7 : # & 5, 7 4 & " 7 4 &
4= Agent/TCP set window_ 100 ;# Changes the class variable $tcp set window_ 2.0 ;# Changes window_ for the $tcp object! 5, & 5, # & 5 < 5, & 5 < & 5 < Agent/TCPSink set packetsize_ 40 4 & 5 < 5, Agent/TCPSink/DelAck set interval_ 100ms & 5 < 5, " Agent/TCPSink set maxsackblocks_ 3
5 <" 4 ' 6!! $ >?! % $ns rtproto Static ;# Enable static route strategy for the simulation $ns rtproto Session ;# Enable session routing for this simulation $ns rtproto DV $n1 $n2 $n3 ;# Run DV agents on nodes $n1, $n2, and $n3 $ns rtproto LS $n1 $n2 ;# Run link state routing on specified nodes
'!! $'%! $ $ns cost $n1 $n2 10 ;# set cost of link from $n1 to $n2 to 10 $ns cost $n2 $n1 5 ;# set cost of link in reverse direction to 5 [$ns link $n1 $n2] cost? ;# query cost of link from $n1 to $n2 [$ns link $n2 $n1] cost? ;# query cost of link in reverse direction
5 " 3 4 = ", 8! #file:static-routing.tcl
5 " @A5):=:@A," 4 - "8 4 ; #. # ( changes in the topology received route update #files: session-routing.tcl dv-routing.tcl
5 B 5" $ns rtproto Manual set n1 [$ns node] set n2 [$ns node] $ns duplex-link $n1 $n2 10Mb 100ms DropTail [$n1 get-module "Manual"] add-route-to-adj-node -default $n2 [$n1 get-module "Manual"] add-route-to-adj-node $n2 [$n2 get-module "Manual"] add-route-to-adj-node -default $n1 #file:manual-routing.tcl
@ >?!% $ #<start> <on> <off> <end> $ns rtmodel Exponential 0.5 1.0 1.0 5.0 $n1 ;#apply to node $ns rtmodel Trace dynamics.trc $n2 $n3 ;#apply to link $ns rtmodel Deterministic 20.0 20.0 $node(1) $node(5) >?! B!$!$ $ns rtmodel-at 3.5 up $n0 $ns rtmodel-at 3.9 up $n3 $n5 $ns rtmodel-at 40 down $n4
C! $ns set-address-format hierarchical /. " */. / 7 9 *7 7 *7 7 # $ns set-address-format hierarchical <#n hierarchy levels> <#bits for level1> <#bits for level 2>...<#bits for nth level> The AddrParams class is used to store the topology hierarchy. AddrParams set domain_num_ 2 lappend cluster_num 2 2 AddrParams set cluster_num_ $cluster_num lappend eilastlevel 2 3 2 3 AddrParams set nodes_num_ $eilastlevel This defines a topology with 2 domains, say D1 and D2 with 2 clusters each (C11 & C12 in D1 and C21 & C22 in D2). Then number of nodes in each of these 4 clusters is specified as 2,3,2 and 3 respectively.
+ %!" > >? ' & 5, @ A %!" " set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(ant) Antenna/OmniAntenna ;# Antenna type set val(ll) LL ;# Link layer type set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type set val(ifqlen) 50 ;# max packet in ifq set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type set val(rp) DSDV ;# ad-hoc routing protocol set val(nn) 2 ;# number of mobilenodes
+ + %&!!% %$ set topo [new Topography] B %!D;;ED;;$ + %& /! %)/FD;;FD;;," $topo load_flatgrid 500 500 #file:simple-wireless.tcl
+ / %&3!" create-god $val(nn) 3)3@," ) the next hop information is normally loaded into god object from movement pattern files.
+ +!" # $ns_ node-config -addressingtype flat or hierarchical or expanded # -adhocrouting DSDV or DSR or TORA # -lltype LL # -mactype Mac/802_11 # -proptype "Propagation/TwoRayGround" # -ifqtype "Queue/DropTail/PriQueue" # -ifqlen 50 # -phytype "Phy/WirelessPhy" # -anttype "Antenna/OmniAntenna" # -channeltype "Channel/WirelessChannel" # -topoinstance $topo # -energymodel "EnergyModel" # -initialenergy (in Joules) # -rxpower (in W) # -txpower (in W) # -agenttrace ON or OFF # -routertrace ON or OFF # -mactrace ON or OFF # -movementtrace ON or OFF
+ # Create nodes: for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node ] $node_($i) random-motion 0;# disable random motion } # Provide initial (X,Y,Z) co-ordinates for node_(0) and node_(1) $node_(0) set X_ 5.0 $node_(0) set Y_ 2.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 390.0 $node_(1) set Y_ 385.0 $node_(1) set Z_ 0.0
+ # Produce some node movements # # Node_(1) starts to move towards node_(0) # command: $node setdest <x2> <y2> <speed> $ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0" $ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0" # Node_(1) then starts to move away from node_(0) $ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
+ # Tell nodes when the simulation ends # for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at 150.0 "$node_($i) reset"; } $ns_ at 150.0001 "stop" $ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt" #for # Tell {set nodes i 0} when {$i < the $val(nn) simulation } {incr ends $ns_ at 150.0 "$node_($i) reset"; i} { }$ns_ $ns_ proc at 150.0001 150.0002 "stop" global stop {} ns_ { tracefd "puts \"NS EXITING...\" ; $ns_ halt" }At time close 150.0s, $tracefd later And finally after resetting the command the simulation the to nodes. start shall The the procedure simulation, stop. The stop{} nodes are is called reset at to that flush time out and traces the and "$ns_ close halt" the is trace called file. at 150.0002s, a little puts $ns_ "Starting run Simulation..." proc stop {} { global ns_ tracefd close $tracefd } puts "Starting Simulation..." $ns_ run
+ MAC r 160.093884945 _6_ RTR --- 5 tcp 1492 [a2 4 6 800] ------- [65536:0 16777984:0 31 16777984] [1 0] 2 0 NETWORK TRANSPORT TCP packet received by routing agent of node 6 at time 160.09... UID: 5 size: 1492 Mac Layer fields: time src_addr dst_addr type (800: IP, 806: ARP) Network Layer fields: src_addr dst_addr TTL next_hop_addr Transport Layer fields: seq_no ack_no no_of_fwds opt_no_of_fwds
+< %% ns use-newtrace %! ", @ A
+ 9%!" set namtrace [open wireless1-out.nam w] $ns_ namtrace-all-wireless $namtrace $val(x) $val(y) "+ %B 4 / #file:wireless1.tcl
4!! % cd <ns_dir>/indep-utils/cmu-scen-gen/setdest./setdest [-v version of setdest] [-n num_of_nodes] [-p pausetime] [-s maxspeed] [-t simtime] [-x maxx] [-y maxy] > [outdir/movement-file]
4!! % 9/" setdest -n 5 -p 200 -s 0.1 -t 180 -x 50 -y 50 > nodes.tcl.in % ( B
+ wireless node WL0 wired node base station BS0 link 5mbps 2 ms W0 5.0mbps, 2 ms W1 BS1 5.0mbps 2 ms WL1 WL2 #file:wireless2.tcl
+ Domain 1 1.0.1 n0 1.0.0 Use hierarchical routing 3 domains BS0 0.0.0 0.0.1 2.0.1 2.0.0 BS1 W0 W1 Domain 0 n1 2.0.2 Domain 2 n2
9! % $! %! $ 9$ '!!! )/=/=== =,
9 + 6 %%!" $ns_ node-config... -energymodel $opt(energymodel) \ #EnergyModel -idlepower 1.0 \ #watts -rxpower 1.0 \ #watts -txpower 2.0 \ #watts -sleeppower 0.001 \ #watts -transitionpower 0.2 \ #watts -transitiontime 0.005 \ #seconds -initialenergy $opt(initialenergy) #joules
9 9/" -8.::/:$ "::$$::!::B $ " GHIH$HI;;;;$;I0;$;;;;$;;J;$;;KL " "'@9 " 99= " "