sch_generic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 <net/sch_generic.h>
  29. #include <net/pkt_sched.h>
  30. #include <net/dst.h>
  31. #include <net/ip.h>
  32. #include <net/ipv6.h>
  33. /* Qdisc to use by default */
  34. const struct Qdisc_ops *default_qdisc_ops = &pfifo_fast_ops;
  35. EXPORT_SYMBOL(default_qdisc_ops);
  36. /* Main transmission queue. */
  37. /* Modifications to data participating in scheduling must be protected with
  38. * qdisc_lock(qdisc) spinlock.
  39. *
  40. * The idea is the following:
  41. * - enqueue, dequeue are serialized via qdisc root lock
  42. * - ingress filtering is also serialized via qdisc root lock
  43. * - updates to tree and tree walking are only done under the rtnl mutex.
  44. */
  45. static inline int dev_requeue_skb(struct sk_buff *skb, struct Qdisc *q)
  46. {
  47. q->gso_skb = skb;
  48. q->qstats.requeues++;
  49. q->q.qlen++; /* it's still part of the queue */
  50. __netif_schedule(q);
  51. return 0;
  52. }
  53. static void try_bulk_dequeue_skb(struct Qdisc *q,
  54. struct sk_buff *skb,
  55. const struct netdev_queue *txq,
  56. int *packets)
  57. {
  58. int bytelimit = qdisc_avail_bulklimit(txq) - skb->len;
  59. while (bytelimit > 0) {
  60. struct sk_buff *nskb = q->dequeue(q);
  61. if (!nskb)
  62. break;
  63. bytelimit -= nskb->len; /* covers GSO len */
  64. skb->next = nskb;
  65. skb = nskb;
  66. (*packets)++; /* GSO counts as one pkt */
  67. }
  68. skb->next = NULL;
  69. }
  70. /* Note that dequeue_skb can possibly return a SKB list (via skb->next).
  71. * A requeued skb (via q->gso_skb) can also be a SKB list.
  72. */
  73. static struct sk_buff *dequeue_skb(struct Qdisc *q, bool *validate,
  74. int *packets)
  75. {
  76. struct sk_buff *skb = q->gso_skb;
  77. const struct netdev_queue *txq = q->dev_queue;
  78. *packets = 1;
  79. *validate = true;
  80. if (unlikely(skb)) {
  81. /* check the reason of requeuing without tx lock first */
  82. txq = skb_get_tx_queue(txq->dev, skb);
  83. if (!netif_xmit_frozen_or_stopped(txq)) {
  84. q->gso_skb = NULL;
  85. q->q.qlen--;
  86. } else
  87. skb = NULL;
  88. /* skb in gso_skb were already validated */
  89. *validate = false;
  90. } else {
  91. if (!(q->flags & TCQ_F_ONETXQUEUE) ||
  92. !netif_xmit_frozen_or_stopped(txq)) {
  93. skb = q->dequeue(q);
  94. if (skb && qdisc_may_bulk(q))
  95. try_bulk_dequeue_skb(q, skb, txq, packets);
  96. }
  97. }
  98. return skb;
  99. }
  100. static inline int handle_dev_cpu_collision(struct sk_buff *skb,
  101. struct netdev_queue *dev_queue,
  102. struct Qdisc *q)
  103. {
  104. int ret;
  105. if (unlikely(dev_queue->xmit_lock_owner == smp_processor_id())) {
  106. /*
  107. * Same CPU holding the lock. It may be a transient
  108. * configuration error, when hard_start_xmit() recurses. We
  109. * detect it by checking xmit owner and drop the packet when
  110. * deadloop is detected. Return OK to try the next skb.
  111. */
  112. kfree_skb_list(skb);
  113. net_warn_ratelimited("Dead loop on netdevice %s, fix it urgently!\n",
  114. dev_queue->dev->name);
  115. ret = qdisc_qlen(q);
  116. } else {
  117. /*
  118. * Another cpu is holding lock, requeue & delay xmits for
  119. * some time.
  120. */
  121. __this_cpu_inc(softnet_data.cpu_collision);
  122. ret = dev_requeue_skb(skb, q);
  123. }
  124. return ret;
  125. }
  126. /*
  127. * Transmit possibly several skbs, and handle the return status as
  128. * required. Holding the __QDISC___STATE_RUNNING bit guarantees that
  129. * only one CPU can execute this function.
  130. *
  131. * Returns to the caller:
  132. * 0 - queue is empty or throttled.
  133. * >0 - queue is not empty.
  134. */
  135. int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
  136. struct net_device *dev, struct netdev_queue *txq,
  137. spinlock_t *root_lock, bool validate)
  138. {
  139. int ret = NETDEV_TX_BUSY;
  140. /* And release qdisc */
  141. spin_unlock(root_lock);
  142. /* Note that we validate skb (GSO, checksum, ...) outside of locks */
  143. if (validate)
  144. skb = validate_xmit_skb_list(skb, dev);
  145. if (skb) {
  146. HARD_TX_LOCK(dev, txq, smp_processor_id());
  147. if (!netif_xmit_frozen_or_stopped(txq))
  148. skb = dev_hard_start_xmit(skb, dev, txq, &ret);
  149. HARD_TX_UNLOCK(dev, txq);
  150. }
  151. #ifdef CONFIG_MTK_NET_LOGGING
  152. if (ret != NETDEV_TX_OK) {
  153. if (qdisc_qlen(q) < 16) {
  154. if (4 == (qdisc_qlen(q)) % 16)
  155. pr_debug("[mtk_net][sched]dev_hard_start_xmit ret = %d(%s), txq state = %lu\n",
  156. ret, dev->name, txq->state);
  157. } else {
  158. if (64 == (qdisc_qlen(q)) % 128)
  159. pr_debug("[mtk_net][sched]warning: dev_hard_start_xmit ret = %d(%s), txq state = %lu\n",
  160. ret, dev->name, txq->state);
  161. }
  162. }
  163. #endif
  164. spin_lock(root_lock);
  165. if (dev_xmit_complete(ret)) {
  166. /* Driver sent out skb successfully or skb was consumed */
  167. ret = qdisc_qlen(q);
  168. } else if (ret == NETDEV_TX_LOCKED) {
  169. /* Driver try lock failed */
  170. ret = handle_dev_cpu_collision(skb, txq, q);
  171. } else {
  172. /* Driver returned NETDEV_TX_BUSY - requeue skb */
  173. if (unlikely(ret != NETDEV_TX_BUSY))
  174. net_warn_ratelimited("BUG %s code %d qlen %d\n",
  175. dev->name, ret, q->q.qlen);
  176. ret = dev_requeue_skb(skb, q);
  177. }
  178. if (ret && netif_xmit_frozen_or_stopped(txq))
  179. ret = 0;
  180. return ret;
  181. }
  182. /*
  183. * NOTE: Called under qdisc_lock(q) with locally disabled BH.
  184. *
  185. * __QDISC___STATE_RUNNING guarantees only one CPU can process
  186. * this qdisc at a time. qdisc_lock(q) serializes queue accesses for
  187. * this queue.
  188. *
  189. * netif_tx_lock serializes accesses to device driver.
  190. *
  191. * qdisc_lock(q) and netif_tx_lock are mutually exclusive,
  192. * if one is grabbed, another must be free.
  193. *
  194. * Note, that this procedure can be called by a watchdog timer
  195. *
  196. * Returns to the caller:
  197. * 0 - queue is empty or throttled.
  198. * >0 - queue is not empty.
  199. *
  200. */
  201. static inline int qdisc_restart(struct Qdisc *q, int *packets)
  202. {
  203. struct netdev_queue *txq;
  204. struct net_device *dev;
  205. spinlock_t *root_lock;
  206. struct sk_buff *skb;
  207. bool validate;
  208. /* Dequeue packet */
  209. skb = dequeue_skb(q, &validate, packets);
  210. if (unlikely(!skb))
  211. return 0;
  212. root_lock = qdisc_lock(q);
  213. dev = qdisc_dev(q);
  214. txq = skb_get_tx_queue(dev, skb);
  215. return sch_direct_xmit(skb, q, dev, txq, root_lock, validate);
  216. }
  217. void __qdisc_run(struct Qdisc *q)
  218. {
  219. int quota = weight_p;
  220. int packets;
  221. while (qdisc_restart(q, &packets)) {
  222. /*
  223. * Ordered by possible occurrence: Postpone processing if
  224. * 1. we've exceeded packet quota
  225. * 2. another process needs the CPU;
  226. */
  227. quota -= packets;
  228. if (quota <= 0 || need_resched()) {
  229. __netif_schedule(q);
  230. break;
  231. }
  232. }
  233. qdisc_run_end(q);
  234. }
  235. unsigned long dev_trans_start(struct net_device *dev)
  236. {
  237. unsigned long val, res;
  238. unsigned int i;
  239. if (is_vlan_dev(dev))
  240. dev = vlan_dev_real_dev(dev);
  241. res = dev->trans_start;
  242. for (i = 0; i < dev->num_tx_queues; i++) {
  243. val = netdev_get_tx_queue(dev, i)->trans_start;
  244. if (val && time_after(val, res))
  245. res = val;
  246. }
  247. dev->trans_start = res;
  248. return res;
  249. }
  250. EXPORT_SYMBOL(dev_trans_start);
  251. static void dev_watchdog(unsigned long arg)
  252. {
  253. struct net_device *dev = (struct net_device *)arg;
  254. netif_tx_lock(dev);
  255. if (!qdisc_tx_is_noop(dev)) {
  256. if (netif_device_present(dev) &&
  257. netif_running(dev) &&
  258. netif_carrier_ok(dev)) {
  259. int some_queue_timedout = 0;
  260. unsigned int i;
  261. unsigned long trans_start;
  262. for (i = 0; i < dev->num_tx_queues; i++) {
  263. struct netdev_queue *txq;
  264. txq = netdev_get_tx_queue(dev, i);
  265. /*
  266. * old device drivers set dev->trans_start
  267. */
  268. trans_start = txq->trans_start ? : dev->trans_start;
  269. if (netif_xmit_stopped(txq) &&
  270. time_after(jiffies, (trans_start +
  271. dev->watchdog_timeo))) {
  272. some_queue_timedout = 1;
  273. txq->trans_timeout++;
  274. break;
  275. }
  276. }
  277. if (some_queue_timedout) {
  278. WARN_ONCE(1, KERN_INFO "NETDEV WATCHDOG: %s (%s): transmit queue %u timed out\n",
  279. dev->name, netdev_drivername(dev), i);
  280. dev->netdev_ops->ndo_tx_timeout(dev);
  281. }
  282. if (!mod_timer(&dev->watchdog_timer,
  283. round_jiffies(jiffies +
  284. dev->watchdog_timeo)))
  285. dev_hold(dev);
  286. }
  287. }
  288. netif_tx_unlock(dev);
  289. dev_put(dev);
  290. }
  291. void __netdev_watchdog_up(struct net_device *dev)
  292. {
  293. if (dev->netdev_ops->ndo_tx_timeout) {
  294. if (dev->watchdog_timeo <= 0)
  295. dev->watchdog_timeo = 5*HZ;
  296. if (!mod_timer(&dev->watchdog_timer,
  297. round_jiffies(jiffies + dev->watchdog_timeo)))
  298. dev_hold(dev);
  299. }
  300. }
  301. static void dev_watchdog_up(struct net_device *dev)
  302. {
  303. __netdev_watchdog_up(dev);
  304. }
  305. static void dev_watchdog_down(struct net_device *dev)
  306. {
  307. netif_tx_lock_bh(dev);
  308. if (del_timer(&dev->watchdog_timer))
  309. dev_put(dev);
  310. netif_tx_unlock_bh(dev);
  311. }
  312. /**
  313. * netif_carrier_on - set carrier
  314. * @dev: network device
  315. *
  316. * Device has detected that carrier.
  317. */
  318. void netif_carrier_on(struct net_device *dev)
  319. {
  320. if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  321. if (dev->reg_state == NETREG_UNINITIALIZED)
  322. return;
  323. atomic_inc(&dev->carrier_changes);
  324. linkwatch_fire_event(dev);
  325. if (netif_running(dev))
  326. __netdev_watchdog_up(dev);
  327. }
  328. }
  329. EXPORT_SYMBOL(netif_carrier_on);
  330. /**
  331. * netif_carrier_off - clear carrier
  332. * @dev: network device
  333. *
  334. * Device has detected loss of carrier.
  335. */
  336. void netif_carrier_off(struct net_device *dev)
  337. {
  338. if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
  339. if (dev->reg_state == NETREG_UNINITIALIZED)
  340. return;
  341. atomic_inc(&dev->carrier_changes);
  342. linkwatch_fire_event(dev);
  343. }
  344. }
  345. EXPORT_SYMBOL(netif_carrier_off);
  346. /* "NOOP" scheduler: the best scheduler, recommended for all interfaces
  347. under all circumstances. It is difficult to invent anything faster or
  348. cheaper.
  349. */
  350. static int noop_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  351. {
  352. kfree_skb(skb);
  353. return NET_XMIT_CN;
  354. }
  355. static struct sk_buff *noop_dequeue(struct Qdisc *qdisc)
  356. {
  357. return NULL;
  358. }
  359. struct Qdisc_ops noop_qdisc_ops __read_mostly = {
  360. .id = "noop",
  361. .priv_size = 0,
  362. .enqueue = noop_enqueue,
  363. .dequeue = noop_dequeue,
  364. .peek = noop_dequeue,
  365. .owner = THIS_MODULE,
  366. };
  367. static struct netdev_queue noop_netdev_queue = {
  368. .qdisc = &noop_qdisc,
  369. .qdisc_sleeping = &noop_qdisc,
  370. };
  371. struct Qdisc noop_qdisc = {
  372. .enqueue = noop_enqueue,
  373. .dequeue = noop_dequeue,
  374. .flags = TCQ_F_BUILTIN,
  375. .ops = &noop_qdisc_ops,
  376. .list = LIST_HEAD_INIT(noop_qdisc.list),
  377. .q.lock = __SPIN_LOCK_UNLOCKED(noop_qdisc.q.lock),
  378. .dev_queue = &noop_netdev_queue,
  379. .busylock = __SPIN_LOCK_UNLOCKED(noop_qdisc.busylock),
  380. };
  381. EXPORT_SYMBOL(noop_qdisc);
  382. static struct Qdisc_ops noqueue_qdisc_ops __read_mostly = {
  383. .id = "noqueue",
  384. .priv_size = 0,
  385. .enqueue = noop_enqueue,
  386. .dequeue = noop_dequeue,
  387. .peek = noop_dequeue,
  388. .owner = THIS_MODULE,
  389. };
  390. static struct Qdisc noqueue_qdisc;
  391. static struct netdev_queue noqueue_netdev_queue = {
  392. .qdisc = &noqueue_qdisc,
  393. .qdisc_sleeping = &noqueue_qdisc,
  394. };
  395. static struct Qdisc noqueue_qdisc = {
  396. .enqueue = NULL,
  397. .dequeue = noop_dequeue,
  398. .flags = TCQ_F_BUILTIN,
  399. .ops = &noqueue_qdisc_ops,
  400. .list = LIST_HEAD_INIT(noqueue_qdisc.list),
  401. .q.lock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.q.lock),
  402. .dev_queue = &noqueue_netdev_queue,
  403. .busylock = __SPIN_LOCK_UNLOCKED(noqueue_qdisc.busylock),
  404. };
  405. static const u8 prio2band[TC_PRIO_MAX + 1] = {
  406. 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
  407. };
  408. /* 3-band FIFO queue: old style, but should be a bit faster than
  409. generic prio+fifo combination.
  410. */
  411. #define PFIFO_FAST_BANDS 3
  412. /*
  413. * Private data for a pfifo_fast scheduler containing:
  414. * - queues for the three band
  415. * - bitmap indicating which of the bands contain skbs
  416. */
  417. struct pfifo_fast_priv {
  418. u32 bitmap;
  419. struct sk_buff_head q[PFIFO_FAST_BANDS];
  420. };
  421. /*
  422. * Convert a bitmap to the first band number where an skb is queued, where:
  423. * bitmap=0 means there are no skbs on any band.
  424. * bitmap=1 means there is an skb on band 0.
  425. * bitmap=7 means there are skbs on all 3 bands, etc.
  426. */
  427. static const int bitmap2band[] = {-1, 0, 1, 0, 2, 0, 1, 0};
  428. static inline struct sk_buff_head *band2list(struct pfifo_fast_priv *priv,
  429. int band)
  430. {
  431. return priv->q + band;
  432. }
  433. static int pfifo_fast_enqueue(struct sk_buff *skb, struct Qdisc *qdisc)
  434. {
  435. if (skb_queue_len(&qdisc->q) < qdisc_dev(qdisc)->tx_queue_len) {
  436. int band = prio2band[skb->priority & TC_PRIO_MAX];
  437. struct pfifo_fast_priv *priv;
  438. struct sk_buff_head *list;
  439. /*mtk_net_change*/
  440. if (skb->protocol == htons(ETH_P_IP)) {
  441. if (skb->len <= 52 && (ip_hdr(skb)->protocol) == IPPROTO_TCP)
  442. band = 0;
  443. }
  444. if (skb->protocol == htons(ETH_P_IPV6)) {
  445. if (skb->len <= 128) {
  446. struct tcphdr *tcph;
  447. __be16 frag_off;
  448. struct ipv6hdr *iph = ipv6_hdr(skb);
  449. u8 nexthdr = iph->nexthdr;
  450. u32 total_len = sizeof(struct ipv6hdr) + ntohs(iph->payload_len);
  451. u32 l4_off = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, &frag_off);
  452. tcph = (struct tcphdr *)(skb_network_header(skb) + l4_off);
  453. if (nexthdr == IPPROTO_TCP && !tcph->syn && !tcph->fin && !tcph->rst &&
  454. ((total_len - l4_off) == (tcph->doff << 2))) {
  455. band = 0;
  456. }
  457. }
  458. }
  459. priv = qdisc_priv(qdisc);
  460. list = band2list(priv, band);
  461. priv->bitmap |= (1 << band);
  462. qdisc->q.qlen++;
  463. return __qdisc_enqueue_tail(skb, qdisc, list);
  464. }
  465. return qdisc_drop(skb, qdisc);
  466. }
  467. static struct sk_buff *pfifo_fast_dequeue(struct Qdisc *qdisc)
  468. {
  469. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  470. int band = bitmap2band[priv->bitmap];
  471. if (likely(band >= 0)) {
  472. struct sk_buff_head *list = band2list(priv, band);
  473. struct sk_buff *skb = __qdisc_dequeue_head(qdisc, list);
  474. qdisc->q.qlen--;
  475. if (skb_queue_empty(list))
  476. priv->bitmap &= ~(1 << band);
  477. return skb;
  478. }
  479. return NULL;
  480. }
  481. static struct sk_buff *pfifo_fast_peek(struct Qdisc *qdisc)
  482. {
  483. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  484. int band = bitmap2band[priv->bitmap];
  485. if (band >= 0) {
  486. struct sk_buff_head *list = band2list(priv, band);
  487. return skb_peek(list);
  488. }
  489. return NULL;
  490. }
  491. static void pfifo_fast_reset(struct Qdisc *qdisc)
  492. {
  493. int prio;
  494. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  495. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  496. __qdisc_reset_queue(qdisc, band2list(priv, prio));
  497. priv->bitmap = 0;
  498. qdisc->qstats.backlog = 0;
  499. qdisc->q.qlen = 0;
  500. }
  501. static int pfifo_fast_dump(struct Qdisc *qdisc, struct sk_buff *skb)
  502. {
  503. struct tc_prio_qopt opt = { .bands = PFIFO_FAST_BANDS };
  504. memcpy(&opt.priomap, prio2band, TC_PRIO_MAX + 1);
  505. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  506. goto nla_put_failure;
  507. return skb->len;
  508. nla_put_failure:
  509. return -1;
  510. }
  511. static int pfifo_fast_init(struct Qdisc *qdisc, struct nlattr *opt)
  512. {
  513. int prio;
  514. struct pfifo_fast_priv *priv = qdisc_priv(qdisc);
  515. for (prio = 0; prio < PFIFO_FAST_BANDS; prio++)
  516. __skb_queue_head_init(band2list(priv, prio));
  517. /* Can by-pass the queue discipline */
  518. qdisc->flags |= TCQ_F_CAN_BYPASS;
  519. return 0;
  520. }
  521. struct Qdisc_ops pfifo_fast_ops __read_mostly = {
  522. .id = "pfifo_fast",
  523. .priv_size = sizeof(struct pfifo_fast_priv),
  524. .enqueue = pfifo_fast_enqueue,
  525. .dequeue = pfifo_fast_dequeue,
  526. .peek = pfifo_fast_peek,
  527. .init = pfifo_fast_init,
  528. .reset = pfifo_fast_reset,
  529. .dump = pfifo_fast_dump,
  530. .owner = THIS_MODULE,
  531. };
  532. static struct lock_class_key qdisc_tx_busylock;
  533. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  534. const struct Qdisc_ops *ops)
  535. {
  536. void *p;
  537. struct Qdisc *sch;
  538. unsigned int size = QDISC_ALIGN(sizeof(*sch)) + ops->priv_size;
  539. int err = -ENOBUFS;
  540. struct net_device *dev = dev_queue->dev;
  541. p = kzalloc_node(size, GFP_KERNEL,
  542. netdev_queue_numa_node_read(dev_queue));
  543. if (!p)
  544. goto errout;
  545. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  546. /* if we got non aligned memory, ask more and do alignment ourself */
  547. if (sch != p) {
  548. kfree(p);
  549. p = kzalloc_node(size + QDISC_ALIGNTO - 1, GFP_KERNEL,
  550. netdev_queue_numa_node_read(dev_queue));
  551. if (!p)
  552. goto errout;
  553. sch = (struct Qdisc *) QDISC_ALIGN((unsigned long) p);
  554. sch->padded = (char *) sch - (char *) p;
  555. }
  556. INIT_LIST_HEAD(&sch->list);
  557. skb_queue_head_init(&sch->q);
  558. spin_lock_init(&sch->busylock);
  559. lockdep_set_class(&sch->busylock,
  560. dev->qdisc_tx_busylock ?: &qdisc_tx_busylock);
  561. sch->ops = ops;
  562. sch->enqueue = ops->enqueue;
  563. sch->dequeue = ops->dequeue;
  564. sch->dev_queue = dev_queue;
  565. dev_hold(dev);
  566. atomic_set(&sch->refcnt, 1);
  567. return sch;
  568. errout:
  569. return ERR_PTR(err);
  570. }
  571. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  572. const struct Qdisc_ops *ops,
  573. unsigned int parentid)
  574. {
  575. struct Qdisc *sch;
  576. if (!try_module_get(ops->owner))
  577. goto errout;
  578. sch = qdisc_alloc(dev_queue, ops);
  579. if (IS_ERR(sch))
  580. goto errout;
  581. sch->parent = parentid;
  582. if (!ops->init || ops->init(sch, NULL) == 0)
  583. return sch;
  584. qdisc_destroy(sch);
  585. errout:
  586. return NULL;
  587. }
  588. EXPORT_SYMBOL(qdisc_create_dflt);
  589. /* Under qdisc_lock(qdisc) and BH! */
  590. void qdisc_reset(struct Qdisc *qdisc)
  591. {
  592. const struct Qdisc_ops *ops = qdisc->ops;
  593. if (ops->reset)
  594. ops->reset(qdisc);
  595. if (qdisc->gso_skb) {
  596. kfree_skb_list(qdisc->gso_skb);
  597. qdisc->gso_skb = NULL;
  598. qdisc->q.qlen = 0;
  599. }
  600. }
  601. EXPORT_SYMBOL(qdisc_reset);
  602. static void qdisc_rcu_free(struct rcu_head *head)
  603. {
  604. struct Qdisc *qdisc = container_of(head, struct Qdisc, rcu_head);
  605. if (qdisc_is_percpu_stats(qdisc))
  606. free_percpu(qdisc->cpu_bstats);
  607. kfree((char *) qdisc - qdisc->padded);
  608. }
  609. void qdisc_destroy(struct Qdisc *qdisc)
  610. {
  611. const struct Qdisc_ops *ops = qdisc->ops;
  612. if (qdisc->flags & TCQ_F_BUILTIN ||
  613. !atomic_dec_and_test(&qdisc->refcnt))
  614. return;
  615. #ifdef CONFIG_NET_SCHED
  616. qdisc_list_del(qdisc);
  617. qdisc_put_stab(rtnl_dereference(qdisc->stab));
  618. #endif
  619. gen_kill_estimator(&qdisc->bstats, &qdisc->rate_est);
  620. if (ops->reset)
  621. ops->reset(qdisc);
  622. if (ops->destroy)
  623. ops->destroy(qdisc);
  624. module_put(ops->owner);
  625. dev_put(qdisc_dev(qdisc));
  626. kfree_skb_list(qdisc->gso_skb);
  627. /*
  628. * gen_estimator est_timer() might access qdisc->q.lock,
  629. * wait a RCU grace period before freeing qdisc.
  630. */
  631. call_rcu(&qdisc->rcu_head, qdisc_rcu_free);
  632. }
  633. EXPORT_SYMBOL(qdisc_destroy);
  634. /* Attach toplevel qdisc to device queue. */
  635. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  636. struct Qdisc *qdisc)
  637. {
  638. struct Qdisc *oqdisc = dev_queue->qdisc_sleeping;
  639. spinlock_t *root_lock;
  640. root_lock = qdisc_lock(oqdisc);
  641. spin_lock_bh(root_lock);
  642. /* Prune old scheduler */
  643. if (oqdisc && atomic_read(&oqdisc->refcnt) <= 1)
  644. qdisc_reset(oqdisc);
  645. /* ... and graft new one */
  646. if (qdisc == NULL)
  647. qdisc = &noop_qdisc;
  648. dev_queue->qdisc_sleeping = qdisc;
  649. rcu_assign_pointer(dev_queue->qdisc, &noop_qdisc);
  650. spin_unlock_bh(root_lock);
  651. return oqdisc;
  652. }
  653. EXPORT_SYMBOL(dev_graft_qdisc);
  654. static void attach_one_default_qdisc(struct net_device *dev,
  655. struct netdev_queue *dev_queue,
  656. void *_unused)
  657. {
  658. struct Qdisc *qdisc = &noqueue_qdisc;
  659. if (dev->tx_queue_len) {
  660. qdisc = qdisc_create_dflt(dev_queue,
  661. default_qdisc_ops, TC_H_ROOT);
  662. if (!qdisc) {
  663. netdev_info(dev, "activation failed\n");
  664. return;
  665. }
  666. if (!netif_is_multiqueue(dev))
  667. qdisc->flags |= TCQ_F_ONETXQUEUE;
  668. }
  669. dev_queue->qdisc_sleeping = qdisc;
  670. }
  671. static void attach_default_qdiscs(struct net_device *dev)
  672. {
  673. struct netdev_queue *txq;
  674. struct Qdisc *qdisc;
  675. txq = netdev_get_tx_queue(dev, 0);
  676. if (!netif_is_multiqueue(dev) || dev->tx_queue_len == 0) {
  677. netdev_for_each_tx_queue(dev, attach_one_default_qdisc, NULL);
  678. dev->qdisc = txq->qdisc_sleeping;
  679. atomic_inc(&dev->qdisc->refcnt);
  680. } else {
  681. qdisc = qdisc_create_dflt(txq, &mq_qdisc_ops, TC_H_ROOT);
  682. if (qdisc) {
  683. dev->qdisc = qdisc;
  684. qdisc->ops->attach(qdisc);
  685. }
  686. }
  687. }
  688. static void transition_one_qdisc(struct net_device *dev,
  689. struct netdev_queue *dev_queue,
  690. void *_need_watchdog)
  691. {
  692. struct Qdisc *new_qdisc = dev_queue->qdisc_sleeping;
  693. int *need_watchdog_p = _need_watchdog;
  694. if (!(new_qdisc->flags & TCQ_F_BUILTIN))
  695. clear_bit(__QDISC_STATE_DEACTIVATED, &new_qdisc->state);
  696. rcu_assign_pointer(dev_queue->qdisc, new_qdisc);
  697. if (need_watchdog_p && new_qdisc != &noqueue_qdisc) {
  698. dev_queue->trans_start = 0;
  699. *need_watchdog_p = 1;
  700. }
  701. }
  702. void dev_activate(struct net_device *dev)
  703. {
  704. int need_watchdog;
  705. /* No queueing discipline is attached to device;
  706. * create default one for devices, which need queueing
  707. * and noqueue_qdisc for virtual interfaces
  708. */
  709. pr_debug("[mtk net][sched]dev activate dev = %s\n", dev->name);
  710. if (dev->qdisc == &noop_qdisc)
  711. attach_default_qdiscs(dev);
  712. if (!netif_carrier_ok(dev))
  713. /* Delay activation until next carrier-on event */
  714. return;
  715. need_watchdog = 0;
  716. netdev_for_each_tx_queue(dev, transition_one_qdisc, &need_watchdog);
  717. if (dev_ingress_queue(dev))
  718. transition_one_qdisc(dev, dev_ingress_queue(dev), NULL);
  719. if (need_watchdog) {
  720. dev->trans_start = jiffies;
  721. dev_watchdog_up(dev);
  722. }
  723. }
  724. EXPORT_SYMBOL(dev_activate);
  725. static void dev_deactivate_queue(struct net_device *dev,
  726. struct netdev_queue *dev_queue,
  727. void *_qdisc_default)
  728. {
  729. struct Qdisc *qdisc_default = _qdisc_default;
  730. struct Qdisc *qdisc;
  731. qdisc = rtnl_dereference(dev_queue->qdisc);
  732. if (qdisc) {
  733. spin_lock_bh(qdisc_lock(qdisc));
  734. if (!(qdisc->flags & TCQ_F_BUILTIN))
  735. set_bit(__QDISC_STATE_DEACTIVATED, &qdisc->state);
  736. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  737. qdisc_reset(qdisc);
  738. spin_unlock_bh(qdisc_lock(qdisc));
  739. }
  740. }
  741. static bool some_qdisc_is_busy(struct net_device *dev)
  742. {
  743. unsigned int i;
  744. for (i = 0; i < dev->num_tx_queues; i++) {
  745. struct netdev_queue *dev_queue;
  746. spinlock_t *root_lock;
  747. struct Qdisc *q;
  748. int val;
  749. dev_queue = netdev_get_tx_queue(dev, i);
  750. q = dev_queue->qdisc_sleeping;
  751. /*MTK_NET_CHANGES*/
  752. if (q == NULL) {
  753. pr_err("some_qdisc_is_busy dev = %p, i = %d, dev_q = %p", dev, i, dev_queue);
  754. BUG_ON(q == NULL);
  755. }
  756. root_lock = qdisc_lock(q);
  757. spin_lock_bh(root_lock);
  758. val = (qdisc_is_running(q) ||
  759. test_bit(__QDISC_STATE_SCHED, &q->state));
  760. spin_unlock_bh(root_lock);
  761. if (val)
  762. return true;
  763. }
  764. return false;
  765. }
  766. /**
  767. * dev_deactivate_many - deactivate transmissions on several devices
  768. * @head: list of devices to deactivate
  769. *
  770. * This function returns only when all outstanding transmissions
  771. * have completed, unless all devices are in dismantle phase.
  772. */
  773. void dev_deactivate_many(struct list_head *head)
  774. {
  775. struct net_device *dev;
  776. bool sync_needed = false;
  777. list_for_each_entry(dev, head, close_list) {
  778. netdev_for_each_tx_queue(dev, dev_deactivate_queue,
  779. &noop_qdisc);
  780. if (dev_ingress_queue(dev))
  781. dev_deactivate_queue(dev, dev_ingress_queue(dev),
  782. &noop_qdisc);
  783. dev_watchdog_down(dev);
  784. sync_needed |= !dev->dismantle;
  785. }
  786. /* Wait for outstanding qdisc-less dev_queue_xmit calls.
  787. * This is avoided if all devices are in dismantle phase :
  788. * Caller will call synchronize_net() for us
  789. */
  790. if (sync_needed)
  791. synchronize_net();
  792. /* Wait for outstanding qdisc_run calls. */
  793. list_for_each_entry(dev, head, close_list)
  794. while (some_qdisc_is_busy(dev))
  795. yield();
  796. }
  797. void dev_deactivate(struct net_device *dev)
  798. {
  799. LIST_HEAD(single);
  800. list_add(&dev->close_list, &single);
  801. pr_debug("[mtk net][sched]dev deactivate dev = %s\n", dev->name);
  802. dev_deactivate_many(&single);
  803. list_del(&single);
  804. }
  805. EXPORT_SYMBOL(dev_deactivate);
  806. static void dev_init_scheduler_queue(struct net_device *dev,
  807. struct netdev_queue *dev_queue,
  808. void *_qdisc)
  809. {
  810. struct Qdisc *qdisc = _qdisc;
  811. rcu_assign_pointer(dev_queue->qdisc, qdisc);
  812. dev_queue->qdisc_sleeping = qdisc;
  813. }
  814. void dev_init_scheduler(struct net_device *dev)
  815. {
  816. dev->qdisc = &noop_qdisc;
  817. netdev_for_each_tx_queue(dev, dev_init_scheduler_queue, &noop_qdisc);
  818. if (dev_ingress_queue(dev))
  819. dev_init_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  820. setup_timer(&dev->watchdog_timer, dev_watchdog, (unsigned long)dev);
  821. }
  822. static void shutdown_scheduler_queue(struct net_device *dev,
  823. struct netdev_queue *dev_queue,
  824. void *_qdisc_default)
  825. {
  826. struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
  827. struct Qdisc *qdisc_default = _qdisc_default;
  828. if (qdisc) {
  829. rcu_assign_pointer(dev_queue->qdisc, qdisc_default);
  830. dev_queue->qdisc_sleeping = qdisc_default;
  831. qdisc_destroy(qdisc);
  832. }
  833. }
  834. void dev_shutdown(struct net_device *dev)
  835. {
  836. netdev_for_each_tx_queue(dev, shutdown_scheduler_queue, &noop_qdisc);
  837. if (dev_ingress_queue(dev))
  838. shutdown_scheduler_queue(dev, dev_ingress_queue(dev), &noop_qdisc);
  839. qdisc_destroy(dev->qdisc);
  840. dev->qdisc = &noop_qdisc;
  841. WARN_ON(timer_pending(&dev->watchdog_timer));
  842. }
  843. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  844. const struct tc_ratespec *conf,
  845. u64 rate64)
  846. {
  847. memset(r, 0, sizeof(*r));
  848. r->overhead = conf->overhead;
  849. r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
  850. r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
  851. r->mult = 1;
  852. /*
  853. * The deal here is to replace a divide by a reciprocal one
  854. * in fast path (a reciprocal divide is a multiply and a shift)
  855. *
  856. * Normal formula would be :
  857. * time_in_ns = (NSEC_PER_SEC * len) / rate_bps
  858. *
  859. * We compute mult/shift to use instead :
  860. * time_in_ns = (len * mult) >> shift;
  861. *
  862. * We try to get the highest possible mult value for accuracy,
  863. * but have to make sure no overflows will ever happen.
  864. */
  865. if (r->rate_bytes_ps > 0) {
  866. u64 factor = NSEC_PER_SEC;
  867. for (;;) {
  868. r->mult = div64_u64(factor, r->rate_bytes_ps);
  869. if (r->mult & (1U << 31) || factor & (1ULL << 63))
  870. break;
  871. factor <<= 1;
  872. r->shift++;
  873. }
  874. }
  875. }
  876. EXPORT_SYMBOL(psched_ratecfg_precompute);