How to Calculate Network Accounting in NS2

To calculate the Network Accounting in Network Simulator 2 (NS2), we need to track the use of network resources including bandwidth, traffic and data consumption for different nodes or services. In NS2, we can replicate it by estimating and logging several network parameters like packet transmission, packet reception, data throughput, packet loss and latency. It permits you to evaluate how various nodes or users are utilizing network assets, which is vital for billing, network management and performance analysis. Here, we offered the details for you to calculate it using ns2:

Steps to Simulate and Calculate Network Accounting in NS2

To calculate network accounting in NS2, you will:

  1. Set up the network topology where traffic is created amongst various nodes.
  2. Observe traffic for each node, tracking metrics like bandwidth usage, packet counts, and dropped packets.
  3. Capture trace data to log network events like packet transmissions and receptions.
  4. Analyze metrics to estimate bandwidth usage, throughput, and packet loss for each node.
  1. Set Up the NS2 Simulation

In this instance, we will simulate several nodes interacting with one another, and we will track the number of data sent and obtained by each node to compute network accounting metrics.

Example NS2 Script for Network Accounting

# Create NS2 simulator instance

set ns [new Simulator]

# Open trace file for output

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Define nodes

set node1 [$ns node]    ;# First node

set node2 [$ns node]    ;# Second node

set node3 [$ns node]    ;# Third node

# Create links between nodes

$ns duplex-link $node1 $node2 1Mb 10ms DropTail

$ns duplex-link $node2 $node3 1Mb 10ms DropTail

# Set up UDP traffic from node1 to node3

set udp1 [new Agent/UDP]

$ns attach-agent $node1 $udp1

set null1 [new Agent/Null]

$ns attach-agent $node3 $null1

$ns connect $udp1 $null1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set interval_ 0.01

$cbr1 attach-agent $udp1

# Set up UDP traffic from node2 to node3

set udp2 [new Agent/UDP]

$ns attach-agent $node2 $udp2

set null2 [new Agent/Null]

$ns attach-agent $node3 $null2

$ns connect $udp2 $null2

set cbr2 [new Application/Traffic/CBR]

$cbr2 set packetSize_ 500

$cbr2 set interval_ 0.02

$cbr2 attach-agent $udp2

# Start and stop traffic

$ns at 0.5 “$cbr1 start”

$ns at 0.5 “$cbr2 start”

$ns at 4.5 “$cbr1 stop”

$ns at 4.5 “$cbr2 stop”

# End simulation

$ns at 5.0 “finish”

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# Run the simulation

$ns run

Explanation of the Script:

  • Nodes: Three nodes (node1, node2, and node3) are designed, with node3 behaving like a destination for traffic from node1 and node2.
  • Traffic: Traffic is produced from node1 to node3 and from node2 to node3 using UDP agents.
  • Links: Duplex links are developed amongst the nodes with 1 Mbps bandwidth and 10 ms delay.
  1. Monitor Traffic

Execute the network accounting by monitoring the traffic flowing through each node. The trace file producing by NS2 will log all events like packet transmissions, receptions, and drops. These events can be assessed to measure the total of traffic each node sends, gets, and drops.

  1. Capture Data from the Trace File

The trace file (out.tr) has material about all packets sent, received, or dropped in the network. Each event in the trace file offers details include the time, source node, destination node, packet size, and packet type. This data can be used to quantify accounting metrics for each node.

Example trace file entry:

+ 0.500000 0 1 udp 500 ——- 1 0.0 0.0 1.0

– 0.510000 0 1 udp 500 ——- 1 0.0 0.0 1.0

d 0.520000 0 1 udp 500 ——- 1 0.0 0.0 1.0  # Packet drop

  1. Calculate Network Accounting Metrics

Total Data Sent by Each Node

You can calculate the total number of data delivered by each node by counting the sizes of the packets transmitted.

Here’s an AWK script to estimate the total data sent by each node:

awk ‘

{

if ($1 == “+” && $3 == “node1”) {  # Node 1 sends a packet

node1_sent += $6;              # Add packet size to total sent by node 1

}

if ($1 == “+” && $3 == “node2”) {  # Node 2 sends a packet

node2_sent += $6;              # Add packet size to total sent by node 2

}

}

END {

print “Total Data Sent by Node 1:”, node1_sent, “bytes”;

print “Total Data Sent by Node 2:”, node2_sent, “bytes”;

}’ out.tr

This script calculates the total number of bytes sent by node1 and node2 by summing the packet sizes ($6) of all packets sent (+).

Total Data Received by Each Node

Similarly, you can calculate the total data acquired by totalling the sizes of the packets that each node receives.

Here’s an AWK script to calculate the total data received by each node:

awk ‘

{

if ($1 == “-” && $4 == “node3”) {  # Node 3 receives a packet

node3_received += $6;          # Add packet size to total received by node 3

}

}

END {

print “Total Data Received by Node 3:”, node3_received, “bytes”;

}’ out.tr

This script computes the overall number of bytes received by node3 by summing the packet sizes of all obtained packets.

Total Packets Dropped by Each Node

You can also track how many packets were dropped by each node. This is useful for understanding network congestion or detecting performance issues.

Here’s an AWK script to calculate packet drops at each node:

awk ‘

{

if ($1 == “d” && $3 == “node1”) {  # Packet dropped by node 1

node1_drops++;

}

if ($1 == “d” && $3 == “node2”) {  # Packet dropped by node 2

node2_drops++;

}

if ($1 == “d” && $3 == “node3”) {  # Packet dropped by node 3

node3_drops++;

}

}

END {

print “Total Packets Dropped by Node 1:”, node1_drops;

print “Total Packets Dropped by Node 2:”, node2_drops;

print “Total Packets Dropped by Node 3:”, node3_drops;

}’ out.tr

This script sums how many packets were dropped by each node by looking for d (drop) events.

Throughput for Each Node

Throughput is the rate at which data is successfully transferred or obtained by a node. It can be calculated as the total data transmitted or received over a specific time period.

Here’s an AWK script to calculate the throughput for each node:

awk ‘

{

if ($1 == “-” && $4 == “node3”) {  # Node 3 receives a packet

total_bytes_received += $6;

}

}

END {

print “Throughput for Node 3:”, total_bytes_received / 5.0, “bytes/sec”;  # Divide by simulation time (5 seconds)

}’ out.tr

This script estimates the throughput for node3 by counting the total number of bytes acquired and dividing it by the total simulation time (5 seconds).

  1. Visualize Network Accounting Data

You can plot the network accounting metrics (like data sent, received, or dropped) over time using tools like Python (matplotlib) or Excel.

Example Python Plot for Throughput:

import matplotlib.pyplot as plt

# Example data for throughput over time

time = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

throughput = [600, 650, 700, 750, 800, 850, 900, 950, 1000]  # Example throughput data (bytes/sec)

plt.plot(time, throughput, marker=’o’)

plt.title(‘Throughput Over Time for Node 3’)

plt.xlabel(‘Time (seconds)’)

plt.ylabel(‘Throughput (bytes/sec)’)

plt.grid(True)

plt.show()

Summary

To measure network accounting in NS2:

  1. Set up the simulation: State the nodes and traffic flows, and build links amongst nodes.
  2. Monitor traffic: Trace file is used to log packet transmissions, receptions, and drops.
  3. Analyze metrics: Use AWK scripts to compute entire data sent, received, dropped, and throughput for each node.
  4. Visualize the data: Plot the outputs using tools like Python or Excel to get a clear view of network consumed over time.

From this process, you can gain the knowledge of how to compute the accounting in the network using the ns2 tool by observing the consumption of network resources like bandwidth, etc,. It also provides sample snippets and its evaluation process.

We assist you in calculating Network Accounting using the NS2 tool for your project ideas and topics that we share. Let our experts handle it for you to achieve the best results. Our developers are here to support you in tracking various network parameters, including packet transmission, packet reception, data throughput, packet loss, and latency