To implement the Hot Potato Routing (HPR) in ns3,they also known as deflection routing where the packets were forwarded to any obtainable output channel before being held in queue. To avoid the load balancing and congestion avoidance you should forward the packet to another path if all the primary routes are busy.
Here are the procedures to implement the HPR in ns3 simulator:
Step-by-Step Implementation:
- Set Up Your ns3 Workspace: make certain ns3 is installed in the computer.
- Create a New Module for Hot Potato Routing:
- Navigate to the src directory of ns3 and create a new directory named hot-potato-routing.
- Inside the hot-potato-routing directory, create subdirectories: model, helper, and examples.
- Define the Hot Potato Routing Protocol:
- In the model directory, create .cc and .h files for the hot potato routing protocol. Define the HotPotatoRoutingProtocol class that inherits from the Ipv4RoutingProtocol class.
- Implement the Routing Logic:
- To forward packets to any available output channel if the preferred route is busy by implementing the hot potato routing logic.
- Integrate with ns3s Routing System:
- Modify the RoutingHelper class to include your hot potato routing protocol.
- Register the hot potato routing protocol as a routing protocol in ns3.
- Create a Simulation Script:
- Set up a network topology.
- Download the Internet stack.
- Use the hot potato routing helper to enable the hot potato routing protocol.
- Configure the applications and execute the simulation.
Example Code Structure
Here is the sample snippet to implement the hot potato routing in ns3:
- Define Hot Potato Routing Protocol
// src/hot-potato-routing/model/hot-potato-routing-protocol.h
#ifndef HOT_POTATO_ROUTING_PROTOCOL_H
#define HOT_POTATO_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 HotPotatoRoutingProtocol : public Ipv4RoutingProtocol
{
public:
static TypeId GetTypeId (void);
HotPotatoRoutingProtocol ();
virtual ~HotPotatoRoutingProtocol ();
// 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);
private:
void Start ();
void Stop ();
Ptr<Ipv4> m_ipv4;
std::vector<Ptr<Ipv4Route>> m_routes;
};
} // namespace ns3
#endif // HOT_POTATO_ROUTING_PROTOCOL_H
2. Implement Hot Potato Routing Protocol
// src/hot-potato-routing/model/hot-potato-routing-protocol.cc
#include “hot-potato-routing-protocol.h”
#include “ns3/log.h”
#include “ns3/ipv4-static-routing.h”
#include “ns3/ipv4-static-routing-helper.h”
namespace ns3 {
NS_LOG_COMPONENT_DEFINE (“HotPotatoRoutingProtocol”);
NS_OBJECT_ENSURE_REGISTERED (HotPotatoRoutingProtocol);
TypeId
HotPotatoRoutingProtocol::GetTypeId (void)
{
static TypeId tid = TypeId (“ns3::HotPotatoRoutingProtocol”)
.SetParent<Ipv4RoutingProtocol> ()
.SetGroupName (“Internet”)
.AddConstructor<HotPotatoRoutingProtocol> ();
return tid;
}
HotPotatoRoutingProtocol::HotPotatoRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
HotPotatoRoutingProtocol::~HotPotatoRoutingProtocol ()
{
NS_LOG_FUNCTION (this);
}
void
HotPotatoRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)
{
NS_LOG_FUNCTION (this << ipv4);
m_ipv4 = ipv4;
}
void
HotPotatoRoutingProtocol::NotifyInterfaceUp (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
HotPotatoRoutingProtocol::NotifyInterfaceDown (uint32_t interface)
{
NS_LOG_FUNCTION (this << interface);
}
void
HotPotatoRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
HotPotatoRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)
{
NS_LOG_FUNCTION (this << interface << address);
}
void
HotPotatoRoutingProtocol::Start ()
{
NS_LOG_FUNCTION (this);
}
void
HotPotatoRoutingProtocol::Stop ()
{
NS_LOG_FUNCTION (this);
}
Ptr<Ipv4Route>
HotPotatoRoutingProtocol::RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr)
{
NS_LOG_FUNCTION (this << p << header << oif << sockerr);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
route->SetDestination (dest);
// Check for the primary route
for (const auto &r : m_routes)
{
if (r->GetDestination () == dest)
{
route = r;
break;
}
}
// If no primary route, deflect to any available route
if (route->GetGateway () == Ipv4Address ())
{
if (!m_routes.empty ())
{
route = m_routes.front (); // Deflect to the first available route
}
}
return route;
}
bool
HotPotatoRoutingProtocol::RouteInput (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)
{
NS_LOG_FUNCTION (this << p << header << idev << ucb << mcb << lcb << ecb);
Ipv4Address dest = header.GetDestination ();
Ptr<Ipv4Route> route = Create<Ipv4Route> ();
route->SetDestination (dest);
// Check for the primary route
for (const auto &r : m_routes)
{
if (r->GetDestination () == dest)
{
ucb (r, p, header);
return true;
}
}
// If no primary route, deflect to any available route
if (route->GetGateway () == Ipv4Address ())
{
if (!m_routes.empty ())
{
route = m_routes.front (); // Deflect to the first available route
ucb (route, p, header);
return true;
}
}
return false;
}
} // namespace ns3
3. Helper Class
// src/hot-potato-routing/helper/hot-potato-routing-helper.h
#ifndef HOT_POTATO_ROUTING_HELPER_H
#define HOT_POTATO_ROUTING_HELPER_H
#include “ns3/ipv4-routing-helper.h”
#include “ns3/object-factory.h”
#include “ns3/hot-potato-routing-protocol.h”
namespace ns3 {
class HotPotatoRoutingHelper : public Ipv4RoutingHelper
{
public:
HotPotatoRoutingHelper ();
HotPotatoRoutingHelper (const HotPotatoRoutingHelper &);
HotPotatoRoutingHelper &operator= (const HotPotatoRoutingHelper &);
virtual ~HotPotatoRoutingHelper ();
virtual HotPotatoRoutingHelper* Copy (void) const;
virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
};
} // namespace ns3
#endif // HOT_POTATO_ROUTING_HELPER_H
4. Implement Helper Class
// src/hot-potato-routing/helper/hot-potato-routing-helper.cc
#include “hot-potato-routing-helper.h”
#include “ns3/hot-potato-routing-protocol.h”
#include “ns3/node.h”
namespace ns3 {
HotPotatoRoutingHelper::HotPotatoRoutingHelper ()
{
}
HotPotatoRoutingHelper::HotPotatoRoutingHelper (const HotPotatoRoutingHelper &o)
{
}
HotPotatoRoutingHelper&
HotPotatoRoutingHelper::operator= (const HotPotatoRoutingHelper &o)
{
return *this;
}
HotPotatoRoutingHelper::~HotPotatoRoutingHelper ()
{
}
HotPotatoRoutingHelper*
HotPotatoRoutingHelper::Copy (void) const
{
return new HotPotatoRoutingHelper (*this);
}
Ptr<Ipv4RoutingProtocol>
HotPotatoRoutingHelper::Create (Ptr<Node> node) const
{
Ptr<HotPotatoRoutingProtocol> protocol = CreateObject<HotPotatoRoutingProtocol> ();
node->AggregateObject (protocol);
return protocol;
}
} // namespace ns3
Example Simulation Script
// examples/hot-potato-routing-simulation.cc
#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/hot-potato-routing-helper.h”
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“HotPotatoRoutingExample”);
int main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
// Create nodes
NodeContainer nodes;
nodes.Create (4);
// Create point-to-point links
PointToPointHelper p2p;
p2p.SetDeviceAttribute (“DataRate”, StringValue (“1Mbps”));
p2p.SetChannelAttribute (“Delay”, StringValue (“10ms”));
NetDeviceContainer devices01 = p2p.Install (nodes.Get (0), nodes.Get (1));
NetDeviceContainer devices02 = p2p.Install (nodes.Get (0), nodes.Get (2));
NetDeviceContainer devices13 = p2p.Install (nodes.Get (1), nodes.Get (3));
NetDeviceContainer devices23 = p2p.Install (nodes.Get (2), nodes.Get (3));
// Install the Internet stack
InternetStackHelper stack;
HotPotatoRoutingHelper hotPotatoRouting;
stack.SetRoutingHelper (hotPotatoRouting);
stack.Install (nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase (“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces01 = address.Assign (devices01);
address.SetBase (“10.1.2.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces02 = address.Assign (devices02);
address.SetBase (“10.1.3.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces13 = address.Assign (devices13);
address.SetBase (“10.1.4.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces23 = address.Assign (devices23);
// Create a packet sink to receive packets
uint16_t sinkPort = 8080;
Address sinkAddress (InetSocketAddress (Ipv4Address (“10.1.4.1”), sinkPort));
PacketSinkHelper packetSinkHelper (“ns3::TcpSocketFactory”, sinkAddress);
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (3));
sinkApps.Start (Seconds (1.0));
sinkApps.Stop (Seconds (10.0));
// Create a TCP client to send packets
OnOffHelper clientHelper (“ns3::TcpSocketFactory”, sinkAddress);
clientHelper.SetAttribute(“OnTime”,StringValue(“ns3::ConstantRandomVariable[Constant=1]”));
clientHelper.SetAttribute(“OffTime”,StringValue(“ns3::ConstantRandomVariable[Constant=0]”));
clientHelper.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));
clientHelper.SetAttribute (“PacketSize”, UintegerValue (1024));
ApplicationContainer clientApps = clientHelper.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
// Enable tracing
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream (“hot-potato-routing.tr”));
p2p.EnablePcapAll (“hot-potato-routing”);
// Run the simulation
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Running the Simulation
To run the simulation, compile the script and execute it:
./waf configure –enable-examples
./waf build
./waf –run hot-potato-routing-example
Overall, we had implemented the deflection routing protocol in ns3 simulator that has creates a network topology by the use of HPR functionalities.
Hot Potato Routing (HPR) comparative analysis are done as per your project concept from ns3simulation.com team of programmers.