http://cnlab.kaist.ac.kr/~swkim/NS2workshop2008/ns2_workshop_20080128.ppt 문서의 HTML 버전입니다.
G o o g l e은 웹문서를 수집(crawl)하면서 자동으로 문서의 HTML 버전을 생성합니다.
 

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

 

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

 

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 

 

62  

TCP Jump Start – Step 5 

 

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 

 

69  

New Packet Header – Step 2 

 

70  

New Packet Header – Step 3 

 

71  

New Packet Header – Step 4 

 
 

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 

 

75  

Agent/Message – Step 3 

 

76  

Agent/Message – Step 4 

 

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 

 

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 

 

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