How to Begin Implement Greedy Perimeter Routing in NS3
To implement Greedy Perimeter Stateless Routing (GPSR) using NS3 that is a geographical routing protocol frequently utilized within wireless ad hoc and sensor networks. It leverages node locations for packet forwarding and switches among the greedy forwarding and perimeter forwarding if greedy methodology be unsuccessful.
Below is a sequential technique to execute GPSR Routing using NS3:
Steps to Begin Implement GPSR Routing in NS3
Step 1: Understand GPSR
- Key Features:
- Greedy Forwarding:
- Packets are sent to the neighbor nearby to the destination.
- Perimeter Forwarding:
- Once a packet meets a local minimum which means it has no closer neighbors, the protocol changes to limit forwarding for passing through the network leveraging the right-hand rule.
- Greedy Forwarding:
- Data Structures:
- Neighbor Table: It saves the neighbors locations.
- Geographical Information: Nodes require its places and organizes destination.
Step 2: Set Up NS3 Environment
- Create a Protocol Directory:
- Make a protocol directory in src/, for example src/gpsr-routing/.
- Add Necessary Files:
- gpsr-routing-helper.h and gpsr-routing-helper.cc: Helper classes.
- gpsr-routing-protocol.cc: Execution file.
- gpsr-routing-protocol.h: Header file for GPSR.
- Update Build System:
- We need to change wscript in src/ with the new module for updating the system.
Step 3: Design GPSR Protocol
Define the Protocol Class
For prolonging the Ipv4RoutingProtocol class, we can execute GPSR protocol.
Header File (gpsr-routing-protocol.h)
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/socket.h”
#include “ns3/mobility-module.h”
#include <map>
class GpsrRoutingProtocol : public ns3::Ipv4RoutingProtocol {
public:
static ns3::TypeId GetTypeId (void);
GpsrRoutingProtocol ();
virtual ~GpsrRoutingProtocol ();
// Override Ipv4RoutingProtocol methods
virtual ns3::Ptr<ns3::Ipv4Route> RouteOutput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<ns3::NetDevice> oif,
ns3::Socket::SocketErrno &sockerr);
virtual bool RouteInput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<const ns3::NetDevice> idev,
ns3::UnicastForwardCallback ucb,
ns3::MulticastForwardCallback mcb,
ns3::LocalDeliverCallback lcb,
ns3::ErrorCallback ecb);
void AddNeighbor (ns3::Ipv4Address neighbor, ns3::Vector position);
void RemoveNeighbor (ns3::Ipv4Address neighbor);
private:
struct NeighborEntry {
ns3::Ipv4Address address;
ns3::Vector position;
};
ns3::Vector GetNodePosition ();
ns3::Ipv4Address GreedyForward (ns3::Vector destination);
ns3::Ipv4Address PerimeterForward (ns3::Vector destination);
std::map<ns3::Ipv4Address, NeighborEntry> m_neighborTable; // Neighbor table
ns3::Ipv4Address m_selfAddress; // Node’s own IP address
};
Implement Core Functions
Neighbor Management
Sustain a routing neighbors table and its locations.
void GpsrRoutingProtocol::AddNeighbor (ns3::Ipv4Address neighbor, ns3::Vector position) {
NeighborEntry entry = {neighbor, position};
m_neighborTable[neighbor] = entry;
}
void GpsrRoutingProtocol::RemoveNeighbor (ns3::Ipv4Address neighbor) {
m_neighborTable.erase(neighbor);
}
Get Node Position
Recover the node’s present location to utilize MobilityModel of NS3.
ns3::Vector GpsrRoutingProtocol::GetNodePosition () {
ns3::Ptr<ns3::MobilityModel> mobility = GetObject<ns3::MobilityModel>();
return mobility->GetPosition();
}
Greedy Forwarding
Transmit the packets to the neighbor which is nearest to the destination.
ns3::Ipv4Address GpsrRoutingProtocol::GreedyForward (ns3::Vector destination) {
ns3::Vector selfPosition = GetNodePosition();
double minDistance = std::numeric_limits<double>::max();
ns3::Ipv4Address bestNeighbor;
for (const auto &entry : m_neighborTable) {
double distance = CalculateDistance(selfPosition, entry.second.position);
if (distance < minDistance) {
minDistance = distance;
bestNeighbor = entry.second.address;
}
}
return bestNeighbor;
}
Perimeter Forwarding
Pass through the network perimeter with the help of right-hand rule.
ns3::Ipv4Address GpsrRoutingProtocol::PerimeterForward (ns3::Vector destination) {
// Implement perimeter forwarding logic using the right-hand rule
// Traverse the graph based on edges and neighbors
ns3::Ipv4Address nextHop;
// …
return nextHop;
}
Route Output
Manage the route outgoing packets and compute the next hop.
ns3::Ptr<ns3::Ipv4Route> GpsrRoutingProtocol::RouteOutput (
ns3::Ptr<const ns3::Packet> packet,
const ns3::Ipv4Header &header,
ns3::Ptr<ns3::NetDevice> oif,
ns3::Socket::SocketErrno &sockerr) {
ns3::Vector destination = …; // Extract destination coordinates
ns3::Ipv4Address nextHop = GreedyForward(destination);
if (nextHop.IsEqual(ns3::Ipv4Address::GetAny())) {
nextHop = PerimeterForward(destination); // Switch to perimeter mode
}
if (!nextHop.IsEqual(ns3::Ipv4Address::GetAny())) {
ns3::Ptr<ns3::Ipv4Route> route = ns3::Create<ns3::Ipv4Route> ();
route->SetGateway(nextHop);
route->SetOutputDevice(oif);
return route;
}
sockerr = ns3::Socket::ERROR_NOROUTETOHOST;
return nullptr;
}
Step 4: Write a Helper Class
Make a helper class to incorporate the GPSR to simulations.
#include “gpsr-routing-protocol.h”
class GpsrRoutingHelper {
public:
void Install (ns3::NodeContainer nodes) {
for (auto it = nodes.Begin(); it != nodes.End(); ++it) {
ns3::Ptr<GpsrRoutingProtocol> protocol = ns3::CreateObject<GpsrRoutingProtocol>();
(*it)->AggregateObject(protocol);
}
}
};
Step 5: Write a Simulation Script
Example Simulation Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/mobility-module.h”
#include “gpsr-routing-helper.h”
using namespace ns3;
int main (int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create (10);
InternetStackHelper stack;
stack.Install (nodes);
GpsrRoutingHelper gpsrHelper;
gpsrHelper.Install(nodes);
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (10.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,
“Bounds”, RectangleValue (Rectangle (0, 50, 0, 50)));
mobility.Install (nodes);
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Step 6: Compile and Run
- Build the Protocol:
./waf configure
./waf build
- Run the Simulation:
./waf –run your-script
Step 7: Analyze and Extend
- Tracing:
- Examine the packet forwarding utilising AsciiTraceHelper or PcapHelper.
- Enhancements:
- Integrate support for neighbor updates and mobility.
- Execute the energy-aware forwarding.
Comprehensive guidance for implementing the Greedy Perimeter Routing has been shared in this manual using NS3 simulation tool. We are prepared to expand it with further details based on your requirements.