sch_generic.c 18 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. dev_queue->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 netdev_queue *dev_queue,
  78. struct Qdisc *q)
  79. {
  80. struct sk_buff *skb;
  81. if ((skb = dev_queue->gso_skb))
  82. dev_queue->gso_skb = NULL;
  83. else
  84. skb = q->dequeue(q);
  85. return skb;
  86. }
  87. static inline int handle_dev_cpu_collision(struct sk_buff *skb,
  88. struct netdev_queue *dev_queue,
  89. struct Qdisc *q)
  90. {
  91. int ret;
  92. if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
  93. /*
  94. * Same CPU holding the lock. It may be a transient
  95. * configuration error, when hard_start_xmit() recurses. We
  96. * detect it by checking xmit owner and drop the packet when
  97. * deadloop is detected. Return OK to try the next skb.
  98. */
  99. kfree_skb(skb);
  100. if (net_ratelimit())
  101. printk(KERN_WARNING "Dead loop on netdevice %s, "
  102. "fix it urgently!\n", dev_queue->dev->name);
  103. ret = qdisc_qlen(q);
  104. } else {
  105. /*
  106. * Another cpu is holding lock, requeue & delay xmits for
  107. * some time.
  108. */
  109. __get_cpu_var(netdev_rx_stat).cpu_collision++;
  110. ret = dev_requeue_skb(skb, dev_queue, q);
  111. }
  112. return ret;
  113. }
  114. /*
  115. * NOTE: Called under queue->lock with locally disabled BH.
  116. *
  117. * __QUEUE_STATE_QDISC_RUNNING guarantees only one CPU can process
  118. * this queue at a time. queue->lock serializes queue accesses for
  119. * this queue AND txq->qdisc pointer itself.
  120. *
  121. * netif_tx_lock serializes accesses to device driver.
  122. *
  123. * queue->lock and netif_tx_lock are mutually exclusive,
  124. * if one is grabbed, another must be free.
  125. *
  126. * Note, that this procedure can be called by a watchdog timer
  127. *
  128. * Returns to the caller:
  129. * 0 - queue is empty or throttled.
  130. * >0 - queue is not empty.
  131. *
  132. */
  133. static inline int qdisc_restart(struct netdev_queue *txq)
  134. {
  135. struct Qdisc *q = txq->qdisc;
  136. int ret = NETDEV_TX_BUSY;
  137. struct net_device *dev;
  138. struct sk_buff *skb;
  139. /* Dequeue packet */
  140. if (unlikely((skb = dequeue_skb(txq, q)) == NULL))
  141. return 0;
  142. /* And release queue */
  143. spin_unlock(&txq->lock);
  144. dev = txq->dev;
  145. HARD_TX_LOCK(dev, txq, smp_processor_id());
  146. if (!netif_subqueue_stopped(dev, skb))
  147. ret = dev_hard_start_xmit(skb, dev);
  148. HARD_TX_UNLOCK(dev, txq);
  149. spin_lock(&txq->lock);
  150. q = txq->qdisc;
  151. switch (ret) {
  152. case NETDEV_TX_OK:
  153. /* Driver sent out skb successfully */
  154. ret = qdisc_qlen(q);
  155. break;
  156. case NETDEV_TX_LOCKED:
  157. /* Driver try lock failed */
  158. ret = handle_dev_cpu_collision(skb, txq, q);
  159. break;
  160. default:
  161. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  162. if (unlikely (ret != NETDEV_TX_BUSY && net_ratelimit()))
  163. printk(KERN_WARNING "BUG %s code %d qlen %d\n",
  164. dev->name, ret, q->q.qlen);
  165. ret = dev_requeue_skb(skb, txq, q);
  166. break;
  167. }
  168. return ret;
  169. }
  170. void __qdisc_run(struct netdev_queue *txq)
  171. {
  172. struct net_device *dev = txq->dev;
  173. unsigned long start_time = jiffies;
  174. while (qdisc_restart(txq)) {
  175. if (netif_queue_stopped(dev))
  176. break;
  177. /*
  178. * Postpone processing if
  179. * 1. another process needs the CPU;
  180. * 2. we've been doing it for too long.
  181. */
  182. if (need_resched() || jiffies != start_time) {
  183. netif_schedule_queue(txq);
  184. break;
  185. }
  186. }
  187. clear_bit(__QUEUE_STATE_QDISC_RUNNING, &txq->state);
  188. }
  189. static void dev_watchdog(unsigned long arg)
  190. {
  191. struct net_device *dev = (struct net_device *)arg;
  192. netif_tx_lock(dev);
  193. if (!qdisc_tx_is_noop(dev)) {
  194. if (netif_device_present(dev) &&
  195. netif_running(dev) &&
  196. netif_carrier_ok(dev)) {
  197. int some_queue_stopped = 0;
  198. unsigned int i;
  199. for (i = 0; i < dev->num_tx_queues; i++) {
  200. struct netdev_queue *txq;
  201. txq = netdev_get_tx_queue(dev, i);
  202. if (netif_tx_queue_stopped(txq)) {
  203. some_queue_stopped = 1;
  204. break;
  205. }
  206. }
  207. if (some_queue_stopped &&
  208. time_after(jiffies, (dev->trans_start +
  209. dev->watchdog_timeo))) {
  210. printk(KERN_INFO "NETDEV WATCHDOG: %s: "
  211. "transmit timed out\n",
  212. dev->name);
  213. dev->tx_timeout(dev);
  214. WARN_ON_ONCE(1);
  215. }
  216. if (!mod_timer(&dev->watchdog_timer,
  217. round_jiffies(jiffies +
  218. dev->watchdog_timeo)))
  219. dev_hold(dev);
  220. }
  221. }
  222. netif_tx_unlock(dev);
  223. dev_put(dev);
  224. }
  225. void __netdev_watchdog_up(struct net_device *dev)
  226. {
  227. if (dev->tx_timeout) {
  228. if (dev->watchdog_timeo <= 0)
  229. dev->watchdog_timeo = 5*HZ;
  230. if (!mod_timer(&dev->watchdog_timer,
  231. round_jiffies(jiffies + dev->watchdog_timeo)))
  232. dev_hold(dev);
  233. }
  234. }
  235. static void dev_watchdog_up(struct net_device *dev)
  236. {
  237. __netdev_watchdog_up(dev);
  238. }
  239. static void dev_watchdog_down(struct net_device *dev)
  240. {
  241. netif_tx_lock_bh(dev);
  242. if (del_timer(&dev->watchdog_timer))
  243. dev_put(dev);
  244. netif_tx_unlock_bh(dev);
  245. }
  246. /**
  247. * netif_carrier_on - set carrier
  248. * @dev: network device
  249. *
  250. * Device has detected that carrier.
  251. */
  252. void netif_carrier_on(struct net_device *dev)
  253. {
  254. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  255. linkwatch_fire_event(dev);
  256. if (netif_running(dev))
  257. __netdev_watchdog_up(dev);
  258. }
  259. }
  260. EXPORT_SYMBOL(netif_carrier_on);
  261. /**
  262. * netif_carrier_off - clear carrier
  263. * @dev: network device
  264. *
  265. * Device has detected loss of carrier.
  266. */
  267. void netif_carrier_off(struct net_device *dev)
  268. {
  269. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state))
  270. linkwatch_fire_event(dev);
  271. }
  272. EXPORT_SYMBOL(netif_carrier_off);
  273. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  274. under all circumstances. It is difficult to invent anything faster or
  275. cheaper.
  276. */
  277. static int noop_enqueue(struct sk_buff *skb, struct Qdisc * qdisc)
  278. {
  279. kfree_skb(skb);
  280. return NET_XMIT_CN;
  281. }
  282. static struct sk_buff *noop_dequeue(struct Qdisc * qdisc)
  283. {
  284. return NULL;
  285. }
  286. static int noop_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  287. {
  288. if (net_ratelimit())
  289. printk(KERN_DEBUG "%s deferred output. It is buggy.\n",
  290. skb->dev->name);
  291. kfree_skb(skb);
  292. return NET_XMIT_CN;
  293. }
  294. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  295. .id = "noop",
  296. .priv_size = 0,
  297. .enqueue = noop_enqueue,
  298. .dequeue = noop_dequeue,
  299. .requeue = noop_requeue,
  300. .owner = THIS_MODULE,
  301. };
  302. struct Qdisc noop_qdisc = {
  303. .enqueue = noop_enqueue,
  304. .dequeue = noop_dequeue,
  305. .flags = TCQ_F_BUILTIN,
  306. .ops = &noop_qdisc_ops,
  307. .list = LIST_HEAD_INIT(noop_qdisc.list),
  308. };
  309. EXPORT_SYMBOL(noop_qdisc);
  310. static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  311. .id = "noqueue",
  312. .priv_size = 0,
  313. .enqueue = noop_enqueue,
  314. .dequeue = noop_dequeue,
  315. .requeue = noop_requeue,
  316. .owner = THIS_MODULE,
  317. };
  318. static struct Qdisc noqueue_qdisc = {
  319. .enqueue = NULL,
  320. .dequeue = noop_dequeue,
  321. .flags = TCQ_F_BUILTIN,
  322. .ops = &noqueue_qdisc_ops,
  323. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  324. };
  325. static const u8 prio2band[TC_PRIO_MAX+1] =
  326. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  327. /* 3-band FIFO queue: old style, but should be a bit faster than
  328. generic prio+fifo combination.
  329. */
  330. #define PFIFO_FAST_BANDS 3
  331. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  332. struct Qdisc *qdisc)
  333. {
  334. struct sk_buff_head *list = qdisc_priv(qdisc);
  335. return list + prio2band[skb->priority & TC_PRIO_MAX];
  336. }
  337. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  338. {
  339. struct sk_buff_head *list = prio2list(skb, qdisc);
  340. if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
  341. qdisc->q.qlen++;
  342. return __qdisc_enqueue_tail(skb, qdisc, list);
  343. }
  344. return qdisc_drop(skb, qdisc);
  345. }
  346. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  347. {
  348. int prio;
  349. struct sk_buff_head *list = qdisc_priv(qdisc);
  350. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  351. if (!skb_queue_empty(list + prio)) {
  352. qdisc->q.qlen--;
  353. return __qdisc_dequeue_head(qdisc, list + prio);
  354. }
  355. }
  356. return NULL;
  357. }
  358. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  359. {
  360. qdisc->q.qlen++;
  361. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  362. }
  363. static void pfifo_fast_reset(struct Qdisc* qdisc)
  364. {
  365. int prio;
  366. struct sk_buff_head *list = qdisc_priv(qdisc);
  367. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  368. __qdisc_reset_queue(qdisc, list + prio);
  369. qdisc->qstats.backlog = 0;
  370. qdisc->q.qlen = 0;
  371. }
  372. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  373. {
  374. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  375. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  376. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  377. return skb->len;
  378. nla_put_failure:
  379. return -1;
  380. }
  381. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  382. {
  383. int prio;
  384. struct sk_buff_head *list = qdisc_priv(qdisc);
  385. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  386. skb_queue_head_init(list + prio);
  387. return 0;
  388. }
  389. static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  390. .id = "pfifo_fast",
  391. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  392. .enqueue = pfifo_fast_enqueue,
  393. .dequeue = pfifo_fast_dequeue,
  394. .requeue = pfifo_fast_requeue,
  395. .init = pfifo_fast_init,
  396. .reset = pfifo_fast_reset,
  397. .dump = pfifo_fast_dump,
  398. .owner = THIS_MODULE,
  399. };
  400. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  401. struct Qdisc_ops *ops)
  402. {
  403. void *p;
  404. struct Qdisc *sch;
  405. unsigned int size;
  406. int err = -ENOBUFS;
  407. /* ensure that the Qdisc and the private data are 32-byte aligned */
  408. size = QDISC_ALIGN(sizeof(*sch));
  409. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  410. p = kzalloc(size, GFP_KERNEL);
  411. if (!p)
  412. goto errout;
  413. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  414. sch->padded = (char *) sch - (char *) p;
  415. INIT_LIST_HEAD(&sch->list);
  416. skb_queue_head_init(&sch->q);
  417. sch->ops = ops;
  418. sch->enqueue = ops->enqueue;
  419. sch->dequeue = ops->dequeue;
  420. sch->dev_queue = dev_queue;
  421. dev_hold(qdisc_dev(sch));
  422. atomic_set(&sch->refcnt, 1);
  423. return sch;
  424. errout:
  425. return ERR_PTR(err);
  426. }
  427. struct Qdisc * qdisc_create_dflt(struct net_device *dev,
  428. struct netdev_queue *dev_queue,
  429. struct Qdisc_ops *ops,
  430. unsigned int parentid)
  431. {
  432. struct Qdisc *sch;
  433. sch = qdisc_alloc(dev_queue, ops);
  434. if (IS_ERR(sch))
  435. goto errout;
  436. sch->parent = parentid;
  437. if (!ops->init || ops->init(sch, NULL) == 0)
  438. return sch;
  439. qdisc_destroy(sch);
  440. errout:
  441. return NULL;
  442. }
  443. EXPORT_SYMBOL(qdisc_create_dflt);
  444. /* Under queue->lock and BH! */
  445. void qdisc_reset(struct Qdisc *qdisc)
  446. {
  447. const struct Qdisc_ops *ops = qdisc->ops;
  448. if (ops->reset)
  449. ops->reset(qdisc);
  450. }
  451. EXPORT_SYMBOL(qdisc_reset);
  452. /* this is the rcu callback function to clean up a qdisc when there
  453. * are no further references to it */
  454. static void __qdisc_destroy(struct rcu_head *head)
  455. {
  456. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  457. kfree((char *) qdisc - qdisc->padded);
  458. }
  459. /* Under queue->lock and BH! */
  460. void qdisc_destroy(struct Qdisc *qdisc)
  461. {
  462. const struct Qdisc_ops *ops = qdisc->ops;
  463. if (qdisc->flags & TCQ_F_BUILTIN ||
  464. !atomic_dec_and_test(&qdisc->refcnt))
  465. return;
  466. list_del(&qdisc->list);
  467. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  468. if (ops->reset)
  469. ops->reset(qdisc);
  470. if (ops->destroy)
  471. ops->destroy(qdisc);
  472. module_put(ops->owner);
  473. dev_put(qdisc_dev(qdisc));
  474. call_rcu(&qdisc->q_rcu, __qdisc_destroy);
  475. }
  476. EXPORT_SYMBOL(qdisc_destroy);
  477. static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
  478. {
  479. unsigned int i;
  480. for (i = 0; i < dev->num_tx_queues; i++) {
  481. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  482. if (txq->qdisc_sleeping != &noop_qdisc)
  483. return false;
  484. }
  485. return true;
  486. }
  487. static void attach_one_default_qdisc(struct net_device *dev,
  488. struct netdev_queue *dev_queue,
  489. void *_unused)
  490. {
  491. struct Qdisc *qdisc;
  492. if (dev->tx_queue_len) {
  493. qdisc = qdisc_create_dflt(dev, dev_queue,
  494. &pfifo_fast_ops, TC_H_ROOT);
  495. if (!qdisc) {
  496. printk(KERN_INFO "%s: activation failed\n", dev->name);
  497. return;
  498. }
  499. list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
  500. } else {
  501. qdisc = &noqueue_qdisc;
  502. }
  503. dev_queue->qdisc_sleeping = qdisc;
  504. }
  505. static void transition_one_qdisc(struct net_device *dev,
  506. struct netdev_queue *dev_queue,
  507. void *_need_watchdog)
  508. {
  509. int *need_watchdog_p = _need_watchdog;
  510. spin_lock_bh(&dev_queue->lock);
  511. rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
  512. if (dev_queue->qdisc != &noqueue_qdisc)
  513. *need_watchdog_p = 1;
  514. spin_unlock_bh(&dev_queue->lock);
  515. }
  516. void dev_activate(struct net_device *dev)
  517. {
  518. int need_watchdog;
  519. /* No queueing discipline is attached to device;
  520. create default one i.e. pfifo_fast for devices,
  521. which need queueing and noqueue_qdisc for
  522. virtual interfaces
  523. */
  524. if (dev_all_qdisc_sleeping_noop(dev))
  525. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  526. if (!netif_carrier_ok(dev))
  527. /* Delay activation until next carrier-on event */
  528. return;
  529. need_watchdog = 0;
  530. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  531. if (need_watchdog) {
  532. dev->trans_start = jiffies;
  533. dev_watchdog_up(dev);
  534. }
  535. }
  536. static void dev_deactivate_queue(struct net_device *dev,
  537. struct netdev_queue *dev_queue,
  538. void *_qdisc_default)
  539. {
  540. struct Qdisc *qdisc_default = _qdisc_default;
  541. struct Qdisc *qdisc;
  542. struct sk_buff *skb;
  543. spin_lock_bh(&dev_queue->lock);
  544. qdisc = dev_queue->qdisc;
  545. if (qdisc) {
  546. dev_queue->qdisc = qdisc_default;
  547. qdisc_reset(qdisc);
  548. }
  549. skb = dev_queue->gso_skb;
  550. dev_queue->gso_skb = NULL;
  551. spin_unlock_bh(&dev_queue->lock);
  552. kfree_skb(skb);
  553. }
  554. static bool some_qdisc_is_running(struct net_device *dev, int lock)
  555. {
  556. unsigned int i;
  557. for (i = 0; i < dev->num_tx_queues; i++) {
  558. struct netdev_queue *dev_queue;
  559. int val;
  560. dev_queue = netdev_get_tx_queue(dev, i);
  561. if (lock)
  562. spin_lock_bh(&dev_queue->lock);
  563. val = test_bit(__QUEUE_STATE_QDISC_RUNNING, &dev_queue->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. }