Академический Документы
Профессиональный Документы
Культура Документы
Topics to be covered
Overview of NS2
Basic Architecture of NS2
NS2 Implementation
Tcl basics
Wired Scenario
Wireless Scenario
Overview of NS2
Useful for networking research work at packet level.
Provide substantial support to simulate bunch of
protocols like TCP, UDP, FTP, HTTP and routing
protocols.
Simulate wired and wireless network.
Is primarily Unix based.
Use TCL as its scripting language.
ns-2 creates a standard experiment environment in
research community.
3
Platforms supported
Most UNIX and UNIX-like systems
o
o
o
o
FreeBSD
Linux
Solaris
Ubuntu
Windows 98 onwards
o Cygwin required
NS2 Implementation
Tcl
Tcl (Tool Command Language) is a very powerful
dynamic programming language,
o Is easy to learn
o Suitable for a very wide range of uses,
Basic tcl
proc test {}
{
set a 43
set b 27
set c [expr $a + $b]
set d [expr [expr $a - $b] * $c]
for {set k 0} {$k < 10} {incr k}
{ puts k = $k
}
}
; a = 43
; b = 27
;c=a+b
; d = (a b) * c
; for (k=0; k<10; k++)
11
Fundamental Skills
n1
n2
12
14
Create a link
o link_type: e.g. simplex-link, duplex-link
o queue_type: e.g. DropTail, SFQ
Example:
$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail
16
18
19
20
Event Scheduling
Tell the simulator when to start and stop the traffic
flow
Schedule events
Example:
Cleaning up
Example:
$ns at 5.0 finish
21
Cleaning up
Define procedure to clean up
proc <proc_name> { } {
global <vaiable_list> <file_list>
$<simulator_name> flush-trace
close $<file_name>
exit 0
}
Example
proc finish { } {
global ns nt na
$ns flush-trace
close $na
close $nt
exit 0
}
22
23
Setting Properties
Setting Properties
Each traffic type comes with default values of traffic
parameters like rate and packet size which can be easily
overridden with the set command.
Example
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.005
24
Link Orientation
$ns duplex-link-op $n0 $n3 orient right-down
$ns duplex-link-op $n1 $n3 orient middle
Queue limit
$ns queue-limit $n3 $n4 50
25
27
Example 1
28
30
Creating events
31
We want the first CBR agent to start sending at 0.5 seconds and to stop at
4.5 seconds while the second CBR agent starts at 1.0 seconds and stops at
4.0 seconds.
Marking flows
Now add the following piece of code to your Tcl script, preferably at the
beginning after the simulator object has been created. This code allows you
to set different colors for each flow id.
32
Monitoring a queue
33
34
35
Trace file
Once the simulation is complete, we can see two
files: trace.tr, and nam.out.
In ns2, each time a packet moves from one node to
another, or onto a link, or into a buffer, etc., it gets
recorded in the trace file (trace.tr).
Each row represents one of these events and each
column has its own meaning.
Open file for NS tracing
set f [open out.tr w]
$ns trace-all $f
<event> <time> <from> <to> <pkt> <size> -+ 1
0 2 cbr 210 ------- 0 0.0
- 1
0 2 cbr 210 ------- 0 0.0
r 1.00234
0 2 cbr 210 ------- 0 0.0
<fid>
3.1 0
3.1 0
3.1 0
NamTrace File
Start nam with the command 'nam <nam-file>'
o Where '<nam-file>' is the name of a nam trace file
that was generated by ns.
o Network Animator
o A visual aid showing how packets flow along the
network
Wireless Scenario
38
Movement denition
Trace le analysis
39
Scenario definition
Area: 500m 500m
2 mobile nodes:
one moving from left to right, the other vice versa
40
Channel/WirelessChannel
Propagation/TwoRayGround
Antenna/OmniAntenna
LL
Queue/DropTail/PriQueue
50
Phy/WirelessPhy
Mac/802_11
DSDV
2
;# channel type
;# radio-propagation model
;# Antenna type
;# Link layer type
;# Interface queue type
;# max packet in ifq
;# network interface type
;# MAC type
;# ad-hoc routing protocol
;# number of mobilenodes
41
Basic Setup
Create simulator instance:
set ns [new Simulator]
-adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo a\
-channel $chan \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
44
45
Movement specification
Set start position:
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
46
Connection setup
TCP connection from node 0 to node 1
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
Create data source
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
47
49