sch_generic.c 33 KB

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