sch_generic.c 22 KB

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