sch_generic.c 24 KB

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