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

How to Calculate Network Routing hop count in ns3

Calculating network routing hop count in ns3 requires to use the FlowMonitor tool, that tracks the number of times packets are forwarded by intermediate nodes. This count, known as timesForwarded, is used to determine the hop count for each flow. The hop count is calculated as timesForwarded + 1 as timesForwarded does not include the initial transmission from the source node.

Here is an example on calculating the network routing hop count in ns3.

Steps for calculating Network routing hop count

  1. Set up the simulation :
  • Make sure that ns3 is installed in the computer. If not, install it and include necessary modules.
  1. Create Network Topology:
  • create nodes and configure the network topology.
  • Set up routing protocols as required (e.g., OLSR, AODV).
  1. Configure Applications:
  • On the nodes, setup applications to generate and receive traffic.
  1. Enable tracing and Metrics Collection:
  • To capture relevant metrics such as packet transmissions and receptions, use ns3 tracing capabilities.
  1. Run the simulation :
  • Execute the simulation and collect the trace data.
  1. Analyze the results :
  • Post-process the trace data to calculate the routing hop count.

Example to Calculate the Network routing hop count

#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 “ns3/olsr-helper.h”

#include “ns3/flow-monitor-module.h”

#include “ns3/log.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“RoutingHopCountExample”);

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

{

// Set up logging

LogComponentEnable (“UdpClient”, LOG_LEVEL_INFO);

LogComponentEnable (“UdpServer”, LOG_LEVEL_INFO);

// Create nodes

NodeContainer nodes;

nodes.Create (10);

// Install Mobility model

MobilityHelper mobility;

mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

mobility.Install (nodes);

// Create point-to-point links

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NetDeviceContainer devices;

for (uint32_t i = 0; i < nodes.GetN () – 1; ++i)

{

devices.Add (pointToPoint.Install (nodes.Get (i), nodes.Get (i + 1)));

}

// Install Internet stack and OLSR routing protocol

InternetStackHelper internet;

OlsrHelper olsr;

internet.SetRoutingHelper (olsr);

internet.Install (nodes);

// Assign IP addresses

Ipv4AddressHelper ipv4;

ipv4.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);

// Install and start applications on nodes

uint16_t port = 9; // Discard port (RFC 863)

UdpServerHelper server (port);

ApplicationContainer serverApp = server.Install (nodes.Get (nodes.GetN () – 1));

serverApp.Start (Seconds (1.0));

serverApp.Stop (Seconds (10.0));

UdpClientHelper client (interfaces.GetAddress (nodes.GetN () – 1), port);

client.SetAttribute (“MaxPackets”, UintegerValue (320));

client.SetAttribute (“Interval”, TimeValue (MilliSeconds (50)));

client.SetAttribute (“PacketSize”, UintegerValue (1024));

ApplicationContainer clientApp = client.Install (nodes.Get (0));

clientApp.Start (Seconds (2.0));

clientApp.Stop (Seconds (10.0));

// Set up FlowMonitor to collect performance metrics

FlowMonitorHelper flowmon;

Ptr<FlowMonitor> monitor = flowmon.InstallAll ();

// Run the simulation

Simulator::Stop (Seconds (11.0));

Simulator::Run ();

// Calculate hop count for each flow

monitor->CheckForLostPackets ();

Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());

std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();

for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)

{

Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);

if (t.sourceAddress == Ipv4Address (“10.1.1.1”) && t.destinationAddress == Ipv4Address (“10.1.1.10”))

{

NS_LOG_UNCOND (“Flow ID: ” << i->first << “, Source: ” << t.sourceAddress << “, Destination: ” << t.destinationAddress);

NS_LOG_UNCOND (“Tx Packets: ” << i->second.txPackets);

NS_LOG_UNCOND (“Rx Packets: ” << i->second.rxPackets);

NS_LOG_UNCOND (“Times Forwarded: ” << i->second.timesForwarded);

NS_LOG_UNCOND (“Hop Count: ” << (i->second.timesForwarded + 1)); // timesForwarded counts only forwards, so +1 for the first hop

}

}

// Clean up

Simulator::Destroy ();

return 0;

}

Explanation:

  1. Setup Logging:
    • To track activities, enable logging for the UDP applications.
  2. Create Nodes:
    • Create a set of nodes representing devices in the network.
  3. Install Mobility Model:
    • On the nodes, install a mobility model (e.g., ConstantPositionMobilityModel).
  4. Create Point-to-Point Links:
    • Between the nodes, install point-to-point links.
  5. Install Internet Stack and Routing Protocol:
    • On the nodes, install the Internet stack and set OLSR as the routing protocol.
  6. Assign IP Addresses:
    • Assign IP addresses to the network interfaces.
  7. Install Applications:
    • On the nodes, install UDP server and client applications.
  8. Set Up FlowMonitor:
    • To collect and analyze flow statistics, install FlowMonitor.
  9. Run Simulation:
    • Run the simulation for the specified duration.
  10. Calculate Hop Count:
    • Extract flow statistics, after the simulation.
    • Calculate the hop count for each flow using the timesForwarded statistic, which counts the number of times a packet is forwarded by intermediate nodes.
    • The hop count is timesForwarded + 1 because the timesForwarded statistic does not include the initial transmission from the source node.

Analyzing the Results:

  • Hop Count:
    • The hop count is calculated as the number of intermediate nodes a packet traverses from the source to the destination plus one (for the initial transmission).
    • This metric provides an indication of the path length and routing efficiency in the network.

On the whole we had a performance analysis on calculating network routing hop count in ns3 by using the FlowMonitor tool, which tracks the number of times packets are forwarded by intermediate nodes. Also, we provide detailed explanation on Network Routing Hop Count.

For your project, we carry out thorough simulations in NS3 environments to determine the number of routing hops required.