Sie sind auf Seite 1von 6

Computer Communication Networks

Lecture 8: Introduction to Network Simulator 2


Notes taken by Ferhat Cagan
27 October 2009

Summary: This document contains notes taken by Ferhat Cagan for Dr. Stephen Bush‘s Computer
Communication Networks class at University at Albany on 27 October 2009.

What is NS2?
NS (version 2) is an object-oriented, discrete event driven network simulator developed at UC Berkely written in C++
and OTcl (Tcl script language with Object-oriented extensions). It implements network protocols such as TCP and
UPD, traffic source behavior such as FTP, Telnet, Web, CBR and VBR, router queue management mechanism such as
Drop Tail, RED and CBQ, routing algorithms such as Dijkstra, and more. NS also implements multicasting and some
of the MAC layer protocols for LAN simulations.
• It is a discrete event simulator.
• It allows fast development.
• It provides a graphical interface. NAM

• It is flexible for integration.


• it is easy to use.
• Simulations are designed at the packet level.

• Many different protocols can be simulated like TCP, UDP, FTP, HTTP.
• Both wireless and wired networks can be simulated.
• It is a unix based simulator.
• It is uses TCL scripting language.

• C++ hierarchy underlies of the TCL scripting language.


• It is a great tool for learning fundamentals of evaluating network performance via simulation.
• Practical applications of network theories can be simulated, evaluated and compared.

• Real-systems are not available, are complex/costly or dangerous to implement.


• Bugs in the design can be determined in advance.
• It is free to use.

1
Background on the NS2
• NS based on two programming languages: object oriented simulator, written in C++, and a Tcl interpreter, used
to execute user‘s command scripts.
• NS is a discrete event simulator, timing of events maintained by a scheduler. Each event is an object in C++
hierarchy specified by a unique ID, a scheduled time and the pointer to an object that handles the event.
• NS scheduler uses a simple linked-list with the events to be executed. Then, it starts each event one by one,
invoking the handler of the event.
• We can define a TCL script for a particular network topology using specific protocols and applications that we
would like to simulate. All of the behaviours of the network components are already defined in the compiled
C++ hierarchy. So, we cannot implement any other specifications that is not being defined in C++ hierarchy
unless we develop our own network components in C++ hierarchy.

• NS simulator creates data trace file that can be used to analyze the simulation. This output file can be used to
create statistical relations of the simulation. Also, NS creates nam visualization file that can be used as an input
for Network Animator(NAM). User can view the structure of the network.
• There is a file called Everything.pdf under ns-2/doc which helps to explain the inner workings of NS2, as well
as its components.

NS2 Class Specific Notes


• We have analyzed different types of NS2 implementations.
– We have constructed a simple M/M/1 queing model. We defined our metrics as arrival rate and service
time. Then, we have analytically calculated expected utilization of link as measured by expected number
of packets in queue. Then, we compared this result with the mean value of the number of packets in
queue after running our simulation. We discussed details about the difference between analytical result(10
packets in queue) and simulation result(8 packets in queue).
– We take a look at simple wireless simulation settings.
– We also created a worm propagation model.
– There are many different sample simulation settings can be found online. Also, there are many sample .tcl
scripts come with ns2 under the ns/tcl folder.

• Final Project presentation will take like 10-15 minutes. Presentations should be e-mailed the Prof. by Monday
Nov 30th.

2
A Simulation Sample
#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)


$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file


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

#Open the Trace file


set tf [open out.tr w]
$ns trace-all $tf

#Define a ’finish’ procedure


proc finish {} {
global ns nf tf
$ns flush-trace
#Close the NAM trace file
close $nf
#Close the Trace file
close $tf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}

#Create four nodes


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

#Create links between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail

#Set Queue Size of link (n2-n3) to 10


$ns queue-limit $n2 $n3 10

#Give node position (for NAM)


$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

#Monitor the queue for link (n2-n3). (for NAM)


$ns duplex-link-op $n2 $n3 queuePos 0.5

3
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $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

#Setup a UDP connection


set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

#Setup a CBR over UDP connection


set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false

#Schedule events for the CBR and FTP agents


$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"

#Detach tcp and sink agents (not really necessary)


$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Print CBR packet size and interval


puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"

#Run the simulation


$ns run

4
This network consists of 4 nodes n0, n1, n2, n3. The duplex links between n0 and n2, and n1 and n2 have 2 Mbps of
bandwidth and 10 ms of delay. The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of delay.
Each node uses a DropTail queue, of which the maximum size is 10. A“tcp” agent is attached to n0, and a connection
is established to a tcp “sink” agent attached to n3. As default, the maximum size of a packet that a “tcp” agent can
generate is 1KByte. A tcp “sink” agent generates and sends ACK packets to the sender (tcp agent) and frees the
received packets. A “udp” agent that is attached to n1 is connected to a “null” agent attached to n3. A “null” agent just
frees the packets received. A “ftp” and a “cbr” traffic generator are attached to “tcp” and “udp” agents respectively,
and the “cbr” is configured to generate 1 KByte packets at the rate of 1 Mbps. The “cbr” is set to start at 0.1 sec and
stop at 4.5 sec, and “ftp” is set to start at 1.0 sec and stop at 4.0 sec.

5
References:
1. Stephen F. Bush, Slides of Lecture 8: Network Simulator, Fall 2009
2. Ns by Example, Jae Chung and Mark Claypool, Sample program: http://nile.wpi.edu/NS/
3. http://www.isi.edu/nsnam/ns/

Das könnte Ihnen auch gefallen