How to Calculate Network Gateways in NS2

To calculate the network gateways using the virtual environment NS2 (Network Simulator 2), which is normally performs as a node that connects two or more various network segments, and allowing the communication among them. Gateways frequently perform routing, protocol translation, or address translation among various networks. We can be replicated a gateway in NS2 by making a node, which connects various network segments and routes traffic among them. We deliver the step-by-step computation process to compute the gateway performance within NS2:

Steps to Simulate and Calculate Gateway Performance in NS2

  1. Set up a Gateway in NS2: A gateway can denote by a node, which connects various subnets or network segments. For instance, we can have two distinct networks connected via a central gateway node.
  2. Create an NS2 Simulation with a Gateway: The following is an instance NS2 script in which a gateway (n0) connects two network segments: n1, n2 on one side and n3, n4 on the other side.

Example NS2 Script to Simulate a Gateway

# 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 (n0 is the gateway, connecting two network segments)

set gateway [$ns node]   ;# Node acting as the gateway

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

# Create links between gateway and other nodes

$ns duplex-link $gateway $n1 1Mb 10ms DropTail

$ns duplex-link $gateway $n2 1Mb 10ms DropTail

$ns duplex-link $gateway $n3 1Mb 10ms DropTail

$ns duplex-link $gateway $n4 1Mb 10ms DropTail

# Set up traffic from n1 to n3 via gateway

set udp1 [new Agent/UDP]

$ns attach-agent $n1 $udp1

set null1 [new Agent/Null]

$ns attach-agent $n3 $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 traffic from n2 to n4 via gateway

set udp2 [new Agent/UDP]

$ns attach-agent $n2 $udp2

set null2 [new Agent/Null]

$ns attach-agent $n4 $null2

$ns connect $udp2 $null2

set cbr2 [new Application/Traffic/CBR]

$cbr2 set packetSize_ 500

$cbr2 set interval_ 0.01

$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

In this script:

  • Gateway node: n0 performs as the gateway, routing traffic among the two network segments.
  • Network segments: The initial segment contains the nodes n1 and n2, and the second segment consists of n3 and n4.
  • Traffic:
    • n1 transmits traffic to n3 via the gateway.
    • n2 sends traffic to n4 via the gateway.
  1. Gateway Performance Metrics

To compute the performance of a gateway, we normally concentrate on the metrics like:

  • Throughput: The total data effectively routed via the gateway.
  • Delay: The time packets are taken to pass through the gateway.
  • Packet loss: The number of packets are dropped at the gateway because of the congestion or buffer overflow.
  • Packet forwarding: The number of packets are effectively forwarded by the gateway.
  1. Capture Data from the Trace File

The trace file (out.tr) encompasses comprehensive details regarding packet events such as sending (+), receiving (-), and dropping (d). We can be used these events to compute the metrics for the gateway.

  1. Calculate Gateway Throughput

Throughput is the total amount of data effectively forwarded by the gateway. We can estimate it by counting the number of packets are forwarded by the gateway.

The following is an AWK script to compute the throughput at the gateway:

awk ‘

{

if ($1 == “-” && $3 == “gateway”) {

total_bytes += $6;  # Count the total bytes forwarded by the gateway

}

}

END { print “Throughput at Gateway:”, total_bytes / 5.0, “bytes/sec”; }’ out.tr

This script adds the number of bytes forwarded by the gateway node (n0) then divides it by the simulation time (5 seconds) to compute the throughput in bytes for each second.

  1. Calculate Delay through the Gateway

Delay is the time it takes for a packet to travel from one node to other node through the gateway. To estimate it, we require to follow the time a packet is transmitted and the time it arrives at its end.

Given below is an AWK script to estimate the average delay via the gateway:

awk ‘

{

if ($1 == “+”) {

send_time[$7] = $2;  # Store the send time using the packet ID ($7)

}

if ($1 == “-” && $3 == “gateway”) {

delay = $2 – send_time[$7];  # Calculate the delay (time at gateway – send time)

total_delay += delay;

count++;

}

}

END { print “Average Delay at Gateway:”, total_delay / count, “seconds”; }’ out.tr

This script evaluates the average delay for packets are passing via the gateway.

  1. Calculate Packet Loss at the Gateway

Packet loss happens when packets are dropped at the gateway by reason of congestion or buffer overflow. We can be counted the number of packet drop (d) events at the gateway.

The following is an AWK script to compute the packet loss at the gateway:

awk ‘

{

if ($1 == “d” && $3 == “gateway”) {

dropped_packets++;

}

}

END { print “Packet Loss at Gateway:”, dropped_packets, “packets”; }’ out.tr

This script counts the number of dropped packets at the gateway.

  1. Analyse Gateway Performance

When we have estimated the performance metrics then we can be evaluated the performance of the gateway:

  • High throughput: Shows that the gateway can effectively route traffic among the network segments.
  • Low delay: Intimates that the gateway is able to forward packets with minimal delay.
  • Low packet loss: Suggests that the gateway is not experiencing congestion or buffer overflows.

As illustrated above, we had demonstrated on how to successfully compute and analyse the Network Gateways using the computation technique in the virtual environment NS2. Moreover, we will provide further insights regarding this network gateway in various manual.

Share with us  your parameter details, and we will assist you with a performance analysis. Allow us to implement customized Network Gateways in the NS2 tool to achieve optimal results for your project.