fq_impl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2016 Qualcomm Atheros, Inc
  3. *
  4. * GPL v2
  5. *
  6. * Based on net/sched/sch_fq_codel.c
  7. */
  8. #ifndef __NET_SCHED_FQ_IMPL_H
  9. #define __NET_SCHED_FQ_IMPL_H
  10. #include <net/fq.h>
  11. /* functions that are embedded into includer */
  12. static struct sk_buff *fq_flow_dequeue(struct fq *fq,
  13. struct fq_flow *flow)
  14. {
  15. struct fq_tin *tin = flow->tin;
  16. struct fq_flow *i;
  17. struct sk_buff *skb;
  18. lockdep_assert_held(&fq->lock);
  19. skb = __skb_dequeue(&flow->queue);
  20. if (!skb)
  21. return NULL;
  22. tin->backlog_bytes -= skb->len;
  23. tin->backlog_packets--;
  24. flow->backlog -= skb->len;
  25. fq->backlog--;
  26. fq->memory_usage -= skb->truesize;
  27. if (flow->backlog == 0) {
  28. list_del_init(&flow->backlogchain);
  29. } else {
  30. i = flow;
  31. list_for_each_entry_continue(i, &fq->backlogs, backlogchain)
  32. if (i->backlog < flow->backlog)
  33. break;
  34. list_move_tail(&flow->backlogchain,
  35. &i->backlogchain);
  36. }
  37. return skb;
  38. }
  39. static struct sk_buff *fq_tin_dequeue(struct fq *fq,
  40. struct fq_tin *tin,
  41. fq_tin_dequeue_t dequeue_func)
  42. {
  43. struct fq_flow *flow;
  44. struct list_head *head;
  45. struct sk_buff *skb;
  46. lockdep_assert_held(&fq->lock);
  47. begin:
  48. head = &tin->new_flows;
  49. if (list_empty(head)) {
  50. head = &tin->old_flows;
  51. if (list_empty(head))
  52. return NULL;
  53. }
  54. flow = list_first_entry(head, struct fq_flow, flowchain);
  55. if (flow->deficit <= 0) {
  56. flow->deficit += fq->quantum;
  57. list_move_tail(&flow->flowchain,
  58. &tin->old_flows);
  59. goto begin;
  60. }
  61. skb = dequeue_func(fq, tin, flow);
  62. if (!skb) {
  63. /* force a pass through old_flows to prevent starvation */
  64. if ((head == &tin->new_flows) &&
  65. !list_empty(&tin->old_flows)) {
  66. list_move_tail(&flow->flowchain, &tin->old_flows);
  67. } else {
  68. list_del_init(&flow->flowchain);
  69. flow->tin = NULL;
  70. }
  71. goto begin;
  72. }
  73. flow->deficit -= skb->len;
  74. tin->tx_bytes += skb->len;
  75. tin->tx_packets++;
  76. return skb;
  77. }
  78. static struct fq_flow *fq_flow_classify(struct fq *fq,
  79. struct fq_tin *tin,
  80. struct sk_buff *skb,
  81. fq_flow_get_default_t get_default_func)
  82. {
  83. struct fq_flow *flow;
  84. u32 hash;
  85. u32 idx;
  86. lockdep_assert_held(&fq->lock);
  87. hash = skb_get_hash_perturb(skb, &fq->perturbation);
  88. idx = reciprocal_scale(hash, fq->flows_cnt);
  89. flow = &fq->flows[idx];
  90. if (flow->tin && flow->tin != tin) {
  91. flow = get_default_func(fq, tin, idx, skb);
  92. tin->collisions++;
  93. fq->collisions++;
  94. }
  95. if (!flow->tin)
  96. tin->flows++;
  97. return flow;
  98. }
  99. static void fq_recalc_backlog(struct fq *fq,
  100. struct fq_tin *tin,
  101. struct fq_flow *flow)
  102. {
  103. struct fq_flow *i;
  104. if (list_empty(&flow->backlogchain))
  105. list_add_tail(&flow->backlogchain, &fq->backlogs);
  106. i = flow;
  107. list_for_each_entry_continue_reverse(i, &fq->backlogs,
  108. backlogchain)
  109. if (i->backlog > flow->backlog)
  110. break;
  111. list_move(&flow->backlogchain, &i->backlogchain);
  112. }
  113. static void fq_tin_enqueue(struct fq *fq,
  114. struct fq_tin *tin,
  115. struct sk_buff *skb,
  116. fq_skb_free_t free_func,
  117. fq_flow_get_default_t get_default_func)
  118. {
  119. struct fq_flow *flow;
  120. bool oom;
  121. lockdep_assert_held(&fq->lock);
  122. flow = fq_flow_classify(fq, tin, skb, get_default_func);
  123. flow->tin = tin;
  124. flow->backlog += skb->len;
  125. tin->backlog_bytes += skb->len;
  126. tin->backlog_packets++;
  127. fq->memory_usage += skb->truesize;
  128. fq->backlog++;
  129. fq_recalc_backlog(fq, tin, flow);
  130. if (list_empty(&flow->flowchain)) {
  131. flow->deficit = fq->quantum;
  132. list_add_tail(&flow->flowchain,
  133. &tin->new_flows);
  134. }
  135. __skb_queue_tail(&flow->queue, skb);
  136. oom = (fq->memory_usage > fq->memory_limit);
  137. while (fq->backlog > fq->limit || oom) {
  138. flow = list_first_entry_or_null(&fq->backlogs,
  139. struct fq_flow,
  140. backlogchain);
  141. if (!flow)
  142. return;
  143. skb = fq_flow_dequeue(fq, flow);
  144. if (!skb)
  145. return;
  146. free_func(fq, flow->tin, flow, skb);
  147. flow->tin->overlimit++;
  148. fq->overlimit++;
  149. if (oom) {
  150. fq->overmemory++;
  151. oom = (fq->memory_usage > fq->memory_limit);
  152. }
  153. }
  154. }
  155. static void fq_flow_reset(struct fq *fq,
  156. struct fq_flow *flow,
  157. fq_skb_free_t free_func)
  158. {
  159. struct sk_buff *skb;
  160. while ((skb = fq_flow_dequeue(fq, flow)))
  161. free_func(fq, flow->tin, flow, skb);
  162. if (!list_empty(&flow->flowchain))
  163. list_del_init(&flow->flowchain);
  164. if (!list_empty(&flow->backlogchain))
  165. list_del_init(&flow->backlogchain);
  166. flow->tin = NULL;
  167. WARN_ON_ONCE(flow->backlog);
  168. }
  169. static void fq_tin_reset(struct fq *fq,
  170. struct fq_tin *tin,
  171. fq_skb_free_t free_func)
  172. {
  173. struct list_head *head;
  174. struct fq_flow *flow;
  175. for (;;) {
  176. head = &tin->new_flows;
  177. if (list_empty(head)) {
  178. head = &tin->old_flows;
  179. if (list_empty(head))
  180. break;
  181. }
  182. flow = list_first_entry(head, struct fq_flow, flowchain);
  183. fq_flow_reset(fq, flow, free_func);
  184. }
  185. WARN_ON_ONCE(tin->backlog_bytes);
  186. WARN_ON_ONCE(tin->backlog_packets);
  187. }
  188. static void fq_flow_init(struct fq_flow *flow)
  189. {
  190. INIT_LIST_HEAD(&flow->flowchain);
  191. INIT_LIST_HEAD(&flow->backlogchain);
  192. __skb_queue_head_init(&flow->queue);
  193. }
  194. static void fq_tin_init(struct fq_tin *tin)
  195. {
  196. INIT_LIST_HEAD(&tin->new_flows);
  197. INIT_LIST_HEAD(&tin->old_flows);
  198. }
  199. static int fq_init(struct fq *fq, int flows_cnt)
  200. {
  201. int i;
  202. memset(fq, 0, sizeof(fq[0]));
  203. INIT_LIST_HEAD(&fq->backlogs);
  204. spin_lock_init(&fq->lock);
  205. fq->flows_cnt = max_t(u32, flows_cnt, 1);
  206. get_random_bytes(&fq->perturbation, sizeof(fq->perturbation));
  207. fq->quantum = 300;
  208. fq->limit = 8192;
  209. fq->memory_limit = 16 << 20; /* 16 MBytes */
  210. fq->flows = kcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
  211. if (!fq->flows)
  212. return -ENOMEM;
  213. for (i = 0; i < fq->flows_cnt; i++)
  214. fq_flow_init(&fq->flows[i]);
  215. return 0;
  216. }
  217. static void fq_reset(struct fq *fq,
  218. fq_skb_free_t free_func)
  219. {
  220. int i;
  221. for (i = 0; i < fq->flows_cnt; i++)
  222. fq_flow_reset(fq, &fq->flows[i], free_func);
  223. kfree(fq->flows);
  224. fq->flows = NULL;
  225. }
  226. #endif