CN Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

NS Simulation Basic

.Common NS classes

The following figure shows the most commonly used NS classes in writing anetwork simulation:

Application/FTP

Agent/TCP/Reno Agent/TCPSink

Node Node

$NS duplex-link

o A quick overview on these classes:

1. Node: simulate routers. They simulate the IP protocol layer.

2. Links: links are created using the duplex-link method call in an Ns simulation object.

3. Agent/TCP/Reno and Agent/TCP/Tahoe: simulate the Semding TCP protocol layer.

A Agent/TCP/Reno or Agent/TCP/Tahoe needs to be attached to some Node (see figure)

Also, a Agent/TCP/Reno or Agent/TCP/Tahoe need to be associated with a Agent/TCP/Sink!


4. Agent/TCP/Sink: simulate the Receiving TCP protocol layer.
A Agent/TCP/Sink sends ACK packets and necds to be attached to some Node (see figure)

5. Application/FTP: simulate an continuously transmitting agent that uses a TCP protocol (Tahoe, Reno, or some other version of
TCP)
A Application/FTP needs to be attached to some TCP agent (see figure)

The Application/FTP does not need to be asscoiated with a receiver:

The FTP agent triggers the TCP agent to transmit packets


TCP packets are acknowledged by the asscociated TCP-Sink
When the TCP source receives the ACK, it reports to the associated FTP agent that it can transmit more.

Steps in writing a network simulation in NS

1. Create the network:

1. Create nodes (routers): click here


2. Connect the nodes with (duplex) links: click here
2. Define transport sources and sinks at end point nodes

1.Create transport source agents (Agent/TCP/Reno) and transport sink agents (Agent/TCP/Sink)|
2. Associate the transport source/sink agent to a end point node
3. Connect a source transport agent to a transport sink agent

3. Put traffic load (Application/FTP) on the transport sources

.Using NS
oIn order to use the NS simulator, you need to create a Simulator object

provides many methods needed to construct the network simulation.


The Simulator object is the network simulation system and

o How to create a Simulator object:


new Simulator

o Example:

set ns [new Simulator]

You only need one Simulator object

see this statement at the top of the program!


Ifyou look in the source of a NS script, you will always

.Creating Nodes in NS
o The Node class in NS is used to simulate routers

oThe node must be created through the special method node defined in the Simulator class.
object in NS that you use to simulate ONE
router:
The following expression will return a Node

set ns [new Simulator]

[Sns node]
o Use a set command to remember the Node object in some variable (e.g., n0):

set ns [new Simulator]

set ne [$ns node]

o If you need to create a lot of routers, use a for loop.

Example: create Nodes n(0), n(1), .., n(9):

for set i 0} {$i « 10} {incr i} {


set n($i) [Sns node]

.Creating Linksin NS
o Links in NS are used to provide connectivity between Nodes (routers)

oLinks is not implemented as a class, but as a part ofthe Simulator object

o Links have a number of properties:

1. Duplex (bi-directional) or Simplex (uni-directional)

BTW, a duplex link is actually 2 simplex links..

2. Bandwidth (data transmission rate)

Units: b (#bits/sec), Mb (#Megabits/sec)

3. Propagation delay

Units: s (seconds), ms (milliseconds)

4. Queue management: specifies how packets in the queue are managed

Some commonly used values:

DropTail: drop the last arriving packet when queue is full


RED: Random Early Drop method (this method tries to breakthe TCP Synchronization Syndrome)

DDR: Deficit Round Robin method (research material)

FQ: Fair Queuing method (research material)

SFQ: Stochastic Fair Queuing method (research material)

We will mostly use DropTail

oExample: duplex (bi-directional) link, 10 Mbps, 10 msec delay


set ne [$ns node]
set n1 [sns node]

$ns duplex-link $ne $n1 10Mb 10ms DropTail

.NS Programming trick: More flexible way to ereate links

Often, you want to experiment with the same network configuration but using diferent bottleneck link capacities.

For example, the bottleneck link isftimes the normal bandwidth in other network links
o Trick:

set bw 10
set f 9.5

$ns duplex-link $ne $n1 [expr $bw]Mb 1@ms DropTail


Sns duplex-link $ne $n1 [expr $f*$bw]Mb 10ms DropTail1

Link: Changing its buffer size


oYou can change the buffer size of one particular link using the sns queue-1imit method

o
Example: set the queue length to 10 packets

$ns duplex-link $ne $n1 19Mb 1ems DropTail

$ns queue-limit $ne $n1 10

Link: Changing the default buffer size of all links


o Links in NS are created with a certain default queue size

o Each class (DropTail, RED, FQ, etc) of queue has its own default queue size
oYou can change the buffer size ofa class of queue using the set queue-limit command in the class Queue/QUEUE-CLASS

Example: set the default queue length of DropTail queues to 10 packets

||Queue/DropTail set queue-limit $ne $n1 10

.Routingin NS

After you created the nodes and connected them through links, NS will provide Internet routing automatically

oSo, aftercreated the nodes and connected them through links, you have an Internet.
o All you need to do now is:

Add transport protocol to the end point nodes (no need to do so to intermediate nodes)

Add traffic (load)

.Transport LayerProtocol modules


o For one reason or another, the transport protocol modules in NS are called agents

oThere are an ever increasing number ofsending transport agents (because people are developing new TCP transnmission protocols to
improve network performance.

o There are 2 receiving transport agents that sends back ACKs to the sending agent.

oThe most commonly used sending transport protocol modules are:

1. Agent/TCP/Tahoe: TCP Tahoe

2. Agent/TCP/Reno: TCP Reno

3. Agent/TCP/Newreno: Improved TCP Reno that paces the transmission interval|


4. Agent/TCP/Vegas: TCPVegas
Example:

set tcp1 [new Agent/TCP/Reno]

oThe most commonly used receiving transport protocol modules are:

1. Agent/TCPSink: sends ACK packet immediately after receiving a packet

2. Agent/TCPSink/DelAck: ACK is delayed. Receiver sends ACK packet after receiving 2 packets or after a certain delay
(tries to reduce number of ACK packets)

Example:

set sink1 [new Agent/TCPSink]

Connecting Transport Protocol Agents to Nodes

oTransport agents are end points of communication

They can be attached to any Node object


o You can attach multiple Transport agents to one Node object

used to associate Transport agents to a Node:


o The attach-agent command in a Simulator object is a

$ns attach-agent NODE TCP-Agent

o Example:
set ns [new Simulator
set node1 [$ns node]
set tcp1 [new Agent/TCP/Reno]

Sns attach-agent $node1 $tcp1

.Connecting a Sending Transport Agent to a Receiving Transport Agent


o Transport agents anre end points of communication

We need to tell NS the destination of each sending Transport Agent.


The destination must be a receiving Transport Agent.
o In order to tell NS where to transmit the packets, we need to connect a sending communication end points to a receiving
communication end points

This is done through the NS method: connect SOURCE DESTINATION

o Exanmple:

set ns [new Simulator]


set tcp1 [new Agent/TCP/Reno]
set sink1 [new Agent/TCPSink]

(tcp1 and sink1 must also be attached to nodes, this step is omitted)

$ns connect $tcp1 $sink1


This will make NS route all packets from the source tcp1 towards the destination sink1

Some Parameters of the TCP module

o After a TCP agent has been created, you can set some of its parameters (using the set command of the TCP agent)

oThe most useful parameters are:


1. packetSize_: the packet size used by TCP (default packet size is 1000 bytes)
2. maxcwnd_: the maximum of CWND (0 means infinite)
3. window_: the Advertised Window size
Example:

set tep1 [new Agent/TCP/Reno]


$tcp1 set packetsize 552
o NOTE: recall that the set method without arguments will return the value of the variable !

Example:

set tcp1 [new Agent/TCP/Reno]

set pksize ($tcp1 set packetSize_] / get packetzise used

Generating Traffice for TCP

oThe standard traffic generator


for TCP protocol is the FTP application.
o The FTP application transmits continuously -
as long as the TCP protocol permits.
o FTP application are created using:

new Application/FTP

o Example:

[new Application/FTP]
set ftp1
.Associating FTP Traffic Generator with a TCP protocl module
o You need to associate a FTP traftic generator with a sending TCP module
oThe association is made using the attach-agent method defined in the Application/FTP class.
o Example:

set tcp1 [new Agent/TCP/Reno]


set ftp1 [new Application/FTP]
$ftp1 attach-agent Stcp1

Starting the FTP traffic generator


o The FTP traffic generator is run (started) using the start method defined in the Application/FTP class.
o Example:

set ftp1 [new Application/FTP]


$ftp1 start // Start the FTP application

Stopping the FTP traffie generator

oThe FTP traffic generator canbe stopped usingthe stap method defined in the Application/FTP class.
o Example:

set ftp1 [new Application/FTP]

$ftp1 stop // Stop the FTP application

o NOTE: the TCP module does not need to be stopped and in fact, cannot be stopped.

When the FTP traffic generator stops generating traffic, TCP will have no data to send and will "go quiet"
You can start the FTP again after stopping it.
Part- A Programs

.Implenment three nodes point - to - point network with duplex links berwee
the queue size, ped.
vary the bandwidth, and find the number of
packets uP

#Create a simulator object

set ns (new Simulator]

#Open the trace file

set ntrace [open prog1.tr w]

Sns trace-all $ntrace

#Open the NAM trace file

set namfile [open prog1.nam w]

Sns namtrace-all $namfile

#Define a finish' procedure

procfinish (0

global ns ntrace namfile

close the files


# Dump all the trace data and

$ns flush-trace

close $ntrace

close $namfile
HExecute NAM Animation file.

#To call subprocesses within another process, exec is used, which creates a suopr

it to complete.

exec nam prog1.nam &

#show the number of packets dropped

e x e c e c h o " T h e n u m b e r o f p a c k e t d r o p is " &

in the files. Here

*Brep" isa simple yet powerful unix/linux command. It searchesfor a given string
is the syntax of grep

count of matching lines only.


#grep [options] <pattern> [<file>] option c : Output

execgrep- "d" prog1.tr &

exit 0

#Create three nodes

set no [$ns node]

set n1 Sns node]

set n2 [$ns node]

# label the nodes

$no label "TCP Source"


Destination"
$n2 label "Sink/

#Define color for data flows (for NAM)

Sns color 1 Blue


#Create links between the nodes

#Modification on bandwidth is required to observe variation in packet drop

$ns duplex-link $nO $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

#Give node
position/orientation (for NAM)
$ns duplex-link-op $no $ni orient right

Sns duplex-link-op $n1 $n2 orient right

#Set Queue Size of link.

# You can modify the que length as well to observe variation in packet.

$ns queue-limit $no Sn1 10

Sns queue-limit $n1 $n2 10

#Setup a Transport layer TCP connection

set tepo [new Agent/TCP]

$ns attach-agent $nO $tcp0

set sinko [new Agent/TCPSink]

$ns attach-agent $n2 $sinko

Sns connect $tcp0 $sinko


NSetup an
Application Layer FTP over TCP connection
set ftpo [new Application/FTP]
$ftp0 attach-agent $tepo
$ftp0 set type_ FTP

# for colour attachment

Stcp0set class_1

#Schedule events
$ns at 0.0 "$ftp0 start"

#Call the finish procedure after 3 units of simulation time


Sns at 3.0 "finish"

#Run the simulation

$ns run

2. Implement transmission of ping messages/trace route over a network topology consisting


of 6 nodes and find the number of packets dropped due to congestion

#Create a simulator object

set ns (new Simulator

#Use colors to differentiate the traffic (for NAM)

Sns color 1 Blue

Sns color 2 Red


#Open the trace file

set ntrace [lopen prog2.tr w]


$ns trace-all $ntrace

#Open the NAM trace file

set namfile lopen prog2.nam w


$ns namtrace-all $namfile

#Define a finish' procedure

proc finish {}{

global ns ntrace namfile

#Dump all the trace data and close the files

$ns flush-trace

close $ntrace

close $namfile

#Execute NAM Animation file.

#To call subprocesses within another process,


exec is used, which creates a subprocess and waits for
it to complete.

exec nam prog2.nam &

#show the number of ping packets dropped

exec echo "The number of ping packet dropped are "&


command. It searches for a given string in the files. Here
# "grep" is a simple yet powerful unix/linux
is the syntax of grep
#grep [options] <pattern> [<file>] option-c:
Output count of matching lines ony
exec grep "d" prog2.tr | cut -d" " -f5 grep-c "ping" &

exit

#Create six nodes using for loop : n0 [$ns node]

for (set i 0} H$i < 6} tincr i){

set n($i) I$ns node]

#Connect the nodes with two links $ns duplex-link $no $n1 1Mb 10ms DropTail

for (set j 0} ($i < 5) fincr i}

Sns duplex-link $n($i) $n([expr (Sj+1)]) 0.1Mb 10ms DropTail

#Define a 'recv' function for the class 'Agent/Ping'

Agent/Ping instproc recv{from rtt}

$self instvar node_

answer from \$from with round-trip-time $rtt ms."


puts "node [$node_ id] received ping

and n5
#Create two ping agents and attach them to the nodes n0

set po [new Agent/Ping]


#forcolor class

$p0set class_1
Sns attach-agent $n(0) $po

set p1 [new Agent/Ping]

# for color class

$p1 set class 1

$ns attach-agent $n(5) $pl

#Connect the two agents

$ns connect $po $p1

#Set Queue Size of link.

# You can modify the que length as well to observe variation in packet.

Sns queue-limit $n(2) $n(3) 2

#Monitor the queue for link $n(2) to $n(3)

Sns duplex-link-op $n(2) $n(3) queuePos 0.5

#create Congestion

#Generate a huge FTP traffic between n{2) and n{4)

#Setup a Transport layer TCP connection

set tepo new Agent/TCP]

# for color class

$tcp0 set class_2

Sns attach-agent $n(2) Stcpo


set sinko [new Agent/TCPSink]
$ns attach-agent $n(4) $sinko

Sns connect $tcp0 $sinko

#Setup an Application Layer FTP over TCP connection


set ftpo Inew Application/FTP]
$ftp0 set packetsize_ 500

$ftp0 set rate_1Mb

$ftp0 attach-agent $tcp0


$ftp0 set type_FTP

#Schedule events

Sns at 0.2 "$p0 send"

$ns at 0.4 "$p1 send"

Sns at 0.4 "$ftp0 start"

$ns at 0.8 "$p0 send"

$ns at 1.0 "$p1 send"

Sns at 1.2 "$ftp0 stop"

Sns at 1.4 "$p0 send"

Sns at 1.6 "$p1 send"

$ns at 1.8 "finish"

#Run the simulation

$ns run

You might also like