How to Begin Implement Network Latency in NS3

To begin implementing the network latency using NS3, we can estimate the duration for data packets moving from the source node to the destination node. It is one of the general performance parameter within networking and also it can be attained by NS3’s tracing and logging approaches. We can follow these steps to get started:

Steps to Implement Network Latency in NS3

  1. Understand Network Latency
  • Definition: The time interval undergone by data since it moves from source to destination.
  • Components:
    • Propagation Delay: Time for a signal to pass through the medium.
    • Transmission Delay: It’s time for impelling all packet bits on the wire.
    • Processing Delay: Time taken to execute the packet by nodes.
    • Queuing Delay: Time consumed within queues staying for transmission.
  1. Set Up the NS3 Simulation
  1. Install NS3: Make sure that we have installed and download NS3 on the system.
  2. Choose a Base Scenario:
    • Select the kind of network for replicating point-to-point, wireless, or more complex topologies.
    • We can utilize or change an existing NS3 example script such as simple-point-to-point.cc.
  1. Measure Latency
  1. Add Timestamp to Packets:
    • Insert a timestamp to packets at the sender using Packet::AddByteTag technique.
  2. Capture Packets at the Receiver:
    • Extort the timestamp at the receiver and determine the delay using callback.
  1. Implement Latency Measurement

Here’s a sample script for a simple point-to-point network to estimate the latency.

Example Script:

#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”

using namespace ns3;

class TimestampTag : public Tag {

public:

TimestampTag() {}

void SetTimestamp(Time time) { m_timestamp = time; }

Time GetTimestamp() const { return m_timestamp; }

static TypeId GetTypeId() {

static TypeId tid = TypeId(“TimestampTag”)

.SetParent<Tag>()

.SetGroupName(“Network”);

return tid;

}

TypeId GetInstanceTypeId() const override { return GetTypeId(); }

void Serialize(TagBuffer i) const override { i.WriteDouble(m_timestamp.GetSeconds()); }

void Deserialize(TagBuffer i) override { m_timestamp = Seconds(i.ReadDouble()); }

uint32_t GetSerializedSize() const override { return sizeof(double); }

void Print(std::ostream &os) const override { os << “Timestamp: ” << m_timestamp.GetSeconds(); }

private:

Time m_timestamp;

};

void SendPacket(Ptr<Socket> socket) {

Ptr<Packet> packet = Create<Packet>(1024); // 1024 bytes

TimestampTag timestamp;

timestamp.SetTimestamp(Simulator::Now());

packet->AddPacketTag(timestamp);

socket->Send(packet);

Simulator::Schedule(Seconds(1.0), &SendPacket, socket); // Send every 1 second

}

void ReceivePacket(Ptr<Socket> socket) {

Ptr<Packet> packet = socket->Recv();

TimestampTag timestamp;

if (packet->PeekPacketTag(timestamp)) {

Time sendTime = timestamp.GetTimestamp();

Time receiveTime = Simulator::Now();

Time delay = receiveTime – sendTime;

NS_LOG_UNCOND(“Latency: ” << delay.GetMilliSeconds() << ” ms”);

}

}

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

CommandLine cmd;

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(2);

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices = pointToPoint.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Create sockets

Ptr<Socket> sender = Socket::CreateSocket(nodes.Get(0), TypeId::LookupByName(“ns3::UdpSocketFactory”));

Ptr<Socket> receiver = Socket::CreateSocket(nodes.Get(1), TypeId::LookupByName(“ns3::UdpSocketFactory”));

InetSocketAddress remote = InetSocketAddress(interfaces.GetAddress(1), 8080);

receiver->Bind(InetSocketAddress(Ipv4Address::GetAny(), 8080));

receiver->SetRecvCallback(MakeCallback(&ReceivePacket));

sender->Connect(remote);

// Schedule packet sending

Simulator::Schedule(Seconds(1.0), &SendPacket, sender);

Simulator::Run();

Simulator::Destroy();

return 0;

}

  1. Explanation of the Script
  1. Nodes and Links:
    • Two nodes are associated with a point-to-point link.
    • The link contains a propagation delay of 2ms and data rate of 5Mbps.
  2. Packet Tagging:
    • A custom tag (TimestampTag) is inserted into packets including the present simulation time.
  3. Latency Calculation:
    • Latency is computed at the receiver:

Time delay = receiveTime – sendTime;

  1. Logging:
    • The latency is logged for every received packet.
  1. Run the Simulation
  1. Build and Execute:

./waf –run “your-script-name”

  1. Output:
    • Monitor the latency within the terminal for every packet.
  1. Analyze Results
  • Record latencies for numerous packets.
  • Examine the statistical parameter such as:
    • Maximum and Minimum Latency
    • Latency Variation (Jitter)
    • Average Latency

Advanced Extensions

  1. Complex Topologies:
    • Make complex scenarios to leverage additional nodes, routers, and links.
  2. Dynamic Traffic:
    • Create the realistic traffic models using applications such as OnOffApplication.
  3. Mobility:
    • Integrate the mobility patterns for replicating latency in dynamic scenarios.
  4. Visualization:
    • Transfer records of latency into external tools such as Excel or MATLAB for in-depth analysis.

We’ve shared a structured approach that has numerous steps with sample coding for implementing and examining the Network Latency using NS3 environment. Further explanations can be shared later.