To implement the network monitoring in ns3, we can accumulate and analyze the network performance metrics like throughput, delay, packet loss and jitter by developing the network simulation. To accomplish the simulation, ns3 offered us different tools and models includes FlowMonitor module. Here, we provide the step-by-step implementation details about the network monitoring in the ns3 from the scratch:
Step-by-Step Implementation:
Step 1: Setup ns3 Environment
Make sure that you have installed ns3 on your system.
Step 2: Include Necessary Modules
In this step, embrace the essential ns3 modules into the 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”
Step 3: Create the Simulation Script
- Setup Nodes and Network:
using namespace ns3;
NS_LOG_COMPONENT_DEFINE (“NetworkMonitoringExample”);
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));
// 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”;
std::cout << ” Delay: ” << i->second.delaySum.GetSeconds() / i->second.rxPackets << ” s\n”;
}
monitor->SerializeToXmlFile(“flowmon-results.xml”, true, true);
Simulator::Destroy ();
return 0;
}
Step 4: Run the Simulation
Compile and run your simulation script:
sh
./waf configure
./waf build
./waf –run NetworkMonitoringExample
Explanation
- Node Creation: Create nodes representing various 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: Disperse the IP addresses to the interfaces.
- Applications: Simulating the traffic amongst the nodes by using OnOffApplication and PacketSink.
- Flow Monitor: Accumulate the traffic data such as bytes that are transmitted and also received, packets, throughput and delay with the help of flow monitor.
Advanced Network Monitoring Techniques
- Additional Metrics:
By executing the sample below, we can accumulate and analyze added metrics like jitter, packet loss rate, and hop count.
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
std::cout << ” Jitter: ” << i->second.jitterSum.GetSeconds() / i->second.rxPackets << ” s\n”;
std::cout << ” Packet Loss Rate: ” << (i->second.txPackets – i->second.rxPackets) * 100.0 / i->second.txPackets << ” %\n”;
}
- Multiple Traffic Flows:
Using various properties to simulate and analyze multiple traffic flows (includes different data rates, protocols).
// Additional client application on another node
OnOffHelper onoff2 (“ns3::UdpSocketFactory”, Address (InetSocketAddress (interfaces.GetAddress (3), port)));
onoff2.SetConstantRate (DataRate (“500Kbps”));
ApplicationContainer apps2 = onoff2.Install (nodes.Get (1));
apps2.Start (Seconds (3.0));
apps2.Stop (Seconds (20.0));
- Dynamic Network Conditions:
We have to simulate the dynamic network conditions in scenarios like link failures, node mobility, or varying data rates.
// Change data rate of a link after 10 seconds
Simulator::Schedule (Seconds (10.0), &Config::Set, “/NodeList/1/DeviceList/0/$ns3::PointToPointNetDevice/DataRate”, StringValue (“5Mbps”));
- Visualization Tools:
Visualizing the network topology and its traffic flows by using the network visualization tool called NetAnim.
AnimationInterface anim (“network-monitoring.xml”);
anim.SetConstantPosition (nodes.Get (0), 1.0, 2.0);
anim.SetConstantPosition (nodes.Get (1), 2.0, 2.0);
anim.SetConstantPosition (nodes.Get (2), 3.0, 2.0);
anim.SetConstantPosition (nodes.Get (3), 4.0, 2.0);
In conclusion, this step-by-step detail will walk you through creating a basic network setup, generating traffic, and monitoring the network performance. We will help you with other network monitoring methods and its uses for your future references.
We have developed all models that incorporate the FlowMonitor module, so if you encounter challenges in discovering new topics on Implementation Network Monitoring in ns3tool, we are here to assist you.