xref: /openbmc/linux/net/sched/sch_fq.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2afe4fd06SEric Dumazet /*
3afe4fd06SEric Dumazet  * net/sched/sch_fq.c Fair Queue Packet Scheduler (per flow pacing)
4afe4fd06SEric Dumazet  *
586b3bfe9SEric Dumazet  *  Copyright (C) 2013-2015 Eric Dumazet <edumazet@google.com>
6afe4fd06SEric Dumazet  *
705e8bb86SSimon Horman  *  Meant to be mostly used for locally generated traffic :
8afe4fd06SEric Dumazet  *  Fast classification depends on skb->sk being set before reaching us.
9afe4fd06SEric Dumazet  *  If not, (router workload), we use rxhash as fallback, with 32 bits wide hash.
10afe4fd06SEric Dumazet  *  All packets belonging to a socket are considered as a 'flow'.
11afe4fd06SEric Dumazet  *
12afe4fd06SEric Dumazet  *  Flows are dynamically allocated and stored in a hash table of RB trees
13afe4fd06SEric Dumazet  *  They are also part of one Round Robin 'queues' (new or old flows)
14afe4fd06SEric Dumazet  *
15afe4fd06SEric Dumazet  *  Burst avoidance (aka pacing) capability :
16afe4fd06SEric Dumazet  *
17afe4fd06SEric Dumazet  *  Transport (eg TCP) can set in sk->sk_pacing_rate a rate, enqueue a
18afe4fd06SEric Dumazet  *  bunch of packets, and this packet scheduler adds delay between
19afe4fd06SEric Dumazet  *  packets to respect rate limitation.
20afe4fd06SEric Dumazet  *
21afe4fd06SEric Dumazet  *  enqueue() :
22afe4fd06SEric Dumazet  *   - lookup one RB tree (out of 1024 or more) to find the flow.
23afe4fd06SEric Dumazet  *     If non existent flow, create it, add it to the tree.
24afe4fd06SEric Dumazet  *     Add skb to the per flow list of skb (fifo).
25afe4fd06SEric Dumazet  *   - Use a special fifo for high prio packets
26afe4fd06SEric Dumazet  *
27afe4fd06SEric Dumazet  *  dequeue() : serves flows in Round Robin
28afe4fd06SEric Dumazet  *  Note : When a flow becomes empty, we do not immediately remove it from
29afe4fd06SEric Dumazet  *  rb trees, for performance reasons (its expected to send additional packets,
30afe4fd06SEric Dumazet  *  or SLAB cache will reuse socket for another flow)
31afe4fd06SEric Dumazet  */
32afe4fd06SEric Dumazet 
33afe4fd06SEric Dumazet #include <linux/module.h>
34afe4fd06SEric Dumazet #include <linux/types.h>
35afe4fd06SEric Dumazet #include <linux/kernel.h>
36afe4fd06SEric Dumazet #include <linux/jiffies.h>
37afe4fd06SEric Dumazet #include <linux/string.h>
38afe4fd06SEric Dumazet #include <linux/in.h>
39afe4fd06SEric Dumazet #include <linux/errno.h>
40afe4fd06SEric Dumazet #include <linux/init.h>
41afe4fd06SEric Dumazet #include <linux/skbuff.h>
42afe4fd06SEric Dumazet #include <linux/slab.h>
43afe4fd06SEric Dumazet #include <linux/rbtree.h>
44afe4fd06SEric Dumazet #include <linux/hash.h>
4508f89b98SEric Dumazet #include <linux/prefetch.h>
46c3bd8549SEric Dumazet #include <linux/vmalloc.h>
47afe4fd06SEric Dumazet #include <net/netlink.h>
48afe4fd06SEric Dumazet #include <net/pkt_sched.h>
49afe4fd06SEric Dumazet #include <net/sock.h>
50afe4fd06SEric Dumazet #include <net/tcp_states.h>
5198781965SEric Dumazet #include <net/tcp.h>
52afe4fd06SEric Dumazet 
53eeb84aa0SEric Dumazet struct fq_skb_cb {
54eeb84aa0SEric Dumazet 	u64	        time_to_send;
55eeb84aa0SEric Dumazet };
56eeb84aa0SEric Dumazet 
fq_skb_cb(struct sk_buff * skb)57eeb84aa0SEric Dumazet static inline struct fq_skb_cb *fq_skb_cb(struct sk_buff *skb)
58eeb84aa0SEric Dumazet {
59eeb84aa0SEric Dumazet 	qdisc_cb_private_validate(skb, sizeof(struct fq_skb_cb));
60eeb84aa0SEric Dumazet 	return (struct fq_skb_cb *)qdisc_skb_cb(skb)->data;
61eeb84aa0SEric Dumazet }
62eeb84aa0SEric Dumazet 
63afe4fd06SEric Dumazet /*
64eeb84aa0SEric Dumazet  * Per flow structure, dynamically allocated.
65eeb84aa0SEric Dumazet  * If packets have monotically increasing time_to_send, they are placed in O(1)
66eeb84aa0SEric Dumazet  * in linear list (head,tail), otherwise are placed in a rbtree (t_root).
67afe4fd06SEric Dumazet  */
68afe4fd06SEric Dumazet struct fq_flow {
697ba0537cSEric Dumazet /* First cache line : used in fq_gc(), fq_enqueue(), fq_dequeue() */
70eeb84aa0SEric Dumazet 	struct rb_root	t_root;
71afe4fd06SEric Dumazet 	struct sk_buff	*head;		/* list of skbs for this flow : first skb */
72afe4fd06SEric Dumazet 	union {
73afe4fd06SEric Dumazet 		struct sk_buff *tail;	/* last skb in the list */
74dde0a648SEric Dumazet 		unsigned long  age;	/* (jiffies | 1UL) when flow was emptied, for gc */
75afe4fd06SEric Dumazet 	};
76afe4fd06SEric Dumazet 	struct rb_node	fq_node;	/* anchor in fq_root[] trees */
77afe4fd06SEric Dumazet 	struct sock	*sk;
78afe4fd06SEric Dumazet 	u32		socket_hash;	/* sk_hash */
797ba0537cSEric Dumazet 	int		qlen;		/* number of packets in flow queue */
807ba0537cSEric Dumazet 
817ba0537cSEric Dumazet /* Second cache line, used in fq_dequeue() */
827ba0537cSEric Dumazet 	int		credit;
837ba0537cSEric Dumazet 	/* 32bit hole on 64bit arches */
847ba0537cSEric Dumazet 
85dde0a648SEric Dumazet 	struct fq_flow *next;		/* next pointer in RR lists */
86afe4fd06SEric Dumazet 
87afe4fd06SEric Dumazet 	struct rb_node  rate_node;	/* anchor in q->delayed tree */
88afe4fd06SEric Dumazet 	u64		time_next_packet;
897ba0537cSEric Dumazet } ____cacheline_aligned_in_smp;
90afe4fd06SEric Dumazet 
91afe4fd06SEric Dumazet struct fq_flow_head {
92afe4fd06SEric Dumazet 	struct fq_flow *first;
93afe4fd06SEric Dumazet 	struct fq_flow *last;
94afe4fd06SEric Dumazet };
95afe4fd06SEric Dumazet 
96afe4fd06SEric Dumazet struct fq_sched_data {
97afe4fd06SEric Dumazet 	struct fq_flow_head new_flows;
98afe4fd06SEric Dumazet 
99afe4fd06SEric Dumazet 	struct fq_flow_head old_flows;
100afe4fd06SEric Dumazet 
101afe4fd06SEric Dumazet 	struct rb_root	delayed;	/* for rate limited flows */
102afe4fd06SEric Dumazet 	u64		time_next_delayed_flow;
10339d01050SEric Dumazet 	u64		ktime_cache;	/* copy of last ktime_get_ns() */
104fefa569aSEric Dumazet 	unsigned long	unthrottle_latency_ns;
105afe4fd06SEric Dumazet 
106afe4fd06SEric Dumazet 	struct fq_flow	internal;	/* for non classified or high prio packets */
107afe4fd06SEric Dumazet 	u32		quantum;
108afe4fd06SEric Dumazet 	u32		initial_quantum;
109f52ed899SEric Dumazet 	u32		flow_refill_delay;
110afe4fd06SEric Dumazet 	u32		flow_plimit;	/* max packets per flow */
11176a9ebe8SEric Dumazet 	unsigned long	flow_max_rate;	/* optional max rate per flow */
11248872c11SEric Dumazet 	u64		ce_threshold;
11339d01050SEric Dumazet 	u64		horizon;	/* horizon in ns */
11406eb395fSEric Dumazet 	u32		orphan_mask;	/* mask for orphaned skb */
11577879147SEric Dumazet 	u32		low_rate_threshold;
116afe4fd06SEric Dumazet 	struct rb_root	*fq_root;
117afe4fd06SEric Dumazet 	u8		rate_enable;
118afe4fd06SEric Dumazet 	u8		fq_trees_log;
11939d01050SEric Dumazet 	u8		horizon_drop;
120afe4fd06SEric Dumazet 	u32		flows;
121afe4fd06SEric Dumazet 	u32		inactive_flows;
122afe4fd06SEric Dumazet 	u32		throttled_flows;
123afe4fd06SEric Dumazet 
124afe4fd06SEric Dumazet 	u64		stat_gc_flows;
125afe4fd06SEric Dumazet 	u64		stat_internal_packets;
126afe4fd06SEric Dumazet 	u64		stat_throttled;
12748872c11SEric Dumazet 	u64		stat_ce_mark;
12839d01050SEric Dumazet 	u64		stat_horizon_drops;
12939d01050SEric Dumazet 	u64		stat_horizon_caps;
130afe4fd06SEric Dumazet 	u64		stat_flows_plimit;
131afe4fd06SEric Dumazet 	u64		stat_pkts_too_long;
132afe4fd06SEric Dumazet 	u64		stat_allocation_errors;
133583396f4SEric Dumazet 
134583396f4SEric Dumazet 	u32		timer_slack; /* hrtimer slack in ns */
135afe4fd06SEric Dumazet 	struct qdisc_watchdog watchdog;
136afe4fd06SEric Dumazet };
137afe4fd06SEric Dumazet 
138dde0a648SEric Dumazet /*
139dde0a648SEric Dumazet  * f->tail and f->age share the same location.
140dde0a648SEric Dumazet  * We can use the low order bit to differentiate if this location points
141dde0a648SEric Dumazet  * to a sk_buff or contains a jiffies value, if we force this value to be odd.
142dde0a648SEric Dumazet  * This assumes f->tail low order bit must be 0 since alignof(struct sk_buff) >= 2
143dde0a648SEric Dumazet  */
fq_flow_set_detached(struct fq_flow * f)144afe4fd06SEric Dumazet static void fq_flow_set_detached(struct fq_flow *f)
145afe4fd06SEric Dumazet {
146dde0a648SEric Dumazet 	f->age = jiffies | 1UL;
147afe4fd06SEric Dumazet }
148afe4fd06SEric Dumazet 
fq_flow_is_detached(const struct fq_flow * f)149afe4fd06SEric Dumazet static bool fq_flow_is_detached(const struct fq_flow *f)
150afe4fd06SEric Dumazet {
151dde0a648SEric Dumazet 	return !!(f->age & 1UL);
152afe4fd06SEric Dumazet }
153afe4fd06SEric Dumazet 
154dde0a648SEric Dumazet /* special value to mark a throttled flow (not on old/new list) */
155dde0a648SEric Dumazet static struct fq_flow throttled;
156dde0a648SEric Dumazet 
fq_flow_is_throttled(const struct fq_flow * f)1577df40c26SEric Dumazet static bool fq_flow_is_throttled(const struct fq_flow *f)
1587df40c26SEric Dumazet {
1597df40c26SEric Dumazet 	return f->next == &throttled;
1607df40c26SEric Dumazet }
1617df40c26SEric Dumazet 
fq_flow_add_tail(struct fq_flow_head * head,struct fq_flow * flow)1627df40c26SEric Dumazet static void fq_flow_add_tail(struct fq_flow_head *head, struct fq_flow *flow)
1637df40c26SEric Dumazet {
1647df40c26SEric Dumazet 	if (head->first)
1657df40c26SEric Dumazet 		head->last->next = flow;
1667df40c26SEric Dumazet 	else
1677df40c26SEric Dumazet 		head->first = flow;
1687df40c26SEric Dumazet 	head->last = flow;
1697df40c26SEric Dumazet 	flow->next = NULL;
1707df40c26SEric Dumazet }
1717df40c26SEric Dumazet 
fq_flow_unset_throttled(struct fq_sched_data * q,struct fq_flow * f)1727df40c26SEric Dumazet static void fq_flow_unset_throttled(struct fq_sched_data *q, struct fq_flow *f)
1737df40c26SEric Dumazet {
1747df40c26SEric Dumazet 	rb_erase(&f->rate_node, &q->delayed);
1757df40c26SEric Dumazet 	q->throttled_flows--;
1767df40c26SEric Dumazet 	fq_flow_add_tail(&q->old_flows, f);
1777df40c26SEric Dumazet }
1787df40c26SEric Dumazet 
fq_flow_set_throttled(struct fq_sched_data * q,struct fq_flow * f)179afe4fd06SEric Dumazet static void fq_flow_set_throttled(struct fq_sched_data *q, struct fq_flow *f)
180afe4fd06SEric Dumazet {
181afe4fd06SEric Dumazet 	struct rb_node **p = &q->delayed.rb_node, *parent = NULL;
182afe4fd06SEric Dumazet 
183afe4fd06SEric Dumazet 	while (*p) {
184afe4fd06SEric Dumazet 		struct fq_flow *aux;
185afe4fd06SEric Dumazet 
186afe4fd06SEric Dumazet 		parent = *p;
187e124557dSGeliang Tang 		aux = rb_entry(parent, struct fq_flow, rate_node);
188afe4fd06SEric Dumazet 		if (f->time_next_packet >= aux->time_next_packet)
189afe4fd06SEric Dumazet 			p = &parent->rb_right;
190afe4fd06SEric Dumazet 		else
191afe4fd06SEric Dumazet 			p = &parent->rb_left;
192afe4fd06SEric Dumazet 	}
193afe4fd06SEric Dumazet 	rb_link_node(&f->rate_node, parent, p);
194afe4fd06SEric Dumazet 	rb_insert_color(&f->rate_node, &q->delayed);
195afe4fd06SEric Dumazet 	q->throttled_flows++;
196afe4fd06SEric Dumazet 	q->stat_throttled++;
197afe4fd06SEric Dumazet 
198afe4fd06SEric Dumazet 	f->next = &throttled;
199afe4fd06SEric Dumazet 	if (q->time_next_delayed_flow > f->time_next_packet)
200afe4fd06SEric Dumazet 		q->time_next_delayed_flow = f->time_next_packet;
201afe4fd06SEric Dumazet }
202afe4fd06SEric Dumazet 
203afe4fd06SEric Dumazet 
204afe4fd06SEric Dumazet static struct kmem_cache *fq_flow_cachep __read_mostly;
205afe4fd06SEric Dumazet 
206afe4fd06SEric Dumazet 
207afe4fd06SEric Dumazet /* limit number of collected flows per round */
208afe4fd06SEric Dumazet #define FQ_GC_MAX 8
209afe4fd06SEric Dumazet #define FQ_GC_AGE (3*HZ)
210afe4fd06SEric Dumazet 
fq_gc_candidate(const struct fq_flow * f)211afe4fd06SEric Dumazet static bool fq_gc_candidate(const struct fq_flow *f)
212afe4fd06SEric Dumazet {
213afe4fd06SEric Dumazet 	return fq_flow_is_detached(f) &&
214afe4fd06SEric Dumazet 	       time_after(jiffies, f->age + FQ_GC_AGE);
215afe4fd06SEric Dumazet }
216afe4fd06SEric Dumazet 
fq_gc(struct fq_sched_data * q,struct rb_root * root,struct sock * sk)217afe4fd06SEric Dumazet static void fq_gc(struct fq_sched_data *q,
218afe4fd06SEric Dumazet 		  struct rb_root *root,
219afe4fd06SEric Dumazet 		  struct sock *sk)
220afe4fd06SEric Dumazet {
221afe4fd06SEric Dumazet 	struct rb_node **p, *parent;
22282a0aa53SEric Dumazet 	void *tofree[FQ_GC_MAX];
22382a0aa53SEric Dumazet 	struct fq_flow *f;
22482a0aa53SEric Dumazet 	int i, fcnt = 0;
225afe4fd06SEric Dumazet 
226afe4fd06SEric Dumazet 	p = &root->rb_node;
227afe4fd06SEric Dumazet 	parent = NULL;
228afe4fd06SEric Dumazet 	while (*p) {
229afe4fd06SEric Dumazet 		parent = *p;
230afe4fd06SEric Dumazet 
231e124557dSGeliang Tang 		f = rb_entry(parent, struct fq_flow, fq_node);
232afe4fd06SEric Dumazet 		if (f->sk == sk)
233afe4fd06SEric Dumazet 			break;
234afe4fd06SEric Dumazet 
235afe4fd06SEric Dumazet 		if (fq_gc_candidate(f)) {
236afe4fd06SEric Dumazet 			tofree[fcnt++] = f;
237afe4fd06SEric Dumazet 			if (fcnt == FQ_GC_MAX)
238afe4fd06SEric Dumazet 				break;
239afe4fd06SEric Dumazet 		}
240afe4fd06SEric Dumazet 
241afe4fd06SEric Dumazet 		if (f->sk > sk)
242afe4fd06SEric Dumazet 			p = &parent->rb_right;
243afe4fd06SEric Dumazet 		else
244afe4fd06SEric Dumazet 			p = &parent->rb_left;
245afe4fd06SEric Dumazet 	}
246afe4fd06SEric Dumazet 
24782a0aa53SEric Dumazet 	if (!fcnt)
24882a0aa53SEric Dumazet 		return;
24982a0aa53SEric Dumazet 
25082a0aa53SEric Dumazet 	for (i = fcnt; i > 0; ) {
25182a0aa53SEric Dumazet 		f = tofree[--i];
25282a0aa53SEric Dumazet 		rb_erase(&f->fq_node, root);
25382a0aa53SEric Dumazet 	}
254afe4fd06SEric Dumazet 	q->flows -= fcnt;
255afe4fd06SEric Dumazet 	q->inactive_flows -= fcnt;
256afe4fd06SEric Dumazet 	q->stat_gc_flows += fcnt;
257afe4fd06SEric Dumazet 
25882a0aa53SEric Dumazet 	kmem_cache_free_bulk(fq_flow_cachep, fcnt, tofree);
259afe4fd06SEric Dumazet }
260afe4fd06SEric Dumazet 
fq_classify(struct sk_buff * skb,struct fq_sched_data * q)261afe4fd06SEric Dumazet static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
262afe4fd06SEric Dumazet {
263afe4fd06SEric Dumazet 	struct rb_node **p, *parent;
264afe4fd06SEric Dumazet 	struct sock *sk = skb->sk;
265afe4fd06SEric Dumazet 	struct rb_root *root;
266afe4fd06SEric Dumazet 	struct fq_flow *f;
267afe4fd06SEric Dumazet 
268afe4fd06SEric Dumazet 	/* warning: no starvation prevention... */
2692abc2f07SMaciej Żenczykowski 	if (unlikely((skb->priority & TC_PRIO_MAX) == TC_PRIO_CONTROL))
270afe4fd06SEric Dumazet 		return &q->internal;
271afe4fd06SEric Dumazet 
272ca6fb065SEric Dumazet 	/* SYNACK messages are attached to a TCP_NEW_SYN_RECV request socket
273e446f9dfSEric Dumazet 	 * or a listener (SYNCOOKIE mode)
274ca6fb065SEric Dumazet 	 * 1) request sockets are not full blown,
275ca6fb065SEric Dumazet 	 *    they do not contain sk_pacing_rate
276ca6fb065SEric Dumazet 	 * 2) They are not part of a 'flow' yet
277ca6fb065SEric Dumazet 	 * 3) We do not want to rate limit them (eg SYNFLOOD attack),
27806eb395fSEric Dumazet 	 *    especially if the listener set SO_MAX_PACING_RATE
279ca6fb065SEric Dumazet 	 * 4) We pretend they are orphaned
28006eb395fSEric Dumazet 	 */
281e446f9dfSEric Dumazet 	if (!sk || sk_listener(sk)) {
28206eb395fSEric Dumazet 		unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
28306eb395fSEric Dumazet 
284afe4fd06SEric Dumazet 		/* By forcing low order bit to 1, we make sure to not
285afe4fd06SEric Dumazet 		 * collide with a local flow (socket pointers are word aligned)
286afe4fd06SEric Dumazet 		 */
28706eb395fSEric Dumazet 		sk = (struct sock *)((hash << 1) | 1UL);
28806eb395fSEric Dumazet 		skb_orphan(skb);
28937c0aeadSEric Dumazet 	} else if (sk->sk_state == TCP_CLOSE) {
29037c0aeadSEric Dumazet 		unsigned long hash = skb_get_hash(skb) & q->orphan_mask;
29137c0aeadSEric Dumazet 		/*
29237c0aeadSEric Dumazet 		 * Sockets in TCP_CLOSE are non connected.
29337c0aeadSEric Dumazet 		 * Typical use case is UDP sockets, they can send packets
29437c0aeadSEric Dumazet 		 * with sendto() to many different destinations.
29537c0aeadSEric Dumazet 		 * We probably could use a generic bit advertising
29637c0aeadSEric Dumazet 		 * non connected sockets, instead of sk_state == TCP_CLOSE,
29737c0aeadSEric Dumazet 		 * if we care enough.
29837c0aeadSEric Dumazet 		 */
29937c0aeadSEric Dumazet 		sk = (struct sock *)((hash << 1) | 1UL);
300afe4fd06SEric Dumazet 	}
301afe4fd06SEric Dumazet 
30229c58472SEric Dumazet 	root = &q->fq_root[hash_ptr(sk, q->fq_trees_log)];
303afe4fd06SEric Dumazet 
304afe4fd06SEric Dumazet 	if (q->flows >= (2U << q->fq_trees_log) &&
305afe4fd06SEric Dumazet 	    q->inactive_flows > q->flows/2)
306afe4fd06SEric Dumazet 		fq_gc(q, root, sk);
307afe4fd06SEric Dumazet 
308afe4fd06SEric Dumazet 	p = &root->rb_node;
309afe4fd06SEric Dumazet 	parent = NULL;
310afe4fd06SEric Dumazet 	while (*p) {
311afe4fd06SEric Dumazet 		parent = *p;
312afe4fd06SEric Dumazet 
313e124557dSGeliang Tang 		f = rb_entry(parent, struct fq_flow, fq_node);
314afe4fd06SEric Dumazet 		if (f->sk == sk) {
315afe4fd06SEric Dumazet 			/* socket might have been reallocated, so check
316afe4fd06SEric Dumazet 			 * if its sk_hash is the same.
317afe4fd06SEric Dumazet 			 * It not, we need to refill credit with
318afe4fd06SEric Dumazet 			 * initial quantum
319afe4fd06SEric Dumazet 			 */
32037c0aeadSEric Dumazet 			if (unlikely(skb->sk == sk &&
321afe4fd06SEric Dumazet 				     f->socket_hash != sk->sk_hash)) {
322afe4fd06SEric Dumazet 				f->credit = q->initial_quantum;
323afe4fd06SEric Dumazet 				f->socket_hash = sk->sk_hash;
324bb3d0b8bSEric Dumazet 				if (q->rate_enable)
325bb3d0b8bSEric Dumazet 					smp_store_release(&sk->sk_pacing_status,
326bb3d0b8bSEric Dumazet 							  SK_PACING_FQ);
3277df40c26SEric Dumazet 				if (fq_flow_is_throttled(f))
3287df40c26SEric Dumazet 					fq_flow_unset_throttled(q, f);
329fc59d5bdSEric Dumazet 				f->time_next_packet = 0ULL;
330afe4fd06SEric Dumazet 			}
331afe4fd06SEric Dumazet 			return f;
332afe4fd06SEric Dumazet 		}
333afe4fd06SEric Dumazet 		if (f->sk > sk)
334afe4fd06SEric Dumazet 			p = &parent->rb_right;
335afe4fd06SEric Dumazet 		else
336afe4fd06SEric Dumazet 			p = &parent->rb_left;
337afe4fd06SEric Dumazet 	}
338afe4fd06SEric Dumazet 
339afe4fd06SEric Dumazet 	f = kmem_cache_zalloc(fq_flow_cachep, GFP_ATOMIC | __GFP_NOWARN);
340afe4fd06SEric Dumazet 	if (unlikely(!f)) {
341afe4fd06SEric Dumazet 		q->stat_allocation_errors++;
342afe4fd06SEric Dumazet 		return &q->internal;
343afe4fd06SEric Dumazet 	}
344eeb84aa0SEric Dumazet 	/* f->t_root is already zeroed after kmem_cache_zalloc() */
345eeb84aa0SEric Dumazet 
346afe4fd06SEric Dumazet 	fq_flow_set_detached(f);
347afe4fd06SEric Dumazet 	f->sk = sk;
348bb3d0b8bSEric Dumazet 	if (skb->sk == sk) {
349afe4fd06SEric Dumazet 		f->socket_hash = sk->sk_hash;
350bb3d0b8bSEric Dumazet 		if (q->rate_enable)
351bb3d0b8bSEric Dumazet 			smp_store_release(&sk->sk_pacing_status,
352bb3d0b8bSEric Dumazet 					  SK_PACING_FQ);
353bb3d0b8bSEric Dumazet 	}
354afe4fd06SEric Dumazet 	f->credit = q->initial_quantum;
355afe4fd06SEric Dumazet 
356afe4fd06SEric Dumazet 	rb_link_node(&f->fq_node, parent, p);
357afe4fd06SEric Dumazet 	rb_insert_color(&f->fq_node, root);
358afe4fd06SEric Dumazet 
359afe4fd06SEric Dumazet 	q->flows++;
360afe4fd06SEric Dumazet 	q->inactive_flows++;
361afe4fd06SEric Dumazet 	return f;
362afe4fd06SEric Dumazet }
363afe4fd06SEric Dumazet 
fq_peek(struct fq_flow * flow)364eeb84aa0SEric Dumazet static struct sk_buff *fq_peek(struct fq_flow *flow)
365eeb84aa0SEric Dumazet {
366eeb84aa0SEric Dumazet 	struct sk_buff *skb = skb_rb_first(&flow->t_root);
367eeb84aa0SEric Dumazet 	struct sk_buff *head = flow->head;
368eeb84aa0SEric Dumazet 
369eeb84aa0SEric Dumazet 	if (!skb)
370eeb84aa0SEric Dumazet 		return head;
371eeb84aa0SEric Dumazet 
372eeb84aa0SEric Dumazet 	if (!head)
373eeb84aa0SEric Dumazet 		return skb;
374eeb84aa0SEric Dumazet 
375eeb84aa0SEric Dumazet 	if (fq_skb_cb(skb)->time_to_send < fq_skb_cb(head)->time_to_send)
376eeb84aa0SEric Dumazet 		return skb;
377eeb84aa0SEric Dumazet 	return head;
378eeb84aa0SEric Dumazet }
379eeb84aa0SEric Dumazet 
fq_erase_head(struct Qdisc * sch,struct fq_flow * flow,struct sk_buff * skb)380eeb84aa0SEric Dumazet static void fq_erase_head(struct Qdisc *sch, struct fq_flow *flow,
381eeb84aa0SEric Dumazet 			  struct sk_buff *skb)
382eeb84aa0SEric Dumazet {
383eeb84aa0SEric Dumazet 	if (skb == flow->head) {
384eeb84aa0SEric Dumazet 		flow->head = skb->next;
385eeb84aa0SEric Dumazet 	} else {
386eeb84aa0SEric Dumazet 		rb_erase(&skb->rbnode, &flow->t_root);
387eeb84aa0SEric Dumazet 		skb->dev = qdisc_dev(sch);
388eeb84aa0SEric Dumazet 	}
389eeb84aa0SEric Dumazet }
390afe4fd06SEric Dumazet 
391c288b0caSEric Dumazet /* Remove one skb from flow queue.
392c288b0caSEric Dumazet  * This skb must be the return value of prior fq_peek().
393c288b0caSEric Dumazet  */
fq_dequeue_skb(struct Qdisc * sch,struct fq_flow * flow,struct sk_buff * skb)394c288b0caSEric Dumazet static void fq_dequeue_skb(struct Qdisc *sch, struct fq_flow *flow,
395c288b0caSEric Dumazet 			   struct sk_buff *skb)
396afe4fd06SEric Dumazet {
397eeb84aa0SEric Dumazet 	fq_erase_head(sch, flow, skb);
398a8305bffSDavid S. Miller 	skb_mark_not_on_list(skb);
399afe4fd06SEric Dumazet 	flow->qlen--;
40025331d6cSJohn Fastabend 	qdisc_qstats_backlog_dec(sch, skb);
4018d34ce10SEric Dumazet 	sch->q.qlen--;
402afe4fd06SEric Dumazet }
403afe4fd06SEric Dumazet 
flow_queue_add(struct fq_flow * flow,struct sk_buff * skb)404afe4fd06SEric Dumazet static void flow_queue_add(struct fq_flow *flow, struct sk_buff *skb)
405afe4fd06SEric Dumazet {
406eeb84aa0SEric Dumazet 	struct rb_node **p, *parent;
407eeb84aa0SEric Dumazet 	struct sk_buff *head, *aux;
408afe4fd06SEric Dumazet 
409eeb84aa0SEric Dumazet 	head = flow->head;
410eeb84aa0SEric Dumazet 	if (!head ||
411eeb84aa0SEric Dumazet 	    fq_skb_cb(skb)->time_to_send >= fq_skb_cb(flow->tail)->time_to_send) {
412afe4fd06SEric Dumazet 		if (!head)
413afe4fd06SEric Dumazet 			flow->head = skb;
414afe4fd06SEric Dumazet 		else
41590caf67bSEric Dumazet 			flow->tail->next = skb;
41690caf67bSEric Dumazet 		flow->tail = skb;
417eeb84aa0SEric Dumazet 		skb->next = NULL;
418eeb84aa0SEric Dumazet 		return;
419eeb84aa0SEric Dumazet 	}
420eeb84aa0SEric Dumazet 
421eeb84aa0SEric Dumazet 	p = &flow->t_root.rb_node;
422eeb84aa0SEric Dumazet 	parent = NULL;
423eeb84aa0SEric Dumazet 
424eeb84aa0SEric Dumazet 	while (*p) {
425eeb84aa0SEric Dumazet 		parent = *p;
426eeb84aa0SEric Dumazet 		aux = rb_to_skb(parent);
427eeb84aa0SEric Dumazet 		if (fq_skb_cb(skb)->time_to_send >= fq_skb_cb(aux)->time_to_send)
428eeb84aa0SEric Dumazet 			p = &parent->rb_right;
429eeb84aa0SEric Dumazet 		else
430eeb84aa0SEric Dumazet 			p = &parent->rb_left;
431eeb84aa0SEric Dumazet 	}
432eeb84aa0SEric Dumazet 	rb_link_node(&skb->rbnode, parent, p);
433eeb84aa0SEric Dumazet 	rb_insert_color(&skb->rbnode, &flow->t_root);
434afe4fd06SEric Dumazet }
435afe4fd06SEric Dumazet 
fq_packet_beyond_horizon(const struct sk_buff * skb,const struct fq_sched_data * q)43639d01050SEric Dumazet static bool fq_packet_beyond_horizon(const struct sk_buff *skb,
43739d01050SEric Dumazet 				    const struct fq_sched_data *q)
43839d01050SEric Dumazet {
43939d01050SEric Dumazet 	return unlikely((s64)skb->tstamp > (s64)(q->ktime_cache + q->horizon));
44039d01050SEric Dumazet }
44139d01050SEric Dumazet 
fq_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)442520ac30fSEric Dumazet static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
443520ac30fSEric Dumazet 		      struct sk_buff **to_free)
444afe4fd06SEric Dumazet {
445afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
446afe4fd06SEric Dumazet 	struct fq_flow *f;
447afe4fd06SEric Dumazet 
448afe4fd06SEric Dumazet 	if (unlikely(sch->q.qlen >= sch->limit))
449520ac30fSEric Dumazet 		return qdisc_drop(skb, sch, to_free);
450afe4fd06SEric Dumazet 
45139d01050SEric Dumazet 	if (!skb->tstamp) {
45239d01050SEric Dumazet 		fq_skb_cb(skb)->time_to_send = q->ktime_cache = ktime_get_ns();
45339d01050SEric Dumazet 	} else {
45439d01050SEric Dumazet 		/* Check if packet timestamp is too far in the future.
45539d01050SEric Dumazet 		 * Try first if our cached value, to avoid ktime_get_ns()
45639d01050SEric Dumazet 		 * cost in most cases.
45739d01050SEric Dumazet 		 */
45839d01050SEric Dumazet 		if (fq_packet_beyond_horizon(skb, q)) {
45939d01050SEric Dumazet 			/* Refresh our cache and check another time */
46039d01050SEric Dumazet 			q->ktime_cache = ktime_get_ns();
46139d01050SEric Dumazet 			if (fq_packet_beyond_horizon(skb, q)) {
46239d01050SEric Dumazet 				if (q->horizon_drop) {
46339d01050SEric Dumazet 					q->stat_horizon_drops++;
46439d01050SEric Dumazet 					return qdisc_drop(skb, sch, to_free);
46539d01050SEric Dumazet 				}
46639d01050SEric Dumazet 				q->stat_horizon_caps++;
46739d01050SEric Dumazet 				skb->tstamp = q->ktime_cache + q->horizon;
46839d01050SEric Dumazet 			}
46939d01050SEric Dumazet 		}
47039d01050SEric Dumazet 		fq_skb_cb(skb)->time_to_send = skb->tstamp;
47139d01050SEric Dumazet 	}
47239d01050SEric Dumazet 
473afe4fd06SEric Dumazet 	f = fq_classify(skb, q);
474afe4fd06SEric Dumazet 	if (unlikely(f->qlen >= q->flow_plimit && f != &q->internal)) {
475afe4fd06SEric Dumazet 		q->stat_flows_plimit++;
476520ac30fSEric Dumazet 		return qdisc_drop(skb, sch, to_free);
477afe4fd06SEric Dumazet 	}
478afe4fd06SEric Dumazet 
479afe4fd06SEric Dumazet 	f->qlen++;
48025331d6cSJohn Fastabend 	qdisc_qstats_backlog_inc(sch, skb);
481afe4fd06SEric Dumazet 	if (fq_flow_is_detached(f)) {
482afe4fd06SEric Dumazet 		fq_flow_add_tail(&q->new_flows, f);
483f52ed899SEric Dumazet 		if (time_after(jiffies, f->age + q->flow_refill_delay))
484f52ed899SEric Dumazet 			f->credit = max_t(u32, f->credit, q->quantum);
485afe4fd06SEric Dumazet 		q->inactive_flows--;
486afe4fd06SEric Dumazet 	}
487f52ed899SEric Dumazet 
488f52ed899SEric Dumazet 	/* Note: this overwrites f->age */
489f52ed899SEric Dumazet 	flow_queue_add(f, skb);
490f52ed899SEric Dumazet 
491afe4fd06SEric Dumazet 	if (unlikely(f == &q->internal)) {
492afe4fd06SEric Dumazet 		q->stat_internal_packets++;
493afe4fd06SEric Dumazet 	}
494afe4fd06SEric Dumazet 	sch->q.qlen++;
495afe4fd06SEric Dumazet 
496afe4fd06SEric Dumazet 	return NET_XMIT_SUCCESS;
497afe4fd06SEric Dumazet }
498afe4fd06SEric Dumazet 
fq_check_throttled(struct fq_sched_data * q,u64 now)499afe4fd06SEric Dumazet static void fq_check_throttled(struct fq_sched_data *q, u64 now)
500afe4fd06SEric Dumazet {
501fefa569aSEric Dumazet 	unsigned long sample;
502afe4fd06SEric Dumazet 	struct rb_node *p;
503afe4fd06SEric Dumazet 
504afe4fd06SEric Dumazet 	if (q->time_next_delayed_flow > now)
505afe4fd06SEric Dumazet 		return;
506afe4fd06SEric Dumazet 
507fefa569aSEric Dumazet 	/* Update unthrottle latency EWMA.
508fefa569aSEric Dumazet 	 * This is cheap and can help diagnosing timer/latency problems.
509fefa569aSEric Dumazet 	 */
510fefa569aSEric Dumazet 	sample = (unsigned long)(now - q->time_next_delayed_flow);
511fefa569aSEric Dumazet 	q->unthrottle_latency_ns -= q->unthrottle_latency_ns >> 3;
512fefa569aSEric Dumazet 	q->unthrottle_latency_ns += sample >> 3;
513fefa569aSEric Dumazet 
514afe4fd06SEric Dumazet 	q->time_next_delayed_flow = ~0ULL;
515afe4fd06SEric Dumazet 	while ((p = rb_first(&q->delayed)) != NULL) {
516e124557dSGeliang Tang 		struct fq_flow *f = rb_entry(p, struct fq_flow, rate_node);
517afe4fd06SEric Dumazet 
518afe4fd06SEric Dumazet 		if (f->time_next_packet > now) {
519afe4fd06SEric Dumazet 			q->time_next_delayed_flow = f->time_next_packet;
520afe4fd06SEric Dumazet 			break;
521afe4fd06SEric Dumazet 		}
5227df40c26SEric Dumazet 		fq_flow_unset_throttled(q, f);
523afe4fd06SEric Dumazet 	}
524afe4fd06SEric Dumazet }
525afe4fd06SEric Dumazet 
fq_dequeue(struct Qdisc * sch)526afe4fd06SEric Dumazet static struct sk_buff *fq_dequeue(struct Qdisc *sch)
527afe4fd06SEric Dumazet {
528afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
529afe4fd06SEric Dumazet 	struct fq_flow_head *head;
530afe4fd06SEric Dumazet 	struct sk_buff *skb;
531afe4fd06SEric Dumazet 	struct fq_flow *f;
53276a9ebe8SEric Dumazet 	unsigned long rate;
53376a9ebe8SEric Dumazet 	u32 plen;
5346b015a52SEric Dumazet 	u64 now;
5356b015a52SEric Dumazet 
5366b015a52SEric Dumazet 	if (!sch->q.qlen)
5376b015a52SEric Dumazet 		return NULL;
538afe4fd06SEric Dumazet 
539c288b0caSEric Dumazet 	skb = fq_peek(&q->internal);
540c288b0caSEric Dumazet 	if (unlikely(skb)) {
541c288b0caSEric Dumazet 		fq_dequeue_skb(sch, &q->internal, skb);
542afe4fd06SEric Dumazet 		goto out;
543c288b0caSEric Dumazet 	}
5446b015a52SEric Dumazet 
54539d01050SEric Dumazet 	q->ktime_cache = now = ktime_get_ns();
546afe4fd06SEric Dumazet 	fq_check_throttled(q, now);
547afe4fd06SEric Dumazet begin:
548afe4fd06SEric Dumazet 	head = &q->new_flows;
549afe4fd06SEric Dumazet 	if (!head->first) {
550afe4fd06SEric Dumazet 		head = &q->old_flows;
551afe4fd06SEric Dumazet 		if (!head->first) {
552afe4fd06SEric Dumazet 			if (q->time_next_delayed_flow != ~0ULL)
553583396f4SEric Dumazet 				qdisc_watchdog_schedule_range_ns(&q->watchdog,
554583396f4SEric Dumazet 							q->time_next_delayed_flow,
555583396f4SEric Dumazet 							q->timer_slack);
556afe4fd06SEric Dumazet 			return NULL;
557afe4fd06SEric Dumazet 		}
558afe4fd06SEric Dumazet 	}
559afe4fd06SEric Dumazet 	f = head->first;
560afe4fd06SEric Dumazet 
561afe4fd06SEric Dumazet 	if (f->credit <= 0) {
562afe4fd06SEric Dumazet 		f->credit += q->quantum;
563afe4fd06SEric Dumazet 		head->first = f->next;
564afe4fd06SEric Dumazet 		fq_flow_add_tail(&q->old_flows, f);
565afe4fd06SEric Dumazet 		goto begin;
566afe4fd06SEric Dumazet 	}
567afe4fd06SEric Dumazet 
568eeb84aa0SEric Dumazet 	skb = fq_peek(f);
5697baf33bdSEric Dumazet 	if (skb) {
570eeb84aa0SEric Dumazet 		u64 time_next_packet = max_t(u64, fq_skb_cb(skb)->time_to_send,
571ab408b6dSEric Dumazet 					     f->time_next_packet);
572ab408b6dSEric Dumazet 
573ab408b6dSEric Dumazet 		if (now < time_next_packet) {
574afe4fd06SEric Dumazet 			head->first = f->next;
575ab408b6dSEric Dumazet 			f->time_next_packet = time_next_packet;
576afe4fd06SEric Dumazet 			fq_flow_set_throttled(q, f);
577afe4fd06SEric Dumazet 			goto begin;
578afe4fd06SEric Dumazet 		}
579348e289bSEric Dumazet 		prefetch(&skb->end);
580e9c43addSEric Dumazet 		if ((s64)(now - time_next_packet - q->ce_threshold) > 0) {
58148872c11SEric Dumazet 			INET_ECN_set_ce(skb);
58248872c11SEric Dumazet 			q->stat_ce_mark++;
58348872c11SEric Dumazet 		}
584c288b0caSEric Dumazet 		fq_dequeue_skb(sch, f, skb);
585c288b0caSEric Dumazet 	} else {
586afe4fd06SEric Dumazet 		head->first = f->next;
587afe4fd06SEric Dumazet 		/* force a pass through old_flows to prevent starvation */
588afe4fd06SEric Dumazet 		if ((head == &q->new_flows) && q->old_flows.first) {
589afe4fd06SEric Dumazet 			fq_flow_add_tail(&q->old_flows, f);
590afe4fd06SEric Dumazet 		} else {
591afe4fd06SEric Dumazet 			fq_flow_set_detached(f);
592afe4fd06SEric Dumazet 			q->inactive_flows++;
593afe4fd06SEric Dumazet 		}
594afe4fd06SEric Dumazet 		goto begin;
595afe4fd06SEric Dumazet 	}
59608e14fe4SEric Dumazet 	plen = qdisc_pkt_len(skb);
59708e14fe4SEric Dumazet 	f->credit -= plen;
598afe4fd06SEric Dumazet 
59908e14fe4SEric Dumazet 	if (!q->rate_enable)
60098781965SEric Dumazet 		goto out;
60198781965SEric Dumazet 
6020eab5eb7SEric Dumazet 	rate = q->flow_max_rate;
60308e14fe4SEric Dumazet 
60408e14fe4SEric Dumazet 	/* If EDT time was provided for this skb, we need to
60508e14fe4SEric Dumazet 	 * update f->time_next_packet only if this qdisc enforces
60608e14fe4SEric Dumazet 	 * a flow max rate.
60708e14fe4SEric Dumazet 	 */
60808e14fe4SEric Dumazet 	if (!skb->tstamp) {
60986b3bfe9SEric Dumazet 		if (skb->sk)
6107eec4174SEric Dumazet 			rate = min(skb->sk->sk_pacing_rate, rate);
6117eec4174SEric Dumazet 
61277879147SEric Dumazet 		if (rate <= q->low_rate_threshold) {
61377879147SEric Dumazet 			f->credit = 0;
61477879147SEric Dumazet 		} else {
61508e14fe4SEric Dumazet 			plen = max(plen, q->quantum);
61677879147SEric Dumazet 			if (f->credit > 0)
61777879147SEric Dumazet 				goto out;
61877879147SEric Dumazet 		}
61908e14fe4SEric Dumazet 	}
62076a9ebe8SEric Dumazet 	if (rate != ~0UL) {
6210eab5eb7SEric Dumazet 		u64 len = (u64)plen * NSEC_PER_SEC;
622afe4fd06SEric Dumazet 
6237eec4174SEric Dumazet 		if (likely(rate))
62476a9ebe8SEric Dumazet 			len = div64_ul(len, rate);
625afe4fd06SEric Dumazet 		/* Since socket rate can change later,
626ced7a04eSEric Dumazet 		 * clamp the delay to 1 second.
627ced7a04eSEric Dumazet 		 * Really, providers of too big packets should be fixed !
628afe4fd06SEric Dumazet 		 */
629ced7a04eSEric Dumazet 		if (unlikely(len > NSEC_PER_SEC)) {
630ced7a04eSEric Dumazet 			len = NSEC_PER_SEC;
631afe4fd06SEric Dumazet 			q->stat_pkts_too_long++;
632afe4fd06SEric Dumazet 		}
633fefa569aSEric Dumazet 		/* Account for schedule/timers drifts.
634fefa569aSEric Dumazet 		 * f->time_next_packet was set when prior packet was sent,
635fefa569aSEric Dumazet 		 * and current time (@now) can be too late by tens of us.
636fefa569aSEric Dumazet 		 */
637fefa569aSEric Dumazet 		if (f->time_next_packet)
638fefa569aSEric Dumazet 			len -= min(len/2, now - f->time_next_packet);
639afe4fd06SEric Dumazet 		f->time_next_packet = now + len;
640afe4fd06SEric Dumazet 	}
641afe4fd06SEric Dumazet out:
642afe4fd06SEric Dumazet 	qdisc_bstats_update(sch, skb);
643afe4fd06SEric Dumazet 	return skb;
644afe4fd06SEric Dumazet }
645afe4fd06SEric Dumazet 
fq_flow_purge(struct fq_flow * flow)646e14ffdfdSEric Dumazet static void fq_flow_purge(struct fq_flow *flow)
647e14ffdfdSEric Dumazet {
648eeb84aa0SEric Dumazet 	struct rb_node *p = rb_first(&flow->t_root);
649eeb84aa0SEric Dumazet 
650eeb84aa0SEric Dumazet 	while (p) {
651eeb84aa0SEric Dumazet 		struct sk_buff *skb = rb_to_skb(p);
652eeb84aa0SEric Dumazet 
653eeb84aa0SEric Dumazet 		p = rb_next(p);
654eeb84aa0SEric Dumazet 		rb_erase(&skb->rbnode, &flow->t_root);
655eeb84aa0SEric Dumazet 		rtnl_kfree_skbs(skb, skb);
656eeb84aa0SEric Dumazet 	}
657e14ffdfdSEric Dumazet 	rtnl_kfree_skbs(flow->head, flow->tail);
658e14ffdfdSEric Dumazet 	flow->head = NULL;
659e14ffdfdSEric Dumazet 	flow->qlen = 0;
660e14ffdfdSEric Dumazet }
661e14ffdfdSEric Dumazet 
fq_reset(struct Qdisc * sch)662afe4fd06SEric Dumazet static void fq_reset(struct Qdisc *sch)
663afe4fd06SEric Dumazet {
6648d34ce10SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
6658d34ce10SEric Dumazet 	struct rb_root *root;
6668d34ce10SEric Dumazet 	struct rb_node *p;
6678d34ce10SEric Dumazet 	struct fq_flow *f;
6688d34ce10SEric Dumazet 	unsigned int idx;
669afe4fd06SEric Dumazet 
670e14ffdfdSEric Dumazet 	sch->q.qlen = 0;
671e14ffdfdSEric Dumazet 	sch->qstats.backlog = 0;
672e14ffdfdSEric Dumazet 
673e14ffdfdSEric Dumazet 	fq_flow_purge(&q->internal);
6748d34ce10SEric Dumazet 
6758d34ce10SEric Dumazet 	if (!q->fq_root)
6768d34ce10SEric Dumazet 		return;
6778d34ce10SEric Dumazet 
6788d34ce10SEric Dumazet 	for (idx = 0; idx < (1U << q->fq_trees_log); idx++) {
6798d34ce10SEric Dumazet 		root = &q->fq_root[idx];
6808d34ce10SEric Dumazet 		while ((p = rb_first(root)) != NULL) {
681e124557dSGeliang Tang 			f = rb_entry(p, struct fq_flow, fq_node);
6828d34ce10SEric Dumazet 			rb_erase(p, root);
6838d34ce10SEric Dumazet 
684e14ffdfdSEric Dumazet 			fq_flow_purge(f);
6858d34ce10SEric Dumazet 
6868d34ce10SEric Dumazet 			kmem_cache_free(fq_flow_cachep, f);
6878d34ce10SEric Dumazet 		}
6888d34ce10SEric Dumazet 	}
6898d34ce10SEric Dumazet 	q->new_flows.first	= NULL;
6908d34ce10SEric Dumazet 	q->old_flows.first	= NULL;
6918d34ce10SEric Dumazet 	q->delayed		= RB_ROOT;
6928d34ce10SEric Dumazet 	q->flows		= 0;
6938d34ce10SEric Dumazet 	q->inactive_flows	= 0;
6948d34ce10SEric Dumazet 	q->throttled_flows	= 0;
695afe4fd06SEric Dumazet }
696afe4fd06SEric Dumazet 
fq_rehash(struct fq_sched_data * q,struct rb_root * old_array,u32 old_log,struct rb_root * new_array,u32 new_log)697afe4fd06SEric Dumazet static void fq_rehash(struct fq_sched_data *q,
698afe4fd06SEric Dumazet 		      struct rb_root *old_array, u32 old_log,
699afe4fd06SEric Dumazet 		      struct rb_root *new_array, u32 new_log)
700afe4fd06SEric Dumazet {
701afe4fd06SEric Dumazet 	struct rb_node *op, **np, *parent;
702afe4fd06SEric Dumazet 	struct rb_root *oroot, *nroot;
703afe4fd06SEric Dumazet 	struct fq_flow *of, *nf;
704afe4fd06SEric Dumazet 	int fcnt = 0;
705afe4fd06SEric Dumazet 	u32 idx;
706afe4fd06SEric Dumazet 
707afe4fd06SEric Dumazet 	for (idx = 0; idx < (1U << old_log); idx++) {
708afe4fd06SEric Dumazet 		oroot = &old_array[idx];
709afe4fd06SEric Dumazet 		while ((op = rb_first(oroot)) != NULL) {
710afe4fd06SEric Dumazet 			rb_erase(op, oroot);
711e124557dSGeliang Tang 			of = rb_entry(op, struct fq_flow, fq_node);
712afe4fd06SEric Dumazet 			if (fq_gc_candidate(of)) {
713afe4fd06SEric Dumazet 				fcnt++;
714afe4fd06SEric Dumazet 				kmem_cache_free(fq_flow_cachep, of);
715afe4fd06SEric Dumazet 				continue;
716afe4fd06SEric Dumazet 			}
71729c58472SEric Dumazet 			nroot = &new_array[hash_ptr(of->sk, new_log)];
718afe4fd06SEric Dumazet 
719afe4fd06SEric Dumazet 			np = &nroot->rb_node;
720afe4fd06SEric Dumazet 			parent = NULL;
721afe4fd06SEric Dumazet 			while (*np) {
722afe4fd06SEric Dumazet 				parent = *np;
723afe4fd06SEric Dumazet 
724e124557dSGeliang Tang 				nf = rb_entry(parent, struct fq_flow, fq_node);
725afe4fd06SEric Dumazet 				BUG_ON(nf->sk == of->sk);
726afe4fd06SEric Dumazet 
727afe4fd06SEric Dumazet 				if (nf->sk > of->sk)
728afe4fd06SEric Dumazet 					np = &parent->rb_right;
729afe4fd06SEric Dumazet 				else
730afe4fd06SEric Dumazet 					np = &parent->rb_left;
731afe4fd06SEric Dumazet 			}
732afe4fd06SEric Dumazet 
733afe4fd06SEric Dumazet 			rb_link_node(&of->fq_node, parent, np);
734afe4fd06SEric Dumazet 			rb_insert_color(&of->fq_node, nroot);
735afe4fd06SEric Dumazet 		}
736afe4fd06SEric Dumazet 	}
737afe4fd06SEric Dumazet 	q->flows -= fcnt;
738afe4fd06SEric Dumazet 	q->inactive_flows -= fcnt;
739afe4fd06SEric Dumazet 	q->stat_gc_flows += fcnt;
740afe4fd06SEric Dumazet }
741afe4fd06SEric Dumazet 
fq_free(void * addr)742c3bd8549SEric Dumazet static void fq_free(void *addr)
743c3bd8549SEric Dumazet {
7444cb28970SWANG Cong 	kvfree(addr);
745c3bd8549SEric Dumazet }
746c3bd8549SEric Dumazet 
fq_resize(struct Qdisc * sch,u32 log)747c3bd8549SEric Dumazet static int fq_resize(struct Qdisc *sch, u32 log)
748c3bd8549SEric Dumazet {
749c3bd8549SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
750afe4fd06SEric Dumazet 	struct rb_root *array;
7512d8d40afSEric Dumazet 	void *old_fq_root;
752afe4fd06SEric Dumazet 	u32 idx;
753afe4fd06SEric Dumazet 
754afe4fd06SEric Dumazet 	if (q->fq_root && log == q->fq_trees_log)
755afe4fd06SEric Dumazet 		return 0;
756afe4fd06SEric Dumazet 
757c3bd8549SEric Dumazet 	/* If XPS was setup, we can allocate memory on right NUMA node */
758dcda9b04SMichal Hocko 	array = kvmalloc_node(sizeof(struct rb_root) << log, GFP_KERNEL | __GFP_RETRY_MAYFAIL,
759c3bd8549SEric Dumazet 			      netdev_queue_numa_node_read(sch->dev_queue));
760afe4fd06SEric Dumazet 	if (!array)
761afe4fd06SEric Dumazet 		return -ENOMEM;
762afe4fd06SEric Dumazet 
763afe4fd06SEric Dumazet 	for (idx = 0; idx < (1U << log); idx++)
764afe4fd06SEric Dumazet 		array[idx] = RB_ROOT;
765afe4fd06SEric Dumazet 
7662d8d40afSEric Dumazet 	sch_tree_lock(sch);
7672d8d40afSEric Dumazet 
7682d8d40afSEric Dumazet 	old_fq_root = q->fq_root;
7692d8d40afSEric Dumazet 	if (old_fq_root)
7702d8d40afSEric Dumazet 		fq_rehash(q, old_fq_root, q->fq_trees_log, array, log);
7712d8d40afSEric Dumazet 
772afe4fd06SEric Dumazet 	q->fq_root = array;
773afe4fd06SEric Dumazet 	q->fq_trees_log = log;
774afe4fd06SEric Dumazet 
7752d8d40afSEric Dumazet 	sch_tree_unlock(sch);
7762d8d40afSEric Dumazet 
7772d8d40afSEric Dumazet 	fq_free(old_fq_root);
7782d8d40afSEric Dumazet 
779afe4fd06SEric Dumazet 	return 0;
780afe4fd06SEric Dumazet }
781afe4fd06SEric Dumazet 
782*7041101fSDavide Caratti static struct netlink_range_validation iq_range = {
783*7041101fSDavide Caratti 	.max = INT_MAX,
784*7041101fSDavide Caratti };
785*7041101fSDavide Caratti 
786afe4fd06SEric Dumazet static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
787583396f4SEric Dumazet 	[TCA_FQ_UNSPEC]			= { .strict_start_type = TCA_FQ_TIMER_SLACK },
788583396f4SEric Dumazet 
789afe4fd06SEric Dumazet 	[TCA_FQ_PLIMIT]			= { .type = NLA_U32 },
790afe4fd06SEric Dumazet 	[TCA_FQ_FLOW_PLIMIT]		= { .type = NLA_U32 },
791afe4fd06SEric Dumazet 	[TCA_FQ_QUANTUM]		= { .type = NLA_U32 },
792*7041101fSDavide Caratti 	[TCA_FQ_INITIAL_QUANTUM]	= NLA_POLICY_FULL_RANGE(NLA_U32, &iq_range),
793afe4fd06SEric Dumazet 	[TCA_FQ_RATE_ENABLE]		= { .type = NLA_U32 },
794afe4fd06SEric Dumazet 	[TCA_FQ_FLOW_DEFAULT_RATE]	= { .type = NLA_U32 },
795afe4fd06SEric Dumazet 	[TCA_FQ_FLOW_MAX_RATE]		= { .type = NLA_U32 },
796afe4fd06SEric Dumazet 	[TCA_FQ_BUCKETS_LOG]		= { .type = NLA_U32 },
797f52ed899SEric Dumazet 	[TCA_FQ_FLOW_REFILL_DELAY]	= { .type = NLA_U32 },
7987e6dc03eSJakub Kicinski 	[TCA_FQ_ORPHAN_MASK]		= { .type = NLA_U32 },
79977879147SEric Dumazet 	[TCA_FQ_LOW_RATE_THRESHOLD]	= { .type = NLA_U32 },
80048872c11SEric Dumazet 	[TCA_FQ_CE_THRESHOLD]		= { .type = NLA_U32 },
801583396f4SEric Dumazet 	[TCA_FQ_TIMER_SLACK]		= { .type = NLA_U32 },
80239d01050SEric Dumazet 	[TCA_FQ_HORIZON]		= { .type = NLA_U32 },
80339d01050SEric Dumazet 	[TCA_FQ_HORIZON_DROP]		= { .type = NLA_U8 },
804afe4fd06SEric Dumazet };
805afe4fd06SEric Dumazet 
fq_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)8062030721cSAlexander Aring static int fq_change(struct Qdisc *sch, struct nlattr *opt,
8072030721cSAlexander Aring 		     struct netlink_ext_ack *extack)
808afe4fd06SEric Dumazet {
809afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
810afe4fd06SEric Dumazet 	struct nlattr *tb[TCA_FQ_MAX + 1];
811afe4fd06SEric Dumazet 	int err, drop_count = 0;
8122ccccf5fSWANG Cong 	unsigned drop_len = 0;
813afe4fd06SEric Dumazet 	u32 fq_log;
814afe4fd06SEric Dumazet 
8158cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_FQ_MAX, opt, fq_policy,
8168cb08174SJohannes Berg 					  NULL);
817afe4fd06SEric Dumazet 	if (err < 0)
818afe4fd06SEric Dumazet 		return err;
819afe4fd06SEric Dumazet 
820afe4fd06SEric Dumazet 	sch_tree_lock(sch);
821afe4fd06SEric Dumazet 
822afe4fd06SEric Dumazet 	fq_log = q->fq_trees_log;
823afe4fd06SEric Dumazet 
824afe4fd06SEric Dumazet 	if (tb[TCA_FQ_BUCKETS_LOG]) {
825afe4fd06SEric Dumazet 		u32 nval = nla_get_u32(tb[TCA_FQ_BUCKETS_LOG]);
826afe4fd06SEric Dumazet 
827afe4fd06SEric Dumazet 		if (nval >= 1 && nval <= ilog2(256*1024))
828afe4fd06SEric Dumazet 			fq_log = nval;
829afe4fd06SEric Dumazet 		else
830afe4fd06SEric Dumazet 			err = -EINVAL;
831afe4fd06SEric Dumazet 	}
832afe4fd06SEric Dumazet 	if (tb[TCA_FQ_PLIMIT])
833afe4fd06SEric Dumazet 		sch->limit = nla_get_u32(tb[TCA_FQ_PLIMIT]);
834afe4fd06SEric Dumazet 
835afe4fd06SEric Dumazet 	if (tb[TCA_FQ_FLOW_PLIMIT])
836afe4fd06SEric Dumazet 		q->flow_plimit = nla_get_u32(tb[TCA_FQ_FLOW_PLIMIT]);
837afe4fd06SEric Dumazet 
8383725a269SKenneth Klette Jonassen 	if (tb[TCA_FQ_QUANTUM]) {
8393725a269SKenneth Klette Jonassen 		u32 quantum = nla_get_u32(tb[TCA_FQ_QUANTUM]);
8403725a269SKenneth Klette Jonassen 
841d9e15a27SEric Dumazet 		if (quantum > 0 && quantum <= (1 << 20)) {
8423725a269SKenneth Klette Jonassen 			q->quantum = quantum;
843d9e15a27SEric Dumazet 		} else {
844d9e15a27SEric Dumazet 			NL_SET_ERR_MSG_MOD(extack, "invalid quantum");
8453725a269SKenneth Klette Jonassen 			err = -EINVAL;
8463725a269SKenneth Klette Jonassen 		}
847d9e15a27SEric Dumazet 	}
848afe4fd06SEric Dumazet 
849afe4fd06SEric Dumazet 	if (tb[TCA_FQ_INITIAL_QUANTUM])
850ede869cdSEric Dumazet 		q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
851afe4fd06SEric Dumazet 
852afe4fd06SEric Dumazet 	if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
85365c5189aSEric Dumazet 		pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
85465c5189aSEric Dumazet 				    nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
855afe4fd06SEric Dumazet 
85676a9ebe8SEric Dumazet 	if (tb[TCA_FQ_FLOW_MAX_RATE]) {
85776a9ebe8SEric Dumazet 		u32 rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
858afe4fd06SEric Dumazet 
85976a9ebe8SEric Dumazet 		q->flow_max_rate = (rate == ~0U) ? ~0UL : rate;
86076a9ebe8SEric Dumazet 	}
86177879147SEric Dumazet 	if (tb[TCA_FQ_LOW_RATE_THRESHOLD])
86277879147SEric Dumazet 		q->low_rate_threshold =
86377879147SEric Dumazet 			nla_get_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]);
86477879147SEric Dumazet 
865afe4fd06SEric Dumazet 	if (tb[TCA_FQ_RATE_ENABLE]) {
866afe4fd06SEric Dumazet 		u32 enable = nla_get_u32(tb[TCA_FQ_RATE_ENABLE]);
867afe4fd06SEric Dumazet 
868afe4fd06SEric Dumazet 		if (enable <= 1)
869afe4fd06SEric Dumazet 			q->rate_enable = enable;
870afe4fd06SEric Dumazet 		else
871afe4fd06SEric Dumazet 			err = -EINVAL;
872afe4fd06SEric Dumazet 	}
873afe4fd06SEric Dumazet 
874f52ed899SEric Dumazet 	if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
875f52ed899SEric Dumazet 		u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
876f52ed899SEric Dumazet 
877f52ed899SEric Dumazet 		q->flow_refill_delay = usecs_to_jiffies(usecs_delay);
878f52ed899SEric Dumazet 	}
879f52ed899SEric Dumazet 
88006eb395fSEric Dumazet 	if (tb[TCA_FQ_ORPHAN_MASK])
88106eb395fSEric Dumazet 		q->orphan_mask = nla_get_u32(tb[TCA_FQ_ORPHAN_MASK]);
88206eb395fSEric Dumazet 
88348872c11SEric Dumazet 	if (tb[TCA_FQ_CE_THRESHOLD])
88448872c11SEric Dumazet 		q->ce_threshold = (u64)NSEC_PER_USEC *
88548872c11SEric Dumazet 				  nla_get_u32(tb[TCA_FQ_CE_THRESHOLD]);
88648872c11SEric Dumazet 
887583396f4SEric Dumazet 	if (tb[TCA_FQ_TIMER_SLACK])
888583396f4SEric Dumazet 		q->timer_slack = nla_get_u32(tb[TCA_FQ_TIMER_SLACK]);
889583396f4SEric Dumazet 
89039d01050SEric Dumazet 	if (tb[TCA_FQ_HORIZON])
89139d01050SEric Dumazet 		q->horizon = (u64)NSEC_PER_USEC *
89239d01050SEric Dumazet 				  nla_get_u32(tb[TCA_FQ_HORIZON]);
89339d01050SEric Dumazet 
89439d01050SEric Dumazet 	if (tb[TCA_FQ_HORIZON_DROP])
89539d01050SEric Dumazet 		q->horizon_drop = nla_get_u8(tb[TCA_FQ_HORIZON_DROP]);
89639d01050SEric Dumazet 
8972d8d40afSEric Dumazet 	if (!err) {
89839d01050SEric Dumazet 
8992d8d40afSEric Dumazet 		sch_tree_unlock(sch);
900c3bd8549SEric Dumazet 		err = fq_resize(sch, fq_log);
9012d8d40afSEric Dumazet 		sch_tree_lock(sch);
9022d8d40afSEric Dumazet 	}
903afe4fd06SEric Dumazet 	while (sch->q.qlen > sch->limit) {
904afe4fd06SEric Dumazet 		struct sk_buff *skb = fq_dequeue(sch);
905afe4fd06SEric Dumazet 
9068d34ce10SEric Dumazet 		if (!skb)
9078d34ce10SEric Dumazet 			break;
9082ccccf5fSWANG Cong 		drop_len += qdisc_pkt_len(skb);
909e14ffdfdSEric Dumazet 		rtnl_kfree_skbs(skb, skb);
910afe4fd06SEric Dumazet 		drop_count++;
911afe4fd06SEric Dumazet 	}
9122ccccf5fSWANG Cong 	qdisc_tree_reduce_backlog(sch, drop_count, drop_len);
913afe4fd06SEric Dumazet 
914afe4fd06SEric Dumazet 	sch_tree_unlock(sch);
915afe4fd06SEric Dumazet 	return err;
916afe4fd06SEric Dumazet }
917afe4fd06SEric Dumazet 
fq_destroy(struct Qdisc * sch)918afe4fd06SEric Dumazet static void fq_destroy(struct Qdisc *sch)
919afe4fd06SEric Dumazet {
920afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
921afe4fd06SEric Dumazet 
9228d34ce10SEric Dumazet 	fq_reset(sch);
923c3bd8549SEric Dumazet 	fq_free(q->fq_root);
924afe4fd06SEric Dumazet 	qdisc_watchdog_cancel(&q->watchdog);
925afe4fd06SEric Dumazet }
926afe4fd06SEric Dumazet 
fq_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)927e63d7dfdSAlexander Aring static int fq_init(struct Qdisc *sch, struct nlattr *opt,
928e63d7dfdSAlexander Aring 		   struct netlink_ext_ack *extack)
929afe4fd06SEric Dumazet {
930afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
931afe4fd06SEric Dumazet 	int err;
932afe4fd06SEric Dumazet 
933afe4fd06SEric Dumazet 	sch->limit		= 10000;
934afe4fd06SEric Dumazet 	q->flow_plimit		= 100;
935afe4fd06SEric Dumazet 	q->quantum		= 2 * psched_mtu(qdisc_dev(sch));
936afe4fd06SEric Dumazet 	q->initial_quantum	= 10 * psched_mtu(qdisc_dev(sch));
937f52ed899SEric Dumazet 	q->flow_refill_delay	= msecs_to_jiffies(40);
93876a9ebe8SEric Dumazet 	q->flow_max_rate	= ~0UL;
939fefa569aSEric Dumazet 	q->time_next_delayed_flow = ~0ULL;
940afe4fd06SEric Dumazet 	q->rate_enable		= 1;
941afe4fd06SEric Dumazet 	q->new_flows.first	= NULL;
942afe4fd06SEric Dumazet 	q->old_flows.first	= NULL;
943afe4fd06SEric Dumazet 	q->delayed		= RB_ROOT;
944afe4fd06SEric Dumazet 	q->fq_root		= NULL;
945afe4fd06SEric Dumazet 	q->fq_trees_log		= ilog2(1024);
94606eb395fSEric Dumazet 	q->orphan_mask		= 1024 - 1;
94777879147SEric Dumazet 	q->low_rate_threshold	= 550000 / 8;
94848872c11SEric Dumazet 
949583396f4SEric Dumazet 	q->timer_slack = 10 * NSEC_PER_USEC; /* 10 usec of hrtimer slack */
950583396f4SEric Dumazet 
95139d01050SEric Dumazet 	q->horizon = 10ULL * NSEC_PER_SEC; /* 10 seconds */
95239d01050SEric Dumazet 	q->horizon_drop = 1; /* by default, drop packets beyond horizon */
95339d01050SEric Dumazet 
95448872c11SEric Dumazet 	/* Default ce_threshold of 4294 seconds */
95548872c11SEric Dumazet 	q->ce_threshold		= (u64)NSEC_PER_USEC * ~0U;
95648872c11SEric Dumazet 
957fb420d5dSEric Dumazet 	qdisc_watchdog_init_clockid(&q->watchdog, sch, CLOCK_MONOTONIC);
958afe4fd06SEric Dumazet 
959afe4fd06SEric Dumazet 	if (opt)
9602030721cSAlexander Aring 		err = fq_change(sch, opt, extack);
961afe4fd06SEric Dumazet 	else
962c3bd8549SEric Dumazet 		err = fq_resize(sch, q->fq_trees_log);
963afe4fd06SEric Dumazet 
964afe4fd06SEric Dumazet 	return err;
965afe4fd06SEric Dumazet }
966afe4fd06SEric Dumazet 
fq_dump(struct Qdisc * sch,struct sk_buff * skb)967afe4fd06SEric Dumazet static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
968afe4fd06SEric Dumazet {
969afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
97048872c11SEric Dumazet 	u64 ce_threshold = q->ce_threshold;
97139d01050SEric Dumazet 	u64 horizon = q->horizon;
972afe4fd06SEric Dumazet 	struct nlattr *opts;
973afe4fd06SEric Dumazet 
974ae0be8deSMichal Kubecek 	opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
975afe4fd06SEric Dumazet 	if (opts == NULL)
976afe4fd06SEric Dumazet 		goto nla_put_failure;
977afe4fd06SEric Dumazet 
97865c5189aSEric Dumazet 	/* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
97965c5189aSEric Dumazet 
98048872c11SEric Dumazet 	do_div(ce_threshold, NSEC_PER_USEC);
98139d01050SEric Dumazet 	do_div(horizon, NSEC_PER_USEC);
98248872c11SEric Dumazet 
983afe4fd06SEric Dumazet 	if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
984afe4fd06SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
985afe4fd06SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
986afe4fd06SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
987afe4fd06SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
98876a9ebe8SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE,
98976a9ebe8SEric Dumazet 			min_t(unsigned long, q->flow_max_rate, ~0U)) ||
990f52ed899SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
991f52ed899SEric Dumazet 			jiffies_to_usecs(q->flow_refill_delay)) ||
99206eb395fSEric Dumazet 	    nla_put_u32(skb, TCA_FQ_ORPHAN_MASK, q->orphan_mask) ||
99377879147SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_LOW_RATE_THRESHOLD,
99477879147SEric Dumazet 			q->low_rate_threshold) ||
99548872c11SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_CE_THRESHOLD, (u32)ce_threshold) ||
996583396f4SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log) ||
99739d01050SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_TIMER_SLACK, q->timer_slack) ||
99839d01050SEric Dumazet 	    nla_put_u32(skb, TCA_FQ_HORIZON, (u32)horizon) ||
99939d01050SEric Dumazet 	    nla_put_u8(skb, TCA_FQ_HORIZON_DROP, q->horizon_drop))
1000afe4fd06SEric Dumazet 		goto nla_put_failure;
1001afe4fd06SEric Dumazet 
1002d59b7d80SYang Yingliang 	return nla_nest_end(skb, opts);
1003afe4fd06SEric Dumazet 
1004afe4fd06SEric Dumazet nla_put_failure:
1005afe4fd06SEric Dumazet 	return -1;
1006afe4fd06SEric Dumazet }
1007afe4fd06SEric Dumazet 
fq_dump_stats(struct Qdisc * sch,struct gnet_dump * d)1008afe4fd06SEric Dumazet static int fq_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
1009afe4fd06SEric Dumazet {
1010afe4fd06SEric Dumazet 	struct fq_sched_data *q = qdisc_priv(sch);
1011695b4ec0SEric Dumazet 	struct tc_fq_qd_stats st;
1012695b4ec0SEric Dumazet 
1013695b4ec0SEric Dumazet 	sch_tree_lock(sch);
1014695b4ec0SEric Dumazet 
1015695b4ec0SEric Dumazet 	st.gc_flows		  = q->stat_gc_flows;
1016695b4ec0SEric Dumazet 	st.highprio_packets	  = q->stat_internal_packets;
101790caf67bSEric Dumazet 	st.tcp_retrans		  = 0;
1018695b4ec0SEric Dumazet 	st.throttled		  = q->stat_throttled;
1019695b4ec0SEric Dumazet 	st.flows_plimit		  = q->stat_flows_plimit;
1020695b4ec0SEric Dumazet 	st.pkts_too_long	  = q->stat_pkts_too_long;
1021695b4ec0SEric Dumazet 	st.allocation_errors	  = q->stat_allocation_errors;
1022583396f4SEric Dumazet 	st.time_next_delayed_flow = q->time_next_delayed_flow + q->timer_slack -
1023583396f4SEric Dumazet 				    ktime_get_ns();
1024695b4ec0SEric Dumazet 	st.flows		  = q->flows;
1025695b4ec0SEric Dumazet 	st.inactive_flows	  = q->inactive_flows;
1026695b4ec0SEric Dumazet 	st.throttled_flows	  = q->throttled_flows;
1027fefa569aSEric Dumazet 	st.unthrottle_latency_ns  = min_t(unsigned long,
1028fefa569aSEric Dumazet 					  q->unthrottle_latency_ns, ~0U);
102948872c11SEric Dumazet 	st.ce_mark		  = q->stat_ce_mark;
103039d01050SEric Dumazet 	st.horizon_drops	  = q->stat_horizon_drops;
103139d01050SEric Dumazet 	st.horizon_caps		  = q->stat_horizon_caps;
1032695b4ec0SEric Dumazet 	sch_tree_unlock(sch);
1033afe4fd06SEric Dumazet 
1034afe4fd06SEric Dumazet 	return gnet_stats_copy_app(d, &st, sizeof(st));
1035afe4fd06SEric Dumazet }
1036afe4fd06SEric Dumazet 
1037afe4fd06SEric Dumazet static struct Qdisc_ops fq_qdisc_ops __read_mostly = {
1038afe4fd06SEric Dumazet 	.id		=	"fq",
1039afe4fd06SEric Dumazet 	.priv_size	=	sizeof(struct fq_sched_data),
1040afe4fd06SEric Dumazet 
1041afe4fd06SEric Dumazet 	.enqueue	=	fq_enqueue,
1042afe4fd06SEric Dumazet 	.dequeue	=	fq_dequeue,
1043afe4fd06SEric Dumazet 	.peek		=	qdisc_peek_dequeued,
1044afe4fd06SEric Dumazet 	.init		=	fq_init,
1045afe4fd06SEric Dumazet 	.reset		=	fq_reset,
1046afe4fd06SEric Dumazet 	.destroy	=	fq_destroy,
1047afe4fd06SEric Dumazet 	.change		=	fq_change,
1048afe4fd06SEric Dumazet 	.dump		=	fq_dump,
1049afe4fd06SEric Dumazet 	.dump_stats	=	fq_dump_stats,
1050afe4fd06SEric Dumazet 	.owner		=	THIS_MODULE,
1051afe4fd06SEric Dumazet };
1052afe4fd06SEric Dumazet 
fq_module_init(void)1053afe4fd06SEric Dumazet static int __init fq_module_init(void)
1054afe4fd06SEric Dumazet {
1055afe4fd06SEric Dumazet 	int ret;
1056afe4fd06SEric Dumazet 
1057afe4fd06SEric Dumazet 	fq_flow_cachep = kmem_cache_create("fq_flow_cache",
1058afe4fd06SEric Dumazet 					   sizeof(struct fq_flow),
1059afe4fd06SEric Dumazet 					   0, 0, NULL);
1060afe4fd06SEric Dumazet 	if (!fq_flow_cachep)
1061afe4fd06SEric Dumazet 		return -ENOMEM;
1062afe4fd06SEric Dumazet 
1063afe4fd06SEric Dumazet 	ret = register_qdisc(&fq_qdisc_ops);
1064afe4fd06SEric Dumazet 	if (ret)
1065afe4fd06SEric Dumazet 		kmem_cache_destroy(fq_flow_cachep);
1066afe4fd06SEric Dumazet 	return ret;
1067afe4fd06SEric Dumazet }
1068afe4fd06SEric Dumazet 
fq_module_exit(void)1069afe4fd06SEric Dumazet static void __exit fq_module_exit(void)
1070afe4fd06SEric Dumazet {
1071afe4fd06SEric Dumazet 	unregister_qdisc(&fq_qdisc_ops);
1072afe4fd06SEric Dumazet 	kmem_cache_destroy(fq_flow_cachep);
1073afe4fd06SEric Dumazet }
1074afe4fd06SEric Dumazet 
1075afe4fd06SEric Dumazet module_init(fq_module_init)
1076afe4fd06SEric Dumazet module_exit(fq_module_exit)
1077afe4fd06SEric Dumazet MODULE_AUTHOR("Eric Dumazet");
1078afe4fd06SEric Dumazet MODULE_LICENSE("GPL");
107967c20de3SRob Gill MODULE_DESCRIPTION("Fair Queue Packet Scheduler");
1080