10
Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @ http://www.isi.edu/nsnam/ns/tutorial/ index.html

Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Embed Size (px)

Citation preview

Page 1: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Brief Intro to ns2: The most widely used Network Simulator

COMP5416

Advanced Network Technologies

Based on: Marc Greis's tutorial @

http://www.isi.edu/nsnam/ns/tutorial/index.html

Page 2: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

ns-2 ns-2 is a discrete event network simulator for

wired and wireless networks alike It is popular for its extensibility (due to its open

source model) Supports an array of popular network protocols

– Popularly used in the simulation of routing and multicast protocols, and is heavily used in ad-hoc research

built in C++ and provides a simulation interface through OTcl, an object-oriented dialect of Tcl

Page 3: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

How to start? Learn how to set up nodes and links, how to send data from one node to

another, and how to start nam from your simulation script to visualize your simulation.

Our first Tcl script - call this 'example1.tcl'.

First of all, you need to create a simulator object. This is done with the command

set ns [new Simulator]

Now we open a file for writing that is going to be used for the nam trace data.

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

Page 4: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

example1.tcl The next step is to add a 'finish' procedure that

closes the trace file and starts nam.proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0}

Page 5: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Two nodes, one link

Define the two nodesset n0 [$ns node]set n1 [$ns node]

The next line connects the two nodes.$ns duplex-link $n0 $n1 1Mb 10ms DropTail– This line tells to connect the nodes n0 and n1 with a duplex

link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue.

Page 6: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Sending data Next step is to send some data from node n0 to node n1. In ns,

data is always being sent from one 'agent' to another So the next step is to create an agent object that sends data from

node n0, and another agent object that receives the data on node n1.#Create a UDP agent and attach it to node n0set 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

These lines create a UDP agent and attach it to the node n0, then attach a CBR traffic generator to the UDP agent

Page 7: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Sending data II The next lines create a Null agent which acts as traffic sink and attach

it to node n1.set null0 [new Agent/Null] $ns attach-agent $n1 $null0

Now the two agents have to be connected with each other.$ns connect $udp0 $null0

And now we have to tell the CBR agent when to send data and when to stop sending$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop"

The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation time.$ns at 5.0 "finish"

The last line finally starts the simulation.$ns run

Page 8: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

# create a simulator objectset ns [new Simulator]

# open file for writing; later used by namset nf [open out.nam w]$ns namtrace-all $nf

# 'finish' procedure that closes the traceproc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0}

# creates two nodesset n0 [$ns node]set n1 [$ns node]

# connects the two nodes$ns duplex-link $n0 $n1 1Mb 10ms DropTail

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

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

# create a Null agent and attach it to node n1set null0 [new Agent/Null]$ns attach-agent $n1 $null0

# connect with each other$ns connect $udp0 $null0

# tell CBR agent when to send and stop sending$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop"

# when to stop simulation (5 seconds)$ns at 5.0 "finish"

$ns run

example.tcl

Page 9: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Sample runs Run using:

ns example1.tcl Simulation runs in the

background creating a trace of information

This trace is then used by nam, which invoked by finish

Page 10: Brief Intro to ns2: The most widely used Network Simulator COMP5416 Advanced Network Technologies Based on: Marc Greis's tutorial @

Statistics provided in nam