1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sched/sch_sfb.c Stochastic Fair Blue
5 * Copyright (c) 2008-2011 Juliusz Chroboczek <jch@pps.jussieu.fr>
6 * Copyright (c) 2011 Eric Dumazet <eric.dumazet@gmail.com>
8 * W. Feng, D. Kandlur, D. Saha, K. Shin. Blue:
9 * A New Class of Active Queue Management Algorithms.
10 * U. Michigan CSE-TR-387-99, April 1999.
12 * http://www.thefengs.com/wuchang/blue/CSE-TR-387-99.pdf
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <linux/random.h>
21 #include <linux/siphash.h>
23 #include <net/pkt_sched.h>
24 #include <net/pkt_cls.h>
25 #include <net/inet_ecn.h>
28 * SFB uses two B[l][n] : L x N arrays of bins (L levels, N bins per level)
29 * This implementation uses L = 8 and N = 16
30 * This permits us to split one 32bit hash (provided per packet by rxhash or
31 * external classifier) into 8 subhashes of 4 bits.
33 #define SFB_BUCKET_SHIFT 4
34 #define SFB_NUMBUCKETS (1 << SFB_BUCKET_SHIFT) /* N bins per Level */
35 #define SFB_BUCKET_MASK (SFB_NUMBUCKETS - 1)
36 #define SFB_LEVELS (32 / SFB_BUCKET_SHIFT) /* L */
38 /* SFB algo uses a virtual queue, named "bin" */
40 u16 qlen; /* length of virtual queue */
41 u16 p_mark; /* marking probability */
44 /* We use a double buffering right before hash change
45 * (Section 4.4 of SFB reference : moving hash functions)
48 siphash_key_t perturbation; /* siphash key */
49 struct sfb_bucket bins[SFB_LEVELS][SFB_NUMBUCKETS];
52 struct sfb_sched_data {
54 struct tcf_proto __rcu *filter_list;
55 struct tcf_block *block;
56 unsigned long rehash_interval;
57 unsigned long warmup_time; /* double buffering warmup time in jiffies */
59 u32 bin_size; /* maximum queue length per bin */
60 u32 increment; /* d1 */
61 u32 decrement; /* d2 */
62 u32 limit; /* HARD maximal queue length */
66 unsigned long rehash_time;
67 unsigned long token_time;
69 u8 slot; /* current active bins (0 or 1) */
70 bool double_buffering;
71 struct sfb_bins bins[2];
78 u32 childdrop; /* drops in child qdisc */
79 u32 marked; /* ECN mark */
84 * Each queued skb might be hashed on one or two bins
85 * We store in skb_cb the two hash values.
86 * (A zero value means double buffering was not used)
92 static inline struct sfb_skb_cb *sfb_skb_cb(const struct sk_buff *skb)
94 qdisc_cb_private_validate(skb, sizeof(struct sfb_skb_cb));
95 return (struct sfb_skb_cb *)qdisc_skb_cb(skb)->data;
99 * If using 'internal' SFB flow classifier, hash comes from skb rxhash
100 * If using external classifier, hash comes from the classid.
102 static u32 sfb_hash(const struct sk_buff *skb, u32 slot)
104 return sfb_skb_cb(skb)->hashes[slot];
107 /* Probabilities are coded as Q0.16 fixed-point values,
108 * with 0xFFFF representing 65535/65536 (almost 1.0)
109 * Addition and subtraction are saturating in [0, 65535]
111 static u32 prob_plus(u32 p1, u32 p2)
115 return min_t(u32, res, SFB_MAX_PROB);
118 static u32 prob_minus(u32 p1, u32 p2)
120 return p1 > p2 ? p1 - p2 : 0;
123 static void increment_one_qlen(u32 sfbhash, u32 slot, struct sfb_sched_data *q)
126 struct sfb_bucket *b = &q->bins[slot].bins[0][0];
128 for (i = 0; i < SFB_LEVELS; i++) {
129 u32 hash = sfbhash & SFB_BUCKET_MASK;
131 sfbhash >>= SFB_BUCKET_SHIFT;
132 if (b[hash].qlen < 0xFFFF)
134 b += SFB_NUMBUCKETS; /* next level */
138 static void increment_qlen(const struct sfb_skb_cb *cb, struct sfb_sched_data *q)
142 sfbhash = cb->hashes[0];
144 increment_one_qlen(sfbhash, 0, q);
146 sfbhash = cb->hashes[1];
148 increment_one_qlen(sfbhash, 1, q);
151 static void decrement_one_qlen(u32 sfbhash, u32 slot,
152 struct sfb_sched_data *q)
155 struct sfb_bucket *b = &q->bins[slot].bins[0][0];
157 for (i = 0; i < SFB_LEVELS; i++) {
158 u32 hash = sfbhash & SFB_BUCKET_MASK;
160 sfbhash >>= SFB_BUCKET_SHIFT;
161 if (b[hash].qlen > 0)
163 b += SFB_NUMBUCKETS; /* next level */
167 static void decrement_qlen(const struct sk_buff *skb, struct sfb_sched_data *q)
171 sfbhash = sfb_hash(skb, 0);
173 decrement_one_qlen(sfbhash, 0, q);
175 sfbhash = sfb_hash(skb, 1);
177 decrement_one_qlen(sfbhash, 1, q);
180 static void decrement_prob(struct sfb_bucket *b, struct sfb_sched_data *q)
182 b->p_mark = prob_minus(b->p_mark, q->decrement);
185 static void increment_prob(struct sfb_bucket *b, struct sfb_sched_data *q)
187 b->p_mark = prob_plus(b->p_mark, q->increment);
190 static void sfb_zero_all_buckets(struct sfb_sched_data *q)
192 memset(&q->bins, 0, sizeof(q->bins));
196 * compute max qlen, max p_mark, and avg p_mark
198 static u32 sfb_compute_qlen(u32 *prob_r, u32 *avgpm_r, const struct sfb_sched_data *q)
201 u32 qlen = 0, prob = 0, totalpm = 0;
202 const struct sfb_bucket *b = &q->bins[q->slot].bins[0][0];
204 for (i = 0; i < SFB_LEVELS * SFB_NUMBUCKETS; i++) {
207 totalpm += b->p_mark;
208 if (prob < b->p_mark)
213 *avgpm_r = totalpm / (SFB_LEVELS * SFB_NUMBUCKETS);
218 static void sfb_init_perturbation(u32 slot, struct sfb_sched_data *q)
220 get_random_bytes(&q->bins[slot].perturbation,
221 sizeof(q->bins[slot].perturbation));
224 static void sfb_swap_slot(struct sfb_sched_data *q)
226 sfb_init_perturbation(q->slot, q);
228 q->double_buffering = false;
231 /* Non elastic flows are allowed to use part of the bandwidth, expressed
232 * in "penalty_rate" packets per second, with "penalty_burst" burst
234 static bool sfb_rate_limit(struct sk_buff *skb, struct sfb_sched_data *q)
236 if (q->penalty_rate == 0 || q->penalty_burst == 0)
239 if (q->tokens_avail < 1) {
240 unsigned long age = min(10UL * HZ, jiffies - q->token_time);
242 q->tokens_avail = (age * q->penalty_rate) / HZ;
243 if (q->tokens_avail > q->penalty_burst)
244 q->tokens_avail = q->penalty_burst;
245 q->token_time = jiffies;
246 if (q->tokens_avail < 1)
254 static bool sfb_classify(struct sk_buff *skb, struct tcf_proto *fl,
255 int *qerr, u32 *salt)
257 struct tcf_result res;
260 result = tcf_classify(skb, NULL, fl, &res, false);
262 #ifdef CONFIG_NET_CLS_ACT
267 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
273 *salt = TC_H_MIN(res.classid);
279 static int sfb_enqueue(struct sk_buff *skb, struct Qdisc *sch,
280 struct sk_buff **to_free)
283 struct sfb_sched_data *q = qdisc_priv(sch);
284 unsigned int len = qdisc_pkt_len(skb);
285 struct Qdisc *child = q->qdisc;
286 struct tcf_proto *fl;
287 struct sfb_skb_cb cb;
293 int ret = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
295 if (unlikely(sch->q.qlen >= q->limit)) {
296 qdisc_qstats_overlimit(sch);
297 q->stats.queuedrop++;
301 if (q->rehash_interval > 0) {
302 unsigned long limit = q->rehash_time + q->rehash_interval;
304 if (unlikely(time_after(jiffies, limit))) {
306 q->rehash_time = jiffies;
307 } else if (unlikely(!q->double_buffering && q->warmup_time > 0 &&
308 time_after(jiffies, limit - q->warmup_time))) {
309 q->double_buffering = true;
313 fl = rcu_dereference_bh(q->filter_list);
317 /* If using external classifiers, get result and record it. */
318 if (!sfb_classify(skb, fl, &ret, &salt))
320 sfbhash = siphash_1u32(salt, &q->bins[slot].perturbation);
322 sfbhash = skb_get_hash_perturb(skb, &q->bins[slot].perturbation);
328 sfb_skb_cb(skb)->hashes[slot] = sfbhash;
330 for (i = 0; i < SFB_LEVELS; i++) {
331 u32 hash = sfbhash & SFB_BUCKET_MASK;
332 struct sfb_bucket *b = &q->bins[slot].bins[i][hash];
334 sfbhash >>= SFB_BUCKET_SHIFT;
336 decrement_prob(b, q);
337 else if (b->qlen >= q->bin_size)
338 increment_prob(b, q);
339 if (minqlen > b->qlen)
341 if (p_min > b->p_mark)
346 sfb_skb_cb(skb)->hashes[slot] = 0;
348 if (unlikely(minqlen >= q->max)) {
349 qdisc_qstats_overlimit(sch);
350 q->stats.bucketdrop++;
354 if (unlikely(p_min >= SFB_MAX_PROB)) {
356 if (q->double_buffering) {
357 sfbhash = skb_get_hash_perturb(skb,
358 &q->bins[slot].perturbation);
361 sfb_skb_cb(skb)->hashes[slot] = sfbhash;
363 for (i = 0; i < SFB_LEVELS; i++) {
364 u32 hash = sfbhash & SFB_BUCKET_MASK;
365 struct sfb_bucket *b = &q->bins[slot].bins[i][hash];
367 sfbhash >>= SFB_BUCKET_SHIFT;
369 decrement_prob(b, q);
370 else if (b->qlen >= q->bin_size)
371 increment_prob(b, q);
374 if (sfb_rate_limit(skb, q)) {
375 qdisc_qstats_overlimit(sch);
376 q->stats.penaltydrop++;
382 r = get_random_u16() & SFB_MAX_PROB;
384 if (unlikely(r < p_min)) {
385 if (unlikely(p_min > SFB_MAX_PROB / 2)) {
386 /* If we're marking that many packets, then either
387 * this flow is unresponsive, or we're badly congested.
388 * In either case, we want to start dropping packets.
390 if (r < (p_min - SFB_MAX_PROB / 2) * 2) {
391 q->stats.earlydrop++;
395 if (INET_ECN_set_ce(skb)) {
398 q->stats.earlydrop++;
404 memcpy(&cb, sfb_skb_cb(skb), sizeof(cb));
405 ret = qdisc_enqueue(skb, child, to_free);
406 if (likely(ret == NET_XMIT_SUCCESS)) {
407 sch->qstats.backlog += len;
409 increment_qlen(&cb, q);
410 } else if (net_xmit_drop_count(ret)) {
411 q->stats.childdrop++;
412 qdisc_qstats_drop(sch);
417 qdisc_drop(skb, sch, to_free);
420 if (ret & __NET_XMIT_BYPASS)
421 qdisc_qstats_drop(sch);
426 static struct sk_buff *sfb_dequeue(struct Qdisc *sch)
428 struct sfb_sched_data *q = qdisc_priv(sch);
429 struct Qdisc *child = q->qdisc;
432 skb = child->dequeue(q->qdisc);
435 qdisc_bstats_update(sch, skb);
436 qdisc_qstats_backlog_dec(sch, skb);
438 decrement_qlen(skb, q);
444 static struct sk_buff *sfb_peek(struct Qdisc *sch)
446 struct sfb_sched_data *q = qdisc_priv(sch);
447 struct Qdisc *child = q->qdisc;
449 return child->ops->peek(child);
452 /* No sfb_drop -- impossible since the child doesn't return the dropped skb. */
454 static void sfb_reset(struct Qdisc *sch)
456 struct sfb_sched_data *q = qdisc_priv(sch);
458 if (likely(q->qdisc))
459 qdisc_reset(q->qdisc);
461 q->double_buffering = false;
462 sfb_zero_all_buckets(q);
463 sfb_init_perturbation(0, q);
466 static void sfb_destroy(struct Qdisc *sch)
468 struct sfb_sched_data *q = qdisc_priv(sch);
470 tcf_block_put(q->block);
474 static const struct nla_policy sfb_policy[TCA_SFB_MAX + 1] = {
475 [TCA_SFB_PARMS] = { .len = sizeof(struct tc_sfb_qopt) },
478 static const struct tc_sfb_qopt sfb_default_ops = {
479 .rehash_interval = 600 * MSEC_PER_SEC,
480 .warmup_time = 60 * MSEC_PER_SEC,
484 .increment = (SFB_MAX_PROB + 500) / 1000, /* 0.1 % */
485 .decrement = (SFB_MAX_PROB + 3000) / 6000,
490 static int sfb_change(struct Qdisc *sch, struct nlattr *opt,
491 struct netlink_ext_ack *extack)
493 struct sfb_sched_data *q = qdisc_priv(sch);
494 struct Qdisc *child, *old;
495 struct nlattr *tb[TCA_SFB_MAX + 1];
496 const struct tc_sfb_qopt *ctl = &sfb_default_ops;
501 err = nla_parse_nested_deprecated(tb, TCA_SFB_MAX, opt,
506 if (tb[TCA_SFB_PARMS] == NULL)
509 ctl = nla_data(tb[TCA_SFB_PARMS]);
514 limit = qdisc_dev(sch)->tx_queue_len;
516 child = fifo_create_dflt(sch, &pfifo_qdisc_ops, limit, extack);
518 return PTR_ERR(child);
520 if (child != &noop_qdisc)
521 qdisc_hash_add(child, true);
524 qdisc_purge_queue(q->qdisc);
528 q->rehash_interval = msecs_to_jiffies(ctl->rehash_interval);
529 q->warmup_time = msecs_to_jiffies(ctl->warmup_time);
530 q->rehash_time = jiffies;
532 q->increment = ctl->increment;
533 q->decrement = ctl->decrement;
535 q->bin_size = ctl->bin_size;
536 q->penalty_rate = ctl->penalty_rate;
537 q->penalty_burst = ctl->penalty_burst;
538 q->tokens_avail = ctl->penalty_burst;
539 q->token_time = jiffies;
542 q->double_buffering = false;
543 sfb_zero_all_buckets(q);
544 sfb_init_perturbation(0, q);
545 sfb_init_perturbation(1, q);
547 sch_tree_unlock(sch);
553 static int sfb_init(struct Qdisc *sch, struct nlattr *opt,
554 struct netlink_ext_ack *extack)
556 struct sfb_sched_data *q = qdisc_priv(sch);
559 err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
563 q->qdisc = &noop_qdisc;
564 return sfb_change(sch, opt, extack);
567 static int sfb_dump(struct Qdisc *sch, struct sk_buff *skb)
569 struct sfb_sched_data *q = qdisc_priv(sch);
571 struct tc_sfb_qopt opt = {
572 .rehash_interval = jiffies_to_msecs(q->rehash_interval),
573 .warmup_time = jiffies_to_msecs(q->warmup_time),
576 .bin_size = q->bin_size,
577 .increment = q->increment,
578 .decrement = q->decrement,
579 .penalty_rate = q->penalty_rate,
580 .penalty_burst = q->penalty_burst,
583 sch->qstats.backlog = q->qdisc->qstats.backlog;
584 opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
586 goto nla_put_failure;
587 if (nla_put(skb, TCA_SFB_PARMS, sizeof(opt), &opt))
588 goto nla_put_failure;
589 return nla_nest_end(skb, opts);
592 nla_nest_cancel(skb, opts);
596 static int sfb_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
598 struct sfb_sched_data *q = qdisc_priv(sch);
599 struct tc_sfb_xstats st = {
600 .earlydrop = q->stats.earlydrop,
601 .penaltydrop = q->stats.penaltydrop,
602 .bucketdrop = q->stats.bucketdrop,
603 .queuedrop = q->stats.queuedrop,
604 .childdrop = q->stats.childdrop,
605 .marked = q->stats.marked,
608 st.maxqlen = sfb_compute_qlen(&st.maxprob, &st.avgprob, q);
610 return gnet_stats_copy_app(d, &st, sizeof(st));
613 static int sfb_dump_class(struct Qdisc *sch, unsigned long cl,
614 struct sk_buff *skb, struct tcmsg *tcm)
619 static int sfb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
620 struct Qdisc **old, struct netlink_ext_ack *extack)
622 struct sfb_sched_data *q = qdisc_priv(sch);
627 *old = qdisc_replace(sch, new, &q->qdisc);
631 static struct Qdisc *sfb_leaf(struct Qdisc *sch, unsigned long arg)
633 struct sfb_sched_data *q = qdisc_priv(sch);
638 static unsigned long sfb_find(struct Qdisc *sch, u32 classid)
643 static void sfb_unbind(struct Qdisc *sch, unsigned long arg)
647 static int sfb_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
648 struct nlattr **tca, unsigned long *arg,
649 struct netlink_ext_ack *extack)
654 static int sfb_delete(struct Qdisc *sch, unsigned long cl,
655 struct netlink_ext_ack *extack)
660 static void sfb_walk(struct Qdisc *sch, struct qdisc_walker *walker)
663 tc_qdisc_stats_dump(sch, 1, walker);
667 static struct tcf_block *sfb_tcf_block(struct Qdisc *sch, unsigned long cl,
668 struct netlink_ext_ack *extack)
670 struct sfb_sched_data *q = qdisc_priv(sch);
677 static unsigned long sfb_bind(struct Qdisc *sch, unsigned long parent,
684 static const struct Qdisc_class_ops sfb_class_ops = {
688 .change = sfb_change_class,
689 .delete = sfb_delete,
691 .tcf_block = sfb_tcf_block,
692 .bind_tcf = sfb_bind,
693 .unbind_tcf = sfb_unbind,
694 .dump = sfb_dump_class,
697 static struct Qdisc_ops sfb_qdisc_ops __read_mostly = {
699 .priv_size = sizeof(struct sfb_sched_data),
700 .cl_ops = &sfb_class_ops,
701 .enqueue = sfb_enqueue,
702 .dequeue = sfb_dequeue,
706 .destroy = sfb_destroy,
707 .change = sfb_change,
709 .dump_stats = sfb_dump_stats,
710 .owner = THIS_MODULE,
713 static int __init sfb_module_init(void)
715 return register_qdisc(&sfb_qdisc_ops);
718 static void __exit sfb_module_exit(void)
720 unregister_qdisc(&sfb_qdisc_ops);
723 module_init(sfb_module_init)
724 module_exit(sfb_module_exit)
726 MODULE_DESCRIPTION("Stochastic Fair Blue queue discipline");
727 MODULE_AUTHOR("Juliusz Chroboczek");
728 MODULE_AUTHOR("Eric Dumazet");
729 MODULE_LICENSE("GPL");