xref: /openbmc/linux/net/sched/sch_fq_codel.c (revision 264b87fa)
14b549a2eSEric Dumazet /*
24b549a2eSEric Dumazet  * Fair Queue CoDel discipline
34b549a2eSEric Dumazet  *
44b549a2eSEric Dumazet  *	This program is free software; you can redistribute it and/or
54b549a2eSEric Dumazet  *	modify it under the terms of the GNU General Public License
64b549a2eSEric Dumazet  *	as published by the Free Software Foundation; either version
74b549a2eSEric Dumazet  *	2 of the License, or (at your option) any later version.
84b549a2eSEric Dumazet  *
980ba92faSEric Dumazet  *  Copyright (C) 2012,2015 Eric Dumazet <edumazet@google.com>
104b549a2eSEric Dumazet  */
114b549a2eSEric Dumazet 
124b549a2eSEric Dumazet #include <linux/module.h>
134b549a2eSEric Dumazet #include <linux/types.h>
144b549a2eSEric Dumazet #include <linux/kernel.h>
154b549a2eSEric Dumazet #include <linux/jiffies.h>
164b549a2eSEric Dumazet #include <linux/string.h>
174b549a2eSEric Dumazet #include <linux/in.h>
184b549a2eSEric Dumazet #include <linux/errno.h>
194b549a2eSEric Dumazet #include <linux/init.h>
204b549a2eSEric Dumazet #include <linux/skbuff.h>
214b549a2eSEric Dumazet #include <linux/jhash.h>
224b549a2eSEric Dumazet #include <linux/slab.h>
234b549a2eSEric Dumazet #include <linux/vmalloc.h>
244b549a2eSEric Dumazet #include <net/netlink.h>
254b549a2eSEric Dumazet #include <net/pkt_sched.h>
264b549a2eSEric Dumazet #include <net/codel.h>
27d068ca2aSMichal Kazior #include <net/codel_impl.h>
28d068ca2aSMichal Kazior #include <net/codel_qdisc.h>
294b549a2eSEric Dumazet 
304b549a2eSEric Dumazet /*	Fair Queue CoDel.
314b549a2eSEric Dumazet  *
324b549a2eSEric Dumazet  * Principles :
334b549a2eSEric Dumazet  * Packets are classified (internal classifier or external) on flows.
344b549a2eSEric Dumazet  * This is a Stochastic model (as we use a hash, several flows
354b549a2eSEric Dumazet  *			       might be hashed on same slot)
364b549a2eSEric Dumazet  * Each flow has a CoDel managed queue.
374b549a2eSEric Dumazet  * Flows are linked onto two (Round Robin) lists,
384b549a2eSEric Dumazet  * so that new flows have priority on old ones.
394b549a2eSEric Dumazet  *
404b549a2eSEric Dumazet  * For a given flow, packets are not reordered (CoDel uses a FIFO)
414b549a2eSEric Dumazet  * head drops only.
424b549a2eSEric Dumazet  * ECN capability is on by default.
434b549a2eSEric Dumazet  * Low memory footprint (64 bytes per flow)
444b549a2eSEric Dumazet  */
454b549a2eSEric Dumazet 
464b549a2eSEric Dumazet struct fq_codel_flow {
474b549a2eSEric Dumazet 	struct sk_buff	  *head;
484b549a2eSEric Dumazet 	struct sk_buff	  *tail;
494b549a2eSEric Dumazet 	struct list_head  flowchain;
504b549a2eSEric Dumazet 	int		  deficit;
514b549a2eSEric Dumazet 	u32		  dropped; /* number of drops (or ECN marks) on this flow */
524b549a2eSEric Dumazet 	struct codel_vars cvars;
534b549a2eSEric Dumazet }; /* please try to keep this structure <= 64 bytes */
544b549a2eSEric Dumazet 
554b549a2eSEric Dumazet struct fq_codel_sched_data {
5625d8c0d5SJohn Fastabend 	struct tcf_proto __rcu *filter_list; /* optional external classifier */
574b549a2eSEric Dumazet 	struct fq_codel_flow *flows;	/* Flows table [flows_cnt] */
584b549a2eSEric Dumazet 	u32		*backlogs;	/* backlog table [flows_cnt] */
594b549a2eSEric Dumazet 	u32		flows_cnt;	/* number of flows */
604b549a2eSEric Dumazet 	u32		quantum;	/* psched_mtu(qdisc_dev(sch)); */
619d18562aSEric Dumazet 	u32		drop_batch_size;
6295b58430SEric Dumazet 	u32		memory_limit;
634b549a2eSEric Dumazet 	struct codel_params cparams;
644b549a2eSEric Dumazet 	struct codel_stats cstats;
6595b58430SEric Dumazet 	u32		memory_usage;
6695b58430SEric Dumazet 	u32		drop_overmemory;
674b549a2eSEric Dumazet 	u32		drop_overlimit;
684b549a2eSEric Dumazet 	u32		new_flow_count;
694b549a2eSEric Dumazet 
704b549a2eSEric Dumazet 	struct list_head new_flows;	/* list of new flows */
714b549a2eSEric Dumazet 	struct list_head old_flows;	/* list of old flows */
724b549a2eSEric Dumazet };
734b549a2eSEric Dumazet 
744b549a2eSEric Dumazet static unsigned int fq_codel_hash(const struct fq_codel_sched_data *q,
75342db221STom Herbert 				  struct sk_buff *skb)
764b549a2eSEric Dumazet {
77264b87faSAndrew Collins 	return reciprocal_scale(skb_get_hash(skb), q->flows_cnt);
784b549a2eSEric Dumazet }
794b549a2eSEric Dumazet 
804b549a2eSEric Dumazet static unsigned int fq_codel_classify(struct sk_buff *skb, struct Qdisc *sch,
814b549a2eSEric Dumazet 				      int *qerr)
824b549a2eSEric Dumazet {
834b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
8425d8c0d5SJohn Fastabend 	struct tcf_proto *filter;
854b549a2eSEric Dumazet 	struct tcf_result res;
864b549a2eSEric Dumazet 	int result;
874b549a2eSEric Dumazet 
884b549a2eSEric Dumazet 	if (TC_H_MAJ(skb->priority) == sch->handle &&
894b549a2eSEric Dumazet 	    TC_H_MIN(skb->priority) > 0 &&
904b549a2eSEric Dumazet 	    TC_H_MIN(skb->priority) <= q->flows_cnt)
914b549a2eSEric Dumazet 		return TC_H_MIN(skb->priority);
924b549a2eSEric Dumazet 
9369204cf7SValdis.Kletnieks@vt.edu 	filter = rcu_dereference_bh(q->filter_list);
9425d8c0d5SJohn Fastabend 	if (!filter)
954b549a2eSEric Dumazet 		return fq_codel_hash(q, skb) + 1;
964b549a2eSEric Dumazet 
974b549a2eSEric Dumazet 	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
983b3ae880SDaniel Borkmann 	result = tc_classify(skb, filter, &res, false);
994b549a2eSEric Dumazet 	if (result >= 0) {
1004b549a2eSEric Dumazet #ifdef CONFIG_NET_CLS_ACT
1014b549a2eSEric Dumazet 		switch (result) {
1024b549a2eSEric Dumazet 		case TC_ACT_STOLEN:
1034b549a2eSEric Dumazet 		case TC_ACT_QUEUED:
1044b549a2eSEric Dumazet 			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
1054b549a2eSEric Dumazet 		case TC_ACT_SHOT:
1064b549a2eSEric Dumazet 			return 0;
1074b549a2eSEric Dumazet 		}
1084b549a2eSEric Dumazet #endif
1094b549a2eSEric Dumazet 		if (TC_H_MIN(res.classid) <= q->flows_cnt)
1104b549a2eSEric Dumazet 			return TC_H_MIN(res.classid);
1114b549a2eSEric Dumazet 	}
1124b549a2eSEric Dumazet 	return 0;
1134b549a2eSEric Dumazet }
1144b549a2eSEric Dumazet 
1154b549a2eSEric Dumazet /* helper functions : might be changed when/if skb use a standard list_head */
1164b549a2eSEric Dumazet 
1174b549a2eSEric Dumazet /* remove one skb from head of slot queue */
1184b549a2eSEric Dumazet static inline struct sk_buff *dequeue_head(struct fq_codel_flow *flow)
1194b549a2eSEric Dumazet {
1204b549a2eSEric Dumazet 	struct sk_buff *skb = flow->head;
1214b549a2eSEric Dumazet 
1224b549a2eSEric Dumazet 	flow->head = skb->next;
1234b549a2eSEric Dumazet 	skb->next = NULL;
1244b549a2eSEric Dumazet 	return skb;
1254b549a2eSEric Dumazet }
1264b549a2eSEric Dumazet 
1274b549a2eSEric Dumazet /* add skb to flow queue (tail add) */
1284b549a2eSEric Dumazet static inline void flow_queue_add(struct fq_codel_flow *flow,
1294b549a2eSEric Dumazet 				  struct sk_buff *skb)
1304b549a2eSEric Dumazet {
1314b549a2eSEric Dumazet 	if (flow->head == NULL)
1324b549a2eSEric Dumazet 		flow->head = skb;
1334b549a2eSEric Dumazet 	else
1344b549a2eSEric Dumazet 		flow->tail->next = skb;
1354b549a2eSEric Dumazet 	flow->tail = skb;
1364b549a2eSEric Dumazet 	skb->next = NULL;
1374b549a2eSEric Dumazet }
1384b549a2eSEric Dumazet 
139520ac30fSEric Dumazet static unsigned int fq_codel_drop(struct Qdisc *sch, unsigned int max_packets,
140520ac30fSEric Dumazet 				  struct sk_buff **to_free)
1414b549a2eSEric Dumazet {
1424b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
1434b549a2eSEric Dumazet 	struct sk_buff *skb;
1444b549a2eSEric Dumazet 	unsigned int maxbacklog = 0, idx = 0, i, len;
1454b549a2eSEric Dumazet 	struct fq_codel_flow *flow;
1469d18562aSEric Dumazet 	unsigned int threshold;
14795b58430SEric Dumazet 	unsigned int mem = 0;
1484b549a2eSEric Dumazet 
1499d18562aSEric Dumazet 	/* Queue is full! Find the fat flow and drop packet(s) from it.
1504b549a2eSEric Dumazet 	 * This might sound expensive, but with 1024 flows, we scan
1514b549a2eSEric Dumazet 	 * 4KB of memory, and we dont need to handle a complex tree
1524b549a2eSEric Dumazet 	 * in fast path (packet queue/enqueue) with many cache misses.
1539d18562aSEric Dumazet 	 * In stress mode, we'll try to drop 64 packets from the flow,
1549d18562aSEric Dumazet 	 * amortizing this linear lookup to one cache line per drop.
1554b549a2eSEric Dumazet 	 */
1564b549a2eSEric Dumazet 	for (i = 0; i < q->flows_cnt; i++) {
1574b549a2eSEric Dumazet 		if (q->backlogs[i] > maxbacklog) {
1584b549a2eSEric Dumazet 			maxbacklog = q->backlogs[i];
1594b549a2eSEric Dumazet 			idx = i;
1604b549a2eSEric Dumazet 		}
1614b549a2eSEric Dumazet 	}
1629d18562aSEric Dumazet 
1639d18562aSEric Dumazet 	/* Our goal is to drop half of this fat flow backlog */
1649d18562aSEric Dumazet 	threshold = maxbacklog >> 1;
1659d18562aSEric Dumazet 
1664b549a2eSEric Dumazet 	flow = &q->flows[idx];
1679d18562aSEric Dumazet 	len = 0;
1689d18562aSEric Dumazet 	i = 0;
1699d18562aSEric Dumazet 	do {
1704b549a2eSEric Dumazet 		skb = dequeue_head(flow);
1719d18562aSEric Dumazet 		len += qdisc_pkt_len(skb);
172008830bcSEric Dumazet 		mem += get_codel_cb(skb)->mem_usage;
173520ac30fSEric Dumazet 		__qdisc_drop(skb, to_free);
1749d18562aSEric Dumazet 	} while (++i < max_packets && len < threshold);
1759d18562aSEric Dumazet 
1769d18562aSEric Dumazet 	flow->dropped += i;
1779d18562aSEric Dumazet 	q->backlogs[idx] -= len;
17895b58430SEric Dumazet 	q->memory_usage -= mem;
1799d18562aSEric Dumazet 	sch->qstats.drops += i;
1809d18562aSEric Dumazet 	sch->qstats.backlog -= len;
1819d18562aSEric Dumazet 	sch->q.qlen -= i;
1824b549a2eSEric Dumazet 	return idx;
1834b549a2eSEric Dumazet }
1844b549a2eSEric Dumazet 
185520ac30fSEric Dumazet static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch,
186520ac30fSEric Dumazet 			    struct sk_buff **to_free)
1874b549a2eSEric Dumazet {
1884b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
1899d18562aSEric Dumazet 	unsigned int idx, prev_backlog, prev_qlen;
1904b549a2eSEric Dumazet 	struct fq_codel_flow *flow;
1914b549a2eSEric Dumazet 	int uninitialized_var(ret);
19280e509dbSEric Dumazet 	unsigned int pkt_len;
19395b58430SEric Dumazet 	bool memory_limited;
1944b549a2eSEric Dumazet 
1954b549a2eSEric Dumazet 	idx = fq_codel_classify(skb, sch, &ret);
1964b549a2eSEric Dumazet 	if (idx == 0) {
1974b549a2eSEric Dumazet 		if (ret & __NET_XMIT_BYPASS)
19825331d6cSJohn Fastabend 			qdisc_qstats_drop(sch);
199520ac30fSEric Dumazet 		__qdisc_drop(skb, to_free);
2004b549a2eSEric Dumazet 		return ret;
2014b549a2eSEric Dumazet 	}
2024b549a2eSEric Dumazet 	idx--;
2034b549a2eSEric Dumazet 
2044b549a2eSEric Dumazet 	codel_set_enqueue_time(skb);
2054b549a2eSEric Dumazet 	flow = &q->flows[idx];
2064b549a2eSEric Dumazet 	flow_queue_add(flow, skb);
2074b549a2eSEric Dumazet 	q->backlogs[idx] += qdisc_pkt_len(skb);
20825331d6cSJohn Fastabend 	qdisc_qstats_backlog_inc(sch, skb);
2094b549a2eSEric Dumazet 
2104b549a2eSEric Dumazet 	if (list_empty(&flow->flowchain)) {
2114b549a2eSEric Dumazet 		list_add_tail(&flow->flowchain, &q->new_flows);
2124b549a2eSEric Dumazet 		q->new_flow_count++;
2134b549a2eSEric Dumazet 		flow->deficit = q->quantum;
2144b549a2eSEric Dumazet 		flow->dropped = 0;
2154b549a2eSEric Dumazet 	}
216008830bcSEric Dumazet 	get_codel_cb(skb)->mem_usage = skb->truesize;
217008830bcSEric Dumazet 	q->memory_usage += get_codel_cb(skb)->mem_usage;
21895b58430SEric Dumazet 	memory_limited = q->memory_usage > q->memory_limit;
21995b58430SEric Dumazet 	if (++sch->q.qlen <= sch->limit && !memory_limited)
2204b549a2eSEric Dumazet 		return NET_XMIT_SUCCESS;
2214b549a2eSEric Dumazet 
2222ccccf5fSWANG Cong 	prev_backlog = sch->qstats.backlog;
2239d18562aSEric Dumazet 	prev_qlen = sch->q.qlen;
2244b549a2eSEric Dumazet 
22580e509dbSEric Dumazet 	/* save this packet length as it might be dropped by fq_codel_drop() */
22680e509dbSEric Dumazet 	pkt_len = qdisc_pkt_len(skb);
2279d18562aSEric Dumazet 	/* fq_codel_drop() is quite expensive, as it performs a linear search
2289d18562aSEric Dumazet 	 * in q->backlogs[] to find a fat flow.
2299d18562aSEric Dumazet 	 * So instead of dropping a single packet, drop half of its backlog
2309d18562aSEric Dumazet 	 * with a 64 packets limit to not add a too big cpu spike here.
2319d18562aSEric Dumazet 	 */
232520ac30fSEric Dumazet 	ret = fq_codel_drop(sch, q->drop_batch_size, to_free);
2339d18562aSEric Dumazet 
23480e509dbSEric Dumazet 	prev_qlen -= sch->q.qlen;
23580e509dbSEric Dumazet 	prev_backlog -= sch->qstats.backlog;
23680e509dbSEric Dumazet 	q->drop_overlimit += prev_qlen;
23795b58430SEric Dumazet 	if (memory_limited)
23880e509dbSEric Dumazet 		q->drop_overmemory += prev_qlen;
2399d18562aSEric Dumazet 
24080e509dbSEric Dumazet 	/* As we dropped packet(s), better let upper stack know this.
24180e509dbSEric Dumazet 	 * If we dropped a packet for this flow, return NET_XMIT_CN,
24280e509dbSEric Dumazet 	 * but in this case, our parents wont increase their backlogs.
24380e509dbSEric Dumazet 	 */
24480e509dbSEric Dumazet 	if (ret == idx) {
24580e509dbSEric Dumazet 		qdisc_tree_reduce_backlog(sch, prev_qlen - 1,
24680e509dbSEric Dumazet 					  prev_backlog - pkt_len);
24780e509dbSEric Dumazet 		return NET_XMIT_CN;
24880e509dbSEric Dumazet 	}
24980e509dbSEric Dumazet 	qdisc_tree_reduce_backlog(sch, prev_qlen, prev_backlog);
25080e509dbSEric Dumazet 	return NET_XMIT_SUCCESS;
2514b549a2eSEric Dumazet }
2524b549a2eSEric Dumazet 
2534b549a2eSEric Dumazet /* This is the specific function called from codel_dequeue()
2544b549a2eSEric Dumazet  * to dequeue a packet from queue. Note: backlog is handled in
2554b549a2eSEric Dumazet  * codel, we dont need to reduce it here.
2564b549a2eSEric Dumazet  */
25779bdc4c8SMichal Kazior static struct sk_buff *dequeue_func(struct codel_vars *vars, void *ctx)
2584b549a2eSEric Dumazet {
25979bdc4c8SMichal Kazior 	struct Qdisc *sch = ctx;
260865ec552SEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
2614b549a2eSEric Dumazet 	struct fq_codel_flow *flow;
2624b549a2eSEric Dumazet 	struct sk_buff *skb = NULL;
2634b549a2eSEric Dumazet 
2644b549a2eSEric Dumazet 	flow = container_of(vars, struct fq_codel_flow, cvars);
2654b549a2eSEric Dumazet 	if (flow->head) {
2664b549a2eSEric Dumazet 		skb = dequeue_head(flow);
267865ec552SEric Dumazet 		q->backlogs[flow - q->flows] -= qdisc_pkt_len(skb);
268008830bcSEric Dumazet 		q->memory_usage -= get_codel_cb(skb)->mem_usage;
2694b549a2eSEric Dumazet 		sch->q.qlen--;
27079bdc4c8SMichal Kazior 		sch->qstats.backlog -= qdisc_pkt_len(skb);
2714b549a2eSEric Dumazet 	}
2724b549a2eSEric Dumazet 	return skb;
2734b549a2eSEric Dumazet }
2744b549a2eSEric Dumazet 
27579bdc4c8SMichal Kazior static void drop_func(struct sk_buff *skb, void *ctx)
27679bdc4c8SMichal Kazior {
27779bdc4c8SMichal Kazior 	struct Qdisc *sch = ctx;
27879bdc4c8SMichal Kazior 
279520ac30fSEric Dumazet 	kfree_skb(skb);
280520ac30fSEric Dumazet 	qdisc_qstats_drop(sch);
28179bdc4c8SMichal Kazior }
28279bdc4c8SMichal Kazior 
2834b549a2eSEric Dumazet static struct sk_buff *fq_codel_dequeue(struct Qdisc *sch)
2844b549a2eSEric Dumazet {
2854b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
2864b549a2eSEric Dumazet 	struct sk_buff *skb;
2874b549a2eSEric Dumazet 	struct fq_codel_flow *flow;
2884b549a2eSEric Dumazet 	struct list_head *head;
2894b549a2eSEric Dumazet 	u32 prev_drop_count, prev_ecn_mark;
2902ccccf5fSWANG Cong 	unsigned int prev_backlog;
2914b549a2eSEric Dumazet 
2924b549a2eSEric Dumazet begin:
2934b549a2eSEric Dumazet 	head = &q->new_flows;
2944b549a2eSEric Dumazet 	if (list_empty(head)) {
2954b549a2eSEric Dumazet 		head = &q->old_flows;
2964b549a2eSEric Dumazet 		if (list_empty(head))
2974b549a2eSEric Dumazet 			return NULL;
2984b549a2eSEric Dumazet 	}
2994b549a2eSEric Dumazet 	flow = list_first_entry(head, struct fq_codel_flow, flowchain);
3004b549a2eSEric Dumazet 
3014b549a2eSEric Dumazet 	if (flow->deficit <= 0) {
3024b549a2eSEric Dumazet 		flow->deficit += q->quantum;
3034b549a2eSEric Dumazet 		list_move_tail(&flow->flowchain, &q->old_flows);
3044b549a2eSEric Dumazet 		goto begin;
3054b549a2eSEric Dumazet 	}
3064b549a2eSEric Dumazet 
3074b549a2eSEric Dumazet 	prev_drop_count = q->cstats.drop_count;
3084b549a2eSEric Dumazet 	prev_ecn_mark = q->cstats.ecn_mark;
3092ccccf5fSWANG Cong 	prev_backlog = sch->qstats.backlog;
3104b549a2eSEric Dumazet 
31179bdc4c8SMichal Kazior 	skb = codel_dequeue(sch, &sch->qstats.backlog, &q->cparams,
31279bdc4c8SMichal Kazior 			    &flow->cvars, &q->cstats, qdisc_pkt_len,
31379bdc4c8SMichal Kazior 			    codel_get_enqueue_time, drop_func, dequeue_func);
3144b549a2eSEric Dumazet 
3154b549a2eSEric Dumazet 	flow->dropped += q->cstats.drop_count - prev_drop_count;
3164b549a2eSEric Dumazet 	flow->dropped += q->cstats.ecn_mark - prev_ecn_mark;
3174b549a2eSEric Dumazet 
3184b549a2eSEric Dumazet 	if (!skb) {
3194b549a2eSEric Dumazet 		/* force a pass through old_flows to prevent starvation */
3204b549a2eSEric Dumazet 		if ((head == &q->new_flows) && !list_empty(&q->old_flows))
3214b549a2eSEric Dumazet 			list_move_tail(&flow->flowchain, &q->old_flows);
3224b549a2eSEric Dumazet 		else
3234b549a2eSEric Dumazet 			list_del_init(&flow->flowchain);
3244b549a2eSEric Dumazet 		goto begin;
3254b549a2eSEric Dumazet 	}
3264b549a2eSEric Dumazet 	qdisc_bstats_update(sch, skb);
3274b549a2eSEric Dumazet 	flow->deficit -= qdisc_pkt_len(skb);
3282ccccf5fSWANG Cong 	/* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
3294b549a2eSEric Dumazet 	 * or HTB crashes. Defer it for next round.
3304b549a2eSEric Dumazet 	 */
3314b549a2eSEric Dumazet 	if (q->cstats.drop_count && sch->q.qlen) {
3322ccccf5fSWANG Cong 		qdisc_tree_reduce_backlog(sch, q->cstats.drop_count,
3332ccccf5fSWANG Cong 					  q->cstats.drop_len);
3344b549a2eSEric Dumazet 		q->cstats.drop_count = 0;
3352ccccf5fSWANG Cong 		q->cstats.drop_len = 0;
3364b549a2eSEric Dumazet 	}
3374b549a2eSEric Dumazet 	return skb;
3384b549a2eSEric Dumazet }
3394b549a2eSEric Dumazet 
340ece5d4c7SEric Dumazet static void fq_codel_flow_purge(struct fq_codel_flow *flow)
341ece5d4c7SEric Dumazet {
342ece5d4c7SEric Dumazet 	rtnl_kfree_skbs(flow->head, flow->tail);
343ece5d4c7SEric Dumazet 	flow->head = NULL;
344ece5d4c7SEric Dumazet }
345ece5d4c7SEric Dumazet 
3464b549a2eSEric Dumazet static void fq_codel_reset(struct Qdisc *sch)
3474b549a2eSEric Dumazet {
3483d0e0af4SEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
3493d0e0af4SEric Dumazet 	int i;
3504b549a2eSEric Dumazet 
3513d0e0af4SEric Dumazet 	INIT_LIST_HEAD(&q->new_flows);
3523d0e0af4SEric Dumazet 	INIT_LIST_HEAD(&q->old_flows);
3533d0e0af4SEric Dumazet 	for (i = 0; i < q->flows_cnt; i++) {
3543d0e0af4SEric Dumazet 		struct fq_codel_flow *flow = q->flows + i;
3553d0e0af4SEric Dumazet 
356ece5d4c7SEric Dumazet 		fq_codel_flow_purge(flow);
3573d0e0af4SEric Dumazet 		INIT_LIST_HEAD(&flow->flowchain);
3583d0e0af4SEric Dumazet 		codel_vars_init(&flow->cvars);
3593d0e0af4SEric Dumazet 	}
3603d0e0af4SEric Dumazet 	memset(q->backlogs, 0, q->flows_cnt * sizeof(u32));
3613d0e0af4SEric Dumazet 	sch->q.qlen = 0;
362ece5d4c7SEric Dumazet 	sch->qstats.backlog = 0;
36377f57761SEric Dumazet 	q->memory_usage = 0;
3643d0e0af4SEric Dumazet }
3653d0e0af4SEric Dumazet 
3664b549a2eSEric Dumazet static const struct nla_policy fq_codel_policy[TCA_FQ_CODEL_MAX + 1] = {
3674b549a2eSEric Dumazet 	[TCA_FQ_CODEL_TARGET]	= { .type = NLA_U32 },
3684b549a2eSEric Dumazet 	[TCA_FQ_CODEL_LIMIT]	= { .type = NLA_U32 },
3694b549a2eSEric Dumazet 	[TCA_FQ_CODEL_INTERVAL]	= { .type = NLA_U32 },
3704b549a2eSEric Dumazet 	[TCA_FQ_CODEL_ECN]	= { .type = NLA_U32 },
3714b549a2eSEric Dumazet 	[TCA_FQ_CODEL_FLOWS]	= { .type = NLA_U32 },
3724b549a2eSEric Dumazet 	[TCA_FQ_CODEL_QUANTUM]	= { .type = NLA_U32 },
37380ba92faSEric Dumazet 	[TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
3749d18562aSEric Dumazet 	[TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
37595b58430SEric Dumazet 	[TCA_FQ_CODEL_MEMORY_LIMIT] = { .type = NLA_U32 },
3764b549a2eSEric Dumazet };
3774b549a2eSEric Dumazet 
3784b549a2eSEric Dumazet static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
3794b549a2eSEric Dumazet {
3804b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
3814b549a2eSEric Dumazet 	struct nlattr *tb[TCA_FQ_CODEL_MAX + 1];
3824b549a2eSEric Dumazet 	int err;
3834b549a2eSEric Dumazet 
3844b549a2eSEric Dumazet 	if (!opt)
3854b549a2eSEric Dumazet 		return -EINVAL;
3864b549a2eSEric Dumazet 
3874b549a2eSEric Dumazet 	err = nla_parse_nested(tb, TCA_FQ_CODEL_MAX, opt, fq_codel_policy);
3884b549a2eSEric Dumazet 	if (err < 0)
3894b549a2eSEric Dumazet 		return err;
3904b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_FLOWS]) {
3914b549a2eSEric Dumazet 		if (q->flows)
3924b549a2eSEric Dumazet 			return -EINVAL;
3934b549a2eSEric Dumazet 		q->flows_cnt = nla_get_u32(tb[TCA_FQ_CODEL_FLOWS]);
3944b549a2eSEric Dumazet 		if (!q->flows_cnt ||
3954b549a2eSEric Dumazet 		    q->flows_cnt > 65536)
3964b549a2eSEric Dumazet 			return -EINVAL;
3974b549a2eSEric Dumazet 	}
3984b549a2eSEric Dumazet 	sch_tree_lock(sch);
3994b549a2eSEric Dumazet 
4004b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_TARGET]) {
4014b549a2eSEric Dumazet 		u64 target = nla_get_u32(tb[TCA_FQ_CODEL_TARGET]);
4024b549a2eSEric Dumazet 
4034b549a2eSEric Dumazet 		q->cparams.target = (target * NSEC_PER_USEC) >> CODEL_SHIFT;
4044b549a2eSEric Dumazet 	}
4054b549a2eSEric Dumazet 
40680ba92faSEric Dumazet 	if (tb[TCA_FQ_CODEL_CE_THRESHOLD]) {
40780ba92faSEric Dumazet 		u64 val = nla_get_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]);
40880ba92faSEric Dumazet 
40980ba92faSEric Dumazet 		q->cparams.ce_threshold = (val * NSEC_PER_USEC) >> CODEL_SHIFT;
41080ba92faSEric Dumazet 	}
41180ba92faSEric Dumazet 
4124b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_INTERVAL]) {
4134b549a2eSEric Dumazet 		u64 interval = nla_get_u32(tb[TCA_FQ_CODEL_INTERVAL]);
4144b549a2eSEric Dumazet 
4154b549a2eSEric Dumazet 		q->cparams.interval = (interval * NSEC_PER_USEC) >> CODEL_SHIFT;
4164b549a2eSEric Dumazet 	}
4174b549a2eSEric Dumazet 
4184b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_LIMIT])
4194b549a2eSEric Dumazet 		sch->limit = nla_get_u32(tb[TCA_FQ_CODEL_LIMIT]);
4204b549a2eSEric Dumazet 
4214b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_ECN])
4224b549a2eSEric Dumazet 		q->cparams.ecn = !!nla_get_u32(tb[TCA_FQ_CODEL_ECN]);
4234b549a2eSEric Dumazet 
4244b549a2eSEric Dumazet 	if (tb[TCA_FQ_CODEL_QUANTUM])
4254b549a2eSEric Dumazet 		q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
4264b549a2eSEric Dumazet 
4279d18562aSEric Dumazet 	if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
4289d18562aSEric Dumazet 		q->drop_batch_size = min(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]));
4299d18562aSEric Dumazet 
43095b58430SEric Dumazet 	if (tb[TCA_FQ_CODEL_MEMORY_LIMIT])
43195b58430SEric Dumazet 		q->memory_limit = min(1U << 31, nla_get_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]));
43295b58430SEric Dumazet 
43395b58430SEric Dumazet 	while (sch->q.qlen > sch->limit ||
43495b58430SEric Dumazet 	       q->memory_usage > q->memory_limit) {
4354b549a2eSEric Dumazet 		struct sk_buff *skb = fq_codel_dequeue(sch);
4364b549a2eSEric Dumazet 
4372ccccf5fSWANG Cong 		q->cstats.drop_len += qdisc_pkt_len(skb);
438ece5d4c7SEric Dumazet 		rtnl_kfree_skbs(skb, skb);
4394b549a2eSEric Dumazet 		q->cstats.drop_count++;
4404b549a2eSEric Dumazet 	}
4412ccccf5fSWANG Cong 	qdisc_tree_reduce_backlog(sch, q->cstats.drop_count, q->cstats.drop_len);
4424b549a2eSEric Dumazet 	q->cstats.drop_count = 0;
4432ccccf5fSWANG Cong 	q->cstats.drop_len = 0;
4444b549a2eSEric Dumazet 
4454b549a2eSEric Dumazet 	sch_tree_unlock(sch);
4464b549a2eSEric Dumazet 	return 0;
4474b549a2eSEric Dumazet }
4484b549a2eSEric Dumazet 
4494b549a2eSEric Dumazet static void *fq_codel_zalloc(size_t sz)
4504b549a2eSEric Dumazet {
4514b549a2eSEric Dumazet 	void *ptr = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN);
4524b549a2eSEric Dumazet 
4534b549a2eSEric Dumazet 	if (!ptr)
4544b549a2eSEric Dumazet 		ptr = vzalloc(sz);
4554b549a2eSEric Dumazet 	return ptr;
4564b549a2eSEric Dumazet }
4574b549a2eSEric Dumazet 
4584b549a2eSEric Dumazet static void fq_codel_free(void *addr)
4594b549a2eSEric Dumazet {
4604cb28970SWANG Cong 	kvfree(addr);
4614b549a2eSEric Dumazet }
4624b549a2eSEric Dumazet 
4634b549a2eSEric Dumazet static void fq_codel_destroy(struct Qdisc *sch)
4644b549a2eSEric Dumazet {
4654b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
4664b549a2eSEric Dumazet 
4674b549a2eSEric Dumazet 	tcf_destroy_chain(&q->filter_list);
4684b549a2eSEric Dumazet 	fq_codel_free(q->backlogs);
4694b549a2eSEric Dumazet 	fq_codel_free(q->flows);
4704b549a2eSEric Dumazet }
4714b549a2eSEric Dumazet 
4724b549a2eSEric Dumazet static int fq_codel_init(struct Qdisc *sch, struct nlattr *opt)
4734b549a2eSEric Dumazet {
4744b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
4754b549a2eSEric Dumazet 	int i;
4764b549a2eSEric Dumazet 
4774b549a2eSEric Dumazet 	sch->limit = 10*1024;
4784b549a2eSEric Dumazet 	q->flows_cnt = 1024;
47995b58430SEric Dumazet 	q->memory_limit = 32 << 20; /* 32 MBytes */
4809d18562aSEric Dumazet 	q->drop_batch_size = 64;
4814b549a2eSEric Dumazet 	q->quantum = psched_mtu(qdisc_dev(sch));
4824b549a2eSEric Dumazet 	INIT_LIST_HEAD(&q->new_flows);
4834b549a2eSEric Dumazet 	INIT_LIST_HEAD(&q->old_flows);
48479bdc4c8SMichal Kazior 	codel_params_init(&q->cparams);
4854b549a2eSEric Dumazet 	codel_stats_init(&q->cstats);
4864b549a2eSEric Dumazet 	q->cparams.ecn = true;
48779bdc4c8SMichal Kazior 	q->cparams.mtu = psched_mtu(qdisc_dev(sch));
4884b549a2eSEric Dumazet 
4894b549a2eSEric Dumazet 	if (opt) {
4904b549a2eSEric Dumazet 		int err = fq_codel_change(sch, opt);
4914b549a2eSEric Dumazet 		if (err)
4924b549a2eSEric Dumazet 			return err;
4934b549a2eSEric Dumazet 	}
4944b549a2eSEric Dumazet 
4954b549a2eSEric Dumazet 	if (!q->flows) {
4964b549a2eSEric Dumazet 		q->flows = fq_codel_zalloc(q->flows_cnt *
4974b549a2eSEric Dumazet 					   sizeof(struct fq_codel_flow));
4984b549a2eSEric Dumazet 		if (!q->flows)
4994b549a2eSEric Dumazet 			return -ENOMEM;
5004b549a2eSEric Dumazet 		q->backlogs = fq_codel_zalloc(q->flows_cnt * sizeof(u32));
5014b549a2eSEric Dumazet 		if (!q->backlogs) {
5024b549a2eSEric Dumazet 			fq_codel_free(q->flows);
5034b549a2eSEric Dumazet 			return -ENOMEM;
5044b549a2eSEric Dumazet 		}
5054b549a2eSEric Dumazet 		for (i = 0; i < q->flows_cnt; i++) {
5064b549a2eSEric Dumazet 			struct fq_codel_flow *flow = q->flows + i;
5074b549a2eSEric Dumazet 
5084b549a2eSEric Dumazet 			INIT_LIST_HEAD(&flow->flowchain);
509b379135cSEric Dumazet 			codel_vars_init(&flow->cvars);
5104b549a2eSEric Dumazet 		}
5114b549a2eSEric Dumazet 	}
5124b549a2eSEric Dumazet 	if (sch->limit >= 1)
5134b549a2eSEric Dumazet 		sch->flags |= TCQ_F_CAN_BYPASS;
5144b549a2eSEric Dumazet 	else
5154b549a2eSEric Dumazet 		sch->flags &= ~TCQ_F_CAN_BYPASS;
5164b549a2eSEric Dumazet 	return 0;
5174b549a2eSEric Dumazet }
5184b549a2eSEric Dumazet 
5194b549a2eSEric Dumazet static int fq_codel_dump(struct Qdisc *sch, struct sk_buff *skb)
5204b549a2eSEric Dumazet {
5214b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
5224b549a2eSEric Dumazet 	struct nlattr *opts;
5234b549a2eSEric Dumazet 
5244b549a2eSEric Dumazet 	opts = nla_nest_start(skb, TCA_OPTIONS);
5254b549a2eSEric Dumazet 	if (opts == NULL)
5264b549a2eSEric Dumazet 		goto nla_put_failure;
5274b549a2eSEric Dumazet 
5284b549a2eSEric Dumazet 	if (nla_put_u32(skb, TCA_FQ_CODEL_TARGET,
5294b549a2eSEric Dumazet 			codel_time_to_us(q->cparams.target)) ||
5304b549a2eSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_LIMIT,
5314b549a2eSEric Dumazet 			sch->limit) ||
5324b549a2eSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_INTERVAL,
5334b549a2eSEric Dumazet 			codel_time_to_us(q->cparams.interval)) ||
5344b549a2eSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_ECN,
5354b549a2eSEric Dumazet 			q->cparams.ecn) ||
5364b549a2eSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
5374b549a2eSEric Dumazet 			q->quantum) ||
5389d18562aSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
5399d18562aSEric Dumazet 			q->drop_batch_size) ||
54095b58430SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
54195b58430SEric Dumazet 			q->memory_limit) ||
5424b549a2eSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
5434b549a2eSEric Dumazet 			q->flows_cnt))
5444b549a2eSEric Dumazet 		goto nla_put_failure;
5454b549a2eSEric Dumazet 
54680ba92faSEric Dumazet 	if (q->cparams.ce_threshold != CODEL_DISABLED_THRESHOLD &&
54780ba92faSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CODEL_CE_THRESHOLD,
54880ba92faSEric Dumazet 			codel_time_to_us(q->cparams.ce_threshold)))
54980ba92faSEric Dumazet 		goto nla_put_failure;
55080ba92faSEric Dumazet 
551d59b7d80SYang Yingliang 	return nla_nest_end(skb, opts);
5524b549a2eSEric Dumazet 
5534b549a2eSEric Dumazet nla_put_failure:
5544b549a2eSEric Dumazet 	return -1;
5554b549a2eSEric Dumazet }
5564b549a2eSEric Dumazet 
5574b549a2eSEric Dumazet static int fq_codel_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
5584b549a2eSEric Dumazet {
5594b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
5604b549a2eSEric Dumazet 	struct tc_fq_codel_xstats st = {
5614b549a2eSEric Dumazet 		.type				= TCA_FQ_CODEL_XSTATS_QDISC,
5624b549a2eSEric Dumazet 	};
5634b549a2eSEric Dumazet 	struct list_head *pos;
5644b549a2eSEric Dumazet 
565669d67bfSSasha Levin 	st.qdisc_stats.maxpacket = q->cstats.maxpacket;
566669d67bfSSasha Levin 	st.qdisc_stats.drop_overlimit = q->drop_overlimit;
567669d67bfSSasha Levin 	st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
568669d67bfSSasha Levin 	st.qdisc_stats.new_flow_count = q->new_flow_count;
56980ba92faSEric Dumazet 	st.qdisc_stats.ce_mark = q->cstats.ce_mark;
57095b58430SEric Dumazet 	st.qdisc_stats.memory_usage  = q->memory_usage;
57195b58430SEric Dumazet 	st.qdisc_stats.drop_overmemory = q->drop_overmemory;
572669d67bfSSasha Levin 
573edb09eb1SEric Dumazet 	sch_tree_lock(sch);
5744b549a2eSEric Dumazet 	list_for_each(pos, &q->new_flows)
5754b549a2eSEric Dumazet 		st.qdisc_stats.new_flows_len++;
5764b549a2eSEric Dumazet 
5774b549a2eSEric Dumazet 	list_for_each(pos, &q->old_flows)
5784b549a2eSEric Dumazet 		st.qdisc_stats.old_flows_len++;
579edb09eb1SEric Dumazet 	sch_tree_unlock(sch);
5804b549a2eSEric Dumazet 
5814b549a2eSEric Dumazet 	return gnet_stats_copy_app(d, &st, sizeof(st));
5824b549a2eSEric Dumazet }
5834b549a2eSEric Dumazet 
5844b549a2eSEric Dumazet static struct Qdisc *fq_codel_leaf(struct Qdisc *sch, unsigned long arg)
5854b549a2eSEric Dumazet {
5864b549a2eSEric Dumazet 	return NULL;
5874b549a2eSEric Dumazet }
5884b549a2eSEric Dumazet 
5894b549a2eSEric Dumazet static unsigned long fq_codel_get(struct Qdisc *sch, u32 classid)
5904b549a2eSEric Dumazet {
5914b549a2eSEric Dumazet 	return 0;
5924b549a2eSEric Dumazet }
5934b549a2eSEric Dumazet 
5944b549a2eSEric Dumazet static unsigned long fq_codel_bind(struct Qdisc *sch, unsigned long parent,
5954b549a2eSEric Dumazet 			      u32 classid)
5964b549a2eSEric Dumazet {
5974b549a2eSEric Dumazet 	/* we cannot bypass queue discipline anymore */
5984b549a2eSEric Dumazet 	sch->flags &= ~TCQ_F_CAN_BYPASS;
5994b549a2eSEric Dumazet 	return 0;
6004b549a2eSEric Dumazet }
6014b549a2eSEric Dumazet 
6024b549a2eSEric Dumazet static void fq_codel_put(struct Qdisc *q, unsigned long cl)
6034b549a2eSEric Dumazet {
6044b549a2eSEric Dumazet }
6054b549a2eSEric Dumazet 
60625d8c0d5SJohn Fastabend static struct tcf_proto __rcu **fq_codel_find_tcf(struct Qdisc *sch,
60725d8c0d5SJohn Fastabend 						  unsigned long cl)
6084b549a2eSEric Dumazet {
6094b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
6104b549a2eSEric Dumazet 
6114b549a2eSEric Dumazet 	if (cl)
6124b549a2eSEric Dumazet 		return NULL;
6134b549a2eSEric Dumazet 	return &q->filter_list;
6144b549a2eSEric Dumazet }
6154b549a2eSEric Dumazet 
6164b549a2eSEric Dumazet static int fq_codel_dump_class(struct Qdisc *sch, unsigned long cl,
6174b549a2eSEric Dumazet 			  struct sk_buff *skb, struct tcmsg *tcm)
6184b549a2eSEric Dumazet {
6194b549a2eSEric Dumazet 	tcm->tcm_handle |= TC_H_MIN(cl);
6204b549a2eSEric Dumazet 	return 0;
6214b549a2eSEric Dumazet }
6224b549a2eSEric Dumazet 
6234b549a2eSEric Dumazet static int fq_codel_dump_class_stats(struct Qdisc *sch, unsigned long cl,
6244b549a2eSEric Dumazet 				     struct gnet_dump *d)
6254b549a2eSEric Dumazet {
6264b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
6274b549a2eSEric Dumazet 	u32 idx = cl - 1;
6284b549a2eSEric Dumazet 	struct gnet_stats_queue qs = { 0 };
6294b549a2eSEric Dumazet 	struct tc_fq_codel_xstats xstats;
6304b549a2eSEric Dumazet 
6314b549a2eSEric Dumazet 	if (idx < q->flows_cnt) {
6324b549a2eSEric Dumazet 		const struct fq_codel_flow *flow = &q->flows[idx];
633edb09eb1SEric Dumazet 		const struct sk_buff *skb;
6344b549a2eSEric Dumazet 
6354b549a2eSEric Dumazet 		memset(&xstats, 0, sizeof(xstats));
6364b549a2eSEric Dumazet 		xstats.type = TCA_FQ_CODEL_XSTATS_CLASS;
6374b549a2eSEric Dumazet 		xstats.class_stats.deficit = flow->deficit;
6384b549a2eSEric Dumazet 		xstats.class_stats.ldelay =
6394b549a2eSEric Dumazet 			codel_time_to_us(flow->cvars.ldelay);
6404b549a2eSEric Dumazet 		xstats.class_stats.count = flow->cvars.count;
6414b549a2eSEric Dumazet 		xstats.class_stats.lastcount = flow->cvars.lastcount;
6424b549a2eSEric Dumazet 		xstats.class_stats.dropping = flow->cvars.dropping;
6434b549a2eSEric Dumazet 		if (flow->cvars.dropping) {
6444b549a2eSEric Dumazet 			codel_tdiff_t delta = flow->cvars.drop_next -
6454b549a2eSEric Dumazet 					      codel_get_time();
6464b549a2eSEric Dumazet 
6474b549a2eSEric Dumazet 			xstats.class_stats.drop_next = (delta >= 0) ?
6484b549a2eSEric Dumazet 				codel_time_to_us(delta) :
6494b549a2eSEric Dumazet 				-codel_time_to_us(-delta);
6504b549a2eSEric Dumazet 		}
651edb09eb1SEric Dumazet 		if (flow->head) {
652edb09eb1SEric Dumazet 			sch_tree_lock(sch);
653edb09eb1SEric Dumazet 			skb = flow->head;
6544b549a2eSEric Dumazet 			while (skb) {
6554b549a2eSEric Dumazet 				qs.qlen++;
6564b549a2eSEric Dumazet 				skb = skb->next;
6574b549a2eSEric Dumazet 			}
658edb09eb1SEric Dumazet 			sch_tree_unlock(sch);
659edb09eb1SEric Dumazet 		}
6604b549a2eSEric Dumazet 		qs.backlog = q->backlogs[idx];
6614b549a2eSEric Dumazet 		qs.drops = flow->dropped;
6624b549a2eSEric Dumazet 	}
663aafddbf0SEric Dumazet 	if (gnet_stats_copy_queue(d, NULL, &qs, qs.qlen) < 0)
6644b549a2eSEric Dumazet 		return -1;
6654b549a2eSEric Dumazet 	if (idx < q->flows_cnt)
6664b549a2eSEric Dumazet 		return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
6674b549a2eSEric Dumazet 	return 0;
6684b549a2eSEric Dumazet }
6694b549a2eSEric Dumazet 
6704b549a2eSEric Dumazet static void fq_codel_walk(struct Qdisc *sch, struct qdisc_walker *arg)
6714b549a2eSEric Dumazet {
6724b549a2eSEric Dumazet 	struct fq_codel_sched_data *q = qdisc_priv(sch);
6734b549a2eSEric Dumazet 	unsigned int i;
6744b549a2eSEric Dumazet 
6754b549a2eSEric Dumazet 	if (arg->stop)
6764b549a2eSEric Dumazet 		return;
6774b549a2eSEric Dumazet 
6784b549a2eSEric Dumazet 	for (i = 0; i < q->flows_cnt; i++) {
6794b549a2eSEric Dumazet 		if (list_empty(&q->flows[i].flowchain) ||
6804b549a2eSEric Dumazet 		    arg->count < arg->skip) {
6814b549a2eSEric Dumazet 			arg->count++;
6824b549a2eSEric Dumazet 			continue;
6834b549a2eSEric Dumazet 		}
6844b549a2eSEric Dumazet 		if (arg->fn(sch, i + 1, arg) < 0) {
6854b549a2eSEric Dumazet 			arg->stop = 1;
6864b549a2eSEric Dumazet 			break;
6874b549a2eSEric Dumazet 		}
6884b549a2eSEric Dumazet 		arg->count++;
6894b549a2eSEric Dumazet 	}
6904b549a2eSEric Dumazet }
6914b549a2eSEric Dumazet 
6924b549a2eSEric Dumazet static const struct Qdisc_class_ops fq_codel_class_ops = {
6934b549a2eSEric Dumazet 	.leaf		=	fq_codel_leaf,
6944b549a2eSEric Dumazet 	.get		=	fq_codel_get,
6954b549a2eSEric Dumazet 	.put		=	fq_codel_put,
6964b549a2eSEric Dumazet 	.tcf_chain	=	fq_codel_find_tcf,
6974b549a2eSEric Dumazet 	.bind_tcf	=	fq_codel_bind,
6984b549a2eSEric Dumazet 	.unbind_tcf	=	fq_codel_put,
6994b549a2eSEric Dumazet 	.dump		=	fq_codel_dump_class,
7004b549a2eSEric Dumazet 	.dump_stats	=	fq_codel_dump_class_stats,
7014b549a2eSEric Dumazet 	.walk		=	fq_codel_walk,
7024b549a2eSEric Dumazet };
7034b549a2eSEric Dumazet 
7044b549a2eSEric Dumazet static struct Qdisc_ops fq_codel_qdisc_ops __read_mostly = {
7054b549a2eSEric Dumazet 	.cl_ops		=	&fq_codel_class_ops,
7064b549a2eSEric Dumazet 	.id		=	"fq_codel",
7074b549a2eSEric Dumazet 	.priv_size	=	sizeof(struct fq_codel_sched_data),
7084b549a2eSEric Dumazet 	.enqueue	=	fq_codel_enqueue,
7094b549a2eSEric Dumazet 	.dequeue	=	fq_codel_dequeue,
7104b549a2eSEric Dumazet 	.peek		=	qdisc_peek_dequeued,
7114b549a2eSEric Dumazet 	.init		=	fq_codel_init,
7124b549a2eSEric Dumazet 	.reset		=	fq_codel_reset,
7134b549a2eSEric Dumazet 	.destroy	=	fq_codel_destroy,
7144b549a2eSEric Dumazet 	.change		=	fq_codel_change,
7154b549a2eSEric Dumazet 	.dump		=	fq_codel_dump,
7164b549a2eSEric Dumazet 	.dump_stats =	fq_codel_dump_stats,
7174b549a2eSEric Dumazet 	.owner		=	THIS_MODULE,
7184b549a2eSEric Dumazet };
7194b549a2eSEric Dumazet 
7204b549a2eSEric Dumazet static int __init fq_codel_module_init(void)
7214b549a2eSEric Dumazet {
7224b549a2eSEric Dumazet 	return register_qdisc(&fq_codel_qdisc_ops);
7234b549a2eSEric Dumazet }
7244b549a2eSEric Dumazet 
7254b549a2eSEric Dumazet static void __exit fq_codel_module_exit(void)
7264b549a2eSEric Dumazet {
7274b549a2eSEric Dumazet 	unregister_qdisc(&fq_codel_qdisc_ops);
7284b549a2eSEric Dumazet }
7294b549a2eSEric Dumazet 
7304b549a2eSEric Dumazet module_init(fq_codel_module_init)
7314b549a2eSEric Dumazet module_exit(fq_codel_module_exit)
7324b549a2eSEric Dumazet MODULE_AUTHOR("Eric Dumazet");
7334b549a2eSEric Dumazet MODULE_LICENSE("GPL");
734