fq_impl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 void fq_adjust_removal(struct fq *fq,
  13. struct fq_flow *flow,
  14. struct sk_buff *skb)
  15. {
  16. struct fq_tin *tin = flow->tin;
  17. tin->backlog_bytes -= skb->len;
  18. tin->backlog_packets--;
  19. flow->backlog -= skb->len;
  20. fq->backlog--;
  21. fq->memory_usage -= skb->truesize;
  22. }
  23. static void fq_rejigger_backlog(struct fq *fq, struct fq_flow *flow)
  24. {
  25. struct fq_flow *i;
  26. if (flow->backlog == 0) {
  27. list_del_init(&flow->backlogchain);
  28. } else {
  29. i = flow;
  30. list_for_each_entry_continue(i, &fq->backlogs, backlogchain)
  31. if (i->backlog < flow->backlog)
  32. break;
  33. list_move_tail(&flow->backlogchain,
  34. &i->backlogchain);
  35. }
  36. }
  37. static struct sk_buff *fq_flow_dequeue(struct fq *fq,
  38. struct fq_flow *flow)
  39. {
  40. struct sk_buff *skb;
  41. lockdep_assert_held(&fq->lock);
  42. skb = __skb_dequeue(&flow->queue);
  43. if (!skb)
  44. return NULL;
  45. fq_adjust_removal(fq, flow, skb);
  46. fq_rejigger_backlog(fq, flow);
  47. return skb;
  48. }
  49. static struct sk_buff *fq_tin_dequeue(struct fq *fq,
  50. struct fq_tin *tin,
  51. fq_tin_dequeue_t dequeue_func)
  52. {
  53. struct fq_flow *flow;
  54. struct list_head *head;
  55. struct sk_buff *skb;
  56. lockdep_assert_held(&fq->lock);
  57. begin:
  58. head = &tin->new_flows;
  59. if (list_empty(head)) {
  60. head = &tin->old_flows;
  61. if (list_empty(head))
  62. return NULL;
  63. }
  64. flow = list_first_entry(head, struct fq_flow, flowchain);
  65. if (flow->deficit <= 0) {
  66. flow->deficit += fq->quantum;
  67. list_move_tail(&flow->flowchain,
  68. &tin->old_flows);
  69. goto begin;
  70. }
  71. skb = dequeue_func(fq, tin, flow);
  72. if (!skb) {
  73. /* force a pass through old_flows to prevent starvation */
  74. if ((head == &tin->new_flows) &&
  75. !list_empty(&tin->old_flows)) {
  76. list_move_tail(&flow->flowchain, &tin->old_flows);
  77. } else {
  78. list_del_init(&flow->flowchain);
  79. flow->tin = NULL;
  80. }
  81. goto begin;
  82. }
  83. flow->deficit -= skb->len;
  84. tin->tx_bytes += skb->len;
  85. tin->tx_packets++;
  86. return skb;
  87. }
  88. static struct fq_flow *fq_flow_classify(struct fq *fq,
  89. struct fq_tin *tin,
  90. struct sk_buff *skb,
  91. fq_flow_get_default_t get_default_func)
  92. {
  93. struct fq_flow *flow;
  94. u32 hash;
  95. u32 idx;
  96. lockdep_assert_held(&fq->lock);
  97. hash = skb_get_hash_perturb(skb, &fq->perturbation);
  98. idx = reciprocal_scale(hash, fq->flows_cnt);
  99. flow = &fq->flows[idx];
  100. if (flow->tin && flow->tin != tin) {
  101. flow = get_default_func(fq, tin, idx, skb);
  102. tin->collisions++;
  103. fq->collisions++;
  104. }
  105. if (!flow->tin)
  106. tin->flows++;
  107. return flow;
  108. }
  109. static void fq_recalc_backlog(struct fq *fq,
  110. struct fq_tin *tin,
  111. struct fq_flow *flow)
  112. {
  113. struct fq_flow *i;
  114. if (list_empty(&flow->backlogchain))
  115. list_add_tail(&flow->backlogchain, &fq->backlogs);
  116. i = flow;
  117. list_for_each_entry_continue_reverse(i, &fq->backlogs,
  118. backlogchain)
  119. if (i->backlog > flow->backlog)
  120. break;
  121. list_move(&flow->backlogchain, &i->backlogchain);
  122. }
  123. static void fq_tin_enqueue(struct fq *fq,
  124. struct fq_tin *tin,
  125. struct sk_buff *skb,
  126. fq_skb_free_t free_func,
  127. fq_flow_get_default_t get_default_func)
  128. {
  129. struct fq_flow *flow;
  130. bool oom;
  131. lockdep_assert_held(&fq->lock);
  132. flow = fq_flow_classify(fq, tin, skb, get_default_func);
  133. flow->tin = tin;
  134. flow->backlog += skb->len;
  135. tin->backlog_bytes += skb->len;
  136. tin->backlog_packets++;
  137. fq->memory_usage += skb->truesize;
  138. fq->backlog++;
  139. fq_recalc_backlog(fq, tin, flow);
  140. if (list_empty(&flow->flowchain)) {
  141. flow->deficit = fq->quantum;
  142. list_add_tail(&flow->flowchain,
  143. &tin->new_flows);
  144. }
  145. __skb_queue_tail(&flow->queue, skb);
  146. oom = (fq->memory_usage > fq->memory_limit);
  147. while (fq->backlog > fq->limit || oom) {
  148. flow = list_first_entry_or_null(&fq->backlogs,
  149. struct fq_flow,
  150. backlogchain);
  151. if (!flow)
  152. return;
  153. skb = fq_flow_dequeue(fq, flow);
  154. if (!skb)
  155. return;
  156. free_func(fq, flow->tin, flow, skb);
  157. flow->tin->overlimit++;
  158. fq->overlimit++;
  159. if (oom) {
  160. fq->overmemory++;
  161. oom = (fq->memory_usage > fq->memory_limit);
  162. }
  163. }
  164. }
  165. static void fq_flow_filter(struct fq *fq,
  166. struct fq_flow *flow,
  167. fq_skb_filter_t filter_func,
  168. void *filter_data,
  169. fq_skb_free_t free_func)
  170. {
  171. struct fq_tin *tin = flow->tin;
  172. struct sk_buff *skb, *tmp;
  173. lockdep_assert_held(&fq->lock);
  174. skb_queue_walk_safe(&flow->queue, skb, tmp) {
  175. if (!filter_func(fq, tin, flow, skb, filter_data))
  176. continue;
  177. __skb_unlink(skb, &flow->queue);
  178. fq_adjust_removal(fq, flow, skb);
  179. free_func(fq, tin, flow, skb);
  180. }
  181. fq_rejigger_backlog(fq, flow);
  182. }
  183. static void fq_tin_filter(struct fq *fq,
  184. struct fq_tin *tin,
  185. fq_skb_filter_t filter_func,
  186. void *filter_data,
  187. fq_skb_free_t free_func)
  188. {
  189. struct fq_flow *flow;
  190. lockdep_assert_held(&fq->lock);
  191. list_for_each_entry(flow, &tin->new_flows, flowchain)
  192. fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
  193. list_for_each_entry(flow, &tin->old_flows, flowchain)
  194. fq_flow_filter(fq, flow, filter_func, filter_data, free_func);
  195. }
  196. static void fq_flow_reset(struct fq *fq,
  197. struct fq_flow *flow,
  198. fq_skb_free_t free_func)
  199. {
  200. struct sk_buff *skb;
  201. while ((skb = fq_flow_dequeue(fq, flow)))
  202. free_func(fq, flow->tin, flow, skb);
  203. if (!list_empty(&flow->flowchain))
  204. list_del_init(&flow->flowchain);
  205. if (!list_empty(&flow->backlogchain))
  206. list_del_init(&flow->backlogchain);
  207. flow->tin = NULL;
  208. WARN_ON_ONCE(flow->backlog);
  209. }
  210. static void fq_tin_reset(struct fq *fq,
  211. struct fq_tin *tin,
  212. fq_skb_free_t free_func)
  213. {
  214. struct list_head *head;
  215. struct fq_flow *flow;
  216. for (;;) {
  217. head = &tin->new_flows;
  218. if (list_empty(head)) {
  219. head = &tin->old_flows;
  220. if (list_empty(head))
  221. break;
  222. }
  223. flow = list_first_entry(head, struct fq_flow, flowchain);
  224. fq_flow_reset(fq, flow, free_func);
  225. }
  226. WARN_ON_ONCE(tin->backlog_bytes);
  227. WARN_ON_ONCE(tin->backlog_packets);
  228. }
  229. static void fq_flow_init(struct fq_flow *flow)
  230. {
  231. INIT_LIST_HEAD(&flow->flowchain);
  232. INIT_LIST_HEAD(&flow->backlogchain);
  233. __skb_queue_head_init(&flow->queue);
  234. }
  235. static void fq_tin_init(struct fq_tin *tin)
  236. {
  237. INIT_LIST_HEAD(&tin->new_flows);
  238. INIT_LIST_HEAD(&tin->old_flows);
  239. }
  240. static int fq_init(struct fq *fq, int flows_cnt)
  241. {
  242. int i;
  243. memset(fq, 0, sizeof(fq[0]));
  244. INIT_LIST_HEAD(&fq->backlogs);
  245. spin_lock_init(&fq->lock);
  246. fq->flows_cnt = max_t(u32, flows_cnt, 1);
  247. get_random_bytes(&fq->perturbation, sizeof(fq->perturbation));
  248. fq->quantum = 300;
  249. fq->limit = 8192;
  250. fq->memory_limit = 16 << 20; /* 16 MBytes */
  251. fq->flows = kvcalloc(fq->flows_cnt, sizeof(fq->flows[0]), GFP_KERNEL);
  252. if (!fq->flows)
  253. return -ENOMEM;
  254. for (i = 0; i < fq->flows_cnt; i++)
  255. fq_flow_init(&fq->flows[i]);
  256. return 0;
  257. }
  258. static void fq_reset(struct fq *fq,
  259. fq_skb_free_t free_func)
  260. {
  261. int i;
  262. for (i = 0; i < fq->flows_cnt; i++)
  263. fq_flow_reset(fq, &fq->flows[i], free_func);
  264. kvfree(fq->flows);
  265. fq->flows = NULL;
  266. }
  267. #endif