How to Implement Network Packet Scheduling in NS2
To implement network packet scheduling in NS2, we have to mimic how packets are queued and routed according to different scheduling algorithms such as FIFO, Priority Queuing, Round Robin, Weighted Fair Queuing, etc. NS2 supports some simple queuing mechanisms by default; however we can customize the packet scheduling to fits the particular scenario. Below is the brief procedure to achieve this process through ns2:
Step-by-Step implementation:
- Understand Packet Scheduling Basics:
Packet scheduling is a technique used in routers and switches to decide which packet should be sent next from a queue. Some common scheduling techniques are:
- FIFO (First In, First Out): Packets are processed in the order they arrive.
- Priority Queueing: Packets are classified into different priority queues, with higher-priority packets sent first.
- Round Robin: Packets are sent in a rotating order via queues.
- Weighted Fair Queueing (WFQ): Queues are serviced based on allocated weights.
In NS2, packet scheduling is mainly managed using queues. Queues can be attached to nodes, and the scheduling behaviour is controlled by the queue type that specify.
- Basic NS2 Setup:
Initiate by generating a simple network topology with nodes and links in which packet scheduling will be implemented.
set ns [new Simulator]
# Create trace files
set tracefile [open “tracefile.tr” w]
$ns trace-all $tracefile
# Define nodes
set node1 [$ns node]
set node2 [$ns node]
- Specify the Queue Type (Scheduling Algorithm):
NS2 supports different types of queue scheduling restraints that can be attached to links among the nodes. Some built-in queue types are:
- DropTail (FIFO)
- RED (Random Early Detection)
- CBQ (Class-Based Queuing)
Here’s how to describe a basic FIFO queue:
# Define a link with DropTail queue (FIFO) between node1 and node2
$ns duplex-link $node1 $node2 1Mb 10ms DropTail
Example for Priority Queueing:
For priority queuing, NS2 can identify packets according to priority levels. we can use the Queue/DropTail/PriQueue to implement this:
# Define a priority queue between node1 and node2
set ifq [new Queue/DropTail/PriQueue]
# Attach priority queue to the link
$ns duplex-link $node1 $node2 1Mb 10ms $ifq
We can classify traffic to different priority levels by connecting the packets or agents with different priorities.
- Create Traffic Generators:
To see the impacts of packet scheduling, we need to create traffic from the source to the destination. Different traffic types such as UDP, TCP can be generated.
Example for UDP traffic:
# Create a UDP agent and attach to node1
set udp [new Agent/UDP]
$nsa attach-agent $node1 $udp
# Create a traffic source (CBR)
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 1000
$cbr set rate_ 500Kb
$cbr attach-agent $udp
# Create a null agent (sink) and attach to node2
set null [new Agent/Null]
$ns attach-agent $node2 $null
# Connect UDP agent to null agent
$ns connect $udp $null
# Start and stop traffic
$ns at 1.0 “$cbr start”
$ns at 5.0 “$cbr stop”
- Implement Weighted Fair Queuing (WFQ):
WFQ permits to allocate diverse weights to queues because of that packets are scheduled according to assigned weights. NS2 doesn’t have a direct WFQ implementation by default, however we can mimic a similar behaviour using CBQ (Class-Based Queuing).
# Define a Class-Based Queue (CBQ)
set cbq [new Queue/CBQ]
# Attach CBQ to a link between node1 and node2
$ns duplex-link $node1 $node2 1Mb 10ms $cbq
In CBQ, packets can be identified into different classes, and each class can be allocated a different weight.
- Set Traffic Prioritization (for Priority Queueing):
If we are using priority queues, we can set different traffic priorities for different flows.
Example for setting priority:
# Set the priority for UDP traffic
$udp set prio_ 1 ; # Higher priority
- Monitor and Analyze Performance Metrics:
To monitor the impacts of different packet scheduling techniques, that can observe throughput, delay, packet drops, etc., using trace files.
Example to enable tracing:
# Enable trace for queue monitoring
$ns trace-queue $node1 $node2 [open “queue.tr” w]
# Start simulation
$ns run
After executing the simulation, we can evaluate the trace file to monitor on how different scheduling algorithms that impacts performance.
- Example of Packet Scheduling with Multiple Queues:
Below is an sample simulation script that executes a basic priority queue and multiple traffic sources with different priorities.
# Create the simulator
set ns [new Simulator]
# Create trace files
set tracefile [open “tracefile.tr” w]
$ns trace-all $tracefile
# Define nodes
set node1 [$ns node]
set node2 [$ns node]
# Define a link with priority queue between node1 and node2
$ns duplex-link $node1 $node2 1Mb 10ms Queue/DropTail/PriQueue
# Create a UDP agent for high priority traffic
set udp1 [new Agent/UDP]
$ns attach-agent $node1 $udp1
$udp1 set prio_ 1 ;# High priority
# Create a traffic source (high priority)
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 1000
$cbr1 set rate_ 700Kb
$cbr1 attach-agent $udp1
# Create a sink for high priority traffic
set null1 [new Agent/Null]
$ns attach-agent $node2 $null1
$ns connect $udp1 $null1
# Create a UDP agent for low priority traffic
set udp2 [new Agent/UDP]
$ns attach-agent $node1 $udp2
$udp2 set prio_ 2 ;# Low priority
# Create a traffic source (low priority)
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 1000
$cbr2 set rate_ 200Kb
$cbr2 attach-agent $udp2
# Create a sink for low priority traffic
set null2 [new Agent/Null]
$ns attach-agent $node2 $null2
$ns connect $udp2 $null2
# Start the high priority traffic at time 1.0s
$ns at 1.0 “$cbr1 start”
# Start the low priority traffic at time 2.0s
$ns at 2.0 “$cbr2 start”
# Stop the traffic at time 5.0s
$ns at 5.0 “$cbr1 stop”
$ns at 5.0 “$cbr2 stop”
# Finish procedure
proc finish {} {
global ns tracefile
$ns flush-trace
close $tracefile
exit 0
}
# Schedule finish
$ns at 6.0 “finish”
# Run the simulation
$ns run
We had demonstrated how to setup the simulation and how to implement the network packet scheduling in the network using the ns2 tool. We will deliver more information about the network packet scheduling performance in other scenario. Get in contact with us so we can assist you with implementing your projects and we also guarantee best results.