sch_generic.c 38 KB

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