gnanavelmeblog.files.wordpress.com … · web viewex. no: 10study of ns-2 simulator. date: a...
TRANSCRIPT
Ex. No: 10STUDY OF NS-2 SIMULATOR
Date:
A simulator is a device, software or system which behaves or operates like a given system when provided with a set of controlled inputs. The need for simulators is:
Provide users with practical feedback such as accuracy, efficiency, cost, etc., when designing real world systems.
Permit system designers to study at several different levels of abstraction
Simulation can give results that are not experimentally measurable with our current level of technology.
Simulations take the building/rebuilding phase out of the loop by using the model already created in the design phase.
Effective means for teaching or demonstrating concepts to students.
A few popular network simulators are NS-2, OPNET, GLOMOSIM, etc.
Network Simulator NS2
NS2 is an object-oriented, discrete event driven network simulator developed at UC Berkley written in C++ and OTcl (Object-oriented Tool Command Language). NS is useful for simulating local and wide area networks. NS2 is an open-source simulation tool that primarily runs on Linux (cygwin for Windows). The features of NS2 are:
Is a discrete event simulator for networking research Works at packet level.
Provide support to simulate bunch of protocols like TCP, UDP, FTP, etc. Simulate wired and wireless network.
Is a standard experiment environment in research community.
Class Hierarchy
Network Animator (NAM)
NS together with NAM forms a very powerful set of tools for teaching networking concepts. With NAM protocols can be visualized as animations. The NAM graphical editor is the latest addition to NAM. With this editor, one can create their network topology and simulate various protocols and traffic sources by dragging the mouse.
NS2 Execution
The overall simulation procedure in NS is shown below. NS is composed of OTcl Script and Interpreter. NS simulation results can be observed through graphs by analyzing the trace file or viewing animations with NAM.
$ ns filename.tcl
NS2 Program Elements
Event Scheduler
a Creating event scheduler
set ns [new Simulator]
b Schedule events
$ns at time"event"
c Start scheduler
$ns run
Creating Network
a Create set of Nodes
set n0 [$ns node]
set n1 [$ns node]
b Create links and queuing
$ns duplex-link $n0 $n1 bandwidth delay queue_typeBandwidth is generally in MB
Delay is generally in ms
Queue type is either DropTail, RED, CBQ, FQ, SFQ, etc
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
c Layout
$ns duplex-link-op $n0 $n2 orient position
where position is either right,right-up,right-down,left,left-up, left-down, up, down
d Marking flows
$ns color 1 Blue
$ns color 2 Red
$udp0 set class_ 1
$udp1 set class_2
Tracing
a NAM Trace all links (must succeed scheduler creation)
set nf [open out.nam w]
$ns namtrace-all $nf
b Trace all links (must succeed scheduler creation)
set tf [open out.tr w]
$ns trace-all $tf
Trace file ouput format
event, time, from_node, to_node, pkt type, pkt size, flags, fid, src_addr, dst_addr, seq_num, pkt_id
where events are r received, + enqueued, - dequeued, d dropped
c Tracing specific links
$ns trace-queue $n0 $n1
$ns namtrace-queue $n0 $n1
Connection
a UDP
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp0
$ns attach-agent $n1 $null
$ns connect $udp0 $null
b TCP
set tcp0 [new Agent/TCP/FullTcp]
$tcp0 set window_ 30
$tcp0 set segsize_ 536
$ns attach-agent $n0 $tcp0
set sink0 [new Agent/TCP/FullTcp]
$ns attach-agent $n5 $sink0
$sink0 listen
$ns connect $tcp0 $sink0
Traffic Generation
a UDP
set src [new Application/Traffic/type]
$src attach-agent $udp0
where type is either CBR,Exponential,Pareto
b TCP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
set telnet [new Application/Telnet]
$telnet attach-agent $tcp
Finish procedure
a Flush NS tracing, Close tracing files and execute any post-analysis programs (display results, run NAM, etc)
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit
RESULT:
Thus the study of NS-2 simulator is done
57
Ex. No:11SIMULATION OF DV ROUTING PROTOCOL
Date:
AIM:
To simulate the Distance Vector (DV) routing protocol.
ALGORITHM:
1. Create a new simulator instance.
2. Set the color for data flows in NAM with blue and red color.
3. Open the trace file and NAM file in write mode.
4. In the finish function, flush the trace file and execute the NAM file.
5. Create a node for the corresponding network path.
6. Create the link between the nodes, set the duplex-link and speed, time and droptail techniques.
7. Set the position for the nodes and set the queue limits of nodes.
8. Set up the TCP and UDP connection between the nodes.
9. Set up FTP over TCP connection.
10.Start ns and simulate till 5 minutes are over.
11. Display using xgraph the simulated data.
CODING:
//dv.tcl
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace file set file1 [open dv.tr w]
$ns trace-all $file1
#Open the NAM trace file set file2 [open dv.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure proc finish {} {
global ns file1 file2
$ns flush-trace close $file1 close $file2
exec nam dv.nam &
exit 0
}
# Next line should be commented out to have the static routing
$ns rtproto DV
#Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient up
$ns duplex-link-op $n1 $n4 orient up-left
$ns duplex-link-op $n3 $n5 orient left-up
$ns duplex-link-op $n4 $n5 orient right-up
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4
$ns rtmodel-at 4.5 up $n1 $n4
$ns at 0.1 "$ftp start"
$ns at 6.0 "finish"
$ns run
OUTPUT:
[student@localhost ~]$ ns dv.tcl
RESULT: Thus the simulation of the Distance Vector (DV) routing protocol is executed successfully.
Ex. No:12 SIMULATION FOR COMPARISON OF LS AND DV
Date :
AIM:
To compare the performance of Link State (LS) and Distance Vector (DV) routing protocol.
ALGORITHM:
1. Create a new simulator instance.
2. Set the color for data flows in NAM with blue and red color.
3. Open the trace file and NAM file in write mode for both LS and DV.
4. Create six nodes.
5. Create duplex link between the nodes created and with proper orientation.
6. Set up a TCP connection
7. Then set up FTP over TCP.
8. In the finish function, flush the trace file and execute the NAM file.
9. The link from node 1 to node 4 is set to down at time 1.0 and up at time 4.5
10.Start ns and simulate till 5 minutes are over.
11. The comparison of LS and DV is displayed using xgraph tool.
CODING:
//dv.tcl
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace file set file1 [open dv.tr w]
$ns trace-all $file1
#Open the NAM trace file set file2 [open dv.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure proc finish {} {
global ns file1 file2
$ns flush-trace close $file1 close $file2
exec nam dv.nam &
exit 0
}
# Next line should be commented out to have the static routing
$ns rtproto DV
#Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient up
$ns duplex-link-op $n1 $n4 orient up-left
$ns duplex-link-op $n3 $n5 orient left-up
$ns duplex-link-op $n4 $n5 orient right-up
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4
$ns rtmodel-at 4.5 up $n1 $n4
$ns at 0.1 "$ftp start"
$ns at 6.0 "finish"
$ns run
//ls.tcl
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace file set file1 [open ls.tr w]
$ns trace-all $file1
#Open the NAM trace file set file2 [open ls.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure proc finish {} {
global ns file1 file2
$ns flush-trace close $file1 close $file2
exec nam ls.nam &
exit 0
}
# Next line should be commented out to have the static routing
$ns rtproto LS
#Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n1 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail
$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail
$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail
$ns duplex-link $n4 $n5 0.5Mb 10ms DropTail
#Give node position (for NAM)
$ns duplex-link-op $n0 $n1 orient right
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n2 $n3 orient up
$ns duplex-link-op $n1 $n4 orient up-left
$ns duplex-link-op $n3 $n5 orient left-up
$ns duplex-link-op $n4 $n5 orient right-up
#Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4
$ns rtmodel-at 4.5 up $n1 $n4
$ns at 0.1 "$ftp start"
$ns at 6.0 "finish"
$ns run
OUTPUT:
[student@localhost ~]$ ns dv.tcl
[student@localhost ~]$ ns ls.tcl
[student@localhost ~]$ raw2xg -a ls.tr > ls.xg
[student@localhost~]$ raw2xg -adv.tr>dv.xg
[student@localhost~]$xgraphls.xgdv.xg -tComparisonof LS&DV
RESULT: Thus the performance of Link State (LS) and Distance Vector (DV) routing protocol is compared and studied.
61