1
0

032-fq_codel-add-batch-ability-to-fq_codel_drop.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. From: Eric Dumazet <edumazet@google.com>
  2. Date: Sun, 1 May 2016 16:47:26 -0700
  3. Subject: [PATCH] fq_codel: add batch ability to fq_codel_drop()
  4. In presence of inelastic flows and stress, we can call
  5. fq_codel_drop() for every packet entering fq_codel qdisc.
  6. fq_codel_drop() is quite expensive, as it does a linear scan
  7. of 4 KB of memory to find a fat flow.
  8. Once found, it drops the oldest packet of this flow.
  9. Instead of dropping a single packet, try to drop 50% of the backlog
  10. of this fat flow, with a configurable limit of 64 packets per round.
  11. TCA_FQ_CODEL_DROP_BATCH_SIZE is the new attribute to make this
  12. limit configurable.
  13. With this strategy the 4 KB search is amortized to a single cache line
  14. per drop [1], so fq_codel_drop() no longer appears at the top of kernel
  15. profile in presence of few inelastic flows.
  16. [1] Assuming a 64byte cache line, and 1024 buckets
  17. Signed-off-by: Eric Dumazet <edumazet@google.com>
  18. Reported-by: Dave Taht <dave.taht@gmail.com>
  19. Cc: Jonathan Morton <chromatix99@gmail.com>
  20. Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
  21. Acked-by: Dave Taht
  22. Signed-off-by: David S. Miller <davem@davemloft.net>
  23. ---
  24. --- a/include/uapi/linux/pkt_sched.h
  25. +++ b/include/uapi/linux/pkt_sched.h
  26. @@ -711,6 +711,7 @@ enum {
  27. TCA_FQ_CODEL_FLOWS,
  28. TCA_FQ_CODEL_QUANTUM,
  29. TCA_FQ_CODEL_CE_THRESHOLD,
  30. + TCA_FQ_CODEL_DROP_BATCH_SIZE,
  31. __TCA_FQ_CODEL_MAX
  32. };
  33. --- a/net/sched/sch_fq_codel.c
  34. +++ b/net/sched/sch_fq_codel.c
  35. @@ -57,6 +57,7 @@ struct fq_codel_sched_data {
  36. u32 flows_cnt; /* number of flows */
  37. u32 perturbation; /* hash perturbation */
  38. u32 quantum; /* psched_mtu(qdisc_dev(sch)); */
  39. + u32 drop_batch_size;
  40. struct codel_params cparams;
  41. struct codel_stats cstats;
  42. u32 drop_overlimit;
  43. @@ -133,17 +134,20 @@ static inline void flow_queue_add(struct
  44. skb->next = NULL;
  45. }
  46. -static unsigned int fq_codel_drop(struct Qdisc *sch)
  47. +static unsigned int fq_codel_drop(struct Qdisc *sch, unsigned int max_packets)
  48. {
  49. struct fq_codel_sched_data *q = qdisc_priv(sch);
  50. struct sk_buff *skb;
  51. unsigned int maxbacklog = 0, idx = 0, i, len;
  52. struct fq_codel_flow *flow;
  53. + unsigned int threshold;
  54. - /* Queue is full! Find the fat flow and drop packet from it.
  55. + /* Queue is full! Find the fat flow and drop packet(s) from it.
  56. * This might sound expensive, but with 1024 flows, we scan
  57. * 4KB of memory, and we dont need to handle a complex tree
  58. * in fast path (packet queue/enqueue) with many cache misses.
  59. + * In stress mode, we'll try to drop 64 packets from the flow,
  60. + * amortizing this linear lookup to one cache line per drop.
  61. */
  62. for (i = 0; i < q->flows_cnt; i++) {
  63. if (q->backlogs[i] > maxbacklog) {
  64. @@ -151,15 +155,24 @@ static unsigned int fq_codel_drop(struct
  65. idx = i;
  66. }
  67. }
  68. +
  69. + /* Our goal is to drop half of this fat flow backlog */
  70. + threshold = maxbacklog >> 1;
  71. +
  72. flow = &q->flows[idx];
  73. - skb = dequeue_head(flow);
  74. - len = qdisc_pkt_len(skb);
  75. + len = 0;
  76. + i = 0;
  77. + do {
  78. + skb = dequeue_head(flow);
  79. + len += qdisc_pkt_len(skb);
  80. + kfree_skb(skb);
  81. + } while (++i < max_packets && len < threshold);
  82. +
  83. + flow->dropped += i;
  84. q->backlogs[idx] -= len;
  85. - sch->q.qlen--;
  86. - qdisc_qstats_drop(sch);
  87. - qdisc_qstats_backlog_dec(sch, skb);
  88. - kfree_skb(skb);
  89. - flow->dropped++;
  90. + sch->qstats.drops += i;
  91. + sch->qstats.backlog -= len;
  92. + sch->q.qlen -= i;
  93. return idx;
  94. }
  95. @@ -168,14 +181,14 @@ static unsigned int fq_codel_qdisc_drop(
  96. unsigned int prev_backlog;
  97. prev_backlog = sch->qstats.backlog;
  98. - fq_codel_drop(sch);
  99. + fq_codel_drop(sch, 1U);
  100. return prev_backlog - sch->qstats.backlog;
  101. }
  102. static int fq_codel_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  103. {
  104. struct fq_codel_sched_data *q = qdisc_priv(sch);
  105. - unsigned int idx, prev_backlog;
  106. + unsigned int idx, prev_backlog, prev_qlen;
  107. struct fq_codel_flow *flow;
  108. int uninitialized_var(ret);
  109. @@ -204,16 +217,22 @@ static int fq_codel_enqueue(struct sk_bu
  110. return NET_XMIT_SUCCESS;
  111. prev_backlog = sch->qstats.backlog;
  112. - q->drop_overlimit++;
  113. - /* Return Congestion Notification only if we dropped a packet
  114. - * from this flow.
  115. + prev_qlen = sch->q.qlen;
  116. +
  117. + /* fq_codel_drop() is quite expensive, as it performs a linear search
  118. + * in q->backlogs[] to find a fat flow.
  119. + * So instead of dropping a single packet, drop half of its backlog
  120. + * with a 64 packets limit to not add a too big cpu spike here.
  121. */
  122. - if (fq_codel_drop(sch) == idx)
  123. - return NET_XMIT_CN;
  124. + ret = fq_codel_drop(sch, q->drop_batch_size);
  125. +
  126. + q->drop_overlimit += prev_qlen - sch->q.qlen;
  127. - /* As we dropped a packet, better let upper stack know this */
  128. - qdisc_tree_reduce_backlog(sch, 1, prev_backlog - sch->qstats.backlog);
  129. - return NET_XMIT_SUCCESS;
  130. + /* As we dropped packet(s), better let upper stack know this */
  131. + qdisc_tree_reduce_backlog(sch, prev_qlen - sch->q.qlen,
  132. + prev_backlog - sch->qstats.backlog);
  133. +
  134. + return ret == idx ? NET_XMIT_CN : NET_XMIT_SUCCESS;
  135. }
  136. /* This is the specific function called from codel_dequeue()
  137. @@ -323,6 +342,7 @@ static const struct nla_policy fq_codel_
  138. [TCA_FQ_CODEL_FLOWS] = { .type = NLA_U32 },
  139. [TCA_FQ_CODEL_QUANTUM] = { .type = NLA_U32 },
  140. [TCA_FQ_CODEL_CE_THRESHOLD] = { .type = NLA_U32 },
  141. + [TCA_FQ_CODEL_DROP_BATCH_SIZE] = { .type = NLA_U32 },
  142. };
  143. static int fq_codel_change(struct Qdisc *sch, struct nlattr *opt)
  144. @@ -374,6 +394,9 @@ static int fq_codel_change(struct Qdisc
  145. if (tb[TCA_FQ_CODEL_QUANTUM])
  146. q->quantum = max(256U, nla_get_u32(tb[TCA_FQ_CODEL_QUANTUM]));
  147. + if (tb[TCA_FQ_CODEL_DROP_BATCH_SIZE])
  148. + q->drop_batch_size = min(1U, nla_get_u32(tb[TCA_FQ_CODEL_DROP_BATCH_SIZE]));
  149. +
  150. while (sch->q.qlen > sch->limit) {
  151. struct sk_buff *skb = fq_codel_dequeue(sch);
  152. @@ -419,6 +442,7 @@ static int fq_codel_init(struct Qdisc *s
  153. sch->limit = 10*1024;
  154. q->flows_cnt = 1024;
  155. + q->drop_batch_size = 64;
  156. q->quantum = psched_mtu(qdisc_dev(sch));
  157. q->perturbation = prandom_u32();
  158. INIT_LIST_HEAD(&q->new_flows);
  159. @@ -476,6 +500,8 @@ static int fq_codel_dump(struct Qdisc *s
  160. q->cparams.ecn) ||
  161. nla_put_u32(skb, TCA_FQ_CODEL_QUANTUM,
  162. q->quantum) ||
  163. + nla_put_u32(skb, TCA_FQ_CODEL_DROP_BATCH_SIZE,
  164. + q->drop_batch_size) ||
  165. nla_put_u32(skb, TCA_FQ_CODEL_FLOWS,
  166. q->flows_cnt))
  167. goto nla_put_failure;