Vous êtes sur la page 1sur 47

Network Simulator-2 (NS2)

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

Basic architecture of NS2

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,

including web and desktop applications,


networking,
administration,
testing and many more.

o Open source and business-friendly


o Tcl is a feature yet evolving language that is truly cross
platform, easily deployed and highly extensible.
8

Creating simulator instance


set ns [new Simulator]
o Usually the first non-comment statement in
ns-2 script
o Initialize the packet format
o Create a scheduler (default is a calendar
scheduler)
o Create a null agent

Hello World - Batch mode


Simple.tcl:
set ns [new Simulator]
$ns at 1 puts \Hello World!\
$ns at 1.5 exit
$ns run
[Linux]$ ns simple.tcl
Hello World!
[Linux]$
10

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

set ns [new Simulator]


set n1 [new Node]
set n2 [new Node]

n1

n2

$ns duplex-link $n1 $n2 5Mb 2ms DropTail

12

Structure of a typical ns script


Creating event scheduler
Opening trace files
Creating topology
Defining a traffic flow for application

Event Scheduling (Start & stop the traffic flows)


Cleaning and Executing
13

Creating Event Scheduler


Create Scheduler
Example: set ns [new Simulator]

14

NODE and LINK Creation


Create node
Example:
set n0 [$ns node]
set n1 [$ns node]

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

Defining a Traffic Flow


To define the traffic flow, we need to designate
the nodes as source or destination and decide
the type of application/traffic
Steps
1. Define the source agent
2. Designate which node assumes the agent we defined
3. Define the traffic/application type and attach it to
source agent
4. Define the destination agent and attach it to
destination node
5. Connect the agents
17

Defining a Traffic Flow


1. Define Source Agent
o src_agent_type: e.g. TCP, UDP

Example: set tcp0 [new Agent/TCP]

2. Designate which node assumes the agent we


defined
Example: $ns attach-agent $n0 $tcp0

18

Defining a Traffic Flow


3. Define the traffic/application type
o traffic_type: e.g. CBR
o application_type: e.g. FTP

Attach it to source agent


Example: set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0

19

Defining a Traffic Flow


4. Define the destination agent
o dest_agent_type: TCPSink, Null

Attach it to destination node


Example: set sink0 [new Agent/TCPSink]

$ns attach-agent $n1 $sink0

5. Connect the agents


Example: $ns connect $tcp0 $sink0

20

Event Scheduling
Tell the simulator when to start and stop the traffic
flow
Schedule events
Example:

$ns at 0.0 $ftp0 start


$ns at 4.0 $ftp0 stop

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

Executing the Simulator


Run the simulation
Example: $ns run

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

Queuing policy, Link orientation, Queue limit


Queuing policy
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms RED
$ns duplex-link $n0 $n3 1Mb 10ms SFQ

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

Flow id, color


Set flow ids for packet flows
$udp0 set fid_ 1
$udp1 set fid_ 2
$udp2 set fid_ 3

Set color for various flows


$ns color 1 Red
$ns color 2 Green
$ns color 3 Blue
26

27

Example 1

28

# Define a 'finish' procedure


set ns [new Simulator]
proc finish {} {
set nf [open out.nam w]
global ns nf
$ns namtrace-all $nf
$ns flush-trace
# Close the NAM trace file
set n0 [$ns node]
close $nf
set n1 [$ns node]
# Execute NAM on the trace file
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
exec nam out.nam &
exit 0 }
# Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a Null agent (acts as traffic sink) & attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0 005
$cbr0 attach-agent $udp0
$ns connect $udp0 $null0
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop
29
$ns at 5.0 "finish
$ns run

Example 2: Creating Topology


Create four nodes

Create three duplex links between the nodes.

To have more control over the layout:

30

Create two UDP agents with CBR traffic


sources and attach them to the nodes n0
and n1. Then create a Null agent and
attach it to node n3.

Creating events

31

The two CBR agents have to be connected to the Null agent.

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

You can see the packets


in the queue now, and
after a while you can even
see how the packets are
being dropped, though
only blue packets are
being dropped.

33

To improve the queueing by using a SFQ (stochastic fair queueing)


queue for the link from n2 to n3.

The queueing should be 'fair' now.


The same amount of blue and red
packets should be dropped.

34

- Generate the following topology by setting the traffic flow


from n0 to n3 and from n1 to n2.
- Illustrate one flow with blue and the other one with red.
- Monitor the queue for the link from n4 to n5 after 1sec

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

<src> <dst> <seq> <attr>


0
0
36
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

Open file for nam tracing


set nf [open out.nam w]
$ns namtrace-all $nf
37

Wireless Scenario

38

Creating a wireless scenario


Node conguration
Basic simulation setup

Movement denition
Trace le analysis

39

Scenario definition
Area: 500m 500m
2 mobile nodes:
one moving from left to right, the other vice versa

A TCP connection between them


Expectation: the nodes exchange data as long
as they are in radio range of each other

40

Specify components of the mobile node


set val(chan)
set val(prop)
set val(ant)
set val(ll)
set val(ifq)
set val(ifqlen)
set val(netif)
set val(mac)
set val(rp)
set val(nn)

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]

Open trace file and activate it:


set tracefd [open wireless.tr w]
$ns trace-all $tracefd

Create topography and channel:


set topo [new Topography]
$topo load_flatgrid 500 500
set chan [new $val(chan)]

Create the GOD object (General Operations Director)


create-god $val(nn)
42

The GOD object


Stores information that an omniscient observer
would have
number of nodes
connectivity information which else would have to be
calculated on-the-fly

One single GOD object per simulation


Needed by the MAC layer
Gives the possibility to evaluate e.g. optimality of
routes
43

Configuration of the mobile 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) \
-topoInstance $topo a\
-channel $chan \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF

44

Creating the nodes


After setting the configuration options, the nodes are
created:
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0
}

The node movement will be explicitly provided in the


following.

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

Node 1 starts to move towards node 0:


$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"

Node 1 then starts to move away from node 0:


$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.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

Starting the scheduler


for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd
close $tracefd
}
puts "Starting Simulation..."
$ns_ run
48

Analysis of the trace file


Format:
event time node level --- pktnr type pktsize [MAC info] ...
Data packet:
s 100.000000000 _0_ AGT --- 21 tcp 40 [0 0 0 0] ------- [...]
r 100.000000000 _0_ RTR --- 21 tcp 40 [0 0 0 0] ------- [...]
s 100.000000000 _0_ RTR --- 21 tcp 60 [0 0 0 0] ------- [...]
r 100.000644018 _1_ AGT --- 21 tcp 60 [13a 1 0 800] ------- [...]
Acknowledgment:
s 100.000644018 _1_ AGT --- 22 ack 40 [0 0 0 0] ------- [...]
r 100.000644018 _1_ RTR --- 22 ack 40 [0 0 0 0] ------- [...]
s 100.000644018 _1_ RTR --- 22 ack 60 [0 0 0 0] ------- [...]
r 100.001552036 _0_ AGT --- 22 ack 60 [13a 0 1 800] ------- [...]

49

Vous aimerez peut-être aussi