sch_generic.c 32 KB

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