More ns2; More Internetworks




CS158a

Chris Pollett

Mar. 23, 2011

Outline

  • Trace files in ns2
  • grep and AWK
  • AWK and Trace Files
  • Error Reporting (ICMP)
  • Virtual Networks and Tunnels
  • More on ns2

    ns2 script with ftp, cbr traffic.

    set ns [new Simulator]
    
    
    #open trace files for writing
    set tf [open out.tr w]
    set nf [open out.nam w]
    $ns trace-all $tf
    $ns namtrace-all $nf
    
    proc finish {} {
        global ns nf tf
            $ns flush-trace
            close $tf
            close $nf
        #close trace files
            exit 0
    }
    
    #Create nodes
    set n0 [$ns node]
    set n1 [$ns node]
    set n2 [$ns node]
    set n3 [$ns node]
    
    #Create link between 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
    
    #Set up 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
    
    #Set up 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
    
    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 event for our traffic
    $ns at 0.1 "$cbr start"
    $ns at 1.0 "$ftp start"
    $ns at 4.0 "$ftp stop"
    $ns at 4.5 "$cbr stop"
    
    $ns at 5.0 "finish"
    $ns run
    

    Some Remarks on the Script

    The ns2 trace file format

  • Here is a brief snippet out of the out.tr tracefile, slightly after the 1 second mark so that we can we both the cbr and tcp traffic:
    r 1.109002 2 3 cbr 1000 ------- 2 1.0 3.1 121 125
    r 1.113896 2 3 tcp 1040 ------- 1 0.0 3.0 2 124
    + 1.113896 3 2 ack 40 ------- 1 3.0 0.0 2 132
    - 1.113896 3 2 ack 40 ------- 1 3.0 0.0 2 132
    r 1.114 1 2 cbr 1000 ------- 2 1.0 3.1 125 129
    + 1.114 2 3 cbr 1000 ------- 2 1.0 3.1 125 129
    - 1.114 2 3 cbr 1000 ------- 2 1.0 3.1 125 129
    + 1.116 1 2 cbr 1000 ------- 2 1.0 3.1 127 133
    
  • More ns2 trace file format

    Mining trace files

    Mining trace file -- awk

    AWK code

    More awk examples

    # use tab rather than space as a field separator (FS)
    # compute the average length of first field
    BEGIN {FS = "\t"}
    {nl++}
    {s=s+length($1)}
    END{ print "average = " s/nl}
    
    #As another example - compute
    # the number of lines, number of words, and the number of char's in a file
    {w += NF; c += length}
    END {print NR, w, c}
    

    Associative Arrays in AWK

    # computes word frequencies in a doc
    BEGIN { FS = "[^a-zA-Z]+"}
    { for(i = 1; i < NF; i++)
        words[tolower($i)]++
    } 
    END { for (i in words)
        print i, words[i]
    }
    

    Associative Arrays in AWK

    function Square(number, temp) {
       temp = number * number
       return temp
    }
    END {print Square(9)}
    

    Notice in the argument list we include local variables of the function; however, when we call the function we just give the value for the non-local variables.

    An example of an AWK script for use with an NS-2 trace file

    # Script to compute the packet loss rate between
    # nodes 1,2 for the application having the flow id = 2
    #
    BEGIN {
    #fsDrops : packets dropped.  numFS: packets sent
        fsDrops = 0;
        numFs = 0;
    }
    {
        action = $1;
        time = $2;
        from = $3;
        to =  $4;
        type = $5;
        pktsize = $6;
        flags = $7;
        flow_id = $8
        src = $9;
        dst = $10;
        seq_no = $11;
        packet_id = $12;
    
        if(from == 1 && to == 2 && action = "+") numFs++;
        if(flow_id == 2 && action == "d") fsDrops++;
    }
    END {
       printf("number of packets sent:%d lost %d\n", numFs, fsDrops);
    }
    

    Error Reporting (ICMP)

    Virtual Networks and Tunnels

    Why bother to use tunnels?