How to Calculate Network Accuracy in ns3

To calculate accuracy in ns3, it varies on the context in which we are measuring accuracy. For an instance, accuracy may refer to the correct reception of packets, the precision of a protocol’s operation, or the correct detection in a simulated network intrusion detection system.

Example : packet delivery accuracy

If we’re measuring the accuracy of packet delivery in a network simulation, we can compare the number of packets sent with the number of packets received successfully. Accuracy can be defined as the ratio of successfully received packets to the total packets sent.

Steps for calculating packet delivery accuracy

  1. Set up the simulation :
  • To simulate the network , create a network topology with nodes and links configured.

 

  1. Install applications :
  • To send and receive packets, use a UDP client and server application.

 

  1. Capture sent and received packets :
  • To record the number of packets sent and received, use packet tracing.

 

  1. Calculate accuracy :
  • Compute the accuracy as the ratio of received packets to sent packets.

Implementation

The following is an example to set up a basic point-to-point network in ns3 calculate the packet delivery accuracy.

Setting Up the Network Simulation

#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/packet.h”

#include <iostream>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE(“AccuracyExample”);

uint32_t packetsSent = 0;

uint32_t packetsReceived = 0;

void

PacketSentCallback(Ptr<const Packet> packet)

{

packetsSent++;

}

void

PacketReceivedCallback(Ptr<const Packet> packet)

{

packetsReceived++;

}

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

{

Time::SetResolution(Time::NS);

LogComponentEnable(“AccuracyExample”, LOG_LEVEL_INFO);

// Create nodes

NodeContainer nodes;

nodes.Create(2);

// Set up point-to-point link

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

devices = pointToPoint.Install(nodes);

// Install the Internet stack

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Set up UDP server on Node 1

uint16_t port = 9;

UdpServerHelper server(port);

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

serverApp.Start(Seconds(1.0));

serverApp.Stop(Seconds(10.0));

// Set up UDP client on Node 0

UdpClientHelper client(interfaces.GetAddress(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));

// Connect packet sent and received callbacks

devices.Get(0)->TraceConnectWithoutContext(“PhyTxEnd”, MakeCallback(&PacketSentCallback));

devices.Get(1)->TraceConnectWithoutContext(“PhyRxEnd”, MakeCallback(&PacketReceivedCallback));

Simulator::Run();

// Calculate accuracy

double accuracy = 0.0;

if (packetsSent > 0)

{

accuracy = static_cast<double>(packetsReceived) / packetsSent;

}

std::cout << “Packets sent: ” << packetsSent << std::endl;

std::cout << “Packets received: ” << packetsReceived << std::endl;

std::cout << “Packet delivery accuracy: ” << accuracy * 100 << “%” << std::endl;

Simulator::Destroy();

return 0;

}

Explanation

  1. Simulation setup :

Two nodes are created. Those nodes are connected using a point-to-point link.

  1. Application setup :

On destination node, a UDP server is installed. and On source node, a UDP echo client is installed to generate traffic.

  1. Packet tracing :

Whenever a packet is sent, PacketSentCallback: increments the packetsSent counter.

  1. Running the Simulation :

The simulation runs. and the remaining energy of each node is reported at regular intervals.

  1. Calculating accuracy :

The accuracy is calculated as the ratio of received packets to send packets and is printed to the console as a percentage.

Overall, we had learned on calculating accuracy in ns3 by comparing the number of packets sent with the number of packets successfully received. Also, we provide more related project ideas  on accuracy. Share with us your details we will carry on performance analysis by calculating accuracy in ns3simulation and provide you with best results.