sch_generic.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 <linux/skb_array.h>
  29. #include <linux/if_macvlan.h>
  30. #include <net/sch_generic.h>
  31. #include <net/pkt_sched.h>
  32. #include <net/dst.h>
  33. #include <trace/events/qdisc.h>
  34. #include <net/xfrm.h>
  35. /* Qdisc to use by default */
  36. const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
  37. EXPORT_SYMBOL(default_qdisc_ops);
  38. /* Main transmission queue. */
  39. /* Modifications to data participating in scheduling must be protected with
  40. * qdisc_lock(qdisc) spinlock.
  41. *
  42. * The idea is the following:
  43. * - enqueue, dequeue are serialized via qdisc root lock
  44. * - ingress filtering is also serialized via qdisc root lock
  45. * - updates to tree and tree walking are only done under the rtnl mutex.
  46. */
  47. #define SKB_XOFF_MAGIC ((struct sk_buff *)1UL)
  48. static inline struct sk_buff *__skb_dequeue_bad_txq(struct Qdisc *q)
  49. {
  50. const struct netdev_queue *txq = q->dev_queue;
  51. spinlock_t *lock = NULL;
  52. struct sk_buff *skb;
  53. if (q->flags & TCQ_F_NOLOCK) {
  54. lock = qdisc_lock(q);
  55. spin_lock(lock);
  56. }
  57. skb = skb_peek(&q->skb_bad_txq);
  58. if (skb) {
  59. /* check the reason of requeuing without tx lock first */
  60. txq = skb_get_tx_queue(txq->dev, skb);
  61. if (!netif_xmit_frozen_or_stopped(txq)) {
  62. skb = __skb_dequeue(&q->skb_bad_txq);
  63. if (qdisc_is_percpu_stats(q)) {
  64. qdisc_qstats_cpu_backlog_dec(q, skb);
  65. qdisc_qstats_atomic_qlen_dec(q);
  66. } else {
  67. qdisc_qstats_backlog_dec(q, skb);
  68. q->q.qlen--;
  69. }
  70. } else {
  71. skb = SKB_XOFF_MAGIC;
  72. }
  73. }
  74. if (lock)
  75. spin_unlock(lock);
  76. return skb;
  77. }
  78. static inline struct sk_buff *qdisc_dequeue_skb_bad_txq(struct Qdisc *q)
  79. {
  80. struct sk_buff *skb = skb_peek(&q->skb_bad_txq);
  81. if (unlikely(skb))
  82. skb = __skb_dequeue_bad_txq(q);
  83. return skb;
  84. }
  85. static inline void qdisc_enqueue_skb_bad_txq(struct Qdisc *q,
  86. struct sk_buff *skb)
  87. {
  88. spinlock_t *lock = NULL;
  89. if (q->flags & TCQ_F_NOLOCK) {
  90. lock = qdisc_lock(q);
  91. spin_lock(lock);
  92. }
  93. __skb_queue_tail(&q->skb_bad_txq, skb);
  94. if (qdisc_is_percpu_stats(q)) {
  95. qdisc_qstats_cpu_backlog_inc(q, skb);
  96. qdisc_qstats_atomic_qlen_inc(q);
  97. } else {
  98. qdisc_qstats_backlog_inc(q, skb);
  99. q->q.qlen++;
  100. }
  101. if (lock)
  102. spin_unlock(lock);
  103. }
  104. static inline int __dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
  105. {
  106. while (skb) {
  107. struct sk_buff *next = skb->next;
  108. __skb_queue_tail(&q->gso_skb, skb);
  109. q->qstats.requeues++;
  110. qdisc_qstats_backlog_inc(q, skb);
  111. q->q.qlen++; /* it's still part of the queue */
  112. skb = next;
  113. }
  114. __netif_schedule(q);
  115. return 0;
  116. }
  117. static inline int dev_requeue_skb_locked(struct sk_buff *skb, struct Qdisc *q)
  118. {
  119. spinlock_t *lock = qdisc_lock(q);
  120. spin_lock(lock);
  121. while (skb) {
  122. struct sk_buff *next = skb->next;
  123. __skb_queue_tail(&q->gso_skb, skb);
  124. qdisc_qstats_cpu_requeues_inc(q);
  125. qdisc_qstats_cpu_backlog_inc(q, skb);
  126. qdisc_qstats_atomic_qlen_inc(q);
  127. skb = next;
  128. }
  129. spin_unlock(lock);
  130. __netif_schedule(q);
  131. return 0;
  132. }
  133. static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
  134. {
  135. if (q->flags & TCQ_F_NOLOCK)
  136. return dev_requeue_skb_locked(skb, q);
  137. else
  138. return __dev_requeue_skb(skb, q);
  139. }
  140. static void try_bulk_dequeue_skb(struct Qdisc *q,
  141. struct sk_buff *skb,
  142. const struct netdev_queue *txq,
  143. int *packets)
  144. {
  145. int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
  146. while (bytelimit > 0) {
  147. struct sk_buff *nskb = q->dequeue(q);
  148. if (!nskb)
  149. break;
  150. bytelimit -= nskb->len; /* covers GSO len */
  151. skb->next = nskb;
  152. skb = nskb;
  153. (*packets)++; /* GSO counts as one pkt */
  154. }
  155. skb->next = NULL;
  156. }
  157. /* This variant of try_bulk_dequeue_skb() makes sure
  158. * all skbs in the chain are for the same txq
  159. */
  160. static void try_bulk_dequeue_skb_slow(struct Qdisc *q,
  161. struct sk_buff *skb,
  162. int *packets)
  163. {
  164. int mapping = skb_get_queue_mapping(skb);
  165. struct sk_buff *nskb;
  166. int cnt = 0;
  167. do {
  168. nskb = q->dequeue(q);
  169. if (!nskb)
  170. break;
  171. if (unlikely(skb_get_queue_mapping(nskb) != mapping)) {
  172. qdisc_enqueue_skb_bad_txq(q, nskb);
  173. break;
  174. }
  175. skb->next = nskb;
  176. skb = nskb;
  177. } while (++cnt < 8);
  178. (*packets) += cnt;
  179. skb->next = NULL;
  180. }
  181. /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
  182. * A requeued skb (via q->gso_skb) can also be a SKB list.
  183. */
  184. static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
  185. int *packets)
  186. {
  187. const struct netdev_queue *txq = q->dev_queue;
  188. struct sk_buff *skb = NULL;
  189. *packets = 1;
  190. if (unlikely(!skb_queue_empty(&q->gso_skb))) {
  191. spinlock_t *lock = NULL;
  192. if (q->flags & TCQ_F_NOLOCK) {
  193. lock = qdisc_lock(q);
  194. spin_lock(lock);
  195. }
  196. skb = skb_peek(&q->gso_skb);
  197. /* skb may be null if another cpu pulls gso_skb off in between
  198. * empty check and lock.
  199. */
  200. if (!skb) {
  201. if (lock)
  202. spin_unlock(lock);
  203. goto validate;
  204. }
  205. /* skb in gso_skb were already validated */
  206. *validate = false;
  207. if (xfrm_offload(skb))
  208. *validate = true;
  209. /* check the reason of requeuing without tx lock first */
  210. txq = skb_get_tx_queue(txq->dev, skb);
  211. if (!netif_xmit_frozen_or_stopped(txq)) {
  212. skb = __skb_dequeue(&q->gso_skb);
  213. if (qdisc_is_percpu_stats(q)) {
  214. qdisc_qstats_cpu_backlog_dec(q, skb);
  215. qdisc_qstats_atomic_qlen_dec(q);
  216. } else {
  217. qdisc_qstats_backlog_dec(q, skb);
  218. q->q.qlen--;
  219. }
  220. } else {
  221. skb = NULL;
  222. }
  223. if (lock)
  224. spin_unlock(lock);
  225. goto trace;
  226. }
  227. validate:
  228. *validate = true;
  229. if ((q->flags & TCQ_F_ONETXQUEUE) &&
  230. netif_xmit_frozen_or_stopped(txq))
  231. return skb;
  232. skb = qdisc_dequeue_skb_bad_txq(q);
  233. if (unlikely(skb)) {
  234. if (skb == SKB_XOFF_MAGIC)
  235. return NULL;
  236. goto bulk;
  237. }
  238. skb = q->dequeue(q);
  239. if (skb) {
  240. bulk:
  241. if (qdisc_may_bulk(q))
  242. try_bulk_dequeue_skb(q, skb, txq, packets);
  243. else
  244. try_bulk_dequeue_skb_slow(q, skb, packets);
  245. }
  246. trace:
  247. trace_qdisc_dequeue(q, txq, *packets, skb);
  248. return skb;
  249. }
  250. /*
  251. * Transmit possibly several skbs, and handle the return status as
  252. * required. Owning running seqcount bit guarantees that
  253. * only one CPU can execute this function.
  254. *
  255. * Returns to the caller:
  256. * false - hardware queue frozen backoff
  257. * true - feel free to send more pkts
  258. */
  259. bool sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
  260. struct net_device *dev, struct netdev_queue *txq,
  261. spinlock_t *root_lock, bool validate)
  262. {
  263. int ret = NETDEV_TX_BUSY;
  264. bool again = false;
  265. /* And release qdisc */
  266. if (root_lock)
  267. spin_unlock(root_lock);
  268. /* Note that we validate skb (GSO, checksum, ...) outside of locks */
  269. if (validate)
  270. skb = validate_xmit_skb_list(skb, dev, &again);
  271. #ifdef CONFIG_XFRM_OFFLOAD
  272. if (unlikely(again)) {
  273. if (root_lock)
  274. spin_lock(root_lock);
  275. dev_requeue_skb(skb, q);
  276. return false;
  277. }
  278. #endif
  279. if (likely(skb)) {
  280. HARD_TX_LOCK(dev, txq, smp_processor_id());
  281. if (!netif_xmit_frozen_or_stopped(txq))
  282. skb = dev_hard_start_xmit(skb, dev, txq, &ret);
  283. HARD_TX_UNLOCK(dev, txq);
  284. } else {
  285. if (root_lock)
  286. spin_lock(root_lock);
  287. return true;
  288. }
  289. if (root_lock)
  290. spin_lock(root_lock);
  291. if (!dev_xmit_complete(ret)) {
  292. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  293. if (unlikely(ret != NETDEV_TX_BUSY))
  294. net_warn_ratelimited("BUG %s code %d qlen %d\n",
  295. dev->name, ret, q->q.qlen);
  296. dev_requeue_skb(skb, q);
  297. return false;
  298. }
  299. return true;
  300. }
  301. /*
  302. * NOTE: Called under qdisc_lock(q) with locally disabled BH.
  303. *
  304. * running seqcount guarantees only one CPU can process
  305. * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
  306. * this queue.
  307. *
  308. * netif_tx_lock serializes accesses to device driver.
  309. *
  310. * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
  311. * if one is grabbed, another must be free.
  312. *
  313. * Note, that this procedure can be called by a watchdog timer
  314. *
  315. * Returns to the caller:
  316. * 0 - queue is empty or throttled.
  317. * >0 - queue is not empty.
  318. *
  319. */
  320. static inline bool qdisc_restart(struct Qdisc *q, int *packets)
  321. {
  322. spinlock_t *root_lock = NULL;
  323. struct netdev_queue *txq;
  324. struct net_device *dev;
  325. struct sk_buff *skb;
  326. bool validate;
  327. /* Dequeue packet */
  328. skb = dequeue_skb(q, &validate, packets);
  329. if (unlikely(!skb))
  330. return false;
  331. if (!(q->flags & TCQ_F_NOLOCK))
  332. root_lock = qdisc_lock(q);
  333. dev = qdisc_dev(q);
  334. txq = skb_get_tx_queue(dev, skb);
  335. return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
  336. }
  337. void __qdisc_run(struct Qdisc *q)
  338. {
  339. int quota = dev_tx_weight;
  340. int packets;
  341. while (qdisc_restart(q, &packets)) {
  342. /*
  343. * Ordered by possible occurrence: Postpone processing if
  344. * 1. we've exceeded packet quota
  345. * 2. another process needs the CPU;
  346. */
  347. quota -= packets;
  348. if (quota <= 0 || need_resched()) {
  349. __netif_schedule(q);
  350. break;
  351. }
  352. }
  353. }
  354. unsigned long dev_trans_start(struct net_device *dev)
  355. {
  356. unsigned long val, res;
  357. unsigned int i;
  358. if (is_vlan_dev(dev))
  359. dev = vlan_dev_real_dev(dev);
  360. else if (netif_is_macvlan(dev))
  361. dev = macvlan_dev_real_dev(dev);
  362. res = netdev_get_tx_queue(dev, 0)->trans_start;
  363. for (i = 1; i < dev->num_tx_queues; i++) {
  364. val = netdev_get_tx_queue(dev, i)->trans_start;
  365. if (val && time_after(val, res))
  366. res = val;
  367. }
  368. return res;
  369. }
  370. EXPORT_SYMBOL(dev_trans_start);
  371. static void dev_watchdog(struct timer_list *t)
  372. {
  373. struct net_device *dev = from_timer(dev, t, watchdog_timer);
  374. netif_tx_lock(dev);
  375. if (!qdisc_tx_is_noop(dev)) {
  376. if (netif_device_present(dev) &&
  377. netif_running(dev) &&
  378. netif_carrier_ok(dev)) {
  379. int some_queue_timedout = 0;
  380. unsigned int i;
  381. unsigned long trans_start;
  382. for (i = 0; i < dev->num_tx_queues; i++) {
  383. struct netdev_queue *txq;
  384. txq = netdev_get_tx_queue(dev, i);
  385. trans_start = txq->trans_start;
  386. if (netif_xmit_stopped(txq) &&
  387. time_after(jiffies, (trans_start +
  388. dev->watchdog_timeo))) {
  389. some_queue_timedout = 1;
  390. txq->trans_timeout++;
  391. break;
  392. }
  393. }
  394. if (some_queue_timedout) {
  395. WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
  396. dev->name, netdev_drivername(dev), i);
  397. dev->netdev_ops->ndo_tx_timeout(dev);
  398. }
  399. if (!mod_timer(&dev->watchdog_timer,
  400. round_jiffies(jiffies +
  401. dev->watchdog_timeo)))
  402. dev_hold(dev);
  403. }
  404. }
  405. netif_tx_unlock(dev);
  406. dev_put(dev);
  407. }
  408. void __netdev_watchdog_up(struct net_device *dev)
  409. {
  410. if (dev->netdev_ops->ndo_tx_timeout) {
  411. if (dev->watchdog_timeo <= 0)
  412. dev->watchdog_timeo = 5*HZ;
  413. if (!mod_timer(&dev->watchdog_timer,
  414. round_jiffies(jiffies + dev->watchdog_timeo)))
  415. dev_hold(dev);
  416. }
  417. }
  418. EXPORT_SYMBOL_GPL(__netdev_watchdog_up);
  419. static void dev_watchdog_up(struct net_device *dev)
  420. {
  421. __netdev_watchdog_up(dev);
  422. }
  423. static void dev_watchdog_down(struct net_device *dev)
  424. {
  425. netif_tx_lock_bh(dev);
  426. if (del_timer(&dev->watchdog_timer))
  427. dev_put(dev);
  428. netif_tx_unlock_bh(dev);
  429. }
  430. /**
  431. * netif_carrier_on - set carrier
  432. * @dev: network device
  433. *
  434. * Device has detected that carrier.
  435. */
  436. void netif_carrier_on(struct net_device *dev)
  437. {
  438. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  439. if (dev->reg_state == NETREG_UNINITIALIZED)
  440. return;
  441. atomic_inc(&dev->carrier_up_count);
  442. linkwatch_fire_event(dev);
  443. if (netif_running(dev))
  444. __netdev_watchdog_up(dev);
  445. }
  446. }
  447. EXPORT_SYMBOL(netif_carrier_on);
  448. /**
  449. * netif_carrier_off - clear carrier
  450. * @dev: network device
  451. *
  452. * Device has detected loss of carrier.
  453. */
  454. void netif_carrier_off(struct net_device *dev)
  455. {
  456. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  457. if (dev->reg_state == NETREG_UNINITIALIZED)
  458. return;
  459. atomic_inc(&dev->carrier_down_count);
  460. linkwatch_fire_event(dev);
  461. }
  462. }
  463. EXPORT_SYMBOL(netif_carrier_off);
  464. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  465. under all circumstances. It is difficult to invent anything faster or
  466. cheaper.
  467. */
  468. static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
  469. struct sk_buff **to_free)
  470. {
  471. __qdisc_drop(skb, to_free);
  472. return NET_XMIT_CN;
  473. }
  474. static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
  475. {
  476. return NULL;
  477. }
  478. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  479. .id = "noop",
  480. .priv_size = 0,
  481. .enqueue = noop_enqueue,
  482. .dequeue = noop_dequeue,
  483. .peek = noop_dequeue,
  484. .owner = THIS_MODULE,
  485. };
  486. static struct netdev_queue noop_netdev_queue = {
  487. .qdisc = &noop_qdisc,
  488. .qdisc_sleeping = &noop_qdisc,
  489. };
  490. struct Qdisc noop_qdisc = {
  491. .enqueue = noop_enqueue,
  492. .dequeue = noop_dequeue,
  493. .flags = TCQ_F_BUILTIN,
  494. .ops = &noop_qdisc_ops,
  495. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  496. .dev_queue = &noop_netdev_queue,
  497. .running = SEQCNT_ZERO(noop_qdisc.running),
  498. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  499. .gso_skb = {
  500. .next = (struct sk_buff *)&noop_qdisc.gso_skb,
  501. .prev = (struct sk_buff *)&noop_qdisc.gso_skb,
  502. .qlen = 0,
  503. .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.gso_skb.lock),
  504. },
  505. .skb_bad_txq = {
  506. .next = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
  507. .prev = (struct sk_buff *)&noop_qdisc.skb_bad_txq,
  508. .qlen = 0,
  509. .lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.skb_bad_txq.lock),
  510. },
  511. };
  512. EXPORT_SYMBOL(noop_qdisc);
  513. static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
  514. struct netlink_ext_ack *extack)
  515. {
  516. /* register_qdisc() assigns a default of noop_enqueue if unset,
  517. * but __dev_queue_xmit() treats noqueue only as such
  518. * if this is NULL - so clear it here. */
  519. qdisc->enqueue = NULL;
  520. return 0;
  521. }
  522. struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  523. .id = "noqueue",
  524. .priv_size = 0,
  525. .init = noqueue_init,
  526. .enqueue = noop_enqueue,
  527. .dequeue = noop_dequeue,
  528. .peek = noop_dequeue,
  529. .owner = THIS_MODULE,
  530. };
  531. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  532. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  533. };
  534. /* 3-band FIFO queue: old style, but should be a bit faster than
  535. generic prio+fifo combination.
  536. */
  537. #define PFIFO_FAST_BANDS 3
  538. /*
  539. * Private data for a pfifo_fast scheduler containing:
  540. * - rings for priority bands
  541. */
  542. struct pfifo_fast_priv {
  543. struct skb_array q[PFIFO_FAST_BANDS];
  544. };
  545. static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
  546. int band)
  547. {
  548. return &priv->q[band];
  549. }
  550. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
  551. struct sk_buff **to_free)
  552. {
  553. int band = prio2band[skb->priority & TC_PRIO_MAX];
  554. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  555. struct skb_array *q = band2list(priv, band);
  556. unsigned int pkt_len = qdisc_pkt_len(skb);
  557. int err;
  558. err = skb_array_produce(q, skb);
  559. if (unlikely(err))
  560. return qdisc_drop_cpu(skb, qdisc, to_free);
  561. qdisc_qstats_atomic_qlen_inc(qdisc);
  562. /* Note: skb can not be used after skb_array_produce(),
  563. * so we better not use qdisc_qstats_cpu_backlog_inc()
  564. */
  565. this_cpu_add(qdisc->cpu_qstats->backlog, pkt_len);
  566. return NET_XMIT_SUCCESS;
  567. }
  568. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  569. {
  570. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  571. struct sk_buff *skb = NULL;
  572. int band;
  573. for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
  574. struct skb_array *q = band2list(priv, band);
  575. if (__skb_array_empty(q))
  576. continue;
  577. skb = __skb_array_consume(q);
  578. }
  579. if (likely(skb)) {
  580. qdisc_qstats_cpu_backlog_dec(qdisc, skb);
  581. qdisc_bstats_cpu_update(qdisc, skb);
  582. qdisc_qstats_atomic_qlen_dec(qdisc);
  583. }
  584. return skb;
  585. }
  586. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  587. {
  588. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  589. struct sk_buff *skb = NULL;
  590. int band;
  591. for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
  592. struct skb_array *q = band2list(priv, band);
  593. skb = __skb_array_peek(q);
  594. }
  595. return skb;
  596. }
  597. static void pfifo_fast_reset(struct Qdisc *qdisc)
  598. {
  599. int i, band;
  600. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  601. for (band = 0; band < PFIFO_FAST_BANDS; band++) {
  602. struct skb_array *q = band2list(priv, band);
  603. struct sk_buff *skb;
  604. /* NULL ring is possible if destroy path is due to a failed
  605. * skb_array_init() in pfifo_fast_init() case.
  606. */
  607. if (!q->ring.queue)
  608. continue;
  609. while ((skb = __skb_array_consume(q)) != NULL)
  610. kfree_skb(skb);
  611. }
  612. for_each_possible_cpu(i) {
  613. struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i);
  614. q->backlog = 0;
  615. }
  616. }
  617. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  618. {
  619. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  620. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  621. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  622. goto nla_put_failure;
  623. return skb->len;
  624. nla_put_failure:
  625. return -1;
  626. }
  627. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
  628. struct netlink_ext_ack *extack)
  629. {
  630. unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
  631. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  632. int prio;
  633. /* guard against zero length rings */
  634. if (!qlen)
  635. return -EINVAL;
  636. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  637. struct skb_array *q = band2list(priv, prio);
  638. int err;
  639. err = skb_array_init(q, qlen, GFP_KERNEL);
  640. if (err)
  641. return -ENOMEM;
  642. }
  643. /* Can by-pass the queue discipline */
  644. qdisc->flags |= TCQ_F_CAN_BYPASS;
  645. return 0;
  646. }
  647. static void pfifo_fast_destroy(struct Qdisc *sch)
  648. {
  649. struct pfifo_fast_priv *priv = qdisc_priv(sch);
  650. int prio;
  651. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  652. struct skb_array *q = band2list(priv, prio);
  653. /* NULL ring is possible if destroy path is due to a failed
  654. * skb_array_init() in pfifo_fast_init() case.
  655. */
  656. if (!q->ring.queue)
  657. continue;
  658. /* Destroy ring but no need to kfree_skb because a call to
  659. * pfifo_fast_reset() has already done that work.
  660. */
  661. ptr_ring_cleanup(&q->ring, NULL);
  662. }
  663. }
  664. static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
  665. unsigned int new_len)
  666. {
  667. struct pfifo_fast_priv *priv = qdisc_priv(sch);
  668. struct skb_array *bands[PFIFO_FAST_BANDS];
  669. int prio;
  670. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  671. struct skb_array *q = band2list(priv, prio);
  672. bands[prio] = q;
  673. }
  674. return skb_array_resize_multiple(bands, PFIFO_FAST_BANDS, new_len,
  675. GFP_KERNEL);
  676. }
  677. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  678. .id = "pfifo_fast",
  679. .priv_size = sizeof(struct pfifo_fast_priv),
  680. .enqueue = pfifo_fast_enqueue,
  681. .dequeue = pfifo_fast_dequeue,
  682. .peek = pfifo_fast_peek,
  683. .init = pfifo_fast_init,
  684. .destroy = pfifo_fast_destroy,
  685. .reset = pfifo_fast_reset,
  686. .dump = pfifo_fast_dump,
  687. .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
  688. .owner = THIS_MODULE,
  689. .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
  690. };
  691. EXPORT_SYMBOL(pfifo_fast_ops);
  692. static struct lock_class_key qdisc_tx_busylock;
  693. static struct lock_class_key qdisc_running_key;
  694. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  695. const struct Qdisc_ops *ops,
  696. struct netlink_ext_ack *extack)
  697. {
  698. void *p;
  699. struct Qdisc *sch;
  700. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  701. int err = -ENOBUFS;
  702. struct net_device *dev;
  703. if (!dev_queue) {
  704. NL_SET_ERR_MSG(extack, "No device queue given");
  705. err = -EINVAL;
  706. goto errout;
  707. }
  708. dev = dev_queue->dev;
  709. p = kzalloc_node(size, GFP_KERNEL,
  710. netdev_queue_numa_node_read(dev_queue));
  711. if (!p)
  712. goto errout;
  713. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  714. /* if we got non aligned memory, ask more and do alignment ourself */
  715. if (sch != p) {
  716. kfree(p);
  717. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  718. netdev_queue_numa_node_read(dev_queue));
  719. if (!p)
  720. goto errout;
  721. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  722. sch->padded = (char *) sch - (char *) p;
  723. }
  724. __skb_queue_head_init(&sch->gso_skb);
  725. __skb_queue_head_init(&sch->skb_bad_txq);
  726. qdisc_skb_head_init(&sch->q);
  727. spin_lock_init(&sch->q.lock);
  728. if (ops->static_flags & TCQ_F_CPUSTATS) {
  729. sch->cpu_bstats =
  730. netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  731. if (!sch->cpu_bstats)
  732. goto errout1;
  733. sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  734. if (!sch->cpu_qstats) {
  735. free_percpu(sch->cpu_bstats);
  736. goto errout1;
  737. }
  738. }
  739. spin_lock_init(&sch->busylock);
  740. lockdep_set_class(&sch->busylock,
  741. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  742. /* seqlock has the same scope of busylock, for NOLOCK qdisc */
  743. spin_lock_init(&sch->seqlock);
  744. lockdep_set_class(&sch->busylock,
  745. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  746. seqcount_init(&sch->running);
  747. lockdep_set_class(&sch->running,
  748. dev->qdisc_running_key ?: &qdisc_running_key);
  749. sch->ops = ops;
  750. sch->flags = ops->static_flags;
  751. sch->enqueue = ops->enqueue;
  752. sch->dequeue = ops->dequeue;
  753. sch->dev_queue = dev_queue;
  754. dev_hold(dev);
  755. refcount_set(&sch->refcnt, 1);
  756. return sch;
  757. errout1:
  758. kfree(p);
  759. errout:
  760. return ERR_PTR(err);
  761. }
  762. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  763. const struct Qdisc_ops *ops,
  764. unsigned int parentid,
  765. struct netlink_ext_ack *extack)
  766. {
  767. struct Qdisc *sch;
  768. if (!try_module_get(ops->owner)) {
  769. NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
  770. return NULL;
  771. }
  772. sch = qdisc_alloc(dev_queue, ops, extack);
  773. if (IS_ERR(sch)) {
  774. module_put(ops->owner);
  775. return NULL;
  776. }
  777. sch->parent = parentid;
  778. if (!ops->init || ops->init(sch, NULL, extack) == 0)
  779. return sch;
  780. qdisc_destroy(sch);
  781. return NULL;
  782. }
  783. EXPORT_SYMBOL(qdisc_create_dflt);
  784. /* Under qdisc_lock(qdisc) and BH! */
  785. void qdisc_reset(struct Qdisc *qdisc)
  786. {
  787. const struct Qdisc_ops *ops = qdisc->ops;
  788. struct sk_buff *skb, *tmp;
  789. if (ops->reset)
  790. ops->reset(qdisc);
  791. skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
  792. __skb_unlink(skb, &qdisc->gso_skb);
  793. kfree_skb_list(skb);
  794. }
  795. skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
  796. __skb_unlink(skb, &qdisc->skb_bad_txq);
  797. kfree_skb_list(skb);
  798. }
  799. qdisc->q.qlen = 0;
  800. qdisc->qstats.backlog = 0;
  801. }
  802. EXPORT_SYMBOL(qdisc_reset);
  803. void qdisc_free(struct Qdisc *qdisc)
  804. {
  805. if (qdisc_is_percpu_stats(qdisc)) {
  806. free_percpu(qdisc->cpu_bstats);
  807. free_percpu(qdisc->cpu_qstats);
  808. }
  809. kfree((char *) qdisc - qdisc->padded);
  810. }
  811. void qdisc_destroy(struct Qdisc *qdisc)
  812. {
  813. const struct Qdisc_ops *ops;
  814. struct sk_buff *skb, *tmp;
  815. if (!qdisc)
  816. return;
  817. ops = qdisc->ops;
  818. if (qdisc->flags & TCQ_F_BUILTIN ||
  819. !refcount_dec_and_test(&qdisc->refcnt))
  820. return;
  821. #ifdef CONFIG_NET_SCHED
  822. qdisc_hash_del(qdisc);
  823. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  824. #endif
  825. gen_kill_estimator(&qdisc->rate_est);
  826. if (ops->reset)
  827. ops->reset(qdisc);
  828. if (ops->destroy)
  829. ops->destroy(qdisc);
  830. module_put(ops->owner);
  831. dev_put(qdisc_dev(qdisc));
  832. skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
  833. __skb_unlink(skb, &qdisc->gso_skb);
  834. kfree_skb_list(skb);
  835. }
  836. skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
  837. __skb_unlink(skb, &qdisc->skb_bad_txq);
  838. kfree_skb_list(skb);
  839. }
  840. qdisc_free(qdisc);
  841. }
  842. EXPORT_SYMBOL(qdisc_destroy);
  843. /* Attach toplevel qdisc to device queue. */
  844. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  845. struct Qdisc *qdisc)
  846. {
  847. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  848. spinlock_t *root_lock;
  849. root_lock = qdisc_lock(oqdisc);
  850. spin_lock_bh(root_lock);
  851. /* ... and graft new one */
  852. if (qdisc == NULL)
  853. qdisc = &noop_qdisc;
  854. dev_queue->qdisc_sleeping = qdisc;
  855. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  856. spin_unlock_bh(root_lock);
  857. return oqdisc;
  858. }
  859. EXPORT_SYMBOL(dev_graft_qdisc);
  860. static void attach_one_default_qdisc(struct net_device *dev,
  861. struct netdev_queue *dev_queue,
  862. void *_unused)
  863. {
  864. struct Qdisc *qdisc;
  865. const struct Qdisc_ops *ops = default_qdisc_ops;
  866. if (dev->priv_flags & IFF_NO_QUEUE)
  867. ops = &noqueue_qdisc_ops;
  868. qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
  869. if (!qdisc) {
  870. netdev_info(dev, "activation failed\n");
  871. return;
  872. }
  873. if (!netif_is_multiqueue(dev))
  874. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  875. dev_queue->qdisc_sleeping = qdisc;
  876. }
  877. static void attach_default_qdiscs(struct net_device *dev)
  878. {
  879. struct netdev_queue *txq;
  880. struct Qdisc *qdisc;
  881. txq = netdev_get_tx_queue(dev, 0);
  882. if (!netif_is_multiqueue(dev) ||
  883. dev->priv_flags & IFF_NO_QUEUE) {
  884. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  885. dev->qdisc = txq->qdisc_sleeping;
  886. qdisc_refcount_inc(dev->qdisc);
  887. } else {
  888. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
  889. if (qdisc) {
  890. dev->qdisc = qdisc;
  891. qdisc->ops->attach(qdisc);
  892. }
  893. }
  894. #ifdef CONFIG_NET_SCHED
  895. if (dev->qdisc != &noop_qdisc)
  896. qdisc_hash_add(dev->qdisc, false);
  897. #endif
  898. }
  899. static void transition_one_qdisc(struct net_device *dev,
  900. struct netdev_queue *dev_queue,
  901. void *_need_watchdog)
  902. {
  903. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  904. int *need_watchdog_p = _need_watchdog;
  905. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  906. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  907. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  908. if (need_watchdog_p) {
  909. dev_queue->trans_start = 0;
  910. *need_watchdog_p = 1;
  911. }
  912. }
  913. void dev_activate(struct net_device *dev)
  914. {
  915. int need_watchdog;
  916. /* No queueing discipline is attached to device;
  917. * create default one for devices, which need queueing
  918. * and noqueue_qdisc for virtual interfaces
  919. */
  920. if (dev->qdisc == &noop_qdisc)
  921. attach_default_qdiscs(dev);
  922. if (!netif_carrier_ok(dev))
  923. /* Delay activation until next carrier-on event */
  924. return;
  925. need_watchdog = 0;
  926. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  927. if (dev_ingress_queue(dev))
  928. transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
  929. if (need_watchdog) {
  930. netif_trans_update(dev);
  931. dev_watchdog_up(dev);
  932. }
  933. }
  934. EXPORT_SYMBOL(dev_activate);
  935. static void dev_deactivate_queue(struct net_device *dev,
  936. struct netdev_queue *dev_queue,
  937. void *_qdisc_default)
  938. {
  939. struct Qdisc *qdisc = rtnl_dereference(dev_queue->qdisc);
  940. struct Qdisc *qdisc_default = _qdisc_default;
  941. if (qdisc) {
  942. if (!(qdisc->flags & TCQ_F_BUILTIN))
  943. set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
  944. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  945. }
  946. }
  947. static void dev_reset_queue(struct net_device *dev,
  948. struct netdev_queue *dev_queue,
  949. void *_unused)
  950. {
  951. struct Qdisc *qdisc;
  952. bool nolock;
  953. qdisc = dev_queue->qdisc_sleeping;
  954. if (!qdisc)
  955. return;
  956. nolock = qdisc->flags & TCQ_F_NOLOCK;
  957. if (nolock)
  958. spin_lock_bh(&qdisc->seqlock);
  959. spin_lock_bh(qdisc_lock(qdisc));
  960. qdisc_reset(qdisc);
  961. spin_unlock_bh(qdisc_lock(qdisc));
  962. if (nolock)
  963. spin_unlock_bh(&qdisc->seqlock);
  964. }
  965. static bool some_qdisc_is_busy(struct net_device *dev)
  966. {
  967. unsigned int i;
  968. for (i = 0; i < dev->num_tx_queues; i++) {
  969. struct netdev_queue *dev_queue;
  970. spinlock_t *root_lock;
  971. struct Qdisc *q;
  972. int val;
  973. dev_queue = netdev_get_tx_queue(dev, i);
  974. q = dev_queue->qdisc_sleeping;
  975. root_lock = qdisc_lock(q);
  976. spin_lock_bh(root_lock);
  977. val = (qdisc_is_running(q) ||
  978. test_bit(__QDISC_STATE_SCHED, &q->state));
  979. spin_unlock_bh(root_lock);
  980. if (val)
  981. return true;
  982. }
  983. return false;
  984. }
  985. static void dev_qdisc_reset(struct net_device *dev,
  986. struct netdev_queue *dev_queue,
  987. void *none)
  988. {
  989. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  990. if (qdisc)
  991. qdisc_reset(qdisc);
  992. }
  993. /**
  994. * dev_deactivate_many - deactivate transmissions on several devices
  995. * @head: list of devices to deactivate
  996. *
  997. * This function returns only when all outstanding transmissions
  998. * have completed, unless all devices are in dismantle phase.
  999. */
  1000. void dev_deactivate_many(struct list_head *head)
  1001. {
  1002. struct net_device *dev;
  1003. list_for_each_entry(dev, head, close_list) {
  1004. netdev_for_each_tx_queue(dev, dev_deactivate_queue,
  1005. &noop_qdisc);
  1006. if (dev_ingress_queue(dev))
  1007. dev_deactivate_queue(dev, dev_ingress_queue(dev),
  1008. &noop_qdisc);
  1009. dev_watchdog_down(dev);
  1010. }
  1011. /* Wait for outstanding qdisc-less dev_queue_xmit calls or
  1012. * outstanding qdisc enqueuing calls.
  1013. * This is avoided if all devices are in dismantle phase :
  1014. * Caller will call synchronize_net() for us
  1015. */
  1016. synchronize_net();
  1017. list_for_each_entry(dev, head, close_list) {
  1018. netdev_for_each_tx_queue(dev, dev_reset_queue, NULL);
  1019. if (dev_ingress_queue(dev))
  1020. dev_reset_queue(dev, dev_ingress_queue(dev), NULL);
  1021. }
  1022. /* Wait for outstanding qdisc_run calls. */
  1023. list_for_each_entry(dev, head, close_list) {
  1024. while (some_qdisc_is_busy(dev))
  1025. yield();
  1026. /* The new qdisc is assigned at this point so we can safely
  1027. * unwind stale skb lists and qdisc statistics
  1028. */
  1029. netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
  1030. if (dev_ingress_queue(dev))
  1031. dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
  1032. }
  1033. }
  1034. void dev_deactivate(struct net_device *dev)
  1035. {
  1036. LIST_HEAD(single);
  1037. list_add(&dev->close_list, &single);
  1038. dev_deactivate_many(&single);
  1039. list_del(&single);
  1040. }
  1041. EXPORT_SYMBOL(dev_deactivate);
  1042. static int qdisc_change_tx_queue_len(struct net_device *dev,
  1043. struct netdev_queue *dev_queue)
  1044. {
  1045. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  1046. const struct Qdisc_ops *ops = qdisc->ops;
  1047. if (ops->change_tx_queue_len)
  1048. return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
  1049. return 0;
  1050. }
  1051. int dev_qdisc_change_tx_queue_len(struct net_device *dev)
  1052. {
  1053. bool up = dev->flags & IFF_UP;
  1054. unsigned int i;
  1055. int ret = 0;
  1056. if (up)
  1057. dev_deactivate(dev);
  1058. for (i = 0; i < dev->num_tx_queues; i++) {
  1059. ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
  1060. /* TODO: revert changes on a partial failure */
  1061. if (ret)
  1062. break;
  1063. }
  1064. if (up)
  1065. dev_activate(dev);
  1066. return ret;
  1067. }
  1068. static void dev_init_scheduler_queue(struct net_device *dev,
  1069. struct netdev_queue *dev_queue,
  1070. void *_qdisc)
  1071. {
  1072. struct Qdisc *qdisc = _qdisc;
  1073. rcu_assign_pointer(dev_queue->qdisc, qdisc);
  1074. dev_queue->qdisc_sleeping = qdisc;
  1075. }
  1076. void dev_init_scheduler(struct net_device *dev)
  1077. {
  1078. dev->qdisc = &noop_qdisc;
  1079. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  1080. if (dev_ingress_queue(dev))
  1081. dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  1082. timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
  1083. }
  1084. static void shutdown_scheduler_queue(struct net_device *dev,
  1085. struct netdev_queue *dev_queue,
  1086. void *_qdisc_default)
  1087. {
  1088. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  1089. struct Qdisc *qdisc_default = _qdisc_default;
  1090. if (qdisc) {
  1091. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  1092. dev_queue->qdisc_sleeping = qdisc_default;
  1093. qdisc_destroy(qdisc);
  1094. }
  1095. }
  1096. void dev_shutdown(struct net_device *dev)
  1097. {
  1098. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  1099. if (dev_ingress_queue(dev))
  1100. shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  1101. qdisc_destroy(dev->qdisc);
  1102. dev->qdisc = &noop_qdisc;
  1103. WARN_ON(timer_pending(&dev->watchdog_timer));
  1104. }
  1105. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  1106. const struct tc_ratespec *conf,
  1107. u64 rate64)
  1108. {
  1109. memset(r, 0, sizeof(*r));
  1110. r->overhead = conf->overhead;
  1111. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  1112. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  1113. r->mult = 1;
  1114. /*
  1115. * The deal here is to replace a divide by a reciprocal one
  1116. * in fast path (a reciprocal divide is a multiply and a shift)
  1117. *
  1118. * Normal formula would be :
  1119. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  1120. *
  1121. * We compute mult/shift to use instead :
  1122. * time_in_ns = (len * mult) >> shift;
  1123. *
  1124. * We try to get the highest possible mult value for accuracy,
  1125. * but have to make sure no overflows will ever happen.
  1126. */
  1127. if (r->rate_bytes_ps > 0) {
  1128. u64 factor = NSEC_PER_SEC;
  1129. for (;;) {
  1130. r->mult = div64_u64(factor, r->rate_bytes_ps);
  1131. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  1132. break;
  1133. factor <<= 1;
  1134. r->shift++;
  1135. }
  1136. }
  1137. }
  1138. EXPORT_SYMBOL(psched_ratecfg_precompute);
  1139. static void mini_qdisc_rcu_func(struct rcu_head *head)
  1140. {
  1141. }
  1142. void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
  1143. struct tcf_proto *tp_head)
  1144. {
  1145. struct mini_Qdisc *miniq_old = rtnl_dereference(*miniqp->p_miniq);
  1146. struct mini_Qdisc *miniq;
  1147. if (!tp_head) {
  1148. RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
  1149. /* Wait for flying RCU callback before it is freed. */
  1150. rcu_barrier_bh();
  1151. return;
  1152. }
  1153. miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
  1154. &miniqp->miniq1 : &miniqp->miniq2;
  1155. /* We need to make sure that readers won't see the miniq
  1156. * we are about to modify. So wait until previous call_rcu_bh callback
  1157. * is done.
  1158. */
  1159. rcu_barrier_bh();
  1160. miniq->filter_list = tp_head;
  1161. rcu_assign_pointer(*miniqp->p_miniq, miniq);
  1162. if (miniq_old)
  1163. /* This is counterpart of the rcu barriers above. We need to
  1164. * block potential new user of miniq_old until all readers
  1165. * are not seeing it.
  1166. */
  1167. call_rcu_bh(&miniq_old->rcu, mini_qdisc_rcu_func);
  1168. }
  1169. EXPORT_SYMBOL(mini_qdisc_pair_swap);
  1170. void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
  1171. struct mini_Qdisc __rcu **p_miniq)
  1172. {
  1173. miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
  1174. miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
  1175. miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
  1176. miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
  1177. miniqp->p_miniq = p_miniq;
  1178. }
  1179. EXPORT_SYMBOL(mini_qdisc_pair_init);