6.2.4. Example: Linux Packet Scheduling

Linux uses queuing disciplines associated with network devices to determine how packets should be scheduled. Some queuing disciplines can combine other queuing disciplines. Queueing disciplines are connected through classes. Filters tell what packets go to what class.

# Root qdisc is prio with 3 bands
tc qdisc add dev ppp0 root handle 1: prio bands 3

# Band 1 qdisc is sfq and filter is ICMP & SSH & DNS & outbound HTTP
tc qdisc add dev ppp0 parent 1:1 sfq perturb 16
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip protocol 1 0xff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip sport 22 0xffff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip dport 22 0xffff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip sport 53 0xffff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip dport 53 0xffff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip sport 80 0xffff flowid 1:1
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip sport 443 0xffff flowid 1:1

# Band 2 qdisc is sfq and filter is anything unfiltered
tc qdisc add dev ppp0 parent 1:2 sfq perturb 16
tc filter add dev ppp0 parent 1: protocol ip prio 9 u32 match u8 0 0 flowid 1:2

# Band 3 qdisc is tbf and filter is outbound SMTP
tc qdisc add dev ppp0 parent 1:3 tbf rate 128kbit buffer 100000 latency 100s
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip dport 25 0xffff flowid 1:3
tc filter add dev ppp0 parent 1: protocol ip prio 1 u32 match ip dport 465 0xffff flowid 1:3

The example first attaches a priority queuing discipline to ppp0. The queuing discipline distinguishes three priority bands and schedules higher priority bands before lower priority bands.

Next, the example attaches a shared fair queuing discipline as a child of the priority queuing discipline with priority 1. The queuing discipline schedules packets from multiple streams in round robin manner. A series of filters then tells that ICMP (IP protocol 1), SSH (port 22), DNS (port 53) and outgoing web replies (port 80) packets belong to class 1:1, which is this queuing discipline.

Next, the example attaches a shared fair queuing discipline as a child of the priority queuing discipline with priority 2. A filter then tells that all packets belong to class 1:2, which is this queuing discipline. The filter has a priority 9 as opposed to priority 1 of other filters, this makes it the last filter matched.

Next, the example attaches a token bucket discipline as a child of the priority queuing discipline with priority 3. The queuing discipline schedules packets with a bandwidth limit. A pair of filters then tells that outgoing SMTP (port 80) packets belong to class 1:3, which is this queuing discipline.

Together, the filter tells Linux to first send ICMP, SSH, DNS and outgoing web replies to ppp0. If there are no such packets, the filter tells Linux to send any packets except outgoing SMTP. If there are no such packets, the filter tells Linux to send outgoing SMTP with a bandwidth limit.