tc (2/4)
Part 2 of tc goes alphabetically from cake to flower: 17 qdiscs, filters and actions. They fall into five groups: AQM qdiscs against bufferbloat, schedulers that share bandwidth, TSN shapers for exact timing, filters that classify, and conntrack-related actions.
194.1 Why is it used?
- AQM qdiscs (CAKE, CoDel, CHOKe) keep queues short so a big upload does not push game/VoIP latency to hundreds of ms.
- Schedulers (CBQ, DRR, ETS) share bandwidth between classes by rate or quantum.
- TSN shapers (CBS, ETF) give exact timing for industrial and audio/video networks.
- Filters (cgroup, flow, flower, ematch) put packets in classes; actions (connmark, csum, ct, ctinfo) do extra work like conntrack, NAT and checksum fixes.
- Every feature is a kernel module (sch_codel, cls_flower, act_ct ...) loaded on first use; tc talks to it over netlink. Windows/macOS have no tc.
194.2 How it works
- Step 1
1.Laptop uploads a big file, router queue fills up
- Step 2
2.Phone's game packet waits behind the same queue: ping jumps to ~300 ms
- Step 3
3.CoDel measures how long each packet has waited (sojourn time)
- Step 4
4.If delay stays above target (5ms) for an interval (100ms), it drops/ECN-marks
- Step 5
5.CAKE adds a shaper + per-flow fairness, so each flow gets its own queue
- Step 6
6.Queue stays short, game ping stays low
- Step 1
1.Client SSH packet reaches the server's eth0 ingress
- Step 2
2.flower filter matches ip_proto tcp, dst_port 22
- Step 3
3.ct action sends it through connection tracking
- Step 4
4.connmark / ctinfo copy the conntrack mark into fwmark or DSCP
- Step 5
5.Control word (pipe, ok, drop) decides what happens next
194.3 Practical: kernel check and netlink guard
tc -Vzcat /proc/config.gz > /tmp/kgrep -E 'SCH_(CAKE|CBS|ETS|CODEL)[ =]' /tmp/kgrep -E 'CLS_FLOWER|ACT_C' /tmp/kgrep -cE '^(sch|cls|act|em)_' /proc/modules$ grep -cE '^(sch|cls|act|em)_' /proc/modules0- Container with --network none and no NET_ADMIN. The kernel config shows CAKE, CBS and ETS are not set in this kernel at all; codel/flower are modules, and 0 are loaded.
tc qdisc shownl-guard tc qdisc show dev lonl-guard nl-trace tc filter add flower help$ nl-guard nl-trace tc filter add flower helpsocket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE) = 3setsockopt(3, SOL_SOCKET, SO_SNDBUF, [32768], 4) = 0bind(3, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 0getsockname(3, {sa_family=AF_NETLINK, nl_pid=118, nl_groups=00000000}, [12]) = 0Where: MATCH-LIST := [ MATCH-LIST ] MATCH+++ exited with 1 +++- nl-guard is a small seccomp guard that blocks sendto/sendmsg, so even 'tc qdisc show' fails inside it.
- strace shows that 'help' only opens and binds a netlink socket; no message reaches the kernel. So all help output below is userspace-only and safe.
194.4 AQM qdiscs: cake, codel, choke
$ nl-guard tc qdisc add dev lo root cake helpUsage: ... cake [ bandwidth RATE | unlimited* | autorate-ingress ] [ rtt TIME | datacentre | lan | metro | regional | internet* | oceanic | satellite | interplanetary ] [ besteffort | diffserv8 | diffserv4 | diffserv3* ] [ flowblind | srchost | dsthost | hosts | flows | dual-srchost | dual-dsthost | triple-isolate* ] [ nat | nonat* ] [ wash | nowash* ] [ split-gso* | no-split-gso ] [ ack-filter | ack-filter-aggressive | no-ack-filter* ] [ memlimit LIMIT ] [ fwmark MASK ] [ ptm | atm | noatm* ] [ overhead N | conservative | raw* ] [ mpu N ] [ ingress | egress* ] (* marks defaults)- CAKE (61 options) = shaper + AQM + flow isolation in one: bandwidth, overhead/atm/ptm describe the link; rtt presets; diffserv4 etc. create priority tins; nat + dual-srchost/dual-dsthost give per-host fairness; ack-filter thins ACKs.
# tc qdisc add dev eth0 root codel limit 100 target 4ms interval 30ms ecn# tc -s qdisc show qdisc codel 801c: dev eth0 root refcnt 2 limit 100p target 4.0ms interval 30.0ms ecn Sent 237573074 bytes 268561 pkt (dropped 0, overlimits 0 requeues 5) backlog 0b 0p requeues 5 count 0 lastcount 0 ldelay 76us drop_next 0us maxpacket 2962 ecn_mark 0 drop_overlimit 0- CoDel: limit (1000p), target (acceptable standing delay, 5ms), interval (~worst RTT, 100ms), ecn/noecn, ce_threshold. Stats show ldelay, drop_next, ecn_mark, drop_overlimit.
- CHOKe: RED-like (limit, min, max, avpkt, burst, probability, ecn), but on a growing queue it picks a random packet and drops both if they belong to the same flow.
194.5 Schedulers: cbq, drr, ets
$ nl-guard tc qdisc add dev lo root cbq helpUnknown qdisc "cbq", hence option "help" is unparsable- CBQ (rate, allot, prio, avpkt, bounded, isolated, defmap) has been removed from the kernel and iproute2. Its classification loop: skb priority, then filters, then defmap, then BEST_EFFORT.
- DRR: each class has a quantum (bytes per round); there is no default class, so a filter is required.
- ETS (802.1Qaz): bands, strict bands first, then the rest share by quanta; priomap maps priority to band.
# tc class change dev eth0 classid 1:1 ets quantum 2650# tc qdisc show dev eth0qdisc ets 1: root refcnt 2 bands 8 quanta 2650 1514 1514 1514 1514 1514 1514 1514 priomap 7 6 5 4 3 2 1 0 7 7 7 7 7 7 7 7194.6 TSN shapers: cbs and etf
| Qdisc | Key options | What it does |
|---|---|---|
| cbs (802.1Q) | idleslope, sendslope, hicredit, locredit, offload 0|1 | credit-based shaper: credit grows while waiting, drops while sending |
| etf | clockid CLOCK_TAI, delta NANOS, deadline_mode, offload, skip_sock_check | sends each packet exactly at its SO_TXTIME, waking delta ns early |
194.7 Filters: cgroup, flow, flower, ematch
| Filter | Matches on | Note |
|---|---|---|
| cgroup | net_cls classid of the sending process (0x10002 = class 1:2) | locally generated traffic only |
| flow | map key KEY (direct) or hash keys src,dst,... (spread) | keys: src, dst, proto, mark, nfct-*, sk-uid, vlan-tag, rxhash |
| flower | MAC, VLAN, MPLS, ip_proto, IPs, ports, tcp_flags, ARP, tunnel enc_*, ct_state | 58 options; skip_sw offloads to NIC hardware |
| basic + ematch | cmp, meta, nbyte, u32, ipset, ipt, canid combined with and/or/not | meta(list) prints all metadata IDs |
$ nl-guard tc filter add basic match 'meta(list)' ID Type DescriptionGeneric: random INT Random value (32 bit) loadavg_1 INT Load average in last minuteInterfaces: dev INT,VAR Device the packet is onPacket attributes: priority INT Priority of packet protocol INT Link layer protocol pkt_len INT Length of packetNetfilter: nf_mark INT Netfilter markTraffic Control: tc_index INT TC Index vlan INT Vlan tag194.8 Actions: connmark, csum, ct, ctinfo
| Action | Does | Key options |
|---|---|---|
| connmark | restores conntrack mark into packet fwmark | zone, control word, index |
| csum | recomputes checksums after edits (e.g. after pedit changes dst IP) | ip4h, icmp, igmp, tcp, udp, udplite, sctp |
| ct | sends packet through conntrack; can NAT | commit, force, zone, mark, label, nat src/dst, clear |
| ctinfo | restores DSCP (or cpmark) saved in connmark | dscp MASK STATEMASK, cpmark, zone |
$ nl-guard tc actions add action ct helpUsage: ct clear ct commit [force] [zone ZONE] [mark MASKED_MARK] [label MASKED_LABEL] [nat NAT_SPEC] [helper HELPER] ct [nat] [zone ZONE]Where: ZONE is the conntrack zone table number NAT_SPEC is {src|dst} addr addr1[-addr2] [port port1[-port2]] HELPER is family-proto-name such as ipv4-tcp-ftptc filter add dev eth0 parent ffff: protocol all prio 10 u32 \ match u32 0 0 flowid 1:1 action \ ctinfo dscp 0xfc000000 0x01000000 \ mirred egress redirect dev ifb4eth0 action order 1: ctinfo zone 0 pipe index 2 ref 1 bind 1 dscp 0xfc000000 0x01000000 installed 72 sec used 0 sec DSCP set 1333 error 0 CPMARK set 0 Action statistics: Sent 658484 bytes 1833 pkt (dropped 0, overlimits 0 requeues 0)- In the docs example ctinfo restored DSCP on 1333 packets (DSCP set 1333) and redirected to ifb4eth0 for ingress shaping.
194.9 Common mistakes
- Thinking 'help printed' means the feature exists: help is the userspace parser; CAKE is not set in this kernel, so a real add fails.
- Thinking tc add is only a userspace setting: it is a netlink request and auto-loads kernel modules. Check /proc/modules and the kernel config, test in a lab first.
- Using CBQ in new setups: it is removed; use HTB/HFSC/DRR/ETS.
- Forgetting a filter with DRR (no default class), or csum after pedit.
194.10 Interview tips
- Bufferbloat fix: CAKE on egress with bandwidth slightly below real upload speed so the queue builds in your router, not the ISP modem.
- Downloads: CAKE ingress on an ifb device; with nat use dual-srchost / dual-dsthost for per-device fairness.
- Verify with tc -s qdisc show (drops, ecn_mark, delay).
- CoDel controls delay (target/interval), not queue length.
- flower with skip_sw = hardware-offloaded classification.