sch_generic.c 17 KB

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