sch_generic.c 20 KB

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