sch_generic.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. .list = LIST_HEAD_INIT(noop_qdisc.list),
  377. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  378. .dev_queue = &noop_netdev_queue,
  379. .running = SEQCNT_ZERO(noop_qdisc.running),
  380. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  381. };
  382. EXPORT_SYMBOL(noop_qdisc);
  383. static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
  384. {
  385. /* register_qdisc() assigns a default of noop_enqueue if unset,
  386. * but __dev_queue_xmit() treats noqueue only as such
  387. * if this is NULL - so clear it here. */
  388. qdisc->enqueue = NULL;
  389. return 0;
  390. }
  391. struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  392. .id = "noqueue",
  393. .priv_size = 0,
  394. .init = noqueue_init,
  395. .enqueue = noop_enqueue,
  396. .dequeue = noop_dequeue,
  397. .peek = noop_dequeue,
  398. .owner = THIS_MODULE,
  399. };
  400. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  401. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  402. };
  403. /* 3-band FIFO queue: old style, but should be a bit faster than
  404. generic prio+fifo combination.
  405. */
  406. #define PFIFO_FAST_BANDS 3
  407. /*
  408. * Private data for a pfifo_fast scheduler containing:
  409. * - queues for the three band
  410. * - bitmap indicating which of the bands contain skbs
  411. */
  412. struct pfifo_fast_priv {
  413. u32 bitmap;
  414. struct sk_buff_head q[PFIFO_FAST_BANDS];
  415. };
  416. /*
  417. * Convert a bitmap to the first band number where an skb is queued, where:
  418. * bitmap=0 means there are no skbs on any band.
  419. * bitmap=1 means there is an skb on band 0.
  420. * bitmap=7 means there are skbs on all 3 bands, etc.
  421. */
  422. static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
  423. static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
  424. int band)
  425. {
  426. return priv->q + band;
  427. }
  428. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
  429. struct sk_buff **to_free)
  430. {
  431. if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
  432. int band = prio2band[skb->priority & TC_PRIO_MAX];
  433. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  434. struct sk_buff_head *list = band2list(priv, band);
  435. priv->bitmap |= (1 << band);
  436. qdisc->q.qlen++;
  437. return __qdisc_enqueue_tail(skb, qdisc, list);
  438. }
  439. return qdisc_drop(skb, qdisc, to_free);
  440. }
  441. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  442. {
  443. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  444. int band = bitmap2band[priv->bitmap];
  445. if (likely(band >= 0)) {
  446. struct sk_buff_head *list = band2list(priv, band);
  447. struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
  448. qdisc->q.qlen--;
  449. if (skb_queue_empty(list))
  450. priv->bitmap &= ~(1 << band);
  451. return skb;
  452. }
  453. return NULL;
  454. }
  455. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  456. {
  457. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  458. int band = bitmap2band[priv->bitmap];
  459. if (band >= 0) {
  460. struct sk_buff_head *list = band2list(priv, band);
  461. return skb_peek(list);
  462. }
  463. return NULL;
  464. }
  465. static void pfifo_fast_reset(struct Qdisc *qdisc)
  466. {
  467. int prio;
  468. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  469. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  470. __qdisc_reset_queue(band2list(priv, prio));
  471. priv->bitmap = 0;
  472. qdisc->qstats.backlog = 0;
  473. qdisc->q.qlen = 0;
  474. }
  475. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  476. {
  477. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  478. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  479. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  480. goto nla_put_failure;
  481. return skb->len;
  482. nla_put_failure:
  483. return -1;
  484. }
  485. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  486. {
  487. int prio;
  488. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  489. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  490. __skb_queue_head_init(band2list(priv, prio));
  491. /* Can by-pass the queue discipline */
  492. qdisc->flags |= TCQ_F_CAN_BYPASS;
  493. return 0;
  494. }
  495. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  496. .id = "pfifo_fast",
  497. .priv_size = sizeof(struct pfifo_fast_priv),
  498. .enqueue = pfifo_fast_enqueue,
  499. .dequeue = pfifo_fast_dequeue,
  500. .peek = pfifo_fast_peek,
  501. .init = pfifo_fast_init,
  502. .reset = pfifo_fast_reset,
  503. .dump = pfifo_fast_dump,
  504. .owner = THIS_MODULE,
  505. };
  506. EXPORT_SYMBOL(pfifo_fast_ops);
  507. static struct lock_class_key qdisc_tx_busylock;
  508. static struct lock_class_key qdisc_running_key;
  509. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  510. const struct Qdisc_ops *ops)
  511. {
  512. void *p;
  513. struct Qdisc *sch;
  514. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  515. int err = -ENOBUFS;
  516. struct net_device *dev = dev_queue->dev;
  517. p = kzalloc_node(size, GFP_KERNEL,
  518. netdev_queue_numa_node_read(dev_queue));
  519. if (!p)
  520. goto errout;
  521. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  522. /* if we got non aligned memory, ask more and do alignment ourself */
  523. if (sch != p) {
  524. kfree(p);
  525. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  526. netdev_queue_numa_node_read(dev_queue));
  527. if (!p)
  528. goto errout;
  529. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  530. sch->padded = (char *) sch - (char *) p;
  531. }
  532. INIT_LIST_HEAD(&sch->list);
  533. skb_queue_head_init(&sch->q);
  534. spin_lock_init(&sch->busylock);
  535. lockdep_set_class(&sch->busylock,
  536. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  537. seqcount_init(&sch->running);
  538. lockdep_set_class(&sch->running,
  539. dev->qdisc_running_key ?: &qdisc_running_key);
  540. sch->ops = ops;
  541. sch->enqueue = ops->enqueue;
  542. sch->dequeue = ops->dequeue;
  543. sch->dev_queue = dev_queue;
  544. dev_hold(dev);
  545. atomic_set(&sch->refcnt, 1);
  546. return sch;
  547. errout:
  548. return ERR_PTR(err);
  549. }
  550. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  551. const struct Qdisc_ops *ops,
  552. unsigned int parentid)
  553. {
  554. struct Qdisc *sch;
  555. if (!try_module_get(ops->owner))
  556. goto errout;
  557. sch = qdisc_alloc(dev_queue, ops);
  558. if (IS_ERR(sch))
  559. goto errout;
  560. sch->parent = parentid;
  561. if (!ops->init || ops->init(sch, NULL) == 0)
  562. return sch;
  563. qdisc_destroy(sch);
  564. errout:
  565. return NULL;
  566. }
  567. EXPORT_SYMBOL(qdisc_create_dflt);
  568. /* Under qdisc_lock(qdisc) and BH! */
  569. void qdisc_reset(struct Qdisc *qdisc)
  570. {
  571. const struct Qdisc_ops *ops = qdisc->ops;
  572. if (ops->reset)
  573. ops->reset(qdisc);
  574. kfree_skb(qdisc->skb_bad_txq);
  575. qdisc->skb_bad_txq = NULL;
  576. if (qdisc->gso_skb) {
  577. kfree_skb_list(qdisc->gso_skb);
  578. qdisc->gso_skb = NULL;
  579. }
  580. qdisc->q.qlen = 0;
  581. }
  582. EXPORT_SYMBOL(qdisc_reset);
  583. static void qdisc_rcu_free(struct rcu_head *head)
  584. {
  585. struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
  586. if (qdisc_is_percpu_stats(qdisc)) {
  587. free_percpu(qdisc->cpu_bstats);
  588. free_percpu(qdisc->cpu_qstats);
  589. }
  590. kfree((char *) qdisc - qdisc->padded);
  591. }
  592. void qdisc_destroy(struct Qdisc *qdisc)
  593. {
  594. const struct Qdisc_ops *ops = qdisc->ops;
  595. if (qdisc->flags & TCQ_F_BUILTIN ||
  596. !atomic_dec_and_test(&qdisc->refcnt))
  597. return;
  598. #ifdef CONFIG_NET_SCHED
  599. qdisc_list_del(qdisc);
  600. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  601. #endif
  602. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  603. if (ops->reset)
  604. ops->reset(qdisc);
  605. if (ops->destroy)
  606. ops->destroy(qdisc);
  607. module_put(ops->owner);
  608. dev_put(qdisc_dev(qdisc));
  609. kfree_skb_list(qdisc->gso_skb);
  610. kfree_skb(qdisc->skb_bad_txq);
  611. /*
  612. * gen_estimator est_timer() might access qdisc->q.lock,
  613. * wait a RCU grace period before freeing qdisc.
  614. */
  615. call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
  616. }
  617. EXPORT_SYMBOL(qdisc_destroy);
  618. /* Attach toplevel qdisc to device queue. */
  619. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  620. struct Qdisc *qdisc)
  621. {
  622. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  623. spinlock_t *root_lock;
  624. root_lock = qdisc_lock(oqdisc);
  625. spin_lock_bh(root_lock);
  626. /* Prune old scheduler */
  627. if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
  628. qdisc_reset(oqdisc);
  629. /* ... and graft new one */
  630. if (qdisc == NULL)
  631. qdisc = &noop_qdisc;
  632. dev_queue->qdisc_sleeping = qdisc;
  633. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  634. spin_unlock_bh(root_lock);
  635. return oqdisc;
  636. }
  637. EXPORT_SYMBOL(dev_graft_qdisc);
  638. static void attach_one_default_qdisc(struct net_device *dev,
  639. struct netdev_queue *dev_queue,
  640. void *_unused)
  641. {
  642. struct Qdisc *qdisc;
  643. const struct Qdisc_ops *ops = default_qdisc_ops;
  644. if (dev->priv_flags & IFF_NO_QUEUE)
  645. ops = &noqueue_qdisc_ops;
  646. qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT);
  647. if (!qdisc) {
  648. netdev_info(dev, "activation failed\n");
  649. return;
  650. }
  651. if (!netif_is_multiqueue(dev))
  652. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  653. dev_queue->qdisc_sleeping = qdisc;
  654. }
  655. static void attach_default_qdiscs(struct net_device *dev)
  656. {
  657. struct netdev_queue *txq;
  658. struct Qdisc *qdisc;
  659. txq = netdev_get_tx_queue(dev, 0);
  660. if (!netif_is_multiqueue(dev) ||
  661. dev->priv_flags & IFF_NO_QUEUE) {
  662. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  663. dev->qdisc = txq->qdisc_sleeping;
  664. atomic_inc(&dev->qdisc->refcnt);
  665. } else {
  666. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
  667. if (qdisc) {
  668. dev->qdisc = qdisc;
  669. qdisc->ops->attach(qdisc);
  670. }
  671. }
  672. }
  673. static void transition_one_qdisc(struct net_device *dev,
  674. struct netdev_queue *dev_queue,
  675. void *_need_watchdog)
  676. {
  677. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  678. int *need_watchdog_p = _need_watchdog;
  679. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  680. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  681. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  682. if (need_watchdog_p) {
  683. dev_queue->trans_start = 0;
  684. *need_watchdog_p = 1;
  685. }
  686. }
  687. void dev_activate(struct net_device *dev)
  688. {
  689. int need_watchdog;
  690. /* No queueing discipline is attached to device;
  691. * create default one for devices, which need queueing
  692. * and noqueue_qdisc for virtual interfaces
  693. */
  694. if (dev->qdisc == &noop_qdisc)
  695. attach_default_qdiscs(dev);
  696. if (!netif_carrier_ok(dev))
  697. /* Delay activation until next carrier-on event */
  698. return;
  699. need_watchdog = 0;
  700. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  701. if (dev_ingress_queue(dev))
  702. transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
  703. if (need_watchdog) {
  704. netif_trans_update(dev);
  705. dev_watchdog_up(dev);
  706. }
  707. }
  708. EXPORT_SYMBOL(dev_activate);
  709. static void dev_deactivate_queue(struct net_device *dev,
  710. struct netdev_queue *dev_queue,
  711. void *_qdisc_default)
  712. {
  713. struct Qdisc *qdisc_default = _qdisc_default;
  714. struct Qdisc *qdisc;
  715. qdisc = rtnl_dereference(dev_queue->qdisc);
  716. if (qdisc) {
  717. spin_lock_bh(qdisc_lock(qdisc));
  718. if (!(qdisc->flags & TCQ_F_BUILTIN))
  719. set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
  720. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  721. qdisc_reset(qdisc);
  722. spin_unlock_bh(qdisc_lock(qdisc));
  723. }
  724. }
  725. static bool some_qdisc_is_busy(struct net_device *dev)
  726. {
  727. unsigned int i;
  728. for (i = 0; i < dev->num_tx_queues; i++) {
  729. struct netdev_queue *dev_queue;
  730. spinlock_t *root_lock;
  731. struct Qdisc *q;
  732. int val;
  733. dev_queue = netdev_get_tx_queue(dev, i);
  734. q = dev_queue->qdisc_sleeping;
  735. root_lock = qdisc_lock(q);
  736. spin_lock_bh(root_lock);
  737. val = (qdisc_is_running(q) ||
  738. test_bit(__QDISC_STATE_SCHED, &q->state));
  739. spin_unlock_bh(root_lock);
  740. if (val)
  741. return true;
  742. }
  743. return false;
  744. }
  745. /**
  746. * dev_deactivate_many - deactivate transmissions on several devices
  747. * @head: list of devices to deactivate
  748. *
  749. * This function returns only when all outstanding transmissions
  750. * have completed, unless all devices are in dismantle phase.
  751. */
  752. void dev_deactivate_many(struct list_head *head)
  753. {
  754. struct net_device *dev;
  755. bool sync_needed = false;
  756. list_for_each_entry(dev, head, close_list) {
  757. netdev_for_each_tx_queue(dev, dev_deactivate_queue,
  758. &noop_qdisc);
  759. if (dev_ingress_queue(dev))
  760. dev_deactivate_queue(dev, dev_ingress_queue(dev),
  761. &noop_qdisc);
  762. dev_watchdog_down(dev);
  763. sync_needed |= !dev->dismantle;
  764. }
  765. /* Wait for outstanding qdisc-less dev_queue_xmit calls.
  766. * This is avoided if all devices are in dismantle phase :
  767. * Caller will call synchronize_net() for us
  768. */
  769. if (sync_needed)
  770. synchronize_net();
  771. /* Wait for outstanding qdisc_run calls. */
  772. list_for_each_entry(dev, head, close_list)
  773. while (some_qdisc_is_busy(dev))
  774. yield();
  775. }
  776. void dev_deactivate(struct net_device *dev)
  777. {
  778. LIST_HEAD(single);
  779. list_add(&dev->close_list, &single);
  780. dev_deactivate_many(&single);
  781. list_del(&single);
  782. }
  783. EXPORT_SYMBOL(dev_deactivate);
  784. static void dev_init_scheduler_queue(struct net_device *dev,
  785. struct netdev_queue *dev_queue,
  786. void *_qdisc)
  787. {
  788. struct Qdisc *qdisc = _qdisc;
  789. rcu_assign_pointer(dev_queue->qdisc, qdisc);
  790. dev_queue->qdisc_sleeping = qdisc;
  791. }
  792. void dev_init_scheduler(struct net_device *dev)
  793. {
  794. dev->qdisc = &noop_qdisc;
  795. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  796. if (dev_ingress_queue(dev))
  797. dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  798. setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
  799. }
  800. static void shutdown_scheduler_queue(struct net_device *dev,
  801. struct netdev_queue *dev_queue,
  802. void *_qdisc_default)
  803. {
  804. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  805. struct Qdisc *qdisc_default = _qdisc_default;
  806. if (qdisc) {
  807. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  808. dev_queue->qdisc_sleeping = qdisc_default;
  809. qdisc_destroy(qdisc);
  810. }
  811. }
  812. void dev_shutdown(struct net_device *dev)
  813. {
  814. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  815. if (dev_ingress_queue(dev))
  816. shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  817. qdisc_destroy(dev->qdisc);
  818. dev->qdisc = &noop_qdisc;
  819. WARN_ON(timer_pending(&dev->watchdog_timer));
  820. }
  821. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  822. const struct tc_ratespec *conf,
  823. u64 rate64)
  824. {
  825. memset(r, 0, sizeof(*r));
  826. r->overhead = conf->overhead;
  827. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  828. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  829. r->mult = 1;
  830. /*
  831. * The deal here is to replace a divide by a reciprocal one
  832. * in fast path (a reciprocal divide is a multiply and a shift)
  833. *
  834. * Normal formula would be :
  835. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  836. *
  837. * We compute mult/shift to use instead :
  838. * time_in_ns = (len * mult) >> shift;
  839. *
  840. * We try to get the highest possible mult value for accuracy,
  841. * but have to make sure no overflows will ever happen.
  842. */
  843. if (r->rate_bytes_ps > 0) {
  844. u64 factor = NSEC_PER_SEC;
  845. for (;;) {
  846. r->mult = div64_u64(factor, r->rate_bytes_ps);
  847. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  848. break;
  849. factor <<= 1;
  850. r->shift++;
  851. }
  852. }
  853. }
  854. EXPORT_SYMBOL(psched_ratecfg_precompute);