sch_generic.c 24 KB

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