How to Implement Mesh Protocols in NS2

To implement the mesh protocols in ns2, we have to generate or simulate routing protocols developed for wireless mesh networks. It is commonly build to manage the distinct threats of mesh networking includes multi-hop communication, dynamic topology, and efficient route discovery and maintenance.

One of the well-known mesh protocols is the Hybrid Wireless Mesh Protocol (HWMP), which is part of the IEEE 802.11s standard. However, for educational purposes, you might execute a simpler mesh protocol or adjust an existing one like AODV (Ad hoc On-Demand Distance Vector).

In the given below, we will help you to accomplish this protocol in ns2:

Step-by-Step Implementation:

Step 1: Define the Protocol Requirements

Decide on the certain mesh protocol you want to implement. This could include:

  • Proactive Mesh Protocol: Incessantly updates routes (similar to OLSR).
  • Reactive Mesh Protocol: Finds routes on-demand (similar to AODV).
  • Hybrid Mesh Protocol: Integrates proactive and reactive tactics (similar to HWMP).

Step 2: Set Up NS2

Make sure to install NS2. Authenticate the installation by executing a basic simulation. If you are executing a new protocol, it may be essential to alter or expand NS2’s existing functionality.

Step 3: Implement the Mesh Protocol in C++

  1. Create the Mesh Protocol Agent Class

Here’s a simplified structure for executing a mesh protocol. The example delivered below plans the skeleton for a simple mesh routing protocol that incorporates reactive route discovery with proactive route maintenance.

#include <agent.h>

#include <packet.h>

#include <trace.h>

#include <address.h>

#include <map>

class MeshRoutingAgent : public Agent {

public:

MeshRoutingAgent();

void recv(Packet* p, Handler* h);

void sendRouteRequest(int dest);

void sendRouteReply(int dest, int nextHop);

void maintainRoutes();

void handleRouteError(int dest);

void forwardPacket(Packet* p);

protected:

std::map<int, int> routingTable_; // Destination -> Next Hop

std::map<int, int> routeRequestTable_; // Destination -> Request ID

};

// Constructor

MeshRoutingAgent::MeshRoutingAgent() : Agent(PT_UDP) {

// Initialization code here

}

// Packet reception

void MeshRoutingAgent::recv(Packet* p, Handler* h) {

hdr_ip* iph = hdr_ip::access(p);

int dest = iph->dst();

if (routingTable_.find(dest) != routingTable_.end()) {

forwardPacket(p);

} else {

sendRouteRequest(dest);

}

}

// Send a route request to discover a path

void MeshRoutingAgent::sendRouteRequest(int dest) {

// Implement route request logic

Packet* p = allocpkt();

hdr_ip* iph = hdr_ip::access(p);

// Fill in RREQ data and send to neighbors

send(p, 0);

}

// Send a route reply in response to a route request

void MeshRoutingAgent::sendRouteReply(int dest, int nextHop) {

// Implement route reply logic

Packet* p = allocpkt();

hdr_ip* iph = hdr_ip::access(p);

// Fill in RREP data and send to the requester

send(p, 0);

}

// Maintain routes proactively

void MeshRoutingAgent::maintainRoutes() {

// Implement route maintenance (e.g., periodic hello messages)

// Send periodic updates to neighbors

}

// Handle route errors (e.g., link breakages)

void MeshRoutingAgent::handleRouteError(int dest) {

// Implement logic for handling route errors

}

// Forward packet to the next hop

void MeshRoutingAgent::forwardPacket(Packet* p) {

hdr_ip* iph = hdr_ip::access(p);

int nextHop = routingTable_[iph->dst()];

// Forward the packet to the next hop

send(p, 0);

}

  1. Integrate the Protocol into NS2
  1. Modify the Makefile: Attach new MeshRoutingAgent class to the NS2 Makefile so that it gets compiled with the remains of the simulator.
  2. Recompile NS2:

make clean

make

Step 4: Create a Tcl Script to Simulate the Mesh Protocol

After the compilation is done, set up a Tcl script to recreate a network using your custom protocol.

Example Tcl Script:

# Create a simulator object

set ns [new Simulator]

# Define the topology

set val(chan)   Channel/WirelessChannel

set val(prop)   Propagation/TwoRayGround

set val(netif)  Phy/WirelessPhy

set val(mac)    Mac/802_11

set val(ifq)    Queue/DropTail/PriQueue

set val(ifqlen) 50

set val(nn)     10

set val(x)      1000

set val(y)      1000

set val(stop)   100.0

set val(rp)     MeshRouting   ;# Mesh Routing Protocol

# Initialize the topology object

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

# Create the God object

create-god $val(nn)

# Configure the nodes

$ns node-config -adhocRouting $val(rp) \

-llType $val(ll) \

-macType $val(mac) \

-ifqType $val(ifq) \

-ifqLen $val(ifqlen) \

-antType $val(ant) \

-propType $val(prop) \

-phyType $val(netif) \

-channelType $val(chan) \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace ON \

-movementTrace ON

# Create nodes

for {set i 0} {$i < $val(nn)} {incr i} {

set node_($i) [$ns node]

}

# Define node positions and movement model

$node_(0) set X_ 100.0; $node_(0) set Y_ 200.0

$node_(1) set X_ 200.0; $node_(1) set Y_ 300.0

$node_(2) set X_ 300.0; $node_(2) set Y_ 400.0

$node_(3) set X_ 400.0; $node_(3) set Y_ 500.0

$node_(4) set X_ 500.0; $node_(4) set Y_ 600.0

# Start the simulation

$ns at 0.0 “$node_(0) sendRouteRequest 1”

$ns at 10.0 “$node_(1) sendRouteReply 0 1”

# Simulation end

$ns at $val(stop) “stop”

$ns at $val(stop) “exit 0”

proc stop {} {

global ns

$ns flush-trace

exit 0

}

# Run the simulation

$ns run

Step 5: Run the Simulation

  1. Store the Tcl script (mesh_protocol_simulation.tcl).
  2. Open a terminal and route to the directory where you logged the Tcl script.
  3. Execute the simulation using the following command:

ns mesh_protocol_simulation.tcl

Step 6: Analyze the Results

  • Assess the performance of mesh protocol based on the route discovery time, packet delivery ratio, and network overhead by using trace files and the network animator (NAM).
  • Compute how well your protocol manages dynamic topologies, node mobility, and multi-hop communication.

Additional Considerations

  • Proactive vs. Reactive: According to the protocol design, examine both proactive (regular route updates) and reactive (on-demand route discovery) behaviors to define their effectiveness in mesh network situations.
  • Scalability: Compute the scalability and robustness of the protocol by simulating larger mesh networks with more nodes and changing levels of mobility.
  • Comparison: Compare your custom mesh protocol with available protocols like AODV, OLSR, or HWMP to analyze its relative performance.

In this set up, we delivered the step-by-step demonstration which makes it easier for you to implement the Mesh Protocols using ns2 tool. We also provide the examples, simulation set up and evaluation process of it. If needed, we will offer any extra information on this protocol.

Our developers  conduct performance analysis tailored to your needs. Our expertise in Mesh Protocols within NS2 implementation ensures you receive outstanding results. Feel free to reach out to ns2project.com for more information.