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

How to Implement Network Traffic shaping in ns3

To implement the network traffic shaping in ns3 has need to configure and simulate the network that is used to control the bandwidth, delay, and packet loss experienced by traffic flows. The traffic shaping is usually used to select the urgency types of traffic and then apply the rate limits then enhance the overall network performance. Here, we offer the step-by-step procedure on how to implement the network traffic shaping in ns3:

Step-by-Step Implementation:

Step 1: Setup ns3 Environment

Make sure ns3 is installed in the system.

Step 2: Include Necessary Modules

Include the necessary ns3 modules in your 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”

#include “ns3/traffic-control-module.h”

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

Step 3: Create the Simulation Script

  1. Setup Nodes and Network:

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“NetworkTrafficShapingExample”);

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create (4);

// Create point-to-point links

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

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

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

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

// Install Internet stack

InternetStackHelper stack;

stack.Install (nodes);

// Assign IP addresses

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Set up applications

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

// Server application on node 3

Address serverAddress (InetSocketAddress (Ipv4Address::GetAny (), port));

PacketSinkHelper packetSinkHelper (“ns3::UdpSocketFactory”, serverAddress);

ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (3));

sinkApps.Start (Seconds (1.0));

sinkApps.Stop (Seconds (20.0));

// Client application on node 0

OnOffHelper onoff (“ns3::UdpSocketFactory”, Address (InetSocketAddress (interfaces.GetAddress (3), port)));

onoff.SetConstantRate (DataRate (“1Mbps”));

ApplicationContainer apps = onoff.Install (nodes.Get (0));

apps.Start (Seconds (2.0));

apps.Stop (Seconds (20.0));

// Enable pcap tracing for packet capture

pointToPoint.EnablePcapAll (“network-traffic-shaping”);

// Enable traffic control

TrafficControlHelper tch;

tch.SetRootQueueDisc (“ns3::RedQueueDisc”);

tch.Install (devices);

// Enable flow monitor

FlowMonitorHelper flowmon;

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

Simulator::Stop (Seconds (20.0));

Simulator::Run ();

// Print per-flow statistics

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

std::cout << “Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)\n”;

std::cout << ”  Tx Bytes:   ” << i->second.txBytes << “\n”;

std::cout << ”  Rx Bytes:   ” << i->second.rxBytes << “\n”;

std::cout << ”  Tx Packets: ” << i->second.txPackets << “\n”;

std::cout << ”  Rx Packets: ” << i->second.rxPackets << “\n”;

std::cout << ”  Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() – i->second.timeFirstTxPacket.GetSeconds()) / 1024 / 1024 << ” Mbps\n”;

}

Simulator::Destroy ();

return 0;

}

Step 4: Run the Simulation

Compile and run your simulation script:

sh

./waf configure

./waf build

./waf –run NetworkTrafficShapingExample

Explanation

  • Node Creation: Create nodes representing different devices in the network.
  • Point-to-Point Links: Configure point-to-point links between nodes with specified data rates and delays.
  • Internet Stack: Install the Internet stack on all nodes.
  • IP Configuration: Assign IP addresses to the interfaces.
  • Applications: Use OnOffApplication and PacketSink to simulate traffic between nodes.
  • Traffic Control: Enable traffic control using the RedQueueDisc queue discipline to shape traffic.
  • Pcap Tracing: Enable pcap tracing on the point-to-point links to capture packets.
  • Flow Monitor: Use the FlowMonitor module to collect and print statistics about the traffic flows.

Step 5: Analyse the Results

  1. Open Pcap Files with Wireshark:

Open the generated .pcap files in Wireshark to analyze the captured packets.

  1. Analyse Flow Monitor Output:

Review the flow monitor output printed to the console to analyze traffic flow statistics.

Advanced Traffic Shaping Techniques

  1. Different Queue Disciplines:

Use different queue disciplines to shape traffic in various ways. For example, you can use FqCoDelQueueDisc, PfifoFastQueueDisc, or TbfQueueDisc.

// Example for FqCoDelQueueDisc

TrafficControlHelper tch;

tch.SetRootQueueDisc (“ns3::FqCoDelQueueDisc”);

tch.Install (devices);

  1. Prioritizing Traffic:

Implement mechanisms to prioritize certain types of traffic. For example, you can use traffic classes and filters to give priority to specific types of traffic.

TrafficControlHelper tch;

tch.SetRootQueueDisc (“ns3::FqCoDelQueueDisc”);

// Define a traffic class with higher priority

tch.AddPacketFilter (tch.NewPacketFilter (“ns3::Ipv4PacketFilter”));

tch.Install (devices);

  1. Rate Limiting:

Use Token Bucket Filter (TBF) to enforce rate limits on traffic.

TrafficControlHelper tch;

tch.SetRootQueueDisc (“ns3::TbfQueueDisc”, “Burst”, StringValue (“500B”), “Rate”, DataRateValue (DataRate (“5Mbps”)));

tch.Install (devices);

  1. Simulating Packet Loss and Delay:

Introduce packet loss and delay to simulate real-world network conditions.

Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();

em->SetAttribute (“ErrorRate”, DoubleValue (0.01));  // 1% packet loss

devices.Get (1)->SetReceiveErrorModel (em);

// Introduce delay

Config::SetDefault (“ns3::PointToPointChannel::Delay”, StringValue (“10ms”));

From the script, we concentrate on how to enhance the network performance and how to select the urgency messages in the traffic using the network traffic shaping characteristics that were implemented using ns3 tool. We will plan to offer the more information regarding the network traffic shaping.

We offer excellent help with Network Traffic Shaping in the ns3 program. Our team analyzes the performance of your projects to ensure they succeed.