CS158a
Chris Pollett
Mar. 23, 2011
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
$ns trace-queue $n2 $n3 $file1
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
"-": disable
1st = "E": ECN (Explicit Congestion Notification) echo is enabled.
2nd = "P": the priority in the IP header is enabled.
3rd : Not in use
4th = "A": Congestion action
5th = "E": Congestion has occurred.
6th = "F": The TCP fast start is used.
7th = "N": Explicit Congestion Notification (ECN) is on.
$ns at $time "$ns trace-annotate \"My comment\""Such a line would might look like:
v $time eval {set sim_annotation {My Comment}}
in the trace file.
grep "regex" input_file_name > output_file_name
grep "0 2 tcp" out.tr > node12.tr #only packets going from node 0 to 2 grep "^r" out.tr > received.tr #only lines starting with r i.e., received lines grep "^r" out.tr | grep "tcp 1040" > received.tr #only received tcp packets with size 1040
awk -f my_code.awk my_data.txt or awk -f my_code.awk my_data.txt > my_output.txt
/pattern/ {action}
BEGIN {action }
# executes action commands before processing any of the file
END {action }
# executes action commands after processing the file
/pattern/
# (no action) prints the line
{ action }
#(no pattern) executes the command for each line of the input
{print $1}
# 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}
# 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]
}
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.
# 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);
}