To implement the greedy perimeter stateless routing (GPSR) in ns3, the GPSR is geographical routing protocol that is used for wireless sensor networks (WSNs) and Mobile ad hoc networks (MANETs). It helps to positions of routers and a packets destination to create the forwarding decisions.
Here the given below are the implementation procedures on how to implement the GPSR in ns3:
Steps to Implement GPSR in ns3
- Install ns3: Make certain ns3 is installed in the computer.
- Set Up ns3 Workspace: To make a working directory for the project.
- Create a New Module for GPSR:
- In the src directory of ns-3, create a new directory for your GPSR implementation.
- Create the necessary files: .cc, .h, and helper files.
- Define the GPSR Protocol:
- Create a GPSR class that inherits from the base RoutingProtocol class in ns3.
- Implement the key functions for the GPSR protocol, such as Start, Stop, Recv, SendPacket, and neighbor management.
- Position Handling:
- To get and store the positions of nodes by implementing their mechanism by using the mobility model in ns3.
- Incorporate GPSR with ns3 position handling to create forwarding decisions based on the geographical position.
- Greedy Forwarding:
- Implement the greedy forwarding strategy: the packet is forwarded to the neighbor closest to the destination.
- Handle the case where greedy forwarding fails (no neighbor is closer to the destination).
- Perimeter Mode:
- Implement the perimeter routing mode by the right-hand rule to forward packets around a void.
- Switch among greedy mode and perimeter mode as essential.
- Integrate with ns3’s Routing System:
- Modify the RoutingHelper class to include GPSR.
- Register GPSR as a routing protocol in ns3.
- Testing and Validation:
- Create a test script to simulate a network using GPSR.
- Validate the performance and correctness of the GPSR implementation by running various scenarios.
Sample Code Structure
The below is the sample snippet that demonstrates how GPSR implement in ns3:
// src/gpsr/model/gpsr-routing-protocol.h
#ifndef GPSR_ROUTING_PROTOCOL_H
#define GPSR_ROUTING_PROTOCOL_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4.h”
#include “ns3/ipv4-l3-protocol.h”
#include “ns3/ipv4-route.h”
#include “ns3/node.h”
#include “ns3/net-device.h”
#include “ns3/socket.h”
#include “ns3/timer.h”
namespace ns3 {
class GpsrRoutingProtocol : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
GpsrRoutingProtocol ();
virtual ~GpsrRoutingProtocol ();
// Inherited from Ipv4RoutingProtocol
virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
virtual bool RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb,LocalDeliverCallback lcb, ErrorCallback ecb);
virtual void NotifyInterfaceUp (uint32_t interface);
virtual void NotifyInterfaceDown (uint32_t interface);
virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);
virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);
virtual void SetIpv4 (Ptr<Ipv4> ipv4);
void RecvGpsr (Ptr<Socket> socket);
private:
void Start ();
void Stop ();
Ptr<Ipv4> m_ipv4;
std::map<Ptr<Socket>, Ipv4InterfaceAddress> m_socketAddresses;
};
} // namespace ns3
#endif // GPSR_ROUTING_PROTOCOL_H
Helper Class
// src/gpsr/helper/gpsr-helper.h
#ifndef GPSR_HELPER_H
#define GPSR_HELPER_H
#include “ns3/ipv4-routing-helper.h”
#include “ns3/object-factory.h”
#include “ns3/gpsr-routing-protocol.h”
namespace ns3 {
class GpsrHelper : public Ipv4RoutingHelper
{
public:
GpsrHelper ();
GpsrHelper (const GpsrHelper &);
GpsrHelper &operator= (const GpsrHelper &);
virtual ~GpsrHelper ();
virtual GpsrHelper* Copy (void) const;
virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
};
} // namespace ns3
#endif // GPSR_HELPER_H
Example Simulation Script
At this point we provide the sample script for GPSR in ns3 simulator:
// examples/gpsr-simulation.cc
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/mobility-module.h”
#include “ns3/internet-module.h”
#include “ns3/gpsr-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“GpsrSimulation”);
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (10);
MobilityHelper mobility;
mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“DeltaX”, DoubleValue (5.0),
“DeltaY”, DoubleValue (10.0),
“GridWidth”, UintegerValue (5),
“LayoutType”, StringValue (“RowFirst”));
mobility.SetMobilityModel(“ns3::RandomWalk2dMobilityModel”,”Bounds”,RectangleValue (Rectangle (-50, 50, -50, 50)));
mobility.Install (nodes);
InternetStackHelper stack;
GpsrHelper gpsr;
stack.SetRoutingHelper (gpsr);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Your simulation code here
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Running the Simulation
To run the simulation, compile ns-3 with your new GPSR module and the example script:
./waf configure –enable-examples
./waf build
./waf –run gpsr-simulation
Overall, we had implemented and executed the greedy perimeter stateless routing and it has created the forwarding decision in the network.
Implementing the greedy perimeter stateless routing (GPSR) in ns3 will be properly executed by ns3simulation.com team of developers.