How to Implement Network Traffic Shaping in NS2

To implement network traffic shaping in NS2 has includes controlling the rate at which packets are transfer via the network to enhance performance, reduce congestion, and make sure the quality of service (QoS). Traffic shaping permits to adjust the bandwidth usage by adjusting the key metrics like packet size, queue length, and transmission rates.

Traffic shaping can be executed in NS2 using different mechanisms such as:

  • Token Bucket or Leaky Bucket techniques to control bandwidth.
  • Queue management approaches to control traffic flows.
  • Setting bandwidth limits on certain links or agents.

Steps to Implement Network Traffic Shaping in NS2:

  1. Set up the Network Topology: Describe nodes and links.
  2. Control the Traffic: Adjust traffic flows using rate-limiting mechanisms or queue limits.
  3. Apply Queue Management: Handle the queues to make sure appropriate buffering and dropping policies.
  4. Monitor Performance: Evaluate traffic flow and measure throughput, latency, and packet drops.

Example Scenario: Traffic Shaping Using Queue Limit and Bandwidth Control

In this example, we will:

  • Configure a basic network in which one node sends traffic to another.
  • Use traffic shaping to control the bandwidth by adaptable the queue length and transmission rate.
  • Measure on how traffic shaping impacts the flow of data.

Example TCL Script for Network Traffic Shaping:

# Create a new simulator instance

set ns [new Simulator]

# Define the output trace file for packet tracing

set tracefile [open traffic_shaping.tr w]

$ns trace-all $tracefile

# Define animation file for NAM visualization (optional)

set namfile [open traffic_shaping.nam w]

$ns namtrace-all $namfile

# Create network nodes

set n0 [$ns node]  # Sender node

set n1 [$ns node]  # Receiver node

# Create a duplex link between nodes n0 and n1 with bandwidth, delay, and DropTail queue management

$ns duplex-link $n0 $n1 10Mb 20ms DropTail

# Set the queue limit to implement traffic shaping (regulate the buffer size)

$ns queue-limit $n0 $n1 10  # Limit the queue size to 10 packets

# Define UDP agent to send traffic from n0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Define Null agent as a sink at the receiver node n1

set null0 [new Agent/Null]

$ns attach-agent $n1 $null0

# Connect the UDP agent to the Null sink

$ns connect $udp0 $null0

# Define a Constant Bit Rate (CBR) traffic generator to create UDP traffic

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp0

# Set parameters for CBR traffic

$cbr set packetSize_ 1000  # Packet size of 1000 bytes

$cbr set rate_ 5Mb         # Data rate of 5Mb (this is where we control the rate of traffic flow)

$cbr set interval_ 0.02    # Interval between packets (50 packets per second)

# Start the CBR traffic at 0.5 seconds

$ns at 0.5 “$cbr start”

# Stop the traffic at 4.0 seconds

$ns at 4.0 “$cbr stop”

# Schedule simulation end at 5.0 seconds

$ns at 5.0 “finish”

# Define finish procedure

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam traffic_shaping.nam &

exit 0

}

# Run the simulation

$ns run

Explanation of the Script:

  1. Network Topology:
    • Two nodes are defined: n0 (sender) and n1 (receiver), associated by a duplex link with a bandwidth of 10Mb and delay of 20ms.
    • Traffic shaping is executed by configure a queue limit of 10 packets on the link using $ns queue-limit $n0 $n1 10. This makes sure that only 10 packets can be buffered at a time, and additional packets are dropped if the queue is full.
  2. Traffic Generation:
    • A UDP agent is attached to node n0, and a Null agent is attached to node n1 to behave as the receiver.
    • A CBR (Constant Bit Rate) application is used to create UDP traffic from n0 to n1. The traffic is setting up to send packets of size 1000 bytes at a rate of 5Mb per second.
    • The traffic initiates at 0.5 seconds and terminate at 4.0 seconds.
  3. Traffic Shaping:
    • Traffic shaping is performed by controlling the rate at which traffic is created (5Mb rate in CBR) and by setting a queue limit of 10 packets on the link. The queue limit makes sure that the network cannot buffer more than 10 packets, applying congestion control.
  4. Simulation Control:
    • The simulation executes for 5.0 seconds, and trace files are created for post-simulation analysis.
  1. Monitoring and Analysing the Trace File

After the simulation is done, the trace file (traffic_shaping.tr) will log events like packet transmission, reception, and drops. We can measure the trace file to evaluate the effects of traffic shaping on the network performance.

Important Metrics to Analyse:

  • Throughput: Measure on how much data was successfully transmitted over time.
  • Packet Drops: evaluate on how many packets were dropped because of the queue limit.
  • End-to-End Delay: Assess the delay experienced by packets as they travel from the sender to the receiver.

Example: Calculate Packet Drops Using awk

We can use an awk script to measure the number of dropped packets from the trace file:

awk ‘

$1 == “d” && $4 == “udp” {

dropped_packets += 1;

}

END {

print “Total Dropped Packets: ” dropped_packets;

}

‘ traffic_shaping.tr

This script counts the number of UDP packets that were dropped because of the queue being full.

  1. Advanced Traffic Shaping Techniques in NS2

We can execute more advanced traffic shaping mechanisms in NS2 by:

  • Token Bucket/Leaky Bucket: To mimic rate-limiting techniques that enables for burst traffic however enforce long-term rate control.
  • Weighted Fair Queuing (WFQ): Executing fair bandwidth sharing between multiple flows.
  • Traffic Policing: Implementing policies to drop or mark packets that go beyond the allowable rate.

Example: Implementing Traffic Policing with DropTail

# Set up a policing mechanism by adjusting queue length and buffer size

$ns queue-limit $n0 $n1 5 # smaller queue size to drop excess packets

This forces more aggressive packet drops if the traffic exceeds the network’s capacity.

  1. Simulating Multiple Traffic Flows with Traffic Shaping

We can expand the simulation to contain multiple traffic flows to see how traffic shaping affects the different types of flows.

Example: Multiple CBR Traffic Flows with Different Rates

# Define a second UDP agent and traffic flow

set udp1 [new Agent/UDP]

$ns attach-agent $n0 $udp1

set null1 [new Agent/Null]

$ns attach-agent $n1 $null1

$ns connect $udp1 $null1

# Define a second CBR traffic generator

set cbr2 [new Application/Traffic/CBR]

$cbr2 attach-agent $udp1

# Set parameters for the second CBR flow

$cbr2 set packetSize_ 500

$cbr2 set rate_ 3Mb    # Different rate for the second flow

$cbr2 set interval_ 0.01

# Start and stop the second CBR traffic

$ns at 0.8 “$cbr2 start”

$ns at 3.0 “$cbr2 stop”

This sample mimic two traffic flows (5Mb and 3Mb) to study how the queue manages competing flows.

From the demonstration, we concentrate on how to enhance the network performance, reduce congestion over the network traffic using the network traffic shaping characteristics that were implemented using ns2 tool. We will plan to offer the more information regarding the network traffic shaping.

We at ns2project.com implement the Network Traffic Shaping in NS2 tool you can approach us we provide you with on time results. Get comparison analysis done by our team with detailed explanation.