myNewMod.tcl

advertisement
TNK092: Network Simulation/Nätverkssimulering
Network Simulation---ns2
Lecture 2
An Example with all we’ve seen so far.
set ns [new Simulator]
#Define different colors
#for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAMtrace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the Trace file
set tf [open out.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {} {
global ns nf tf
$ns flush-trace
#Close the NAM trace file
close $nf
#Close the Trace file
close $tf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#Create links between the 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
#Give links position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
n1
n0 n0
n2
n1
n2
n3
n3
2
An Example with all we’ve seen so far..
#Setup
TCP connection
set ns a[new
Simulator]
set
tcp [new
Agent/TCP]
#Define
different
colors
$ns
$tcp
#forattach-agent
data flows $n0
(for
NAM)
set
sink
[new
Agent/TCPSink]
$ns color 1 Blue
$ns attach-agent $n3 $sink
$ns color 2 Red
$ns connect $tcp $sink
$tcp set fid_ 1
#Schedule
events
for the CBR and FTP agents
#Create four
nodes
set at
n00.1
[$ns
node]
$ns
"$cbr
start"
set at
n11.0
[$ns
node]
$ns
"$ftp
start"
set at
n24.0
[$ns
node]
$ns
"$ftp
stop"
set
n3
[$ns
node]
$ns at 4.5 "$cbr stop"
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#Open
thetype_
Trace
$ftp set
FTPfile
#Call
finish
procedure
#Givethe
links
position
(forafter
NAM)5
#seconds
of simulation$n0
time
$ns duplex-link-op
$n2 orient right-down
$ns
5.0 "finish” $n1 $n2 orient right-up
$ns at
duplex-link-op
#Open the NAMtrace file
set nf [open out.nam w]
#Setup an FTP over TCP connection
$ns namtrace-all $nf
set tf [open out.tr w]
$ns
trace-all
$tf
#Setup
a UDP connection
set udp [new Agent/UDP]
#Define
a 'finish'
$ns attach-agent
$n1 procedure
$udp
proc
finish
{
set null
[new {}
Agent/Null]
global ns
tf
$ns attach-agent
$n3nf
$null
$ns connect
$null
$ns$udp
flush-trace
$udp set #Close
fid_ 2 the NAM trace file
close $nf
#Setup a CBR over UDP connection
#Close the Trace file
set cbr [new Application/Traffic/CBR]
close $tf$udp
$cbr attach-agent
$cbr set #Execute
type_ CBR NAM on the trace file
nam out.nam
$cbr set exec
packet_size_
1000 &
$cbr set exit
rate_ 01mb
$cbr set random_ false
}
#Create links between the nodes
#Detach
tcp and sink
(not
so necessary)
$ns duplex-link
$n0agents
$n2 2Mb
10ms
DropTail
$ns
at
4.5
"$ns
detach-agent
$n0
$tcp
;
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
in
SAME line $n2
$ns $n3
detach-agent
$n3DropTail
$sink"
$nsthe
duplex-link
1.7Mb 20ms
$ns duplex-link-op $n2 $n3 orient right
#Print CBR packet size and interval
puts
packet
= [$cbr
set packet_size_]"
#Set "CBR
Queue
Sizesize
of link
(n2-n3)
to 10
$ns
queue-limit
$n2
$n3
10
puts "CBR interval = [$cbr set interval_]"
cbr
#Run
the simulation
#Monitor
the queue for link (n2-n3). (for NAM)
$ns run
duplex-link-op $n2 $n3 queuePosftp0.5
$ns
0 0.1 1.0
ftp
4.0 4.5
tcp
n0
sink
cbr
n2
upd
n3
null
n1
3
sec
An Example with all we’ve seen so far... epimyth

To create agents or traffic sources, a user should know the class names
these objects (Agent/TCP, Agnet/TCPSink, Application/FTP and so on).

Remember to look at look at the …/tcl/libs/ns-default.tcl file.

This file contains the default configurable parameter value settings for
available network objects.

Hint: a good indicator of what kind of network objects are available in NS
and what are their configurable parameters.
cbr
ftp
0 0.1 1.0
ftp
4.0 4.5
tcp
n0
sink
cbr
n2
upd
n3
null
n1
4
sec
Tracing

Besides the NAM trace ns2 can provide an ASCII trace file.

At every link a packet is
5
Trace Enabled Simple NS Simulation Script
6
About Awk
AWK is a tool that allows “simple” data manipulation on trace files.
Sample AWK code:
BEGIN {sum+=$2; array[N]=$2} END
{ for(i=1;i<=N;i++) {
sumsq+=((array[i]-(sum/N))^2);
}
print sqrt(sumsq/N)
}
_______________________________________________________________________________________________________________________
To run use:
awk -f Average.awk out.tr
7
End to end delay (measure-delay.awk)
BEGIN {
#initial the highest packet id
highest_packet_id = 0;
}
{
event = $1;
time = $2;
from = $3;
to = $4;
type = $5;
pktsize = $6;
flow_id = $8;
src = $9;
dst = $10;
seq_no = $11;
packet_id = $12;
END {
for (packet_id=0; packet_id<=highest_packet_id; packet_id++ ) {
start = start_time[packet_id];
end = end_time[packet_id];
packet_duration = end - start;
if ( start < end ) printf("%f %f\n", start, packet_duration);
}
}
if ( packet_id > highest_packet_id )
highest_packet_id = packet_id;
if ( start_time[packet_id] == 0 )
start_time[packet_id] = time;
if ( flow_id == 2 && action != "d" ) {
if ( event == "r" ) {
end_time[packet_id] = time;
}
} else {
end_time[packet_id] = -1;
}
Remember . . .
awk -f measure-delay.awk out.tr > cbr-delay
}
8
Plotting with gnuplot
Gnuplot(http://www.gnuplot.info/;http://www.gnuplot.info/documentation.html)
#plotdelay.plot
set term png medium #000000
set output "cbr-delay.png"
set ylabel "End-to-End delay(sec)"
set xlabel "Start transmission time(sec)"
set xrang [0:5]
set xtics 0, 0.5, 5
set yrang [0:0.1]
set ytics 0, 0.01, 0.1
set title "CBR end-to-end delay"
plot "cbr-delay" title "CBR" with linespoints lt -1 pt 8
gnuplot>load ’plotdelay.plot’
9
xgraph
xgraph cbr-delay
10
Jitter (awk codes)
#cbr-jitter.awk
BEGIN {
old_time=0;
old_seq_no=0;
i=0;
}
{
action = $1;
time = $2;
node_1 = $3;
node_2 = $4;
type = $5;
flow_id = $8;
node_1_address = $9;
node_2_address = $10;
seq_no = $11;
packet_id = $12;
END {
for (j=1; j <i ;j++)
printf("%d\t%f\n",seq[j],jitter[j]);
}
awk –f cbr-jitter.awk out.tr > cbr-jitter
if(node_1==2 && node_2==3 && type=="cbr" &&
action=="r")
{
dif=seq_no-old_seq_no;
if(dif==0)
dif=1;
jitter[i]=(time-old_time)/dif;
seq[i]=seq_no;
i=i+1;
old_seq_no=seq_no;
old_time=time;
}
}
11
Jitter (...more awk codes)
#cbr-jitter2.awk
END {
BEGIN {
last_seqno = 0;
highest_packet_id = 0;
last_delay = 0;
}{
seqno_diff = 0;
action = $1;
for ( packet_id = 0;
time = $2;
packet_id <= highest_packet_id; packet_id++ ) {
node_1 = $3;
start = start_time[packet_id];
node_2 = $4;
end =end_time[packet_id];
type = $5;
packet_duration = end - start;
flow_id = $8;
if ( start < end ) {
node_1_address = $9;
seqno_diff = pkt_seqno[packet_id]- last_seqno;
node_2_address = $10;
delay_diff = packet_duration - last_delay;
seq_no = $11;
if (seqno_diff == 0) {
packet_id = $12;
jitter =0;
if ( packet_id > highest_packet_id )
} else {
{
jitter = delay_diff/seqno_diff;
highest_packet_id = packet_id;
}
}
printf("%f %f\n", start, jitter);
if ( start_time[packet_id] == 0 ) {
last_seqno = pkt_seqno[packet_id];
pkt_seqno[packet_id] = seq_no;
last_delay = packet_duration;
start_time[packet_id] = time;
}
}
}
if ( flow_id == 2 && action != "d" )
}
{
if ( action == "r" ) {
$awk –f cbr-jitter2.awk out.tr > cbr-jitter
end_time[packet_id] = time;
}
} else {
end_time[packet_id] = -1;
}
12
}
Packet loss (awk codes)
# cbr-loss.awk
BEGIN {
fsDrops = 0;
numFs = 0;
}
{
action = $1;
time = $2;
node_1 = $3;
node_2 = $4;
src = $5;
flow_id = $8;
node_1_address = $9;
node_2_address = $10;
seq_no = $11;
packet_id = $12;
if (node_1==1 && node_2==2 && action == "+")
numFs++;
if (flow_id==2 && action == "d")
fsDrops++;
}
END {
printf("number of packets sent:%d lost:%d\n", numFs,
fsDrops);
}
13
Throughput (awk codes)
BEGIN {
init=0;
i=0;
}
{
action = $1;
time = $2;
from = $3;
to = $4;
type = $5;
pktsize = $6;
flow_id = $8;
src = $9;
dst = $10;
seq_no = $11;
packet_id = $12;
if(action=="r" && from==2 && to==3 && flow_id==2) {
pkt_byte_sum[i+1]=pkt_byte_sum[i]+ pktsize;
if(init==0) {
start_time = time;
init = 1;
}
end_time[i] = time;
i = i+1;
}
}
END
{ printf("%.2f\t%.2f\n", end_time[0], 0);
for(j=1 ; j<i ; j++){
th = pkt_byte_sum[j] / (end_time[j] start_time)*8/1000;
printf("%.2f\t%.2f\n", end_time[j], th);
}
printf("%.2f\t%.2f\n", end_time[i-1], 0);
}
14
Packet loss (awk codes)
BEGIN {
fsDrops = 0;
numFs = 0;
}
{
action = $1;
time = $2;
from = $3;
to = $4;
type = $5;
pktsize = $6;
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);
}
15
Some basic ns2 structuring


In …/ns-2.34/tcl/lib:
ns-lib.tcl:


ns-default.tcl:


The actial simulator class, most of its member function definitions are
here
default values for configurable parameters for various network
components, configure the parameter in otcl which actually take effect
through C++…
ns-packet.tcl:

Packet header initialization & implementation, where you register your
own packet type
16
Adding your own modules...



You will develop NS2 modules in one the following file types:
File type
Example
C++ code
myNewMod.cc
Header definitions
myNewMod.h
Tcl code
myNewMod.tcl
What you would like to do is to incorporate these files into
NS2
…how?
17
Working assumption...
Assume that we wanted to build a multimedia application that
runs over a UDP connection.
Lets say that this application implements a "five rate media
scaling" which can respond to network congestion to some
extent by changing encoding and transmission policy pairs
associated with scale parameter values
18
Adding modules Step by step...
STEP 1: Register the new application header, by modifying the files:
…/common/packet.h and …/tcl/lib/ns-packet.tcl
1. In packet.h
A. Look for:
// insert new packet types here
Static packet_t PT_NTYPE = 62; // This MUST be the LAST one
And insert new packet types between those 2 lines, eg:
static const packet_t PT_Multimedia = 70;
B. Look for:
class p_info {
In  public:
In  static void initName() in the long list of name_ add:
name_[PT_Multimedia] = "Multimedia";
2. In ns-packet.tcl
- Look for:
foreach prot {
And in the list of protocols add: Multimedia
19
Adding modules Step by step...
STEP 2: Add new methods to the Agent and Application classes, by modifying the
files: …/common/agent.h and …/apps/app.h
1. In agent.h
-look for:
class Agent : public Connector {
public:
Agent(packet_t pktType);
And insert new agent methods as e.g.:
virtual int supportMM() {return 0;}
virtual void enableMM() {}
2. In app.h
-look for:
class Application : public Process {
public:
Application();
And in that list also insert your application methods defintions as e.g.:
virtual void recv_msg(int nbytes, const char *msg = 0){}
20
Adding modules Step by step...
STEP 3: Set defaults for the new configurable parameters,
in /tcl/lib/ns-default.tcl add
......
Application/MmApp
Application/MmApp
Application/MmApp
Application/MmApp
Application/MmApp
Application/MmApp
Application/MmApp
......
set
set
set
set
set
set
set
rate0_ 0.3mb
rate1_ 0.6mb
rate2_ 0.9mb
rate3_ 1.2mb
rate4_ 1.5mb
pktsize_ 1000
random_ false
21
Adding modules Step by step...
STEP 4: Modify the Makefile
Assuming that you have all three .h, .cc and .tcl file types you must make the following
modifications (if not, reduce steps accordingly).
Note: the Makefile lies in the …/ns-2.23 directory
Lets assume your files are in …/ns-2.34/myWork
You will have to modify three places in the Makefile
1. OBJ_CC
2. NS_TCL_LIB
3. INCLUDE
As follows:

1. Look for
File type
Example
C++ code
myNewMod.cc
Header definitions
myNewMod.h
Tcl code
myNewMod.tcl
OBJ_CC = \
And add:
myDir/myNewMod.o \
To one line below
22
Adding modules Step by step...
File type
Example
C++ code
myNewMod.cc
Header definitions
myNewMod.h
Tcl code
myNewMod.tcl
2. Look for
NS_TCL_LIB = \
And add:
myDir/myfile.tcl \
To one line below
3. (OPTIONAL) Look for
INCLUDES = \
And add:
-I. /myWorks \
To one line below
23
Adding modules Step by step...
STEP 6:
Recompile the software
./make
Note: These will be key instruction steps for Lab Assignment 2
24
Download