sch_generic.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. }
  586. EXPORT_SYMBOL(qdisc_reset);
  587. static void qdisc_rcu_free(struct rcu_head *head)
  588. {
  589. struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
  590. if (qdisc_is_percpu_stats(qdisc)) {
  591. free_percpu(qdisc->cpu_bstats);
  592. free_percpu(qdisc->cpu_qstats);
  593. }
  594. kfree((char *) qdisc - qdisc->padded);
  595. }
  596. void qdisc_destroy(struct Qdisc *qdisc)
  597. {
  598. const struct Qdisc_ops *ops = qdisc->ops;
  599. if (qdisc->flags & TCQ_F_BUILTIN ||
  600. !atomic_dec_and_test(&qdisc->refcnt))
  601. return;
  602. #ifdef CONFIG_NET_SCHED
  603. qdisc_hash_del(qdisc);
  604. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  605. #endif
  606. gen_kill_estimator(&qdisc->rate_est);
  607. if (ops->reset)
  608. ops->reset(qdisc);
  609. if (ops->destroy)
  610. ops->destroy(qdisc);
  611. module_put(ops->owner);
  612. dev_put(qdisc_dev(qdisc));
  613. kfree_skb_list(qdisc->gso_skb);
  614. kfree_skb(qdisc->skb_bad_txq);
  615. /*
  616. * gen_estimator est_timer() might access qdisc->q.lock,
  617. * wait a RCU grace period before freeing qdisc.
  618. */
  619. call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
  620. }
  621. EXPORT_SYMBOL(qdisc_destroy);
  622. /* Attach toplevel qdisc to device queue. */
  623. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  624. struct Qdisc *qdisc)
  625. {
  626. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  627. spinlock_t *root_lock;
  628. root_lock = qdisc_lock(oqdisc);
  629. spin_lock_bh(root_lock);
  630. /* Prune old scheduler */
  631. if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
  632. qdisc_reset(oqdisc);
  633. /* ... and graft new one */
  634. if (qdisc == NULL)
  635. qdisc = &noop_qdisc;
  636. dev_queue->qdisc_sleeping = qdisc;
  637. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  638. spin_unlock_bh(root_lock);
  639. return oqdisc;
  640. }
  641. EXPORT_SYMBOL(dev_graft_qdisc);
  642. static void attach_one_default_qdisc(struct net_device *dev,
  643. struct netdev_queue *dev_queue,
  644. void *_unused)
  645. {
  646. struct Qdisc *qdisc;
  647. const struct Qdisc_ops *ops = default_qdisc_ops;
  648. if (dev->priv_flags & IFF_NO_QUEUE)
  649. ops = &noqueue_qdisc_ops;
  650. qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
  651. if (!qdisc) {
  652. netdev_info(dev, "activation failed\n");
  653. return;
  654. }
  655. if (!netif_is_multiqueue(dev))
  656. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  657. dev_queue->qdisc_sleeping = qdisc;
  658. }
  659. static void attach_default_qdiscs(struct net_device *dev)
  660. {
  661. struct netdev_queue *txq;
  662. struct Qdisc *qdisc;
  663. txq = netdev_get_tx_queue(dev, 0);
  664. if (!netif_is_multiqueue(dev) ||
  665. dev->priv_flags & IFF_NO_QUEUE) {
  666. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  667. dev->qdisc = txq->qdisc_sleeping;
  668. atomic_inc(&dev->qdisc->refcnt);
  669. } else {
  670. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
  671. if (qdisc) {
  672. dev->qdisc = qdisc;
  673. qdisc->ops->attach(qdisc);
  674. }
  675. }
  676. #ifdef CONFIG_NET_SCHED
  677. if (dev->qdisc)
  678. qdisc_hash_add(dev->qdisc);
  679. #endif
  680. }
  681. static void transition_one_qdisc(struct net_device *dev,
  682. struct netdev_queue *dev_queue,
  683. void *_need_watchdog)
  684. {
  685. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  686. int *need_watchdog_p = _need_watchdog;
  687. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  688. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  689. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  690. if (need_watchdog_p) {
  691. dev_queue->trans_start = 0;
  692. *need_watchdog_p = 1;
  693. }
  694. }
  695. void dev_activate(struct net_device *dev)
  696. {
  697. int need_watchdog;
  698. /* No queueing discipline is attached to device;
  699. * create default one for devices, which need queueing
  700. * and noqueue_qdisc for virtual interfaces
  701. */
  702. if (dev->qdisc == &noop_qdisc)
  703. attach_default_qdiscs(dev);
  704. if (!netif_carrier_ok(dev))
  705. /* Delay activation until next carrier-on event */
  706. return;
  707. need_watchdog = 0;
  708. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  709. if (dev_ingress_queue(dev))
  710. transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
  711. if (need_watchdog) {
  712. netif_trans_update(dev);
  713. dev_watchdog_up(dev);
  714. }
  715. }
  716. EXPORT_SYMBOL(dev_activate);
  717. static void dev_deactivate_queue(struct net_device *dev,
  718. struct netdev_queue *dev_queue,
  719. void *_qdisc_default)
  720. {
  721. struct Qdisc *qdisc_default = _qdisc_default;
  722. struct Qdisc *qdisc;
  723. qdisc = rtnl_dereference(dev_queue->qdisc);
  724. if (qdisc) {
  725. spin_lock_bh(qdisc_lock(qdisc));
  726. if (!(qdisc->flags & TCQ_F_BUILTIN))
  727. set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
  728. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  729. qdisc_reset(qdisc);
  730. spin_unlock_bh(qdisc_lock(qdisc));
  731. }
  732. }
  733. static bool some_qdisc_is_busy(struct net_device *dev)
  734. {
  735. unsigned int i;
  736. for (i = 0; i < dev->num_tx_queues; i++) {
  737. struct netdev_queue *dev_queue;
  738. spinlock_t *root_lock;
  739. struct Qdisc *q;
  740. int val;
  741. dev_queue = netdev_get_tx_queue(dev, i);
  742. q = dev_queue->qdisc_sleeping;
  743. root_lock = qdisc_lock(q);
  744. spin_lock_bh(root_lock);
  745. val = (qdisc_is_running(q) ||
  746. test_bit(__QDISC_STATE_SCHED, &q->state));
  747. spin_unlock_bh(root_lock);
  748. if (val)
  749. return true;
  750. }
  751. return false;
  752. }
  753. /**
  754. * dev_deactivate_many - deactivate transmissions on several devices
  755. * @head: list of devices to deactivate
  756. *
  757. * This function returns only when all outstanding transmissions
  758. * have completed, unless all devices are in dismantle phase.
  759. */
  760. void dev_deactivate_many(struct list_head *head)
  761. {
  762. struct net_device *dev;
  763. bool sync_needed = false;
  764. list_for_each_entry(dev, head, close_list) {
  765. netdev_for_each_tx_queue(dev, dev_deactivate_queue,
  766. &noop_qdisc);
  767. if (dev_ingress_queue(dev))
  768. dev_deactivate_queue(dev, dev_ingress_queue(dev),
  769. &noop_qdisc);
  770. dev_watchdog_down(dev);
  771. sync_needed |= !dev->dismantle;
  772. }
  773. /* Wait for outstanding qdisc-less dev_queue_xmit calls.
  774. * This is avoided if all devices are in dismantle phase :
  775. * Caller will call synchronize_net() for us
  776. */
  777. if (sync_needed)
  778. synchronize_net();
  779. /* Wait for outstanding qdisc_run calls. */
  780. list_for_each_entry(dev, head, close_list)
  781. while (some_qdisc_is_busy(dev))
  782. yield();
  783. }
  784. void dev_deactivate(struct net_device *dev)
  785. {
  786. LIST_HEAD(single);
  787. list_add(&dev->close_list, &single);
  788. dev_deactivate_many(&single);
  789. list_del(&single);
  790. }
  791. EXPORT_SYMBOL(dev_deactivate);
  792. static void dev_init_scheduler_queue(struct net_device *dev,
  793. struct netdev_queue *dev_queue,
  794. void *_qdisc)
  795. {
  796. struct Qdisc *qdisc = _qdisc;
  797. rcu_assign_pointer(dev_queue->qdisc, qdisc);
  798. dev_queue->qdisc_sleeping = qdisc;
  799. }
  800. void dev_init_scheduler(struct net_device *dev)
  801. {
  802. dev->qdisc = &noop_qdisc;
  803. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  804. if (dev_ingress_queue(dev))
  805. dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  806. setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
  807. }
  808. static void shutdown_scheduler_queue(struct net_device *dev,
  809. struct netdev_queue *dev_queue,
  810. void *_qdisc_default)
  811. {
  812. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  813. struct Qdisc *qdisc_default = _qdisc_default;
  814. if (qdisc) {
  815. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  816. dev_queue->qdisc_sleeping = qdisc_default;
  817. qdisc_destroy(qdisc);
  818. }
  819. }
  820. void dev_shutdown(struct net_device *dev)
  821. {
  822. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  823. if (dev_ingress_queue(dev))
  824. shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  825. qdisc_destroy(dev->qdisc);
  826. dev->qdisc = &noop_qdisc;
  827. WARN_ON(timer_pending(&dev->watchdog_timer));
  828. }
  829. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  830. const struct tc_ratespec *conf,
  831. u64 rate64)
  832. {
  833. memset(r, 0, sizeof(*r));
  834. r->overhead = conf->overhead;
  835. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  836. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  837. r->mult = 1;
  838. /*
  839. * The deal here is to replace a divide by a reciprocal one
  840. * in fast path (a reciprocal divide is a multiply and a shift)
  841. *
  842. * Normal formula would be :
  843. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  844. *
  845. * We compute mult/shift to use instead :
  846. * time_in_ns = (len * mult) >> shift;
  847. *
  848. * We try to get the highest possible mult value for accuracy,
  849. * but have to make sure no overflows will ever happen.
  850. */
  851. if (r->rate_bytes_ps > 0) {
  852. u64 factor = NSEC_PER_SEC;
  853. for (;;) {
  854. r->mult = div64_u64(factor, r->rate_bytes_ps);
  855. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  856. break;
  857. factor <<= 1;
  858. r->shift++;
  859. }
  860. }
  861. }
  862. EXPORT_SYMBOL(psched_ratecfg_precompute);