How to Begin Implement a CGSR Protocol in NS3
To create the Clusterhead Gateway Switch Routing (CGSR) is a cluster-based routing protocol for mobile ad hoc networks (MANETs). It classifies the network in clusters by selected clusterheads which handle the communication. Executing the CGSR in ns3 tool needs a build of custom routing protocol because CGSR is not natively helps in ns3.
Here’s how you can implement CGSR in ns-3:
Steps to Begin Implement a CGSR Protocol in NS3
- Understand CGSR Protocol
- Key Features:
- Nodes are structured in clusters, every through a clusterhead.
- Communication among clusters follows through gateways such as nodes connected to multiple clusterheads.
- Clusterheads has handled the routing information for their cluster.
- Responsibilities:
- Clusterhead: It maintains the intra-cluster routing.
- Gateway: Enables the inter-cluster communication.
- Nodes: Forwarding the packets for their clusterhead.
- Set up ns-3 Environment
- Install ns-3:
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
./build.py
- Validate the installation:
./ns3 run hello-simulator
- Plan the Network Topology
- Components:
- Nodes are making the clusters.
- Clusterheads are handling the intra-cluster routing.
- Gateways for inter-cluster communication.
- Topology:
- The network topology has fixed or dynamic clustering of nodes through mobility models.
- Implement the CGSR Protocol
- Define the Protocol Class
- Generate a new class inheriting from Ipv4RoutingProtocol:
#include “ns3/ipv4-routing-protocol.h”
#include “ns3/mobility-module.h”
class CgsrRouting : public ns3::Ipv4RoutingProtocol {
public:
CgsrRouting();
virtual ~CgsrRouting();
Ptr<Ipv4Route> RouteOutput(Ptr<Packet> packet, const Ipv4Header &header,
Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) override;
bool RouteInput(Ptr<const Packet> packet, const Ipv4Header &header,
Ptr<const NetDevice> idev, UnicastForwardCallback ucb,
MulticastForwardCallback mcb, LocalDeliverCallback lcb,
ErrorCallback ecb) override;
void ClusterFormation();
void IntraClusterRouting();
void InterClusterRouting();
private:
void ElectClusterheads();
Ptr<Ipv4Route> GetRouteToClusterhead(ns3::Ipv4Address dest);
Ptr<Ipv4Route> GetRouteViaGateway(ns3::Ipv4Address dest);
};
- Cluster Formation:
- Use a clustering procedure for sample Lowest ID or Highest Connectivity.
- Continue the table for cluster memberships and clusterhead tasks.
- Intra-Cluster Routing:
- Direct communication used in a cluster.
- Inter-Cluster Routing:
- Route packets through gateways for other clusters.
- Create a Helper Class
- Describe a assistant for installing CGSR:
class CgsrHelper : public ns3::Ipv4RoutingHelper {
public:
CgsrHelper() {}
Ptr<Ipv4RoutingProtocol> Create(Ptr<Node> node) const override {
return CreateObject<CgsrRouting>();
}
};
- Install the assistant in the replication script:
CgsrHelper cgsrHelper;
ns3::InternetStackHelper stack;
stack.SetRoutingHelper(cgsrHelper);
stack.Install(nodes);
- Set Up the Simulation
- Define Nodes
ns3::NodeContainer nodes;
nodes.Create(10); // Create 10 nodes
- Set Up Mobility
- Random Mobility Model:
ns3::MobilityHelper mobility;
mobility.SetMobilityModel(“ns3::RandomWaypointMobilityModel”,
“Speed”, ns3::StringValue(“ns3::UniformRandomVariable[Min=5.0|Max=20.0]”),
“Pause”, ns3::StringValue(“ns3::ConstantRandomVariable[Constant=2.0]”),
“PositionAllocator”, ns3::StringValue(“ns3::RandomRectanglePositionAllocator”));
mobility.Install(nodes);
- Static Placement (Optional):
ns3::MobilityHelper mobility;
mobility.SetPositionAllocator(“ns3::GridPositionAllocator”,
“MinX”, ns3::DoubleValue(0.0),
“MinY”, ns3::DoubleValue(0.0),
“DeltaX”, ns3::DoubleValue(50.0),
“DeltaY”, ns3::DoubleValue(50.0),
“GridWidth”, ns3::UintegerValue(5),
“LayoutType”, ns3::StringValue(“RowFirst”));
mobility.Install(nodes);
- Set Up Wi-Fi Communication
ns3::WifiHelper wifi;
wifi.SetStandard(ns3::WIFI_PHY_STANDARD_80211);
ns3::WifiMacHelper mac;
mac.SetType(“ns3::AdhocWifiMac”);
ns3::WifiPhyHelper phy = ns3::WifiPhyHelper::Default();
phy.SetChannel(ns3::YansWifiChannelHelper::Default().Create());
ns3::NetDeviceContainer devices = wifi.Install(phy, mac, nodes);
- Assign IP Addresses
ns3::Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
ns3::Ipv4InterfaceContainer interfaces = address.Assign(devices);
- Generate Traffic
UDP Traffic
// UDP Echo server on Node 9
ns3::UdpEchoServerHelper echoServer(9);
ns3::ApplicationContainer serverApp = echoServer.Install(nodes.Get(9));
serverApp.Start(ns3::Seconds(1.0));
serverApp.Stop(ns3::Seconds(20.0));
// UDP Echo client on Node 0
ns3::UdpEchoClientHelper echoClient(interfaces.GetAddress(9), 9);
echoClient.SetAttribute(“MaxPackets”, ns3::UintegerValue(10));
echoClient.SetAttribute(“Interval”, ns3::TimeValue(ns3::Seconds(1.0)));
echoClient.SetAttribute(“PacketSize”, ns3::UintegerValue(512));
ns3::ApplicationContainer clientApp = echoClient.Install(nodes.Get(0));
clientApp.Start(ns3::Seconds(2.0));
clientApp.Stop(ns3::Seconds(20.0));
- Run the Simulation
ns3::Simulator::Run();
ns3::Simulator::Destroy();
- Analyze Results
Metrics:
- Packet Delivery Ratio (PDR):
- Amount the ratio successfully has delivered in the packets.
- Latency:
- Calculate the normal delay for packet distribution.
- Cluster Stability:
- Estimate the cluster re-formation percentages due to mobility.
Tracing and Visualization:
- .pcap it assure the tracing for packet study:
ns3::AsciiTraceHelper ascii;
phy.EnableAsciiAll(ascii.CreateFileStream(“cgsr.tr”));
phy.EnablePcapAll(“cgsr”);
- Trace use the tool like NetAnim for envision:
./waf –run “cgsr-simulation –vis”
- Iterate and Enhance
- Advanced Features:
- Apply the new characteristics for dynamic cluster re-formation.
- Improve the gateway for choose an inter-cluster communication.
- Test Scenarios:
- Establish the mobility for examine the protocol stability.
- It replicate the node or connection failures for validate the resilience.
- Protocol Comparison:
- Associate the CGSR through AODV, DSDV, and other protocols.
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.