sch_generic.c 17 KB

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