0% found this document useful (0 votes)
31 views

Network Simulator (Version 2)

The document describes the network simulator NS2, including that it is an event-driven simulation tool used to simulate wired and wireless networks and network protocols, it has a modular architecture with C++ defining the simulation objects and OTcl setting up and configuring simulations, and it works by scheduling discrete events in a simulator object and passing packets between network objects using send and receive methods.

Uploaded by

Shiraz Khurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Network Simulator (Version 2)

The document describes the network simulator NS2, including that it is an event-driven simulation tool used to simulate wired and wireless networks and network protocols, it has a modular architecture with C++ defining the simulation objects and OTcl setting up and configuring simulations, and it works by scheduling discrete events in a simulator object and passing packets between network objects using send and receive methods.

Uploaded by

Shiraz Khurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 51

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 simple t=1, A enqueues pkt on LAN


on an Ethernet: queuing t=1.01, LAN dequeues pkt
model: and triggers B

A B t=1.0: A sends pkt to NIC


As NIC starts carrier sense
detailed
t=1.005: As NIC concludes cs,
CSMA/CD
starts tx
model:
t=1.006: Bs NIC begins reciving pkt
t=1.01: Bs NIC concludes pkt
Bs 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: 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] }

control flow: Also lists, associative arrays, etc.


if {$x > 0} { return $x } else { => can use a real programming
return [expr -$x] } language to build network
while { $x > 0 } { topologies, traffic models, etc.
puts $x
incr x 1
}
13
Basic otcl
Class Person # subclass:
# constructor: Class Kid -superclass Person
Person instproc init {age} { Kid instproc greet {} {
$self instvar age_ $self instvar age_
set age_ $age puts $age_ years old kid:
} Whats up, dude?
# method: }
Person instproc greet {} {
$self instvar age_ set a [new Person 45]
puts $age_ years old: How are set b [new Kid 15]
you doing? $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

Data Link
Network Node
Access Link
Physical
17
02/22/17 Wireless Network Lab.,CYUT 17
Using ns

Problem

Result Simulation Modify


analysis model 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 #Create a simulator object
New a simulator object with name ns set ns [new Simulator]

#Define different colors for data flows (for NAM)


$ns color 1 Blue
Ask ns object to set parameters color1 and color2
$ns color 2 Red

#Open the NAM trace file


set nf [open out.nam w]
Open file out.nam and set the handler to nf object $ns namtrace-all $nf
Ask ns object to record all trace data to nf
#Define a 'finish' procedure
proc finish {} {
finish procedure will be called by its schedule global ns nf
Set nf to global such that this procedure can access $ns flush-trace
#Close the NAM trace file
Ask ns object to flush data
close $nf
#Execute NAM on the trace file
Close file object nf exec nam out.nam &
exit 0
Run nam program automatically }
Terminate
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

Ask ns to create 4 nodes n0 to n3


#Create links between the nodes
Ask ns object to create three duplex links with proper $ns duplex-link $n0 $n2 2Mb 10ms DropTail
parameters $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
Ask ns object to set parameter for link $n2 to $n3
#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
Create tcp agent object with name tcp $ns attach-agent $n0 $tcp
Ask tcp to set class to 2 set sink [new Agent/TCPSink]
Ask ns to attach tcp tonode n0 $ns attach-agent $n3 $sink
$ns connect $tcp $sink
Create tcpsink agent object with name sink $tcp set fid_ 1
Ask ns to attach sink tonode n3
Ask ns to create a tcp connection by connecting
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
Create a ftp session with name ftp
Ask ftp to attach to tcp connection $ftp attach-agent $tcp
Ask ftp to set type to FTP $ftp set type_ FTP

#Setup a UDP connection


Create CBR session in the similar way 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"
Schedule the event $ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really
Ask ns object to de-attach tcp connection at time 4.5 necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns
detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of


Schedule the event to call finish procedure at 5.0 sec 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
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 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;

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
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_

Addr t Node st Nodeclassifier_


Classifier
Node entry Node entry
0 dmux_ 0
entry_ entry_ 1 Multicast
classifier_ 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 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_

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

Port Application/FTP Port


Classifier dst_=1.0 Classifier dst_=0.0
Addr Agent/TCP Addr Agent/TCPSink
0 0
Classifier agents_ Classifier agents_
0 dmux_ 1 dmux_
entry_ entry_
classifier_ classifier_

set ftp [new Application/FTP]


$ftp attach-agent $tcp
$ns at 1.2 $ftp start 48
Routing

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

Port Application/FTP Port


Classifier Classifier
Addr Agent/TCP Addr Agent/TCP
Classifier agents_ Classifier
0 dmux_ 1 dmux_
Link n0-n1
entry_ 1 entry_ 0
classifier_ classifier_

Link n1-n0

50
Plumbing: Packet Flow

n0 n1

Port Application/FTP Port


Classifier dst_=1.0 Classifier dst_=0.0
Addr Agent/TCP Addr Agent/TCPSink
Classifier 0 Classifier 0
0 1
Link n0-n1
entry_ 1 entry_ 0

Link n1-n0

51

You might also like