sch_generic.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. };
  499. EXPORT_SYMBOL(noop_qdisc);
  500. static int noqueue_init(struct Qdisc *qdisc, struct nlattr *opt,
  501. struct netlink_ext_ack *extack)
  502. {
  503. /* register_qdisc() assigns a default of noop_enqueue if unset,
  504. * but __dev_queue_xmit() treats noqueue only as such
  505. * if this is NULL - so clear it here. */
  506. qdisc->enqueue = NULL;
  507. return 0;
  508. }
  509. struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  510. .id = "noqueue",
  511. .priv_size = 0,
  512. .init = noqueue_init,
  513. .enqueue = noop_enqueue,
  514. .dequeue = noop_dequeue,
  515. .peek = noop_dequeue,
  516. .owner = THIS_MODULE,
  517. };
  518. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  519. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  520. };
  521. /* 3-band FIFO queue: old style, but should be a bit faster than
  522. generic prio+fifo combination.
  523. */
  524. #define PFIFO_FAST_BANDS 3
  525. /*
  526. * Private data for a pfifo_fast scheduler containing:
  527. * - rings for priority bands
  528. */
  529. struct pfifo_fast_priv {
  530. struct skb_array q[PFIFO_FAST_BANDS];
  531. };
  532. static inline struct skb_array *band2list(struct pfifo_fast_priv *priv,
  533. int band)
  534. {
  535. return &priv->q[band];
  536. }
  537. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc,
  538. struct sk_buff **to_free)
  539. {
  540. int band = prio2band[skb->priority & TC_PRIO_MAX];
  541. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  542. struct skb_array *q = band2list(priv, band);
  543. unsigned int pkt_len = qdisc_pkt_len(skb);
  544. int err;
  545. err = skb_array_produce(q, skb);
  546. if (unlikely(err))
  547. return qdisc_drop_cpu(skb, qdisc, to_free);
  548. qdisc_qstats_atomic_qlen_inc(qdisc);
  549. /* Note: skb can not be used after skb_array_produce(),
  550. * so we better not use qdisc_qstats_cpu_backlog_inc()
  551. */
  552. this_cpu_add(qdisc->cpu_qstats->backlog, pkt_len);
  553. return NET_XMIT_SUCCESS;
  554. }
  555. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  556. {
  557. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  558. struct sk_buff *skb = NULL;
  559. int band;
  560. for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
  561. struct skb_array *q = band2list(priv, band);
  562. if (__skb_array_empty(q))
  563. continue;
  564. skb = __skb_array_consume(q);
  565. }
  566. if (likely(skb)) {
  567. qdisc_qstats_cpu_backlog_dec(qdisc, skb);
  568. qdisc_bstats_cpu_update(qdisc, skb);
  569. qdisc_qstats_atomic_qlen_dec(qdisc);
  570. }
  571. return skb;
  572. }
  573. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  574. {
  575. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  576. struct sk_buff *skb = NULL;
  577. int band;
  578. for (band = 0; band < PFIFO_FAST_BANDS && !skb; band++) {
  579. struct skb_array *q = band2list(priv, band);
  580. skb = __skb_array_peek(q);
  581. }
  582. return skb;
  583. }
  584. static void pfifo_fast_reset(struct Qdisc *qdisc)
  585. {
  586. int i, band;
  587. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  588. for (band = 0; band < PFIFO_FAST_BANDS; band++) {
  589. struct skb_array *q = band2list(priv, band);
  590. struct sk_buff *skb;
  591. /* NULL ring is possible if destroy path is due to a failed
  592. * skb_array_init() in pfifo_fast_init() case.
  593. */
  594. if (!q->ring.queue)
  595. continue;
  596. while ((skb = __skb_array_consume(q)) != NULL)
  597. kfree_skb(skb);
  598. }
  599. for_each_possible_cpu(i) {
  600. struct gnet_stats_queue *q = per_cpu_ptr(qdisc->cpu_qstats, i);
  601. q->backlog = 0;
  602. }
  603. }
  604. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  605. {
  606. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  607. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  608. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  609. goto nla_put_failure;
  610. return skb->len;
  611. nla_put_failure:
  612. return -1;
  613. }
  614. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt,
  615. struct netlink_ext_ack *extack)
  616. {
  617. unsigned int qlen = qdisc_dev(qdisc)->tx_queue_len;
  618. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  619. int prio;
  620. /* guard against zero length rings */
  621. if (!qlen)
  622. return -EINVAL;
  623. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  624. struct skb_array *q = band2list(priv, prio);
  625. int err;
  626. err = skb_array_init(q, qlen, GFP_KERNEL);
  627. if (err)
  628. return -ENOMEM;
  629. }
  630. /* Can by-pass the queue discipline */
  631. qdisc->flags |= TCQ_F_CAN_BYPASS;
  632. return 0;
  633. }
  634. static void pfifo_fast_destroy(struct Qdisc *sch)
  635. {
  636. struct pfifo_fast_priv *priv = qdisc_priv(sch);
  637. int prio;
  638. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  639. struct skb_array *q = band2list(priv, prio);
  640. /* NULL ring is possible if destroy path is due to a failed
  641. * skb_array_init() in pfifo_fast_init() case.
  642. */
  643. if (!q->ring.queue)
  644. continue;
  645. /* Destroy ring but no need to kfree_skb because a call to
  646. * pfifo_fast_reset() has already done that work.
  647. */
  648. ptr_ring_cleanup(&q->ring, NULL);
  649. }
  650. }
  651. static int pfifo_fast_change_tx_queue_len(struct Qdisc *sch,
  652. unsigned int new_len)
  653. {
  654. struct pfifo_fast_priv *priv = qdisc_priv(sch);
  655. struct skb_array *bands[PFIFO_FAST_BANDS];
  656. int prio;
  657. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++) {
  658. struct skb_array *q = band2list(priv, prio);
  659. bands[prio] = q;
  660. }
  661. return skb_array_resize_multiple(bands, PFIFO_FAST_BANDS, new_len,
  662. GFP_KERNEL);
  663. }
  664. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  665. .id = "pfifo_fast",
  666. .priv_size = sizeof(struct pfifo_fast_priv),
  667. .enqueue = pfifo_fast_enqueue,
  668. .dequeue = pfifo_fast_dequeue,
  669. .peek = pfifo_fast_peek,
  670. .init = pfifo_fast_init,
  671. .destroy = pfifo_fast_destroy,
  672. .reset = pfifo_fast_reset,
  673. .dump = pfifo_fast_dump,
  674. .change_tx_queue_len = pfifo_fast_change_tx_queue_len,
  675. .owner = THIS_MODULE,
  676. .static_flags = TCQ_F_NOLOCK | TCQ_F_CPUSTATS,
  677. };
  678. EXPORT_SYMBOL(pfifo_fast_ops);
  679. static struct lock_class_key qdisc_tx_busylock;
  680. static struct lock_class_key qdisc_running_key;
  681. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  682. const struct Qdisc_ops *ops,
  683. struct netlink_ext_ack *extack)
  684. {
  685. void *p;
  686. struct Qdisc *sch;
  687. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  688. int err = -ENOBUFS;
  689. struct net_device *dev;
  690. if (!dev_queue) {
  691. NL_SET_ERR_MSG(extack, "No device queue given");
  692. err = -EINVAL;
  693. goto errout;
  694. }
  695. dev = dev_queue->dev;
  696. p = kzalloc_node(size, GFP_KERNEL,
  697. netdev_queue_numa_node_read(dev_queue));
  698. if (!p)
  699. goto errout;
  700. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  701. /* if we got non aligned memory, ask more and do alignment ourself */
  702. if (sch != p) {
  703. kfree(p);
  704. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  705. netdev_queue_numa_node_read(dev_queue));
  706. if (!p)
  707. goto errout;
  708. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  709. sch->padded = (char *) sch - (char *) p;
  710. }
  711. __skb_queue_head_init(&sch->gso_skb);
  712. __skb_queue_head_init(&sch->skb_bad_txq);
  713. qdisc_skb_head_init(&sch->q);
  714. spin_lock_init(&sch->q.lock);
  715. if (ops->static_flags & TCQ_F_CPUSTATS) {
  716. sch->cpu_bstats =
  717. netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  718. if (!sch->cpu_bstats)
  719. goto errout1;
  720. sch->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  721. if (!sch->cpu_qstats) {
  722. free_percpu(sch->cpu_bstats);
  723. goto errout1;
  724. }
  725. }
  726. spin_lock_init(&sch->busylock);
  727. lockdep_set_class(&sch->busylock,
  728. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  729. /* seqlock has the same scope of busylock, for NOLOCK qdisc */
  730. spin_lock_init(&sch->seqlock);
  731. lockdep_set_class(&sch->busylock,
  732. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  733. seqcount_init(&sch->running);
  734. lockdep_set_class(&sch->running,
  735. dev->qdisc_running_key ?: &qdisc_running_key);
  736. sch->ops = ops;
  737. sch->flags = ops->static_flags;
  738. sch->enqueue = ops->enqueue;
  739. sch->dequeue = ops->dequeue;
  740. sch->dev_queue = dev_queue;
  741. dev_hold(dev);
  742. refcount_set(&sch->refcnt, 1);
  743. return sch;
  744. errout1:
  745. kfree(p);
  746. errout:
  747. return ERR_PTR(err);
  748. }
  749. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  750. const struct Qdisc_ops *ops,
  751. unsigned int parentid,
  752. struct netlink_ext_ack *extack)
  753. {
  754. struct Qdisc *sch;
  755. if (!try_module_get(ops->owner)) {
  756. NL_SET_ERR_MSG(extack, "Failed to increase module reference counter");
  757. return NULL;
  758. }
  759. sch = qdisc_alloc(dev_queue, ops, extack);
  760. if (IS_ERR(sch)) {
  761. module_put(ops->owner);
  762. return NULL;
  763. }
  764. sch->parent = parentid;
  765. if (!ops->init || ops->init(sch, NULL, extack) == 0)
  766. return sch;
  767. qdisc_destroy(sch);
  768. return NULL;
  769. }
  770. EXPORT_SYMBOL(qdisc_create_dflt);
  771. /* Under qdisc_lock(qdisc) and BH! */
  772. void qdisc_reset(struct Qdisc *qdisc)
  773. {
  774. const struct Qdisc_ops *ops = qdisc->ops;
  775. struct sk_buff *skb, *tmp;
  776. if (ops->reset)
  777. ops->reset(qdisc);
  778. skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
  779. __skb_unlink(skb, &qdisc->gso_skb);
  780. kfree_skb_list(skb);
  781. }
  782. skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
  783. __skb_unlink(skb, &qdisc->skb_bad_txq);
  784. kfree_skb_list(skb);
  785. }
  786. qdisc->q.qlen = 0;
  787. qdisc->qstats.backlog = 0;
  788. }
  789. EXPORT_SYMBOL(qdisc_reset);
  790. void qdisc_free(struct Qdisc *qdisc)
  791. {
  792. if (qdisc_is_percpu_stats(qdisc)) {
  793. free_percpu(qdisc->cpu_bstats);
  794. free_percpu(qdisc->cpu_qstats);
  795. }
  796. kfree((char *) qdisc - qdisc->padded);
  797. }
  798. void qdisc_destroy(struct Qdisc *qdisc)
  799. {
  800. const struct Qdisc_ops *ops;
  801. struct sk_buff *skb, *tmp;
  802. if (!qdisc)
  803. return;
  804. ops = qdisc->ops;
  805. if (qdisc->flags & TCQ_F_BUILTIN ||
  806. !refcount_dec_and_test(&qdisc->refcnt))
  807. return;
  808. #ifdef CONFIG_NET_SCHED
  809. qdisc_hash_del(qdisc);
  810. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  811. #endif
  812. gen_kill_estimator(&qdisc->rate_est);
  813. if (ops->reset)
  814. ops->reset(qdisc);
  815. if (ops->destroy)
  816. ops->destroy(qdisc);
  817. module_put(ops->owner);
  818. dev_put(qdisc_dev(qdisc));
  819. skb_queue_walk_safe(&qdisc->gso_skb, skb, tmp) {
  820. __skb_unlink(skb, &qdisc->gso_skb);
  821. kfree_skb_list(skb);
  822. }
  823. skb_queue_walk_safe(&qdisc->skb_bad_txq, skb, tmp) {
  824. __skb_unlink(skb, &qdisc->skb_bad_txq);
  825. kfree_skb_list(skb);
  826. }
  827. qdisc_free(qdisc);
  828. }
  829. EXPORT_SYMBOL(qdisc_destroy);
  830. /* Attach toplevel qdisc to device queue. */
  831. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  832. struct Qdisc *qdisc)
  833. {
  834. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  835. spinlock_t *root_lock;
  836. root_lock = qdisc_lock(oqdisc);
  837. spin_lock_bh(root_lock);
  838. /* ... and graft new one */
  839. if (qdisc == NULL)
  840. qdisc = &noop_qdisc;
  841. dev_queue->qdisc_sleeping = qdisc;
  842. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  843. spin_unlock_bh(root_lock);
  844. return oqdisc;
  845. }
  846. EXPORT_SYMBOL(dev_graft_qdisc);
  847. static void attach_one_default_qdisc(struct net_device *dev,
  848. struct netdev_queue *dev_queue,
  849. void *_unused)
  850. {
  851. struct Qdisc *qdisc;
  852. const struct Qdisc_ops *ops = default_qdisc_ops;
  853. if (dev->priv_flags & IFF_NO_QUEUE)
  854. ops = &noqueue_qdisc_ops;
  855. qdisc = qdisc_create_dflt(dev_queue, ops, TC_H_ROOT, NULL);
  856. if (!qdisc) {
  857. netdev_info(dev, "activation failed\n");
  858. return;
  859. }
  860. if (!netif_is_multiqueue(dev))
  861. qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
  862. dev_queue->qdisc_sleeping = qdisc;
  863. }
  864. static void attach_default_qdiscs(struct net_device *dev)
  865. {
  866. struct netdev_queue *txq;
  867. struct Qdisc *qdisc;
  868. txq = netdev_get_tx_queue(dev, 0);
  869. if (!netif_is_multiqueue(dev) ||
  870. dev->priv_flags & IFF_NO_QUEUE) {
  871. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  872. dev->qdisc = txq->qdisc_sleeping;
  873. qdisc_refcount_inc(dev->qdisc);
  874. } else {
  875. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT, NULL);
  876. if (qdisc) {
  877. dev->qdisc = qdisc;
  878. qdisc->ops->attach(qdisc);
  879. }
  880. }
  881. #ifdef CONFIG_NET_SCHED
  882. if (dev->qdisc != &noop_qdisc)
  883. qdisc_hash_add(dev->qdisc, false);
  884. #endif
  885. }
  886. static void transition_one_qdisc(struct net_device *dev,
  887. struct netdev_queue *dev_queue,
  888. void *_need_watchdog)
  889. {
  890. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  891. int *need_watchdog_p = _need_watchdog;
  892. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  893. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  894. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  895. if (need_watchdog_p) {
  896. dev_queue->trans_start = 0;
  897. *need_watchdog_p = 1;
  898. }
  899. }
  900. void dev_activate(struct net_device *dev)
  901. {
  902. int need_watchdog;
  903. /* No queueing discipline is attached to device;
  904. * create default one for devices, which need queueing
  905. * and noqueue_qdisc for virtual interfaces
  906. */
  907. if (dev->qdisc == &noop_qdisc)
  908. attach_default_qdiscs(dev);
  909. if (!netif_carrier_ok(dev))
  910. /* Delay activation until next carrier-on event */
  911. return;
  912. need_watchdog = 0;
  913. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  914. if (dev_ingress_queue(dev))
  915. transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
  916. if (need_watchdog) {
  917. netif_trans_update(dev);
  918. dev_watchdog_up(dev);
  919. }
  920. }
  921. EXPORT_SYMBOL(dev_activate);
  922. static void dev_deactivate_queue(struct net_device *dev,
  923. struct netdev_queue *dev_queue,
  924. void *_qdisc_default)
  925. {
  926. struct Qdisc *qdisc_default = _qdisc_default;
  927. struct Qdisc *qdisc;
  928. qdisc = rtnl_dereference(dev_queue->qdisc);
  929. if (qdisc) {
  930. bool nolock = qdisc->flags & TCQ_F_NOLOCK;
  931. if (nolock)
  932. spin_lock_bh(&qdisc->seqlock);
  933. spin_lock_bh(qdisc_lock(qdisc));
  934. if (!(qdisc->flags & TCQ_F_BUILTIN))
  935. set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
  936. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  937. qdisc_reset(qdisc);
  938. spin_unlock_bh(qdisc_lock(qdisc));
  939. if (nolock)
  940. spin_unlock_bh(&qdisc->seqlock);
  941. }
  942. }
  943. static bool some_qdisc_is_busy(struct net_device *dev)
  944. {
  945. unsigned int i;
  946. for (i = 0; i < dev->num_tx_queues; i++) {
  947. struct netdev_queue *dev_queue;
  948. spinlock_t *root_lock;
  949. struct Qdisc *q;
  950. int val;
  951. dev_queue = netdev_get_tx_queue(dev, i);
  952. q = dev_queue->qdisc_sleeping;
  953. root_lock = qdisc_lock(q);
  954. spin_lock_bh(root_lock);
  955. val = (qdisc_is_running(q) ||
  956. test_bit(__QDISC_STATE_SCHED, &q->state));
  957. spin_unlock_bh(root_lock);
  958. if (val)
  959. return true;
  960. }
  961. return false;
  962. }
  963. static void dev_qdisc_reset(struct net_device *dev,
  964. struct netdev_queue *dev_queue,
  965. void *none)
  966. {
  967. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  968. if (qdisc)
  969. qdisc_reset(qdisc);
  970. }
  971. /**
  972. * dev_deactivate_many - deactivate transmissions on several devices
  973. * @head: list of devices to deactivate
  974. *
  975. * This function returns only when all outstanding transmissions
  976. * have completed, unless all devices are in dismantle phase.
  977. */
  978. void dev_deactivate_many(struct list_head *head)
  979. {
  980. struct net_device *dev;
  981. list_for_each_entry(dev, head, close_list) {
  982. netdev_for_each_tx_queue(dev, dev_deactivate_queue,
  983. &noop_qdisc);
  984. if (dev_ingress_queue(dev))
  985. dev_deactivate_queue(dev, dev_ingress_queue(dev),
  986. &noop_qdisc);
  987. dev_watchdog_down(dev);
  988. }
  989. /* Wait for outstanding qdisc-less dev_queue_xmit calls.
  990. * This is avoided if all devices are in dismantle phase :
  991. * Caller will call synchronize_net() for us
  992. */
  993. synchronize_net();
  994. /* Wait for outstanding qdisc_run calls. */
  995. list_for_each_entry(dev, head, close_list) {
  996. while (some_qdisc_is_busy(dev))
  997. yield();
  998. /* The new qdisc is assigned at this point so we can safely
  999. * unwind stale skb lists and qdisc statistics
  1000. */
  1001. netdev_for_each_tx_queue(dev, dev_qdisc_reset, NULL);
  1002. if (dev_ingress_queue(dev))
  1003. dev_qdisc_reset(dev, dev_ingress_queue(dev), NULL);
  1004. }
  1005. }
  1006. void dev_deactivate(struct net_device *dev)
  1007. {
  1008. LIST_HEAD(single);
  1009. list_add(&dev->close_list, &single);
  1010. dev_deactivate_many(&single);
  1011. list_del(&single);
  1012. }
  1013. EXPORT_SYMBOL(dev_deactivate);
  1014. static int qdisc_change_tx_queue_len(struct net_device *dev,
  1015. struct netdev_queue *dev_queue)
  1016. {
  1017. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  1018. const struct Qdisc_ops *ops = qdisc->ops;
  1019. if (ops->change_tx_queue_len)
  1020. return ops->change_tx_queue_len(qdisc, dev->tx_queue_len);
  1021. return 0;
  1022. }
  1023. int dev_qdisc_change_tx_queue_len(struct net_device *dev)
  1024. {
  1025. bool up = dev->flags & IFF_UP;
  1026. unsigned int i;
  1027. int ret = 0;
  1028. if (up)
  1029. dev_deactivate(dev);
  1030. for (i = 0; i < dev->num_tx_queues; i++) {
  1031. ret = qdisc_change_tx_queue_len(dev, &dev->_tx[i]);
  1032. /* TODO: revert changes on a partial failure */
  1033. if (ret)
  1034. break;
  1035. }
  1036. if (up)
  1037. dev_activate(dev);
  1038. return ret;
  1039. }
  1040. static void dev_init_scheduler_queue(struct net_device *dev,
  1041. struct netdev_queue *dev_queue,
  1042. void *_qdisc)
  1043. {
  1044. struct Qdisc *qdisc = _qdisc;
  1045. rcu_assign_pointer(dev_queue->qdisc, qdisc);
  1046. dev_queue->qdisc_sleeping = qdisc;
  1047. __skb_queue_head_init(&qdisc->gso_skb);
  1048. __skb_queue_head_init(&qdisc->skb_bad_txq);
  1049. }
  1050. void dev_init_scheduler(struct net_device *dev)
  1051. {
  1052. dev->qdisc = &noop_qdisc;
  1053. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  1054. if (dev_ingress_queue(dev))
  1055. dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  1056. timer_setup(&dev->watchdog_timer, dev_watchdog, 0);
  1057. }
  1058. static void shutdown_scheduler_queue(struct net_device *dev,
  1059. struct netdev_queue *dev_queue,
  1060. void *_qdisc_default)
  1061. {
  1062. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  1063. struct Qdisc *qdisc_default = _qdisc_default;
  1064. if (qdisc) {
  1065. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  1066. dev_queue->qdisc_sleeping = qdisc_default;
  1067. qdisc_destroy(qdisc);
  1068. }
  1069. }
  1070. void dev_shutdown(struct net_device *dev)
  1071. {
  1072. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  1073. if (dev_ingress_queue(dev))
  1074. shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  1075. qdisc_destroy(dev->qdisc);
  1076. dev->qdisc = &noop_qdisc;
  1077. WARN_ON(timer_pending(&dev->watchdog_timer));
  1078. }
  1079. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  1080. const struct tc_ratespec *conf,
  1081. u64 rate64)
  1082. {
  1083. memset(r, 0, sizeof(*r));
  1084. r->overhead = conf->overhead;
  1085. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  1086. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  1087. r->mult = 1;
  1088. /*
  1089. * The deal here is to replace a divide by a reciprocal one
  1090. * in fast path (a reciprocal divide is a multiply and a shift)
  1091. *
  1092. * Normal formula would be :
  1093. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  1094. *
  1095. * We compute mult/shift to use instead :
  1096. * time_in_ns = (len * mult) >> shift;
  1097. *
  1098. * We try to get the highest possible mult value for accuracy,
  1099. * but have to make sure no overflows will ever happen.
  1100. */
  1101. if (r->rate_bytes_ps > 0) {
  1102. u64 factor = NSEC_PER_SEC;
  1103. for (;;) {
  1104. r->mult = div64_u64(factor, r->rate_bytes_ps);
  1105. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  1106. break;
  1107. factor <<= 1;
  1108. r->shift++;
  1109. }
  1110. }
  1111. }
  1112. EXPORT_SYMBOL(psched_ratecfg_precompute);
  1113. static void mini_qdisc_rcu_func(struct rcu_head *head)
  1114. {
  1115. }
  1116. void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
  1117. struct tcf_proto *tp_head)
  1118. {
  1119. struct mini_Qdisc *miniq_old = rtnl_dereference(*miniqp->p_miniq);
  1120. struct mini_Qdisc *miniq;
  1121. if (!tp_head) {
  1122. RCU_INIT_POINTER(*miniqp->p_miniq, NULL);
  1123. /* Wait for flying RCU callback before it is freed. */
  1124. rcu_barrier_bh();
  1125. return;
  1126. }
  1127. miniq = !miniq_old || miniq_old == &miniqp->miniq2 ?
  1128. &miniqp->miniq1 : &miniqp->miniq2;
  1129. /* We need to make sure that readers won't see the miniq
  1130. * we are about to modify. So wait until previous call_rcu_bh callback
  1131. * is done.
  1132. */
  1133. rcu_barrier_bh();
  1134. miniq->filter_list = tp_head;
  1135. rcu_assign_pointer(*miniqp->p_miniq, miniq);
  1136. if (miniq_old)
  1137. /* This is counterpart of the rcu barriers above. We need to
  1138. * block potential new user of miniq_old until all readers
  1139. * are not seeing it.
  1140. */
  1141. call_rcu_bh(&miniq_old->rcu, mini_qdisc_rcu_func);
  1142. }
  1143. EXPORT_SYMBOL(mini_qdisc_pair_swap);
  1144. void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
  1145. struct mini_Qdisc __rcu **p_miniq)
  1146. {
  1147. miniqp->miniq1.cpu_bstats = qdisc->cpu_bstats;
  1148. miniqp->miniq1.cpu_qstats = qdisc->cpu_qstats;
  1149. miniqp->miniq2.cpu_bstats = qdisc->cpu_bstats;
  1150. miniqp->miniq2.cpu_qstats = qdisc->cpu_qstats;
  1151. miniqp->p_miniq = p_miniq;
  1152. }
  1153. EXPORT_SYMBOL(mini_qdisc_pair_init);