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 Recovery in Ns3

To calculate network recovery in ns3, we need to measure the time taken by the network when restoring to its normal operations using simulating the network failure. Here the steps given below will guide on how to calculate the network recovery time by using a link failure.

Step-by-step to calculate Network Recovery in ns3

  1. Set Up the NS3 Environment:
    • Make sure ns3 is installed.
  2. Define the Network Topology:
    • Create a network topology with nodes.
  3. Install Applications:
    • Install traffic-generating applications on the nodes to simulate network traffic.
  1. Simulating a Network failure:
  • Simulate a network failure to measure the time taken by the network while restoring the normal operations.
  1. Monitor Performance Metrics:
    • Monitoring the metrics performance while before, during and after the network failure.
  1. Calculating Recovery time:
    • Calculate the time taken by the network while restoring the normal operation to measure how quickly the network recovers.

Example Code

Here an example given on how to set up a simple ns3 simulation to calculate network recovery time by using a link failure and monitoring how fast the network recovers.

#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/flow-monitor-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“NetworkRecoveryExample”);

std::map<uint64_t, Time> packetSentTime;

std::map<uint64_t, Time> packetReceivedTime;

void PacketSentCallback (Ptr<const Packet> packet)

{

packetSentTime[packet->GetUid ()] = Simulator::Now ();

}

void PacketReceivedCallback (Ptr<const Packet> packet)

{

packetReceivedTime[packet->GetUid ()] = Simulator::Now ();

}

void LinkFailure (Ptr<PointToPointNetDevice> dev)

{

dev->SetReceiveCallback (MakeNullCallback<bool, Ptr<NetDevice>, Ptr<const Packet>, uint16_t, const Address &> ());

NS_LOG_UNCOND (“Link failure at ” << Simulator::Now ().GetSeconds () << ” seconds”);

}

void LinkRecovery (Ptr<PointToPointNetDevice> dev)

{

dev->SetReceiveCallback (MakeCallback (&PointToPointNetDevice::Receive, dev));

NS_LOG_UNCOND (“Link recovery at ” << Simulator::Now ().GetSeconds () << ” seconds”);

}

void CalculateRecoveryTime ()

{

Time recoveryTime = Seconds (0);

for (auto const &entry : packetSentTime)

{

uint64_t packetUid = entry.first;

if (packetReceivedTime.find (packetUid) != packetReceivedTime.end ())

{

Time sentTime = packetSentTime[packetUid];

Time receivedTime = packetReceivedTime[packetUid];

Time difference = receivedTime – sentTime;

if (difference > recoveryTime)

{

recoveryTime = difference;

}

}

}

NS_LOG_UNCOND (“Network recovery time: ” << recoveryTime.GetSeconds () << ” seconds”);

}

 

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

Time::SetResolution (Time::NS);

NodeContainer nodes;

nodes.Create (4);

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes.Get (0), nodes.Get (1));

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

devices.Add (pointToPoint.Install (nodes.Get (2), nodes.Get (3)));

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

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

address.Assign (devices);

// Create a UDP server on node 3

UdpServerHelper server (9);

ApplicationContainer serverApp = server.Install (nodes.Get (3));

serverApp.Start (Seconds (1.0));

serverApp.Stop (Seconds (20.0));

// Create a UDP client on node 0

UdpClientHelper client (Ipv4Address (“10.1.1.4”), 9);

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

// Trace packet sent and received events

Config::ConnectWithoutContext (“/NodeList/0/ApplicationList/*/$ns3::UdpClient/Tx”, MakeCallback (&PacketSentCallback));

Config::ConnectWithoutContext (“/NodeList/3/ApplicationList/*/$ns3::UdpServer/Rx”, MakeCallback (&PacketReceivedCallback));

// Simulate link failure and recovery

Ptr<PointToPointNetDevice> dev1 = DynamicCast<PointToPointNetDevice> (devices.Get (1));

Simulator::Schedule (Seconds (5.0), &LinkFailure, dev1);

Simulator::Schedule (Seconds (10.0), &LinkRecovery, dev1);

Simulator::Stop (Seconds (20.0));

Simulator::Run ();

CalculateRecoveryTime ();

 

Simulator::Destroy ();

return 0;

}

Explanation

  1. Setup: The code sets up a simple point-to-point network with four nodes connected in a line.
  2. Applications: A UDP server is installed on node 3, and a UDP client is installed on node 0 to generate traffic.
  3. Packet Sent and Received Callbacks: The PacketSentCallback and PacketReceivedCallback functions log the time each packet is sent and received, respectively.
  4. Link Failure and Recovery: The LinkFailure function disables the receive callback on a device to simulate a link failure. The LinkRecovery function re-enables the receive callback to simulate link recovery.
  5. Recovery Time Calculation: The CalculateRecoveryTime function calculates the recovery time based on the difference between the packet sent and received times.

Running the Simulation

Compile and run the simulation using the following commands in ns3 environment:

./waf configure

./waf build

./waf –run your-script-name

Replace your-script-name with the actual name of the script file.

At last, we get know by introducing the link failure in the network we can calculate how fast the network will recover after its restore to normal operations.

To figure out how long it takes to get your network back up and running, just send us your project’s specifics. We’ll do our best to give you the most accurate results.