2009-12-20

在ns2.33中加入新的路由协议

http://hi.baidu.com/%D2%C1%C2%E4%B5%A4/blog/item/7352e9fa9b1b97d9b48f31f2.html

Abstract

ns2 is a discrete event simulator on networking systems, with development capability to add new network protocol models. To study algorithm enhancement or brand new protocol based on ns2, it's inevitable to implement the new algorithm or protocol. Here I discuss the work flow to add a flood routing protocol against ns2.33 specifically.

0. General Work Flow

The work flow is summarized as follows:
Implement the protocol in cpp source code
Modify ns2's cpp headers and tcl files to support and recognize the new protocol
Rebuild the whole ns2 package


1. Implement the New Protocol

1.1 The Flood Routing Protocol
The protocol is essentially a flood routing protocol whose working process is summarized as follows:

To prevent permanent route loop, sequencing is employed, dropping packets from a source node with past sequence number.
On receiving a packet, build a route entry for it in a fashion of {source_node, sequence_number}

The representation and operation of the route table is implemented in 2 C++ classes: MFlood_RTEntry and MFlood_RTable, in file mflood-seqtable.{cc, h}. While the process of the protocol itself is implemented in C++ class MFlood, in file mflood.{cc, h}.

Routine work for our packet type is done in mflood-packet.h.


1.2 Routine for otcl Hook

Though our protocol has been implemented in preceding steps, we have to make it play with the Split Object mechanism of ns2. For that we have to create a TCL class.

static class MFloodclass: public TclClass {
public :
MFloodclass() : TclClass ("Agent/MFlood") {}
TclObject * create(int argc, const char* const* argv) {
std::cout<<"*********running**************"< assert(argc==5);
return (new MFlood((nsaddr_t) atoi(argv[4])));
}
}class_rtProtocolMFlood;

For all agents in ns2, the TCL classes look similar to each other. In the code above, MFlood, the constructor of our routing agent, is involved to play together within the SplitObject hierarchy.

For a further discription of the TCL class, refer to Section 3.5 "Class TclClass" in The ns Manual.


2. Modify ns2's Headers and tcl Library to Recognize our New Agent

2.1 common/packet.h

Add packet type const and an element in class p_info's name_ array.

// insert new packet types here
static const packet_t PT_MFLOOD = 61;
static packet_t PT_NTYPE = 62; // This MUST be the LAST one


class p_info {
...

static void initName()
{
...
//mflood
name_[PT_MFLOOD] = "MFlood";

name_[PT_NTYPE]= "undefined";
}
};


2.2 tcl/lib/ns-packet.tcl

Add code to support mflood's packet header.

foreach prot {

...

# Routing Protocols:
#mflood
MFlood
NV # NixVector classifier for stateless routing
rtProtoDV # distance vector routing protocol
rtProtoLS # link state routing protocol
...

} {
add-packet-header $prot
}


2.3 tcl/lib/ns-lib.tcl

The instance procedure create-wireless-node of otcl class Simulator, is invoked when creating a wireless node. Here we have to add code to support our agent.

switch -exact $routingAgent_ {
MFlood {
set ragent [$self create-mflood-agent $node]
}
DSDV {
set ragent [$self create-dsdv-agent $node]
}

Do not forget to implement procedure create-mflood-agent, which is invoked in above code, somewhere in this otcl file.


Simulator instproc create-mflood-agent { node } {
set ragent [new Agent/MFlood [$node id]]
$node set ragent_ $ragent
return $ragent
}



2.4 Makefile

Find OBJ_CC, modify the piece of code as follows (at approximately Line 326):

The instance procedure create-wireless-node of otcl class Simulator, is invoked when creating a wireless node. Here we have to add code to support our agent.

wpan/p802_15_4trace.o wpan/p802_15_4transac.o \
apps/pbc.o \
src/mflood/mflood.o src/mflood/mflood-seqtable.o \
$(OBJ_STL)


3. Checklist of Related Files

Created source code and headers include:

mflood-packet.h
mflood-seqtable.h
mflood-seqtable.cc
mflood.h
mflood.cc


Modified ns2's exsiting files are:

common/packet.h
tcl/lib/ns-lib.tcl
tcl/lib/ns-packet.tcl
Makefile

For the modified files I've created a patch file that is available for download. To apply the patch, run

cd ns2
patch -p0 -b < mflood.patch


4. Rebuild the Whole ns2 Package

At the root path of ns2, run the following commands:

make
sudo make install

If you don't use 'sudo' in your Linux OS, run it with super user priviledge instead. For ns2-allinone package, just run the installation shell script again.


5. Run in a Typical Wireless Scenario

In this scenario, which consists of 3 wireless nodes, node0 cannot reach node2 directly. Instead node1 has to forward packets from node0 to node2. Below is a snippit of the trace. (otcl script download )

s 0.500000000 _0_ AGT --- 0 cbr 500 [0 0 0 0] ------- [0:0 2:0 32 0] [0] 0 0
r 0.500000000 _0_ RTR --- 0 cbr 500 [0 0 0 0] ------- [0:0 2:0 32 0] [0] 0 0
s 0.500000000 _0_ RTR --- 0 cbr 520 [0 0 0 0] ------- [0:0 2:0 30 0] [0] 0 0
r 0.504764500 _1_ RTR --- 0 cbr 520 [0 ffffffff 0 800] ------- [0:0 2:0 30 0] [0] 1 0
f 0.506526953 _1_ RTR --- 0 cbr 520 [0 ffffffff 0 800] ------- [0:0 2:0 29 0] [0] 1 0
r 0.511571453 _0_ RTR --- 0 cbr 520 [0 ffffffff 1 800] ------- [0:0 2:0 29 0] [0] 2 0
D 0.511571453 _0_ RTR LOOP 0 cbr 520 [0 ffffffff 1 800] ------- [0:0 2:0 29 0] [0] 2 0
r 0.511571787 _2_ AGT --- 0 cbr 520 [0 ffffffff 1 800] ------- [0:0 2:0 29 0] [0] 2 0

From the trace above, we can see that node0 sends a packet out.
node0 receives a copy of the packet itself before the IP header is added (note the packet size).
node1 receives the packet and forwards it.
As a result, node0 receives it again and drops it immediately.
Finally node2, the real destination, receives it.

And we can find out, that the end to end delay from its origination to destination for this packet, is 0.511571787 - 0.5 = 0.011571787 (s) ~= 11.6 (ms)

Each packet is 500*8 bits, so a transmission delay is 500*8/2E6 = 2 (ms). And there's a random forward delay of 10ms (see mflood.h).
These are the 2 main contributors to the total delay.


6. Future Work

Run more detailed and complex scenarios using this protocol/agent. Add some interesting features for this protocol.




Biblography

[Web] Ros, F.J. and Ruiz, P.M. "Implementing a New Manet Unicast Routing Protocol in NS2 ".http://masimum.dif.um.es/nsrt-howto/html/nsrt-howto.html
[Web] Ke, C.H. "如何使得MFLOOD可以在ns-2.27的環境下運作". http://140.116.72.80/%7Esmallko/ns2/mflood.htm
[Book] 徐雷鸣. 等. "ns与网络模拟" 人民邮电出版社, 2003

沒有留言: