Linux Audio

Check our new training course

Loading...
v6.13.7
  1/* SPDX-License-Identifier: GPL-2.0-only */
  2/*
  3 * Copyright (c) 2016 Qualcomm Atheros, Inc
  4 *
 
 
  5 * Based on net/sched/sch_fq_codel.c
  6 */
  7#ifndef __NET_SCHED_FQ_IMPL_H
  8#define __NET_SCHED_FQ_IMPL_H
  9
 10#include <net/fq.h>
 11
 12/* functions that are embedded into includer */
 13
 14
 15static void
 16__fq_adjust_removal(struct fq *fq, struct fq_flow *flow, unsigned int packets,
 17		    unsigned int bytes, unsigned int truesize)
 18{
 19	struct fq_tin *tin = flow->tin;
 20	int idx;
 21
 22	tin->backlog_bytes -= bytes;
 23	tin->backlog_packets -= packets;
 24	flow->backlog -= bytes;
 25	fq->backlog -= packets;
 26	fq->memory_usage -= truesize;
 27
 28	if (flow->backlog)
 29		return;
 30
 31	if (flow == &tin->default_flow) {
 32		list_del_init(&tin->tin_list);
 33		return;
 34	}
 35
 36	idx = flow - fq->flows;
 37	__clear_bit(idx, fq->flows_bitmap);
 
 
 
 38}
 39
 40static void fq_adjust_removal(struct fq *fq,
 41			      struct fq_flow *flow,
 42			      struct sk_buff *skb)
 43{
 44	__fq_adjust_removal(fq, flow, 1, skb->len, skb->truesize);
 
 
 
 
 
 
 
 
 
 
 
 
 
 45}
 46
 47static struct sk_buff *fq_flow_dequeue(struct fq *fq,
 48				       struct fq_flow *flow)
 49{
 50	struct sk_buff *skb;
 51
 52	lockdep_assert_held(&fq->lock);
 53
 54	skb = __skb_dequeue(&flow->queue);
 55	if (!skb)
 56		return NULL;
 57
 58	fq_adjust_removal(fq, flow, skb);
 
 59
 60	return skb;
 61}
 62
 63static int fq_flow_drop(struct fq *fq, struct fq_flow *flow,
 64			fq_skb_free_t free_func)
 65{
 66	unsigned int packets = 0, bytes = 0, truesize = 0;
 67	struct fq_tin *tin = flow->tin;
 68	struct sk_buff *skb;
 69	int pending;
 70
 71	lockdep_assert_held(&fq->lock);
 72
 73	pending = min_t(int, 32, skb_queue_len(&flow->queue) / 2);
 74	do {
 75		skb = __skb_dequeue(&flow->queue);
 76		if (!skb)
 77			break;
 78
 79		packets++;
 80		bytes += skb->len;
 81		truesize += skb->truesize;
 82		free_func(fq, tin, flow, skb);
 83	} while (packets < pending);
 84
 85	__fq_adjust_removal(fq, flow, packets, bytes, truesize);
 86
 87	return packets;
 88}
 89
 90static struct sk_buff *fq_tin_dequeue(struct fq *fq,
 91				      struct fq_tin *tin,
 92				      fq_tin_dequeue_t dequeue_func)
 93{
 94	struct fq_flow *flow;
 95	struct list_head *head;
 96	struct sk_buff *skb;
 97
 98	lockdep_assert_held(&fq->lock);
 99
100begin:
101	head = &tin->new_flows;
102	if (list_empty(head)) {
103		head = &tin->old_flows;
104		if (list_empty(head))
105			return NULL;
106	}
107
108	flow = list_first_entry(head, struct fq_flow, flowchain);
109
110	if (flow->deficit <= 0) {
111		flow->deficit += fq->quantum;
112		list_move_tail(&flow->flowchain,
113			       &tin->old_flows);
114		goto begin;
115	}
116
117	skb = dequeue_func(fq, tin, flow);
118	if (!skb) {
119		/* force a pass through old_flows to prevent starvation */
120		if ((head == &tin->new_flows) &&
121		    !list_empty(&tin->old_flows)) {
122			list_move_tail(&flow->flowchain, &tin->old_flows);
123		} else {
124			list_del_init(&flow->flowchain);
125			flow->tin = NULL;
126		}
127		goto begin;
128	}
129
130	flow->deficit -= skb->len;
131	tin->tx_bytes += skb->len;
132	tin->tx_packets++;
133
134	return skb;
135}
136
137static u32 fq_flow_idx(struct fq *fq, struct sk_buff *skb)
138{
139	u32 hash = skb_get_hash(skb);
140
141	return reciprocal_scale(hash, fq->flows_cnt);
142}
143
144static struct fq_flow *fq_flow_classify(struct fq *fq,
145					struct fq_tin *tin, u32 idx,
146					struct sk_buff *skb)
 
147{
148	struct fq_flow *flow;
 
 
149
150	lockdep_assert_held(&fq->lock);
151
 
 
152	flow = &fq->flows[idx];
 
153	if (flow->tin && flow->tin != tin) {
154		flow = &tin->default_flow;
155		tin->collisions++;
156		fq->collisions++;
157	}
158
159	if (!flow->tin)
160		tin->flows++;
161
162	return flow;
163}
164
165static struct fq_flow *fq_find_fattest_flow(struct fq *fq)
166{
167	struct fq_tin *tin;
168	struct fq_flow *flow = NULL;
169	u32 len = 0;
170	int i;
171
172	for_each_set_bit(i, fq->flows_bitmap, fq->flows_cnt) {
173		struct fq_flow *cur = &fq->flows[i];
174		unsigned int cur_len;
175
176		cur_len = cur->backlog;
177		if (cur_len <= len)
178			continue;
179
180		flow = cur;
181		len = cur_len;
182	}
183
184	list_for_each_entry(tin, &fq->tin_backlog, tin_list) {
185		unsigned int cur_len = tin->default_flow.backlog;
186
187		if (cur_len <= len)
188			continue;
189
190		flow = &tin->default_flow;
191		len = cur_len;
192	}
193
194	return flow;
195}
196
197static void fq_tin_enqueue(struct fq *fq,
198			   struct fq_tin *tin, u32 idx,
199			   struct sk_buff *skb,
200			   fq_skb_free_t free_func)
 
201{
202	struct fq_flow *flow;
203	struct sk_buff *next;
204	bool oom;
205
206	lockdep_assert_held(&fq->lock);
207
208	flow = fq_flow_classify(fq, tin, idx, skb);
209
210	if (!flow->backlog) {
211		if (flow != &tin->default_flow)
212			__set_bit(idx, fq->flows_bitmap);
213		else if (list_empty(&tin->tin_list))
214			list_add(&tin->tin_list, &fq->tin_backlog);
215	}
216
217	flow->tin = tin;
218	skb_list_walk_safe(skb, skb, next) {
219		skb_mark_not_on_list(skb);
220		flow->backlog += skb->len;
221		tin->backlog_bytes += skb->len;
222		tin->backlog_packets++;
223		fq->memory_usage += skb->truesize;
224		fq->backlog++;
225		__skb_queue_tail(&flow->queue, skb);
226	}
227
228	if (list_empty(&flow->flowchain)) {
229		flow->deficit = fq->quantum;
230		list_add_tail(&flow->flowchain,
231			      &tin->new_flows);
232	}
233
 
234	oom = (fq->memory_usage > fq->memory_limit);
235	while (fq->backlog > fq->limit || oom) {
236		flow = fq_find_fattest_flow(fq);
 
 
237		if (!flow)
238			return;
239
240		if (!fq_flow_drop(fq, flow, free_func))
 
241			return;
242
 
 
243		flow->tin->overlimit++;
244		fq->overlimit++;
245		if (oom) {
246			fq->overmemory++;
247			oom = (fq->memory_usage > fq->memory_limit);
248		}
249	}
250}
251
252static void fq_flow_filter(struct fq *fq,
253			   struct fq_flow *flow,
254			   fq_skb_filter_t filter_func,
255			   void *filter_data,
256			   fq_skb_free_t free_func)
257{
258	struct fq_tin *tin = flow->tin;
259	struct sk_buff *skb, *tmp;
260
261	lockdep_assert_held(&fq->lock);
262
263	skb_queue_walk_safe(&flow->queue, skb, tmp) {
264		if (!filter_func(fq, tin, flow, skb, filter_data))
265			continue;
266
267		__skb_unlink(skb, &flow->queue);
268		fq_adjust_removal(fq, flow, skb);
269		free_func(fq, tin, flow, skb);
270	}
 
 
271}
272
273static void fq_tin_filter(struct fq *fq,
274			  struct fq_tin *tin,
275			  fq_skb_filter_t filter_func,
276			  void *filter_data,
277			  fq_skb_free_t free_func)
278{
279	struct fq_flow *flow;
280
281	lockdep_assert_held(&fq->lock);
282
283	list_for_each_entry(flow, &tin->new_flows, flowchain)
284		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
285	list_for_each_entry(flow, &tin->old_flows, flowchain)
286		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
287}
288
289static void fq_flow_reset(struct fq *fq,
290			  struct fq_flow *flow,
291			  fq_skb_free_t free_func)
292{
293	struct fq_tin *tin = flow->tin;
294	struct sk_buff *skb;
295
296	while ((skb = fq_flow_dequeue(fq, flow)))
297		free_func(fq, tin, flow, skb);
298
299	if (!list_empty(&flow->flowchain)) {
300		list_del_init(&flow->flowchain);
301		if (list_empty(&tin->new_flows) &&
302		    list_empty(&tin->old_flows))
303			list_del_init(&tin->tin_list);
304	}
305
306	flow->tin = NULL;
307
308	WARN_ON_ONCE(flow->backlog);
309}
310
311static void fq_tin_reset(struct fq *fq,
312			 struct fq_tin *tin,
313			 fq_skb_free_t free_func)
314{
315	struct list_head *head;
316	struct fq_flow *flow;
317
318	for (;;) {
319		head = &tin->new_flows;
320		if (list_empty(head)) {
321			head = &tin->old_flows;
322			if (list_empty(head))
323				break;
324		}
325
326		flow = list_first_entry(head, struct fq_flow, flowchain);
327		fq_flow_reset(fq, flow, free_func);
328	}
329
330	WARN_ON_ONCE(!list_empty(&tin->tin_list));
331	WARN_ON_ONCE(tin->backlog_bytes);
332	WARN_ON_ONCE(tin->backlog_packets);
333}
334
335static void fq_flow_init(struct fq_flow *flow)
336{
337	INIT_LIST_HEAD(&flow->flowchain);
 
338	__skb_queue_head_init(&flow->queue);
339}
340
341static void fq_tin_init(struct fq_tin *tin)
342{
343	INIT_LIST_HEAD(&tin->new_flows);
344	INIT_LIST_HEAD(&tin->old_flows);
345	INIT_LIST_HEAD(&tin->tin_list);
346	fq_flow_init(&tin->default_flow);
347}
348
349static int fq_init(struct fq *fq, int flows_cnt)
350{
351	int i;
352
353	memset(fq, 0, sizeof(fq[0]));
 
354	spin_lock_init(&fq->lock);
355	INIT_LIST_HEAD(&fq->tin_backlog);
356	fq->flows_cnt = max_t(u32, flows_cnt, 1);
 
357	fq->quantum = 300;
358	fq->limit = 8192;
359	fq->memory_limit = 16 << 20; /* 16 MBytes */
360
361	fq->flows = kvcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
362	if (!fq->flows)
363		return -ENOMEM;
364
365	fq->flows_bitmap = bitmap_zalloc(fq->flows_cnt, GFP_KERNEL);
366	if (!fq->flows_bitmap) {
367		kvfree(fq->flows);
368		fq->flows = NULL;
369		return -ENOMEM;
370	}
371
372	for (i = 0; i < fq->flows_cnt; i++)
373		fq_flow_init(&fq->flows[i]);
374
375	return 0;
376}
377
378static void fq_reset(struct fq *fq,
379		     fq_skb_free_t free_func)
380{
381	int i;
382
383	for (i = 0; i < fq->flows_cnt; i++)
384		fq_flow_reset(fq, &fq->flows[i], free_func);
385
386	kvfree(fq->flows);
387	fq->flows = NULL;
388
389	bitmap_free(fq->flows_bitmap);
390	fq->flows_bitmap = NULL;
391}
392
393#endif
v4.17
 
  1/*
  2 * Copyright (c) 2016 Qualcomm Atheros, Inc
  3 *
  4 * GPL v2
  5 *
  6 * Based on net/sched/sch_fq_codel.c
  7 */
  8#ifndef __NET_SCHED_FQ_IMPL_H
  9#define __NET_SCHED_FQ_IMPL_H
 10
 11#include <net/fq.h>
 12
 13/* functions that are embedded into includer */
 14
 15static void fq_adjust_removal(struct fq *fq,
 16			      struct fq_flow *flow,
 17			      struct sk_buff *skb)
 
 18{
 19	struct fq_tin *tin = flow->tin;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 20
 21	tin->backlog_bytes -= skb->len;
 22	tin->backlog_packets--;
 23	flow->backlog -= skb->len;
 24	fq->backlog--;
 25	fq->memory_usage -= skb->truesize;
 26}
 27
 28static void fq_rejigger_backlog(struct fq *fq, struct fq_flow *flow)
 
 
 29{
 30	struct fq_flow *i;
 31
 32	if (flow->backlog == 0) {
 33		list_del_init(&flow->backlogchain);
 34	} else {
 35		i = flow;
 36
 37		list_for_each_entry_continue(i, &fq->backlogs, backlogchain)
 38			if (i->backlog < flow->backlog)
 39				break;
 40
 41		list_move_tail(&flow->backlogchain,
 42			       &i->backlogchain);
 43	}
 44}
 45
 46static struct sk_buff *fq_flow_dequeue(struct fq *fq,
 47				       struct fq_flow *flow)
 48{
 49	struct sk_buff *skb;
 50
 51	lockdep_assert_held(&fq->lock);
 52
 53	skb = __skb_dequeue(&flow->queue);
 54	if (!skb)
 55		return NULL;
 56
 57	fq_adjust_removal(fq, flow, skb);
 58	fq_rejigger_backlog(fq, flow);
 59
 60	return skb;
 61}
 62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 63static struct sk_buff *fq_tin_dequeue(struct fq *fq,
 64				      struct fq_tin *tin,
 65				      fq_tin_dequeue_t dequeue_func)
 66{
 67	struct fq_flow *flow;
 68	struct list_head *head;
 69	struct sk_buff *skb;
 70
 71	lockdep_assert_held(&fq->lock);
 72
 73begin:
 74	head = &tin->new_flows;
 75	if (list_empty(head)) {
 76		head = &tin->old_flows;
 77		if (list_empty(head))
 78			return NULL;
 79	}
 80
 81	flow = list_first_entry(head, struct fq_flow, flowchain);
 82
 83	if (flow->deficit <= 0) {
 84		flow->deficit += fq->quantum;
 85		list_move_tail(&flow->flowchain,
 86			       &tin->old_flows);
 87		goto begin;
 88	}
 89
 90	skb = dequeue_func(fq, tin, flow);
 91	if (!skb) {
 92		/* force a pass through old_flows to prevent starvation */
 93		if ((head == &tin->new_flows) &&
 94		    !list_empty(&tin->old_flows)) {
 95			list_move_tail(&flow->flowchain, &tin->old_flows);
 96		} else {
 97			list_del_init(&flow->flowchain);
 98			flow->tin = NULL;
 99		}
100		goto begin;
101	}
102
103	flow->deficit -= skb->len;
104	tin->tx_bytes += skb->len;
105	tin->tx_packets++;
106
107	return skb;
108}
109
 
 
 
 
 
 
 
110static struct fq_flow *fq_flow_classify(struct fq *fq,
111					struct fq_tin *tin,
112					struct sk_buff *skb,
113					fq_flow_get_default_t get_default_func)
114{
115	struct fq_flow *flow;
116	u32 hash;
117	u32 idx;
118
119	lockdep_assert_held(&fq->lock);
120
121	hash = skb_get_hash_perturb(skb, fq->perturbation);
122	idx = reciprocal_scale(hash, fq->flows_cnt);
123	flow = &fq->flows[idx];
124
125	if (flow->tin && flow->tin != tin) {
126		flow = get_default_func(fq, tin, idx, skb);
127		tin->collisions++;
128		fq->collisions++;
129	}
130
131	if (!flow->tin)
132		tin->flows++;
133
134	return flow;
135}
136
137static void fq_recalc_backlog(struct fq *fq,
138			      struct fq_tin *tin,
139			      struct fq_flow *flow)
140{
141	struct fq_flow *i;
142
143	if (list_empty(&flow->backlogchain))
144		list_add_tail(&flow->backlogchain, &fq->backlogs);
145
146	i = flow;
147	list_for_each_entry_continue_reverse(i, &fq->backlogs,
148					     backlogchain)
149		if (i->backlog > flow->backlog)
150			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
152	list_move(&flow->backlogchain, &i->backlogchain);
153}
154
155static void fq_tin_enqueue(struct fq *fq,
156			   struct fq_tin *tin,
157			   struct sk_buff *skb,
158			   fq_skb_free_t free_func,
159			   fq_flow_get_default_t get_default_func)
160{
161	struct fq_flow *flow;
 
162	bool oom;
163
164	lockdep_assert_held(&fq->lock);
165
166	flow = fq_flow_classify(fq, tin, skb, get_default_func);
 
 
 
 
 
 
 
167
168	flow->tin = tin;
169	flow->backlog += skb->len;
170	tin->backlog_bytes += skb->len;
171	tin->backlog_packets++;
172	fq->memory_usage += skb->truesize;
173	fq->backlog++;
174
175	fq_recalc_backlog(fq, tin, flow);
 
 
176
177	if (list_empty(&flow->flowchain)) {
178		flow->deficit = fq->quantum;
179		list_add_tail(&flow->flowchain,
180			      &tin->new_flows);
181	}
182
183	__skb_queue_tail(&flow->queue, skb);
184	oom = (fq->memory_usage > fq->memory_limit);
185	while (fq->backlog > fq->limit || oom) {
186		flow = list_first_entry_or_null(&fq->backlogs,
187						struct fq_flow,
188						backlogchain);
189		if (!flow)
190			return;
191
192		skb = fq_flow_dequeue(fq, flow);
193		if (!skb)
194			return;
195
196		free_func(fq, flow->tin, flow, skb);
197
198		flow->tin->overlimit++;
199		fq->overlimit++;
200		if (oom) {
201			fq->overmemory++;
202			oom = (fq->memory_usage > fq->memory_limit);
203		}
204	}
205}
206
207static void fq_flow_filter(struct fq *fq,
208			   struct fq_flow *flow,
209			   fq_skb_filter_t filter_func,
210			   void *filter_data,
211			   fq_skb_free_t free_func)
212{
213	struct fq_tin *tin = flow->tin;
214	struct sk_buff *skb, *tmp;
215
216	lockdep_assert_held(&fq->lock);
217
218	skb_queue_walk_safe(&flow->queue, skb, tmp) {
219		if (!filter_func(fq, tin, flow, skb, filter_data))
220			continue;
221
222		__skb_unlink(skb, &flow->queue);
223		fq_adjust_removal(fq, flow, skb);
224		free_func(fq, tin, flow, skb);
225	}
226
227	fq_rejigger_backlog(fq, flow);
228}
229
230static void fq_tin_filter(struct fq *fq,
231			  struct fq_tin *tin,
232			  fq_skb_filter_t filter_func,
233			  void *filter_data,
234			  fq_skb_free_t free_func)
235{
236	struct fq_flow *flow;
237
238	lockdep_assert_held(&fq->lock);
239
240	list_for_each_entry(flow, &tin->new_flows, flowchain)
241		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
242	list_for_each_entry(flow, &tin->old_flows, flowchain)
243		fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
244}
245
246static void fq_flow_reset(struct fq *fq,
247			  struct fq_flow *flow,
248			  fq_skb_free_t free_func)
249{
 
250	struct sk_buff *skb;
251
252	while ((skb = fq_flow_dequeue(fq, flow)))
253		free_func(fq, flow->tin, flow, skb);
254
255	if (!list_empty(&flow->flowchain))
256		list_del_init(&flow->flowchain);
257
258	if (!list_empty(&flow->backlogchain))
259		list_del_init(&flow->backlogchain);
 
260
261	flow->tin = NULL;
262
263	WARN_ON_ONCE(flow->backlog);
264}
265
266static void fq_tin_reset(struct fq *fq,
267			 struct fq_tin *tin,
268			 fq_skb_free_t free_func)
269{
270	struct list_head *head;
271	struct fq_flow *flow;
272
273	for (;;) {
274		head = &tin->new_flows;
275		if (list_empty(head)) {
276			head = &tin->old_flows;
277			if (list_empty(head))
278				break;
279		}
280
281		flow = list_first_entry(head, struct fq_flow, flowchain);
282		fq_flow_reset(fq, flow, free_func);
283	}
284
 
285	WARN_ON_ONCE(tin->backlog_bytes);
286	WARN_ON_ONCE(tin->backlog_packets);
287}
288
289static void fq_flow_init(struct fq_flow *flow)
290{
291	INIT_LIST_HEAD(&flow->flowchain);
292	INIT_LIST_HEAD(&flow->backlogchain);
293	__skb_queue_head_init(&flow->queue);
294}
295
296static void fq_tin_init(struct fq_tin *tin)
297{
298	INIT_LIST_HEAD(&tin->new_flows);
299	INIT_LIST_HEAD(&tin->old_flows);
 
 
300}
301
302static int fq_init(struct fq *fq, int flows_cnt)
303{
304	int i;
305
306	memset(fq, 0, sizeof(fq[0]));
307	INIT_LIST_HEAD(&fq->backlogs);
308	spin_lock_init(&fq->lock);
 
309	fq->flows_cnt = max_t(u32, flows_cnt, 1);
310	fq->perturbation = prandom_u32();
311	fq->quantum = 300;
312	fq->limit = 8192;
313	fq->memory_limit = 16 << 20; /* 16 MBytes */
314
315	fq->flows = kcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
316	if (!fq->flows)
317		return -ENOMEM;
318
 
 
 
 
 
 
 
319	for (i = 0; i < fq->flows_cnt; i++)
320		fq_flow_init(&fq->flows[i]);
321
322	return 0;
323}
324
325static void fq_reset(struct fq *fq,
326		     fq_skb_free_t free_func)
327{
328	int i;
329
330	for (i = 0; i < fq->flows_cnt; i++)
331		fq_flow_reset(fq, &fq->flows[i], free_func);
332
333	kfree(fq->flows);
334	fq->flows = NULL;
 
 
 
335}
336
337#endif