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

How to Implement Traffic aware Routing in ns3

To implement traffic aware routing in ns3 has encompasses to generate the network simulation that cans enthusiastically modify the routes based on the current traffic scenarios. This is commonly contains to gather the traffic statistics, analyse the network performance and update the routing tables based on the performance. The below are the procedures to achieve this:

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

#include “ns3/ipv4-global-routing-helper.h”

Step 3: Create the Simulation Script

  1. Setup Nodes and Network:

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“TrafficAwareRouting”);

void MonitorTraffic (Ptr<Ipv4FlowClassifier> classifier, Ptr<FlowMonitor> monitor)

{

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

NS_LOG_UNCOND (“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)\n”

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

<< ” Rx Bytes:  “<< i->second.rxBytes << “\n”                      <<“Throughput:”<<i>second.rxBytes*8.0/(i>second.timeLastRxPacket.GetSeconds()-i>second.timeFirstTxPacket.GetSeconds()) / 1024 / 1024 << ” Mbps”);

}

Simulator::Schedule (Seconds (1.0), &MonitorTraffic, classifier, monitor);

}

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create (6);

// Create point-to-point links

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

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

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

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

devices = pointToPoint.Install (nodes.Get (3), nodes.Get (4));

devices = pointToPoint.Install (nodes.Get (4), nodes.Get (5));

// 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 5

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

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

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

sinkApps.Start (Seconds (1.0));

sinkApps.Stop (Seconds (20.0));

// Client application on node 0

UdpEchoServerHelper echoServer (port);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (5));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (20.0));

UdpEchoClientHelper echoClient (interfaces.GetAddress (5), port);

echoClient.SetAttribute (“MaxPackets”, UintegerValue (100));

echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

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

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (20.0));

// Flow monitor

FlowMonitorHelper flowmon;

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

// Schedule traffic monitoring

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

Simulator::Schedule (Seconds (1.0), &MonitorTraffic, classifier, monitor);

Simulator::Stop (Seconds (20.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 4: Run the Simulation

Compile and run your simulation script:

./waf configure

./waf build

./waf –run TrafficAwareRouting

Explanation

  • Node Creation: Create nodes representing different devices in the network.
  • Point-to-Point Links: Configure point-to-point links between nodes.
  • Internet Stack: Install the Internet stack on all nodes.
  • IP Configuration: Assign IP addresses to the nodes.
  • Applications: Set up a UDP echo server and client to simulate network traffic.
  • Flow Monitor: Use the flow monitor to collect traffic data and schedule the MonitorTraffic function to log traffic statistics periodically.

Advanced Traffic-Aware Routing Techniques

  1. Dynamic Routing Updates:

Implement logic to update routing tables based on traffic conditions.

void UpdateRouting (Ptr<Ipv4> ipv4)

{

Ipv4StaticRoutingHelper staticRoutingHelper;

Ptr<Ipv4StaticRouting> staticRouting = staticRoutingHelper.GetStaticRouting (ipv4);

// Example: change route based on traffic conditions

staticRouting->RemoveRoute (1); // Remove old route

staticRouting->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.2”), 1); // Add new route

}

void MonitorTrafficAndUpdateRouting (Ptr<Ipv4FlowClassifier> classifier, Ptr<FlowMonitor> monitor, Ptr<Node> node)

{

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

NS_LOG_UNCOND (“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)\n”

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

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

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

// Example condition to trigger routing update

if (i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds() – i->second.timeFirstTxPacket.GetSeconds()) / 1024 / 1024 > 1.0)

{

UpdateRouting (node->GetObject<Ipv4> ());

}

}

Simulator::Schedule (Seconds (1.0), &MonitorTrafficAndUpdateRouting, classifier, monitor, node);

}

// In main function, replace MonitorTraffic with MonitorTrafficAndUpdateRouting

Simulator::Schedule (Seconds (1.0), &MonitorTrafficAndUpdateRouting, classifier, monitor, nodes.Get (1));

Quality of Service (QoS):

Implement QoS to prioritize traffic and manage congestion.

TrafficControlHelper tch;

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

tch.Install (devices);

// In main function, enable QoS

tch.Install (devices);

Load Balancing:

Implement load balancing to distribute traffic evenly across multiple paths.

void LoadBalancing (Ptr<Node> node)

{

Ipv4StaticRoutingHelper staticRoutingHelper;

Ptr<Ipv4StaticRouting> staticRouting = staticRoutingHelper.GetStaticRouting (node->GetObject<Ipv4> ());

// Implement load balancing logic here

staticRouting->RemoveRoute (1);

staticRouting->AddNetworkRouteTo (Ipv4Address (“10.1.1.0”), Ipv4Mask (“255.255.255.0”), Ipv4Address (“10.1.1.3”), 1);

}

// In main function, schedule load balancing

Simulator::Schedule (Seconds (5.0), &LoadBalancing, nodes.Get (1));

Here, we understand the process of traffic aware routing how it analyse the results in ns3 tool and we also provide the sample complete script to execute the traffic aware routing. We will describe how the traffic aware routing is carried out in alternative simulation environments.

At ns3simulation.com, we perform comparative analyses in the field of networking and offer a variety of project topics related to Traffic Aware Routing using the ns3 tool. If you need implementation support or have any questions, please reach out to us for additional assistance.