From: Christopher Faulet Date: Wed, 28 Jan 2026 09:54:41 +0000 (+0100) Subject: MINOR: tree-wide: Use the buffer size instead of global setting when possible X-Git-Tag: v3.4-dev5~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6bf450b7fea1f2a2432bc102b49760e704946039;p=thirdparty%2Fhaproxy.git MINOR: tree-wide: Use the buffer size instead of global setting when possible At many places, we rely on global.tune.bufsize value instead of using the buffer size. For now, it is not a problem. But if we want to be able to deal with buffers of different sizes, it is good to reduce as far as possible dependencies on the global value. most of time, we can use b_size() or c_size() functions. The main change is performed on the error snapshot where the buffer size was added into the error_snapshot structure. --- diff --git a/include/haproxy/channel.h b/include/haproxy/channel.h index 0c872dc31..af2989416 100644 --- a/include/haproxy/channel.h +++ b/include/haproxy/channel.h @@ -787,8 +787,12 @@ static inline int channel_recv_max(const struct channel *chn) */ static inline size_t channel_data_limit(const struct channel *chn) { - size_t max = (global.tune.bufsize - global.tune.maxrewrite); + size_t max; + + if (!c_size(chn)) + return 0; + max = (c_size(chn) - global.tune.maxrewrite); if (IS_HTX_STRM(chn_strm(chn))) max -= HTX_BUF_OVERHEAD; return max; diff --git a/include/haproxy/proxy-t.h b/include/haproxy/proxy-t.h index 20c359126..5f58bdb4c 100644 --- a/include/haproxy/proxy-t.h +++ b/include/haproxy/proxy-t.h @@ -293,7 +293,8 @@ struct error_snapshot { struct server *srv; /* server associated with the error (or NULL) */ /* @64 */ unsigned int ev_id; /* event number (counter incremented for each capture) */ - /* @68: 4 bytes hole here */ + unsigned int buf_size; /* buffer size */ + struct sockaddr_storage src; /* client's address */ /**** protocol-specific part ****/ diff --git a/include/haproxy/sc_strm.h b/include/haproxy/sc_strm.h index 5d473f3b8..bac877dc1 100644 --- a/include/haproxy/sc_strm.h +++ b/include/haproxy/sc_strm.h @@ -133,7 +133,7 @@ static inline void sc_have_room(struct stconn *sc) static inline void sc_need_room(struct stconn *sc, ssize_t room_needed) { sc->flags |= SC_FL_NEED_ROOM; - BUG_ON_HOT(room_needed > (ssize_t)global.tune.bufsize); + BUG_ON_HOT(room_needed > (ssize_t)c_size(sc_ic(sc))); sc->room_needed = room_needed; } diff --git a/src/hlua.c b/src/hlua.c index b826e6ab8..34b93b036 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -3952,7 +3952,7 @@ __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KCon if (!len) goto dup; if (len == -1) - len = global.tune.bufsize; + len = c_size(chn); if (len < 0) { lua_pushfstring(L, "length out of range."); WILL_LJMP(lua_error(L)); @@ -4035,7 +4035,7 @@ __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KCon if (!len) goto dup; if (len == -1) - len = global.tune.bufsize; + len = c_size(chn); if (len < 0) { lua_pushfstring(L, "length out of range."); WILL_LJMP(lua_error(L)); @@ -7470,7 +7470,7 @@ __LJMP static int hlua_http_msg_get_body(lua_State *L) if (!len) goto dup; if (len == -1) - len = global.tune.bufsize; + len = c_size(msg->chn); if (len < 0) { lua_pushfstring(L, "length out of range."); WILL_LJMP(lua_error(L)); diff --git a/src/http_fetch.c b/src/http_fetch.c index b8b6f616c..1f8ff8c99 100644 --- a/src/http_fetch.c +++ b/src/http_fetch.c @@ -138,7 +138,7 @@ static int get_http_auth(struct sample *smp, struct htx *htx) len = base64dec(txn->auth.method_data.area, txn->auth.method_data.data, - http_auth->area, global.tune.bufsize - 1); + http_auth->area, http_auth->size -1); if (len < 0) return 0; diff --git a/src/mux_h1.c b/src/mux_h1.c index 81bd071ba..045fd603a 100644 --- a/src/mux_h1.c +++ b/src/mux_h1.c @@ -4008,8 +4008,8 @@ static int h1_recv(struct h1c *h1c) * a pointer swap and the next read will be doable at once in * an empty buffer. */ - if (max > global.tune.bufsize - global.tune.maxrewrite) - max = global.tune.bufsize - global.tune.maxrewrite; + if (max > b_size(&h1c->ibuf) - global.tune.maxrewrite) + max = b_size(&h1c->ibuf) - global.tune.maxrewrite; } if (max) { diff --git a/src/mux_h2.c b/src/mux_h2.c index a870426dc..41fbbc26d 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -4256,7 +4256,7 @@ static void h2_process_demux(struct h2c *h2c) goto done; } - if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { + if ((int)hdr.len < 0 || (int)hdr.len > b_size(&h2c->dbuf)) { /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ h2c_report_glitch(h2c, 1, "invalid settings frame length"); TRACE_ERROR("invalid settings frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS|H2_EV_PROTO_ERR, h2c->conn); @@ -4303,7 +4303,7 @@ static void h2_process_demux(struct h2c *h2c) break; } - if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { + if ((int)hdr.len < 0 || (int)hdr.len > b_size(&h2c->dbuf)) { h2c_report_glitch(h2c, 1, "invalid H2 frame length"); TRACE_ERROR("invalid H2 frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn); h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); @@ -4384,7 +4384,7 @@ static void h2_process_demux(struct h2c *h2c) h2c->st0 = H2_CS_FRAME_P; /* check for minimum basic frame format validity */ - ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize); + ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, b_size(&h2c->dbuf)); if (ret != H2_ERR_NO_ERROR) { h2c_report_glitch(h2c, 1, "received invalid H2 frame header"); TRACE_ERROR("received invalid H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn); @@ -6066,7 +6066,7 @@ next_frame: goto fail; } - if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) { + if ((unsigned)hdr.len > (unsigned)b_size(&h2c->dbuf)) { /* RFC7540#4.2: invalid frame length */ h2c_report_glitch(h2c, 1, "too large CONTINUATION frame"); TRACE_STATE("too large CONTINUATION frame", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR|H2_EV_RX_CONT|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn); diff --git a/src/payload.c b/src/payload.c index 62071aca6..e1a158355 100644 --- a/src/payload.c +++ b/src/payload.c @@ -1205,7 +1205,6 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw unsigned int len_size = arg_p[1].data.sint; unsigned int buf_offset; unsigned int buf_size = 0; - struct channel *chn = NULL; char *head = NULL; size_t max, data; int i; @@ -1215,12 +1214,15 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */ if (smp->strm) { + struct channel *chn = NULL; + /* meaningless for HTX buffers */ if (IS_HTX_STRM(smp->strm)) return 0; chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; head = ci_head(chn); data = ci_data(chn); + max = c_size(chn); } else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) { struct check *check = __objt_check(smp->sess->origin); @@ -1230,8 +1232,9 @@ smp_fetch_payload_lv(const struct arg *arg_p, struct sample *smp, const char *kw return 0; head = b_head(&check->bi); data = b_data(&check->bi); + max = b_size(&check->bi); } - max = global.tune.bufsize; + if (!head) goto too_short; @@ -1290,6 +1293,7 @@ smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, v chn = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) ? &smp->strm->res : &smp->strm->req; head = ci_head(chn); data = ci_data(chn); + max = c_size(chn); } else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK) { struct check *check = __objt_check(smp->sess->origin); @@ -1299,8 +1303,8 @@ smp_fetch_payload(const struct arg *arg_p, struct sample *smp, const char *kw, v return 0; head = b_head(&check->bi); data = b_data(&check->bi); + max = b_size(&check->bi); } - max = global.tune.bufsize; if (!head) goto too_short; diff --git a/src/proxy.c b/src/proxy.c index 06c3fb877..778c9d9e4 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -4173,6 +4173,7 @@ void proxy_capture_error(struct proxy *proxy, int is_back, if (!es) return; + es->buf_size = buf->size; es->buf_len = buf_len; es->ev_id = ev_id; @@ -5224,7 +5225,7 @@ static int cli_io_handler_show_errors(struct appctx *appctx) es->srv ? es->srv->puid : -1, es->ev_id, pn, port, es->buf_ofs, es->buf_out, - global.tune.bufsize - es->buf_out - es->buf_len, + es->buf_size - es->buf_out - es->buf_len, es->buf_len, es->buf_wrap, es->buf_err); if (es->show) @@ -5250,12 +5251,12 @@ static int cli_io_handler_show_errors(struct appctx *appctx) } /* OK, ptr >= 0, so we have to dump the current line */ - while (ctx->ptr < es->buf_len && ctx->ptr < global.tune.bufsize) { + while (ctx->ptr < es->buf_len && ctx->ptr < es->buf_size) { int newptr; int newline; newline = ctx->bol; - newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, ctx->ptr); + newptr = dump_text_line(&trash, es->buf, es->buf_size, es->buf_len, &newline, ctx->ptr); if (newptr == ctx->ptr) { applet_fl_set(appctx, APPCTX_FL_OUTBLK_FULL); goto cant_send_unlock; diff --git a/src/ssl_sock.c b/src/ssl_sock.c index a7323426c..291aa9467 100644 --- a/src/ssl_sock.c +++ b/src/ssl_sock.c @@ -5918,7 +5918,7 @@ static int ssl_sock_init(struct connection *conn, void **xprt_ctx) * a buffer. */ (!b_is_null(&ctx->early_buf)) ? - global.tune.bufsize - global.tune.maxrewrite : 0); + ctx->early_buf.size - global.tune.maxrewrite : 0); } #endif