To implement the Clusterhead Gateway Switch Routing (CGSR) requires making a custom routing protocol by crucial for its features that consists to form a clusterheads, gateways, member nodes and to handle their interaction. We work on all types of Clusterhead Gateway Switch Routing protocols. CGSR is a structured routing protocol considered for mobile ad-hoc networks (MANETs).
Here is a procedure to simulate the general version CGSR in ns3 environment:
Step-by-Step Guide to Implementing CGSR in ns-3
- Set Up Your Environment
Make sure ns3 is installed. If have not installed download it from the website.
- Create a New ns-3 Module
Create a new module for CGSR in the src directory of your ns-3 installation. This involves creating the necessary directory structure and files.
cd ns-3.xx
cd src
mkdir -p cgsr/model
mkdir -p cgsr/helper
3. Create the CGSR Header File
Create the CGSR routing protocol header file cgsr-routing-protocol.h in the model directory.
#ifndef CGSR_ROUTING_PROTOCOL_H
#define CGSR_ROUTING_PROTOCOL_H
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/ipv4-address.h”
#include “ns3/timer.h”
#include “ns3/mobility-model.h”
#include “ns3/node.h”
#include “ns3/net-device.h”
#include “ns3/ipv4.h”
#include “ns3/ipv4-routing-table-entry.h”
#include <map>
namespace ns3 {
class CgsrRoutingProtocol : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
CgsrRoutingProtocol ();
virtual ~CgsrRoutingProtocol ();
// 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);
virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const;
private:
Ptr<Ipv4> m_ipv4;
std::map<Ipv4Address, Ipv4RoutingTableEntry> m_routingTable;
std::map<Ipv4Address, Ipv4Address> m_clusterheads; // Maps nodes to their clusterheads
std::map<Ipv4Address, std::vector<Ipv4Address>> m_clusters; // Maps clusterheads to their cluster members
Ptr<NetDevice> GetNetDevice (uint32_t interface);
Ptr<MobilityModel> GetMobilityModel (Ptr<NetDevice> netDevice);
Ipv4Address GetNextHop (Ipv4Address dest);
void CreateClusters ();
};
} // namespace ns3
#endif /* CGSR_ROUTING_PROTOCOL_H */
4. Create the CGSR Source File
Create the CGSR routing protocol source file cgsr-routing-protocol.cc in the model directory.
#include “cgsr-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-route.h”
#include “ns3/simulator.h”
#include <algorithm>
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“CgsrRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (CgsrRoutingProtocol);
TypeId
CgsrRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::CgsrRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName(“Internet”)
.AddConstructor<CgsrRoutingProtocol> ();
return tid;
}
CgsrRoutingProtocol::CgsrRoutingProtocol ()
{
}
CgsrRoutingProtocol::~CgsrRoutingProtocol ()
{
}
void
CgsrRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)
{
m_ipv4 = ipv4;
}
Ptr<NetDevice>
CgsrRoutingProtocol::GetNetDevice (uint32_t interface)
{
return m_ipv4->GetNetDevice (interface);
}
Ptr<MobilityModel>
CgsrRoutingProtocol::GetMobilityModel (Ptr<NetDevice> netDevice)
{
return netDevice->GetNode ()->GetObject<MobilityModel> ();
}
void
CgsrRoutingProtocol::CreateClusters ()
{
// Simplified example of creating clusters
// In a real implementation, clusterhead election algorithms and dynamic clustering would be used
Ipv4Address clusterhead1 = Ipv4Address (“10.1.1.1”);
Ipv4Address clusterhead2 = Ipv4Address (“10.1.1.2”); m_clusters[clusterhead1].push_back(Ipv4Address (“10.1.1.1”)); m_clusters[clusterhead1].push_back(Ipv4Address (“10.1.1.3”)); m_clusters[clusterhead1].push_back(Ipv4Address (“10.1.1.4”));
m_clusters[clusterhead2].push_back(Ipv4Address (“10.1.1.2”));
m_clusters[clusterhead2].push_back(Ipv4Address (“10.1.1.5”));
m_clusters[clusterhead2].push_back(Ipv4Address (“10.1.1.6”));
for (auto &cluster : m_clusters)
{
for (auto &member : cluster.second)
{
m_clusterheads[member] = cluster.first;
}
}
}
Ipv4Address
CgsrRoutingProtocol::GetNextHop (Ipv4Address dest)
{
// Simplified example of next hop selection based on CGSR
Ipv4Address nextHop = Ipv4Address::GetBroadcast ();
if (m_clusterheads.find(dest) != m_clusterheads.end())
{
Ipv4Address clusterhead = m_clusterheads[dest];
if (clusterhead == m_ipv4->GetAddress (1, 0).GetLocal ())
{
// Destination is in the same cluster
nextHop = dest;
}
else
{
// Route through the clusterhead
nextHop = clusterhead;
}
}
return nextHop;
}
Ptr<Ipv4Route>
CgsrRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
{
Ipv4Address dest = header.GetDestination ();
Ipv4Address nextHop = GetNextHop (dest);
if (nextHop == Ipv4Address::GetBroadcast ())
{
sockerr = Socket::ERROR_NOROUTETOHOST;
return nullptr;
}
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
route->SetDestination (dest);
route->SetGateway (nextHop);
route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));
return route;
}
bool
CgsrRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)
{
Ipv4Address dest = header.GetDestination ();
Ipv4Address nextHop = GetNextHop (dest);
if (nextHop == Ipv4Address::GetBroadcast ())
{
ecb (p, header, Socket::ERROR_NOROUTETOHOST);
return false;
}
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
route->SetDestination (dest);
route->SetGateway (nextHop);
route->SetOutputDevice (m_ipv4->GetNetDevice (m_ipv4->GetInterfaceForAddress (nextHop)));
ucb (route, p, header);
return true;
}
void
CgsrRoutingProtocol::NotifyInterfaceUp (uint32_t interface)
{
}
void
CgsrRoutingProtocol::NotifyInterfaceDown (uint32_t interface)
{
}
void
CgsrRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
}
void
CgsrRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
}
void
CgsrRoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const
{
*stream->GetStream () << “CGSR Routing Table” << std::endl;
for (auto const &entry : m_routingTable)
{
*stream->GetStream () << entry.first << ” -> ” << entry.second.GetGateway () << ” via ” << entry.second.GetInterface () << std::endl;
}
}
} // namespace ns3
5. Define CGSR Helper
Create the CGSR helper header file cgsr-helper.h in the helper directory.
#ifndef CGSR_HELPER_H
#define CGSR_HELPER_H
#include “ns3/ipv4-routing-helper.h”
#include “cgsr-routing-protocol.h”
namespace ns3 {
class CgsrHelper : public Ipv4RoutingHelper
{
public:
CgsrHelper ();
virtual ~CgsrHelper ();
CgsrHelper* Copy (void) const;
virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
};
} // namespace ns3
#endif /* CGSR_HELPER_H */
Create the CGSR helper source file cgsr-helper.cc in the helper directory.
#include “cgsr-helper.h”
#include “ns3/node.h”
#include “ns3/ipv4.h”
namespace ns3 {
CgsrHelper::CgsrHelper ()
{
}
CgsrHelper::~CgsrHelper ()
{
}
CgsrHelper*
CgsrHelper::Copy (void) const
{
return new CgsrHelper (*this);
}
Ptr<Ipv4RoutingProtocol>
CgsrHelper::Create (Ptr<Node> node) const
{
Ptr<CgsrRoutingProtocol> cgsrRouting = CreateObject<CgsrRoutingProtocol> ();
node->AggregateObject (cgsrRouting);
return cgsrRouting;
}
} // namespace ns3
6. Update CMakeLists.txt
Add the new CGSR module to the ns-3 build system. Edit src/CMakeLists.txt and add the following line:
add_subdirectory (cgsr)
Create src/cgsr/CMakeLists.txt with the following content:
ns3_add_library (cgsr
model/cgsr-routing-protocol.cc
helper/cgsr-helper.cc
)
target_link_libraries (cgsr)
7. Set Up the Network Topology
Create a simulation script in the scratch directory to use the CGSR protocol.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include “ns3/mobility-module.h”
#include “cgsr-helper.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (6);
// Set up WiFi
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Set up mobility model
MobilityHelper mobility;
Ptr<UniformRandomVariable> random = CreateObject<UniformRandomVariable> ();
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“MaxX”, DoubleValue (100.0),
“MaxY”, DoubleValue (100.0));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::ConstantRandomVariable[Constant=20.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=0.0]”),
“PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator[MinX=0.0|MinY=0.0|MaxX=100.0|MaxY=100.0]”));
mobility.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
CgsrHelper cgsr;
stack.SetRoutingHelper (cgsr);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (5));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (5), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“cgsr-simulation.tr”));
wifiPhy.EnablePcapAll (“cgsr-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
8. Build and Run the Simulation
After writing your script, you need to build and run it.
./waf build
./waf –run scratch/cgsr-simulation
9. Analyze the Results
After running the simulation, you can analyze the results using the generated trace files (cgsr-simulation.tr and cgsr-simulation-0-0.pcap).
Complete Example Script for CGSR
The given below is the sample script to complete the script for CGSR in ns3:
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “cgsr-helper.h”
using namespace ns3;
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
NodeContainer nodes;
nodes.Create (6);
// Set up WiFi
WifiHelper wifi;
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiMacHelper wifiMac;
wifiMac.SetType (“ns3::AdhocWifiMac”);
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
// Set up mobility model
MobilityHelper mobility;
Ptr<UniformRandomVariable> random = CreateObject<UniformRandomVariable> ();
mobility.SetPositionAllocator (“ns3::RandomRectanglePositionAllocator”,
“MinX”, DoubleValue (0.0),
“MinY”, DoubleValue (0.0),
“MaxX”, DoubleValue (100.0),
“MaxY”, DoubleValue (100.0));
mobility.SetMobilityModel (“ns3::RandomWaypointMobilityModel”,
“Speed”, StringValue (“ns3::ConstantRandomVariable[Constant=20.0]”),
“Pause”, StringValue (“ns3::ConstantRandomVariable[Constant=0.0]”),
“PositionAllocator”, StringValue (“ns3::RandomRectanglePositionAllocator[MinX=0.0|MinY=0.0|MaxX=100.0|MaxY=100.0]”));
mobility.Install (nodes);
// Install the internet stack on nodes
InternetStackHelper stack;
CgsrHelper cgsr;
stack.SetRoutingHelper (cgsr);
stack.Install (nodes);
// Assign IP addresses to the devices
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Set up applications (e.g., a UDP echo server and client)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (5));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (5), 9);
echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));
echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));
echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“cgsr-simulation.tr”));
wifiPhy.EnablePcapAll (“cgsr-simulation”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Overall we had implemented the CGSR to analyse their performance in ns3 environment and also we help to provide further information related to CGSR routing protocol. For comparative analysis support on all areas of CGSR you can contact us.