0% found this document useful (0 votes)
29 views69 pages

Chapter - 7 Network Simulator

Network Simulator 2 (NS-2) is a discrete event simulator for networking research. It models networks at the packet level and includes implementations of many networking protocols. NS-2 uses C++ for efficient modeling of low-level protocol functions and OTcl as a front-end for configuration of simulations and scheduling events. It supports simulation of both wired and wireless networks and provides tools for visualization, analysis, and debugging of simulation results.

Uploaded by

Etemu kifle
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)
29 views69 pages

Chapter - 7 Network Simulator

Network Simulator 2 (NS-2) is a discrete event simulator for networking research. It models networks at the packet level and includes implementations of many networking protocols. NS-2 uses C++ for efficient modeling of low-level protocol functions and OTcl as a front-end for configuration of simulations and scheduling events. It supports simulation of both wired and wireless networks and provides tools for visualization, analysis, and debugging of simulation results.

Uploaded by

Etemu kifle
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/ 69

Network Simulator (ns-2)

1
Documentation
 Introductory: Marc Greis’s tutorial
 reference: Ns Manual (formerly called "ns Notes
and Documentation")
 ns by Example
 Practical Programming in Tcl and Tk
 (http: //www.beedub.com/book/)
 http://hpds.ee.ncku.edu.tw/~smallko/
ns2/ns2.htm

2
Useful links

ns-2 web page


http://www.isi.edu/nsnam/ns/

nam web page


http://www.isi.edu/nsnam/nam/
ns-2 tutorial [ns-2.29/tutorial/]
http://www.isi.edu/nsnam/ns/tutorial/index.html

ns-2 manual
http://www.isi.edu/nsnam/ns/ns-documentation.html

3
Introduction
 Methods for network research
 Analytical
•Mathematical model
 Emulation
•Real code
•Duplicates the functions of one system with a different system
 Simulation
•Abstract model
•Behavior of system

4
Introduction…
 Simulation frameworks
 Commercial
•OPNET
•QualNet
•OMNEST (commercial version of OMNetT++)
 Free
•NS-2 (network simulator version 2)
•OMNetT++

5
Installation
 Platform
 Unix
 Windows (cygwin)
 Packages
 Tcl/tk
 Otcl tclcl
 Ns-2
 Nam
 Xgraph
 C++ compiler
 Ns AllInOne package

6
Installation…
 Cygwin, AllInOne installation (windows)
http://140.116.72.80/~smallko/ns2/setup_en.htm
 Installation problems
http://www.isis.edu/nsnam/ns/ns-problems.html#general
 Cygwin setup
 Gcc and g++
 Ns-2 setup
 Path setting
 Cygwin/home/user-name/.bashrc
 Testing (startxwin.bat) - ~/ns-allinone-x.x/ns-x.x/ns-tutorial/examples

7
Platforms supported
 Most UNIX and UNIX-like systems
 FreeBSD
 Linux
 Solaris

 Windows 98/2000/2003/XP/7/10
 Cygwin required

8
NS-goals
 Support networking research and education
 Protocols design and comparison
 Provide collaborative environment
 Free distributed, open source
[email protected]

9
Preliminary for NS-2
 Ability to write correct programs
 Familiarity with object-oriented programming
 Patience to debug NS source code when needed
 Simple usage will not need NS source code debugging
 More complex simulations may need modification to
NS source code

 Debugging skills
 NS uses C++ and Otcl
 User scripts are in Otcl

10
Network simulator (ns-2)
 Ns-2 is a discrete event simulator
 Change of state happens only in discrete point of time
 Advance of time depends on the timing of events
 Events
 Basic component that changes a system state
 Packet arrival/departure, time-out events
 Event handlers
 Process events
 Agents, Nodes, Links in NS-2 are event handlers.
 Object-oriented simulator
 C++ : fast to run, slower to change – protocol implementation
 Otcl : slower to run, fast to change – simulation configuration
11
Basic Mechanisms of Event Handlers
 If there exists an event to process
 Call process (event)
 During the processing, the event is held for a certain
duration.
 Agents in NS-2 does this.

12
Discrete event simulator
 Ns-2 is a discrete event driven simulator
 Physical activities are translated to events
 Events are queued and processed in the order of their
scheduled occurrences
 Time progresses as the events are processed

Time: 1.5 Time: 1.7


sec sec
1 2

Time: 2.0 Time: 1.8


sec sec

13
Simulation Network using ns-2
 Wired Network
 Point-to-point link, LAN
 Routing: Distance Vector (Bellman-ford), Link State (Dijikistra)
 Transportation: TCP and UDP
 Queuing disciplines: drop-tail, RED, FQ, SFQ, DRR, RR
 Traffic characterizations - Poisson, Exponential, Pareto etc.
 QoS: IntServ and DiffServ
 Different mobility model
 Wireless Network
 Ad-hoc routing and mobile IP: AODV, DSR,…
 Sensor-MAC, WiMAX , Wi-Fi…
 Satellite network
 Power control in wireless networks
 Tracing, Visualization, Analysis, Other utilities
14
Simulation Network using ns-2…

 Model the network at packet level

 Modeling Network protocols


Collection of Various protocols at multiple layers
TCP(reno, tahoe, vegas, sack, westhood,…)
MAC(802.11, 802.3, TDMA)
Ad-hoc Routing (DSDV, DSR, AODV,AOMDV, TORA,…)
Sensor Network
Multicast protocols, Satellite protocols, and many others
15
What you can do using NS-2
 You can modify NS-2:
 to implement your own versions of the above
protocols or
 even code totally new protocols
 Measurement of Statistics:
 Throughput, Delay, Jitter, routing overhead, etc.
 Queue Monitoring, Drops at Queues.
 Literally all that you will need to know with your
simulations.
 Graphic visualization - using “NAM” (Network
Animator)
16
NS-2 : Components
 NS – The simulator itself
 NAM – Network AniMator
 Visual demonstration of NS output
 Nam editor: GUI interface to generate ns scripts
 Preprocessing
 Handwritten TCL or
 Traffic and topology generator
 Post analysis
 Trace analysis using Perl/TCL/AWK/MATLAB

17
NS-2 : Components…
 Pre-processing
•Traffic and topology model
 Post-processing
•Trace analysis, often in awk, perl, or tcl
 Simulation procedure
Define Simulation
problem model

Extend
simulator

Post-process Execute
results simulation

18
User’s view of NS-2

19
Architectural View of NS-2

Event ns-2
Scheduler

Component
tclcl

Network
otcl
tcl8.0
 otcl: Object-oriented support
 tclcl: C++ and otcl linkage
 Discrete event scheduler
 Data network components
20
NS-2 : C++ / OTCL
 NS-2 Code contains two sets of languages,
namely C++ and OTcl.

 C++ is used for the creation of objects


because of speed and efficiency.

 OTcl is used as a front-end to setup the


simulator, configure objects, and schedule
events because of its ease of use.

21
Why two languages? (Tcl & C++)
 C++: Detailed protocol simulations require
systems programming language
 byte manipulation, packet processing, algorithm
implementation
 Fast to run, detailed, slow to modify
 Turn around time (run simulation, find bug, fix bug,
recompile, re-run) is slower
 Tcl: Simulation of slightly varying parameters or
configurations
 quickly exploring a number of scenarios
 Simulation scenario configurations
 Manipulating existing C++ objects
 fast to write and change
 iteration time (change the model and re-run) is more
important 22
Tcl or C++?
 Tcl
 Simple Configuration, Setup, Scenario
 If it’s something that can be done without
modifying existing Tcl module.
 C++
 Anything that requires processing each
packet
 Needs to change behavior of existing
module
+ running vs. writing speed
– Learning and debugging (two languages)
23
OTcl-C++ linkage

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
24
Shadowing

TclObject OTcl class C++ class TclObject()


hierarchy hierarchy

Agent Agent()

Agent/DSDV DSDVAgent()

Agent/DSDV OTcl Agent/DSDV C++


shadow object object
25
NS-2 Environment
Simulation 1 2
Scenario

set ns_ [new Simulator]


Tcl Script set node_(0) [$ns_ node]
set node_(1) [$ns_ node]

class MobileNode : public Node


{
C++ friend class PositionHandler;
public:
Implementation MobileNode();


}
26
NS-2 Directory Structure

sim

tcl8.0 tk8.0 otcl Tcl ns-2 nam-1


tcl code C++ code
tcl ...
example
ex test lib ...
validation test tcl code core

27
NS Models
 Traffic models and applications:
 Web, FTP, telnet, constant-bit rate (CBR), real audio
 Transport protocols:
 unicast: TCP (Reno, Vegas, etc.), UDP
 Multicast:
 Routing and queuing:
 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
28
Design your simulation
 Goal and expected results
 Network topology
 Node
 Link
 Specify Agents
 Protocol
 Traffic
 Simulation Scenario

29
Network Topology - Node

n0 n1

Port Unicast Multicast


Classifier dmux_
Node Node
Addr classifier_
Classifier
Node entry Node entry
dmux_
entry_ entry_ Multicast
classifier_ Classifier

multiclassifier_

30
Network Topology - Link

n0 n1

n1
head_ entry_
enqT_ queue_ deqT_ link_ ttl_

drophead_ drpT_

31
Network Topology - Routing

n0 n1

Port
Classifier
Addr
Classifier
Node entry n1
0 dmux_ head_ entry
enqT_ queue_ deqT_ link_ ttl_ _
entry_ 1
classifier_ drophead_ drpT_

32
Network Topology – Routing…

n0 n1

Port Port
Classifier Classifier
Addr Addr
Classifier Classifier
0 dmux_ 1 dmux_
Link n0-n1
entry_ 1 entry_ 0
classifier_ classifier_

Link n1-n0

33
Network Topology - Transport

n0 n1

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

Link n1-n0

34
Network Topology - Application

n0 n1

Port Application/FTP Port


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

Link n1-n0

35
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

36
NS programming
 Create the event scheduler
 Turn on tracing
 Create network
 Setup routing
 Insert errors
 Create transport connection
 Create traffic
 Transmit application-level data

37
TCL script
 TCL script for scenario setup
 Scenario script format
 Simulator object
 Trace file
 Finish procedure
 Network setup (node, link, agent, parameter…)
 Other procedure, if any
 Event scheduling (run simulation, stop simulation …)

38
A TCL script example

n0 n1

Network Topology Traffic Generation


set ns [new Simulator] set ftp [new Application/FTP]
set n0 [$ns node] $ftp attach-agent $tcp
set n1 [$ns node] $ns at 0.2 "$ftp start"
$ns at 1.2 "exit"
$ns duplex-link $n0 $n1 1.5Mb
$ns run
10ms DropTail
Transport Protocol
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink[new Agent/TCPSink]
$ns attach-agent $n1 $sink 39
A Simulation Example

40
TCL Script Step 1

#Create a simulator object # has denotes a one line


comment
set ns [new Simulator] ns holds the name of the
new simulation
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Open the general trace file


set f [open out.tr w]
$ns trace-all $f

41
TCL Script Step 2

#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

42
TCL Script Step 3
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10

#Setup a TCP connection


set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink

#Setup a FTP over TCP connection


set ftp [new Application/FTP]
$ftp attach-agent $tcp
43
TCL Script Step 4
#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

#Setup a CBR over UDP connection


set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packet_size_ 1000
$cbr set rate_ 1mb

44
TCL Script Step 5

#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"
$ns at 4.5 "$cbr stop"

#Call the finish procedure after 5 seconds of simulation


time
$ns at 5.0 "finish"

45
TCL Script Step 6
#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
}
#Run the simulation
$ns run
46
Run simulation program
 Usage: ns file.tcl
 If the C++ source codes are modified, re-
compilation is required

 What ns-2 does:


 Read the tcl file
 Run the simulation program
 Create trace files
 NAM (network animator) input files
 Statistics (you may need to write post processing
scripts like awk file to find throughput, PDR…)

47
Analyze trace file
 Use some scripts like awk or Perl, to filter
the trace file

 Use Excel, MATLAB or xgraph to plot the


results

48
Trace file

49
Plot results in MATLAB, Excel

50
8
Matlab scalar 2D plots
7

50
5

40
4
30

3
20

10 2
8
6 8
4 6 1
4 1 2 3 4 5 6 7
2 2
0 0

1
50
2
40
3
30
4
20
5
10
8 6
6 8
4 6
4 7
2 2
0 0
8
51
1 2 3 4 5 6 7
Simple Simulation using NAM

52
Ns-2 for Wireless and Mobile
networks

53
Wireless and mobile network
simulation in ns-2
 Contributed from CMU’s Monarch project
(Wireless extension to ns-2)
 Various modules were added to ns-2 to simulate
node mobility and wireless networking
 Mobile Node
 Ad-hoc Routing(DSR, DSDV, TORA, AODV)
 MAC 802.11
 Radio Propagation Model
 Channel

54
Wireless and Mobile networks
Internals
 Mobile node
 Basic node that has address and port de-muxes,
routing agent etc
 Stack of network components consisting of LL,
MAC, NetIF, radio-model etc
 Wireless channel

55
Representation of A Mobile Node

port
Node classifier
protocol
agent Classifier:
Forwarding
255
Agent: Protocol
routing
addr
classifier defaulttarge agent
Entity
t_ Node Entry
LL ARP
LL LL: Link layer object
IFQ
IFQ IFQ: Interface queue
MAC

Propagation
MAC MAC: Mac object
PHY and antenna
models
MobileNode
PHY PHY: Net interface
CHANNEL
Prop/ant Radio propagation/
56
antenna models
Mobile Node Components
 Agent
 Responsible for packet generations and receptions
 Can think of it as an application and transport layers
 CBR(Constant Bit Rate), TCP, Sink, UDP, FTP, etc.
 Rtagent (DSDV, AODV …)
 Ad-hoc network routing protocols
 Configure multi-hop routes for packets
 LL (Link Layer)
 Runs data link protocols
 Fragmentation and reassembly of packet
 Runs Address Resolution Protocol(ARP) to resolve IP
address to MAC address conversions
57
Mobile Node Components…
 Classifiers
• defaulttarget_ points to routing agent object
• 255 is the port id assigned for rtagent_
 ARP
• Resolves IP address to hardware (MAC) address
• Broadcasts ARP query
 IFq (Interface Queue)
 PriQueue is implemented to give priority to routing protocol
packets
 Supports filter to remove packets destined to specific address

58
Mobile Node: Components
 MAC Layer
 802.11
•IEEE RTS/CTS/DATA/ACK for unicast
•Sends DATA directly for broadcast
 NetIF (Network Interfaces)
 Hardware interface used by mobile node to access the channel
 Simulates signal integrity, collision, tx error
 Mark each transmitted packet with transmission power,
wavelength etc.
 Radio Propagation Model
 Friss-space model – attenuation at near distance
 Two-ray ground reflection model for far distance
 Shadowing model -probabilistic
 Antenna
 Implements Omni Directional Antenna module which has
unity gain for all direction

59
Wireless Simulation in ns-2 (Mobile Node
Diagram - DSDV)
Agent
(Src/Sink)

Demux
Port
Demux
Addr
RTagent
(DSDV)

LL ARP

IFq

MAC

Radio
Propagation NetIF
Model

Channel

60
Running a simulation-Scenario
500m

TCP TCP TCPsink TCPsink


TCPsink
500m

node_(0) node_(0) node_(1) node_(1)


node_(1)

61
Setting Up Variables

# Define how a mobile node is configured


#======================================================================
# Define options
#======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ll) LL ;# Link layer type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) 2 ;# number of mobilenodes

62
Setting Up Variables…
#Instantiate simulator object

set ns_ [new Simulator]

#Setup Trace File


set tracefd [open simple.tr w]
$ns_ trace-all $tracefd

#create a topology in a 1000m x 900m area

set topo [new Topography]


$topo load_flatgrid 1000 900

#Create Object God


create-god $val(nn)

63
GOD
(General Operations Director)
 Stores smallest number of hops from one node
to another
 Optimal case to compare routing protocol
performance
 Automatically generated by scenario file
 set god [create-god <no of mnodes>]
 $god set-dist <from> <to> <#hops>

64
Configuring Mobile nodes
# Configure nodes
$ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
65
$node_($i) random-motion 0 ;# disable random motion }
Configuring nodes’ movement
#Configure Initial location of mobile nodes
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0

#Create movement of mobile nodes

# Node_(1) starts to move towards node_(0)


$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"

# Node_(1) then starts to move away from node_(0)


$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
66
Setup traffic flow
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $node_(0) $tcp
$ns attach-agent $node_(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 10.0 "$ftp start"

FTP

TCP Sink

node_(0) node_(1) 67
Set Stop Time and Start Simulation
#Set Simulation Stop Time

# Telling nodes when the simulation ends


for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
#Ending the simulation
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ;
$ns_ halt" proc stop {} { global ns_ tracefd
close $tracefd }

#Finally, Start The Simulation


puts "Starting Simulation..."
$ns_ run
68
Trace File
r 100.381997477 _1_ AGT --- 82 tcp 1060 [13a 1 0 800]
------- [0:0 1:0 32 1] [32 0] 1 0

r:receive event, 100.381997477:time stamps,


_1_:node 1, AGT:trace generated by agent,
82:event(pkt) id, tcp: tcp packet,
1060:packet size,
13a(hex):expected duration of pkt transmission (not working), 1:sender
mac id, 0:transmitter mac id,
800:pkt type IP (806 for ARP), 0:0: sender address:port#
1:0: receiver address:port#, 32: TTL
1: next hop address, [32 0]: TCP sequence #, ack #

69

You might also like