Вы находитесь на странице: 1из 24

3.2.

The script demystified


In this section we present a walkthrough of the script which is used to run the simulation for
analyzing the performance of routing protocols in MANETs. The script can be used as a skeleton
to simulate any kind of routing protocol desired.
(a) Set up the simulation and define the constants
The first step in the simulation is to define the wireless physical medium parameters and
initialize the simulation.
#---------------------------------------------------------------# Definition of the physical layer
#---------------------------------------------------------------set val(chan)

Channel/WirelessChannel

set val(prop)

Propagation/TwoRayGround

set val(netif)

Phy/WirelessPhy

set val(mac)

Mac/802_11

set val(ifq)

Queue/DropTail/PriQueue

set val(ll)

LL

set val(ant)

Antenna/OmniAntenna

#----------------------------------------------------------------# Scenario parameters


#-----------------------------------------------------------------set val(x)

2000

;# X dimension of the topography

set val(y)

2000

;# Y dimension of the topography

set val(ifqlen)

100

;# max packet in queue

set val(seed)

0.0

;#random seed

set val(adhocRouting)

[routing protocol]

set val(nn)

[no. of nodes]

set val(cp)

[traffic pattern file]

set val(sc)

[mobility scenario file]

set val(stop)

[simulation duration]

;# how many nodes are simulated

;# simulation time

(b) After setting up the initial parameters, we now create the simulator objects
#---------------------------------------------------------------------#

Set up simulator objects

#---------------------------------------------------------------------# create simulator instance


set ns_

[new Simulator]

# setup topography object


set topo

[new Topography]

# create trace object for ns and nam


set tracefd [open output trace file name w]
$ns_ use-newtrace
set namtrace

;# use the new wireless trace file format

[open nam trace file name w]

$ns_ trace-all $tracefd


$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
# define topology
$topo load_flatgrid $val(x) $val(y)
# Create God
set god_ [create-god $val(nn)]

NOTE:
(i) GOD or General Operations Director is a ns-2 simulator object, which is used to store global information about
the state of the environment, network, or nodes that an omniscient observer would have, but that should not be made
known to any participant in the simulation.
(ii) The load_flatgrid object is used to specify a 2-D terrain. Support is available for simulation of 3D terrains for
more realistic depiction of scenarios.

(c) Define node properties


Now we define the properties of a node in the ad hoc network through the following code
snippet$ns_ node-config -adhocRouting $val(adhocRouting) \
-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 ON

A unicast node in ns-2 has the following structure [ ]

Fig.3. Structure of a unicast node in ns-2 [from the ns manual]

By default, a node is specified as a unicast node. If a multicast protocol is desired, a separate


clause has to be specified during simulator initializationsetns[newSimulatormulticaston]

(d) Attach the nodes to the channel and specify their movements
#

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

to the channel.

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


set node_($i) [$ns_ node]
$node_($i) random-motion 0
}
# Define node movement model
puts "Loading connection pattern..."

;# disable random motion

source $val(cp)
# Define traffic model
puts "Loading scenario file..."
source $val(sc)
# Define node initial position in nam
for {set i 0} {$i < $val(nn)} {incr i} {
# 50 defines the node size in nam, must adjust it according to your

scenario

# The function must be called after mobility model is defined


# puts "Processing node $i"
$ns_ initial_node_pos $node_($i) 50
}
NOTE: The above code attaches the nodes to the channel and specifies the movement of the nodes and the traffic
flow between them. The default random motion of the nodes must be disabled.

(e) Finish up and run the simulation


#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
$ns_ at

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

# dump the initial simulation info to the trace file


puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"
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

Back Notes:
1) Pt_ is the antenna power, and it is used to set the distance,
for e.g.
Phy/WirelessPhy set Pt_ 7.214e-3

sets the antenna distance exactly at 100 m in FreeSpace and


TwoRayGround Propagation model.
You can also set the distance when you set RXThresh_
(also can be done from tcl level), but you don't have to use it. (Pt_ and
RXTresh_ are defined in wireless-phy.cc file) Pt_ is defined in wirelessphy.cc
2) rxPower and txPower are used in Energy Model
You can set the initial battery capacity, and rxPower is the power
used to receive a packet, and txPower i one used to transmit a
packet.
3) $ns_ at 0.0 "$node_(0) NodeLabel PAN Coor"
4) simple.tcl
set sim [new Simulator]
$sim at 1 puts \Hello World!\
$sim at 1.5 exit
$sim run
arches 74% ns simple.tcl
Hello World!
arches 75%
5) Discrete Event Simulation
6)
Where can you write Scripts?;
Top

After login into terminal, follow step given below


1. vi filename.tcl
It will open page, there you can write tcl scripts.
2. save file
Press esc-> colon (shift + semicolon) ->wq (save and quit)
It save the file
3. To run tcl script
ns filename.tcl

Basically NS 2 programming contains the following steps.


1.Create the event scheduler
2.Turn on tracing
3.Creating network
a) Computing setup routing - rtproto
b) Creating transport connection-Agents
c) Creating traffic-Applications
4. Monitoring
a) Visualization using nam
Note: every tcl script must write in small letters only except protocol agents i.e. TCP, FTP

Top

Template

Top

Every ns2 script starts with creating simulator object

set ns [new Simulator]

How to create node

set n0 [$ns node]


set n1 [$ns node]

Creating link

$ns duplex-link $n0 $n1 1Mb 10ms DropTail


This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the
bandwidth.1Megabit, a delay of 10ms and a DropTail queue.
How to use Trace?
We use simulator to see results. How is it achieved? Using trace
Two types of trace
1. generic trace
for use with xgraph, and other things
2. nam trace
for use with visualization
# open trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
Since we have started tracing, we should end also. For this we use finish procedure.
#Define a 'finish' procedure
proc finish {}
{
global ns tracefile nf

Follow the template tcl like Bible.


All communication between 2 agents.
Upper layer to lower layer, we attach agents.
Same layer in two nodes, we connect agents.
Agents are tcp, udp, telnet, cbretc
UDP communication
In UDP communication, data is flows from UDP agent to Null agent.

#Create a UDP agent and attach it to node n0


set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# create a null agent which act as traffic sink and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
# connect two agents with each other
$ns connect $udp0 $null0
TCP Communication
In TCP communication, data is flows from TCP agent to TCPsink agent.

# create Tcp agent and attach it to node no


set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
# create a tcpsink agent which act as traffic sink and attach it to node n1
set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink0
# connect two agents with each other
$ns connect $tcp0 $tcpsink0

Simple Examples

Top

So far we are not talking about data flow. Here we will explain with CBR protocol.
Example1:( CBR over UDP)
set ns [new Simulator]

Creatingns simulator object

set tracefile [open out.tr w]


$ns trace-all $tracefile

Open trace file

set nf [open out.nam w]


$ns namtrace-all $nf

Open the nam trace file

proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
exec nam out.nam &
exit 0
}

'finish' procedure

set n0 [$ns node]


set n1 [$ns node]
$ns simplex-link $n0 $n1 1Mb 10ms DropTail

Create your topology


- set n0 nodes.
- $ns duplex-links

set udp0 [new Agent/UDP]


$ns attach-agent $n0 $udp0
set cbr[new Application/Traffic/CBR]
$cbr attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
$ns connect $udp0 $null0

Create your agents


-transport layer and application layers stuff

$ns at 1.0 "$cbr start"


$ns at 3.0 "finish"

Scheduling Events
- $ns at 1.0 start
and at 3.0 finish

$ns run

starts the simulation.

Result
Before 1.0ms

After 1.0ms

Example 2:(CBR over TCP)

Top

set ns [new Simulator]

Creatingns simulator object

set tracefile [open out.tr w]


$ns trace-all $tracefile

Open trace file

set nf [open out.nam w]


$ns namtrace-all $nf

Open the nam trace file

proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
exec nam out.nam &
exit 0
}

'finish' procedure

set n0 [$ns node]


set n1 [$ns node]
$ns simplex-link $n0 $n1 1Mb 10ms DropTail

Create your topology


- set n0 nodes.
- $ns duplex-links

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $tcp0
set tcpsink0 [new Agent/TCPSink]

Create your agents


-transport layer and application layers stuff

$ns attach-agent $n1 $tcpsink0


$ns connect $tcp0 $tcpsink0
$ns at 1.0 "$cbr start"
$ns at 3.0 "finish"

Scheduling Events
- $ns at 1.0 start
and at 3.0 finish

$ns run

starts the simulation.

Result:
Before 1.0ms

After 1.0ms

Example3:(FTP over TCP)

Top

set ns [new Simulator]

Creatingns simulator object

set tracefile [open out.tr w]


$ns trace-all $tracefile

Open trace file

set nf [open out.nam w]


$ns namtrace-all $nf

Open the nam trace file

proc finish {}
{
global ns tracefile nf
$ns flush-trace
close $nf
close $tracefile
exec nam out.nam &
exit 0
}

'finish' procedure

set n0 [$ns node]


set n1 [$ns node]
$ns simplex-link $n0 $n1 1Mb 10ms DropTail

Create your topology


- set n0 nodes.
- $ns duplex-links

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set tcpsink0 [new Agent/TCPSink]

Create your agents


-transport layer and application layers stuff

$ns attach-agent $n1 $tcpsink0


$ns connect $tcp0 $tcpsink0
$ns at 1.0 "$ftp0 start"
$ns at 3.0 "finish"

Scheduling Events
- $ns at 1.0 start
and at 3.0 finish

$ns run

starts the simulation.

Result:
Here we are using FTP Application agent as a traffic generator instead of CBR. The difference is CBR traffic
generator will produce constant bit rate where as FTP traffic generator produces maximum available bit rate.
We are writing code
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0

and Similarly for telnet also


set telneto [new Application/TELNET]
$ftp0 attach-agent $telnet0
Instead of CBR
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $tcp0
Before 1.0ms

After 1.0ms

# 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) 50 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 1000 ;# X dimension of topography
set val(y) 1000 ;# Y dimension of topography
set val(stop) 150 ;# time of simulation end
set ns [new Simulator]

set tracefd [open simple.tr w]


set namtrace [open simwrls.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)

# configure the 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
for {set i 0} {$i < $val(nn) } { incr i } {
set n($i) [$ns node]
}
# Provide initial location of mobilenodes
$n(0) set X_ 347.0
$n(0) set Y_ 3.0
$n(0) set Z_ 0.0
$n(1) set X_ 345.0

$n(1) set Y_ 36.0


$n(1) set Z_ 0.0
$n(2) set X_ 330.0
$n(2) set Y_ 121.0
$n(2) set Z_ 0.0
$n(3) set X_ 316.0
$n(3) set Y_ 152.0
$n(3) set Z_ 0.0
$n(4) set X_ 246.0
$n(4) set Y_ 90.0
$n(4) set Z_ 0.0
$n(5) set X_ 379.0
$n(5) set Y_ 6.0
$n(5) set Z_ 0.0

# Set a TCP connection between n(1) and n(31)


set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n(1) $tcp
$ns attach-agent $n(31) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"
# Set a TCP connection between n(31) and n(43)
set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n(31) $tcp
$ns attach-agent $n(43) $sink
$ns connect $tcp $sink
#defining heads

$ns at 0.0 "$n(0) label CH"


$ns at 0.0 "$n(1) label Source"
#$ns at 0.0 "$n(2) label N2"

$ns at 10.0 "$n(5) setdest 785.0 228.0 5.0"


$ns at 13.0 "$n(26) setdest 700.0 20.0 5.0"
$ns at 15.0 "$n(14) setdest 115.0 85.0 5.0"
#Color change while moving from one group to another
$ns at 73.0 "$n(2) delete-mark N2"
$ns at 73.0 "$n(2) add-mark N2 pink circle"
$ns at 124.0 "$n(11) delete-mark N11"
$ns at 124.0 "$n(11) add-mark N11 purple circle"
$ns at 103.0 "$n(5) delete-mark N5"
$ns at 103.0 "$n(5) add-mark N5 white circle"
$ns at 87.0 "$n(26) delete-mark N26"
$ns at 87.0 "$n(26) add-mark N26 yellow circle"
$ns at 92.0 "$n(14) delete-mark N14"
$ns at 92.0 "$n(14) add-mark N14 green circle"
# Define node initial position in nam
for {set i 0} {$i < $val(nn)} { incr i } {
# 20 defines the node size for nam
$ns initial_node_pos $n($i) 20
}
# Telling nodes when the simulation ends
for {set i 0} {$i < $val(nn) } { incr i } {
$ns at $val(stop) "$n($i) reset";
}
# ending nam and the simulation
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 150.01 "puts \"end simulation\" ; $ns halt"
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd

close $namtrace
exec nam simwrls.nam &
}
$ns run
Tcl example]
set val(chan)
set val(prop)
set val(netif)
set val(mac)
set val(ifq)
set val(ll)
set val(ant)
set val(ifqlen)

node0 ------- node1


Channel/WirelessChannel ;# channel type
Propagation/TwoRayGround ;# radio-propagation model
Phy/WirelessPhy
;# network interface type
Mac/802_11
;# MAC type
Queue/DropTail/PriQueue ;# interface queue type
LL
;# link layer type
Antenna/OmniAntenna
;# antenna model
32768
;# max packet in ifq

set val(rp) DumbAgent


set ns [new Simulator]
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0
Phy/WirelessPhy set CPThresh_ 10.0
Phy/WirelessPhy set CSThresh_ 1.559e-11
Phy/WirelessPhy set RXThresh_ 3.652e-10
Phy/WirelessPhy set bandwidth_ 2e6
Phy/WirelessPhy set Pt_ 0.28183815
Phy/WirelessPhy set freq_ 914e+6
Phy/WirelessPhy set L_ 1.0
set f [open test.tr w]
$ns trace-all $f
$ns eventtrace-all
set nf [open test.nam w]
$ns namtrace-all-wireless $nf 500 500
# set up topography object
set topo
[new Topography]

$topo load_flatgrid 500 500


# Create God
create-god 2
# create channel
set chan [new $val(chan)]
$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 OFF \
-macTrace ON \
-movementTrace OFF
for {set i 0} {$i < 2} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
}
$node_(0) set X_ 30.0
$node_(0) set Y_ 30.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 280.0
$node_(1) set Y_ 30.0
$node_(1) set Z_ 0.0
set udp [new Agent/mUDP]
#set the sender trace file name to sd
$udp set_filename sd
$ns attach-agent $node_(0) $udp
set null [new Agent/mUdpSink]
#set the receiver filename to rd
$null set_filename rd
$ns attach-agent $node_(1) $null
$ns connect $udp $null

set cbr [new Application/Traffic/CBR]


$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1Mb
$cbr set random_ false
$ns at 0.0 "$cbr start"
$ns at 15.0 "$cbr stop"
for {set i 0} {$i < 2} {incr i} {
$ns initial_node_pos $node_($i) 30
$ns at 20.0 "$node_($i) reset";
}
$ns at 20.0 "finish"
$ns at 20.1 "puts \"NS EXITING...\"; $ns halt"
#INSERT ANNOTATIONS HERE
proc finish {} {
global ns f nf val
$ns flush-trace
close $f
close $nf
}
puts "Starting Simulation..."
$ns run
[compile threshold.cc]
$ cd ns/indep-utils/propagation/
$ g++ -lm threshold.cc -o threshold
[example]
$ threshold -m TwoRayGround 250
distance = 250
propagation model: TwoRayGround
Selected parameters:
transmit power: 0.281838
frequency: 9.14e+08
transmit antenna gain: 1
receive antenna gain: 1
system loss: 1
transmit antenna height: 1.5
receive antenna height: 1.5

Receiving threshold RXThresh_ is: 3.65262e-10


$
p.s. (0.281838151.521.52) / (2504)=3.652e-10
the more information can be found at http://140.116.164.80/~smallko/ns2/11b.htm
[usage]
$ threshold
USAGE: find receiving threshold for certain communication range
(distance)
SYNOPSIS: threshold -m <propagation-model> [other-options]
distance
<propagation-model>: FreeSpace, TwoRayGround or Shadowing
[other-options]: set parameters other than default values:
Common parameters:
-Pt <transmit-power>
-fr <frequency>
-Gt <transmit-antenna-gain>
-Gr <receive-antenna-gain>
-L <system-loss>
For two-ray ground model:
-ht <transmit-antenna-height>
-hr <receive-antenna-height>
For shadowing model:
-pl <path-loss-exponent>
-std <shadowing-deviation>
-d0 <reference-distance>
-r <receiving-rate>

How to generate nodes position, the initial network topology?


I used this script: initial_topology.tcl
The initial_topology.tcl script requires four parameters to be input.
"1.number of node >0" "2. X coordinate" "3. Y coordinate" "4. output file name"
Ex:
# ns initial_topology.tcl 100 1900 1900 topology_100_1900

this example will generate positions and transmission range for 100 nodes in
1900x1900m area in the file topology_100_1900.
$node_(0) set X_ 1267.6484042162301
$node_(0) set Y_ 47.771891694409724

$node_(0)
$node_(0)
$node_(1)
$node_(1)
$node_(1)
$node_(1)

set Z_
radius
set X_
set Y_
set Z_
radius

0.0
214.2403287413718
804.46613477750975
1733.0320414775199
0.0
204.81749854740571

Вам также может понравиться