ns2无线局域网基本仿真测量

Wlan 的协议802.11 定义了两种工作模式 ad hoc (对等)模式和infrastructure (架构)模式。在ad hoc 模式中,至少需要包含两个STA ,每两个STA 之间直接相连实现资源共享,不需要AP 和分布式系统,由此构成的无线局域网也称为IBSS 网络。

infrastructure 模式中,各无线站点STA 通过AP 与现有的骨干网相连接,这种 配置组成一个BSS 。在BSS 中,AP 不仅提供STA 之间通信的桥接功能,还提供STA 与有线局域网的连接,以便无线用户访问有线网络上的设备或服务

ns2无线局域网基本仿真测量_第1张图片

分别在两种模式下进行无线仿真实验,从而更好的了解影响无线网络性能的因素,对实际应用也有一定的指导作用,通过仿真指导实际网络构建也是一个很好的应用。譬如:我一个同学目前在做一个无线通信设备公司,主要是在农村地区布设wifi 网络,提供internet 服务,由于他们没有经过很好的论证就构建了一个网络,发现网络性能非常不稳定,多个用户使用时每个人的带宽会急剧下降,这时,他们的解决方法是增加有线外网接入带宽并给每个用户限定使用带宽,但是还是出现不稳定的情况,我知道后,利用ns2 做了一个仿真,发现他们的无线网络构架是不可能提供预期性能的。这也说明仿真还是有一定价值的。下面就分两个部分介绍如何使用ns2 进行802.11网络基本仿真 。通过对TCP 应用的仿真,得出TCP 应用的吞吐量。我们常说的54m 的无线网络的性能到底如何呢,通过仿真我们可以得到一个理性的认识。

Adhoc 网络的仿真:(参考

实验代码:

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 set datarate $opt(0); set end $opt(1); set nodes $opt(2); # ====================================================================== # Define options # ====================================================================== 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(nn) $nodes ;# number of mobilenodes #set val(rp) DSDV ;# routing protocol set val(rp) AODV ;# routing protocol # # Initialize Global Variables # set ns_ [new Simulator] set tracefd [open 2nodetrace.tr w] $ns_ trace-all $tracefd $ns_ use-newtrace Phy/WirelessPhy set freq_ 2.4e+9 Phy/WirelessPhy set RXThresh_ 1.74293e-08 Phy/WirelessPhy set CSThresh_ 3.44283e-09 Phy/WirelessPhy set Pt_ 0.28183815 if {$datarate == "2"} { puts "FHSS (IEEE802.11)" Mac/802_11 set CWMin 31 Mac/802_11 set CWMax 1023 Mac/802_11 set SlotTime_ 0.000050 Mac/802_11 set SIFS_ 0.000028 Mac/802_11 set PreambleLength_ 0 Mac/802_11 set PLCPHeaderLength_ 128 Mac/802_11 set PLCPDataRate_ 1.0e6 Mac/802_11 set dataRate_ 2.0e6 Mac/802_11 set basicRate_ 1.0e6 ; } elseif {$datarate == "11"} { puts "DSSS (IEEE802.11b)" Mac/802_11 set CWMin_ 31 Mac/802_11 set CWMax_ 1023 Mac/802_11 set SlotTime_ 0.000020 Mac/802_11 set SIFS_ 0.000010 Mac/802_11 set PreambleLength_ 144 Mac/802_11 set PLCPHeaderLength_ 48 Mac/802_11 set PLCPDataRate_ 1.0e6 Mac/802_11 set dataRate_ 11.0e6 Mac/802_11 set basicRate_ 1.0e6 } elseif {$datarate == "54"} { puts "DSSS (IEEE802.11g)" Mac/802_11 set CWMin 31 Mac/802_11 set CWMax 1023 Mac/802_11 set SlotTime_ 0.000009 Mac/802_11 set SIFS_ 0.000010 Mac/802_11 set PreambleLength_ 96 Mac/802_11 set PLCPHeaderLength_ 40 Mac/802_11 set PLCPDataRate_ 6.0e6 Mac/802_11 set dataRate_ 54.0e6 Mac/802_11 set basicRate_ 1.0e6 } else { puts "Error datarate configuration." } Mac/802_11 set RTSThreshold_ 256 Mac/802_11 set ShortRetryLimit_ 7 Mac/802_11 set LongRetryLimit_ 4 # set up topography object set topo [new Topography] $topo load_flatgrid 500 500 # # Create God create-god $val(nn) # # Create the specified number of mobilenodes [$val(nn)] and "attach" them # to the channel. # Here two nodes are created : node(0) and node(1) # Create a new channel set channel1_ [new $val(chan)] # configure node $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 $channel1_ / -topoInstance $topo / -agentTrace ON / -routerTrace ON / -macTrace OFF / -movementTrace OFF for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0 $node_($i) set X_ [expr 30.0 * $i] $node_($i) set Y_ 00.0 $node_($i) set Z_ 00.0 $ns_ initial_node_pos $node_($i) 15 } set tcp [new Agent/TCP/Reno] $tcp set packetSize_ 1440 $tcp set window_ 64 $tcp set maxburst_ 2 $tcp set fid_ 0 set sink [new Agent/TCPSink] $sink set fid_ 0 $ns_ attach-agent $node_(0) $tcp $ns_ attach-agent $node_([expr $nodes-1]) $sink $ns_ connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns_ at 0.0 "$ftp start" for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $end "$node_($i) reset"; } $ns_ at $end "stop; $ns_ halt" proc stop {} { global ns_ tracefd tcp sink datarate end tcpv set thr [expr [$tcp set ack_] * [$tcp set packetSize_]*8/$end/1000000.0] puts [format "th = %.2f, util = %.2f" $thr [expr $thr/$datarate*100]] $ns_ flush-trace close $tracefd } puts "Starting Simulation..." $ns_ run

1, 单跳

当使用2mb802.11 网络时结果为:

th = 1.23, util = 61.32 (吞吐量1.23mbutil 是使用率和2mb 的比对)

当使用11mb802.11b 网络时结果为:

th = 3.35, util = 30.48

当使用54mb802.11g 网络时结果为:

th = 9.23, util = 17.10

2 ,多跳,在adhoc 网络中路由算法显得非常重要,尤其是自组织网络在网络拓扑变化迅速情况下对性能的保持是研究的重点和难点,我们这个实验就是简单的线性路由,我们会发现当跳数(路由经过节点数)增加后性能的下降时非常明显的。当使用11mb 的带宽时,1 跳的记过是3.34mb 吞吐,3 跳为1.67mb4 跳为0.77mb5 跳降为0.56mb 。多跳时候网络性能的下降归结为每一跳的节点需要接受和发送数据包从而导致性能的下降(参考

Infrastructure 网络的仿真:

修改代码重点在制定移动节点的基站节点,基站节点是无线和有线的混合节点,这里面我也把节点的地址修改为层次地址,代码如下:

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 set datarate $opt(0); set opt(stop) $opt(1); set opt(nn) 4 set opt(x) 500 set opt(y) 500 set opt(tr-ns) out.tr set opt(tr-nam) out.nam set ns_ [new Simulator] # set up for hierarchical routing (needed for routing over a basestation) $ns_ node-config -addressType hierarchical AddrParams set domain_num_ 2 ;# domain number AddrParams set cluster_num_ {1 1} ;# cluster number for each domain AddrParams set nodes_num_ {1 3} ;# number of nodes for each cluster set tracefd [open $opt(tr-ns) w] set namtrace [open $opt(tr-nam) w] $ns_ trace-all $tracefd $ns_ use-newtrace $ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y) set topo [new Topography] $topo load_flatgrid $opt(x) $opt(y) create-god [expr $opt(nn) + 2] set CN [$ns_ node 0.0.0] Phy/WirelessPhy set freq_ 2.4e+9 Phy/WirelessPhy set RXThresh_ 1.74293e-08 Phy/WirelessPhy set CSThresh_ 3.44283e-09 Phy/WirelessPhy set Pt_ 0.28183815 if {$datarate == "2"} { puts "FHSS (IEEE802.11)" Mac/802_11 set CWMin 31 Mac/802_11 set CWMax 1023 Mac/802_11 set SlotTime_ 0.000050 Mac/802_11 set SIFS_ 0.000028 Mac/802_11 set PreambleLength_ 0 Mac/802_11 set PLCPHeaderLength_ 128 Mac/802_11 set PLCPDataRate_ 1.0e6 Mac/802_11 set dataRate_ 2.0e6 Mac/802_11 set basicRate_ 1.0e6 ; } elseif {$datarate == "11"} { puts "DSSS (IEEE802.11b)" Mac/802_11 set CWMin_ 31 Mac/802_11 set CWMax_ 1023 Mac/802_11 set SlotTime_ 0.000020 Mac/802_11 set SIFS_ 0.000010 Mac/802_11 set PreambleLength_ 144 Mac/802_11 set PLCPHeaderLength_ 48 Mac/802_11 set PLCPDataRate_ 1.0e6 Mac/802_11 set dataRate_ 11.0e6 Mac/802_11 set basicRate_ 1.0e6 } elseif {$datarate == "54"} { puts "DSSS (IEEE802.11g)" Mac/802_11 set CWMin 31 Mac/802_11 set CWMax 1023 Mac/802_11 set SlotTime_ 0.000009 Mac/802_11 set SIFS_ 0.000010 Mac/802_11 set PreambleLength_ 96 Mac/802_11 set PLCPHeaderLength_ 40 Mac/802_11 set PLCPDataRate_ 6.0e6 Mac/802_11 set dataRate_ 54.0e6 Mac/802_11 set basicRate_ 1.0e6 } else { puts "Error datarate configuration." } Mac/802_11 set RTSThreshold_ 256 Mac/802_11 set ShortRetryLimit_ 7 Mac/802_11 set LongRetryLimit_ 4 $ns_ node-config -adhocRouting DSDV / -llType LL / -macType Mac/802_11 / -ifqType Queue/DropTail/PriQueue / -ifqLen 50 / -antType Antenna/OmniAntenna / -propType Propagation/TwoRayGround / -phyType Phy/WirelessPhy / -channelType Channel/WirelessChannel / -topoInstance $topo / -wiredRouting ON / -agentTrace ON / -routerTrace OFF / -macTrace OFF set HA [$ns_ node 1.0.0] ;#domain 2, cluster 1, node 1 $HA set X_ 100.00 $HA set Y_ 100.00 $HA set Z_ 0.00 # create links between wired and BaseStation nodes $ns_ duplex-link $CN $HA 5Mb 2ms DropTail $ns_ node-config -wiredRouting OFF set MN_1 [$ns_ node 1.0.1] $MN_1 base-station [AddrParams addr2id [$HA node-addr]] $MN_1 set X_ 120.00 $MN_1 set Y_ 100.00 $MN_1 set Z_ 0.00 set MN_2 [$ns_ node 1.0.2] $MN_2 base-station [AddrParams addr2id [$HA node-addr]] $MN_2 set X_ 140.00 $MN_2 set Y_ 100.00 $MN_2 set Z_ 0.00 set tcp1 [new Agent/TCP] set sink1 [new Agent/TCPSink] $ns_ attach-agent $MN_1 $tcp1 $ns_ attach-agent $CN $sink1 $ns_ connect $tcp1 $sink1 set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 set tcp2 [new Agent/TCP] set sink2 [new Agent/TCPSink] $ns_ attach-agent $MN_2 $tcp2 $ns_ attach-agent $CN $sink2 $ns_ connect $tcp2 $sink2 set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 $ns_ at 10.00 "$ftp1 start" $ns_ at 10.00 "$ftp2 start" $ns_ at $opt(stop) "finish; $ns_ halt" proc finish {} { global ns_ tracefd namtrace tcp1 sink datarate opt set thr [expr [$tcp1 set ack_] * [$tcp1 set packetSize_]*8/($opt(stop)-10)/1000000.0] puts [format "th = %.2f, util = %.2f" $thr [expr $thr/$datarate*100]] close $tracefd close $namtrace } puts "Starting Simulation..." $ns_ run

当只有一个无线节点工作时(注释掉ftp2start 代码),结果为:

th = 1.11, util = 55.33 ,而当有无线节点同时工作时结果为:th = 0.55, util = 27.72 ,我们发现吞吐量正好减少了一半,这里我们避谈较为复杂的信道共享机制,使用常识推理也可以想到,多了一个工作节点当然性能会下降了。最后说一下仿真和实际的区别,仿真结果和实际结果会有一些不同(参考 介绍了如何调整参数是仿真结果和实际结果接近)实际吞吐量可以使用ixchariot 进行测量。

 

 

你可能感兴趣的:(工作,tcp,网络,domain,internet,interface)