0% found this document useful (0 votes)
15 views15 pages

Ns 2

Network simulator (ns) is a discrete event simulator focused on modeling network protocols like TCP, UDP, multicast, and unicast for wired, wireless and satellite networks. It allows for collaboration through open source distribution and supports networking research and education through evaluation of existing and new network protocols via large-scale simulations not possible with real experiments. Ns uses an event-driven approach where the simulator maintains a list of events that are processed in order of increasing virtual time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

Ns 2

Network simulator (ns) is a discrete event simulator focused on modeling network protocols like TCP, UDP, multicast, and unicast for wired, wireless and satellite networks. It allows for collaboration through open source distribution and supports networking research and education through evaluation of existing and new network protocols via large-scale simulations not possible with real experiments. Ns uses an event-driven approach where the simulator maintains a list of events that are processed in order of increasing virtual time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

What is ns?

• 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.

ns -- goals ns --- what is it good for?

• 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

How does it work: • Object oriented (C++, OTcl) – code reuse


• Event-driven simulator • Scalability + Extensibility
• Model world as events • Control/”data” separation
• Simulator has list of events • Split C++/OTcl object
• Process: take next one, run it, until done
• C++ for packet-processing (fast to run)
• Each event happens in instant of virtual time, but
takes arbitrary real time • OTcl for control - (fast to write)
• Single thread of control • Simulation setup and configuration
• Packet level

otcl and C++: The Duality Development Status

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

• Overview proc fact {x} {


set ret 1
• Tcl, OTcl basics if {$x > 2} {
• ns basics for {set i 1} {$i <= $x} {incr i} {
• Extending ns set ret [expr $i * $ret]
}
• ns internals }
puts “factorial of $x is $ret”
}
fact 5 à factorial of 5 is 120

Tcl basics Basic otcl

proc fact {x} {


• $ for de-referencing Class mom set a [new mom 45]
set ret 1 $a greet
if {$x > 2} { • Spaces - important mom instproc init {age} {
for {set i 1} {$i <= $x} {incr i} { •{} defines a block $self instvar age_
• instead of single class declaration
set ret [expr $i * $ret] • set, puts set age_ $age
multiple definitions
• proc definition: }
} • instproc adds class methods
proc name args body
} mom instproc greet {} { • instvar adds instance variable, and
$self instvar age_ brings them to the local scope
puts “factorial of $x is $ret”
puts “$age_ years old mom:
} How are you doing?” • $self : this in Java, C++
} • all methods virtual (as in Java)
fact 5 à factorial of 5 is 120

3
Basic otcl - inheritance Outline

Class kid -superclass mom • Overview


kid instproc greet {} {
• Tcl, OTcl basics
$self instvar age_ • ns basics
puts “$age_ years old kid:
What’s up, dude?” • Extending ns
}
• ns internals

set b [new kid 15]


$b greet

Basic structure of ns-scripts Creating Event Scheduler

• 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

Routing + traffic Transport Layer

• 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]

• $ns attach-agent $n0 $udp • $ns attach-agent $n0 $tcp


• $ns attach-agent $n1 $null • $ns attach-agent $n1 $tcpsink

• $ns connect $udp $null


• $ns connect $tcp $tcpsink

Transport Layer Application Layer

Class Application
Class Agent

{Simulated Applications} {Traffic generators}


Agent/UDP Agent/TCP (=Tahoe) (on top of TCP) (on top of UDP)


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]

• $ftp attach-agent $tcp • Exponential or Pareto on-off


• $ns at <time> “$ftp start” • set src [new Application/Traffic/Exponential]
• set src [new Application/Traffic/Pareto]

Telnet • Trace driven traffic


• set telnet [new Application/Telnet] • Inter-packet time and packet-size

• $telnet attach-agent $tcp

Attaching a traffic source Tracing


Trace packets on all links:
• set f[open out.tr w]
• set cbr [new Application/Traffic/CBR] • $ns trace-all $f

• $cbr attach-agent $udp • $ns flush-trace


• close $f
• $ns at <time> “$cbr start”
<event><time><from><to><type><size>--<flags>--<flow id><src><dst><seqno> <pckt id>
+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0

Is tracing all links always the best thing to do?

7
More Tracing Controlling object parameters

• Tracing specific links • Almost all ns objects have parameters


• $ns trace-queue $n0 $n1 $f • ex. Application/Traffic/Exponential has rate and
packetSize
• set parameters in OTcl
• Tracing variables • set etraf [new Application/Traffic/Exponential]
• set cwnd_chan_ [open all.cwnd w] • $etraf set rate_ 1Mb
• $tcp trace cwnd_ • $etraf set packetSize_ 1024

• $tcp attach $cwnd_chan_

Putting it all together nam – the network animator


set ns [new Simulator]
set n0 [$ns node] set nf [open out.nam w]
set n1 [$ns node]
Creating Topology $ns namtrace-all $nf
$ns duplex-link $n0 $n1 1.5Mb …
10ms DropTail
exec nam out.nam &
$ns trace-queue $n0 $n1 $f
set tcp [$ns create-connection TCP Creating Transport layer
$n0 TCPSink $n1 0]

set ftp [new Application/FTP] n0 n1


Creating Applications
$ftp attach-agent $tcp

$ns at 0.2 "$ftp start“ Schedule Events


$ns at 1.2 ”exit“
$ns run

8
ns “components” Network Dynamics: Link failures

• ns, the simulator itself


• $ns rtmodel-at <time> <up|down> $n0 $n1
• nam
nam, the Network AniMator
• Visualize ns output
• $ns rtmodel Trace <config_file> $n0 $n1
• GUI input simple ns scenarios
• Pre-processing:
• Traffic and topology generators • $ns rtmodel <model> <params> $n0 $n1
• Post-processing: <model>: Deterministic, Exponential
• Simple trace analysis, often in Awk, Perl, or Tcl

Issues in Simulations Another practical issue: Memory


• Suppose you want to study the way TCP sources
~ns/tcl/ex/cmcast-150.tcl:
share a bottleneck link…
Agent/FTP 150 nodes, 2200 links => 53MB
Agent/FTP Which topology? 2420 nodes, 2465 links => 800MB
Agent/FTP
Agent/FTP

Which traffic sources? Background Traffic?


• Avoid trace
trace--all
When to start sources? • Use arrays for a sequence of variables
What else affects results? • Instead of n$i
n$i, say n($i)

9
Basic ns-2: Not Covered Outline

• Overview
• mobile IP • Tcl, OTcl basics
• multicasting • ns basics
• satellite • Extending ns
• emulation • ns internals

Making changes to ns – where??? ns directory structure

ns-allinone
Pure OTcl Pure C++
objects objects
Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1

C++/OTcl split tcl Makefile.in ... C++ code


OTcl objects C++
ns
ex test lib mcast ...
Where would you implement
• one-time configuration variables examples
validation tests OTcl code
• complex procedures
• per packet action

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

Outline How does linkage work?

• Overview
• Tcl, OTcl basics Pure C++ Pure OTcl
objects objects
• ns basics
• Extending ns
C++/OTcl split
• ns internals C++ objects
OTcl
ns

• how to access Tcl variables from C++


• how is C++ object created from interpreter
• ……

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

TclObject: Hierarchy and Shadowing TclObject::bind()

C++ class • Link C++ member variables to otcl object


TclObject otcl class TclObject
variables
hierarchy hierarchy
• C++
TcpAgent::TcpAgent() {
Agent Agent
bind(“window_”, &wnd_);
… …
}
Agent/TCP TcpAgent • bind_time(), bind_bool(), bind_bw()
_o123 *tcp • otcl
Agent/TCP otcl Agent/TCP C++ $tcp set window_ 200
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”?

process and return Invoke parent:


return Agent::command()

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

Agent/TCP parent TclObject


constructor constructor constructor
invoke parent which C++
invoke parent create C++
Static
Staticclass
classTcpClass
TcpClass::public
publicTclClass
TclClass{{
constructor constructor
object to create? object public:
public:
complete – TclClass
complete create OTcl TcpClass()
TcpClass()::TclClass(“Agent/TCP”)
TclClass(“Agent/TCP”){}{}
initialization initialization shadow object TclObject*
OTcl TclObject* create(int,const
create(int, constchar*const*)
char*const*){{
return
return (new
(newTcpAgent());
TcpAgent());
C++ }}
TclObject (C++) parent (Agent) AgentTCP
constructor constructor constructor }}class_tcp;
class_tcp;
invoke parent invoke parent
do nothing, constructor constructor
return bind variables bind variables
and return and return

Class Tcl Class Tcl

Tcl& tcl = Tcl::instance();


• Singleton class with a handle to Tcl
interpreter Passing results if (strcmp(argv[1], “now”) == 0) {
• While writing C++ code to the interpreter: tcl.resultf(“%g”, clock());
return TCL_OK;
• Usage }
• Invoke otcl procedure
• Obtain otcl evaluation results
Executing Otcl if (strcmp(argv[1], “helloworld”) {
• Pass a result string to otcl commands from tcl.evalc(“puts stdout Hello World”);
• Return success/failure code to otcl C++ return TCL_OK;
}

14
Class TclCommand Summary

Root of ns-2 object hierarchy


• C++ implementation of global otcl commands
bind(): link variable values between
class RandomCommand : public TclCommand {
TclObject C++ and OTcl
public:
RandomCommand() : TclCommand("ns -random") {}
command(): link OTcl methods to C++
virtual int command(int argc, const char*const* argv) ;
}; implementations
int RandomCommand::command(int argc, const char*const* argv) TclClass Create and initialize TclObject’s
{
Tcl& tcl = Tcl::instance(); Tcl C++ methods to access Tcl interpreter
if ( argc == 1) {
sprintf(tcl.buffer(), "%u", Random::random()); TclCommand Standalone global commands
tcl.result(tcl.buffer());

EmbeddedTcl ns script initialization


}

15

You might also like