tc (1/4)
tc (traffic control, from iproute2) decides how the Linux kernel queues, schedules, shapes and drops packets on every interface. This first part covers the qdisc/class/filter model, the global and output options, a real pfifo/bfifo/pfifo_fast lab, units, tc monitor and the syntax of classes, filters, chains, actions and bpf.
193.1 Why is it used?
- Shape or limit bandwidth (e.g. give SSH a guaranteed 2 Mbit), prioritise traffic, police or drop flows.
- Emulate bad networks (delay, loss) for testing and fight bufferbloat with AQM qdiscs.
- Linux only: package iproute2 (Debian/Ubuntu) or iproute-tc (RHEL/Fedora).
- show works for anyone; add/change needs root or CAP_NET_ADMIN.
193.2 How it works
- qdisc: the queue of an interface; decides when a packet leaves and when it is dropped.
- class: a bandwidth share inside a classful qdisc (HTB, HFSC, PRIO ...).
- filter: looks at every packet and picks its class (flowid); unmatched packets go to the default class.
- Step 1
1.Client app sends a packet
- Step 2
2.Packet is enqueued in the root qdisc of eth0 (not sent straight to the NIC)
- Step 3
3.Kernel dequeues it and hands it to the eth0 driver
- Step 4
4.Packet reaches the server (192.168.213.20)
- Step 5
5.If the queue is full, the new packet is dropped; tc -s shows Sent and dropped
- Step 1
1.Packet arrives at root qdisc (e.g. HTB 1:)
- Step 2
2.Filters are tried in prio order
- Step 3
3.Filter matches SSH by port 22
- Step 4
4.Packet goes to the SSH class (rate 2mbit)
- Step 5
5.No filter matched: packet goes to the default class
Classful qdiscs are covered in detail in parts 2-4
193.3 Lab: the default qdisc
tc -Vip -d link show eth0 | head -3tc qdisc show$ tc -Vtc utility, iproute2-6.9.0$ tc qdisc showqdisc noqueue 0: dev lo root refcnt 2qdisc noqueue 0: dev eth0 root refcnt 2- Docker lab 192.168.213.0/24: client (with NET_ADMIN) and an iperf3 server.
- eth0 is a veth with txqueuelen 0, so its default qdisc is noqueue, not pfifo_fast.
193.4 Output format options
tc -s qdisc show dev eth0tc -s -d -p -j -pretty -iec -t qdisc show$ tc -s -d -p -j -pretty -iec -t qdisc show[ { "kind": "noqueue", "handle": "0:", "dev": "lo", "root": true, "refcnt": 2, "options": {}, "bytes": 0, "packets": 0, "drops": 0, "overlimits": 0, "requeues": 0, "backlog": 0, "qlen": 0 },{ "kind": "noqueue", "dev": "eth0", ... } ]| Option | Meaning |
|---|---|
| -s, -stats | packets, bytes, drops |
| -d, -details | rates, cell sizes detail |
| -r, -raw | handles in raw hex |
| -p, -pretty | decode u32, indent JSON |
| -iec | IEC units (1K = 1024) |
| -g, -graph (+ -s) | ASCII tree of classes (with stats) |
| -c, -color | always | auto | never |
| -j, -json | JSON output (best for scripts and monitoring) |
| -br, -brief | short output for filters/actions |
193.5 tc qdisc: add, change, replace, delete
tc qdisc add dev eth0 root pfifo limit 100tc qdisc add dev eth0 root pfifo limit 100 # againtc -s qdisc show dev eth0$ tc qdisc add dev eth0 root pfifo limit 100Error: Exclusivity flag on, cannot modify.$ tc -s qdisc show dev eth0qdisc pfifo 8003: root refcnt 9 limit 100p Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0| Verb | What it does |
|---|---|
| add | attach a new qdisc; fails with 'Exclusivity flag on' if one exists |
| change | modify in place, handle stays the same |
| replace | remove + add, or create if missing |
| link | replace, but the node must already exist |
| del / delete | remove the qdisc and its children |
| show / list | qdiscs of every device (dev, root, ingress, parent, invisible) |
- Location keywords: root (egress), parent 1:10 (under a class), handle 1: (MAJOR:, hex), ingress_block / egress_block for shared filter blocks.
193.6 pfifo, bfifo and pfifo_fast
ping -c 5 -i 0.2 192.168.213.20iperf3 -c 192.168.213.20 -t 3 -b 20Mtc -s qdisc show dev eth0tc qdisc change dev eth0 root pfifo limit 50tc qdisc show dev eth0tc qdisc replace dev eth0 root \ bfifo limit 64kbiperf3 -c 192.168.213.20 -t 3 -b 20Mtc -s qdisc show dev eth0$ tc qdisc replace dev eth0 root bfifo limit 64kb(exit 0)$ iperf3 -c 192.168.213.20 -t 3 -b 20M[ 5] 0.00-3.00 sec 7.25 MBytes 20.2 Mbits/sec 0 sender$ tc -s qdisc show dev eth0qdisc bfifo 8004: root refcnt 9 limit 64Kb Sent 7952344 bytes 5297 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0- pfifo limits by packets, bfifo by bytes. After ping + iperf3 about 8 MB / 5297 packets left with 0 drops.
- change set limit 50 in place (same handle 8003); replace swapped in bfifo 8004 with limit 64kb.
tc qdisc replace dev eth0 root pfifo_fasttc -s qdisc show dev eth0tc qdisc delete dev eth0 roottc -s qdisc show dev eth0$ tc qdisc delete dev eth0 root(exit 0)$ tc -s qdisc show dev eth0qdisc noqueue 0: root refcnt 2 Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0- pfifo_fast has 3 bands and a priomap that puts packets in band 0/1/2 by ToS.
- Deleting the root qdisc restores the default: here noqueue, because the veth has txqueuelen 0.
193.7 Units: rates, times and sizes
| Unit | Meaning | Trap |
|---|---|---|
| kbit, mbit, gbit | kilo/mega/giga bits per second (rate) | mbit = megabits |
| kbps, mbps, gbps | kilo/mega/giga BYTES per second | mbps = 8 x mbit |
| 25% | percent of device speed | rate stored as 32-bit (max 4294967295 bps) |
| kb, k / mb, m | kilobytes / megabytes (size) | 64kb shows as 65536 in JSON (1 kb = 1024 bytes) |
| s, ms, us | seconds, milliseconds, microseconds (bare number = us) | - |
| mibit, kibps | IEC (1024-based) units | - |
193.8 tc monitor and global options
timeout 40 tc -ts monitor > /tmp/mon.txt &# ... qdisc add, change, replace ...cat /tmp/mon.txt$ cat /tmp/mon.txt[2026-11-09T11:54:03.704419] deleted qdisc noqueue 0: dev eth0 root[2026-11-09T11:54:03.704708] qdisc pfifo 8003: dev eth0 root refcnt 9 limit 100p[2026-11-09T11:54:20.757005] qdisc pfifo 8003: dev eth0 root refcnt 9 limit 50p[2026-11-09T11:54:27.009634] deleted qdisc pfifo 8003: dev eth0 root limit 50p[2026-11-09T11:54:27.009692] qdisc bfifo 8004: dev eth0 root refcnt 9 limit 64Kb[2026-11-09T11:54:29.142890] qdisc bfifo 8004: dev eth0 root refcnt 9 limit 64Kb- Adding pfifo produced two events: noqueue deleted, then pfifo 8003 created; change/replace events came with timestamps.
| Global option | Meaning |
|---|---|
| -b, -batch FILE | run tc commands from a file (stops at first error) |
| -force -b FILE | continue after errors (exit code still 1) |
| -o, -oneline | one record per line |
| -n, -netns NS | run inside named namespace NS |
| -N, -Numeric | numeric protocol/dsfield |
| -nm, -cf FILE | class names from tc_cls / config file |
| -t, -ts | timestamp / short timestamp for monitor |
$ tc -o qdisc showqdisc noqueue 0: dev lo root refcnt 2 \qdisc noqueue 0: dev eth0 root refcnt 2 \$ tc -n n193ns qdisc showCannot open network namespace "n193ns": No such file or directory(exit 255)193.9 Classless vs classful qdiscs
| Type | Examples | Use |
|---|---|---|
| Classless | pfifo, bfifo, pfifo_fast, fq_codel, fq, codel, pie, fq_pie, red, choke, sfq, sfb, hhf, tbf, netem, ingress, mqprio | one queue policy for the whole interface |
| Classful | htb, hfsc, cbq, prio, drr, qfq, ets, dsmark, atm | tree of classes, filters pick the class |
- In the lab Docker VM everything except pfifo, bfifo and pfifo_fast is a kernel module not loaded, so the rest was shown as syntax only.
- HTB is the most used classful qdisc: guaranteed rate + ceil per class.
193.10 class, filter, chain, actions, bpf (syntax)
tc class add dev eth0 parent 1: classid 1:10 htb rate 10mbit ceil 20mbittc filter add dev eth0 parent 1: protocol ip prio 1 handle 1 fw flowid 1:10tc chain add dev eth0 ingress chain 1 flowertc filter add dev eth0 ... action gact index 10 # reuse a shared actiontc filter add dev em1 parent 1: bpf obj bpf.o sec mycls flowid 1:1tc filter add dev em1 parent 1: bpf bytecode '1,6 0 0 4294967295,' flowid 1:1- filter: prio (lower number tried first), protocol (ip, ipv6, all, 802.1q), flowid (target class), block (shared block).
- Classifiers: basic (ematch), bpf, cgroup, flow, flower, fw, route, rsvp, tcindex, u32, matchall.
- chain groups filters into numbered chains; chain add can take a template like flower.
- tc actions defines actions separately with an index so many filters can share one.
- bpf: attach your own eBPF/cBPF program; with da (direct-action) the program's return code is the verdict.
| Action control | Effect |
|---|---|
| pipe | run the next action (default) |
| drop | drop the packet right there |
| continue | classify with the next filter |
| reclassify | restart from the first filter |
| pass / ok | end classification, return packet to the qdisc |
193.11 Common mistakes
- Using add when a qdisc already exists: 'Exclusivity flag on, cannot modify'. Use replace or del first.
- Confusing mbit (bits) with mbps (bytes, 8x more), or kb (bytes) with kbit (bits).
- Assuming the default is always pfifo_fast: on a Docker veth (txqueuelen 0) it is noqueue.
- Forgetting that most qdiscs/classifiers are modules; adding one auto-loads a kernel module.
193.12 Interview tips
- qdisc = interface queue (when to send/drop); class = bandwidth share; filter = picks the class.
- Every outgoing packet passes the root qdisc; tc -s shows sent/dropped/overlimits.
- add creates, change modifies in place, replace swaps or creates.
- pfifo limits packets, bfifo bytes; mbit vs mbps is an 8x trap.
- Use -j -p for scripts and tc monitor for live events.