sch_generic.c 20 KB

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