Upload
shivamdave
View
240
Download
0
Embed Size (px)
Citation preview
8/12/2019 Bits Pilani Ns 2 Tutorial
1/14
NS2 Tutorial Tutorialsome
Here we are providing a 'providing a 'providing a 'providing a 'template' that we can use it for all tcl scripts.We will introduce basics of how to create event scheduler, create a simple pair of nodes and make
ommunication between the two nodes. Then we will introduce a standard template for all tcl scripts with simplexamples.
n this page we are going to introduce the concepts of:
art of introduction to TCP/IP modelWhere can you write scripts?
asic ns2 programming stepsemplate
Golden rulesimple examples
CBR over UDP CBR over TCP FTP over TCP
Part of Introductionto TCP/IPmodel
Top
TCP/IP model
pplication LayerApplications agents are Telnet, FTP, Rlogin, CBR
ransport LayerThe transport uses two protocols, UDP and TCP. UDP which stands for User Datagram Protocol does
ot guarantee packet delivery and applications which use this must provide their own means of verifyingelivery.CP does guarantee delivery of packets to the applications which use it.
etwork LayerThe network layer is concerned with packet routing and used low level protocols such as ICMP, IP, and
GMP. Here Network protocols are omitted for simple examples
ink Layer
Application Layer CBR,FTP,telnet..
Transport layer TCP,UDP
Network layer rtproto
Data link Layer
Transmission mode
Simplex mode Duplex mode
Physical layer
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#parthttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#parthttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#parthttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htm8/12/2019 Bits Pilani Ns 2 Tutorial
2/14
The link layer is concerned with the actual transmittal of packets as well as IP to Ethernet addressranslation. Data transfer in two modes i.e. simplex mode and in duplex mode.ll the links are created at data link layer. Network layer next is to data link layer.
Where can you write Scripts?; Top
fter login into terminal, follow step given below
. vi filename.tclIt will open page, there you can write tcl scripts.
. save filePress esc-> colon (shift + semicolon) ->wq (save and quit)
t save the file
. To run tcl scriptns filename.tcl
Basically NS 2 programmingcontains the following steps. Top
.Create the event scheduler
.Turn on tracing
.Creating network
a)Computing setup routing - rtprotob) Creating transport connection-Agentsc)Creating traffic-Applications
. Monitoring
a) Visualization using nam
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
3/14
ote: every tcl script must write in small letters only except protocol agents i.e. TCP, FTP
8/12/2019 Bits Pilani Ns 2 Tutorial
4/14
Template Top
Every ns2 script starts with creating simulator object set ns [new Simulator]
How to create node
set n0 [$ns node]set n1 [$ns node]
Creating link
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the
andwidth.1Megabit, a delay of 10ms and a DropTail queue.
How to use Trace?
We use simulator to see results. How is it achieved? Using trace
Two types of trace
1. generic tracefor use with xgraph, and other things
2. nam tracefor use with visualization
# open trace fileset tracefile [open out.tr w]$ns trace-all $tracefile
#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf
Since we have started tracing, we should end also. For this we use finish procedure.
#Define a 'finish' procedure
proc finish {}{
global ns tracefile nf$ns flush-trace
close $nfclose $tracefile # close tracefileexec nam out.nam & #Execute nam on the trace file
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
5/14
exit 0}
Finish procedure is forced to be called at the end with the line
$ns at 5.0 finish
Every tcl script must contain following statement
$ns run
8/12/2019 Bits Pilani Ns 2 Tutorial
6/14
Goldenules Top
Follow the template tcl like Bible.
All communication between 2 agents.
Upper layer to lower layer, we attach agents.
Same layer in two nodes, we connect agents.
Agents are tcp, udp, telnet, cbretc
UDP communication
In UDP communication, data is flows from UDP agent to Null agent.
#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0
# create a null agent which act as traffic sink and attach it to node n1set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
# connect two agents with each other$ns connect $udp0 $null0
TCP Communication
In TCP communication, data is flows from TCP agent to TCPsink agent.
# create Tcp agent and attach it to node no
set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0
# create a tcpsink agent which act as traffic sink and attach it to node n1
set tcpsink0 [new Agent/TCPSink]$ns attach-agent $n1 $tcpsink0
# connect two agents with each other$ns connect $tcp0 $tcpsink0
Traffic generator
For actual data to flow, we need traffic generators.They simulate some application traffic.
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
7/14
Simple example using CBR
# creating CBR agentset cbr0 [new Application/Traffic/CBR]
# Attach the CBR agent to some udp/tcp agent$cbr0 attach-agent $udp0
Scheduling the events
Here at place major role.
$ns at 1.0 $cbr0 start$ns at 5.0 finish"
cbr0 will start at a time of 1.0 ms and whole process will stops at 5.0ms.we can also stop each and trafficenerator. for example
$ns at 4.0 $cbr0 stop
Traffic generator cbr0 will stops at 4.0
8/12/2019 Bits Pilani Ns 2 Tutorial
8/14
Simple Examples Top
o far we are not talking about data flow. Here we will explain with CBR protocol.
xample1:( CBR over UDP)
set ns [new Simulator] Creatingns simulator object
set tracefile [open out.tr w]$ns trace-all $tracefile
Open trace file
set nf [open out.nam w]
$ns namtrace-all $nfOpen the nam trace file
proc finish {}{
global ns tracefile nf$ns flush-traceclose $nf
close $tracefileexec nam out.nam &exit 0
}
'finish' procedure
set n0 [$ns node]
set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail
Create your topology
- set n0 nodes.- $ns duplex-links
set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr[new Application/Traffic/CBR]
$cbr attach-agent $udp0set null0 [new Agent/Null]
$ns attach-agent $n1 $null0$ns connect $udp0 $null0
Create your agents-transport layer and application layers stuff
$ns at 1.0 "$cbr start"$ns at 3.0 "finish"
Scheduling Events- $ns at 1.0 start
and at 3.0 finish
$ns run starts the simulation.
Result
efore 1.0ms
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
9/14
fter 1.0ms
8/12/2019 Bits Pilani Ns 2 Tutorial
10/14
xample 2:(CBR over TCP) Top
set ns [new Simulator] Creatingns simulator object
set tracefile [open out.tr w]$ns trace-all $tracefile
Open trace file
set nf [open out.nam w]$ns namtrace-all $nf
Open the nam trace file
proc finish {}{
global ns tracefile nf$ns flush-trace
close $nfclose $tracefileexec nam out.nam &
exit 0}
'finish' procedure
set n0 [$ns node]
set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail
Create your topology
- set n0 nodes.- $ns duplex-links
set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink0$ns connect $tcp0 $tcpsink0
Create your agents-transport layer and application layers stuff
$ns at 1.0 "$cbr start"$ns at 3.0 "finish"
Scheduling Events
- $ns at 1.0 startand at 3.0 finish
$ns run starts the simulation.
Result:
efore 1.0ms
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
11/14
fter 1.0ms
8/12/2019 Bits Pilani Ns 2 Tutorial
12/14
xample3:(FTP over TCP) Top
set ns [new Simulator] Creatingns simulator object
set tracefile [open out.tr w]$ns trace-all $tracefile
Open trace file
set nf [open out.nam w]$ns namtrace-all $nf
Open the nam trace file
proc finish {}
{global ns tracefile nf$ns flush-traceclose $nfclose $tracefileexec nam out.nam &
exit 0}
'finish' procedure
set n0 [$ns node]set n1 [$ns node]
$ns simplex-link $n0 $n1 1Mb 10ms DropTail
Create your topology- set n0 nodes.
- $ns duplex-links
set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0set ftp0 [new Application/FTP]$ftp0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink0$ns connect $tcp0 $tcpsink0
Create your agents-transport layer and application layers stuff
$ns at 1.0 "$ftp0 start"$ns at 3.0 "finish"
Scheduling Events- $ns at 1.0 start
and at 3.0 finish
$ns run starts the simulation.
Result:
ere we are using FTP Application agent as a traffic generator instead of CBR. The difference is CBR trafficenerator will produce constant bit rate where as FTP traffic generator produces maximum available bit rate.
We are writing code
et ftp0 [new Application/FTP]ftp0 attach-agent $tcp0
nd Similarly for telnet also
et telneto [new Application/TELNET]
ftp0 attach-agent $telnet0
nstead of CBR
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top8/12/2019 Bits Pilani Ns 2 Tutorial
13/14
et cbr0 [new Application/Traffic/CBR]cbr0 attach-agent $tcp0
efore 1.0ms
fter 1.0ms
8/12/2019 Bits Pilani Ns 2 Tutorial
14/14
Top
Tutorials home
This text is provided as its basic only, with the intention of providing novices with a quick glance on the topic
concerned. The reader is advised to visit official tutorials available elsewhere for up to date advance topic.We have made efforts to keep this pageW3C XHTML Compliant.Last Modified: 11thNovember 2007Copyright (c) 2007 Murali.P, Ravindra Kr. Jangir, Manasakrishna Reddy. T. All Rights Reserved.
http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#tophttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://validator.w3.org/docs/why.htmlhttp://validator.w3.org/docs/why.htmlhttp://validator.w3.org/docs/why.htmlhttp://validator.w3.org/docs/why.htmlhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/index.htmhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#top