sch_generic.c 22 KB

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