27
NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Embed Size (px)

Citation preview

Page 1: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

NS2 Tutorial – Part I

Internet Computing Laboratory @ KUT

Youn-Hee Han

Page 2: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure2

VINT and NS (Network Simulator)

VINT Virtual InterNet Testbed a collaborative project between USC/ISI,

LBL/UC Berkeley, and Xerox PARC Goal

to extend the ns simulator so that network researchers can study the complex interactions between network protocols (e.g., unicast routing, multicast routing, TCP, reliable multicast, integrated services, etc.) in complex topologies and with a rich set of traffic generators.

ISI/USC: University of Southern California/Information Sciences InstituteLBL/UC Berkeley: Lawrence Berkeley National Laboratory/University of California, Berkeley

Page 3: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure3

What is NS?

http://www.isi.edu/nsnam

NS version2 is a discrete-event driven and object-oriented network simulator

Events Packet and Timer

Characteristics Freely distributed and open source Discrete event simulator Packet-level Event Wired and wireless Network Stacks from Layer 1 to Layer 7 Node mobility

Page 4: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure4

What is NS?

History Columbia NEST UCB REAL ns-1

Modified from REAL network simulator

ns-2 100K lines of C++ 70K lines of OTcl 30K lines of test suite 20K lines of documentation

Page 5: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure5

Functionality of NSWired world

Point-to-point link, LAN Unicast/multicast routing QoS

Intserve, Diffserv Transport

TCP, UDP Congestion control

Application layer Web Caching Mulimedia

Wireless Mobile IP Ad hoc routing

Tracing, visualization, various utilities

Page 6: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure6

NS Flatform

Most UNIX and UNIX-like systems Linux

It is preferred Remote access with Xmanager

FreeBSD or NetBSD Sun Solaris

Window 95/98/NT with Cygwin

Some of features are not supported

Emulation only for FreeBSD for now

VMWare Window host Linux guest

Page 7: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure7

NS Components

NS – SimulatorNAM – Network AniMator

visual demonstration of NS output

Preprocessing Handwritten TCL or Topology generator

Post analysis Trace analysis using Perl/TCL/AWK/GnuProt/MATLAB

tcl script(specificationof experiment)

ns-2 trace file (output) nam

We just saw this… Now I will show you this…

Page 8: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure8

Current Status

Users from approximately 600 institutes 50 countries

Releases Periodic releases (currently 2.29_3) NS3 project just started (July 1, 2006) Available from: USC (University of Southern

California)/ISI (Information Sciences Institute), UC Berkeley, UK mirror

Page 9: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure9

NS2 Language

NS2 is written in C++ and OTcl OTcl = Tcl + OO C++ implements the code that executed frequently

It implements the system behavior of NS2 (e.g., routing) OTcl configures the system (e.g., topology).

Object-oriented (C++, OTcl) Fine-grained object composition

Modular approach

Page 10: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure10

C++ and OTcl Separation

C++ for “data/behavior” NS2 Core & packet processing Fast Execution Good for…

Frequent Access & Less Compilation

OTcl for “control” Periodic or triggered action Simulation Scenario setup Good for…

Less Access & Frequent Configuration

Pros. Compromise between composibility and speed

Cons. Learning and debugging

Page 11: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure11

OTcl and C++: The Duality

C++ OTcl

Pure C++objects

Pure OTclobjects

C++/OTcl split objects

ns

Page 12: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure12

OTcl and C++: The Duality

Simulation Scenario

Tcl Script

C++ Implementation

1 2

set ns_ [new Simulator]

set node_(0) [$ns_ node]

set node_(1) [$ns_ node]

class MobileNode : public Node { friend class PositionHandler; public: MobileNode();

••

}

Page 13: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure13

Agent/DSDVConstructor

AgentConstructor

Invoke parent

TclObjectConstructor

Invoke parent

AgentDSDV()Constructor

Create C++

Agent()Constructor

Invoke parent

TCL

C++

TclObject()Constructor

Invoke parentDo nothing,

returnbind and return

bind and return

OTcl shadow init complete init complete

Object Correspondence

Page 14: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure14

User’s Perspective

From the user’s perspective, NS−2 is an OTcl interpreter that takes an OTcl script as input and produces a trace file as output

Page 15: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure15

Extending Tcl Interpreter

Otcl object-oriented Tcl

TclCL (Tcl with classes) C++ and OTcl linkage

Discrete event schedulerData network components

Link layer and up Emulation support

Tcl

OTcl

TclCL

ns-2

Event

Sch

edu

ler

Network Components

C/C++

Page 16: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure16

Discrete event simulator

ns-2 is an discrete event driven simulation Physical activities are translated to events Events are queued and processed in the order of their

scheduled occurrences Time progresses as the events are processed

1 2

Time: 1.5 sec Time: 1.7 sec

Time: 1.8 secTime: 2.0 sec

Page 17: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure17

Using NS2

Problem

Simulationmodel

Setup/run simulation

with ns

Resultanalysis

Modifyns

Page 18: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure18

OTcl overviewprogramming language used for setting up simulation environment

object oriented interpreted (slow)

Used for Setting up topology Placing agents Injecting events Configuring tracing

Examples:variables

set x 10 puts “x is $x”

expressions set y [pow x 2] set y [expr x+x*3]

control if ($x>0) { return $x }

else { return [expr -$x] }

while ($x >0) { puts $x set x [eval x+1] }

Page 19: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure19

OTcl overviewprogramming language used for setting up simulation environment

object oriented interpreted (slow)

Used for Setting up topology Placing agents Injecting events Configuring tracing

Examples:variables

set x 10 puts “x is $x”

expressions set y [pow x 2] set y [expr x+x*3]

control if ($x>0) { return $x }

else { return [expr -$x] }

while ($x >0) { puts $x set x [eval x+1] }

Page 20: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure20

Basic ns-2Create scheduler

set ns [new Simulator]

Create node set <var> [$ns node] example: set n0 [$ns node]

Create link $ns <link-type> <node1> <node2> <bandwidth>

<delay> <queuetype> example: $ns duplex-link $n0 $n1 10Mb 100ms

DropTail

Schedule event $ns at <time> <event> example: $ns at 10.0 “$ftp start”

Start scheduler $ns run

Page 21: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure21

The 1st ns-2 script (two nodes, one link)

set ns [new Simulator]

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

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

}

$ns at 5.0 "finish" $ns run

Create the simulator object

Open a file for nam trace data

“finish” procedureCloses the trace file and starts nam

Execute the finish procedure after 5.0 seconds

Page 22: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure22

set n0 [$ns node] set n1 [$ns node]

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

Creates two nodes and assigns them to the handle “n0” and “n1”

connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue

The 1st ns-2 script (two nodes, one link)

Page 23: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure23

set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0

set null0 [new Agent/Null] $ns attach-agent $n1 $null0

$ns connect $udp0 $null0

Create a UDP agent and attach it to node n0

Create a CBR traffic source and attach it to udp0

Create a Null agent and attach it to node n1

Connect the two agents

The 1st ns-2 script (two nodes, one link)

Page 24: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure24

$ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop"

tell the CBR agent when to send data and when to stop sending

The 1st ns-2 script (two nodes, one link)

Page 25: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure25

The 2nd ns-2 script (three nodes, two links)set ns [new Simulator]

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

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

}

$ns at 10.0 "finish" $ns run

Create the simulator object

Open a file for nam trace data

“finish” procedureCloses the trace file and starts nam

Execute the finish procedure after 10.0 seconds

Page 26: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure26

#Create three nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]

#Create link between the nodes$ns duplex-link $n0 $n1 4Mb 10ms DropTail$ns duplex-link $n2 $n1 1Mb 10ms DropTail$ns queue-limit $n1 $n2 10

Creates three nodes and assigns them to the handle “n0”, “n1”, and “n1”

connect the nodes n0 and n1 with a duplex link with the bandwidth 4Megabit, a delay of 10ms and a DropTail queue

connect the nodes n0 and n2 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue

The 2nd ns-2 script (three nodes, two links)

n0 n1 n2

the maximum buffer size (#packets) of the queue in the link

Page 27: NS2 Tutorial – Part I Internet Computing Laboratory @ KUT Youn-Hee Han

Data Structure27

#Create a TCP agent and attach it to node n0set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0

#Create a TCP sink agent and attach it to node n2set sink [new Agent/TCPSink]$ns attach-agent $n2 $sink

#Connect both agents$ns connect $tcp0 $sink

# create an FTP source set ftp [new Application/FTP]$ftp set maxpkts_ 1000$ftp attach-agent $tcp0

#Inject starting events$ns at 0.0 "$ftp start"$ns at 10.0 "$ftp stop"$ns at 10.1 "finish"

#Run the simulation$ns run

Create a TCP agent and attach it to node n0

Create a TCP sink and attach it to node n2

Create a FTP application and attach it to agent tcp0

Connect the two agents

The 2nd ns-2 script (three nodes, two links)

The maximum number of packets generated by the source.

n0 n1

ftptcp tcp-sink

n2