ns - NCNU Moodle 課程

Download Report

Transcript ns - NCNU Moodle 課程

2.1 Introduction
• Network Simulator (Version 2)
– widely known as NS2
– Event driven simulation tool
– wired / wireless network, protocols (e.g., routing
algorithms, TCP, UDP)
– birth in 1989.
• REAL network simulator (University of California and Cornell
University)
• Supported by Defense Advanced Research Projects Agency
(DARPA) in 1995.
• Currently, National Science Foundation (NSF) has joined
2.2 Basic Architecture
• An executable command ns
– Input: tcl simulation scripting file
– Output: trace file  Animation by NAM or plotting
graph by Xgraph (gunplot)
• NS2 consists of two key languages
– C++
• defines the internal mechanism of the simulation objects,
– Object-oriented Tool Command Language (OTcl).
• sets up simulation by assembling and configuring the objects
as well as scheduling discrete events
• The C++ and the OTcl are linked together using
TclCL.
– Mapped to a C++ object, variables in the OTcl domains
are sometimes referred to as handles.
• a handle (e.g., n as a Node handle) is just a string (e.g., _o10)
in the OTcl domain, and does not contain any functionality.
• functionality (e.g., receiving a packet) is defined in the mapped
C++ object (e.g., of class Connector).
2.4 Directories and Convention
• 2.4.1 Directories
“Ns” Components
• Ns, the simulator itself
• Nam, the network animator
– Visualize ns (or other) output
– Nam editor: GUI interface to generate ns scripts
• Pre-processing:
– Traffic and topology generators
• Post-processing:
– Simple trace analysis, often in Awk, Perl, or Tcl
5
Ns Models
• Traffic models and applications:
– Web, FTP, telnet, constant-bit rate, real audio
• Transport protocols:
– unicast: TCP (Reno, Vegas, etc.), UDP
– Multicast: SRM
• Routing and queueing:
– Wired routing, ad hoc rtg and directed diffusion
– queueing protocols: RED, drop-tail, etc
• Physical media:
– Wired (point-to-point, LANs), wireless (multiple
propagation models), satellite
6
Ns-2, the Network Simulator
• A discrete event simulator
– Simple model
• Focused on modeling network protocols
–
–
–
–
–
Wired, wireless, satellite
TCP, UDP, multicast, unicast
Web, telnet, ftp
Ad hoc routing, sensor networks
Infrastructure: stats, tracing, error models, etc
7
Discrete Event Simulation
• Model world as events
– Simulator has list of events
– Process: take next one, run it, until done
– Each event happens in an instant of virtual (simulated)
time, but takes an arbitrary amount of real time
• Ns uses simple model: single thread of control
=> no locking or race conditions to worry about
(very easy)
8
Discrete Event Examples
Consider two nodes
on an Ethernet:
A
B
simple
queuing
model:
t=1, A enqueues pkt on LAN
t=1.01, LAN dequeues pkt
and triggers B
t=1.0: A sends pkt to NIC
A’s NIC starts carrier sense
detailed
t=1.005: A’s NIC concludes cs,
CSMA/CD
starts tx
model:
t=1.006: B’s NIC begins reciving pkt
t=1.01: B’s NIC concludes pkt
B’s NIC passes pkt to app
9
Ns Architecture
• Object-oriented (C++, OTcl)
• Modular approach
– Fine-grained object composition
+
+
–
–
Reusability
Maintenance
Performance (speed and memory)
Careful planning of modularity
10
C++ and OTcl Separation
• “data” / control separation
– C++ for “data”:
• per packet processing, core of ns
• fast to run, detailed, complete control
– OTcl for control:
•
•
•
•
Simulation scenario configurations
Periodic or triggered action
Manipulating existing C++ objects
fast to write and change
+ running vs. writing speed
– Learning and debugging (two languages)
11
Otcl and C++: The Duality
C++
C++/OTcl
split objects
•
•
•
•
otcl
OTcl (object variant of Tcl) and C++ share class hierarchy
TclCL is glue library that makes it easy to share functions,
variables, etc
OTcl to call C++ : command(), tcl.result()
C++ to call OTcl: tcl.eval()
12
Basic Tcl
variables:
set x 10
puts “x is $x”
functions and expressions:
set y [pow x 2]
set y [expr x*x]
control flow:
if {$x > 0} { return $x } else {
return [expr -$x] }
while { $x > 0 } {
puts $x
incr x –1
}
procedures:
proc pow {x n} {
if {$n == 1} { return $x }
set part [pow x [expr $n-1]]
return [expr $x*$part]
}
Also lists, associative arrays, etc.
=> can use a real programming
language to build network
topologies, traffic models, etc.
13
Basic otcl
Class Person
# constructor:
Person instproc init {age} {
$self instvar age_
set age_ $age
}
# method:
Person instproc greet {} {
$self instvar age_
puts “$age_ years old: How are
you doing?”
}
# subclass:
Class Kid -superclass Person
Kid instproc greet {} {
$self instvar age_
puts “$age_ years old kid:
What’s up, dude?”
}
set a [new Person 45]
set b [new Kid 15]
$a greet
$b greet
=> can easily make variations of existing things (TCP, TCP/Reno)14
C++ and OTcl Linkage
• Class Tcl: instance of OTcl interpreter
Tcl& tcl = Tcl::instance();
tcl.evalc(“puts stdout hello world”);
tcl.result() and tcl.error
• Class TclObject and TclClass
– Variable bindings ( set parameters to C++ module)
bind(“rtt_”, &t_rtt_)
– Invoking command method in shadow class
$tcp advanceby 10
15
C++ and Otcl linkage II
• Some important objects:
– NsObject: has recv() method
– Connector: has target() and drop()
– BiConnector: uptarget() & downtarget()
16
NS-2 Overview
• TCP/IP、NS2 和OSI 7-Layer 的大致對應關係
Application
Application
Application
Presentation
Session
TCP/UDP
Transport
Agent
IP
Network
Access
2017/4/4
Network
Node
Link
Wireless Network Lab.,CYUT
Data Link
Physical
17
17
Using ns
Problem
Result
analysis
Simulation
model
Modify
ns
Setup/run
simulation
with ns
18
Ns programming
•
•
•
•
•
•
•
•
Create the event scheduler
Turn on tracing
Create network
Setup routing
Insert errors
Create transport connection
Create traffic
Transmit application-level data
19
Creating Event Scheduler
• Create event scheduler
set ns [new Simulator]
• Schedule events
$ns at <time> <event>
• <event>: any legitimate ns/tcl commands
$ns at 5.0 “finish”
• Start scheduler
$ns run
20
Event Scheduler
• Event: at-event and packet
• List scheduler: default
– Heap and calendar queue scheduler
• Real-time scheduler
– Synchronize with real-time
– Network emulation
set ns_ [new Simulator]
$ns_ use-scheduler Heap
$ns_ at 300.5 “$self halt”
21
Discrete Event Scheduler
class Event {
public:
Event* next_; /* event list */
Handler* handler_; /* handler to call when event ready */
double time_; /* time at which event is ready */
int uid_; /* unique ID */
Event() : time_(0), uid_(0) {}
};
22
Packet forwarding
• packets are handed from one network object
to another using
– send(Packet* p) {target_->recv(p)}; method of
the sender and recv(Packet*, Handler* h = 0)
method of the receiver.
23
Simple Simulation Example
• This network consists of 4 nodes (n0, n1, n2, n3)
• The duplex links between n0 and n2, and n1 and n2 have
• The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and
20 ms of delay 2 Mbps of bandwidth and 10 ms of delay.
• Each node uses a DropTail queue,
– of which the maximum size is 10
• ftp session is based on tcp
– tcp agent: generate packet (l=1k bytes)
– sink: generate ack and free packets
• cbr session is based on udp
– udp agent: generate packet (l=1k bytes)
@ 1 Mbps
– null: free packets
# means remarks
New a simulator object with name “ns”
Ask “ns” object to set parameters color1 and color2
Open file “out.nam” and set the handler to “nf” object
Ask “ns” object to record all trace data to “nf”
“finish” procedure will be called by its schedule
Set “nf” to global such that this procedure can access
Ask “ns” object to flush data
Close file object “nf”
Run “nam” program automatically
Terminate
Ask “ns” to create 4 nodes n0 to n3
•
•
#Create a simulator object
set ns [new Simulator]
•
•
•
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
•
•
•
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
•
•
•
•
•
•
•
•
•
•
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#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]
Ask “ns” object to create three duplex links with proper
parameters
•
•
•
•
#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
Ask “ns” object to set parameter for link $n2 to $n3
•
•
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
•
•
•
•
#Give node 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
•
•
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
•
•
•
•
•
•
•
#Setup 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
Create tcp agent object with name “tcp”
Ask “tcp” to set class to 2
Ask “ns” to attach “tcp” tonode “n0”
Create tcpsink agent object with name “sink”
Ask “ns” to attach “sink” tonode “n3”
Ask “ns” to create a tcp connection by connecting
Create a ftp session with name “ftp”
Ask “ftp” to attach to “tcp” connection
Ask “ftp” to set type to “FTP”
Create CBR session in the similar way
Schedule the event
•
•
•
•
#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 $n3 $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_ 1mb
$cbr set random_ false
•
•
•
•
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
•
Ask “ns” object to de-attach tcp connection at time 4.5
•
•
#Detach tcp and sink agents (not really
necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns
detach-agent $n3 $sink"
•
#Call the finish procedure after 5 seconds of
simulation time
$ns at 5.0 "finish"
Print some information
•
•
•
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
Start simulation
•
•
#Run the simulation
$ns run
Schedule the event to call “finish” procedure at 5.0 sec
Trace Analysis Example
#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
}
Format of trace file
• Open out.tr
Performance measure
• Use modified version of UDP/TCP
• Record sequence no., time, and size in a file
• For UDP
– Packet delay (received time – send time)
– Jitter (= delay(j)-delay(i) / (j-i))
– Throughput = total size / total time
• For TCP
– Only throughput is considered
31
wired-measure.tcl
# mTcpSink是TCPsink的延申,除了具有TCPSink的功能外,也能記錄所送出封包資訊
set sink [new Agent/TCPSink/mTcpSink]
#設定tcp接收記錄檔的檔名為tcp_sink
$sink set_filename tcp_sink
#建立一條mUDP的連線
#mUDP是UDP的延申,除了具有UDP的功能外,也能記錄所送出封包資訊
set udp [new Agent/mUDP]
#設定傳送記錄檔檔名為sd_udp
$udp set_filename sd_udp
$ns attach-agent $s2 $udp
#新增的接收Agent,可以把接收封包資訊記錄到檔案中
set null [new Agent/mUdpSink]
#設定接收檔記錄檔檔名為rd_udp
$null set_filename rd_udp
32
wired-measure.tcl
• Output 3 files: tcp_sink, sd_udp, rd_udp
• CBR:
– Packet loss rate = 542/550 = 1.45 %
• Check the number of packet in log files
– Packet delay
• rd_udp file format:
0
0.100000
0.138706
0.038706
• seq
send time rev time
delay
• $awk ‘{print $1, $4}’ rd_udp > cbr_delay
• Plot the result by gnuplot or excel
1000
size
33
34
AWK
• a programming language that is designed for
processing text-based data
• created at Bell Labs in the 1970s
• The print command is used to output text.
• $# : data in #th field of current line
– $2 means the data in the 2nd field
35
awk program (measure-jitter.awk)
BEGIN{
last_pkt_id = -1;
last_e2e_delay = -1;
}
{
Run: awk –f measure-jitter.awk rd_udp > cbr_jitter
pkt_id = $1;
send_time = $2;
rcv_time = $3;
e2e_delay = $4;
pkt_size = $5;
if( last_pkt_id !=-1) {
jitter = (e2e_delay - last_e2e_delay) / (pkt_id - last_pkt_id);
printf("%f %f\n", send_time, jitter);
}
last_pkt_id = pkt_id;
last_e2e_delay = e2e_delay;
}
{
}
36
37
Perl
• Perl is a high-level, general-purpose,
interpreted, dynamic programming language.
• originally developed by Larry Wall, a linguist
working as a systems administrator for NASA,
in 1987,
• powerful text processing facilities
38
#計算單位時間內累積的封包大小
$sum=$sum+$x[4];
measure-throughput.pl
#計算累積的總封包大小
$sum_total=$sum_total+$x[4];
}
else
{
#計算吞吐量
$throughput=$sum*8.0/$granularity;
#使用方法: perl measure-throughput.pl <trace file> <granlarity>
#記錄檔檔名
$infile=$ARGV[0];
if ($throughput > $maxrate){
$maxrate=$throughput;
}
#多少時間計算一次(單位為秒)
$granularity=$ARGV[1];
#輸出結果: 時間 吞吐量(bps)
print STDOUT "$x[2]: $throughput bps\n";
$sum=0;
$sum_total=0;
$clock=0;
$maxrate=0;
$init=0;
#打開記錄檔
open (DATA,"<$infile")
|| die "Can't open $infile $!";
#讀取記錄檔中的每行資料,資料是以空白分成眾多欄位
while (<DATA>) {
@x = split(' ');
if($init==0){
$start=$x[2];
$init=1;
}
#讀取的第零個欄位是pkt_id
#讀取的第一個欄位是封包傳送時間
#讀取的第二個欄位是封包接收時間
#讀取的第三個欄位是封包end to end delay
#讀取的第四個欄位是封包大小
#設定下次要計算吞吐量的時間
$clock=$clock+$granularity;
$sum_total=$sum_total+$x[4];
$sum=$x[4];
}
}
$endtime=$x[2];
#計算最後一次的吞吐量大小
$throughput=$sum*8.0/$granularity;
print STDOUT "$x[2]: $throughput bps\n";
$clock=$clock+$granularity;
$sum=0;
#print STDOUT "$sum_total $start $endtime\n";
$avgrate=$sum_total*8.0/($endtime-$start);
print STDOUT "Average rate: $avgrate bps\n";
print STDOUT "Peak rate: $maxrate bps\n";
#關閉檔案
close DATA;
exit(0);
39
ywkuo@office ~/tcl
$ perl measure-throughput.pl rd_udp 0.5
0.506706: 736000 bps
1.002706: 992000 bps
1.507553: 848000 bps
2.001294: 992000 bps
2.505294: 1040000 bps
3.003553: 960000 bps
3.501906: 1040000 bps
4.000259: 928000 bps
4.506706: 1072000 bps
4.530706: 64000 bps
Average rate: 987249.544626594 bps
Peak rate: 1072000 bps
ywkuo@office ~/tcl
40
ywkuo@office ~/tcl
$ perl measure-TCP.pl tcp_sink 0.1
1.034894 0
1.104296 3200
1.113896 83200
1.175600 83200
1.180494 83200
1.190094 83200
1.199694 83200
1.247600 83200
1.252494 83200
1.262094 83200
1.266988 83200
1.276588 83200
1.329106 332800
1.401576 832000
1.502847 1081600
1.607600 249600
1.751600 499200
1.823600 499200
1.900776 832000
2.006188 832000
2.102000 748800
2.207224 748800
2.327600 416000
2.404494 249600
2.500588 582400
2.615600 582400
2.711412 748800
2.807224 748800
2.903035 748800
3.008447 832000
3.113859 832000
3.200071 665600
3.343600 249600
3.415600 332800
3.506800 582400
3.602894 748800
3.717718 832000
3.813529 748800
3.904635 748800
4.005153 748800
4.072353 665600
244440 1.034894 4.072353
Average rate: 643801.282585214 bps
TCP result
41
HW
• Run this example
• Run again with different delay for the link n0
to n1
– $ns duplex-link $n0 $n2 2Mb 10ms DropTail
• Compare the two experiments. (Write
anything you find in your report).
42
Network Topology: Node
n0
Port
Classifier
Addr
Classifier
Node entry
0
entry_
dmux_
n1
Unicast
Node
Multicast
Node classifier_
Node entry
entry_
classifier_
dmux_
0
1
Multicast
Classifier
multiclassifier_
set n0 [ns_ node]
Set ns_ [new Simulator –multicast on]
43
Set n1 [ns_ node]
Node Addressing
• Two basic address styles available: flat and
hierarchical
• Default flat address: 32 bits each for node
and port id
• Default hier address:
– 3 levels of hierarchy
– 10 11 11 or 1 9 11 11 if mcast specified
• Different bit allocation possible for specific
hier addresses
44
Hierarchical Node
n2
Address
classifier
Node
entry
To Port
demux
Level 3
Level 1
Level 2
ns_ node-config –addressing hier
45
Network Topology: Link
n0
n1
duplex link
head_
enqT_
tracing
queue_
drophead_
deqT_
link_
drpT_
[ns_ duplex-link $n0 $n1 5Mb 2ms drop-tail]
ttl_
n1
entry_
simplex link
46
Transport
n0
n1
Port
Classifier
Port
Classifier
dst_=1.0
Addr
Classifier
0
0
dmux_
entry_
Agent/TCP
agents_
Addr
Classifier
0
1
dmux_
dst_=0.0
Agent/TCPSink
agents_
entry_
classifier_
classifier_
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
ns_ attach-agent $n0 $tcp
ns_ attach-agent $n1 $tcpsink
47
ns_ connect $tcp $tcpsink
Application: Traffic Generator
n0
n1
Application/FTP
dst_=1.0
Port
Classifier
Addr
Classifier
0
0
Port
Classifier
Addr
Classifier
Agent/TCP
agents_
1
dmux_
entry_
0
dst_=0.0
Agent/TCPSink
agents_
dmux_
entry_
classifier_
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 “$ftp start”
classifier_
48
Routing
n0
n1
Application/FTP
Port
Classifier
Addr
Classifier
Node entry
entry_
0
1
classifier_
0
dmux_
Agent/TCP
agents_
head_
enqT_
queue_
drophead_
deqT_
link_
ttl_
n1
entry
_
drpT_
49
Routing (con’t)
n0
n1
Application/FTP
Port
Classifier
Addr
Classifier
entry_
0
1
Port
Classifier
agents_
dmux_
Agent/TCP
Addr
Classifier
Agent/TCP
Link n0-n1
entry_
classifier_
1
0
dmux_
classifier_
Link n1-n0
50
Plumbing: Packet Flow
n0
n1
Application/FTP
dst_=1.0
Port
Classifier
Addr
Classifier
entry_
0
1
0
Port
Classifier
Addr
Classifier
Agent/TCP
Link n0-n1
entry_
0
dst_=0.0
Agent/TCPSink
1
0
Link n1-n0
51