免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1303 | 回复: 0
打印 上一主题 下一主题

在NS2中添加路由协议(整理版)1 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:53 |只看该作者 |倒序浏览
最近正在研究怎样把自己新写的协议添加到NS2中去,正好借鉴了一些朋友的文章,现在整理下来,以便以后参考,也希望能给广大博友一些方便。
step 1:比如我们新建的协议名字就叫做protoname,以ns2.27平台为例,我们在ns2.27目录下建立一个protoname目录。此目录包含protoname.h,protoname.cc,protoname_pkt.h,protoname_rtable.h,protoname_rtable.cc五个文件。
其中五个文件的具体功能和作用如下:
(1)protoname.h 定义必要的计时器和路由代理
(2)protoname.cc 执行计时器、路由代理和Tcl文件
(3)protoname_pkt.h 声明protoname路由协议需要在无线自组网节点交换的数据包
(4)protoname_rtable.h 声明我们自己的路由选择表
(5)protoname_rtable.cc 执行路由选择表
step 2:相应文件的代码
(1)protoname.h
  1. #ifndef __protoname_h__
  2. #define __protoname_h__
  3. // 下面包含一些需要的头文件
  4. #include "protoname_pkt.h" //数据包报头
  5. #include "protoname_rtable.h"
  6. #include <agent.h> //代理基本类
  7. #include <packet.h> //数据包类
  8. #include <trace.h> //跟踪类,用于在跟踪文件里记录输出的仿真结果
  9. #include <timer-handler.h> //计时器基本类,创建我们自定义的计时器
  10. #include <random.h> //随机类,用于产生伪随机数
  11. #include <classifier-port.h> //端口分类器类,用于淘汰向上层传输的数据包
  12. #include <mobilenode.h>
  13. #include "arp.h"
  14. #include "ll.h"
  15. #include "mac.h"
  16. #include "ip.h"
  17. #include "delay.h"
  18. #define CURRENT_TIME Scheduler::instance().clock() //定义了一个用于得到当前仿真时间的宏
  19. //通过一个调度类的实例完成
  20. #define JITTER (Random::uniform()*0.5) //在0-0.5之间去随机数作为发送数据的延迟时间
  21. class Protoname; // forward declaration
  22. /* Timers */ //自定义计时器发送定时的控制包
  23. class Protoname_PktTimer : public TimerHandler {
  24. public:
  25. Protoname_PktTimer(Protoname* agent) : TimerHandler() {
  26. agent_ = agent;
  27. }
  28. protected:
  29. Protoname* agent_;
  30. virtual void expire(Event* e);
  31. };
  32. /* Agent */ //定义Protoname 类
  33. class Protoname : public Agent {
  34. /* Friends */
  35. friend class Protoname_PktTimer;
  36. /* Private members */ //封装了自身的地址、内状态、路由表、可变的Tcl
  37. //以及一个负责指定输出数量的计数器
  38. nsaddr_t ra_addr_;
  39. //protoname_state state_;
  40. protoname_rtable rtable_;
  41. int accesible_var_; //用来读取Tcl代码或脚本语言
  42. u_int8_t seq_num_;
  43. protected:
  44. MobileNode* node_;
  45. PortClassifier* dmux_; // For passing packets up to agents.端口分类器
  46. Trace* logtarget_; // For logging.跟踪器
  47. Protoname_PktTimer pkt_timer_; // Timer for sending packets.自定义计时器
  48. //内部属性
  49. inline nsaddr_t& ra_addr() { return ra_addr_; }
  50. //inline protoname_state& state() { return state_; }
  51. inline int& accessible_var() { return accesible_var_; }
  52. void forward_data(Packet*); //数据包被正确传输的目的地
  53. void recv_protoname_pkt(Packet*);
  54. void send_protoname_pkt();
  55. void reset_protoname_pkt_timer();
  56. public:
  57. Protoname(nsaddr_t);
  58. int command(int, const char*const*);
  59. void recv(Packet*, Handler*);
  60. //void mac_failed(Packet*);
  61. };
  62. #endif
(2)protoname.cc
  1. #include "protoname.h"
  2. #include "protoname_pkt.h"
  3. #include <random.h>
  4. #include <cmu-trace.h>
  5. #include <iostream>
  6. int hdr_protoname_pkt::offset_;
  7. static class ProtonameHeaderClass : public PacketHeaderClass {
  8. public:
  9. ProtonameHeaderClass() : PacketHeaderClass("PacketHeader/Protoname", sizeof(hdr_protoname_pkt)) {
  10. bind_offset(&hdr_protoname_pkt::offset_);
  11. }
  12. } class_rtProtoProtoname_hdr;
  13. static class ProtonameClass : public TclClass {
  14. public:
  15. ProtonameClass() : TclClass("Agent/Protoname") {}
  16. TclObject* create(int argc, const char*const* argv) {
  17. assert(argc == 5);
  18. return (new Protoname((nsaddr_t) Address::instance().str2addr(argv[4])));
  19. }
  20. } class_rtProtoProtoname;
  21. void
  22. Protoname_PktTimer::expire(Event* e) {
  23. agent_->send_protoname_pkt();
  24. agent_->reset_protoname_pkt_timer();
  25. }
  26. Protoname::Protoname(nsaddr_t id) : Agent(PT_PROTONAME), pkt_timer_(this) {
  27. bind_bool("accesible_var_", &accesible_var_);
  28. ra_addr_ = id;
  29. node_ = (MobileNode*)Node::get_node_by_address(id);
  30. }
  31. int
  32. Protoname::command(int argc, const char*const* argv) {
  33. if (argc == 2) {
  34. if (strcasecmp(argv[1], "start") == 0) {
  35. pkt_timer_.resched(0.0);
  36. return TCL_OK;
  37. }
  38. else if (strcasecmp(argv[1], "print_rtable") == 0) {
  39. if (logtarget_ != 0) {
  40. sprintf(logtarget_->pt_->buffer(), "P %f _%d_ Routing Table", CURRENT_TIME, ra_addr());
  41. logtarget_->pt_->dump();
  42. rtable_.print(logtarget_);
  43. }
  44. else {
  45. fprintf(stdout, "%f _%d_ If you want to print this routing table "
  46. "you must create a trace file in your tcl script", CURRENT_TIME, ra_addr());
  47. }
  48. return TCL_OK;
  49. }
  50. }
  51. else if (argc == 3) {
  52. // Obtains corresponding dmux to carry packets to upper layers
  53. if (strcmp(argv[1], "port-dmux") == 0) {
  54. dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);
  55. if (dmux_ == 0) {
  56. fprintf(stderr, "%s: %s lookup of %s failed\n", __FILE__, argv[1], argv[2]);
  57. return TCL_ERROR;
  58. }
  59. return TCL_OK;
  60. }
  61. // Obtains corresponding tracer
  62. else if (strcmp(argv[1], "log-target") == 0 || strcmp(argv[1], "tracetarget") == 0) {
  63. logtarget_ = (Trace*)TclObject::lookup(argv[2]);
  64. if (logtarget_ == 0)
  65. return TCL_ERROR;
  66. return TCL_OK;
  67. }
  68. }
  69. // Pass the command to the base class
  70. return Agent::command(argc, argv);
  71. }
  72. void
  73. Protoname::recv(Packet* p, Handler* h) {
  74. struct hdr_cmn* ch = HDR_CMN(p);
  75. struct hdr_ip* ih = HDR_IP(p);
  76. if (ih->saddr() == ra_addr()) {
  77. // If there exists a loop, must drop the packet
  78. if (ch->num_forwards() > 0) {
  79. drop(p, DROP_RTR_ROUTE_LOOP);
  80. return;
  81. }
  82. // else if this is a packet I am originating, must add IP header
  83. else if (ch->num_forwards() == 0)
  84. ch->size() += IP_HDR_LEN;
  85. }
  86. // If it is a protoname packet, must process it
  87. if (ch->ptype() == PT_PROTONAME)
  88. recv_protoname_pkt(p);
  89. // Otherwise, must forward the packet (unless TTL has reached zero)
  90. else {
  91. ih->ttl_--;
  92. if (ih->ttl_ == 0) {
  93. drop(p, DROP_RTR_TTL);
  94. return;
  95. }
  96. forward_data(p);
  97. }
  98. }
  99. void
  100. Protoname::recv_protoname_pkt(Packet* p) {
  101. struct hdr_ip* ih = HDR_IP(p);
  102. struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
  103. // All routing messages are sent from and to port RT_PORT,
  104. // so we check it.
  105. assert(ih->sport() == RT_PORT);
  106. assert(ih->dport() == RT_PORT);
  107. /* ... processing of protoname packet ... */
  108. // Release resources
  109. Packet::free(p);
  110. }
  111. void
  112. Protoname::send_protoname_pkt() {
  113. Packet* p = allocpkt();
  114. struct hdr_cmn* ch = HDR_CMN(p);
  115. struct hdr_ip* ih = HDR_IP(p);
  116. struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
  117. ph->pkt_src() = ra_addr();
  118. ph->pkt_len() = 7;
  119. ph->pkt_seq_num() = seq_num_++;
  120. ch->ptype() = PT_PROTONAME;
  121. ch->direction() = hdr_cmn::DOWN;
  122. ch->size() = IP_HDR_LEN + ph->pkt_len();
  123. ch->error() = 0;
  124. ch->next_hop() = IP_BROADCAST;
  125. ch->addr_type() = NS_AF_INET;
  126. ih->saddr() = ra_addr();
  127. ih->daddr() = IP_BROADCAST;
  128. ih->sport() = RT_PORT;
  129. ih->dport() = RT_PORT;
  130. ih->ttl() = IP_DEF_TTL;
  131. Scheduler::instance().schedule(target_, p, JITTER);
  132. }
  133. void
  134. Protoname::reset_protoname_pkt_timer() {
  135. pkt_timer_.resched((double)5.0);
  136. }
  137. void
  138. Protoname::forward_data(Packet* p) {
  139. struct hdr_cmn* ch = HDR_CMN(p);
  140. struct hdr_ip* ih = HDR_IP(p);
  141. if (ch->direction() == hdr_cmn::UP &&
  142. ((u_int32_t)ih->daddr() == IP_BROADCAST || ih->daddr() == ra_addr())) {
  143. dmux_->recv(p, 0);
  144. return;
  145. }
  146. else {
  147. ch->direction() = hdr_cmn::DOWN;
  148. ch->addr_type() = NS_AF_INET;
  149. if ((u_int32_t)ih->daddr() == IP_BROADCAST)
  150. ch->next_hop() = IP_BROADCAST;
  151. else {
  152. nsaddr_t next_hop = rtable_.lookup(ih->daddr());
  153. if (next_hop == IP_BROADCAST) {
  154. debug("%f: Agent %d can not forward a packet destined to %d\n",
  155. CURRENT_TIME,
  156. ra_addr(),
  157. ih->daddr());
  158. drop(p, DROP_RTR_NO_ROUTE);
  159. return;
  160. }
  161. else
  162. ch->next_hop() = next_hop;
  163. }
  164. Scheduler::instance().schedule(target_, p, 0.0);
  165. }
  166. }
  167. (3)protoname_pkt.h
  168. #ifndef __protoname_pkt_h__
  169. #define __protoname_pkt_h__
  170. #include <packet.h>
  171. #define HDR_PROTONAME_PKT(p) hdr_protoname_pkt::access(p)
  172. struct hdr_protoname_pkt {
  173. nsaddr_t pkt_src_; // Node which originated this packet
  174. u_int16_t pkt_len_; // Packet length (in bytes)
  175. u_int8_t pkt_seq_num_; // Packet sequence number
  176. inline nsaddr_t& pkt_src() { return pkt_src_; }
  177. inline u_int16_t& pkt_len() { return pkt_len_; }
  178. inline u_int8_t& pkt_seq_num() { return pkt_seq_num_; }
  179. static int offset_;
  180. inline static int& offset() { return offset_; }
  181. inline static hdr_protoname_pkt* access(const Packet* p) {
  182. return (hdr_protoname_pkt*)p->access(offset_);
  183. }
  184. };
  185. #endif
  186. (4)protoname_rtable.h
  187. #ifndef __protoname_rtable_h__
  188. #define __protoname_rtable_h__
  189. #include <trace.h>
  190. #include <map>
  191. typedef std::map<nsaddr_t, nsaddr_t> rtable_t;
  192. class protoname_rtable {
  193. rtable_t rt_;
  194. public:
  195. protoname_rtable();
  196. void print(Trace*);
  197. void clear();
  198. void rm_entry(nsaddr_t);
  199. void add_entry(nsaddr_t, nsaddr_t);
  200. nsaddr_t lookup(nsaddr_t);
  201. u_int32_t size();
  202. };
  203. #endif
  204. (5)protoname_rtable.cc
  205. #include "protoname_rtable.h"
  206. #include "ip.h"
  207. protoname_rtable::protoname_rtable() { }
  208. void
  209. protoname_rtable::print(Trace* out) {
  210. sprintf(out->pt_->buffer(), "P\tdest\tnext");
  211. out->pt_->dump();
  212. for (rtable_t::iterator it = rt_.begin(); it != rt_.end(); it++) {
  213. sprintf(out->pt_->buffer(), "P\t%d\t%d", (*it).first, (*it).second);
  214. out->pt_->dump();
  215. }
  216. }
  217. void
  218. protoname_rtable::clear() {
  219. rt_.clear();
  220. }
  221. void
  222. protoname_rtable::rm_entry(nsaddr_t dest) {
  223. rt_.erase(dest);
  224. }
  225. void
  226. protoname_rtable::add_entry(nsaddr_t dest, nsaddr_t next) {
  227. rt_[dest] = next;
  228. }
  229. nsaddr_t
  230. protoname_rtable::lookup(nsaddr_t dest) {
  231. rtable_t::iterator it = rt_.find(dest);
  232. if (it == rt_.end())
  233. return IP_BROADCAST;
  234. else
  235. return (*it).second;
  236. }
  237. u_int32_t
  238. protoname_rtable::size() {
  239. return rt_.size();
  240. }

step 3:我们需要对ns2中的一些文件进行修改,来使这个协议在tcl中被调用,

需要修改的文件有以下几个,你可以在ns目录下找到它们:

Common/packet.h

Trace/cmu-trace.h

Trace/cmu-trace.cc

Tcl/lib/ns-packet.tcl

Tcl/lib/ns-default.tcl

Tcl/lib/ns-lib.tcl

Queue/priqueue.cc

Makefile

step4:需要修改的具体内容(在需要修改的地方添加红色的字)

1.Common/packet.h (两个需要修改的地方)

1: enum packet_t {

2: PT_TCP,

3: PT_UDP,

4: PT_CBR,

5: /* ... much more packet types ... */

6: PT_PROTONAME,

7: PT_NTYPE // This MUST be the LAST one

8: };

=======================================

1: p_info() {

2: name_[PT_TCP]= "tcp";

3: name_[PT_UDP]= "udp";

4: name_[PT_CBR]= "cbr";

5: /* ... much more names ... */

6: name_[PT_PROTONAME]= "protoname";

7: }

2.Trace/cmu-trace.h (一个)

1: class CMUTrace : public Trace {

2: /* ... definitions ... */

3: private:

4: /* ... */

5: void format_aodv(Packet *p, int offset);

6: void format_protoname(Packet *p, int offset);

7: };

3.Trace/cmu-trace.cc (三个,先在最上面加头文件,在找一个合适的地方加函数)

1: #include <protoname/protoname_pkt.h>

2:

3: /* ... */

4:

5: void

6: CMUTrace::format_protoname(Packet *p, int offset)

7: {

8: struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);

9:

10: if (pt_->tagged()) {

11: sprintf(pt_->buffer() + offset,

12: "-protoname:o %d -protoname:s %d -protoname:l %d ",

13: ph->pkt_src(),

14: ph->pkt_seq_num(),

15: ph->pkt_len());

16: }

17: else if (newtrace_) {

18: sprintf(pt_->buffer() + offset,

19: "-P protoname -Po %d -Ps %d -Pl %d ",

20: ph->pkt_src(),

21: ph->pkt_seq_num(),

22: ph->pkt_len());

23: }

24: else {

25: sprintf(pt_->buffer() + offset,

26: "[protoname %d %d %d] ",

27: ph->pkt_src(),

28: ph->pkt_seq_num(),

29: ph->pkt_len());

30: }

31: }

=========================================

1: void

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

3: {

4: /* ... */

5: case PT_PING:

6: break;

7:

8: case PT_PROTONAME:

9: format_protoname(p, offset);

10: break;

11:

12: default:

13: /* ... */

14: }

4.Tcl/lib/ns-packet.tcl(一个)

1: foreach prot {

2: Protoname

3: AODV

4: ARP

5: # ...

6: NV

7: } {

8: add-packet-header $prot

9: }

 

5.Tcl/lib/ns-default.tcl(一个)

1: # ...

2: # Defaults defined for Protoname

3: Agent/Protoname set accessible_var_ true

6.Tcl/lib/ns-lib.tcl(两个)

1: Simulator instproc create-wireless-node args {

2: # ...

3: switch -exact $routingAgent_ {

4: Protoname {

5: set ragent [$self create-protoname-agent $node]

6: }

7: # ...

8: }

9: # ...

10: }

=======================================

1: Simulator instproc create-protoname-agent { node } {

2: # Create Protoname routing agent

3: set ragent [new Agent/Protoname [$node node-addr]]

4: $self at 0.0 "$ragent start"

5: $node set ragent_ $ragent

6: return $ragent

7: }

7.Queue/priqueue.cc(一个)

1: void

2: PriQueue::recv(Packet *p, Handler *h)

3: {

4: struct hdr_cmn *ch = HDR_CMN(p);

5:

6: if (Prefer_Routing_Protocols) {

7:

8: switch(ch->ptype()) {

9: case PT_DSR:

10: case PT_MESSAGE:

11: case PT_TORA:

12: case PT_AODV:

13: case PT_PROTONAME:

14: recvHighPriority(p, h);

15: break;

16:

17: default:

18: Queue::recv(p, h);

19: }

20: }

21: else {

22: Queue::recv(p, h);

23: }

24: }

8.Makefile(一个)

1: OBJ_CC = \

2: tools/random.o tools/rng.o tools/ranvar.o common/misc.o common/timer-handler.o \

3: # ...

4: protoname/protoname.o protoname/protoname_rtable.o \

5: # ...

6: $(OBJ_STL)

step 5:编译

在ns目录下输入下名命令进行编译:

$ make clean

$ touch common/packet.cc

$ make

到这里,我们添加新协议的过程就结束了。

step 6:测试

协议写完了,要用一个tcl对它进行测试,下面这是个很简单的而且可用的例子

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]Implementing a New Manet Unicast Routing Protocol in NS2,Francisco J. Ros  Pedro M. Ruiz

[2]http://ourlab.blog.sohu.com/64266258.html



您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP