5. Configuring the traffic filters and queues

After the secondary route lookup table is in place, the next step is to configure the packet queues and filters. The iproute2 Linux kernel modules and tools are described in detail in the Linux Advanced Routing & Traffic Contro HOWTO.

5.1. HTB and SFQ packet queues

The HTB (Hierarchical Token Bucket) queue is used because it scales well and provides a nice default queue feature. Designating a default queue dumps packets into a queue if they aren't specifically filtered and put into another queue. SFQ (Stochastic Fairness Queueing) is added to each of the HTB queues. SFQ offers a reasonable level of equality (among different flows) for limiting traffic while keeping the overhead low. According to the documentation, it works well for queues that are consistently full. The route packet classifier is used to assign outbound packets to queues based on the realm from the routing table.

5.2. Sample traffic shaping script

The following is a sample traffic shaping init script. These configurations correspond to those used in the refresh-qos-routes.pl script described above.

Example 4. traffic.sh


#!/bin/sh
#
# traffic - script that configures network traffic shaping

extif=eth0

# line speed of the network interface
ifrate=1000mbit

# maximum traffic rate
maxrate=500mbit

# shaped limits
localrate=500mbit
i2rate=300mbit
i1rate=35mbit

TC=/sbin/tc

start() {
	# clear existing rules
	$TC qdisc del dev $extif root 2>/dev/null

	# root qdisc 1:0
	$TC qdisc add dev $extif root handle 1: \
		htb default 12

	# root class 1:1
	$TC class add dev $extif parent 1:0 classid 1:1 \
		htb rate $maxrate

	# class 1:10 -- local destinations
	$TC class add dev $extif parent 1:1 classid 1:10 \
		htb rate $localrate

	# class 1:11 -- I2 destinations
	$TC class add dev $extif parent 1:1 classid 1:11 \
		htb rate $i2rate

	# class 1:12 -- non-I2 destinations
	$TC class add dev $extif parent 1:1 classid 1:12 \
		htb rate $i1rate

	# qdisc defs for classes
	$TC qdisc add dev $extif parent 1:10 handle 10: \
		sfq quantum 1514b perturb 15
	$TC qdisc add dev $extif parent 1:11 handle 11: \
		sfq quantum 1514b perturb 15
	$TC qdisc add dev $extif parent 1:12 handle 12: \
		sfq quantum 1514b perturb 15

	# filter for 1:10 -- local destinations
	$TC filter add dev $extif parent 1:0 protocol ip pref 100 \
		route to 2 flowid 1:10

	# filter for 1:11 -- I2 routes
	$TC filter add dev $extif parent 1:0 protocol ip pref 100 \
		route to 5 flowid 1:11
}

stop() {
	# clear existing rules
	$TC qdisc del dev $extif root 2>/dev/null
}

status() {
	echo "qdisc:"
	$TC qdisc show dev $extif
	echo "filter:"
	$TC filter show dev $extif parent 1:
	echo "class:"
	$TC class show dev $extif
}

counts() {
	echo "qdisc:"
	$TC -s qdisc show dev $extif
	echo "class:"
	$TC -s class show dev $extif
}

case "$1" in
        start)
            start
            ;;
        
        stop)
            stop
            ;;
        
        status)
            status
            ;;
        counts)
            counts
            ;;
        restart)
            stop
            start
            ;;
        *)
            echo $"Usage: $0 {start|stop|restart|status|counts}"
            exit 1
esac

exit 0

      

5.3. Sample traffic shaping script explained

Four packet queues are created in the sample script -- one root queue and three child queues. Each queue is assigned a maximum bandwidth. When the queue rate limit is reached, outbound traffic is forcibly slowed. Since TCP-based applications adjust, this is very effective and doesn't sever the connections.

The filtering, which places packets into the appropriate queue, is based on the realm value assigned by the refresh-qos-routes.pl script. The script assigned realm value 5 to the Internet2 routes, and realm 2 to the local network routes. Thus, the route filter puts packets that match a local network route into the high bandwidth queue. Internet2 destination route packets go into the Internet2 queue. Any packet that doesn't match a route is dumped into the default HTB queue, which has a lower traffic limit.