Vous êtes sur la page 1sur 6

NS examples for wireless scenario

[ A Simple One | A Complex One | Link Error Example | Back to Network Simulator 2 for Wireless ] This is a simple test scenario for 4x4 grid and 4 flows.
# =================================================== # Author: ZHIBIN WU 06/19/2003 # ==================================================

set set set set

cbr_size 500 cbr_interval 0.002 num_row 4 time_duration 100

set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type set val(ifq) Queue/DropTail/PriQueue ;# interface queue type set val(ll) LL ;# link layer type set val(ant) Antenna/OmniAntenna ;# antenna model set val(ifqlen) 50 ;# max packet in ifq set val(rp) DSDV ;# routing protocol # # Initialize ns # set ns_ [new Simulator] set tracefd [open simple.tr w] $ns_ trace-all $tracefd # set up topography object set topo [new Topography] $topo load_flatgrid 1000 1000 create-god [expr $num_row * $num_row ] $ns_ node-config -adhocRouting $val(rp) -llType $val(ll) \ -macType $val(mac) -ifqType $val(ifq) \ -ifqLen $val(ifqlen) -antType $val(ant) \ -propType $val(prop) -phyType $val(netif) \ -channel [new $val(chan)] -topoInstance $topo \ -agentTrace ON -routerTrace OFF\ -macTrace ON \ -movementTrace OFF for {set i 0} {$i < [expr $num_row*$num_row]} {incr i} { set node_($i) [$ns_ node] } set k 0; while {$k < $num_row } { for {set i 0} {$i < $num_row } {incr i} { set m [expr $i+$k*$num_row]; $node_($m) set X_ [expr $i*240]; $node_($m) set Y_ [expr $k*240+20.0]; $node_($m) set Z_ 0.0 } incr k; }; for {set i 0} {$i < $num_row } {incr i} { set udp_($i) [new Agent/UDP] set null_($i) [new Agent/Null] } $ns_ attach-agent $node_(0) $udp_(0) $ns_ attach-agent $node_(7) $udp_(1) $ns_ attach-agent $node_(2) $udp_(2) $ns_ attach-agent $node_(7) $udp_(3) $ns_ attach-agent $node_(6) $null_(0) $ns_ attach-agent $node_(1) $null_(1) $ns_ attach-agent $node_(8) $null_(2)

$ns_ attach-agent $node_(15) $null_(3) for {set i 0} {$i < $num_row } {incr i} { $ns_ connect $udp_($i) $null_($i) } for {set i 0} {$i < $num_row } {incr i} { set cbr_($i) [new Application/Traffic/CBR] $cbr_($i) set packetSize_ $cbr_size $cbr_($i) set interval_ 0.5 $cbr_($i) attach-agent $udp_($i) } $ns_ at 11.0234 "$cbr_(0) start" $ns_ at 10.4578 "$cbr_(1) start" $ns_ at 12.7184 "$cbr_(2) start" $ns_ at 12.2456 "$cbr_(3) start" # Tell nodes when the simulation ends # for {set i 0} {$i < [expr $num_row*$num_row] } {incr i} { $ns_ at [expr $time_duration +10.0] "$node_($i) reset"; } $ns_ at [expr $time_duration +10.0] "finish" $ns_ at [expr $time_duration +10.01] "puts \"NS Exiting...\"; $ns_ halt" proc finish {} { global ns_ tracefd $ns_ flush-trace close $tracefd } puts "Starting Simulation..." $ns_ run

However, a more professional way to write the test tcl is to following the way in ns examples. For example, it is better to write traffic patten file and mobility file seperately and then source them in the main test tcl. Those files are generated respectively with cbrgen and setdest tools. Those files are ususally stored in "/ns-2.??/tcl/mobility/scene" directory. This is an example for DSDV routing with 50 nodes in a 670*670 m2 area
# ====================================================================== # Default Script Options # ====================================================================== set opt(chan) Channel/WirelessChannel set opt(prop) Propagation/TwoRayGround #set opt(netif) NetIf/SharedMedia set opt(netif) Phy/WirelessPhy #set opt(mac) Mac/802_11 set opt(mac) Mac/802_11 set opt(ifq) Queue/DropTail/PriQueue set opt(ll) LL set opt(ant) Antenna/OmniAntenna set set set set set set set set set set set opt(x) opt(y) opt(cp) opt(sc) opt(ifqlen) opt(nn) opt(seed) opt(stop) opt(tr) opt(rp) opt(lm) 670 ;# X dimension of the topography 670 ;# Y dimension of the topography "../mobility/scene/cbr-50-10-4-512" "../mobility/scene/scen-670x670-50-600-20-0" 50 50 0.0 1000.0 out.tr dsdv "off" ;# max packet in ifq ;# number of nodes ;# ;# ;# ;# simulation time trace file routing protocol script log movement

# ====================================================================== #set AgentTrace #set RouterTrace #set MacTrace LL set mindelay_ ON ON OFF 50us

#useless code, the only way to toggle Mac trace on is to use node-config

LL LL LL LL

set set set set

delay_ bandwidth_ off_prune_ off_CtrMcast_

25us 0 0 0 0 0 0 0 0 0 0 0 1460

;# not used ;# not used ;# not used

Agent/Null set sport_ Agent/Null set dport_ Agent/CBR set sport_ Agent/CBR set dport_ Agent/TCPSink set sport_ Agent/TCPSink set dport_ Agent/TCP set sport_ Agent/TCP set dport_ Agent/TCP set packetSize_

Queue/DropTail/PriQueue set Prefer_Routing_Protocols

# unity gain, omni-directional antennas # set up the antennas to be centered in the node and 1.5 meters above it Antenna/OmniAntenna set X_ 0 Antenna/OmniAntenna set Y_ 0 Antenna/OmniAntenna set Z_ 1.5 Antenna/OmniAntenna set Gt_ 1.0 Antenna/OmniAntenna set Gr_ 1.0 # Initialize the SharedMedia interface with parameters to make # it work like the 914MHz Lucent WaveLAN DSSS radio interface Phy/WirelessPhy set CPThresh_ 10.0 Phy/WirelessPhy set CSThresh_ 1.559e-11 Phy/WirelessPhy set RXThresh_ 3.652e-10 Phy/WirelessPhy set Rb_ 2*1e6 Phy/WirelessPhy set Pt_ 0.2818 Phy/WirelessPhy set freq_ 914e+6 Phy/WirelessPhy set L_ 1.0 # ====================================================================== proc usage { puts puts puts puts puts puts } argv0 } { "Usage: $argv0" "\tmandatory arguments:" "\t\t\[-x MAXX\] \[-y MAXY\]" "\toptional arguments:" "\t\t\[-cp conn pattern\] \[-sc scenario\] \[-nn nodes\]" "\t\t\[-seed seed\] \[-stop sec\] \[-tr tracefile\]\n"

proc getopt {argc argv} { global opt lappend optlist cp nn seed sc stop tr x y for {set i 0} {$i < $argc} {incr i} { set arg [lindex $argv $i] if {[string range $arg 0 0] != "-"} continue set name [string range $arg 1 end] set opt($name) [lindex $argv [expr $i+1]] } }

proc cmu-trace { ttype atype node } { global ns_ tracefd if { $tracefd == "" } { return "" } set T [new CMUTrace/$ttype $atype] $T target [$ns_ set nullAgent_] $T attach $tracefd $T set src_ [$node id]

$T node $node return $T }

proc log-movement {} { global logtimer ns_ ns set ns $ns_ source ../mobility/timer.tcl Class LogTimer -superclass Timer LogTimer instproc timeout {} { global opt node_; for {set i 0} {$i < $opt(nn)} {incr i} { $node_($i) log-movement } $self sched 0.1 } set logtimer [new LogTimer] $logtimer sched 0.1 } # ====================================================================== # Main Program # ====================================================================== getopt $argc $argv # # Source External TCL Scripts # #source ../lib/ns-mobilenode.tcl #if { $opt(rp) != "" } { #source ../mobility/$opt(rp).tcl #} elseif { [catch { set env(NS_PROTO_SCRIPT) } ] == 1 } { #puts "\nenvironment variable NS_PROTO_SCRIPT not set!\n" #exit #} else { #puts "\n*** using script $env(NS_PROTO_SCRIPT)\n\n"; #source $env(NS_PROTO_SCRIPT) #} #source ../lib/ns-cmutrace.tcl source ../lib/ns-bsnode.tcl source ../mobility/com.tcl # do the get opt again incase the routing protocol file added some more # options to look for getopt $argc $argv if { $opt(x) == 0 || $opt(y) == 0 } { usage $argv0 exit 1 } if {$opt(seed) > 0} { puts "Seeding Random number generator with $opt(seed)\n" ns-random $opt(seed) } # # Initialize Global Variables # set ns_ [new Simulator] set chan [new $opt(chan)] set prop [new $opt(prop)] set topo [new Topography] set tracefd [open $opt(tr) w] $topo load_flatgrid $opt(x) $opt(y) $prop topography $topo

# # Create God # create-god $opt(nn)

# # log the mobile nodes movements if desired # if { $opt(lm) == "on" } { log-movement } $ns_ node-config -macTrace ON

# # # # # # #

Create the specified number of nodes $opt(nn) and "attach" them the channel. Each routing protocol script is expected to have defined a proc create-mobile-node that builds a mobile node and inserts it into the array global $node_($i)

if { [string compare $opt(rp) "dsr"] == 0} { for {set i 0} {$i < $opt(nn) } {incr i} { dsr-create-mobile-node $i } } elseif { [string compare $opt(rp) "dsdv"] == 0} { for {set i 0} {$i < $opt(nn) } {incr i} { dsdv-create-mobile-node $i } }

# # Source the Connection and Movement scripts # if { $opt(cp) == "" } { puts "*** NOTE: no connection pattern specified." set opt(cp) "none" } else { puts "Loading connection pattern..." source $opt(cp) }

# # Tell all the nodes when the simulation ends # for {set i 0} {$i < $opt(nn) } {incr i} { $ns_ at $opt(stop).000000001 "$node_($i) reset"; } $ns_ at $opt(stop).00000001 "puts \"NS EXITING...\" ; $ns_ halt"

if { $opt(sc) == "" } { puts "*** NOTE: no scenario file specified." set opt(sc) "none" } else { puts "Loading scenario file..." source $opt(sc) puts "Load complete..." }

puts $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp $opt(rp)" puts $tracefd "M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)" puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)" puts "Starting Simulation..." $ns_ run

Note that all options listed in the beginning of the file could be initialized with command line arguments by using "getopt $argc $argv"

Example 3 : To attach the error model, add following codes:

.......... proc UniformErr {} { set err [new ErrorModel] $err unit packet $err set rate_ 0.05 return $err } # $err rate_ 0.5 "invalid command ErrModel::Command does not support" # the other valid method is " set err [new ErrorModel/Uniform 0.05 pkt] " # the parameter rate ( 0 or 0.9) is a threshold that all packets all going through and 90% packets are # going to be drop proc TwoStateMarkovErr {} { set tmp0 [new ErrorModel/Uniform 0 pkt] set tmp1 [new ErrorModel/Uniform .7 pkt] # Array of states (error models) set m_states [list $tmp0 $tmp1] # Durations for each of the states, tmp, tmp1 and tmp2, respectively set m_periods [list 0.1 .075 ] # Transition state model matrix set m_transmx { {0.9 0.1 } {0.7 0.3 } } set m_trunit byte # Use time-based transition set m_sttype time set m_nstates 2 set m_nstart [lindex $m_states 0] set em [new ErrorModel/MultiState $m_states $m_periods $m_transmx \ $m_trunit $m_sttype $m_nstates $m_nstart] return $em } $ns_ node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channel $chan1 \ -topoInstance $topo \ -agentTrace ON \ -routerTrace OFF\ -macTrace ON \ -movementTrace OFF \ -IncomingErrProc TwoStateMarkovErr #\ #-OutcomingErrProc TwoStateMarkovErr #The other way to configure error # $ns_ node-config --IncomingErrProc $opt(err)

Vous aimerez peut-être aussi