Sie sind auf Seite 1von 44

NS-2 Tutorial

Biplab Sikdar
Associate Professor
Department of ECSE
Rensselaer Polytechnic institute
Troy, NY 12180 USA

Acknowledgements
Important input from students and
colleagues: Sampad Mishra, David Doria
Some slides based on those of Padmaparna
Haldar, Ya Xu, Aga Zhang and Polly Huang

Outline

NS fundamentals
Example walkthrough
Data Collection and Visualization
Simulating WiMAX scenarios
Conclusions

NS Background

Started as REAL in 1989


Discrete event packet level simulator
Written in C++ with Otcl frontend
Wired, wireless and emulation modes
Link layer and up for most wired
Additional lower layers for wireless

ATG Extensions
IEEE 802.16 MAC layer
TDD operation
UGS, rtPS and BE service classes
Block ARQ

OFDMA PHY layer


ITU PDP modules
PHY layer model library

Mobility extensions

Platforms
Most UNIX and UNIX-like systems

Linux
FreeBSD
SunOS/Solaris
HP/SGI (with some tweaking)

Windows 95/98/NT/ME/2000

NS: Basic Model


Discrete event simulator
Scheduler main controller of events
Scheduler clock - simulator virtual time
[$ns_ now] returns the current simulator time

Event queue - holds events in the order of


their firing times
Events have a firing time and a handler
function

Scheduler Operation
Event-driven simulator
Maintain a sorted event queue
Dequeue head event
Packet arrival

Assign event to its handler


At TCP agent

Handler processes event


Update Window

Enqueue more events in the event queue


Schedule delivery of ACK

Example
Event
Queue
Head_ ->

time_, uid_, next_, handler_


Dispatch

Deque

handler_ -> handle()


Reschedule
Insert

time_, uid_, next_, handler_

Four types of scheduler:


List: simple linked list,
order-preserving, O(N)

Heap: O(logN)

Calendar: hash-based,
fastest, default, O(1)

Real-time: subclass of list,


sync with real-time, O(N)

NS Architecture
Back-end C++
Protocols & Framework

Front-end Otcl (Object-Oriented Tcl)


Scenarios, configuration (one time stuff)

Split Object
Object created in otcl has a corresponding object in C+
+

Fast to run and easy to reconfigure


This tutorial only deals with tcl

Getting started
Download the latest code (all-in-one
package strongly recommended) from
http://www.isi.edu/nsnam/ns
Download 802.16 extensions
Compile and install
Basic skills:
Tcl
C++

C++ and OTcl Separation


C++ for data
Per packet action

OTcl for control


Configuration, one-time task

Advantage: Compromise between


composibility and speed
Disadvantage: Learning and debugging

OTcl and C++: The Duality

Pure OTcl
objects

Pure C++
objects

C++

C++/OTcl split
objects

ns

OTcl

Short Introduction to tcl


Variables
set v1 10
set v2 $v1

Array (String indexed)


a($i)

Printing
puts $filename string (default filename is stdout)

Arithmetic Expressions
set value [expr $v1+($v2 * 5.2)]

Control Structures
if {condition} then {.}
for {set i 0} {$i < 10} {incr i 2} {}

Procedures
proc proc_name {arg1 arg2} { }

NS Communication Model
Nodes
Hosts
Routers
Base stations

Links

Queue management
Queue monitoring

Agents
Protocols

TCP
4

TCP Sink

Elements of a NS Simulation

Create the event scheduler


[Turn on tracing]
Create network
Setup routing
[Insert errors]
Create transport connection
Create traffic

A Simple Script
# Create a topology
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
# Add Transport agents
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink
$tcp connect $tcpsink
# Add application
set ftp [new Application/FTP]
$ftp attach-agent $tcp

# Create a schedule
$ns at 0.5 "$ftp start"
$ns at 4.5 "$ftp stop"
#Run the simulation
$ns run
FTP

FTP

TCP

TCP

n0

1Mb
10ms

n1

Script (cont.)

Run your program


% ns Simple.tcl

Script (cont.)

Script (cont.)

Script (cont.)

Script (cont.)

Setting up NS Nodes
n0

n1

Port
Classifier

Port
Classifier

Addr
Classifier

entry_

0
1

Addr
Classifier
dmux_
entry_

classifier_

1
0
classifier_

set n0 [$ns node]


set n1 [$ns node]

dmux_

Setting up a Network Link


n0

head_

enqT_

n1

queue_
drophead_

deqT_

link_

drpT_

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

ttl_

n1
entry_

Routing
n0

n1

Port
Classifier

Port
Classifier

Addr
Classifier

entry_

0
1

Addr
Classifier
dmux_

Link n0-n1
entry_

classifier_

1
0
classifier_

Link n1-n0

dmux_

Transport
n0

n1

Port
Classifier

Port
Classifier

Addr
Classifier

entry_

0
1

dst_=1.0

Addr
Classifier

Agent/TCP
agents_

dmux_

Link n0-n1
entry_

classifier_

classifier_

Link n1-n0

set tcp [new Agent/TCP]


$ns attach-agent $n0 $tcp

1
0

0
dmux_

dst_=0.0
Agent/TCPSink
agents_

Application
n0

n1

Port
Classifier
Addr
Classifier

entry_

0
1

Application/FTP
dst_=1.0

Port
Classifier
Addr
Classifier

Agent/TCP
agents_

dmux_

Link n0-n1
entry_

classifier_

classifier_

Link n1-n0

set ftp [new Application/FTP]


$tcp attach-agent $ftp

1
0

0
dmux_

dst_=0.0
Agent/TCPSink
agents_

Packet Flow
n0

n1

Port
Classifier
Addr
Classifier

entry_

0
1

Application/FTP
dst_=1.0

Port
Classifier
Addr
Classifier

Agent/TCP

Link n0-n1
entry_

Link n1-n0

1
0

dst_=0.0
Agent/TCPSink

Tracing and Visualization


Trace packets on all links
$ns trace-all [open test.out w]
<event> <time> <from> <to> <pkt>
<dst> <seq> <attr>
+ 1 0 2 cbr 210 ------- 0 0.0
- 1 0 2 cbr 210 ------- 0 0.0
r 1.00234 0 2 cbr 210 -------

<size> -- <fid> <src>


3.1 0
3.1 0
0 0.0

0
0
3.1 0 0

Trace packets on all links in nam format


$ns namtrace-all [open test.nam w]
$ns namtrace-all-wireless [open wtest.nam w]

Visualization Tools
Nam-1 (Network AniMator Version)
Packet-level animation
Well-supported by ns

Xgraph
Convert trace output into xgraph format

Simulating WiMAX Scenarios


Extensions developed by RPI under the
aegis of AATG
Other parties: NIST, Washington University,
BUPT and ICU
Features
IEEE 802.16 MAC layer
OFDMA based PHY
Extensions for mobility

Define MAC/PHY Parameters


WimaxScheduler set dlratio_ 0.6
Mac/802_16 set fbandwidth_ 20e+6
set diuc 7
Mac/802_16 set rtg_ 20
Mac/802_16 set ttg_ 20
Mac/802_16 set frame_duration_ 0.005
Mac/802_16 set ITU_PDP_ 1
Phy/WirelessPhy/OFDMA set g_ 0.25
Phy/WirelessPhy set Pt_ 0.2
Phy/WirelessPhy set RXThresh_ 1.90546e-16
Phy/WirelessPhy set CSThresh_ [expr
0.9*[Phy/WirelessPhy set RXThresh_]]

Parameters for Wireless Nodes


set opt(chan)
Channel/WirelessChannel
set opt(prop)
Propagation/OFDMA
set opt(netif)
Phy/WirelessPhy/OFDMA
set opt(mac)
Mac/802_16
set opt(ifq)
Queue/DropTail/PriQueue
set opt(ant)
Antenna/OmniAntenna
set opt(ifqlen) 50
set opt(x)
670
set opt(y)
670
set prop_inst [$ns set propInstance_]
$prop_inst ITU_PDP VEHIC_A

Creating Base Stations


$ns node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType Mac/802_16/BS \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop)
\
-phyType $opt(netif) \
-channel $channel \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF

Base Station (cont.)


set bstation [$ns node 1.0.0]
$bstation random-motion 0
$bstation set X_ 300.0
$bstation set Y_ 300.0
$bstation set Z_ 0.0
[$bstation set mac_(0)] set-channel 0

Creating Mobile Stations


$ns node-config -macType Mac/802_16/SS \
-wiredRouting OFF \
-macTrace OFF
set wl_node [$ns node 1.0.0]
$wl_node random-motion 0
$wl_node base-station [AddrParams addr2id
[$bstation node-addr]]
$wl_node set X_ 100.0
$wl_node set Y_ 200.0
$wl_node set Z_ 0.0
[$wl_node set mac_(0)] set-channel 0
[$wl_node($i) set mac_(0)] setflow UL 10000 BE
700 2 0 0.05 15 1 0 0 0 0 0 0 0 0 0 0}

Other Utilities in NS
Nam editor
Available as part of nam-1

Tcl debugger
For source and documentation, see
http://www.isi.edu/nsnam/ns/ns-debugging.html

Topology generator
http://www.isi.edu/nsnam/ns/ns-topogen.html

Scenario generator
http://www.isi.edu/nsnam/ns/ns-scengeneration.html

Other NS Features
Other areas in wired domain

LANs
Diffserv
Multicast
Full TCP
Applications like web-caching

Wireless domain

Ad hoc routing
Mobile IP
Satellite networking
Directed diffusion (sensor networks)

Other NS Features
Emulator
Connect simulator in a real network
Can receive and send out live packets from/into
the real world

NS Related Resources
NS distribution download
http://www.isi.edu/nsnam/ns/ns-build.html

Installation problems and bug-fix


http://www.isi.edu/nsnam/ns/ns-problems.html

Ns-users mailing list


Ns-users@isi.edu
See http://www.isi.edu/nsnam/ns/ns-lists.html
Archives from above URL

More Resources
Marc Greis tutorial
http://www.isi.edu/nsnam/ns/tutorial

Ns-users archive
Ns-manual
http://www.isi.edu/nsnam/ns/ns-documentation.html

Tcl (Tool Command Language)


http://dev.scriptics.com/scripting
Practical programming in Tcl and Tk, Brent Welch

Otcl (MIT Object Tcl)


~otcl/doc/tutorial.html (in distribution)

Extra Slides

Otcl Basics
Creating a class
Class class_name
Class class_name superclass Base_class

Defining instance procedures


class_name instproc proc_name {args} {..}

Defining instance variables


$self instvar variable_name (inside a class method)

Creating an instance
set new_inst [new class_name]

Using value of an instance variable


$new_inst set v1 10 or set v2 [$new_inst set v1]

Packet Format
cmn header
header
data

ip header
tcp header
rtp header
trace header
...

ts_
ptype_
uid_
size_
iface_

Das könnte Ihnen auch gefallen