You are on page 1of 7

11/12/2009

NS2 by Example

Obaid Ullah Khalid

Network Topology

 6 nodes with 5 links


 Two different flows
◦ 1-3-4-5
◦ 2-3-4-6
 Link 3-4 is the bottleneck
 Flow 1 runs FTP traffic over TCP Reno
 Flow 2 runs CBR(Constant Bit Rate) over UDP.

1
11/12/2009

The simulator object and Trace files


 Create a simulator object
◦ set ns [new Simulator]

 Open the NAM and Trace file


◦ set nf [open out.nam w]
◦ $ns namtrace-all $
$ $nf
◦ set f0 [open out1.tr w]
◦ ‘w’ specifies to write on the file

Creating the topology


 Create six nodes
◦ set n1 [$ns node]
◦ set n22 [$ns
[$ node]
d ]
◦ set n3 [$ns node]
◦ set n4 [$ns node]
◦ set n5 [$ns node]
◦ set n6 [$ns node]

 $ns node
$ d creates the
h new node
d object
bj
 Using for loop:
◦ for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node] }

2
11/12/2009

Creating the topology


 Create the 5 links: duplex links, 2Mb
bandwidth (except link3-4 with 1Mb
bandwidth), 10ms delay and a droptail
queue.
◦ $ns duplex-link $n1 $n3 2Mb 10ms DropTail
◦ $ns duplex-link $n2 $n3 2Mb 10ms DropTail
◦ $ns duplex-link $n3 $n4 1Mb 10ms DropTail
◦ $ns duplex-link $n4 $n5 2Mb 10ms DropTail
◦ $ns duplex-link $n4 $n6 2Mb 10ms DropTail

Creating Flow1(TCP)
 Source:
◦ set tcpp [[new Agent/TCP/Reno]
g ]
◦ $tcp set packetsize_ 1000 (set packet size to
1000 bytes)
◦ $ns attach-agent $n1 $tcp (attach it to node 1)
 Sink:
◦ set sink [new Agent/TCPSink]
◦ $ns attach-agent $n5 $sink (attach sink to node5)
 Connect source and sink
◦ $ns connect $tcp $sink (connect source and sink)

3
11/12/2009

Traffic over Flow 1(FTP)


 FTP over TCP connection
◦ set ftp [new Application/FTP]
◦ $ftp attach-agent $tcp
 The 2nd command attaches the traffic
agent to the source.

Creating Flow 2(UDP)


 Source:
◦ set udp [new Agent/UDP]
◦ $ns attach-agent $n2 $udp
 Sink:
◦ set null0 [new Agent/Null] (sink)
◦ $ns attach-agent $n6 $null0
 Connect source to sink:
◦ $ns connect $udp $null0

4
11/12/2009

Traffic over Flow 2 (CBR)


 CBR over UDP
◦ set cbr0 [new Application/Traffic/CBR]
◦ $cbr0 set packetSize_ 500
◦ $cbr0 set interval_ 0.005 (packets sent every
0.005 seconds)
◦ $cbr0 attach-agent $udp

Sending and stopping the data


 Schedule events for both flows
◦ $ns at 0.0
0 0 "$cbr
$cbr start
start"
◦ $ns at 0.0 "$ftp start"
◦ $ns at 25.0 "$ftp stop"
◦ $ns at 30.0 "$cbr stop"

5
11/12/2009

Finish Procedure
 Closes the trace files, starts NAM and flushes out
any old values/traces.
◦ proc finish
fi i h {} {
global ns nf f0
$ns flush-trace
#Close the NAM trace file
close $nf
close $f0
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}

Finalizing and running the simulation


 The finish procedure i.e. the simulation is
stopped after the specified time
◦ $ns at 30.0 "finish"

 The last command starts the simulation


◦ $ns run

6
11/12/2009

Extras
 Creating a diagram of your simulation on
NAM
◦ $ns duplex-link-op $n1 $n3 orient right-down
◦ $ns duplex-link-op $n2 $n3 orient right-up
◦ $ns duplex-link-op $n3 $n4 orient right
◦ $ns duplex-link-op $n4 $n5 orient right-up
◦ $ns duplex-link-op $n4 $n6 orient right-down

Extras
 Setting up the queue size (link 3-4)
◦ $ns queue-limit
queue limit $n3 $n4 10 (10 packet queue
size)

 Monitor the queue


◦ $ns duplex-link-op $n3 $n4 queuePos 0.5 (0.5
specifies the angle in NAM)

You might also like