sch_generic.c 24 KB

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