在本小節中,筆者參考Jin Ding的投影片,為大家簡單介紹如何在NS2中模擬無線網路。

 

[網路拓樸]

模擬的範圍區域為1000m x 1000m,共有三個Mobile Nodes。在節點0和節點2之間有一條CBR/UDP的連線。在模擬時間200秒的時候,節點1開始從位置(500, 500)移動到(500, 900),然後在500秒的時候,再從位置(500, 900)移動到(500, 100)。全部的模擬時間為1000秒。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


[Tcl code]

 

# 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(x)             1000                                     ;# X dimension of topology (拓樸範圍:X)

set val(y)             1000                                     ;# Y dimension of topology (拓樸範圍:Y)

set val(cp)            ""                               ;# node movement model file

set val(sc)         ""                                  ;# traffic model file

set val(ifqlen)      50                                 ;# max packet in ifq

set val(nn)        3                                  ;# number of mobilenodes (節點個數)

set val(seed)         0.0

set val(stop)          1000.0                                  ;# simulation time  (模擬總時間)

set val(tr)          exp.tr                                    ;# trace file name

set val(rp)        DSDV                              ;# routing protocol (路由協定)

set AgentTrace    ON

set RouterTrace   ON

set MacTrace     OFF

 

# Initialize Global Variables

set ns_              [new Simulator]

$ns_ color 1     Blue

$ns_ color 2   Red

 

# Open trace file 開啟trace file

$ns_ use-newtrace                             ;# Use new trace format 使用新的trace format

set namfd         [open nam-exp.tr w]

$ns_ namtrace-all-wireless $namfd $val(x) $val(y)

set tracefd     [open $val(tr) w]

$ns_ trace-all $tracefd

 

# set up topography object

#建立一個拓樸物件,以紀錄mobilenodes在拓樸內移動的情況

set topo       [new Topography]

 

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

$topo load_flatgrid $val(x) $val(y)

 

# create channel

set chan [new $val(chan)]

 

# Create God

set god_ [create-god $val(nn)]

 

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

#  to the channel. Three nodes are created : node(0), node(1) and node(2)

#  設置Mobile 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 \

                         -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            ;# disable random motion

        }

 

# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes

#

# 設定節點0在一開始時,位置在(350.0, 500.0)

$node_(0) set X_ 350.0

$node_(0) set Y_ 500.0

$node_(0) set Z_ 0.0

 

# 設定節點1在一開始時,位置在(500.0, 500.0)

$node_(1) set X_ 500.0

$node_(1) set Y_ 500.0

$node_(1) set Z_ 0.0

 

# 設定節點2在一開始時,位置在(650.0, 500.0)

$node_(2) set X_ 650.0

$node_(2) set Y_ 500.0

$node_(2) set Z_ 0.0

 

# Load the god object with shortest hop information

# 在節點1和節點2之間最短的hop數為1

$god_ set-dist 1 2 1

 

# 在節點0和節點2之間最短的hop數為2

$god_ set-dist 0 2 2

 

# 在節點0和節點1之間最短的hop數為1

$god_ set-dist 0 1 1

 

# Now produce some simple node movements

# Node_(1) starts to move upward and then downward

set god_ [God instance]

 

# 在模擬時間200秒的時候,節點1開始從位置(500, 500)移動到(500, 900),速度為2.0 m/sec

$ns_ at 200.0 "$node_(1) setdest 500.0 900.0 2.0"

 

# 然後在500秒的時候,再從位置(500, 900)移動到(500, 100),速度為2.0 m/sec

$ns_ at 500.0 "$node_(1) setdest 500.0 100.0 2.0"

 

# Setup traffic flow between nodes   0 connecting to 2 at time 100.0

# 在節點0和節點2建立一條CBR/UDP的連線,且在時間為100秒開始傳送

set udp_(0) [new Agent/UDP]

$udp_(0) set fid_ 1

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

set null_(0) [new Agent/Null]

$ns_ attach-agent $node_(2) $null_(0)

 

set cbr_(0) [new Application/Traffic/CBR]

$cbr_(0) set packetSize_ 200

$cbr_(0) set interval_ 2.0

$cbr_(0) set random_ 1

$cbr_(0) set maxpkts_ 10000

$cbr_(0) attach-agent $udp_(0)

$ns_ connect $udp_(0) $null_(0)

$ns_ at 100.0 "$cbr_(0) start"

 

#Define node initial position in nam, only for nam

# nam中定義節點初始所在位置

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

        # The function must be called after mobility model is defined.

        $ns_ initial_node_pos $node_($i) 60

}

 

# Tell nodes when the simulation ends

# 設定節點模擬結束時間

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

    $ns_ at $val(stop)  "$node_($i) reset";

}

$ns_ at $val(stop)  "stop"

$ns_ at $val(stop)  "puts \"NS EXITING...\" ; $ns_ halt"

proc stop {} {

    global ns_ tracefd namfd

    $ns_ flush-trace

    close $tracefd

    close $namfd

}

puts $tracefd "M 0.0 nn $val(nn) x  $val(x) y $val(y)  rp $val(rp)"

puts $tracefd "M 0.0 sc $val(sc) cp  $val(cp) seed $val(seed)"

puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"

puts "Starting Simulation..."

$ns_ run

 

 

[New Wireless Trace File Format]

使用文字編輯軟體把exp.tr打開,則可以看到類似下面的記錄

s -t 163.001503520 -Hs 0 -Hd -2 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 200 -If 1 -Ii 77 -Iv 32 -Pn cbr -Pi 32 -Pf 0 -Po 2

 

欄位0: event type (事件類型)

s: send (傳送)          r: receive(接收)        d:drop(丟棄)            f: forward(轉送)

 

欄位1: general flag

-t: time (時間)

 

欄位3: Next hop info (下一站的資訊)

-Hs: id for this node

-Hd: id for next hop towards the destination

 

欄位4: Node property type tag (節點屬性類型標籤)

-Ni: node id  (節點ID)  

-Nx Ny -Nz: nodes x/y/z coordinate (節點x/y/z的座標位置)

-Ne: node energy level

-Nl: trace level, such as AGT, RTR, MAC

-Nw: reason for the event (事件發生原因)

 

欄位5: Packet information at IP level (封包在IP層的資訊)

-Is: source address. Source port number (來源位置,a.b其中a為節點IDb為埠號)

-Id: dest address.dest port number (目的位置)

-It: packet type (封包類型)

-Il: packet size (封包大小)

-If: flow id (資料流ID)

-Ii: unique id (唯一的ID編號)

-Iv: ttl value (Time To Live的值)

 

欄位6: packet info at MAC level (封包在Mac層的資訊)

-Ma: duration

-Md: dest’s ethernet address

-Ms: src’s ethernet address

-Mt: ethernet type

 

欄位7: Packet info at “Application level” which consists of the type of application like arp, tcp, the type of adhoc routing protocol like DSDV, DSR, AODV etc. The field consists of a leading –P and the list of tags for different applications.

(封包在應用層的資訊。包含的應用程式類型如arp, tcp或者是adhoc路由協定像是DSDR, DSR, AODV等等。這個欄位都是以P所開頭的,且標籤為隨著應用程式不同而不同)

 

[範例-Routing message Update]

s -t 0.001560381 -Hs 0 -Hd -1 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 0 -Iv 32

 

r -t 0.002332881 -Hs 1 -Hd -1 -Ni 1 -Nx 500.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 0 -Mt 800 -Is 0.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 0 -Iv 32

 

s -t 0.036574082 -Hs 2 -Hd -1 -Ni 2 -Nx 650.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 2.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 1 -Iv 32

 

r -t 0.037706582 -Hs 1 -Hd -1 -Ni 1 -Nx 500.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 2 -Mt 800 -Is 2.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 1 -Iv 32

 

s -t 1.120124667 -Hs 1 -Hd -1 -Ni 1 -Nx 500.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 1.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 2 -Iv 32

 

r -t 1.121437167 -Hs 2 -Hd -1 -Ni 2 -Nx 650.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 1 -Mt 800 -Is 1.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 2 -Iv 32

 

r -t 1.121437167 -Hs 0 -Hd -1 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md ffffffff -Ms 1 -Mt 800 -Is 1.255 -Id -1.255 -It message -Il 32 -If 0 -Ii 2 -Iv 32

 

[範例-Packet Transmission]

s -t 100.000000000 -Hs 0 -Hd -2 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 200 -If 1 -Ii 30 -Iv 32 -Pn cbr -Pi 0 -Pf 0 -Po 3

 

r -t 100.000000000 -Hs 0 -Hd -2 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 200 -If 1 -Ii 30 -Iv 32 -Pn cbr -Pi 0 -Pf 0 -Po 3

 

s -t 100.000000000 -Hs 0 -Hd 1 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 30 -Iv 32 -Pn cbr -Pi 0 -Pf 0 -Po 3

 

r -t 100.005990500 -Hs 1 -Hd 1 -Ni 1 -Nx 500.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 1 -Ms 0 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 30 -Iv 32 -Pn cbr -Pi 0 -Pf 1 -Po 3

 

f -t 100.005990500 -Hs 1 -Hd 2 -Ni 1 -Nx 500.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 1 -Ms 0 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 30 -Iv 31 -Pn cbr -Pi 0 -Pf 1 -Po 3

 

r -t 100.011765000 -Hs 2 -Hd 2 -Ni 2 -Nx 650.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 13a -Md 2 -Ms 1 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 30 -Iv 31 -Pn cbr -Pi 0 -Pf 2 -Po 3

 

[範例-node movement and packet drop]

M 200.00000 1 (500.00, 500.00, 0.00), (500.00, 900.00), 2.00

 

d -t 302.284948727 -Hs 0 -Hd 1 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw CBK -Ma 13a -Md 1 -Ms 0 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 174 -Iv 32 -Pn cbr -Pi 98 -Pf 0 -Po 3

 

[範例-Route Path Recovery]

s -t 603.992660540 -Hs 0 -Hd -2 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 200 -If 1 -Ii 402 -Iv 32 -Pn cbr -Pi 249 -Pf 0 -Po 3

 

r -t 603.992660540 -Hs 0 -Hd -2 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 200 -If 1 -Ii 402 -Iv 32 -Pn cbr -Pi 249 -Pf 0 -Po 3

 

s -t 603.992660540 -Hs 0 -Hd 1 -Ni 0 -Nx 350.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 0 -Md 0 -Ms 0 -Mt 0 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 402 -Iv 32 -Pn cbr -Pi 249 -Pf 0 -Po 3

 

r -t 603.995974976 -Hs 1 -Hd 1 -Ni 1 -Nx 500.00 -Ny 692.01 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 1 -Ms 0 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 402 -Iv 32 -Pn cbr -Pi 249 -Pf 1 -Po 3

 

f -t 603.995974976 -Hs 1 -Hd 2 -Ni 1 -Nx 500.00 -Ny 692.01 -Nz 0.00 -Ne -1.000000 -Nl RTR -Nw --- -Ma 13a -Md 1 -Ms 0 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 402 -Iv 31 -Pn cbr -Pi 249 -Pf 1 -Po 3

 

r -t 603.999413413 -Hs 2 -Hd 2 -Ni 2 -Nx 650.00 -Ny 500.00 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw --- -Ma 13a -Md 2 -Ms 1 -Mt 800 -Is 0.0 -Id 2.0 -It cbr -Il 220 -If 1 -Ii 402 -Iv 31 -Pn cbr -Pi 249 -Pf 2 -Po 3

 

[參考文件]

1.http://www.eecs.wsu.edu/~smedidi/WirelessNS.ppt