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 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 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 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 qdisc_qlen_dec(q); 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 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 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 qdisc_qlen_inc(q); 163 } 164 165 if (lock) 166 spin_unlock(lock); 167 } 168 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 qdisc_qlen_inc(q); 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 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 */ 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 */ 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 qdisc_qlen_dec(q); 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 */ 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 */ 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 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 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 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 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 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 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 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 spin_lock(&dev->watchdog_lock); 572 mod_timer(&dev->watchdog_timer, 573 round_jiffies(oldest_start + 574 dev->watchdog_timeo)); 575 release = false; 576 spin_unlock(&dev->watchdog_lock); 577 } 578 } 579 spin_unlock(&dev->tx_global_lock); 580 581 spin_lock(&dev->watchdog_lock); 582 if (timer_pending(&dev->watchdog_timer)) 583 release = false; 584 if (release && dev->watchdog_ref_held) { 585 netdev_put(dev, &dev->watchdog_dev_tracker); 586 dev->watchdog_ref_held = false; 587 } 588 spin_unlock(&dev->watchdog_lock); 589 } 590 591 void netdev_watchdog_up(struct net_device *dev) 592 { 593 if (!dev->netdev_ops->ndo_tx_timeout) 594 return; 595 if (dev->watchdog_timeo <= 0) 596 dev->watchdog_timeo = 5*HZ; 597 598 spin_lock_bh(&dev->watchdog_lock); 599 if (!mod_timer(&dev->watchdog_timer, 600 round_jiffies(jiffies + dev->watchdog_timeo))) { 601 if (!dev->watchdog_ref_held) { 602 netdev_hold(dev, &dev->watchdog_dev_tracker, 603 GFP_ATOMIC); 604 dev->watchdog_ref_held = true; 605 } 606 } 607 spin_unlock_bh(&dev->watchdog_lock); 608 } 609 EXPORT_SYMBOL_GPL(netdev_watchdog_up); 610 611 static void netdev_watchdog_down(struct net_device *dev) 612 { 613 netif_tx_lock_bh(dev); 614 615 spin_lock(&dev->watchdog_lock); 616 if (timer_delete(&dev->watchdog_timer)) { 617 netdev_put(dev, &dev->watchdog_dev_tracker); 618 dev->watchdog_ref_held = false; 619 } 620 spin_unlock(&dev->watchdog_lock); 621 622 netif_tx_unlock_bh(dev); 623 } 624 625 /** 626 * netif_carrier_on - set carrier 627 * @dev: network device 628 * 629 * Device has detected acquisition of carrier. 630 */ 631 void netif_carrier_on(struct net_device *dev) 632 { 633 if (READ_ONCE(dev->proto_down)) 634 return; 635 636 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { 637 if (dev->reg_state == NETREG_UNINITIALIZED) 638 return; 639 atomic_inc(&dev->carrier_up_count); 640 linkwatch_fire_event(dev); 641 } 642 } 643 EXPORT_SYMBOL(netif_carrier_on); 644 645 /** 646 * netif_carrier_off - clear carrier 647 * @dev: network device 648 * 649 * Device has detected loss of carrier. 650 */ 651 void netif_carrier_off(struct net_device *dev) 652 { 653 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) { 654 if (dev->reg_state == NETREG_UNINITIALIZED) 655 return; 656 atomic_inc(&dev->carrier_down_count); 657 linkwatch_fire_event(dev); 658 } 659 } 660 EXPORT_SYMBOL(netif_carrier_off); 661 662 /** 663 * netif_carrier_event - report carrier state event 664 * @dev: network device 665 * 666 * Device has detected a carrier event but the carrier state wasn't changed. 667 * Use in drivers when querying carrier state asynchronously, to avoid missing 668 * events (link flaps) if link recovers before it's queried. 669 */ 670 void netif_carrier_event(struct net_device *dev) 671 { 672 if (dev->reg_state == NETREG_UNINITIALIZED) 673 return; 674 atomic_inc(&dev->carrier_up_count); 675 atomic_inc(&dev->carrier_down_count); 676 linkwatch_fire_event(dev); 677 } 678 EXPORT_SYMBOL_GPL(netif_carrier_event); 679 680 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces 681 under all circumstances. It is difficult to invent anything faster or 682 cheaper. 683 */ 684 685 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc, 686 struct sk_buff **to_free) 687 { 688 dev_core_stats_tx_dropped_inc(skb->dev); 689 __qdisc_drop(skb, to_free); 690 return NET_XMIT_CN; 691 } 692 693 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc) 694 { 695 return NULL; 696 } 697 698 struct Qdisc_ops noop_qdisc_ops __read_mostly = { 699 .id = "noop", 700 .priv_size = 0, 701 .enqueue = noop_enqueue, 702 .dequeue = noop_dequeue, 703 .peek = noop_dequeue, 704 .owner = THIS_MODULE, 705 }; 706 707 static struct netdev_queue noop_netdev_queue = { 708 RCU_POINTER_INITIALIZER(qdisc, &noop_qdisc), 709 RCU_POINTER_INITIALIZER(qdisc_sleeping, &noop_qdisc), 710 }; 711 712 struct Qdisc noop_qdisc = { 713 .enqueue = noop_enqueue, 714 .dequeue = noop_dequeue, 715 .flags = TCQ_F_BUILTIN, 716 .ops = &noop_qdisc_ops, 717 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock), 718 .dev_queue = &noop_netdev_queue, 719 .gso_skb = { 720 .next = (struct sk_buff *)&noop_qdisc.gso_skb, 721 .prev = (struct sk_buff *)&noop_qdisc.gso_skb, 722 .qlen = 0, 723 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock), 724 }, 725 .skb_bad_txq = { 726 .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq, 727 .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq, 728 .qlen = 0, 729 .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock), 730 }, 731 }; 732 EXPORT_SYMBOL(noop_qdisc); 733 734 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt, 735 struct netlink_ext_ack *extack) 736 { 737 /* register_qdisc() assigns a default of noop_enqueue if unset, 738 * but __dev_queue_xmit() treats noqueue only as such 739 * if this is NULL - so clear it here. */ 740 qdisc->enqueue = NULL; 741 return 0; 742 } 743 744 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = { 745 .id = "noqueue", 746 .priv_size = 0, 747 .init = noqueue_init, 748 .enqueue = noop_enqueue, 749 .dequeue = noop_dequeue, 750 .peek = noop_dequeue, 751 .owner = THIS_MODULE, 752 }; 753 754 const u8 sch_default_prio2band[TC_PRIO_MAX + 1] = { 755 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 756 }; 757 EXPORT_SYMBOL(sch_default_prio2band); 758 759 /* 3-band FIFO queue: old style, but should be a bit faster than 760 generic prio+fifo combination. 761 */ 762 763 #define PFIFO_FAST_BANDS 3 764 765 /* 766 * Private data for a pfifo_fast scheduler containing: 767 * - rings for priority bands 768 */ 769 struct pfifo_fast_priv { 770 struct skb_array q[PFIFO_FAST_BANDS]; 771 }; 772 773 static inline struct skb_array *band2list(struct pfifo_fast_priv *priv, 774 int band) 775 { 776 return &priv->q[band]; 777 } 778 779 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc, 780 struct sk_buff **to_free) 781 { 782 int band = sch_default_prio2band[skb->priority & TC_PRIO_MAX]; 783 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 784 struct skb_array *q = band2list(priv, band); 785 unsigned int pkt_len = qdisc_pkt_len(skb); 786 int err; 787 788 err = skb_array_produce(q, skb); 789 790 if (unlikely(err)) { 791 tcf_set_qdisc_drop_reason(skb, QDISC_DROP_OVERLIMIT); 792 793 if (qdisc_is_percpu_stats(qdisc)) 794 return qdisc_drop_cpu(skb, qdisc, to_free); 795 else 796 return qdisc_drop(skb, qdisc, to_free); 797 } 798 799 qdisc_update_stats_at_enqueue(qdisc, pkt_len); 800 return NET_XMIT_SUCCESS; 801 } 802 803 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc) 804 { 805 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 806 struct sk_buff *skb = NULL; 807 bool need_retry = true; 808 int band; 809 810 retry: 811 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) { 812 struct skb_array *q = band2list(priv, band); 813 814 if (__skb_array_empty(q)) 815 continue; 816 817 skb = __skb_array_consume(q); 818 } 819 if (likely(skb)) { 820 qdisc_update_stats_at_dequeue(qdisc, skb); 821 } else if (need_retry && 822 READ_ONCE(qdisc->state) & QDISC_STATE_NON_EMPTY) { 823 /* Delay clearing the STATE_MISSED here to reduce 824 * the overhead of the second spin_trylock() in 825 * qdisc_run_begin() and __netif_schedule() calling 826 * in qdisc_run_end(). 827 */ 828 clear_bit(__QDISC_STATE_MISSED, &qdisc->state); 829 clear_bit(__QDISC_STATE_DRAINING, &qdisc->state); 830 831 /* Make sure dequeuing happens after clearing 832 * STATE_MISSED. 833 */ 834 smp_mb__after_atomic(); 835 836 need_retry = false; 837 838 goto retry; 839 } 840 841 return skb; 842 } 843 844 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc) 845 { 846 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 847 struct sk_buff *skb = NULL; 848 int band; 849 850 for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) { 851 struct skb_array *q = band2list(priv, band); 852 853 skb = __skb_array_peek(q); 854 } 855 856 return skb; 857 } 858 859 static void pfifo_fast_reset(struct Qdisc *qdisc) 860 { 861 int i, band; 862 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 863 864 for (band = 0; band < PFIFO_FAST_BANDS; band++) { 865 struct skb_array *q = band2list(priv, band); 866 struct sk_buff *skb; 867 868 /* NULL ring is possible if destroy path is due to a failed 869 * skb_array_init() in pfifo_fast_init() case. 870 */ 871 if (!q->ring.queue) 872 continue; 873 874 while ((skb = __skb_array_consume(q)) != NULL) 875 rtnl_kfree_skbs(skb, skb); 876 } 877 878 if (qdisc_is_percpu_stats(qdisc)) { 879 for_each_possible_cpu(i) { 880 struct gnet_stats_queue *q; 881 882 q = per_cpu_ptr(qdisc->cpu_qstats, i); 883 q->backlog = 0; 884 q->qlen = 0; 885 } 886 } 887 } 888 889 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) 890 { 891 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; 892 893 memcpy(&opt.priomap, sch_default_prio2band, TC_PRIO_MAX + 1); 894 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 895 goto nla_put_failure; 896 return skb->len; 897 898 nla_put_failure: 899 return -1; 900 } 901 902 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt, 903 struct netlink_ext_ack *extack) 904 { 905 unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len; 906 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 907 int prio; 908 909 /* guard against zero length rings */ 910 if (!qlen) 911 return -EINVAL; 912 913 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { 914 struct skb_array *q = band2list(priv, prio); 915 int err; 916 917 err = skb_array_init(q, qlen, GFP_KERNEL); 918 if (err) 919 return -ENOMEM; 920 } 921 922 /* Can by-pass the queue discipline */ 923 qdisc->flags |= TCQ_F_CAN_BYPASS; 924 return 0; 925 } 926 927 static void pfifo_fast_destroy(struct Qdisc *sch) 928 { 929 struct pfifo_fast_priv *priv = qdisc_priv(sch); 930 int prio; 931 932 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { 933 struct skb_array *q = band2list(priv, prio); 934 935 /* NULL ring is possible if destroy path is due to a failed 936 * skb_array_init() in pfifo_fast_init() case. 937 */ 938 if (!q->ring.queue) 939 continue; 940 /* Destroy ring but no need to kfree_skb because a call to 941 * pfifo_fast_reset() has already done that work. 942 */ 943 ptr_ring_cleanup(&q->ring, NULL); 944 } 945 } 946 947 static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch, 948 unsigned int new_len) 949 { 950 struct pfifo_fast_priv *priv = qdisc_priv(sch); 951 struct skb_array *bands[PFIFO_FAST_BANDS]; 952 int prio; 953 954 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) { 955 struct skb_array *q = band2list(priv, prio); 956 957 bands[prio] = q; 958 } 959 960 return skb_array_resize_multiple_bh(bands, PFIFO_FAST_BANDS, new_len, 961 GFP_KERNEL); 962 } 963 964 struct Qdisc_ops pfifo_fast_ops __read_mostly = { 965 .id = "pfifo_fast", 966 .priv_size = sizeof(struct pfifo_fast_priv), 967 .enqueue = pfifo_fast_enqueue, 968 .dequeue = pfifo_fast_dequeue, 969 .peek = pfifo_fast_peek, 970 .init = pfifo_fast_init, 971 .destroy = pfifo_fast_destroy, 972 .reset = pfifo_fast_reset, 973 .dump = pfifo_fast_dump, 974 .change_tx_queue_len = pfifo_fast_change_tx_queue_len, 975 .owner = THIS_MODULE, 976 .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS, 977 }; 978 EXPORT_SYMBOL(pfifo_fast_ops); 979 980 static struct lock_class_key qdisc_tx_busylock; 981 982 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, 983 const struct Qdisc_ops *ops, 984 struct netlink_ext_ack *extack) 985 { 986 struct Qdisc *sch; 987 unsigned int size = sizeof(*sch) + ops->priv_size; 988 int err = -ENOBUFS; 989 struct net_device *dev; 990 991 if (!dev_queue) { 992 NL_SET_ERR_MSG(extack, "No device queue given"); 993 err = -EINVAL; 994 goto errout; 995 } 996 997 dev = dev_queue->dev; 998 sch = kzalloc_node(size, GFP_KERNEL, netdev_queue_numa_node_read(dev_queue)); 999 1000 if (!sch) 1001 goto errout; 1002 __skb_queue_head_init(&sch->gso_skb); 1003 __skb_queue_head_init(&sch->skb_bad_txq); 1004 gnet_stats_basic_sync_init(&sch->bstats); 1005 qdisc_lock_init(sch, ops); 1006 1007 if (ops->static_flags & TCQ_F_CPUSTATS) { 1008 sch->cpu_bstats = 1009 netdev_alloc_pcpu_stats(struct gnet_stats_basic_sync); 1010 if (!sch->cpu_bstats) 1011 goto errout1; 1012 1013 sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue); 1014 if (!sch->cpu_qstats) { 1015 free_percpu(sch->cpu_bstats); 1016 goto errout1; 1017 } 1018 } 1019 1020 /* seqlock has the same scope of busylock, for NOLOCK qdisc */ 1021 spin_lock_init(&sch->seqlock); 1022 lockdep_set_class(&sch->seqlock, 1023 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock); 1024 1025 sch->ops = ops; 1026 sch->flags = ops->static_flags; 1027 sch->enqueue = ops->enqueue; 1028 sch->dequeue = ops->dequeue; 1029 sch->dev_queue = dev_queue; 1030 netdev_hold(dev, &sch->dev_tracker, GFP_KERNEL); 1031 refcount_set(&sch->refcnt, 1); 1032 1033 return sch; 1034 errout1: 1035 qdisc_lock_uninit(sch, ops); 1036 kfree(sch); 1037 errout: 1038 return ERR_PTR(err); 1039 } 1040 1041 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, 1042 const struct Qdisc_ops *ops, 1043 unsigned int parentid, 1044 struct netlink_ext_ack *extack) 1045 { 1046 struct Qdisc *sch; 1047 1048 if (!bpf_try_module_get(ops, ops->owner)) { 1049 NL_SET_ERR_MSG(extack, "Failed to increase module reference counter"); 1050 return NULL; 1051 } 1052 1053 sch = qdisc_alloc(dev_queue, ops, extack); 1054 if (IS_ERR(sch)) { 1055 bpf_module_put(ops, ops->owner); 1056 return NULL; 1057 } 1058 sch->parent = parentid; 1059 1060 if (!ops->init || ops->init(sch, NULL, extack) == 0) { 1061 trace_qdisc_create(ops, dev_queue->dev, parentid); 1062 return sch; 1063 } 1064 1065 qdisc_put(sch); 1066 return NULL; 1067 } 1068 EXPORT_SYMBOL(qdisc_create_dflt); 1069 1070 /* Under qdisc_lock(qdisc) and BH! */ 1071 1072 void qdisc_reset(struct Qdisc *qdisc) 1073 { 1074 const struct Qdisc_ops *ops = qdisc->ops; 1075 1076 trace_qdisc_reset(qdisc); 1077 1078 if (ops->reset) 1079 ops->reset(qdisc); 1080 1081 __skb_queue_purge(&qdisc->gso_skb); 1082 __skb_queue_purge(&qdisc->skb_bad_txq); 1083 1084 WRITE_ONCE(qdisc->q.qlen, 0); 1085 WRITE_ONCE(qdisc->qstats.backlog, 0); 1086 } 1087 EXPORT_SYMBOL(qdisc_reset); 1088 1089 void qdisc_free(struct Qdisc *qdisc) 1090 { 1091 if (qdisc_is_percpu_stats(qdisc)) { 1092 free_percpu(qdisc->cpu_bstats); 1093 free_percpu(qdisc->cpu_qstats); 1094 } 1095 1096 kfree(qdisc); 1097 } 1098 1099 static void qdisc_free_cb(struct rcu_head *head) 1100 { 1101 struct Qdisc *q = container_of(head, struct Qdisc, rcu); 1102 1103 qdisc_free(q); 1104 } 1105 1106 static void __qdisc_destroy(struct Qdisc *qdisc) 1107 { 1108 const struct Qdisc_ops *ops = qdisc->ops; 1109 struct net_device *dev = qdisc_dev(qdisc); 1110 1111 #ifdef CONFIG_NET_SCHED 1112 qdisc_hash_del(qdisc); 1113 1114 qdisc_put_stab(rtnl_dereference(qdisc->stab)); 1115 #endif 1116 gen_kill_estimator(&qdisc->rate_est); 1117 1118 qdisc_reset(qdisc); 1119 1120 1121 if (ops->destroy) 1122 ops->destroy(qdisc); 1123 1124 qdisc_lock_uninit(qdisc, ops); 1125 bpf_module_put(ops, ops->owner); 1126 netdev_put(dev, &qdisc->dev_tracker); 1127 1128 trace_qdisc_destroy(qdisc); 1129 1130 call_rcu(&qdisc->rcu, qdisc_free_cb); 1131 } 1132 1133 void qdisc_destroy(struct Qdisc *qdisc) 1134 { 1135 if (qdisc->flags & TCQ_F_BUILTIN) 1136 return; 1137 1138 __qdisc_destroy(qdisc); 1139 } 1140 1141 void qdisc_put(struct Qdisc *qdisc) 1142 { 1143 if (!qdisc) 1144 return; 1145 1146 if (qdisc->flags & TCQ_F_BUILTIN || 1147 !refcount_dec_and_test(&qdisc->refcnt)) 1148 return; 1149 1150 __qdisc_destroy(qdisc); 1151 } 1152 EXPORT_SYMBOL(qdisc_put); 1153 1154 /* Version of qdisc_put() that is called with rtnl mutex unlocked. 1155 * Intended to be used as optimization, this function only takes rtnl lock if 1156 * qdisc reference counter reached zero. 1157 */ 1158 1159 void qdisc_put_unlocked(struct Qdisc *qdisc) 1160 { 1161 if (qdisc->flags & TCQ_F_BUILTIN || 1162 !refcount_dec_and_rtnl_lock(&qdisc->refcnt)) 1163 return; 1164 1165 __qdisc_destroy(qdisc); 1166 rtnl_unlock(); 1167 } 1168 EXPORT_SYMBOL(qdisc_put_unlocked); 1169 1170 /* Attach toplevel qdisc to device queue. */ 1171 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, 1172 struct Qdisc *qdisc) 1173 { 1174 struct Qdisc *oqdisc = rtnl_dereference(dev_queue->qdisc_sleeping); 1175 spinlock_t *root_lock; 1176 1177 root_lock = qdisc_lock(oqdisc); 1178 spin_lock_bh(root_lock); 1179 1180 /* ... and graft new one */ 1181 if (qdisc == NULL) 1182 qdisc = &noop_qdisc; 1183 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc); 1184 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc); 1185 1186 spin_unlock_bh(root_lock); 1187 1188 return oqdisc; 1189 } 1190 EXPORT_SYMBOL(dev_graft_qdisc); 1191 1192 static void shutdown_scheduler_queue(struct net_device *dev, 1193 struct netdev_queue *dev_queue, 1194 void *_qdisc_default) 1195 { 1196 struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping); 1197 struct Qdisc *qdisc_default = _qdisc_default; 1198 1199 if (qdisc) { 1200 rcu_assign_pointer(dev_queue->qdisc, qdisc_default); 1201 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc_default); 1202 1203 qdisc_put(qdisc); 1204 } 1205 } 1206 1207 static void attach_one_default_qdisc(struct net_device *dev, 1208 struct netdev_queue *dev_queue, 1209 void *_unused) 1210 { 1211 struct Qdisc *qdisc; 1212 const struct Qdisc_ops *ops = default_qdisc_ops; 1213 1214 if (dev->priv_flags & IFF_NO_QUEUE) 1215 ops = &noqueue_qdisc_ops; 1216 else if(dev->type == ARPHRD_CAN) 1217 ops = &pfifo_fast_ops; 1218 1219 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL); 1220 if (!qdisc) 1221 return; 1222 1223 if (!netif_is_multiqueue(dev)) 1224 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 1225 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc); 1226 } 1227 1228 static void attach_default_qdiscs(struct net_device *dev) 1229 { 1230 struct netdev_queue *txq; 1231 struct Qdisc *qdisc; 1232 1233 txq = netdev_get_tx_queue(dev, 0); 1234 1235 if (!netif_is_multiqueue(dev) || 1236 dev->priv_flags & IFF_NO_QUEUE) { 1237 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); 1238 qdisc = rtnl_dereference(txq->qdisc_sleeping); 1239 rcu_assign_pointer(dev->qdisc, qdisc); 1240 qdisc_refcount_inc(qdisc); 1241 } else { 1242 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL); 1243 if (qdisc) { 1244 rcu_assign_pointer(dev->qdisc, qdisc); 1245 qdisc->ops->attach(qdisc); 1246 } 1247 } 1248 qdisc = rtnl_dereference(dev->qdisc); 1249 1250 /* Detect default qdisc setup/init failed and fallback to "noqueue" */ 1251 if (qdisc == &noop_qdisc) { 1252 netdev_warn(dev, "default qdisc (%s) fail, fallback to %s\n", 1253 default_qdisc_ops->id, noqueue_qdisc_ops.id); 1254 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); 1255 dev->priv_flags |= IFF_NO_QUEUE; 1256 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); 1257 qdisc = rtnl_dereference(txq->qdisc_sleeping); 1258 rcu_assign_pointer(dev->qdisc, qdisc); 1259 qdisc_refcount_inc(qdisc); 1260 dev->priv_flags ^= IFF_NO_QUEUE; 1261 } 1262 1263 #ifdef CONFIG_NET_SCHED 1264 if (qdisc != &noop_qdisc) 1265 qdisc_hash_add(qdisc, false); 1266 #endif 1267 } 1268 1269 static void transition_one_qdisc(struct net_device *dev, 1270 struct netdev_queue *dev_queue, 1271 void *_need_watchdog) 1272 { 1273 struct Qdisc *new_qdisc = rtnl_dereference(dev_queue->qdisc_sleeping); 1274 int *need_watchdog_p = _need_watchdog; 1275 1276 if (!(new_qdisc->flags & TCQ_F_BUILTIN)) 1277 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state); 1278 1279 rcu_assign_pointer(dev_queue->qdisc, new_qdisc); 1280 if (need_watchdog_p) { 1281 WRITE_ONCE(dev_queue->trans_start, 0); 1282 *need_watchdog_p = 1; 1283 } 1284 } 1285 1286 void dev_activate(struct net_device *dev) 1287 { 1288 int need_watchdog; 1289 1290 /* No queueing discipline is attached to device; 1291 * create default one for devices, which need queueing 1292 * and noqueue_qdisc for virtual interfaces 1293 */ 1294 1295 if (rtnl_dereference(dev->qdisc) == &noop_qdisc) 1296 attach_default_qdiscs(dev); 1297 1298 if (!netif_carrier_ok(dev)) 1299 /* Delay activation until next carrier-on event */ 1300 return; 1301 1302 need_watchdog = 0; 1303 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog); 1304 if (dev_ingress_queue(dev)) 1305 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL); 1306 1307 if (need_watchdog) { 1308 netif_trans_update(dev); 1309 netdev_watchdog_up(dev); 1310 } 1311 } 1312 EXPORT_SYMBOL(dev_activate); 1313 1314 static void qdisc_deactivate(struct Qdisc *qdisc) 1315 { 1316 if (qdisc->flags & TCQ_F_BUILTIN) 1317 return; 1318 1319 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); 1320 } 1321 1322 static void dev_deactivate_queue(struct net_device *dev, 1323 struct netdev_queue *dev_queue, 1324 void *_sync_needed) 1325 { 1326 bool *sync_needed = _sync_needed; 1327 struct Qdisc *qdisc; 1328 1329 qdisc = rtnl_dereference(dev_queue->qdisc); 1330 if (qdisc) { 1331 if (qdisc->enqueue) 1332 *sync_needed = true; 1333 qdisc_deactivate(qdisc); 1334 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc); 1335 } 1336 } 1337 1338 static bool some_qdisc_is_busy(struct net_device *dev) 1339 { 1340 unsigned int i; 1341 1342 for (i = 0; i < dev->num_tx_queues; i++) { 1343 struct netdev_queue *dev_queue; 1344 spinlock_t *root_lock; 1345 struct Qdisc *q; 1346 int val; 1347 1348 dev_queue = netdev_get_tx_queue(dev, i); 1349 q = rtnl_dereference(dev_queue->qdisc_sleeping); 1350 1351 root_lock = qdisc_lock(q); 1352 spin_lock_bh(root_lock); 1353 1354 val = (qdisc_is_running(q) || 1355 test_bit(__QDISC_STATE_SCHED, &q->state)); 1356 1357 spin_unlock_bh(root_lock); 1358 1359 if (val) 1360 return true; 1361 } 1362 return false; 1363 } 1364 1365 /** 1366 * dev_deactivate_many - deactivate transmissions on several devices 1367 * @head: list of devices to deactivate 1368 * @reset_needed: qdisc should be reset if true. 1369 * 1370 * This function returns only when all outstanding transmissions 1371 * have completed, unless all devices are in dismantle phase. 1372 */ 1373 void dev_deactivate_many(struct list_head *head, bool reset_needed) 1374 { 1375 bool sync_needed = false; 1376 struct net_device *dev; 1377 1378 list_for_each_entry(dev, head, close_list) { 1379 netdev_for_each_tx_queue(dev, dev_deactivate_queue, 1380 &sync_needed); 1381 if (dev_ingress_queue(dev)) 1382 dev_deactivate_queue(dev, dev_ingress_queue(dev), 1383 &sync_needed); 1384 1385 netdev_watchdog_down(dev); 1386 } 1387 1388 /* Wait for outstanding qdisc enqueuing calls. */ 1389 if (sync_needed) 1390 synchronize_net(); 1391 1392 if (reset_needed) { 1393 list_for_each_entry(dev, head, close_list) { 1394 netdev_for_each_tx_queue(dev, dev_reset_queue, NULL); 1395 1396 if (dev_ingress_queue(dev)) 1397 dev_reset_queue(dev, dev_ingress_queue(dev), 1398 NULL); 1399 } 1400 } 1401 1402 /* Wait for outstanding qdisc_run calls. */ 1403 list_for_each_entry(dev, head, close_list) { 1404 while (some_qdisc_is_busy(dev)) { 1405 /* wait_event() would avoid this sleep-loop but would 1406 * require expensive checks in the fast paths of packet 1407 * processing which isn't worth it. 1408 */ 1409 schedule_timeout_uninterruptible(1); 1410 } 1411 } 1412 } 1413 1414 void dev_deactivate(struct net_device *dev, bool reset_needed) 1415 { 1416 LIST_HEAD(single); 1417 1418 list_add(&dev->close_list, &single); 1419 dev_deactivate_many(&single, reset_needed); 1420 list_del(&single); 1421 } 1422 EXPORT_SYMBOL(dev_deactivate); 1423 1424 static int qdisc_change_tx_queue_len(struct net_device *dev, 1425 struct netdev_queue *dev_queue) 1426 { 1427 struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc_sleeping); 1428 const struct Qdisc_ops *ops = qdisc->ops; 1429 1430 if (ops->change_tx_queue_len) 1431 return ops->change_tx_queue_len(qdisc, dev->tx_queue_len); 1432 return 0; 1433 } 1434 1435 void dev_qdisc_change_real_num_tx(struct net_device *dev, 1436 unsigned int new_real_tx) 1437 { 1438 struct Qdisc *qdisc = rtnl_dereference(dev->qdisc); 1439 1440 if (qdisc->ops->change_real_num_tx) 1441 qdisc->ops->change_real_num_tx(qdisc, new_real_tx); 1442 } 1443 1444 void mq_change_real_num_tx(struct Qdisc *sch, unsigned int new_real_tx) 1445 { 1446 #ifdef CONFIG_NET_SCHED 1447 struct net_device *dev = qdisc_dev(sch); 1448 struct Qdisc *qdisc; 1449 unsigned int i; 1450 1451 for (i = new_real_tx; i < dev->real_num_tx_queues; i++) { 1452 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping); 1453 /* Only update the default qdiscs we created, 1454 * qdiscs with handles are always hashed. 1455 */ 1456 if (qdisc != &noop_qdisc && !qdisc->handle) 1457 qdisc_hash_del(qdisc); 1458 } 1459 for (i = dev->real_num_tx_queues; i < new_real_tx; i++) { 1460 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc_sleeping); 1461 if (qdisc != &noop_qdisc && !qdisc->handle) 1462 qdisc_hash_add(qdisc, false); 1463 } 1464 #endif 1465 } 1466 EXPORT_SYMBOL(mq_change_real_num_tx); 1467 1468 int dev_qdisc_change_tx_queue_len(struct net_device *dev) 1469 { 1470 bool up = dev->flags & IFF_UP; 1471 unsigned int i; 1472 int ret = 0; 1473 1474 if (up) 1475 dev_deactivate(dev, false); 1476 1477 for (i = 0; i < dev->num_tx_queues; i++) { 1478 ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]); 1479 1480 /* TODO: revert changes on a partial failure */ 1481 if (ret) 1482 break; 1483 } 1484 1485 if (up) 1486 dev_activate(dev); 1487 return ret; 1488 } 1489 1490 static void dev_init_scheduler_queue(struct net_device *dev, 1491 struct netdev_queue *dev_queue, 1492 void *_qdisc) 1493 { 1494 struct Qdisc *qdisc = _qdisc; 1495 1496 rcu_assign_pointer(dev_queue->qdisc, qdisc); 1497 rcu_assign_pointer(dev_queue->qdisc_sleeping, qdisc); 1498 } 1499 1500 void dev_init_scheduler(struct net_device *dev) 1501 { 1502 rcu_assign_pointer(dev->qdisc, &noop_qdisc); 1503 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc); 1504 if (dev_ingress_queue(dev)) 1505 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); 1506 1507 timer_setup(&dev->watchdog_timer, dev_watchdog, 0); 1508 } 1509 1510 void dev_shutdown(struct net_device *dev) 1511 { 1512 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); 1513 if (dev_ingress_queue(dev)) 1514 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); 1515 qdisc_put(rtnl_dereference(dev->qdisc)); 1516 rcu_assign_pointer(dev->qdisc, &noop_qdisc); 1517 1518 WARN_ON(timer_pending(&dev->watchdog_timer)); 1519 } 1520 1521 /** 1522 * psched_ratecfg_precompute__() - Pre-compute values for reciprocal division 1523 * @rate: Rate to compute reciprocal division values of 1524 * @mult: Multiplier for reciprocal division 1525 * @shift: Shift for reciprocal division 1526 * 1527 * The multiplier and shift for reciprocal division by rate are stored 1528 * in mult and shift. 1529 * 1530 * The deal here is to replace a divide by a reciprocal one 1531 * in fast path (a reciprocal divide is a multiply and a shift) 1532 * 1533 * Normal formula would be : 1534 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps 1535 * 1536 * We compute mult/shift to use instead : 1537 * time_in_ns = (len * mult) >> shift; 1538 * 1539 * We try to get the highest possible mult value for accuracy, 1540 * but have to make sure no overflows will ever happen. 1541 * 1542 * reciprocal_value() is not used here it doesn't handle 64-bit values. 1543 */ 1544 static void psched_ratecfg_precompute__(u64 rate, u32 *mult, u8 *shift) 1545 { 1546 u64 factor = NSEC_PER_SEC; 1547 1548 *mult = 1; 1549 *shift = 0; 1550 1551 if (rate <= 0) 1552 return; 1553 1554 for (;;) { 1555 *mult = div64_u64(factor, rate); 1556 if (*mult & (1U << 31) || factor & (1ULL << 63)) 1557 break; 1558 factor <<= 1; 1559 (*shift)++; 1560 } 1561 } 1562 1563 void psched_ratecfg_precompute(struct psched_ratecfg *r, 1564 const struct tc_ratespec *conf, 1565 u64 rate64) 1566 { 1567 memset(r, 0, sizeof(*r)); 1568 r->overhead = conf->overhead; 1569 r->mpu = conf->mpu; 1570 r->rate_bytes_ps = max_t(u64, conf->rate, rate64); 1571 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK); 1572 psched_ratecfg_precompute__(r->rate_bytes_ps, &r->mult, &r->shift); 1573 } 1574 EXPORT_SYMBOL(psched_ratecfg_precompute); 1575 1576 void psched_ppscfg_precompute(struct psched_pktrate *r, u64 pktrate64) 1577 { 1578 r->rate_pkts_ps = pktrate64; 1579 psched_ratecfg_precompute__(r->rate_pkts_ps, &r->mult, &r->shift); 1580 } 1581 EXPORT_SYMBOL(psched_ppscfg_precompute); 1582 1583 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp, 1584 struct tcf_proto *tp_head) 1585 { 1586 /* Protected with chain0->filter_chain_lock. 1587 * Can't access chain directly because tp_head can be NULL. 1588 */ 1589 struct mini_Qdisc *miniq_old = 1590 rcu_dereference_protected(*miniqp->p_miniq, 1); 1591 struct mini_Qdisc *miniq; 1592 1593 if (!tp_head) { 1594 RCU_INIT_POINTER(*miniqp->p_miniq, NULL); 1595 } else { 1596 miniq = miniq_old != &miniqp->miniq1 ? 1597 &miniqp->miniq1 : &miniqp->miniq2; 1598 1599 /* We need to make sure that readers won't see the miniq 1600 * we are about to modify. So ensure that at least one RCU 1601 * grace period has elapsed since the miniq was made 1602 * inactive. 1603 */ 1604 if (IS_ENABLED(CONFIG_PREEMPT_RT)) 1605 cond_synchronize_rcu(miniq->rcu_state); 1606 else if (!poll_state_synchronize_rcu(miniq->rcu_state)) 1607 synchronize_rcu_expedited(); 1608 1609 miniq->filter_list = tp_head; 1610 rcu_assign_pointer(*miniqp->p_miniq, miniq); 1611 } 1612 1613 if (miniq_old) 1614 /* This is counterpart of the rcu sync above. We need to 1615 * block potential new user of miniq_old until all readers 1616 * are not seeing it. 1617 */ 1618 miniq_old->rcu_state = start_poll_synchronize_rcu(); 1619 } 1620 EXPORT_SYMBOL(mini_qdisc_pair_swap); 1621 1622 void mini_qdisc_pair_block_init(struct mini_Qdisc_pair *miniqp, 1623 struct tcf_block *block) 1624 { 1625 miniqp->miniq1.block = block; 1626 miniqp->miniq2.block = block; 1627 } 1628 EXPORT_SYMBOL(mini_qdisc_pair_block_init); 1629 1630 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc, 1631 struct mini_Qdisc __rcu **p_miniq) 1632 { 1633 miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats; 1634 miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats; 1635 miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats; 1636 miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats; 1637 miniqp->miniq1.rcu_state = get_state_synchronize_rcu(); 1638 miniqp->miniq2.rcu_state = miniqp->miniq1.rcu_state; 1639 miniqp->p_miniq = p_miniq; 1640 } 1641 EXPORT_SYMBOL(mini_qdisc_pair_init); 1642