1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/sch_generic.c Generic packet scheduler routines.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601
7 * - Ingress support
8 */
9
10 #include <linux/bitops.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/init.h>
21 #include <linux/rcupdate.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/if_vlan.h>
25 #include <linux/skb_array.h>
26 #include <linux/if_macvlan.h>
27 #include <linux/bpf.h>
28 #include <trace/events/qdisc.h>
29 #include <net/sch_generic.h>
30 #include <net/pkt_sched.h>
31 #include <net/dst.h>
32 #include <net/hotdata.h>
33 #include <trace/events/net.h>
34 #include <net/xfrm.h>
35
36 /* Qdisc to use by default */
37 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
38 EXPORT_SYMBOL(default_qdisc_ops);
39
__tcf_kfree_skb_list(struct sk_buff * skb,struct Qdisc * q,struct netdev_queue * txq,struct net_device * dev)40 void __tcf_kfree_skb_list(struct sk_buff *skb, struct Qdisc *q,
41 struct netdev_queue *txq, struct net_device *dev)
42 {
43 while (skb) {
44 u32 reason = tc_skb_cb(skb)->drop_reason;
45 struct sk_buff *next = skb->next;
46 enum skb_drop_reason skb_reason;
47
48 prefetch(next);
49 /* TC classifier and qdisc share drop_reason storage.
50 * Check subsystem mask to identify qdisc drop reasons,
51 * else pass through skb_drop_reason set by TC classifier.
52 */
53 if ((reason & SKB_DROP_REASON_SUBSYS_MASK) == __QDISC_DROP_REASON) {
54 trace_qdisc_drop(q, txq, dev, skb, (enum qdisc_drop_reason)reason);
55 skb_reason = SKB_DROP_REASON_QDISC_DROP;
56 } else {
57 skb_reason = (enum skb_drop_reason)reason;
58 }
59 kfree_skb_reason(skb, skb_reason);
60 skb = next;
61 }
62 }
63 EXPORT_SYMBOL(__tcf_kfree_skb_list);
64
qdisc_maybe_clear_missed(struct Qdisc * q,const struct netdev_queue * txq)65 static void qdisc_maybe_clear_missed(struct Qdisc *q,
66 const struct netdev_queue *txq)
67 {
68 clear_bit(__QDISC_STATE_MISSED, &q->state);
69
70 /* Make sure the below netif_xmit_frozen_or_stopped()
71 * checking happens after clearing STATE_MISSED.
72 */
73 smp_mb__after_atomic();
74
75 /* Checking netif_xmit_frozen_or_stopped() again to
76 * make sure STATE_MISSED is set if the STATE_MISSED
77 * set by netif_tx_wake_queue()'s rescheduling of
78 * net_tx_action() is cleared by the above clear_bit().
79 */
80 if (!netif_xmit_frozen_or_stopped(txq))
81 set_bit(__QDISC_STATE_MISSED, &q->state);
82 else
83 set_bit(__QDISC_STATE_DRAINING, &q->state);
84 }
85
86 /* Main transmission queue. */
87
88 /* Modifications to data participating in scheduling must be protected with
89 * qdisc_lock(qdisc) spinlock.
90 *
91 * The idea is the following:
92 * - enqueue, dequeue are serialized via qdisc root lock
93 * - ingress filtering is also serialized via qdisc root lock
94 * - updates to tree and tree walking are only done under the rtnl mutex.
95 */
96
97 #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
98
__skb_dequeue_bad_txq(struct Qdisc * q)99 static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
100 {
101 const struct netdev_queue *txq = q->dev_queue;
102 spinlock_t *lock = NULL;
103 struct sk_buff *skb;
104
105 if (q->flags & TCQ_F_NOLOCK) {
106 lock = qdisc_lock(q);
107 spin_lock(lock);
108 }
109
110 skb = skb_peek(&q->skb_bad_txq);
111 if (skb) {
112 /* check the reason of requeuing without tx lock first */
113 txq = skb_get_tx_queue(txq->dev, skb);
114 if (!netif_xmit_frozen_or_stopped(txq)) {
115 skb = __skb_dequeue(&q->skb_bad_txq);
116 if (qdisc_is_percpu_stats(q)) {
117 qdisc_qstats_cpu_backlog_dec(q, skb);
118 qdisc_qstats_cpu_qlen_dec(q);
119 } else {
120 qdisc_qstats_backlog_dec(q, skb);
121 q->q.qlen--;
122 }
123 } else {
124 skb = SKB_XOFF_MAGIC;
125 qdisc_maybe_clear_missed(q, txq);
126 }
127 }
128
129 if (lock)
130 spin_unlock(lock);
131
132 return skb;
133 }
134
qdisc_dequeue_skb_bad_txq(struct Qdisc * q)135 static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
136 {
137 struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
138
139 if (unlikely(skb))
140 skb = __skb_dequeue_bad_txq(q);
141
142 return skb;
143 }
144
qdisc_enqueue_skb_bad_txq(struct Qdisc * q,struct sk_buff * skb)145 static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
146 struct sk_buff *skb)
147 {
148 spinlock_t *lock = NULL;
149
150 if (q->flags & TCQ_F_NOLOCK) {
151 lock = qdisc_lock(q);
152 spin_lock(lock);
153 }
154
155 __skb_queue_tail(&q->skb_bad_txq, skb);
156
157 if (qdisc_is_percpu_stats(q)) {
158 qdisc_qstats_cpu_backlog_inc(q, skb);
159 qdisc_qstats_cpu_qlen_inc(q);
160 } else {
161 qdisc_qstats_backlog_inc(q, skb);
162 q->q.qlen++;
163 }
164
165 if (lock)
166 spin_unlock(lock);
167 }
168
dev_requeue_skb(struct sk_buff * skb,struct Qdisc * q)169 static inline void dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
170 {
171 spinlock_t *lock = NULL;
172
173 if (q->flags & TCQ_F_NOLOCK) {
174 lock = qdisc_lock(q);
175 spin_lock(lock);
176 }
177
178 while (skb) {
179 struct sk_buff *next = skb->next;
180
181 __skb_queue_tail(&q->gso_skb, skb);
182
183 /* it's still part of the queue */
184 if (qdisc_is_percpu_stats(q)) {
185 qdisc_qstats_cpu_requeues_inc(q);
186 qdisc_qstats_cpu_backlog_inc(q, skb);
187 qdisc_qstats_cpu_qlen_inc(q);
188 } else {
189 q->qstats.requeues++;
190 qdisc_qstats_backlog_inc(q, skb);
191 q->q.qlen++;
192 }
193
194 skb = next;
195 }
196
197 if (lock) {
198 spin_unlock(lock);
199 set_bit(__QDISC_STATE_MISSED, &q->state);
200 } else {
201 __netif_schedule(q);
202 }
203 }
204
try_bulk_dequeue_skb(struct Qdisc * q,struct sk_buff * skb,const struct netdev_queue * txq,int * packets,int budget)205 static void try_bulk_dequeue_skb(struct Qdisc *q,
206 struct sk_buff *skb,
207 const struct netdev_queue *txq,
208 int *packets, int budget)
209 {
210 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
211 int cnt = 0;
212
213 while (bytelimit > 0) {
214 struct sk_buff *nskb = q->dequeue(q);
215
216 if (!nskb)
217 break;
218
219 bytelimit -= nskb->len; /* covers GSO len */
220 skb->next = nskb;
221 skb = nskb;
222 if (++cnt >= budget)
223 break;
224 }
225 (*packets) += cnt;
226 skb_mark_not_on_list(skb);
227 }
228
229 /* This variant of try_bulk_dequeue_skb() makes sure
230 * all skbs in the chain are for the same txq
231 */
try_bulk_dequeue_skb_slow(struct Qdisc * q,struct sk_buff * skb,int * packets)232 static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
233 struct sk_buff *skb,
234 int *packets)
235 {
236 int mapping = skb_get_queue_mapping(skb);
237 struct sk_buff *nskb;
238 int cnt = 0;
239
240 do {
241 nskb = q->dequeue(q);
242 if (!nskb)
243 break;
244 if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
245 qdisc_enqueue_skb_bad_txq(q, nskb);
246 break;
247 }
248 skb->next = nskb;
249 skb = nskb;
250 } while (++cnt < 8);
251 (*packets) += cnt;
252 skb_mark_not_on_list(skb);
253 }
254
255 /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
256 * A requeued skb (via q->gso_skb) can also be a SKB list.
257 */
dequeue_skb(struct Qdisc * q,bool * validate,int * packets,int budget)258 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
259 int *packets, int budget)
260 {
261 const struct netdev_queue *txq = q->dev_queue;
262 struct sk_buff *skb = NULL;
263
264 *packets = 1;
265 if (unlikely(!skb_queue_empty(&q->gso_skb))) {
266 spinlock_t *lock = NULL;
267
268 if (q->flags & TCQ_F_NOLOCK) {
269 lock = qdisc_lock(q);
270 spin_lock(lock);
271 }
272
273 skb = skb_peek(&q->gso_skb);
274
275 /* skb may be null if another cpu pulls gso_skb off in between
276 * empty check and lock.
277 */
278 if (!skb) {
279 if (lock)
280 spin_unlock(lock);
281 goto validate;
282 }
283
284 /* skb in gso_skb were already validated */
285 *validate = false;
286 if (xfrm_offload(skb))
287 *validate = true;
288 /* check the reason of requeuing without tx lock first */
289 txq = skb_get_tx_queue(txq->dev, skb);
290 if (!netif_xmit_frozen_or_stopped(txq)) {
291 skb = __skb_dequeue(&q->gso_skb);
292 if (qdisc_is_percpu_stats(q)) {
293 qdisc_qstats_cpu_backlog_dec(q, skb);
294 qdisc_qstats_cpu_qlen_dec(q);
295 } else {
296 qdisc_qstats_backlog_dec(q, skb);
297 q->q.qlen--;
298 }
299 } else {
300 skb = NULL;
301 qdisc_maybe_clear_missed(q, txq);
302 }
303 if (lock)
304 spin_unlock(lock);
305 goto trace;
306 }
307 validate:
308 *validate = true;
309
310 if ((q->flags & TCQ_F_ONETXQUEUE) &&
311 netif_xmit_frozen_or_stopped(txq)) {
312 qdisc_maybe_clear_missed(q, txq);
313 return skb;
314 }
315
316 skb = qdisc_dequeue_skb_bad_txq(q);
317 if (unlikely(skb)) {
318 if (skb == SKB_XOFF_MAGIC)
319 return NULL;
320 goto bulk;
321 }
322 skb = q->dequeue(q);
323 if (skb) {
324 bulk:
325 if (qdisc_may_bulk(q))
326 try_bulk_dequeue_skb(q, skb, txq, packets, budget);
327 else
328 try_bulk_dequeue_skb_slow(q, skb, packets);
329 }
330 trace:
331 trace_qdisc_dequeue(q, txq, *packets, skb);
332 return skb;
333 }
334
335 /*
336 * Transmit possibly several skbs, and handle the return status as
337 * required. Owning qdisc running bit guarantees that only one CPU
338 * can execute this function.
339 *
340 * Returns to the caller:
341 * false - hardware queue frozen backoff
342 * true - feel free to send more pkts
343 */
sch_direct_xmit(struct sk_buff * skb,struct Qdisc * q,struct net_device * dev,struct netdev_queue * txq,spinlock_t * root_lock,bool validate)344 bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
345 struct net_device *dev, struct netdev_queue *txq,
346 spinlock_t *root_lock, bool validate)
347 {
348 int ret = NETDEV_TX_BUSY;
349 bool again = false;
350
351 /* And release qdisc */
352 if (root_lock)
353 spin_unlock(root_lock);
354
355 /* Note that we validate skb (GSO, checksum, ...) outside of locks */
356 if (validate)
357 skb = validate_xmit_skb_list(skb, dev, &again);
358
359 #ifdef CONFIG_XFRM_OFFLOAD
360 if (unlikely(again)) {
361 if (root_lock)
362 spin_lock(root_lock);
363
364 dev_requeue_skb(skb, q);
365 return false;
366 }
367 #endif
368
369 if (likely(skb)) {
370 HARD_TX_LOCK(dev, txq, smp_processor_id());
371 if (!netif_xmit_frozen_or_stopped(txq))
372 skb = dev_hard_start_xmit(skb, dev, txq, &ret);
373 else
374 qdisc_maybe_clear_missed(q, txq);
375
376 HARD_TX_UNLOCK(dev, txq);
377 } else {
378 if (root_lock)
379 spin_lock(root_lock);
380 return true;
381 }
382
383 if (root_lock)
384 spin_lock(root_lock);
385
386 if (!dev_xmit_complete(ret)) {
387 /* Driver returned NETDEV_TX_BUSY - requeue skb */
388 if (unlikely(ret != NETDEV_TX_BUSY))
389 net_warn_ratelimited("BUG %s code %d qlen %d\n",
390 dev->name, ret, q->q.qlen);
391
392 dev_requeue_skb(skb, q);
393 return false;
394 }
395
396 return true;
397 }
398
399 /*
400 * NOTE: Called under qdisc_lock(q) with locally disabled BH.
401 *
402 * running seqcount guarantees only one CPU can process
403 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
404 * this queue.
405 *
406 * netif_tx_lock serializes accesses to device driver.
407 *
408 * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
409 * if one is grabbed, another must be free.
410 *
411 * Note, that this procedure can be called by a watchdog timer
412 *
413 * Returns to the caller:
414 * 0 - queue is empty or throttled.
415 * >0 - queue is not empty.
416 *
417 */
qdisc_restart(struct Qdisc * q,int * packets,int budget)418 static inline bool qdisc_restart(struct Qdisc *q, int *packets, int budget)
419 {
420 spinlock_t *root_lock = NULL;
421 struct netdev_queue *txq;
422 struct net_device *dev;
423 struct sk_buff *skb;
424 bool validate;
425
426 /* Dequeue packet */
427 skb = dequeue_skb(q, &validate, packets, budget);
428 if (unlikely(!skb))
429 return false;
430
431 if (!(q->flags & TCQ_F_NOLOCK))
432 root_lock = qdisc_lock(q);
433
434 dev = qdisc_dev(q);
435 txq = skb_get_tx_queue(dev, skb);
436
437 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
438 }
439
__qdisc_run(struct Qdisc * q)440 void __qdisc_run(struct Qdisc *q)
441 {
442 int quota = READ_ONCE(net_hotdata.dev_tx_weight);
443 int packets;
444
445 while (qdisc_restart(q, &packets, quota)) {
446 quota -= packets;
447 if (quota <= 0) {
448 if (q->flags & TCQ_F_NOLOCK)
449 set_bit(__QDISC_STATE_MISSED, &q->state);
450 else
451 __netif_schedule(q);
452
453 break;
454 }
455 }
456 }
457
dev_trans_start(struct net_device * dev)458 unsigned long dev_trans_start(struct net_device *dev)
459 {
460 unsigned long res = READ_ONCE(netdev_get_tx_queue(dev, 0)->trans_start);
461 unsigned long val;
462 unsigned int i;
463
464 for (i = 1; i < dev->num_tx_queues; i++) {
465 val = READ_ONCE(netdev_get_tx_queue(dev, i)->trans_start);
466 if (val && time_after(val, res))
467 res = val;
468 }
469
470 return res;
471 }
472 EXPORT_SYMBOL(dev_trans_start);
473
netif_freeze_queues(struct net_device * dev)474 static void netif_freeze_queues(struct net_device *dev)
475 {
476 unsigned int i;
477 int cpu;
478
479 cpu = smp_processor_id();
480 for (i = 0; i < dev->num_tx_queues; i++) {
481 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
482
483 /* We are the only thread of execution doing a
484 * freeze, but we have to grab the _xmit_lock in
485 * order to synchronize with threads which are in
486 * the ->hard_start_xmit() handler and already
487 * checked the frozen bit.
488 */
489 __netif_tx_lock(txq, cpu);
490 set_bit(__QUEUE_STATE_FROZEN, &txq->state);
491 __netif_tx_unlock(txq);
492 }
493 }
494
netif_tx_lock(struct net_device * dev)495 void netif_tx_lock(struct net_device *dev)
496 {
497 spin_lock(&dev->tx_global_lock);
498 netif_freeze_queues(dev);
499 }
500 EXPORT_SYMBOL(netif_tx_lock);
501
netif_unfreeze_queues(struct net_device * dev)502 static void netif_unfreeze_queues(struct net_device *dev)
503 {
504 unsigned int i;
505
506 for (i = 0; i < dev->num_tx_queues; i++) {
507 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
508
509 /* No need to grab the _xmit_lock here. If the
510 * queue is not stopped for another reason, we
511 * force a schedule.
512 */
513 clear_bit(__QUEUE_STATE_FROZEN, &txq->state);
514 netif_schedule_queue(txq);
515 }
516 }
517
netif_tx_unlock(struct net_device * dev)518 void netif_tx_unlock(struct net_device *dev)
519 {
520 netif_unfreeze_queues(dev);
521 spin_unlock(&dev->tx_global_lock);
522 }
523 EXPORT_SYMBOL(netif_tx_unlock);
524
dev_watchdog(struct timer_list * t)525 static void dev_watchdog(struct timer_list *t)
526 {
527 struct net_device *dev = timer_container_of(dev, t, watchdog_timer);
528 bool release = true;
529
530 spin_lock(&dev->tx_global_lock);
531 if (!qdisc_tx_is_noop(dev)) {
532 if (netif_device_present(dev) &&
533 netif_running(dev) &&
534 netif_carrier_ok(dev)) {
535 unsigned int timedout_ms = 0;
536 unsigned int i;
537 unsigned long trans_start;
538 unsigned long oldest_start = jiffies;
539
540 for (i = 0; i < dev->num_tx_queues; i++) {
541 struct netdev_queue *txq;
542
543 txq = netdev_get_tx_queue(dev, i);
544 if (!netif_xmit_stopped(txq))
545 continue;
546
547 /* Paired with WRITE_ONCE() + smp_mb...() in
548 * netdev_tx_sent_queue() and netif_tx_stop_queue().
549 */
550 smp_mb();
551 trans_start = READ_ONCE(txq->trans_start);
552
553 if (time_after(jiffies, trans_start + dev->watchdog_timeo)) {
554 timedout_ms = jiffies_to_msecs(jiffies - trans_start);
555 atomic_long_inc(&txq->trans_timeout);
556 break;
557 }
558 if (time_after(oldest_start, trans_start))
559 oldest_start = trans_start;
560 }
561
562 if (unlikely(timedout_ms)) {
563 trace_net_dev_xmit_timeout(dev, i);
564 netdev_crit(dev, "NETDEV WATCHDOG: CPU: %d: transmit queue %u timed out %u ms\n",
565 raw_smp_processor_id(),
566 i, timedout_ms);
567 netif_freeze_queues(dev);
568 dev->netdev_ops->ndo_tx_timeout(dev, i);
569 netif_unfreeze_queues(dev);
570 }
571 if (!mod_timer(&dev->watchdog_timer,
572 round_jiffies(oldest_start +
573 dev->watchdog_timeo)))
574 release = false;
575 }
576 }
577 spin_unlock(&dev->tx_global_lock);
578
579 if (release)
580 netdev_put(dev, &dev->watchdog_dev_tracker);
581 }
582
netdev_watchdog_up(struct net_device * dev)583 void netdev_watchdog_up(struct net_device *dev)
584 {
585 if (!dev->netdev_ops->ndo_tx_timeout)
586 return;
587 if (dev->watchdog_timeo <= 0)
588 dev->watchdog_timeo = 5*HZ;
589 if (!mod_timer(&dev->watchdog_timer,
590 round_jiffies(jiffies + dev->watchdog_timeo)))
591 netdev_hold(dev, &dev->watchdog_dev_tracker,
592 GFP_ATOMIC);
593 }
594 EXPORT_SYMBOL_GPL(netdev_watchdog_up);
595
netdev_watchdog_down(struct net_device * dev)596 static void netdev_watchdog_down(struct net_device *dev)
597 {
598 netif_tx_lock_bh(dev);
599 if (timer_delete(&dev->watchdog_timer))
600 netdev_put(dev, &dev->watchdog_dev_tracker);
601 netif_tx_unlock_bh(dev);
602 }
603
604 /**
605 * netif_carrier_on - set carrier
606 * @dev: network device
607 *
608 * Device has detected acquisition of carrier.
609 */
netif_carrier_on(struct net_device * dev)610 void netif_carrier_on(struct net_device *dev)
611 {
612 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
613 if (dev->reg_state == NETREG_UNINITIALIZED)
614 return;
615 atomic_inc(&dev->carrier_up_count);
616 linkwatch_fire_event(dev);
617 if (netif_running(dev))
618 netdev_watchdog_up(dev);
619 }
620 }
621 EXPORT_SYMBOL(netif_carrier_on);
622
623 /**
624 * netif_carrier_off - clear carrier
625 * @dev: network device
626 *
627 * Device has detected loss of carrier.
628 */
netif_carrier_off(struct net_device * dev)629 void netif_carrier_off(struct net_device *dev)
630 {
631 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
632 if (dev->reg_state == NETREG_UNINITIALIZED)
633 return;
634 atomic_inc(&dev->carrier_down_count);
635 linkwatch_fire_event(dev);
636 }
637 }
638 EXPORT_SYMBOL(netif_carrier_off);
639
640 /**
641 * netif_carrier_event - report carrier state event
642 * @dev: network device
643 *
644 * Device has detected a carrier event but the carrier state wasn't changed.
645 * Use in drivers when querying carrier state asynchronously, to avoid missing
646 * events (link flaps) if link recovers before it's queried.
647 */
netif_carrier_event(struct net_device * dev)648 void netif_carrier_event(struct net_device *dev)
649 {
650 if (dev->reg_state == NETREG_UNINITIALIZED)
651 return;
652 atomic_inc(&dev->carrier_up_count);
653 atomic_inc(&dev->carrier_down_count);
654 linkwatch_fire_event(dev);
655 }
656 EXPORT_SYMBOL_GPL(netif_carrier_event);
657
658 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
659 under all circumstances. It is difficult to invent anything faster or
660 cheaper.
661 */
662
noop_enqueue(struct sk_buff * skb,struct Qdisc * qdisc,struct sk_buff ** to_free)663 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
664 struct sk_buff **to_free)
665 {
666 dev_core_stats_tx_dropped_inc(skb->dev);
667 __qdisc_drop(skb, to_free);
668 return NET_XMIT_CN;
669 }
670
noop_dequeue(struct Qdisc * qdisc)671 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
672 {
673 return NULL;
674 }
675
676 struct Qdisc_ops noop_qdisc_ops __read_mostly = {
677 .id = "noop",
678 .priv_size = 0,
679 .enqueue = noop_enqueue,
680 .dequeue = noop_dequeue,
681 .peek = noop_dequeue,
682 .owner = THIS_MODULE,
683 };
684
685 static struct netdev_queue noop_netdev_queue = {
686 RCU_POINTER_INITIALIZER(qdisc, &noop_qdisc),
687 RCU_POINTER_INITIALIZER(qdisc_sleeping, &noop_qdisc),
688 };
689
690 struct Qdisc noop_qdisc = {
691 .enqueue = noop_enqueue,
692 .dequeue = noop_dequeue,
693 .flags = TCQ_F_BUILTIN,
694 .ops = &noop_qdisc_ops,
695 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
696 .dev_queue = &noop_netdev_queue,
697 .gso_skb = {
698 .next = (struct sk_buff *)&noop_qdisc.gso_skb,
699 .prev = (struct sk_buff *)&noop_qdisc.gso_skb,
700 .qlen = 0,
701 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock),
702 },
703 .skb_bad_txq = {
704 .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
705 .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
706 .qlen = 0,
707 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock),
708 },
709 };
710 EXPORT_SYMBOL(noop_qdisc);
711
noqueue_init(struct Qdisc * qdisc,struct nlattr * opt,struct netlink_ext_ack * extack)712 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
713 struct netlink_ext_ack *extack)
714 {
715 /* register_qdisc() assigns a default of noop_enqueue if unset,
716 * but __dev_queue_xmit() treats noqueue only as such
717 * if this is NULL - so clear it here. */
718 qdisc->enqueue = NULL;
719 return 0;
720 }
721
722 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
723 .id = "noqueue",
724 .priv_size = 0,
725 .init = noqueue_init,
726 .enqueue = noop_enqueue,
727 .dequeue = noop_dequeue,
728 .peek = noop_dequeue,
729 .owner = THIS_MODULE,
730 };
731
732 const u8 sch_default_prio2band[TC_PRIO_MAX + 1] = {
733 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
734 };
735 EXPORT_SYMBOL(sch_default_prio2band);
736
737 /* 3-band FIFO queue: old style, but should be a bit faster than
738 generic prio+fifo combination.
739 */
740
741 #define PFIFO_FAST_BANDS 3
742
743 /*
744 * Private data for a pfifo_fast scheduler containing:
745 * - rings for priority bands
746 */
747 struct pfifo_fast_priv {
748 struct skb_array q[PFIFO_FAST_BANDS];
749 };
750
band2list(struct pfifo_fast_priv * priv,int band)751 static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
752 int band)
753 {
754 return &priv->q[band];
755 }
756
pfifo_fast_enqueue(struct sk_buff * skb,struct Qdisc * qdisc,struct sk_buff ** to_free)757 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
758 struct sk_buff **to_free)
759 {
760 int band = sch_default_prio2band[skb->priority & TC_PRIO_MAX];
761 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
762 struct skb_array *q = band2list(priv, band);
763 unsigned int pkt_len = qdisc_pkt_len(skb);
764 int err;
765
766 err = skb_array_produce(q, skb);
767
768 if (unlikely(err)) {
769 tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT);
770
771 if (qdisc_is_percpu_stats(qdisc))
772 return qdisc_drop_cpu(skb, qdisc, to_free);
773 else
774 return qdisc_drop(skb, qdisc, to_free);
775 }
776
777 qdisc_update_stats_at_enqueue(qdisc, pkt_len);
778 return NET_XMIT_SUCCESS;
779 }
780
pfifo_fast_dequeue(struct Qdisc * qdisc)781 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
782 {
783 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
784 struct sk_buff *skb = NULL;
785 bool need_retry = true;
786 int band;
787
788 retry:
789 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
790 struct skb_array *q = band2list(priv, band);
791
792 if (__skb_array_empty(q))
793 continue;
794
795 skb = __skb_array_consume(q);
796 }
797 if (likely(skb)) {
798 qdisc_update_stats_at_dequeue(qdisc, skb);
799 } else if (need_retry &&
800 READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY) {
801 /* Delay clearing the STATE_MISSED here to reduce
802 * the overhead of the second spin_trylock() in
803 * qdisc_run_begin() and __netif_schedule() calling
804 * in qdisc_run_end().
805 */
806 clear_bit(__QDISC_STATE_MISSED, &qdisc->state);
807 clear_bit(__QDISC_STATE_DRAINING, &qdisc->state);
808
809 /* Make sure dequeuing happens after clearing
810 * STATE_MISSED.
811 */
812 smp_mb__after_atomic();
813
814 need_retry = false;
815
816 goto retry;
817 }
818
819 return skb;
820 }
821
pfifo_fast_peek(struct Qdisc * qdisc)822 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
823 {
824 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
825 struct sk_buff *skb = NULL;
826 int band;
827
828 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
829 struct skb_array *q = band2list(priv, band);
830
831 skb = __skb_array_peek(q);
832 }
833
834 return skb;
835 }
836
pfifo_fast_reset(struct Qdisc * qdisc)837 static void pfifo_fast_reset(struct Qdisc *qdisc)
838 {
839 int i, band;
840 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
841
842 for (band = 0; band < PFIFO_FAST_BANDS; band++) {
843 struct skb_array *q = band2list(priv, band);
844 struct sk_buff *skb;
845
846 /* NULL ring is possible if destroy path is due to a failed
847 * skb_array_init() in pfifo_fast_init() case.
848 */
849 if (!q->ring.queue)
850 continue;
851
852 while ((skb = __skb_array_consume(q)) != NULL)
853 rtnl_kfree_skbs(skb, skb);
854 }
855
856 if (qdisc_is_percpu_stats(qdisc)) {
857 for_each_possible_cpu(i) {
858 struct gnet_stats_queue *q;
859
860 q = per_cpu_ptr(qdisc->cpu_qstats, i);
861 q->backlog = 0;
862 q->qlen = 0;
863 }
864 }
865 }
866
pfifo_fast_dump(struct Qdisc * qdisc,struct sk_buff * skb)867 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
868 {
869 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
870
871 memcpy(&opt.priomap, sch_default_prio2band, TC_PRIO_MAX + 1);
872 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
873 goto nla_put_failure;
874 return skb->len;
875
876 nla_put_failure:
877 return -1;
878 }
879
pfifo_fast_init(struct Qdisc * qdisc,struct nlattr * opt,struct netlink_ext_ack * extack)880 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
881 struct netlink_ext_ack *extack)
882 {
883 unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
884 struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
885 int prio;
886
887 /* guard against zero length rings */
888 if (!qlen)
889 return -EINVAL;
890
891 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
892 struct skb_array *q = band2list(priv, prio);
893 int err;
894
895 err = skb_array_init(q, qlen, GFP_KERNEL);
896 if (err)
897 return -ENOMEM;
898 }
899
900 /* Can by-pass the queue discipline */
901 qdisc->flags |= TCQ_F_CAN_BYPASS;
902 return 0;
903 }
904
pfifo_fast_destroy(struct Qdisc * sch)905 static void pfifo_fast_destroy(struct Qdisc *sch)
906 {
907 struct pfifo_fast_priv *priv = qdisc_priv(sch);
908 int prio;
909
910 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
911 struct skb_array *q = band2list(priv, prio);
912
913 /* NULL ring is possible if destroy path is due to a failed
914 * skb_array_init() in pfifo_fast_init() case.
915 */
916 if (!q->ring.queue)
917 continue;
918 /* Destroy ring but no need to kfree_skb because a call to
919 * pfifo_fast_reset() has already done that work.
920 */
921 ptr_ring_cleanup(&q->ring, NULL);
922 }
923 }
924
pfifo_fast_change_tx_queue_len(struct Qdisc * sch,unsigned int new_len)925 static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
926 unsigned int new_len)
927 {
928 struct pfifo_fast_priv *priv = qdisc_priv(sch);
929 struct skb_array *bands[PFIFO_FAST_BANDS];
930 int prio;
931
932 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
933 struct skb_array *q = band2list(priv, prio);
934
935 bands[prio] = q;
936 }
937
938 return skb_array_resize_multiple_bh(bands, PFIFO_FAST_BANDS, new_len,
939 GFP_KERNEL);
940 }
941
942 struct Qdisc_ops pfifo_fast_ops __read_mostly = {
943 .id = "pfifo_fast",
944 .priv_size = sizeof(struct pfifo_fast_priv),
945 .enqueue = pfifo_fast_enqueue,
946 .dequeue = pfifo_fast_dequeue,
947 .peek = pfifo_fast_peek,
948 .init = pfifo_fast_init,
949 .destroy = pfifo_fast_destroy,
950 .reset = pfifo_fast_reset,
951 .dump = pfifo_fast_dump,
952 .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
953 .owner = THIS_MODULE,
954 .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
955 };
956 EXPORT_SYMBOL(pfifo_fast_ops);
957
958 static struct lock_class_key qdisc_tx_busylock;
959
qdisc_alloc(struct netdev_queue * dev_queue,const struct Qdisc_ops * ops,struct netlink_ext_ack * extack)960 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
961 const struct Qdisc_ops *ops,
962 struct netlink_ext_ack *extack)
963 {
964 struct Qdisc *sch;
965 unsigned int size = sizeof(*sch) + ops->priv_size;
966 int err = -ENOBUFS;
967 struct net_device *dev;
968
969 if (!dev_queue) {
970 NL_SET_ERR_MSG(extack, "No device queue given");
971 err = -EINVAL;
972 goto errout;
973 }
974
975 dev = dev_queue->dev;
976 sch = kzalloc_node(size, GFP_KERNEL, netdev_queue_numa_node_read(dev_queue));
977
978 if (!sch)
979 goto errout;
980 __skb_queue_head_init(&sch->gso_skb);
981 __skb_queue_head_init(&sch->skb_bad_txq);
982 gnet_stats_basic_sync_init(&sch->bstats);
983 qdisc_lock_init(sch, ops);
984
985 if (ops->static_flags & TCQ_F_CPUSTATS) {
986 sch->cpu_bstats =
987 netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync);
988 if (!sch->cpu_bstats)
989 goto errout1;
990
991 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
992 if (!sch->cpu_qstats) {
993 free_percpu(sch->cpu_bstats);
994 goto errout1;
995 }
996 }
997
998 /* seqlock has the same scope of busylock, for NOLOCK qdisc */
999 spin_lock_init(&sch->seqlock);
1000 lockdep_set_class(&sch->seqlock,
1001 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
1002
1003 sch->ops = ops;
1004 sch->flags = ops->static_flags;
1005 sch->enqueue = ops->enqueue;
1006 sch->dequeue = ops->dequeue;
1007 sch->dev_queue = dev_queue;
1008 netdev_hold(dev, &sch->dev_tracker, GFP_KERNEL);
1009 refcount_set(&sch->refcnt, 1);
1010
1011 return sch;
1012 errout1:
1013 qdisc_lock_uninit(sch, ops);
1014 kfree(sch);
1015 errout:
1016 return ERR_PTR(err);
1017 }
1018
qdisc_create_dflt(struct netdev_queue * dev_queue,const struct Qdisc_ops * ops,unsigned int parentid,struct netlink_ext_ack * extack)1019 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
1020 const struct Qdisc_ops *ops,
1021 unsigned int parentid,
1022 struct netlink_ext_ack *extack)
1023 {
1024 struct Qdisc *sch;
1025
1026 if (!bpf_try_module_get(ops, ops->owner)) {
1027 NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
1028 return NULL;
1029 }
1030
1031 sch = qdisc_alloc(dev_queue, ops, extack);
1032 if (IS_ERR(sch)) {
1033 bpf_module_put(ops, ops->owner);
1034 return NULL;
1035 }
1036 sch->parent = parentid;
1037
1038 if (!ops->init || ops->init(sch, NULL, extack) == 0) {
1039 trace_qdisc_create(ops, dev_queue->dev, parentid);
1040 return sch;
1041 }
1042
1043 qdisc_put(sch);
1044 return NULL;
1045 }
1046 EXPORT_SYMBOL(qdisc_create_dflt);
1047
1048 /* Under qdisc_lock(qdisc) and BH! */
1049
qdisc_reset(struct Qdisc * qdisc)1050 void qdisc_reset(struct Qdisc *qdisc)
1051 {
1052 const struct Qdisc_ops *ops = qdisc->ops;
1053
1054 trace_qdisc_reset(qdisc);
1055
1056 if (ops->reset)
1057 ops->reset(qdisc);
1058
1059 __skb_queue_purge(&qdisc->gso_skb);
1060 __skb_queue_purge(&qdisc->skb_bad_txq);
1061
1062 qdisc->q.qlen = 0;
1063 qdisc->qstats.backlog = 0;
1064 }
1065 EXPORT_SYMBOL(qdisc_reset);
1066
qdisc_free(struct Qdisc * qdisc)1067 void qdisc_free(struct Qdisc *qdisc)
1068 {
1069 if (qdisc_is_percpu_stats(qdisc)) {
1070 free_percpu(qdisc->cpu_bstats);
1071 free_percpu(qdisc->cpu_qstats);
1072 }
1073
1074 kfree(qdisc);
1075 }
1076
qdisc_free_cb(struct rcu_head * head)1077 static void qdisc_free_cb(struct rcu_head *head)
1078 {
1079 struct Qdisc *q = container_of(head, struct Qdisc, rcu);
1080
1081 qdisc_free(q);
1082 }
1083
__qdisc_destroy(struct Qdisc * qdisc)1084 static void __qdisc_destroy(struct Qdisc *qdisc)
1085 {
1086 const struct Qdisc_ops *ops = qdisc->ops;
1087 struct net_device *dev = qdisc_dev(qdisc);
1088
1089 #ifdef CONFIG_NET_SCHED
1090 qdisc_hash_del(qdisc);
1091
1092 qdisc_put_stab(rtnl_dereference(qdisc->stab));
1093 #endif
1094 gen_kill_estimator(&qdisc->rate_est);
1095
1096 qdisc_reset(qdisc);
1097
1098
1099 if (ops->destroy)
1100 ops->destroy(qdisc);
1101
1102 qdisc_lock_uninit(qdisc, ops);
1103 bpf_module_put(ops, ops->owner);
1104 netdev_put(dev, &qdisc->dev_tracker);
1105
1106 trace_qdisc_destroy(qdisc);
1107
1108 call_rcu(&qdisc->rcu, qdisc_free_cb);
1109 }
1110
qdisc_destroy(struct Qdisc * qdisc)1111 void qdisc_destroy(struct Qdisc *qdisc)
1112 {
1113 if (qdisc->flags & TCQ_F_BUILTIN)
1114 return;
1115
1116 __qdisc_destroy(qdisc);
1117 }
1118
qdisc_put(struct Qdisc * qdisc)1119 void qdisc_put(struct Qdisc *qdisc)
1120 {
1121 if (!qdisc)
1122 return;
1123
1124 if (qdisc->flags & TCQ_F_BUILTIN ||
1125 !refcount_dec_and_test(&qdisc->refcnt))
1126 return;
1127
1128 __qdisc_destroy(qdisc);
1129 }
1130 EXPORT_SYMBOL(qdisc_put);
1131
1132 /* Version of qdisc_put() that is called with rtnl mutex unlocked.
1133 * Intended to be used as optimization, this function only takes rtnl lock if
1134 * qdisc reference counter reached zero.
1135 */
1136
qdisc_put_unlocked(struct Qdisc * qdisc)1137 void qdisc_put_unlocked(struct Qdisc *qdisc)
1138 {
1139 if (qdisc->flags & TCQ_F_BUILTIN ||
1140 !refcount_dec_and_rtnl_lock(&qdisc->refcnt))
1141 return;
1142
1143 __qdisc_destroy(qdisc);
1144 rtnl_unlock();
1145 }
1146 EXPORT_SYMBOL(qdisc_put_unlocked);
1147
1148 /* Attach toplevel qdisc to device queue. */
dev_graft_qdisc(struct netdev_queue * dev_queue,struct Qdisc * qdisc)1149 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
1150 struct Qdisc *qdisc)
1151 {
1152 struct Qdisc *oqdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1153 spinlock_t *root_lock;
1154
1155 root_lock = qdisc_lock(oqdisc);
1156 spin_lock_bh(root_lock);
1157
1158 /* ... and graft new one */
1159 if (qdisc == NULL)
1160 qdisc = &noop_qdisc;
1161 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1162 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1163
1164 spin_unlock_bh(root_lock);
1165
1166 return oqdisc;
1167 }
1168 EXPORT_SYMBOL(dev_graft_qdisc);
1169
shutdown_scheduler_queue(struct net_device * dev,struct netdev_queue * dev_queue,void * _qdisc_default)1170 static void shutdown_scheduler_queue(struct net_device *dev,
1171 struct netdev_queue *dev_queue,
1172 void *_qdisc_default)
1173 {
1174 struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1175 struct Qdisc *qdisc_default = _qdisc_default;
1176
1177 if (qdisc) {
1178 rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
1179 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc_default);
1180
1181 qdisc_put(qdisc);
1182 }
1183 }
1184
attach_one_default_qdisc(struct net_device * dev,struct netdev_queue * dev_queue,void * _unused)1185 static void attach_one_default_qdisc(struct net_device *dev,
1186 struct netdev_queue *dev_queue,
1187 void *_unused)
1188 {
1189 struct Qdisc *qdisc;
1190 const struct Qdisc_ops *ops = default_qdisc_ops;
1191
1192 if (dev->priv_flags & IFF_NO_QUEUE)
1193 ops = &noqueue_qdisc_ops;
1194 else if(dev->type == ARPHRD_CAN)
1195 ops = &pfifo_fast_ops;
1196
1197 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
1198 if (!qdisc)
1199 return;
1200
1201 if (!netif_is_multiqueue(dev))
1202 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
1203 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1204 }
1205
attach_default_qdiscs(struct net_device * dev)1206 static void attach_default_qdiscs(struct net_device *dev)
1207 {
1208 struct netdev_queue *txq;
1209 struct Qdisc *qdisc;
1210
1211 txq = netdev_get_tx_queue(dev, 0);
1212
1213 if (!netif_is_multiqueue(dev) ||
1214 dev->priv_flags & IFF_NO_QUEUE) {
1215 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1216 qdisc = rtnl_dereference(txq->qdisc_sleeping);
1217 rcu_assign_pointer(dev->qdisc, qdisc);
1218 qdisc_refcount_inc(qdisc);
1219 } else {
1220 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
1221 if (qdisc) {
1222 rcu_assign_pointer(dev->qdisc, qdisc);
1223 qdisc->ops->attach(qdisc);
1224 }
1225 }
1226 qdisc = rtnl_dereference(dev->qdisc);
1227
1228 /* Detect default qdisc setup/init failed and fallback to "noqueue" */
1229 if (qdisc == &noop_qdisc) {
1230 netdev_warn(dev, "default qdisc (%s) fail, fallback to %s\n",
1231 default_qdisc_ops->id, noqueue_qdisc_ops.id);
1232 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
1233 dev->priv_flags |= IFF_NO_QUEUE;
1234 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
1235 qdisc = rtnl_dereference(txq->qdisc_sleeping);
1236 rcu_assign_pointer(dev->qdisc, qdisc);
1237 qdisc_refcount_inc(qdisc);
1238 dev->priv_flags ^= IFF_NO_QUEUE;
1239 }
1240
1241 #ifdef CONFIG_NET_SCHED
1242 if (qdisc != &noop_qdisc)
1243 qdisc_hash_add(qdisc, false);
1244 #endif
1245 }
1246
transition_one_qdisc(struct net_device * dev,struct netdev_queue * dev_queue,void * _need_watchdog)1247 static void transition_one_qdisc(struct net_device *dev,
1248 struct netdev_queue *dev_queue,
1249 void *_need_watchdog)
1250 {
1251 struct Qdisc *new_qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1252 int *need_watchdog_p = _need_watchdog;
1253
1254 if (!(new_qdisc->flags & TCQ_F_BUILTIN))
1255 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
1256
1257 rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
1258 if (need_watchdog_p) {
1259 WRITE_ONCE(dev_queue->trans_start, 0);
1260 *need_watchdog_p = 1;
1261 }
1262 }
1263
dev_activate(struct net_device * dev)1264 void dev_activate(struct net_device *dev)
1265 {
1266 int need_watchdog;
1267
1268 /* No queueing discipline is attached to device;
1269 * create default one for devices, which need queueing
1270 * and noqueue_qdisc for virtual interfaces
1271 */
1272
1273 if (rtnl_dereference(dev->qdisc) == &noop_qdisc)
1274 attach_default_qdiscs(dev);
1275
1276 if (!netif_carrier_ok(dev))
1277 /* Delay activation until next carrier-on event */
1278 return;
1279
1280 need_watchdog = 0;
1281 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
1282 if (dev_ingress_queue(dev))
1283 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
1284
1285 if (need_watchdog) {
1286 netif_trans_update(dev);
1287 netdev_watchdog_up(dev);
1288 }
1289 }
1290 EXPORT_SYMBOL(dev_activate);
1291
qdisc_deactivate(struct Qdisc * qdisc)1292 static void qdisc_deactivate(struct Qdisc *qdisc)
1293 {
1294 if (qdisc->flags & TCQ_F_BUILTIN)
1295 return;
1296
1297 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
1298 }
1299
dev_deactivate_queue(struct net_device * dev,struct netdev_queue * dev_queue,void * _sync_needed)1300 static void dev_deactivate_queue(struct net_device *dev,
1301 struct netdev_queue *dev_queue,
1302 void *_sync_needed)
1303 {
1304 bool *sync_needed = _sync_needed;
1305 struct Qdisc *qdisc;
1306
1307 qdisc = rtnl_dereference(dev_queue->qdisc);
1308 if (qdisc) {
1309 if (qdisc->enqueue)
1310 *sync_needed = true;
1311 qdisc_deactivate(qdisc);
1312 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
1313 }
1314 }
1315
some_qdisc_is_busy(struct net_device * dev)1316 static bool some_qdisc_is_busy(struct net_device *dev)
1317 {
1318 unsigned int i;
1319
1320 for (i = 0; i < dev->num_tx_queues; i++) {
1321 struct netdev_queue *dev_queue;
1322 spinlock_t *root_lock;
1323 struct Qdisc *q;
1324 int val;
1325
1326 dev_queue = netdev_get_tx_queue(dev, i);
1327 q = rtnl_dereference(dev_queue->qdisc_sleeping);
1328
1329 root_lock = qdisc_lock(q);
1330 spin_lock_bh(root_lock);
1331
1332 val = (qdisc_is_running(q) ||
1333 test_bit(__QDISC_STATE_SCHED, &q->state));
1334
1335 spin_unlock_bh(root_lock);
1336
1337 if (val)
1338 return true;
1339 }
1340 return false;
1341 }
1342
1343 /**
1344 * dev_deactivate_many - deactivate transmissions on several devices
1345 * @head: list of devices to deactivate
1346 * @reset_needed: qdisc should be reset if true.
1347 *
1348 * This function returns only when all outstanding transmissions
1349 * have completed, unless all devices are in dismantle phase.
1350 */
dev_deactivate_many(struct list_head * head,bool reset_needed)1351 void dev_deactivate_many(struct list_head *head, bool reset_needed)
1352 {
1353 bool sync_needed = false;
1354 struct net_device *dev;
1355
1356 list_for_each_entry(dev, head, close_list) {
1357 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
1358 &sync_needed);
1359 if (dev_ingress_queue(dev))
1360 dev_deactivate_queue(dev, dev_ingress_queue(dev),
1361 &sync_needed);
1362
1363 netdev_watchdog_down(dev);
1364 }
1365
1366 /* Wait for outstanding qdisc enqueuing calls. */
1367 if (sync_needed)
1368 synchronize_net();
1369
1370 if (reset_needed) {
1371 list_for_each_entry(dev, head, close_list) {
1372 netdev_for_each_tx_queue(dev, dev_reset_queue, NULL);
1373
1374 if (dev_ingress_queue(dev))
1375 dev_reset_queue(dev, dev_ingress_queue(dev),
1376 NULL);
1377 }
1378 }
1379
1380 /* Wait for outstanding qdisc_run calls. */
1381 list_for_each_entry(dev, head, close_list) {
1382 while (some_qdisc_is_busy(dev)) {
1383 /* wait_event() would avoid this sleep-loop but would
1384 * require expensive checks in the fast paths of packet
1385 * processing which isn't worth it.
1386 */
1387 schedule_timeout_uninterruptible(1);
1388 }
1389 }
1390 }
1391
dev_deactivate(struct net_device * dev,bool reset_needed)1392 void dev_deactivate(struct net_device *dev, bool reset_needed)
1393 {
1394 LIST_HEAD(single);
1395
1396 list_add(&dev->close_list, &single);
1397 dev_deactivate_many(&single, reset_needed);
1398 list_del(&single);
1399 }
1400 EXPORT_SYMBOL(dev_deactivate);
1401
qdisc_change_tx_queue_len(struct net_device * dev,struct netdev_queue * dev_queue)1402 static int qdisc_change_tx_queue_len(struct net_device *dev,
1403 struct netdev_queue *dev_queue)
1404 {
1405 struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping);
1406 const struct Qdisc_ops *ops = qdisc->ops;
1407
1408 if (ops->change_tx_queue_len)
1409 return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
1410 return 0;
1411 }
1412
dev_qdisc_change_real_num_tx(struct net_device * dev,unsigned int new_real_tx)1413 void dev_qdisc_change_real_num_tx(struct net_device *dev,
1414 unsigned int new_real_tx)
1415 {
1416 struct Qdisc *qdisc = rtnl_dereference(dev->qdisc);
1417
1418 if (qdisc->ops->change_real_num_tx)
1419 qdisc->ops->change_real_num_tx(qdisc, new_real_tx);
1420 }
1421
mq_change_real_num_tx(struct Qdisc * sch,unsigned int new_real_tx)1422 void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx)
1423 {
1424 #ifdef CONFIG_NET_SCHED
1425 struct net_device *dev = qdisc_dev(sch);
1426 struct Qdisc *qdisc;
1427 unsigned int i;
1428
1429 for (i = new_real_tx; i < dev->real_num_tx_queues; i++) {
1430 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping);
1431 /* Only update the default qdiscs we created,
1432 * qdiscs with handles are always hashed.
1433 */
1434 if (qdisc != &noop_qdisc && !qdisc->handle)
1435 qdisc_hash_del(qdisc);
1436 }
1437 for (i = dev->real_num_tx_queues; i < new_real_tx; i++) {
1438 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping);
1439 if (qdisc != &noop_qdisc && !qdisc->handle)
1440 qdisc_hash_add(qdisc, false);
1441 }
1442 #endif
1443 }
1444 EXPORT_SYMBOL(mq_change_real_num_tx);
1445
dev_qdisc_change_tx_queue_len(struct net_device * dev)1446 int dev_qdisc_change_tx_queue_len(struct net_device *dev)
1447 {
1448 bool up = dev->flags & IFF_UP;
1449 unsigned int i;
1450 int ret = 0;
1451
1452 if (up)
1453 dev_deactivate(dev, false);
1454
1455 for (i = 0; i < dev->num_tx_queues; i++) {
1456 ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
1457
1458 /* TODO: revert changes on a partial failure */
1459 if (ret)
1460 break;
1461 }
1462
1463 if (up)
1464 dev_activate(dev);
1465 return ret;
1466 }
1467
dev_init_scheduler_queue(struct net_device * dev,struct netdev_queue * dev_queue,void * _qdisc)1468 static void dev_init_scheduler_queue(struct net_device *dev,
1469 struct netdev_queue *dev_queue,
1470 void *_qdisc)
1471 {
1472 struct Qdisc *qdisc = _qdisc;
1473
1474 rcu_assign_pointer(dev_queue->qdisc, qdisc);
1475 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc);
1476 }
1477
dev_init_scheduler(struct net_device * dev)1478 void dev_init_scheduler(struct net_device *dev)
1479 {
1480 rcu_assign_pointer(dev->qdisc, &noop_qdisc);
1481 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
1482 if (dev_ingress_queue(dev))
1483 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1484
1485 timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
1486 }
1487
dev_shutdown(struct net_device * dev)1488 void dev_shutdown(struct net_device *dev)
1489 {
1490 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
1491 if (dev_ingress_queue(dev))
1492 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
1493 qdisc_put(rtnl_dereference(dev->qdisc));
1494 rcu_assign_pointer(dev->qdisc, &noop_qdisc);
1495
1496 WARN_ON(timer_pending(&dev->watchdog_timer));
1497 }
1498
1499 /**
1500 * psched_ratecfg_precompute__() - Pre-compute values for reciprocal division
1501 * @rate: Rate to compute reciprocal division values of
1502 * @mult: Multiplier for reciprocal division
1503 * @shift: Shift for reciprocal division
1504 *
1505 * The multiplier and shift for reciprocal division by rate are stored
1506 * in mult and shift.
1507 *
1508 * The deal here is to replace a divide by a reciprocal one
1509 * in fast path (a reciprocal divide is a multiply and a shift)
1510 *
1511 * Normal formula would be :
1512 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
1513 *
1514 * We compute mult/shift to use instead :
1515 * time_in_ns = (len * mult) >> shift;
1516 *
1517 * We try to get the highest possible mult value for accuracy,
1518 * but have to make sure no overflows will ever happen.
1519 *
1520 * reciprocal_value() is not used here it doesn't handle 64-bit values.
1521 */
psched_ratecfg_precompute__(u64 rate,u32 * mult,u8 * shift)1522 static void psched_ratecfg_precompute__(u64 rate, u32 *mult, u8 *shift)
1523 {
1524 u64 factor = NSEC_PER_SEC;
1525
1526 *mult = 1;
1527 *shift = 0;
1528
1529 if (rate <= 0)
1530 return;
1531
1532 for (;;) {
1533 *mult = div64_u64(factor, rate);
1534 if (*mult & (1U << 31) || factor & (1ULL << 63))
1535 break;
1536 factor <<= 1;
1537 (*shift)++;
1538 }
1539 }
1540
psched_ratecfg_precompute(struct psched_ratecfg * r,const struct tc_ratespec * conf,u64 rate64)1541 void psched_ratecfg_precompute(struct psched_ratecfg *r,
1542 const struct tc_ratespec *conf,
1543 u64 rate64)
1544 {
1545 memset(r, 0, sizeof(*r));
1546 r->overhead = conf->overhead;
1547 r->mpu = conf->mpu;
1548 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
1549 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
1550 psched_ratecfg_precompute__(r->rate_bytes_ps, &r->mult, &r->shift);
1551 }
1552 EXPORT_SYMBOL(psched_ratecfg_precompute);
1553
psched_ppscfg_precompute(struct psched_pktrate * r,u64 pktrate64)1554 void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64)
1555 {
1556 r->rate_pkts_ps = pktrate64;
1557 psched_ratecfg_precompute__(r->rate_pkts_ps, &r->mult, &r->shift);
1558 }
1559 EXPORT_SYMBOL(psched_ppscfg_precompute);
1560
mini_qdisc_pair_swap(struct mini_Qdisc_pair * miniqp,struct tcf_proto * tp_head)1561 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
1562 struct tcf_proto *tp_head)
1563 {
1564 /* Protected with chain0->filter_chain_lock.
1565 * Can't access chain directly because tp_head can be NULL.
1566 */
1567 struct mini_Qdisc *miniq_old =
1568 rcu_dereference_protected(*miniqp->p_miniq, 1);
1569 struct mini_Qdisc *miniq;
1570
1571 if (!tp_head) {
1572 RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
1573 } else {
1574 miniq = miniq_old != &miniqp->miniq1 ?
1575 &miniqp->miniq1 : &miniqp->miniq2;
1576
1577 /* We need to make sure that readers won't see the miniq
1578 * we are about to modify. So ensure that at least one RCU
1579 * grace period has elapsed since the miniq was made
1580 * inactive.
1581 */
1582 if (IS_ENABLED(CONFIG_PREEMPT_RT))
1583 cond_synchronize_rcu(miniq->rcu_state);
1584 else if (!poll_state_synchronize_rcu(miniq->rcu_state))
1585 synchronize_rcu_expedited();
1586
1587 miniq->filter_list = tp_head;
1588 rcu_assign_pointer(*miniqp->p_miniq, miniq);
1589 }
1590
1591 if (miniq_old)
1592 /* This is counterpart of the rcu sync above. We need to
1593 * block potential new user of miniq_old until all readers
1594 * are not seeing it.
1595 */
1596 miniq_old->rcu_state = start_poll_synchronize_rcu();
1597 }
1598 EXPORT_SYMBOL(mini_qdisc_pair_swap);
1599
mini_qdisc_pair_block_init(struct mini_Qdisc_pair * miniqp,struct tcf_block * block)1600 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp,
1601 struct tcf_block *block)
1602 {
1603 miniqp->miniq1.block = block;
1604 miniqp->miniq2.block = block;
1605 }
1606 EXPORT_SYMBOL(mini_qdisc_pair_block_init);
1607
mini_qdisc_pair_init(struct mini_Qdisc_pair * miniqp,struct Qdisc * qdisc,struct mini_Qdisc __rcu ** p_miniq)1608 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
1609 struct mini_Qdisc __rcu **p_miniq)
1610 {
1611 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
1612 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
1613 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
1614 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
1615 miniqp->miniq1.rcu_state = get_state_synchronize_rcu();
1616 miniqp->miniq2.rcu_state = miniqp->miniq1.rcu_state;
1617 miniqp->p_miniq = p_miniq;
1618 }
1619 EXPORT_SYMBOL(mini_qdisc_pair_init);
1620