Skip to main content

ns-3 Tutorial: Simulating P2P & CSMA Networks with NetAnim, Wireshark, and FlowMonitor

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:

  1. NetAnim (Network Animation)

  2. AsciiTraceHelper (TraceMetrics-1.4.0)

  3. Gnuplot (Graphing data)

  4. Packet Capture (Wireshark & .pcap files)

  5. 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:

Bash
cd ns-allinone-3.44/ns-3.44
cp examples/tutorial/second.cc scratch/

Now, let's verify that the basic simulation works:

Bash
./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:

C++
#include "ns3/netanim-module.h"

Add the following line strictly before Simulator::Run():

C++
AnimationInterface anim("animation_second.xml");

Run the simulation:

Bash
./ns3 run scratch/second.cc

View the Animation: Open a new terminal to run the NetAnim tool:

Bash
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():

C++
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("p2p.tr"));
csma.EnableAsciiAll(ascii.CreateFileStream("csma.tr"));

Run the simulation:

Bash
./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.

Bash
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:

Plaintext
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:

Code snippet
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:

Bash
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):

C++
pointToPoint.EnablePcapAll("p2p");
csma.EnablePcapAll("csma");

Run and Analyze:

Bash
./ns3 run scratch/second.cc

To view the output, you can use Wireshark GUI or TCPDump in the terminal:

Bash
# 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:

C++
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"

Add this block before Simulator::Run():

C++
// Flow monitor
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
Simulator::Stop(Seconds(10));

Add this line after Simulator::Run():

C++
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.

  1. Copy the parser to your main directory for easy access:

    Bash
    cp src/flow-monitor/examples/flowmon-parse-results.py .
    
  2. 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

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libc...

Installation of NS2 in Ubuntu 22.04 | NS2 Tutorial 2

NS-2.35 installation in Ubuntu 22.04 This post shows how to install ns-2.35 in Ubuntu 22.04 Operating System Since ns-2.35 is too old, it needs the following packages gcc-4.8 g++-4.8 gawk and some more libraries Follow the video for more instructions So, here are the steps to install this software: To download and extract the ns2 software Download the software from the following link http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download Extract it to home folder and in my case its /home/pradeepkumar (I recommend to install it under your home folder) $ tar zxvf ns-allinone-2.35.tar.gz or Right click over the file and click extract here and select the home folder. $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev gawk To install gcc-4.8 and g++-4.8 $ sudo gedit /etc/apt/sources.list make an entry in the above file deb http://in.archive.ubuntu.com/ubuntu/ bionic main universe $ sudo apt update Since, it...

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide]

How to Create Ubuntu 24.04 Bootable USB Using Rufus [Step-by-Step Guide] Are you planning to install or try Ubuntu 24.04 LTS ? The easiest and most reliable method is to create a bootable USB drive using Rufus on a Windows system. This detailed guide will help you create a Ubuntu 24.04 USB bootloader using Rufus with easy-to-follow steps and screenshots (optional). Here is the complete video of the bootloader creation and OS installation in Windows 11. 🧰 Requirements A USB flash drive (minimum 8GB recommended) A Windows PC Ubuntu 24.04 LTS ISO file Rufus USB creation tool 🧾 Steps to Create a Ubuntu 24.04 Bootable USB Using Rufus ✅ Step 1: Download Ubuntu 24.04 ISO Visit the official Ubuntu website and download the Ubuntu 24.04 LTS ISO file . ✅ Step 2: Download and Run Rufus Head to Rufus official site and download the latest version. Open the executable file (no installation required). ✅ Step 3: Insert USB Drive Plug in your USB drive. Rufus ...