How to Implement Network Traffic Analysis in NS2
To implement network traffic analysis in Network Simulator 2 (NS2) has need to replicate a network topology and create traffic among the nodes. After that, we can gather statistics like packet loss, throughput, latency, and other parameters by evaluating the trace files that NS2 creates during the simulation.
The given below is guide to implement the network traffic analysis in NS2:
Steps to Implement Network Traffic Analysis in NS2
- Set up NS2
Make sure NS2 is installed on the system. We need to install it using the following command on Ubuntu/Linux:
sudo apt-get update
sudo apt-get install ns2
- Create a Network Topology in NS2
For the purpose of network traffic analysis, we will start to generate a simple topology and make some traffic among nodes. This traffic can then be evaluated by investigative the trace file produced by NS2.
Example TCL Script for Network Traffic Simulation
The following TCL script generates a basic wired network topology and creates traffic among nodes using UDP and TCP. The trace file will be used for traffic analysis.
# Define the simulator
set ns [new Simulator]
# Open trace files for output
set tracefile [open traffic_analysis_out.tr w]
$ns trace-all $tracefile
# Define a network topology
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Create links with different characteristics between nodes
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 20ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
# Set up TCP traffic from node n0 to node n3
set tcp0 [new Agent/TCP]
set sink0 [new Agent/TCPSink]
$ns attach-agent $n0 $tcp0
$ns attach-agent $n3 $sink0
$ns connect $tcp0 $sink0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 0.5 “$ftp0 start”
$ns at 4.5 “$ftp0 stop”
# Set up UDP traffic from node n1 to node n2
set udp1 [new Agent/UDP]
set null1 [new Agent/Null]
$ns attach-agent $n1 $udp1
$ns attach-agent $n2 $null1
$ns connect $udp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 512
$cbr1 set interval_ 0.01
$cbr1 attach-agent $udp1
$ns at 1.0 “$cbr1 start”
$ns at 3.0 “$cbr1 stop”
# Schedule simulation end
$ns at 5.0 “finish”
# Finish procedure to close the simulation and trace files
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exec nam traffic_analysis_out.nam &
exit 0
}
# Run the simulation
$ns run
Explanation of the Script:
- Network Topology:
- Four nodes (n0, n1, n2, n3) are generated and interconnected with wired links of 10Mb bandwidth and changing delay.
- Traffic Setup:
- TCP traffic is created from n0 to n3 using a FTP application, that mimic file transfer over TCP.
- UDP traffic is created from n1 to n2 using a CBR (Constant Bit Rate) traffic generator.
- Trace File:
- The simulation output is recorded in a trace file (traffic_analysis_out.tr), that will later be used to evaluate network traffic.
- NAM Visualization:
- The NAM file is also generated for visualization (traffic_analysis_out.nam), though the concentrate here is on analyzing the trace file.
- Run the Simulation
Save the script as traffic_analysis.tcl and executed it in NS2:
ns traffic_analysis.tcl
This will create a trace file named traffic_analysis_out.tr encompassing elaborated data about the packets sent, received, dropped, and their timestamps.
- Analyze the Network Traffic
The trace file (traffic_analysis_out.tr) covers elaborated data about the network events that can be used to estimate diverse parameters like throughput, packet loss, delay, etc. The file contains lines with event types like sending, receiving, and dropping packets.
Each line in the trace file looks something like this:
r 1.000000000 _1_ AGT — 0 cbr 512 [0 0 0 0]
s 2.000000000 _0_ AGT — 1 tcp 1040 [0 0 0 0]
d 3.000000000 _2_ RTR — 2 tcp 1040 [0 0 0 0]
Where:
- r stands for receiving a packet.
- s stands for sending a packet.
- d stands for dropping a packet.
- The rest of the fields denotes node IDs, packet sizes, protocols, etc.
- Metrics for Network Traffic Analysis
We need to evaluate diverse contexts of the network traffic by processing the trace file.
- Throughput Calculation
Throughput is the rate at which data is successfully sends from one node to another. To estimate throughput:
- Filter all received packets (r events).
- Sum the size of all received packets for a specific flow (e.g., TCP).
- Divide the total size by the simulation time to acquire the throughput in Mbps.
- Packet Loss Calculation
Packet loss is estimated as the difference among the number of packets sent and the number of packets received.
- Count the number of sent packets (s events).
- Count the number of received packets (r events).
- Subtract the received packets from sent packets to acquire the packet loss.
- Delay Calculation
Delay can be estimated by evaluating the difference in timestamps among when a packet is sent and when it is received.
- For each packet, record the send time (s event).
- For the same packet, record the receive time (r event).
- Subtract the send time from the receive time to get the latency for each packet.
- Average the delays to get the overall average delay.
Example Python Script to Calculate Throughput:
We utilize a Python script to evaluate the trace file and estimate throughput, latency, and packet loss. Here’s a basic example:
def calculate_throughput(trace_file, sim_time):
with open(trace_file, ‘r’) as f:
received_packets = 0
total_bytes = 0
for line in f:
parts = line.split()
if parts[0] == ‘r’ and parts[3] == ‘AGT’:
packet_size = int(parts[5])
total_bytes += packet_size
received_packets += 1
throughput = (total_bytes * 8) / (sim_time * 1e6) # Throughput in Mbps
print(f”Total Bytes Received: {total_bytes} bytes”)
print(f”Throughput: {throughput:.2f} Mbps”)
print(f”Total Packets Received: {received_packets}”)
# Call the function with the trace file and simulation time (in seconds)
calculate_throughput(‘traffic_analysis_out.tr’, 5.0)
This script parses the trace file, filters all r (received) events for agent (application-level) packets, and estimates the total bytes received and the throughput.
We had clearly explained how to install, analyse, gather and implement the traffic data by using the ns2 tool and also we offer the sample script to complete the procedure. If you want any additional information regarding the network traffic analysis we will provide.
Check out ns2project.com, where we help you with Network Traffic Analysis for your NS2 projects. Just send us your project details, and we’ll assist you with performance analysis. We’re here to provide you with all the implementation support you need!