From a75bcef8677ab567d398faa2ba558809b5940bab Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 24 Aug 2012 22:56:11 +0200 Subject: [PATCH] REORG: buffer: move buffer_flush, b_adv and b_rew to buffer.h These one now operate over real buffers, not channels anymore. --- include/common/buffer.h | 33 +++++++++++++++++++++++++++++++++ include/proto/channel.h | 34 ---------------------------------- src/backend.c | 12 ++++++------ src/channel.c | 8 ++++---- src/proto_http.c | 8 ++++---- src/session.c | 4 ++-- src/stream_interface.c | 2 +- 7 files changed, 50 insertions(+), 51 deletions(-) diff --git a/include/common/buffer.h b/include/common/buffer.h index a51744bb0..b1b7c4607 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -61,6 +61,29 @@ void buffer_bounce_realign(struct buffer *buf); __ret; \ }) +/* Advances the buffer by bytes, which means that the buffer + * pointer advances, and that as many bytes from in are transferred + * to out. The caller is responsible for ensuring that adv is always + * smaller than or equal to b->i. + */ +static inline void b_adv(struct buffer *b, unsigned int adv) +{ + b->i -= adv; + b->o += adv; + b->p = b_ptr(b, adv); +} + +/* Rewinds the buffer by bytes, which means that the buffer pointer goes + * backwards, and that as many bytes from out are moved to in. The caller is + * responsible for ensuring that adv is always smaller than or equal to b->o. + */ +static inline void b_rew(struct buffer *b, unsigned int adv) +{ + b->i += adv; + b->o -= adv; + b->p = b_ptr(b, (int)-adv); +} + /* Returns the start of the input data in a buffer */ static inline char *bi_ptr(const struct buffer *b) { @@ -289,6 +312,16 @@ static inline int buffer_realign(struct buffer *buf) return buffer_contig_space(buf); } +/* Schedule all remaining buffer data to be sent. ->o is not touched if it + * already covers those data. That permits doing a flush even after a forward, + * although not recommended. + */ +static inline void buffer_flush(struct buffer *buf) +{ + buf->p = buffer_wrap_add(buf, buf->p + buf->i); + buf->o += buf->i; + buf->i = 0; +} #endif /* _COMMON_BUFFER_H */ diff --git a/include/proto/channel.h b/include/proto/channel.h index 83881ba8b..4aa6a5722 100644 --- a/include/proto/channel.h +++ b/include/proto/channel.h @@ -158,29 +158,6 @@ static inline int bi_avail(const struct channel *b) return 0; } -/* Advances the buffer by bytes, which means that the buffer - * pointer advances, and that as many bytes from in are transferred - * to out. The caller is responsible for ensuring that adv is always - * smaller than or equal to b->i. - */ -static inline void b_adv(struct channel *b, unsigned int adv) -{ - b->buf.i -= adv; - b->buf.o += adv; - b->buf.p = b_ptr(&b->buf, adv); -} - -/* Rewinds the buffer by bytes, which means that the buffer pointer goes - * backwards, and that as many bytes from out are moved to in. The caller is - * responsible for ensuring that adv is always smaller than or equal to b->o. - */ -static inline void b_rew(struct channel *b, unsigned int adv) -{ - b->buf.i += adv; - b->buf.o -= adv; - b->buf.p = b_ptr(&b->buf, (int)-adv); -} - /* Return the amount of bytes that can be written into the buffer at once, * excluding reserved space, which is preserved. */ @@ -222,17 +199,6 @@ static inline void buffer_check_timeouts(struct channel *b) b->flags |= BF_ANA_TIMEOUT; } -/* Schedule all remaining buffer data to be sent. ->o is not touched if it - * already covers those data. That permits doing a flush even after a forward, - * although not recommended. - */ -static inline void buffer_flush(struct channel *buf) -{ - buf->buf.p = buffer_wrap_add(&buf->buf, buf->buf.p + buf->buf.i); - buf->buf.o += buf->buf.i; - buf->buf.i = 0; -} - /* Erase any content from buffer and adjusts flags accordingly. Note * that any spliced data is not affected since we may not have any access to * it. diff --git a/src/backend.c b/src/backend.c index 998e02c1f..ea0d8949a 100644 --- a/src/backend.c +++ b/src/backend.c @@ -419,12 +419,12 @@ struct server *get_server_rch(struct session *s) args[0].data.str.len = px->hh_len; args[1].type = ARGT_STOP; - b_rew(s->req, rewind = s->req->buf.o); + b_rew(&s->req->buf, rewind = s->req->buf.o); ret = smp_fetch_rdp_cookie(px, s, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, args, &smp); len = smp.data.str.len; - b_adv(s->req, rewind); + b_adv(&s->req->buf, rewind); if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0) return NULL; @@ -905,13 +905,13 @@ static void assign_tproxy_address(struct session *s) ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0; ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0; - b_rew(s->req, rewind = s->req->buf.o); + b_rew(&s->req->buf, rewind = s->req->buf.o); if (http_get_hdr(&s->txn.req, srv->bind_hdr_name, srv->bind_hdr_len, &s->txn.hdr_idx, srv->bind_hdr_occ, NULL, &vptr, &vlen)) { ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = htonl(inetaddr_host_lim(vptr, vptr + vlen)); } - b_adv(s->req, rewind); + b_adv(&s->req->buf, rewind); } break; default: @@ -939,13 +939,13 @@ static void assign_tproxy_address(struct session *s) ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_port = 0; ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = 0; - b_rew(s->req, rewind = s->req->buf.o); + b_rew(&s->req->buf, rewind = s->req->buf.o); if (http_get_hdr(&s->txn.req, s->be->bind_hdr_name, s->be->bind_hdr_len, &s->txn.hdr_idx, s->be->bind_hdr_occ, NULL, &vptr, &vlen)) { ((struct sockaddr_in *)&s->req->cons->addr.from)->sin_addr.s_addr = htonl(inetaddr_host_lim(vptr, vptr + vlen)); } - b_adv(s->req, rewind); + b_adv(&s->req->buf, rewind); } break; default: diff --git a/src/channel.c b/src/channel.c index 825ed7bcb..86fa81130 100644 --- a/src/channel.c +++ b/src/channel.c @@ -60,13 +60,13 @@ unsigned long long buffer_forward(struct channel *buf, unsigned long long bytes) /* OK this amount of bytes might be forwarded at once */ if (!bytes32) return 0; - b_adv(buf, bytes32); + b_adv(&buf->buf, bytes32); return bytes; } } forwarded = buf->buf.i; - b_adv(buf, buf->buf.i); + b_adv(&buf->buf, buf->buf.i); /* Note: the case below is the only case where we may return * a byte count that does not fit into a 32-bit number. @@ -159,7 +159,7 @@ int bi_putchr(struct channel *buf, char c) if (buf->to_forward >= 1) { if (buf->to_forward != BUF_INFINITE_FORWARD) buf->to_forward--; - b_adv(buf, 1); + b_adv(&buf->buf, 1); } buf->total++; @@ -211,7 +211,7 @@ int bi_putblk(struct channel *buf, const char *blk, int len) fwd = buf->to_forward; buf->to_forward -= fwd; } - b_adv(buf, fwd); + b_adv(&buf->buf, fwd); } buf->flags &= ~BF_FULL; diff --git a/src/proto_http.c b/src/proto_http.c index f38e37877..a3b2e9ba3 100644 --- a/src/proto_http.c +++ b/src/proto_http.c @@ -787,12 +787,12 @@ void perform_http_redirect(struct session *s, struct stream_interface *si) * to temporarily rewind the buffer. */ txn = &s->txn; - b_rew(s->req, rewind = s->req->buf.o); + b_rew(&s->req->buf, rewind = s->req->buf.o); path = http_get_path(txn); len = buffer_count(&s->req->buf, path, b_ptr(&s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); - b_adv(s->req, rewind); + b_adv(&s->req->buf, rewind); if (!path) return; @@ -3711,7 +3711,7 @@ int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* sr old_o = req->buf.o; if (old_o) { /* The request was already skipped, let's restore it */ - b_rew(req, old_o); + b_rew(&req->buf, old_o); } old_i = req->buf.i; @@ -3734,7 +3734,7 @@ int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* sr * data to be forwarded in order to take into account the size * variations. */ - b_adv(req, old_o + req->buf.i - old_i); + b_adv(&req->buf, old_o + req->buf.i - old_i); } return 0; diff --git a/src/session.c b/src/session.c index 1aec3bfc0..28dbd1939 100644 --- a/src/session.c +++ b/src/session.c @@ -1856,7 +1856,7 @@ struct task *process_session(struct task *t) buffer_auto_read(s->req); buffer_auto_connect(s->req); buffer_auto_close(s->req); - buffer_flush(s->req); + buffer_flush(&s->req->buf); /* We'll let data flow between the producer (if still connected) * to the consumer (which might possibly not be connected yet). @@ -1991,7 +1991,7 @@ struct task *process_session(struct task *t) */ buffer_auto_read(s->rep); buffer_auto_close(s->rep); - buffer_flush(s->rep); + buffer_flush(&s->rep->buf); /* We'll let data flow between the producer (if still connected) * to the consumer. diff --git a/src/stream_interface.c b/src/stream_interface.c index 2066b06ba..14e2cd5ce 100644 --- a/src/stream_interface.c +++ b/src/stream_interface.c @@ -1071,7 +1071,7 @@ void si_conn_recv_cb(struct connection *conn) fwd = b->to_forward; b->to_forward -= fwd; } - b_adv(b, fwd); + b_adv(&b->buf, fwd); } if (conn->flags & CO_FL_WAIT_L4_CONN)