sch_generic.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. spinlock_t *root_lock;
  138. struct sk_buff *skb;
  139. /* Dequeue packet */
  140. if (unlikely((skb = dequeue_skb(q)) == NULL))
  141. return 0;
  142. root_lock = qdisc_root_lock(q);
  143. /* And release qdisc */
  144. spin_unlock(root_lock);
  145. dev = txq->dev;
  146. HARD_TX_LOCK(dev, txq, smp_processor_id());
  147. if (!netif_subqueue_stopped(dev, skb))
  148. ret = dev_hard_start_xmit(skb, dev, txq);
  149. HARD_TX_UNLOCK(dev, txq);
  150. spin_lock(root_lock);
  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. unsigned long start_time = jiffies;
  173. struct Qdisc *q = txq->qdisc;
  174. while (qdisc_restart(txq, q)) {
  175. if (netif_tx_queue_stopped(txq))
  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(__QDISC_STATE_RUNNING, &q->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. static struct netdev_queue noop_netdev_queue = {
  303. .lock = __SPIN_LOCK_UNLOCKED(noop_netdev_queue.lock),
  304. .qdisc = &noop_qdisc,
  305. };
  306. struct Qdisc noop_qdisc = {
  307. .enqueue = noop_enqueue,
  308. .dequeue = noop_dequeue,
  309. .flags = TCQ_F_BUILTIN,
  310. .ops = &noop_qdisc_ops,
  311. .list = LIST_HEAD_INIT(noop_qdisc.list),
  312. .dev_queue = &noop_netdev_queue,
  313. };
  314. EXPORT_SYMBOL(noop_qdisc);
  315. static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  316. .id = "noqueue",
  317. .priv_size = 0,
  318. .enqueue = noop_enqueue,
  319. .dequeue = noop_dequeue,
  320. .requeue = noop_requeue,
  321. .owner = THIS_MODULE,
  322. };
  323. static struct Qdisc noqueue_qdisc = {
  324. .enqueue = NULL,
  325. .dequeue = noop_dequeue,
  326. .flags = TCQ_F_BUILTIN,
  327. .ops = &noqueue_qdisc_ops,
  328. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  329. };
  330. static const u8 prio2band[TC_PRIO_MAX+1] =
  331. { 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1 };
  332. /* 3-band FIFO queue: old style, but should be a bit faster than
  333. generic prio+fifo combination.
  334. */
  335. #define PFIFO_FAST_BANDS 3
  336. static inline struct sk_buff_head *prio2list(struct sk_buff *skb,
  337. struct Qdisc *qdisc)
  338. {
  339. struct sk_buff_head *list = qdisc_priv(qdisc);
  340. return list + prio2band[skb->priority & TC_PRIO_MAX];
  341. }
  342. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc* qdisc)
  343. {
  344. struct sk_buff_head *list = prio2list(skb, qdisc);
  345. if (skb_queue_len(list) < qdisc_dev(qdisc)->tx_queue_len) {
  346. qdisc->q.qlen++;
  347. return __qdisc_enqueue_tail(skb, qdisc, list);
  348. }
  349. return qdisc_drop(skb, qdisc);
  350. }
  351. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc* qdisc)
  352. {
  353. int prio;
  354. struct sk_buff_head *list = qdisc_priv(qdisc);
  355. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  356. if (!skb_queue_empty(list + prio)) {
  357. qdisc->q.qlen--;
  358. return __qdisc_dequeue_head(qdisc, list + prio);
  359. }
  360. }
  361. return NULL;
  362. }
  363. static int pfifo_fast_requeue(struct sk_buff *skb, struct Qdisc* qdisc)
  364. {
  365. qdisc->q.qlen++;
  366. return __qdisc_requeue(skb, qdisc, prio2list(skb, qdisc));
  367. }
  368. static void pfifo_fast_reset(struct Qdisc* qdisc)
  369. {
  370. int prio;
  371. struct sk_buff_head *list = qdisc_priv(qdisc);
  372. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  373. __qdisc_reset_queue(qdisc, list + prio);
  374. qdisc->qstats.backlog = 0;
  375. qdisc->q.qlen = 0;
  376. }
  377. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  378. {
  379. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  380. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX+1);
  381. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  382. return skb->len;
  383. nla_put_failure:
  384. return -1;
  385. }
  386. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  387. {
  388. int prio;
  389. struct sk_buff_head *list = qdisc_priv(qdisc);
  390. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  391. skb_queue_head_init(list + prio);
  392. return 0;
  393. }
  394. static struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  395. .id = "pfifo_fast",
  396. .priv_size = PFIFO_FAST_BANDS * sizeof(struct sk_buff_head),
  397. .enqueue = pfifo_fast_enqueue,
  398. .dequeue = pfifo_fast_dequeue,
  399. .requeue = pfifo_fast_requeue,
  400. .init = pfifo_fast_init,
  401. .reset = pfifo_fast_reset,
  402. .dump = pfifo_fast_dump,
  403. .owner = THIS_MODULE,
  404. };
  405. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  406. struct Qdisc_ops *ops)
  407. {
  408. void *p;
  409. struct Qdisc *sch;
  410. unsigned int size;
  411. int err = -ENOBUFS;
  412. /* ensure that the Qdisc and the private data are 32-byte aligned */
  413. size = QDISC_ALIGN(sizeof(*sch));
  414. size += ops->priv_size + (QDISC_ALIGNTO - 1);
  415. p = kzalloc(size, GFP_KERNEL);
  416. if (!p)
  417. goto errout;
  418. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  419. sch->padded = (char *) sch - (char *) p;
  420. INIT_LIST_HEAD(&sch->list);
  421. skb_queue_head_init(&sch->q);
  422. sch->ops = ops;
  423. sch->enqueue = ops->enqueue;
  424. sch->dequeue = ops->dequeue;
  425. sch->dev_queue = dev_queue;
  426. dev_hold(qdisc_dev(sch));
  427. atomic_set(&sch->refcnt, 1);
  428. return sch;
  429. errout:
  430. return ERR_PTR(err);
  431. }
  432. struct Qdisc * qdisc_create_dflt(struct net_device *dev,
  433. struct netdev_queue *dev_queue,
  434. struct Qdisc_ops *ops,
  435. unsigned int parentid)
  436. {
  437. struct Qdisc *sch;
  438. sch = qdisc_alloc(dev_queue, ops);
  439. if (IS_ERR(sch))
  440. goto errout;
  441. sch->parent = parentid;
  442. if (!ops->init || ops->init(sch, NULL) == 0)
  443. return sch;
  444. qdisc_destroy(sch);
  445. errout:
  446. return NULL;
  447. }
  448. EXPORT_SYMBOL(qdisc_create_dflt);
  449. /* Under queue->lock and BH! */
  450. void qdisc_reset(struct Qdisc *qdisc)
  451. {
  452. const struct Qdisc_ops *ops = qdisc->ops;
  453. if (ops->reset)
  454. ops->reset(qdisc);
  455. }
  456. EXPORT_SYMBOL(qdisc_reset);
  457. /* this is the rcu callback function to clean up a qdisc when there
  458. * are no further references to it */
  459. static void __qdisc_destroy(struct rcu_head *head)
  460. {
  461. struct Qdisc *qdisc = container_of(head, struct Qdisc, q_rcu);
  462. kfree((char *) qdisc - qdisc->padded);
  463. }
  464. /* Under queue->lock and BH! */
  465. void qdisc_destroy(struct Qdisc *qdisc)
  466. {
  467. const struct Qdisc_ops *ops = qdisc->ops;
  468. if (qdisc->flags & TCQ_F_BUILTIN ||
  469. !atomic_dec_and_test(&qdisc->refcnt))
  470. return;
  471. list_del(&qdisc->list);
  472. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  473. if (ops->reset)
  474. ops->reset(qdisc);
  475. if (ops->destroy)
  476. ops->destroy(qdisc);
  477. module_put(ops->owner);
  478. dev_put(qdisc_dev(qdisc));
  479. call_rcu(&qdisc->q_rcu, __qdisc_destroy);
  480. }
  481. EXPORT_SYMBOL(qdisc_destroy);
  482. static bool dev_all_qdisc_sleeping_noop(struct net_device *dev)
  483. {
  484. unsigned int i;
  485. for (i = 0; i < dev->num_tx_queues; i++) {
  486. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  487. if (txq->qdisc_sleeping != &noop_qdisc)
  488. return false;
  489. }
  490. return true;
  491. }
  492. static void attach_one_default_qdisc(struct net_device *dev,
  493. struct netdev_queue *dev_queue,
  494. void *_unused)
  495. {
  496. struct Qdisc *qdisc;
  497. if (dev->tx_queue_len) {
  498. qdisc = qdisc_create_dflt(dev, dev_queue,
  499. &pfifo_fast_ops, TC_H_ROOT);
  500. if (!qdisc) {
  501. printk(KERN_INFO "%s: activation failed\n", dev->name);
  502. return;
  503. }
  504. list_add_tail(&qdisc->list, &dev_queue->qdisc_list);
  505. } else {
  506. qdisc = &noqueue_qdisc;
  507. }
  508. dev_queue->qdisc_sleeping = qdisc;
  509. }
  510. static void transition_one_qdisc(struct net_device *dev,
  511. struct netdev_queue *dev_queue,
  512. void *_need_watchdog)
  513. {
  514. int *need_watchdog_p = _need_watchdog;
  515. spin_lock_bh(&dev_queue->lock);
  516. rcu_assign_pointer(dev_queue->qdisc, dev_queue->qdisc_sleeping);
  517. if (dev_queue->qdisc != &noqueue_qdisc)
  518. *need_watchdog_p = 1;
  519. spin_unlock_bh(&dev_queue->lock);
  520. }
  521. void dev_activate(struct net_device *dev)
  522. {
  523. int need_watchdog;
  524. /* No queueing discipline is attached to device;
  525. create default one i.e. pfifo_fast for devices,
  526. which need queueing and noqueue_qdisc for
  527. virtual interfaces
  528. */
  529. if (dev_all_qdisc_sleeping_noop(dev))
  530. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  531. if (!netif_carrier_ok(dev))
  532. /* Delay activation until next carrier-on event */
  533. return;
  534. need_watchdog = 0;
  535. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  536. if (need_watchdog) {
  537. dev->trans_start = jiffies;
  538. dev_watchdog_up(dev);
  539. }
  540. }
  541. static void dev_deactivate_queue(struct net_device *dev,
  542. struct netdev_queue *dev_queue,
  543. void *_qdisc_default)
  544. {
  545. struct Qdisc *qdisc_default = _qdisc_default;
  546. struct sk_buff *skb = NULL;
  547. struct Qdisc *qdisc;
  548. spin_lock_bh(&dev_queue->lock);
  549. qdisc = dev_queue->qdisc;
  550. if (qdisc) {
  551. dev_queue->qdisc = qdisc_default;
  552. qdisc_reset(qdisc);
  553. skb = qdisc->gso_skb;
  554. qdisc->gso_skb = NULL;
  555. }
  556. spin_unlock_bh(&dev_queue->lock);
  557. kfree_skb(skb);
  558. }
  559. static bool some_qdisc_is_running(struct net_device *dev, int lock)
  560. {
  561. unsigned int i;
  562. for (i = 0; i < dev->num_tx_queues; i++) {
  563. struct netdev_queue *dev_queue;
  564. spinlock_t *root_lock;
  565. struct Qdisc *q;
  566. int val;
  567. dev_queue = netdev_get_tx_queue(dev, i);
  568. q = dev_queue->qdisc;
  569. root_lock = qdisc_root_lock(q);
  570. if (lock)
  571. spin_lock_bh(root_lock);
  572. val = test_bit(__QDISC_STATE_RUNNING, &q->state);
  573. if (lock)
  574. spin_unlock_bh(root_lock);
  575. if (val)
  576. return true;
  577. }
  578. return false;
  579. }
  580. void dev_deactivate(struct net_device *dev)
  581. {
  582. bool running;
  583. netdev_for_each_tx_queue(dev, dev_deactivate_queue, &noop_qdisc);
  584. dev_watchdog_down(dev);
  585. /* Wait for outstanding qdisc-less dev_queue_xmit calls. */
  586. synchronize_rcu();
  587. /* Wait for outstanding qdisc_run calls. */
  588. do {
  589. while (some_qdisc_is_running(dev, 0))
  590. yield();
  591. /*
  592. * Double-check inside queue lock to ensure that all effects
  593. * of the queue run are visible when we return.
  594. */
  595. running = some_qdisc_is_running(dev, 1);
  596. /*
  597. * The running flag should never be set at this point because
  598. * we've already set dev->qdisc to noop_qdisc *inside* the same
  599. * pair of spin locks. That is, if any qdisc_run starts after
  600. * our initial test it should see the noop_qdisc and then
  601. * clear the RUNNING bit before dropping the queue lock. So
  602. * if it is set here then we've found a bug.
  603. */
  604. } while (WARN_ON_ONCE(running));
  605. }
  606. static void dev_init_scheduler_queue(struct net_device *dev,
  607. struct netdev_queue *dev_queue,
  608. void *_qdisc)
  609. {
  610. struct Qdisc *qdisc = _qdisc;
  611. dev_queue->qdisc = qdisc;
  612. dev_queue->qdisc_sleeping = qdisc;
  613. INIT_LIST_HEAD(&dev_queue->qdisc_list);
  614. }
  615. void dev_init_scheduler(struct net_device *dev)
  616. {
  617. qdisc_lock_tree(dev);
  618. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  619. dev_init_scheduler_queue(dev, &dev->rx_queue, NULL);
  620. qdisc_unlock_tree(dev);
  621. setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
  622. }
  623. static void shutdown_scheduler_queue(struct net_device *dev,
  624. struct netdev_queue *dev_queue,
  625. void *_qdisc_default)
  626. {
  627. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  628. struct Qdisc *qdisc_default = _qdisc_default;
  629. if (qdisc) {
  630. dev_queue->qdisc = qdisc_default;
  631. dev_queue->qdisc_sleeping = qdisc_default;
  632. qdisc_destroy(qdisc);
  633. }
  634. }
  635. void dev_shutdown(struct net_device *dev)
  636. {
  637. qdisc_lock_tree(dev);
  638. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  639. shutdown_scheduler_queue(dev, &dev->rx_queue, NULL);
  640. BUG_TRAP(!timer_pending(&dev->watchdog_timer));
  641. qdisc_unlock_tree(dev);
  642. }