sch_generic.c 23 KB

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