添加自行設計的Protocol到NS2中以利模擬使用。

環境:Ubuntu 12.04 + NS-2.35

步驟:

Step 1:  假設protocol名稱為protoname,在~/ns-allinone-2.35/ns2.35目錄下建立protoname此目錄,目錄中包含有

  • protoname.h     定義必要的Timer及Routing Agent
     
  • protoname.cc    執行Timer、Routing Agent、TCL檔
     
  • protoname_pkt.h 定義protoname routing protocol需要在wireless ad hoc network中節點交換的packets
     
  • protoname_rtable.h 定義routing table
     
  • protoname_rtable.cc 執行routing table

以下為google找到的五個文件,可直接下載並放置在protoname目錄下

連結一:http://140.116.164.80/~smallko/ns2/protoname.rar

連結二:www.cs.nccu.edu.tw/~g10031/protoname.rar

Step 2:  對NS-2中的文件進行修改,讓自行定義之protocol能夠被NS-2使用,

請到~/ns-allinone-2.35/ns2.35目錄下找到以下檔案個別修改之

註: 紅色字體為加入或修改部分

common/packet.h (三個部分需要修改)

static const packet_t PT_DCCP_DATAACK = 68;
static const packet_t PT_DCCP_CLOSE = 69;
static const packet_t PT_DCCP_CLOSEREQ = 70;
static const packet_t PT_DCCP_RESET = 71;

// M-DART packets
static const packet_t PT_MDART = 72;
//--------------------------------------------
static const packet_t PT_PROTONAME = 73;
//--------------------------------------------
// insert new packet types here
static packet_t PT_NTYPE = 74; // This MUST be the LAST one


static bool data_packet(packet_t type) {
return ( (type) == PT_TCP || \
(type) == PT_TELNET || \
(type) == PT_CBR || \
(type) == PT_AUDIO || \
(type) == PT_VIDEO || \
(type) == PT_ACK || \
(type) == PT_SCTP || \
(type) == PT_SCTP_APP1 || \
(type) == PT_HDLC \
//----------------------------
(type) == PT_PROTONAME \
//----------------------------
);
}


static void initName()
{
 ....

name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
name_[PT_DCCP_RESET]="DCCP_Reset";
//------------------------------------
name_[PT_PROTONAME]="protoname";
//------------------------------------
name_[PT_NTYPE]= "undefined";
}



trace/cmu-trace.h (一個部分需要修改)

class CMUTrace : public Trace {
public:
CMUTrace(const char *s, char t);
...

private:
char tracename[MAX_ID_LEN + 1];
int nodeColor[MAX_NODE];

...

void format_tora(Packet *p, int offset);
void format_imep(Packet *p, int offset);
void format_aodv(Packet *p, int offset);
//----------------------------------------------
void format_protoname(Packet *p, int offset);
//----------------------------------------------
void format_aomdv(Packet *p, int offset);
void format_mdart(Packet *p, int offset);

// This holds all the tracers added at run-time
static PacketTracer *pktTrc_;

};

#endif /* __cmu_trace__ */



trace/cmu-trace.cc (三個部分需要修改)

在開頭的部份加入此行

//-----------------------------------------
#include <protoname/protoname_pkt.h>
//-----------------------------------------


在此格式附近加入紅色整段

void
CMUTrace::format_mdart(Packet *p, int offset)
{...} 

//-------------------------------------------------------------------
void
CMUTrace::format_protoname(Packet *p, int offset)
{
struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);

if(pt_->tagged()){
sprintf(pt_->buffer() + offset, "-protoname:o %d -proto
ph->pkt_src(),
pk->pkt_seq_num(),
ph->pkt_len());
}
else if(newtrace_){
sprintf(pt_->buffer() + offset,
"-P protoname -Po %d -Ps %d -Pl %d",
ph->pkt_src(),
ph->pkt_seq_num(),
ph->pkt_len());
}
else{
sprintf(pt_->buffer() + offset,
"[protoname %d %d %d]",
ph->pkt_src(),
ph->pkt_seq_num(),
ph->pkt_len());
}
}

//-------------------------------------------------------------------


void CMUTrace::format(Packet* p, const char *why)

{...

switch(ch->ptype()) {
case PT_MAC:
...

case PT_GAF:
case PT_PING:
break;
//--------------------------------------------
case PT_PROTONAME:
format_protoname(p, offset);
break;
//--------------------------------------------
default:
...

}



tcl/lib/ns-packet.tcl (一個部分需要修改) 

# Mobility, Ad-Hoc Networks, Sensor Nets:
AODV # routing protocol for ad-hoc networks
#/--------------------------------------------------------------------
Protoname # new routing protocol for ad-hoc networks
#/--------------------------------------------------------------------
Diffusion # diffusion/diffusion.cc



tcl/lib/ns-default.tcl (一個部分需要修改) 

Agent/MDART set macFailed_ true
Agent/MDART set etxMetric_ true

#Defaults defined for Protoname
Agent/Protoname set accessible_var_ true



tcl/lib/ns-lib.tcl (二個部分需要修改) 

switch -exact $routingAgent_ {
... 

ManualRtg {
set ragent [$self create-manual-rtg-agent $
}
#/-------------------------------------------------
Protoname {
set ragent [$self create-protoname-agent $node]
}
#/-------------------------------------------------
default {
...
}


Simulator instproc create-omnimcast-agent {node} {

...

}
#/-----------------------------------------------------
Simulator instproc create-protoname-agent {node} {
#Create Protoname routing agent
set ragent [new Agent/Protoname [$node node-addr]]
$self at 0.0 "ragent_ start"
$node set ragent_ $ragent
return $ragent
}

#/-----------------------------------------------------

# XXX These are very simulation-specific methods, why should they belon
Simulator instproc put-in-list {agent} {
...
}



queue/priqueue.cc (一個部分需要修改) 

void
PriQueue::recv(Packet *p, Handler *h)
{
struct hdr_cmn *ch = HDR_CMN(p);

if(Prefer_Routing_Protocols) {

switch(ch->ptype()) {
...
case PT_MDART:
//--------------------------------------------
case PT_PROTONAME:
//--------------------------------------------
recvHighPriority(p, h);
break;
default:
Queue::recv(p, h);
}
}
else {
Queue::recv(p, h);
}
}



Makefile (一個部分需要修改) 

 OBJ_CC = \
tools/random.o tools/rng.o tools/ranvar.o common/misc.o common/
...
wpan/p802_15_4trace.o wpan/p802_15_4transac.o \
apps/pbc.o \
#//------------------------------------------------
protoname/protoname.o protoname/protoname_rtable.o \
#//------------------------------------------------
$(OBJ_STL)



 Step 3: 編譯,在~/ns-allinone-2.35/ns2.35目錄下進行編譯:

$ make clean

$ touch common/packet.cc

$make

Step 4: 測試

建立TCL檔進行測試

For example:

set ns [new Simulator]

$ns node-config -Routing protoname  

set nf [open out.nam w]    

$ns namtrace-all $nf      

set nd [open out.tr w]      

$ns trace-all $nd            

  proc finish {} {

          global ns nf  nd

          $ns flush-trace

          close $nf      

          close $nd      

          exec nam out.nam &

          exit 0

   }

 

for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }

for {set i 0} {$i < 7} {incr i} {

$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail

}

set udp0 [new Agent/UDP]  

$ns attach-agent $n(0) $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500    

$cbr0 set interval_ 0.005     

$cbr0 attach-agent $udp0

set null0 [new Agent/Null]

$ns attach-agent $n(3) $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"

$ns rtmodel-at 1.0 down $n(1) $n(2)

$ns rtmodel-at 2.0 up $n(1) $n(2)  

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run

 執行畫面:

 

參考:

  1. http://skchaudhari.blogspot.tw/2011/11/ns2-how-addimplement-new-protocol-into.html 
     
  2. http://blog.163.com/caroline_zhang/blog/static/266255720093393331330/ 

  3. http://elmurod.net/index.php/2010/01/20/how-to-add-new-routing-protocol-in-ns2/ , NS2: How to add new routing protocol

  4. http://isi.edu/nsnam/ns/ ,The Network Simulator - ns-2
     
  5. http://masimum.inf.um.es/fjrm/wp-uploads/nsrt-howto/html/index.html ,Implementing a New Manet Unicast Routing Protocol in NS2.html

  6. http://masimum.inf.um.es/fjrm/wp-uploads/nsrt-howto/pdf/nsrt-howto.pdf ,Implementing a New Manet Unicast Routing Protocol in NS2.pdf

  7. http://hpds.ee.ncku.edu.tw/~smallko/ns2/ns2.htm , NS2 教學手冊 ( NS2 Learning Guide)
文章標籤
全站熱搜
創作者介紹
創作者 blackcomedy 的頭像
blackcomedy

Life Show

blackcomedy 發表在 痞客邦 留言(0) 人氣(1,400)