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