1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2016 Qualcomm Atheros, Inc
5 * Based on net/sched/sch_fq_codel.c
7 #ifndef __NET_SCHED_FQ_IMPL_H
8 #define __NET_SCHED_FQ_IMPL_H
12 /* functions that are embedded into includer */
16 __fq_adjust_removal(struct fq *fq, struct fq_flow *flow, unsigned int packets,
17 unsigned int bytes, unsigned int truesize)
19 struct fq_tin *tin = flow->tin;
22 tin->backlog_bytes -= bytes;
23 tin->backlog_packets -= packets;
24 flow->backlog -= bytes;
25 fq->backlog -= packets;
26 fq->memory_usage -= truesize;
31 if (flow == &tin->default_flow) {
32 list_del_init(&tin->tin_list);
36 idx = flow - fq->flows;
37 __clear_bit(idx, fq->flows_bitmap);
40 static void fq_adjust_removal(struct fq *fq,
44 __fq_adjust_removal(fq, flow, 1, skb->len, skb->truesize);
47 static struct sk_buff *fq_flow_dequeue(struct fq *fq,
52 lockdep_assert_held(&fq->lock);
54 skb = __skb_dequeue(&flow->queue);
58 fq_adjust_removal(fq, flow, skb);
63 static int fq_flow_drop(struct fq *fq, struct fq_flow *flow,
64 fq_skb_free_t free_func)
66 unsigned int packets = 0, bytes = 0, truesize = 0;
67 struct fq_tin *tin = flow->tin;
71 lockdep_assert_held(&fq->lock);
73 pending = min_t(int, 32, skb_queue_len(&flow->queue) / 2);
75 skb = __skb_dequeue(&flow->queue);
81 truesize += skb->truesize;
82 free_func(fq, tin, flow, skb);
83 } while (packets < pending);
85 __fq_adjust_removal(fq, flow, packets, bytes, truesize);
90 static struct sk_buff *fq_tin_dequeue(struct fq *fq,
92 fq_tin_dequeue_t dequeue_func)
95 struct list_head *head;
98 lockdep_assert_held(&fq->lock);
101 head = &tin->new_flows;
102 if (list_empty(head)) {
103 head = &tin->old_flows;
104 if (list_empty(head))
108 flow = list_first_entry(head, struct fq_flow, flowchain);
110 if (flow->deficit <= 0) {
111 flow->deficit += fq->quantum;
112 list_move_tail(&flow->flowchain,
117 skb = dequeue_func(fq, tin, flow);
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);
124 list_del_init(&flow->flowchain);
130 flow->deficit -= skb->len;
131 tin->tx_bytes += skb->len;
137 static u32 fq_flow_idx(struct fq *fq, struct sk_buff *skb)
139 u32 hash = skb_get_hash(skb);
141 return reciprocal_scale(hash, fq->flows_cnt);
144 static struct fq_flow *fq_flow_classify(struct fq *fq,
145 struct fq_tin *tin, u32 idx,
148 struct fq_flow *flow;
150 lockdep_assert_held(&fq->lock);
152 flow = &fq->flows[idx];
153 if (flow->tin && flow->tin != tin) {
154 flow = &tin->default_flow;
165 static struct fq_flow *fq_find_fattest_flow(struct fq *fq)
168 struct fq_flow *flow = NULL;
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;
176 cur_len = cur->backlog;
184 list_for_each_entry(tin, &fq->tin_backlog, tin_list) {
185 unsigned int cur_len = tin->default_flow.backlog;
190 flow = &tin->default_flow;
197 static void fq_tin_enqueue(struct fq *fq,
198 struct fq_tin *tin, u32 idx,
200 fq_skb_free_t free_func)
202 struct fq_flow *flow;
205 lockdep_assert_held(&fq->lock);
207 flow = fq_flow_classify(fq, tin, idx, skb);
209 if (!flow->backlog) {
210 if (flow != &tin->default_flow)
211 __set_bit(idx, fq->flows_bitmap);
212 else if (list_empty(&tin->tin_list))
213 list_add(&tin->tin_list, &fq->tin_backlog);
217 flow->backlog += skb->len;
218 tin->backlog_bytes += skb->len;
219 tin->backlog_packets++;
220 fq->memory_usage += skb->truesize;
223 if (list_empty(&flow->flowchain)) {
224 flow->deficit = fq->quantum;
225 list_add_tail(&flow->flowchain,
229 __skb_queue_tail(&flow->queue, skb);
230 oom = (fq->memory_usage > fq->memory_limit);
231 while (fq->backlog > fq->limit || oom) {
232 flow = fq_find_fattest_flow(fq);
236 if (!fq_flow_drop(fq, flow, free_func))
239 flow->tin->overlimit++;
243 oom = (fq->memory_usage > fq->memory_limit);
248 static void fq_flow_filter(struct fq *fq,
249 struct fq_flow *flow,
250 fq_skb_filter_t filter_func,
252 fq_skb_free_t free_func)
254 struct fq_tin *tin = flow->tin;
255 struct sk_buff *skb, *tmp;
257 lockdep_assert_held(&fq->lock);
259 skb_queue_walk_safe(&flow->queue, skb, tmp) {
260 if (!filter_func(fq, tin, flow, skb, filter_data))
263 __skb_unlink(skb, &flow->queue);
264 fq_adjust_removal(fq, flow, skb);
265 free_func(fq, tin, flow, skb);
269 static void fq_tin_filter(struct fq *fq,
271 fq_skb_filter_t filter_func,
273 fq_skb_free_t free_func)
275 struct fq_flow *flow;
277 lockdep_assert_held(&fq->lock);
279 list_for_each_entry(flow, &tin->new_flows, flowchain)
280 fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
281 list_for_each_entry(flow, &tin->old_flows, flowchain)
282 fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
285 static void fq_flow_reset(struct fq *fq,
286 struct fq_flow *flow,
287 fq_skb_free_t free_func)
289 struct fq_tin *tin = flow->tin;
292 while ((skb = fq_flow_dequeue(fq, flow)))
293 free_func(fq, tin, flow, skb);
295 if (!list_empty(&flow->flowchain)) {
296 list_del_init(&flow->flowchain);
297 if (list_empty(&tin->new_flows) &&
298 list_empty(&tin->old_flows))
299 list_del_init(&tin->tin_list);
304 WARN_ON_ONCE(flow->backlog);
307 static void fq_tin_reset(struct fq *fq,
309 fq_skb_free_t free_func)
311 struct list_head *head;
312 struct fq_flow *flow;
315 head = &tin->new_flows;
316 if (list_empty(head)) {
317 head = &tin->old_flows;
318 if (list_empty(head))
322 flow = list_first_entry(head, struct fq_flow, flowchain);
323 fq_flow_reset(fq, flow, free_func);
326 WARN_ON_ONCE(!list_empty(&tin->tin_list));
327 WARN_ON_ONCE(tin->backlog_bytes);
328 WARN_ON_ONCE(tin->backlog_packets);
331 static void fq_flow_init(struct fq_flow *flow)
333 INIT_LIST_HEAD(&flow->flowchain);
334 __skb_queue_head_init(&flow->queue);
337 static void fq_tin_init(struct fq_tin *tin)
339 INIT_LIST_HEAD(&tin->new_flows);
340 INIT_LIST_HEAD(&tin->old_flows);
341 INIT_LIST_HEAD(&tin->tin_list);
342 fq_flow_init(&tin->default_flow);
345 static int fq_init(struct fq *fq, int flows_cnt)
349 memset(fq, 0, sizeof(fq[0]));
350 spin_lock_init(&fq->lock);
351 INIT_LIST_HEAD(&fq->tin_backlog);
352 fq->flows_cnt = max_t(u32, flows_cnt, 1);
355 fq->memory_limit = 16 << 20; /* 16 MBytes */
357 fq->flows = kvcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
361 fq->flows_bitmap = kcalloc(BITS_TO_LONGS(fq->flows_cnt), sizeof(long),
363 if (!fq->flows_bitmap) {
369 for (i = 0; i < fq->flows_cnt; i++)
370 fq_flow_init(&fq->flows[i]);
375 static void fq_reset(struct fq *fq,
376 fq_skb_free_t free_func)
380 for (i = 0; i < fq->flows_cnt; i++)
381 fq_flow_reset(fq, &fq->flows[i], free_func);
386 kfree(fq->flows_bitmap);
387 fq->flows_bitmap = NULL;