Chinaunix首页 | 论坛 | 博客
  • 博客访问: 850317
  • 博文数量: 180
  • 博客积分: 10029
  • 博客等级: 上将
  • 技术积分: 2185
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-06 09:15
文章存档

2010年(133)

2009年(47)

我的朋友

分类: 系统运维

2009-11-06 13:54:09

NS2仿真系列博文超链接
E-mail:
 
 关于NS2的网络仿真原理, 请看 NS2中文教材(1):NS2网络模拟的原理 
  
 
   以下是利用NS2做进一步研究工作的典型实例! 在描述NS2的书籍、个人网站、Mac Grei的Tutorial上均有介绍! 有必要好好分析,对研究工作会有很好的指导意义的!
   关键点:
        学习采样过程的编写,如何获得所需参数,如何利用单独的文件做记录;
        了解吞吐量、丢包率和端到端时延的具体物理含义;
        进一步了解NS2提供的参数接口!
 
[scenario] It consists of 8 mobile nodes: 4 source nodes and 4 destination node. Each source is a CBR source over UDP. The size of a transmitted packet is 512 bytes. Transmission rate of a node is 600 Kbps. We assumed that the nodes are in transmission range at a constant distance of 195 m. The simulation time lasted for 80 sec.
[场景说明]: 8个节点中,4个作为数据流的源节点,其余作为数据流的目的节点;
           每个源节点和对应目的节点间通过UDP方式连接,运行CBR应用;
           CBR应用的参数: 数据包大小--512字节;  传输率----600Kbps;
      假定每个节点在对方(数据包的下一中转路由节点)的通信半径内(每个节点的通信半径:195m);
      网络模拟过程持续时间: 80秒。
 
   实例1:(注意:注释行以 # 开始, 拷贝运行时应做相应调整哦!)
  

#
#
#
#===================================================================
# Define Node Configuration paramaters
#===================================================================
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)             8                          ;# number of mobilenodes
set val(rp)             DSDV                       ;# routing protocol
set val(x)              500                        ;# X dimension of the topography
set val(y)              500                        ;# Y dimension of the topography
Mac/802_11 set RTSThreshold_  3000
Mac/802_11 set basicRate_ 1Mb
Mac/802_11 set dataRate_  2Mb
#=====================================================================
# Initialize trace file desctiptors
#=====================================================================
# *** Throughput Trace ***
set f0 [open out02.tr w]
set f1 [open out12.tr w]
set f2 [open out22.tr w]
set f3 [open out32.tr w]
# *** Packet Loss Trace ***
set f4 [open lost02.tr w]
set f5 [open lost12.tr w]
set f6 [open lost22.tr w]
set f7 [open lost32.tr w]
# *** Packet Delay Trace ***
set f8 [open delay02.tr w]
set f9 [open delay12.tr w]
set f10 [open delay22.tr w]
set f11 [open delay32.tr w]

#以下部分的代码,在 入门实例2 中已有详尽的注释啦!
set ns_ [new Simulator]
set tracefd   [open trace2.tr w]
$ns_ trace-all $tracefd
set namtrace [open sim12.nam w]
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)

set topo [new Topography]
$topo load_flatgrid 500 500
 
# Create  General Operations Director (GOD) object.
#It is used to store global information about the state of the environment, network, or nodes that an
# omniscent observer would have, but that should not be made known to any participant in the simulation.
 
create-god $val(nn)
# configure nodes
        $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) \
                         -channelType $val(chan) \
                         -topoInstance $topo \
                         -agentTrace ON \
                         -routerTrace ON \
                         -macTrace OFF \
                         -movementTrace 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
        }
# Initialize Node Coordinates
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 200.0
$node_(1) set Y_ 5.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 5.0
$node_(2) set Y_ 50.0
$node_(2) set Z_ 0.0

$node_(3) set X_ 200.0
$node_(3) set Y_ 50.0
$node_(3) set Z_ 0.0
 
$node_(4) set X_ 5.0
$node_(4) set Y_ 100.0
$node_(4) set Z_ 0.0

$node_(5) set X_ 200.0
$node_(5) set Y_ 100.0
$node_(5) set Z_ 0.0

$node_(6) set X_ 2.0
$node_(6) set Y_ 150.0
$node_(6) set Z_ 0.0

$node_(7) set X_ 200.0
$node_(7) set Y_ 150.0
$node_(7) set Z_ 0.0

# Setup traffic flow between nodes
# TCP connections between node_(0) and node_(1)
# Create Constant four Bit Rate Traffic sources
#============================================================
#以下的这段代码可以写成一个过程,那样就方便多啦!
#先整理, 以后我再回头改吧!
#============================================================
set agent1 [new Agent/UDP]              ;# Create TCP Agent
$agent1 set prio_ 0                     ;# Set Its priority to 0
# Create Loss Monitor Sink in order to be able to trace the number obytes received

set sink [new Agent/LossMonitor]       
$ns_ attach-agent $node_(0) $agent1     ;# Attach Agent to source node
$ns_ attach-agent $node_(1) $sink       ;# Attach Agent to sink node
$ns_ connect $agent1 $sink              ;# Connect the nodes
set app1 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application
$app1 set packetSize_ 512               ;# Set Packet Size to 512 bytes
$app1 set rate_ 600Kb                   ;# Set CBR rate to 200 Kbits/sec
$app1 attach-agent $agent1              ;# Attach Application to agent

set agent2 [new Agent/UDP]              ;# Create TCP Agent
$agent2 set prio_ 1                     ;# Set Its priority to 1
# Create Loss Monitor Sink in order to be able to trace the number obytes received
set sink2 [new Agent/LossMonitor]        
$ns_ attach-agent $node_(2) $agent2     ;# Attach Agent to source node
$ns_ attach-agent $node_(3) $sink2      ;# Attach Agent to sink node
$ns_ connect $agent2 $sink2             ;# Connect the nodes
set app2 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application
$app2 set packetSize_ 512               ;# Set Packet Size to 512 bytes
$app2 set rate_ 600Kb                   ;# Set CBR rate to 200 Kbits/sec
$app2 attach-agent $agent2              ;# Attach Application to agent

set agent3 [new Agent/UDP]              ;# Create TCP Agent
$agent3 set prio_ 2                     ;# Set Its priority to 2
# Create Loss Monitor Sink in order to be able to trace the number obytes received
set sink3 [new Agent/LossMonitor]        
$ns_ attach-agent $node_(4) $agent3     ;# Attach Agent to source node
$ns_ attach-agent $node_(5) $sink3      ;# Attach Agent to sink node
$ns_ connect $agent3 $sink3             ;# Connect the nodes
set app3 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application
$app3 set packetSize_ 512               ;# Set Packet Size to 512 bytes
$app3 set rate_ 600Kb                   ;# Set CBR rate to 200 Kbits/sec
$app3 attach-agent $agent3              ;# Attach Application to agent

set agent4 [new Agent/UDP]              ;# Create TCP Agent
$agent4 set prio_ 3                     ;# Set Its priority to 3
# Create Loss Monitor Sink in order to be able to trace the number obytes received
set sink4 [new Agent/LossMonitor]        
$ns_ attach-agent $node_(6) $agent4     ;# Attach Agent to source node
$ns_ attach-agent $node_(7) $sink4      ;# Attach Agent to sink node
$ns_ connect $agent4 $sink4             ;# Connect the nodes
set app4 [new Application/Traffic/CBR]  ;# Create Constant Bit Rate application
$app4 set packetSize_ 512               ;# Set Packet Size to 512 bytes
$app4 set rate_ 600Kb                   ;# Set CBR rate to 200 Kbits/sec
$app4 attach-agent $agent4              ;# Attach Application to agent

# defines the node size in Network Animator
for {set i 0} {$i < $val(nn)} {incr i} {
    $ns_ initial_node_pos $node_($i) 20
}

# Initialize Flags
set holdtime 0
set holdseq 0

set holdtime1 0
set holdseq1 0

set holdtime2 0
set holdseq2 0

set holdtime3 0
set holdseq3 0

set holdrate1 0
set holdrate2 0
set holdrate3 0
set holdrate4 0

# Function To record Statistcis (Bit Rate, Delay, Drop)
proc record {} {
        global sink sink2 sink3 sink4 f0 f1 f2 f3 f4 f5 f6 f7 holdtime holdseq holdtime1 holdseq1 holdtime2 holdseq2 holdtime3 holdseq3 f8 f9 f10 f11 holdrate1 holdrate2 holdrate3 holdrate4
        set ns [Simulator instance]
    set time 0.9 ;#Set Sampling Time to 0.9 Sec
        set bw0 [$sink set bytes_]
        set bw1 [$sink2 set bytes_]
        set bw2 [$sink3 set bytes_]
        set bw3 [$sink4 set bytes_]
        set bw4 [$sink set nlost_]
        set bw5 [$sink2 set nlost_]
        set bw6 [$sink3 set nlost_]
        set bw7 [$sink4 set nlost_]
        set bw8 [$sink set lastPktTime_]
        set bw9 [$sink set npkts_]
        set bw10 [$sink2 set lastPktTime_]
        set bw11 [$sink2 set npkts_]
        set bw12 [$sink3 set lastPktTime_]
        set bw13 [$sink3 set npkts_]
        set bw14 [$sink4 set lastPktTime_]
        set bw15 [$sink4 set npkts_]
       
    set now [$ns now]
        # Record Bit Rate in Trace Files
        puts $f0 "$now [expr (($bw0+$holdrate1)*8)/(2*$time*1000000)]"
        puts $f1 "$now [expr (($bw1+$holdrate2)*8)/(2*$time*1000000)]"
        puts $f2 "$now [expr (($bw2+$holdrate3)*8)/(2*$time*1000000)]"
        puts $f3 "$now [expr (($bw3+$holdrate4)*8)/(2*$time*1000000)]"
       
        # Record Packet Loss Rate in File
        puts $f4 "$now [expr $bw4/$time]"
        puts $f5 "$now [expr $bw5/$time]"
        puts $f6 "$now [expr $bw6/$time]"
        puts $f7 "$now [expr $bw7/$time]"
 
        # Record Packet Delay in File
        if { $bw9 > $holdseq } {
                puts $f8 "$now [expr ($bw8 - $holdtime)/($bw9 - $holdseq)]"
        } else {
                puts $f8 "$now [expr ($bw9 - $holdseq)]"
        }

        if { $bw11 > $holdseq1 } {
                puts $f9 "$now [expr ($bw10 - $holdtime1)/($bw11 - $holdseq1)]"
        } else {
                puts $f9 "$now [expr ($bw11 - $holdseq1)]"
        }

        if { $bw13 > $holdseq2 } {
                puts $f10 "$now [expr ($bw12 - $holdtime2)/($bw13 - $holdseq2)]"
        } else {
                puts $f10 "$now [expr ($bw13 - $holdseq2)]"
        }
 
        if { $bw15 > $holdseq3 } {
                puts $f11 "$now [expr ($bw14 - $holdtime3)/($bw15 - $holdseq3)]"
        } else {
                puts $f11 "$now [expr ($bw15 - $holdseq3)]"
        }
    

        # Reset Variables
        $sink set bytes_ 0
        $sink2 set bytes_ 0
        $sink3 set bytes_ 0
        $sink4 set bytes_ 0

        $sink set nlost_ 0
        $sink2 set nlost_ 0
        $sink3 set nlost_ 0
        $sink4 set nlost_ 0

        set holdtime $bw8
        set holdseq $bw9
        set  holdrate1 $bw0
        set  holdrate2 $bw1
        set  holdrate3 $bw2
        set  holdrate4 $bw3

    $ns at [expr $now+$time] "record"   ;# 定时采样的设置, 哈哈!
}
# set events for simulating!
$ns_ at 0.0 "record"
$ns_ at 1.4 "$app1 start"            
$ns_ at 10.0 "$app2 start"              
$ns_ at 20.0 "$app3 start"              
$ns_ at 30.0 "$app4 start"             

$ns_ at 80.0 "stop"
# Reset Nodes at time 80 sec
for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at 80.0 "$node_($i) reset";
}

# Exit Simulatoion at Time 80.01 sec
$ns_ at 80.01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
        global ns_ tracefd f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11
        # Close Trace Files
        close $f0 
        close $f1
        close $f2
        close $f3
        close $f4 
        close $f5
        close $f6
        close $f7
        close $f8
        close $f9
        close $f10
        close $f11

        # Plot Recorded Statistics
        exec xgraph out02.tr out12.tr out22.tr out32.tr -geometry 800x400 &
        exec xgraph lost02.tr lost12.tr lost22.tr lost32.tr -geometry 800x400 &
        exec xgraph delay02.tr delay12.tr delay22.tr delay32.tr -geometry 800x400 &
        # Reset Trace File
        $ns_ flush-trace
        close $tracefd
        exit 0
}
puts "Starting Simulation..."
$ns_ run

 
运行结果:
     吞吐量:
 
    

Analysis: Node 1 starts transmitting at time T =1.4 sec while Node 2 starts transmitting at time T=10 sec. During the period of time [1.4 sec, 10 sec] Node 1 is the only transmitting node using the entire available bandwidth. This justifies the high performance of Node 1 during the specified interval of time. At time T=10 sec, Node 2 starts transmission hence sharing channel resources with Node 1. This explains the heavy reduction of bit rate(? in fact, no influence at first!). In addition, the bit rate plot experiences heavier oscillations and reduction as the number of transmitting nodes increases. Oscillations are reflected in heavy disorders in network performance.

分析:
1. 节点1在T =1.4秒开始传送数据,节点2在T=10秒开始传送数据。
  [1.4, 10.0]内,唯有节点1传送数据,它占用了所有的网络带宽,所以上图该时段内,节点1的性能在整个模拟过程是最好的。
2. 节点2在T=10秒开始传送数据,开始与节点1争用信道资源,不过在T=20.0秒前,还未超出信道的服务能力; 随着更多节点的相继加入,我们看到各节点的传输率一再下降,并且由于争用,各自均处于波动状态。
   
端到端时延:
 

Analysis: When the number of nodes that are sharing the network resources increases, the delay significantly increases and readjusting CW of each node takes longer time.

分析: 随着争用通信信道的节点数增加,数据包的传输时延明显增大,并且拥塞窗口大小(CW)调整所需时间更长。

 

丢包率

Analysis: This figure shows a high packet drop rate whenever the number of nodes sharing network resources increases. It can be shown that the packet drop rate in the interval [1.4 sec, 10 sec] is 0. This can be easily justified since only one node is using the network during this time interval. However this high-quality performance is deteriorated as more nodes start sharing the network resources.

分析: 从图中可以看出,每当争用通信信道的节点数增加,数据丢包率就相应增大。
      因为[1.4 sec, 10 sec]时段内,只有一个节点使用通信新到时,数据丢包率为0, 而当有更多的节点加入并共同争用信道时,节点的通信性能开始变坏(即,开始丢包)。
 
(发现网络上所传的该博文所参考的最终原文是 , 源码是Joe Naoum-Sawaya编写的, 感谢)
   
E-mail:
 
 
网友询问汇总:(仅作交流)
1. 网友阿元: (非常感谢阿元, 他的问题相当棒!)
 
想问你一个问题,就是你在测量分组延时时间的时候的代码是:
        set bw8 [$sink set lastPktTime_]
        set bw9 [$sink set npkts_]
       if { $bw9 > $holdseq } {
                puts $f8 "$now [expr ($bw8 - $holdtime)/($bw9 - $holdseq)]"
        } else {
                puts $f8 "$now [expr ($bw9 - $holdseq)]"
        }
    请问bw8、bw9和holdseq的含义是什么?
    ($bw8 - $holdtime)/($bw9 - $holdseq)和 ($bw9 - $holdseq)的含义是什么?这样算的单位又是什么?
    还有就是else上为什么这么减,这样不就出负数了吗?
 
    在计算丢包率的时候的公式
set bw4 [$sink set nlost_]
$bw4/$time
    这个的含义是不是单位时间丢掉的字节数?单位是什么呢?丢包率的概念不是丢失的包数占发送包数的比例吗?
 
  针对第一个问题,以下是我的一点初略见解, 请网友指正:
 
首先,我想说下你可能忽略的一个问题: Record过程的执行过程。
          proc record { } {
                  set time 0.9
 
                  set time [$ns now]
 
                  $ns at [expr $now + $time]   "record"
          }
         这实际上是一个采样过程,采样间隔0.9秒;
 
第一,计算端到端时延
          注意:  set bw8 [$sink set lastPktTime_]
                      set bw9 [$sink set npkts_]
 
                      set holdtime $bw8
                      set holeseq $bw9
          lastPktTime: 本次采样时,收到最后一个数据包的时刻
          npkts:  (no of pkts),  数据包的序号值
 
          puts $f8 "$now [expr ($bw8 - $holdtime)/($bw9 - $holdseq)]"
          这是时延计算公式,确切地说是平均时延(采样间隔这样一时段内的平均);
          显然每个数据包传输的平均时延,可以通过选定两个具体的数据包的接受时刻,再求其差值,用时间差值除以数据包的序号差(即,某时段内,共传输了多少个数据包)。
          “延时多少秒”, 时延的单位当然是 时间的单位啦!  公式中的计算结果是时间单位啊!
 
       至于[expr ($bw9 - $holdseq)] 的正负, 请看:      
       set bw9 [$sink set npkts_]
       if { $bw9 > $holdseq } {
               。。。。。。。。。。
        } else {
                puts $f8 "$now [expr ($bw9 - $holdseq)]"
        }
 
        set holeseq $bw9
        明白了吗? 
       (每当进入else时, 只可能是 $bw9 = $holdseq , 而不可能是小于,
         因为这跟UDP协议对数据包生成序号相关, 序号是递增的!)
 
2. 寻求网友的看法:
   
第二,计算丢包率
         丢包率的概念不是丢失的包数占发送包数的比例吗?   是的
        
        set sink [new Agent/LossMonitor]   
  
        set bw4 [$sink set nlost_]
        $bw4/$time
        这个的含义是不是单位时间丢掉的字节数?单位是什么呢?
        你的问题,跟Agent/LossMonitor本身相关;
          
int nlost_
         再看看后面丢包率的图, 丢包率的值居然有超过100%的,所以我觉得是代码写错啦,源代码来源:
(不好意思,我还没搞清 nlost 的单位, 但无论如何,不该除以时间time!!)
我需要继续查查,希望你也自己多多参考、查阅,把它搞清楚!!
 
        以后多多交流!  我也是处于继续学习的阶段,“有朋自远方来,不亦悦乎!”!
 
 
 
 
  
  
 
阅读(10441) | 评论(3) | 转发(2) |
给主人留下些什么吧!~~

chinaunix网友2011-04-20 20:12:37

请问文章中提到的第2个问题,就是关于丢包率,正确的tcl应该怎么写呢?谢谢

chinaunix网友2010-10-16 11:43:05

你好,我用NS3中的AODV协议,建了一个简单的网络模型,结果丢包率达到100%,原因会是什么呢?我的结点、设备、链路都已经搭建好了啊,运动模型也都已经弄好了啊。谢谢了!

chinaunix网友2009-12-22 10:14:10

你好,对于无线网络trace文件的分析,我一直感到很迷茫。也找了不少.awk分析脚本,但好像都不通用。恳请作者指点 有没有通用的方法,或者通用的分析脚本?不胜感激~