tc (3/4)
Part 3 of tc covers fair-queuing qdiscs (fq, fq_codel, fq_pie), class-tree shapers (htb, hfsc) and NIC queue mapping (mqprio), netem for emulating bad networks, the fw and matchall filters, and actions that move or rewrite packets (gate, ife, mirred, mpls, nat, pedit).
195.1 Why is it used?
- fq / fq_codel / fq_pie: fairness between flows and low queue delay; fq also paces TCP per flow.
- htb / hfsc: share a link through a tree of classes with guaranteed and maximum rates.
- netem: test apps under delay, jitter, loss, duplication, reordering and corruption.
- mirred + matchall: port mirroring (SPAN) or redirecting traffic (e.g. to ifb for ingress shaping).
- Linux only (iproute2 / iproute-tc), root needed; each qdisc/filter/action is a kernel module.
195.2 How it works
- Step 1
1.Server app sends a VoIP packet; iptables gives it fwmark 6
- Step 2
2.Packet reaches the root HTB qdisc 1:
- Step 3
3.fw filter sees mark 6 and picks the VoIP class
- Step 4
4.HTB class releases it according to rate and ceil
- Step 5
5.fq_codel on the leaf keeps the class queue fair and short
- Step 6
6.Packet goes to the NIC
- Step 1
1.Test client has netem delay 100ms loss 1%
- Step 2
2.Packet 1 waits 100 ms in the netem qdisc, then reaches the server
- Step 3
3.Next packet is randomly dropped (1% chance)
- Step 4
4.Server sees higher RTT and occasional loss
- Step 5
5.App behaviour under a weak network can be tested
- Step 1
1.Packet arrives on server eth0 ingress
- Step 2
2.matchall filter matches every packet
- Step 3
3.mirred egress mirror sends a copy to the monitor port
- Step 4
4.Monitor PC runs tcpdump on the copy
- Step 5
5.Original packet continues normally to the app
195.3 Practical: safe, help path only
uname -rzcat /proc/config.gz | grep -E \ -e 'NET_SCH_(HTB|HFSC|NETEM|MQPRIO)[= ]' \ -e 'NET_SCH_(FQ|FQ_CODEL|PIE)[= ]' \ -e 'NET_CLS_(FW|MATCHALL)[= ]' \ -e 'NET_ACT_(MIRRED|NAT|PEDIT)[= ]' \ -e 'NET_ACT_(MPLS|IFE|GATE)[= ]'grep -cE '^(sch_|cls_|act_)' /proc/modulestc qdisc show$ grep -cE '^(sch_|cls_|act_)' /proc/modules0$ tc qdisc showqdisc noqueue 0: dev lo root refcnt 2(exit 0)- Kernel 6.18: HTB, HFSC, netem, pedit ... are only modules; PIE, MPLS, IFE and gate are not even compiled. 0 tc modules loaded.
- The container runs with a seccomp profile blocking sendto/sendmsg/sendmmsg and non-route netlink sockets, and no capabilities, so even tc qdisc show fails with 'Cannot send dump request: Operation not permitted'.
tc qdisc add dev lo root netem helpstrace -f -qq -e trace=%network \ -o /tmp/st.txt \ tc qdisc add dev lo root netem helpcat /tmp/st.txtgrep -cE 'send(to|msg|mmsg)\(' /tmp/st.txt$ cat /tmp/st.txt415 bind(3, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 0415 getsockname(3, {sa_family=AF_NETLINK, nl_pid=415, nl_groups=00000000}, [12]) = 0$ grep -cE 'send(to|msg|mmsg)\(' /tmp/st.txt0- 'help' is parsed before any netlink request is built (checked in iproute2 6.9 source); strace shows bind/getsockname only and 0 send calls. So every usage shown below came from userspace tc.
195.4 Fair queuing: fq, fq_codel, fq_pie
| Qdisc | Key options | Good for |
|---|---|---|
| fq | limit 10000, flow_limit 100, quantum, maxrate, buckets, pacing/nopacing, ce_threshold | per-flow TCP pacing on servers |
| fq_codel | limit 10240, flows 1024, target 5ms, interval 100ms, quantum 1514, ecn, memory_limit, drop_batch | general default AQM, routers/hosts |
| fq_pie | target 15ms, tupdate 15ms, alpha/beta, ecn_prob, bytemode, dq_rate_estimator | PIE control with flow queuing |
#tc qdisc add dev eth0 root fq ce_threshold 4ms#tc -s -d qdisc show dev eth0qdisc fq 8001: dev eth0 root refcnt 2 limit 10000p flow_limit 100pbuckets 1024 orphan_mask 1023 quantum 3028b initial_quantum 15140blow_rate_threshold 550Kbit refill_delay 40.0ms ce_threshold 4.0ms Sent 72149092 bytes 48062 pkt (dropped 2176, overlimits 0requeues 0) backlog 1937920b 1280p requeues 0 flows 34 (inactive 17 throttled 0) gc 0 highprio 0 throttled 0 ce_mark 47622 flows_plimit 2176#tc qdisc add dev eth0 root fq_codel limit 2000 target 3msinterval 40ms noecn#tc -s qdisc showqdisc fq_codel 8003: dev eth0 root refcnt 2 limit 2000p flows 1024quantum 1514 target 3.0ms interval 40.0ms Sent 2588985006 bytes 1783629 pkt (dropped 0, overlimits 0requeues 34869) backlog 0b 0p requeues 34869 maxpacket 65226 drop_overlimit 0 new_flow_count 73 ecn_mark 0 new_flows_len 1 old_flows_len 3195.5 Shapers: htb, hfsc, mqprio
tc qdisc add dev eth0 root handle 1: htb default 30tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbittc class add dev eth0 parent 1:1 classid 1:10 htb rate 30mbit ceil 100mbit prio 1tc qdisc add dev eth0 parent 1:10 fq_codel # AQM on the leafiptables -t mangle -A OUTPUT ... -j MARK --set-mark 6tc filter add dev eth0 parent 1: handle 6 fw classid 1:10$ tc qdisc add dev lo root handle 1: htb helpWhat is "help"?Usage: ... qdisc add ... htb [default N] [r2q N] [direct_qlen P] [offload] default minor id of class to which unclassified packets are sent {0} r2q DRR quantums are computed as rate in Bps/r2q {10} debug string of 16 numbers each 0-3 {0} direct_qlen Limit of the direct queue {in packets} offload enable hardware offload... class add ... htb rate R1 [burst B1] [mpu B] [overhead O] [prio P] [slot S] [pslot PS] [ceil R2] [cburst B2] [mtu MTU] [quantum Q]| HTB option | Meaning |
|---|---|
| default 30 | unclassified traffic goes to 1:30 |
| rate | guaranteed bandwidth of the class |
| ceil | maximum, by borrowing the parent's spare bandwidth |
| burst / cburst | burst at ceil speed / at wire speed |
| prio | lower prio gets spare bandwidth first |
| r2q / quantum | quantum = rate / r2q (bytes per turn) |
$ tc class add dev lo parent 1: classid 1:10 hfsc helpUsage: ... hfsc [ [ rt SC ] [ ls SC ] | [ sc SC ] ] [ ul SC ] SC := [ [ m1 BPS ] d SEC ] m2 BPS m1 : slope of first segment d : x-coordinate of intersection- HFSC: rt = real-time guarantee, ls = link-sharing of spare bandwidth, sc = both, ul = upper limit; m1/d/m2 make a two-segment curve (burst then steady).
- mqprio: num_tc, map (priority -> TC), queues 1@0 1@1 2@2 (TC -> TX queues), hw 1 offloads to NIC, mode dcb|channel, shaper bw_rlimit with min_rate/max_rate.
195.6 netem
tc qdisc add dev eth0 root netem delay 100ms 10ms 25% loss 1%tc qdisc change dev eth0 root netem loss 0.1%# tc qdisc change dev eth0 root netem loss 0.1% This causes 1/10th of a percent (i.e 1 out of 1000) packets to be randomly dropped.| netem option | Effect |
|---|---|
| delay 100ms 10ms 25% | delay, jitter, correlation (distribution normal/pareto ...) |
| loss random 1% / state / gemodel | random, Markov burst or Gilbert-Elliot loss |
| corrupt / duplicate | bit flips / duplicate packets |
| reorder 25% 50% / gap 5 | some packets go early |
| rate 5kbit | bandwidth cap with overhead/cells |
| slot 800us 2ms | bursty MAC slot emulation |
| limit 1000, ecn | queue size; mark instead of drop |
195.7 Filters: fw and matchall
- fw: handle 6 fw classid 1:1 maps fwmark 6 to class 1:1; handle 0x8/0x8 matches masked bits; the mark comes from iptables -j MARK --set-mark 6.
- matchall: matches every packet; options action, classid, skip_sw, skip_hw.
tc qdisc add dev eth1 handle ffff: clsacttc filter add dev eth1 ingress \ matchall skip_sw \ action mirred egress mirror \ dev eth2195.8 Actions: mirred, nat, pedit, mpls, ife, gate
| Action | Does | Key options |
|---|---|---|
| mirred | mirror (copy) or redirect (move) to another device/block | ingress | egress, mirror | redirect, dev ifb0 |
| nat | stateless NAT, no conntrack | ingress OLD NEW (DNAT), egress OLD NEW (SNAT) |
| pedit | edit raw bytes or fields: eth, ip, ip6, tcp, udp | ex mode for IPv6/TCP/TTL; set, add, decrement, clear |
| mpls | push / pop / modify MPLS headers | label, tc, ttl, bos, dec_ttl, mac_push |
| ife | encode/decode metadata (mark, prio, tcindex) in an outer Ethernet header | encode, decode, allow/use, type 0xED3E |
| gate | 802.1Qci stream gate: open/close on a time schedule | base-time, clockid CLOCK_TAI, sched-entry open|close |
$ tc actions add action nat helpIllegal nat construct (help)Usage: ... nat NATNAT := DIRECTION OLD NEWDIRECTION := { ingress | egress }OLD := PREFIXNEW := ADDRESSbad action parsingparse_action: bad value (2:nat)!Illegal "action"(exit 255)# tc qdisc add dev eth0 ingress# tc filter add dev eth0 parent ffff: protocol ip \ flower skip_hw src_ip 192.168.0.20 \ action gate index 2 clockid CLOCK_TAI \ base-time 200000000000ns \ sched-entry open 200000000ns -1 8000000b \ sched-entry close 100000000ns195.9 Common mistakes
- Thinking fq_codel limits bandwidth: it gives fairness and low delay, it does not cap rate. Use HTB or tbf, with fq_codel on the HTB leaf.
- Forgetting HTB default: unclassified traffic then goes to the direct queue unshaped.
- Running netem on production interfaces and forgetting to remove it (tc qdisc del dev eth0 root).
- Using pedit on IPv6/TCP fields without ex, or without csum afterwards.
- nat action is stateless: replies are not translated automatically; add the reverse rule.
195.10 Interview tips
- HTB rate = guaranteed, ceil = max by borrowing spare bandwidth, prio = order for spare, default = unclassified class.
- Delay + loss test: netem delay 100ms loss 1%; add a cap with netem rate or HTB + netem.
- fq_codel = fairness + AQM, not a rate limiter.
- mirred mirror = SPAN copy; redirect = move (e.g. to ifb for ingress shaping).
- Proving safety: help is parsed in userspace, strace shows 0 send calls.