sch_generic.c 21 KB

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