]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: channel/buffer: replace buffer_slow_realign() with channel_slow_realign()...
authorWilly Tarreau <w@1wt.eu>
Wed, 6 Jun 2018 04:53:15 +0000 (06:53 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 19 Jul 2018 14:23:40 +0000 (16:23 +0200)
Where relevant, the channel version is used instead. The buffer version
was ported to be more generic and now takes a swap buffer and the output
byte count to know where to set the alignment point. The H2 mux still
uses buffer_slow_realign() with buf->o but it will change later.

include/common/buf.h
include/common/buffer.h
include/proto/channel.h
src/buffer.c
src/hlua.c
src/mux_h2.c
src/proto_http.c

index 564e0ebad3dbd984d48e9e66c19977128a5b50da..fccad45b2f1ec52cd9a42ef4eb0b5d33999c9c51 100644 (file)
@@ -329,6 +329,47 @@ static inline void b_realign_if_empty(struct buffer *b)
                b->p = b->data;
 }
 
+/* b_slow_realign() : this function realigns a possibly wrapping buffer so that
+ * the part remaining to be parsed is contiguous and starts at the beginning of
+ * the buffer and the already parsed output part ends at the end of the buffer.
+ * This provides the best conditions since it allows the largest inputs to be
+ * processed at once and ensures that once the output data leaves, the whole
+ * buffer is available at once. The number of output bytes supposedly present
+ * at the beginning of the buffer and which need to be moved to the end must be
+ * passed in <output>. A temporary swap area at least as large as b->size must
+ * be provided in <swap>. It's up to the caller to ensure <output> is no larger
+ * than the difference between the whole buffer's length and its input.
+ */
+static inline void b_slow_realign(struct buffer *b, char *swap, size_t output)
+{
+       size_t block1 = output;
+       size_t block2 = 0;
+
+       /* process output data in two steps to cover wrapping */
+       if (block1 > b_size(b) - b_head_ofs(b)) {
+               block2 = b_size(b) - b_head_ofs(b);
+               block1 -= block2;
+       }
+       memcpy(swap + b_size(b) - output, b_head(b), block1);
+       memcpy(swap + b_size(b) - block2, b_orig(b), block2);
+
+       /* process input data in two steps to cover wrapping */
+       block1 = b_data(b) - output;
+       block2 = 0;
+
+       if (block1 > b_tail_ofs(b)) {
+               block2 = b_tail_ofs(b);
+               block1 = block1 - block2;
+       }
+       memcpy(swap, b_peek(b, output), block1);
+       memcpy(swap + block1, b_orig(b), block2);
+
+       /* reinject changes into the buffer */
+       memcpy(b_orig(b), swap, b_data(b) - output);
+       memcpy(b_wrap(b) - output, swap + b_size(b) - output, output);
+
+       b->p = b->data;
+}
 
 #endif /* _COMMON_BUF_H */
 
index 0218afa693770d25bd398e3de55f2e1044486cb5..3b34267b0cb10b4465648e2a1e0c1ffbfd42f97d 100644 (file)
@@ -52,7 +52,7 @@ void deinit_buffer();
 int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int len);
 int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len);
 void buffer_dump(FILE *o, struct buffer *b, int from, int to);
-void buffer_slow_realign(struct buffer *buf);
+void buffer_slow_realign(struct buffer *buf, size_t output);
 
 /*****************************************************************/
 /* These functions are used to compute various buffer area sizes */
index 3f30cc0f9c63964eef044aac5a9f78a1a54f37f1..c7135de010c2461e1667e4198a42343d98cd408d 100644 (file)
@@ -701,6 +701,17 @@ static inline void channel_truncate(struct channel *chn)
        chn->buf->i = 0;
 }
 
+/* This function realigns a possibly wrapping channel buffer so that the input
+ * part is contiguous and starts at the beginning of the buffer and the output
+ * part ends at the end of the buffer. This provides the best conditions since
+ * it allows the largest inputs to be processed at once and ensures that once
+ * the output data leaves, the whole buffer is available at once.
+ */
+static inline void channel_slow_realign(struct channel *chn)
+{
+       return buffer_slow_realign(chn->buf, co_data(chn));
+}
+
 /*
  * Advance the channel buffer's read pointer by <len> bytes. This is useful
  * when data have been read directly from the buffer. It is illegal to call
index b6ece4ba67ecc3909943b0d8cf4c16b2a92331e2..becaa1066eb30e2bb9f84877b03a7d555fdbef72 100644 (file)
@@ -172,37 +172,15 @@ int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
  * contiguous and starts at the beginning of the buffer and the output part
  * ends at the end of the buffer. This provides the best conditions since it
  * allows the largest inputs to be processed at once and ensures that once the
- * output data leaves, the whole buffer is available at once.
+ * output data leaves, the whole buffer is available at once. The number of
+ * output bytes supposedly present at the beginning of the buffer and which
+ * need to be moved to the end must be passed in <output>. It's up to the
+ * caller to ensure <output> is no larger than the difference between the
+ * while buffer's length and its input.
  */
-void buffer_slow_realign(struct buffer *buf)
+void buffer_slow_realign(struct buffer *buf, size_t output)
 {
-       int block1 = buf->o;
-       int block2 = 0;
-
-       /* process output data in two steps to cover wrapping */
-       if (block1 > buf->p - buf->data) {
-               block2 = buf->p - buf->data;
-               block1 -= block2;
-       }
-       memcpy(swap_buffer + buf->size - buf->o, bo_ptr(buf), block1);
-       memcpy(swap_buffer + buf->size - block2, buf->data, block2);
-
-       /* process input data in two steps to cover wrapping */
-       block1 = buf->i;
-       block2 = 0;
-
-       if (block1 > buf->data + buf->size - buf->p) {
-               block1 = buf->data + buf->size - buf->p;
-               block2 = buf->i - block1;
-       }
-       memcpy(swap_buffer, bi_ptr(buf), block1);
-       memcpy(swap_buffer + block1, buf->data, block2);
-
-       /* reinject changes into the buffer */
-       memcpy(buf->data, swap_buffer, buf->i);
-       memcpy(buf->data + buf->size - buf->o, swap_buffer + buf->size - buf->o, buf->o);
-
-       buf->p = buf->data;
+       return b_slow_realign(buf, swap_buffer, output);
 }
 
 /*
index 4715639a16ffb52d6eba6c0437d8ceb4603b3a9a..cdb5ff69c673f7abf1a7b77b3952ec63124ebc84 100644 (file)
@@ -3041,7 +3041,7 @@ __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext
         * detects a non contiguous buffer and realign it.
         */
        if (bi_space_for_replace(chn->buf) < max)
-               buffer_slow_realign(chn->buf);
+               channel_slow_realign(chn);
 
        /* Copy input data in the buffer. */
        max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max);
index 1eba104b3d72d1f99b7a874d5a9088dfee44d776..32103026131dca9a7191831584409d54efaf0c7f 100644 (file)
@@ -2688,7 +2688,7 @@ static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
                /* it doesn't fit and the buffer is fragmented,
                 * so let's defragment it and try again.
                 */
-               buffer_slow_realign(buf);
+               buffer_slow_realign(buf, 0);
        }
 
        /* first check if we have some room after p+i */
@@ -2995,7 +2995,7 @@ static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
                if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
                        break;
        realign_again:
-               buffer_slow_realign(h2c->mbuf);
+               buffer_slow_realign(h2c->mbuf, h2c->mbuf->o);
        }
 
        if (outbuf.size < 9) {
@@ -3153,7 +3153,7 @@ static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
                if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
                        break;
        realign_again:
-               buffer_slow_realign(h2c->mbuf);
+               buffer_slow_realign(h2c->mbuf, h2c->mbuf->o);
        }
 
        if (outbuf.size < 9) {
index c447929c85429440020e2631654e7f6acb2155f9..a4db33a48ebcec3703f557a9bef6733b7d2ea94b 100644 (file)
@@ -1652,7 +1652,7 @@ int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
                        }
                        if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
                                     bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
-                               buffer_slow_realign(req->buf);
+                               channel_slow_realign(req);
                }
 
                if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
@@ -5139,7 +5139,7 @@ int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
 
                if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
                             bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
-                       buffer_slow_realign(rep->buf);
+                       channel_slow_realign(rep);
 
                if (likely(msg->next < rep->buf->i))
                        http_msg_analyzer(msg, &txn->hdr_idx);
@@ -9517,7 +9517,7 @@ int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
                 */
                if (s->req.buf->p > s->req.buf->data &&
                    s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
-                       buffer_slow_realign(s->req.buf);
+                       channel_slow_realign(&s->req);
 
                if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
                        if (msg->msg_state == HTTP_MSG_ERROR)