sch_generic.c 32 KB

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