How to add a Protocol

Writing routing protocol is fairly easy in NS2, but for beginners it seems very difficult. Therefore, if you are new to NS2 and want to write your own routing protocol, I would strongly recommend to revise AODV source code. Because, I believe AODV source code is straightforward and fairly easy to understand due to the simplicity of the AODV protocol.
Before you begin reading this post, I assume that you have already installed NS2 on Linux. I have used version 2.34, which is current release. If you have not installed yet, DOWNLOAD HERE and INSTALL. Okey, simple requirements to write your own routing protocol
  • NS2 installed
  • You should know how to program in C/C++.
  • Optionally, shell scripting and perl.
Let’s start with creating directory of routing protocol. Goto the “$NS_ROOT/ ns-2.34/”. Create directory named as wfrp, we call it WSN Flooding-based Routing Protocol in which sink nodes periodically send a beacon message and other nodes construct route towards the sink nodes. Then nodes report to sink node every certain period using UDP protocol. Direct Diffusion may be an example of such protocol, but what we are writing is simpler and has more functionalities.
1 mkdir wfrp
In the directory we create three files : wrfp.ccwrfp.hwrfp_packet.h. Download and put these files in wfrpdirectory. I will not explain the code here, and if you don’t understand just leave comment I will try to answer.
Now, we are going to modify following files. Therefore it is better you backup these files before you start adding new protocol, so that you can easily go back.
  • $NS_ROOT/Makefile
  • $NS_ROOT/queue/priqueue.cc
  • $NS_ROOT/common/packet.h
  • $NS_ROOT/trace/cmu-trace.h
  • $NS_ROOT/trace/cmu-trace.cc
  • $NS_ROOT/tcl/lib/ns-packet.tcl
  • $NS_ROOT/tcl/lib/ns-lib.tcl
  • $NS_ROOT/tcl/lib/ns-agent.tcl
  • $NS_ROOT/tcl/lib/ns-mobilenode.tcl
Let’s start with ~/ns-allinone-2.34/ns-2.34/Makefile just add following lien at 269
1 wfrp/wfrp.o
Add following lines to ~/ns-allinone-2.34/ns-2.34/queue/priqueue.cc from line 93.
1 // WFRP patch
2 case PT_WFRP:
To define new routing protocol packet type we have to modify ~/ns-allinone-2.34/ns-2.34/common/packet.h file. We change PT_NTYPE to 63, and for our protocol PT_WFRP = 62. If you have already installed another routing protocol. Just make sure PT_NTYPE is last, and protocol number is ordered sequentially. From line 85 changes would be :
1 // WFRP packet
2 static const packet_t PT_WFRP = 62;
3
4 // insert new packet types here
5 static packet_t PT_NTYPE = 63; // This MUST be the LAST one
We make following code change at line 254 of ~/ns-allinone-2.34/ns-2.34/common/packet.h. The code is used that the packet is routing protocol packet and has high priority.
1 type == PT_AODV ||
2 type == PT_WFRP)
And at line 390 of the same file
1 // WFRP patch
2 name_[PT_WFRP] = "WFRP";
Now we will make NS2 trace our simulation and write it to *something*.tr, in order to do that we have to modify cmu-trace.h and cmu-trace.cc.
To add trace function we add following line to ~/ns-allinone-2.34/ns-2.34/trace/cmu-trace.h at line 163:
1 void    format_wfrp(Packet *p, int offset);
~/ns-allinone-2.34/ns-2.34/trace/cmu-trace.cc must be added following code at line 1071
1 // WFRP patch
2 void
3 CMUTrace::format_wfrp(Packet *p, int offset)
4 {
5     struct hdr_wfrp *wh = HDR_WFRP(p);
6     struct hdr_wfrp_beacon *wb = HDR_WFRP_BEACON(p);
7     struct hdr_wfrp_error  *we = HDR_WFRP_ERROR(p);
8
9     switch(wh->pkt_type) {
10         case WFRP_BEACON:
11
12             if (pt_->tagged()) {
13                 sprintf(pt_->buffer() + offset,
14                           "-wfrp:t %x -wfrp:h %d -wfrp:b %d -wfrp:s %d "
15                           "-wfrp:px %d -wfrp:py %d -wfrp:ts %f "
16                           "-wfrp:c BEACON ",
17                           wb->pkt_type,
18                           wb->beacon_hops,
19                           wb->beacon_id,
20                           wb->beacon_src,
21                           wb->beacon_posx,
22                           wb->beacon_posy,
23                           wb->timestamp);
24             else if (newtrace_) {
25
26                 sprintf(pt_->buffer() + offset,
27                           "-P wfrp -Pt 0x%x -Ph %d -Pb %d -Ps %d -Ppx %d -Ppy %d -Pts %f -Pc BEACON ",
28                           wb->pkt_type,
29                           wb->beacon_hops,
30                           wb->beacon_id,
31                           wb->beacon_src,
32                           wb->beacon_posx,
33                           wb->beacon_posy,
34                           wb->timestamp);
35
36             else {
37
38                 sprintf(pt_->buffer() + offset,
39                           "[0x%x %d %d [%d %d] [%d %f]] (BEACON)",
40                           wb->pkt_type,
41                           wb->beacon_hops,
42                           wb->beacon_id,
43                           wb->beacon_src,
44                           wb->beacon_posx,
45                           wb->beacon_posy,
46                           wb->timestamp);
47             }
48             break;
49
50         case WFRP_ERROR:
51             // TODO: need to add code
52             break;
53
54         default:
55 #ifdef WIN32
56             fprintf(stderr,
57                       "CMUTrace::format_wfrp: invalid WFRP packet typen");
58 #else
59             fprintf(stderr,
60                       "%s: invalid WFRP packet typen", __FUNCTION__);
61 #endif
62             abort();
63     }
64 }
Now we will modify tcl files to create routing agent. First we define protocol name to use in tcl file. It would done by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-packet.tcl @ line 172
1 # WFRP patch
2 WFRP
Now we set routing agent by modifying ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-lib.tcl @ line 633
1 WFRP {
2   set ragent [$self create-wfrp-agent $node]
3 }
From line 860 of the same file following code should be added.
1 Simulator instproc create-wfrp-agent { node } {
2    #  Create WFRP routing agent
3    set ragent [new Agent/WFRP [$node node-addr]]
4    $self at 0.0 "$ragent start"
5    $node set ragent_ $ragent
6    return $ragent
7 }
Now we will set port numbers of routing agent. sport is source port, dport is destination port. Modify ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-agent.tcl line 202
1 Agent/WFRP instproc init args {
2   $self next $args
3 }
4
5 Agent/WFRP set sport_   0
6 Agent/WFRP set dport_   0
Frankly speaking I have no idea why I have to add following things. But I believe it should be done according to some tutorial : ~/ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl line 201
1 # Special processing for WFRP
2 set wfrponly [string first "WFRP" [$agent info class]]
3 if {$wfrponly != -1 } {
4   $agent if-queue [$self set ifq_(0)]   ;# ifq between LL and MAC
5 }
We are done. got to ~/ns-allinone-2.34/ns-2.34/ directory and do
1 make clean
2 make
When the compile is finished, you can test using wfrp_802_15_4.tcl file as :
1 ns wfrp_802_15_4.tcl
In this test the NODE 0 is sink node, starts sending beacon 1 second after simulation i started, and NODE 10 is reporting node. It starts sending report over CBR/UDP at 5.0 seconds (after simulation is started). Report interval is 2 seconds.
To remove debugging WFRP, uncomment #define DEBUG (line 36 of wfrp.cc & re-make it).