1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Flow Queue PIE discipline
4 * Copyright (C) 2019 Mohit P. Tahiliani <tahiliani@nitk.edu.in>
5 * Copyright (C) 2019 Sachin D. Patil <sdp.sachin@gmail.com>
6 * Copyright (C) 2019 V. Saicharan <vsaicharan1998@gmail.com>
7 * Copyright (C) 2019 Mohit Bhasi <mohitbhasi1998@gmail.com>
8 * Copyright (C) 2019 Leslie Monis <lesliemonis@gmail.com>
9 * Copyright (C) 2019 Gautam Ramakrishnan <gautamramk@gmail.com>
12 #include <linux/jhash.h>
13 #include <linux/module.h>
14 #include <linux/sizes.h>
15 #include <linux/vmalloc.h>
16 #include <net/pkt_cls.h>
22 * - Packets are classified on flows.
23 * - This is a Stochastic model (as we use a hash, several flows might
24 * be hashed to the same slot)
25 * - Each flow has a PIE managed queue.
26 * - Flows are linked onto two (Round Robin) lists,
27 * so that new flows have priority on old ones.
28 * - For a given flow, packets are not reordered.
29 * - Drops during enqueue only.
30 * - ECN capability is off by default.
31 * - ECN threshold (if ECN is enabled) is at 10% by default.
32 * - Uses timestamps to calculate queue delay by default.
36 * struct fq_pie_flow - contains data for each flow
37 * @vars: pie vars associated with the flow
38 * @deficit: number of remaining byte credits
39 * @backlog: size of data in the flow
40 * @qlen: number of packets in the flow
41 * @flowchain: flowchain for the flow
42 * @head: first packet in the flow
43 * @tail: last packet in the flow
50 struct list_head flowchain;
55 struct fq_pie_sched_data {
56 struct tcf_proto __rcu *filter_list; /* optional external classifier */
57 struct tcf_block *block;
58 struct fq_pie_flow *flows;
60 struct list_head old_flows;
61 struct list_head new_flows;
62 struct pie_params p_params;
71 struct pie_stats stats;
72 struct timer_list adapt_timer;
75 static unsigned int fq_pie_hash(const struct fq_pie_sched_data *q,
78 return reciprocal_scale(skb_get_hash(skb), q->flows_cnt);
81 static unsigned int fq_pie_classify(struct sk_buff *skb, struct Qdisc *sch,
84 struct fq_pie_sched_data *q = qdisc_priv(sch);
85 struct tcf_proto *filter;
86 struct tcf_result res;
89 if (TC_H_MAJ(skb->priority) == sch->handle &&
90 TC_H_MIN(skb->priority) > 0 &&
91 TC_H_MIN(skb->priority) <= q->flows_cnt)
92 return TC_H_MIN(skb->priority);
94 filter = rcu_dereference_bh(q->filter_list);
96 return fq_pie_hash(q, skb) + 1;
98 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
99 result = tcf_classify(skb, NULL, filter, &res, false);
101 #ifdef CONFIG_NET_CLS_ACT
106 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
112 if (TC_H_MIN(res.classid) <= q->flows_cnt)
113 return TC_H_MIN(res.classid);
118 /* add skb to flow queue (tail add) */
119 static inline void flow_queue_add(struct fq_pie_flow *flow,
125 flow->tail->next = skb;
130 static int fq_pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
131 struct sk_buff **to_free)
133 enum skb_drop_reason reason = SKB_DROP_REASON_QDISC_OVERLIMIT;
134 struct fq_pie_sched_data *q = qdisc_priv(sch);
135 struct fq_pie_flow *sel_flow;
137 u8 memory_limited = false;
142 /* Classifies packet into corresponding flow */
143 idx = fq_pie_classify(skb, sch, &ret);
145 if (ret & __NET_XMIT_BYPASS)
146 qdisc_qstats_drop(sch);
147 __qdisc_drop(skb, to_free);
152 sel_flow = &q->flows[idx];
153 /* Checks whether adding a new packet would exceed memory limit */
154 get_pie_cb(skb)->mem_usage = skb->truesize;
155 memory_limited = q->memory_usage > q->memory_limit + skb->truesize;
157 /* Checks if the qdisc is full */
158 if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
159 q->stats.overlimit++;
161 } else if (unlikely(memory_limited)) {
165 reason = SKB_DROP_REASON_QDISC_CONGESTED;
167 if (!pie_drop_early(sch, &q->p_params, &sel_flow->vars,
168 sel_flow->backlog, skb->len)) {
170 } else if (q->p_params.ecn &&
171 sel_flow->vars.prob <= (MAX_PROB / 100) * q->ecn_prob &&
172 INET_ECN_set_ce(skb)) {
173 /* If packet is ecn capable, mark it if drop probability
174 * is lower than the parameter ecn_prob, else drop it.
180 /* Set enqueue time only when dq_rate_estimator is disabled. */
181 if (!q->p_params.dq_rate_estimator)
182 pie_set_enqueue_time(skb);
184 pkt_len = qdisc_pkt_len(skb);
185 q->stats.packets_in++;
186 q->memory_usage += skb->truesize;
187 sch->qstats.backlog += pkt_len;
189 flow_queue_add(sel_flow, skb);
190 if (list_empty(&sel_flow->flowchain)) {
191 list_add_tail(&sel_flow->flowchain, &q->new_flows);
193 sel_flow->deficit = q->quantum;
195 sel_flow->backlog = 0;
198 sel_flow->backlog += pkt_len;
199 return NET_XMIT_SUCCESS;
203 sel_flow->vars.accu_prob = 0;
204 qdisc_drop_reason(skb, sch, to_free, reason);
208 static const struct netlink_range_validation fq_pie_q_range = {
213 static const struct nla_policy fq_pie_policy[TCA_FQ_PIE_MAX + 1] = {
214 [TCA_FQ_PIE_LIMIT] = {.type = NLA_U32},
215 [TCA_FQ_PIE_FLOWS] = {.type = NLA_U32},
216 [TCA_FQ_PIE_TARGET] = {.type = NLA_U32},
217 [TCA_FQ_PIE_TUPDATE] = {.type = NLA_U32},
218 [TCA_FQ_PIE_ALPHA] = {.type = NLA_U32},
219 [TCA_FQ_PIE_BETA] = {.type = NLA_U32},
220 [TCA_FQ_PIE_QUANTUM] =
221 NLA_POLICY_FULL_RANGE(NLA_U32, &fq_pie_q_range),
222 [TCA_FQ_PIE_MEMORY_LIMIT] = {.type = NLA_U32},
223 [TCA_FQ_PIE_ECN_PROB] = {.type = NLA_U32},
224 [TCA_FQ_PIE_ECN] = {.type = NLA_U32},
225 [TCA_FQ_PIE_BYTEMODE] = {.type = NLA_U32},
226 [TCA_FQ_PIE_DQ_RATE_ESTIMATOR] = {.type = NLA_U32},
229 static inline struct sk_buff *dequeue_head(struct fq_pie_flow *flow)
231 struct sk_buff *skb = flow->head;
233 flow->head = skb->next;
238 static struct sk_buff *fq_pie_qdisc_dequeue(struct Qdisc *sch)
240 struct fq_pie_sched_data *q = qdisc_priv(sch);
241 struct sk_buff *skb = NULL;
242 struct fq_pie_flow *flow;
243 struct list_head *head;
247 head = &q->new_flows;
248 if (list_empty(head)) {
249 head = &q->old_flows;
250 if (list_empty(head))
254 flow = list_first_entry(head, struct fq_pie_flow, flowchain);
255 /* Flow has exhausted all its credits */
256 if (flow->deficit <= 0) {
257 flow->deficit += q->quantum;
258 list_move_tail(&flow->flowchain, &q->old_flows);
263 skb = dequeue_head(flow);
264 pkt_len = qdisc_pkt_len(skb);
265 sch->qstats.backlog -= pkt_len;
267 qdisc_bstats_update(sch, skb);
271 /* force a pass through old_flows to prevent starvation */
272 if (head == &q->new_flows && !list_empty(&q->old_flows))
273 list_move_tail(&flow->flowchain, &q->old_flows);
275 list_del_init(&flow->flowchain);
280 flow->deficit -= pkt_len;
281 flow->backlog -= pkt_len;
282 q->memory_usage -= get_pie_cb(skb)->mem_usage;
283 pie_process_dequeue(skb, &q->p_params, &flow->vars, flow->backlog);
287 static int fq_pie_change(struct Qdisc *sch, struct nlattr *opt,
288 struct netlink_ext_ack *extack)
290 struct fq_pie_sched_data *q = qdisc_priv(sch);
291 struct nlattr *tb[TCA_FQ_PIE_MAX + 1];
292 unsigned int len_dropped = 0;
293 unsigned int num_dropped = 0;
296 err = nla_parse_nested(tb, TCA_FQ_PIE_MAX, opt, fq_pie_policy, extack);
301 if (tb[TCA_FQ_PIE_LIMIT]) {
302 u32 limit = nla_get_u32(tb[TCA_FQ_PIE_LIMIT]);
304 WRITE_ONCE(q->p_params.limit, limit);
305 WRITE_ONCE(sch->limit, limit);
307 if (tb[TCA_FQ_PIE_FLOWS]) {
309 NL_SET_ERR_MSG_MOD(extack,
310 "Number of flows cannot be changed");
313 q->flows_cnt = nla_get_u32(tb[TCA_FQ_PIE_FLOWS]);
314 if (!q->flows_cnt || q->flows_cnt > 65536) {
315 NL_SET_ERR_MSG_MOD(extack,
316 "Number of flows must range in [1..65536]");
321 /* convert from microseconds to pschedtime */
322 if (tb[TCA_FQ_PIE_TARGET]) {
323 /* target is in us */
324 u32 target = nla_get_u32(tb[TCA_FQ_PIE_TARGET]);
326 /* convert to pschedtime */
327 WRITE_ONCE(q->p_params.target,
328 PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC));
331 /* tupdate is in jiffies */
332 if (tb[TCA_FQ_PIE_TUPDATE])
333 WRITE_ONCE(q->p_params.tupdate,
334 usecs_to_jiffies(nla_get_u32(tb[TCA_FQ_PIE_TUPDATE])));
336 if (tb[TCA_FQ_PIE_ALPHA])
337 WRITE_ONCE(q->p_params.alpha,
338 nla_get_u32(tb[TCA_FQ_PIE_ALPHA]));
340 if (tb[TCA_FQ_PIE_BETA])
341 WRITE_ONCE(q->p_params.beta,
342 nla_get_u32(tb[TCA_FQ_PIE_BETA]));
344 if (tb[TCA_FQ_PIE_QUANTUM])
345 WRITE_ONCE(q->quantum, nla_get_u32(tb[TCA_FQ_PIE_QUANTUM]));
347 if (tb[TCA_FQ_PIE_MEMORY_LIMIT])
348 WRITE_ONCE(q->memory_limit,
349 nla_get_u32(tb[TCA_FQ_PIE_MEMORY_LIMIT]));
351 if (tb[TCA_FQ_PIE_ECN_PROB])
352 WRITE_ONCE(q->ecn_prob,
353 nla_get_u32(tb[TCA_FQ_PIE_ECN_PROB]));
355 if (tb[TCA_FQ_PIE_ECN])
356 WRITE_ONCE(q->p_params.ecn,
357 nla_get_u32(tb[TCA_FQ_PIE_ECN]));
359 if (tb[TCA_FQ_PIE_BYTEMODE])
360 WRITE_ONCE(q->p_params.bytemode,
361 nla_get_u32(tb[TCA_FQ_PIE_BYTEMODE]));
363 if (tb[TCA_FQ_PIE_DQ_RATE_ESTIMATOR])
364 WRITE_ONCE(q->p_params.dq_rate_estimator,
365 nla_get_u32(tb[TCA_FQ_PIE_DQ_RATE_ESTIMATOR]));
367 /* Drop excess packets if new limit is lower */
368 while (sch->q.qlen > sch->limit) {
369 struct sk_buff *skb = qdisc_dequeue_internal(sch, false);
371 len_dropped += qdisc_pkt_len(skb);
373 rtnl_kfree_skbs(skb, skb);
375 qdisc_tree_reduce_backlog(sch, num_dropped, len_dropped);
377 sch_tree_unlock(sch);
381 sch_tree_unlock(sch);
385 static void fq_pie_timer(struct timer_list *t)
387 struct fq_pie_sched_data *q = timer_container_of(q, t, adapt_timer);
388 unsigned long next, tupdate;
389 struct Qdisc *sch = q->sch;
390 spinlock_t *root_lock; /* to lock qdisc for probability calculations */
394 root_lock = qdisc_lock(qdisc_root_sleeping(sch));
395 spin_lock(root_lock);
397 /* Limit this expensive loop to 2048 flows per round. */
398 max_cnt = min_t(int, q->flows_cnt - q->flows_cursor, 2048);
399 for (i = 0; i < max_cnt; i++) {
400 pie_calculate_probability(&q->p_params,
401 &q->flows[q->flows_cursor].vars,
402 q->flows[q->flows_cursor].backlog);
406 tupdate = q->p_params.tupdate;
408 if (q->flows_cursor >= q->flows_cnt) {
413 mod_timer(&q->adapt_timer, jiffies + next);
414 spin_unlock(root_lock);
418 static int fq_pie_init(struct Qdisc *sch, struct nlattr *opt,
419 struct netlink_ext_ack *extack)
421 struct fq_pie_sched_data *q = qdisc_priv(sch);
425 pie_params_init(&q->p_params);
426 sch->limit = 10 * 1024;
427 q->p_params.limit = sch->limit;
428 q->quantum = psched_mtu(qdisc_dev(sch));
432 q->memory_limit = SZ_32M;
434 INIT_LIST_HEAD(&q->new_flows);
435 INIT_LIST_HEAD(&q->old_flows);
436 timer_setup(&q->adapt_timer, fq_pie_timer, 0);
439 err = fq_pie_change(sch, opt, extack);
445 err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
449 q->flows = kvcalloc(q->flows_cnt, sizeof(struct fq_pie_flow),
455 for (idx = 0; idx < q->flows_cnt; idx++) {
456 struct fq_pie_flow *flow = q->flows + idx;
458 INIT_LIST_HEAD(&flow->flowchain);
459 pie_vars_init(&flow->vars);
462 mod_timer(&q->adapt_timer, jiffies + HZ / 2);
472 static int fq_pie_dump(struct Qdisc *sch, struct sk_buff *skb)
474 struct fq_pie_sched_data *q = qdisc_priv(sch);
477 opts = nla_nest_start(skb, TCA_OPTIONS);
481 /* convert target from pschedtime to us */
482 if (nla_put_u32(skb, TCA_FQ_PIE_LIMIT, READ_ONCE(sch->limit)) ||
483 nla_put_u32(skb, TCA_FQ_PIE_FLOWS, READ_ONCE(q->flows_cnt)) ||
484 nla_put_u32(skb, TCA_FQ_PIE_TARGET,
485 ((u32)PSCHED_TICKS2NS(READ_ONCE(q->p_params.target))) /
487 nla_put_u32(skb, TCA_FQ_PIE_TUPDATE,
488 jiffies_to_usecs(READ_ONCE(q->p_params.tupdate))) ||
489 nla_put_u32(skb, TCA_FQ_PIE_ALPHA, READ_ONCE(q->p_params.alpha)) ||
490 nla_put_u32(skb, TCA_FQ_PIE_BETA, READ_ONCE(q->p_params.beta)) ||
491 nla_put_u32(skb, TCA_FQ_PIE_QUANTUM, READ_ONCE(q->quantum)) ||
492 nla_put_u32(skb, TCA_FQ_PIE_MEMORY_LIMIT,
493 READ_ONCE(q->memory_limit)) ||
494 nla_put_u32(skb, TCA_FQ_PIE_ECN_PROB, READ_ONCE(q->ecn_prob)) ||
495 nla_put_u32(skb, TCA_FQ_PIE_ECN, READ_ONCE(q->p_params.ecn)) ||
496 nla_put_u32(skb, TCA_FQ_PIE_BYTEMODE, READ_ONCE(q->p_params.bytemode)) ||
497 nla_put_u32(skb, TCA_FQ_PIE_DQ_RATE_ESTIMATOR,
498 READ_ONCE(q->p_params.dq_rate_estimator)))
499 goto nla_put_failure;
501 return nla_nest_end(skb, opts);
504 nla_nest_cancel(skb, opts);
508 static int fq_pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
510 struct fq_pie_sched_data *q = qdisc_priv(sch);
511 struct tc_fq_pie_xstats st = {
512 .packets_in = q->stats.packets_in,
513 .overlimit = q->stats.overlimit,
514 .overmemory = q->overmemory,
515 .dropped = q->stats.dropped,
516 .ecn_mark = q->stats.ecn_mark,
517 .new_flow_count = q->new_flow_count,
518 .memory_usage = q->memory_usage,
520 struct list_head *pos;
523 list_for_each(pos, &q->new_flows)
526 list_for_each(pos, &q->old_flows)
528 sch_tree_unlock(sch);
530 return gnet_stats_copy_app(d, &st, sizeof(st));
533 static void fq_pie_reset(struct Qdisc *sch)
535 struct fq_pie_sched_data *q = qdisc_priv(sch);
538 INIT_LIST_HEAD(&q->new_flows);
539 INIT_LIST_HEAD(&q->old_flows);
540 for (idx = 0; idx < q->flows_cnt; idx++) {
541 struct fq_pie_flow *flow = q->flows + idx;
543 /* Removes all packets from flow */
544 rtnl_kfree_skbs(flow->head, flow->tail);
547 INIT_LIST_HEAD(&flow->flowchain);
548 pie_vars_init(&flow->vars);
552 static void fq_pie_destroy(struct Qdisc *sch)
554 struct fq_pie_sched_data *q = qdisc_priv(sch);
556 tcf_block_put(q->block);
557 q->p_params.tupdate = 0;
558 timer_delete_sync(&q->adapt_timer);
562 static struct Qdisc_ops fq_pie_qdisc_ops __read_mostly = {
564 .priv_size = sizeof(struct fq_pie_sched_data),
565 .enqueue = fq_pie_qdisc_enqueue,
566 .dequeue = fq_pie_qdisc_dequeue,
567 .peek = qdisc_peek_dequeued,
569 .destroy = fq_pie_destroy,
570 .reset = fq_pie_reset,
571 .change = fq_pie_change,
573 .dump_stats = fq_pie_dump_stats,
574 .owner = THIS_MODULE,
576 MODULE_ALIAS_NET_SCH("fq_pie");
578 static int __init fq_pie_module_init(void)
580 return register_qdisc(&fq_pie_qdisc_ops);
583 static void __exit fq_pie_module_exit(void)
585 unregister_qdisc(&fq_pie_qdisc_ops);
588 module_init(fq_pie_module_init);
589 module_exit(fq_pie_module_exit);
591 MODULE_DESCRIPTION("Flow Queue Proportional Integral controller Enhanced (FQ-PIE)");
592 MODULE_AUTHOR("Mohit P. Tahiliani");
593 MODULE_LICENSE("GPL");