Tuesday, 15 September 2015




M.S. RAMAIAH INSTITUTE OF TECHNOLOGY
BANGALORE
(Autonomous Institute, Affiliated to VTU)

Project members:- 

                                             Aditya Walvekar(1MS13IS004)
                                             Himanshu Sharma(1MS13IS040)

 


                                       

 


Data Communication Project



For calculating the number of packets
transferred in a network by simulating with ns2






ABSTRACT




In this project we create the nodes using the simulator
ns2 and calculate the throughput , the number of packets
transferred between these nodes in the network , and the
packet delivery ratio.







Acknowledgement



We are very grateful to S.R Manisekhar, Assistant Professor
M.S Ramaiah Institute of Technology, Bangalore for his able
guidance and advice in the development and completion of our Project.





THE NETWORK SIMULATOR –ns2


Ns-2 is a discrete event simulator targeted at networking research. Ns-2 provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks.Ns-2 is written in C++ and an Object oriented version of Tcl called OTcl.
History- Ns began as a variant of the REAL network simulator in 1989 and has evolved substantially over the past few years. In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI. Currently ns development is support through DARPA with SAMAN and through NSF with CONSER, both in collaboration with other researchers including ACIRI. Ns has always included substantal contributions from other researchers, including wireless code from the UCB Daedelus and CMU Monarch projects and Sun Microsystems.














set ns [new Simulator -multicast on]

#$ns multicast


#Turn on Tracing

set tf [open output.tr w]

$ns trace-all $tf


# Turn on nam Tracing

set fd [open mcast.nam w]

$ns namtrace-all $fd

#Create 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]



$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
$ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 30ms DropTail


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

$ns queue-limit $n2 $n3 20

$ns at 0.0 "$n0 label Sender1"
$ns at 0.0 "$n4 label Receiver1"

$ns at 0.0 "$n2 label Router1"
$ns at 0.0 "$n3 label Router2"

$ns at 0.0 "$n1 label Sender2"
$ns at 0.0 "$n5 label Receiver2"

#Setup a TCP connection

set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552

#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 $n5 $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_ 0.01mb
$cbr set random_ false

$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 10.0 "$ftp stop"
$ns at 10.5 "$cbr stop"


#Define a 'finish' procedure

proc finish {} {
        global ns tf#file1 file2
        $ns flush-trace
#        close $tf
#        close $fd
        exec nam mcast.nam &
#        exec awk -f PacketLoss.awk a.tr > output.tr &
        exit 0
}


$ns at 12.0 "finish"
$ns run


PacketLoss.awk

BEGIN {

dcount = 0;

rcount = 0;

}

{

event = $1;

if(event == "d")

{

dcount++;

}

if(event == "r")

{

rcount++;

}

}

END {

printf("The no.of packets dropped  : %d\n ",dcount);

printf("The no.of packets recieved : %d\n ",rcount);

}


# Filename: test12.tcl


#create links between the nodes

$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
$ns duplex-link $n3 $n4 0.5Mb 40ms DropTail
$ns duplex-link $n3 $n5 0.5Mb 30ms DropTail


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

$ns queue-limit $n2 $n3 20

$ns at 0.0 "$n0 label Sender1"
$ns at 0.0 "$n4 label Receiver1"

$ns at 0.0 "$n2 label Router1"
$ns at 0.0 "$n3 label Router2"

$ns at 0.0 "$n1 label Sender2"
$ns at 0.0 "$n5 label Receiver2"

#Setup a TCP connection

set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552

#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 $n5 $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_ 0.01mb
$cbr set random_ false

$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 10.0 "$ftp stop"
$ns at 10.5 "$cbr stop"


#Define a 'finish' procedure

proc finish {} {
        global ns file1 file2
        $ns flush-trace
        close $file1
        close $file2
        exec nam test12.nam &
      exec awk -f PacketLoss.awk test12.tr > output.tr &
        exit 0
}


$ns at 12.0 "finish"
$ns run

############################################################

#Filename: PacketLoss.awk

#--------- Formula ------------:
Packet Loss = GeneratedPackets - ReceivedPackets

#--------- AWK script Format--------#

The script has the following format:
BEGIN {}
{
content
}
END {}
Begin part comprises of initialization of variable.
Commands in the content part scan every row of trace file only once.
Ex:
    if (pattern) {
        action;
    }

If the pattern is matched with the line in the trace in the trace file specified action will be performed.

In the END part final calculation is performed on the data obtained from the content part.
   
#--------- Steps ------------:
1.    set pattern and action for received packets
2.    set pattern and action for generated packets
3.    Apply the result in formula


 









To find the throughput of the Network


   BEGIN {

          recvdSize = 0
          startTime = 400
          stopTime = 0
     }
      
     {
                event = $1
                time = $2
                node_id = $3
                pkt_size = $8
                level = $4
      
     # Store start time
     if (level == "AGT" &;& event == "s" && pkt_size >= 512) {
       if (time <; startTime) {
                startTime = time
                }
          }
      
     # Update total received packets' size and store packets arrival time
     if (level == "AGT" &;& event == "r" && pkt_size >= 512) {
          if (time >; stopTime) {
                stopTime = time
                }
          # Rip off the header
          hdr_size = pkt_size % 512
          pkt_size -= hdr_size
          # Store received packet's size
          recvdSize += pkt_size
          }
     }
      
     END {
          printf("Average Throughput[kbps] = %.2f\t\t StartTime=%.2f\tStopTime=%.2f\n",(recvdSize/(stopTime-startTime))*(8

To print the Congestion window size


   BEGIN {
    
   }
   {
   if($6=="cwnd_") {
       printf("%f\t%f\n",$1,$7);
   }
   }
   END {
    
   }
 
 
 
To print packet Delivery ratio 


BEGIN {
           sendLine = 0;
           recvLine = 0;
           fowardLine = 0;
   }
    
   $0 ~/^s.* AGT/ {
           sendLine ++ ;
   }
    
   $0 ~/^r.* AGT/ {
           recvLine ++ ;
   }
    
   $0 ~/^f.* RTR/ {
           fowardLine ++ ;
   }
    
   END {
           printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
   }
 
AWK Script for calculating the Send, Received, Dropped 
Packets, Received Packets, Packet Delivery Ratio and Average end to End 
Delay 


   BEGIN {

   seqno = -1; 

   droppedPackets = 0;
   receivedPackets = 0;
   count = 0;
   }
   {
   #packet delivery ratio
   if($4 == "AGT" &;& $1 == "s" && seqno < $6) {
   seqno = $6;
   } else if(($4 == "AGT") && ($1 == "r")) {
   receivedPackets++;
   } else if ($1 == "D" && $7 == "tcp" && $8 > 512){
   droppedPackets++; 
   }
   #end-to-end delay
   if($4 == "AGT" &;& $1 == "s") {
   start_time[$6] = $2;
   } else if(($7 == "tcp") && ($1 == "r")) {
   end_time[$6] = $2;
   } else if($1 == "D" && $7 == "tcp") {
   end_time[$6] = -1;
   }
   }
    
   END { 
   for(i=0; i<=seqno; i++) {
   if(end_time[i] >; 0) {
   delay[i] = end_time[i] - start_time[i];
   count++;
   }
   else
   {
   delay[i] = -1;
   }
   }
   for(i=0; i<count; i++) {
   if(delay[i] >; 0) {
   n_to_n_delay = n_to_n_delay + delay[i];
   } 
   }
   n_to_n_delay = n_to_n_delay/count;
   print "\n";
   print "GeneratedPackets = " seqno+1;
   print "ReceivedPackets = " receivedPackets;
   print "Packet Delivery Ratio = " receivedPackets/(seqno+1)*100
   "%";
   print "Total Dropped Packets = " droppedPackets;
   print "Average End-to-End Delay = " n_to_n_delay * 1000 " ms";
   print "\n";
   }
 
 
 
 
Screenshot from 2013-03-24 22^%04^%58
 

Description:


Packet loss in a communication is the difference between the generated and received packets. Packet Loss is calculated using awk script which processes the trace file and produces the result.

Screenshot:

Congestion