sch_generic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #include <linux/bitops.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/rtnetlink.h>
  23. #include <linux/init.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/list.h>
  26. #include <net/pkt_sched.h>
  27. /* Main transmission queue. */
  28. /* Modifications to data participating in scheduling must be protected with
  29. * queue->lock spinlock.
  30. *
  31. * The idea is the following:
  32. * - enqueue, dequeue are serialized via top level device
  33. * spinlock queue->lock.
  34. * - ingress filtering is serialized via top level device
  35. * spinlock dev->rx_queue.lock.
  36. * - updates to tree and tree walking are only done under the rtnl mutex.
  37. */
  38. void qdisc_lock_tree(struct net_device *dev)
  39. __acquires(dev->rx_queue.lock)
  40. {
  41. unsigned int i;
  42. local_bh_disable();
  43. for (i = 0; i < dev->num_tx_queues; i++) {
  44. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  45. spin_lock(&txq->lock);
  46. }
  47. spin_lock(&dev->rx_queue.lock);
  48. }
  49. EXPORT_SYMBOL(qdisc_lock_tree);
  50. void qdisc_unlock_tree(struct net_device *dev)
  51. __releases(dev->rx_queue.lock)
  52. {
  53. unsigned int i;
  54. spin_unlock(&dev->rx_queue.lock);
  55. for (i = 0; i < dev->num_tx_queues; i++) {
  56. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  57. spin_unlock(&txq->lock);
  58. }
  59. local_bh_enable();
  60. }
  61. EXPORT_SYMBOL(qdisc_unlock_tree);
  62. static inline int qdisc_qlen(struct Qdisc *q)
  63. {
  64. return q->q.qlen;
  65. }
  66. static inline int dev_requeue_skb(struct sk_buff *skb,
  67. struct netdev_queue *dev_queue,
  68. struct Qdisc *q)
  69. {
  70. if (unlikely(skb->next))
  71. q->gso_skb = skb;
  72. else
  73. q->ops->requeue(skb, q);
  74. netif_schedule_queue(dev_queue);
  75. return 0;
  76. }
  77. static inline struct sk_buff *dequeue_skb(struct Qdisc *q)
  78. {
  79. struct sk_buff *skb;
  80. if ((skb = q->gso_skb))
  81. q->gso_skb = NULL;
  82. else
  83. skb = q->dequeue(q);
  84. return skb;
  85. }
  86. static inline int handle_dev_cpu_collision(struct sk_buff *skb,
  87. struct netdev_queue *dev_queue,
  88. struct Qdisc *q)
  89. {
  90. int ret;
  91. if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
  92. /*
  93. * Same CPU holding the lock. It may be a transient
  94. * configuration error, when hard_start_xmit() recurses. We
  95. * detect it by checking xmit owner and drop the packet when
  96. * deadloop is detected. Return OK to try the next skb.
  97. */
  98. kfree_skb(skb);
  99. if (net_ratelimit())
  100. printk(KERN_WARNING "Dead loop on netdevice %s, "
  101. "fix it urgently!\n", dev_queue->dev->name);
  102. ret = qdisc_qlen(q);
  103. } else {
  104. /*
  105. * Another cpu is holding lock, requeue & delay xmits for
  106. * some time.
  107. */
  108. __get_cpu_var(netdev_rx_stat).cpu_collision++;
  109. ret = dev_requeue_skb(skb, dev_queue, q);
  110. }
  111. return ret;
  112. }
  113. /*
  114. * NOTE: Called under queue->lock with locally disabled BH.
  115. *
  116. * __QDISC_STATE_RUNNING guarantees only one CPU can process
  117. * this qdisc at a time. queue->lock serializes queue accesses for
  118. * this queue AND txq->qdisc pointer itself.
  119. *
  120. * netif_tx_lock serializes accesses to device driver.
  121. *
  122. * queue->lock and netif_tx_lock are mutually exclusive,
  123. * if one is grabbed, another must be free.
  124. *
  125. * Note, that this procedure can be called by a watchdog timer
  126. *
  127. * Returns to the caller:
  128. * 0 - queue is empty or throttled.
  129. * >0 - queue is not empty.
  130. *
  131. */
  132. static inline int qdisc_restart(struct netdev_queue *txq,
  133. struct Qdisc *q)
  134. {
  135. int ret = NETDEV_TX_BUSY;
  136. struct net_device *dev;
  137. struct sk_buff *skb;
  138. /* Dequeue packet */
  139. if (unlikely((skb = dequeue_skb(q)) == NULL))
  140. return 0;
  141. /* And release queue */
  142. spin_unlock(&txq->lock);
  143. dev = txq->dev;
  144. HARD_TX_LOCK(dev, txq, smp_processor_id());
  145. if (!netif_subqueue_stopped(dev, skb))
  146. ret = dev_hard_start_xmit(skb, dev, txq);
  147. HARD_TX_UNLOCK(dev, txq);
  148. spin_lock(&txq->lock);
  149. switch (ret) {
  150. case NETDEV_TX_OK:
  151. /* Driver sent out skb successfully */
  152. ret = qdisc_qlen(q);
  153. break;
  154. case NETDEV_TX_LOCKED:
  155. /* Driver try lock failed */
  156. ret = handle_dev_cpu_collision(skb, txq, q);
  157. break;
  158. default:
  159. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  160. if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
  161. printk(KERN_WARNING "BUG %s code %d qlen %d\n",
  162. dev->name, ret, q->q.qlen);
  163. ret = dev_requeue_skb(skb, txq, q);
  164. break;
  165. }
  166. return ret;
  167. }
  168. void __qdisc_run(struct netdev_queue *txq)
  169. {
  170. unsigned long start_time = jiffies;
  171. struct Qdisc *q = txq->qdisc;
  172. while (qdisc_restart(txq, q)) {
  173. if (netif_tx_queue_stopped(txq))
  174. break;
  175. /*
  176. * Postpone processing if
  177. * 1. another process needs the CPU;
  178. * 2. we've been doing it for too long.
  179. */
  180. if (need_resched() || jiffies != start_time) {
  181. netif_schedule_queue(txq);
  182. break;
  183. }
  184. }
  185. clear_bit(__QDISC_STATE_RUNNING, &q->state);
  186. }
  187. static void dev_watchdog(unsigned long arg)
  188. {
  189. struct net_device *dev = (struct net_device *)arg;
  190. netif_tx_lock(dev);
  191. if (!qdisc_tx_is_noop(dev)) {
  192. if (netif_device_present(dev) &&
  193. netif_running(dev) &&
  194. netif_carrier_ok(dev)) {
  195. int some_queue_stopped = 0;
  196. unsigned int i;
  197. for (i = 0; i < dev->num_tx_queues; i++) {
  198. struct netdev_queue *txq;
  199. txq = netdev_get_tx_queue(dev, i);
  200. if (netif_tx_queue_stopped(txq)) {
  201. some_queue_stopped = 1;
  202. break;
  203. }
  204. }
  205. if (some_queue_stopped &&
  206. time_after(jiffies, (dev->trans_start +
  207. dev->watchdog_timeo))) {
  208. printk(KERN_INFO "NETDEV WATCHDOG: %s: "
  209. "transmit timed out\n",
  210. dev->name);
  211. dev->tx_timeout(dev);
  212. WARN_ON_ONCE(1);
  213. }
  214. if (!mod_timer(&dev->watchdog_timer,
  215. round_jiffies(jiffies +
  216. dev->watchdog_timeo)))
  217. dev_hold(dev);
  218. }
  219. }
  220. netif_tx_unlock(dev);
  221. dev_put(dev);
  222. }
  223. void __netdev_watchdog_up(struct net_device *dev)
  224. {
  225. if (dev->tx_timeout) {
  226. if (dev->watchdog_timeo <= 0)
  227. dev->watchdog_timeo = 5*HZ;
  228. if (!mod_timer(&dev->watchdog_timer,
  229. round_jiffies(jiffies + dev->watchdog_timeo)))
  230. dev_hold(dev);
  231. }
  232. }
  233. static void dev_watchdog_up(struct net_device *dev)
  234. {
  235. __netdev_watchdog_up(dev);
  236. }
  237. static void dev_watchdog_down(struct net_device *dev)
  238. {
  239. netif_tx_lock_bh(dev);
  240. if (del_timer(&dev->watchdog_timer))
  241. dev_put(dev);
  242. netif_tx_unlock_bh(dev);
  243. }
  244. /**
  245. * netif_carrier_on - set carrier
  246. * @dev: network device
  247. *
  248. * Device has detected that carrier.
  249. */
  250. void netif_carrier_on(struct net_device *dev)
  251. {
  252. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  253. linkwatch_fire_event(dev);
  254. if (netif_running(dev))
  255. __netdev_watchdog_up(dev);
  256. }
  257. }
  258. EXPORT_SYMBOL(netif_carrier_on);
  259. /**
  260. * netif_carrier_off - clear carrier
  261. * @dev: network device
  262. *
  263. * Device has detected loss of carrier.
  264. */
  265. void netif_carrier_off(struct net_device *dev)
  266. {
  267. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
  268. linkwatch_fire_event(dev);
  269. }
  270. EXPORT_SYMBOL(netif_carrier_off);
  271. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  272. under all circumstances. It is difficult to invent anything faster or
  273. cheaper.
  274. */
  275. static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
  276. {
  277. kfree_skb(skb);
  278. return NET_XMIT_CN;
  279. }
  280. static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
  281. {
  282. return NULL;
  283. }
  284. static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  285. {
  286. if (net_ratelimit())
  287. printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
  288. skb->dev->name);
  289. kfree_skb(skb);
  290. return NET_XMIT_CN;
  291. }
  292. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  293. .id = "noop",
  294. .priv_size = 0,
  295. .enqueue = noop_enqueue,
  296. .dequeue = noop_dequeue,
  297. .requeue = noop_requeue,
  298. .owner = THIS_MODULE,
  299. };
  300. struct Qdisc noop_qdisc = {
  301. .enqueue = noop_enqueue,
  302. .dequeue = noop_dequeue,
  303. .flags = TCQ_F_BUILTIN,
  304. .ops = &noop_qdisc_ops,
  305. .list = LIST_HEAD_INIT(noop_qdisc.list),
  306. };
  307. EXPORT_SYMBOL(noop_qdisc);
  308. static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  309. .id = "noqueue",
  310. .priv_size = 0,
  311. .enqueue = noop_enqueue,
  312. .dequeue = noop_dequeue,
  313. .requeue = noop_requeue,
  314. .owner = THIS_MODULE,
  315. };
  316. static struct Qdisc noqueue_qdisc = {
  317. .enqueue = NULL,
  318. .dequeue = noop_dequeue,
  319. .flags = TCQ_F_BUILTIN,
  320. .ops = &noqueue_qdisc_ops,
  321. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  322. };
  323. static const u8 prio2band[TC_PRIO_MAX+1] =
  324. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  325. /* 3-band FIFO queue: old style, but should be a bit faster than
  326. generic prio+fifo combination.
  327. */
  328. #define PFIFO_FAST_BANDS 3
  329. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  330. struct Qdisc *qdisc)
  331. {
  332. struct sk_buff_head *list = qdisc_priv(qdisc);
  333. return list + prio2band[skb->priority & TC_PRIO_MAX];
  334. }
  335. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  336. {
  337. struct sk_buff_head *list = prio2list(skb, qdisc);
  338. if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
  339. qdisc->q.qlen++;
  340. return __qdisc_enqueue_tail(skb, qdisc, list);
  341. }
  342. return qdisc_drop(skb, qdisc);
  343. }
  344. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  345. {
  346. int prio;
  347. struct sk_buff_head *list = qdisc_priv(qdisc);
  348. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  349. if (!skb_queue_empty(list + prio)) {
  350. qdisc->q.qlen--;
  351. return __qdisc_dequeue_head(qdisc, list + prio);
  352. }
  353. }
  354. return NULL;
  355. }
  356. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  357. {
  358. qdisc->q.qlen++;
  359. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  360. }
  361. static void pfifo_fast_reset(struct Qdisc* qdisc)
  362. {
  363. int prio;
  364. struct sk_buff_head *list = qdisc_priv(qdisc);
  365. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  366. __qdisc_reset_queue(qdisc, list + prio);
  367. qdisc->qstats.backlog = 0;
  368. qdisc->q.qlen = 0;
  369. }
  370. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  371. {
  372. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  373. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  374. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  375. return skb->len;
  376. nla_put_failure:
  377. return -1;
  378. }
  379. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  380. {
  381. int prio;
  382. struct sk_buff_head *list = qdisc_priv(qdisc);
  383. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  384. skb_queue_head_init(list + prio);
  385. return 0;
  386. }
  387. static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  388. .id = "pfifo_fast",
  389. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  390. .enqueue = pfifo_fast_enqueue,
  391. .dequeue = pfifo_fast_dequeue,
  392. .requeue = pfifo_fast_requeue,
  393. .init = pfifo_fast_init,
  394. .reset = pfifo_fast_reset,
  395. .dump = pfifo_fast_dump,
  396. .owner = THIS_MODULE,
  397. };
  398. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  399. struct Qdisc_ops *ops)
  400. {
  401. void *p;
  402. struct Qdisc *sch;
  403. unsigned int size;
  404. int err = -ENOBUFS;
  405. /* ensure that the Qdisc and the private data are 32-byte aligned */
  406. size = QDISC_ALIGN(sizeof(*sch));
  407. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  408. p = kzalloc(size, GFP_KERNEL);
  409. if (!p)
  410. goto errout;
  411. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  412. sch->padded = (char *) sch - (char *) p;
  413. INIT_LIST_HEAD(&sch->list);
  414. skb_queue_head_init(&sch->q);
  415. sch->ops = ops;
  416. sch->enqueue = ops->enqueue;
  417. sch->dequeue = ops->dequeue;
  418. sch->dev_queue = dev_queue;
  419. dev_hold(qdisc_dev(sch));
  420. atomic_set(&sch->refcnt, 1);
  421. return sch;
  422. errout:
  423. return ERR_PTR(err);
  424. }
  425. struct Qdisc * qdisc_create_dflt(struct net_device *dev,
  426. struct netdev_queue *dev_queue,
  427. struct Qdisc_ops *ops,
  428. unsigned int parentid)
  429. {
  430. struct Qdisc *sch;
  431. sch = qdisc_alloc(dev_queue, ops);
  432. if (IS_ERR(sch))
  433. goto errout;
  434. sch->parent = parentid;
  435. if (!ops->init || ops->init(sch, NULL) == 0)
  436. return sch;
  437. qdisc_destroy(sch);
  438. errout:
  439. return NULL;
  440. }
  441. EXPORT_SYMBOL(qdisc_create_dflt);
  442. /* Under queue->lock and BH! */
  443. void qdisc_reset(struct Qdisc *qdisc)
  444. {
  445. const struct Qdisc_ops *ops = qdisc->ops;
  446. if (ops->reset)
  447. ops->reset(qdisc);
  448. }
  449. EXPORT_SYMBOL(qdisc_reset);
  450. /* this is the rcu callback function to clean up a qdisc when there
  451. * are no further references to it */
  452. static void __qdisc_destroy(struct rcu_head *head)
  453. {
  454. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  455. kfree((char *) qdisc - qdisc->padded);
  456. }
  457. /* Under queue->lock and BH! */
  458. void qdisc_destroy(struct Qdisc *qdisc)
  459. {
  460. const struct Qdisc_ops *ops = qdisc->ops;
  461. if (qdisc->flags & TCQ_F_BUILTIN ||
  462. !atomic_dec_and_test(&qdisc->refcnt))
  463. return;
  464. list_del(&qdisc->list);
  465. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  466. if (ops->reset)
  467. ops->reset(qdisc);
  468. if (ops->destroy)
  469. ops->destroy(qdisc);
  470. module_put(ops->owner);
  471. dev_put(qdisc_dev(qdisc));
  472. call_rcu(&qdisc->q_rcu, __qdisc_destroy);
  473. }
  474. EXPORT_SYMBOL(qdisc_destroy);
  475. static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
  476. {
  477. unsigned int i;
  478. for (i = 0; i < dev->num_tx_queues; i++) {
  479. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  480. if (txq->qdisc_sleeping != &noop_qdisc)
  481. return false;
  482. }
  483. return true;
  484. }
  485. static void attach_one_default_qdisc(struct net_device *dev,
  486. struct netdev_queue *dev_queue,
  487. void *_unused)
  488. {
  489. struct Qdisc *qdisc;
  490. if (dev->tx_queue_len) {
  491. qdisc = qdisc_create_dflt(dev, dev_queue,
  492. &pfifo_fast_ops, TC_H_ROOT);
  493. if (!qdisc) {
  494. printk(KERN_INFO "%s: activation failed\n", dev->name);
  495. return;
  496. }
  497. list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
  498. } else {
  499. qdisc = &noqueue_qdisc;
  500. }
  501. dev_queue->qdisc_sleeping = qdisc;
  502. }
  503. static void transition_one_qdisc(struct net_device *dev,
  504. struct netdev_queue *dev_queue,
  505. void *_need_watchdog)
  506. {
  507. int *need_watchdog_p = _need_watchdog;
  508. spin_lock_bh(&dev_queue->lock);
  509. rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
  510. if (dev_queue->qdisc != &noqueue_qdisc)
  511. *need_watchdog_p = 1;
  512. spin_unlock_bh(&dev_queue->lock);
  513. }
  514. void dev_activate(struct net_device *dev)
  515. {
  516. int need_watchdog;
  517. /* No queueing discipline is attached to device;
  518. create default one i.e. pfifo_fast for devices,
  519. which need queueing and noqueue_qdisc for
  520. virtual interfaces
  521. */
  522. if (dev_all_qdisc_sleeping_noop(dev))
  523. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  524. if (!netif_carrier_ok(dev))
  525. /* Delay activation until next carrier-on event */
  526. return;
  527. need_watchdog = 0;
  528. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  529. if (need_watchdog) {
  530. dev->trans_start = jiffies;
  531. dev_watchdog_up(dev);
  532. }
  533. }
  534. static void dev_deactivate_queue(struct net_device *dev,
  535. struct netdev_queue *dev_queue,
  536. void *_qdisc_default)
  537. {
  538. struct Qdisc *qdisc_default = _qdisc_default;
  539. struct sk_buff *skb = NULL;
  540. struct Qdisc *qdisc;
  541. spin_lock_bh(&dev_queue->lock);
  542. qdisc = dev_queue->qdisc;
  543. if (qdisc) {
  544. dev_queue->qdisc = qdisc_default;
  545. qdisc_reset(qdisc);
  546. skb = qdisc->gso_skb;
  547. qdisc->gso_skb = NULL;
  548. }
  549. spin_unlock_bh(&dev_queue->lock);
  550. kfree_skb(skb);
  551. }
  552. static bool some_qdisc_is_running(struct net_device *dev, int lock)
  553. {
  554. unsigned int i;
  555. for (i = 0; i < dev->num_tx_queues; i++) {
  556. struct netdev_queue *dev_queue;
  557. struct Qdisc *q;
  558. int val;
  559. dev_queue = netdev_get_tx_queue(dev, i);
  560. q = dev_queue->qdisc;
  561. if (lock)
  562. spin_lock_bh(&dev_queue->lock);
  563. val = test_bit(__QDISC_STATE_RUNNING, &q->state);
  564. if (lock)
  565. spin_unlock_bh(&dev_queue->lock);
  566. if (val)
  567. return true;
  568. }
  569. return false;
  570. }
  571. void dev_deactivate(struct net_device *dev)
  572. {
  573. bool running;
  574. netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
  575. dev_watchdog_down(dev);
  576. /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
  577. synchronize_rcu();
  578. /* Wait for outstanding qdisc_run calls. */
  579. do {
  580. while (some_qdisc_is_running(dev, 0))
  581. yield();
  582. /*
  583. * Double-check inside queue lock to ensure that all effects
  584. * of the queue run are visible when we return.
  585. */
  586. running = some_qdisc_is_running(dev, 1);
  587. /*
  588. * The running flag should never be set at this point because
  589. * we've already set dev->qdisc to noop_qdisc *inside* the same
  590. * pair of spin locks. That is, if any qdisc_run starts after
  591. * our initial test it should see the noop_qdisc and then
  592. * clear the RUNNING bit before dropping the queue lock. So
  593. * if it is set here then we've found a bug.
  594. */
  595. } while (WARN_ON_ONCE(running));
  596. }
  597. static void dev_init_scheduler_queue(struct net_device *dev,
  598. struct netdev_queue *dev_queue,
  599. void *_qdisc)
  600. {
  601. struct Qdisc *qdisc = _qdisc;
  602. dev_queue->qdisc = qdisc;
  603. dev_queue->qdisc_sleeping = qdisc;
  604. INIT_LIST_HEAD(&dev_queue->qdisc_list);
  605. }
  606. void dev_init_scheduler(struct net_device *dev)
  607. {
  608. qdisc_lock_tree(dev);
  609. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  610. dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
  611. qdisc_unlock_tree(dev);
  612. setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
  613. }
  614. static void shutdown_scheduler_queue(struct net_device *dev,
  615. struct netdev_queue *dev_queue,
  616. void *_qdisc_default)
  617. {
  618. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  619. struct Qdisc *qdisc_default = _qdisc_default;
  620. if (qdisc) {
  621. dev_queue->qdisc = qdisc_default;
  622. dev_queue->qdisc_sleeping = qdisc_default;
  623. qdisc_destroy(qdisc);
  624. }
  625. }
  626. void dev_shutdown(struct net_device *dev)
  627. {
  628. qdisc_lock_tree(dev);
  629. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  630. shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
  631. BUG_TRAP(!timer_pending(&dev->watchdog_timer));
  632. qdisc_unlock_tree(dev);
  633. }