sch_generic.c 25 KB

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