sch_generic.c 39 KB

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