You are on page 1of 40

NS2 - Wireless Network

Ming-Feng Yang
2
Outline
Basic wireless model
Extensions wireless model
Example - Ad hoc network
Example WLAN
Example Mobile IP
3
Basic wireless model
Creating wireless topology
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)
# where opt(x) and opt(y) are the boundaries used in simulation

4
Basic wireless model
Configure the node
$ns_ node-config -adhocRouting DSDV or DSR or TORA or AODV \
-topoInstance $topo \
-addressType flat or hierarchical or expanded\
-wiredRouting ON \
-mobileIP ON \
-llType LL \
-macType Mac/802_11 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-ifqType Queue/DropTail/PriQueue \
-ifqLen <integer> \
-agentTrace ON or OFF \
-routerTrace ON or OFF \
-macTrace ON or OFF \
-movementTrace ON or OFF
MobileIP
Wired-cum-wireless
5
Basic wireless model
Node movement
$node set X_ <x1>
$node set Y_ <y1>
$node set Z_ <z1>
# Start-position
$ns at $time $node setdest <x2> <y2> <speed>
# Future destinations

6
Basic wireless model
New traces format
$ns use-newtrace
s -t 0.029290548 -Hs 1 -Hd -1 -Ni 1 -Nx 390.00 -Ny 385.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 0 -Iv 32
event
s,r,d,f
time
id for
next hop
next hop
destination
node
id
nodes
x-coordinate
nodes
z-coordinate
nodes
y-coordinate
node
energy level
trace
level
reason for
this event
duration
dests
ethernet addr
sources
ethernet addr
ethernet
type
sources
addr.port
dests
addr.port
packet
type
packet
size
flow
id
unique
id
ttl
value -H: next hop info
-N: node info
-M: MAC level info
-I: IP level info
7
Basic wireless model
Movement scenario generator
cd ns-allinone-2.27/ns-2.27/indep-utils/cmu-scen-gen/setdest
./setdest -n 3 -p 2.0 -s 20.0 -t 300 -x 600 -y 600 > pattern-file
Num-of-nodes Pause-time Max-speed Sim-time Topo-boundary
source pattern-file
set god_ [God instance]
$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743 170.519203111152 3.371785899154
$ns_ at 51.000000000000 "$node_(1) setdest 221.826585497093 80.855495003839 14.909259208114
$ns_ at 33.000000000000 "$node_(0) setdest 89.663708107313 283.494644426442 19.153832288917
$god_ set-dist 1 2 2
$god_ set-dist 0 2 3
$god_ set-dist 0 1 1
$node_(2) set Z_ 0.000000000000
$node_(2) set Y_ 199.373306816804
$node_(2) set X_ 591.256560093833
$node_(1) set Z_ 0.000000000000
$node_(1) set Y_ 345.357731779204
$node_(1) set X_ 257.046298323157
$node_(0) set Z_ 0.000000000000
$node_(0) set Y_ 239.438009831261
$node_(0) set X_ 83.364418416244
:
8
Basic wireless model
Traffic scenario generator
ns cbrgen.tcl -type tcp -nn 25 -seed 0.0 -mc 8 > pattern-file
ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 8 -rate 4.0 > pattern-file
CBR-or-TCP Num-of-nodes Random-seed Max-connection CBR-rate
source pattern-file
# nodes: 10, max conn: 8, send rate: 0.25, seed: 1.0
# 1 connecting to 2 at time 2.5568388786897245
set udp_(0) [new Agent/UDP]
$ns_ attach-agent $node_(1) $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_ 512
$cbr_(0) set interval_ 0.25
$cbr_(0) set random_ 1
$cbr_(0) set maxpkts_ 10000
$cbr_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $null_(0)
$ns_ at 2.5568388786897245 "$cbr_(0) start
:
9
Extensions wireless model
wired-cum-wireless
$ns_ node-config -addressType hierarchical
AddrParams set domain_num_ 2
lappend cluster_num 2 1
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 4
AddrParams set nodes_num_ $eilastlevel
set temp {0.0.0 0.1.0}
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns_ node [lindex $temp $i]] }
$ns_ node-config -wiredRouting ON
set temp {1.0.0 1.0.1 1.0.2 1.0.3}
set BS(0) [$ns_ node [lindex $temp 0]]
$BS(0) random-motion 0
$ns_ node-config -wiredRouting OFF
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns_ node [lindex $temp [expr $j+1]] ]
$node_($j) base-station [AddrParams addr2id [$BS(0) node-addr]] }
W(0)
W(1)
BS(0)
node_(1)
node_(0)
node_(2)
domain 0
domain 1
cluster 0
cluster 1
cluster 0
0,0,0
0,1,0
1,0,0
1,0,1
1,0,2
1,0,3
10
Extensions wireless model
Mobile IP
$ns_ node-config -mobileIP ON
set HA [$ns_ node 1.0.0]
set FA [$ns_ node 2.0.0]
$HA random-motion 0
$FA random-motion 0
set MH [$ns_ node 1.0.1]
set node_(0) $MH
set HAaddress [AddrParams addr2id [$HA node-addr]]
[$MH set regagent_] set home_agent_ $HAaddress
W(0)
W(1)
HA
MH
FA
11
Example - Ad hoc network
Traffic: node(0) to node(1)
12
Example - Ad hoc network
Traffic: node(0) to node(1)
13
Example - Ad hoc network
Traffic: node(0) to node(1)
14
Example - Ad hoc network
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) 3 ;# number of nodes
set val(rp) DSDV ;# routing protocol
set val(x) 500 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set val(stop) 150 ;# time of simulation end
# Define options
set ns [new Simulator]
set tracefd [open out.tr w]
set namtrace [open out.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
15
Example - Ad hoc network
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Create a topology object that keeps track of movements of nodes within the topological boundary
create-god $val(nn)
# God object is used to store global information about the state of the environment, network or 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 ON
# Configure the nodes
16
Example - Ad hoc network
for {set i 0} {$i < $val(nn) } { incr i } {
set node_($i) [$ns node]
$node_($i) random-motion 0 ;# disable random motion
}
# Create the specified number of nodes [$val(nn)] and "attach" them to the channel
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 490.0
$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 150.0
$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0
# Provide initial location of nodes
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 30.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 100.0 "$node_(0) setdest 480.0 300.0 5.0"
# Generation of movements
17
Example - Ad hoc network
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$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
$ns at 10.0 "$ftp start"
# Set up a TCP connection between node_(0) and node_(1)
for {set i 0} {$i < $val(nn)} { incr i } {
$ns initial_node_pos $node_($i) 40
}
# Define node initial position in NAM, 40 defines the node size for NAM
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$node_($i) reset";
}
# Telling nodes when the simulation ends
18
Example - Ad hoc network
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam out.nam &
exit 0
}
# ending nam and the simulation
$ns run
19
Example - WLAN
Traffic: node(3) to node(0), node(1) to node(5),
20
Example - WLAN
Traffic: node(3) to node(0), node(1) to node(5),
21
Example - WLAN
Traffic: node(3) to node(0), node(1) to node(5),
22
Example - WLAN
Traffic: node(3) to node(0), node(1) to node(5),
23
Example - WLAN
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 3 ;# number of mobilenodes
set opt(rp) DSDV ;# routing protocol
set opt(x) 300 ;# x coordinate of topology
set opt(y) 300 ;# y coordinate of topology
set opt(stop) 30 ;# time to stop simulation
set opt(ftp1-start) 5.0
set opt(ftp2-start) 5.0
set num_wired_nodes 2
set num_bs_nodes 1
# Define options
24
Example - WLAN
set ns [new Simulator]
set tracefd [open out.tr w]
set namtrace [open out.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $opt(x) $opt(y)

$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2 ;# number of domains
lappend cluster_num 2 1 ;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 4 ;# number of nodes in each cluster of each domain
AddrParams set nodes_num_ $eilastlevel
# set up for hierarchical routing
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)
# Create a topology object that keeps track of movements of nodes within the topological boundary
create-god [expr $opt(nn) + $num_bs_nodes]
# God object is used to store global information about the state of the environment, network or nodes
25
Example - WLAN
set temp {0.0.0 0.1.0} ;# hierarchical addresses for wired domain
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns node [lindex $temp $i]] }
#Create wired nodes
$ns node-config -adhocRouting $opt(rp) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
# Configure for base-station node
26
Example - WLAN
set temp {1.0.0 1.0.1 1.0.2 1.0.3} ;# hierarchical addresses to be used for wireless domain
set BS(0) [$ns node [lindex $temp 0]]
$BS(0) random-motion 0 ;# disable random motion
#create base-station node
$BS(0) set X_ 150.0
$BS(0) set Y_ 250.0
$BS(0) set Z_ 0.0
# provide some co-ord (fixed) to base-station node
$ns node-config -wiredRouting OFF
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns node [lindex $temp [expr $j+1]] ]
$node_($j) base-station [AddrParams addr2id [$BS(0) node-addr]]
}
# create mobilenodes in the same domain as BS(0) and configure for mobilenodes
$ns duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns duplex-link $W(1) $BS(0) 5Mb 2ms DropTail
#create links between wired and BS nodes
$ns duplex-link-op $W(0) $W(1) orient down
$ns duplex-link-op $W(1) $BS(0) orient left-down
27
Example - WLAN
set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp1
$ns attach-agent $W(0) $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at $opt(ftp1-start) "$ftp1 start"
# setup first TCP connection
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns attach-agent $W(1) $tcp2
$ns attach-agent $node_(2) $sink2
$ns connect $tcp2 $sink2
$tcp2 set fid_ 2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns at $opt(ftp2-start) "$ftp2 start"
# setup second TCP connection
28
Example - WLAN
$node_(0) set X_ 120.0
$node_(0) set Y_ 220.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 250.0
$node_(1) set Y_ 250.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 20.0
$node_(2) set Y_ 20.0
$node_(2) set Z_ 0.0
# Provide initial location of nodes
$ns at 10.0 "$node_(0) setdest 290.0 10.0 30.0"
$ns at 15.0 "$node_(1) setdest 50.0 100.0 20.0"
$ns at 5.0 "$node_(2) setdest 170.0 240.0 30.0"
# Generation of movements
29
Example - WLAN
for {set i } {$i < $opt(nn) } {incr i} {
$ns at $opt(stop) "$node_($i) reset";
}
$ns at $opt(stop) "$BS(0) reset";
# Tell all nodes when the simulation ends
$ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"
$ns at $opt(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam out.nam &
exit 0
}
# ending nam and the simulation
$ns run
30
Example Mobile IP
Traffic: node(0) to node(4)
31
Example Mobile IP
Traffic: node(0) to node(4)
32
Example Mobile IP
Traffic: node(0) to node(4)
33
Example Mobile IP
Traffic: node(0) to node(4) Traffic: node(0) to node(4)
34
Example Mobile IP
Traffic: node(0) to node(4)
35
Example Mobile IP
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 1 ;# number of mobilenodes
set opt(rp) DSDV ;# routing protocol
set opt(x) 500.0 ;# x coordinate of topology
set opt(y) 500.0 ;# y coordinate of topology
set opt(stop) 50.0 ;# time to stop simulation
set opt(ftp1-start) 0.0
set num_wired_nodes 2
set num_bs_nodes 2
# Define options
36
Example Mobile IP
set ns [new Simulator]
set tracefd [open out.tr w]
set namtrace [open out.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $opt(x) $opt(y)
$ns color 1 Red

$ns node-config -addressType hierarchical
AddrParams set domain_num_ 3 ;# number of domains
lappend cluster_num 2 1 1 ;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 2 1 ;# number of nodes in each cluster of each domain
AddrParams set nodes_num_ $eilastlevel
# set up for hierarchical routing
set topo [new Topography]
$topo load_flatgrid $opt(x) $opt(y)
# Create a topology object that keeps track of movements of nodes within the topological boundary
37
Example Mobile IP
create-god [expr $opt(nn) + $num_bs_nodes]
# God object is used to store global information about the state of the environment, network or nodes
set temp {0.0.0 0.1.0} ;# hierarchical addresses for wired domain
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns node [lindex $temp $i]] }
#Create wired nodes
$ns node-config -mobileIP ON \
-adhocRouting $opt(rp) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF
# Configure for ForeignAgent and HomeAgent nodes
38
Example Mobile IP
set HA_ [$ns node 1.0.0]
set FA_ [$ns node 2.0.0]
$HA_ random-motion 0
$FA_ random-motion 0
# Create HA and FA
$HA_ set X_ 100.0
$HA_ set Y_ 200.0
$HA_ set Z_ 0.0
$FA_ set X_ 400.0
$FA_ set Y_ 200.0
$FA_ set Z_ 0.0
# Position (fixed) for base-station nodes (HA & FA).
$ns node-config -wiredRouting OFF
set MH_ [$ns node 1.0.1]
set HAaddress [AddrParams addr2id [$HA_ node-addr]]
[$MH_ set regagent_] set home_agent_ $HAaddress
# note address of MH indicates its in the same domain as HA.
$ns duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns duplex-link $W(1) $HA_ 5Mb 2ms DropTail
$ns duplex-link $W(1) $FA_ 5Mb 2ms DropTail
# create links between wired and BaseStation nodes
39
Example Mobile IP
set tcp1 [new Agent/TCP]
set sink1 [new Agent/TCPSink]
$ns attach-agent $W(0) $tcp1
$ns attach-agent $MH_ $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at $opt(ftp1-start) "$ftp1 start"
# setup TCP connections between a wired node and the MobileHost
$MH_ set X_ 100.0
$MH_ set Y_ 170.0
$MH_ set Z_ 0.0
# Provide initial location of MH
$ns at 5.0 "$MH_ setdest 400.0 170.0 20.0"
# MH starts to move towards FA
$ns at 25.0 "$MH_ setdest 100.0 170.0 20.0"
# goes back to HA
$ns initial_node_pos $MH_ 10
# Define initial node position in NAM
40
Example Mobile IP
$ns at $opt(stop) "$MH_ reset";
$ns at $opt(stop) "$HA_ reset";
$ns at $opt(stop) "$FA_ reset";
# Tell all nodes when the simulation ends
$ns at $opt(stop) "$ns nam-end-wireless $opt(stop)"
$ns at $opt(stop) "stop"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam out.nam &
exit 0
}
# ending nam and the simulation
$ns run

You might also like