2 * Copyright (c) 2016 Qualcomm Atheros, Inc
6 * Based on net/sched/sch_fq_codel.c
8 #ifndef __NET_SCHED_FQ_IMPL_H
9 #define __NET_SCHED_FQ_IMPL_H
13 /* functions that are embedded into includer */
15 static struct sk_buff
*fq_flow_dequeue(struct fq
*fq
,
18 struct fq_tin
*tin
= flow
->tin
;
22 lockdep_assert_held(&fq
->lock
);
24 skb
= __skb_dequeue(&flow
->queue
);
28 tin
->backlog_bytes
-= skb
->len
;
29 tin
->backlog_packets
--;
30 flow
->backlog
-= skb
->len
;
32 fq
->memory_usage
-= skb
->truesize
;
34 if (flow
->backlog
== 0) {
35 list_del_init(&flow
->backlogchain
);
39 list_for_each_entry_continue(i
, &fq
->backlogs
, backlogchain
)
40 if (i
->backlog
< flow
->backlog
)
43 list_move_tail(&flow
->backlogchain
,
50 static struct sk_buff
*fq_tin_dequeue(struct fq
*fq
,
52 fq_tin_dequeue_t dequeue_func
)
55 struct list_head
*head
;
58 lockdep_assert_held(&fq
->lock
);
61 head
= &tin
->new_flows
;
62 if (list_empty(head
)) {
63 head
= &tin
->old_flows
;
68 flow
= list_first_entry(head
, struct fq_flow
, flowchain
);
70 if (flow
->deficit
<= 0) {
71 flow
->deficit
+= fq
->quantum
;
72 list_move_tail(&flow
->flowchain
,
77 skb
= dequeue_func(fq
, tin
, flow
);
79 /* force a pass through old_flows to prevent starvation */
80 if ((head
== &tin
->new_flows
) &&
81 !list_empty(&tin
->old_flows
)) {
82 list_move_tail(&flow
->flowchain
, &tin
->old_flows
);
84 list_del_init(&flow
->flowchain
);
90 flow
->deficit
-= skb
->len
;
91 tin
->tx_bytes
+= skb
->len
;
97 static struct fq_flow
*fq_flow_classify(struct fq
*fq
,
100 fq_flow_get_default_t get_default_func
)
102 struct fq_flow
*flow
;
106 lockdep_assert_held(&fq
->lock
);
108 hash
= skb_get_hash_perturb(skb
, fq
->perturbation
);
109 idx
= reciprocal_scale(hash
, fq
->flows_cnt
);
110 flow
= &fq
->flows
[idx
];
112 if (flow
->tin
&& flow
->tin
!= tin
) {
113 flow
= get_default_func(fq
, tin
, idx
, skb
);
124 static void fq_recalc_backlog(struct fq
*fq
,
126 struct fq_flow
*flow
)
130 if (list_empty(&flow
->backlogchain
))
131 list_add_tail(&flow
->backlogchain
, &fq
->backlogs
);
134 list_for_each_entry_continue_reverse(i
, &fq
->backlogs
,
136 if (i
->backlog
> flow
->backlog
)
139 list_move(&flow
->backlogchain
, &i
->backlogchain
);
142 static void fq_tin_enqueue(struct fq
*fq
,
145 fq_skb_free_t free_func
,
146 fq_flow_get_default_t get_default_func
)
148 struct fq_flow
*flow
;
150 lockdep_assert_held(&fq
->lock
);
152 flow
= fq_flow_classify(fq
, tin
, skb
, get_default_func
);
155 flow
->backlog
+= skb
->len
;
156 tin
->backlog_bytes
+= skb
->len
;
157 tin
->backlog_packets
++;
158 fq
->memory_usage
+= skb
->truesize
;
161 fq_recalc_backlog(fq
, tin
, flow
);
163 if (list_empty(&flow
->flowchain
)) {
164 flow
->deficit
= fq
->quantum
;
165 list_add_tail(&flow
->flowchain
,
169 __skb_queue_tail(&flow
->queue
, skb
);
171 if (fq
->backlog
> fq
->limit
|| fq
->memory_usage
> fq
->memory_limit
) {
172 flow
= list_first_entry_or_null(&fq
->backlogs
,
178 skb
= fq_flow_dequeue(fq
, flow
);
182 free_func(fq
, flow
->tin
, flow
, skb
);
184 flow
->tin
->overlimit
++;
186 if (fq
->memory_usage
> fq
->memory_limit
)
191 static void fq_flow_reset(struct fq
*fq
,
192 struct fq_flow
*flow
,
193 fq_skb_free_t free_func
)
197 while ((skb
= fq_flow_dequeue(fq
, flow
)))
198 free_func(fq
, flow
->tin
, flow
, skb
);
200 if (!list_empty(&flow
->flowchain
))
201 list_del_init(&flow
->flowchain
);
203 if (!list_empty(&flow
->backlogchain
))
204 list_del_init(&flow
->backlogchain
);
208 WARN_ON_ONCE(flow
->backlog
);
211 static void fq_tin_reset(struct fq
*fq
,
213 fq_skb_free_t free_func
)
215 struct list_head
*head
;
216 struct fq_flow
*flow
;
219 head
= &tin
->new_flows
;
220 if (list_empty(head
)) {
221 head
= &tin
->old_flows
;
222 if (list_empty(head
))
226 flow
= list_first_entry(head
, struct fq_flow
, flowchain
);
227 fq_flow_reset(fq
, flow
, free_func
);
230 WARN_ON_ONCE(tin
->backlog_bytes
);
231 WARN_ON_ONCE(tin
->backlog_packets
);
234 static void fq_flow_init(struct fq_flow
*flow
)
236 INIT_LIST_HEAD(&flow
->flowchain
);
237 INIT_LIST_HEAD(&flow
->backlogchain
);
238 __skb_queue_head_init(&flow
->queue
);
241 static void fq_tin_init(struct fq_tin
*tin
)
243 INIT_LIST_HEAD(&tin
->new_flows
);
244 INIT_LIST_HEAD(&tin
->old_flows
);
247 static int fq_init(struct fq
*fq
, int flows_cnt
)
251 memset(fq
, 0, sizeof(fq
[0]));
252 INIT_LIST_HEAD(&fq
->backlogs
);
253 spin_lock_init(&fq
->lock
);
254 fq
->flows_cnt
= max_t(u32
, flows_cnt
, 1);
255 fq
->perturbation
= prandom_u32();
258 fq
->memory_limit
= 16 << 20; /* 16 MBytes */
260 fq
->flows
= kcalloc(fq
->flows_cnt
, sizeof(fq
->flows
[0]), GFP_KERNEL
);
264 for (i
= 0; i
< fq
->flows_cnt
; i
++)
265 fq_flow_init(&fq
->flows
[i
]);
270 static void fq_reset(struct fq
*fq
,
271 fq_skb_free_t free_func
)
275 for (i
= 0; i
< fq
->flows_cnt
; i
++)
276 fq_flow_reset(fq
, &fq
->flows
[i
], free_func
);