Ns 2
Ns 2
• Network simulator
• a discrete event simulator
15-744: Computer Networking
• focused on modeling network protocols
• wired, wireless, satellite
The network simulator ns-2 • TCP, UDP, multicast, unicast
Amit Manjhi
Slides loosely based on tutorials by Polly Huang (ETH), John Heidemann (USC/ICSI) and
• Web, telnet, ftp
Bianca (CMU).
• Ad hoc routing; sensor networks
• Infrastructure: stats, tracing, error models etc.
• Allow collaboration
Used to:
• Freely distributed, open source
• Results can be verified • Evaluate performance of existing
• Protocols can be compared network protocols.
• Support networking research and education • Prototyping and evaluation of new protocols.
• Large-scale simulations not possible
in real experiments.
1
ns ns - software structure
Otcl / Tcl
Pure OTcl Pure C++
Current status:
objects objects • 100K lines of C++ code tclcl
Your ns-script • 70K lines of otcl code
• 20K lines of documentation
• User base about 1K institutions, 10K C++
C++/OTcl split users.
OTcl objects C++
ns
2
Outline Tcl basics
3
Basic otcl - inheritance Outline
• Create scheduler
• Creating the event scheduler • set ns [new Simulator]
• [Tracing]
• Schedule event
• Creating network topology
• $ns at <time> <event>
• Creating Transport Layer - Agents • <event>: any legitimate ns/tcl commands
• Creating Applications - Applications
• Start scheduler
• Events! • $ns run
4
“Hello World” in ns Creating Network
simple.tcl • Nodes
set ns [new Simulator] • set n0 [$ns node]
$ns at 1 “puts \“Hello World!\”” • set n1 [$ns node]
$ns at 1.5 “exit”
$ns run
• Links & Queuing
bovik@gs19% ns simple.tcl • $ns duplex-link $n0 $n1 <bandwidth>
<delay> <queue_type>
Hello World!
• Queue type: DropTail, RED, CBQ, FQ,
bovik@gs19%
SFQ, DRR
• Unicast
• $ns rtproto <type> Class Agent
• <type>: Static, Session, DV
• Multicast support also.
• Traffic
Agent/UDP Agent/TCP (=Tahoe)
• Simple two layers: transport and application.
• Transport: TCP, UDP etc.
• Applications: web, ftp, telnet etc. …
Other TCP flavors
5
The transport layer: UDP The transport layer: TCP
• UDP • TCP
• set udp [new Agent/UDP] • set tcp [new Agent/TCP]
• set null [new Agent/NULL] • set tcpsink [new Agent/TCPSink]
Class Application
Class Agent
…
Agent/TCP/FullTCP
Other TCP flavors
6
Creating Traffic: On Top of TCP Creating Traffic: On Top of UDP
FTP • CBR
• set ftp [new Application/FTP] • set src [new Application/Traffic/CBR]
7
More Tracing Controlling object parameters
8
ns “components” Network Dynamics: Link failures
9
Basic ns-2: Not Covered Outline
• Overview
• mobile IP • Tcl, OTcl basics
• multicasting • ns basics
• satellite • Extending ns
• emulation • ns internals
ns-allinone
Pure OTcl Pure C++
objects objects
Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1
10
New component purely in Otcl New component in C++
ns-allinone
• Create C++ class, fill in methods
Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1
• Define otcl linkage
tcl Makefile.in... C++ code • Write otcl code (if any)
ex test mysrc lib mcast ... • Build (and debug)
examples validation tests msg.tcl nslib.tcl
OTcl code
source tcl/mysrc/msg.tcl
• Overview
• Tcl, OTcl basics Pure C++ Pure OTcl
objects objects
• ns basics
• Extending ns
C++/OTcl split
• ns internals C++ objects
OTcl
ns
11
TclObject: Hierarchy and Shadowing TclObject
• Example
C++ class
TclObject otcl class TclObject
hierarchy hierarchy set tcp [new Agent/TCP]
=> how is corresponding C++ object created?
Agent Agent
$tcp set window_ 500
=> how is corresponding C++ variable set?
Agent/TCP TcpAgent
$tcp advance 5000
_o123 *tcp => how is C++ procedure called?
Agent/TCP otcl Agent/TCP C++
shadow object object
12
TclObject::command() TclObject::command()
OTcl space
no such
$tcp advance TclObject::unknown{} $tcp cmd advance
procedure
• Implement otcl methods in C++
• Trap point: otcl method cmd{} C++ space
TcpAgent::command()
• Send all arguments after cmd{} call to
TclObject::command() Yes match No
“advance”?
TclObject::command() TclObject
• otcl • Example
set tcp [new Agent/TCP]
$tcp advance 10
set tcp [new Agent/TCP]
=> how is corresponding C++ object created?
?
• C++
int TcpAgent::command(int argc,
const char*const* argv) {
$tcp set window_ 500
if (argc == 3) { => how is corresponding C++ variable set?
if (strcmp(argv[1], “advance”) == 0) {
int newseq = atoi(argv[2]);
…… $tcp advance 5000
return(TCL_OK); => how is C++ procedure called?
}
}
return (Agent::command(argc, argv);
}
13
TclObject: Creation and Deletion TclClass
14
Class TclCommand Summary
15