[一個簡單的802.11b ad-hoc網路實驗]

        資料來源:http://mailman.isi.edu/pipermail/ns-users/2004-March/040068.html

 

[模擬網路的架構圖]

        n0  ---------------------   n1

     (802.11b ad-hoc network)

 

[TCL程式碼]

# wireless.tcl

# A simple example for wireless simulation

#======================================================================

# Define options

#======================================================================

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)         50                         ;# max packet in ifq

set val(nn)             2                          ;# number of mobilenodes

set val(rp)             DSDV                       ;# routing protocol

 

# =====================================================================

# Main Program (主程式)

# =====================================================================

 

# 產生一個模擬的物件

set ns_              [new Simulator]

 

#定義一個記錄檔,用來記錄封包傳送的過程

set tracefd     [open simple.tr w]

$ns_ trace-all $tracefd

 

#開啟一個NAM trace file

set nf [open out.nam w]

$ns_ namtrace-all-wireless $nf 500 500

 

# set up topography object

#建立一個拓樸物件

set topo       [new Topography]

 

# 拓樸的範圍為 500m x 500m

$topo load_flatgrid 500 500

 

#

# Create God

#

create-god $val(nn)

 

set chan_1_ [new $val(chan)]

 

#

#  Create the specified number of mobilenodes [$val(nn)] and "attach" them

#  to the channel.

#  Here two nodes are created : node(0) and node(1)

 

# 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 $chan_1_ \

                         -topoInstance $topo \

                         -agentTrace ON \

                         -routerTrace ON \

                         -macTrace OFF \

                         -movementTrace OFF                   

 

#802.11b的參數設定

#Configuration for Orinoco 802.11b 11Mbps PC card with ->22.5m range

Phy/WirelessPhy set Pt_ 0.031622777

Phy/WirelessPhy set bandwidth_ 11Mb

Mac/802_11 set dataRate_ 11Mb

Mac/802_11 set basicRate_ 1Mb

# for broadcast packets

Phy/WirelessPhy set freq_ 2.472e9

# channel-13.2.472GHz

Phy/WirelessPhy set CPThresh_ 10.0

Phy/WirelessPhy set CSThresh_ 5.011872e-12

Phy/WirelessPhy set L_ 1.0              

Phy/WirelessPhy set RXThresh_ 5.82587e-09

                         

        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, for now Z=0) co-ordinates for mobilenodes

#

# 設定節點0的位置在(10.0, 20.0, 0.0)

$node_(0) set X_ 10.0

$node_(0) set Y_ 20.0

$node_(0) set Z_ 0.0

 

# 設定節點1的位置在(10.0, 40.0, 0.0)

$node_(1) set X_ 10.0

$node_(1) set Y_ 40.0

$node_(1) set Z_ 0.0

 

$ns_ initial_node_pos $node_(0) 10

$ns_ initial_node_pos $node_(1) 10

 

# Setup traffic flow between nodes

#在節點0到節點1之間建立一條FTP連線

set tcp [new Agent/TCP/Sack1]

$tcp set class_ 2

set sink [new Agent/TCPSink/Sack1]

$ns_ attach-agent $node_(0) $tcp

$ns_ attach-agent $node_(1) $sink

$ns_ connect $tcp $sink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

 

#1.0秒時,開始傳送資料

$ns_ at 1.0 "$ftp start"

 

# Tell nodes when the simulation ends

 

for {set i 0} {$i < $val(nn) } {incr i} {

    $ns_ at 40.0 "$node_($i) reset";

}

 

#40.0秒時,結束傳送資料

$ns_ at 40.0 "stop"

$ns_ at 40.01 "puts \"NS EXITING...\" ; $ns_ halt"

 

proc stop {} {

    global ns_ tracefd

    $ns_ flush-trace

    close $tracefd

}

 

puts "Starting Simulation..."

$ns_ run

 

[執行方法]

$ns wireless.tcl

 

[執行結果]

        會產生一個simple.tr的記錄檔,部份的內容如下。

s 0.029290548 _1_ RTR  --- 0 message 32 [0 0 0 0] ------- [1:255 -1:255 32 0]

r 0.030062615 _0_ RTR  --- 0 message 32 [0 ffffffff 1 800] ------- [1:255 -1:255 32 0]

s 1.000000000 _0_ AGT  --- 1 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0

r 1.000000000 _0_ RTR  --- 1 tcp 40 [0 0 0 0] ------- [0:0 1:0 32 0] [0 0] 0 0

s 1.000000000 _0_ RTR  --- 1 tcp 60 [0 0 0 0] ------- [0:0 1:0 32 1] [0 0] 0 0

r 1.003800194 _1_ AGT  --- 1 tcp 60 [13a 1 0 800] ------- [0:0 1:0 32 1] [0 0] 1 0

s 1.003800194 _1_ AGT  --- 2 ack 40 [0 0 0 0] ------- [1:0 0:0 32 0] [0 0] 0 0

r 1.003800194 _1_ RTR  --- 2 ack 40 [0 0 0 0] ------- [1:0 0:0 32 0] [0 0] 0 0

................................

 

格式說明:

1.      第一個欄位是Event Typessendr: Received: Dropf: Forward

2.      第二欄位是Time

3.      第三欄位是Node ID

4.      第四欄位是Trace NameRTRRouter TraceAGTAgent TraceMACMac Trace

5.      第七欄位是Packet Type

6.      第八欄位是Packet Size

7.      第十四欄位是Source Address(NodeID.PortNumber)

8.      第十五欄位是Destination Address(NodeID.PortNumber)

 

[Throughput分析程式碼]

 

1.      cc.sh

awk -f cc.awk simple.tr | sort -u

 

1.      cc.awk

{

 if (($1 == "r") && (NF == 21)) {

  if ($4 == "AGT") {

    print $14, $15;

  }

 }

}

[] NFAWK的內定變數,意思是number of fields (NF),簡單的說,就是每一行有幾個欄位資料。

 

2.      st.sh

while read s d

do

    printf "%s\t %s\t" $s $d

    awk -f st.awk -v src="$s" -v dst="$d" simple.tr

done

 

3.      st.awk

BEGIN {

  total = 0;

  n=0;

}

{

 if (($14 == src) && ($15 == dst)) {

  if (($1 =="r") && ($4 =="AGT")) {

   if (n == 0) start = $2;

   total += $8 * 8;

   n +=1;

   end = $2;

  }

 }

}

END {

 throughput = total / (end-start) / 1024;

 

 print start, "\t", end, "\t",throughput, "Kbps";

}

 

[執行的方法]

$sh cc.sh | sh st.sh

 

[執行的結果]

[0:0  1:0 1.003800194  39.999500087       2213.26 Kbps

[1:0  0:0 1.114528190  39.997336614       125.562 Kbps

 

[說明]

節點0到節點1FTP資料傳送,average throughput2213.26 Kbps