tc (4/4)
The last part of tc completes the remaining qdiscs (pie, prio, red, sfb, sfq, skbprio, taprio, tbf and the stab size table), filters (route, tcindex, u32) and actions (police, sample, simple, skbedit, skbmod, tunnel_key, vlan, xt). The key ideas are shaping vs policing, strict priority and the u32 classifier.
196.1 Why is it used?
- tbf caps the rate of an interface (shaping); police drops traffic above a limit (policing).
- prio gives strict priority (VoIP before bulk); sfq/sfb give fairness between flows; red/pie keep the average queue short.
- taprio builds an 802.1Qbv time schedule for TSN; stab corrects packet sizes (e.g. ATM cells on ADSL).
- u32 matches any bits in a packet; actions edit metadata, VLAN tags, tunnel keys or sample packets.
- Lab kernel (WSL2 6.18): these are modules not loaded or not built, so only the help path was run.
196.2 How it works
- Step 1
1.Client app packet arrives at the tbf qdisc on eth0
- Step 2
2.Bucket is filled with tokens at the configured rate
- Step 3
3.Token available: packet is forwarded to the server immediately
- Step 4
4.Bucket empty: next packet waits in the queue for new tokens
- Step 5
5.rate caps the speed; burst decides how much can leave in one go
- Step 1
1.prio has 3 bands; band 0 always goes out first
- Step 2
2.VoIP packet is in band 0, bulk download in band 2
- Step 3
3.VoIP reaches the server first; bulk waits
- Step 4
4.On ingress, police drops a flood packet above the limit right there
- Step 5
5.Heavy band 0 traffic can starve band 2: put tbf under the high band
196.3 Lab: kernel check and guard
- One container n196-tc: --network none, 64 MB memory, no capabilities. prio and tbf are modules; pie and taprio not built; tcindex and act_ipt not in the config at all; 0 modules loaded.
- Two seccomp layers block every netlink send, so even tc qdisc show returns Operation not permitted.
grep -E '^(Seccomp|CapEff)' /proc/self/statusnl-guard tc qdisc showstrace -f -qq -e trace=%network,write,writev \ -o /tmp/st.txt nl-guard tc qdisc add \ dev lo root pie helpgrep -cE 'send(to|msg|mmsg)\(' /tmp/st.txt146 socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE) = 3146 setsockopt(3, SOL_SOCKET, SO_SNDBUF, [32768], 4) = 0146 setsockopt(3, SOL_SOCKET, SO_RCVBUF, [1048576], 4) = 0146 setsockopt(3, SOL_NETLINK, NETLINK_EXT_ACK, [1], 4) = 0146 bind(3, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 0146 getsockname(3, {sa_family=AF_NETLINK, nl_pid=146, nl_groups=00000000}, [12]) = 0send* syscalls: 0, write on fd 3: 0196.4 AQM qdiscs: pie, red, sfb, sfq
# tc qdisc add dev eth0 root pie# tc -s qdisc show qdisc pie 8036: dev eth0 root refcnt 2 limit 1000p target 15.0ms tupdate 16.0ms alpha 2 beta 20 Sent 31216108 bytes 20800 pkt (dropped 80, overlimits 0 requeues 0) backlog 16654b 11p requeues 0 prob 0.006161 delay 15666us pkts_in 20811 overlimit 0 dropped 80 maxq 50 ecn_mark 0| Qdisc | Key options | Idea |
|---|---|---|
| pie | limit, target 15ms, tupdate, alpha/beta, ecn, bytemode, dq_rate_estimator | drop probability driven by queue delay (prob, delay in stats) |
| red | limit, min, max (>= 2x min), avpkt, burst, probability, ecn, harddrop, adaptive, qevent | random early drop/mark between min and max average queue |
| sfb | rehash, db, limit, max, target, increment, decrement, penalty_rate, penalty_burst | per-bin marking probability; bad flows limited to penalty rate |
| sfq | limit, perturb, quantum, divisor, flows, depth, headdrop, redflowlimit | round robin per flow, hash perturbed; optional per-flow RED |
tc qdisc add dev eth0 parent 1:1 handle 10: red \ limit 400000 min 30000 max 90000 avpkt 1000 \ burst 55 ecn adaptive bandwidth 10Mbittc qdisc add dev eth0 parent 1:1 handle 10: sfq \ limit 3000 flows 512 divisor 16384 \ redflowlimit 100000 min 8000 max 60000 \ probability 0.20 ecn headdroptc qdisc add dev $DEV handle 1: root sfbtc filter add dev $DEV parent 1: handle 1 flow hash keys dst perturb 600196.5 Priority: prio and skbprio
$ nl-guard tc qdisc add dev lo root prio helpUsage: ... prio bands NUMBER priomap P1 P2...[multiqueue](exit 1)- prio: 3 bands by default (1:1, 1:2, 1:3); priomap maps the 16 packet priorities to bands; filters can send traffic straight to a band; each band can have a child qdisc (e.g. tbf).
- skbprio limit 128: 64 priority lists; when full, the lowest-priority packet is dropped (useful during DoS). Pair with skbedit inheritdsfield.
196.6 Shaping and scheduling: tbf, taprio, stab
$ nl-guard tc qdisc add dev lo root tbf rate 1mbit burst 32kbittbf: either "limit" or "latency" is required.Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ] [ peakrate KBPS ] [ latency TIME ] [ overhead BYTES ] [ linklayer TYPE ](exit 1)- tbf: rate and burst are mandatory, plus either latency or limit; peakrate/mtu add a second bucket. The missing-latency error came from the parser, nothing reached the kernel.
- taprio (802.1Qbv): num_tc, map, queues, base-time, clockid CLOCK_TAI, sched-entry S <gate-mask> <ns>; flags 0x1 txtime-assist, 0x2 full offload.
- stab is a size table, not a qdisc: mtu, tsize, mpu, overhead, linklayer ethernet|atm|adsl.
tc qdisc add dev eth0 handle 10: root tbf \ rate 0.5mbit burst 5kb latency 70ms \ peakrate 1mbit minburst 1540tc qdisc add dev eth0 parent 10:1 handle 100: sfqtc qdisc replace dev eth0 parent root handle 100 stab overhead 24 taprio \ num_tc 3 map 2 2 1 0 2 2 2 2 2 2 2 2 2 2 2 2 \ queues 1@0 1@1 2@2 \ base-time 1528743495910289987 \ sched-entry S 01 300000 \ sched-entry S 02 300000 \ sched-entry S 04 300000 \ clockid CLOCK_TAI196.7 Filters: u32, route, tcindex
tc filter add dev eth0 parent 999:0 prio 99 protocol ip u32 \ match ip src 192.168.8.0/24 classid 1:1ip route add 192.168.2.0/24 dev eth0 realm 2tc filter add ... route from 2 classid 1:2filter parent 1: protocol ip pref 99 u32filter parent 1: protocol ip pref 99 u32 fh 800: ht divisor 1filter parent 1: protocol ip pref 99 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 match c0a80800/ffffff00 at 12- u32 options: handle, divisor (hash table size, max 256), ht, link (jump to table), hashkey, offset, classid, order, indev, skip_sw/skip_hw; several match selectors are ANDed.
- u32 selectors: raw u32/u16/u8 VAL MASK at N, plus shortcuts ip src/dst/dport/protocol/tos, ip6 ..., tcp/udp src/dst, icmp, mark, ether src/dst.
- The show output turns 'ip src 192.168.8.0/24' into 'match c0a80800/ffffff00 at 12' (source IP sits at offset 12 of the IPv4 header).
- route: matches routing realms set by ip route ... realm 2 (from, fromif, to).
$ nl-guard tc filter add block 1 tcindex helpUnknown filter "tcindex", hence option "help" is unparsable(exit 1)196.8 Actions
| Action | Does | Key options |
|---|---|---|
| police | token-bucket policing; drops or reclassifies excess | rate, burst, pkts_rate, mtu, peakrate, conform-exceed drop/ok/reclassify/pipe |
| sample | copies 1 in N packets to a psample group | rate 100, group 12, trunc, index |
| simple | prints sdata string to the kernel log (teaching) | sdata, index, control |
| skbedit | edits kernel metadata, not packet data | queue_mapping, priority, mark, ptype, inheritdsfield |
| skbmod | sets/swaps MACs and ethertype, or ECN marks | set dmac/smac/etype, swap mac, ecn |
| tunnel_key | sets/unsets tunnel metadata for vxlan/geneve devices | set src_ip dst_ip id dst_port, unset, csum/nocsum |
| vlan | push / pop / modify 802.1Q tag; push_eth/pop_eth | id, protocol 802.1ad, priority |
| xt | calls an iptables target | removed in iproute2 6.9: 'Bad action type xt' |
tc qdisc add dev eth0 handle ffff: ingresstc filter add dev eth0 parent ffff: u32 \ match u32 0 0 \ police rate 1mbit burst 100ktc filter add dev eth0 parent ffff: matchall \ action sample rate 100 group 12 index 19$ tc filter add dev eth0 parent ffff: protocol ip prio 5 u32 match ip protocol 1 0xff flowid 1:1 action simple sdata "Incoming ICMP"$ dmesg | grep simple[135354.473951] simple: Incoming ICMP_1196.9 Comparison: shaping vs policing
| Point | Shaping (tbf, htb) | Policing (police) |
|---|---|---|
| Excess packets | held in a queue, sent at rate | dropped (or reclassified) at once |
| Delay | adds delay | no extra delay |
| Loss | fewer drops | more drops; TCP backs off |
| Where | egress qdisc | usually ingress filter action |
196.10 Common mistakes
- Trusting the man page alone: iproute2 6.9 says Unknown filter tcindex and Bad action type xt; check real output on your tc and kernel.
- Using strict prio without limiting band 0: lower bands starve.
- tbf without latency or limit: rejected.
- Policing TCP too tightly: heavy drops; shaping is gentler.
196.11 Interview tips
- Shaping queues and delays excess traffic; policing drops it immediately.
- tbf = token bucket: rate caps speed, burst sets the bucket size.
- prio is strict; protect lower bands with tbf under the high band.
- u32 matches arbitrary bits; ip src becomes a hex match at offset 12.
- Always verify features against your own tc/kernel, not just docs.