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