sch_generic.c 17 KB

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