Network Simulator 2 Tejas Vasavada. Contents Simulation of wired networks Simulation of wireless...

63
Network Simulator 2 Tejas Vasavada

Transcript of Network Simulator 2 Tejas Vasavada. Contents Simulation of wired networks Simulation of wireless...

Page 1: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Network Simulator 2Tejas Vasavada

Page 2: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Contents

Simulation of wired networksSimulation of wireless networks • Traffic and movement-pattern files• Trace files• Analyzing trace files using 'awk' script• 'gnuplot' – utility to plot graphs

Page 3: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Contents (contd.)

DSR – A protocol for MANETs Source code of DSR in NS2 How to change DSR (with examples)

• Printing packet header• Configuring nodes to drop packets• Adding a new function in DSR • Calling previous function from Tcl• Passing arguments from Tcl to DSR• Configuring nodes to drop packets• Use of timers

Page 4: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Contents

How to add new package in NS Simulation of Wired-cum-Wireless Networks

Page 5: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Introduction

• Simulation comes before implementation.

• A newly designed protocol is simulated first to test the performance.

Page 6: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simulation Tools

• OmNet++, NS2, Qualnet, Opnet, GlomoSim etc.

• NS2 - an open source tool, widely used in academics for research purpose.

Page 7: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

How to use NS2

• Front end – Tcl (Tool Command Langauage)

• Back end – C++

• Today we will learn Tcl.

Page 8: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wired Network

Page 9: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script

• set ns [new Simulator] creates an instance of ‘Simulator’ class.

• Output is stored in .nam files (Network AniMator)

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

Page 10: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script (contd.)

• proc finish {} {global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0}

‘finish’ will be called at the end of simulation.

Page 11: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl Script (contd.)

• $ns at 5.0 "finish“finish() is called at 5th second. • $ns run Starts simulation.

Page 12: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script (contd.)

• set n0 [$ns node] set n1 [$ns node]Creates two nodes - n0 and n1.

• $ns duplex-link $n0 $n1 1Mb 10ms DropTailConnects both the nodes.

Page 13: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script (contd.)• #Create a UDP agent and attach it to node n0

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

• # Create a CBR traffic source and attach it to udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0

Page 14: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script (contd.)

• set null0 [new Agent/Null] $ns attach-agent $n1 $null0Creates a null agent and attaches it to n1.

• $ns connect $udp0 $null0Two agents are connected togother.

Page 15: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Tcl script (contd.)

• $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop“Node n0 will start sending packets at time0.5 sec and will stop at 4.5 sec.

Page 16: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Exercise 1

• Read example1.tcl provided to you.

• Run it using following commandns example1.tcl

• Read & Run Example2.tcl

Page 17: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example2.tcl

• $ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right

Creates star shape topology.

Page 18: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example2.tcl (contd.)

Page 19: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example2.tcl (contd.)

• Nodes n0 and n1 connect to n3 via n2.

• Bandwidth n2-n3 < (bandwidth n0-n2+

bandwidth n1-n2)• Node n2 drops packets.

Page 20: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example2.tcl

• To see what is happening at n2, different flows should be coloured differently.

• $udp0 set class_ 1 $udp1 set class_ 2

• $ns color 1 Blue$ns color 2 Red

Page 21: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example2.tcl (contd.)

Page 22: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl

• for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }

Creates 7 nodes.• for {set i 0} {$i < 7} {incr i} {

$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail}

Page 23: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl (contd.)

Page 24: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl (contd.)

• Node n0 is connected to n3. Packets will pass through n1 and n2 (shortest path).

• Break n1-n2 link using following commands.$ns rtmodel-at 1.0 down $n(1) $n(2)$ns rtmodel-at 2.0 up $n(1) $n(2)

Page 25: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl (contd.)

Page 26: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl (contd.)

• Use following command to use alternate path.

$ns rtproto DV

Page 27: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Example3.tcl (contd.)

Page 28: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wireless Network

Page 29: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl

• Components of a mobile node…Link Layer,Interface Queue between LL and Network

Layer,MAC LayerRouting Layer

• Parameters of a mobile nodeType of antenna,Radio propagation model

Page 30: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(ant) Antenna/OmniAntenna ;# Antenna typeset val(ll) LL ;# Link layer typeset val(ifq) Queue/DropTail/PriQueue ;# Interface queue typeset val(ifqlen) 50 ;# max packet in ifqset val(netif) Phy/WirelessPhy ;# network interface typeset val(mac) Mac/802_11 ;# MAC typeset val(rp) DSDV ;# ad-hoc routing protocolset val(nn) 2 ;# number of mobilenodes

Page 31: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• set ns_ [new Simulator]Creates new instance of ‘Simulator’.

• set tracefd [open simple.tr w]$ns_ trace-all $tracefd

Creates new instance of trace file.

Page 32: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• set topo [new Topography]Creates new topology.

• $topo load_flatgrid 500 500Sets resolution of topology to 500 x 500.

Page 33: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)• # Configure 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 \ -channelType $val(chan) \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace OFF

Page 34: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• for {set i 0} {$i < $val(nn) } {incr i} {set node_($i) [$ns_ node ]$node_($i) random-motion 0 }

Random motion is disabled. We will provide initial position and movement parameters.

Page 35: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• $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

Page 36: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• # 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"

Page 37: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)• # TCP connections between node_(0) and node_(1) set tcp [new Agent/TCP]

$tcp set class_ 2set sink [new Agent/TCPSink]$ns_ attach-agent $node_(0) $tcp$ns_ attach-agent $node_(1) $sink$ns_ connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns_ at 10.0 "$ftp start"

Page 38: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)# Tell nodes when the simulation ends

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_ tracefdclose $tracefd}

Page 39: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Simple-wireless.tcl (contd.)

• puts "Starting Simulation..."$ns_ run

Prints the message and starts simulation.

Page 40: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Traffic-pattern fileand

movement-pattern file

Page 41: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wireless-1.tcl

• set val(cp) “/root/cbr-3-test"set val(sc) “/root/scen-3-test“

cbr-3-test is connection-pattern file.scen-3-test is movement-pattern file.

Page 42: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wireless-1.tcl (contd.)

• set val(x) 670 set val(y) 670

• set namtrace [open wireless1-out.nam w] $ns_ namtrace-all-wireless $namtrace $val(x)

$val(y)

Page 43: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wireless-1.tcl (contd.)

• puts "Loading connection pattern..."source $val(cp)

Loads connection-pattern file.

• puts "Loading scenario file..."source $val(sc)

Loads movement-pattern(scenario) file.

Page 44: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Wireless-1.tcl (contd.)

• for {set i 0} {$i < $val(nn)} {incr i} {$ns_ initial_node_pos $node_($i) 20}

Defines size of node in nam

Page 45: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Traffic pattern file• ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed]

[-mc connections] [-rate rate] > [filenm]

Generates a traffic pattern. It is saved in file ‘filenm’.-type : type of connection cbr or tcp-nn : no. of nodes-seed : seed value used by random number generator-mc : no. of connections-rate : rate at with packets are sent

Page 46: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Traffic pattern file (contd.)

• ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 8 -rate 4.0 > cbr-10-test

Generates traffic-pattern and saves in file cbr-10-test.

Create traffice-pattern file for tcp traffic and check how it differs from cbr-10-test

Page 47: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Movement-pattern file

• ‘setdest’ utility available at: ns-x.xx/indep-utils/cmu-scen-gen/setdest

• /setdest –v 1 –n <nodes> -p <pause time> -M <max speed> -t <sim time> -x <maxx> -y <maxy>

Page 48: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Movement-pattern file (contd.)

• ./setdest –v 2 –n <nodes> -s <speed type> -m <min speed> -M <max speed> -t <sim time> -P <pause type> -p <pause time> -x <maxx> -y <maxy>

Speed type: 1 - uniform speed 2 - normal speed

Page 49: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

• Pause type: 1 - constant 2 – uniform [ 0, 2 x pause ]

For e.g../setdest –v 2 -n 50 -s 1 -m 1 -M 20 -t 200 -P 1 -p

0 -x 200 -y 200 > /root/Desktop/scen-3-test

Page 50: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace file format

Page 51: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace files

• Output of simulation is in trace format.• Trace file has two formats: old and new• To use new format add following line in tcl

script before calling trace-all .

$ns use-newtrace

Page 52: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace file format

• Event type s sendd dropr receivef forward

• Time -t

Page 53: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace file format (contd.)

• Node property tag-Ni Node id-Nx Node’s x-coordinate-Ny Node’s y-coordinate-Nz Node’s z-coordinate-Ne Node’s energy level-Nl Trace level (AGT, RTR, MAC)

Page 54: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace file format (contd.)

• -Is source address.source port number-Id dest address.dest port number-It packet type-Il packet size-If flow id-Ii unique id-Iv ttl value

Page 55: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Trace file format (contd.)

• Next hop information

-Hs id for this node-Hd id of next node towards destination

Page 56: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Awk script

Page 57: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Awk script

• ‘Awk’ is a scripting language used to process text files.

• Processes the file row by row.• Columns are identified like $1,$2 etc.• awk –f <.awk file> <.tr> file.• Use the awk file provided to calculate

throughput from tracefile.

Page 58: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Sample Script - dr.awk

• BEGIN– {– sent=0– recd=0– }

• {– sent=0– recd=0

Page 59: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

dr.awk

– event=$1– level=$19– if(event == “s” && level == “AGT”)

• sent++– if(event == “r” && level == “AGT”)

• recd++• }

Page 60: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

dr.awk

• END {

– printf("sent %g, recd %g, fwd %g, del. %g\n",sent,recd,fwd,recd/sent);

• }

Page 61: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

GnuPlot

Page 62: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

'gnuplot'

• Syntax:– gnuplot <file.gnu>

• Read dr.gnu file to understand use of 'gnuplot' to plot graphs.

Page 63: Network Simulator 2 Tejas Vasavada. Contents  Simulation of wired networks  Simulation of wireless networks Traffic and movement-pattern files Trace.

Adding new Files in NS

• Find ns2_ricean_dist folder available with this presentation.

• Follow the procedure in “README” file.