You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
8.4 KiB

  1. commit 5a9c875f0f1ee83bd5889dd1ad53e9da43e6c34e
  2. Author: Willy Tarreau <w@1wt.eu>
  3. Date: Fri Aug 2 07:52:08 2019 +0200
  4. BUG/MEDIUM: mux-h2: split the stream's and connection's window sizes
  5. The SETTINGS frame parser updates all streams' window for each
  6. INITIAL_WINDOW_SIZE setting received on the connection (like h2spec
  7. does in test 6.5.3), which can start to be expensive if repeated when
  8. there are many streams (up to 100 by default). A quick test shows that
  9. it's possible to parse only 35000 settings per second on a 3 GHz core
  10. for 100 streams, which is rather small.
  11. Given that window sizes are relative and may be negative, there's no
  12. point in pre-initializing them for each stream and update them from
  13. the settings. Instead, let's make them relative to the connection's
  14. initial window size so that any change immediately affects all streams.
  15. The only thing that remains needed is to wake up the streams that were
  16. unblocked by the update, which is now done once at the end of
  17. h2_process_demux() instead of once per setting. This now results in
  18. 5.7 million settings being processed per second, which is way better.
  19. In order to keep the change small, the h2s' mws field was renamed to
  20. "sws" for "stream window size", and an h2s_mws() function was added
  21. to add it to the connection's initial window setting and determine the
  22. window size to use when muxing. The h2c_update_all_ws() function was
  23. renamed to h2c_unblock_sfctl() since it's now only used to unblock
  24. previously blocked streams.
  25. This needs to be backported to all versions till 1.8.
  26. (cherry picked from commit 1d4a0f88100daeb17dd0c9470c659b1ec288bc07)
  27. [wt: context adjustment, port to legacy parts]
  28. Signed-off-by: Willy Tarreau <w@1wt.eu>
  29. diff --git a/src/mux_h2.c b/src/mux_h2.c
  30. index d605fe94..f90e9435 100644
  31. --- a/src/mux_h2.c
  32. +++ b/src/mux_h2.c
  33. @@ -208,7 +208,7 @@ struct h2s {
  34. struct eb32_node by_id; /* place in h2c's streams_by_id */
  35. int32_t id; /* stream ID */
  36. uint32_t flags; /* H2_SF_* */
  37. - int mws; /* mux window size for this stream */
  38. + int sws; /* stream window size, to be added to the mux's initial window size */
  39. enum h2_err errcode; /* H2 err code (H2_ERR_*) */
  40. enum h2_ss st;
  41. uint16_t status; /* HTTP response status */
  42. @@ -707,6 +707,14 @@ static inline __maybe_unused int h2s_id(const struct h2s *h2s)
  43. return h2s ? h2s->id : 0;
  44. }
  45. +/* returns the sum of the stream's own window size and the mux's initial
  46. + * window, which together form the stream's effective window size.
  47. + */
  48. +static inline int h2s_mws(const struct h2s *h2s)
  49. +{
  50. + return h2s->sws + h2s->h2c->miw;
  51. +}
  52. +
  53. /* returns true of the mux is currently busy as seen from stream <h2s> */
  54. static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
  55. {
  56. @@ -945,7 +953,7 @@ static struct h2s *h2s_new(struct h2c *h2c, int id)
  57. LIST_INIT(&h2s->sending_list);
  58. h2s->h2c = h2c;
  59. h2s->cs = NULL;
  60. - h2s->mws = h2c->miw;
  61. + h2s->sws = 0;
  62. h2s->flags = H2_SF_NONE;
  63. h2s->errcode = H2_ERR_NO_ERROR;
  64. h2s->st = H2_SS_IDLE;
  65. @@ -1543,30 +1551,23 @@ static void h2_wake_some_streams(struct h2c *h2c, int last)
  66. }
  67. }
  68. -/* Increase all streams' outgoing window size by the difference passed in
  69. - * argument. This is needed upon receipt of the settings frame if the initial
  70. - * window size is different. The difference may be negative and the resulting
  71. - * window size as well, for the time it takes to receive some window updates.
  72. +/* Wake up all blocked streams whose window size has become positive after the
  73. + * mux's initial window was adjusted. This should be done after having processed
  74. + * SETTINGS frames which have updated the mux's initial window size.
  75. */
  76. -static void h2c_update_all_ws(struct h2c *h2c, int diff)
  77. +static void h2c_unblock_sfctl(struct h2c *h2c)
  78. {
  79. struct h2s *h2s;
  80. struct eb32_node *node;
  81. - if (!diff)
  82. - return;
  83. -
  84. node = eb32_first(&h2c->streams_by_id);
  85. while (node) {
  86. h2s = container_of(node, struct h2s, by_id);
  87. - h2s->mws += diff;
  88. -
  89. - if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
  90. + if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
  91. h2s->flags &= ~H2_SF_BLK_SFCTL;
  92. if (h2s->send_wait && !LIST_ADDED(&h2s->list))
  93. LIST_ADDQ(&h2c->send_list, &h2s->list);
  94. }
  95. -
  96. node = eb32_next(node);
  97. }
  98. }
  99. @@ -1607,7 +1608,6 @@ static int h2c_handle_settings(struct h2c *h2c)
  100. error = H2_ERR_FLOW_CONTROL_ERROR;
  101. goto fail;
  102. }
  103. - h2c_update_all_ws(h2c, arg - h2c->miw);
  104. h2c->miw = arg;
  105. break;
  106. case H2_SETTINGS_MAX_FRAME_SIZE:
  107. @@ -1869,13 +1869,13 @@ static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
  108. goto strm_err;
  109. }
  110. - if (h2s->mws >= 0 && h2s->mws + inc < 0) {
  111. + if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
  112. error = H2_ERR_FLOW_CONTROL_ERROR;
  113. goto strm_err;
  114. }
  115. - h2s->mws += inc;
  116. - if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
  117. + h2s->sws += inc;
  118. + if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
  119. h2s->flags &= ~H2_SF_BLK_SFCTL;
  120. if (h2s->send_wait && !LIST_ADDED(&h2s->list))
  121. LIST_ADDQ(&h2c->send_list, &h2s->list);
  122. @@ -2237,6 +2237,7 @@ static void h2_process_demux(struct h2c *h2c)
  123. struct h2s *h2s = NULL, *tmp_h2s;
  124. struct h2_fh hdr;
  125. unsigned int padlen = 0;
  126. + int32_t old_iw = h2c->miw;
  127. if (h2c->st0 >= H2_CS_ERROR)
  128. return;
  129. @@ -2625,6 +2626,9 @@ static void h2_process_demux(struct h2c *h2c)
  130. h2s_notify_recv(h2s);
  131. }
  132. + if (old_iw != h2c->miw)
  133. + h2c_unblock_sfctl(h2c);
  134. +
  135. h2c_restart_reading(h2c, 0);
  136. }
  137. @@ -4259,8 +4263,8 @@ static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf,
  138. if (size > max)
  139. size = max;
  140. - if (size > h2s->mws)
  141. - size = h2s->mws;
  142. + if (size > h2s_mws(h2s))
  143. + size = h2s_mws(h2s);
  144. if (size <= 0) {
  145. h2s->flags |= H2_SF_BLK_SFCTL;
  146. @@ -4362,7 +4366,7 @@ static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf,
  147. ofs += size;
  148. total += size;
  149. h1m->curr_len -= size;
  150. - h2s->mws -= size;
  151. + h2s->sws -= size;
  152. h2c->mws -= size;
  153. if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
  154. @@ -4390,7 +4394,7 @@ static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf,
  155. }
  156. end:
  157. - trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
  158. + trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s_mws(h2s), (unsigned int)b_data(buf));
  159. return total;
  160. }
  161. @@ -4937,7 +4941,7 @@ static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, si
  162. */
  163. if (unlikely(fsize == count &&
  164. htx->used == 1 && type == HTX_BLK_DATA &&
  165. - fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
  166. + fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
  167. void *old_area = mbuf->area;
  168. if (b_data(mbuf)) {
  169. @@ -4972,7 +4976,7 @@ static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, si
  170. h2_set_frame_size(outbuf.area, fsize);
  171. /* update windows */
  172. - h2s->mws -= fsize;
  173. + h2s->sws -= fsize;
  174. h2c->mws -= fsize;
  175. /* and exchange with our old area */
  176. @@ -5024,7 +5028,7 @@ static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, si
  177. if (!fsize)
  178. goto send_empty;
  179. - if (h2s->mws <= 0) {
  180. + if (h2s_mws(h2s) <= 0) {
  181. h2s->flags |= H2_SF_BLK_SFCTL;
  182. if (LIST_ADDED(&h2s->list))
  183. LIST_DEL_INIT(&h2s->list);
  184. @@ -5034,8 +5038,8 @@ static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, si
  185. if (fsize > count)
  186. fsize = count;
  187. - if (fsize > h2s->mws)
  188. - fsize = h2s->mws; // >0
  189. + if (fsize > h2s_mws(h2s))
  190. + fsize = h2s_mws(h2s); // >0
  191. if (h2c->mfs && fsize > h2c->mfs)
  192. fsize = h2c->mfs; // >0
  193. @@ -5071,7 +5075,7 @@ static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, si
  194. /* now let's copy this this into the output buffer */
  195. memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
  196. - h2s->mws -= fsize;
  197. + h2s->sws -= fsize;
  198. h2c->mws -= fsize;
  199. count -= fsize;