View
226
Download
0
Category
Preview:
Citation preview
7/27/2019 about Network Simulator 2
1/54
An Introduction to NS-2*
Gayatri Swamynathan
CS 276 TA
*some slides are from a presentation by Haob o Yu & Nader Salehi, USC/ISI
7/27/2019 about Network Simulator 2
2/54
NS-2 Learning Resources
TA web page for 276:
http://www.cs.ucsb.edu/~gayatri/ta/cs276.html
Installation instructions
Using related tools (nam, xgraph, etc)
NS-2 official website and documentation
Tutorials to get you started
Sample coding exercises
7/27/2019 about Network Simulator 2
3/54
Roadmap For Todays Lecture
1. ns Primer2. Extending ns
7/27/2019 about Network Simulator 2
4/54
Part I: ns Primer
7/27/2019 about Network Simulator 2
5/54
What is ns?
Object-oriented, discrete event-driven network simulator Written in C++ and OTcl
By VINT: Virtual InterNet Testbed
7/27/2019 about Network Simulator 2
6/54
ns Architecture
Separate data path and control pathimplementations.
7/27/2019 about Network Simulator 2
7/54
What is ns?
Object-oriented, discrete event-driven network simulator Written in C++ and OTcl
By VINT: Virtual InterNet Testbed
7/27/2019 about Network Simulator 2
8/54
ns Architecture
Separate data path and control pathimplementations.
7/27/2019 about Network Simulator 2
9/54
ns Architecture
7/27/2019 about Network Simulator 2
10/54
Hello World Interactive mode
bash-shell$ns
% set ns [new Simulator]
_o3
% $ns at 1 puts \Hello World!\1
% $ns at 1.5 exit
2% $ns run
Hello World!
bash-shell$
7/27/2019 about Network Simulator 2
11/54
Hello World Batch mode
simple.tcl
set ns [new Simulator]
$ns at 1 puts \Hello World!\
$ns at 1.5 exit$ns run
bash-shell$ns simple.tcl
Hello World!bash-shell$
7/27/2019 about Network Simulator 2
12/54
Basic Tcl: ex-tcl.tcl
7/27/2019 about Network Simulator 2
13/54
Basic OTcl
Class Mom
Mom instproc greet {} {
$selfinstvar age_
puts$age_ years old mom:How are you doing?
}
Class Kid -superclass Mom
Kid instproc greet {} {
$selfinstvar age_
puts$age_ years old kid:Whats up, dude?
}
set mom [new Mom]
$mom set age_ 45
set kid [new Kid]
$kid set age_ 15
$mom greet
$kid greet
7/27/2019 about Network Simulator 2
14/54
NS-2 Generic Script Structure
1. Create Simulator object2. [Turn on tracing]
3. Create topology
4. [Setup packet loss, link dynamics]5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
7/27/2019 about Network Simulator 2
15/54
Step1: Create Simulator Object
Create event scheduler
set ns [new Simulator]
7/27/2019 about Network Simulator 2
16/54
Step2: Tracing
Insert immediately after scheduler!
Trace packets on all links
set nf [open out.nam w]$ns trace-all $nf
$ns namtrace-all $nf
7/27/2019 about Network Simulator 2
17/54
Step2: Tracing
7/27/2019 about Network Simulator 2
18/54
NS-2 Generic Script Structure
1. Create Simulator object2. [Turn on tracing]
3. Create topology
4. [Setup packet loss, link dynamics]5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
7/27/2019 about Network Simulator 2
19/54
Step 3: Create network
Two nodes, One link
n1
n0
7/27/2019 about Network Simulator 2
20/54
Step 3: Create Network
Nodes set n0 [$ns node]
set n1 [$ns node]
Links and queuing
$ns duplex-link $n0 $n1 1Mb 10ms RED
$ns duplex-link $n0 $n1
: DropTail, RED, etc.
n1
n0
7/27/2019 about Network Simulator 2
21/54
Creating a larger topology
for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node]
}
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms RED
}
7/27/2019 about Network Simulator 2
22/54
NS-2 Generic Script Structure
1. Create Simulator object2. [Turn on tracing]
3. Create topology
4. [Setup packet loss, link dynamics]5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
7/27/2019 about Network Simulator 2
23/54
Step 4: Network Dynamics
Link failures
Hooks in routing module to reflect routingchanges
$ns rtmodel-at up|down $n0 $n1
For example:
$ns rtmodel-at 1.0 down $n0 $n1
$ns rtmodel-at 2.0 up $n0 $n1
7/27/2019 about Network Simulator 2
24/54
Step 5: Creating UDP connection
set udp [new Agent/UDP]set null [new Agent/Null]
$ns attach-agent $n0 $udp$ns attach-agent $n1 $null
$ns connect $udp $null
n1
n0
udp
null
7/27/2019 about Network Simulator 2
25/54
Step 6: Creating Traffic
(On Top of UDP)
CBR
set cbr [newApplication/Traffic/CBR]
$cbr set packetSize_ 500
$cbr set interval_ 0.005
$cbr attach-agent $udp
n1
n0
udp
cbr
null
7/27/2019 about Network Simulator 2
26/54
Creating TCP connection
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp$ns attach-agent $n1 $tcpsink
$ns connect $tcp $tcpsink
n1
n0
tcp
sink
7/27/2019 about Network Simulator 2
27/54
Step 6: Creating Traffic
(On Top of TCP)
FTP
set ftp [new Application/FTP]
$ftp attach-agent $tcp Telnet
set telnet [newApplication/Telnet]
$telnet attach-agent $tcp n1
n0
tcp
ftp
sink
7/27/2019 about Network Simulator 2
28/54
Recall: Generic Script Structure
1. set ns [new Simulator]2. [Turn on tracing]
3. Create topology
4. [Setup packet loss, link dynamics]5. Create agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulationExamples
7/27/2019 about Network Simulator 2
29/54
Post-Processing Procedures
Add a 'finish' procedure that closes the trace fileand starts nam.
proc finish {} {global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
7/27/2019 about Network Simulator 2
30/54
Run Simulation
Schedule Events$ns at
: any legitimate ns/tcl commands
$ns at 0.5 "$cbr start"$ns at 4.5 "$cbr stop
Call finish
$ns at 5.0 "finish"
Run the simulation
$ns run
7/27/2019 about Network Simulator 2
31/54
Recall: Generic Script Structure
1. set ns [new Simulator]2. [Turn on tracing]
3. Create topology
4. [Setup packet loss, link dynamics]5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulationExamples
7/27/2019 about Network Simulator 2
32/54
Visualization Tools
nam-1 (Network AniMator Version 1)
Packet-level animation
Well supported by ns
xgraph Simulation results
7/27/2019 about Network Simulator 2
33/54
7/27/2019 about Network Simulator 2
34/54
nam Interface: Nodes
Color$node color red
Shape (cant be changed after sim starts)$node shape box (circle, box, hexagon)
Label (single string)$ns at 1.1 $n0 label \web cache 0\
7/27/2019 about Network Simulator 2
35/54
nam Interfaces: Links
Color$ns duplex-link-op $n0 $n1 color"green"
Label$ns duplex-link-op $n0 $n1 label
backbone"
7/27/2019 about Network Simulator 2
36/54
nam Interface: Topology Layout
Manual layout: specify everything
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(1) $n(2) orient right
$ns duplex-link-op $n(2) $n(3) orient right$ns duplex-link-op $n(3) $n(4) orient 60deg
If anything missing automatic layout
7/27/2019 about Network Simulator 2
37/54
Simulation Example
7/27/2019 about Network Simulator 2
38/54
Examples
7/27/2019 about Network Simulator 2
39/54
Part II: Extending ns
7/27/2019 about Network Simulator 2
40/54
7/27/2019 about Network Simulator 2
41/54
TclObject: Hierarchy and Shadowing
TclObject
Agent
Agent/TCP
Agent/TCP OTclshadow object
_o123
Agent/TCP C++object
*tcp
TclObject
Agent
TcpAgent
OTcl classhierarchy
C++ classhierarchy
7/27/2019 about Network Simulator 2
42/54
Extending ns
In OTcl
In C++
TK8.0 OTcl tclclTcl8.0 ns-2 nam-1
tcl
ex test lib
...
...
examples validation tests
C++ code
OTcl code
ns-allinone
mcast
7/27/2019 about Network Simulator 2
43/54
Extending ns in OTcl
If you dont want to compile source your changes in your sim scripts
Modifying exisiting code
Recompile Adding new files
Change Makefile (NS_TCL_LIB),
Update tcl/lib/ns-lib.tcl
Recompile
7/27/2019 about Network Simulator 2
44/54
Add Your Changes into ns
TK8.0 OTcl tclclTcl8.0 ns-2 nam-1
tcl
ex test lib
...
...
examples validation tests
C++ code
OTcl code
ns-allinone
mcastmysrc
msg.tcl
7/27/2019 about Network Simulator 2
45/54
7/27/2019 about Network Simulator 2
46/54
OTcl Linkage
Lets create a new agent MyAgent
Dummy agent
Derived from the Agent class
7/27/2019 about Network Simulator 2
47/54
Step 1: Export C++ class to OTcl
7/27/2019 about Network Simulator 2
48/54
Step 2 : Export C++ class variables to OTcl
set the default value for the variables in the
"ns-2/tcl/lib/ns-lib.tcl" file
7/27/2019 about Network Simulator 2
49/54
Step 3: Export C++ Object Control
Commands to OTcl
7/27/2019 about Network Simulator 2
50/54
Step 4: Execute an OTcl command from
C++.
7/27/2019 about Network Simulator 2
51/54
Step 5: Compile
Save above code as ex-linkage.cc
Open "Makefile", add "ex-linkage.o" at the
end of object file list.
Re-compile NS using the "make" command.
7/27/2019 about Network Simulator 2
52/54
Step 5: Run and Test MyAgent
S
7/27/2019 about Network Simulator 2
53/54
Step 5: Run and Test MyAgent
R d F T d L
7/27/2019 about Network Simulator 2
54/54
Roadmap For Todays Lecture
1. ns Primer2. Extending ns
Recommended