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