* [PATCH] net/sched: fq_codel: choose random fat flow to drop from, if multiple are equal
@ 2026-12-03 16:42 Andreea-Carina Deaconu
2026-12-03 16:43 ` Andreea-Carina Deaconu
0 siblings, 1 reply; 2+ messages in thread
From: Andreea-Carina Deaconu @ 2026-12-03 16:42 UTC (permalink / raw)
To: netdev; +Cc: Thomas Begin
Original bias: When the FQ Codel queue capacity is reached and we
proceed
in fq_codel_drop() to drop packets from the "fat flow" (the flow with
the biggest size), if there are two or more equal "fat flows", we always
drop from the first flow found (the flow with the lowest index).
Fix: When searching for the "fat flow", keep a candidate count and, if
there is more than one candidate, choose a random one to drop from.
Reported-by: Thomas Begin <thomas.begin@ens-lyon.fr>
Reported-by: Andreea-Carina Deaconu <carina.deaconu@ens-lyon.fr>
Signed-off-by: Andreea-Carina Deaconu <carina.deaconu@ens-lyon.fr>
---
net/sched/sch_fq_codel.c | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/net/sched/sch_fq_codel.c b/net/sched/sch_fq_codel.c
index e0fcda26f..b63d3a3de 100644
--- a/net/sched/sch_fq_codel.c
+++ b/net/sched/sch_fq_codel.c
@@ -13,6 +13,7 @@
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/init.h>
+#include <linux/random.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
@@ -139,8 +140,9 @@ static unsigned int fq_codel_drop(struct Qdisc *sch,
unsigned int max_packets,
{
struct fq_codel_sched_data *q = qdisc_priv(sch);
struct sk_buff *skb;
- unsigned int maxbacklog = 0, idx = 0, i, len;
struct fq_codel_flow *flow;
+ unsigned int maxbacklog = 0, idx = 0, ties = 0;
+ unsigned int target = 0, match_idx = 0, i, len;
unsigned int threshold;
unsigned int mem = 0;
@@ -154,7 +156,26 @@ static unsigned int fq_codel_drop(struct Qdisc
*sch, unsigned int max_packets,
for (i = 0; i < q->flows_cnt; i++) {
if (q->backlogs[i] > maxbacklog) {
maxbacklog = q->backlogs[i];
- idx = i;
+ ties = 1;
+ } else if (maxbacklog > 0 && q->backlogs[i] ==
maxbacklog) {
+ ties++;
+ }
+ }
+
+ if (maxbacklog > 0 && ties > 0) {
+ if (ties == 1)
+ target = 0;
+ else
+ target = get_random_u32() % ties;
+
+ for (i = 0; i < q->flows_cnt; i++) {
+ if (q->backlogs[i] == maxbacklog) {
+ if (match_idx == target) {
+ idx = i;
+ break;
+ }
+ match_idx++;
+ }
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] net/sched: fq_codel: choose random fat flow to drop from, if multiple are equal
2026-12-03 16:42 [PATCH] net/sched: fq_codel: choose random fat flow to drop from, if multiple are equal Andreea-Carina Deaconu
@ 2026-12-03 16:43 ` Andreea-Carina Deaconu
0 siblings, 0 replies; 2+ messages in thread
From: Andreea-Carina Deaconu @ 2026-12-03 16:43 UTC (permalink / raw)
To: netdev; +Cc: Thomas Begin
Hi,
When performing a stress test on an FQ Codel queue, we found a bias when
searching for the "fat flow" to drop packets from.
Bias:
When the FQ Codel queue capacity is reached, we proceed to drop packets
from the biggest flow (the "fat flow") in fq_codel_drop(). If there are
two or more biggest flows, with equal size, we will always choose the
flow with the lowest index (the first flow we find during the
iteration).
Steps to reproduce:
Use the script below to:
1. Configure two interfaces that will receive UDP traffic at equal
rates, on ports 5201 and 5202.
2. Configure an FQ Codel queue with a limit of 1p and no ECN so that:
the queue capacity is immediately reached, and when we proceed to drop
packets from the biggest flow in fq_codel_drop(), there will likely be
two equal ones to choose from.
3. Perform two runs by switching the start order: In the first run, the
traffic towards the 5201 port is started first. In the second run, the
traffic towards the 5202 port is started first.
4. At the end, inspect the loss rate for each of the two flows in both
runs.
Expected behavior:
The two flows should have similar loss rates in both runs, no matter
which one is started first, since their traffic intensities are the
same.
Actual behavior (before the patch) (see captured output below):
In each run, the flow that is started first has loss rate of 80%, while
the flow that is started second has a <1% loss rate.
Fix:
When searching for the "fat flow" in fq_codel_drop(), keep account of
the number of candidates. If there are multiple fat flows with equal
size, randomly choose a flow index to drop packets from.
Behavior after the patch (see captured output below):
Both flows have similar loss rates: 39% and 40% in the first run, 39%
and 41% in the second run.
Regards
Script to stress test:
---begin script---
#!/usr/bin/env sh
set -eu
LIMIT="${1:-1}"
# Check required tools before setting up namespaces
MISSING=""
command -v tc >/dev/null 2>&1 || MISSING="$MISSING iproute2"
command -v iperf3 >/dev/null 2>&1 || MISSING="$MISSING iperf3"
if [ -n "$MISSING" ]; then
printf "Missing packages:%s. Do you want to run 'sudo apt install -y%s'
to install them? [y/N] " "$MISSING" "$MISSING"
read -r ANSWER || ANSWER="n"
case "$ANSWER" in
y|Y|yes|YES)
sudo apt update
# Preseed 'No' for iperf3 daemon prompt if debconf asks, then run
non-interactively
printf "iperf3 iperf3/start_daemon boolean false\n" | sudo
debconf-set-selections 2>/dev/null || true
sudo DEBIAN_FRONTEND=noninteractive apt install -y $MISSING
;;
*)
echo "Aborting: required packages not installed."
exit 1
;;
esac
fi
tc -V
# Clean up any leftover state from a previous run
sudo pkill -f "iperf3 -s" 2>/dev/null || true
sudo ip netns del ns1 2>/dev/null || true
sudo ip netns del ns2 2>/dev/null || true
# Create two isolated network namespaces linked by a veth pair
sudo ip netns add ns1
sudo ip netns add ns2
sudo ip link add veth1 type veth peer name veth2
sudo ip link set veth1 netns ns1
sudo ip link set veth2 netns ns2
sudo ip netns exec ns1 ip addr add 10.0.0.1/24 dev veth1
sudo ip netns exec ns2 ip addr add 10.0.0.2/24 dev veth2
sudo ip netns exec ns1 ip link set lo up && sudo ip netns exec ns1 ip
link set veth1 up
sudo ip netns exec ns2 ip link set lo up && sudo ip netns exec ns2 ip
link set veth2 up
# Shape veth1 egress to 10mbit with fq_codel limit 1p (noecn)
sudo ip netns exec ns1 tc qdisc add dev veth1 root handle 1: htb default
10
sudo ip netns exec ns1 tc class add dev veth1 parent 1: classid 1:10 htb
rate 10mbit
sudo ip netns exec ns1 tc qdisc add dev veth1 parent 1:10 handle 10:
fq_codel limit "${LIMIT}" noecn
# Start two UDP iperf3 servers on separate ports
sudo ip netns exec ns2 iperf3 -s -p 5201 -D
sudo ip netns exec ns2 iperf3 -s -p 5202 -D
sleep 0.5
#
==============================================================================
echo
"=================================================================="
echo "RUN 1: Launching Flow 1 (port 5201) first, then Flow 2 (port
5202)"
echo
"=================================================================="
sudo ip netns exec ns1 iperf3 -c 10.0.0.2 -p 5201 -u -l 1000 -b 8m -t 15
> /tmp/run1_flow1.log &
PID1=$!
sleep 0.002
sudo ip netns exec ns1 iperf3 -c 10.0.0.2 -p 5202 -u -l 1000 -b 8m -t 15
> /tmp/run1_flow2.log &
PID2=$!
wait "$PID1" "$PID2"
echo "--- Run 1 | Flow 1 (port 5201 - started 1st) ---"
tail -n 6 /tmp/run1_flow1.log
echo "--- Run 1 | Flow 2 (port 5202 - started 2nd) ---"
tail -n 6 /tmp/run1_flow2.log
echo "--- Run 1 | qdisc stats ---"
sudo ip netns exec ns1 tc -s qdisc show dev veth1
# Reset htb and fq_codel cleanly before Run 2
sudo ip netns exec ns1 tc qdisc del dev veth1 root
sudo ip netns exec ns1 tc qdisc add dev veth1 root handle 1: htb default
10
sudo ip netns exec ns1 tc class add dev veth1 parent 1: classid 1:10 htb
rate 10mbit
sudo ip netns exec ns1 tc qdisc add dev veth1 parent 1:10 handle 10:
fq_codel limit "${LIMIT}" noecn
sleep 1
#
==============================================================================
echo ""
echo
"=================================================================="
echo "RUN 2: Launching Flow 2 (port 5202) first, then Flow 1 (port
5201)"
echo
"=================================================================="
sudo ip netns exec ns1 iperf3 -c 10.0.0.2 -p 5202 -u -l 1000 -b 8m -t 15
> /tmp/run2_flow2.log &
PID2=$!
sleep 0.002
sudo ip netns exec ns1 iperf3 -c 10.0.0.2 -p 5201 -u -l 1000 -b 8m -t 15
> /tmp/run2_flow1.log &
PID1=$!
wait "$PID1" "$PID2"
echo "--- Run 2 | Flow 2 (port 5202 - started 1st) ---"
tail -n 6 /tmp/run2_flow2.log
echo "--- Run 2 | Flow 1 (port 5201 - started 2nd) ---"
tail -n 6 /tmp/run2_flow1.log
echo "--- Run 2 | qdisc stats ---"
sudo ip netns exec ns1 tc -s qdisc show dev veth1
# Clean up background servers and namespaces
sudo pkill -f "iperf3 -s" 2>/dev/null || true
sudo ip netns del ns1 2>/dev/null || true
sudo ip netns del ns2 2>/dev/null || true
---end script---
Output before the patch:
---captured output---
queue-engineer@fq-codel-lab:~/kernel-lab/linux$
../run_fqcodel_stress_test.sh
tc utility, iproute2-6.19.0, libbpf 1.6.3
==================================================================
RUN 1: Launching Flow 1 (port 5201) first, then Flow 2 (port 5202)
==================================================================
--- Run 1 | Flow 1 (port 5201 - started 1st) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 2.91 MBytes 1.63 Mbits/sec 0.382 ms
11946/14998 (80%) receiver
iperf Done.
--- Run 1 | Flow 2 (port 5202 - started 2nd) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 14.2 MBytes 7.97 Mbits/sec 0.300 ms 64/15001
(0.43%) receiver
iperf Done.
--- Run 1 | qdisc stats ---
qdisc htb 1: root refcnt 9 r2q 10 default 0x10 direct_packets_stat 0
direct_qlen 1000
Sent 18748170 bytes 18029 pkt (dropped 12015, overlimits 17971 requeues
0)
backlog 0b 0p requeues 0
qdisc fq_codel 10: parent 1:10 limit 1p flows 1024 quantum 1514 target
5ms interval 100ms memory_limit 32Mb drop_batch 64
Sent 18748170 bytes 18029 pkt (dropped 12015, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
maxpacket 1042 drop_overlimit 12015 new_flow_count 9388 ecn_mark 0
new_flows_len 0 old_flows_len 2
==================================================================
RUN 2: Launching Flow 2 (port 5202) first, then Flow 1 (port 5201)
==================================================================
--- Run 2 | Flow 2 (port 5202 - started 1st) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 2.87 MBytes 1.60 Mbits/sec 0.070 ms
11992/14999 (80%) receiver
iperf Done.
--- Run 2 | Flow 1 (port 5201 - started 2nd) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 14.3 MBytes 7.99 Mbits/sec 0.324 ms 24/15001
(0.16%) receiver
iperf Done.
--- Run 2 | qdisc stats ---
qdisc htb 1: root refcnt 9 r2q 10 default 0x10 direct_packets_stat 0
direct_qlen 1000
Sent 18742451 bytes 18017 pkt (dropped 12019, overlimits 17968 requeues
0)
backlog 0b 0p requeues 0
qdisc fq_codel 10: parent 1:10 limit 1p flows 1024 quantum 1514 target
5ms interval 100ms memory_limit 32Mb drop_batch 64
Sent 18742451 bytes 18017 pkt (dropped 12019, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
maxpacket 1042 drop_overlimit 12019 new_flow_count 9172 ecn_mark 0
new_flows_len 1 old_flows_len 2
queue-engineer@fq-codel-lab:~/kernel-lab/linux$ date
Thu Dec 3 04:27:31 PM CET 2026
queue-engineer@fq-codel-lab:~/kernel-lab/linux$ cat /proc/version
Linux version 7.0.0-44-generic (buildd@bos03-arm64-054)
(aarch64-linux-gnu-gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, GNU ld (GNU
Binutils for Ubuntu) 2.46) #44-Ubuntu SMP PREEMPT_DYNAMIC Fri Nov 6
02:50:28 UTC 2026
queue-engineer@fq-codel-lab:~/kernel-lab/linux$ uname -a
Linux fq-codel-lab 7.0.0-44-generic #44-Ubuntu SMP PREEMPT_DYNAMIC Fri
Nov 6 02:50:28 UTC 2026 aarch64 GNU/Linux
---captured output end---
Output after the patch:
---captured output---
queue-engineer@fq-codel-lab:~$ uname -a
Linux fq-codel-lab 7.4.0-rc5-g37ba33ea2993 #5 SMP PREEMPT_DYNAMIC Thu
Dec 3 16:32:22 CET 2026 aarch64 GNU/Linux
queue-engineer@fq-codel-lab:~$ cat /proc/version
Linux version 7.4.0-rc5-g37ba33ea2993 (queue-engineer@fq-codel-lab) (gcc
(Ubuntu 15.2.0-16ubuntu1) 15.2.0, GNU ld (GNU Binutils for Ubuntu) 2.46)
#5 SMP PREEMPT_DYNAMIC Thu Dec 3 16:32:22 CET 2026
queue-engineer@fq-codel-lab:~$ cd kernel-lab/
queue-engineer@fq-codel-lab:~/kernel-lab$ ./run_fqcodel_stress_test.sh
tc utility, iproute2-6.19.0, libbpf 1.6.3
[sudo: authenticate] Password:
==================================================================
RUN 1: Launching Flow 1 (port 5201) first, then Flow 2 (port 5202)
==================================================================
--- Run 1 | Flow 1 (port 5201 - started 1st) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 8.73 MBytes 4.88 Mbits/sec 0.404 ms
5847/15001 (39%) receiver
iperf Done.
--- Run 1 | Flow 2 (port 5202 - started 2nd) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15002
(0%) sender
[ 5] 0.00-15.00 sec 8.59 MBytes 4.80 Mbits/sec 0.019 ms
5994/15002 (40%) receiver
iperf Done.
--- Run 1 | qdisc stats ---
qdisc htb 1: root refcnt 9 r2q 10 default 0x10 direct_packets_stat 0
direct_qlen 1000
Sent 18928361 bytes 18200 pkt (dropped 11843, overlimits 17739 requeues
0)
backlog 0b 0p requeues 0
qdisc fq_codel 10: parent 1:10 limit 1p flows 1024 quantum 1514 target
5ms interval 100ms memory_limit 32Mb drop_batch 64
Sent 18928361 bytes 18200 pkt (dropped 11843, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
maxpacket 1042 drop_overlimit 11843 new_flow_count 5590 ecn_mark 0
new_flows_len 1 old_flows_len 1
==================================================================
RUN 2: Launching Flow 2 (port 5202) first, then Flow 1 (port 5201)
==================================================================
--- Run 2 | Flow 2 (port 5202 - started 1st) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 8.68 MBytes 4.85 Mbits/sec 0.373 ms
5900/15000 (39%) receiver
iperf Done.
--- Run 2 | Flow 1 (port 5201 - started 2nd) ---
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Jitter
Lost/Total Datagrams
[ 5] 0.00-15.00 sec 14.3 MBytes 8.00 Mbits/sec 0.000 ms 0/15001
(0%) sender
[ 5] 0.00-15.00 sec 8.48 MBytes 4.74 Mbits/sec 0.314 ms
6109/15001 (41%) receiver
iperf Done.
--- Run 2 | qdisc stats ---
qdisc htb 1: root refcnt 9 r2q 10 default 0x10 direct_packets_stat 0
direct_qlen 1000
Sent 18750641 bytes 18023 pkt (dropped 12011, overlimits 17976 requeues
0)
backlog 0b 0p requeues 0
qdisc fq_codel 10: parent 1:10 limit 1p flows 1024 quantum 1514 target
5ms interval 100ms memory_limit 32Mb drop_batch 64
Sent 18750641 bytes 18023 pkt (dropped 12011, overlimits 0 requeues 0)
backlog 0b 0p requeues 0
maxpacket 1042 drop_overlimit 12011 new_flow_count 5672 ecn_mark 0
new_flows_len 1 old_flows_len 1
queue-engineer@fq-codel-lab:~/kernel-lab$ date
Thu Dec 3 04:42:30 PM CET 2026
---captured output end---
On 2026-12-03 17:42, Andreea-Carina Deaconu wrote:
> Original bias: When the FQ Codel queue capacity is reached and we
> proceed
> in fq_codel_drop() to drop packets from the "fat flow" (the flow with
> the biggest size), if there are two or more equal "fat flows", we
> always
> drop from the first flow found (the flow with the lowest index).
>
> Fix: When searching for the "fat flow", keep a candidate count and, if
> there is more than one candidate, choose a random one to drop from.
>
> Reported-by: Thomas Begin <thomas.begin@ens-lyon.fr>
> Reported-by: Andreea-Carina Deaconu <carina.deaconu@ens-lyon.fr>
> Signed-off-by: Andreea-Carina Deaconu <carina.deaconu@ens-lyon.fr>
> ---
> net/sched/sch_fq_codel.c | 25 +++++++++++++++++++++++--
> 1 file changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/net/sched/sch_fq_codel.c b/net/sched/sch_fq_codel.c
> index e0fcda26f..b63d3a3de 100644
> --- a/net/sched/sch_fq_codel.c
> +++ b/net/sched/sch_fq_codel.c
> @@ -13,6 +13,7 @@
> #include <linux/in.h>
> #include <linux/errno.h>
> #include <linux/init.h>
> +#include <linux/random.h>
> #include <linux/skbuff.h>
> #include <linux/slab.h>
> #include <linux/vmalloc.h>
> @@ -139,8 +140,9 @@ static unsigned int fq_codel_drop(struct Qdisc
> *sch, unsigned int max_packets,
> {
> struct fq_codel_sched_data *q = qdisc_priv(sch);
> struct sk_buff *skb;
> - unsigned int maxbacklog = 0, idx = 0, i, len;
> struct fq_codel_flow *flow;
> + unsigned int maxbacklog = 0, idx = 0, ties = 0;
> + unsigned int target = 0, match_idx = 0, i, len;
> unsigned int threshold;
> unsigned int mem = 0;
>
> @@ -154,7 +156,26 @@ static unsigned int fq_codel_drop(struct Qdisc
> *sch, unsigned int max_packets,
> for (i = 0; i < q->flows_cnt; i++) {
> if (q->backlogs[i] > maxbacklog) {
> maxbacklog = q->backlogs[i];
> - idx = i;
> + ties = 1;
> + } else if (maxbacklog > 0 && q->backlogs[i] ==
> maxbacklog) {
> + ties++;
> + }
> + }
> +
> + if (maxbacklog > 0 && ties > 0) {
> + if (ties == 1)
> + target = 0;
> + else
> + target = get_random_u32() % ties;
> +
> + for (i = 0; i < q->flows_cnt; i++) {
> + if (q->backlogs[i] == maxbacklog) {
> + if (match_idx == target) {
> + idx = i;
> + break;
> + }
> + match_idx++;
> + }
> }
> }
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 16:43 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:42 [PATCH] net/sched: fq_codel: choose random fat flow to drop from, if multiple are equal Andreea-Carina Deaconu
2026-12-03 16:43 ` Andreea-Carina Deaconu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox