p { margin-bottom: 0.08in; }a:link { }
过几天,项目组老师要求对 NS做一个简单的介绍,可是到现在对 NS的研究还很肤浅,实在不知道,如何是好。手边的资料是不少,可是没有亲自的编写,亲自的调试,是不可能会很好掌握的。
不过无论怎么还是抛砖引玉吧。
下面是两个例子。
1。这个程序是验证 802.11在基本模式下,系统所能达到的最大吞吐率。
proc getopt {argc argv} {
global opt
lappend optlist nn
for {set i 0} {$i < $argc} {incr i} {
set opt($i) [lindex $argv $i]
}
}
getopt $argc $argv
add-packet-header IP LL Mac ARP TCP ;# needed headers
Mac/802_11 set CWMin_ 31
Mac/802_11 set CWMax_ 1023
Mac/802_11 set SlotTime_ 0.000020 ;# 20us
Mac/802_11 set SIFS_ 0.000010 ;# 10us
Mac/802_11 set PreambleLength_ 144 ;# 144 bit
Mac/802_11 set ShortPreambleLength_ 72 ;# 72 bit
Mac/802_11 set PreambleDataRate_ 1.0e6 ;# 1Mbps
Mac/802_11 set PLCPHeaderLength_ 48 ;# 48 bits
Mac/802_11 set PLCPDataRate_ 1.0e6 ;# 1Mbps
Mac/802_11 set ShortPLCPDataRate_ 2.0e6 ;# 2Mbps
Mac/802_11 set RTSThreshold_ 3000 ;# bytes Disable RTS/CTS
Mac/802_11 set ShortRetryLimit_ 7 ;# retransmissions
Mac/802_11 set LongRetryLimit_ 4 ;# retransmissions
Mac/802_11 set newchipset_ false ;# use new chipset, allowing a more recent
;# packet to be correctly received in place
;# of the first sensed packet
Mac/802_11 set dataRate_ 11Mb ;# 802.11 data transmission rate
Mac/802_11 set basicRate_ 1Mb ;# 802.11 basic transmission rate
#Mac/802_11 set aarf_ false ;# 802.11 Auto Rate Fallback
#opt(1) 1: short preamble 0:long preamble
if {$opt(1) > 0} {
ErrorModel80211 shortpreamble 1 ;# toggle 802.11 short preamble on/off
}
#===================================
# Simulation parameters setup
#===================================
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) 10 ;# max packet in ifq
set val(nn) 2 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(stop) 100.0 ;# time of simulation end
set ns_ [new Simulator]
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
set nf [open out.nam w]
$ns_ namtrace-all-wireless $nf 100 100
# set up topography object
set topo [new Topography]
$topo load_flatgrid 100 100
# Create God
create-god $val(nn)
set chan_1_ [new $val(chan)]
$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 $chan_1_ /
-topoInstance $topo /
-agentTrace ON /
-routerTrace OFF /
-macTrace ON /
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
}
set rng [new RNG]
$rng seed 1
set rand1 [new RandomVariable/Uniform]
for {set i 0} {$i < $val(nn) } {incr i} {
puts "wireless node $i created ..."
set x [expr 50+[$rand1 value]*50]
set y [expr 50+[$rand1 value]*50]
$node_($i) set X_ $x
$node_($i) set Y_ $y
$node_($i) set Z_ 0.0
puts "X_:$x Y_:$y"
}
for {set i 0} {$i < $val(nn) } {incr i} {
set udp_($i) [new Agent/UDP]
$udp_($i) set packetSize_ 2000
$ns_ attach-agent $node_($i) $udp_($i)
set null_($i) [new Agent/LossMonitor]
$ns_ attach-agent $node_($i) $null_($i)
}
for {set i 0} {$i < $val(nn) } {incr i} {
if {$i == ($val(nn)-1)} {
$ns_ connect $udp_($i) $null_(0)
} else {
set j [expr $i+1]
$ns_ connect $udp_($i) $null_($j)
}
set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) attach-agent $udp_($i)
$cbr_($i) set type_ CBR
$cbr_($i) set packet_size_ [expr $opt(0)-20]
$cbr_($i) set rate_ 5Mb
$cbr_($i) set random_ false
}
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 1.1 "$cbr_($i) start"
$ns_ at 5.1 "$cbr_($i) stop"
}
# Tell nodes when the simulation ends
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 100.0 "$node_($i) reset";
}
$ns_ at 100.0 "stop"
$ns_ at 100.01 "puts /"NS EXITING.../" ; $ns_ halt"
$ns_ at 45.0 "record"
set first_time 10000.0
set last_time 0.0
proc record {} {
global ns_ null_ val first_time last_time
set sum 0
for {set i 0} {$i < $val(nn) } {incr i} {
set th 0
set a [$null_($i) set bytes_]
set b [$null_($i) set lastPktTime_]
set c [$null_($i) set firstPktTime_]
if {$first_time>$c} {
set first_time $c
}
if {$last_time<$b} {
set last_time $b
}
if {$b>$c} {
set t_bytes [expr $a*8]
set sum [expr $sum+$t_bytes]
}
}
puts "total throughput:[expr $sum/($last_time-0]bps"
}
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run
在没有修改 tools/loss-monitor.h 和 tools/loss-monitor.cc 之前。会出现“ ns: record: can't read "firstPktTime_": no such variable” 这样的错误。可是修改了以后,错误变成了” warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
Starting Simulation...
channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
highestAntennaZ_ = 1.5, distCST_ = 550.0
SORTING LISTS ...DONE!
ns: record: syntax error in expression "27392800/(5.1405303368993662-0": looking for close parenthesis
while executing
"expr $sum/($last_time-0"
(procedure "record" line 21)
invoked from within
"record" ”
目前没有找到解决的办法。
2 。下面这个 TCL 脚本同样是测试系统吞吐率,但是没有分析跟踪文件
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagationmodel
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) 1000 ;# max packet in ifq
set val(nn) 5 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
Mac/802_11 set RTSThreshold_ 3000 ;# bytes
Mac/802_11 set ShortRetryLimit_ 7 ;# retransmittions
Mac/802_11 set LongRetryLimit_ 4 ;# retransmissions
Mac/802_11 set PreambleLength_ 144 ;# 144 bit
Mac/802_11 set PLCPHeaderLength_ 48 ;# 48 bits
Mac/802_11 set PLCPDataRate_ 1Mb ;# 1Mbps
Mac/802_11 set dataRate_ 1Mb
Mac/802_11 set basicRate_ 1Mb
Mac/802_11 set CWMin_ 31
Mac/802_11 set CWMax_ 1023
Mac/802_11 set SlotTime_ 0.000020 ;# 20us
Mac/802_11 set SIFS_ 0.000010 ;# 10us
set ns_ [new Simulator]
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
set nf [open out.nam w]
$ns_ namtrace-all-wireless $nf 300 300
set topo [new Topography]
$topo load_flatgrid 300 300
create-god $val(nn)
set chan_1_ [new $val(chan)]
$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 $chan_1_ /
-topoInstance $topo /
-agentTrace ON /
-routerTrace OFF /
-macTrace OFF /
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
}
set rng [new RNG]
$rng seed 1
set rand1 [new RandomVariable/Uniform]
for {set i 0} {$i < $val(nn) } {incr i} {
puts "wireless node $i created ..."
set x [expr 150+[$rand1 value]*4]
set y [expr 150+[$rand1 value]*1]
$node_($i) set X_ $x
$node_($i) set Y_ $y
$node_($i) set Z_ 0.0
puts "X_:$x Y_:$y"
}
for {set i 0} {$i < $val(nn) } {incr i} {
set udp_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $udp_($i)
set null_($i) [new Agent/LossMonitor]
$ns_ attach-agent $node_($i) $null_($i)
}
for {set i 0} {$i < $val(nn) } {incr i} {
if {$i == ($val(nn)-1)} {
$ns_ connect $udp_($i) $null_(0)
} else {
set j [expr $i+1]
$ns_ connect $udp_($i) $null_($j)
}
set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) attach-agent $udp_($i)
$cbr_($i) set type_ CBR
$cbr_($i) set packet_size_ 1000
$cbr_($i) set rate_ 500kb
$cbr_($i) set random_ false
}
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 1.0 "$cbr_($i) start"
$ns_ at 50.0 "$cbr_($i) stop"
}
# Tell nodes when the simulation ends
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 100.0 "$node_($i) reset";
}
$ns_ at 100.0 "stop"
$ns_ at 100.01 "puts /"NS EXITING.../" ; $ns_ halt"
$ns_ at 45.0 "record"
proc record {} {
global ns_ null_ val
set sum 0
for {set i 0} {$i < $val(nn) } {incr i} {
set th 0
set a [$null_($i) set bytes_]
set b [$null_($i) set lastPktTime_]
set c [$null_($i) set firstPktTime_]
set d [$null_($i) set npkts_]
if {$b>$c} {
set th [expr ($a-$d*20)*8/($b-$c)]
puts "flow $i has $th bps"
}
set sum [expr $sum+$th]
}
puts "total throughput:$sum bps"
}
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run
虽然可以得到最后的吞吐率,但是存在 warning 。并且和上面的例子一样,看来是 NS 的内部模块出现了问题。” warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/LossMonitor::firstPktTime_
see tcl-object.tcl in tclcl for info about this warning. ” 而事实上,查看了 tcl.object.tcl 了以后还是没有任何的解释。
纠结中。还是在好好看看手册吧。
参考文献
[ 1 ] http://hpds.ee.ncku.edu.tw/~smallko/ns2/total_throghput.htm