Network Simulator ns-2
• Event driven: Events are created based on
the applications and associated protocols,
and then organized in a chronological way
using a scheduler.
time separation is not uniform
E E E E E
1 2 3 4 5
time
2
• Time driven: The scheduler has
predefined cycles (e.g. every 1 ns)
E E E
1 2 2 Uniform time separation
time
3
Introduction
NS-2: network simulator version 2
Discrete event simulator
Packet level simulation
Features
Open source
Scheduling, routing and congestion control
Wired networks: P2P links, LAN
Wireless networks: terrestrial (ad-hoc, cellular;
GPRS, UMTS, WLAN, Bluetooth), satellite
Emulation and trace
4
NS-2: Paradigm
Object-oriented programming
Protocol layering
Modularity and extensibility
Large scale simulation
Maintenance and reusability
Split-language programming
Scripting language (Tcl)
System programming language (C)
5
NS-2: Split Languages
Tcl scripts (Tcl/OTcl)
Interpreted (interactive)
Setup and configuration
C codes (C/C++)
Compiled (efficient)
Algorithms and protocols
TclCL (OTcl/C++)
Link Tcl/OTcl scripts and C/C++ codes
Provide a layer of C++ glue over OTcl
6
NS-2: Split Objects
Nam file Trace file
*.nam *.tr
7
NS-2: A Tcl Script Example
#!/home/hsieh/ns-allinone-2.27/bin/ns
set ns [new Simulator]
set nf [open out.tr w]
$ns trace-all $nf
for {set i 0} {$i<2} {incr i} { ;# create the nodes
set n($i) [$ns node]
}
$ns duplex-link $n(0) $n(1) 1Mb 10ms DropTail
# Create a UDP agent
set udp(src) [new Agent/UDP]
$udp(src) set packetSize_ 500
$ns attach-agent $n(0) $udp(src)
/home>ns abc.tcl
proc finish {} {
global ns nf /home>abc.tcl
$ns flush-trace; close $nf
}
$ns at 5.0 "finish"
$ns run
8
NS-2: A C++ Code Example
static class UdpAgentClass : public TclClass {
public:
UdpAgentClass() : TclClass("Agent/UDP") {}
TclObject* create(int, const char*const*) {
return (new UdpAgent());
}
} class_udp_agent;
UdpAgent::UdpAgent() : Agent(PT_UDP), seqno_(-1)
{
bind("packetSize_", &size_);
}
void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp::access(p)->seqno() = ++seqno_;
p->setdata(data);
target_->recv(p);
}
9
TclCL: Class TclObject
Base class in NS-2 for split objects
Mirrored in both C++ (TclObject) and OTcl
(SplitObject)
Usage
Instantiation, bind and command
Example
set tcp [new Agent/TCP]
$tcp set window_ 30
10
Class TclObject: Hierarchy
SplitObject OTcl class C++ class TclObject
hierarchy hierarchy
Connector
Agent Agent
Agent/TCP TcpAgent
_o123 *tcp
Agent/TCP Agent/TCP
OTcl object C++ object
11
NS-2: Directory Structure
ns-allinone http://www.isi.edu/nsnam/ns/
Tcl8 TK8 OTcl TclCL ns-2 nam-1 ...
tcl ... C++ code
ex test lib mcast ...
examples validation tests
OTcl code
12
Network Simulator ns-2
Part II: Wired Network
Event Scheduler
Create event scheduler
set ns [new Simulator]
Schedule events (OTcl)
OTcl: $ns at <time> <TCL_command>
C++: Scheduler::schedule(h,e, delay)
Obtain simulation time
OTcl: $ns now
C++: Scheduler::clock()
Start scheduler
$ns run
The last line of your OTcl script
14
!"#$%&'(!)*(+*,-."/(
0 1 . Trace
&,23
Trace packets on all links
!"#$%&'()%*+(),"-#.
15
/+(&++0(.,(+1."#$.(.2+(&+$+33#"4(0#.#(,5.(,6(.2%3(6%*+7((8,9:
Network Topology
Nodes
set n0 [$ns node]
set n1 [$ns node]
Links and queues
$ns duplex-link $n0 $n1 \
<bandwidth> <delay> <queue>
queue: DropTail, RED, CBQ, FQ, …
Link delay = f (bandwidth, delay)
= packet transmission time + propagation delay
16
Transport: TCP
TCP
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n1 $tcpsink
$ns connect $tcp $tcpsink
Use create-connection{}
Customization
$agent set fid <fid>
$agent set packetSize_ <size>
17
Application
FTP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$tcp attach-app FTP
CBR
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1000
$cbr set rate_ 16000
Start traffic generation
$ns at <time> “$app start”
18
Example
brows to:
~/ns-allinone-2.xx/ns-2.xx/tcl/ex/
Open
simple.tcl
Run the “ns simple.tcl”
19