NS-2 Internal Structure and
Extension
ICU 공학부
김승운
(swkim@icu.ac.kr)
2
Contents
3
Contents
4
Ns Internals
5
Hierarchy of Network
Components
TclObject
NsObject
Connector
Classifier
Delay
AddrClassifier
Agent
McastClasifier
Queue
Trace
DropTail
RED
TCP
Enq
Deq
Drop
Reno
SACK
Handler
6
TclOject & NsObjevt
7
Connector
Connector
recv(p,h)
send(p,h)
8
Classifier
Classifier
9
Network Topology: Node
n0
n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
Node entry
Unicast Node
Multicast
Classifier
classifier_
dmux_
entry_
Node entry
Multicast Node
multiclassifier_
set n0 [$ns_ node]
set ns_ [new Simulator –multicast on]
set n1 [$ns_ node]
0
0
1
10
Node Addressing
11
Hierarchical Node
n2
Node
entry
Level 1
Level 2
Level 3
Address
classifier
To Port
demux
$ns_ node-config –addressing hier
12
Network Topology: Link
n0
n1
enqT_
queue_
deqT_
drophead_
drpT_
link_
ttl_
n1 entry_
head_
tracing
simplex link
duplex link
[$ns_ duplex-link $n0 $n1 5Mb 2ms
drop-tail]
13
Queue
Queue
PacketQueue
recv
resume
enque
deque
14
Queue blocking
15
Transport
n0
n1
Addr
Classifier
Port
Classifier
classifier_
dmux_
entry_
Agent/TCP
agents_
Addr
Classifier
Port
Classifier
classifier_
dmux_
entry_
Agent/TCPSink
agents_
dst_=1.0
dst_=0.0
set tcp [new Agent/TCP]
$ns_ attach-agent $n0 $tcp
set tcpsink [new Agent/TCPSink]
$ns_ attach-agent $n1 $tcpsink
$ns_ connect $tcp $tcpsink
0
0
1
0
16
Agent
17
Agent Functions
18
Application: Traffic
Generator
n0
n1
Addr
Classifier
Port
Classifier
classifier_
dmux_
entry_
Agent/TCP
agents_
Addr
Classifier
Port
Classifier
classifier_
dmux_
entry_
Agent/TCPSink
agents_
dst_=1.0
dst_=0.0
Application/FTP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 “$ftp start”
0
0
1
0
19
Routing
n0
n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
Node entry
0
1
enqT_
queue_
deqT_
drophead_
drpT_
link_
ttl_
n1 entry_
head_
0
Agent/TCP
agents_
Application/FTP
20
n0
n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
0
1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1
0
Link n0-n1
Link n1-n0
Agent/TCP
agents_
Application/FTP
Agent/TCP
Routing (con’t)
21
Routing
22
Routing module
(RtModule)
23
When add-route?
24
Plumbing: Packet Flow
0
1
n0
n1
Addr
Classifier
Port
Classifier
entry_
0
Agent/TCP
Addr
Classifier
Port
Classifier
entry_
1
0
Link
n0-n1
Link
n1-n0
0
Agent/TCPSink
dst_=1.0
dst_=0.0
Application/FTP
25
Packet
26
Packet Format
header
data
ip header
tcp header
rtp header
trace
header
cmn header
...
ts_
ptype_
uid_
size_
iface_
27
Wireless Network
Components
28
Wireless Packet Format
header
data
ts_
ptype_
uid_
size_
iface_
IP header
......
cmn header
LL
MAC 802_11
......
ARP
wireless
headers
29
Mobile Node Abstraction
30
Portrait of A Mobile
Node
Node
ARP
Propagation and antenna models
MobileNode
LL
MAC
PHY
LL
CHANNEL
LL
MAC
PHY
Classifier: Forwarding
Agent: Protocol Entity
Node Entry
LL: Link layer object
IFQ: Interface queue
MAC: Mac object
PHY: Net interface
protocol
agent
routing
agent
addr
classifier
port
classifier
255
IFQ
IFQ
defaulttarget_
31
Mobile Node: Components
32
Mobile Node: Components
33
Wireless Channel
34
Contents
35
ns Directory Structure
TK8.4
OTcl
tclcl
Tcl8.4
ns-2
nam-1
tcl
ex
test
lib
...
...
examples
validation tests
C++ code
OTcl code
ns-allinone
mcast
36
Extending ns in OTcl
37
Example: Agent/Message
n0
n1
n4
n5
n2
n3
128Kb, 50ms
10Mb, 1ms
10Mb, 1ms
C
C
cross
traffic
S
R
msg agent
38
Agent/Message
S
R
pkt: 64 bytes
of arbitrary string
Receiver-side
processing
39
Agent/Message: Step 1
Class Sender
–superclass Agent/Message
# Message format: “Addr Op SeqNo”
Sender instproc send-next {} {
$self instvar seq_ agent_addr_
$self send “$agent_addr_ send $seq_”
incr seq_
global ns
$ns at [expr [$ns now]+0.1] "$self send-next"
}
40
Agent/Message: Step 2
Sender instproc recv msg {
$self instvar agent_addr_
set sdr [lindex $msg 0]
set seq [lindex $msg 2]
puts "Sender gets ack $seq from $sdr"
}
41
Agent/Message: Step 3
Class Receiver –superclass Agent/Message
Receiver instproc recv msg {
$self instvar agent_addr_
set sdr [lindex $msg 0]
set seq [lindex $msg 2]
puts “Receiver gets seq $seq from $sdr”
$self send “$agent_addr_ ack $seq”
}
42
Agent/Message: Step 4
# Create scheduler
set ns [new
Simulator]
# Turn on Tracing
set fd [open message.tr w]
$ns trace-all $fd
43
Agent/Message: Step 5
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail
$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail
$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail
$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail
$ns duplex-link $n(0)
$n(3) 10Mb 1ms DropTail
$ns queue-limit $n(0) $n(1) 5
$ns queue-limit $n(1) $n(0) 5
44
Agent/Message: Step 6
# Packet loss
produced by queueing
# Routing protocol: let’s run distance vector
$ns rtproto
DV
45
Agent/Message: Step 7
set udp0 [new Agent/UDP]
$ns attach-agent $n(2) $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(4) $null0
$ns connect $udp0
$null0
set exp0 [new Application/Traffic/Exponential]
$exp0 set rate_ 128k
$exp0 attach-agent $udp0
$ns at 1.0 “$exp0 start”
46
Agent/Message: Step 8
set sdr [new Sender]
$sdr set seq_ 0
$sdr set packetSize_
1000
set rcvr [new Receiver]
$rcvr set packetSize_
40
$ns attach-agent $n(3) $sdr
$ns attach-agent $n(5) $rcvr
$ns connect $sdr $rcvr
$ns at 1.1 “$sdr
send-next”
47
Agent/Message: Step 9
$ns at 2.0 finish
proc finish {} {
global ns fd
$ns flush-trace
close $fd
exit 0
}
$ns run
48
Agent/Message: Result
% ./ns msg.tcl
Receiver gets seq 0 from 3
Sender gets ack 0 from 5
Receiver gets seq 1 from 3
Sender gets ack 1 from 5
Receiver gets seq 2 from 3
Sender gets ack 2 from 5
Receiver gets seq 4 from 3
Sender gets ack 4 from 5
Receiver gets seq 7 from 3
49
Add Your Changes into ns
TK8.3
OTcl
tclcl
Tcl8.3
ns-2
nam-1
tcl
ex
test
lib
...
...
examples
validation tests
C++ code
OTcl code
ns-allinone
mcast
mysrc
msg.tcl
50
Add Your Change into ns
Class Simulator
…
source ../mysrc/msg.tcl
NS_TCL_LIB = \
tcl/mysrc/msg.tcl \
…
make depend and make
51
Contents
52
Extending ns in C++
53
Creating New Components
54
Guidelines
55
New Agent, Existing
Header
56
TCP Jump Start – Step 1
TclObject
NsObject
Connector
Classifier
Delay
AddrClassifier
Agent
McastClasifier
Queue
Trace
DropTail
RED
TCP
Enq
Deq
Drop
Reno
SACK
JS
Handler
57
Reminder: C++/OTcl
Linkage
$ns/tcp/tcp.{h,cc}
class TcpAgent : public Agent {
public:
TcpAgent();
…
}
static class TcpClass: public TclClass {
public:
TcpClass() : TclClass(“Agent/Tcp”){}
TclObject* create(int , const char*const*) {
return (new TcpAgent()); }
} class_tcp;
Class Definition
TclClass
58
Reminder: C++/OTcl
Linkage
$ns/tcp/tcp.{h,cc}
TcpAgent::TcpAgent(): Agent(PT_TCP), … {
bind_(“t_seqno_”, &t_seqno_);
…
}
int TcpAgent::command(int argc,
const char*const* argv){
if (argc == 3) {
if (strcmp(argv[1], “advance”) == 0) {
…
}… } …
}
$ns/tcl/lib/ns-agent.tcl:
Agent/TCP instproc init {} {… }
$ns/tcl/lib/ns-default.tcl:
Agent/TCP set t_seqno_
0 …
Class Body
Otcl setting
59
TCP Jump Start – Step 2
#include “tcp.h”
class JSTCPAgent : public TcpAgent {
public:
virtual void set_initial_window() {
cwnd_ = MAXWIN_;
}
private:
int MAXWIN_;
};
60
TCP Jump Start – Step 3
#include <tclcl.h>
#include “tcp.h”
#include “tcp-js.h”
static class JSTcpClass : public TclClass {
public:
JSTcpClass() : TclClass("Agent/TCP/JS") {}
TclObject* create(int, const char*const*){
return (new JSTcpAgent());
}
} tcpjs_agent;
JSTcpAgent::JSTcpAgent() {
bind(“MAXWIN_”, &MAXWIN_);
}
61
TCP Jump Start – Step 4
% configure --enable-debug –with-tcldebug=../tcl-debug-2.0
% make depend
% make
62
TCP Jump Start – Step 5
% ns tcp-js.tcl
63
TCP vs TCP-JS
TCP: initial cwnd = 1
TCP-JS: initial cwnd = 10
time
time
Packet sequence number
64
New Packet Header
65
Packet Format
header
data
ip header
tcp header
rtp header
trace
header
cmn header
...
ts_
ptype_
uid_
size_
iface_
66
How Packet Header Works
Packet
next_
hdrlen_
bits_
size determined
at compile time
size determined
at compile time
size determined
at compile time
……
hdr_cmn
hdr_ip
hdr_tcp
size determined
at simulator
startup time
(PacketHeaderManager)
PacketHeader/Common
PacketHeader/IP
PacketHeader/TCP
67
Example: Agent/Message
68
New Packet Header – Step
1
struct hdr_msg {
char msg_[64];
static int offset_;
inline static int& offset() { return offset_; }
inline static hdr_msg* access(Packet* p) {
return (hdr_msg*) p->access(offset_);
}
/* per-field member functions */
char* msg() { return (msg_); }
int maxmsg() { return (sizeof(msg_)); }
};
69
New Packet Header – Step
2
static class MessageHeaderClass :
public PacketHeaderClass {
public:
MessageHeaderClass() :
PacketHeaderClass("PacketHeader/Message",
sizeof(hdr_msg)) {
bind_offset(&hdr_msg::offset_);
}
} class_msghdr;
70
New Packet Header – Step
3
enum packet_t {
PT_TCP,
…,
PT_MESSAGE, // Packet Type
PT_NTYPE // This MUST be the LAST one
};
class p_info {
……
name_[PT_MESSAGE] = “message”;
name_[PT_NTYPE]= "undefined";
……
};
71
New Packet Header – Step
4
foreach prot {
Common
…
Message # Protocol name
… }{
add-packet-header $prot
}
72
Accessing the Header – Step
5
73
Agent/Message – Step 1
TclObject
NsObject
Connector
Classifier
Delay
AddrClassifier
Agent
McastClasifier
Queue
Trace
DropTail
RED
TCP
Enq
Deq
Drop
Reno
SACK
Message
74
Agent/Message – Step 2
// Standard split object declaration
static …
class MessageAgent : public Agent {
public:
MessageAgent() : Agent(PT_MESSAGE) {}
virtual int command(int argc, const char*const* argv);
virtual void recv(Packet*, Handler*);
};
static class MessageClass : public TclClass {
public:
MessageClass() : TclClass("Agent/Message") {}
TclObject* create(int, const char*const*) {
return (new MessageAgent());
}
} class_message;
75
Agent/Message – Step 3
int MessageAgent::command(int, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (strcmp(argv[1], "send") == 0) {
Packet* pkt = allocpkt();
hdr_msg* mh = hdr_msg::access(pkt);
// We ignore message size check...
strcpy(mh->msg(), argv[2]);
send(pkt, 0);
return (TCL_OK);
}
return (Agent::command(argc, argv));
}
76
Agent/Message – Step 4
void MessageAgent::recv(Packet* pkt, Handler*)
{
hdr_msg* mh =
hdr_msg::access(pkt);
// OTcl callback
char wrk[128];
sprintf(wrk, "%s recv {%s}", name(), mh->msg());
Tcl& tcl = Tcl::instance();
tcl.eval(wrk);
Packet::free(pkt);
}
77
Contents
78
Debugging C++ in ns
79
C++/OTcl Debugging
80
C++/OTcl Debugging
(gdb) call Tcl::instance().eval(“debug 1”)
15: lappend auto_path $dbg_library
dbg15.3> w
*0: application
15: lappend auto_path $dbg_library
dbg15.4> Simulator info instances
_o1
dbg15.5> _o1 now
0
dbg15.6> # and other fun stuff
dbg15.7> c
(gdb) where
#0 0x102218 in write()
......
81
Installing tcl-debug in
ns-2
1. tcl-debug-2.0 설치
http://www.isi.edu/nsnam/ns/ns-debugging.html의 Tcl-level Debugging 부분을 참조하여 tcl-debug.tar.gz 파일을 다운로드 받음
ns-2.xx와 parallel하도록 ~/ns-allinone-2.xx/tcl-debug-2.0 디렉토리에 압축 품
tcl-debug-2.0/INSTALL 화일을 참조하여 다음과 같이 설치
1$ ./configure --enable-gcc
2$ make
3$ make install
2. ns-2.xx에 tcp-debug 추가
ns-2.xx/ns_tclsh.cc 화일에 다음 두 문장을 추가
// 시작부에 tcldbg.h를 include
29th line : #include "tcldbg.h” // 이 라인을 추가할 것
// TclAppInit(Tcl ...) 함수 내에 Tcldbg_Init(interp); 추가
42nd line : Tcl::init(interp, "ns");
43rd line : Tcldbg_Init(interp); // 이 라인을 추가할 것
ns-2.xx를 다시 컴파일
1$ ./configure --enable-debug --with-tcldebug=../tcl-debug-2.0
2$ make depend
3$ make
82
Memory Debugging in ns
83
dmalloc: Usage
84
Pitfalls
85
Scalability vs
Flexibility
86
THE Merit of OTcl
Program size,
complexity
C/C++
OTcl
high
low
split objects
87
Object Granularity Tips
88
Memory Conservation Tips
89
Memory Leaks
for {set i 0} {$i < 500} {incr i} {
set a [new RandomVariable/Constant]
}
90
Final Word
Thank you
Multiple inhertitance
Domain/cluster/nodes
Domain, cluster. nodes
- or change Makefile.in, make distclean, then
Multiple inhertitance
Valgrind a new deugging tool for linux