1
0

033-fq_codel-add-memory-limitation-per-queue.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. From: Eric Dumazet <edumazet@google.com>
  2. Date: Fri, 6 May 2016 08:55:12 -0700
  3. Subject: [PATCH] fq_codel: add memory limitation per queue
  4. MIME-Version: 1.0
  5. Content-Type: text/plain; charset=UTF-8
  6. Content-Transfer-Encoding: 8bit
  7. On small embedded routers, one wants to control maximal amount of
  8. memory used by fq_codel, instead of controlling number of packets or
  9. bytes, since GRO/TSO make these not practical.
  10. Assuming skb->truesize is accurate, we have to keep track of
  11. skb->truesize sum for skbs in queue.
  12. This patch adds a new TCA_FQ_CODEL_MEMORY_LIMIT attribute.
  13. I chose a default value of 32 MBytes, which looks reasonable even
  14. for heavy duty usages. (Prior fq_codel users should not be hurt
  15. when they upgrade their kernels)
  16. Two fields are added to tc_fq_codel_qd_stats to report :
  17. - Current memory usage
  18. - Number of drops caused by memory limits
  19. # tc qd replace dev eth1 root est 1sec 4sec fq_codel memory_limit 4M
  20. ..
  21. # tc -s -d qd sh dev eth1
  22. qdisc fq_codel 8008: root refcnt 257 limit 10240p flows 1024
  23. quantum 1514 target 5.0ms interval 100.0ms memory_limit 4Mb ecn
  24. Sent 2083566791363 bytes 1376214889 pkt (dropped 4994406, overlimits 0
  25. requeues 21705223)
  26. rate 9841Mbit 812549pps backlog 3906120b 376p requeues 21705223
  27. maxpacket 68130 drop_overlimit 4994406 new_flow_count 28855414
  28. ecn_mark 0 memory_used 4190048 drop_overmemory 4994406
  29. new_flows_len 1 old_flows_len 177
  30. Signed-off-by: Eric Dumazet <edumazet@google.com>
  31. Cc: Jesper Dangaard Brouer <brouer@redhat.com>
  32. Cc: Dave Täht <dave.taht@gmail.com>
  33. Cc: Sebastian Möller <moeller0@gmx.de>
  34. Signed-off-by: David S. Miller <davem@davemloft.net>
  35. ---
  36. --- a/include/uapi/linux/pkt_sched.h
  37. +++ b/include/uapi/linux/pkt_sched.h
  38. @@ -712,6 +712,7 @@ enum {
  39. TCA_FQ_CODEL_QUANTUM,
  40. TCA_FQ_CODEL_CE_THRESHOLD,
  41. TCA_FQ_CODEL_DROP_BATCH_SIZE,
  42. + TCA_FQ_CODEL_MEMORY_LIMIT,
  43. __TCA_FQ_CODEL_MAX
  44. };
  45. @@ -736,6 +737,8 @@ struct tc_fq_codel_qd_stats {
  46. __u32 new_flows_len; /* count of flows in new list */
  47. __u32 old_flows_len; /* count of flows in old list */
  48. __u32 ce_mark; /* packets above ce_threshold */
  49. + __u32 memory_usage; /* in bytes */
  50. + __u32 drop_overmemory;
  51. };
  52. struct tc_fq_codel_cl_stats {
  53. --- a/net/sched/sch_fq_codel.c
  54. +++ b/net/sched/sch_fq_codel.c
  55. @@ -58,8 +58,11 @@ struct fq_codel_sched_data {
  56. u32 perturbation; /* hash perturbation */
  57. u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
  58. u32 drop_batch_size;
  59. + u32 memory_limit;
  60. struct codel_params cparams;
  61. struct codel_stats cstats;
  62. + u32 memory_usage;
  63. + u32 drop_overmemory;
  64. u32 drop_overlimit;
  65. u32 new_flow_count;
  66. @@ -141,6 +144,7 @@ static unsigned int fq_codel_drop(struct
  67. unsigned int maxbacklog = 0, idx = 0, i, len;
  68. struct fq_codel_flow *flow;
  69. unsigned int threshold;
  70. + unsigned int mem = 0;
  71. /* Queue is full! Find the fat flow and drop packet(s) from it.
  72. * This might sound expensive, but with 1024 flows, we scan
  73. @@ -165,11 +169,13 @@ static unsigned int fq_codel_drop(struct
  74. do {
  75. skb = dequeue_head(flow);
  76. len += qdisc_pkt_len(skb);
  77. + mem += skb->truesize;
  78. kfree_skb(skb);
  79. } while (++i < max_packets && len < threshold);
  80. flow->dropped += i;
  81. q->backlogs[idx] -= len;
  82. + q->memory_usage -= mem;
  83. sch->qstats.drops += i;
  84. sch->qstats.backlog -= len;
  85. sch->q.qlen -= i;
  86. @@ -191,6 +197,7 @@ static int fq_codel_enqueue(struct sk_bu
  87. unsigned int idx, prev_backlog, prev_qlen;
  88. struct fq_codel_flow *flow;
  89. int uninitialized_var(ret);
  90. + bool memory_limited;
  91. idx = fq_codel_classify(skb, sch, &ret);
  92. if (idx == 0) {
  93. @@ -213,7 +220,9 @@ static int fq_codel_enqueue(struct sk_bu
  94. flow->deficit = q->quantum;
  95. flow->dropped = 0;
  96. }
  97. - if (++sch->q.qlen <= sch->limit)
  98. + q->memory_usage += skb->truesize;
  99. + memory_limited = q->memory_usage > q->memory_limit;
  100. + if (++sch->q.qlen <= sch->limit && !memory_limited)
  101. return NET_XMIT_SUCCESS;
  102. prev_backlog = sch->qstats.backlog;
  103. @@ -227,7 +236,8 @@ static int fq_codel_enqueue(struct sk_bu
  104. ret = fq_codel_drop(sch, q->drop_batch_size);
  105. q->drop_overlimit += prev_qlen - sch->q.qlen;
  106. -
  107. + if (memory_limited)
  108. + q->drop_overmemory += prev_qlen - sch->q.qlen;
  109. /* As we dropped packet(s), better let upper stack know this */
  110. qdisc_tree_reduce_backlog(sch, prev_qlen - sch->q.qlen,
  111. prev_backlog - sch->qstats.backlog);
  112. @@ -296,6 +306,7 @@ begin:
  113. list_del_init(&flow->flowchain);
  114. goto begin;
  115. }
  116. + q->memory_usage -= skb->truesize;
  117. qdisc_bstats_update(sch, skb);
  118. flow->deficit -= qdisc_pkt_len(skb);
  119. /* We cant call qdisc_tree_reduce_backlog() if our qlen is 0,
  120. @@ -343,6 +354,7 @@ static const struct nla_policy fq_codel_
  121. [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
  122. [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
  123. [TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
  124. + [TCA_FQ_CODEL_MEMORY_LIMIT] = { .type = NLA_U32 },
  125. };
  126. static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
  127. @@ -397,7 +409,11 @@ static int fq_codel_change(struct Qdisc
  128. if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
  129. q->drop_batch_size = min(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]));
  130. - while (sch->q.qlen > sch->limit) {
  131. + if (tb[TCA_FQ_CODEL_MEMORY_LIMIT])
  132. + q->memory_limit = min(1U << 31, nla_get_u32(tb[TCA_FQ_CODEL_MEMORY_LIMIT]));
  133. +
  134. + while (sch->q.qlen > sch->limit ||
  135. + q->memory_usage > q->memory_limit) {
  136. struct sk_buff *skb = fq_codel_dequeue(sch);
  137. q->cstats.drop_len += qdisc_pkt_len(skb);
  138. @@ -442,6 +458,7 @@ static int fq_codel_init(struct Qdisc *s
  139. sch->limit = 10*1024;
  140. q->flows_cnt = 1024;
  141. + q->memory_limit = 32 << 20; /* 32 MBytes */
  142. q->drop_batch_size = 64;
  143. q->quantum = psched_mtu(qdisc_dev(sch));
  144. q->perturbation = prandom_u32();
  145. @@ -502,6 +519,8 @@ static int fq_codel_dump(struct Qdisc *s
  146. q->quantum) ||
  147. nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
  148. q->drop_batch_size) ||
  149. + nla_put_u32(skb, TCA_FQ_CODEL_MEMORY_LIMIT,
  150. + q->memory_limit) ||
  151. nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
  152. q->flows_cnt))
  153. goto nla_put_failure;
  154. @@ -530,6 +549,8 @@ static int fq_codel_dump_stats(struct Qd
  155. st.qdisc_stats.ecn_mark = q->cstats.ecn_mark;
  156. st.qdisc_stats.new_flow_count = q->new_flow_count;
  157. st.qdisc_stats.ce_mark = q->cstats.ce_mark;
  158. + st.qdisc_stats.memory_usage = q->memory_usage;
  159. + st.qdisc_stats.drop_overmemory = q->drop_overmemory;
  160. list_for_each(pos, &q->new_flows)
  161. st.qdisc_stats.new_flows_len++;