Traffic Control with HTB & CBQ

By agung

Linux offers up a real productive correct of instruments for dealing and controlling the transmitting of packages. The greater Linux community of interests is really acquainted the instruments usable below Linux for package mangling and firewalling (netfilter, and before that, ipchains) also as centuries of net avails which can keep going the OS. Few at bottom the community of interests and fewer largest the Linux community of interests are aware of the fantastic ability of the traffic control subsystem which has grown and matured under kernels 2.2 and 2.4.

I powerfully recommend to the great reviewer building a first raid the condition of traffic control, to go all casually acquainted the tc instruction line public utility, before revolving about tcng. The tcng software product delimitates an integral linguistic communication for discovering traffic control structures. At the start, this language dismissed dashing, but mastery of these basics will quickly provide the user with a much wider ability to employ (and deploy) traffic control configurations than the direct use of tc would afford.

Wherever potential, I’ll try to choose discovering the behaviour of the Linux traffic control system of rules in an abstract style, although in many cases I’ll need to append the syntax of one or the other common systems for specifying these structures. I may not supply good examples in both the tcng language and the tc command line, so the wise user will have some familiarity with both.
Examples :

#!/bin/bash
PATH=/usr/sbin:${PATH}
DEV=${DEV:-$6}

DOWNLINK=8192
UPLINK=1024
PRIODOWN=$[DOWNLINK/40]
MAXUP=$[UPLINK-DOWNLINK/40]
HALFUP=$[MAXUP/2]

TC=/sbin/tc

# clean existing down- and uplink qdiscs, hide errors
${TC} qdisc del dev $DEV root    2> /dev/null > /dev/null

###### uplink
# install root HTB, point default traffic to 1:30:
${TC} qdisc add dev $DEV root handle 1: htb default 30

# shape everything at $UPLINK speed - this prevents huge queues in your
# DSL modem which destroy latency:
${TC} class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k

# high prio class 1:10:
${TC} class add dev $DEV parent 1:1 classid 1:10 htb rate ${PRIODOWN}kbit burst 6k prio 0 ceil ${UPLINK}kbit

${TC} class add dev $DEV parent 1:1 classid 1:15 htb rate ${MAXUP}kbit burst 6k prio 1 ceil ${MAXUP}kbit

# bulk & default class 1:20 - gets slightly less traffic,
# and a lower priority:
${TC} class add dev $DEV parent 1:15 classid 1:20 htb rate ${HALFUP}kbit burst 6k prio 2 ceil ${MAXUP}kbit

${TC} class add dev $DEV parent 1:15 classid 1:30 htb rate ${HALFUP}kbit burst 6k prio 3 ceil ${MAXUP}kbit

# all get Stochastic Fairness:
${TC} qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
${TC} qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
${TC} qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10

${TC} filter add dev $DEV parent 1: protocol ip prio 1 handle 1 fw flowid 1:10
${TC} filter add dev $DEV parent 1: protocol ip prio 1 handle 2 fw flowid 1:20
${TC} filter add dev $DEV parent 1: protocol ip prio 1 handle 3 fw flowid 1:30

# TOS Minimum Delay (ssh, NOT scp) in 1:10:
${TC} filter add dev $DEV parent 1:0 protocol ip prio 10 u32 match ip tos 0x10 0xff  flowid 1:10

# ICMP (ip protocol 1) in the interactive class 1:10 so we
# can do measurements & impress our friends:
${TC} filter add dev $DEV parent 1:0 protocol ip prio 10 u32 match ip protocol 1 0xff flowid 1:10

# To speed up downloads while an upload is going on, put ACK packets in
# the interactive class:
${TC} filter add dev $DEV parent 1: protocol ip prio 10 u32 \
  match ip protocol 6 0xff \
  match u8 0x05 0x0f at 0 \
  match u16 0x0000 0xffc0 at 2 \
  match u8 0x10 0xff at 33 \
  flowid 1:10

# rest is 'non-interactive' ie 'bulk' and ends up in 1:30
${TC} filter add dev $DEV parent 1: protocol ip prio 20 u32 match ip dst 0.0.0.0/0 flowid 1:30

Tags: , , ,

Leave a Reply