SkillPilotAI

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.
The egress queue
  1. 1.Client app sends a packet

  2. 2.Packet is enqueued in the root qdisc of eth0 (not sent straight to the NIC)

  3. 3.Kernel dequeues it and hands it to the eth0 driver

  4. 4.Packet reaches the server (192.168.213.20)

  5. 5.If the queue is full, the new packet is dropped; tc -s shows Sent and dropped

Classful tree: qdisc, filter, class
  1. 1.Packet arrives at root qdisc (e.g. HTB 1:)

  2. 2.Filters are tried in prio order

  3. 3.Filter matches SSH by port 22

  4. 4.Packet goes to the SSH class (rate 2mbit)

  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

Terminal
tc -Vip -d link show eth0 | head -3tc qdisc show
Output
$ 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

Terminal
tc -s qdisc show dev eth0tc -s -d -p -j -pretty -iec -t qdisc show
Output
$ 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",        ...    } ]
OptionMeaning
-s, -statspackets, bytes, drops
-d, -detailsrates, cell sizes detail
-r, -rawhandles in raw hex
-p, -prettydecode u32, indent JSON
-iecIEC units (1K = 1024)
-g, -graph (+ -s)ASCII tree of classes (with stats)
-c, -coloralways | auto | never
-j, -jsonJSON output (best for scripts and monitoring)
-br, -briefshort output for filters/actions

193.5 tc qdisc: add, change, replace, delete

Terminal
tc qdisc add dev eth0 root pfifo limit 100tc qdisc add dev eth0 root pfifo limit 100   # againtc -s qdisc show dev eth0
Output
$ 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
VerbWhat it does
addattach a new qdisc; fails with 'Exclusivity flag on' if one exists
changemodify in place, handle stays the same
replaceremove + add, or create if missing
linkreplace, but the node must already exist
del / deleteremove the qdisc and its children
show / listqdiscs 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

Terminal
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
Output
$ 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.
Terminal
tc qdisc replace dev eth0 root pfifo_fasttc -s qdisc show dev eth0tc qdisc delete dev eth0 roottc -s qdisc show dev eth0
Output
$ 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

UnitMeaningTrap
kbit, mbit, gbitkilo/mega/giga bits per second (rate)mbit = megabits
kbps, mbps, gbpskilo/mega/giga BYTES per secondmbps = 8 x mbit
25%percent of device speedrate stored as 32-bit (max 4294967295 bps)
kb, k / mb, mkilobytes / megabytes (size)64kb shows as 65536 in JSON (1 kb = 1024 bytes)
s, ms, usseconds, milliseconds, microseconds (bare number = us)-
mibit, kibpsIEC (1024-based) units-

193.8 tc monitor and global options

Terminal
timeout 40 tc -ts monitor > /tmp/mon.txt &# ... qdisc add, change, replace ...cat /tmp/mon.txt
Output
$ 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 optionMeaning
-b, -batch FILErun tc commands from a file (stops at first error)
-force -b FILEcontinue after errors (exit code still 1)
-o, -onelineone record per line
-n, -netns NSrun inside named namespace NS
-N, -Numericnumeric protocol/dsfield
-nm, -cf FILEclass names from tc_cls / config file
-t, -tstimestamp / short timestamp for monitor
Output
$ 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

TypeExamplesUse
Classlesspfifo, bfifo, pfifo_fast, fq_codel, fq, codel, pie, fq_pie, red, choke, sfq, sfb, hhf, tbf, netem, ingress, mqprioone queue policy for the whole interface
Classfulhtb, hfsc, cbq, prio, drr, qfq, ets, dsmark, atmtree 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)

Terminal
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 controlEffect
piperun the next action (default)
dropdrop the packet right there
continueclassify with the next filter
reclassifyrestart from the first filter
pass / okend 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.