SkillPilotAI

QoS and traffic shaping

QoS (Quality of Service) decides which traffic goes first when a link is full: mark packets with DSCP, sort them into queues, and limit rates by policing or shaping. On Linux all of this is done with tc.

184.1 Why is it used?

  • Problem: one colleague is on a video call (192.168.1.10) while another downloads a huge file (192.168.1.20) over the same 10 Mbps uplink.
  • Download packets fill the router queue; the voice packet waits behind them and the call breaks up.
  • The same delay makes no difference to the download, so giving voice priority costs almost nothing.
  • QoS also keeps you under an ISP rate limit without losing packets (shaping).

184.2 How it works: mark, queue, schedule

  • Every IP header has one byte once called ToS; today its upper 6 bits are DSCP.
  • Voice gets DSCP 46 = EF (Expedited Forwarding), full byte 0xb8. The download gets DSCP 0 = best effort (0x00).
  • The router reads each packet's DSCP and puts it in the right queue: priority queue for EF, default queue for the rest.
  • Whenever the link is free, the scheduler empties the priority queue first: voice leaves, download follows.
QoS on the office router
  1. 1.Voice packet marked DSCP 46 (EF), ToS 0xb8; download DSCP 0

  2. 2.Both reach the office router (uplink 10 Mbps)

  3. 3.Router reads DSCP: queue 1 Priority (46 EF), queue 2 Default (0 BE)

  4. 4.Link free: scheduler empties the priority queue first

  5. 5.Voice goes out to the ISP, download after it

  6. 6.Call runs smoothly, download barely notices

184.3 Queue types

Queue / schedulerHow it behavesCatch
FIFOFirst in, first outNo priority at all
Strict priorityEF always firstToo much priority traffic starves other queues
WFQ / CBWFQEach class gets a fixed share of bandwidthNeeds class configuration
fq_codel (Linux)A fair turn per flow, keeps queues shortReduces bufferbloat; not class-based priority

184.4 Policing vs shaping

  • Example: ISP plan 10 Mbps, your server sends 15 Mbps.
  • Policing: packets within the limit pass with no delay; excess is dropped immediately (or re-marked), no buffer. TCP sees drops as congestion, so the graph saw-tooths.
  • Shaping: excess is held in a buffer and sent as soon as the rate allows, so output is a smooth 10 Mbps. Cost: extra delay, and drops still happen if the buffer fills.
Same 15 Mbps, two rate limiters
  1. 1.Office server sends 15 Mbps to a 10 Mbps limiter

  2. 2.Policer: within 10 Mbps passes, excess dropped at once

  3. 3.TCP backs off on drops: throughput goes up and down

  4. 4.Shaper: excess waits in a buffer instead

  5. 5.Buffer drains at 10 Mbps: smooth output, a little more latency

  6. 6.Buffer full: shaper also drops

Policing drops the excess; shaping delays it

PointPolicingShaping
Excess trafficDropped (or re-marked)Delayed in a buffer
BufferNoneYes
Delay addedNoneSome
OutputBursty, TCP saw-toothSmooth
Usual placeISP ingressYour egress
Linux tcpolice filter on ingresstbf (or HTB) qdisc

184.5 Mark at the edge, trust at the core

  • The access switch at the edge classifies and marks every packet.
  • A laptop that marks itself EF gets re-marked to 0 by the edge switch.
  • A known device such as an IP phone (DSCP 46) is trusted.
  • The core router only reads DSCP to choose a queue, because the edge already made it correct.

184.6 The idea of Linux tc

PieceRole
qdiscQueueing discipline of each interface: order and rate of leaving packets (noqueue, pfifo, fq_codel)
class + filterClassful qdisc like HTB has classes; filters pick the class by fields such as DSCP
netemSimulates delay, jitter and loss for testing
tbf / policetbf = shaping with a token bucket, police = policing

184.7 Lab: default qdisc

Docker client and server on 192.168.204.0/24. A veth interface needs no queue of its own, so it shows noqueue.

Terminal
tc -Vtc qdisc show dev eth0tc -s qdisc show dev eth0
Output
$ tc qdisc show dev eth0qdisc noqueue 0: root refcnt 2$ 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

184.8 Lab: mark DSCP EF with ping

Terminal
# servertcpdump -n -v -i eth0 -c 8 icmp# clientping -c 2 192.168.204.20ping -c 2 -Q 0xb8 192.168.204.20
Output
IP (tos 0xb8, ttl 64, id 30330, offset 0, flags [DF], proto ICMP (1), length 84)    192.168.204.10 > 192.168.204.20: ICMP echo request, id 17874, seq 1, length 64IP (tos 0xb8, ttl 64, id 18749, offset 0, flags [none], proto ICMP (1), length 84)    192.168.204.20 > 192.168.204.10: ICMP echo reply, id 17874, seq 1, length 648 packets captured
  • A normal ping shows tos 0x0; ping -Q 0xb8 (DSCP 46, EF) shows tos 0xb8 on both request and reply.
  • 0xb8 = 1011 1000: upper 6 bits 101110 = 46.

184.9 Lab: pfifo and iperf3 with DSCP

netem, tbf and htb are modules not loaded in this VM, so only the built-in pfifo was used.

Terminal
tc qdisc replace dev eth0 root pfifo limit 100tc -s qdisc show dev eth0iperf3 -c 192.168.204.20 -t 3 -b 5M -S 0xb8# server: tcpdump -n -v -i eth0 -c 2 \#   'tcp dst port 5201 and greater 1000'tc -s qdisc show dev eth0
Output
$ tc -s qdisc show dev eth0qdisc pfifo 8002: root refcnt 9 limit 100p Sent 2058066 bytes 1385 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0
  • pfifo limit 100: simple FIFO, at most 100 packets wait.
  • iperf3 -b 5M limits the rate in userspace (about 5 Mbits/sec each second); -S 0xb8 marks every packet EF (tcpdump shows tos 0xb8).
  • Counters: about 2 MB through the queue, 0 dropped.

184.10 Syntax: netem, tbf, police

Not run in the lab (the add would load modules); syntax from the man pages.

Terminal
# netem: delay aur losstc qdisc add dev eth0 root netem delay 100mstc qdisc change dev eth0 root netem \  delay 100ms 10ms 25%tc qdisc change dev eth0 root netem loss 0.1%# tbf: shapingtc qdisc add dev eth0 handle 10: root tbf \  rate 0.5mbit burst 5kb latency 70ms \  peakrate 1mbit minburst 1540# police: ingress pe policingtc qdisc add dev eth0 handle ffff: ingresstc filter add dev eth0 parent ffff: u32 \  match u32 0 0 \  police rate 1mbit burst 100k

184.11 Common mistakes

  • Thinking QoS adds bandwidth: it only decides order during congestion; on an idle link it does nothing.
  • Expecting DSCP to be honoured on the public internet: usually it is not.
  • Strict priority with too much EF traffic: other queues starve.
  • Trusting end-user markings: always re-mark at the edge.

184.12 Interview tips

  • Policing drops or re-marks excess with no delay (ingress); shaping buffers excess for smooth output with more latency (egress).
  • ISP polices at 10 Mbps and you burst 15: shape your egress slightly below 10 Mbps.
  • DSCP = upper 6 bits of the ToS byte; voice = EF 46 = 0xb8.
  • Linux: tc qdiscs (pfifo, fq_codel, HTB, tbf), netem for delay/loss testing.