Welcome back! Today, we are going to explore a hybrid network topology in ns-3.44. We will be simulating a network that consists of two Point-to-Point (P2P) nodes and three CSMA (Carrier Sense Multiple Access) nodes (representing a LAN).
Beyond just running the simulation, we are going to experiment with five essential modules to visualize and analyze our network performance:
NetAnim (Network Animation)
AsciiTraceHelper (TraceMetrics-1.4.0)
Gnuplot (Graphing data)
Packet Capture (Wireshark & .pcap files)
Flow Monitor (Performance statistics)
Let's get started!
Check the full video here:Prerequisites & Setup
We will be using the standard example file second.cc. First, we need to copy this file from the examples directory to our scratch directory so we can modify it without ruining the original.
Open your terminal and run:
cd ns-allinone-3.44/ns-3.44
cp examples/tutorial/second.cc scratch/
Now, let's verify that the basic simulation works:
./ns3 run scratch/second.cc
Using Command Line Arguments
One of the great features of ns-3 is the ability to change variables without editing the code. Try these variations:
Change the number of CSMA nodes to 10:
Bash./ns3 run "scratch/second.cc --nCsma=10"Turn off logging:
Bash./ns3 run "scratch/second.cc --nCsma=10 --verbose=false"Turn on logging:
Bash./ns3 run "scratch/second.cc --nCsma=10 --verbose=true"
1. Network Animation (NetAnim)
To visualize how packets move through our P2P and CSMA network, we need to generate an XML file for NetAnim.
Modify scratch/second.cc:
Add the following header at the top of your file:
#include "ns3/netanim-module.h"
Add the following line strictly before Simulator::Run():
AnimationInterface anim("animation_second.xml");
Run the simulation:
./ns3 run scratch/second.cc
View the Animation: Open a new terminal to run the NetAnim tool:
cd ns-allinone-3.44/netanim-3.109/
./NetAnim
Click the folder icon to open animation_second.xml and hit the Play button.
2. ASCII Trace Metrics
To analyze throughput numerically, we can generate ASCII trace files.
Modify scratch/second.cc:
Add these lines above Simulator::Run():
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("p2p.tr"));
csma.EnableAsciiAll(ascii.CreateFileStream("csma.tr"));
Run the simulation:
./ns3 run scratch/second.cc
This will generate p2p.tr and csma.tr. You can analyze these manually or use TraceMetrics.
Using TraceMetrics: Note: This is an older tool but works well with Java 8/11.
cd tracemetrics-1.4.0/
java -jar tracemetrics.jar
Load your .tr files into the application to view the analysis.
3. Plotting with Gnuplot
Let's visualize the throughput data using Gnuplot. For this example, let's assume we have extracted the following throughput data from our traces.
Create a file named thru.data and paste this content:
0 936.1025627361905 909.458277269316
1 936.1025627361905 909.458277269316
1 938.707466900344 913.8292756336998
2 0.0 0.0
3 0.0 0.0
4 938.707466900344 913.8292756336998
Create a Gnuplot script named plot.plt:
set terminal pdf
set output "throughput.pdf"
set title "Throughput and Goodput of P2P and CSMA Network"
set xlabel "Node Number"
set ylabel "Throughput in bps"
plot "thru.data" using 1:2 with linespoints title "Throughput" lw 4, "thru.data" using 1:3 with linespoints title "Goodput" lw 4
Generate the Graph:
sudo apt update
sudo apt install gnuplot
gnuplot plot.plt
Check your folder for the newly created throughput.pdf.
4. Wireshark (Packet Capture)
ns-3 can generate .pcap files readable by Wireshark, which is great for inspecting packet headers.
Modify scratch/second.cc:
Look for these lines above Simulator::Run() (if they aren't there, add them):
pointToPoint.EnablePcapAll("p2p");
csma.EnablePcapAll("csma");
Run and Analyze:
./ns3 run scratch/second.cc
To view the output, you can use Wireshark GUI or TCPDump in the terminal:
# Using Wireshark GUI
wireshark csma-4-0.pcap
# Using TCPDump (Terminal)
tcpdump -n -t -r csma-4-0.pcap
5. Flow Monitor
Finally, the most powerful tool for statistics (Delay, Jitter, Packet Loss, etc.) is the Flow Monitor.
Modify scratch/second.cc:
Add the headers:
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
Add this block before Simulator::Run():
// Flow monitor
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
Simulator::Stop(Seconds(10));
Add this line after Simulator::Run():
flowMonitor->SerializeToXmlFile("flomon_second.xml", true, true);
Parsing the Results:
The simulation creates flomon_second.xml, but it's hard to read manually. We use a Python script provided by ns-3 to parse it.
Copy the parser to your main directory for easy access:
Bashcp src/flow-monitor/examples/flowmon-parse-results.py .Run the simulation and parse the results:
Bash./ns3 run scratch/second.cc python3 flowmon-parse-results.py flomon_second.xml
This will output a clear summary of flows, delay, and jitter directly in your terminal.
Summary In this tutorial, we successfully simulated a P2P/CSMA hybrid network and analyzed it using five different methods. Mastering these tools gives you complete control over your network analysis in ns-3.
Thanks for reading! Please subscribe to the blog for more ns-3 tutorials.
Comments
Post a Comment