1 /* 2 * net/sched/sch_generic.c Generic packet scheduler routines. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 * Jamal Hadi Salim, <hadi@cyberus.ca> 990601 11 * - Ingress support 12 */ 13 14 #include <linux/bitops.h> 15 #include <linux/module.h> 16 #include <linux/types.h> 17 #include <linux/kernel.h> 18 #include <linux/sched.h> 19 #include <linux/string.h> 20 #include <linux/errno.h> 21 #include <linux/netdevice.h> 22 #include <linux/skbuff.h> 23 #include <linux/rtnetlink.h> 24 #include <linux/init.h> 25 #include <linux/rcupdate.h> 26 #include <linux/list.h> 27 #include <linux/slab.h> 28 #include <linux/if_vlan.h> 29 #include <net/sch_generic.h> 30 #include <net/pkt_sched.h> 31 #include <net/dst.h> 32 33 /* Qdisc to use by default */ 34 const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops; 35 EXPORT_SYMBOL(default_qdisc_ops); 36 37 /* Main transmission queue. */ 38 39 /* Modifications to data participating in scheduling must be protected with 40 * qdisc_lock(qdisc) spinlock. 41 * 42 * The idea is the following: 43 * - enqueue, dequeue are serialized via qdisc root lock 44 * - ingress filtering is also serialized via qdisc root lock 45 * - updates to tree and tree walking are only done under the rtnl mutex. 46 */ 47 48 static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q) 49 { 50 q->gso_skb = skb; 51 q->qstats.requeues++; 52 q->q.qlen++; /* it's still part of the queue */ 53 __netif_schedule(q); 54 55 return 0; 56 } 57 58 static void try_bulk_dequeue_skb(struct Qdisc *q, 59 struct sk_buff *skb, 60 const struct netdev_queue *txq, 61 int *packets) 62 { 63 int bytelimit = qdisc_avail_bulklimit(txq) - skb->len; 64 65 while (bytelimit > 0) { 66 struct sk_buff *nskb = q->dequeue(q); 67 68 if (!nskb) 69 break; 70 71 bytelimit -= nskb->len; /* covers GSO len */ 72 skb->next = nskb; 73 skb = nskb; 74 (*packets)++; /* GSO counts as one pkt */ 75 } 76 skb->next = NULL; 77 } 78 79 /* Note that dequeue_skb can possibly return a SKB list (via skb->next). 80 * A requeued skb (via q->gso_skb) can also be a SKB list. 81 */ 82 static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate, 83 int *packets) 84 { 85 struct sk_buff *skb = q->gso_skb; 86 const struct netdev_queue *txq = q->dev_queue; 87 88 *packets = 1; 89 *validate = true; 90 if (unlikely(skb)) { 91 /* check the reason of requeuing without tx lock first */ 92 txq = skb_get_tx_queue(txq->dev, skb); 93 if (!netif_xmit_frozen_or_stopped(txq)) { 94 q->gso_skb = NULL; 95 q->q.qlen--; 96 } else 97 skb = NULL; 98 /* skb in gso_skb were already validated */ 99 *validate = false; 100 } else { 101 if (!(q->flags & TCQ_F_ONETXQUEUE) || 102 !netif_xmit_frozen_or_stopped(txq)) { 103 skb = q->dequeue(q); 104 if (skb && qdisc_may_bulk(q)) 105 try_bulk_dequeue_skb(q, skb, txq, packets); 106 } 107 } 108 return skb; 109 } 110 111 /* 112 * Transmit possibly several skbs, and handle the return status as 113 * required. Holding the __QDISC___STATE_RUNNING bit guarantees that 114 * only one CPU can execute this function. 115 * 116 * Returns to the caller: 117 * 0 - queue is empty or throttled. 118 * >0 - queue is not empty. 119 */ 120 int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q, 121 struct net_device *dev, struct netdev_queue *txq, 122 spinlock_t *root_lock, bool validate) 123 { 124 int ret = NETDEV_TX_BUSY; 125 126 /* And release qdisc */ 127 spin_unlock(root_lock); 128 129 /* Note that we validate skb (GSO, checksum, ...) outside of locks */ 130 if (validate) 131 skb = validate_xmit_skb_list(skb, dev); 132 133 if (likely(skb)) { 134 HARD_TX_LOCK(dev, txq, smp_processor_id()); 135 if (!netif_xmit_frozen_or_stopped(txq)) 136 skb = dev_hard_start_xmit(skb, dev, txq, &ret); 137 138 HARD_TX_UNLOCK(dev, txq); 139 } else { 140 spin_lock(root_lock); 141 return qdisc_qlen(q); 142 } 143 spin_lock(root_lock); 144 145 if (dev_xmit_complete(ret)) { 146 /* Driver sent out skb successfully or skb was consumed */ 147 ret = qdisc_qlen(q); 148 } else { 149 /* Driver returned NETDEV_TX_BUSY - requeue skb */ 150 if (unlikely(ret != NETDEV_TX_BUSY)) 151 net_warn_ratelimited("BUG %s code %d qlen %d\n", 152 dev->name, ret, q->q.qlen); 153 154 ret = dev_requeue_skb(skb, q); 155 } 156 157 if (ret && netif_xmit_frozen_or_stopped(txq)) 158 ret = 0; 159 160 return ret; 161 } 162 163 /* 164 * NOTE: Called under qdisc_lock(q) with locally disabled BH. 165 * 166 * __QDISC___STATE_RUNNING guarantees only one CPU can process 167 * this qdisc at a time. qdisc_lock(q) serializes queue accesses for 168 * this queue. 169 * 170 * netif_tx_lock serializes accesses to device driver. 171 * 172 * qdisc_lock(q) and netif_tx_lock are mutually exclusive, 173 * if one is grabbed, another must be free. 174 * 175 * Note, that this procedure can be called by a watchdog timer 176 * 177 * Returns to the caller: 178 * 0 - queue is empty or throttled. 179 * >0 - queue is not empty. 180 * 181 */ 182 static inline int qdisc_restart(struct Qdisc *q, int *packets) 183 { 184 struct netdev_queue *txq; 185 struct net_device *dev; 186 spinlock_t *root_lock; 187 struct sk_buff *skb; 188 bool validate; 189 190 /* Dequeue packet */ 191 skb = dequeue_skb(q, &validate, packets); 192 if (unlikely(!skb)) 193 return 0; 194 195 root_lock = qdisc_lock(q); 196 dev = qdisc_dev(q); 197 txq = skb_get_tx_queue(dev, skb); 198 199 return sch_direct_xmit(skb, q, dev, txq, root_lock, validate); 200 } 201 202 void __qdisc_run(struct Qdisc *q) 203 { 204 int quota = weight_p; 205 int packets; 206 207 while (qdisc_restart(q, &packets)) { 208 /* 209 * Ordered by possible occurrence: Postpone processing if 210 * 1. we've exceeded packet quota 211 * 2. another process needs the CPU; 212 */ 213 quota -= packets; 214 if (quota <= 0 || need_resched()) { 215 __netif_schedule(q); 216 break; 217 } 218 } 219 220 qdisc_run_end(q); 221 } 222 223 unsigned long dev_trans_start(struct net_device *dev) 224 { 225 unsigned long val, res; 226 unsigned int i; 227 228 if (is_vlan_dev(dev)) 229 dev = vlan_dev_real_dev(dev); 230 res = netdev_get_tx_queue(dev, 0)->trans_start; 231 for (i = 1; i < dev->num_tx_queues; i++) { 232 val = netdev_get_tx_queue(dev, i)->trans_start; 233 if (val && time_after(val, res)) 234 res = val; 235 } 236 237 return res; 238 } 239 EXPORT_SYMBOL(dev_trans_start); 240 241 static void dev_watchdog(unsigned long arg) 242 { 243 struct net_device *dev = (struct net_device *)arg; 244 245 netif_tx_lock(dev); 246 if (!qdisc_tx_is_noop(dev)) { 247 if (netif_device_present(dev) && 248 netif_running(dev) && 249 netif_carrier_ok(dev)) { 250 int some_queue_timedout = 0; 251 unsigned int i; 252 unsigned long trans_start; 253 254 for (i = 0; i < dev->num_tx_queues; i++) { 255 struct netdev_queue *txq; 256 257 txq = netdev_get_tx_queue(dev, i); 258 trans_start = txq->trans_start; 259 if (netif_xmit_stopped(txq) && 260 time_after(jiffies, (trans_start + 261 dev->watchdog_timeo))) { 262 some_queue_timedout = 1; 263 txq->trans_timeout++; 264 break; 265 } 266 } 267 268 if (some_queue_timedout) { 269 WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n", 270 dev->name, netdev_drivername(dev), i); 271 dev->netdev_ops->ndo_tx_timeout(dev); 272 } 273 if (!mod_timer(&dev->watchdog_timer, 274 round_jiffies(jiffies + 275 dev->watchdog_timeo))) 276 dev_hold(dev); 277 } 278 } 279 netif_tx_unlock(dev); 280 281 dev_put(dev); 282 } 283 284 void __netdev_watchdog_up(struct net_device *dev) 285 { 286 if (dev->netdev_ops->ndo_tx_timeout) { 287 if (dev->watchdog_timeo <= 0) 288 dev->watchdog_timeo = 5*HZ; 289 if (!mod_timer(&dev->watchdog_timer, 290 round_jiffies(jiffies + dev->watchdog_timeo))) 291 dev_hold(dev); 292 } 293 } 294 295 static void dev_watchdog_up(struct net_device *dev) 296 { 297 __netdev_watchdog_up(dev); 298 } 299 300 static void dev_watchdog_down(struct net_device *dev) 301 { 302 netif_tx_lock_bh(dev); 303 if (del_timer(&dev->watchdog_timer)) 304 dev_put(dev); 305 netif_tx_unlock_bh(dev); 306 } 307 308 /** 309 * netif_carrier_on - set carrier 310 * @dev: network device 311 * 312 * Device has detected that carrier. 313 */ 314 void netif_carrier_on(struct net_device *dev) 315 { 316 if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { 317 if (dev->reg_state == NETREG_UNINITIALIZED) 318 return; 319 atomic_inc(&dev->carrier_changes); 320 linkwatch_fire_event(dev); 321 if (netif_running(dev)) 322 __netdev_watchdog_up(dev); 323 } 324 } 325 EXPORT_SYMBOL(netif_carrier_on); 326 327 /** 328 * netif_carrier_off - clear carrier 329 * @dev: network device 330 * 331 * Device has detected loss of carrier. 332 */ 333 void netif_carrier_off(struct net_device *dev) 334 { 335 if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) { 336 if (dev->reg_state == NETREG_UNINITIALIZED) 337 return; 338 atomic_inc(&dev->carrier_changes); 339 linkwatch_fire_event(dev); 340 } 341 } 342 EXPORT_SYMBOL(netif_carrier_off); 343 344 /* "NOOP" scheduler: the best scheduler, recommended for all interfaces 345 under all circumstances. It is difficult to invent anything faster or 346 cheaper. 347 */ 348 349 static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc) 350 { 351 kfree_skb(skb); 352 return NET_XMIT_CN; 353 } 354 355 static struct sk_buff *noop_dequeue(struct Qdisc *qdisc) 356 { 357 return NULL; 358 } 359 360 struct Qdisc_ops noop_qdisc_ops __read_mostly = { 361 .id = "noop", 362 .priv_size = 0, 363 .enqueue = noop_enqueue, 364 .dequeue = noop_dequeue, 365 .peek = noop_dequeue, 366 .owner = THIS_MODULE, 367 }; 368 369 static struct netdev_queue noop_netdev_queue = { 370 .qdisc = &noop_qdisc, 371 .qdisc_sleeping = &noop_qdisc, 372 }; 373 374 struct Qdisc noop_qdisc = { 375 .enqueue = noop_enqueue, 376 .dequeue = noop_dequeue, 377 .flags = TCQ_F_BUILTIN, 378 .ops = &noop_qdisc_ops, 379 .list = LIST_HEAD_INIT(noop_qdisc.list), 380 .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock), 381 .dev_queue = &noop_netdev_queue, 382 .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock), 383 }; 384 EXPORT_SYMBOL(noop_qdisc); 385 386 static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt) 387 { 388 /* register_qdisc() assigns a default of noop_enqueue if unset, 389 * but __dev_queue_xmit() treats noqueue only as such 390 * if this is NULL - so clear it here. */ 391 qdisc->enqueue = NULL; 392 return 0; 393 } 394 395 struct Qdisc_ops noqueue_qdisc_ops __read_mostly = { 396 .id = "noqueue", 397 .priv_size = 0, 398 .init = noqueue_init, 399 .enqueue = noop_enqueue, 400 .dequeue = noop_dequeue, 401 .peek = noop_dequeue, 402 .owner = THIS_MODULE, 403 }; 404 405 static const u8 prio2band[TC_PRIO_MAX + 1] = { 406 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 407 }; 408 409 /* 3-band FIFO queue: old style, but should be a bit faster than 410 generic prio+fifo combination. 411 */ 412 413 #define PFIFO_FAST_BANDS 3 414 415 /* 416 * Private data for a pfifo_fast scheduler containing: 417 * - queues for the three band 418 * - bitmap indicating which of the bands contain skbs 419 */ 420 struct pfifo_fast_priv { 421 u32 bitmap; 422 struct sk_buff_head q[PFIFO_FAST_BANDS]; 423 }; 424 425 /* 426 * Convert a bitmap to the first band number where an skb is queued, where: 427 * bitmap=0 means there are no skbs on any band. 428 * bitmap=1 means there is an skb on band 0. 429 * bitmap=7 means there are skbs on all 3 bands, etc. 430 */ 431 static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0}; 432 433 static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv, 434 int band) 435 { 436 return priv->q + band; 437 } 438 439 static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc) 440 { 441 if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) { 442 int band = prio2band[skb->priority & TC_PRIO_MAX]; 443 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 444 struct sk_buff_head *list = band2list(priv, band); 445 446 priv->bitmap |= (1 << band); 447 qdisc->q.qlen++; 448 return __qdisc_enqueue_tail(skb, qdisc, list); 449 } 450 451 return qdisc_drop(skb, qdisc); 452 } 453 454 static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc) 455 { 456 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 457 int band = bitmap2band[priv->bitmap]; 458 459 if (likely(band >= 0)) { 460 struct sk_buff_head *list = band2list(priv, band); 461 struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list); 462 463 qdisc->q.qlen--; 464 if (skb_queue_empty(list)) 465 priv->bitmap &= ~(1 << band); 466 467 return skb; 468 } 469 470 return NULL; 471 } 472 473 static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc) 474 { 475 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 476 int band = bitmap2band[priv->bitmap]; 477 478 if (band >= 0) { 479 struct sk_buff_head *list = band2list(priv, band); 480 481 return skb_peek(list); 482 } 483 484 return NULL; 485 } 486 487 static void pfifo_fast_reset(struct Qdisc *qdisc) 488 { 489 int prio; 490 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 491 492 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) 493 __qdisc_reset_queue(qdisc, band2list(priv, prio)); 494 495 priv->bitmap = 0; 496 qdisc->qstats.backlog = 0; 497 qdisc->q.qlen = 0; 498 } 499 500 static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb) 501 { 502 struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS }; 503 504 memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1); 505 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt)) 506 goto nla_put_failure; 507 return skb->len; 508 509 nla_put_failure: 510 return -1; 511 } 512 513 static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt) 514 { 515 int prio; 516 struct pfifo_fast_priv *priv = qdisc_priv(qdisc); 517 518 for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) 519 __skb_queue_head_init(band2list(priv, prio)); 520 521 /* Can by-pass the queue discipline */ 522 qdisc->flags |= TCQ_F_CAN_BYPASS; 523 return 0; 524 } 525 526 struct Qdisc_ops pfifo_fast_ops __read_mostly = { 527 .id = "pfifo_fast", 528 .priv_size = sizeof(struct pfifo_fast_priv), 529 .enqueue = pfifo_fast_enqueue, 530 .dequeue = pfifo_fast_dequeue, 531 .peek = pfifo_fast_peek, 532 .init = pfifo_fast_init, 533 .reset = pfifo_fast_reset, 534 .dump = pfifo_fast_dump, 535 .owner = THIS_MODULE, 536 }; 537 EXPORT_SYMBOL(pfifo_fast_ops); 538 539 static struct lock_class_key qdisc_tx_busylock; 540 541 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, 542 const struct Qdisc_ops *ops) 543 { 544 void *p; 545 struct Qdisc *sch; 546 unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size; 547 int err = -ENOBUFS; 548 struct net_device *dev = dev_queue->dev; 549 550 p = kzalloc_node(size, GFP_KERNEL, 551 netdev_queue_numa_node_read(dev_queue)); 552 553 if (!p) 554 goto errout; 555 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); 556 /* if we got non aligned memory, ask more and do alignment ourself */ 557 if (sch != p) { 558 kfree(p); 559 p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL, 560 netdev_queue_numa_node_read(dev_queue)); 561 if (!p) 562 goto errout; 563 sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p); 564 sch->padded = (char *) sch - (char *) p; 565 } 566 INIT_LIST_HEAD(&sch->list); 567 skb_queue_head_init(&sch->q); 568 569 spin_lock_init(&sch->busylock); 570 lockdep_set_class(&sch->busylock, 571 dev->qdisc_tx_busylock ?: &qdisc_tx_busylock); 572 573 sch->ops = ops; 574 sch->enqueue = ops->enqueue; 575 sch->dequeue = ops->dequeue; 576 sch->dev_queue = dev_queue; 577 dev_hold(dev); 578 atomic_set(&sch->refcnt, 1); 579 580 return sch; 581 errout: 582 return ERR_PTR(err); 583 } 584 585 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, 586 const struct Qdisc_ops *ops, 587 unsigned int parentid) 588 { 589 struct Qdisc *sch; 590 591 if (!try_module_get(ops->owner)) 592 goto errout; 593 594 sch = qdisc_alloc(dev_queue, ops); 595 if (IS_ERR(sch)) 596 goto errout; 597 sch->parent = parentid; 598 599 if (!ops->init || ops->init(sch, NULL) == 0) 600 return sch; 601 602 qdisc_destroy(sch); 603 errout: 604 return NULL; 605 } 606 EXPORT_SYMBOL(qdisc_create_dflt); 607 608 /* Under qdisc_lock(qdisc) and BH! */ 609 610 void qdisc_reset(struct Qdisc *qdisc) 611 { 612 const struct Qdisc_ops *ops = qdisc->ops; 613 614 if (ops->reset) 615 ops->reset(qdisc); 616 617 if (qdisc->gso_skb) { 618 kfree_skb_list(qdisc->gso_skb); 619 qdisc->gso_skb = NULL; 620 qdisc->q.qlen = 0; 621 } 622 } 623 EXPORT_SYMBOL(qdisc_reset); 624 625 static void qdisc_rcu_free(struct rcu_head *head) 626 { 627 struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head); 628 629 if (qdisc_is_percpu_stats(qdisc)) { 630 free_percpu(qdisc->cpu_bstats); 631 free_percpu(qdisc->cpu_qstats); 632 } 633 634 kfree((char *) qdisc - qdisc->padded); 635 } 636 637 void qdisc_destroy(struct Qdisc *qdisc) 638 { 639 const struct Qdisc_ops *ops = qdisc->ops; 640 641 if (qdisc->flags & TCQ_F_BUILTIN || 642 !atomic_dec_and_test(&qdisc->refcnt)) 643 return; 644 645 #ifdef CONFIG_NET_SCHED 646 qdisc_list_del(qdisc); 647 648 qdisc_put_stab(rtnl_dereference(qdisc->stab)); 649 #endif 650 gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est); 651 if (ops->reset) 652 ops->reset(qdisc); 653 if (ops->destroy) 654 ops->destroy(qdisc); 655 656 module_put(ops->owner); 657 dev_put(qdisc_dev(qdisc)); 658 659 kfree_skb_list(qdisc->gso_skb); 660 /* 661 * gen_estimator est_timer() might access qdisc->q.lock, 662 * wait a RCU grace period before freeing qdisc. 663 */ 664 call_rcu(&qdisc->rcu_head, qdisc_rcu_free); 665 } 666 EXPORT_SYMBOL(qdisc_destroy); 667 668 /* Attach toplevel qdisc to device queue. */ 669 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, 670 struct Qdisc *qdisc) 671 { 672 struct Qdisc *oqdisc = dev_queue->qdisc_sleeping; 673 spinlock_t *root_lock; 674 675 root_lock = qdisc_lock(oqdisc); 676 spin_lock_bh(root_lock); 677 678 /* Prune old scheduler */ 679 if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1) 680 qdisc_reset(oqdisc); 681 682 /* ... and graft new one */ 683 if (qdisc == NULL) 684 qdisc = &noop_qdisc; 685 dev_queue->qdisc_sleeping = qdisc; 686 rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc); 687 688 spin_unlock_bh(root_lock); 689 690 return oqdisc; 691 } 692 EXPORT_SYMBOL(dev_graft_qdisc); 693 694 static void attach_one_default_qdisc(struct net_device *dev, 695 struct netdev_queue *dev_queue, 696 void *_unused) 697 { 698 struct Qdisc *qdisc; 699 const struct Qdisc_ops *ops = default_qdisc_ops; 700 701 if (dev->priv_flags & IFF_NO_QUEUE) 702 ops = &noqueue_qdisc_ops; 703 704 qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT); 705 if (!qdisc) { 706 netdev_info(dev, "activation failed\n"); 707 return; 708 } 709 if (!netif_is_multiqueue(dev)) 710 qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT; 711 dev_queue->qdisc_sleeping = qdisc; 712 } 713 714 static void attach_default_qdiscs(struct net_device *dev) 715 { 716 struct netdev_queue *txq; 717 struct Qdisc *qdisc; 718 719 txq = netdev_get_tx_queue(dev, 0); 720 721 if (!netif_is_multiqueue(dev) || 722 dev->priv_flags & IFF_NO_QUEUE) { 723 netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL); 724 dev->qdisc = txq->qdisc_sleeping; 725 atomic_inc(&dev->qdisc->refcnt); 726 } else { 727 qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT); 728 if (qdisc) { 729 dev->qdisc = qdisc; 730 qdisc->ops->attach(qdisc); 731 } 732 } 733 } 734 735 static void transition_one_qdisc(struct net_device *dev, 736 struct netdev_queue *dev_queue, 737 void *_need_watchdog) 738 { 739 struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping; 740 int *need_watchdog_p = _need_watchdog; 741 742 if (!(new_qdisc->flags & TCQ_F_BUILTIN)) 743 clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state); 744 745 rcu_assign_pointer(dev_queue->qdisc, new_qdisc); 746 if (need_watchdog_p) { 747 dev_queue->trans_start = 0; 748 *need_watchdog_p = 1; 749 } 750 } 751 752 void dev_activate(struct net_device *dev) 753 { 754 int need_watchdog; 755 756 /* No queueing discipline is attached to device; 757 * create default one for devices, which need queueing 758 * and noqueue_qdisc for virtual interfaces 759 */ 760 761 if (dev->qdisc == &noop_qdisc) 762 attach_default_qdiscs(dev); 763 764 if (!netif_carrier_ok(dev)) 765 /* Delay activation until next carrier-on event */ 766 return; 767 768 need_watchdog = 0; 769 netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog); 770 if (dev_ingress_queue(dev)) 771 transition_one_qdisc(dev, dev_ingress_queue(dev), NULL); 772 773 if (need_watchdog) { 774 netif_trans_update(dev); 775 dev_watchdog_up(dev); 776 } 777 } 778 EXPORT_SYMBOL(dev_activate); 779 780 static void dev_deactivate_queue(struct net_device *dev, 781 struct netdev_queue *dev_queue, 782 void *_qdisc_default) 783 { 784 struct Qdisc *qdisc_default = _qdisc_default; 785 struct Qdisc *qdisc; 786 787 qdisc = rtnl_dereference(dev_queue->qdisc); 788 if (qdisc) { 789 spin_lock_bh(qdisc_lock(qdisc)); 790 791 if (!(qdisc->flags & TCQ_F_BUILTIN)) 792 set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state); 793 794 rcu_assign_pointer(dev_queue->qdisc, qdisc_default); 795 qdisc_reset(qdisc); 796 797 spin_unlock_bh(qdisc_lock(qdisc)); 798 } 799 } 800 801 static bool some_qdisc_is_busy(struct net_device *dev) 802 { 803 unsigned int i; 804 805 for (i = 0; i < dev->num_tx_queues; i++) { 806 struct netdev_queue *dev_queue; 807 spinlock_t *root_lock; 808 struct Qdisc *q; 809 int val; 810 811 dev_queue = netdev_get_tx_queue(dev, i); 812 q = dev_queue->qdisc_sleeping; 813 root_lock = qdisc_lock(q); 814 815 spin_lock_bh(root_lock); 816 817 val = (qdisc_is_running(q) || 818 test_bit(__QDISC_STATE_SCHED, &q->state)); 819 820 spin_unlock_bh(root_lock); 821 822 if (val) 823 return true; 824 } 825 return false; 826 } 827 828 /** 829 * dev_deactivate_many - deactivate transmissions on several devices 830 * @head: list of devices to deactivate 831 * 832 * This function returns only when all outstanding transmissions 833 * have completed, unless all devices are in dismantle phase. 834 */ 835 void dev_deactivate_many(struct list_head *head) 836 { 837 struct net_device *dev; 838 bool sync_needed = false; 839 840 list_for_each_entry(dev, head, close_list) { 841 netdev_for_each_tx_queue(dev, dev_deactivate_queue, 842 &noop_qdisc); 843 if (dev_ingress_queue(dev)) 844 dev_deactivate_queue(dev, dev_ingress_queue(dev), 845 &noop_qdisc); 846 847 dev_watchdog_down(dev); 848 sync_needed |= !dev->dismantle; 849 } 850 851 /* Wait for outstanding qdisc-less dev_queue_xmit calls. 852 * This is avoided if all devices are in dismantle phase : 853 * Caller will call synchronize_net() for us 854 */ 855 if (sync_needed) 856 synchronize_net(); 857 858 /* Wait for outstanding qdisc_run calls. */ 859 list_for_each_entry(dev, head, close_list) 860 while (some_qdisc_is_busy(dev)) 861 yield(); 862 } 863 864 void dev_deactivate(struct net_device *dev) 865 { 866 LIST_HEAD(single); 867 868 list_add(&dev->close_list, &single); 869 dev_deactivate_many(&single); 870 list_del(&single); 871 } 872 EXPORT_SYMBOL(dev_deactivate); 873 874 static void dev_init_scheduler_queue(struct net_device *dev, 875 struct netdev_queue *dev_queue, 876 void *_qdisc) 877 { 878 struct Qdisc *qdisc = _qdisc; 879 880 rcu_assign_pointer(dev_queue->qdisc, qdisc); 881 dev_queue->qdisc_sleeping = qdisc; 882 } 883 884 void dev_init_scheduler(struct net_device *dev) 885 { 886 dev->qdisc = &noop_qdisc; 887 netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc); 888 if (dev_ingress_queue(dev)) 889 dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); 890 891 setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev); 892 } 893 894 static void shutdown_scheduler_queue(struct net_device *dev, 895 struct netdev_queue *dev_queue, 896 void *_qdisc_default) 897 { 898 struct Qdisc *qdisc = dev_queue->qdisc_sleeping; 899 struct Qdisc *qdisc_default = _qdisc_default; 900 901 if (qdisc) { 902 rcu_assign_pointer(dev_queue->qdisc, qdisc_default); 903 dev_queue->qdisc_sleeping = qdisc_default; 904 905 qdisc_destroy(qdisc); 906 } 907 } 908 909 void dev_shutdown(struct net_device *dev) 910 { 911 netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc); 912 if (dev_ingress_queue(dev)) 913 shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc); 914 qdisc_destroy(dev->qdisc); 915 dev->qdisc = &noop_qdisc; 916 917 WARN_ON(timer_pending(&dev->watchdog_timer)); 918 } 919 920 void psched_ratecfg_precompute(struct psched_ratecfg *r, 921 const struct tc_ratespec *conf, 922 u64 rate64) 923 { 924 memset(r, 0, sizeof(*r)); 925 r->overhead = conf->overhead; 926 r->rate_bytes_ps = max_t(u64, conf->rate, rate64); 927 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK); 928 r->mult = 1; 929 /* 930 * The deal here is to replace a divide by a reciprocal one 931 * in fast path (a reciprocal divide is a multiply and a shift) 932 * 933 * Normal formula would be : 934 * time_in_ns = (NSEC_PER_SEC * len) / rate_bps 935 * 936 * We compute mult/shift to use instead : 937 * time_in_ns = (len * mult) >> shift; 938 * 939 * We try to get the highest possible mult value for accuracy, 940 * but have to make sure no overflows will ever happen. 941 */ 942 if (r->rate_bytes_ps > 0) { 943 u64 factor = NSEC_PER_SEC; 944 945 for (;;) { 946 r->mult = div64_u64(factor, r->rate_bytes_ps); 947 if (r->mult & (1U << 31) || factor & (1ULL << 63)) 948 break; 949 factor <<= 1; 950 r->shift++; 951 } 952 } 953 } 954 EXPORT_SYMBOL(psched_ratecfg_precompute); 955