Title: ns2 Tutorial, part 2
1ns-2 Tutorial, part 2
- Dave Anderson, Xiaowei Yang
- (Modified from Polly Huangs slides)
2Outline
- Ns-2 internals
- script -gt internal structure
- How to Extend ns
3Script Code
- Create a topology
- Set ns new Simulator
- set n0 ns node
- set n1 ns node
- ns duplex-link n0 n1 1Mb 10ms DropTail
- Add Transport agents
- set tcp new Agent/TCP
- ns attach-agent n0 tcp
- set tcpsink new Agent/TCPSink
- ns attach-agent n1 tcpsink
- tcp connect tcpsink
- Add application
- set ftp new Application/FTP
- ftp attach-agent tcp
Create a schedule ns at 0.5 "ftp start" ns
at 4.5 "ftp stop" Run the simulation ns run
FTP
TCP
1Mb
n0
n1
10ms
4Discrete Event-Driven Simulator
time_, uid_, next_, handler_
head_ -gt
handler_ -gt handle()
insert
time_, uid_, next_, handler_
ns at 0.5 ftp start ns at 4.5 ftp
stop ns run
5Ns Node
set n0 ns node set n1 ns node
6Network Topology - Link
ns duplex-link n0 n1 1Mb 10ms DropTail
7Routing
Link n0-n1
8Transport
n0
n1
Port Classifier
Port Classifier
Addr Classifier
Addr Classifier
0
0
agents_
dmux_
dmux_
Link n0-n1
entry_
entry_
classifier_
classifier_
Link n1-n0
set tcp new Agent/TCP ns attach-agent n0 tcp
9Application
n0
n1
Port Classifier
Port Classifier
Addr Classifier
Agent/TCP
Addr Classifier
Agent/TCPSink
0
0
agents_
agents_
dmux_
dmux_
Link n0-n1
entry_
entry_
classifier_
classifier_
Link n1-n0
set ftp new Application/FTP tcp attach-agent
ftp
10Packet Flow
n0
n1
Port Classifier
Port Classifier
Application/FTP
Addr Classifier
Agent/TCP
Addr Classifier
Agent/TCPSink
0
0
Link n0-n1
entry_
entry_
Link n1-n0
11Packet Format
header
data
12Extending ns-2 Simulator
13otcl and C The Duality
C
otcl
- C for data
- per packet action
- otcl for control
- periodic or triggered action
14Adding New Classes
procedures
variables
otcl
C
15TclClass
Static class TcpClass public TclClass
public TcpClass() TclClass(Agent/Echo)
TclObject create(int, const charconst)
return (new EchoAgent()) class_echo
16TclObject bind()
- C
- EchoAgentTcpAgent()
- bind(msglen_, len_)
-
-
- otcl
- Agent/Echo set msglen_ 50
17TclObject command()
- C
- Int EchoAgentcommand(int argc, const
charconst argv) - if (argc 2)
- if (strcmp(argv1, send) 0)
- send()
- return(TCL_OK)
- else if (strcmp(argv1, recv) 0)
- send()
- return(TCL_OK)
-
-
- return (Agentcommand(argc, argv)
-
- otcl
- set echo new Agent/Echo
- echo send