Network Simulator 2(NS2)

Preview:

DESCRIPTION

Network Simulator 2(NS2). Yingyue Xu 8/10/2014. Overview:. The Network simulator (NS): discrete event simulator for networks. supports wired, wireless, and satellite networks. various routing and multicast protocols. written in C++ and object-oriented tool command language (OTcl). - PowerPoint PPT Presentation

Citation preview

Network Simulator 2(NS2)

Yingyue Xu04/22/23

Overview:The Network simulator (NS): discrete event simulator for networks. supports wired, wireless, and satellite networks. various routing and multicast protocols. written in C++ and object-oriented tool command

language (OTcl). NS output is a file(s) which contains all packet

information. It can be viewed using network animator (NAM), xgraph, or processed with custom scripts (Perl, Tcl or Awk ).

User view of NS:

Why two languages:C++

Fast in running. Slow and hard to modify (need

to recompile the whole NS ). Used to implement detailed

protocols and algorithms, to manipulate bytes since it can run over large number of data

OTcl Slow in running Can be quickly changed (no

need to recompile). Used to implement the

simulation configuration like # of nodes, simulation topology and the dimensions of test area.

That’s why NS object called split object!

NS via Tclcl (Tcl with classes) provides glue to make objects and variables appear on both languages.

Ns functionalities Wired world

Routing DV, LS, PIM-SM Transportation: TCP and UDP Traffic sources:web, ftp, telnet, cbr, stochastic Queuing disciplines:drop-tail, RED, FQ, SFQ, DRR QoS: IntServ and Diffserv Emulation

Wireless Ad hoc routing and mobile IP Directed diffusion, sensor-MAC

Tracing, visualization, various utilities

Ns Models Traffic models and applications:

Web, FTP, telnet, constant-bit rate, real audio Transport protocols:

unicast: TCP (Reno, Vegas, etc.), UDP Multicast: SRM

Routing and queueing: Wired routing, ad hoc rtg and directed diffusion queueing protocols: RED, drop-tail, etc

Physical media: Wired (point-to-point, LANs), wireless (multiple propagation

models), satellite

NS installation: NS can work on most UNIX platforms, and

also on windows platform.Detailed info for downloading and building NS: http://www.isi.edu/nsnam/ns/ns-build.html Easier way to download all in one package

and build NS (this will not work on Windows), this package contains Tcl, Tclcl, OTcl, Tk, and NS source codes.

You need to run install from UNIX shell (Bash or C shell).

Basic Tclvariables:set x 10puts “x is $x”

functions and expressions:set y [pow x 2]set y [expr x*x]

control flow:if {$x > 0} { return $x } else {

return [expr -$x] }while { $x > 0 } {

puts $xincr x –1

}

procedures:proc pow {x n} {

if {$n == 1} { return $x }set part [pow x [expr $n-1]]return [expr $x*$part]

}

Also lists, associative arrays, etc.

=> can use a real programming language to build network topologies, traffic models, etc.

Basic otclClass Person# constructor:Person instproc init {age} {

$self instvar age_set age_ $age

}# method:Person instproc greet {} {

$self instvar age_puts “$age_ years old: How are you doing?”

}

# subclass:Class Kid -superclass PersonKid instproc greet {} {

$self instvar age_puts “$age_ years old kid: What’s up, dude?”

}

set a [new Person 45]set b [new Kid 15]$a greet$b greet

=> can easily make variations of existing things (TCP, TCP/Reno)

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Creating Event Scheduler Create event scheduler

set ns [new Simulator] Schedule events

$ns at <time> <event> <event>: any legitimate ns/tcl commands

$ns at 5.0 “finish” Start scheduler

$ns run

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Tracing and Monitoring Packet tracing:

On all links: $ns trace-all [open out.tr w] On one specific link: $ns trace-queue $n0

$n1$tr<Event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst>

<seq> <attr>

+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Creating Network Nodes

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

Links and queuing$ns <link_type> $n0 $n1 <bandwidth> <delay>

<queue_type> <link_type>: duplex-link, simplex-link <queue_type>: DropTail, RED, CBQ, FQ, SFQ,

DRR, diffserv RED queues

Creating Network: LAN$ns make-lan <node_list> <bandwidth>

<delay> <ll_type> <ifq_type> <mac_type> <channel_type>

<ll_type>: LL<ifq_type>: Queue/DropTail,<mac_type>: MAC/802_3<channel_type>: Channel

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Setup Routing Unicast

$ns rtproto <type><type>: Static, Session, DV, cost, multi-path

Multicast$ns multicast (right after [new Simulator])$ns mrtproto <type><type>: CtrMcast, DM, ST, BST

Other types of routing supported: source routing, hierarchical routing

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Inserting Errors Creating Error Module

set loss_module [new ErrorModel]$loss_module set rate_ 0.01$loss_module unit pkt$loss_module ranvar [new RandomVariable/Uniform]$loss_module drop-target [new Agent/Null]

Inserting Error Module$ns lossmodel $loss_module $n0 $n1

Ns programming Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic

Creating Connection and Traffic UDPset udp [new Agent/UDP]set null [new Agent/Null]$ns attach-agent $n0

$udp$ns attach-agent $n1

$null$ns connect $udp $null

CBRset src [new

Application/Traffic/CBR]

Creating Connection and Traffic II TCPset tcp [new Agent/TCP]set tcpsink [new

Agent/TCPSink]$ns attach-agent $n0 $tcp$ns attach-agent $n1

$tcpsink$ns connect $tcp $tcpsink

FTPset ftp [new Application/FTP]$ftp attach-agent $tcp Telnetset telnet [new

Application/Telnet]$telnet attach-agent $tcp

Summary: Generic Script Structure

set ns [new Simulator]set ns [new Simulator]# [Turn on tracing]# [Turn on tracing]# Create topology# Create topology# Setup packet loss, link dynamics# Setup packet loss, link dynamics# Create routing agents# Create routing agents# Create: # Create: # - multicast groups# - multicast groups# - protocol agents# - protocol agents# - application and/or setup traffic sources# - application and/or setup traffic sources# Post-processing procs# Post-processing procs# Start simulation# Start simulation

Plumbing: Packet Flow

01

n0 n1

Addr Classifier

Port Classifier

entry_

0 Agent/TCP Addr Classifier

Port Classifier

entry_

10

Link n0-n1

Link n1-n0

0 Agent/TCPSinkdst_=1.0 dst_=0.0

Application/FTP

Getting started with nam Turn on nam tracing in your Tcl script

As easy as turning on normal tracing$ns namtrace $file

Specify link orientation (or node position for wireless)$ns duplex-link-op $node1 $node2 orient left

Execute namexec nam $filename

Advanced nam capabilities Node options — color, shape, label

$node color red$node shape hexagon$node label “my text”$node label-color blue$node label-at up

Link options$ns duplex-link-op $n1 $n2 color green$ns duplex-link-op queuePos right$ns duplex-link-op $n1 $n2 label “my text”$ns duplex-link-op $n1 $n2 label-color blue$ns duplex-link-op $n1 $n2 label-at down

Advanced nam capabilities Packet colors

$ns color $n blue$agent set fid_ $n

Annotation$ns at $time “$ns trace-annotate $text”

Control playback$ns set-animation-rate 3ms

The nam user interface

The nam editor Create simple scenarios graphically Good for those who don’t want to learn

Tcl, but only a limited subset of ns is available

The nam editor

Wireless model Mobilenode at core of mobility model Mobilenodes can move in a given topology,

receive/transmit signals from/to wireless channels Wireless network stack consists of LL, ARP,

MAC, IFQ etc Allows simulations of multi-hop ad hoc networks,

wireless LANs, sensor networks etc

An Example – Step 1# Define Global Variables# create simulatorset ns [new Simulator]

# create a flat topology in a 670m x 670m areaset topo [new Topography] $topo load_flatgrid 670 670

An Example – Step 2# Define standard ns/nam trace

# ns trace

set tracefd [open demo.tr w]

$ns trace-all $tracefd

# nam trace

set namtrace [open demo.nam w]

$ns namtrace-all-wireless $namtrace 670 670

GOD (General Operations Director) Stores smallest number of hops from one

node to another Optimal case to compare routing protocol

performance Automatically generated by scenario file set god [create-god <no of mnodes>] $god set-dist <from> <to> <#hops>

Example –Step 3 Create God

set god [create-god 3]$ns at 900.00 “$god setdist 2 3 1”

An Example – Step 4# Define how a mobile node is configured$ns node-config \

-adhocRouting DSDV \-llType LL \-macType Mac/802_11 \-ifqLen 50 \-ifqType Queue/DropTail/PriQueue \-antType Antenna/OmniAntenna \-propType Propagation/TwoRayGround \-phyType Phy/WirelessPhy \-channelType Channel/WirelessChannel \-topoInstance $topo-agentTrace ON \-routerTrace OFF \-macTrace OFF

An Example – Step 5# Next create a mobile node, attach it to the channel set node(0) [$ns node]# disable random motion $node(0) random-motion 0

# Use “for” loop to create 3 nodes:

for {set i < 0} {$i < 3} {incr i} {set node($i) [$ns node]$node($i) random-motion 0}

Mobilenode Movement Node position defined in a 3-D model However z axis not used

$node set X_ <x1>$node set Y_ <y1>$node set Z_ <z1>$node at $time setdest <x2> <y2>

<speed> Node movement may be logged

Scenario Generator: Movement

Mobile Movement Generatorsetdest -n <num_of_nodes> -p pausetime -setdest -n <num_of_nodes> -p pausetime -

s <maxspeed> -t <simtime> -x <maxx> -y s <maxspeed> -t <simtime> -x <maxx> -y <maxy><maxy>

Source: ns-2/indep-utils/cmu-scen-gen/setdest/ns-2/indep-utils/cmu-scen-gen/setdest/

Random movement $node random-motion 1 $node start$node start

A Movement File$node_(2) set Z_ 0.000000000000$node_(2) set Y_ 199.373306816804$node_(2) set X_ 591.256560093833$node_(1) set Z_ 0.000000000000$node_(1) set Y_ 345.357731779204$node_(1) set X_ 257.046298323157$node_(0) set Z_ 0.000000000000$node_(0) set Y_ 239.438009831261$node_(0) set X_ 83.364418416244$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743 170.519203111152 3.371785899154"$ns_ at 51.000000000000 "$node_(1) setdest 221.826585497093 80.855495003839 14.909259208114"$ns_ at 33.000000000000 "$node_(0) setdest 89.663708107313 283.494644426442 19.153832288917"

Scenario Generator: Traffic Generating traffic pattern files

CBR trafficns cbrgen.tcl [-type cbr|tcp] [-nn ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections] nodes] [-seed seed] [-mc connections] [-rate rate][-rate rate]

TCP trafficns tcpgen.tcl [-nn nodes] [-seed seed]ns tcpgen.tcl [-nn nodes] [-seed seed]

Source: ns-2/indep-utils/cmu-scen-ns-2/indep-utils/cmu-scen-gen/gen/

A Traffic Scenarioset udp_(0) [new Agent/UDP]$ns_ attach-agent $node_(0) $udp_(0)set null_(0) [new Agent/Null]$ns_ attach-agent $node_(2) $null_(0)set cbr_(0) [new Application/Traffic/CBR]$cbr_(0) set packetSize_ 512$cbr_(0) set interval_ 4.0$cbr_(0) set random_ 1$cbr_(0) set maxpkts_ 10000$cbr_(0) attach-agent $udp_(0)$ns_ connect $udp_(0) $null_(0)$ns_ at 127.93667922166023 "$cbr_(0) start"…….

An Example – Step 6# Define node movement model source <movement-scenario-files>

# Define traffic modelsource <traffic-scenario-files>

An Example – Step 7

# Tell ns/nam the simulation stop time $ns at 200.0 “$ns nam-end-wireless 200.0”$ns at 200.0 “$ns halt”

# Start your simulation $ns run

nam Visualization Replace

$ns namtrace-all $fd$ns namtrace-all $fd

with$ns namtrace-all-wireless $fd$ns namtrace-all-wireless $fd

At the end of simulation, do$ns nam-end-wireless [$ns now]$ns nam-end-wireless [$ns now]

Wireless Trace Support Original cmu trace format A separate wireless trace format developed

later at ISI Current ongoing effort to have ONE format

to combine all wired and wireless formats

Recommended