sch_generic.c 38 KB

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