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