Network Simulator (Version 2)
Network Simulator (Version 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
+ Reusability
+ Maintenance
Performance (speed and memory)
Careful planning of modularity
10
C++ and OTcl Separation
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: procedures:
set x 10
proc pow {x n} {
puts x is $x
if {$n == 1} { return $x }
functions and expressions: set part [pow x [expr $n-1]]
set y [pow x 2] return [expr $x*$part]
set y [expr x*x] }
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
Session
TCP/UDP Transport
Agent
IP Network
Data Link
Network Node
Access Link
Physical
17
02/22/17 Wireless Network Lab.,CYUT 17
Using ns
Problem
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
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 #Create a simulator object
New a simulator object with name ns set ns [new Simulator]
# 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 1000
seq send time rev time delay size
$awk {print $1, $4} rd_udp > cbr_delay
Plot the result by gnuplot or excel
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;
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
if ($x[2]-$clock <= $granularity)
{
#
$sum=$sum+$x[4];
measure-throughput.pl }
else
#
$sum_total=$sum_total+$x[4];
{
#
# : perl measure-throughput.pl <trace file> <granlarity>
$throughput=$sum*8.0/$granularity;
#
$infile=$ARGV[0];
if ($throughput > $maxrate){
$maxrate=$throughput;
# ( )
}
$granularity=$ARGV[1];
# : (bps)
$sum=0;
print STDOUT "$x[2]: $throughput bps\n";
$sum_total=0;
$clock=0;
#
$maxrate=0;
$clock=$clock+$granularity;
$init=0;
$sum_total=$sum_total+$x[4];
#
$sum=$x[4];
open (DATA,"<$infile")
}
|| die "Can't open $infile $!";
}
# ,
$endtime=$x[2];
while (<DATA>) {
@x = split(' ');
#
$throughput=$sum*8.0/$granularity;
if($init==0){
print STDOUT "$x[2]: $throughput bps\n";
$start=$x[2];
$clock=$clock+$granularity;
$init=1;
$sum=0;
}
#print STDOUT "$sum_total $start $endtime\n";
$avgrate=$sum_total*8.0/($endtime-$start);
# pkt_id
print STDOUT "Average rate: $avgrate bps\n";
#
print STDOUT "Peak rate: $maxrate bps\n";
#
# end to end delay
39
#
#
close DATA;
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
TCP result
$ 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 41
Average rate: 643801.282585214 bps
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 n1
Port
Classifier Unicas Multica dmux_
multiclassifier_
n2
Address
classifier To Port
demux
Node
Level 3
entry
Level 2
Level 1
45
ns_ node-config addressing hier
Network Topology: Link
n0 n1
duplex link
n1
head_ entry
enqT_ queue_ deqT_ link_ ttl_ _
drophead_ drpT_
tracing simplex link
46
[ns_ duplex-link $n0 $n1 5Mb 2ms drop-tail]
Transport
n0 n1
Port Port
Classifier dst_=1.0 Classifier dst_=0.0
Addr Agent/TCP Addr Agent/TCPSin
0 0
Classifier agents_ Classifier k
agents_
0 dmux_ 1 dmux_
entry_ entry_
classifier_ classifier_
n0 n1
n0 n1
Port Application/FTP
Classifier
Addr Agent/TCP
0
Classifier agents_
Node entry n1
0 dmux_ head_ entry
enqT_ queue_ deqT_ link_ ttl_ _
entry_ 1
classifier_ drophead_ drpT_
49
Routing (cont)
n0 n1
Link n1-n0
50
Plumbing: Packet Flow
n0 n1
Link n1-n0
51