sch_generic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 (likely(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. } else {
  149. spin_lock(root_lock);
  150. return qdisc_qlen(q);
  151. }
  152. spin_lock(root_lock);
  153. if (dev_xmit_complete(ret)) {
  154. /* Driver sent out skb successfully or skb was consumed */
  155. ret = qdisc_qlen(q);
  156. } else if (ret == NETDEV_TX_LOCKED) {
  157. /* Driver try lock failed */
  158. ret = handle_dev_cpu_collision(skb, txq, q);
  159. } else {
  160. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  161. if (unlikely(ret != NETDEV_TX_BUSY))
  162. net_warn_ratelimited("BUG %s code %d qlen %d\n",
  163. dev->name, ret, q->q.qlen);
  164. ret = dev_requeue_skb(skb, q);
  165. }
  166. if (ret && netif_xmit_frozen_or_stopped(txq))
  167. ret = 0;
  168. return ret;
  169. }
  170. /*
  171. * NOTE: Called under qdisc_lock(q) with locally disabled BH.
  172. *
  173. * __QDISC___STATE_RUNNING guarantees only one CPU can process
  174. * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
  175. * this queue.
  176. *
  177. * netif_tx_lock serializes accesses to device driver.
  178. *
  179. * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
  180. * if one is grabbed, another must be free.
  181. *
  182. * Note, that this procedure can be called by a watchdog timer
  183. *
  184. * Returns to the caller:
  185. * 0 - queue is empty or throttled.
  186. * >0 - queue is not empty.
  187. *
  188. */
  189. static inline int qdisc_restart(struct Qdisc *q, int *packets)
  190. {
  191. struct netdev_queue *txq;
  192. struct net_device *dev;
  193. spinlock_t *root_lock;
  194. struct sk_buff *skb;
  195. bool validate;
  196. /* Dequeue packet */
  197. skb = dequeue_skb(q, &validate, packets);
  198. if (unlikely(!skb))
  199. return 0;
  200. root_lock = qdisc_lock(q);
  201. dev = qdisc_dev(q);
  202. txq = skb_get_tx_queue(dev, skb);
  203. return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
  204. }
  205. void __qdisc_run(struct Qdisc *q)
  206. {
  207. int quota = weight_p;
  208. int packets;
  209. while (qdisc_restart(q, &packets)) {
  210. /*
  211. * Ordered by possible occurrence: Postpone processing if
  212. * 1. we've exceeded packet quota
  213. * 2. another process needs the CPU;
  214. */
  215. quota -= packets;
  216. if (quota <= 0 || need_resched()) {
  217. __netif_schedule(q);
  218. break;
  219. }
  220. }
  221. qdisc_run_end(q);
  222. }
  223. unsigned long dev_trans_start(struct net_device *dev)
  224. {
  225. unsigned long val, res;
  226. unsigned int i;
  227. if (is_vlan_dev(dev))
  228. dev = vlan_dev_real_dev(dev);
  229. res = dev->trans_start;
  230. for (i = 0; i < dev->num_tx_queues; i++) {
  231. val = netdev_get_tx_queue(dev, i)->trans_start;
  232. if (val && time_after(val, res))
  233. res = val;
  234. }
  235. dev->trans_start = res;
  236. return res;
  237. }
  238. EXPORT_SYMBOL(dev_trans_start);
  239. static void dev_watchdog(unsigned long arg)
  240. {
  241. struct net_device *dev = (struct net_device *)arg;
  242. netif_tx_lock(dev);
  243. if (!qdisc_tx_is_noop(dev)) {
  244. if (netif_device_present(dev) &&
  245. netif_running(dev) &&
  246. netif_carrier_ok(dev)) {
  247. int some_queue_timedout = 0;
  248. unsigned int i;
  249. unsigned long trans_start;
  250. for (i = 0; i < dev->num_tx_queues; i++) {
  251. struct netdev_queue *txq;
  252. txq = netdev_get_tx_queue(dev, i);
  253. /*
  254. * old device drivers set dev->trans_start
  255. */
  256. trans_start = txq->trans_start ? : dev->trans_start;
  257. if (netif_xmit_stopped(txq) &&
  258. time_after(jiffies, (trans_start +
  259. dev->watchdog_timeo))) {
  260. some_queue_timedout = 1;
  261. txq->trans_timeout++;
  262. break;
  263. }
  264. }
  265. if (some_queue_timedout) {
  266. WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
  267. dev->name, netdev_drivername(dev), i);
  268. dev->netdev_ops->ndo_tx_timeout(dev);
  269. }
  270. if (!mod_timer(&dev->watchdog_timer,
  271. round_jiffies(jiffies +
  272. dev->watchdog_timeo)))
  273. dev_hold(dev);
  274. }
  275. }
  276. netif_tx_unlock(dev);
  277. dev_put(dev);
  278. }
  279. void __netdev_watchdog_up(struct net_device *dev)
  280. {
  281. if (dev->netdev_ops->ndo_tx_timeout) {
  282. if (dev->watchdog_timeo <= 0)
  283. dev->watchdog_timeo = 5*HZ;
  284. if (!mod_timer(&dev->watchdog_timer,
  285. round_jiffies(jiffies + dev->watchdog_timeo)))
  286. dev_hold(dev);
  287. }
  288. }
  289. static void dev_watchdog_up(struct net_device *dev)
  290. {
  291. __netdev_watchdog_up(dev);
  292. }
  293. static void dev_watchdog_down(struct net_device *dev)
  294. {
  295. netif_tx_lock_bh(dev);
  296. if (del_timer(&dev->watchdog_timer))
  297. dev_put(dev);
  298. netif_tx_unlock_bh(dev);
  299. }
  300. /**
  301. * netif_carrier_on - set carrier
  302. * @dev: network device
  303. *
  304. * Device has detected that carrier.
  305. */
  306. void netif_carrier_on(struct net_device *dev)
  307. {
  308. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  309. if (dev->reg_state == NETREG_UNINITIALIZED)
  310. return;
  311. atomic_inc(&dev->carrier_changes);
  312. linkwatch_fire_event(dev);
  313. if (netif_running(dev))
  314. __netdev_watchdog_up(dev);
  315. }
  316. }
  317. EXPORT_SYMBOL(netif_carrier_on);
  318. /**
  319. * netif_carrier_off - clear carrier
  320. * @dev: network device
  321. *
  322. * Device has detected loss of carrier.
  323. */
  324. void netif_carrier_off(struct net_device *dev)
  325. {
  326. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  327. if (dev->reg_state == NETREG_UNINITIALIZED)
  328. return;
  329. atomic_inc(&dev->carrier_changes);
  330. linkwatch_fire_event(dev);
  331. }
  332. }
  333. EXPORT_SYMBOL(netif_carrier_off);
  334. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  335. under all circumstances. It is difficult to invent anything faster or
  336. cheaper.
  337. */
  338. static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  339. {
  340. kfree_skb(skb);
  341. return NET_XMIT_CN;
  342. }
  343. static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
  344. {
  345. return NULL;
  346. }
  347. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  348. .id = "noop",
  349. .priv_size = 0,
  350. .enqueue = noop_enqueue,
  351. .dequeue = noop_dequeue,
  352. .peek = noop_dequeue,
  353. .owner = THIS_MODULE,
  354. };
  355. static struct netdev_queue noop_netdev_queue = {
  356. .qdisc = &noop_qdisc,
  357. .qdisc_sleeping = &noop_qdisc,
  358. };
  359. struct Qdisc noop_qdisc = {
  360. .enqueue = noop_enqueue,
  361. .dequeue = noop_dequeue,
  362. .flags = TCQ_F_BUILTIN,
  363. .ops = &noop_qdisc_ops,
  364. .list = LIST_HEAD_INIT(noop_qdisc.list),
  365. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  366. .dev_queue = &noop_netdev_queue,
  367. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  368. };
  369. EXPORT_SYMBOL(noop_qdisc);
  370. static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt)
  371. {
  372. /* register_qdisc() assigns a default of noop_enqueue if unset,
  373. * but __dev_queue_xmit() treats noqueue only as such
  374. * if this is NULL - so clear it here. */
  375. qdisc->enqueue = NULL;
  376. return 0;
  377. }
  378. struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  379. .id = "noqueue",
  380. .priv_size = 0,
  381. .init = noqueue_init,
  382. .enqueue = noop_enqueue,
  383. .dequeue = noop_dequeue,
  384. .peek = noop_dequeue,
  385. .owner = THIS_MODULE,
  386. };
  387. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  388. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  389. };
  390. /* 3-band FIFO queue: old style, but should be a bit faster than
  391. generic prio+fifo combination.
  392. */
  393. #define PFIFO_FAST_BANDS 3
  394. /*
  395. * Private data for a pfifo_fast scheduler containing:
  396. * - queues for the three band
  397. * - bitmap indicating which of the bands contain skbs
  398. */
  399. struct pfifo_fast_priv {
  400. u32 bitmap;
  401. struct sk_buff_head q[PFIFO_FAST_BANDS];
  402. };
  403. /*
  404. * Convert a bitmap to the first band number where an skb is queued, where:
  405. * bitmap=0 means there are no skbs on any band.
  406. * bitmap=1 means there is an skb on band 0.
  407. * bitmap=7 means there are skbs on all 3 bands, etc.
  408. */
  409. static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
  410. static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
  411. int band)
  412. {
  413. return priv->q + band;
  414. }
  415. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  416. {
  417. if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
  418. int band = prio2band[skb->priority & TC_PRIO_MAX];
  419. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  420. struct sk_buff_head *list = band2list(priv, band);
  421. priv->bitmap |= (1 << band);
  422. qdisc->q.qlen++;
  423. return __qdisc_enqueue_tail(skb, qdisc, list);
  424. }
  425. return qdisc_drop(skb, qdisc);
  426. }
  427. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  428. {
  429. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  430. int band = bitmap2band[priv->bitmap];
  431. if (likely(band >= 0)) {
  432. struct sk_buff_head *list = band2list(priv, band);
  433. struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
  434. qdisc->q.qlen--;
  435. if (skb_queue_empty(list))
  436. priv->bitmap &= ~(1 << band);
  437. return skb;
  438. }
  439. return NULL;
  440. }
  441. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  442. {
  443. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  444. int band = bitmap2band[priv->bitmap];
  445. if (band >= 0) {
  446. struct sk_buff_head *list = band2list(priv, band);
  447. return skb_peek(list);
  448. }
  449. return NULL;
  450. }
  451. static void pfifo_fast_reset(struct Qdisc *qdisc)
  452. {
  453. int prio;
  454. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  455. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  456. __qdisc_reset_queue(qdisc, band2list(priv, prio));
  457. priv->bitmap = 0;
  458. qdisc->qstats.backlog = 0;
  459. qdisc->q.qlen = 0;
  460. }
  461. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  462. {
  463. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  464. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  465. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  466. goto nla_put_failure;
  467. return skb->len;
  468. nla_put_failure:
  469. return -1;
  470. }
  471. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  472. {
  473. int prio;
  474. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  475. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  476. __skb_queue_head_init(band2list(priv, prio));
  477. /* Can by-pass the queue discipline */
  478. qdisc->flags |= TCQ_F_CAN_BYPASS;
  479. return 0;
  480. }
  481. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  482. .id = "pfifo_fast",
  483. .priv_size = sizeof(struct pfifo_fast_priv),
  484. .enqueue = pfifo_fast_enqueue,
  485. .dequeue = pfifo_fast_dequeue,
  486. .peek = pfifo_fast_peek,
  487. .init = pfifo_fast_init,
  488. .reset = pfifo_fast_reset,
  489. .dump = pfifo_fast_dump,
  490. .owner = THIS_MODULE,
  491. };
  492. static struct lock_class_key qdisc_tx_busylock;
  493. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  494. const struct Qdisc_ops *ops)
  495. {
  496. void *p;
  497. struct Qdisc *sch;
  498. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  499. int err = -ENOBUFS;
  500. struct net_device *dev = dev_queue->dev;
  501. p = kzalloc_node(size, GFP_KERNEL,
  502. netdev_queue_numa_node_read(dev_queue));
  503. if (!p)
  504. goto errout;
  505. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  506. /* if we got non aligned memory, ask more and do alignment ourself */
  507. if (sch != p) {
  508. kfree(p);
  509. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  510. netdev_queue_numa_node_read(dev_queue));
  511. if (!p)
  512. goto errout;
  513. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  514. sch->padded = (char *) sch - (char *) p;
  515. }
  516. INIT_LIST_HEAD(&sch->list);
  517. skb_queue_head_init(&sch->q);
  518. spin_lock_init(&sch->busylock);
  519. lockdep_set_class(&sch->busylock,
  520. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  521. sch->ops = ops;
  522. sch->enqueue = ops->enqueue;
  523. sch->dequeue = ops->dequeue;
  524. sch->dev_queue = dev_queue;
  525. dev_hold(dev);
  526. atomic_set(&sch->refcnt, 1);
  527. return sch;
  528. errout:
  529. return ERR_PTR(err);
  530. }
  531. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  532. const struct Qdisc_ops *ops,
  533. unsigned int parentid)
  534. {
  535. struct Qdisc *sch;
  536. if (!try_module_get(ops->owner))
  537. goto errout;
  538. sch = qdisc_alloc(dev_queue, ops);
  539. if (IS_ERR(sch))
  540. goto errout;
  541. sch->parent = parentid;
  542. if (!ops->init || ops->init(sch, NULL) == 0)
  543. return sch;
  544. qdisc_destroy(sch);
  545. errout:
  546. return NULL;
  547. }
  548. EXPORT_SYMBOL(qdisc_create_dflt);
  549. /* Under qdisc_lock(qdisc) and BH! */
  550. void qdisc_reset(struct Qdisc *qdisc)
  551. {
  552. const struct Qdisc_ops *ops = qdisc->ops;
  553. if (ops->reset)
  554. ops->reset(qdisc);
  555. if (qdisc->gso_skb) {
  556. kfree_skb_list(qdisc->gso_skb);
  557. qdisc->gso_skb = NULL;
  558. qdisc->q.qlen = 0;
  559. }
  560. }
  561. EXPORT_SYMBOL(qdisc_reset);
  562. static void qdisc_rcu_free(struct rcu_head *head)
  563. {
  564. struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
  565. if (qdisc_is_percpu_stats(qdisc)) {
  566. free_percpu(qdisc->cpu_bstats);
  567. free_percpu(qdisc->cpu_qstats);
  568. }
  569. kfree((char *) qdisc - qdisc->padded);
  570. }
  571. void qdisc_destroy(struct Qdisc *qdisc)
  572. {
  573. const struct Qdisc_ops *ops = qdisc->ops;
  574. if (qdisc->flags & TCQ_F_BUILTIN ||
  575. !atomic_dec_and_test(&qdisc->refcnt))
  576. return;
  577. #ifdef CONFIG_NET_SCHED
  578. qdisc_list_del(qdisc);
  579. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  580. #endif
  581. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  582. if (ops->reset)
  583. ops->reset(qdisc);
  584. if (ops->destroy)
  585. ops->destroy(qdisc);
  586. module_put(ops->owner);
  587. dev_put(qdisc_dev(qdisc));
  588. kfree_skb_list(qdisc->gso_skb);
  589. /*
  590. * gen_estimator est_timer() might access qdisc->q.lock,
  591. * wait a RCU grace period before freeing qdisc.
  592. */
  593. call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
  594. }
  595. EXPORT_SYMBOL(qdisc_destroy);
  596. /* Attach toplevel qdisc to device queue. */
  597. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  598. struct Qdisc *qdisc)
  599. {
  600. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  601. spinlock_t *root_lock;
  602. root_lock = qdisc_lock(oqdisc);
  603. spin_lock_bh(root_lock);
  604. /* Prune old scheduler */
  605. if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
  606. qdisc_reset(oqdisc);
  607. /* ... and graft new one */
  608. if (qdisc == NULL)
  609. qdisc = &noop_qdisc;
  610. dev_queue->qdisc_sleeping = qdisc;
  611. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  612. spin_unlock_bh(root_lock);
  613. return oqdisc;
  614. }
  615. EXPORT_SYMBOL(dev_graft_qdisc);
  616. static void attach_one_default_qdisc(struct net_device *dev,
  617. struct netdev_queue *dev_queue,
  618. void *_unused)
  619. {
  620. struct Qdisc *qdisc;
  621. const struct Qdisc_ops *ops = default_qdisc_ops;
  622. if (dev->priv_flags & IFF_NO_QUEUE)
  623. ops = &noqueue_qdisc_ops;
  624. qdisc = qdisc_create_dflt(dev_queue, 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 | TCQ_F_NOPARENT;
  631. dev_queue->qdisc_sleeping = qdisc;
  632. }
  633. static void attach_default_qdiscs(struct net_device *dev)
  634. {
  635. struct netdev_queue *txq;
  636. struct Qdisc *qdisc;
  637. txq = netdev_get_tx_queue(dev, 0);
  638. if (!netif_is_multiqueue(dev) ||
  639. dev->priv_flags & IFF_NO_QUEUE) {
  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) {
  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);