Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement HWMP protocol in ns3

To implement the Hybrid Wireless Mesh Protocol (HWMP) in ns3, we create a custom module for handling the HWMP functionalities. All types of HWMP functionalities support can be handled by ns3simulation.com experts reach us for mere guidance.

The steps given below will guide to implement the HWMP in ns3.

Step-by-Step Guide to Implementing HWMP in ns-3

  1. Set Up Your Environment

Ensure that ns3 is installed and setup correctly.

  1. Create a New ns-3 Module

Create a new module for HWMP in the src directory of the ns3 installation. This involves creating the necessary directory structure and files.

cd ns-3.xx

cd src

mkdir -p hwmp/model

mkdir -p hwmp/helper

3. Create the HWMP Protocol Header File

Create the HWMP routing protocol header file hwmp-routing-protocol.h in the model directory.

#ifndef HWMP_ROUTING_PROTOCOL_H

#define HWMP_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>

#include <vector>

namespace ns3 {

class HwmpRoutingProtocol : public Ipv4RoutingProtocol

{

public:

static TypeId GetTypeId (void);

HwmpRoutingProtocol ();

virtual ~HwmpRoutingProtocol ();

// 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;

void SendPREQ (Ipv4Address dest);

void SendPREP (Ipv4Address dest, Ipv4Address origin);

void SendPERR (Ipv4Address dest);

Ptr<NetDevice> GetNetDevice (uint32_t interface);

Ptr<MobilityModel> GetMobilityModel (Ptr<NetDevice> netDevice);

Ipv4Address GetNextHop (Ipv4Address dest);

};

} // namespace ns3

#endif /* HWMP_ROUTING_PROTOCOL_H */

4. Create the HWMP Protocol Source File

Create the HWMP routing protocol source file hwmp-routing-protocol.cc in the model directory.

#include “hwmp-routing-protocol.h”

#include “ns3/log.h”

#include “ns3/ipv4-route.h”

#include “ns3/simulator.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“HwmpRoutingProtocol”);

NS_OBJECT_ENSURE_REGISTERED (HwmpRoutingProtocol);

TypeId

HwmpRoutingProtocol::GetTypeId (void)

{

static TypeId tid = TypeId (“ns3::HwmpRoutingProtocol”)

.SetParent<Ipv4RoutingProtocol> ()

.SetGroupName(“Internet”)

.AddConstructor<HwmpRoutingProtocol> ();

return tid;

}

HwmpRoutingProtocol::HwmpRoutingProtocol ()

{

}

HwmpRoutingProtocol::~HwmpRoutingProtocol ()

{

}

void

HwmpRoutingProtocol::SetIpv4 (Ptr<Ipv4> ipv4)

{

m_ipv4 = ipv4;

}

Ptr<NetDevice>

HwmpRoutingProtocol::GetNetDevice (uint32_t interface)

{

return m_ipv4->GetNetDevice (interface);

}

Ptr<MobilityModel>

HwmpRoutingProtocol::GetMobilityModel (Ptr<NetDevice> netDevice)

{

return netDevice->GetNode ()->GetObject<MobilityModel> ();

}

Ipv4Address

HwmpRoutingProtocol::GetNextHop (Ipv4Address dest)

{

if (m_routingTable.find(dest) != m_routingTable.end())

{

return m_routingTable[dest].GetGateway();

}

return Ipv4Address::GetBroadcast ();

}

Ptr<Ipv4Route>

HwmpRoutingProtocol::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 ())

{

SendPREQ (dest);

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

HwmpRoutingProtocol::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 ())

{

SendPREQ (dest);

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

HwmpRoutingProtocol::SendPREQ (Ipv4Address dest)

{

// Implementation of PREQ packet sending

NS_LOG_FUNCTION (this << dest);

}

void

HwmpRoutingProtocol::SendPREP (Ipv4Address dest, Ipv4Address origin)

{

// Implementation of PREP packet sending

NS_LOG_FUNCTION (this << dest << origin);

}

void

HwmpRoutingProtocol::SendPERR (Ipv4Address dest)

{

// Implementation of PERR packet sending

NS_LOG_FUNCTION (this << dest);

}

void

HwmpRoutingProtocol::NotifyInterfaceUp (uint32_t interface)

{

}

void

HwmpRoutingProtocol::NotifyInterfaceDown (uint32_t interface)

{

}

void

HwmpRoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

void

HwmpRoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address)

{

}

 

void

HwmpRoutingProtocol::PrintRoutingTable (Ptr<OutputStreamWrapper> stream) const

{

*stream->GetStream () << “HWMP 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 HWMP Helper

Create the HWMP helper header file hwmp-helper.h in the helper directory.

#ifndef HWMP_HELPER_H

#define HWMP_HELPER_H

#include “ns3/ipv4-routing-helper.h”

#include “hwmp-routing-protocol.h”

namespace ns3 {

class HwmpHelper : public Ipv4RoutingHelper

{

public:

HwmpHelper ();

virtual ~HwmpHelper ();

HwmpHelper* Copy (void) const;

virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;

};

} // namespace ns3

#endif /* HWMP_HELPER_H */

Create the HWMP helper source file hwmp-helper.cc in the helper directory.

#include “hwmp-helper.h”

#include “ns3/node.h”

#include “ns3/ipv4.h”

namespace ns3 {

HwmpHelper::HwmpHelper ()

{

}

HwmpHelper::~HwmpHelper ()

{

}

HwmpHelper*

HwmpHelper::Copy (void) const

{

return new HwmpHelper (*this);

}

Ptr<Ipv4RoutingProtocol>

HwmpHelper::Create (Ptr<Node> node) const

{

Ptr<HwmpRoutingProtocol> hwmpRouting = CreateObject<HwmpRoutingProtocol> ();

node->AggregateObject (hwmpRouting);

return hwmpRouting;

}

} // namespace ns3

6. Update CMakeLists.txt

Add the new HWMP module to the ns3 build system. Edit src/CMakeLists.txt and add the following line:

add_subdirectory (hwmp)

Create src/hwmp/CMakeLists.txt with the following content:

ns3_add_library (hwmp

model/hwmp-routing-protocol.cc

helper/hwmp-helper.cc

)

 

target_link_libraries (hwmp)

7. Set Up the Network Topology

Create a simulation script in the scratch directory to use the HWMP protocol.

#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 “hwmp-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (10);

// 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;

HwmpHelper hwmp;

stack.SetRoutingHelper (hwmp);

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 (9));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 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 (“hwmp-simulation.tr”));

wifiPhy.EnablePcapAll (“hwmp-simulation”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

8. Build and Run the Simulation

After writing the script, we need to build and run it.

./waf build

./waf –run scratch/hwmp-simulation

9. Analyze the Results

After running the simulation, we can analyze the results using the generated trace files (hwmp-simulation.tr and hwmp-simulation-0-0.pcap).

Complete Example Script for HWMP Protocol

Here’s a complete example script for reference:

#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 “hwmp-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (10);

// 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;

HwmpHelper hwmp;

stack.SetRoutingHelper (hwmp);

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 (9));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 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 (“hwmp-simulation.tr”));

wifiPhy.EnablePcapAll (“hwmp-simulation”);

// Run the simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Atlast, we had concluded that Hybrid Wireless Mesh Protocol (HWMP) can be implemented by creating a custom module to handle the HWMP functionalities on the simulation process.

If you face any difficulties in HWMP programming then approach us for best results.