sch_generic.c 24 KB

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