xref: /openbmc/linux/include/net/fq_impl.h (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1fb9e53ccSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2557fc4a0SMichal Kazior /*
3557fc4a0SMichal Kazior  * Copyright (c) 2016 Qualcomm Atheros, Inc
4557fc4a0SMichal Kazior  *
5557fc4a0SMichal Kazior  * Based on net/sched/sch_fq_codel.c
6557fc4a0SMichal Kazior  */
7557fc4a0SMichal Kazior #ifndef __NET_SCHED_FQ_IMPL_H
8557fc4a0SMichal Kazior #define __NET_SCHED_FQ_IMPL_H
9557fc4a0SMichal Kazior 
10557fc4a0SMichal Kazior #include <net/fq.h>
11557fc4a0SMichal Kazior 
12557fc4a0SMichal Kazior /* functions that are embedded into includer */
13557fc4a0SMichal Kazior 
1407be2fedSFelix Fietkau 
1507be2fedSFelix Fietkau static void
__fq_adjust_removal(struct fq * fq,struct fq_flow * flow,unsigned int packets,unsigned int bytes,unsigned int truesize)1607be2fedSFelix Fietkau __fq_adjust_removal(struct fq *fq, struct fq_flow *flow, unsigned int packets,
1707be2fedSFelix Fietkau 		    unsigned int bytes, unsigned int truesize)
1807be2fedSFelix Fietkau {
1907be2fedSFelix Fietkau 	struct fq_tin *tin = flow->tin;
20d7b64929SFelix Fietkau 	int idx;
2107be2fedSFelix Fietkau 
2207be2fedSFelix Fietkau 	tin->backlog_bytes -= bytes;
2307be2fedSFelix Fietkau 	tin->backlog_packets -= packets;
2407be2fedSFelix Fietkau 	flow->backlog -= bytes;
2507be2fedSFelix Fietkau 	fq->backlog -= packets;
2607be2fedSFelix Fietkau 	fq->memory_usage -= truesize;
27d7b64929SFelix Fietkau 
28d7b64929SFelix Fietkau 	if (flow->backlog)
29d7b64929SFelix Fietkau 		return;
30d7b64929SFelix Fietkau 
31d7b64929SFelix Fietkau 	if (flow == &tin->default_flow) {
32d7b64929SFelix Fietkau 		list_del_init(&tin->tin_list);
33d7b64929SFelix Fietkau 		return;
34d7b64929SFelix Fietkau 	}
35d7b64929SFelix Fietkau 
36d7b64929SFelix Fietkau 	idx = flow - fq->flows;
37d7b64929SFelix Fietkau 	__clear_bit(idx, fq->flows_bitmap);
3807be2fedSFelix Fietkau }
3907be2fedSFelix Fietkau 
fq_adjust_removal(struct fq * fq,struct fq_flow * flow,struct sk_buff * skb)408c418b5bSJohannes Berg static void fq_adjust_removal(struct fq *fq,
418c418b5bSJohannes Berg 			      struct fq_flow *flow,
428c418b5bSJohannes Berg 			      struct sk_buff *skb)
43557fc4a0SMichal Kazior {
4407be2fedSFelix Fietkau 	__fq_adjust_removal(fq, flow, 1, skb->len, skb->truesize);
458c418b5bSJohannes Berg }
468c418b5bSJohannes Berg 
fq_flow_dequeue(struct fq * fq,struct fq_flow * flow)478c418b5bSJohannes Berg static struct sk_buff *fq_flow_dequeue(struct fq *fq,
488c418b5bSJohannes Berg 				       struct fq_flow *flow)
498c418b5bSJohannes Berg {
508c418b5bSJohannes Berg 	struct sk_buff *skb;
518c418b5bSJohannes Berg 
528c418b5bSJohannes Berg 	lockdep_assert_held(&fq->lock);
538c418b5bSJohannes Berg 
548c418b5bSJohannes Berg 	skb = __skb_dequeue(&flow->queue);
558c418b5bSJohannes Berg 	if (!skb)
568c418b5bSJohannes Berg 		return NULL;
578c418b5bSJohannes Berg 
588c418b5bSJohannes Berg 	fq_adjust_removal(fq, flow, skb);
59557fc4a0SMichal Kazior 
60557fc4a0SMichal Kazior 	return skb;
61557fc4a0SMichal Kazior }
62557fc4a0SMichal Kazior 
fq_flow_drop(struct fq * fq,struct fq_flow * flow,fq_skb_free_t free_func)6307be2fedSFelix Fietkau static int fq_flow_drop(struct fq *fq, struct fq_flow *flow,
6407be2fedSFelix Fietkau 			fq_skb_free_t free_func)
6507be2fedSFelix Fietkau {
6607be2fedSFelix Fietkau 	unsigned int packets = 0, bytes = 0, truesize = 0;
6707be2fedSFelix Fietkau 	struct fq_tin *tin = flow->tin;
6807be2fedSFelix Fietkau 	struct sk_buff *skb;
6907be2fedSFelix Fietkau 	int pending;
7007be2fedSFelix Fietkau 
7107be2fedSFelix Fietkau 	lockdep_assert_held(&fq->lock);
7207be2fedSFelix Fietkau 
7307be2fedSFelix Fietkau 	pending = min_t(int, 32, skb_queue_len(&flow->queue) / 2);
7407be2fedSFelix Fietkau 	do {
7507be2fedSFelix Fietkau 		skb = __skb_dequeue(&flow->queue);
7607be2fedSFelix Fietkau 		if (!skb)
7707be2fedSFelix Fietkau 			break;
7807be2fedSFelix Fietkau 
7907be2fedSFelix Fietkau 		packets++;
8007be2fedSFelix Fietkau 		bytes += skb->len;
8107be2fedSFelix Fietkau 		truesize += skb->truesize;
8207be2fedSFelix Fietkau 		free_func(fq, tin, flow, skb);
8307be2fedSFelix Fietkau 	} while (packets < pending);
8407be2fedSFelix Fietkau 
8507be2fedSFelix Fietkau 	__fq_adjust_removal(fq, flow, packets, bytes, truesize);
8607be2fedSFelix Fietkau 
8707be2fedSFelix Fietkau 	return packets;
8807be2fedSFelix Fietkau }
8907be2fedSFelix Fietkau 
fq_tin_dequeue(struct fq * fq,struct fq_tin * tin,fq_tin_dequeue_t dequeue_func)90557fc4a0SMichal Kazior static struct sk_buff *fq_tin_dequeue(struct fq *fq,
91557fc4a0SMichal Kazior 				      struct fq_tin *tin,
92557fc4a0SMichal Kazior 				      fq_tin_dequeue_t dequeue_func)
93557fc4a0SMichal Kazior {
94557fc4a0SMichal Kazior 	struct fq_flow *flow;
95557fc4a0SMichal Kazior 	struct list_head *head;
96557fc4a0SMichal Kazior 	struct sk_buff *skb;
97557fc4a0SMichal Kazior 
98557fc4a0SMichal Kazior 	lockdep_assert_held(&fq->lock);
99557fc4a0SMichal Kazior 
100557fc4a0SMichal Kazior begin:
101557fc4a0SMichal Kazior 	head = &tin->new_flows;
102557fc4a0SMichal Kazior 	if (list_empty(head)) {
103557fc4a0SMichal Kazior 		head = &tin->old_flows;
104557fc4a0SMichal Kazior 		if (list_empty(head))
105557fc4a0SMichal Kazior 			return NULL;
106557fc4a0SMichal Kazior 	}
107557fc4a0SMichal Kazior 
108557fc4a0SMichal Kazior 	flow = list_first_entry(head, struct fq_flow, flowchain);
109557fc4a0SMichal Kazior 
110557fc4a0SMichal Kazior 	if (flow->deficit <= 0) {
111557fc4a0SMichal Kazior 		flow->deficit += fq->quantum;
112557fc4a0SMichal Kazior 		list_move_tail(&flow->flowchain,
113557fc4a0SMichal Kazior 			       &tin->old_flows);
114557fc4a0SMichal Kazior 		goto begin;
115557fc4a0SMichal Kazior 	}
116557fc4a0SMichal Kazior 
117557fc4a0SMichal Kazior 	skb = dequeue_func(fq, tin, flow);
118557fc4a0SMichal Kazior 	if (!skb) {
119557fc4a0SMichal Kazior 		/* force a pass through old_flows to prevent starvation */
120557fc4a0SMichal Kazior 		if ((head == &tin->new_flows) &&
121557fc4a0SMichal Kazior 		    !list_empty(&tin->old_flows)) {
122557fc4a0SMichal Kazior 			list_move_tail(&flow->flowchain, &tin->old_flows);
123557fc4a0SMichal Kazior 		} else {
124557fc4a0SMichal Kazior 			list_del_init(&flow->flowchain);
125557fc4a0SMichal Kazior 			flow->tin = NULL;
126557fc4a0SMichal Kazior 		}
127557fc4a0SMichal Kazior 		goto begin;
128557fc4a0SMichal Kazior 	}
129557fc4a0SMichal Kazior 
130557fc4a0SMichal Kazior 	flow->deficit -= skb->len;
131557fc4a0SMichal Kazior 	tin->tx_bytes += skb->len;
132557fc4a0SMichal Kazior 	tin->tx_packets++;
133557fc4a0SMichal Kazior 
134557fc4a0SMichal Kazior 	return skb;
135557fc4a0SMichal Kazior }
136557fc4a0SMichal Kazior 
fq_flow_idx(struct fq * fq,struct sk_buff * skb)137f2af2df8SFelix Fietkau static u32 fq_flow_idx(struct fq *fq, struct sk_buff *skb)
138f2af2df8SFelix Fietkau {
13948a54f6bSFelix Fietkau 	u32 hash = skb_get_hash(skb);
140f2af2df8SFelix Fietkau 
141f2af2df8SFelix Fietkau 	return reciprocal_scale(hash, fq->flows_cnt);
142f2af2df8SFelix Fietkau }
143f2af2df8SFelix Fietkau 
fq_flow_classify(struct fq * fq,struct fq_tin * tin,u32 idx,struct sk_buff * skb)144557fc4a0SMichal Kazior static struct fq_flow *fq_flow_classify(struct fq *fq,
145f2af2df8SFelix Fietkau 					struct fq_tin *tin, u32 idx,
146bf9009bfSFelix Fietkau 					struct sk_buff *skb)
147557fc4a0SMichal Kazior {
148557fc4a0SMichal Kazior 	struct fq_flow *flow;
149557fc4a0SMichal Kazior 
150557fc4a0SMichal Kazior 	lockdep_assert_held(&fq->lock);
151557fc4a0SMichal Kazior 
152557fc4a0SMichal Kazior 	flow = &fq->flows[idx];
153557fc4a0SMichal Kazior 	if (flow->tin && flow->tin != tin) {
154bf9009bfSFelix Fietkau 		flow = &tin->default_flow;
155557fc4a0SMichal Kazior 		tin->collisions++;
156557fc4a0SMichal Kazior 		fq->collisions++;
157557fc4a0SMichal Kazior 	}
158557fc4a0SMichal Kazior 
159557fc4a0SMichal Kazior 	if (!flow->tin)
160557fc4a0SMichal Kazior 		tin->flows++;
161557fc4a0SMichal Kazior 
162557fc4a0SMichal Kazior 	return flow;
163557fc4a0SMichal Kazior }
164557fc4a0SMichal Kazior 
fq_find_fattest_flow(struct fq * fq)165d7b64929SFelix Fietkau static struct fq_flow *fq_find_fattest_flow(struct fq *fq)
166557fc4a0SMichal Kazior {
167d7b64929SFelix Fietkau 	struct fq_tin *tin;
168d7b64929SFelix Fietkau 	struct fq_flow *flow = NULL;
169d7b64929SFelix Fietkau 	u32 len = 0;
170d7b64929SFelix Fietkau 	int i;
171557fc4a0SMichal Kazior 
172d7b64929SFelix Fietkau 	for_each_set_bit(i, fq->flows_bitmap, fq->flows_cnt) {
173d7b64929SFelix Fietkau 		struct fq_flow *cur = &fq->flows[i];
174d7b64929SFelix Fietkau 		unsigned int cur_len;
175557fc4a0SMichal Kazior 
176d7b64929SFelix Fietkau 		cur_len = cur->backlog;
177d7b64929SFelix Fietkau 		if (cur_len <= len)
178d7b64929SFelix Fietkau 			continue;
179557fc4a0SMichal Kazior 
180d7b64929SFelix Fietkau 		flow = cur;
181d7b64929SFelix Fietkau 		len = cur_len;
182d7b64929SFelix Fietkau 	}
183d7b64929SFelix Fietkau 
184d7b64929SFelix Fietkau 	list_for_each_entry(tin, &fq->tin_backlog, tin_list) {
185d7b64929SFelix Fietkau 		unsigned int cur_len = tin->default_flow.backlog;
186d7b64929SFelix Fietkau 
187d7b64929SFelix Fietkau 		if (cur_len <= len)
188d7b64929SFelix Fietkau 			continue;
189d7b64929SFelix Fietkau 
190d7b64929SFelix Fietkau 		flow = &tin->default_flow;
191d7b64929SFelix Fietkau 		len = cur_len;
192d7b64929SFelix Fietkau 	}
193d7b64929SFelix Fietkau 
194d7b64929SFelix Fietkau 	return flow;
195b43e7199SMichal Kazior }
196b43e7199SMichal Kazior 
fq_tin_enqueue(struct fq * fq,struct fq_tin * tin,u32 idx,struct sk_buff * skb,fq_skb_free_t free_func)197b43e7199SMichal Kazior static void fq_tin_enqueue(struct fq *fq,
198f2af2df8SFelix Fietkau 			   struct fq_tin *tin, u32 idx,
199b43e7199SMichal Kazior 			   struct sk_buff *skb,
200bf9009bfSFelix Fietkau 			   fq_skb_free_t free_func)
201b43e7199SMichal Kazior {
202b43e7199SMichal Kazior 	struct fq_flow *flow;
203*7d360f60SFelix Fietkau 	struct sk_buff *next;
2040bfe649fSToke Høiland-Jørgensen 	bool oom;
205b43e7199SMichal Kazior 
206b43e7199SMichal Kazior 	lockdep_assert_held(&fq->lock);
207b43e7199SMichal Kazior 
208bf9009bfSFelix Fietkau 	flow = fq_flow_classify(fq, tin, idx, skb);
209b43e7199SMichal Kazior 
210d7b64929SFelix Fietkau 	if (!flow->backlog) {
211d7b64929SFelix Fietkau 		if (flow != &tin->default_flow)
212d7b64929SFelix Fietkau 			__set_bit(idx, fq->flows_bitmap);
213d7b64929SFelix Fietkau 		else if (list_empty(&tin->tin_list))
214d7b64929SFelix Fietkau 			list_add(&tin->tin_list, &fq->tin_backlog);
215d7b64929SFelix Fietkau 	}
216d7b64929SFelix Fietkau 
217b43e7199SMichal Kazior 	flow->tin = tin;
218*7d360f60SFelix Fietkau 	skb_list_walk_safe(skb, skb, next) {
219*7d360f60SFelix Fietkau 		skb_mark_not_on_list(skb);
220b43e7199SMichal Kazior 		flow->backlog += skb->len;
221b43e7199SMichal Kazior 		tin->backlog_bytes += skb->len;
222b43e7199SMichal Kazior 		tin->backlog_packets++;
223097b065bSToke Høiland-Jørgensen 		fq->memory_usage += skb->truesize;
224b43e7199SMichal Kazior 		fq->backlog++;
225*7d360f60SFelix Fietkau 		__skb_queue_tail(&flow->queue, skb);
226*7d360f60SFelix Fietkau 	}
227b43e7199SMichal Kazior 
228557fc4a0SMichal Kazior 	if (list_empty(&flow->flowchain)) {
229557fc4a0SMichal Kazior 		flow->deficit = fq->quantum;
230557fc4a0SMichal Kazior 		list_add_tail(&flow->flowchain,
231557fc4a0SMichal Kazior 			      &tin->new_flows);
232557fc4a0SMichal Kazior 	}
233557fc4a0SMichal Kazior 
2340bfe649fSToke Høiland-Jørgensen 	oom = (fq->memory_usage > fq->memory_limit);
2350bfe649fSToke Høiland-Jørgensen 	while (fq->backlog > fq->limit || oom) {
236d7b64929SFelix Fietkau 		flow = fq_find_fattest_flow(fq);
237557fc4a0SMichal Kazior 		if (!flow)
238557fc4a0SMichal Kazior 			return;
239557fc4a0SMichal Kazior 
24007be2fedSFelix Fietkau 		if (!fq_flow_drop(fq, flow, free_func))
241557fc4a0SMichal Kazior 			return;
242557fc4a0SMichal Kazior 
243557fc4a0SMichal Kazior 		flow->tin->overlimit++;
244557fc4a0SMichal Kazior 		fq->overlimit++;
2450bfe649fSToke Høiland-Jørgensen 		if (oom) {
246097b065bSToke Høiland-Jørgensen 			fq->overmemory++;
2470bfe649fSToke Høiland-Jørgensen 			oom = (fq->memory_usage > fq->memory_limit);
2480bfe649fSToke Høiland-Jørgensen 		}
249557fc4a0SMichal Kazior 	}
250557fc4a0SMichal Kazior }
251557fc4a0SMichal Kazior 
fq_flow_filter(struct fq * fq,struct fq_flow * flow,fq_skb_filter_t filter_func,void * filter_data,fq_skb_free_t free_func)2528c418b5bSJohannes Berg static void fq_flow_filter(struct fq *fq,
2538c418b5bSJohannes Berg 			   struct fq_flow *flow,
2548c418b5bSJohannes Berg 			   fq_skb_filter_t filter_func,
2558c418b5bSJohannes Berg 			   void *filter_data,
2568c418b5bSJohannes Berg 			   fq_skb_free_t free_func)
2578c418b5bSJohannes Berg {
2588c418b5bSJohannes Berg 	struct fq_tin *tin = flow->tin;
2598c418b5bSJohannes Berg 	struct sk_buff *skb, *tmp;
2608c418b5bSJohannes Berg 
2618c418b5bSJohannes Berg 	lockdep_assert_held(&fq->lock);
2628c418b5bSJohannes Berg 
2638c418b5bSJohannes Berg 	skb_queue_walk_safe(&flow->queue, skb, tmp) {
2648c418b5bSJohannes Berg 		if (!filter_func(fq, tin, flow, skb, filter_data))
2658c418b5bSJohannes Berg 			continue;
2668c418b5bSJohannes Berg 
2678c418b5bSJohannes Berg 		__skb_unlink(skb, &flow->queue);
2688c418b5bSJohannes Berg 		fq_adjust_removal(fq, flow, skb);
2698c418b5bSJohannes Berg 		free_func(fq, tin, flow, skb);
2708c418b5bSJohannes Berg 	}
2718c418b5bSJohannes Berg }
2728c418b5bSJohannes Berg 
fq_tin_filter(struct fq * fq,struct fq_tin * tin,fq_skb_filter_t filter_func,void * filter_data,fq_skb_free_t free_func)2738c418b5bSJohannes Berg static void fq_tin_filter(struct fq *fq,
2748c418b5bSJohannes Berg 			  struct fq_tin *tin,
2758c418b5bSJohannes Berg 			  fq_skb_filter_t filter_func,
2768c418b5bSJohannes Berg 			  void *filter_data,
2778c418b5bSJohannes Berg 			  fq_skb_free_t free_func)
2788c418b5bSJohannes Berg {
2798c418b5bSJohannes Berg 	struct fq_flow *flow;
2808c418b5bSJohannes Berg 
2818c418b5bSJohannes Berg 	lockdep_assert_held(&fq->lock);
2828c418b5bSJohannes Berg 
2838c418b5bSJohannes Berg 	list_for_each_entry(flow, &tin->new_flows, flowchain)
2848c418b5bSJohannes Berg 		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
2858c418b5bSJohannes Berg 	list_for_each_entry(flow, &tin->old_flows, flowchain)
2868c418b5bSJohannes Berg 		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
2878c418b5bSJohannes Berg }
2888c418b5bSJohannes Berg 
fq_flow_reset(struct fq * fq,struct fq_flow * flow,fq_skb_free_t free_func)289557fc4a0SMichal Kazior static void fq_flow_reset(struct fq *fq,
290557fc4a0SMichal Kazior 			  struct fq_flow *flow,
291557fc4a0SMichal Kazior 			  fq_skb_free_t free_func)
292557fc4a0SMichal Kazior {
293d7b64929SFelix Fietkau 	struct fq_tin *tin = flow->tin;
294557fc4a0SMichal Kazior 	struct sk_buff *skb;
295557fc4a0SMichal Kazior 
296557fc4a0SMichal Kazior 	while ((skb = fq_flow_dequeue(fq, flow)))
297d7b64929SFelix Fietkau 		free_func(fq, tin, flow, skb);
298557fc4a0SMichal Kazior 
299d7b64929SFelix Fietkau 	if (!list_empty(&flow->flowchain)) {
300557fc4a0SMichal Kazior 		list_del_init(&flow->flowchain);
301d7b64929SFelix Fietkau 		if (list_empty(&tin->new_flows) &&
302d7b64929SFelix Fietkau 		    list_empty(&tin->old_flows))
303d7b64929SFelix Fietkau 			list_del_init(&tin->tin_list);
304d7b64929SFelix Fietkau 	}
305557fc4a0SMichal Kazior 
306557fc4a0SMichal Kazior 	flow->tin = NULL;
307557fc4a0SMichal Kazior 
308557fc4a0SMichal Kazior 	WARN_ON_ONCE(flow->backlog);
309557fc4a0SMichal Kazior }
310557fc4a0SMichal Kazior 
fq_tin_reset(struct fq * fq,struct fq_tin * tin,fq_skb_free_t free_func)311557fc4a0SMichal Kazior static void fq_tin_reset(struct fq *fq,
312557fc4a0SMichal Kazior 			 struct fq_tin *tin,
313557fc4a0SMichal Kazior 			 fq_skb_free_t free_func)
314557fc4a0SMichal Kazior {
315557fc4a0SMichal Kazior 	struct list_head *head;
316557fc4a0SMichal Kazior 	struct fq_flow *flow;
317557fc4a0SMichal Kazior 
318557fc4a0SMichal Kazior 	for (;;) {
319557fc4a0SMichal Kazior 		head = &tin->new_flows;
320557fc4a0SMichal Kazior 		if (list_empty(head)) {
321557fc4a0SMichal Kazior 			head = &tin->old_flows;
322557fc4a0SMichal Kazior 			if (list_empty(head))
323557fc4a0SMichal Kazior 				break;
324557fc4a0SMichal Kazior 		}
325557fc4a0SMichal Kazior 
326557fc4a0SMichal Kazior 		flow = list_first_entry(head, struct fq_flow, flowchain);
327557fc4a0SMichal Kazior 		fq_flow_reset(fq, flow, free_func);
328557fc4a0SMichal Kazior 	}
329557fc4a0SMichal Kazior 
330d7b64929SFelix Fietkau 	WARN_ON_ONCE(!list_empty(&tin->tin_list));
331557fc4a0SMichal Kazior 	WARN_ON_ONCE(tin->backlog_bytes);
332557fc4a0SMichal Kazior 	WARN_ON_ONCE(tin->backlog_packets);
333557fc4a0SMichal Kazior }
334557fc4a0SMichal Kazior 
fq_flow_init(struct fq_flow * flow)335557fc4a0SMichal Kazior static void fq_flow_init(struct fq_flow *flow)
336557fc4a0SMichal Kazior {
337557fc4a0SMichal Kazior 	INIT_LIST_HEAD(&flow->flowchain);
338557fc4a0SMichal Kazior 	__skb_queue_head_init(&flow->queue);
339557fc4a0SMichal Kazior }
340557fc4a0SMichal Kazior 
fq_tin_init(struct fq_tin * tin)341557fc4a0SMichal Kazior static void fq_tin_init(struct fq_tin *tin)
342557fc4a0SMichal Kazior {
343557fc4a0SMichal Kazior 	INIT_LIST_HEAD(&tin->new_flows);
344557fc4a0SMichal Kazior 	INIT_LIST_HEAD(&tin->old_flows);
345d7b64929SFelix Fietkau 	INIT_LIST_HEAD(&tin->tin_list);
346bf9009bfSFelix Fietkau 	fq_flow_init(&tin->default_flow);
347557fc4a0SMichal Kazior }
348557fc4a0SMichal Kazior 
fq_init(struct fq * fq,int flows_cnt)349557fc4a0SMichal Kazior static int fq_init(struct fq *fq, int flows_cnt)
350557fc4a0SMichal Kazior {
351557fc4a0SMichal Kazior 	int i;
352557fc4a0SMichal Kazior 
353557fc4a0SMichal Kazior 	memset(fq, 0, sizeof(fq[0]));
354557fc4a0SMichal Kazior 	spin_lock_init(&fq->lock);
355d7b64929SFelix Fietkau 	INIT_LIST_HEAD(&fq->tin_backlog);
356557fc4a0SMichal Kazior 	fq->flows_cnt = max_t(u32, flows_cnt, 1);
357557fc4a0SMichal Kazior 	fq->quantum = 300;
358557fc4a0SMichal Kazior 	fq->limit = 8192;
359097b065bSToke Høiland-Jørgensen 	fq->memory_limit = 16 << 20; /* 16 MBytes */
360557fc4a0SMichal Kazior 
36171e67c3bSToke Høiland-Jørgensen 	fq->flows = kvcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
362557fc4a0SMichal Kazior 	if (!fq->flows)
363557fc4a0SMichal Kazior 		return -ENOMEM;
364557fc4a0SMichal Kazior 
3652b8bf3d6SChristophe JAILLET 	fq->flows_bitmap = bitmap_zalloc(fq->flows_cnt, GFP_KERNEL);
366d7b64929SFelix Fietkau 	if (!fq->flows_bitmap) {
367d7b64929SFelix Fietkau 		kvfree(fq->flows);
368d7b64929SFelix Fietkau 		fq->flows = NULL;
369d7b64929SFelix Fietkau 		return -ENOMEM;
370d7b64929SFelix Fietkau 	}
371d7b64929SFelix Fietkau 
372557fc4a0SMichal Kazior 	for (i = 0; i < fq->flows_cnt; i++)
373557fc4a0SMichal Kazior 		fq_flow_init(&fq->flows[i]);
374557fc4a0SMichal Kazior 
375557fc4a0SMichal Kazior 	return 0;
376557fc4a0SMichal Kazior }
377557fc4a0SMichal Kazior 
fq_reset(struct fq * fq,fq_skb_free_t free_func)378557fc4a0SMichal Kazior static void fq_reset(struct fq *fq,
379557fc4a0SMichal Kazior 		     fq_skb_free_t free_func)
380557fc4a0SMichal Kazior {
381557fc4a0SMichal Kazior 	int i;
382557fc4a0SMichal Kazior 
383557fc4a0SMichal Kazior 	for (i = 0; i < fq->flows_cnt; i++)
384557fc4a0SMichal Kazior 		fq_flow_reset(fq, &fq->flows[i], free_func);
385557fc4a0SMichal Kazior 
38671e67c3bSToke Høiland-Jørgensen 	kvfree(fq->flows);
387557fc4a0SMichal Kazior 	fq->flows = NULL;
388d7b64929SFelix Fietkau 
3892b8bf3d6SChristophe JAILLET 	bitmap_free(fq->flows_bitmap);
390d7b64929SFelix Fietkau 	fq->flows_bitmap = NULL;
391557fc4a0SMichal Kazior }
392557fc4a0SMichal Kazior 
393557fc4a0SMichal Kazior #endif
394