Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/sch_generic.c Generic packet scheduler routines. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * Jamal Hadi Salim, <hadi@cyberus.ca> 990601 | |
11 | * - Ingress support | |
12 | */ | |
13 | ||
1da177e4 | 14 | #include <linux/bitops.h> |
1da177e4 LT |
15 | #include <linux/module.h> |
16 | #include <linux/types.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/string.h> | |
1da177e4 | 20 | #include <linux/errno.h> |
1da177e4 LT |
21 | #include <linux/netdevice.h> |
22 | #include <linux/skbuff.h> | |
23 | #include <linux/rtnetlink.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/rcupdate.h> | |
26 | #include <linux/list.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
07ce76aa | 28 | #include <linux/if_vlan.h> |
292f1c7f | 29 | #include <net/sch_generic.h> |
1da177e4 | 30 | #include <net/pkt_sched.h> |
7fee226a | 31 | #include <net/dst.h> |
1da177e4 | 32 | |
34aedd3f | 33 | /* Qdisc to use by default */ |
34 | const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops; | |
35 | EXPORT_SYMBOL(default_qdisc_ops); | |
36 | ||
1da177e4 LT |
37 | /* Main transmission queue. */ |
38 | ||
0463d4ae | 39 | /* Modifications to data participating in scheduling must be protected with |
5fb66229 | 40 | * qdisc_lock(qdisc) spinlock. |
0463d4ae PM |
41 | * |
42 | * The idea is the following: | |
c7e4f3bb DM |
43 | * - enqueue, dequeue are serialized via qdisc root lock |
44 | * - ingress filtering is also serialized via qdisc root lock | |
0463d4ae | 45 | * - updates to tree and tree walking are only done under the rtnl mutex. |
1da177e4 | 46 | */ |
1da177e4 | 47 | |
37437bb2 | 48 | static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q) |
c716a81a | 49 | { |
6252352d | 50 | q->gso_skb = skb; |
53e91503 | 51 | q->qstats.requeues++; |
bbd8a0d3 | 52 | q->q.qlen++; /* it's still part of the queue */ |
37437bb2 | 53 | __netif_schedule(q); |
6252352d | 54 | |
c716a81a JHS |
55 | return 0; |
56 | } | |
57 | ||
55a93b3e ED |
58 | static void try_bulk_dequeue_skb(struct Qdisc *q, |
59 | struct sk_buff *skb, | |
b8358d70 JDB |
60 | const struct netdev_queue *txq, |
61 | int *packets) | |
5772e9a3 | 62 | { |
55a93b3e | 63 | int bytelimit = qdisc_avail_bulklimit(txq) - skb->len; |
5772e9a3 JDB |
64 | |
65 | while (bytelimit > 0) { | |
55a93b3e | 66 | struct sk_buff *nskb = q->dequeue(q); |
5772e9a3 | 67 | |
55a93b3e | 68 | if (!nskb) |
5772e9a3 JDB |
69 | break; |
70 | ||
55a93b3e ED |
71 | bytelimit -= nskb->len; /* covers GSO len */ |
72 | skb->next = nskb; | |
73 | skb = nskb; | |
b8358d70 | 74 | (*packets)++; /* GSO counts as one pkt */ |
5772e9a3 | 75 | } |
55a93b3e | 76 | skb->next = NULL; |
5772e9a3 JDB |
77 | } |
78 | ||
79 | /* Note that dequeue_skb can possibly return a SKB list (via skb->next). | |
80 | * A requeued skb (via q->gso_skb) can also be a SKB list. | |
81 | */ | |
b8358d70 JDB |
82 | static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate, |
83 | int *packets) | |
c716a81a | 84 | { |
554794de | 85 | struct sk_buff *skb = q->gso_skb; |
1abbe139 | 86 | const struct netdev_queue *txq = q->dev_queue; |
554794de | 87 | |
b8358d70 | 88 | *packets = 1; |
55a93b3e | 89 | *validate = true; |
ebf05982 | 90 | if (unlikely(skb)) { |
ebf05982 | 91 | /* check the reason of requeuing without tx lock first */ |
10c51b56 | 92 | txq = skb_get_tx_queue(txq->dev, skb); |
73466498 | 93 | if (!netif_xmit_frozen_or_stopped(txq)) { |
6252352d | 94 | q->gso_skb = NULL; |
bbd8a0d3 KK |
95 | q->q.qlen--; |
96 | } else | |
ebf05982 | 97 | skb = NULL; |
55a93b3e ED |
98 | /* skb in gso_skb were already validated */ |
99 | *validate = false; | |
ebf05982 | 100 | } else { |
5772e9a3 JDB |
101 | if (!(q->flags & TCQ_F_ONETXQUEUE) || |
102 | !netif_xmit_frozen_or_stopped(txq)) { | |
1abbe139 | 103 | skb = q->dequeue(q); |
5772e9a3 | 104 | if (skb && qdisc_may_bulk(q)) |
b8358d70 | 105 | try_bulk_dequeue_skb(q, skb, txq, packets); |
50cbe9ab | 106 | } |
ebf05982 | 107 | } |
c716a81a JHS |
108 | return skb; |
109 | } | |
110 | ||
10297b99 | 111 | /* |
10770bc2 | 112 | * Transmit possibly several skbs, and handle the return status as |
f9eb8aea | 113 | * required. Owning running seqcount bit guarantees that |
10770bc2 | 114 | * only one CPU can execute this function. |
6c1361a6 KK |
115 | * |
116 | * Returns to the caller: | |
117 | * 0 - queue is empty or throttled. | |
118 | * >0 - queue is not empty. | |
6c1361a6 | 119 | */ |
bbd8a0d3 KK |
120 | int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q, |
121 | struct net_device *dev, struct netdev_queue *txq, | |
55a93b3e | 122 | spinlock_t *root_lock, bool validate) |
1da177e4 | 123 | { |
5f1a485d | 124 | int ret = NETDEV_TX_BUSY; |
7698b4fc DM |
125 | |
126 | /* And release qdisc */ | |
127 | spin_unlock(root_lock); | |
c716a81a | 128 | |
55a93b3e ED |
129 | /* Note that we validate skb (GSO, checksum, ...) outside of locks */ |
130 | if (validate) | |
131 | skb = validate_xmit_skb_list(skb, dev); | |
572a9d7b | 132 | |
3dcd493f | 133 | if (likely(skb)) { |
55a93b3e ED |
134 | HARD_TX_LOCK(dev, txq, smp_processor_id()); |
135 | if (!netif_xmit_frozen_or_stopped(txq)) | |
136 | skb = dev_hard_start_xmit(skb, dev, txq, &ret); | |
c716a81a | 137 | |
55a93b3e | 138 | HARD_TX_UNLOCK(dev, txq); |
3dcd493f | 139 | } else { |
f9eb8aea | 140 | spin_lock_nested(root_lock, SINGLE_DEPTH_NESTING); |
3dcd493f | 141 | return qdisc_qlen(q); |
55a93b3e | 142 | } |
f9eb8aea | 143 | spin_lock_nested(root_lock, SINGLE_DEPTH_NESTING); |
c716a81a | 144 | |
9a1654ba JP |
145 | if (dev_xmit_complete(ret)) { |
146 | /* Driver sent out skb successfully or skb was consumed */ | |
6c1361a6 | 147 | ret = qdisc_qlen(q); |
9a1654ba | 148 | } else { |
6c1361a6 | 149 | /* Driver returned NETDEV_TX_BUSY - requeue skb */ |
e87cc472 JP |
150 | if (unlikely(ret != NETDEV_TX_BUSY)) |
151 | net_warn_ratelimited("BUG %s code %d qlen %d\n", | |
152 | dev->name, ret, q->q.qlen); | |
6c1361a6 | 153 | |
37437bb2 | 154 | ret = dev_requeue_skb(skb, q); |
6c1361a6 | 155 | } |
c716a81a | 156 | |
73466498 | 157 | if (ret && netif_xmit_frozen_or_stopped(txq)) |
37437bb2 DM |
158 | ret = 0; |
159 | ||
6c1361a6 | 160 | return ret; |
1da177e4 LT |
161 | } |
162 | ||
bbd8a0d3 KK |
163 | /* |
164 | * NOTE: Called under qdisc_lock(q) with locally disabled BH. | |
165 | * | |
f9eb8aea | 166 | * running seqcount guarantees only one CPU can process |
bbd8a0d3 KK |
167 | * this qdisc at a time. qdisc_lock(q) serializes queue accesses for |
168 | * this queue. | |
169 | * | |
170 | * netif_tx_lock serializes accesses to device driver. | |
171 | * | |
172 | * qdisc_lock(q) and netif_tx_lock are mutually exclusive, | |
173 | * if one is grabbed, another must be free. | |
174 | * | |
175 | * Note, that this procedure can be called by a watchdog timer | |
176 | * | |
177 | * Returns to the caller: | |
178 | * 0 - queue is empty or throttled. | |
179 | * >0 - queue is not empty. | |
180 | * | |
181 | */ | |
b8358d70 | 182 | static inline int qdisc_restart(struct Qdisc *q, int *packets) |
bbd8a0d3 KK |
183 | { |
184 | struct netdev_queue *txq; | |
185 | struct net_device *dev; | |
186 | spinlock_t *root_lock; | |
187 | struct sk_buff *skb; | |
55a93b3e | 188 | bool validate; |
bbd8a0d3 KK |
189 | |
190 | /* Dequeue packet */ | |
b8358d70 | 191 | skb = dequeue_skb(q, &validate, packets); |
bbd8a0d3 KK |
192 | if (unlikely(!skb)) |
193 | return 0; | |
10c51b56 | 194 | |
bbd8a0d3 KK |
195 | root_lock = qdisc_lock(q); |
196 | dev = qdisc_dev(q); | |
10c51b56 | 197 | txq = skb_get_tx_queue(dev, skb); |
bbd8a0d3 | 198 | |
55a93b3e | 199 | return sch_direct_xmit(skb, q, dev, txq, root_lock, validate); |
bbd8a0d3 KK |
200 | } |
201 | ||
37437bb2 | 202 | void __qdisc_run(struct Qdisc *q) |
48d83325 | 203 | { |
d5b8aa1d | 204 | int quota = weight_p; |
b8358d70 | 205 | int packets; |
2ba2506c | 206 | |
b8358d70 | 207 | while (qdisc_restart(q, &packets)) { |
2ba2506c | 208 | /* |
d5b8aa1d | 209 | * Ordered by possible occurrence: Postpone processing if |
210 | * 1. we've exceeded packet quota | |
211 | * 2. another process needs the CPU; | |
2ba2506c | 212 | */ |
b8358d70 JDB |
213 | quota -= packets; |
214 | if (quota <= 0 || need_resched()) { | |
37437bb2 | 215 | __netif_schedule(q); |
d90df3ad | 216 | break; |
2ba2506c HX |
217 | } |
218 | } | |
48d83325 | 219 | |
bc135b23 | 220 | qdisc_run_end(q); |
48d83325 HX |
221 | } |
222 | ||
9d21493b ED |
223 | unsigned long dev_trans_start(struct net_device *dev) |
224 | { | |
07ce76aa | 225 | unsigned long val, res; |
9d21493b ED |
226 | unsigned int i; |
227 | ||
07ce76aa | 228 | if (is_vlan_dev(dev)) |
229 | dev = vlan_dev_real_dev(dev); | |
9b36627a FW |
230 | res = netdev_get_tx_queue(dev, 0)->trans_start; |
231 | for (i = 1; i < dev->num_tx_queues; i++) { | |
9d21493b ED |
232 | val = netdev_get_tx_queue(dev, i)->trans_start; |
233 | if (val && time_after(val, res)) | |
234 | res = val; | |
235 | } | |
07ce76aa | 236 | |
9d21493b ED |
237 | return res; |
238 | } | |
239 | EXPORT_SYMBOL(dev_trans_start); | |
240 | ||
1da177e4 LT |
241 | static void dev_watchdog(unsigned long arg) |
242 | { | |
243 | struct net_device *dev = (struct net_device *)arg; | |
244 | ||
932ff279 | 245 | netif_tx_lock(dev); |
e8a0464c | 246 | if (!qdisc_tx_is_noop(dev)) { |
1da177e4 LT |
247 | if (netif_device_present(dev) && |
248 | netif_running(dev) && | |
249 | netif_carrier_ok(dev)) { | |
9d21493b | 250 | int some_queue_timedout = 0; |
e8a0464c | 251 | unsigned int i; |
9d21493b | 252 | unsigned long trans_start; |
e8a0464c DM |
253 | |
254 | for (i = 0; i < dev->num_tx_queues; i++) { | |
255 | struct netdev_queue *txq; | |
256 | ||
257 | txq = netdev_get_tx_queue(dev, i); | |
9b36627a | 258 | trans_start = txq->trans_start; |
73466498 | 259 | if (netif_xmit_stopped(txq) && |
9d21493b ED |
260 | time_after(jiffies, (trans_start + |
261 | dev->watchdog_timeo))) { | |
262 | some_queue_timedout = 1; | |
ccf5ff69 | 263 | txq->trans_timeout++; |
e8a0464c DM |
264 | break; |
265 | } | |
266 | } | |
338f7566 | 267 | |
9d21493b | 268 | if (some_queue_timedout) { |
9d21493b | 269 | WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n", |
3019de12 | 270 | dev->name, netdev_drivername(dev), i); |
d314774c | 271 | dev->netdev_ops->ndo_tx_timeout(dev); |
1da177e4 | 272 | } |
e8a0464c DM |
273 | if (!mod_timer(&dev->watchdog_timer, |
274 | round_jiffies(jiffies + | |
275 | dev->watchdog_timeo))) | |
1da177e4 LT |
276 | dev_hold(dev); |
277 | } | |
278 | } | |
932ff279 | 279 | netif_tx_unlock(dev); |
1da177e4 LT |
280 | |
281 | dev_put(dev); | |
282 | } | |
283 | ||
1da177e4 LT |
284 | void __netdev_watchdog_up(struct net_device *dev) |
285 | { | |
d314774c | 286 | if (dev->netdev_ops->ndo_tx_timeout) { |
1da177e4 LT |
287 | if (dev->watchdog_timeo <= 0) |
288 | dev->watchdog_timeo = 5*HZ; | |
60468d5b VP |
289 | if (!mod_timer(&dev->watchdog_timer, |
290 | round_jiffies(jiffies + dev->watchdog_timeo))) | |
1da177e4 LT |
291 | dev_hold(dev); |
292 | } | |
293 | } | |
294 | ||
295 | static void dev_watchdog_up(struct net_device *dev) | |
296 | { | |
1da177e4 | 297 | __netdev_watchdog_up(dev); |
1da177e4 LT |
298 | } |
299 | ||
300 | static void dev_watchdog_down(struct net_device *dev) | |
301 | { | |
932ff279 | 302 | netif_tx_lock_bh(dev); |
1da177e4 | 303 | if (del_timer(&dev->watchdog_timer)) |
15333061 | 304 | dev_put(dev); |
932ff279 | 305 | netif_tx_unlock_bh(dev); |
1da177e4 LT |
306 | } |
307 | ||
bea3348e SH |
308 | /** |
309 | * netif_carrier_on - set carrier | |
310 | * @dev: network device | |
311 | * | |
312 | * Device has detected that carrier. | |
313 | */ | |
0a242efc DV |
314 | void netif_carrier_on(struct net_device *dev) |
315 | { | |
bfaae0f0 | 316 | if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
b4730016 DM |
317 | if (dev->reg_state == NETREG_UNINITIALIZED) |
318 | return; | |
2d3b479d | 319 | atomic_inc(&dev->carrier_changes); |
0a242efc | 320 | linkwatch_fire_event(dev); |
bfaae0f0 JG |
321 | if (netif_running(dev)) |
322 | __netdev_watchdog_up(dev); | |
323 | } | |
0a242efc | 324 | } |
62e3ba1b | 325 | EXPORT_SYMBOL(netif_carrier_on); |
0a242efc | 326 | |
bea3348e SH |
327 | /** |
328 | * netif_carrier_off - clear carrier | |
329 | * @dev: network device | |
330 | * | |
331 | * Device has detected loss of carrier. | |
332 | */ | |
0a242efc DV |
333 | void netif_carrier_off(struct net_device *dev) |
334 | { | |
b4730016 DM |
335 | if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) { |
336 | if (dev->reg_state == NETREG_UNINITIALIZED) | |
337 | return; | |
2d3b479d | 338 | atomic_inc(&dev->carrier_changes); |
0a242efc | 339 | linkwatch_fire_event(dev); |
b4730016 | 340 | } |
0a242efc | 341 | } |
62e3ba1b | 342 | EXPORT_SYMBOL(netif_carrier_off); |
0a242efc | 343 | |
1da177e4 LT |
344 | /* "NOOP" scheduler: the best scheduler, recommended for all interfaces |
345 | under all circumstances. It is difficult to invent anything faster or | |
346 | cheaper. | |
347 | */ | |
348 | ||
82d567c2 | 349 | static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc) |
1da177e4 LT |
350 | { |
351 | kfree_skb(skb); | |
352 | return NET_XMIT_CN; | |
353 | } | |
354 | ||
82d567c2 | 355 | static struct sk_buff *noop_dequeue(struct Qdisc *qdisc) |
1da177e4 LT |
356 | { |
357 | return NULL; | |
358 | } | |
359 | ||
20fea08b | 360 | struct Qdisc_ops noop_qdisc_ops __read_mostly = { |
1da177e4 LT |
361 | .id = "noop", |
362 | .priv_size = 0, | |
363 | .enqueue = noop_enqueue, | |
364 | .dequeue = noop_dequeue, | |
99c0db26 | 365 | .peek = noop_dequeue, |
1da177e4 LT |
366 | .owner = THIS_MODULE, |
367 | }; | |
368 | ||
7698b4fc | 369 | static struct netdev_queue noop_netdev_queue = { |
7698b4fc | 370 | .qdisc = &noop_qdisc, |
9f3ffae0 | 371 | .qdisc_sleeping = &noop_qdisc, |
7698b4fc DM |
372 | }; |
373 | ||
1da177e4 LT |
374 | struct Qdisc noop_qdisc = { |
375 | .enqueue = noop_enqueue, | |
376 | .dequeue = noop_dequeue, | |
377 | .flags = TCQ_F_BUILTIN, | |
10297b99 | 378 | .ops = &noop_qdisc_ops, |
1da177e4 | 379 | .list = LIST_HEAD_INIT(noop_qdisc.list), |
83874000 | 380 | .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock), |
7698b4fc | 381 | .dev_queue = &noop_netdev_queue, |
f9eb8aea | 382 | .running = SEQCNT_ZERO(noop_qdisc.running), |
7b5edbc4 | 383 | .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock), |
1da177e4 | 384 | }; |
62e3ba1b | 385 | EXPORT_SYMBOL(noop_qdisc); |
1da177e4 | 386 | |
d66d6c31 PS |
387 | static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt) |
388 | { | |
389 | /* register_qdisc() assigns a default of noop_enqueue if unset, | |
390 | * but __dev_queue_xmit() treats noqueue only as such | |
391 | * if this is NULL - so clear it here. */ | |
392 | qdisc->enqueue = NULL; | |
393 | return 0; | |
394 | } | |
395 | ||
396 | struct Qdisc_ops noqueue_qdisc_ops __read_mostly = { | |
1da177e4 LT |
397 | .id = "noqueue", |
398 | .priv_size = 0, | |
d66d6c31 | 399 | .init = noqueue_init, |
1da177e4 LT |
400 | .enqueue = noop_enqueue, |
401 | .dequeue = noop_dequeue, | |
99c0db26 | 402 | .peek = noop_dequeue, |
1da177e4 LT |
403 | .owner = THIS_MODULE, |
404 | }; | |
405 | ||
cc7ec456 ED |
406 | static const u8 prio2band[TC_PRIO_MAX + 1] = { |
407 | 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 | |
408 | }; | |
d3678b46 DM |
409 | |
410 | /* 3-band FIFO queue: old style, but should be a bit faster than | |
411 | generic prio+fifo combination. | |
412 | */ | |
413 | ||
414 | #define PFIFO_FAST_BANDS 3 | |
415 | ||
fd3ae5e8 KK |
416 | /* |
417 | * Private data for a pfifo_fast scheduler containing: | |
418 | * - queues for the three band | |
419 | * - bitmap indicating which of the bands contain skbs | |
420 | */ | |
421 | struct pfifo_fast_priv { | |
422 | u32 bitmap; | |
423 | struct sk_buff_head q[PFIFO_FAST_BANDS]; | |
424 | }; | |
425 | ||
426 | /* | |
427 | * Convert a bitmap to the first band number where an skb is queued, where: | |
428 | * bitmap=0 means there are no skbs on any band. | |
429 | * bitmap=1 means there is an skb on band 0. | |
430 | * bitmap=7 means there are skbs on all 3 bands, etc. | |
431 | */ | |
432 | static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0}; | |
433 | ||
434 | static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv, | |
435 | int band) | |
d3678b46 | 436 | { |
fd3ae5e8 | 437 | return priv->q + band; |
d3678b46 DM |
438 | } |
439 | ||
cc7ec456 | 440 | static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc) |
321090e7 | 441 | { |
a453e068 KK |
442 | if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) { |
443 | int band = prio2band[skb->priority & TC_PRIO_MAX]; | |
444 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); | |
445 | struct sk_buff_head *list = band2list(priv, band); | |
1da177e4 | 446 | |
fd3ae5e8 | 447 | priv->bitmap |= (1 << band); |
d3678b46 | 448 | qdisc->q.qlen++; |
821d24ae | 449 | return __qdisc_enqueue_tail(skb, qdisc, list); |
d3678b46 | 450 | } |
821d24ae TG |
451 | |
452 | return qdisc_drop(skb, qdisc); | |
1da177e4 LT |
453 | } |
454 | ||
cc7ec456 | 455 | static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc) |
1da177e4 | 456 | { |
fd3ae5e8 KK |
457 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
458 | int band = bitmap2band[priv->bitmap]; | |
1da177e4 | 459 | |
fd3ae5e8 KK |
460 | if (likely(band >= 0)) { |
461 | struct sk_buff_head *list = band2list(priv, band); | |
462 | struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list); | |
463 | ||
464 | qdisc->q.qlen--; | |
465 | if (skb_queue_empty(list)) | |
466 | priv->bitmap &= ~(1 << band); | |
467 | ||
468 | return skb; | |
d3678b46 | 469 | } |
f87a9c3d | 470 | |
1da177e4 LT |
471 | return NULL; |
472 | } | |
473 | ||
cc7ec456 | 474 | static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc) |
99c0db26 | 475 | { |
fd3ae5e8 KK |
476 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
477 | int band = bitmap2band[priv->bitmap]; | |
478 | ||
479 | if (band >= 0) { | |
480 | struct sk_buff_head *list = band2list(priv, band); | |
99c0db26 | 481 | |
fd3ae5e8 | 482 | return skb_peek(list); |
99c0db26 JP |
483 | } |
484 | ||
485 | return NULL; | |
486 | } | |
487 | ||
cc7ec456 | 488 | static void pfifo_fast_reset(struct Qdisc *qdisc) |
1da177e4 | 489 | { |
d3678b46 | 490 | int prio; |
fd3ae5e8 | 491 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
d3678b46 DM |
492 | |
493 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) | |
fd3ae5e8 | 494 | __qdisc_reset_queue(qdisc, band2list(priv, prio)); |
d3678b46 | 495 | |
fd3ae5e8 | 496 | priv->bitmap = 0; |
821d24ae | 497 | qdisc->qstats.backlog = 0; |
d3678b46 | 498 | qdisc->q.qlen = 0; |
1da177e4 LT |
499 | } |
500 | ||
d3678b46 DM |
501 | static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) |
502 | { | |
503 | struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; | |
504 | ||
cc7ec456 | 505 | memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1); |
1b34ec43 DM |
506 | if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) |
507 | goto nla_put_failure; | |
d3678b46 DM |
508 | return skb->len; |
509 | ||
510 | nla_put_failure: | |
511 | return -1; | |
512 | } | |
513 | ||
514 | static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt) | |
515 | { | |
516 | int prio; | |
fd3ae5e8 | 517 | struct pfifo_fast_priv *priv = qdisc_priv(qdisc); |
d3678b46 DM |
518 | |
519 | for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) | |
ab34f648 | 520 | __skb_queue_head_init(band2list(priv, prio)); |
d3678b46 | 521 | |
23624935 ED |
522 | /* Can by-pass the queue discipline */ |
523 | qdisc->flags |= TCQ_F_CAN_BYPASS; | |
d3678b46 DM |
524 | return 0; |
525 | } | |
526 | ||
6ec1c69a | 527 | struct Qdisc_ops pfifo_fast_ops __read_mostly = { |
d3678b46 | 528 | .id = "pfifo_fast", |
fd3ae5e8 | 529 | .priv_size = sizeof(struct pfifo_fast_priv), |
d3678b46 DM |
530 | .enqueue = pfifo_fast_enqueue, |
531 | .dequeue = pfifo_fast_dequeue, | |
99c0db26 | 532 | .peek = pfifo_fast_peek, |
d3678b46 DM |
533 | .init = pfifo_fast_init, |
534 | .reset = pfifo_fast_reset, | |
535 | .dump = pfifo_fast_dump, | |
1da177e4 LT |
536 | .owner = THIS_MODULE, |
537 | }; | |
1f27cde3 | 538 | EXPORT_SYMBOL(pfifo_fast_ops); |
1da177e4 | 539 | |
23d3b8bf | 540 | static struct lock_class_key qdisc_tx_busylock; |
f9eb8aea | 541 | static struct lock_class_key qdisc_running_key; |
23d3b8bf | 542 | |
5ce2d488 | 543 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
d2a7f269 | 544 | const struct Qdisc_ops *ops) |
1da177e4 LT |
545 | { |
546 | void *p; | |
547 | struct Qdisc *sch; | |
d276055c | 548 | unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size; |
3d54b82f | 549 | int err = -ENOBUFS; |
23d3b8bf | 550 | struct net_device *dev = dev_queue->dev; |
1da177e4 | 551 | |
f2cd2d3e ED |
552 | p = kzalloc_node(size, GFP_KERNEL, |
553 | netdev_queue_numa_node_read(dev_queue)); | |
554 | ||
1da177e4 | 555 | if (!p) |
3d54b82f | 556 | goto errout; |
3d54b82f | 557 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); |
d276055c ED |
558 | /* if we got non aligned memory, ask more and do alignment ourself */ |
559 | if (sch != p) { | |
560 | kfree(p); | |
561 | p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL, | |
562 | netdev_queue_numa_node_read(dev_queue)); | |
563 | if (!p) | |
564 | goto errout; | |
565 | sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); | |
566 | sch->padded = (char *) sch - (char *) p; | |
567 | } | |
1da177e4 LT |
568 | INIT_LIST_HEAD(&sch->list); |
569 | skb_queue_head_init(&sch->q); | |
23d3b8bf | 570 | |
79640a4c | 571 | spin_lock_init(&sch->busylock); |
23d3b8bf ED |
572 | lockdep_set_class(&sch->busylock, |
573 | dev->qdisc_tx_busylock ?: &qdisc_tx_busylock); | |
574 | ||
f9eb8aea ED |
575 | seqcount_init(&sch->running); |
576 | lockdep_set_class(&sch->running, | |
577 | dev->qdisc_running_key ?: &qdisc_running_key); | |
578 | ||
1da177e4 LT |
579 | sch->ops = ops; |
580 | sch->enqueue = ops->enqueue; | |
581 | sch->dequeue = ops->dequeue; | |
bb949fbd | 582 | sch->dev_queue = dev_queue; |
23d3b8bf | 583 | dev_hold(dev); |
1da177e4 | 584 | atomic_set(&sch->refcnt, 1); |
3d54b82f TG |
585 | |
586 | return sch; | |
587 | errout: | |
01e123d7 | 588 | return ERR_PTR(err); |
3d54b82f TG |
589 | } |
590 | ||
3511c913 | 591 | struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, |
d2a7f269 | 592 | const struct Qdisc_ops *ops, |
593 | unsigned int parentid) | |
3d54b82f TG |
594 | { |
595 | struct Qdisc *sch; | |
10297b99 | 596 | |
6da7c8fc | 597 | if (!try_module_get(ops->owner)) |
598 | goto errout; | |
599 | ||
5ce2d488 | 600 | sch = qdisc_alloc(dev_queue, ops); |
3d54b82f TG |
601 | if (IS_ERR(sch)) |
602 | goto errout; | |
9f9afec4 | 603 | sch->parent = parentid; |
3d54b82f | 604 | |
1da177e4 LT |
605 | if (!ops->init || ops->init(sch, NULL) == 0) |
606 | return sch; | |
607 | ||
0fbbeb1b | 608 | qdisc_destroy(sch); |
3d54b82f | 609 | errout: |
1da177e4 LT |
610 | return NULL; |
611 | } | |
62e3ba1b | 612 | EXPORT_SYMBOL(qdisc_create_dflt); |
1da177e4 | 613 | |
5fb66229 | 614 | /* Under qdisc_lock(qdisc) and BH! */ |
1da177e4 LT |
615 | |
616 | void qdisc_reset(struct Qdisc *qdisc) | |
617 | { | |
20fea08b | 618 | const struct Qdisc_ops *ops = qdisc->ops; |
1da177e4 LT |
619 | |
620 | if (ops->reset) | |
621 | ops->reset(qdisc); | |
67305ebc | 622 | |
bbd8a0d3 | 623 | if (qdisc->gso_skb) { |
3f3c7eec | 624 | kfree_skb_list(qdisc->gso_skb); |
bbd8a0d3 KK |
625 | qdisc->gso_skb = NULL; |
626 | qdisc->q.qlen = 0; | |
627 | } | |
1da177e4 | 628 | } |
62e3ba1b | 629 | EXPORT_SYMBOL(qdisc_reset); |
1da177e4 | 630 | |
5d944c64 ED |
631 | static void qdisc_rcu_free(struct rcu_head *head) |
632 | { | |
633 | struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head); | |
634 | ||
73c20a8b | 635 | if (qdisc_is_percpu_stats(qdisc)) { |
22e0f8b9 | 636 | free_percpu(qdisc->cpu_bstats); |
73c20a8b JF |
637 | free_percpu(qdisc->cpu_qstats); |
638 | } | |
22e0f8b9 | 639 | |
5d944c64 ED |
640 | kfree((char *) qdisc - qdisc->padded); |
641 | } | |
642 | ||
1e0d5a57 | 643 | void qdisc_destroy(struct Qdisc *qdisc) |
1da177e4 | 644 | { |
8a34c5dc DM |
645 | const struct Qdisc_ops *ops = qdisc->ops; |
646 | ||
1e0d5a57 DM |
647 | if (qdisc->flags & TCQ_F_BUILTIN || |
648 | !atomic_dec_and_test(&qdisc->refcnt)) | |
649 | return; | |
650 | ||
3a682fbd | 651 | #ifdef CONFIG_NET_SCHED |
f6e0b239 JP |
652 | qdisc_list_del(qdisc); |
653 | ||
a2da570d | 654 | qdisc_put_stab(rtnl_dereference(qdisc->stab)); |
3a682fbd | 655 | #endif |
8a34c5dc DM |
656 | gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est); |
657 | if (ops->reset) | |
658 | ops->reset(qdisc); | |
659 | if (ops->destroy) | |
660 | ops->destroy(qdisc); | |
661 | ||
662 | module_put(ops->owner); | |
663 | dev_put(qdisc_dev(qdisc)); | |
664 | ||
3f3c7eec | 665 | kfree_skb_list(qdisc->gso_skb); |
5d944c64 ED |
666 | /* |
667 | * gen_estimator est_timer() might access qdisc->q.lock, | |
668 | * wait a RCU grace period before freeing qdisc. | |
669 | */ | |
670 | call_rcu(&qdisc->rcu_head, qdisc_rcu_free); | |
1da177e4 | 671 | } |
62e3ba1b | 672 | EXPORT_SYMBOL(qdisc_destroy); |
1da177e4 | 673 | |
589983cd PM |
674 | /* Attach toplevel qdisc to device queue. */ |
675 | struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, | |
676 | struct Qdisc *qdisc) | |
677 | { | |
678 | struct Qdisc *oqdisc = dev_queue->qdisc_sleeping; | |
679 | spinlock_t *root_lock; | |
680 | ||
681 | root_lock = qdisc_lock(oqdisc); | |
682 | spin_lock_bh(root_lock); | |
683 | ||
684 | /* Prune old scheduler */ | |
685 | if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) | |
686 | qdisc_reset(oqdisc); | |
687 | ||
688 | /* ... and graft new one */ | |
689 | if (qdisc == NULL) | |
690 | qdisc = &noop_qdisc; | |
691 | dev_queue->qdisc_sleeping = qdisc; | |
692 | rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc); | |
693 | ||
694 | spin_unlock_bh(root_lock); | |
695 | ||
696 | return oqdisc; | |
697 | } | |
b8970f0b | 698 | EXPORT_SYMBOL(dev_graft_qdisc); |
589983cd | 699 | |
e8a0464c DM |
700 | static void attach_one_default_qdisc(struct net_device *dev, |
701 | struct netdev_queue *dev_queue, | |
702 | void *_unused) | |
703 | { | |
3e692f21 PS |
704 | struct Qdisc *qdisc; |
705 | const struct Qdisc_ops *ops = default_qdisc_ops; | |
e8a0464c | 706 | |
3e692f21 PS |
707 | if (dev->priv_flags & IFF_NO_QUEUE) |
708 | ops = &noqueue_qdisc_ops; | |
709 | ||
710 | qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT); | |
711 | if (!qdisc) { | |
712 | netdev_info(dev, "activation failed\n"); | |
713 | return; | |
e8a0464c | 714 | } |
3e692f21 | 715 | if (!netif_is_multiqueue(dev)) |
4eaf3b84 | 716 | qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; |
e8a0464c DM |
717 | dev_queue->qdisc_sleeping = qdisc; |
718 | } | |
719 | ||
6ec1c69a DM |
720 | static void attach_default_qdiscs(struct net_device *dev) |
721 | { | |
722 | struct netdev_queue *txq; | |
723 | struct Qdisc *qdisc; | |
724 | ||
725 | txq = netdev_get_tx_queue(dev, 0); | |
726 | ||
4b469955 | 727 | if (!netif_is_multiqueue(dev) || |
4b469955 | 728 | dev->priv_flags & IFF_NO_QUEUE) { |
6ec1c69a DM |
729 | netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); |
730 | dev->qdisc = txq->qdisc_sleeping; | |
731 | atomic_inc(&dev->qdisc->refcnt); | |
732 | } else { | |
3511c913 | 733 | qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT); |
6ec1c69a | 734 | if (qdisc) { |
6ec1c69a | 735 | dev->qdisc = qdisc; |
e57a784d | 736 | qdisc->ops->attach(qdisc); |
6ec1c69a DM |
737 | } |
738 | } | |
739 | } | |
740 | ||
e8a0464c DM |
741 | static void transition_one_qdisc(struct net_device *dev, |
742 | struct netdev_queue *dev_queue, | |
743 | void *_need_watchdog) | |
744 | { | |
83874000 | 745 | struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping; |
e8a0464c DM |
746 | int *need_watchdog_p = _need_watchdog; |
747 | ||
a9312ae8 DM |
748 | if (!(new_qdisc->flags & TCQ_F_BUILTIN)) |
749 | clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state); | |
750 | ||
83874000 | 751 | rcu_assign_pointer(dev_queue->qdisc, new_qdisc); |
3e692f21 | 752 | if (need_watchdog_p) { |
9d21493b | 753 | dev_queue->trans_start = 0; |
e8a0464c | 754 | *need_watchdog_p = 1; |
9d21493b | 755 | } |
e8a0464c DM |
756 | } |
757 | ||
1da177e4 LT |
758 | void dev_activate(struct net_device *dev) |
759 | { | |
e8a0464c | 760 | int need_watchdog; |
b0e1e646 | 761 | |
1da177e4 | 762 | /* No queueing discipline is attached to device; |
6da7c8fc | 763 | * create default one for devices, which need queueing |
764 | * and noqueue_qdisc for virtual interfaces | |
1da177e4 LT |
765 | */ |
766 | ||
6ec1c69a DM |
767 | if (dev->qdisc == &noop_qdisc) |
768 | attach_default_qdiscs(dev); | |
af356afa | 769 | |
cacaddf5 TC |
770 | if (!netif_carrier_ok(dev)) |
771 | /* Delay activation until next carrier-on event */ | |
772 | return; | |
773 | ||
e8a0464c DM |
774 | need_watchdog = 0; |
775 | netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog); | |
24824a09 ED |
776 | if (dev_ingress_queue(dev)) |
777 | transition_one_qdisc(dev, dev_ingress_queue(dev), NULL); | |
e8a0464c DM |
778 | |
779 | if (need_watchdog) { | |
860e9538 | 780 | netif_trans_update(dev); |
1da177e4 LT |
781 | dev_watchdog_up(dev); |
782 | } | |
b0e1e646 | 783 | } |
b8970f0b | 784 | EXPORT_SYMBOL(dev_activate); |
b0e1e646 | 785 | |
e8a0464c DM |
786 | static void dev_deactivate_queue(struct net_device *dev, |
787 | struct netdev_queue *dev_queue, | |
788 | void *_qdisc_default) | |
b0e1e646 | 789 | { |
e8a0464c | 790 | struct Qdisc *qdisc_default = _qdisc_default; |
970565bb | 791 | struct Qdisc *qdisc; |
970565bb | 792 | |
46e5da40 | 793 | qdisc = rtnl_dereference(dev_queue->qdisc); |
b0e1e646 | 794 | if (qdisc) { |
83874000 DM |
795 | spin_lock_bh(qdisc_lock(qdisc)); |
796 | ||
a9312ae8 DM |
797 | if (!(qdisc->flags & TCQ_F_BUILTIN)) |
798 | set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); | |
799 | ||
f7a54c13 | 800 | rcu_assign_pointer(dev_queue->qdisc, qdisc_default); |
b0e1e646 | 801 | qdisc_reset(qdisc); |
d3b753db | 802 | |
83874000 | 803 | spin_unlock_bh(qdisc_lock(qdisc)); |
b0e1e646 | 804 | } |
1da177e4 LT |
805 | } |
806 | ||
4335cd2d | 807 | static bool some_qdisc_is_busy(struct net_device *dev) |
e8a0464c DM |
808 | { |
809 | unsigned int i; | |
810 | ||
811 | for (i = 0; i < dev->num_tx_queues; i++) { | |
812 | struct netdev_queue *dev_queue; | |
7698b4fc | 813 | spinlock_t *root_lock; |
e2627c8c | 814 | struct Qdisc *q; |
e8a0464c DM |
815 | int val; |
816 | ||
817 | dev_queue = netdev_get_tx_queue(dev, i); | |
b9a3b110 | 818 | q = dev_queue->qdisc_sleeping; |
5fb66229 | 819 | root_lock = qdisc_lock(q); |
e8a0464c | 820 | |
4335cd2d | 821 | spin_lock_bh(root_lock); |
e8a0464c | 822 | |
bc135b23 | 823 | val = (qdisc_is_running(q) || |
b9a3b110 | 824 | test_bit(__QDISC_STATE_SCHED, &q->state)); |
e8a0464c | 825 | |
4335cd2d | 826 | spin_unlock_bh(root_lock); |
e8a0464c DM |
827 | |
828 | if (val) | |
829 | return true; | |
830 | } | |
831 | return false; | |
832 | } | |
833 | ||
3137663d ED |
834 | /** |
835 | * dev_deactivate_many - deactivate transmissions on several devices | |
836 | * @head: list of devices to deactivate | |
837 | * | |
838 | * This function returns only when all outstanding transmissions | |
839 | * have completed, unless all devices are in dismantle phase. | |
840 | */ | |
44345724 | 841 | void dev_deactivate_many(struct list_head *head) |
1da177e4 | 842 | { |
44345724 | 843 | struct net_device *dev; |
3137663d | 844 | bool sync_needed = false; |
41a23b07 | 845 | |
5cde2829 | 846 | list_for_each_entry(dev, head, close_list) { |
44345724 OP |
847 | netdev_for_each_tx_queue(dev, dev_deactivate_queue, |
848 | &noop_qdisc); | |
849 | if (dev_ingress_queue(dev)) | |
850 | dev_deactivate_queue(dev, dev_ingress_queue(dev), | |
851 | &noop_qdisc); | |
852 | ||
853 | dev_watchdog_down(dev); | |
3137663d | 854 | sync_needed |= !dev->dismantle; |
44345724 | 855 | } |
1da177e4 | 856 | |
3137663d ED |
857 | /* Wait for outstanding qdisc-less dev_queue_xmit calls. |
858 | * This is avoided if all devices are in dismantle phase : | |
859 | * Caller will call synchronize_net() for us | |
860 | */ | |
861 | if (sync_needed) | |
862 | synchronize_net(); | |
1da177e4 | 863 | |
d4828d85 | 864 | /* Wait for outstanding qdisc_run calls. */ |
5cde2829 | 865 | list_for_each_entry(dev, head, close_list) |
44345724 OP |
866 | while (some_qdisc_is_busy(dev)) |
867 | yield(); | |
868 | } | |
869 | ||
870 | void dev_deactivate(struct net_device *dev) | |
871 | { | |
872 | LIST_HEAD(single); | |
873 | ||
5cde2829 | 874 | list_add(&dev->close_list, &single); |
44345724 | 875 | dev_deactivate_many(&single); |
5f04d506 | 876 | list_del(&single); |
1da177e4 | 877 | } |
b8970f0b | 878 | EXPORT_SYMBOL(dev_deactivate); |
1da177e4 | 879 | |
b0e1e646 DM |
880 | static void dev_init_scheduler_queue(struct net_device *dev, |
881 | struct netdev_queue *dev_queue, | |
e8a0464c | 882 | void *_qdisc) |
b0e1e646 | 883 | { |
e8a0464c DM |
884 | struct Qdisc *qdisc = _qdisc; |
885 | ||
46e5da40 | 886 | rcu_assign_pointer(dev_queue->qdisc, qdisc); |
b0e1e646 | 887 | dev_queue->qdisc_sleeping = qdisc; |
b0e1e646 DM |
888 | } |
889 | ||
1da177e4 LT |
890 | void dev_init_scheduler(struct net_device *dev) |
891 | { | |
af356afa | 892 | dev->qdisc = &noop_qdisc; |
e8a0464c | 893 | netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc); |
24824a09 ED |
894 | if (dev_ingress_queue(dev)) |
895 | dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); | |
1da177e4 | 896 | |
b24b8a24 | 897 | setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev); |
1da177e4 LT |
898 | } |
899 | ||
e8a0464c DM |
900 | static void shutdown_scheduler_queue(struct net_device *dev, |
901 | struct netdev_queue *dev_queue, | |
902 | void *_qdisc_default) | |
1da177e4 | 903 | { |
b0e1e646 | 904 | struct Qdisc *qdisc = dev_queue->qdisc_sleeping; |
e8a0464c | 905 | struct Qdisc *qdisc_default = _qdisc_default; |
b0e1e646 DM |
906 | |
907 | if (qdisc) { | |
f7a54c13 | 908 | rcu_assign_pointer(dev_queue->qdisc, qdisc_default); |
b0e1e646 | 909 | dev_queue->qdisc_sleeping = qdisc_default; |
1da177e4 | 910 | |
1da177e4 | 911 | qdisc_destroy(qdisc); |
10297b99 | 912 | } |
b0e1e646 DM |
913 | } |
914 | ||
915 | void dev_shutdown(struct net_device *dev) | |
916 | { | |
e8a0464c | 917 | netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); |
24824a09 ED |
918 | if (dev_ingress_queue(dev)) |
919 | shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); | |
af356afa PM |
920 | qdisc_destroy(dev->qdisc); |
921 | dev->qdisc = &noop_qdisc; | |
922 | ||
547b792c | 923 | WARN_ON(timer_pending(&dev->watchdog_timer)); |
1da177e4 | 924 | } |
292f1c7f | 925 | |
01cb71d2 | 926 | void psched_ratecfg_precompute(struct psched_ratecfg *r, |
3e1e3aae ED |
927 | const struct tc_ratespec *conf, |
928 | u64 rate64) | |
292f1c7f | 929 | { |
01cb71d2 ED |
930 | memset(r, 0, sizeof(*r)); |
931 | r->overhead = conf->overhead; | |
3e1e3aae | 932 | r->rate_bytes_ps = max_t(u64, conf->rate, rate64); |
8a8e3d84 | 933 | r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK); |
292f1c7f JP |
934 | r->mult = 1; |
935 | /* | |
130d3d68 ED |
936 | * The deal here is to replace a divide by a reciprocal one |
937 | * in fast path (a reciprocal divide is a multiply and a shift) | |
938 | * | |
939 | * Normal formula would be : | |
940 | * time_in_ns = (NSEC_PER_SEC * len) / rate_bps | |
941 | * | |
942 | * We compute mult/shift to use instead : | |
943 | * time_in_ns = (len * mult) >> shift; | |
944 | * | |
945 | * We try to get the highest possible mult value for accuracy, | |
946 | * but have to make sure no overflows will ever happen. | |
292f1c7f | 947 | */ |
130d3d68 ED |
948 | if (r->rate_bytes_ps > 0) { |
949 | u64 factor = NSEC_PER_SEC; | |
950 | ||
951 | for (;;) { | |
952 | r->mult = div64_u64(factor, r->rate_bytes_ps); | |
953 | if (r->mult & (1U << 31) || factor & (1ULL << 63)) | |
292f1c7f | 954 | break; |
130d3d68 ED |
955 | factor <<= 1; |
956 | r->shift++; | |
292f1c7f | 957 | } |
292f1c7f JP |
958 | } |
959 | } | |
960 | EXPORT_SYMBOL(psched_ratecfg_precompute); |