sch_generic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 = netdev_get_tx_queue(txq->dev, skb_get_queue_mapping(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. }
  68. return skb;
  69. }
  70. static inline int handle_dev_cpu_collision(struct sk_buff *skb,
  71. struct netdev_queue *dev_queue,
  72. struct Qdisc *q)
  73. {
  74. int ret;
  75. if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
  76. /*
  77. * Same CPU holding the lock. It may be a transient
  78. * configuration error, when hard_start_xmit() recurses. We
  79. * detect it by checking xmit owner and drop the packet when
  80. * deadloop is detected. Return OK to try the next skb.
  81. */
  82. kfree_skb(skb);
  83. net_warn_ratelimited("Dead loop on netdevice %s, fix it urgently!\n",
  84. dev_queue->dev->name);
  85. ret = qdisc_qlen(q);
  86. } else {
  87. /*
  88. * Another cpu is holding lock, requeue & delay xmits for
  89. * some time.
  90. */
  91. __this_cpu_inc(softnet_data.cpu_collision);
  92. ret = dev_requeue_skb(skb, q);
  93. }
  94. return ret;
  95. }
  96. /*
  97. * Transmit one skb, and handle the return status as required. Holding the
  98. * __QDISC_STATE_RUNNING bit guarantees that only one CPU can execute this
  99. * function.
  100. *
  101. * Returns to the caller:
  102. * 0 - queue is empty or throttled.
  103. * >0 - queue is not empty.
  104. */
  105. int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
  106. struct net_device *dev, struct netdev_queue *txq,
  107. spinlock_t *root_lock)
  108. {
  109. int ret = NETDEV_TX_BUSY;
  110. /* And release qdisc */
  111. spin_unlock(root_lock);
  112. HARD_TX_LOCK(dev, txq, smp_processor_id());
  113. if (!netif_xmit_frozen_or_stopped(txq))
  114. ret = dev_hard_start_xmit(skb, dev, txq);
  115. HARD_TX_UNLOCK(dev, txq);
  116. spin_lock(root_lock);
  117. if (dev_xmit_complete(ret)) {
  118. /* Driver sent out skb successfully or skb was consumed */
  119. ret = qdisc_qlen(q);
  120. } else if (ret == NETDEV_TX_LOCKED) {
  121. /* Driver try lock failed */
  122. ret = handle_dev_cpu_collision(skb, txq, q);
  123. } else {
  124. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  125. if (unlikely(ret != NETDEV_TX_BUSY))
  126. net_warn_ratelimited("BUG %s code %d qlen %d\n",
  127. dev->name, ret, q->q.qlen);
  128. ret = dev_requeue_skb(skb, q);
  129. }
  130. if (ret && netif_xmit_frozen_or_stopped(txq))
  131. ret = 0;
  132. return ret;
  133. }
  134. /*
  135. * NOTE: Called under qdisc_lock(q) with locally disabled BH.
  136. *
  137. * __QDISC_STATE_RUNNING guarantees only one CPU can process
  138. * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
  139. * this queue.
  140. *
  141. * netif_tx_lock serializes accesses to device driver.
  142. *
  143. * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
  144. * if one is grabbed, another must be free.
  145. *
  146. * Note, that this procedure can be called by a watchdog timer
  147. *
  148. * Returns to the caller:
  149. * 0 - queue is empty or throttled.
  150. * >0 - queue is not empty.
  151. *
  152. */
  153. static inline int qdisc_restart(struct Qdisc *q)
  154. {
  155. struct netdev_queue *txq;
  156. struct net_device *dev;
  157. spinlock_t *root_lock;
  158. struct sk_buff *skb;
  159. /* Dequeue packet */
  160. skb = dequeue_skb(q);
  161. if (unlikely(!skb))
  162. return 0;
  163. WARN_ON_ONCE(skb_dst_is_noref(skb));
  164. root_lock = qdisc_lock(q);
  165. dev = qdisc_dev(q);
  166. txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
  167. return sch_direct_xmit(skb, q, dev, txq, root_lock);
  168. }
  169. void __qdisc_run(struct Qdisc *q)
  170. {
  171. int quota = weight_p;
  172. while (qdisc_restart(q)) {
  173. /*
  174. * Ordered by possible occurrence: Postpone processing if
  175. * 1. we've exceeded packet quota
  176. * 2. another process needs the CPU;
  177. */
  178. if (--quota <= 0 || need_resched()) {
  179. __netif_schedule(q);
  180. break;
  181. }
  182. }
  183. qdisc_run_end(q);
  184. }
  185. unsigned long dev_trans_start(struct net_device *dev)
  186. {
  187. unsigned long val, res;
  188. unsigned int i;
  189. if (is_vlan_dev(dev))
  190. dev = vlan_dev_real_dev(dev);
  191. res = dev->trans_start;
  192. for (i = 0; i < dev->num_tx_queues; i++) {
  193. val = netdev_get_tx_queue(dev, i)->trans_start;
  194. if (val && time_after(val, res))
  195. res = val;
  196. }
  197. dev->trans_start = res;
  198. return res;
  199. }
  200. EXPORT_SYMBOL(dev_trans_start);
  201. static void dev_watchdog(unsigned long arg)
  202. {
  203. struct net_device *dev = (struct net_device *)arg;
  204. netif_tx_lock(dev);
  205. if (!qdisc_tx_is_noop(dev)) {
  206. if (netif_device_present(dev) &&
  207. netif_running(dev) &&
  208. netif_carrier_ok(dev)) {
  209. int some_queue_timedout = 0;
  210. unsigned int i;
  211. unsigned long trans_start;
  212. for (i = 0; i < dev->num_tx_queues; i++) {
  213. struct netdev_queue *txq;
  214. txq = netdev_get_tx_queue(dev, i);
  215. /*
  216. * old device drivers set dev->trans_start
  217. */
  218. trans_start = txq->trans_start ? : dev->trans_start;
  219. if (netif_xmit_stopped(txq) &&
  220. time_after(jiffies, (trans_start +
  221. dev->watchdog_timeo))) {
  222. some_queue_timedout = 1;
  223. txq->trans_timeout++;
  224. break;
  225. }
  226. }
  227. if (some_queue_timedout) {
  228. WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
  229. dev->name, netdev_drivername(dev), i);
  230. dev->netdev_ops->ndo_tx_timeout(dev);
  231. }
  232. if (!mod_timer(&dev->watchdog_timer,
  233. round_jiffies(jiffies +
  234. dev->watchdog_timeo)))
  235. dev_hold(dev);
  236. }
  237. }
  238. netif_tx_unlock(dev);
  239. dev_put(dev);
  240. }
  241. void __netdev_watchdog_up(struct net_device *dev)
  242. {
  243. if (dev->netdev_ops->ndo_tx_timeout) {
  244. if (dev->watchdog_timeo <= 0)
  245. dev->watchdog_timeo = 5*HZ;
  246. if (!mod_timer(&dev->watchdog_timer,
  247. round_jiffies(jiffies + dev->watchdog_timeo)))
  248. dev_hold(dev);
  249. }
  250. }
  251. static void dev_watchdog_up(struct net_device *dev)
  252. {
  253. __netdev_watchdog_up(dev);
  254. }
  255. static void dev_watchdog_down(struct net_device *dev)
  256. {
  257. netif_tx_lock_bh(dev);
  258. if (del_timer(&dev->watchdog_timer))
  259. dev_put(dev);
  260. netif_tx_unlock_bh(dev);
  261. }
  262. /**
  263. * netif_carrier_on - set carrier
  264. * @dev: network device
  265. *
  266. * Device has detected that carrier.
  267. */
  268. void netif_carrier_on(struct net_device *dev)
  269. {
  270. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  271. if (dev->reg_state == NETREG_UNINITIALIZED)
  272. return;
  273. atomic_inc(&dev->carrier_changes);
  274. linkwatch_fire_event(dev);
  275. if (netif_running(dev))
  276. __netdev_watchdog_up(dev);
  277. }
  278. }
  279. EXPORT_SYMBOL(netif_carrier_on);
  280. /**
  281. * netif_carrier_off - clear carrier
  282. * @dev: network device
  283. *
  284. * Device has detected loss of carrier.
  285. */
  286. void netif_carrier_off(struct net_device *dev)
  287. {
  288. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  289. if (dev->reg_state == NETREG_UNINITIALIZED)
  290. return;
  291. atomic_inc(&dev->carrier_changes);
  292. linkwatch_fire_event(dev);
  293. }
  294. }
  295. EXPORT_SYMBOL(netif_carrier_off);
  296. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  297. under all circumstances. It is difficult to invent anything faster or
  298. cheaper.
  299. */
  300. static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  301. {
  302. kfree_skb(skb);
  303. return NET_XMIT_CN;
  304. }
  305. static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
  306. {
  307. return NULL;
  308. }
  309. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  310. .id = "noop",
  311. .priv_size = 0,
  312. .enqueue = noop_enqueue,
  313. .dequeue = noop_dequeue,
  314. .peek = noop_dequeue,
  315. .owner = THIS_MODULE,
  316. };
  317. static struct netdev_queue noop_netdev_queue = {
  318. .qdisc = &noop_qdisc,
  319. .qdisc_sleeping = &noop_qdisc,
  320. };
  321. struct Qdisc noop_qdisc = {
  322. .enqueue = noop_enqueue,
  323. .dequeue = noop_dequeue,
  324. .flags = TCQ_F_BUILTIN,
  325. .ops = &noop_qdisc_ops,
  326. .list = LIST_HEAD_INIT(noop_qdisc.list),
  327. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  328. .dev_queue = &noop_netdev_queue,
  329. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  330. };
  331. EXPORT_SYMBOL(noop_qdisc);
  332. static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  333. .id = "noqueue",
  334. .priv_size = 0,
  335. .enqueue = noop_enqueue,
  336. .dequeue = noop_dequeue,
  337. .peek = noop_dequeue,
  338. .owner = THIS_MODULE,
  339. };
  340. static struct Qdisc noqueue_qdisc;
  341. static struct netdev_queue noqueue_netdev_queue = {
  342. .qdisc = &noqueue_qdisc,
  343. .qdisc_sleeping = &noqueue_qdisc,
  344. };
  345. static struct Qdisc noqueue_qdisc = {
  346. .enqueue = NULL,
  347. .dequeue = noop_dequeue,
  348. .flags = TCQ_F_BUILTIN,
  349. .ops = &noqueue_qdisc_ops,
  350. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  351. .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
  352. .dev_queue = &noqueue_netdev_queue,
  353. .busylock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.busylock),
  354. };
  355. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  356. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  357. };
  358. /* 3-band FIFO queue: old style, but should be a bit faster than
  359. generic prio+fifo combination.
  360. */
  361. #define PFIFO_FAST_BANDS 3
  362. /*
  363. * Private data for a pfifo_fast scheduler containing:
  364. * - queues for the three band
  365. * - bitmap indicating which of the bands contain skbs
  366. */
  367. struct pfifo_fast_priv {
  368. u32 bitmap;
  369. struct sk_buff_head q[PFIFO_FAST_BANDS];
  370. };
  371. /*
  372. * Convert a bitmap to the first band number where an skb is queued, where:
  373. * bitmap=0 means there are no skbs on any band.
  374. * bitmap=1 means there is an skb on band 0.
  375. * bitmap=7 means there are skbs on all 3 bands, etc.
  376. */
  377. static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
  378. static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
  379. int band)
  380. {
  381. return priv->q + band;
  382. }
  383. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  384. {
  385. if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
  386. int band = prio2band[skb->priority & TC_PRIO_MAX];
  387. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  388. struct sk_buff_head *list = band2list(priv, band);
  389. priv->bitmap |= (1 << band);
  390. qdisc->q.qlen++;
  391. return __qdisc_enqueue_tail(skb, qdisc, list);
  392. }
  393. return qdisc_drop(skb, qdisc);
  394. }
  395. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  396. {
  397. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  398. int band = bitmap2band[priv->bitmap];
  399. if (likely(band >= 0)) {
  400. struct sk_buff_head *list = band2list(priv, band);
  401. struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
  402. qdisc->q.qlen--;
  403. if (skb_queue_empty(list))
  404. priv->bitmap &= ~(1 << band);
  405. return skb;
  406. }
  407. return NULL;
  408. }
  409. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  410. {
  411. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  412. int band = bitmap2band[priv->bitmap];
  413. if (band >= 0) {
  414. struct sk_buff_head *list = band2list(priv, band);
  415. return skb_peek(list);
  416. }
  417. return NULL;
  418. }
  419. static void pfifo_fast_reset(struct Qdisc *qdisc)
  420. {
  421. int prio;
  422. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  423. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  424. __qdisc_reset_queue(qdisc, band2list(priv, prio));
  425. priv->bitmap = 0;
  426. qdisc->qstats.backlog = 0;
  427. qdisc->q.qlen = 0;
  428. }
  429. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  430. {
  431. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  432. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  433. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  434. goto nla_put_failure;
  435. return skb->len;
  436. nla_put_failure:
  437. return -1;
  438. }
  439. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  440. {
  441. int prio;
  442. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  443. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  444. skb_queue_head_init(band2list(priv, prio));
  445. /* Can by-pass the queue discipline */
  446. qdisc->flags |= TCQ_F_CAN_BYPASS;
  447. return 0;
  448. }
  449. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  450. .id = "pfifo_fast",
  451. .priv_size = sizeof(struct pfifo_fast_priv),
  452. .enqueue = pfifo_fast_enqueue,
  453. .dequeue = pfifo_fast_dequeue,
  454. .peek = pfifo_fast_peek,
  455. .init = pfifo_fast_init,
  456. .reset = pfifo_fast_reset,
  457. .dump = pfifo_fast_dump,
  458. .owner = THIS_MODULE,
  459. };
  460. static struct lock_class_key qdisc_tx_busylock;
  461. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  462. const struct Qdisc_ops *ops)
  463. {
  464. void *p;
  465. struct Qdisc *sch;
  466. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  467. int err = -ENOBUFS;
  468. struct net_device *dev = dev_queue->dev;
  469. p = kzalloc_node(size, GFP_KERNEL,
  470. netdev_queue_numa_node_read(dev_queue));
  471. if (!p)
  472. goto errout;
  473. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  474. /* if we got non aligned memory, ask more and do alignment ourself */
  475. if (sch != p) {
  476. kfree(p);
  477. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  478. netdev_queue_numa_node_read(dev_queue));
  479. if (!p)
  480. goto errout;
  481. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  482. sch->padded = (char *) sch - (char *) p;
  483. }
  484. INIT_LIST_HEAD(&sch->list);
  485. skb_queue_head_init(&sch->q);
  486. spin_lock_init(&sch->busylock);
  487. lockdep_set_class(&sch->busylock,
  488. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  489. sch->ops = ops;
  490. sch->enqueue = ops->enqueue;
  491. sch->dequeue = ops->dequeue;
  492. sch->dev_queue = dev_queue;
  493. dev_hold(dev);
  494. atomic_set(&sch->refcnt, 1);
  495. return sch;
  496. errout:
  497. return ERR_PTR(err);
  498. }
  499. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  500. const struct Qdisc_ops *ops,
  501. unsigned int parentid)
  502. {
  503. struct Qdisc *sch;
  504. if (!try_module_get(ops->owner))
  505. goto errout;
  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. default_qdisc_ops, TC_H_ROOT);
  588. if (!qdisc) {
  589. netdev_info(dev, "activation failed\n");
  590. return;
  591. }
  592. if (!netif_is_multiqueue(dev))
  593. qdisc->flags |= TCQ_F_ONETXQUEUE;
  594. }
  595. dev_queue->qdisc_sleeping = qdisc;
  596. }
  597. static void attach_default_qdiscs(struct net_device *dev)
  598. {
  599. struct netdev_queue *txq;
  600. struct Qdisc *qdisc;
  601. txq = netdev_get_tx_queue(dev, 0);
  602. if (!netif_is_multiqueue(dev) || dev->tx_queue_len == 0) {
  603. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  604. dev->qdisc = txq->qdisc_sleeping;
  605. atomic_inc(&dev->qdisc->refcnt);
  606. } else {
  607. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
  608. if (qdisc) {
  609. dev->qdisc = qdisc;
  610. qdisc->ops->attach(qdisc);
  611. }
  612. }
  613. }
  614. static void transition_one_qdisc(struct net_device *dev,
  615. struct netdev_queue *dev_queue,
  616. void *_need_watchdog)
  617. {
  618. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  619. int *need_watchdog_p = _need_watchdog;
  620. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  621. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  622. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  623. if (need_watchdog_p && new_qdisc != &noqueue_qdisc) {
  624. dev_queue->trans_start = 0;
  625. *need_watchdog_p = 1;
  626. }
  627. }
  628. void dev_activate(struct net_device *dev)
  629. {
  630. int need_watchdog;
  631. /* No queueing discipline is attached to device;
  632. * create default one for devices, which need queueing
  633. * and noqueue_qdisc for 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, close_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, close_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->close_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. }
  762. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  763. const struct tc_ratespec *conf,
  764. u64 rate64)
  765. {
  766. memset(r, 0, sizeof(*r));
  767. r->overhead = conf->overhead;
  768. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  769. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  770. r->mult = 1;
  771. /*
  772. * The deal here is to replace a divide by a reciprocal one
  773. * in fast path (a reciprocal divide is a multiply and a shift)
  774. *
  775. * Normal formula would be :
  776. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  777. *
  778. * We compute mult/shift to use instead :
  779. * time_in_ns = (len * mult) >> shift;
  780. *
  781. * We try to get the highest possible mult value for accuracy,
  782. * but have to make sure no overflows will ever happen.
  783. */
  784. if (r->rate_bytes_ps > 0) {
  785. u64 factor = NSEC_PER_SEC;
  786. for (;;) {
  787. r->mult = div64_u64(factor, r->rate_bytes_ps);
  788. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  789. break;
  790. factor <<= 1;
  791. r->shift++;
  792. }
  793. }
  794. }
  795. EXPORT_SYMBOL(psched_ratecfg_precompute);