sch_generic.c 20 KB

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