2006-06-25 20:48:02 -04:00
/*
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* Stream management functions .
2006-06-25 20:48:02 -04:00
*
2012-04-19 13:28:33 -04:00
* Copyright 2000 - 2012 Willy Tarreau < w @ 1 wt . eu >
2006-06-25 20:48:02 -04:00
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version
* 2 of the License , or ( at your option ) any later version .
*
*/
# include <stdlib.h>
2010-06-01 11:45:26 -04:00
# include <unistd.h>
2006-06-29 12:54:54 -04:00
2020-06-04 16:29:18 -04:00
# include <import/ebistree.h>
2020-06-04 13:11:43 -04:00
# include <haproxy/acl.h>
2020-06-04 04:15:32 -04:00
# include <haproxy/action.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/activity.h>
# include <haproxy/api.h>
2020-06-04 13:42:41 -04:00
# include <haproxy/applet.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/arg.h>
2020-06-04 16:50:02 -04:00
# include <haproxy/backend.h>
2020-06-04 05:18:28 -04:00
# include <haproxy/capture.h>
2020-06-04 18:00:29 -04:00
# include <haproxy/cfgparse.h>
2020-06-04 15:07:02 -04:00
# include <haproxy/channel.h>
2020-06-04 12:21:56 -04:00
# include <haproxy/check.h>
2020-06-04 14:19:54 -04:00
# include <haproxy/cli.h>
2020-06-04 12:02:10 -04:00
# include <haproxy/connection.h>
2020-06-03 12:23:19 -04:00
# include <haproxy/dict.h>
2020-06-02 05:28:02 -04:00
# include <haproxy/dynbuf.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/fd.h>
2020-06-04 15:29:29 -04:00
# include <haproxy/filters.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/freq_ctr.h>
2020-06-04 05:23:07 -04:00
# include <haproxy/frontend.h>
2020-06-04 11:05:57 -04:00
# include <haproxy/global.h>
2020-06-04 03:20:54 -04:00
# include <haproxy/hlua.h>
2020-06-04 15:21:03 -04:00
# include <haproxy/http_ana.h>
2020-06-04 05:40:28 -04:00
# include <haproxy/http_rules.h>
2020-06-03 02:44:35 -04:00
# include <haproxy/htx.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/istbuf.h>
2020-06-04 16:01:04 -04:00
# include <haproxy/log.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/pipe.h>
2020-06-02 03:38:52 -04:00
# include <haproxy/pool.h>
2020-06-04 16:29:18 -04:00
# include <haproxy/proxy.h>
2020-06-04 16:59:39 -04:00
# include <haproxy/queue.h>
2022-05-27 03:25:10 -04:00
# include <haproxy/sc_strm.h>
2020-06-04 17:20:13 -04:00
# include <haproxy/server.h>
2021-02-12 13:42:55 -05:00
# include <haproxy/resolvers.h>
2020-12-10 07:43:57 -05:00
# include <haproxy/sample.h>
2020-06-04 12:58:52 -04:00
# include <haproxy/session.h>
2020-06-04 13:58:55 -04:00
# include <haproxy/stats-t.h>
2022-05-27 03:47:12 -04:00
# include <haproxy/stconn.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/stick_table.h>
2020-06-04 17:46:14 -04:00
# include <haproxy/stream.h>
2020-06-04 11:25:40 -04:00
# include <haproxy/task.h>
2020-06-04 11:42:48 -04:00
# include <haproxy/tcp_rules.h>
2020-06-09 03:07:15 -04:00
# include <haproxy/thread.h>
2022-09-14 11:45:41 -04:00
# include <haproxy/tools.h>
2020-06-04 17:46:14 -04:00
# include <haproxy/trace.h>
2020-06-04 10:25:31 -04:00
# include <haproxy/vars.h>
2006-06-25 20:48:02 -04:00
2018-11-26 05:58:30 -05:00
DECLARE_POOL ( pool_head_stream , " stream " , sizeof ( struct stream ) ) ;
2020-02-28 09:13:33 -05:00
DECLARE_POOL ( pool_head_uniqueid , " uniqueid " , UNIQUEID_LEN ) ;
2018-11-26 05:58:30 -05:00
2021-02-24 05:29:51 -05:00
/* incremented by each "show sess" to fix a delimiter between streams */
unsigned stream_epoch = 0 ;
2006-06-25 20:48:02 -04:00
2015-09-27 13:29:33 -04:00
/* List of all use-service keywords. */
static struct list service_keywords = LIST_HEAD_INIT ( service_keywords ) ;
2017-08-28 11:18:36 -04:00
2019-11-05 10:18:10 -05:00
/* trace source and events */
static void strm_trace ( enum trace_level level , uint64_t mask ,
const struct trace_source * src ,
const struct ist where , const struct ist func ,
const void * a1 , const void * a2 , const void * a3 , const void * a4 ) ;
/* The event representation is split like this :
* strm - stream
2022-05-27 04:13:37 -04:00
* sc - stream connector
2025-04-02 14:44:22 -04:00
* http - http analysis
* tcp - tcp analysis
2019-11-05 10:18:10 -05:00
*
* STRM_EV_ * macros are defined in < proto / stream . h >
*/
static const struct trace_event strm_trace_events [ ] = {
{ . mask = STRM_EV_STRM_NEW , . name = " strm_new " , . desc = " new stream " } ,
{ . mask = STRM_EV_STRM_FREE , . name = " strm_free " , . desc = " release stream " } ,
{ . mask = STRM_EV_STRM_ERR , . name = " strm_err " , . desc = " error during stream processing " } ,
{ . mask = STRM_EV_STRM_ANA , . name = " strm_ana " , . desc = " stream analyzers " } ,
{ . mask = STRM_EV_STRM_PROC , . name = " strm_proc " , . desc = " stream processing " } ,
2022-05-27 03:03:30 -04:00
{ . mask = STRM_EV_CS_ST , . name = " sc_state " , . desc = " processing connector states " } ,
2019-11-05 10:18:10 -05:00
{ . mask = STRM_EV_HTTP_ANA , . name = " http_ana " , . desc = " HTTP analyzers " } ,
2025-04-02 14:44:22 -04:00
{ . mask = STRM_EV_HTTP_ERR , . name = " http_err " , . desc = " error during HTTP analysis " } ,
2019-11-05 10:18:10 -05:00
{ . mask = STRM_EV_TCP_ANA , . name = " tcp_ana " , . desc = " TCP analyzers " } ,
2025-04-02 14:44:22 -04:00
{ . mask = STRM_EV_TCP_ERR , . name = " tcp_err " , . desc = " error during TCP analysis " } ,
2022-03-08 09:47:02 -05:00
{ . mask = STRM_EV_FLT_ANA , . name = " flt_ana " , . desc = " Filter analyzers " } ,
2025-04-02 14:44:22 -04:00
{ . mask = STRM_EV_FLT_ERR , . name = " flt_err " , . desc = " error during filter analysis " } ,
2019-11-05 10:18:10 -05:00
{ }
} ;
static const struct name_desc strm_trace_lockon_args [ 4 ] = {
/* arg1 */ { /* already used by the stream */ } ,
/* arg2 */ { } ,
/* arg3 */ { } ,
/* arg4 */ { }
} ;
static const struct name_desc strm_trace_decoding [ ] = {
# define STRM_VERB_CLEAN 1
{ . name = " clean " , . desc = " only user-friendly stuff, generally suitable for level \" user \" " } ,
# define STRM_VERB_MINIMAL 2
2022-05-17 13:07:51 -04:00
{ . name = " minimal " , . desc = " report info on streams and connectors " } ,
2019-11-05 10:18:10 -05:00
# define STRM_VERB_SIMPLE 3
{ . name = " simple " , . desc = " add info on request and response channels " } ,
# define STRM_VERB_ADVANCED 4
{ . name = " advanced " , . desc = " add info on channel's buffer for data and developer levels only " } ,
# define STRM_VERB_COMPLETE 5
{ . name = " complete " , . desc = " add info on channel's buffer " } ,
{ /* end */ }
} ;
struct trace_source trace_strm = {
. name = IST ( " stream " ) ,
. desc = " Applicative stream " ,
. arg_def = TRC_ARG1_STRM , // TRACE()'s first argument is always a stream
. default_cb = strm_trace ,
. known_events = strm_trace_events ,
. lockon_args = strm_trace_lockon_args ,
. decoding = strm_trace_decoding ,
. report_events = ~ 0 , // report everything by default
} ;
# define TRACE_SOURCE &trace_strm
INITCALL1 ( STG_REGISTER , trace_register_source , TRACE_SOURCE ) ;
/* the stream traces always expect that arg1, if non-null, is of a stream (from
* which we can derive everything ) , that arg2 , if non - null , is an http
* transaction , that arg3 , if non - null , is an http message .
*/
static void strm_trace ( enum trace_level level , uint64_t mask , const struct trace_source * src ,
const struct ist where , const struct ist func ,
const void * a1 , const void * a2 , const void * a3 , const void * a4 )
{
const struct stream * s = a1 ;
const struct http_txn * txn = a2 ;
const struct http_msg * msg = a3 ;
struct task * task ;
const struct channel * req , * res ;
struct htx * htx ;
if ( ! s | | src - > verbosity < STRM_VERB_CLEAN )
return ;
task = s - > task ;
req = & s - > req ;
res = & s - > res ;
htx = ( msg ? htxbuf ( & msg - > chn - > buf ) : NULL ) ;
/* General info about the stream (htx/tcp, id...) */
chunk_appendf ( & trace_buf , " : [%u,%s] " ,
s - > uniq_id , ( ( s - > flags & SF_HTX ) ? " HTX " : " TCP " ) ) ;
2020-03-05 14:19:02 -05:00
if ( isttest ( s - > unique_id ) ) {
chunk_appendf ( & trace_buf , " id= " ) ;
b_putist ( & trace_buf , s - > unique_id ) ;
}
2019-11-05 10:18:10 -05:00
2022-05-17 13:07:51 -04:00
/* Front and back stream connector state */
2022-05-27 05:23:05 -04:00
chunk_appendf ( & trace_buf , " SC=(%s,%s) " ,
2022-05-27 03:03:30 -04:00
sc_state_str ( s - > scf - > state ) , sc_state_str ( s - > scb - > state ) ) ;
2019-11-05 10:18:10 -05:00
/* If txn is defined, HTTP req/rep states */
if ( txn )
chunk_appendf ( & trace_buf , " HTTP=(%s,%s) " ,
h1_msg_state_str ( txn - > req . msg_state ) , h1_msg_state_str ( txn - > rsp . msg_state ) ) ;
if ( msg )
chunk_appendf ( & trace_buf , " %s " , ( ( msg - > chn - > flags & CF_ISRESP ) ? " RESPONSE " : " REQUEST " ) ) ;
if ( src - > verbosity = = STRM_VERB_CLEAN )
return ;
/* If msg defined, display status-line if possible (verbosity > MINIMAL) */
if ( src - > verbosity > STRM_VERB_MINIMAL & & htx & & htx_nbblks ( htx ) ) {
2022-05-30 10:27:48 -04:00
const struct htx_blk * blk = __htx_get_head_blk ( htx ) ;
2019-11-05 10:18:10 -05:00
const struct htx_sl * sl = htx_get_blk_ptr ( htx , blk ) ;
enum htx_blk_type type = htx_get_blk_type ( blk ) ;
if ( type = = HTX_BLK_REQ_SL | | type = = HTX_BLK_RES_SL )
chunk_appendf ( & trace_buf , " - \" %.*s %.*s %.*s \" " ,
HTX_SL_P1_LEN ( sl ) , HTX_SL_P1_PTR ( sl ) ,
HTX_SL_P2_LEN ( sl ) , HTX_SL_P2_PTR ( sl ) ,
HTX_SL_P3_LEN ( sl ) , HTX_SL_P3_PTR ( sl ) ) ;
}
2023-02-16 08:35:51 -05:00
chunk_appendf ( & trace_buf , " - t=%p t.exp=%d s=(%p,0x%08x,0x%x) " ,
task , tick_isset ( task - > expire ) ? TICKS_TO_MS ( task - > expire - now_ms ) : TICK_ETERNITY , s , s - > flags , s - > conn_err_type ) ;
2019-11-05 10:18:10 -05:00
/* If txn defined info about HTTP msgs, otherwise info about SI. */
if ( txn ) {
2023-02-16 08:35:51 -05:00
chunk_appendf ( & trace_buf , " txn.flags=0x%08x, http.flags=(0x%08x,0x%08x) status=%d " ,
txn - > flags , txn - > req . flags , txn - > rsp . flags , txn - > status ) ;
2019-11-05 10:18:10 -05:00
}
else {
2023-02-16 08:35:51 -05:00
chunk_appendf ( & trace_buf , " scf=(%p,%d,0x%08x,0x%x) scb=(%p,%d,0x%08x,0x%x) scf.exp(r,w)=(%d,%d) scb.exp(r,w)=(%d,%d) retries=%d " ,
2023-02-21 12:00:25 -05:00
s - > scf , s - > scf - > state , s - > scf - > flags , s - > scf - > sedesc - > flags ,
s - > scb , s - > scb - > state , s - > scb - > flags , s - > scb - > sedesc - > flags ,
2023-02-16 08:35:51 -05:00
tick_isset ( sc_ep_rcv_ex ( s - > scf ) ) ? TICKS_TO_MS ( sc_ep_rcv_ex ( s - > scf ) - now_ms ) : TICK_ETERNITY ,
tick_isset ( sc_ep_snd_ex ( s - > scf ) ) ? TICKS_TO_MS ( sc_ep_snd_ex ( s - > scf ) - now_ms ) : TICK_ETERNITY ,
tick_isset ( sc_ep_rcv_ex ( s - > scb ) ) ? TICKS_TO_MS ( sc_ep_rcv_ex ( s - > scb ) - now_ms ) : TICK_ETERNITY ,
tick_isset ( sc_ep_snd_ex ( s - > scb ) ) ? TICKS_TO_MS ( sc_ep_snd_ex ( s - > scb ) - now_ms ) : TICK_ETERNITY ,
2022-04-04 05:08:42 -04:00
s - > conn_retries ) ;
2019-11-05 10:18:10 -05:00
}
if ( src - > verbosity = = STRM_VERB_MINIMAL )
return ;
/* If txn defined, don't display all channel info */
if ( src - > verbosity = = STRM_VERB_SIMPLE | | txn ) {
2023-02-16 08:35:51 -05:00
chunk_appendf ( & trace_buf , " req=(%p .fl=0x%08x .exp=%d) " ,
req , req - > flags , tick_isset ( req - > analyse_exp ) ? TICKS_TO_MS ( req - > analyse_exp - now_ms ) : TICK_ETERNITY ) ;
chunk_appendf ( & trace_buf , " res=(%p .fl=0x%08x .exp=%d) " ,
res , res - > flags , tick_isset ( res - > analyse_exp ) ? TICKS_TO_MS ( res - > analyse_exp - now_ms ) : TICK_ETERNITY ) ;
2019-11-05 10:18:10 -05:00
}
else {
2023-02-07 10:06:14 -05:00
chunk_appendf ( & trace_buf , " req=(%p .fl=0x%08x .ana=0x%08x .exp=%u .o=%lu .tot=%llu .to_fwd=%u) " ,
req , req - > flags , req - > analysers , req - > analyse_exp ,
BUILD/MINOR: trace: fix use of long type in a few printf format strings
Building on a 32-bit platform produces these warnings in trace code:
src/stream.c: In function 'strm_trace':
src/stream.c:226:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 9 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " req=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
^
src/stream.c:229:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 9 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " res=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
^
src/mux_fcgi.c: In function 'fcgi_trace':
src/mux_fcgi.c:443:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " - VAL=%lu", *val);
^
src/mux_h1.c: In function 'h1_trace':
src/mux_h1.c:290:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " - VAL=%lu", *val);
^
Let's just cast the type to long. This should be backported to 2.1.
2019-11-27 09:41:31 -05:00
( long ) req - > output , req - > total , req - > to_forward ) ;
2023-02-07 10:06:14 -05:00
chunk_appendf ( & trace_buf , " res=(%p .fl=0x%08x .ana=0x%08x .exp=%u .o=%lu .tot=%llu .to_fwd=%u) " ,
res , res - > flags , res - > analysers , res - > analyse_exp ,
BUILD/MINOR: trace: fix use of long type in a few printf format strings
Building on a 32-bit platform produces these warnings in trace code:
src/stream.c: In function 'strm_trace':
src/stream.c:226:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 9 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " req=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
^
src/stream.c:229:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 9 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " res=(%p .fl=0x%08x .ana=0x%08x .exp(r,w,a)=(%u,%u,%u) .o=%lu .tot=%llu .to_fwd=%u)",
^
src/mux_fcgi.c: In function 'fcgi_trace':
src/mux_fcgi.c:443:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " - VAL=%lu", *val);
^
src/mux_h1.c: In function 'h1_trace':
src/mux_h1.c:290:29: warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'size_t {aka const unsigned int}' [-Wformat=]
chunk_appendf(&trace_buf, " - VAL=%lu", *val);
^
Let's just cast the type to long. This should be backported to 2.1.
2019-11-27 09:41:31 -05:00
( long ) res - > output , res - > total , res - > to_forward ) ;
2019-11-05 10:18:10 -05:00
}
if ( src - > verbosity = = STRM_VERB_SIMPLE | |
( src - > verbosity = = STRM_VERB_ADVANCED & & src - > level < TRACE_LEVEL_DATA ) )
return ;
/* channels' buffer info */
if ( s - > flags & SF_HTX ) {
struct htx * rqhtx = htxbuf ( & req - > buf ) ;
struct htx * rphtx = htxbuf ( & res - > buf ) ;
chunk_appendf ( & trace_buf , " htx=(%u/%u#%u, %u/%u#%u) " ,
rqhtx - > data , rqhtx - > size , htx_nbblks ( rqhtx ) ,
rphtx - > data , rphtx - > size , htx_nbblks ( rphtx ) ) ;
}
else {
chunk_appendf ( & trace_buf , " buf=(%u@%p+%u/%u, %u@%p+%u/%u) " ,
( unsigned int ) b_data ( & req - > buf ) , b_orig ( & req - > buf ) ,
( unsigned int ) b_head_ofs ( & req - > buf ) , ( unsigned int ) b_size ( & req - > buf ) ,
2022-03-08 09:48:55 -05:00
( unsigned int ) b_data ( & res - > buf ) , b_orig ( & res - > buf ) ,
( unsigned int ) b_head_ofs ( & res - > buf ) , ( unsigned int ) b_size ( & res - > buf ) ) ;
2019-11-05 10:18:10 -05:00
}
/* If msg defined, display htx info if defined (level > USER) */
if ( src - > level > TRACE_LEVEL_USER & & htx & & htx_nbblks ( htx ) ) {
int full = 0 ;
/* Full htx info (level > STATE && verbosity > SIMPLE) */
if ( src - > level > TRACE_LEVEL_STATE ) {
if ( src - > verbosity = = STRM_VERB_COMPLETE )
full = 1 ;
}
chunk_memcat ( & trace_buf , " \n \t " , 2 ) ;
htx_dump ( & trace_buf , htx , full ) ;
}
}
2022-05-27 04:13:37 -04:00
/* Upgrade an existing stream for stream connector <sc>. Return < 0 on error. This
2021-12-20 09:34:16 -05:00
* is only valid right after a TCP to H1 upgrade . The stream should be
* " reativated " by removing SF_IGNORE flag . And the right mode must be set . On
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
* success , < input > buffer is transferred to the stream and thus points to
* BUF_NULL . On error , it is unchanged and it is the caller responsibility to
MINOR: stream: Add a function to validate TCP to H1 upgrades
TCP to H1 upgrades are buggy for now. When such upgrade is performed, a
crash is experienced. The bug is the result of the recent H1 mux
refactoring, and more specifically because of the commit c4bfa59f1 ("MAJOR:
mux-h1: Create the client stream as later as possible"). Indeed, now the H1
mux is responsible to create the frontend conn-stream once the request
headers are fully received. Thus the TCP to H1 upgrade is a problem because
the frontend conn-stream already exists.
To fix the bug, we must keep this conn-stream and the associate stream and
use it in the H1 mux. To do so, the upgrade will be performed in two
steps. First, the mux is upgraded from mux-pt to mux-h1. Then, the mux-h1
performs the stream upgrade, once the request headers are fully received and
parsed. To do so, stream_upgrade_from_cs() must be used. This function set
the SF_HTX flags to switch the stream to HTX mode, it removes the SF_IGNORE
flags and eventually it fills the request channel with some input data.
This patch is required to fix the TCP to H1 upgrades and is intimately
linked with the next commits.
2021-01-21 11:36:12 -05:00
* release it ( this never happens for now ) .
*/
2022-05-27 05:11:15 -04:00
int stream_upgrade_from_sc ( struct stconn * sc , struct buffer * input )
MINOR: stream: Add a function to validate TCP to H1 upgrades
TCP to H1 upgrades are buggy for now. When such upgrade is performed, a
crash is experienced. The bug is the result of the recent H1 mux
refactoring, and more specifically because of the commit c4bfa59f1 ("MAJOR:
mux-h1: Create the client stream as later as possible"). Indeed, now the H1
mux is responsible to create the frontend conn-stream once the request
headers are fully received. Thus the TCP to H1 upgrade is a problem because
the frontend conn-stream already exists.
To fix the bug, we must keep this conn-stream and the associate stream and
use it in the H1 mux. To do so, the upgrade will be performed in two
steps. First, the mux is upgraded from mux-pt to mux-h1. Then, the mux-h1
performs the stream upgrade, once the request headers are fully received and
parsed. To do so, stream_upgrade_from_cs() must be used. This function set
the SF_HTX flags to switch the stream to HTX mode, it removes the SF_IGNORE
flags and eventually it fills the request channel with some input data.
This patch is required to fix the TCP to H1 upgrades and is intimately
linked with the next commits.
2021-01-21 11:36:12 -05:00
{
2022-05-27 04:13:37 -04:00
struct stream * s = __sc_strm ( sc ) ;
const struct mux_ops * mux = sc_mux_ops ( sc ) ;
2021-12-17 11:28:35 -05:00
2022-02-28 03:09:05 -05:00
if ( mux ) {
2021-12-20 09:34:16 -05:00
if ( mux - > flags & MX_FL_HTX )
s - > flags | = SF_HTX ;
}
MINOR: stream: Add a function to validate TCP to H1 upgrades
TCP to H1 upgrades are buggy for now. When such upgrade is performed, a
crash is experienced. The bug is the result of the recent H1 mux
refactoring, and more specifically because of the commit c4bfa59f1 ("MAJOR:
mux-h1: Create the client stream as later as possible"). Indeed, now the H1
mux is responsible to create the frontend conn-stream once the request
headers are fully received. Thus the TCP to H1 upgrade is a problem because
the frontend conn-stream already exists.
To fix the bug, we must keep this conn-stream and the associate stream and
use it in the H1 mux. To do so, the upgrade will be performed in two
steps. First, the mux is upgraded from mux-pt to mux-h1. Then, the mux-h1
performs the stream upgrade, once the request headers are fully received and
parsed. To do so, stream_upgrade_from_cs() must be used. This function set
the SF_HTX flags to switch the stream to HTX mode, it removes the SF_IGNORE
flags and eventually it fills the request channel with some input data.
This patch is required to fix the TCP to H1 upgrades and is intimately
linked with the next commits.
2021-01-21 11:36:12 -05:00
if ( ! b_is_null ( input ) ) {
/* Xfer the input buffer to the request channel. <input> will
* than point to BUF_NULL . From this point , it is the stream
* responsibility to release it .
*/
s - > req . buf = * input ;
* input = BUF_NULL ;
s - > req . total = ( IS_HTX_STRM ( s ) ? htxbuf ( & s - > req . buf ) - > data : b_data ( & s - > req . buf ) ) ;
2023-02-16 05:09:31 -05:00
sc_ep_report_read_activity ( s - > scf ) ;
MINOR: stream: Add a function to validate TCP to H1 upgrades
TCP to H1 upgrades are buggy for now. When such upgrade is performed, a
crash is experienced. The bug is the result of the recent H1 mux
refactoring, and more specifically because of the commit c4bfa59f1 ("MAJOR:
mux-h1: Create the client stream as later as possible"). Indeed, now the H1
mux is responsible to create the frontend conn-stream once the request
headers are fully received. Thus the TCP to H1 upgrade is a problem because
the frontend conn-stream already exists.
To fix the bug, we must keep this conn-stream and the associate stream and
use it in the H1 mux. To do so, the upgrade will be performed in two
steps. First, the mux is upgraded from mux-pt to mux-h1. Then, the mux-h1
performs the stream upgrade, once the request headers are fully received and
parsed. To do so, stream_upgrade_from_cs() must be used. This function set
the SF_HTX flags to switch the stream to HTX mode, it removes the SF_IGNORE
flags and eventually it fills the request channel with some input data.
This patch is required to fix the TCP to H1 upgrades and is intimately
linked with the next commits.
2021-01-21 11:36:12 -05:00
}
2022-12-20 12:47:39 -05:00
s - > req . flags | = CF_READ_EVENT ; /* Always report a read event */
MINOR: stream: Add a function to validate TCP to H1 upgrades
TCP to H1 upgrades are buggy for now. When such upgrade is performed, a
crash is experienced. The bug is the result of the recent H1 mux
refactoring, and more specifically because of the commit c4bfa59f1 ("MAJOR:
mux-h1: Create the client stream as later as possible"). Indeed, now the H1
mux is responsible to create the frontend conn-stream once the request
headers are fully received. Thus the TCP to H1 upgrade is a problem because
the frontend conn-stream already exists.
To fix the bug, we must keep this conn-stream and the associate stream and
use it in the H1 mux. To do so, the upgrade will be performed in two
steps. First, the mux is upgraded from mux-pt to mux-h1. Then, the mux-h1
performs the stream upgrade, once the request headers are fully received and
parsed. To do so, stream_upgrade_from_cs() must be used. This function set
the SF_HTX flags to switch the stream to HTX mode, it removes the SF_IGNORE
flags and eventually it fills the request channel with some input data.
This patch is required to fix the TCP to H1 upgrades and is intimately
linked with the next commits.
2021-01-21 11:36:12 -05:00
s - > flags & = ~ SF_IGNORE ;
task_wakeup ( s - > task , TASK_WOKEN_INIT ) ;
return 0 ;
}
2018-11-06 09:50:21 -05:00
/* Callback used to wake up a stream when an input buffer is available. The
2022-05-17 13:07:51 -04:00
* stream < s > ' s stream connectors are checked for a failed buffer allocation
2022-05-25 10:36:21 -04:00
* as indicated by the presence of the SC_FL_NEED_BUFF flag and the lack of a
2018-11-06 09:50:21 -05:00
* buffer , and and input buffer is assigned there ( at most one ) . The function
* returns 1 and wakes the stream up if a buffer was taken , otherwise zero .
* It ' s designed to be called from __offer_buffer ( ) .
*/
int stream_buf_available ( void * arg )
{
struct stream * s = arg ;
2024-04-29 02:52:40 -04:00
if ( ! s - > req . buf . size & & ! sc_ep_have_ff_data ( s - > scb ) & & s - > scf - > flags & SC_FL_NEED_BUFF )
2022-05-25 01:48:07 -04:00
sc_have_buff ( s - > scf ) ;
2024-04-29 02:52:40 -04:00
if ( ! s - > res . buf . size & & ! sc_ep_have_ff_data ( s - > scf ) & & s - > scb - > flags & SC_FL_NEED_BUFF )
2022-05-25 01:48:07 -04:00
sc_have_buff ( s - > scb ) ;
2018-11-06 09:50:21 -05:00
2024-05-07 11:45:31 -04:00
s - > flags | = SF_MAYALLOC ;
2018-11-06 09:50:21 -05:00
task_wakeup ( s - > task , TASK_WOKEN_RES ) ;
return 1 ;
}
2015-04-04 12:50:31 -04:00
/* This function is called from the session handler which detects the end of
2015-04-08 12:26:29 -04:00
* handshake , in order to complete initialization of a valid stream . It must be
2018-11-15 12:14:14 -05:00
* called with a completely initialized session . It returns the pointer to
2015-04-08 12:26:29 -04:00
* the newly created stream , or NULL in case of fatal error . The client - facing
2017-08-28 10:22:54 -04:00
* end point is assigned to < origin > , which must be valid . The stream ' s task
* is configured with a nice value inherited from the listener ' s nice if any .
* The task ' s context is set to the new stream , and its function is set to
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
* process_stream ( ) . Target and analysers are null . < input > is used as input
* buffer for the request channel and may contain data . On success , it is
* transfer to the stream and < input > is set to BUF_NULL . On error , < input >
* buffer is unchanged and it is the caller responsibility to release it .
2012-08-31 10:01:23 -04:00
*/
2022-05-27 04:13:37 -04:00
struct stream * stream_new ( struct session * sess , struct stconn * sc , struct buffer * input )
2012-08-31 10:01:23 -04:00
{
2015-04-04 12:08:21 -04:00
struct stream * s ;
2017-08-28 10:22:54 -04:00
struct task * t ;
2012-08-31 10:01:23 -04:00
2019-11-05 10:18:10 -05:00
DBG_TRACE_ENTER ( STRM_EV_STRM_NEW ) ;
2017-11-24 11:34:44 -05:00
if ( unlikely ( ( s = pool_alloc ( pool_head_stream ) ) = = NULL ) )
2017-08-28 10:22:54 -04:00
goto out_fail_alloc ;
2015-04-04 12:08:21 -04:00
/* minimum stream initialization required for an embryonic stream is
* fairly low . We need very little to execute L4 ACLs , then we need a
* task to make the client - side connection live on its own .
* - flags
* - stick - entry tracking
*/
s - > flags = 0 ;
2015-04-05 12:19:23 -04:00
s - > logs . logwait = sess - > fe - > to_log ;
2015-04-04 12:08:21 -04:00
s - > logs . level = 0 ;
2023-04-27 03:46:02 -04:00
s - > logs . request_ts = 0 ;
2015-04-05 06:03:54 -04:00
s - > logs . t_queue = - 1 ;
s - > logs . t_connect = - 1 ;
s - > logs . t_data = - 1 ;
s - > logs . t_close = 0 ;
s - > logs . bytes_in = s - > logs . bytes_out = 0 ;
2018-05-11 12:52:31 -04:00
s - > logs . prx_queue_pos = 0 ; /* we get the number of pending conns before us */
s - > logs . srv_queue_pos = 0 ; /* we will get this number soon */
2019-01-21 02:34:50 -05:00
s - > obj_type = OBJ_TYPE_STREAM ;
2015-04-05 06:03:54 -04:00
2020-09-30 08:03:54 -04:00
s - > logs . accept_date = sess - > accept_date ;
2023-04-27 03:46:02 -04:00
s - > logs . accept_ts = sess - > accept_ts ;
2020-09-30 08:03:54 -04:00
s - > logs . t_handshake = sess - > t_handshake ;
2020-09-30 07:49:56 -04:00
s - > logs . t_idle = sess - > t_idle ;
2018-11-29 09:19:05 -05:00
2015-04-05 06:03:54 -04:00
/* default logging function */
s - > do_log = strm_log ;
/* default error reporting function, may be changed by analysers */
s - > srv_error = default_srv_error ;
2015-04-04 12:08:21 -04:00
/* Initialise the current rule list pointer to NULL. We are sure that
* any rulelist match the NULL pointer .
*/
s - > current_rule_list = NULL ;
2015-07-22 11:10:58 -04:00
s - > current_rule = NULL ;
2020-07-28 05:56:13 -04:00
s - > rules_exp = TICK_ETERNITY ;
2024-10-31 08:53:56 -04:00
s - > last_entity . type = STRM_ENTITY_NONE ;
2024-10-31 06:23:12 -04:00
s - > last_entity . ptr = NULL ;
2024-10-31 08:53:56 -04:00
s - > waiting_entity . type = STRM_ENTITY_NONE ;
2024-10-31 06:34:54 -04:00
s - > waiting_entity . ptr = NULL ;
2015-04-04 12:08:21 -04:00
2023-01-06 10:09:58 -05:00
s - > stkctr = NULL ;
if ( pool_head_stk_ctr ) {
s - > stkctr = pool_alloc ( pool_head_stk_ctr ) ;
if ( ! s - > stkctr )
goto out_fail_alloc ;
/* Copy SC counters for the stream. We don't touch refcounts because
* any reference we have is inherited from the session . Since the stream
* doesn ' t exist without the session , the session ' s existence guarantees
* we don ' t lose the entry . During the store operation , the stream won ' t
* touch these ones .
*/
memcpy ( s - > stkctr , sess - > stkctr , sizeof ( s - > stkctr [ 0 ] ) * global . tune . nb_stk_ctr ) ;
}
2015-04-04 12:08:21 -04:00
s - > sess = sess ;
2021-02-24 05:29:51 -05:00
s - > stream_epoch = _HA_ATOMIC_LOAD ( & stream_epoch ) ;
2021-04-06 05:57:41 -04:00
s - > uniq_id = _HA_ATOMIC_FETCH_ADD ( & global . req_count , 1 ) ;
2024-12-23 08:30:33 -05:00
s - > term_evts_log = 0 ;
2015-04-04 12:08:21 -04:00
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* OK, we're keeping the stream, so let's properly initialize the stream */
2012-08-31 10:01:23 -04:00
LIST_INIT ( & s - > back_refs ) ;
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
2021-02-20 05:49:49 -05:00
LIST_INIT ( & s - > buffer_wait . list ) ;
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
s - > buffer_wait . target = s ;
2018-11-06 09:50:21 -05:00
s - > buffer_wait . wakeup_cb = stream_buf_available ;
2013-10-14 15:32:07 -04:00
2022-09-07 03:17:45 -04:00
s - > lat_time = s - > cpu_time = 0 ;
2021-04-10 17:00:53 -04:00
s - > call_rate . curr_tick = s - > call_rate . curr_ctr = s - > call_rate . prev_ctr = 0 ;
2024-10-22 10:35:04 -04:00
s - > passes_stconn = s - > passes_reqana = s - > passes_resana = s - > passes_propag = 0 ;
2018-10-26 08:47:40 -04:00
s - > pcli_next_pid = 0 ;
2018-12-11 10:10:57 -05:00
s - > pcli_flags = 0 ;
2020-03-05 14:19:02 -05:00
s - > unique_id = IST_NULL ;
2024-07-16 09:55:57 -04:00
s - > parent = NULL ;
2021-10-01 12:23:30 -04:00
if ( ( t = task_new_here ( ) ) = = NULL )
2017-08-28 10:22:54 -04:00
goto out_fail_alloc ;
2015-04-04 12:08:21 -04:00
s - > task = t ;
2025-01-27 12:38:40 -05:00
s - > pending_events = s - > new_events = STRM_EVT_NONE ;
2024-09-25 09:10:08 -04:00
s - > conn_retries = 0 ;
s - > max_retries = 0 ;
2022-03-29 13:02:31 -04:00
s - > conn_exp = TICK_ETERNITY ;
2022-03-30 13:39:30 -04:00
s - > conn_err_type = STRM_ET_NONE ;
2022-05-17 13:47:17 -04:00
s - > prev_conn_state = SC_ST_INI ;
2015-04-05 18:25:48 -04:00
t - > process = process_stream ;
2012-08-31 10:01:23 -04:00
t - > context = s ;
t - > expire = TICK_ETERNITY ;
2017-08-28 10:22:54 -04:00
if ( sess - > listener )
2023-01-12 13:32:45 -05:00
t - > nice = sess - > listener - > bind_conf - > nice ;
2012-08-31 10:01:23 -04:00
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* Note: initially, the stream's backend points to the frontend.
2012-08-31 10:01:23 -04:00
* This changes later when switching rules are executed or
* when the default backend is assigned .
*/
2015-04-03 09:40:56 -04:00
s - > be = sess - > fe ;
2015-04-03 16:16:32 -04:00
s - > req_cap = NULL ;
s - > res_cap = NULL ;
2010-11-11 04:56:04 -05:00
BUG/MINOR: http_ana/txn: don't re-initialize txn and req var lists
In http_create_txn(): vars_init_head() was performed on both s->vars_txn
and s->var_reqres lists.
But this is wrong, these two lists are already initialized upon stream
creation in stream_new().
Moreover, between stream_new() and http_create_txn(), some variable may
be defined (e.g.: by the frontend), resulting in lists not being empty.
Because of this "extra" list initialization, already defined variables
can be lost.
This causes txn dependant code not being able to access previously defined
variables as well as memory leak because http_destroy_txn() relies on these
lists to perform the purge.
This proved to be the case when a frontend sets variables and lua sample
fetch is used in backend section as described in GH #1935.
Many thanks to Darragh O'Toole for his detailed report.
Removing extra var_init_head (x2) in http_create_txn() to fix the issue.
Adding somme comments in the code in an attempt to prevent future misuses
of s->var_reqres, and s->var_txn lists.
It should be backported in every stable version.
(This is an old bug that seems to exist since 1.6-dev6)
[cf: On 2.0 and 1.8, for the legacy HTTP code, vars_init() are used during
the TXN cleanup, when the stream is reused. So, these calls must be
moved from http_init_txn() to http_reset_txn() and not removed.]
2022-11-18 03:17:29 -05:00
/* Initialize all the variables contexts even if not used.
2015-06-19 05:59:02 -04:00
* This permits to prune these contexts without errors .
BUG/MINOR: http_ana/txn: don't re-initialize txn and req var lists
In http_create_txn(): vars_init_head() was performed on both s->vars_txn
and s->var_reqres lists.
But this is wrong, these two lists are already initialized upon stream
creation in stream_new().
Moreover, between stream_new() and http_create_txn(), some variable may
be defined (e.g.: by the frontend), resulting in lists not being empty.
Because of this "extra" list initialization, already defined variables
can be lost.
This causes txn dependant code not being able to access previously defined
variables as well as memory leak because http_destroy_txn() relies on these
lists to perform the purge.
This proved to be the case when a frontend sets variables and lua sample
fetch is used in backend section as described in GH #1935.
Many thanks to Darragh O'Toole for his detailed report.
Removing extra var_init_head (x2) in http_create_txn() to fix the issue.
Adding somme comments in the code in an attempt to prevent future misuses
of s->var_reqres, and s->var_txn lists.
It should be backported in every stable version.
(This is an old bug that seems to exist since 1.6-dev6)
[cf: On 2.0 and 1.8, for the legacy HTTP code, vars_init() are used during
the TXN cleanup, when the stream is reused. So, these calls must be
moved from http_init_txn() to http_reset_txn() and not removed.]
2022-11-18 03:17:29 -05:00
*
* We need to make sure that those lists are not re - initialized
* by stream - dependant underlying code because we could lose
* track of already defined variables , leading to data inconsistency
* and memory leaks . . .
*
* For reference : we had a very old bug caused by vars_txn and
* vars_reqres being accidentally re - initialized in http_create_txn ( )
* ( https : //github.com/haproxy/haproxy/issues/1935)
2015-06-06 13:29:07 -04:00
*/
2021-08-31 02:13:25 -04:00
vars_init_head ( & s - > vars_txn , SCOPE_TXN ) ;
vars_init_head ( & s - > vars_reqres , SCOPE_REQ ) ;
2015-06-06 13:29:07 -04:00
2021-12-23 06:06:45 -05:00
/* Set SF_HTX flag for HTTP frontends. */
if ( sess - > fe - > mode = = PR_MODE_HTTP )
s - > flags | = SF_HTX ;
2022-05-27 04:13:37 -04:00
s - > scf = sc ;
2022-05-27 02:49:24 -04:00
if ( sc_attach_strm ( s - > scf , s ) < 0 )
2022-05-17 13:40:40 -04:00
goto out_fail_attach_scf ;
2022-03-23 06:01:09 -04:00
2022-05-27 02:33:53 -04:00
s - > scb = sc_new_from_strm ( s , SC_FL_ISBACK ) ;
2022-05-17 13:40:40 -04:00
if ( ! s - > scb )
goto out_fail_alloc_scb ;
2021-12-22 08:22:03 -05:00
2022-05-27 03:03:30 -04:00
sc_set_state ( s - > scf , SC_ST_EST ) ;
2021-12-23 06:06:45 -05:00
2021-12-23 11:28:17 -05:00
if ( likely ( sess - > fe - > options2 & PR_O2_INDEPSTR ) )
2022-05-17 13:44:42 -04:00
s - > scf - > flags | = SC_FL_INDEP_STR ;
2021-12-23 06:06:45 -05:00
if ( likely ( sess - > fe - > options2 & PR_O2_INDEPSTR ) )
2022-05-17 13:44:42 -04:00
s - > scb - > flags | = SC_FL_INDEP_STR ;
2017-12-20 10:31:43 -05:00
2022-05-27 04:13:37 -04:00
if ( sc_ep_test ( sc , SE_FL_WEBSOCKET ) )
2021-12-20 09:34:16 -05:00
s - > flags | = SF_WEBSOCKET ;
2022-05-27 04:13:37 -04:00
if ( sc_conn ( sc ) ) {
const struct mux_ops * mux = sc_mux_ops ( sc ) ;
2021-12-17 11:28:35 -05:00
2022-03-30 10:31:41 -04:00
if ( mux & & mux - > flags & MX_FL_HTX )
s - > flags | = SF_HTX ;
2021-12-15 05:42:23 -05:00
}
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream_init_srv_conn ( s ) ;
2023-01-12 13:18:34 -05:00
s - > target = sess - > fe - > default_target ;
2016-12-04 18:26:31 -05:00
2010-06-01 11:45:26 -04:00
s - > pend_pos = NULL ;
2018-05-11 12:52:31 -04:00
s - > priority_class = 0 ;
s - > priority_offset = 0 ;
2010-06-01 11:45:26 -04:00
/* init store persistence */
s - > store_count = 0 ;
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
channel_init ( & s - > req ) ;
2022-12-20 12:47:39 -05:00
s - > req . flags | = CF_READ_EVENT ; /* the producer is already connected */
2023-01-12 12:39:42 -05:00
s - > req . analysers = sess - > listener ? sess - > listener - > bind_conf - > analysers : sess - > fe - > fe_req_ana ;
2017-11-18 09:39:10 -05:00
2021-03-15 12:09:27 -04:00
if ( IS_HTX_STRM ( s ) ) {
/* Be sure to have HTTP analysers because in case of
* " destructive " stream upgrade , they may be missing ( e . g
* TCP > H2 )
*/
s - > req . analysers | = AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE ;
}
2017-11-18 09:39:10 -05:00
if ( ! sess - > fe - > fe_req_ana ) {
channel_auto_connect ( & s - > req ) ; /* don't wait to establish connection */
channel_auto_close ( & s - > req ) ; /* let the producer forward close requests */
}
2015-04-05 12:15:59 -04:00
2023-02-15 02:13:33 -05:00
s - > scf - > ioto = sess - > fe - > timeout . client ;
2014-11-27 14:45:39 -05:00
s - > req . analyse_exp = TICK_ETERNITY ;
2014-11-24 05:36:57 -05:00
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
channel_init ( & s - > res ) ;
2014-11-28 08:17:09 -05:00
s - > res . flags | = CF_ISRESP ;
2014-11-27 14:45:39 -05:00
s - > res . analysers = 0 ;
2010-06-01 11:45:26 -04:00
2015-04-03 09:40:56 -04:00
if ( sess - > fe - > options2 & PR_O2_NODELAY ) {
2023-03-17 10:38:18 -04:00
s - > scf - > flags | = SC_FL_SND_NEVERWAIT ;
s - > scb - > flags | = SC_FL_SND_NEVERWAIT ;
2011-05-30 12:10:30 -04:00
}
2023-02-15 02:13:33 -05:00
s - > scb - > ioto = TICK_ETERNITY ;
2014-11-27 14:45:39 -05:00
s - > res . analyse_exp = TICK_ETERNITY ;
2010-06-01 11:45:26 -04:00
2015-04-03 17:46:31 -04:00
s - > txn = NULL ;
BUG/MEDIUM: hlua: streams don't support mixing lua-load with lua-load-per-thread (2nd try)
While trying to reproduce another crash case involving lua filters
reported by @bgrooot on GH #2467, we found out that mixing filters loaded
from different contexts ('lua-load' vs 'lua-load-per-thread') for the same
stream isn't supported and may even cause the process to crash.
Historically, mixing lua-load and lua-load-per-threads for a stream wasn't
supported, but this changed thanks to 0913386 ("BUG/MEDIUM: hlua: streams
don't support mixing lua-load with lua-load-per-thread").
However, the above fix didn't consider lua filters's use-case properly:
unlike lua fetches, actions or even services, lua filters don't simply
use the stream hlua context as a "temporary" hlua running context to
process some hlua code. For fetches, actions.. hlua executions are
processed sequentially, so we simply reuse the hlua context from the
previous action/fetch to run the next one (this allows to bypass memory
allocations and initialization, thus it increases performance), unless
we need to run on a different hlua state-id, in which case we perform a
reset of the hlua context.
But this cannot work with filters: indeed, once registered, a filter will
last for the whole stream duration. It means that the filter will rely
on the stream hlua context from ->attach() to ->detach(). And here is the
catch, if for the same stream we register 2 lua filters from different
contexts ('lua-load' + 'lua-load-per-thread'), then we have an issue,
because the hlua stream will be re-created each time we switch between
runtime contexts, which means each time we switch between the filters (may
happen for each stream processing step), and since lua filters rely on the
stream hlua to carry context between filtering steps, this context will be
lost upon a switch. Given that lua filters code was not designed with that
in mind, it would confuse the code and cause unexpected behaviors ranging
from lua errors to crashing process.
So here we take another approach: instead of re-creating the stream hlua
context each time we switch between "global" and "per-thread" runtime
context, let's have both of them inside the stream directly as initially
suggested by Christopher back then when talked about the original issue.
For this we leverage hlua_stream_ctx_prepare() and hlua_stream_ctx_get()
helper functions which return the proper hlua context for a given stream
and state_id combination.
As for debugging infos reported after ha_panic(), we check for both hlua
runtime contexts to check if one of them was active when the panic occured
(only 1 runtime ctx per stream may be active at a given time).
This should be backported to all stable versions with 0913386
("BUG/MEDIUM: hlua: streams don't support mixing lua-load with lua-load-per-thread")
This commit depends on:
- "DEBUG: lua: precisely identify if stream is stuck inside lua or not"
[for versions < 2.9 the ha_thread_dump_one() part should be skipped]
- "MINOR: hlua: use accessors for stream hlua ctx"
For 2.4, the filters API didn't exist. However it may be a good idea to
backport it anyway because ->set_priv()/->get_priv() from tcp/http lua
applets may also be affected by this bug, plus it will ease code
maintenance. Of course, filters-related parts should be skipped in this
case.
2024-03-12 12:05:54 -04:00
s - > hlua [ 0 ] = s - > hlua [ 1 ] = NULL ;
2015-02-16 14:11:43 -05:00
2020-12-23 11:41:43 -05:00
s - > resolv_ctx . requester = NULL ;
s - > resolv_ctx . hostname_dn = NULL ;
s - > resolv_ctx . hostname_dn_len = 0 ;
s - > resolv_ctx . parent = NULL ;
2019-04-23 11:26:33 -04:00
2020-12-10 07:43:53 -05:00
s - > tunnel_timeout = TICK_ETERNITY ;
2021-09-30 13:02:18 -04:00
LIST_APPEND ( & th_ctx - > streams , & s - > list ) ;
2024-09-27 11:16:00 -04:00
_HA_ATOMIC_INC ( & th_ctx - > total_streams ) ;
2024-09-25 03:59:11 -04:00
_HA_ATOMIC_INC ( & th_ctx - > stream_cnt ) ;
2017-06-30 10:23:45 -04:00
2015-11-05 07:35:03 -05:00
if ( flt_stream_init ( s ) < 0 | | flt_stream_start ( s ) < 0 )
MAJOR: filters: Add filters support
This patch adds the support of filters in HAProxy. The main idea is to have a
way to "easely" extend HAProxy by adding some "modules", called filters, that
will be able to change HAProxy behavior in a programmatic way.
To do so, many entry points has been added in code to let filters to hook up to
different steps of the processing. A filter must define a flt_ops sutrctures
(see include/types/filters.h for details). This structure contains all available
callbacks that a filter can define:
struct flt_ops {
/*
* Callbacks to manage the filter lifecycle
*/
int (*init) (struct proxy *p);
void (*deinit)(struct proxy *p);
int (*check) (struct proxy *p);
/*
* Stream callbacks
*/
void (*stream_start) (struct stream *s);
void (*stream_accept) (struct stream *s);
void (*session_establish)(struct stream *s);
void (*stream_stop) (struct stream *s);
/*
* HTTP callbacks
*/
int (*http_start) (struct stream *s, struct http_msg *msg);
int (*http_start_body) (struct stream *s, struct http_msg *msg);
int (*http_start_chunk) (struct stream *s, struct http_msg *msg);
int (*http_data) (struct stream *s, struct http_msg *msg);
int (*http_last_chunk) (struct stream *s, struct http_msg *msg);
int (*http_end_chunk) (struct stream *s, struct http_msg *msg);
int (*http_chunk_trailers)(struct stream *s, struct http_msg *msg);
int (*http_end_body) (struct stream *s, struct http_msg *msg);
void (*http_end) (struct stream *s, struct http_msg *msg);
void (*http_reset) (struct stream *s, struct http_msg *msg);
int (*http_pre_process) (struct stream *s, struct http_msg *msg);
int (*http_post_process) (struct stream *s, struct http_msg *msg);
void (*http_reply) (struct stream *s, short status,
const struct chunk *msg);
};
To declare and use a filter, in the configuration, the "filter" keyword must be
used in a listener/frontend section:
frontend test
...
filter <FILTER-NAME> [OPTIONS...]
The filter referenced by the <FILTER-NAME> must declare a configuration parser
on its own name to fill flt_ops and filter_conf field in the proxy's
structure. An exemple will be provided later to make it perfectly clear.
For now, filters cannot be used in backend section. But this is only a matter of
time. Documentation will also be added later. This is the first commit of a long
list about filters.
It is possible to have several filters on the same listener/frontend. These
filters are stored in an array of at most MAX_FILTERS elements (define in
include/types/filters.h). Again, this will be replaced later by a list of
filters.
The filter API has been highly refactored. Main changes are:
* Now, HA supports an infinite number of filters per proxy. To do so, filters
are stored in list.
* Because filters are stored in list, filters state has been moved from the
channel structure to the filter structure. This is cleaner because there is no
more info about filters in channel structure.
* It is possible to defined filters on backends only. For such filters,
stream_start/stream_stop callbacks are not called. Of course, it is possible
to mix frontend and backend filters.
* Now, TCP streams are also filtered. All callbacks without the 'http_' prefix
are called for all kind of streams. In addition, 2 new callbacks were added to
filter data exchanged through a TCP stream:
- tcp_data: it is called when new data are available or when old unprocessed
data are still waiting.
- tcp_forward_data: it is called when some data can be consumed.
* New callbacks attached to channel were added:
- channel_start_analyze: it is called when a filter is ready to process data
exchanged through a channel. 2 new analyzers (a frontend and a backend)
are attached to channels to call this callback. For a frontend filter, it
is called before any other analyzer. For a backend filter, it is called
when a backend is attached to a stream. So some processing cannot be
filtered in that case.
- channel_analyze: it is called before each analyzer attached to a channel,
expects analyzers responsible for data sending.
- channel_end_analyze: it is called when all other analyzers have finished
their processing. A new analyzers is attached to channels to call this
callback. For a TCP stream, this is always the last one called. For a HTTP
one, the callback is called when a request/response ends, so it is called
one time for each request/response.
* 'session_established' callback has been removed. Everything that is done in
this callback can be handled by 'channel_start_analyze' on the response
channel.
* 'http_pre_process' and 'http_post_process' callbacks have been replaced by
'channel_analyze'.
* 'http_start' callback has been replaced by 'http_headers'. This new one is
called just before headers sending and parsing of the body.
* 'http_end' callback has been replaced by 'channel_end_analyze'.
* It is possible to set a forwarder for TCP channels. It was already possible to
do it for HTTP ones.
* Forwarders can partially consumed forwardable data. For this reason a new
HTTP message state was added before HTTP_MSG_DONE : HTTP_MSG_ENDING.
Now all filters can define corresponding callbacks (http_forward_data
and tcp_forward_data). Each filter owns 2 offsets relative to buf->p, next and
forward, to track, respectively, input data already parsed but not forwarded yet
by the filter and parsed data considered as forwarded by the filter. A any time,
we have the warranty that a filter cannot parse or forward more input than
previous ones. And, of course, it cannot forward more input than it has
parsed. 2 macros has been added to retrieve these offets: FLT_NXT and FLT_FWD.
In addition, 2 functions has been added to change the 'next size' and the
'forward size' of a filter. When a filter parses input data, it can alter these
data, so the size of these data can vary. This action has an effet on all
previous filters that must be handled. To do so, the function
'filter_change_next_size' must be called, passing the size variation. In the
same spirit, if a filter alter forwarded data, it must call the function
'filter_change_forward_size'. 'filter_change_next_size' can be called in
'http_data' and 'tcp_data' callbacks and only these ones. And
'filter_change_forward_size' can be called in 'http_forward_data' and
'tcp_forward_data' callbacks and only these ones. The data changes are the
filter responsability, but with some limitation. It must not change already
parsed/forwarded data or data that previous filters have not parsed/forwarded
yet.
Because filters can be used on backends, when we the backend is set for a
stream, we add filters defined for this backend in the filter list of the
stream. But we must only do that when the backend and the frontend of the stream
are not the same. Else same filters are added a second time leading to undefined
behavior.
The HTTP compression code had to be moved.
So it simplifies http_response_forward_body function. To do so, the way the data
are forwarded has changed. Now, a filter (and only one) can forward data. In a
commit to come, this limitation will be removed to let all filters take part to
data forwarding. There are 2 new functions that filters should use to deal with
this feature:
* flt_set_http_data_forwarder: This function sets the filter (using its id)
that will forward data for the specified HTTP message. It is possible if it
was not already set by another filter _AND_ if no data was yet forwarded
(msg->msg_state <= HTTP_MSG_BODY). It returns -1 if an error occurs.
* flt_http_data_forwarder: This function returns the filter id that will
forward data for the specified HTTP message. If there is no forwarder set, it
returns -1.
When an HTTP data forwarder is set for the response, the HTTP compression is
disabled. Of course, this is not definitive.
2015-04-30 05:48:27 -04:00
goto out_fail_accept ;
2022-05-27 10:49:18 -04:00
/* just in case the caller would have pre-disabled it */
se_will_consume ( s - > scf - > sedesc ) ;
2010-06-01 11:45:26 -04:00
2015-04-05 12:19:23 -04:00
if ( sess - > fe - > accept & & sess - > fe - > accept ( s ) < 0 )
2015-04-05 05:52:08 -04:00
goto out_fail_accept ;
2010-06-01 11:45:26 -04:00
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
if ( ! b_is_null ( input ) ) {
/* Xfer the input buffer to the request channel. <input> will
* than point to BUF_NULL . From this point , it is the stream
* responsibility to release it .
*/
s - > req . buf = * input ;
* input = BUF_NULL ;
2020-12-04 11:22:49 -05:00
s - > req . total = ( IS_HTX_STRM ( s ) ? htxbuf ( & s - > req . buf ) - > data : b_data ( & s - > req . buf ) ) ;
2023-02-16 05:09:31 -05:00
sc_ep_report_read_activity ( s - > scf ) ;
BUG/MEDIUM: stream: Xfer the input buffer to a fully created stream
The input buffer passed as argument to create a new stream must not be
transferred when the request channel is initialized because the channel
flags are not set at this stage. In addition, the API is a bit confusing
regarding the buffer owner when an error occurred. The caller remains the
owner, but reading the code it is not obvious.
So, first of all, to avoid any ambiguities, comments are added on the
calling chain to make it clear. The buffer owner is the caller if any error
occurred. And the ownership is transferred to the stream on success.
Then, to make things simple, the ownership is transferred at the end of
stream_new(), in case of success. And the input buffer is updated to point
on BUF_NULL. Thus, in all cases, if the caller try to release it calling
b_free() on it, it is not a problem. Of course, it remains the caller
responsibility to release it on error.
The patch fixes a bug introduced by the commit 26256f86e ("MINOR: stream:
Pass an optional input buffer when a stream is created"). No backport is
needed.
2020-12-04 10:47:41 -05:00
}
2010-06-01 11:45:26 -04:00
/* it is important not to call the wakeup function directly but to
* pass through task_wakeup ( ) , because this one knows how to apply
2017-05-29 09:26:51 -04:00
* priorities to tasks . Using multi thread we must be sure that
* stream is fully initialized before calling task_wakeup . So
* the caller must handle the task_wakeup
2010-06-01 11:45:26 -04:00
*/
2019-11-05 10:18:10 -05:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_NEW , s ) ;
2021-12-20 09:34:16 -05:00
task_wakeup ( s - > task , TASK_WOKEN_INIT ) ;
2015-04-05 06:00:52 -04:00
return s ;
2010-06-01 11:45:26 -04:00
/* Error unrolling */
2015-04-05 05:52:08 -04:00
out_fail_accept :
2015-11-05 07:35:03 -05:00
flt_stream_release ( s , 0 ) ;
2021-04-21 01:32:39 -04:00
LIST_DELETE ( & s - > list ) ;
2022-09-27 03:14:47 -04:00
sc_free ( s - > scb ) ;
2023-03-20 14:11:08 -04:00
out_fail_alloc_scb :
2022-09-27 03:14:47 -04:00
out_fail_attach_scf :
2021-12-23 06:06:45 -05:00
task_destroy ( t ) ;
2017-08-28 10:22:54 -04:00
out_fail_alloc :
2023-01-06 10:09:58 -05:00
if ( s )
pool_free ( pool_head_stk_ctr , s - > stkctr ) ;
2017-11-24 11:34:44 -05:00
pool_free ( pool_head_stream , s ) ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_DEVEL ( " leaving on error " , STRM_EV_STRM_NEW | STRM_EV_STRM_ERR ) ;
2015-04-05 06:00:52 -04:00
return NULL ;
2010-06-01 11:45:26 -04:00
}
2006-06-25 20:48:02 -04:00
/*
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* frees the context associated to a stream . It must have been removed first .
2006-06-25 20:48:02 -04:00
*/
2022-05-12 08:56:55 -04:00
void stream_free ( struct stream * s )
2006-06-25 20:48:02 -04:00
{
2015-04-03 13:19:59 -04:00
struct session * sess = strm_sess ( s ) ;
struct proxy * fe = sess - > fe ;
2008-12-07 14:16:23 -05:00
struct bref * bref , * back ;
2010-06-06 12:28:49 -04:00
int i ;
2007-01-07 09:46:13 -05:00
2019-11-05 10:18:10 -05:00
DBG_TRACE_POINT ( STRM_EV_STRM_FREE , s ) ;
2019-05-17 08:20:05 -04:00
/* detach the stream from its own task before even releasing it so
* that walking over a task list never exhibits a dying stream .
*/
s - > task - > context = NULL ;
__ha_barrier_store ( ) ;
2018-07-25 05:13:53 -04:00
pendconn_free ( s ) ;
2008-12-04 03:33:58 -05:00
2012-11-11 18:42:33 -05:00
if ( objt_server ( s - > target ) ) { /* there may be requests left pending in queue */
2015-04-02 19:14:29 -04:00
if ( s - > flags & SF_CURR_SESS ) {
s - > flags & = ~ SF_CURR_SESS ;
2021-04-06 07:53:36 -04:00
_HA_ATOMIC_DEC ( & __objt_server ( s - > target ) - > cur_sess ) ;
2008-11-11 14:20:02 -05:00
}
2021-12-06 02:01:02 -05:00
if ( may_dequeue_tasks ( __objt_server ( s - > target ) , s - > be ) )
process_srv_queue ( __objt_server ( s - > target ) ) ;
2008-11-11 14:20:02 -05:00
}
2008-12-04 03:33:58 -05:00
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
if ( unlikely ( s - > srv_conn ) ) {
2024-12-13 12:11:05 -05:00
struct server * oldsrv = s - > srv_conn ;
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* the stream still has a reserved slot on a server, but
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
* it should normally be only the same as the one above ,
* so this should not happen in fact .
*/
2025-02-06 11:07:48 -05:00
/*
* We don ' t want to release the slot just yet
* if we ' re using strict - maxconn , we want to
* free the connection before .
*/
if ( ! ( oldsrv - > flags & SRV_F_STRICT_MAXCONN ) ) {
sess_change_server ( s , NULL ) ;
if ( may_dequeue_tasks ( oldsrv , s - > be ) )
process_srv_queue ( oldsrv ) ;
}
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
}
MAJOR: session: implement a wait-queue for sessions who need a buffer
When a session_alloc_buffers() fails to allocate one or two buffers,
it subscribes the session to buffer_wq, and waits for another session
to release buffers. It's then removed from the queue and woken up with
TASK_WAKE_RES, and can attempt its allocation again.
We decide to try to wake as many waiters as we release buffers so
that if we release 2 and two waiters need only once, they both have
their chance. We must never come to the situation where we don't wake
enough tasks up.
It's common to release buffers after the completion of an I/O callback,
which can happen even if the I/O could not be performed due to half a
failure on memory allocation. In this situation, we don't want to move
out of the wait queue the session that was just added, otherwise it
will never get any buffer. Thus, we only force ourselves out of the
queue when freeing the session.
Note: at the moment, since session_alloc_buffers() is not used, no task
is subscribed to the wait queue.
2014-11-25 15:10:35 -05:00
/* We may still be present in the buffer wait queue */
2024-04-24 12:44:03 -04:00
b_dequeue ( & s - > buffer_wait ) ;
2020-02-26 04:39:36 -05:00
2018-07-10 11:43:27 -04:00
if ( s - > req . buf . size | | s - > res . buf . size ) {
2021-02-20 06:02:46 -05:00
int count = ! ! s - > req . buf . size + ! ! s - > res . buf . size ;
2019-08-08 02:06:27 -04:00
b_free ( & s - > req . buf ) ;
b_free ( & s - > res . buf ) ;
2021-02-20 06:02:46 -05:00
offer_buffers ( NULL , count ) ;
MAJOR: session: implement a wait-queue for sessions who need a buffer
When a session_alloc_buffers() fails to allocate one or two buffers,
it subscribes the session to buffer_wq, and waits for another session
to release buffers. It's then removed from the queue and woken up with
TASK_WAKE_RES, and can attempt its allocation again.
We decide to try to wake as many waiters as we release buffers so
that if we release 2 and two waiters need only once, they both have
their chance. We must never come to the situation where we don't wake
enough tasks up.
It's common to release buffers after the completion of an I/O callback,
which can happen even if the I/O could not be performed due to half a
failure on memory allocation. In this situation, we don't want to move
out of the wait queue the session that was just added, otherwise it
will never get any buffer. Thus, we only force ourselves out of the
queue when freeing the session.
Note: at the moment, since session_alloc_buffers() is not used, no task
is subscribed to the wait queue.
2014-11-25 15:10:35 -05:00
}
2012-10-12 17:49:43 -04:00
2020-03-05 14:19:02 -05:00
pool_free ( pool_head_uniqueid , s - > unique_id . ptr ) ;
s - > unique_id = IST_NULL ;
2019-02-01 12:10:46 -05:00
2020-02-24 10:26:55 -05:00
flt_stream_stop ( s ) ;
flt_stream_release ( s , 0 ) ;
BUG/MEDIUM: hlua: streams don't support mixing lua-load with lua-load-per-thread (2nd try)
While trying to reproduce another crash case involving lua filters
reported by @bgrooot on GH #2467, we found out that mixing filters loaded
from different contexts ('lua-load' vs 'lua-load-per-thread') for the same
stream isn't supported and may even cause the process to crash.
Historically, mixing lua-load and lua-load-per-threads for a stream wasn't
supported, but this changed thanks to 0913386 ("BUG/MEDIUM: hlua: streams
don't support mixing lua-load with lua-load-per-thread").
However, the above fix didn't consider lua filters's use-case properly:
unlike lua fetches, actions or even services, lua filters don't simply
use the stream hlua context as a "temporary" hlua running context to
process some hlua code. For fetches, actions.. hlua executions are
processed sequentially, so we simply reuse the hlua context from the
previous action/fetch to run the next one (this allows to bypass memory
allocations and initialization, thus it increases performance), unless
we need to run on a different hlua state-id, in which case we perform a
reset of the hlua context.
But this cannot work with filters: indeed, once registered, a filter will
last for the whole stream duration. It means that the filter will rely
on the stream hlua context from ->attach() to ->detach(). And here is the
catch, if for the same stream we register 2 lua filters from different
contexts ('lua-load' + 'lua-load-per-thread'), then we have an issue,
because the hlua stream will be re-created each time we switch between
runtime contexts, which means each time we switch between the filters (may
happen for each stream processing step), and since lua filters rely on the
stream hlua to carry context between filtering steps, this context will be
lost upon a switch. Given that lua filters code was not designed with that
in mind, it would confuse the code and cause unexpected behaviors ranging
from lua errors to crashing process.
So here we take another approach: instead of re-creating the stream hlua
context each time we switch between "global" and "per-thread" runtime
context, let's have both of them inside the stream directly as initially
suggested by Christopher back then when talked about the original issue.
For this we leverage hlua_stream_ctx_prepare() and hlua_stream_ctx_get()
helper functions which return the proper hlua context for a given stream
and state_id combination.
As for debugging infos reported after ha_panic(), we check for both hlua
runtime contexts to check if one of them was active when the panic occured
(only 1 runtime ctx per stream may be active at a given time).
This should be backported to all stable versions with 0913386
("BUG/MEDIUM: hlua: streams don't support mixing lua-load with lua-load-per-thread")
This commit depends on:
- "DEBUG: lua: precisely identify if stream is stuck inside lua or not"
[for versions < 2.9 the ha_thread_dump_one() part should be skipped]
- "MINOR: hlua: use accessors for stream hlua ctx"
For 2.4, the filters API didn't exist. However it may be a good idea to
backport it anyway because ->set_priv()/->get_priv() from tcp/http lua
applets may also be affected by this bug, plus it will ease code
maintenance. Of course, filters-related parts should be skipped in this
case.
2024-03-12 12:05:54 -04:00
hlua_ctx_destroy ( s - > hlua [ 0 ] ) ;
hlua_ctx_destroy ( s - > hlua [ 1 ] ) ;
s - > hlua [ 0 ] = s - > hlua [ 1 ] = NULL ;
2015-04-03 17:46:31 -04:00
if ( s - > txn )
2021-03-08 13:12:58 -05:00
http_destroy_txn ( s ) ;
2010-01-07 16:51:47 -05:00
2012-10-12 11:50:05 -04:00
/* ensure the client-side transport layer is destroyed */
2021-12-20 09:34:16 -05:00
/* Be sure it is useless !! */
/* if (cli_cs) */
/* cs_close(cli_cs); */
2012-10-12 11:50:05 -04:00
2010-06-06 12:28:49 -04:00
for ( i = 0 ; i < s - > store_count ; i + + ) {
if ( ! s - > store [ i ] . ts )
continue ;
stksess_free ( s - > store [ i ] . table , s - > store [ i ] . ts ) ;
s - > store [ i ] . ts = NULL ;
}
2020-12-23 11:41:43 -05:00
if ( s - > resolv_ctx . requester ) {
2020-12-23 12:01:04 -05:00
__decl_thread ( struct resolvers * resolvers = s - > resolv_ctx . parent - > arg . resolv . resolvers ) ;
2020-07-22 05:46:32 -04:00
HA_SPIN_LOCK ( DNS_LOCK , & resolvers - > lock ) ;
2021-02-20 04:46:51 -05:00
ha_free ( & s - > resolv_ctx . hostname_dn ) ;
2020-12-23 11:41:43 -05:00
s - > resolv_ctx . hostname_dn_len = 0 ;
2021-10-20 08:07:31 -04:00
resolv_unlink_resolution ( s - > resolv_ctx . requester ) ;
2020-07-22 05:46:32 -04:00
HA_SPIN_UNLOCK ( DNS_LOCK , & resolvers - > lock ) ;
2019-01-21 02:34:50 -05:00
2020-12-23 11:41:43 -05:00
pool_free ( resolv_requester_pool , s - > resolv_ctx . requester ) ;
s - > resolv_ctx . requester = NULL ;
2019-01-21 02:34:50 -05:00
}
2007-10-16 11:34:28 -04:00
if ( fe ) {
BUG/MEDIUM: stream: Be sure to release allocated captures for TCP streams
All TCP and HTTP captures are stored in 2 arrays, one for the request and
another for the response. In HAPRoxy 1.5, these arrays are part of the HTTP
transaction and thus are released during its cleanup. Because in this version,
the transaction is part of the stream (in 1.5, streams are still called
sessions), the cleanup is always performed, for HTTP and TCP streams.
In HAProxy 1.6, the HTTP transaction was moved out from the stream and is now
dynamically allocated only when required (becaues of an HTTP proxy or an HTTP
sample fetch). In addition, still in 1.6, the captures arrays were moved from
the HTTP transaction to the stream. This way, it is still possible to capture
elements from TCP rules for a full TCP stream. Unfortunately, the release is
still exclusively performed during the HTTP transaction cleanup. Thus, for a TCP
stream where the HTTP transaction is not required, the TCP captures, if any, are
never released.
Now, all captures are released when the stream is freed. This fixes the memory
leak for TCP streams. For streams with an HTTP transaction, the captures are now
released when the transaction is reset and not systematically during its
cleanup.
This patch must be backported as fas as 1.6.
2019-11-07 08:27:52 -05:00
if ( s - > req_cap ) {
struct cap_hdr * h ;
for ( h = fe - > req_cap ; h ; h = h - > next )
pool_free ( h - > pool , s - > req_cap [ h - > index ] ) ;
2022-06-23 05:46:14 -04:00
pool_free ( fe - > req_cap_pool , s - > req_cap ) ;
BUG/MEDIUM: stream: Be sure to release allocated captures for TCP streams
All TCP and HTTP captures are stored in 2 arrays, one for the request and
another for the response. In HAPRoxy 1.5, these arrays are part of the HTTP
transaction and thus are released during its cleanup. Because in this version,
the transaction is part of the stream (in 1.5, streams are still called
sessions), the cleanup is always performed, for HTTP and TCP streams.
In HAProxy 1.6, the HTTP transaction was moved out from the stream and is now
dynamically allocated only when required (becaues of an HTTP proxy or an HTTP
sample fetch). In addition, still in 1.6, the captures arrays were moved from
the HTTP transaction to the stream. This way, it is still possible to capture
elements from TCP rules for a full TCP stream. Unfortunately, the release is
still exclusively performed during the HTTP transaction cleanup. Thus, for a TCP
stream where the HTTP transaction is not required, the TCP captures, if any, are
never released.
Now, all captures are released when the stream is freed. This fixes the memory
leak for TCP streams. For streams with an HTTP transaction, the captures are now
released when the transaction is reset and not systematically during its
cleanup.
This patch must be backported as fas as 1.6.
2019-11-07 08:27:52 -05:00
}
if ( s - > res_cap ) {
struct cap_hdr * h ;
for ( h = fe - > rsp_cap ; h ; h = h - > next )
pool_free ( h - > pool , s - > res_cap [ h - > index ] ) ;
2022-06-23 05:46:14 -04:00
pool_free ( fe - > rsp_cap_pool , s - > res_cap ) ;
BUG/MEDIUM: stream: Be sure to release allocated captures for TCP streams
All TCP and HTTP captures are stored in 2 arrays, one for the request and
another for the response. In HAPRoxy 1.5, these arrays are part of the HTTP
transaction and thus are released during its cleanup. Because in this version,
the transaction is part of the stream (in 1.5, streams are still called
sessions), the cleanup is always performed, for HTTP and TCP streams.
In HAProxy 1.6, the HTTP transaction was moved out from the stream and is now
dynamically allocated only when required (becaues of an HTTP proxy or an HTTP
sample fetch). In addition, still in 1.6, the captures arrays were moved from
the HTTP transaction to the stream. This way, it is still possible to capture
elements from TCP rules for a full TCP stream. Unfortunately, the release is
still exclusively performed during the HTTP transaction cleanup. Thus, for a TCP
stream where the HTTP transaction is not required, the TCP captures, if any, are
never released.
Now, all captures are released when the stream is freed. This fixes the memory
leak for TCP streams. For streams with an HTTP transaction, the captures are now
released when the transaction is reset and not systematically during its
cleanup.
This patch must be backported as fas as 1.6.
2019-11-07 08:27:52 -05:00
}
2006-06-25 20:48:02 -04:00
}
2009-12-22 09:03:09 -05:00
2015-06-06 13:29:07 -04:00
/* Cleanup all variable contexts. */
2024-09-15 17:15:59 -04:00
vars_prune ( & s - > vars_txn , s - > sess , s ) ;
vars_prune ( & s - > vars_reqres , s - > sess , s ) ;
2015-06-06 13:29:07 -04:00
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream_store_counters ( s ) ;
2023-01-06 10:09:58 -05:00
pool_free ( pool_head_stk_ctr , s - > stkctr ) ;
2010-06-14 15:04:55 -04:00
2008-12-07 14:16:23 -05:00
list_for_each_entry_safe ( bref , back , & s - > back_refs , users ) {
2009-02-22 09:17:24 -05:00
/* we have to unlink all watchers. We must not relink them if
2021-02-24 07:46:12 -05:00
* this stream was the last one in the list . This is safe to do
* here because we ' re touching our thread ' s list so we know
* that other streams are not active , and the watchers will
* only touch their node under thread isolation .
2009-02-22 09:17:24 -05:00
*/
2021-02-24 07:46:12 -05:00
LIST_DEL_INIT ( & bref - > users ) ;
2021-09-30 13:02:18 -04:00
if ( s - > list . n ! = & th_ctx - > streams )
2021-04-21 01:32:39 -04:00
LIST_APPEND ( & LIST_ELEM ( s - > list . n , struct stream * , list ) - > back_refs , & bref - > users ) ;
2008-12-07 14:16:23 -05:00
bref - > ref = s - > list . n ;
2021-02-24 07:46:12 -05:00
__ha_barrier_store ( ) ;
2008-12-07 14:16:23 -05:00
}
2021-04-21 01:32:39 -04:00
LIST_DELETE ( & s - > list ) ;
2024-09-25 03:59:11 -04:00
_HA_ATOMIC_DEC ( & th_ctx - > stream_cnt ) ;
2017-06-30 10:23:45 -04:00
2022-05-27 02:33:53 -04:00
sc_destroy ( s - > scb ) ;
sc_destroy ( s - > scf ) ;
2025-02-06 11:07:48 -05:00
/*
* Now we ' ve free ' d the connection , so if we ' re running with
* strict - maxconn , now is a good time to free the slot , and see
* if we can dequeue anything .
*/
if ( s - > srv_conn & & ( s - > srv_conn - > flags & SRV_F_STRICT_MAXCONN ) ) {
struct server * oldsrv = s - > srv_conn ;
if ( ( oldsrv - > flags & SRV_F_STRICT_MAXCONN ) ) {
sess_change_server ( s , NULL ) ;
if ( may_dequeue_tasks ( oldsrv , s - > be ) )
process_srv_queue ( oldsrv ) ;
}
}
2018-10-11 11:09:14 -04:00
2017-11-24 11:34:44 -05:00
pool_free ( pool_head_stream , s ) ;
2007-07-11 04:42:35 -04:00
/* We may want to free the maximum amount of pools if the proxy is stopping */
2021-10-06 08:24:19 -04:00
if ( fe & & unlikely ( fe - > flags & ( PR_FL_DISABLED | PR_FL_STOPPED ) ) ) {
2017-11-24 11:34:44 -05:00
pool_flush ( pool_head_buffer ) ;
pool_flush ( pool_head_http_txn ) ;
pool_flush ( pool_head_requri ) ;
pool_flush ( pool_head_capture ) ;
pool_flush ( pool_head_stream ) ;
pool_flush ( pool_head_session ) ;
pool_flush ( pool_head_connection ) ;
pool_flush ( pool_head_pendconn ) ;
pool_flush ( fe - > req_cap_pool ) ;
pool_flush ( fe - > rsp_cap_pool ) ;
2007-07-11 04:42:35 -04:00
}
2007-05-13 13:43:47 -04:00
}
2014-11-25 13:46:36 -05:00
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* Allocates a work buffer for stream <s>. It is meant to be called inside
* process_stream ( ) . It will only allocate the side needed for the function
2015-04-20 09:52:18 -04:00
* to work fine , which is the response buffer so that an error message may be
* built and returned . Response buffers may be allocated from the reserve , this
* is critical to ensure that a response may always flow and will never block a
* server from releasing a connection . Returns 0 in case of failure , non - zero
* otherwise .
2014-11-25 13:46:36 -05:00
*/
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
static int stream_alloc_work_buffer ( struct stream * s )
2014-11-25 13:46:36 -05:00
{
2024-05-07 11:54:03 -04:00
if ( b_alloc ( & s - > res . buf , DB_CHANNEL | ( ( s - > flags & SF_MAYALLOC ) ? DB_F_NOQUEUE : 0 ) ) ) {
2024-05-07 11:45:31 -04:00
s - > flags & = ~ SF_MAYALLOC ;
2014-11-25 13:46:36 -05:00
return 1 ;
2024-05-07 11:45:31 -04:00
}
2024-04-29 02:52:40 -04:00
b_requeue ( DB_CHANNEL , & s - > buffer_wait ) ;
2014-11-25 13:46:36 -05:00
return 0 ;
}
/* releases unused buffers after processing. Typically used at the end of the
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
* update ( ) functions . It will try to wake up as many tasks / applets as the
* number of buffers that it releases . In practice , most often streams are
* blocked on a single buffer , so it makes sense to try to wake two up when two
* buffers are released at once .
2014-11-25 13:46:36 -05:00
*/
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
void stream_release_buffers ( struct stream * s )
2014-11-25 13:46:36 -05:00
{
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
int offer = 0 ;
2014-11-25 13:46:36 -05:00
2018-07-10 03:53:31 -04:00
if ( c_size ( & s - > req ) & & c_empty ( & s - > req ) ) {
2021-02-20 06:02:46 -05:00
offer + + ;
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
b_free ( & s - > req . buf ) ;
}
2018-07-10 03:53:31 -04:00
if ( c_size ( & s - > res ) & & c_empty ( & s - > res ) ) {
2021-02-20 06:02:46 -05:00
offer + + ;
2014-11-27 14:45:39 -05:00
b_free ( & s - > res . buf ) ;
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
}
2014-11-25 13:46:36 -05:00
MAJOR: session: implement a wait-queue for sessions who need a buffer
When a session_alloc_buffers() fails to allocate one or two buffers,
it subscribes the session to buffer_wq, and waits for another session
to release buffers. It's then removed from the queue and woken up with
TASK_WAKE_RES, and can attempt its allocation again.
We decide to try to wake as many waiters as we release buffers so
that if we release 2 and two waiters need only once, they both have
their chance. We must never come to the situation where we don't wake
enough tasks up.
It's common to release buffers after the completion of an I/O callback,
which can happen even if the I/O could not be performed due to half a
failure on memory allocation. In this situation, we don't want to move
out of the wait queue the session that was just added, otherwise it
will never get any buffer. Thus, we only force ourselves out of the
queue when freeing the session.
Note: at the moment, since session_alloc_buffers() is not used, no task
is subscribed to the wait queue.
2014-11-25 15:10:35 -05:00
/* if we're certain to have at least 1 buffer available, and there is
* someone waiting , we can wake up a waiter and offer them .
*/
BUG/MAJOR: Fix how the list of entities waiting for a buffer is handled
When an entity tries to get a buffer, if it cannot be allocted, for example
because the number of buffers which may be allocated per process is limited,
this entity is added in a list (called <buffer_wq>) and wait for an available
buffer.
Historically, the <buffer_wq> list was logically attached to streams because it
were the only entities likely to be added in it. Now, applets can also be
waiting for a free buffer. And with filters, we could imagine to have more other
entities waiting for a buffer. So it make sense to have a generic list.
Anyway, with the current design there is a bug. When an applet failed to get a
buffer, it will wait. But we add the stream attached to the applet in
<buffer_wq>, instead of the applet itself. So when a buffer is available, we
wake up the stream and not the waiting applet. So, it is possible to have
waiting applets and never awakened.
So, now, <buffer_wq> is independant from streams. And we really add the waiting
entity in <buffer_wq>. To be generic, the entity is responsible to define the
callback used to awaken it.
In addition, applets will still request an input buffer when they become
active. But they will not be sleeped anymore if no buffer are available. So this
is the responsibility to the applet I/O handler to check if this buffer is
allocated or not. This way, an applet can decide if this buffer is required or
not and can do additional processing if not.
[wt: backport to 1.7 and 1.6]
2016-12-09 11:30:18 -05:00
if ( offer )
2021-02-20 06:02:46 -05:00
offer_buffers ( s , offer ) ;
2014-11-25 13:46:36 -05:00
}
2007-05-13 13:43:47 -04:00
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
void stream_process_counters ( struct stream * s )
2007-11-26 14:15:35 -05:00
{
2015-04-03 08:46:27 -04:00
struct session * sess = s - > sess ;
2007-11-24 16:12:47 -05:00
unsigned long long bytes ;
2012-12-09 09:55:40 -05:00
int i ;
2007-11-24 16:12:47 -05:00
2014-11-27 14:45:39 -05:00
bytes = s - > req . total - s - > logs . bytes_in ;
s - > logs . bytes_in = s - > req . total ;
if ( bytes ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > bytes_in , bytes ) ;
_HA_ATOMIC_ADD ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > bytes_in , bytes ) ;
2009-10-04 09:43:17 -04:00
2014-11-27 14:45:39 -05:00
if ( objt_server ( s - > target ) )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & __objt_server ( s - > target ) - > counters . shared - > tg [ tgid - 1 ] - > bytes_in , bytes ) ;
2010-06-18 12:33:32 -04:00
2015-04-03 08:46:27 -04:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > bytes_in , bytes ) ;
2010-06-20 05:56:30 -04:00
2023-01-06 10:09:58 -05:00
for ( i = 0 ; i < global . tune . nb_stk_ctr ; i + + ) {
2020-10-06 07:52:40 -04:00
if ( ! stkctr_inc_bytes_in_ctr ( & s - > stkctr [ i ] , bytes ) )
stkctr_inc_bytes_in_ctr ( & sess - > stkctr [ i ] , bytes ) ;
2007-11-26 14:15:35 -05:00
}
2007-11-24 16:12:47 -05:00
}
2014-11-27 14:45:39 -05:00
bytes = s - > res . total - s - > logs . bytes_out ;
s - > logs . bytes_out = s - > res . total ;
if ( bytes ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > bytes_out , bytes ) ;
_HA_ATOMIC_ADD ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > bytes_out , bytes ) ;
2009-10-04 09:43:17 -04:00
2014-11-27 14:45:39 -05:00
if ( objt_server ( s - > target ) )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & __objt_server ( s - > target ) - > counters . shared - > tg [ tgid - 1 ] - > bytes_out , bytes ) ;
2010-08-03 10:29:52 -04:00
2015-04-03 08:46:27 -04:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_ADD ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > bytes_out , bytes ) ;
2010-06-20 05:56:30 -04:00
2023-01-06 10:09:58 -05:00
for ( i = 0 ; i < global . tune . nb_stk_ctr ; i + + ) {
2020-10-06 07:52:40 -04:00
if ( ! stkctr_inc_bytes_out_ctr ( & s - > stkctr [ i ] , bytes ) )
stkctr_inc_bytes_out_ctr ( & sess - > stkctr [ i ] , bytes ) ;
2007-11-26 14:15:35 -05:00
}
2007-11-24 16:12:47 -05:00
}
}
2006-06-25 20:48:02 -04:00
2023-04-13 09:22:29 -04:00
/* Abort processing on the both channels in same time */
void stream_abort ( struct stream * s )
{
channel_abort ( & s - > req ) ;
channel_abort ( & s - > res ) ;
}
2022-03-31 03:47:24 -04:00
/*
* Returns a message to the client ; the connection is shut down for read ,
* and the request is cleared so that no server connection can be initiated .
* The buffer is marked for read shutdown on the other side to protect the
* message , and the buffer write is enabled . The message is contained in a
* " chunk " . If it is null , then an empty message is used . The reply buffer does
* not need to be empty before this , and its contents will not be overwritten .
* The primary goal of this function is to return error messages to a client .
*/
void stream_retnclose ( struct stream * s , const struct buffer * msg )
{
struct channel * ic = & s - > req ;
struct channel * oc = & s - > res ;
channel_auto_read ( ic ) ;
channel_abort ( ic ) ;
channel_erase ( ic ) ;
channel_truncate ( oc ) ;
if ( likely ( msg & & msg - > data ) )
co_inject ( oc , msg - > area , msg - > data ) ;
channel_auto_read ( oc ) ;
channel_auto_close ( oc ) ;
2023-04-13 09:40:10 -04:00
sc_schedule_abort ( s - > scb ) ;
2022-03-31 03:47:24 -04:00
}
2020-12-10 07:43:51 -05:00
int stream_set_timeout ( struct stream * s , enum act_timeout_name name , int timeout )
{
2020-12-10 07:43:52 -05:00
switch ( name ) {
2023-09-27 10:43:07 -04:00
case ACT_TIMEOUT_CLIENT :
s - > scf - > ioto = timeout ;
return 1 ;
2020-12-10 07:43:52 -05:00
case ACT_TIMEOUT_SERVER :
2023-02-15 02:13:33 -05:00
s - > scb - > ioto = timeout ;
2020-12-10 07:43:52 -05:00
return 1 ;
2020-12-10 07:43:53 -05:00
case ACT_TIMEOUT_TUNNEL :
s - > tunnel_timeout = timeout ;
return 1 ;
2020-12-10 07:43:52 -05:00
default :
return 0 ;
}
2020-12-10 07:43:51 -05:00
}
2008-11-30 12:47:21 -05:00
/*
2022-05-17 13:47:17 -04:00
* This function handles the transition between the SC_ST_CON state and the
* SC_ST_EST state . It must only be called after switching from SC_ST_CON ( or
* SC_ST_INI or SC_ST_RDY ) to SC_ST_EST , but only when a - > proto is defined .
* Note that it will switch the interface to SC_ST_DIS if we already have
2023-04-13 10:05:13 -04:00
* the SC_FL_ABRT_DONE flag , it means we were able to forward the request , and
2019-05-21 11:43:50 -04:00
* receive the response , before process_stream ( ) had the opportunity to
2022-05-17 13:47:17 -04:00
* make the switch from SC_ST_CON to SC_ST_EST . When that happens , we want
2020-01-09 12:43:15 -05:00
* to go through back_establish ( ) anyway , to make sure the analysers run .
MEDIUM: stream: re-arrange the connection setup status reporting
Till now when a wakeup happens after a connection is attempted, we go
through sess_update_st_con_tcp() to deal with the various possible events,
then to sess_update_st_cer() to deal with a possible error detected by the
former, or to sess_establish() to complete the connection validation. There
are multiple issues in the way this is handled, which have accumulated over
time. One of them is that any spurious wakeup during SI_ST_CON would validate
the READ_ATTACHED flag and wake the analysers up. Another one is that nobody
feels responsible for clearing SI_FL_EXP if it happened at the same time as
a success (and it is present in all reports of loops to date). And another
issue is that aborts cannot happen after a clean connection setup with no
data transfer (since CF_WRITE_NULL is part of CF_WRITE_ACTIVITY). Last, the
flags cleanup work was hackish, added here and there to please the next
function (typically what had to be donne in commit 7a3367cca to work around
the url_param+reuse issue by moving READ_ATTACHED to CON).
This patch performs a significant lift up of this setup code. First, it
makes sure that the state handlers are the ones responsible for the cleanup
of the stuff they rely on. Typically sess_sestablish() will clean up the
SI_FL_EXP flag because if we decided to validate the connection it means
that we want to ignore this late timeout. Second, it splits the CON and
RDY state handlers because the former only has to deal with failures,
timeouts and non-events, while the latter has to deal with partial or
total successes. Third, everything related to connection success was
moved to sess_establish() since it's the only safe place to do so, and
this function is also called at a few places to deal with synchronous
connections, which are not seen by intermediary state handlers.
The code was made a bit more robust, for example by making sure we
always set SI_FL_NOLINGER when aborting a connection so that we don't
have any risk to leave a connection in SHUTW state in case it was
validated late. The useless return codes of some of these functions
were dropped so that callers only rely on the stream-int's state now
(which was already partially the case anyway).
The code is now a bit cleaner, could be further improved (and functions
renamed) but given the sensitivity of this part, better limit changes to
strictly necessary. It passes all reg tests.
2019-06-05 12:02:04 -04:00
* Timeouts are cleared . Error are reported on the channel so that analysers
* can handle them .
2008-11-30 12:47:21 -05:00
*/
DEBUG: unstatify a few functions that are often present in backtraces
It's useful to be able to recognize certain functions that are often
present in backtraces as they call lower level functions, and for this
they must not be static. Let's remove "static" in front of these
functions:
sc_notify, sc_conn_recv, sc_conn_send, sc_conn_process,
sc_applet_process, back_establish, stream_update_both_sc,
httpclient_applet_io_handler, httpclient_applet_init,
httpclient_applet_release
2023-11-30 11:04:16 -05:00
void back_establish ( struct stream * s )
2008-11-30 12:47:21 -05:00
{
2022-05-18 10:23:22 -04:00
struct connection * conn = sc_conn ( s - > scb ) ;
2014-11-28 09:15:44 -05:00
struct channel * req = & s - > req ;
struct channel * rep = & s - > res ;
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
uint8_t do_log = 0 ;
2008-11-30 12:47:21 -05:00
2022-03-31 03:16:34 -04:00
DBG_TRACE_ENTER ( STRM_EV_STRM_PROC | STRM_EV_CS_ST , s ) ;
MEDIUM: stream: re-arrange the connection setup status reporting
Till now when a wakeup happens after a connection is attempted, we go
through sess_update_st_con_tcp() to deal with the various possible events,
then to sess_update_st_cer() to deal with a possible error detected by the
former, or to sess_establish() to complete the connection validation. There
are multiple issues in the way this is handled, which have accumulated over
time. One of them is that any spurious wakeup during SI_ST_CON would validate
the READ_ATTACHED flag and wake the analysers up. Another one is that nobody
feels responsible for clearing SI_FL_EXP if it happened at the same time as
a success (and it is present in all reports of loops to date). And another
issue is that aborts cannot happen after a clean connection setup with no
data transfer (since CF_WRITE_NULL is part of CF_WRITE_ACTIVITY). Last, the
flags cleanup work was hackish, added here and there to please the next
function (typically what had to be donne in commit 7a3367cca to work around
the url_param+reuse issue by moving READ_ATTACHED to CON).
This patch performs a significant lift up of this setup code. First, it
makes sure that the state handlers are the ones responsible for the cleanup
of the stuff they rely on. Typically sess_sestablish() will clean up the
SI_FL_EXP flag because if we decided to validate the connection it means
that we want to ignore this late timeout. Second, it splits the CON and
RDY state handlers because the former only has to deal with failures,
timeouts and non-events, while the latter has to deal with partial or
total successes. Third, everything related to connection success was
moved to sess_establish() since it's the only safe place to do so, and
this function is also called at a few places to deal with synchronous
connections, which are not seen by intermediary state handlers.
The code was made a bit more robust, for example by making sure we
always set SI_FL_NOLINGER when aborting a connection so that we don't
have any risk to leave a connection in SHUTW state in case it was
validated late. The useless return codes of some of these functions
were dropped so that callers only rely on the stream-int's state now
(which was already partially the case anyway).
The code is now a bit cleaner, could be further improved (and functions
renamed) but given the sensitivity of this part, better limit changes to
strictly necessary. It passes all reg tests.
2019-06-05 12:02:04 -04:00
/* First, centralize the timers information, and clear any irrelevant
* timeout .
*/
2023-04-28 03:16:15 -04:00
s - > logs . t_connect = ns_to_ms ( now_ns - s - > logs . accept_ts ) ;
2022-03-29 13:02:31 -04:00
s - > conn_exp = TICK_ETERNITY ;
s - > flags & = ~ SF_CONN_EXP ;
MEDIUM: stream: re-arrange the connection setup status reporting
Till now when a wakeup happens after a connection is attempted, we go
through sess_update_st_con_tcp() to deal with the various possible events,
then to sess_update_st_cer() to deal with a possible error detected by the
former, or to sess_establish() to complete the connection validation. There
are multiple issues in the way this is handled, which have accumulated over
time. One of them is that any spurious wakeup during SI_ST_CON would validate
the READ_ATTACHED flag and wake the analysers up. Another one is that nobody
feels responsible for clearing SI_FL_EXP if it happened at the same time as
a success (and it is present in all reports of loops to date). And another
issue is that aborts cannot happen after a clean connection setup with no
data transfer (since CF_WRITE_NULL is part of CF_WRITE_ACTIVITY). Last, the
flags cleanup work was hackish, added here and there to please the next
function (typically what had to be donne in commit 7a3367cca to work around
the url_param+reuse issue by moving READ_ATTACHED to CON).
This patch performs a significant lift up of this setup code. First, it
makes sure that the state handlers are the ones responsible for the cleanup
of the stuff they rely on. Typically sess_sestablish() will clean up the
SI_FL_EXP flag because if we decided to validate the connection it means
that we want to ignore this late timeout. Second, it splits the CON and
RDY state handlers because the former only has to deal with failures,
timeouts and non-events, while the latter has to deal with partial or
total successes. Third, everything related to connection success was
moved to sess_establish() since it's the only safe place to do so, and
this function is also called at a few places to deal with synchronous
connections, which are not seen by intermediary state handlers.
The code was made a bit more robust, for example by making sure we
always set SI_FL_NOLINGER when aborting a connection so that we don't
have any risk to leave a connection in SHUTW state in case it was
validated late. The useless return codes of some of these functions
were dropped so that callers only rely on the stream-int's state now
(which was already partially the case anyway).
The code is now a bit cleaner, could be further improved (and functions
renamed) but given the sensitivity of this part, better limit changes to
strictly necessary. It passes all reg tests.
2019-06-05 12:02:04 -04:00
/* errors faced after sending data need to be reported */
2023-04-14 06:07:26 -04:00
if ( ( s - > scb - > flags & SC_FL_ERROR ) & & req - > flags & CF_WROTE_DATA ) {
2023-01-26 10:18:09 -05:00
s - > req . flags | = CF_WRITE_EVENT ;
s - > res . flags | = CF_READ_EVENT ;
2022-03-30 13:39:30 -04:00
s - > conn_err_type = STRM_ET_DATA_ERR ;
2022-03-31 03:16:34 -04:00
DBG_TRACE_STATE ( " read/write error " , STRM_EV_STRM_PROC | STRM_EV_CS_ST | STRM_EV_STRM_ERR , s ) ;
MEDIUM: stream: re-arrange the connection setup status reporting
Till now when a wakeup happens after a connection is attempted, we go
through sess_update_st_con_tcp() to deal with the various possible events,
then to sess_update_st_cer() to deal with a possible error detected by the
former, or to sess_establish() to complete the connection validation. There
are multiple issues in the way this is handled, which have accumulated over
time. One of them is that any spurious wakeup during SI_ST_CON would validate
the READ_ATTACHED flag and wake the analysers up. Another one is that nobody
feels responsible for clearing SI_FL_EXP if it happened at the same time as
a success (and it is present in all reports of loops to date). And another
issue is that aborts cannot happen after a clean connection setup with no
data transfer (since CF_WRITE_NULL is part of CF_WRITE_ACTIVITY). Last, the
flags cleanup work was hackish, added here and there to please the next
function (typically what had to be donne in commit 7a3367cca to work around
the url_param+reuse issue by moving READ_ATTACHED to CON).
This patch performs a significant lift up of this setup code. First, it
makes sure that the state handlers are the ones responsible for the cleanup
of the stuff they rely on. Typically sess_sestablish() will clean up the
SI_FL_EXP flag because if we decided to validate the connection it means
that we want to ignore this late timeout. Second, it splits the CON and
RDY state handlers because the former only has to deal with failures,
timeouts and non-events, while the latter has to deal with partial or
total successes. Third, everything related to connection success was
moved to sess_establish() since it's the only safe place to do so, and
this function is also called at a few places to deal with synchronous
connections, which are not seen by intermediary state handlers.
The code was made a bit more robust, for example by making sure we
always set SI_FL_NOLINGER when aborting a connection so that we don't
have any risk to leave a connection in SHUTW state in case it was
validated late. The useless return codes of some of these functions
were dropped so that callers only rely on the stream-int's state now
(which was already partially the case anyway).
The code is now a bit cleaner, could be further improved (and functions
renamed) but given the sensitivity of this part, better limit changes to
strictly necessary. It passes all reg tests.
2019-06-05 12:02:04 -04:00
}
2012-11-11 18:42:33 -05:00
if ( objt_server ( s - > target ) )
2021-12-06 02:01:02 -05:00
health_adjust ( __objt_server ( s - > target ) , HANA_STATUS_L4_OK ) ;
2009-12-15 16:31:24 -05:00
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
if ( strm_fe ( s ) - > to_log = = LW_LOGSTEPS ) {
if ( log_orig_proxy ( LOG_ORIG_TXN_CONNECT , strm_fe ( s ) ) )
do_log = 1 ;
}
2021-03-08 11:57:53 -05:00
if ( ! IS_HTX_STRM ( s ) ) { /* let's allow immediate data connection in this case */
2008-11-30 12:47:21 -05:00
/* if the user wants to log as soon as possible, without counting
* bytes from the server , then this is the right moment . */
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
if ( strm_fe ( s ) - > to_log ! = LW_LOGSTEPS & &
! lf_expr_isempty ( & strm_fe ( s ) - > logformat ) & & ! ( s - > logs . logwait & LW_BYTES ) )
do_log = 1 ;
2008-11-30 12:47:21 -05:00
}
else {
2023-03-16 09:40:03 -04:00
s - > scb - > flags | = SC_FL_RCV_ONCE ; /* a single read is enough to get response headers */
2008-11-30 12:47:21 -05:00
}
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
if ( do_log ) {
/* note: no pend_pos here, session is established */
s - > logs . t_close = s - > logs . t_connect ; /* to get a valid end date */
s - > do_log ( s , log_orig ( LOG_ORIG_TXN_CONNECT , LOG_ORIG_FL_NONE ) ) ;
}
2018-12-11 12:01:38 -05:00
rep - > analysers | = strm_fe ( s ) - > fe_rsp_ana | s - > be - > be_rsp_ana ;
2015-12-02 03:57:32 -05:00
2022-05-25 09:42:03 -04:00
se_have_more_data ( s - > scb - > sedesc ) ;
2022-12-20 12:47:39 -05:00
rep - > flags | = CF_READ_EVENT ; /* producer is now attached */
2023-02-16 05:09:31 -05:00
sc_ep_report_read_activity ( s - > scb ) ;
2021-12-15 03:50:17 -05:00
if ( conn ) {
2020-12-10 07:43:52 -05:00
/* real connections have timeouts
* if already defined , it means that a set - timeout rule has
* been executed so do not overwrite them
*/
2023-02-15 02:13:33 -05:00
if ( ! tick_isset ( s - > scb - > ioto ) )
s - > scb - > ioto = s - > be - > timeout . server ;
2020-12-10 07:43:53 -05:00
if ( ! tick_isset ( s - > tunnel_timeout ) )
s - > tunnel_timeout = s - > be - > timeout . tunnel ;
2020-12-10 07:43:52 -05:00
2018-11-07 11:55:19 -05:00
/* The connection is now established, try to read data from the
* underlying layer , and subscribe to recv events . We use a
* delayed recv here to give a chance to the data to flow back
* by the time we process other tasks .
*/
2022-05-27 03:00:19 -04:00
sc_chk_rcv ( s - > scb ) ;
2010-05-31 06:31:35 -04:00
}
2019-07-26 08:54:34 -04:00
/* If we managed to get the whole response, and we don't have anything
2022-05-17 13:47:17 -04:00
* left to send , or can ' t , switch to SC_ST_DIS now . */
2023-04-17 10:17:32 -04:00
if ( ( s - > scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) | | ( s - > scf - > flags & SC_FL_SHUT_DONE ) ) {
2022-05-17 13:47:17 -04:00
s - > scb - > state = SC_ST_DIS ;
2022-03-31 03:16:34 -04:00
DBG_TRACE_STATE ( " response channel shutdwn for read/write " , STRM_EV_STRM_PROC | STRM_EV_CS_ST | STRM_EV_STRM_ERR , s ) ;
2019-11-05 10:18:10 -05:00
}
2022-03-31 03:16:34 -04:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_PROC | STRM_EV_CS_ST , s ) ;
2008-11-30 12:47:21 -05:00
}
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* Set correct stream termination flags in case no analyser has done it. It
2011-06-07 20:19:07 -04:00
* also counts a failed request if the server state has not reached the request
* stage .
*/
2023-04-13 08:46:01 -04:00
void sess_set_term_flags ( struct stream * s )
2011-06-07 20:19:07 -04:00
{
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_FINST_MASK ) ) {
2022-05-17 13:47:17 -04:00
if ( s - > scb - > state = = SC_ST_INI ) {
2019-06-05 08:53:22 -04:00
/* anything before REQ in fact */
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & strm_fe ( s ) - > fe_counters . shared - > tg [ tgid - 1 ] - > failed_req ) ;
2015-09-23 06:21:21 -04:00
if ( strm_li ( s ) & & strm_li ( s ) - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & strm_li ( s ) - > counters - > shared - > tg [ tgid - 1 ] - > failed_req ) ;
2011-06-07 20:19:07 -04:00
2015-04-02 19:14:29 -04:00
s - > flags | = SF_FINST_R ;
2011-06-07 20:19:07 -04:00
}
2022-05-17 13:47:17 -04:00
else if ( s - > scb - > state = = SC_ST_QUE )
2015-04-02 19:14:29 -04:00
s - > flags | = SF_FINST_Q ;
2022-05-27 03:03:30 -04:00
else if ( sc_state_in ( s - > scb - > state , SC_SB_REQ | SC_SB_TAR | SC_SB_ASS | SC_SB_CON | SC_SB_CER | SC_SB_RDY ) )
2015-04-02 19:14:29 -04:00
s - > flags | = SF_FINST_C ;
2022-05-17 13:47:17 -04:00
else if ( s - > scb - > state = = SC_ST_EST | | s - > prev_conn_state = = SC_ST_EST )
2015-04-02 19:14:29 -04:00
s - > flags | = SF_FINST_D ;
2011-06-07 20:19:07 -04:00
else
2015-04-02 19:14:29 -04:00
s - > flags | = SF_FINST_L ;
2011-06-07 20:19:07 -04:00
}
}
2015-09-27 13:29:33 -04:00
/* This function parses the use-service action ruleset. It executes
* the associated ACL and set an applet as a stream or txn final node .
* it returns ACT_RET_ERR if an error occurs , the proxy left in
2020-05-05 15:53:22 -04:00
* consistent state . It returns ACT_RET_STOP in success case because
2015-09-27 13:29:33 -04:00
* use - service must be a terminal action . Returns ACT_RET_YIELD
* if the initialisation function require more data .
*/
enum act_return process_use_service ( struct act_rule * rule , struct proxy * px ,
struct session * sess , struct stream * s , int flags )
{
struct appctx * appctx ;
/* Initialises the applet if it is required. */
2019-12-18 08:41:51 -05:00
if ( flags & ACT_OPT_FIRST ) {
2015-09-27 13:29:33 -04:00
/* Register applet. this function schedules the applet. */
s - > target = & rule - > applet . obj_type ;
2022-05-27 02:33:53 -04:00
appctx = sc_applet_create ( s - > scb , objt_applet ( s - > target ) ) ;
2022-02-24 07:45:27 -05:00
if ( unlikely ( ! appctx ) )
2015-09-27 13:29:33 -04:00
return ACT_RET_ERR ;
2022-01-19 08:56:50 -05:00
/* Finish initialisation of the context. */
2025-04-24 10:08:48 -04:00
s - > current_rule = rule ;
2022-05-12 08:59:28 -04:00
if ( appctx_init ( appctx ) = = - 1 )
2022-01-13 10:01:35 -05:00
return ACT_RET_ERR ;
2025-04-24 10:08:48 -04:00
s - > current_rule = NULL ;
2015-09-27 13:29:33 -04:00
}
else
2022-05-18 11:58:02 -04:00
appctx = __sc_appctx ( s - > scb ) ;
2015-09-27 13:29:33 -04:00
2019-03-01 05:44:26 -05:00
if ( rule - > from ! = ACT_F_HTTP_REQ ) {
if ( sess - > fe = = s - > be ) /* report it if the request was intercepted by the frontend */
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > intercepted_req ) ;
2019-03-01 05:44:26 -05:00
/* The flag SF_ASSIGNED prevent from server assignment. */
s - > flags | = SF_ASSIGNED ;
}
2015-09-27 13:29:33 -04:00
/* Now we can schedule the applet. */
2022-05-25 12:21:43 -04:00
applet_need_more_data ( appctx ) ;
2015-09-27 13:29:33 -04:00
appctx_wakeup ( appctx ) ;
return ACT_RET_STOP ;
}
2009-07-07 09:10:31 -04:00
/* This stream analyser checks the switching rules and changes the backend
2010-01-22 13:10:05 -05:00
* if appropriate . The default_backend rule is also considered , then the
* target backend ' s forced persistence rules are also evaluated last if any .
2009-07-07 09:10:31 -04:00
* It returns 1 if the processing can continue on next analysers , or zero if it
* either needs more data or wants to immediately abort the request .
*/
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
static int process_switching_rules ( struct stream * s , struct channel * req , int an_bit )
2009-07-07 09:10:31 -04:00
{
2010-04-24 18:00:51 -04:00
struct persist_rule * prst_rule ;
2015-04-03 19:47:55 -04:00
struct session * sess = s - > sess ;
struct proxy * fe = sess - > fe ;
2010-01-22 13:10:05 -05:00
2009-07-07 09:10:31 -04:00
req - > analysers & = ~ an_bit ;
req - > analyse_exp = TICK_ETERNITY ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_ENTER ( STRM_EV_STRM_ANA , s ) ;
2009-07-07 09:10:31 -04:00
/* now check whether we have some switching rules for this request */
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_BE_ASSIGNED ) ) {
2009-07-07 09:10:31 -04:00
struct switching_rule * rule ;
2015-04-03 09:40:56 -04:00
list_for_each_entry ( rule , & fe - > switching_rules , list ) {
2025-01-22 08:15:47 -05:00
struct proxy * backend = NULL ;
2009-07-07 09:10:31 -04:00
2025-01-22 08:15:47 -05:00
if ( ! acl_match_cond ( rule - > cond , fe , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL ) )
continue ;
2009-07-07 09:10:31 -04:00
2025-01-22 08:15:47 -05:00
/* If the backend name is dynamic, try to resolve the name.
* If we can ' t resolve the name , or if any error occurs , break
* the loop and fallback to the default backend .
*/
2013-11-19 05:43:06 -05:00
2025-01-22 08:15:47 -05:00
if ( rule - > dynamic ) {
struct buffer * tmp ;
2017-10-26 05:25:10 -04:00
2025-01-22 08:15:47 -05:00
tmp = alloc_trash_chunk ( ) ;
if ( ! tmp )
goto sw_failed ;
2017-10-26 05:25:10 -04:00
2025-01-22 08:15:47 -05:00
if ( build_logline ( s , tmp - > area , tmp - > size , & rule - > be . expr ) )
backend = proxy_be_by_name ( tmp - > area ) ;
2017-10-26 05:25:10 -04:00
2025-01-22 08:15:47 -05:00
free_trash_chunk ( tmp ) ;
tmp = NULL ;
2017-10-26 05:25:10 -04:00
2025-01-22 08:15:47 -05:00
if ( ! backend )
break ;
2009-07-07 09:10:31 -04:00
}
2025-01-22 08:15:47 -05:00
else
backend = rule - > be . backend ;
if ( ! stream_set_backend ( s , backend ) )
goto sw_failed ;
break ;
2009-07-07 09:10:31 -04:00
}
/* To ensure correct connection accounting on the backend, we
* have to assign one if it was not set ( eg : a listen ) . This
* measure also takes care of correctly setting the default
2021-03-22 10:07:51 -04:00
* backend if any . Don ' t do anything if an upgrade is already in
* progress .
2009-07-07 09:10:31 -04:00
*/
2021-03-22 10:07:51 -04:00
if ( ! ( s - > flags & ( SF_BE_ASSIGNED | SF_IGNORE ) ) )
2015-04-03 09:40:56 -04:00
if ( ! stream_set_backend ( s , fe - > defbe . be ? fe - > defbe . be : s - > be ) )
2009-07-12 02:27:39 -04:00
goto sw_failed ;
2021-03-22 10:07:51 -04:00
/* No backend assigned but no error reported. It happens when a
* TCP stream is upgraded to HTTP / 2.
*/
if ( ( s - > flags & ( SF_BE_ASSIGNED | SF_IGNORE ) ) = = SF_IGNORE ) {
DBG_TRACE_DEVEL ( " leaving with no backend because of a destructive upgrade " , STRM_EV_STRM_ANA , s ) ;
return 0 ;
}
2009-07-07 09:10:31 -04:00
}
2025-04-02 14:44:22 -04:00
/* Se the max connection retries for the stream. may be overwritten later */
2024-09-25 09:05:07 -04:00
s - > max_retries = s - > be - > conn_retries ;
2010-08-03 08:02:05 -04:00
/* we don't want to run the TCP or HTTP filters again if the backend has not changed */
2015-04-03 09:40:56 -04:00
if ( fe = = s - > be ) {
2014-11-27 14:45:39 -05:00
s - > req . analysers & = ~ AN_REQ_INSPECT_BE ;
s - > req . analysers & = ~ AN_REQ_HTTP_PROCESS_BE ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
s - > req . analysers & = ~ AN_REQ_FLT_START_BE ;
2010-08-03 08:02:05 -04:00
}
2009-07-07 09:10:31 -04:00
2010-04-24 18:00:51 -04:00
/* as soon as we know the backend, we must check if we have a matching forced or ignored
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* persistence rule , and report that in the stream .
2010-01-22 13:10:05 -05:00
*/
2010-04-24 18:00:51 -04:00
list_for_each_entry ( prst_rule , & s - > be - > persist_rules , list ) {
2025-01-22 08:15:47 -05:00
if ( ! acl_match_cond ( prst_rule - > cond , s - > be , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL ) )
continue ;
2010-01-22 13:10:05 -05:00
2025-01-22 08:15:47 -05:00
/* no rule, or the rule matches */
if ( prst_rule - > type = = PERSIST_TYPE_FORCE ) {
s - > flags | = SF_FORCE_PRST ;
} else {
s - > flags | = SF_IGNORE_PRST ;
2010-01-22 13:10:05 -05:00
}
2025-01-22 08:15:47 -05:00
break ;
2010-01-22 13:10:05 -05:00
}
2019-11-05 10:18:10 -05:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_ANA , s ) ;
2009-07-07 09:10:31 -04:00
return 1 ;
2009-07-12 02:27:39 -04:00
sw_failed :
/* immediately abort this request in case of allocation failure */
2023-04-13 09:22:29 -04:00
stream_abort ( s ) ;
2009-07-12 02:27:39 -04:00
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_ERR_MASK ) )
s - > flags | = SF_ERR_RESOURCE ;
if ( ! ( s - > flags & SF_FINST_MASK ) )
s - > flags | = SF_FINST_R ;
2009-07-12 02:27:39 -04:00
2015-04-03 17:46:31 -04:00
if ( s - > txn )
s - > txn - > status = 500 ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
s - > req . analysers & = AN_REQ_FLT_END ;
2014-11-27 14:45:39 -05:00
s - > req . analyse_exp = TICK_ETERNITY ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_DEVEL ( " leaving on error " , STRM_EV_STRM_ANA | STRM_EV_STRM_ERR , s ) ;
2009-07-12 02:27:39 -04:00
return 0 ;
2009-07-07 09:10:31 -04:00
}
2012-04-05 15:09:48 -04:00
/* This stream analyser works on a request. It applies all use-server rules on
* it then returns 1. The data must already be present in the buffer otherwise
* they won ' t match . It always returns 1.
*/
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
static int process_server_rules ( struct stream * s , struct channel * req , int an_bit )
2012-04-05 15:09:48 -04:00
{
struct proxy * px = s - > be ;
2015-04-03 19:47:55 -04:00
struct session * sess = s - > sess ;
2012-04-05 15:09:48 -04:00
struct server_rule * rule ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_ENTER ( STRM_EV_STRM_ANA , s ) ;
2012-04-05 15:09:48 -04:00
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_ASSIGNED ) ) {
2012-04-05 15:09:48 -04:00
list_for_each_entry ( rule , & px - > server_rules , list ) {
2025-01-22 08:15:47 -05:00
struct server * srv ;
2012-04-05 15:09:48 -04:00
2025-01-22 08:15:47 -05:00
if ( ! acl_match_cond ( rule - > cond , s - > be , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL ) )
continue ;
2020-03-29 03:37:12 -04:00
2025-01-22 08:15:47 -05:00
if ( rule - > dynamic ) {
struct buffer * tmp = get_trash_chunk ( ) ;
2020-03-29 03:37:12 -04:00
2025-01-22 08:15:47 -05:00
if ( ! build_logline ( s , tmp - > area , tmp - > size , & rule - > expr ) )
break ;
2020-03-29 03:37:12 -04:00
2025-01-22 08:15:47 -05:00
srv = findserver ( s - > be , tmp - > area ) ;
if ( ! srv )
2012-04-05 15:09:48 -04:00
break ;
}
2025-01-22 08:15:47 -05:00
else
srv = rule - > srv . ptr ;
if ( ( srv - > cur_state ! = SRV_ST_STOPPED ) | |
( px - > options & PR_O_PERSIST ) | |
( s - > flags & SF_FORCE_PRST ) ) {
s - > flags | = SF_DIRECT | SF_ASSIGNED ;
s - > target = & srv - > obj_type ;
break ;
}
/* if the server is not UP, let's go on with next rules
* just in case another one is suited .
*/
2012-04-05 15:09:48 -04:00
}
}
req - > analysers & = ~ an_bit ;
req - > analyse_exp = TICK_ETERNITY ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_ANA , s ) ;
2012-04-05 15:09:48 -04:00
return 1 ;
}
2019-05-20 04:08:27 -04:00
static inline void sticking_rule_find_target ( struct stream * s ,
struct stktable * t , struct stksess * ts )
{
struct proxy * px = s - > be ;
struct eb32_node * node ;
struct dict_entry * de ;
void * ptr ;
struct server * srv ;
/* Look for the server name previously stored in <t> stick-table */
HA_RWLOCK_RDLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
2020-11-20 03:28:26 -05:00
ptr = __stktable_data_ptr ( t , ts , STKTABLE_DT_SERVER_KEY ) ;
2021-06-30 11:18:28 -04:00
de = stktable_data_cast ( ptr , std_t_dict ) ;
2019-05-20 04:08:27 -04:00
HA_RWLOCK_RDUNLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
if ( de ) {
2020-11-20 03:28:26 -05:00
struct ebpt_node * node ;
2019-05-20 04:08:27 -04:00
2020-11-20 03:28:26 -05:00
if ( t - > server_key_type = = STKTABLE_SRV_NAME ) {
node = ebis_lookup ( & px - > conf . used_server_name , de - > value . key ) ;
if ( node ) {
srv = container_of ( node , struct server , conf . name ) ;
goto found ;
}
} else if ( t - > server_key_type = = STKTABLE_SRV_ADDR ) {
HA_RWLOCK_RDLOCK ( PROXY_LOCK , & px - > lock ) ;
node = ebis_lookup ( & px - > used_server_addr , de - > value . key ) ;
HA_RWLOCK_RDUNLOCK ( PROXY_LOCK , & px - > lock ) ;
if ( node ) {
srv = container_of ( node , struct server , addr_node ) ;
goto found ;
}
2019-05-20 04:08:27 -04:00
}
}
/* Look for the server ID */
HA_RWLOCK_RDLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
ptr = __stktable_data_ptr ( t , ts , STKTABLE_DT_SERVER_ID ) ;
2021-06-30 11:18:28 -04:00
node = eb32_lookup ( & px - > conf . used_server_id , stktable_data_cast ( ptr , std_t_sint ) ) ;
2019-05-20 04:08:27 -04:00
HA_RWLOCK_RDUNLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
if ( ! node )
return ;
srv = container_of ( node , struct server , conf . id ) ;
found :
if ( ( srv - > cur_state ! = SRV_ST_STOPPED ) | |
( px - > options & PR_O_PERSIST ) | | ( s - > flags & SF_FORCE_PRST ) ) {
s - > flags | = SF_DIRECT | SF_ASSIGNED ;
s - > target = & srv - > obj_type ;
}
}
2010-01-04 09:47:17 -05:00
/* This stream analyser works on a request. It applies all sticking rules on
* it then returns 1. The data must already be present in the buffer otherwise
* they won ' t match . It always returns 1.
*/
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
static int process_sticking_rules ( struct stream * s , struct channel * req , int an_bit )
2010-01-04 09:47:17 -05:00
{
struct proxy * px = s - > be ;
2015-04-03 19:47:55 -04:00
struct session * sess = s - > sess ;
2010-01-04 09:47:17 -05:00
struct sticking_rule * rule ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_ENTER ( STRM_EV_STRM_ANA , s ) ;
2010-01-04 09:47:17 -05:00
list_for_each_entry ( rule , & px - > sticking_rules , list ) {
2025-01-22 08:15:47 -05:00
struct stktable_key * key ;
2010-01-04 09:47:17 -05:00
int i ;
2013-12-09 06:52:13 -05:00
/* Only the first stick store-request of each table is applied
* and other ones are ignored . The purpose is to allow complex
* configurations which look for multiple entries by decreasing
* order of precision and to stop at the first which matches .
* An example could be a store of the IP address from an HTTP
* header first , then from the source if not found .
*/
2020-01-16 11:37:21 -05:00
if ( rule - > flags & STK_IS_STORE ) {
for ( i = 0 ; i < s - > store_count ; i + + ) {
if ( rule - > table . t = = s - > store [ i ] . table )
break ;
}
2010-01-04 09:47:17 -05:00
2020-01-16 11:37:21 -05:00
if ( i ! = s - > store_count )
continue ;
}
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
if ( ! acl_match_cond ( rule - > cond , px , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL ) )
continue ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
key = stktable_fetch_key ( rule - > table . t , px , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL , rule - > expr , NULL ) ;
if ( ! key )
continue ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
if ( rule - > flags & STK_IS_MATCH ) {
struct stksess * ts ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
if ( ( ts = stktable_lookup_key ( rule - > table . t , key ) ) ! = NULL ) {
if ( ! ( s - > flags & SF_ASSIGNED ) )
sticking_rule_find_target ( s , rule - > table . t , ts ) ;
stktable_touch_local ( rule - > table . t , ts , 1 ) ;
}
}
if ( rule - > flags & STK_IS_STORE ) {
if ( s - > store_count < ( sizeof ( s - > store ) / sizeof ( s - > store [ 0 ] ) ) ) {
2010-01-04 09:47:17 -05:00
struct stksess * ts ;
2025-01-22 08:15:47 -05:00
ts = stksess_new ( rule - > table . t , key ) ;
if ( ts ) {
s - > store [ s - > store_count ] . table = rule - > table . t ;
s - > store [ s - > store_count + + ] . ts = ts ;
2010-01-04 09:47:17 -05:00
}
}
}
}
req - > analysers & = ~ an_bit ;
req - > analyse_exp = TICK_ETERNITY ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_ANA , s ) ;
2010-01-04 09:47:17 -05:00
return 1 ;
}
/* This stream analyser works on a response. It applies all store rules on it
* then returns 1. The data must already be present in the buffer otherwise
* they won ' t match . It always returns 1.
*/
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
static int process_store_rules ( struct stream * s , struct channel * rep , int an_bit )
2010-01-04 09:47:17 -05:00
{
struct proxy * px = s - > be ;
2015-04-03 19:47:55 -04:00
struct session * sess = s - > sess ;
2010-01-04 09:47:17 -05:00
struct sticking_rule * rule ;
int i ;
2013-12-09 06:52:13 -05:00
int nbreq = s - > store_count ;
2010-01-04 09:47:17 -05:00
2019-11-05 10:18:10 -05:00
DBG_TRACE_ENTER ( STRM_EV_STRM_ANA , s ) ;
2010-01-04 09:47:17 -05:00
list_for_each_entry ( rule , & px - > storersp_rules , list ) {
2025-01-22 08:15:47 -05:00
struct stktable_key * key ;
2010-01-04 09:47:17 -05:00
2013-12-09 06:52:13 -05:00
/* Only the first stick store-response of each table is applied
* and other ones are ignored . The purpose is to allow complex
* configurations which look for multiple entries by decreasing
* order of precision and to stop at the first which matches .
* An example could be a store of a set - cookie value , with a
* fallback to a parameter found in a 302 redirect .
*
* The store - response rules are not allowed to override the
* store - request rules for the same table , but they may coexist .
* Thus we can have up to one store - request entry and one store -
* response entry for the same table at any time .
*/
for ( i = nbreq ; i < s - > store_count ; i + + ) {
if ( rule - > table . t = = s - > store [ i ] . table )
break ;
}
/* skip existing entries for this table */
if ( i < s - > store_count )
continue ;
2025-01-22 08:15:47 -05:00
if ( ! acl_match_cond ( rule - > cond , px , sess , s , SMP_OPT_DIR_RES | SMP_OPT_FINAL ) )
continue ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
key = stktable_fetch_key ( rule - > table . t , px , sess , s , SMP_OPT_DIR_RES | SMP_OPT_FINAL , rule - > expr , NULL ) ;
if ( ! key )
continue ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
if ( s - > store_count < ( sizeof ( s - > store ) / sizeof ( s - > store [ 0 ] ) ) ) {
struct stksess * ts ;
2010-01-04 09:47:17 -05:00
2025-01-22 08:15:47 -05:00
ts = stksess_new ( rule - > table . t , key ) ;
if ( ts ) {
s - > store [ s - > store_count ] . table = rule - > table . t ;
s - > store [ s - > store_count + + ] . ts = ts ;
2010-01-04 09:47:17 -05:00
}
}
}
/* process store request and store response */
for ( i = 0 ; i < s - > store_count ; i + + ) {
2010-06-06 09:38:59 -04:00
struct stksess * ts ;
2010-06-06 10:40:39 -04:00
void * ptr ;
2020-11-20 03:28:26 -05:00
char * key ;
2019-05-20 04:08:27 -04:00
struct dict_entry * de ;
2020-11-20 03:28:26 -05:00
struct stktable * t = s - > store [ i ] . table ;
2010-06-06 09:38:59 -04:00
2022-10-25 10:45:38 -04:00
if ( ! objt_server ( s - > target ) | | ( __objt_server ( s - > target ) - > flags & SRV_F_NON_STICK ) ) {
2011-06-24 20:39:49 -04:00
stksess_free ( s - > store [ i ] . table , s - > store [ i ] . ts ) ;
s - > store [ i ] . ts = NULL ;
continue ;
}
2020-11-20 03:28:26 -05:00
ts = stktable_set_entry ( t , s - > store [ i ] . ts ) ;
2017-06-13 13:37:32 -04:00
if ( ts ! = s - > store [ i ] . ts ) {
2010-06-06 09:38:59 -04:00
/* the entry already existed, we can free ours */
2020-11-20 03:28:26 -05:00
stksess_free ( t , s - > store [ i ] . ts ) ;
2010-01-04 09:47:17 -05:00
}
2010-06-06 09:38:59 -04:00
s - > store [ i ] . ts = NULL ;
2017-06-13 13:37:32 -04:00
2020-11-20 03:28:26 -05:00
if ( t - > server_key_type = = STKTABLE_SRV_NAME )
key = __objt_server ( s - > target ) - > id ;
else if ( t - > server_key_type = = STKTABLE_SRV_ADDR )
key = __objt_server ( s - > target ) - > addr_node . key ;
else
2022-10-12 04:35:41 -04:00
key = NULL ;
2020-11-20 03:28:26 -05:00
2019-05-20 04:08:27 -04:00
HA_RWLOCK_WRLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
2022-10-12 04:35:41 -04:00
ptr = __stktable_data_ptr ( t , ts , STKTABLE_DT_SERVER_ID ) ;
stktable_data_cast ( ptr , std_t_sint ) = __objt_server ( s - > target ) - > puid ;
if ( key ) {
de = dict_insert ( & server_key_dict , key ) ;
if ( de ) {
ptr = __stktable_data_ptr ( t , ts , STKTABLE_DT_SERVER_KEY ) ;
stktable_data_cast ( ptr , std_t_dict ) = de ;
}
2019-05-20 04:08:27 -04:00
}
2022-10-12 04:35:41 -04:00
2019-05-20 04:08:27 -04:00
HA_RWLOCK_WRUNLOCK ( STK_SESS_LOCK , & ts - > lock ) ;
2020-11-20 03:28:26 -05:00
stktable_touch_local ( t , ts , 1 ) ;
2010-01-04 09:47:17 -05:00
}
2010-06-18 03:57:45 -04:00
s - > store_count = 0 ; /* everything is stored */
2010-01-04 09:47:17 -05:00
rep - > analysers & = ~ an_bit ;
rep - > analyse_exp = TICK_ETERNITY ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_LEAVE ( STRM_EV_STRM_ANA , s ) ;
2010-01-04 09:47:17 -05:00
return 1 ;
}
2021-03-15 05:42:02 -04:00
/* Set the stream to HTTP mode, if necessary. The minimal request HTTP analysers
* are set and the client mux is upgraded . It returns 1 if the stream processing
* may continue or 0 if it should be stopped . It happens on error or if the
2021-03-15 07:03:44 -04:00
* upgrade required a new stream . The mux protocol may be specified .
2021-03-15 05:42:02 -04:00
*/
2021-03-15 07:03:44 -04:00
int stream_set_http_mode ( struct stream * s , const struct mux_proto_list * mux_proto )
2021-03-15 05:42:02 -04:00
{
2022-05-27 04:13:37 -04:00
struct stconn * sc = s - > scf ;
2021-03-15 05:42:02 -04:00
struct connection * conn ;
/* Already an HTTP stream */
if ( IS_HTX_STRM ( s ) )
return 1 ;
s - > req . analysers | = AN_REQ_WAIT_HTTP | AN_REQ_HTTP_PROCESS_FE ;
if ( unlikely ( ! s - > txn & & ! http_create_txn ( s ) ) )
return 0 ;
2022-05-27 04:13:37 -04:00
conn = sc_conn ( sc ) ;
2024-08-28 09:42:22 -04:00
if ( ! sc_conn_ready ( sc ) )
return 0 ;
2021-12-20 09:34:16 -05:00
if ( conn ) {
2022-05-25 09:42:03 -04:00
se_have_more_data ( s - > scf - > sedesc ) ;
2021-03-15 05:42:02 -04:00
/* Make sure we're unsubscribed, the the new
* mux will probably want to subscribe to
* the underlying XPRT
*/
2022-05-17 13:40:40 -04:00
if ( s - > scf - > wait_event . events )
2022-05-27 04:13:37 -04:00
conn - > mux - > unsubscribe ( sc , s - > scf - > wait_event . events , & ( s - > scf - > wait_event ) ) ;
2021-03-15 07:03:44 -04:00
2021-03-15 05:42:02 -04:00
if ( conn - > mux - > flags & MX_FL_NO_UPG )
return 0 ;
2022-06-16 10:24:16 -04:00
sc_conn_prepare_endp_upgrade ( sc ) ;
2022-05-27 04:13:37 -04:00
if ( conn_upgrade_mux_fe ( conn , sc , & s - > req . buf ,
2021-03-15 07:03:44 -04:00
( mux_proto ? mux_proto - > token : ist ( " " ) ) ,
2022-06-16 10:24:16 -04:00
PROTO_MODE_HTTP ) = = - 1 ) {
sc_conn_abort_endp_upgrade ( sc ) ;
2021-03-15 05:42:02 -04:00
return 0 ;
2022-06-16 10:24:16 -04:00
}
sc_conn_commit_endp_upgrade ( sc ) ;
2021-03-15 05:42:02 -04:00
2022-12-12 02:28:55 -05:00
s - > req . flags & = ~ ( CF_READ_EVENT | CF_AUTO_CONNECT ) ;
2021-03-15 05:42:02 -04:00
s - > req . total = 0 ;
s - > flags | = SF_IGNORE ;
2022-06-17 03:36:57 -04:00
if ( sc_ep_test ( sc , SE_FL_DETACHED ) ) {
/* If stream connector is detached, it means it was not
* reused by the new mux . Son destroy it , disable
* logging , and abort the stream process . Thus the
* stream will be silently destroyed . The new mux will
* create new streams .
2021-03-15 05:42:02 -04:00
*/
s - > logs . logwait = 0 ;
s - > logs . level = 0 ;
2023-04-13 09:22:29 -04:00
stream_abort ( s ) ;
2021-03-15 05:42:02 -04:00
s - > req . analysers & = AN_REQ_FLT_END ;
s - > req . analyse_exp = TICK_ETERNITY ;
}
}
return 1 ;
}
2022-05-17 13:07:51 -04:00
/* Updates at once the channel flags, and timers of both stream connectors of a
2022-04-01 08:48:06 -04:00
* same stream , to complete the work after the analysers , then updates the data
* layer below . This will ensure that any synchronous update performed at the
2022-05-17 13:07:51 -04:00
* data layer will be reflected in the channel flags and / or stream connector .
* Note that this does not change the stream connector ' s current state , though
2022-04-01 08:48:06 -04:00
* it updates the previous state to the current one .
*/
DEBUG: unstatify a few functions that are often present in backtraces
It's useful to be able to recognize certain functions that are often
present in backtraces as they call lower level functions, and for this
they must not be static. Let's remove "static" in front of these
functions:
sc_notify, sc_conn_recv, sc_conn_send, sc_conn_process,
sc_applet_process, back_establish, stream_update_both_sc,
httpclient_applet_io_handler, httpclient_applet_init,
httpclient_applet_release
2023-11-30 11:04:16 -05:00
void stream_update_both_sc ( struct stream * s )
2022-04-01 08:48:06 -04:00
{
2022-05-17 13:40:40 -04:00
struct stconn * scf = s - > scf ;
struct stconn * scb = s - > scb ;
2022-04-01 08:48:06 -04:00
struct channel * req = & s - > req ;
struct channel * res = & s - > res ;
2022-12-20 12:47:39 -05:00
req - > flags & = ~ ( CF_READ_EVENT | CF_WRITE_EVENT ) ;
res - > flags & = ~ ( CF_READ_EVENT | CF_WRITE_EVENT ) ;
2022-04-01 08:48:06 -04:00
2022-05-17 13:40:40 -04:00
s - > prev_conn_state = scb - > state ;
2022-04-01 08:48:06 -04:00
/* let's recompute both sides states */
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scf - > state , SC_SB_RDY | SC_SB_EST ) )
2022-05-27 02:49:24 -04:00
sc_update ( scf ) ;
2022-04-01 08:48:06 -04:00
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scb - > state , SC_SB_RDY | SC_SB_EST ) )
2022-05-27 02:49:24 -04:00
sc_update ( scb ) ;
2022-04-01 08:48:06 -04:00
2022-05-17 13:07:51 -04:00
/* stream connectors are processed outside of process_stream() and must be
2022-04-01 08:48:06 -04:00
* handled at the latest moment .
*/
2022-05-18 11:58:02 -04:00
if ( sc_appctx ( scf ) ) {
2022-05-25 09:00:44 -04:00
if ( sc_is_recv_allowed ( scf ) | | sc_is_send_allowed ( scf ) )
2022-05-18 11:58:02 -04:00
appctx_wakeup ( __sc_appctx ( scf ) ) ;
2022-04-01 08:48:06 -04:00
}
2022-05-18 11:58:02 -04:00
if ( sc_appctx ( scb ) ) {
2022-05-25 09:00:44 -04:00
if ( sc_is_recv_allowed ( scb ) | | sc_is_send_allowed ( scb ) )
2022-05-18 11:58:02 -04:00
appctx_wakeup ( __sc_appctx ( scb ) ) ;
2022-04-01 08:48:06 -04:00
}
}
2023-02-27 10:08:31 -05:00
/* check SC and channel timeouts, and close the corresponding stream connectors
* for future reads or writes .
* Note : this will also concern upper layers but we do not touch any other
* flag . We must be careful and correctly detect state changes when calling
* them .
*/
static void stream_handle_timeouts ( struct stream * s )
{
stream_check_conn_timeout ( s ) ;
sc_check_timeouts ( s - > scf ) ;
channel_check_timeout ( & s - > req ) ;
2023-04-12 12:23:15 -04:00
sc_check_timeouts ( s - > scb ) ;
channel_check_timeout ( & s - > res ) ;
2023-04-13 10:16:15 -04:00
if ( unlikely ( ! ( s - > scb - > flags & SC_FL_SHUT_DONE ) & & ( s - > req . flags & CF_WRITE_TIMEOUT ) ) ) {
2025-01-20 03:00:05 -05:00
stream_report_term_evt ( s - > scb , strm_tevt_type_tout ) ;
2023-02-27 10:08:31 -05:00
s - > scb - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:23:48 -04:00
sc_shutdown ( s - > scb ) ;
2023-02-27 10:08:31 -05:00
}
2023-04-17 10:17:32 -04:00
if ( unlikely ( ! ( s - > scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ( s - > req . flags & CF_READ_TIMEOUT ) ) ) {
2025-01-20 03:00:05 -05:00
stream_report_term_evt ( s - > scf , strm_tevt_type_tout ) ;
2023-02-27 10:08:31 -05:00
if ( s - > scf - > flags & SC_FL_NOHALF )
s - > scf - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:10:23 -04:00
sc_abort ( s - > scf ) ;
2023-02-27 10:08:31 -05:00
}
2023-04-13 10:16:15 -04:00
if ( unlikely ( ! ( s - > scf - > flags & SC_FL_SHUT_DONE ) & & ( s - > res . flags & CF_WRITE_TIMEOUT ) ) ) {
2025-01-20 03:00:05 -05:00
stream_report_term_evt ( s - > scf , strm_tevt_type_tout ) ;
2023-02-27 10:08:31 -05:00
s - > scf - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:23:48 -04:00
sc_shutdown ( s - > scf ) ;
2023-02-27 10:08:31 -05:00
}
2023-04-17 10:17:32 -04:00
if ( unlikely ( ! ( s - > scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ( s - > res . flags & CF_READ_TIMEOUT ) ) ) {
2025-01-20 03:00:05 -05:00
stream_report_term_evt ( s - > scb , strm_tevt_type_tout ) ;
2023-02-27 10:08:31 -05:00
if ( s - > scb - > flags & SC_FL_NOHALF )
s - > scb - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:10:23 -04:00
sc_abort ( s - > scb ) ;
2023-04-03 12:32:50 -04:00
}
2023-02-27 10:08:31 -05:00
if ( HAS_FILTERS ( s ) )
flt_stream_check_timeouts ( s ) ;
}
2022-09-07 03:17:45 -04:00
/* if the current task's wake_date was set, it's being profiled, thus we may
* report latencies and CPU usages in logs , so it ' s desirable to update the
* latency when entering process_stream ( ) .
*/
static void stream_cond_update_cpu_latency ( struct stream * s )
{
2024-11-19 10:42:40 -05:00
uint32_t lat ;
2022-09-07 03:17:45 -04:00
2024-11-19 10:42:40 -05:00
if ( likely ( ! th_ctx - > sched_wake_date ) )
return ;
lat = th_ctx - > sched_call_date - th_ctx - > sched_wake_date ;
2022-09-07 03:17:45 -04:00
s - > lat_time + = lat ;
}
2022-09-07 10:17:49 -04:00
/* if the current task's wake_date was set, it's being profiled, thus we may
* report latencies and CPU usages in logs , so it ' s desirable to do that before
* logging in order to report accurate CPU usage . In this case we count that
* final part and reset the wake date so that the scheduler doesn ' t do it a
* second time , and by doing so we also avoid an extra call to clock_gettime ( ) .
* The CPU usage will be off by the little time needed to run over stream_free ( )
* but that ' s only marginal .
*/
static void stream_cond_update_cpu_usage ( struct stream * s )
{
uint32_t cpu ;
/* stats are only registered for non-zero wake dates */
if ( likely ( ! th_ctx - > sched_wake_date ) )
return ;
2024-11-19 12:14:05 -05:00
cpu = now_mono_time ( ) - th_ctx - > sched_call_date ;
2022-09-07 03:17:45 -04:00
s - > cpu_time + = cpu ;
2022-09-07 10:17:49 -04:00
HA_ATOMIC_ADD ( & th_ctx - > sched_profile_entry - > cpu_time , cpu ) ;
th_ctx - > sched_wake_date = 0 ;
}
2021-03-15 05:42:02 -04:00
2022-09-07 03:17:45 -04:00
/* this functions is called directly by the scheduler for tasks whose
* - > process points to process_stream ( ) , and is used to keep latencies
* and CPU usage measurements accurate .
*/
void stream_update_timings ( struct task * t , uint64_t lat , uint64_t cpu )
{
struct stream * s = t - > context ;
s - > lat_time + = lat ;
s - > cpu_time + = cpu ;
}
2010-01-06 17:53:24 -05:00
/* This macro is very specific to the function below. See the comments in
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* process_stream ( ) below to understand the logic and the tests .
2010-01-06 17:53:24 -05:00
*/
# define UPDATE_ANALYSERS(real, list, back, flag) { \
list = ( ( ( list ) & ~ ( flag ) ) | ~ ( back ) ) & ( real ) ; \
back = real ; \
if ( ! ( list ) ) \
break ; \
if ( ( ( list ) ^ ( ( list ) & ( ( list ) - 1 ) ) ) < ( flag ) ) \
continue ; \
}
2016-05-11 11:06:28 -04:00
/* These 2 following macros call an analayzer for the specified channel if the
* right flag is set . The first one is used for " filterable " analyzers . If a
2016-05-11 11:13:39 -04:00
* stream has some registered filters , pre and post analyaze callbacks are
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
* called . The second are used for other analyzers ( AN_REQ / RES_FLT_ * and
2016-05-11 11:06:28 -04:00
* AN_REQ / RES_HTTP_XFER_BODY ) */
# define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
{ \
if ( ( list ) & ( flag ) ) { \
if ( HAS_FILTERS ( strm ) ) { \
2016-05-11 11:13:39 -04:00
if ( ! flt_pre_analyze ( ( strm ) , ( chn ) , ( flag ) ) ) \
2016-05-11 11:06:28 -04:00
break ; \
if ( ! fun ( ( strm ) , ( chn ) , ( flag ) , # # __VA_ARGS__ ) ) \
break ; \
2016-05-11 11:13:39 -04:00
if ( ! flt_post_analyze ( ( strm ) , ( chn ) , ( flag ) ) ) \
break ; \
2016-05-11 11:06:28 -04:00
} \
else { \
if ( ! fun ( ( strm ) , ( chn ) , ( flag ) , # # __VA_ARGS__ ) ) \
break ; \
} \
UPDATE_ANALYSERS ( ( chn ) - > analysers , ( list ) , \
( back ) , ( flag ) ) ; \
} \
}
# define ANALYZE(strm, chn, fun, list, back, flag, ...) \
{ \
if ( ( list ) & ( flag ) ) { \
if ( ! fun ( ( strm ) , ( chn ) , ( flag ) , # # __VA_ARGS__ ) ) \
break ; \
UPDATE_ANALYSERS ( ( chn ) - > analysers , ( list ) , \
( back ) , ( flag ) ) ; \
} \
}
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* Processes the client, server, request and response jobs of a stream task,
2008-11-30 12:47:21 -05:00
* then puts it back to the wait queue in a clean state , or cleans up its
* resources if it must be deleted . Returns in < next > the date the task wants
* to be woken up , or TICK_ETERNITY . In order not to call all functions for
* nothing too many times , the request and response buffers flags are monitored
* and each function is called only if at least another function has changed at
* least one flag it is interested in .
2024-09-26 12:30:36 -04:00
*
2025-01-27 13:11:02 -05:00
* TASK_WOKEN_ * wake up reasons are mapped to STRM_EVT_ *
*
* This task handler understands a few wake up events :
* - STRM_EVT_MSG forces analysers to be re - evaluated
* - STRM_EVT_TIMER forces timers to be re - evaluated
* - STRM_EVT_SHUT_SRV_DOWN shuts the stream down on server down
* - STRM_EVT_KILLED shuts the stream down on active kill
* - STRM_EVT_SHUT_SRV_UP shuts the stream down because a preferred backend became available
2008-11-30 12:47:21 -05:00
*/
2021-03-02 10:09:26 -05:00
struct task * process_stream ( struct task * t , void * context , unsigned int state )
2008-11-30 12:47:21 -05:00
{
2011-03-10 10:55:02 -05:00
struct server * srv ;
2018-05-25 08:04:04 -04:00
struct stream * s = context ;
2015-04-03 08:46:27 -04:00
struct session * sess = s - > sess ;
2023-04-03 12:32:50 -04:00
unsigned int scf_flags , scb_flags ;
2008-11-30 12:47:21 -05:00
unsigned int rqf_last , rpf_last ;
2010-07-27 11:15:12 -04:00
unsigned int rq_prod_last , rq_cons_last ;
unsigned int rp_cons_last , rp_prod_last ;
2023-05-10 10:40:27 -04:00
unsigned int req_ana_back , res_ana_back ;
2014-11-28 09:07:47 -05:00
struct channel * req , * res ;
2022-05-17 13:40:40 -04:00
struct stconn * scf , * scb ;
2019-04-25 13:15:20 -04:00
unsigned int rate ;
2014-11-28 09:07:47 -05:00
2020-06-17 14:49:49 -04:00
activity [ tid ] . stream_calls + + ;
2022-09-07 03:17:45 -04:00
stream_cond_update_cpu_latency ( s ) ;
2018-01-20 13:30:13 -05:00
2025-03-17 11:07:21 -04:00
req = & s - > req ;
res = & s - > res ;
scf = s - > scf ;
scb = s - > scb ;
DBG_TRACE_ENTER ( STRM_EV_STRM_PROC , s ) ;
/* This flag must explicitly be set every time */
req - > flags & = ~ CF_WAKE_WRITE ;
res - > flags & = ~ CF_WAKE_WRITE ;
/* Keep a copy of req/rep flags so that we can detect shutdowns */
rqf_last = req - > flags & ~ CF_MASK_ANALYSER ;
rpf_last = res - > flags & ~ CF_MASK_ANALYSER ;
/* we don't want the stream connector functions to recursively wake us up */
scf - > flags | = SC_FL_DONT_WAKE ;
scb - > flags | = SC_FL_DONT_WAKE ;
/* Keep a copy of SC flags */
scf_flags = scf - > flags ;
scb_flags = scb - > flags ;
2025-01-27 12:38:40 -05:00
/* update pending events */
s - > pending_events | = stream_map_task_state ( state ) ;
s - > pending_events | = HA_ATOMIC_XCHG ( & s - > new_events , STRM_EVT_NONE ) ;
if ( s - > pending_events & ( STRM_EVT_SHUT_SRV_DOWN | STRM_EVT_SHUT_SRV_UP | STRM_EVT_KILLED ) ) {
2024-09-26 12:30:36 -04:00
/* that an instant kill message, the reason is in _UEVT* */
2025-01-27 12:38:40 -05:00
stream_shutdown_self ( s , ( ( s - > pending_events & STRM_EVT_SHUT_SRV_DOWN ) ? SF_ERR_DOWN :
( s - > pending_events & STRM_EVT_SHUT_SRV_UP ) ? SF_ERR_UP :
SF_ERR_KILLED ) ) ;
2024-09-26 12:30:36 -04:00
}
2018-10-25 04:42:39 -04:00
/* First, attempt to receive pending data from I/O layers */
2024-02-01 10:36:25 -05:00
sc_sync_recv ( scf ) ;
sc_sync_recv ( scb ) ;
2018-11-17 13:51:07 -05:00
BUG/MINOR: stream: make the call_rate only count the no-progress calls
We have an anti-looping protection in process_stream() that detects bugs
that used to affect a few filters like compression in the past which
sometimes forgot to handle a read0 or a particular error, leaving a
thread looping at 100% CPU forever. When such a condition is detected,
an alert it emitted and the process is killed so that it can be replaced
by a sane one:
[ALERT] (19061) : A bogus STREAM [0x274abe0] is spinning at 2057156
calls per second and refuses to die, aborting now! Please
report this error to developers [strm=0x274abe0,3 src=unix
fe=MASTER be=MASTER dst=<MCLI> txn=(nil),0 txn.req=-,0
txn.rsp=-,0 rqf=c02000 rqa=10000 rpf=88000021 rpa=8000000
sif=EST,40008 sib=DIS,84018 af=(nil),0 csf=0x274ab90,8600
ab=0x272fd40,1 csb=(nil),0
cof=0x25d5d80,1300:PASS(0x274aaf0)/RAW((nil))/unix_stream(9)
cob=(nil),0:NONE((nil))/NONE((nil))/NONE(0) filters={}]
call trace(11):
| 0x4dbaab [c7 04 25 01 00 00 00 00]: stream_dump_and_crash+0x17b/0x1b4
| 0x4df31f [e9 bd c8 ff ff 49 83 7c]: process_stream+0x382f/0x53a3
(...)
One problem with this detection is that it used to only count the call
rate because we weren't sure how to make it more accurate, but the
threshold was high enough to prevent accidental false positives.
There is actually one case that manages to trigger it, which is when
sending huge amounts of requests pipelined on the master CLI. Some
short requests such as "show version" are sufficient to be handled
extremely fast and to cause a wake up of an analyser to parse the
next request, then an applet to handle it, back and forth. But this
condition is not an error, since some data are being forwarded by
the stream, and it's easy to detect it.
This patch modifies the detection so that update_freq_ctr() only
applies to calls made without CF_READ_PARTIAL nor CF_WRITE_PARTIAL
set on any of the channels, which really indicates that nothing is
happening at all.
This is greatly sufficient and extremely effective, as the call above
is still caught (shutr being ignored by an analyser) while a loop on
the master CLI now has no effect. The "call_rate" field in the detailed
"show sess" output will now be much lower, except for bogus streams,
which may help spot them. This field is only there for developers
anyway so it's pretty fine to slightly adjust its meaning.
This patch could be backported to stable versions in case of reports
of such an issue, but as that's unlikely, it's not really needed.
2022-01-20 12:42:16 -05:00
/* Let's check if we're looping without making any progress, e.g. due
* to a bogus analyser or the fact that we ' re ignoring a read0 . The
* call_rate counter only counts calls with no progress made .
*/
2022-12-20 12:10:04 -05:00
if ( ! ( ( req - > flags | res - > flags ) & ( CF_READ_EVENT | CF_WRITE_EVENT ) ) ) {
BUG/MINOR: stream: make the call_rate only count the no-progress calls
We have an anti-looping protection in process_stream() that detects bugs
that used to affect a few filters like compression in the past which
sometimes forgot to handle a read0 or a particular error, leaving a
thread looping at 100% CPU forever. When such a condition is detected,
an alert it emitted and the process is killed so that it can be replaced
by a sane one:
[ALERT] (19061) : A bogus STREAM [0x274abe0] is spinning at 2057156
calls per second and refuses to die, aborting now! Please
report this error to developers [strm=0x274abe0,3 src=unix
fe=MASTER be=MASTER dst=<MCLI> txn=(nil),0 txn.req=-,0
txn.rsp=-,0 rqf=c02000 rqa=10000 rpf=88000021 rpa=8000000
sif=EST,40008 sib=DIS,84018 af=(nil),0 csf=0x274ab90,8600
ab=0x272fd40,1 csb=(nil),0
cof=0x25d5d80,1300:PASS(0x274aaf0)/RAW((nil))/unix_stream(9)
cob=(nil),0:NONE((nil))/NONE((nil))/NONE(0) filters={}]
call trace(11):
| 0x4dbaab [c7 04 25 01 00 00 00 00]: stream_dump_and_crash+0x17b/0x1b4
| 0x4df31f [e9 bd c8 ff ff 49 83 7c]: process_stream+0x382f/0x53a3
(...)
One problem with this detection is that it used to only count the call
rate because we weren't sure how to make it more accurate, but the
threshold was high enough to prevent accidental false positives.
There is actually one case that manages to trigger it, which is when
sending huge amounts of requests pipelined on the master CLI. Some
short requests such as "show version" are sufficient to be handled
extremely fast and to cause a wake up of an analyser to parse the
next request, then an applet to handle it, back and forth. But this
condition is not an error, since some data are being forwarded by
the stream, and it's easy to detect it.
This patch modifies the detection so that update_freq_ctr() only
applies to calls made without CF_READ_PARTIAL nor CF_WRITE_PARTIAL
set on any of the channels, which really indicates that nothing is
happening at all.
This is greatly sufficient and extremely effective, as the call above
is still caught (shutr being ignored by an analyser) while a loop on
the master CLI now has no effect. The "call_rate" field in the detailed
"show sess" output will now be much lower, except for bogus streams,
which may help spot them. This field is only there for developers
anyway so it's pretty fine to slightly adjust its meaning.
This patch could be backported to stable versions in case of reports
of such an issue, but as that's unlikely, it's not really needed.
2022-01-20 12:42:16 -05:00
rate = update_freq_ctr ( & s - > call_rate , 1 ) ;
if ( rate > = 100000 & & s - > call_rate . prev_ctr ) // make sure to wait at least a full second
stream_dump_and_crash ( & s - > obj_type , read_freq_ctr ( & s - > call_rate ) ) ;
2019-04-25 13:15:20 -04:00
}
2018-09-11 12:27:21 -04:00
2010-01-29 13:26:18 -05:00
/* this data may be no longer valid, clear it */
2015-04-03 17:46:31 -04:00
if ( s - > txn )
memset ( & s - > txn - > auth , 0 , sizeof ( s - > txn - > auth ) ) ;
2010-01-29 13:26:18 -05:00
2008-11-30 12:47:21 -05:00
/* 1a: Check for low level timeouts if needed. We just set a flag on
2022-05-17 13:07:51 -04:00
* stream connectors when their timeouts have expired .
2008-11-30 12:47:21 -05:00
*/
2025-01-27 12:38:40 -05:00
if ( unlikely ( s - > pending_events & STRM_EVT_TIMER ) ) {
2023-02-27 10:08:31 -05:00
stream_handle_timeouts ( s ) ;
2016-11-10 08:58:05 -05:00
2012-11-08 08:49:17 -05:00
/* Once in a while we're woken up because the task expires. But
2025-01-27 12:38:40 -05:00
* this does not necessarily mean that a timeout has been
* reached . So let ' s not run a whole stream processing if only
* an expiration timeout needs to be refreshed . To do so , we
* must be sure only the TIMER event was triggered and not
* error / timeout / abort / shut occurred . on both sides .
2012-11-08 08:49:17 -05:00
*/
2023-04-17 10:17:32 -04:00
if ( ! ( ( scf - > flags | scb - > flags ) & ( SC_FL_ERROR | SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_SHUT_DONE ) ) & &
2023-04-03 12:32:50 -04:00
! ( ( req - > flags | res - > flags ) & ( CF_READ_EVENT | CF_READ_TIMEOUT | CF_WRITE_EVENT | CF_WRITE_TIMEOUT ) ) & &
2022-03-29 13:02:31 -04:00
! ( s - > flags & SF_CONN_EXP ) & &
2025-01-27 12:38:40 -05:00
( s - > pending_events = = STRM_EVT_TIMER ) ) {
2022-05-17 13:44:42 -04:00
scf - > flags & = ~ SC_FL_DONT_WAKE ;
scb - > flags & = ~ SC_FL_DONT_WAKE ;
2012-11-08 08:49:17 -05:00
goto update_exp_and_leave ;
2016-05-04 04:18:37 -04:00
}
2009-06-21 16:03:51 -04:00
}
2008-11-30 12:47:21 -05:00
2022-05-17 13:07:51 -04:00
resync_stconns :
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
if ( ! stream_alloc_work_buffer ( s ) ) {
2024-04-29 02:52:40 -04:00
scf - > flags & = ~ SC_FL_DONT_WAKE ;
scb - > flags & = ~ SC_FL_DONT_WAKE ;
/* we're stuck for now */
t - > expire = TICK_ETERNITY ;
goto leave ;
MAJOR: session: only allocate buffers when needed
A session doesn't need buffers all the time, especially when they're
empty. With this patch, we don't allocate buffers anymore when the
session is initialized, we only allocate them in two cases :
- during process_session()
- during I/O operations
During process_session(), we try hard to allocate both buffers at once
so that we know for sure that a started operation can complete. Indeed,
a previous version of this patch used to allocate one buffer at a time,
but it can result in a deadlock when all buffers are allocated for
requests for example, and there's no buffer left to emit error responses.
Here, if any of the buffers cannot be allocated, the whole operation is
cancelled and the session is added at the tail of the buffer wait queue.
At the end of process_session(), a call to session_release_buffers() is
done so that we can offer unused buffers to other sessions waiting for
them.
For I/O operations, we only need to allocate a buffer on the Rx path.
For this, we only allocate a single buffer but ensure that at least two
are available to avoid the deadlock situation. In case buffers are not
available, SI_FL_WAIT_ROOM is set on the stream interface and the session
is queued. Unused buffers resulting either from a successful send() or
from an unused read buffer are offered to pending sessions during the
->wake() callback.
2014-11-25 13:46:36 -05:00
}
2022-05-17 13:07:51 -04:00
/* 1b: check for low-level errors reported at the stream connector.
2008-11-30 12:47:21 -05:00
* First we check if it ' s a retryable error ( in which case we don ' t
* want to tell the buffer ) . Otherwise we report the error one level
* upper by setting flags into the buffers . Note that the side towards
* the client cannot have connect ( hence retryable ) errors . Also , the
* connection setup code must be able to deal with any type of abort .
*/
2024-10-22 10:35:04 -04:00
s - > passes_stconn + + ;
2012-11-11 18:42:33 -05:00
srv = objt_server ( s - > target ) ;
2023-04-14 06:07:26 -04:00
if ( unlikely ( scf - > flags & SC_FL_ERROR ) ) {
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scf - > state , SC_SB_EST | SC_SB_DIS ) ) {
2023-04-13 10:10:23 -04:00
sc_abort ( scf ) ;
2023-04-13 10:23:48 -04:00
sc_shutdown ( scf ) ;
2014-11-28 09:07:47 -05:00
if ( ! ( req - > analysers ) & & ! ( res - > analysers ) ) {
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report a client abort (no analysers) " ) ;
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_ERR_MASK ) )
s - > flags | = SF_ERR_CLICL ;
if ( ! ( s - > flags & SF_FINST_MASK ) )
s - > flags | = SF_FINST_D ;
2008-12-14 05:44:04 -05:00
}
2008-11-30 12:47:21 -05:00
}
}
2023-04-14 06:07:26 -04:00
if ( unlikely ( scb - > flags & SC_FL_ERROR ) ) {
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scb - > state , SC_SB_EST | SC_SB_DIS ) ) {
2023-04-13 10:10:23 -04:00
sc_abort ( scb ) ;
2023-04-13 10:23:48 -04:00
sc_shutdown ( scb ) ;
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > failed_resp ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > failed_resp ) ;
2014-11-28 09:07:47 -05:00
if ( ! ( req - > analysers ) & & ! ( res - > analysers ) ) {
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report a client abort (no analysers) " ) ;
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_ERR_MASK ) )
s - > flags | = SF_ERR_SRVCL ;
if ( ! ( s - > flags & SF_FINST_MASK ) )
s - > flags | = SF_FINST_D ;
2008-12-14 05:44:04 -05:00
}
2008-11-30 12:47:21 -05:00
}
/* note: maybe we should process connection errors here ? */
}
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scb - > state , SC_SB_CON | SC_SB_RDY ) ) {
2008-11-30 12:47:21 -05:00
/* we were trying to establish a connection on the server side,
* maybe it succeeded , maybe it failed , maybe we timed out , . . .
*/
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_RDY )
2020-01-09 12:43:15 -05:00
back_handle_st_rdy ( s ) ;
2022-05-17 13:47:17 -04:00
else if ( s - > scb - > state = = SC_ST_CON )
2020-01-09 12:43:15 -05:00
back_handle_st_con ( s ) ;
MEDIUM: stream: re-arrange the connection setup status reporting
Till now when a wakeup happens after a connection is attempted, we go
through sess_update_st_con_tcp() to deal with the various possible events,
then to sess_update_st_cer() to deal with a possible error detected by the
former, or to sess_establish() to complete the connection validation. There
are multiple issues in the way this is handled, which have accumulated over
time. One of them is that any spurious wakeup during SI_ST_CON would validate
the READ_ATTACHED flag and wake the analysers up. Another one is that nobody
feels responsible for clearing SI_FL_EXP if it happened at the same time as
a success (and it is present in all reports of loops to date). And another
issue is that aborts cannot happen after a clean connection setup with no
data transfer (since CF_WRITE_NULL is part of CF_WRITE_ACTIVITY). Last, the
flags cleanup work was hackish, added here and there to please the next
function (typically what had to be donne in commit 7a3367cca to work around
the url_param+reuse issue by moving READ_ATTACHED to CON).
This patch performs a significant lift up of this setup code. First, it
makes sure that the state handlers are the ones responsible for the cleanup
of the stuff they rely on. Typically sess_sestablish() will clean up the
SI_FL_EXP flag because if we decided to validate the connection it means
that we want to ignore this late timeout. Second, it splits the CON and
RDY state handlers because the former only has to deal with failures,
timeouts and non-events, while the latter has to deal with partial or
total successes. Third, everything related to connection success was
moved to sess_establish() since it's the only safe place to do so, and
this function is also called at a few places to deal with synchronous
connections, which are not seen by intermediary state handlers.
The code was made a bit more robust, for example by making sure we
always set SI_FL_NOLINGER when aborting a connection so that we don't
have any risk to leave a connection in SHUTW state in case it was
validated late. The useless return codes of some of these functions
were dropped so that callers only rely on the stream-int's state now
(which was already partially the case anyway).
The code is now a bit cleaner, could be further improved (and functions
renamed) but given the sensitivity of this part, better limit changes to
strictly necessary. It passes all reg tests.
2019-06-05 12:02:04 -04:00
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_CER )
2020-01-09 12:43:15 -05:00
back_handle_st_cer ( s ) ;
2022-05-17 13:47:17 -04:00
else if ( scb - > state = = SC_ST_EST )
2020-01-09 12:43:15 -05:00
back_establish ( s ) ;
2008-11-30 12:47:21 -05:00
2022-05-17 13:47:17 -04:00
/* state is now one of SC_ST_CON (still in progress), SC_ST_EST
* ( established ) , SC_ST_DIS ( abort ) , SC_ST_CLO ( last error ) ,
* SC_ST_ASS / SC_ST_TAR / SC_ST_REQ for retryable errors .
2008-11-30 12:47:21 -05:00
*/
}
2022-05-17 13:40:40 -04:00
rq_prod_last = scf - > state ;
rq_cons_last = scb - > state ;
rp_cons_last = scf - > state ;
rp_prod_last = scb - > state ;
2010-07-27 11:15:12 -04:00
2008-11-30 12:47:21 -05:00
/* Check for connection closure */
2019-11-05 10:18:10 -05:00
DBG_TRACE_POINT ( STRM_EV_STRM_PROC , s ) ;
2008-11-30 12:47:21 -05:00
/* nothing special to be done on client side */
2022-05-17 13:47:17 -04:00
if ( unlikely ( scf - > state = = SC_ST_DIS ) ) {
scf - > state = SC_ST_CLO ;
2008-11-30 12:47:21 -05:00
2022-03-30 11:13:02 -04:00
/* This is needed only when debugging is enabled, to indicate
* client - side close .
*/
if ( unlikely ( ( global . mode & MODE_DEBUG ) & &
( ! ( global . mode & MODE_QUIET ) | |
( global . mode & MODE_VERBOSE ) ) ) ) {
chunk_printf ( & trash , " %08x:%s.clicls[%04x:%04x] \n " ,
s - > uniq_id , s - > be - > id ,
2022-05-18 10:23:22 -04:00
( unsigned short ) conn_fd ( sc_conn ( scf ) ) ,
( unsigned short ) conn_fd ( sc_conn ( scb ) ) ) ;
2022-03-30 11:13:02 -04:00
DISGUISE ( write ( 1 , trash . area , trash . data ) ) ;
}
}
2008-11-30 12:47:21 -05:00
/* When a server-side connection is released, we have to count it and
* check for pending connections on this server .
*/
2022-05-17 13:47:17 -04:00
if ( unlikely ( scb - > state = = SC_ST_DIS ) ) {
scb - > state = SC_ST_CLO ;
2012-11-11 18:42:33 -05:00
srv = objt_server ( s - > target ) ;
2011-03-10 10:55:02 -05:00
if ( srv ) {
2015-04-02 19:14:29 -04:00
if ( s - > flags & SF_CURR_SESS ) {
s - > flags & = ~ SF_CURR_SESS ;
2021-04-06 07:53:36 -04:00
_HA_ATOMIC_DEC ( & srv - > cur_sess ) ;
2008-11-30 12:47:21 -05:00
}
2025-02-06 11:07:48 -05:00
/*
* We don ' t want to release the slot just yet
* if we ' re using strict - maxconn , we want to
* free the connection before .
*/
if ( ! ( srv - > flags & SRV_F_STRICT_MAXCONN ) ) {
sess_change_server ( s , NULL ) ;
if ( may_dequeue_tasks ( srv , s - > be ) )
process_srv_queue ( srv ) ;
}
2008-11-30 12:47:21 -05:00
}
2022-03-30 11:13:02 -04:00
/* This is needed only when debugging is enabled, to indicate
* server - side close .
*/
if ( unlikely ( ( global . mode & MODE_DEBUG ) & &
( ! ( global . mode & MODE_QUIET ) | |
( global . mode & MODE_VERBOSE ) ) ) ) {
2022-05-17 13:47:17 -04:00
if ( s - > prev_conn_state = = SC_ST_EST ) {
2022-03-30 11:13:02 -04:00
chunk_printf ( & trash , " %08x:%s.srvcls[%04x:%04x] \n " ,
s - > uniq_id , s - > be - > id ,
2022-05-18 10:23:22 -04:00
( unsigned short ) conn_fd ( sc_conn ( scf ) ) ,
( unsigned short ) conn_fd ( sc_conn ( scb ) ) ) ;
2022-03-30 11:13:02 -04:00
DISGUISE ( write ( 1 , trash . area , trash . data ) ) ;
}
}
2008-11-30 12:47:21 -05:00
}
/*
* Note : of the transient states ( REQ , CER , DIS ) , only REQ may remain
* at this point .
*/
2009-03-08 14:20:25 -04:00
resync_request :
2024-10-22 10:35:04 -04:00
s - > passes_reqana + + ;
2008-11-30 12:47:21 -05:00
/* Analyse request */
2014-11-28 09:07:47 -05:00
if ( ( ( req - > flags & ~ rqf_last ) & CF_MASK_ANALYSER ) | |
2023-04-17 10:17:32 -04:00
( ( scf - > flags ^ scf_flags ) & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | |
2023-04-13 10:16:15 -04:00
( ( scb - > flags ^ scb_flags ) & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | |
2023-04-13 10:37:37 -04:00
( req - > analysers & & ( scb - > flags & SC_FL_SHUT_DONE ) ) | |
2022-05-17 13:40:40 -04:00
scf - > state ! = rq_prod_last | |
scb - > state ! = rq_cons_last | |
2025-01-27 12:38:40 -05:00
s - > pending_events & STRM_EVT_MSG ) {
2023-04-03 12:32:50 -04:00
unsigned int scf_flags_ana = scf - > flags ;
unsigned int scb_flags_ana = scb - > flags ;
2008-11-30 12:47:21 -05:00
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scf - > state , SC_SB_EST | SC_SB_DIS | SC_SB_CLO ) ) {
2010-01-07 18:32:27 -05:00
int max_loops = global . tune . maxpollevents ;
2010-01-06 17:53:24 -05:00
unsigned int ana_list ;
unsigned int ana_back ;
2009-06-28 13:37:53 -04:00
2010-01-06 18:20:41 -05:00
/* it's up to the analysers to stop new connections,
* disable reading or closing . Note : if an analyser
* disables any of these bits , it is responsible for
* enabling them again when it disables itself , so
* that other analysers are called in similar conditions .
*/
2014-11-28 09:07:47 -05:00
channel_auto_read ( req ) ;
channel_auto_connect ( req ) ;
channel_auto_close ( req ) ;
2008-11-30 17:15:34 -05:00
/* We will call all analysers for which a bit is set in
2014-11-28 09:07:47 -05:00
* req - > analysers , following the bit order from LSB
2008-11-30 17:15:34 -05:00
* to MSB . The analysers must remove themselves from
2009-06-28 13:37:53 -04:00
* the list when not needed . Any analyser may return 0
* to break out of the loop , either because of missing
* data to take a decision , or because it decides to
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* kill the stream . We loop at least once through each
2009-06-28 13:37:53 -04:00
* analyser , and we may loop again if other analysers
* are added in the middle .
2010-01-06 17:53:24 -05:00
*
* We build a list of analysers to run . We evaluate all
* of these analysers in the order of the lower bit to
* the higher bit . This ordering is very important .
* An analyser will often add / remove other analysers ,
* including itself . Any changes to itself have no effect
* on the loop . If it removes any other analysers , we
* want those analysers not to be called anymore during
* this loop . If it adds an analyser that is located
* after itself , we want it to be scheduled for being
* processed during the loop . If it adds an analyser
* which is located before it , we want it to switch to
* it immediately , even if it has already been called
* once but removed since .
*
* In order to achieve this , we compare the analyser
* list after the call with a copy of it before the
* call . The work list is fed with analyser bits that
* appeared during the call . Then we compare previous
* work list with the new one , and check the bits that
* appeared . If the lowest of these bits is lower than
* the current bit , it means we have enabled a previous
* analyser and must immediately loop again .
2008-11-30 17:15:34 -05:00
*/
2009-06-28 13:37:53 -04:00
2014-11-28 09:07:47 -05:00
ana_list = ana_back = req - > analysers ;
MEDIUM: stream: interrupt costly rulesets after too many evaluations
It is not rare to see configurations with a large number of "tcp-request
content" or "http-request" rules for instance. A large number of rules
combined with cpu-demanding actions (e.g.: actions that work on content)
may create thread contention as all the rules from a given ruleset are
evaluated under the same polling loop if the evaluation is not interrupted
Thus, in this patch we add extra logic around "tcp-request content",
"tcp-response content", "http-request" and "http-response" rulesets, so
that when a certain number of rules are evaluated under the single polling
loop, we force the evaluating function to yield. As such, the rule which
was about to be evaluated is saved, and the function starts evaluating
rules from the save pointer when it returns (in the next polling loop).
We use task_wakeup(task, TASK_WOKEN_MSG) to explicitly wake the task so
that no time is wasted and the processing is resumed ASAP. TASK_WOKEN_MSG
is mandatory here because process_stream() expects TASK_WOKEN_MSG for
explicit analyzers re-evaluation.
rules_bcount stream's attribute was added to count how manu rules were
evaluated since last interruption (yield). Also, SF_RULE_FYIELD flag
was added to know that the s->current_rule was assigned due to forced
yield and not regular yield.
By default haproxy will enforce a yield every 50 rules, this behavior
can be configured using the "tune.max-rules-at-once" global keyword.
There is a limitation though: for now, if the ACT_OPT_FINAL flag is set
on act_opts, we consider it is not safe to yield (as it is already the
case for automatic yield). In this case instead of yielding an taking
the risk of not being called back, we skip the yield and hope it will
not create contention. This is something we should ideally try to
improve in order to yield in all conditions.
2025-01-30 07:26:42 -05:00
s - > rules_bcount = 0 ;
2010-01-07 18:32:27 -05:00
while ( ana_list & & max_loops - - ) {
2010-01-06 17:53:24 -05:00
/* Warning! ensure that analysers are always placed in ascending order! */
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , req , flt_start_analyze , ana_list , ana_back , AN_REQ_FLT_START_FE ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , req , tcp_inspect_request , ana_list , ana_back , AN_REQ_INSPECT_FE ) ;
2019-07-16 08:54:53 -04:00
FLT_ANALYZE ( s , req , http_wait_for_request , ana_list , ana_back , AN_REQ_WAIT_HTTP ) ;
FLT_ANALYZE ( s , req , http_wait_for_request_body , ana_list , ana_back , AN_REQ_HTTP_BODY ) ;
FLT_ANALYZE ( s , req , http_process_req_common , ana_list , ana_back , AN_REQ_HTTP_PROCESS_FE , sess - > fe ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , req , process_switching_rules , ana_list , ana_back , AN_REQ_SWITCHING_RULES ) ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , req , flt_start_analyze , ana_list , ana_back , AN_REQ_FLT_START_BE ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , req , tcp_inspect_request , ana_list , ana_back , AN_REQ_INSPECT_BE ) ;
2019-07-16 08:54:53 -04:00
FLT_ANALYZE ( s , req , http_process_req_common , ana_list , ana_back , AN_REQ_HTTP_PROCESS_BE , s - > be ) ;
FLT_ANALYZE ( s , req , http_process_tarpit , ana_list , ana_back , AN_REQ_HTTP_TARPIT ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , req , process_server_rules , ana_list , ana_back , AN_REQ_SRV_RULES ) ;
2019-07-16 08:54:53 -04:00
FLT_ANALYZE ( s , req , http_process_request , ana_list , ana_back , AN_REQ_HTTP_INNER ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , req , tcp_persist_rdp_cookie , ana_list , ana_back , AN_REQ_PRST_RDP_COOKIE ) ;
FLT_ANALYZE ( s , req , process_sticking_rules , ana_list , ana_back , AN_REQ_STICKING_RULES ) ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , req , flt_analyze_http_headers , ana_list , ana_back , AN_REQ_FLT_HTTP_HDRS ) ;
2019-07-16 08:54:53 -04:00
ANALYZE ( s , req , http_request_forward_body , ana_list , ana_back , AN_REQ_HTTP_XFER_BODY ) ;
2018-10-26 08:47:40 -04:00
ANALYZE ( s , req , pcli_wait_for_request , ana_list , ana_back , AN_REQ_WAIT_CLI ) ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , req , flt_xfer_data , ana_list , ana_back , AN_REQ_FLT_XFER_DATA ) ;
ANALYZE ( s , req , flt_end_analyze , ana_list , ana_back , AN_REQ_FLT_END ) ;
2010-01-07 18:32:27 -05:00
break ;
}
2008-11-30 12:47:21 -05:00
}
2009-03-15 17:34:05 -04:00
2022-05-17 13:40:40 -04:00
rq_prod_last = scf - > state ;
rq_cons_last = scb - > state ;
2014-11-28 09:07:47 -05:00
req - > flags & = ~ CF_WAKE_ONCE ;
rqf_last = req - > flags ;
2023-04-17 10:17:32 -04:00
scf_flags = ( scf_flags & ~ ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ;
2023-04-13 10:16:15 -04:00
scb_flags = ( scb_flags & ~ ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | ( scb - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) ;
2010-07-27 11:15:12 -04:00
2023-04-17 10:17:32 -04:00
if ( ( ( scf - > flags ^ scf_flags_ana ) & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) | | ( ( scb - > flags ^ scb_flags_ana ) & SC_FL_SHUT_DONE ) )
2009-06-21 16:43:05 -04:00
goto resync_request ;
}
2010-01-06 18:09:04 -05:00
/* we'll monitor the request analysers while parsing the response,
* because some response analysers may indirectly enable new request
* analysers ( eg : HTTP keep - alive ) .
*/
2014-11-28 09:07:47 -05:00
req_ana_back = req - > analysers ;
2010-01-06 18:09:04 -05:00
2009-06-21 16:43:05 -04:00
resync_response :
2024-10-22 10:35:04 -04:00
s - > passes_resana + + ;
2009-06-21 16:43:05 -04:00
/* Analyse response */
2014-11-28 09:07:47 -05:00
if ( ( ( res - > flags & ~ rpf_last ) & CF_MASK_ANALYSER ) | |
2023-04-17 10:17:32 -04:00
( ( scb - > flags ^ scb_flags ) & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | |
2023-04-13 10:16:15 -04:00
( ( scf - > flags ^ scf_flags ) & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | |
2023-04-13 10:37:37 -04:00
( res - > analysers & & ( scf - > flags & SC_FL_SHUT_DONE ) ) | |
2023-04-03 12:32:50 -04:00
scf - > state ! = rp_cons_last | |
scb - > state ! = rp_prod_last | |
2025-01-27 12:38:40 -05:00
s - > pending_events & STRM_EVT_MSG ) {
2023-04-03 12:32:50 -04:00
unsigned int scb_flags_ana = scb - > flags ;
unsigned int scf_flags_ana = scf - > flags ;
2009-06-21 16:43:05 -04:00
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scb - > state , SC_SB_EST | SC_SB_DIS | SC_SB_CLO ) ) {
2010-01-07 18:32:27 -05:00
int max_loops = global . tune . maxpollevents ;
2010-01-06 17:53:24 -05:00
unsigned int ana_list ;
unsigned int ana_back ;
2009-10-18 16:53:08 -04:00
2010-01-06 18:20:41 -05:00
/* it's up to the analysers to stop disable reading or
* closing . Note : if an analyser disables any of these
* bits , it is responsible for enabling them again when
* it disables itself , so that other analysers are called
* in similar conditions .
*/
2014-11-28 09:07:47 -05:00
channel_auto_read ( res ) ;
channel_auto_close ( res ) ;
2009-10-18 16:53:08 -04:00
/* We will call all analysers for which a bit is set in
2014-11-28 09:07:47 -05:00
* res - > analysers , following the bit order from LSB
2009-10-18 16:53:08 -04:00
* to MSB . The analysers must remove themselves from
* the list when not needed . Any analyser may return 0
* to break out of the loop , either because of missing
* data to take a decision , or because it decides to
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* kill the stream . We loop at least once through each
2009-10-18 16:53:08 -04:00
* analyser , and we may loop again if other analysers
* are added in the middle .
*/
2014-11-28 09:07:47 -05:00
ana_list = ana_back = res - > analysers ;
MEDIUM: stream: interrupt costly rulesets after too many evaluations
It is not rare to see configurations with a large number of "tcp-request
content" or "http-request" rules for instance. A large number of rules
combined with cpu-demanding actions (e.g.: actions that work on content)
may create thread contention as all the rules from a given ruleset are
evaluated under the same polling loop if the evaluation is not interrupted
Thus, in this patch we add extra logic around "tcp-request content",
"tcp-response content", "http-request" and "http-response" rulesets, so
that when a certain number of rules are evaluated under the single polling
loop, we force the evaluating function to yield. As such, the rule which
was about to be evaluated is saved, and the function starts evaluating
rules from the save pointer when it returns (in the next polling loop).
We use task_wakeup(task, TASK_WOKEN_MSG) to explicitly wake the task so
that no time is wasted and the processing is resumed ASAP. TASK_WOKEN_MSG
is mandatory here because process_stream() expects TASK_WOKEN_MSG for
explicit analyzers re-evaluation.
rules_bcount stream's attribute was added to count how manu rules were
evaluated since last interruption (yield). Also, SF_RULE_FYIELD flag
was added to know that the s->current_rule was assigned due to forced
yield and not regular yield.
By default haproxy will enforce a yield every 50 rules, this behavior
can be configured using the "tune.max-rules-at-once" global keyword.
There is a limitation though: for now, if the ACT_OPT_FINAL flag is set
on act_opts, we consider it is not safe to yield (as it is already the
case for automatic yield). In this case instead of yielding an taking
the risk of not being called back, we skip the yield and hope it will
not create contention. This is something we should ideally try to
improve in order to yield in all conditions.
2025-01-30 07:26:42 -05:00
s - > rules_bcount = 0 ;
2010-01-07 18:32:27 -05:00
while ( ana_list & & max_loops - - ) {
2010-01-06 17:53:24 -05:00
/* Warning! ensure that analysers are always placed in ascending order! */
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , res , flt_start_analyze , ana_list , ana_back , AN_RES_FLT_START_FE ) ;
ANALYZE ( s , res , flt_start_analyze , ana_list , ana_back , AN_RES_FLT_START_BE ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , res , tcp_inspect_response , ana_list , ana_back , AN_RES_INSPECT ) ;
2019-07-16 08:54:53 -04:00
FLT_ANALYZE ( s , res , http_wait_for_response , ana_list , ana_back , AN_RES_WAIT_HTTP ) ;
2016-05-11 11:06:28 -04:00
FLT_ANALYZE ( s , res , process_store_rules , ana_list , ana_back , AN_RES_STORE_RULES ) ;
2019-07-16 08:54:53 -04:00
FLT_ANALYZE ( s , res , http_process_res_common , ana_list , ana_back , AN_RES_HTTP_PROCESS_BE , s - > be ) ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , res , flt_analyze_http_headers , ana_list , ana_back , AN_RES_FLT_HTTP_HDRS ) ;
2019-07-16 08:54:53 -04:00
ANALYZE ( s , res , http_response_forward_body , ana_list , ana_back , AN_RES_HTTP_XFER_BODY ) ;
2018-10-26 08:47:40 -04:00
ANALYZE ( s , res , pcli_wait_for_response , ana_list , ana_back , AN_RES_WAIT_CLI ) ;
BUG/MAJOR: channel: Fix the definition order of channel analyzers
It is important to defined analyzers (AN_REQ_* and AN_RES_*) in the same order
they are evaluated in process_stream. This order is really important because
during analyzers evaluation, we run them in the order of the lower bit to the
higher one. This way, when an analyzer adds/removes another one during its
evaluation, we know if it is located before or after it. So, when it adds an
analyzer which is located before it, we can switch to it immediately, even if it
has already been called once but removed since.
With the time, and introduction of new analyzers, this order was broken up. the
main problems come from the filter analyzers. We used values not related with
their evaluation order. Furthermore, we used same values for request and response
analyzers.
So, to fix the bug, filter analyzers have been splitted in 2 distinct lists to
have different analyzers for the request channel than those for the response
channel. And of course, we have moved them to the right place.
Some other analyzers have been reordered to respect the evaluation order:
* AN_REQ_HTTP_TARPIT has been moved just before AN_REQ_SRV_RULES
* AN_REQ_PRST_RDP_COOKIE has been moved just before AN_REQ_STICKING_RULES
* AN_RES_STORE_RULES has been moved just after AN_RES_WAIT_HTTP
Note today we have 29 analyzers, all stored into a 32 bits bitfield. So we can
still add 4 more analyzers before having a problem. A good way to fend off the
problem for a while could be to have a different bitfield for request and
response analyzers.
[wt: all of this must be backported to 1.7, and part of it must be backported
to 1.6 and 1.5]
2017-01-05 08:06:34 -05:00
ANALYZE ( s , res , flt_xfer_data , ana_list , ana_back , AN_RES_FLT_XFER_DATA ) ;
ANALYZE ( s , res , flt_end_analyze , ana_list , ana_back , AN_RES_FLT_END ) ;
2010-01-07 18:32:27 -05:00
break ;
}
2009-06-21 16:43:05 -04:00
}
2022-05-17 13:40:40 -04:00
rp_cons_last = scf - > state ;
rp_prod_last = scb - > state ;
2017-07-06 09:49:30 -04:00
res - > flags & = ~ CF_WAKE_ONCE ;
2014-11-28 09:07:47 -05:00
rpf_last = res - > flags ;
2023-04-17 10:17:32 -04:00
scb_flags = ( scb_flags & ~ ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ;
2023-04-13 10:16:15 -04:00
scf_flags = ( scf_flags & ~ ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | ( scf - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) ;
2010-07-27 11:15:12 -04:00
2023-04-17 10:17:32 -04:00
if ( ( ( scb - > flags ^ scb_flags_ana ) & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) | | ( ( scf - > flags ^ scf_flags_ana ) & SC_FL_SHUT_DONE ) )
2009-06-21 16:43:05 -04:00
goto resync_response ;
}
2023-05-10 10:40:27 -04:00
/* we'll monitor the response analysers because some response analysers
* may be enabled / disabled later
*/
res_ana_back = res - > analysers ;
2010-01-06 18:09:04 -05:00
/* maybe someone has added some request analysers, so we must check and loop */
2014-11-28 09:07:47 -05:00
if ( req - > analysers & ~ req_ana_back )
2010-01-06 18:09:04 -05:00
goto resync_request ;
2014-11-28 09:07:47 -05:00
if ( ( req - > flags & ~ rqf_last ) & CF_MASK_ANALYSER )
2010-12-17 01:13:42 -05:00
goto resync_request ;
2009-06-21 16:43:05 -04:00
/* FIXME: here we should call protocol handlers which rely on
* both buffers .
*/
2024-10-22 10:35:04 -04:00
s - > passes_propag + + ;
2009-06-21 16:43:05 -04:00
/*
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* Now we propagate unhandled errors to the stream . Normally
2010-03-04 14:34:23 -05:00
* we ' re just in a data phase here since it means we have not
* seen any analyser who could set an error status .
2009-06-21 16:43:05 -04:00
*/
2012-11-11 18:42:33 -05:00
srv = objt_server ( s - > target ) ;
2015-04-02 19:14:29 -04:00
if ( unlikely ( ! ( s - > flags & SF_ERR_MASK ) ) ) {
2023-04-14 06:07:26 -04:00
if ( ( scf - > flags & SC_FL_ERROR ) | | req - > flags & ( CF_READ_TIMEOUT | CF_WRITE_TIMEOUT ) ) {
2009-06-21 16:43:05 -04:00
/* Report it if the client got an error or a read timeout expired */
2021-10-18 09:06:20 -04:00
req - > analysers & = AN_REQ_FLT_END ;
2023-05-10 10:28:38 -04:00
channel_auto_close ( req ) ;
2023-04-14 06:07:26 -04:00
if ( scf - > flags & SC_FL_ERROR ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_CLICL ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled client error " ) ;
2010-03-04 14:34:23 -05:00
}
2014-11-28 09:07:47 -05:00
else if ( req - > flags & CF_READ_TIMEOUT ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_CLITO ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled client timeout (RD) " ) ;
2010-03-04 14:34:23 -05:00
}
else {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_SRVTO ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled server timeout (WR) " ) ;
2010-03-04 14:34:23 -05:00
}
2009-03-15 17:34:05 -04:00
sess_set_term_flags ( s ) ;
2019-04-23 11:34:22 -04:00
/* Abort the request if a client error occurred while
2022-05-17 13:47:17 -04:00
* the backend stream connector is in the SC_ST_INI
* state . It is switched into the SC_ST_CLO state and
2019-04-23 11:34:22 -04:00
* the request channel is erased . */
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_INI ) {
s - > scb - > state = SC_ST_CLO ;
2019-04-23 11:34:22 -04:00
channel_abort ( req ) ;
if ( IS_HTX_STRM ( s ) )
channel_htx_erase ( req , htxbuf ( & req - > buf ) ) ;
else
channel_erase ( req ) ;
}
2009-03-15 17:34:05 -04:00
}
2023-04-14 06:07:26 -04:00
else if ( ( scb - > flags & SC_FL_ERROR ) | | res - > flags & ( CF_READ_TIMEOUT | CF_WRITE_TIMEOUT ) ) {
2009-06-21 16:43:05 -04:00
/* Report it if the server got an error or a read timeout expired */
2021-10-18 09:06:20 -04:00
res - > analysers & = AN_RES_FLT_END ;
2023-05-10 10:28:38 -04:00
channel_auto_close ( res ) ;
2023-04-14 06:07:26 -04:00
if ( scb - > flags & SC_FL_ERROR ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_SRVCL ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled server error " ) ;
2010-03-04 14:34:23 -05:00
}
2014-11-28 09:07:47 -05:00
else if ( res - > flags & CF_READ_TIMEOUT ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > srv_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_SRVTO ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled server timeout (RD) " ) ;
2010-03-04 14:34:23 -05:00
}
else {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2020-01-24 05:45:05 -05:00
if ( sess - > listener & & sess - > listener - > counters )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > listener - > counters - > shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2011-03-10 10:55:02 -05:00
if ( srv )
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & srv - > counters . shared - > tg [ tgid - 1 ] - > cli_aborts ) ;
2015-04-02 19:14:29 -04:00
s - > flags | = SF_ERR_CLITO ;
2024-10-22 10:46:34 -04:00
COUNT_IF ( 1 , " Report unhandled client timeout (WR) " ) ;
2010-03-04 14:34:23 -05:00
}
2009-06-21 16:43:05 -04:00
sess_set_term_flags ( s ) ;
}
2009-03-15 17:34:05 -04:00
}
2009-06-21 16:43:05 -04:00
/*
* Here we take care of forwarding unhandled data . This also includes
* connection establishments and shutdown requests .
*/
2021-01-06 11:20:16 -05:00
/* If no one is interested in analysing data, it's time to forward
2009-09-20 06:07:52 -04:00
* everything . We configure the buffer to forward indefinitely .
2023-04-13 09:39:30 -04:00
* Note that we ' re checking SC_FL_ABRT_WANTED as an indication of a possible
2012-08-27 18:06:31 -04:00
* recent call to channel_abort ( ) .
2008-12-14 11:31:54 -05:00
*/
2017-08-29 10:06:38 -04:00
if ( unlikely ( ( ! req - > analysers | | ( req - > analysers = = AN_REQ_FLT_END & & ! ( req - > flags & CF_FLT_ANALYZE ) ) ) & &
2023-04-13 10:16:15 -04:00
! ( scf - > flags & SC_FL_ABRT_WANTED ) & & ! ( scb - > flags & SC_FL_SHUT_DONE ) & &
2023-04-03 12:32:50 -04:00
( sc_state_in ( scf - > state , SC_SB_EST | SC_SB_DIS | SC_SB_CLO ) ) & &
( req - > to_forward ! = CHN_INFINITE_FORWARD ) ) ) {
2012-11-11 17:05:39 -05:00
/* This buffer is freewheeling, there's no analyser
2009-03-08 16:38:23 -04:00
* attached to it . If any data are left in , we ' ll permit them to
* move .
*/
2014-11-28 09:07:47 -05:00
channel_auto_read ( req ) ;
channel_auto_connect ( req ) ;
channel_auto_close ( req ) ;
2009-03-08 16:38:23 -04:00
2018-12-18 15:57:24 -05:00
if ( IS_HTX_STRM ( s ) ) {
struct htx * htx = htxbuf ( & req - > buf ) ;
2018-12-05 05:56:15 -05:00
/* We'll let data flow between the producer (if still connected)
2018-12-18 15:57:24 -05:00
* to the consumer .
2018-12-05 05:56:15 -05:00
*/
2018-12-18 15:57:24 -05:00
co_set_data ( req , htx - > data ) ;
2023-04-03 12:32:50 -04:00
if ( ( global . tune . options & GTUNE_USE_FAST_FWD ) & &
2023-04-17 10:17:32 -04:00
! ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ! ( scb - > flags & SC_FL_SHUT_WANTED ) )
2018-12-18 15:57:24 -05:00
channel_htx_forward_forever ( req , htx ) ;
2018-12-05 05:56:15 -05:00
}
else {
/* We'll let data flow between the producer (if still connected)
* to the consumer ( which might possibly not be connected yet ) .
*/
2018-12-18 15:57:24 -05:00
c_adv ( req , ci_data ( req ) ) ;
2023-04-03 12:32:50 -04:00
if ( ( global . tune . options & GTUNE_USE_FAST_FWD ) & &
2023-04-17 10:17:32 -04:00
! ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ! ( scb - > flags & SC_FL_SHUT_WANTED ) )
2018-12-05 05:56:15 -05:00
channel_forward_forever ( req ) ;
}
2008-12-14 11:31:54 -05:00
}
2008-12-13 15:12:26 -05:00
2008-11-30 12:47:21 -05:00
/* reflect what the L7 analysers have seen last */
2014-11-28 09:07:47 -05:00
rqf_last = req - > flags ;
2023-04-17 10:17:32 -04:00
scf_flags = ( scf_flags & ~ ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ;
2023-04-13 10:16:15 -04:00
scb_flags = ( scb_flags & ~ ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | ( scb - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) ;
2008-11-30 12:47:21 -05:00
2009-09-19 15:04:57 -04:00
/* it's possible that an upper layer has requested a connection setup or abort.
* There are 2 situations where we decide to establish a new connection :
* - there are data scheduled for emission in the buffer
2012-08-27 17:14:58 -04:00
* - the CF_AUTO_CONNECT flag is set ( active connection )
2009-09-19 15:04:57 -04:00
*/
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_INI ) {
2023-04-13 10:16:15 -04:00
if ( ! ( scb - > flags & SC_FL_SHUT_DONE ) ) {
2023-10-10 12:00:38 -04:00
if ( ( req - > flags & CF_AUTO_CONNECT ) | | co_data ( req ) ) {
2013-09-29 11:19:56 -04:00
/* If we have an appctx, there is no connect method, so we
* immediately switch to the connected state , otherwise we
* perform a connection request .
2009-09-19 15:04:57 -04:00
*/
2022-05-17 13:47:17 -04:00
scb - > state = SC_ST_REQ ; /* new connection requested */
2022-03-29 10:08:44 -04:00
s - > conn_retries = 0 ;
2021-05-21 07:46:14 -04:00
if ( ( s - > be - > retry_type & ~ PR_RE_CONN_FAILED ) & &
( s - > be - > mode = = PR_MODE_HTTP ) & &
2022-03-29 09:23:40 -04:00
! ( s - > txn - > flags & TX_D_L7_RETRY ) )
s - > txn - > flags | = TX_L7_RETRY ;
BUG/MEDIUM: stream: Properly handle abortonclose when set on backend only
Since the 2.2 and the commit dedd30610 ("MEDIUM: h1: Don't wake the H1 tasklet
if we got the whole request."), we avoid to subscribe for reads if the H1
message is fully received. However, this broke the abortonclose option. To fix
the issue, a CO_RFL flag was added to instruct the mux it should still wait for
read events to properly handle read0. Only the H1 mux was concerned.
But since then, most of time, the option is only handled if it is set on the
frontend proxy because the request is fully received before selecting the
backend. If the backend is selected before the end of the request there is no
issue. But otherwise, because the backend is not known yet, we are unable to
properly handle the option and we miss to subscribe for reads.
Of course the option cannot be set on a frontend proxy. So concretly it means
the option is properly handled if it is enabled in the defaults section (if
common to frontend and backend) or a listen proxy, but it is ignored if it is
set on backend only.
Thanks to previous patches, we can now instruct the mux it should subscribe for
reads if not already done. We use this mechanism in process_stream() when the
connection is set up, ie when backend SC is set to SC_ST_REQ state.
This patch relies on following patches:
* MINOR: connection: Add a CTL flag to notify mux it should wait for reads again
* MEDIUM: mux-h1: Handle MUX_SUBS_RECV flag in h1_ctl() and susbscribe for reads
This patch should be the issue #2344. All the series must be backported as far
as 2.2.
2023-11-14 01:47:52 -05:00
if ( s - > be - > options & PR_O_ABRT_CLOSE ) {
struct connection * conn = sc_conn ( scf ) ;
2024-10-22 02:26:20 -04:00
se_have_more_data ( scf - > sedesc ) ;
2023-11-14 13:18:53 -05:00
if ( conn & & conn - > mux & & conn - > mux - > ctl )
2023-11-28 08:27:51 -05:00
conn - > mux - > ctl ( conn , MUX_CTL_SUBS_RECV , NULL ) ;
BUG/MEDIUM: stream: Properly handle abortonclose when set on backend only
Since the 2.2 and the commit dedd30610 ("MEDIUM: h1: Don't wake the H1 tasklet
if we got the whole request."), we avoid to subscribe for reads if the H1
message is fully received. However, this broke the abortonclose option. To fix
the issue, a CO_RFL flag was added to instruct the mux it should still wait for
read events to properly handle read0. Only the H1 mux was concerned.
But since then, most of time, the option is only handled if it is set on the
frontend proxy because the request is fully received before selecting the
backend. If the backend is selected before the end of the request there is no
issue. But otherwise, because the backend is not known yet, we are unable to
properly handle the option and we miss to subscribe for reads.
Of course the option cannot be set on a frontend proxy. So concretly it means
the option is properly handled if it is enabled in the defaults section (if
common to frontend and backend) or a listen proxy, but it is ignored if it is
set on backend only.
Thanks to previous patches, we can now instruct the mux it should subscribe for
reads if not already done. We use this mechanism in process_stream() when the
connection is set up, ie when backend SC is set to SC_ST_REQ state.
This patch relies on following patches:
* MINOR: connection: Add a CTL flag to notify mux it should wait for reads again
* MEDIUM: mux-h1: Handle MUX_SUBS_RECV flag in h1_ctl() and susbscribe for reads
This patch should be the issue #2344. All the series must be backported as far
as 2.2.
2023-11-14 01:47:52 -05:00
}
2009-09-19 15:04:57 -04:00
}
2009-08-16 12:27:24 -04:00
}
2009-09-20 02:19:25 -04:00
else {
2022-05-17 13:47:17 -04:00
s - > scb - > state = SC_ST_CLO ; /* shutw+ini = abort */
2023-04-13 09:56:26 -04:00
sc_schedule_shutdown ( scb ) ;
2023-04-13 09:40:10 -04:00
sc_schedule_abort ( scb ) ;
2009-09-20 02:19:25 -04:00
}
2009-03-06 06:51:23 -05:00
}
2008-11-30 12:47:21 -05:00
/* we may have a pending connection request, or a connection waiting
* for completion .
*/
2022-05-27 03:03:30 -04:00
if ( sc_state_in ( scb - > state , SC_SB_REQ | SC_SB_QUE | SC_SB_TAR | SC_SB_ASS ) ) {
2015-06-06 13:29:07 -04:00
/* prune the request variables and swap to the response variables. */
if ( s - > vars_reqres . scope ! = SCOPE_RES ) {
2024-09-15 17:15:59 -04:00
vars_prune ( & s - > vars_reqres , s - > sess , s ) ;
2021-08-31 02:13:25 -04:00
vars_init_head ( & s - > vars_reqres , SCOPE_RES ) ;
2015-06-06 13:29:07 -04:00
}
2008-11-30 12:47:21 -05:00
do {
/* nb: step 1 might switch from QUE to ASS, but we first want
* to give a chance to step 2 to perform a redirect if needed .
*/
2022-05-17 13:47:17 -04:00
if ( scb - > state ! = SC_ST_REQ )
2020-01-09 12:43:15 -05:00
back_try_conn_req ( s ) ;
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_REQ )
2020-01-09 12:43:15 -05:00
back_handle_st_req ( s ) ;
2008-11-30 12:47:21 -05:00
2020-03-04 10:42:03 -05:00
/* get a chance to complete an immediate connection setup */
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_RDY )
2022-05-17 13:07:51 -04:00
goto resync_stconns ;
2020-03-04 10:42:03 -05:00
2013-12-31 17:32:12 -05:00
/* applets directly go to the ESTABLISHED state. Similarly,
* servers experience the same fate when their connection
* is reused .
*/
2022-05-17 13:47:17 -04:00
if ( unlikely ( scb - > state = = SC_ST_EST ) )
2020-01-09 12:43:15 -05:00
back_establish ( s ) ;
2013-12-31 17:16:50 -05:00
2012-11-11 18:42:33 -05:00
srv = objt_server ( s - > target ) ;
2022-05-17 13:47:17 -04:00
if ( scb - > state = = SC_ST_ASS & & srv & & srv - > rdr_len & & ( s - > flags & SF_REDIRECTABLE ) )
2022-05-17 13:40:40 -04:00
http_perform_server_redirect ( s , scb ) ;
2022-05-17 13:47:17 -04:00
} while ( scb - > state = = SC_ST_ASS ) ;
2008-11-30 12:47:21 -05:00
}
2019-06-06 03:17:23 -04:00
/* Let's see if we can send the pending request now */
2024-02-01 10:36:25 -05:00
sc_sync_send ( scb ) ;
2019-06-06 03:17:23 -04:00
/*
* Now forward all shutdown requests between both sides of the request buffer
*/
/* first, let's check if the request buffer needs to shutdown(write), which may
* happen either because the input is closed or because we want to force a close
* once the server has begun to respond . If a half - closed timeout is set , we adjust
2022-04-14 11:39:48 -04:00
* the other side ' s timeout as well . However this doesn ' t have effect during the
* connection setup unless the backend has abortonclose set .
2019-06-06 03:17:23 -04:00
*/
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( req - > flags & CF_AUTO_CLOSE ) & & ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & &
2023-04-13 10:16:15 -04:00
! ( scb - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) & &
2022-05-17 13:47:17 -04:00
( scb - > state ! = SC_ST_CON | | ( s - > be - > options & PR_O_ABRT_CLOSE ) ) ) ) {
2023-04-13 09:56:26 -04:00
sc_schedule_shutdown ( scb ) ;
2019-06-06 03:17:23 -04:00
}
/* shutdown(write) pending */
2023-04-13 10:16:15 -04:00
if ( unlikely ( ( scb - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) = = SC_FL_SHUT_WANTED & &
2024-04-02 12:01:23 -04:00
( ( ! co_data ( req ) & & ! sc_ep_have_ff_data ( scb ) ) | | ( req - > flags & CF_WRITE_TIMEOUT ) ) ) ) {
2023-04-14 06:07:26 -04:00
if ( scf - > flags & SC_FL_ERROR )
2022-05-17 13:44:42 -04:00
scb - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:23:48 -04:00
sc_shutdown ( scb ) ;
2019-06-06 03:17:23 -04:00
}
/* shutdown(write) done on server side, we must stop the client too */
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( scb - > flags & SC_FL_SHUT_DONE ) & & ! ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ) & &
2023-04-03 12:32:50 -04:00
! req - > analysers )
2023-04-13 09:40:10 -04:00
sc_schedule_abort ( scf ) ;
2019-06-06 03:17:23 -04:00
/* shutdown(read) pending */
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) = = SC_FL_ABRT_WANTED ) ) {
2022-05-17 13:44:42 -04:00
if ( scf - > flags & SC_FL_NOHALF )
scf - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:10:23 -04:00
sc_abort ( scf ) ;
2019-06-06 03:17:23 -04:00
}
2009-06-21 16:43:05 -04:00
/* Benchmarks have shown that it's optimal to do a full resync now */
2022-05-17 13:47:17 -04:00
if ( scf - > state = = SC_ST_DIS | |
2022-05-27 03:03:30 -04:00
sc_state_in ( scb - > state , SC_SB_RDY | SC_SB_DIS ) | |
2023-04-14 05:59:15 -04:00
( ( scf - > flags & SC_FL_ERROR ) & & scf - > state ! = SC_ST_CLO ) | |
( ( scb - > flags & SC_FL_ERROR ) & & scb - > state ! = SC_ST_CLO ) )
2022-05-17 13:07:51 -04:00
goto resync_stconns ;
2008-11-30 12:47:21 -05:00
2010-07-27 11:15:12 -04:00
/* otherwise we want to check if we need to resync the req buffer or not */
2023-04-17 10:17:32 -04:00
if ( ( ( scf - > flags ^ scf_flags ) & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) | | ( ( scb - > flags ^ scb_flags ) & SC_FL_SHUT_DONE ) )
2009-03-08 14:20:25 -04:00
goto resync_request ;
2009-06-21 16:43:05 -04:00
/* perform output updates to the response buffer */
2009-03-15 17:34:05 -04:00
2021-01-06 11:20:16 -05:00
/* If no one is interested in analysing data, it's time to forward
2009-09-20 06:07:52 -04:00
* everything . We configure the buffer to forward indefinitely .
2023-04-13 09:39:30 -04:00
* Note that we ' re checking SC_FL_ABRT_WANTED as an indication of a possible
2012-08-27 18:06:31 -04:00
* recent call to channel_abort ( ) .
2008-12-14 11:31:54 -05:00
*/
2017-08-29 10:06:38 -04:00
if ( unlikely ( ( ! res - > analysers | | ( res - > analysers = = AN_RES_FLT_END & & ! ( res - > flags & CF_FLT_ANALYZE ) ) ) & &
2023-04-13 09:45:24 -04:00
! ( scf - > flags & SC_FL_ABRT_WANTED ) & & ! ( scb - > flags & SC_FL_SHUT_WANTED ) & &
2023-04-03 12:32:50 -04:00
sc_state_in ( scb - > state , SC_SB_EST | SC_SB_DIS | SC_SB_CLO ) & &
( res - > to_forward ! = CHN_INFINITE_FORWARD ) ) ) {
2012-11-11 17:05:39 -05:00
/* This buffer is freewheeling, there's no analyser
2009-03-08 16:38:23 -04:00
* attached to it . If any data are left in , we ' ll permit them to
* move .
*/
2014-11-28 09:07:47 -05:00
channel_auto_read ( res ) ;
channel_auto_close ( res ) ;
2010-11-07 14:26:56 -05:00
2018-12-18 15:57:24 -05:00
if ( IS_HTX_STRM ( s ) ) {
struct htx * htx = htxbuf ( & res - > buf ) ;
2012-05-12 06:50:00 -04:00
2018-12-05 05:56:15 -05:00
/* We'll let data flow between the producer (if still connected)
* to the consumer .
*/
2018-12-18 15:57:24 -05:00
co_set_data ( res , htx - > data ) ;
2023-04-03 12:32:50 -04:00
if ( ( global . tune . options & GTUNE_USE_FAST_FWD ) & &
2023-04-17 10:17:32 -04:00
! ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ! ( scb - > flags & SC_FL_SHUT_WANTED ) )
2018-12-18 15:57:24 -05:00
channel_htx_forward_forever ( res , htx ) ;
2018-12-05 05:56:15 -05:00
}
else {
/* We'll let data flow between the producer (if still connected)
* to the consumer .
*/
2018-12-18 15:57:24 -05:00
c_adv ( res , ci_data ( res ) ) ;
2023-04-03 12:32:50 -04:00
if ( ( global . tune . options & GTUNE_USE_FAST_FWD ) & &
2023-04-17 10:17:32 -04:00
! ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & & ! ( scb - > flags & SC_FL_SHUT_WANTED ) )
2018-12-05 05:56:15 -05:00
channel_forward_forever ( res ) ;
}
2015-07-09 12:38:57 -04:00
2012-05-12 06:50:00 -04:00
/* if we have no analyser anymore in any direction and have a
2014-05-10 08:30:07 -04:00
* tunnel timeout set , use it now . Note that we must respect
* the half - closed timeouts as well .
2012-05-12 06:50:00 -04:00
*/
2020-12-10 07:43:53 -05:00
if ( ! req - > analysers & & s - > tunnel_timeout ) {
2023-02-15 02:13:33 -05:00
scf - > ioto = scb - > ioto = s - > tunnel_timeout ;
2014-05-10 08:30:07 -04:00
BUG/MINOR: stream: do not use client-fin/server-fin with HTX
Historically the client-fin and server-fin timeouts were made to allow
a connection closure to be effective quickly if the last data were sent
down a socket and the client didn't close, something that can happen
when the peer's FIN is lost and retransmits are blocked by a firewall
for example. This made complete sense in 1.5 for TCP and HTTP in close
mode. But nowadays with muxes, it's not done at the right layer anymore
and even the description doesn't match what is being done, because what
happens is that the stream will abort the whole transfer after it's done
sending to the mux and this timeout expires.
We've seen in GH issue 2095 that this can happen with very short timeout
values, and while this didn't trigger often before, now that the muxes
(h2 & quic) properly report an end of stream before even the first
sc_conn_sync_recv(), it seems that it can happen more often, and have
two undesirable effects:
- logging a timeout when that's not the case
- aborting the request channel, hence the server-side conn, possibly
before it had a chance to be put back to the idle list, causing
this connection to be closed and not reusable.
Unfortunately for TCP (mux_pt) this remains necessary because the mux
doesn't have a timeout task. So here we're adding tests to only do
this through an HTX mux. But to be really clean we should in fact
completely drop all of this and implement these timeouts in the mux
itself.
This needs to be backported to 2.8 where the issue was discovered,
and maybe carefully to older versions, though that is not sure at
all. In any case, using a higher timeout or removing client-fin in
HTTP proxies is sufficient to make the issue disappear.
2023-06-02 10:19:51 -04:00
if ( ! IS_HTX_STRM ( s ) ) {
if ( ( scf - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_SHUT_DONE ) ) & & tick_isset ( sess - > fe - > timeout . clientfin ) )
scf - > ioto = sess - > fe - > timeout . clientfin ;
if ( ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_SHUT_DONE ) ) & & tick_isset ( s - > be - > timeout . serverfin ) )
scb - > ioto = s - > be - > timeout . serverfin ;
}
2012-05-12 06:50:00 -04:00
}
2008-12-14 11:31:54 -05:00
}
2008-12-13 15:12:26 -05:00
2008-11-30 12:47:21 -05:00
/* reflect what the L7 analysers have seen last */
2014-11-28 09:07:47 -05:00
rpf_last = res - > flags ;
2023-04-17 10:17:32 -04:00
scb_flags = ( scb_flags & ~ ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ;
2023-04-13 10:16:15 -04:00
scf_flags = ( scf_flags & ~ ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | ( scf - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) ;
2008-11-30 12:47:21 -05:00
2019-06-06 03:17:23 -04:00
/* Let's see if we can send the pending response now */
2024-02-01 10:36:25 -05:00
sc_sync_send ( scf ) ;
2019-06-06 03:17:23 -04:00
2008-11-30 12:47:21 -05:00
/*
* Now forward all shutdown requests between both sides of the buffer
*/
/*
* FIXME : this is probably where we should produce error responses .
*/
2008-12-14 11:31:54 -05:00
/* first, let's check if the response buffer needs to shutdown(write) */
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( res - > flags & CF_AUTO_CLOSE ) & & ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE ) ) & &
2023-04-13 10:16:15 -04:00
! ( scf - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) ) ) {
2023-04-13 09:56:26 -04:00
sc_schedule_shutdown ( scf ) ;
2014-05-10 08:30:07 -04:00
}
2008-11-30 12:47:21 -05:00
/* shutdown(write) pending */
2023-04-13 10:16:15 -04:00
if ( unlikely ( ( scf - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) = = SC_FL_SHUT_WANTED & &
2024-04-02 12:01:23 -04:00
( ( ! co_data ( res ) & & ! sc_ep_have_ff_data ( scf ) ) | | ( res - > flags & CF_WRITE_TIMEOUT ) ) ) ) {
2023-04-13 10:23:48 -04:00
sc_shutdown ( scf ) ;
2014-05-10 08:30:07 -04:00
}
2008-11-30 12:47:21 -05:00
/* shutdown(write) done on the client side, we must stop the server too */
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( scf - > flags & SC_FL_SHUT_DONE ) & & ! ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) ) & &
2014-11-28 09:07:47 -05:00
! res - > analysers )
2023-04-13 09:40:10 -04:00
sc_schedule_abort ( scb ) ;
2008-11-30 12:47:21 -05:00
/* shutdown(read) pending */
2023-04-17 10:17:32 -04:00
if ( unlikely ( ( scb - > flags & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) = = SC_FL_ABRT_WANTED ) ) {
2022-05-17 13:44:42 -04:00
if ( scb - > flags & SC_FL_NOHALF )
scb - > flags | = SC_FL_NOLINGER ;
2023-04-13 10:10:23 -04:00
sc_abort ( scb ) ;
2012-05-13 08:48:59 -04:00
}
2008-11-30 12:47:21 -05:00
2022-05-17 13:47:17 -04:00
if ( scf - > state = = SC_ST_DIS | |
2022-05-27 03:03:30 -04:00
sc_state_in ( scb - > state , SC_SB_RDY | SC_SB_DIS ) | |
2023-04-14 05:59:15 -04:00
( ( scf - > flags & SC_FL_ERROR ) & & scf - > state ! = SC_ST_CLO ) | |
( ( scb - > flags & SC_FL_ERROR ) & & scb - > state ! = SC_ST_CLO ) )
2022-05-17 13:07:51 -04:00
goto resync_stconns ;
2008-11-30 12:47:21 -05:00
2019-06-06 08:32:49 -04:00
if ( ( req - > flags & ~ rqf_last ) & CF_MASK_ANALYSER )
2009-03-08 14:20:25 -04:00
goto resync_request ;
2023-04-17 10:17:32 -04:00
if ( ( ( scb - > flags ^ scb_flags ) & ( SC_FL_EOS | SC_FL_ABRT_DONE | SC_FL_ABRT_WANTED ) ) | |
2023-05-10 10:40:27 -04:00
( ( scf - > flags ^ scf_flags ) & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) ) | |
( res - > analysers ^ res_ana_back ) )
2009-03-08 14:20:25 -04:00
goto resync_response ;
2008-11-30 12:47:21 -05:00
2023-05-10 10:40:27 -04:00
if ( ( ( ( req - > flags ^ rqf_last ) | ( res - > flags ^ rpf_last ) ) & CF_MASK_ANALYSER ) | |
( req - > analysers ^ req_ana_back ) )
2019-06-06 03:17:23 -04:00
goto resync_request ;
2009-09-05 14:57:35 -04:00
/* we're interested in getting wakeups again */
2022-05-17 13:44:42 -04:00
scf - > flags & = ~ SC_FL_DONT_WAKE ;
scb - > flags & = ~ SC_FL_DONT_WAKE ;
2009-09-05 14:57:35 -04:00
2022-05-27 03:03:30 -04:00
if ( likely ( ( scf - > state ! = SC_ST_CLO ) | | ! sc_state_in ( scb - > state , SC_SB_INI | SC_SB_CLO ) | |
2019-11-13 05:12:32 -05:00
( req - > analysers & AN_REQ_FLT_END ) | | ( res - > analysers & AN_RES_FLT_END ) ) ) {
2019-07-31 12:05:26 -04:00
if ( ( sess - > fe - > options & PR_O_CONTSTATS ) & & ( s - > flags & SF_BE_ASSIGNED ) & & ! ( s - > flags & SF_IGNORE ) )
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream_process_counters ( s ) ;
2008-11-30 12:47:21 -05:00
2022-05-27 04:13:37 -04:00
stream_update_both_sc ( s ) ;
2008-11-30 12:47:21 -05:00
BUG/MEDIUM: stream: Save unprocessed events for a stream
A stream can be awakened for different reasons. During its processing, it can be
early stopped if no buffer is available. In this situation, the reason why the
stream was awakened is lost, because we rely on the task state, which is reset
after each processing loop.
In many cases, that's not a big deal. But it can be useful to accumulate the
task states if the stream processing is interrupted, especially if some filters
need to be called.
To be clearer, here is an simple example:
1) A stream is awakened with the reason TASK_WOKEN_MSG.
2) Because no buffer is available, the processing is interrupted, the stream
is back to sleep. And the task state is reset.
3) Some buffers become available, so the stream is awakened with the reason
TASK_WOKEN_RES. At this step, the previous reason (TASK_WOKEN_MSG) is lost.
Now, the task states are saved for a stream and reset only when the stream
processing is not interrupted. The correspoing bitfield represents the pending
events for a stream. And we use this one instead of the task state during the
stream processing.
Note that TASK_WOKEN_TIMER and TASK_WOKEN_RES are always removed because these
events are always handled during the stream processing.
[wt: backport to 1.7 and 1.6]
2016-12-08 16:33:52 -05:00
/* Reset pending events now */
2025-01-27 12:38:40 -05:00
s - > pending_events = STRM_EVT_NONE ;
BUG/MEDIUM: stream: Save unprocessed events for a stream
A stream can be awakened for different reasons. During its processing, it can be
early stopped if no buffer is available. In this situation, the reason why the
stream was awakened is lost, because we rely on the task state, which is reset
after each processing loop.
In many cases, that's not a big deal. But it can be useful to accumulate the
task states if the stream processing is interrupted, especially if some filters
need to be called.
To be clearer, here is an simple example:
1) A stream is awakened with the reason TASK_WOKEN_MSG.
2) Because no buffer is available, the processing is interrupted, the stream
is back to sleep. And the task state is reset.
3) Some buffers become available, so the stream is awakened with the reason
TASK_WOKEN_RES. At this step, the previous reason (TASK_WOKEN_MSG) is lost.
Now, the task states are saved for a stream and reset only when the stream
processing is not interrupted. The correspoing bitfield represents the pending
events for a stream. And we use this one instead of the task state during the
stream processing.
Note that TASK_WOKEN_TIMER and TASK_WOKEN_RES are always removed because these
events are always handled during the stream processing.
[wt: backport to 1.7 and 1.6]
2016-12-08 16:33:52 -05:00
2012-11-08 08:49:17 -05:00
update_exp_and_leave :
2022-05-17 13:44:42 -04:00
/* Note: please ensure that if you branch here you disable SC_FL_DONT_WAKE */
2016-11-08 16:03:00 -05:00
if ( ! req - > analysers )
req - > analyse_exp = TICK_ETERNITY ;
2023-08-01 02:25:01 -04:00
if ( ! res - > analysers )
res - > analyse_exp = TICK_ETERNITY ;
2016-11-08 16:03:00 -05:00
if ( ( sess - > fe - > options & PR_O_CONTSTATS ) & & ( s - > flags & SF_BE_ASSIGNED ) & &
( ! tick_isset ( req - > analyse_exp ) | | tick_is_expired ( req - > analyse_exp , now_ms ) ) )
req - > analyse_exp = tick_add ( now_ms , 5000 ) ;
2023-11-06 02:45:22 -05:00
t - > expire = ( tick_is_expired ( t - > expire , now_ms ) ? 0 : t - > expire ) ;
t - > expire = tick_first ( t - > expire , sc_ep_rcv_ex ( scf ) ) ;
t - > expire = tick_first ( t - > expire , sc_ep_snd_ex ( scf ) ) ;
t - > expire = tick_first ( t - > expire , sc_ep_rcv_ex ( scb ) ) ;
t - > expire = tick_first ( t - > expire , sc_ep_snd_ex ( scb ) ) ;
2016-11-08 16:03:00 -05:00
t - > expire = tick_first ( t - > expire , req - > analyse_exp ) ;
2017-11-10 11:14:23 -05:00
t - > expire = tick_first ( t - > expire , res - > analyse_exp ) ;
2022-03-29 13:02:31 -04:00
t - > expire = tick_first ( t - > expire , s - > conn_exp ) ;
2008-11-30 12:47:21 -05:00
2023-02-27 10:21:00 -05:00
if ( unlikely ( tick_is_expired ( t - > expire , now_ms ) ) ) {
/* Some events prevented the timeouts to be handled but nothing evolved.
So do it now and resyunc the stconns
*/
stream_handle_timeouts ( s ) ;
goto resync_stconns ;
}
2024-04-29 02:52:40 -04:00
leave :
2025-01-27 12:38:40 -05:00
s - > pending_events & = ~ STRM_EVT_TIMER ;
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream_release_buffers ( s ) ;
2019-11-05 10:18:10 -05:00
DBG_TRACE_DEVEL ( " queuing " , STRM_EV_STRM_PROC , s ) ;
2009-03-08 04:38:41 -04:00
return t ; /* nothing more to do */
2008-11-30 12:47:21 -05:00
}
2019-11-05 10:18:10 -05:00
DBG_TRACE_DEVEL ( " releasing " , STRM_EV_STRM_PROC , s ) ;
2015-04-02 19:14:29 -04:00
if ( s - > flags & SF_BE_ASSIGNED )
2021-04-06 07:53:36 -04:00
_HA_ATOMIC_DEC ( & s - > be - > beconn ) ;
2017-09-15 03:07:56 -04:00
2008-11-30 12:47:21 -05:00
if ( unlikely ( ( global . mode & MODE_DEBUG ) & &
( ! ( global . mode & MODE_QUIET ) | | ( global . mode & MODE_VERBOSE ) ) ) ) {
2012-10-29 11:51:55 -04:00
chunk_printf ( & trash , " %08x:%s.closed[%04x:%04x] \n " ,
2021-12-15 03:50:17 -05:00
s - > uniq_id , s - > be - > id ,
2022-05-18 10:23:22 -04:00
( unsigned short ) conn_fd ( sc_conn ( scf ) ) ,
( unsigned short ) conn_fd ( sc_conn ( scb ) ) ) ;
2020-03-14 06:03:20 -04:00
DISGUISE ( write ( 1 , trash . area , trash . data ) ) ;
2008-11-30 12:47:21 -05:00
}
2021-01-21 11:10:44 -05:00
if ( ! ( s - > flags & SF_IGNORE ) ) {
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
uint8_t do_log = 0 ;
2023-04-28 03:16:15 -04:00
s - > logs . t_close = ns_to_ms ( now_ns - s - > logs . accept_ts ) ;
2021-01-21 11:10:44 -05:00
2019-07-31 12:05:26 -04:00
stream_process_counters ( s ) ;
2008-11-30 12:47:21 -05:00
2021-01-21 11:10:44 -05:00
if ( s - > txn & & s - > txn - > status ) {
int n ;
2009-10-24 09:36:15 -04:00
2021-01-21 11:10:44 -05:00
n = s - > txn - > status / 100 ;
if ( n < 1 | | n > 5 )
n = 0 ;
2009-10-24 09:36:15 -04:00
2021-01-21 11:10:44 -05:00
if ( sess - > fe - > mode = = PR_MODE_HTTP ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & sess - > fe - > fe_counters . shared - > tg [ tgid - 1 ] - > p . http . rsp [ n ] ) ;
2021-01-21 11:10:44 -05:00
}
if ( ( s - > flags & SF_BE_ASSIGNED ) & &
( s - > be - > mode = = PR_MODE_HTTP ) ) {
2025-05-15 13:55:11 -04:00
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > p . http . rsp [ n ] ) ;
_HA_ATOMIC_INC ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > p . http . cum_req ) ;
2021-01-21 11:10:44 -05:00
}
2012-11-24 08:54:13 -05:00
}
2021-01-21 11:10:44 -05:00
/* let's do a final log if we need it */
MEDIUM: log: consider log-steps proxy setting for existing log origins
During tcp/http transaction processing, haproxy may produce logs at
different steps during the processing (accept, connect, request,
response, close). But the behavior is hardly configurable because
haproxy will only emit a single log per transaction, and by default
it will try to produce the log once all log aliases or fetches used
in the logformat could be satisfied, which means the log is often
emitted during connection teardown, unless "option logasap" is used.
We were often asked to have a way to emit multiple logs for a single
transaction, like for instance emit log during accept, then request,
response and close for instance, see GH #401 for more context.
Thanks to "log-steps" keyword introduced by commit "MINOR: log:
introduce "log-steps" proxy keyword", it is now possible to explictly
configure when logs should be generated by haproxy when processing a
transaction. This commit adds the required checks so that log-steps
proxy option is properly considered for existing logs generated by
haproxy. If "log-steps" is not specified on the proxy, the old behavior
is preserved.
Note: a slight cpu overhead should only be visible when "log-steps"
keyword will be used due to the implementation relying on eb32 lookup
instead of basic bitfield check as described in "MINOR: proxy: add
log_steps struct member". However, the default behavior shouldn't be
affected.
When combining log-steps with log-profiles, user has the ability to
explicitly control how and when haproxy should generate logs during
requests handling.
2024-09-04 09:03:46 -04:00
if ( sess - > fe - > to_log = = LW_LOGSTEPS ) {
if ( log_orig_proxy ( LOG_ORIG_TXN_CLOSE , sess - > fe ) )
do_log = 1 ;
}
else if ( ! lf_expr_isempty ( & sess - > fe - > logformat ) & & s - > logs . logwait )
do_log = 1 ;
if ( do_log & &
2021-01-21 11:10:44 -05:00
! ( s - > flags & SF_MONITOR ) & &
( ! ( sess - > fe - > options & PR_O_NULLNOLOG ) | | req - > total ) ) {
/* we may need to know the position in the queue */
pendconn_free ( s ) ;
2022-09-07 10:17:49 -04:00
stream_cond_update_cpu_usage ( s ) ;
2024-09-23 11:22:45 -04:00
s - > do_log ( s , log_orig ( LOG_ORIG_TXN_CLOSE , LOG_ORIG_FL_NONE ) ) ;
2012-11-24 08:54:13 -05:00
}
2009-10-24 09:36:15 -04:00
2021-01-21 11:10:44 -05:00
/* update time stats for this stream */
stream_update_time_stats ( s ) ;
2008-11-30 12:47:21 -05:00
}
/* the task MUST not be in the run queue anymore */
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream_free ( s ) ;
2019-04-17 16:51:06 -04:00
task_destroy ( t ) ;
2009-03-08 04:38:41 -04:00
return NULL ;
2008-11-30 12:47:21 -05:00
}
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
/* Update the stream's backend and server time stats */
void stream_update_time_stats ( struct stream * s )
2014-06-17 06:19:18 -04:00
{
int t_request ;
int t_queue ;
int t_connect ;
int t_data ;
int t_close ;
struct server * srv ;
2020-05-15 14:02:40 -04:00
unsigned int samples_window ;
2014-06-17 06:19:18 -04:00
t_request = 0 ;
t_queue = s - > logs . t_queue ;
t_connect = s - > logs . t_connect ;
t_close = s - > logs . t_close ;
t_data = s - > logs . t_data ;
if ( s - > be - > mode ! = PR_MODE_HTTP )
t_data = t_connect ;
if ( t_connect < 0 | | t_data < 0 )
return ;
2023-04-27 03:46:02 -04:00
if ( ( llong ) ( s - > logs . request_ts - s - > logs . accept_ts ) > = 0 )
t_request = ns_to_ms ( s - > logs . request_ts - s - > logs . accept_ts ) ;
2014-06-17 06:19:18 -04:00
t_data - = t_connect ;
t_connect - = t_queue ;
t_queue - = t_request ;
srv = objt_server ( s - > target ) ;
if ( srv ) {
2020-05-15 14:02:40 -04:00
samples_window = ( ( ( s - > be - > mode = = PR_MODE_HTTP ) ?
2025-05-15 13:55:11 -04:00
HA_ATOMIC_LOAD ( & srv - > counters . shared - > tg [ tgid - 1 ] - > p . http . cum_req ) : HA_ATOMIC_LOAD ( & srv - > counters . shared - > tg [ tgid - 1 ] - > cum_lbconn ) ) > TIME_STATS_SAMPLES ) ? TIME_STATS_SAMPLES : 0 ;
2020-05-15 14:02:40 -04:00
swrate_add_dynamic ( & srv - > counters . q_time , samples_window , t_queue ) ;
swrate_add_dynamic ( & srv - > counters . c_time , samples_window , t_connect ) ;
swrate_add_dynamic ( & srv - > counters . d_time , samples_window , t_data ) ;
swrate_add_dynamic ( & srv - > counters . t_time , samples_window , t_close ) ;
MINOR: counters: Add fields to store the max observed for {q,c,d,t}_time
For backends and servers, some average times for last 1024 connections are
already calculated. For the moment, the averages for the time passed in the
queue, the connect time, the response time (for HTTP session only) and the total
time are calculated. Now, in addition, the maximum time observed for these
values are also stored.
In addition, These new counters are cleared as all other max values with the CLI
command "clear counters".
This patch is related to #272.
2019-11-08 08:53:15 -05:00
HA_ATOMIC_UPDATE_MAX ( & srv - > counters . qtime_max , t_queue ) ;
HA_ATOMIC_UPDATE_MAX ( & srv - > counters . ctime_max , t_connect ) ;
HA_ATOMIC_UPDATE_MAX ( & srv - > counters . dtime_max , t_data ) ;
HA_ATOMIC_UPDATE_MAX ( & srv - > counters . ttime_max , t_close ) ;
2014-06-17 06:19:18 -04:00
}
2020-05-15 14:02:40 -04:00
samples_window = ( ( ( s - > be - > mode = = PR_MODE_HTTP ) ?
2025-05-15 13:55:11 -04:00
HA_ATOMIC_LOAD ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > p . http . cum_req ) : HA_ATOMIC_LOAD ( & s - > be - > be_counters . shared - > tg [ tgid - 1 ] - > cum_lbconn ) ) > TIME_STATS_SAMPLES ) ? TIME_STATS_SAMPLES : 0 ;
2020-05-15 14:02:40 -04:00
swrate_add_dynamic ( & s - > be - > be_counters . q_time , samples_window , t_queue ) ;
swrate_add_dynamic ( & s - > be - > be_counters . c_time , samples_window , t_connect ) ;
swrate_add_dynamic ( & s - > be - > be_counters . d_time , samples_window , t_data ) ;
swrate_add_dynamic ( & s - > be - > be_counters . t_time , samples_window , t_close ) ;
MINOR: counters: Add fields to store the max observed for {q,c,d,t}_time
For backends and servers, some average times for last 1024 connections are
already calculated. For the moment, the averages for the time passed in the
queue, the connect time, the response time (for HTTP session only) and the total
time are calculated. Now, in addition, the maximum time observed for these
values are also stored.
In addition, These new counters are cleared as all other max values with the CLI
command "clear counters".
This patch is related to #272.
2019-11-08 08:53:15 -05:00
HA_ATOMIC_UPDATE_MAX ( & s - > be - > be_counters . qtime_max , t_queue ) ;
HA_ATOMIC_UPDATE_MAX ( & s - > be - > be_counters . ctime_max , t_connect ) ;
HA_ATOMIC_UPDATE_MAX ( & s - > be - > be_counters . dtime_max , t_data ) ;
HA_ATOMIC_UPDATE_MAX ( & s - > be - > be_counters . ttime_max , t_close ) ;
2014-06-17 06:19:18 -04:00
}
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
/*
* This function adjusts sess - > srv_conn and maintains the previous and new
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* server ' s served stream counts . Setting newsrv to NULL is enough to release
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
* current connection slot . This function also notifies any LB algo which might
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* expect to be informed about any change in the number of active streams on a
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
* server .
*/
2021-03-09 09:43:32 -05:00
void sess_change_server ( struct stream * strm , struct server * newsrv )
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
{
2021-03-09 09:43:32 -05:00
struct server * oldsrv = strm - > srv_conn ;
2021-02-17 07:33:24 -05:00
2024-02-21 09:54:11 -05:00
/* Dynamic servers may be deleted during process lifetime. This
* operation is always conducted under thread isolation . Several
* conditions prevent deletion , one of them is if server streams list
* is not empty . sess_change_server ( ) uses stream_add_srv_conn ( ) to
* ensure the latter condition .
*
* A race condition could exist for stream which referenced a server
* instance ( s - > target ) without registering itself in its server list .
* This is notably the case for SF_DIRECT streams which referenced a
* server earlier during process_stream ( ) . However at this time the
* code is deemed safe as process_stream ( ) cannot be rescheduled before
* invocation of sess_change_server ( ) .
*/
2025-03-18 06:24:56 -04:00
if ( oldsrv = = newsrv ) {
/*
* It is assumed if the stream has a non - NULL srv_conn , then its
* served field has been incremented , so we have to decrement it now .
*/
if ( oldsrv )
_HA_ATOMIC_DEC ( & oldsrv - > served ) ;
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
return ;
2025-03-18 06:24:56 -04:00
}
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
2021-02-17 07:33:24 -05:00
if ( oldsrv ) {
2025-03-18 06:24:56 -04:00
/* Note: we cannot decrement served after calling server_drop_conn
* because that one may rely on served ( e . g . for leastconn ) . The
* real need here is to make sure that when served = = 0 , no stream
* knows the server anymore , mainly for server removal purposes ,
* and that removal will be done under isolation anyway . Thus by
* decrementing served after detaching from the list , we ' re
* guaranteeing that served = = 0 implies that no stream is in the
2025-04-02 14:44:22 -04:00
* list anymore , which is a sufficient guarantee for that goal .
2025-03-18 06:24:56 -04:00
*/
2021-04-06 07:53:36 -04:00
_HA_ATOMIC_DEC ( & oldsrv - > proxy - > served ) ;
2025-03-18 06:24:56 -04:00
stream_del_srv_conn ( strm ) ;
_HA_ATOMIC_DEC ( & oldsrv - > served ) ;
2019-03-08 12:54:51 -05:00
__ha_barrier_atomic_store ( ) ;
2021-02-17 10:01:37 -05:00
if ( oldsrv - > proxy - > lbprm . server_drop_conn )
2021-06-18 12:29:25 -04:00
oldsrv - > proxy - > lbprm . server_drop_conn ( oldsrv ) ;
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
}
if ( newsrv ) {
2021-04-06 07:53:36 -04:00
_HA_ATOMIC_INC ( & newsrv - > proxy - > served ) ;
2019-03-08 12:54:51 -05:00
__ha_barrier_atomic_store ( ) ;
2021-02-17 10:01:37 -05:00
if ( newsrv - > proxy - > lbprm . server_take_conn )
2021-06-18 12:29:25 -04:00
newsrv - > proxy - > lbprm . server_take_conn ( newsrv ) ;
2021-03-09 09:43:32 -05:00
stream_add_srv_conn ( strm , newsrv ) ;
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
}
}
2009-03-15 17:34:05 -04:00
/* Handle server-side errors for default protocols. It is called whenever a a
* connection setup is aborted or a request is aborted in queue . It sets the
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
* stream termination flags so that the caller does not have to worry about
2022-05-17 13:07:51 -04:00
* them . It ' s installed as - > srv_error for the server - side stream connector .
2009-03-15 17:34:05 -04:00
*/
2022-05-27 04:13:37 -04:00
void default_srv_error ( struct stream * s , struct stconn * sc )
2009-03-15 17:34:05 -04:00
{
2022-03-30 13:39:30 -04:00
int err_type = s - > conn_err_type ;
2009-03-15 17:34:05 -04:00
int err = 0 , fin = 0 ;
2022-03-30 13:39:30 -04:00
if ( err_type & STRM_ET_QUEUE_ABRT ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_CLICL ;
fin = SF_FINST_Q ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_CONN_ABRT ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_CLICL ;
fin = SF_FINST_C ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_QUEUE_TO ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_SRVTO ;
fin = SF_FINST_Q ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_QUEUE_ERR ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_SRVCL ;
fin = SF_FINST_Q ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_CONN_TO ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_SRVTO ;
fin = SF_FINST_C ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_CONN_ERR ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_SRVCL ;
fin = SF_FINST_C ;
2009-03-15 17:34:05 -04:00
}
2022-03-30 13:39:30 -04:00
else if ( err_type & STRM_ET_CONN_RES ) {
2015-04-02 19:14:29 -04:00
err = SF_ERR_RESOURCE ;
fin = SF_FINST_C ;
2012-05-14 06:11:47 -04:00
}
2022-03-30 13:39:30 -04:00
else /* STRM_ET_CONN_OTHER and others */ {
2015-04-02 19:14:29 -04:00
err = SF_ERR_INTERNAL ;
fin = SF_FINST_C ;
2009-03-15 17:34:05 -04:00
}
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_ERR_MASK ) )
2009-03-15 17:34:05 -04:00
s - > flags | = err ;
2015-04-02 19:14:29 -04:00
if ( ! ( s - > flags & SF_FINST_MASK ) )
2009-03-15 17:34:05 -04:00
s - > flags | = fin ;
}
[BUG] fix the dequeuing logic to ensure that all requests get served
The dequeuing logic was completely wrong. First, a task was assigned
to all servers to process the queue, but this task was never scheduled
and was only woken up on session free. Second, there was no reservation
of server entries when a task was assigned a server. This means that
as long as the task was not connected to the server, its presence was
not accounted for. This was causing trouble when detecting whether or
not a server had reached maxconn. Third, during a redispatch, a session
could lose its place at the server's and get blocked because another
session at the same moment would have stolen the entry. Fourth, the
redispatch option did not work when maxqueue was reached for a server,
and it was not possible to do so without indefinitely hanging a session.
The root cause of all those problems was the lack of pre-reservation of
connections at the server's, and the lack of tracking of servers during
a redispatch. Everything relied on combinations of flags which could
appear similarly in quite distinct situations.
This patch is a major rework but there was no other solution, as the
internal logic was deeply flawed. The resulting code is cleaner, more
understandable, uses less magics and is overall more robust.
As an added bonus, "option redispatch" now works when maxqueue has
been reached on a server.
2008-06-20 09:04:11 -04:00
2024-09-26 12:30:36 -04:00
/* shutdown the stream from itself. It's also possible for another one from the
* same thread but then an explicit wakeup will be needed since this function
* does not perform it . < why > is a set of SF_ERR_ * flags to pass as the cause
* for shutting down .
*/
void stream_shutdown_self ( struct stream * stream , int why )
2011-09-07 17:01:56 -04:00
{
2023-04-13 10:16:15 -04:00
if ( stream - > scb - > flags & ( SC_FL_SHUT_DONE | SC_FL_SHUT_WANTED ) )
2011-09-07 17:01:56 -04:00
return ;
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream - > task - > nice = 1024 ;
2015-04-02 19:14:29 -04:00
if ( ! ( stream - > flags & SF_ERR_MASK ) )
REORG/MAJOR: session: rename the "session" entity to "stream"
With HTTP/2, we'll have to support multiplexed streams. A stream is in
fact the largest part of what we currently call a session, it has buffers,
logs, etc.
In order to catch any error, this commit removes any reference to the
struct session and tries to rename most "session" occurrences in function
names to "stream" and "sess" to "strm" when that's related to a session.
The files stream.{c,h} were added and session.{c,h} removed.
The session will be reintroduced later and a few parts of the stream
will progressively be moved overthere. It will more or less contain
only what we need in an embryonic session.
Sample fetch functions and converters will have to change a bit so
that they'll use an L5 (session) instead of what's currently called
"L4" which is in fact L6 for now.
Once all changes are completed, we should see approximately this :
L7 - http_txn
L6 - stream
L5 - session
L4 - connection | applet
There will be at most one http_txn per stream, and a same session will
possibly be referenced by multiple streams. A connection will point to
a session and to a stream. The session will hold all the information
we need to keep even when we don't yet have a stream.
Some more cleanup is needed because some code was already far from
being clean. The server queue management still refers to sessions at
many places while comments talk about connections. This will have to
be cleaned up once we have a server-side connection pool manager.
Stream flags "SN_*" still need to be renamed, it doesn't seem like
any of them will need to move to the session.
2015-04-02 18:22:06 -04:00
stream - > flags | = why ;
2025-03-17 09:49:45 -04:00
if ( objt_server ( stream - > target ) ) {
if ( stream - > flags & SF_CURR_SESS ) {
stream - > flags & = ~ SF_CURR_SESS ;
_HA_ATOMIC_DEC ( & __objt_server ( stream - > target ) - > cur_sess ) ;
}
sess_change_server ( stream , NULL ) ;
if ( may_dequeue_tasks ( objt_server ( stream - > target ) , stream - > be ) )
process_srv_queue ( objt_server ( stream - > target ) ) ;
}
/* shutw is enough to stop a connecting socket */
stream - > scb - > flags | = SC_FL_ERROR | SC_FL_NOLINGER ;
sc_shutdown ( stream - > scb ) ;
stream - > scb - > state = SC_ST_CLO ;
if ( stream - > srv_error )
stream - > srv_error ( stream , stream - > scb ) ;
2011-09-07 17:01:56 -04:00
}
2010-06-14 15:04:55 -04:00
2019-05-22 03:33:03 -04:00
/* dumps an error message for type <type> at ptr <ptr> related to stream <s>,
2019-05-22 02:57:01 -04:00
* having reached loop rate < rate > , then aborts hoping to retrieve a core .
2019-05-22 03:33:03 -04:00
*/
void stream_dump_and_crash ( enum obj_type * obj , int rate )
{
MEDIUM: stream: now provide full stream dumps in case of loops
When a stream is caught looping, we produce some output to help figure
its internal state explaining why it's looping. The problem is that this
debug output is quite old and the info it provides are quite insufficient
to debug a modern process, and since such bugs happen only once or twice
a year the situation doesn't improve.
On the other hand the output of "show sess all" is extremely detailed
and kept up to date with code evolutions since it's a heavily used
debugging tool.
This commit replaces the call to the totally outdated stream_dump() with
a call to strm_dump_to_buffer(), and removes the filters dump since they
are already emitted there, and it now produces much more exploitable
output:
[ALERT] (5936) : A bogus STREAM [0x7fa8dc02f660] is spinning at 5653514 calls per second and refuses to die, aborting now! Please report this error to developers:
0x7fa8dc02f660: [28/Sep/2023:09:53:08.811818] id=2 proto=tcpv4 source=127.0.0.1:58306
flags=0xc4a, conn_retries=0, conn_exp=<NEVER> conn_et=0x000 srv_conn=0x133f220, pend_pos=(nil) waiting=0 epoch=0x1
frontend=public (id=2 mode=http), listener=? (id=1) addr=127.0.0.1:4080
backend=public (id=2 mode=http) addr=127.0.0.1:61932
server=s1 (id=1) addr=127.0.0.1:7443
task=0x7fa8dc02fa40 (state=0x01 nice=0 calls=5749559 rate=5653514 exp=3s tid=1(1/1) age=1s)
txn=0x7fa8dc02fbf0 flags=0x3000 meth=1 status=-1 req.st=MSG_DONE rsp.st=MSG_RPBEFORE req.f=0x4c rsp.f=0x00
scf=0x7fa8dc02f5f0 flags=0x00000482 state=EST endp=CONN,0x7fa8dc02b4b0,0x05004001 sub=1 rex=58s wex=<NEVER>
h1s=0x7fa8dc02b4b0 h1s.flg=0x100010 .sd.flg=0x5004001 .req.state=MSG_DONE .res.state=MSG_RPBEFORE
.meth=GET status=0 .sd.flg=0x05004001 .sc.flg=0x00000482 .sc.app=0x7fa8dc02f660
.subs=0x7fa8dc02f608(ev=1 tl=0x7fa8dc02fae0 tl.calls=0 tl.ctx=0x7fa8dc02f5f0 tl.fct=sc_conn_io_cb)
h1c=0x7fa8dc0272d0 h1c.flg=0x0 .sub=0 .ibuf=0@(nil)+0/0 .obuf=0@(nil)+0/0 .task=0x7fa8dc0273f0 .exp=<NEVER>
co0=0x7fa8dc027040 ctrl=tcpv4 xprt=RAW mux=H1 data=STRM target=LISTENER:0x12840c0
flags=0x00000300 fd=32 fd.state=20 updt=0 fd.tmask=0x2
scb=0x7fa8dc02fb30 flags=0x00001411 state=EST endp=CONN,0x7fa8dc0300c0,0x05000001 sub=1 rex=58s wex=<NEVER>
h1s=0x7fa8dc0300c0 h1s.flg=0x4010 .sd.flg=0x5000001 .req.state=MSG_DONE .res.state=MSG_RPBEFORE
.meth=GET status=0 .sd.flg=0x05000001 .sc.flg=0x00001411 .sc.app=0x7fa8dc02f660
.subs=0x7fa8dc02fb48(ev=1 tl=0x7fa8dc02feb0 tl.calls=2 tl.ctx=0x7fa8dc02fb30 tl.fct=sc_conn_io_cb)
h1c=0x7fa8dc02ff00 h1c.flg=0x80000000 .sub=1 .ibuf=0@(nil)+0/0 .obuf=0@(nil)+0/0 .task=0x7fa8dc030020 .exp=<NEVER>
co1=0x7fa8dc02fcd0 ctrl=tcpv4 xprt=RAW mux=H1 data=STRM target=SERVER:0x133f220
flags=0x10000300 fd=33 fd.state=10421 updt=0 fd.tmask=0x2
req=0x7fa8dc02f680 (f=0x1840000 an=0x8000 pipe=0 tofwd=0 total=79)
an_exp=<NEVER> buf=0x7fa8dc02f688 data=(nil) o=0 p=0 i=0 size=0
htx=0xc18f60 flags=0x0 size=0 data=0 used=0 wrap=NO extra=0
res=0x7fa8dc02f6d0 (f=0x80000000 an=0x1400000 pipe=0 tofwd=0 total=0)
an_exp=<NEVER> buf=0x7fa8dc02f6d8 data=(nil) o=0 p=0 i=0 size=0
htx=0xc18f60 flags=0x0 size=0 data=0 used=0 wrap=NO extra=0
call trace(10):
| 0x59f2b7 [0f 0b 0f 1f 80 00 00 00]: stream_dump_and_crash+0x1f7/0x2bf
| 0x5a0d71 [e9 af e6 ff ff ba 40 00]: process_stream+0x19f1/0x3a56
| 0x68d7bb [49 89 c7 4d 85 ff 74 77]: run_tasks_from_lists+0x3ab/0x924
| 0x68e0b4 [29 44 24 14 8b 4c 24 14]: process_runnable_tasks+0x374/0x6d6
| 0x656f67 [83 3d f2 75 84 00 01 0f]: run_poll_loop+0x127/0x5a8
| 0x6575d7 [48 8b 1d 42 50 5c 00 48]: main+0x1b22f7
| 0x7fa8e0f35e45 [64 48 89 04 25 30 06 00]: libpthread:+0x7e45
| 0x7fa8e0e5a4af [48 89 c7 b8 3c 00 00 00]: libc:clone+0x3f/0x5a
Note that the output is subject to the global anon key so that IPs and
object names can be anonymized if required. It could make sense to
backport this and the few related previous patches next time such an
issue is reported.
2023-09-29 02:17:49 -04:00
struct stream * s ;
2019-05-22 03:33:03 -04:00
char * msg = NULL ;
const void * ptr ;
ptr = s = objt_stream ( obj ) ;
if ( ! s ) {
const struct appctx * appctx = objt_appctx ( obj ) ;
if ( ! appctx )
return ;
ptr = appctx ;
2022-05-11 08:09:57 -04:00
s = appctx_strm ( appctx ) ;
2019-05-22 03:33:03 -04:00
if ( ! s )
return ;
}
chunk_reset ( & trash ) ;
MEDIUM: stream: now provide full stream dumps in case of loops
When a stream is caught looping, we produce some output to help figure
its internal state explaining why it's looping. The problem is that this
debug output is quite old and the info it provides are quite insufficient
to debug a modern process, and since such bugs happen only once or twice
a year the situation doesn't improve.
On the other hand the output of "show sess all" is extremely detailed
and kept up to date with code evolutions since it's a heavily used
debugging tool.
This commit replaces the call to the totally outdated stream_dump() with
a call to strm_dump_to_buffer(), and removes the filters dump since they
are already emitted there, and it now produces much more exploitable
output:
[ALERT] (5936) : A bogus STREAM [0x7fa8dc02f660] is spinning at 5653514 calls per second and refuses to die, aborting now! Please report this error to developers:
0x7fa8dc02f660: [28/Sep/2023:09:53:08.811818] id=2 proto=tcpv4 source=127.0.0.1:58306
flags=0xc4a, conn_retries=0, conn_exp=<NEVER> conn_et=0x000 srv_conn=0x133f220, pend_pos=(nil) waiting=0 epoch=0x1
frontend=public (id=2 mode=http), listener=? (id=1) addr=127.0.0.1:4080
backend=public (id=2 mode=http) addr=127.0.0.1:61932
server=s1 (id=1) addr=127.0.0.1:7443
task=0x7fa8dc02fa40 (state=0x01 nice=0 calls=5749559 rate=5653514 exp=3s tid=1(1/1) age=1s)
txn=0x7fa8dc02fbf0 flags=0x3000 meth=1 status=-1 req.st=MSG_DONE rsp.st=MSG_RPBEFORE req.f=0x4c rsp.f=0x00
scf=0x7fa8dc02f5f0 flags=0x00000482 state=EST endp=CONN,0x7fa8dc02b4b0,0x05004001 sub=1 rex=58s wex=<NEVER>
h1s=0x7fa8dc02b4b0 h1s.flg=0x100010 .sd.flg=0x5004001 .req.state=MSG_DONE .res.state=MSG_RPBEFORE
.meth=GET status=0 .sd.flg=0x05004001 .sc.flg=0x00000482 .sc.app=0x7fa8dc02f660
.subs=0x7fa8dc02f608(ev=1 tl=0x7fa8dc02fae0 tl.calls=0 tl.ctx=0x7fa8dc02f5f0 tl.fct=sc_conn_io_cb)
h1c=0x7fa8dc0272d0 h1c.flg=0x0 .sub=0 .ibuf=0@(nil)+0/0 .obuf=0@(nil)+0/0 .task=0x7fa8dc0273f0 .exp=<NEVER>
co0=0x7fa8dc027040 ctrl=tcpv4 xprt=RAW mux=H1 data=STRM target=LISTENER:0x12840c0
flags=0x00000300 fd=32 fd.state=20 updt=0 fd.tmask=0x2
scb=0x7fa8dc02fb30 flags=0x00001411 state=EST endp=CONN,0x7fa8dc0300c0,0x05000001 sub=1 rex=58s wex=<NEVER>
h1s=0x7fa8dc0300c0 h1s.flg=0x4010 .sd.flg=0x5000001 .req.state=MSG_DONE .res.state=MSG_RPBEFORE
.meth=GET status=0 .sd.flg=0x05000001 .sc.flg=0x00001411 .sc.app=0x7fa8dc02f660
.subs=0x7fa8dc02fb48(ev=1 tl=0x7fa8dc02feb0 tl.calls=2 tl.ctx=0x7fa8dc02fb30 tl.fct=sc_conn_io_cb)
h1c=0x7fa8dc02ff00 h1c.flg=0x80000000 .sub=1 .ibuf=0@(nil)+0/0 .obuf=0@(nil)+0/0 .task=0x7fa8dc030020 .exp=<NEVER>
co1=0x7fa8dc02fcd0 ctrl=tcpv4 xprt=RAW mux=H1 data=STRM target=SERVER:0x133f220
flags=0x10000300 fd=33 fd.state=10421 updt=0 fd.tmask=0x2
req=0x7fa8dc02f680 (f=0x1840000 an=0x8000 pipe=0 tofwd=0 total=79)
an_exp=<NEVER> buf=0x7fa8dc02f688 data=(nil) o=0 p=0 i=0 size=0
htx=0xc18f60 flags=0x0 size=0 data=0 used=0 wrap=NO extra=0
res=0x7fa8dc02f6d0 (f=0x80000000 an=0x1400000 pipe=0 tofwd=0 total=0)
an_exp=<NEVER> buf=0x7fa8dc02f6d8 data=(nil) o=0 p=0 i=0 size=0
htx=0xc18f60 flags=0x0 size=0 data=0 used=0 wrap=NO extra=0
call trace(10):
| 0x59f2b7 [0f 0b 0f 1f 80 00 00 00]: stream_dump_and_crash+0x1f7/0x2bf
| 0x5a0d71 [e9 af e6 ff ff ba 40 00]: process_stream+0x19f1/0x3a56
| 0x68d7bb [49 89 c7 4d 85 ff 74 77]: run_tasks_from_lists+0x3ab/0x924
| 0x68e0b4 [29 44 24 14 8b 4c 24 14]: process_runnable_tasks+0x374/0x6d6
| 0x656f67 [83 3d f2 75 84 00 01 0f]: run_poll_loop+0x127/0x5a8
| 0x6575d7 [48 8b 1d 42 50 5c 00 48]: main+0x1b22f7
| 0x7fa8e0f35e45 [64 48 89 04 25 30 06 00]: libpthread:+0x7e45
| 0x7fa8e0e5a4af [48 89 c7 b8 3c 00 00 00]: libc:clone+0x3f/0x5a
Note that the output is subject to the global anon key so that IPs and
object names can be anonymized if required. It could make sense to
backport this and the few related previous patches next time such an
issue is reported.
2023-09-29 02:17:49 -04:00
chunk_printf ( & trash , " " ) ;
strm_dump_to_buffer ( & trash , s , " " , HA_ATOMIC_LOAD ( & global . anon_key ) ) ;
MINOR: stream: report the list of active filters on stream crashes
Now we very rarely catch spinning streams, and whenever we catch one it
seems a filter is involved, but we currently report no info about them.
Let's print the list of enabled filters on the stream with such a crash
to help with the reports. A typical output will now look like this:
[ALERT] 121/165908 (1110) : A bogus STREAM [0x7fcaf4016a60] is spinning at 2 calls per second and refuses to die, aborting now! Please report this error to developers [strm=0x7fcaf4016a60 src=127.0.0.1 fe=l1 be=l1 dst=<CACHE> rqf=6dc42000 rqa=48000 rpf=a0040223 rpa=24000000 sif=EST,10008 sib=DIS,80110 af=(nil),0 csf=0x7fcaf4023c00,10c000 ab=0x7fcaf40235f0,4 csb=(nil),0 cof=0x7fcaf4016610,1300:H1(0x7fcaf4016840)/RAW((nil))/tcpv4(29) cob=(nil),0:NONE((nil))/NONE((nil))/NONE(0) filters={0x7fcaf4016fb0="cache store filter", 0x7fcaf4017080="compression filter"}]
This may be backported to 2.0.
2020-05-01 10:57:02 -04:00
2022-09-02 03:13:12 -04:00
if ( ptr ! = s ) { // that's an appctx
const struct appctx * appctx = ptr ;
chunk_appendf ( & trash , " applet=%p( " , appctx - > applet ) ;
resolve_sym_name ( & trash , NULL , appctx - > applet ) ;
chunk_appendf ( & trash , " ) " ) ;
chunk_appendf ( & trash , " handler=%p( " , appctx - > applet - > fct ) ;
resolve_sym_name ( & trash , NULL , appctx - > applet - > fct ) ;
chunk_appendf ( & trash , " ) " ) ;
}
2019-05-22 02:57:01 -04:00
memprintf ( & msg ,
" A bogus %s [%p] is spinning at %d calls per second and refuses to die, "
MINOR: stream: make stream_dump() always multi-line
There used to be two working modes for this function, a single-line one
and a multi-line one, the difference being made on the "eol" argument
which could contain either a space or an LF (and with the prefix being
adjusted accordingly). Let's get rid of the single-line mode as it's
what limits the output contents because it's difficult to produce
exploitable structured data this way. It was only used in the rare case
of spinning streams and applets and these are the ones lacking info. Now
a spinning stream produces:
[ALERT] (3511) : A bogus STREAM [0x227e7b0] is spinning at 5581202 calls per second and refuses to die, aborting now! Please report this error to developers:
strm=0x227e7b0,c4a src=127.0.0.1 fe=public be=public dst=s1
txn=0x2041650,3000 txn.req=MSG_DONE,4c txn.rsp=MSG_RPBEFORE,0
rqf=1840000 rqa=8000 rpf=80000000 rpa=1400000
scf=0x24af280,EST,482 scb=0x24af430,EST,1411
af=(nil),0 sab=(nil),0
cof=0x7fdb28026630,300:H1(0x24a6f60)/RAW((nil))/tcpv4(33)
cob=0x23199f0,10000300:H1(0x24af630)/RAW((nil))/tcpv4(32)
filters={}
call trace(11):
(...)
2023-09-28 02:06:22 -04:00
" aborting now! Please report this error to developers: \n "
" %s \n " ,
2019-05-22 03:33:03 -04:00
obj_type_name ( obj ) , ptr , rate , trash . area ) ;
2019-04-25 13:08:48 -04:00
ha_alert ( " %s " , msg ) ;
send_log ( NULL , LOG_EMERG , " %s " , msg ) ;
2021-03-02 13:19:41 -05:00
ABORT_NOW ( ) ;
2019-04-25 13:08:48 -04:00
}
2021-02-24 04:37:01 -05:00
/* initialize the require structures */
static void init_stream ( )
{
int thr ;
for ( thr = 0 ; thr < MAX_THREADS ; thr + + )
2021-09-30 13:02:18 -04:00
LIST_INIT ( & ha_thread_ctx [ thr ] . streams ) ;
2021-02-24 04:37:01 -05:00
}
INITCALL0 ( STG_INIT , init_stream ) ;
2020-02-28 09:13:33 -05:00
/* Generates a unique ID based on the given <format>, stores it in the given <strm> and
2020-03-05 14:19:02 -05:00
* returns the unique ID .
2022-05-17 18:22:15 -04:00
*
2020-03-05 14:19:02 -05:00
* If this function fails to allocate memory IST_NULL is returned .
2020-02-28 09:13:33 -05:00
*
2020-03-05 14:19:02 -05:00
* If an ID is already stored within the stream nothing happens existing unique ID is
* returned .
2020-02-28 09:13:33 -05:00
*/
2024-02-23 09:57:21 -05:00
struct ist stream_generate_unique_id ( struct stream * strm , struct lf_expr * format )
2020-02-28 09:13:33 -05:00
{
2020-03-05 14:19:02 -05:00
if ( isttest ( strm - > unique_id ) ) {
return strm - > unique_id ;
2020-02-28 09:13:33 -05:00
}
else {
char * unique_id ;
2020-03-05 14:19:02 -05:00
int length ;
2020-02-28 09:13:33 -05:00
if ( ( unique_id = pool_alloc ( pool_head_uniqueid ) ) = = NULL )
2020-03-05 14:19:02 -05:00
return IST_NULL ;
2020-02-28 09:13:33 -05:00
2020-03-05 14:19:02 -05:00
length = build_logline ( strm , unique_id , UNIQUEID_LEN , format ) ;
strm - > unique_id = ist2 ( unique_id , length ) ;
2020-02-28 09:13:33 -05:00
2020-03-05 14:19:02 -05:00
return strm - > unique_id ;
2020-02-28 09:13:33 -05:00
}
}
2010-06-18 11:46:06 -04:00
/************************************************************************/
/* All supported ACL keywords must be declared here. */
/************************************************************************/
2024-09-25 09:10:08 -04:00
static enum act_return stream_action_set_retries ( struct act_rule * rule , struct proxy * px ,
struct session * sess , struct stream * s , int flags )
{
struct sample * smp ;
if ( ! rule - > arg . expr_int . expr )
s - > max_retries = rule - > arg . expr_int . value ;
else {
smp = sample_fetch_as_type ( px , sess , s , SMP_OPT_DIR_REQ | SMP_OPT_FINAL , rule - > arg . expr_int . expr , SMP_T_SINT ) ;
if ( ! smp | | smp - > data . u . sint < 0 | | smp - > data . u . sint > 100 )
goto end ;
s - > max_retries = smp - > data . u . sint ;
}
end :
return ACT_RET_CONT ;
}
/* Parse a "set-retries" action. It takes the level value as argument. It
* returns ACT_RET_PRS_OK on success , ACT_RET_PRS_ERR on error .
*/
static enum act_parse_ret stream_parse_set_retries ( const char * * args , int * cur_arg , struct proxy * px ,
struct act_rule * rule , char * * err )
{
struct sample_expr * expr ;
char * endp ;
unsigned int where ;
if ( ! * args [ * cur_arg ] ) {
bad_retries :
memprintf ( err , " expects exactly 1 argument (an expression or an integer between 1 and 100) " ) ;
return ACT_RET_PRS_ERR ;
}
if ( ! ( px - > cap & PR_CAP_BE ) ) {
memprintf ( err , " '%s' only available in backend or listen section " , args [ 0 ] ) ;
return ACT_RET_PRS_ERR ;
}
if ( px - > cap & PR_CAP_DEF ) {
memprintf ( err , " '%s' is not allowed in 'defaults' sections " , args [ 0 ] ) ;
return ACT_RET_PRS_ERR ;
}
/* value may be either an unsigned integer or an expression */
rule - > arg . expr_int . expr = NULL ;
rule - > arg . expr_int . value = strtol ( args [ * cur_arg ] , & endp , 0 ) ;
if ( * endp = = ' \0 ' ) {
if ( rule - > arg . expr_int . value < 0 | | rule - > arg . expr_int . value > 100 ) {
memprintf ( err , " expects an expression or an integer between 1 and 100 " ) ;
return ACT_RET_PRS_ERR ;
}
/* valid unsigned integer */
( * cur_arg ) + + ;
}
else { /* invalid unsigned integer, fallback to expr */
expr = sample_parse_expr ( ( char * * ) args , cur_arg , px - > conf . args . file , px - > conf . args . line , err , & px - > conf . args , NULL ) ;
if ( ! expr )
return ACT_RET_PRS_ERR ;
where = 0 ;
if ( px - > cap & PR_CAP_FE )
where | = SMP_VAL_FE_HRQ_HDR ;
if ( px - > cap & PR_CAP_BE )
where | = SMP_VAL_BE_HRQ_HDR ;
if ( ! ( expr - > fetch - > val & where ) ) {
memprintf ( err ,
" fetch method '%s' extracts information from '%s', none of which is available here " ,
args [ * cur_arg - 1 ] , sample_src_names ( expr - > fetch - > use ) ) ;
free ( expr ) ;
return ACT_RET_PRS_ERR ;
}
rule - > arg . expr_int . expr = expr ;
}
/* Register processing function. */
rule - > action = ACT_CUSTOM ;
rule - > action_ptr = stream_action_set_retries ;
rule - > release_ptr = release_expr_int_action ;
return ACT_RET_PRS_OK ;
}
2021-06-25 08:35:29 -04:00
static enum act_return stream_action_set_log_level ( struct act_rule * rule , struct proxy * px ,
struct session * sess , struct stream * s , int flags )
{
s - > logs . level = ( uintptr_t ) rule - > arg . act . p [ 0 ] ;
return ACT_RET_CONT ;
}
/* Parse a "set-log-level" action. It takes the level value as argument. It
* returns ACT_RET_PRS_OK on success , ACT_RET_PRS_ERR on error .
*/
static enum act_parse_ret stream_parse_set_log_level ( const char * * args , int * cur_arg , struct proxy * px ,
struct act_rule * rule , char * * err )
{
int level ;
if ( ! * args [ * cur_arg ] ) {
bad_log_level :
memprintf ( err , " expects exactly 1 argument (log level name or 'silent') " ) ;
return ACT_RET_PRS_ERR ;
}
if ( strcmp ( args [ * cur_arg ] , " silent " ) = = 0 )
level = - 1 ;
else if ( ( level = get_log_level ( args [ * cur_arg ] ) + 1 ) = = 0 )
goto bad_log_level ;
( * cur_arg ) + + ;
/* Register processing function. */
rule - > action_ptr = stream_action_set_log_level ;
rule - > action = ACT_CUSTOM ;
rule - > arg . act . p [ 0 ] = ( void * ) ( uintptr_t ) level ;
return ACT_RET_PRS_OK ;
}
2021-06-25 08:46:02 -04:00
static enum act_return stream_action_set_nice ( struct act_rule * rule , struct proxy * px ,
struct session * sess , struct stream * s , int flags )
{
s - > task - > nice = ( uintptr_t ) rule - > arg . act . p [ 0 ] ;
return ACT_RET_CONT ;
}
/* Parse a "set-nice" action. It takes the nice value as argument. It returns
* ACT_RET_PRS_OK on success , ACT_RET_PRS_ERR on error .
*/
static enum act_parse_ret stream_parse_set_nice ( const char * * args , int * cur_arg , struct proxy * px ,
struct act_rule * rule , char * * err )
{
int nice ;
if ( ! * args [ * cur_arg ] ) {
bad_log_level :
memprintf ( err , " expects exactly 1 argument (integer value) " ) ;
return ACT_RET_PRS_ERR ;
}
nice = atoi ( args [ * cur_arg ] ) ;
if ( nice < - 1024 )
nice = - 1024 ;
else if ( nice > 1024 )
nice = 1024 ;
( * cur_arg ) + + ;
/* Register processing function. */
rule - > action_ptr = stream_action_set_nice ;
rule - > action = ACT_CUSTOM ;
rule - > arg . act . p [ 0 ] = ( void * ) ( uintptr_t ) nice ;
return ACT_RET_PRS_OK ;
}
2021-06-25 08:35:29 -04:00
2021-03-15 07:03:44 -04:00
static enum act_return tcp_action_switch_stream_mode ( struct act_rule * rule , struct proxy * px ,
struct session * sess , struct stream * s , int flags )
{
enum pr_mode mode = ( uintptr_t ) rule - > arg . act . p [ 0 ] ;
const struct mux_proto_list * mux_proto = rule - > arg . act . p [ 1 ] ;
if ( ! IS_HTX_STRM ( s ) & & mode = = PR_MODE_HTTP ) {
if ( ! stream_set_http_mode ( s , mux_proto ) ) {
2023-04-13 09:22:29 -04:00
stream_abort ( s ) ;
2021-03-15 07:03:44 -04:00
return ACT_RET_ABRT ;
}
}
return ACT_RET_STOP ;
}
static int check_tcp_switch_stream_mode ( struct act_rule * rule , struct proxy * px , char * * err )
{
const struct mux_proto_list * mux_ent ;
const struct mux_proto_list * mux_proto = rule - > arg . act . p [ 1 ] ;
enum pr_mode pr_mode = ( uintptr_t ) rule - > arg . act . p [ 0 ] ;
2023-10-19 10:06:03 -04:00
enum proto_proxy_mode mode = conn_pr_mode_to_proto_mode ( pr_mode ) ;
2021-03-15 07:03:44 -04:00
2021-03-15 10:10:38 -04:00
if ( pr_mode = = PR_MODE_HTTP )
px - > options | = PR_O_HTTP_UPG ;
2021-03-15 07:03:44 -04:00
if ( mux_proto ) {
mux_ent = conn_get_best_mux_entry ( mux_proto - > token , PROTO_SIDE_FE , mode ) ;
if ( ! mux_ent | | ! isteq ( mux_ent - > token , mux_proto - > token ) ) {
memprintf ( err , " MUX protocol '%.*s' is not compatible with the selected mode " ,
( int ) mux_proto - > token . len , mux_proto - > token . ptr ) ;
return 0 ;
}
}
else {
mux_ent = conn_get_best_mux_entry ( IST_NULL , PROTO_SIDE_FE , mode ) ;
if ( ! mux_ent ) {
memprintf ( err , " Unable to find compatible MUX protocol with the selected mode " ) ;
return 0 ;
}
}
/* Update the mux */
rule - > arg . act . p [ 1 ] = ( void * ) mux_ent ;
return 1 ;
}
static enum act_parse_ret stream_parse_switch_mode ( const char * * args , int * cur_arg ,
struct proxy * px , struct act_rule * rule ,
char * * err )
{
const struct mux_proto_list * mux_proto = NULL ;
struct ist proto ;
enum pr_mode mode ;
/* must have at least the mode */
if ( * ( args [ * cur_arg ] ) = = 0 ) {
memprintf ( err , " '%s %s' expects a mode as argument. " , args [ 0 ] , args [ * cur_arg - 1 ] ) ;
return ACT_RET_PRS_ERR ;
}
if ( ! ( px - > cap & PR_CAP_FE ) ) {
memprintf ( err , " '%s %s' not allowed because %s '%s' has no frontend capability " ,
args [ 0 ] , args [ * cur_arg - 1 ] , proxy_type_str ( px ) , px - > id ) ;
return ACT_RET_PRS_ERR ;
}
/* Check if the mode. For now "tcp" is disabled because downgrade is not
* supported and PT is the only TCP mux .
*/
if ( strcmp ( args [ * cur_arg ] , " http " ) = = 0 )
mode = PR_MODE_HTTP ;
else {
memprintf ( err , " '%s %s' expects a valid mode (got '%s'). " , args [ 0 ] , args [ * cur_arg - 1 ] , args [ * cur_arg ] ) ;
return ACT_RET_PRS_ERR ;
}
/* check the proto, if specified */
if ( * ( args [ * cur_arg + 1 ] ) & & strcmp ( args [ * cur_arg + 1 ] , " proto " ) = = 0 ) {
if ( * ( args [ * cur_arg + 2 ] ) = = 0 ) {
memprintf ( err , " '%s %s': '%s' expects a protocol as argument. " ,
args [ 0 ] , args [ * cur_arg - 1 ] , args [ * cur_arg + 1 ] ) ;
return ACT_RET_PRS_ERR ;
}
2021-09-15 07:58:44 -04:00
proto = ist ( args [ * cur_arg + 2 ] ) ;
2021-03-15 07:03:44 -04:00
mux_proto = get_mux_proto ( proto ) ;
if ( ! mux_proto ) {
memprintf ( err , " '%s %s': '%s' expects a valid MUX protocol, if specified (got '%s') " ,
args [ 0 ] , args [ * cur_arg - 1 ] , args [ * cur_arg + 1 ] , args [ * cur_arg + 2 ] ) ;
return ACT_RET_PRS_ERR ;
}
* cur_arg + = 2 ;
}
( * cur_arg ) + + ;
/* Register processing function. */
rule - > action_ptr = tcp_action_switch_stream_mode ;
rule - > check_ptr = check_tcp_switch_stream_mode ;
rule - > action = ACT_CUSTOM ;
rule - > arg . act . p [ 0 ] = ( void * ) ( uintptr_t ) mode ;
rule - > arg . act . p [ 1 ] = ( void * ) mux_proto ;
return ACT_RET_PRS_OK ;
}
2010-06-18 11:46:06 -04:00
2015-09-27 13:29:33 -04:00
/* 0=OK, <0=Alert, >0=Warning */
static enum act_parse_ret stream_parse_use_service ( const char * * args , int * cur_arg ,
struct proxy * px , struct act_rule * rule ,
char * * err )
{
struct action_kw * kw ;
/* Check if the service name exists. */
if ( * ( args [ * cur_arg ] ) = = 0 ) {
memprintf ( err , " '%s' expects a service name. " , args [ 0 ] ) ;
2015-11-26 13:48:04 -05:00
return ACT_RET_PRS_ERR ;
2015-09-27 13:29:33 -04:00
}
/* lookup for keyword corresponding to a service. */
kw = action_lookup ( & service_keywords , args [ * cur_arg ] ) ;
if ( ! kw ) {
memprintf ( err , " '%s' unknown service name. " , args [ 1 ] ) ;
return ACT_RET_PRS_ERR ;
}
( * cur_arg ) + + ;
/* executes specific rule parser. */
rule - > kw = kw ;
if ( kw - > parse ( ( const char * * ) args , cur_arg , px , rule , err ) = = ACT_RET_PRS_ERR )
return ACT_RET_PRS_ERR ;
/* Register processing function. */
rule - > action_ptr = process_use_service ;
rule - > action = ACT_CUSTOM ;
return ACT_RET_PRS_OK ;
}
void service_keywords_register ( struct action_kw_list * kw_list )
{
2021-04-21 01:32:39 -04:00
LIST_APPEND ( & service_keywords , & kw_list - > list ) ;
2015-09-27 13:29:33 -04:00
}
2020-11-28 13:32:14 -05:00
struct action_kw * service_find ( const char * kw )
{
return action_lookup ( & service_keywords , kw ) ;
}
2022-03-29 09:10:44 -04:00
/* Lists the known services on <out>. If <out> is null, emit them on stdout one
* per line .
*/
2019-03-19 03:08:10 -04:00
void list_services ( FILE * out )
{
2022-03-30 06:12:44 -04:00
const struct action_kw * akwp , * akwn ;
2019-03-19 03:08:10 -04:00
struct action_kw_list * kw_list ;
int found = 0 ;
int i ;
2022-03-29 09:10:44 -04:00
if ( out )
fprintf ( out , " Available services : " ) ;
2022-03-30 06:12:44 -04:00
for ( akwn = akwp = NULL ; ; akwp = akwn ) {
list_for_each_entry ( kw_list , & service_keywords , list ) {
for ( i = 0 ; kw_list - > kw [ i ] . kw ! = NULL ; i + + ) {
if ( strordered ( akwp ? akwp - > kw : NULL ,
kw_list - > kw [ i ] . kw ,
akwn ! = akwp ? akwn - > kw : NULL ) )
akwn = & kw_list - > kw [ i ] ;
found = 1 ;
}
2019-03-19 03:08:10 -04:00
}
2022-03-30 06:12:44 -04:00
if ( akwn = = akwp )
break ;
if ( out )
fprintf ( out , " %s " , akwn - > kw ) ;
else
printf ( " %s \n " , akwn - > kw ) ;
2019-03-19 03:08:10 -04:00
}
2022-03-29 09:10:44 -04:00
if ( ! found & & out )
2019-03-19 03:08:10 -04:00
fprintf ( out , " none \n " ) ;
}
2016-11-21 02:51:11 -05:00
2022-05-03 04:49:00 -04:00
/* appctx context used by the "show sess" command */
2023-11-17 13:29:02 -05:00
/* flags used for show_sess_ctx.flags */
2024-11-18 09:17:13 -05:00
# define CLI_SHOWSESS_F_SUSP 0x00000001 /* show only suspicious streams */
# define CLI_SHOWSESS_F_DUMP_URI 0x00000002 /* Dump TXN's uri if available in dump */
2025-03-07 04:34:19 -05:00
# define CLI_SHOWSESS_F_SERVER 0x00000004 /* show only streams attached to this server */
# define CLI_SHOWSESS_F_BACKEND 0x00000008 /* show only streams attached to this backend */
# define CLI_SHOWSESS_F_FRONTEND 0x00000010 /* show only streams attached to this frontend */
2022-05-03 04:49:00 -04:00
struct show_sess_ctx {
struct bref bref ; /* back-reference from the session being dumped */
void * target ; /* session we want to dump, or NULL for all */
2025-03-07 04:34:19 -05:00
void * filter ; /* element to filter on (e.g. server if CLI_SHOWSESS_F_SERVER) */
2022-05-03 04:49:00 -04:00
unsigned int thr ; /* the thread number being explored (0..MAX_THREADS-1) */
unsigned int uid ; /* if non-null, the uniq_id of the session being dumped */
2023-11-17 12:32:59 -05:00
unsigned int min_age ; /* minimum age of streams to dump */
2023-11-17 13:29:02 -05:00
unsigned int flags ; /* CLI_SHOWSESS_* */
2022-05-03 04:49:00 -04:00
int section ; /* section of the session being dumped */
int pos ; /* last position of the current session's buffer */
} ;
2023-09-27 02:23:49 -04:00
/* This function appends a complete dump of a stream state onto the buffer,
* possibly anonymizing using the specified anon_key . The caller is responsible
2024-11-18 09:17:13 -05:00
* for ensuring that enough room remains in the buffer to dump a complete stream
* at once . Each new output line will be prefixed with < pfx > if non - null , which
* is used to preserve indenting . The context < ctx > , if non - null , will be used
* to customize the dump .
2016-11-21 02:51:11 -05:00
*/
2024-11-18 09:17:13 -05:00
static void __strm_dump_to_buffer ( struct buffer * buf , const struct show_sess_ctx * ctx ,
const struct stream * strm , const char * pfx , uint32_t anon_key )
2016-11-21 02:51:11 -05:00
{
2022-05-17 13:40:40 -04:00
struct stconn * scf , * scb ;
2016-11-21 02:51:11 -05:00
struct tm tm ;
extern const char * monthname [ 12 ] ;
char pn [ INET6_ADDRSTRLEN ] ;
struct connection * conn ;
struct appctx * tmpctx ;
2025-03-06 12:56:13 -05:00
uint64_t request_ts ;
2016-11-21 02:51:11 -05:00
2023-09-28 02:22:51 -04:00
pfx = pfx ? pfx : " " ;
2025-02-24 05:43:15 -05:00
if ( th_ctx - > flags & TH_FL_IN_ANY_HANDLER ) {
/* avoid calling localtime_r() to avoid a risk of deadlock if we
* interrupted libc for example , cf
* https : //github.com/haproxy/haproxy/issues/2861#issuecomment-2677761037
*/
chunk_appendf ( buf , " %p: [%lu.%06lu] " , strm ,
( ulong ) strm - > logs . accept_date . tv_sec ,
( ulong ) strm - > logs . accept_date . tv_usec ) ;
} else {
get_localtime ( strm - > logs . accept_date . tv_sec , & tm ) ;
chunk_appendf ( buf ,
" %p: [%02d/%s/%04d:%02d:%02d:%02d.%06lu] " ,
strm ,
tm . tm_mday , monthname [ tm . tm_mon ] , tm . tm_year + 1900 ,
tm . tm_hour , tm . tm_min , tm . tm_sec , ( ulong ) strm - > logs . accept_date . tv_usec ) ;
}
chunk_appendf ( buf , " id=%u proto=%s " ,
strm - > uniq_id , strm_li ( strm ) ? strm_li ( strm ) - > rx . proto - > name : " ? " ) ;
2023-09-27 02:17:17 -04:00
conn = objt_conn ( strm_orig ( strm ) ) ;
2025-02-21 10:45:18 -05:00
/* be careful not to allocate RAM from a signal handler! */
2025-02-24 07:37:52 -05:00
if ( conn & & ! conn - > src & & ! ( th_ctx - > flags & TH_FL_IN_ANY_HANDLER ) )
2025-02-21 10:45:18 -05:00
conn_get_src ( conn ) ;
switch ( conn & & conn - > src ? addr_to_str ( conn - > src , pn , sizeof ( pn ) ) : AF_UNSPEC ) {
2023-09-27 02:17:17 -04:00
case AF_INET :
case AF_INET6 :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " source=%s:%d \n " ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , pn ) , get_host_port ( conn - > src ) ) ;
break ;
case AF_UNIX :
MEDIUM: protocol: make abns a custom unix socket address family
This is a pre-requisite to adding the abnsz socket address family:
in this patch we make use of protocol API rework started by 732913f
("MINOR: protocol: properly assign the sock_domain and sock_family") in
order to implement a dedicated address family for ABNS sockets (based on
UNIX parent family).
Thanks to this, it will become trivial to implement a new ABNSZ (for abns
zero) family which is essentially the same as ABNS but with a slight
difference when it comes to path handling (ABNS uses the whole sun_path
length, while ABNSZ's path is zero terminated and evaluation stops at 0)
It was verified that this patch doesn't break reg-tests and behaves
properly (tests performed on the CLI with show sess and show fd).
Anywhere relevant, AF_CUST_ABNS is handled alongside AF_UNIX. If no
distinction needs to be made, real_family() is used to fetch the proper
real family type to handle it properly.
Both stream and dgram were converted, so no functional change should be
expected for this "internal" rework, except that proto will be displayed
as "abns_{stream,dgram}" instead of "unix_{stream,dgram}".
Before ("show sess" output):
0x64c35528aab0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=21,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
After:
0x619da7ad74c0: proto=abns_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=22,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
2024-08-09 12:48:14 -04:00
case AF_CUST_ABNS :
2024-08-09 15:12:23 -04:00
case AF_CUST_ABNSZ :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " source=unix:%d \n " , strm_li ( strm ) - > luid ) ;
2023-09-27 02:17:17 -04:00
break ;
default :
/* no more information to print right now */
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s flags=0x%x, conn_retries=%d, conn_exp=%s conn_et=0x%03x srv_conn=%p, pend_pos=%p waiting=%d epoch=%#x \n " , pfx ,
2023-09-27 02:17:17 -04:00
strm - > flags , strm - > conn_retries ,
strm - > conn_exp ?
tick_is_expired ( strm - > conn_exp , now_ms ) ? " <PAST> " :
human_time ( TICKS_TO_MS ( strm - > conn_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ,
strm - > conn_err_type , strm - > srv_conn , strm - > pend_pos ,
LIST_INLIST ( & strm - > buffer_wait . list ) , strm - > stream_epoch ) ;
2024-10-22 10:35:04 -04:00
chunk_appendf ( buf , " %s p_stc=%u p_req=%u p_res=%u p_prp=%u \n " , pfx ,
strm - > passes_stconn , strm - > passes_reqana , strm - > passes_resana , strm - > passes_propag ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s frontend=%s (id=%u mode=%s), listener=%s (id=%u) " , pfx ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , strm_fe ( strm ) - > id ) , strm_fe ( strm ) - > uuid , proxy_mode_str ( strm_fe ( strm ) - > mode ) ,
strm_li ( strm ) ? strm_li ( strm ) - > name ? strm_li ( strm ) - > name : " ? " : " ? " ,
strm_li ( strm ) ? strm_li ( strm ) - > luid : 0 ) ;
2025-02-21 10:45:18 -05:00
/* be careful not to allocate RAM from a signal handler! */
2025-02-24 07:37:52 -05:00
if ( conn & & ! conn - > dst & & ! ( th_ctx - > flags & TH_FL_IN_ANY_HANDLER ) )
2025-02-21 10:45:18 -05:00
conn_get_dst ( conn ) ;
switch ( conn & & conn - > dst ? addr_to_str ( conn - > dst , pn , sizeof ( pn ) ) : AF_UNSPEC ) {
2023-09-27 02:17:17 -04:00
case AF_INET :
case AF_INET6 :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=%s:%d \n " ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , pn ) , get_host_port ( conn - > dst ) ) ;
break ;
case AF_UNIX :
MEDIUM: protocol: make abns a custom unix socket address family
This is a pre-requisite to adding the abnsz socket address family:
in this patch we make use of protocol API rework started by 732913f
("MINOR: protocol: properly assign the sock_domain and sock_family") in
order to implement a dedicated address family for ABNS sockets (based on
UNIX parent family).
Thanks to this, it will become trivial to implement a new ABNSZ (for abns
zero) family which is essentially the same as ABNS but with a slight
difference when it comes to path handling (ABNS uses the whole sun_path
length, while ABNSZ's path is zero terminated and evaluation stops at 0)
It was verified that this patch doesn't break reg-tests and behaves
properly (tests performed on the CLI with show sess and show fd).
Anywhere relevant, AF_CUST_ABNS is handled alongside AF_UNIX. If no
distinction needs to be made, real_family() is used to fetch the proper
real family type to handle it properly.
Both stream and dgram were converted, so no functional change should be
expected for this "internal" rework, except that proto will be displayed
as "abns_{stream,dgram}" instead of "unix_{stream,dgram}".
Before ("show sess" output):
0x64c35528aab0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=21,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
After:
0x619da7ad74c0: proto=abns_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=22,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
2024-08-09 12:48:14 -04:00
case AF_CUST_ABNS :
2024-08-09 15:12:23 -04:00
case AF_CUST_ABNSZ :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=unix:%d \n " , strm_li ( strm ) - > luid ) ;
2023-09-27 02:17:17 -04:00
break ;
default :
/* no more information to print right now */
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
2016-11-21 02:51:11 -05:00
}
2023-09-27 02:17:17 -04:00
if ( strm - > be - > cap & PR_CAP_BE )
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s backend=%s (id=%u mode=%s) " , pfx ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , strm - > be - > id ) ,
strm - > be - > uuid , proxy_mode_str ( strm - > be - > mode ) ) ;
else
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s backend=<NONE> (id=-1 mode=-) " , pfx ) ;
2023-09-27 02:17:17 -04:00
conn = sc_conn ( strm - > scb ) ;
2025-02-21 10:45:18 -05:00
/* be careful not to allocate RAM from a signal handler! */
2025-02-24 07:37:52 -05:00
if ( conn & & ! conn - > src & & ! ( th_ctx - > flags & TH_FL_IN_ANY_HANDLER ) )
2025-02-21 10:45:18 -05:00
conn_get_src ( conn ) ;
switch ( conn & & conn - > src ? addr_to_str ( conn - > src , pn , sizeof ( pn ) ) : AF_UNSPEC ) {
2023-09-27 02:17:17 -04:00
case AF_INET :
case AF_INET6 :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=%s:%d \n " ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , pn ) , get_host_port ( conn - > src ) ) ;
break ;
case AF_UNIX :
MEDIUM: protocol: make abns a custom unix socket address family
This is a pre-requisite to adding the abnsz socket address family:
in this patch we make use of protocol API rework started by 732913f
("MINOR: protocol: properly assign the sock_domain and sock_family") in
order to implement a dedicated address family for ABNS sockets (based on
UNIX parent family).
Thanks to this, it will become trivial to implement a new ABNSZ (for abns
zero) family which is essentially the same as ABNS but with a slight
difference when it comes to path handling (ABNS uses the whole sun_path
length, while ABNSZ's path is zero terminated and evaluation stops at 0)
It was verified that this patch doesn't break reg-tests and behaves
properly (tests performed on the CLI with show sess and show fd).
Anywhere relevant, AF_CUST_ABNS is handled alongside AF_UNIX. If no
distinction needs to be made, real_family() is used to fetch the proper
real family type to handle it properly.
Both stream and dgram were converted, so no functional change should be
expected for this "internal" rework, except that proto will be displayed
as "abns_{stream,dgram}" instead of "unix_{stream,dgram}".
Before ("show sess" output):
0x64c35528aab0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=21,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
After:
0x619da7ad74c0: proto=abns_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=22,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
2024-08-09 12:48:14 -04:00
case AF_CUST_ABNS :
2024-08-09 15:12:23 -04:00
case AF_CUST_ABNSZ :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=unix \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
default :
/* no more information to print right now */
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
}
2016-11-21 02:51:11 -05:00
2023-09-27 02:17:17 -04:00
if ( strm - > be - > cap & PR_CAP_BE )
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s server=%s (id=%u) " , pfx ,
2023-09-27 02:17:17 -04:00
objt_server ( strm - > target ) ? HA_ANON_STR ( anon_key , __objt_server ( strm - > target ) - > id ) : " <none> " ,
objt_server ( strm - > target ) ? __objt_server ( strm - > target ) - > puid : 0 ) ;
else
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s server=<NONE> (id=-1) " , pfx ) ;
2023-09-27 02:17:17 -04:00
2025-02-21 10:45:18 -05:00
/* be careful not to allocate RAM from a signal handler! */
2025-02-24 07:37:52 -05:00
if ( conn & & ! conn - > dst & & ! ( th_ctx - > flags & TH_FL_IN_ANY_HANDLER ) )
2025-02-21 10:45:18 -05:00
conn_get_dst ( conn ) ;
switch ( conn & & conn - > dst ? addr_to_str ( conn - > dst , pn , sizeof ( pn ) ) : AF_UNSPEC ) {
2023-09-27 02:17:17 -04:00
case AF_INET :
case AF_INET6 :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=%s:%d \n " ,
2023-09-27 02:17:17 -04:00
HA_ANON_STR ( anon_key , pn ) , get_host_port ( conn - > dst ) ) ;
break ;
case AF_UNIX :
MEDIUM: protocol: make abns a custom unix socket address family
This is a pre-requisite to adding the abnsz socket address family:
in this patch we make use of protocol API rework started by 732913f
("MINOR: protocol: properly assign the sock_domain and sock_family") in
order to implement a dedicated address family for ABNS sockets (based on
UNIX parent family).
Thanks to this, it will become trivial to implement a new ABNSZ (for abns
zero) family which is essentially the same as ABNS but with a slight
difference when it comes to path handling (ABNS uses the whole sun_path
length, while ABNSZ's path is zero terminated and evaluation stops at 0)
It was verified that this patch doesn't break reg-tests and behaves
properly (tests performed on the CLI with show sess and show fd).
Anywhere relevant, AF_CUST_ABNS is handled alongside AF_UNIX. If no
distinction needs to be made, real_family() is used to fetch the proper
real family type to handle it properly.
Both stream and dgram were converted, so no functional change should be
expected for this "internal" rework, except that proto will be displayed
as "abns_{stream,dgram}" instead of "unix_{stream,dgram}".
Before ("show sess" output):
0x64c35528aab0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=21,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
After:
0x619da7ad74c0: proto=abns_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=22,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
2024-08-09 12:48:14 -04:00
case AF_CUST_ABNS :
2024-08-09 15:12:23 -04:00
case AF_CUST_ABNSZ :
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " addr=unix \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
default :
/* no more information to print right now */
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2023-09-27 02:17:17 -04:00
break ;
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s task=%p (state=0x%02x nice=%d calls=%u rate=%u exp=%s tid=%d(%d/%d)%s " , pfx ,
2023-09-27 02:17:17 -04:00
strm - > task ,
strm - > task - > state ,
2025-02-21 12:23:23 -05:00
strm - > task - > nice , strm - > task - > calls ,
read_freq_ctr_period_estimate ( & strm - > call_rate , MS_TO_TICKS ( 1000 ) ) ,
2023-09-27 02:17:17 -04:00
strm - > task - > expire ?
tick_is_expired ( strm - > task - > expire , now_ms ) ? " <PAST> " :
human_time ( TICKS_TO_MS ( strm - > task - > expire - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ,
strm - > task - > tid ,
ha_thread_info [ strm - > task - > tid ] . tgid ,
ha_thread_info [ strm - > task - > tid ] . ltid ,
task_in_rq ( strm - > task ) ? " , running " : " " ) ;
2025-03-06 12:56:13 -05:00
request_ts = strm - > logs . accept_ts ;
request_ts + = ms_to_ns ( strm - > logs . t_idle > = 0 ? strm - > logs . t_idle + strm - > logs . t_handshake : 0 ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2025-03-06 12:56:13 -05:00
" age=%s) \n " ,
human_time ( ns_to_sec ( now_ns ) - ns_to_sec ( request_ts ) , 1 ) ) ;
2023-09-27 02:17:17 -04:00
2024-11-18 09:17:13 -05:00
if ( strm - > txn ) {
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2024-11-18 09:17:13 -05:00
" %s txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s req.f=0x%02x rsp.f=0x%02x " , pfx ,
2023-09-27 02:17:17 -04:00
strm - > txn , strm - > txn - > flags , strm - > txn - > meth , strm - > txn - > status ,
h1_msg_state_str ( strm - > txn - > req . msg_state ) , h1_msg_state_str ( strm - > txn - > rsp . msg_state ) ,
strm - > txn - > req . flags , strm - > txn - > rsp . flags ) ;
2024-11-18 09:17:13 -05:00
if ( ctx & & ( ctx - > flags & CLI_SHOWSESS_F_DUMP_URI ) & & strm - > txn - > uri )
chunk_appendf ( buf , " uri= \" %s \" " , HA_ANON_STR ( anon_key , strm - > txn - > uri ) ) ;
chunk_memcat ( buf , " \n " , 1 ) ;
}
2023-09-27 02:17:17 -04:00
scf = strm - > scf ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " %s scf=%p flags=0x%08x ioto=%s state=%s endp=%s,%p,0x%08x sub=%d " , pfx ,
scf , scf - > flags , human_time ( scf - > ioto , TICKS_TO_MS ( 1000 ) ) , sc_state_str ( scf - > state ) ,
2023-09-27 02:17:17 -04:00
( sc_ep_test ( scf , SE_FL_T_MUX ) ? " CONN " : ( sc_ep_test ( scf , SE_FL_T_APPLET ) ? " APPCTX " : " NONE " ) ) ,
scf - > sedesc - > se , sc_ep_get ( scf ) , scf - > wait_event . events ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " rex=%s " ,
2023-09-27 02:17:17 -04:00
sc_ep_rcv_ex ( scf ) ? human_time ( TICKS_TO_MS ( sc_ep_rcv_ex ( scf ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " wex=%s " ,
2023-09-27 02:17:17 -04:00
sc_ep_snd_ex ( scf ) ? human_time ( TICKS_TO_MS ( sc_ep_snd_ex ( scf ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " rto=%s " ,
tick_isset ( scf - > sedesc - > lra ) ? human_time ( TICKS_TO_MS ( tick_add ( scf - > sedesc - > lra , scf - > ioto ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
chunk_appendf ( buf , " wto=%s \n " ,
tick_isset ( scf - > sedesc - > fsb ) ? human_time ( TICKS_TO_MS ( tick_add ( scf - > sedesc - > fsb , scf - > ioto ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-09-27 02:17:17 -04:00
2023-08-03 09:30:55 -04:00
chunk_appendf ( & trash , " %s iobuf.flags=0x%08x .pipe=%d .buf=%u@%p+%u/%u \n " , pfx ,
scf - > sedesc - > iobuf . flags ,
scf - > sedesc - > iobuf . pipe ? scf - > sedesc - > iobuf . pipe - > data : 0 ,
scf - > sedesc - > iobuf . buf ? ( unsigned int ) b_data ( scf - > sedesc - > iobuf . buf ) : 0 ,
scf - > sedesc - > iobuf . buf ? b_orig ( scf - > sedesc - > iobuf . buf ) : NULL ,
scf - > sedesc - > iobuf . buf ? ( unsigned int ) b_head_ofs ( scf - > sedesc - > iobuf . buf ) : 0 ,
scf - > sedesc - > iobuf . buf ? ( unsigned int ) b_size ( scf - > sedesc - > iobuf . buf ) : 0 ) ;
2023-09-27 02:17:17 -04:00
if ( ( conn = sc_conn ( scf ) ) ! = NULL ) {
if ( conn - > mux & & conn - > mux - > show_sd ) {
2023-09-28 02:22:51 -04:00
char muxpfx [ 100 ] = " " ;
snprintf ( muxpfx , sizeof ( muxpfx ) , " %s " , pfx ) ;
chunk_appendf ( buf , " %s " , pfx ) ;
conn - > mux - > show_sd ( buf , scf - > sedesc , muxpfx ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2016-11-21 02:51:11 -05:00
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s co0=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p \n " , pfx ,
2023-09-27 02:17:17 -04:00
conn ,
conn_get_ctrl_name ( conn ) ,
conn_get_xprt_name ( conn ) ,
conn_get_mux_name ( conn ) ,
sc_get_data_name ( scf ) ,
obj_type_name ( conn - > target ) ,
obj_base_ptr ( conn - > target ) ) ;
2016-11-21 02:51:11 -05:00
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s flags=0x%08x fd=%d fd.state=%02x updt=%d fd.tmask=0x%lx \n " , pfx ,
2023-09-27 02:17:17 -04:00
conn - > flags ,
conn_fd ( conn ) ,
conn_fd ( conn ) > = 0 ? fdtab [ conn - > handle . fd ] . state : 0 ,
conn_fd ( conn ) > = 0 ? ! ! ( fdtab [ conn - > handle . fd ] . update_mask & ti - > ltid_bit ) : 0 ,
conn_fd ( conn ) > = 0 ? fdtab [ conn - > handle . fd ] . thread_mask : 0 ) ;
}
else if ( ( tmpctx = sc_appctx ( scf ) ) ! = NULL ) {
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s app0=%p st0=%d st1=%d applet=%s tid=%d nice=%d calls=%u rate=%u \n " , pfx ,
2023-09-27 02:17:17 -04:00
tmpctx ,
tmpctx - > st0 ,
tmpctx - > st1 ,
tmpctx - > applet - > name ,
tmpctx - > t - > tid ,
2025-02-21 12:23:23 -05:00
tmpctx - > t - > nice , tmpctx - > t - > calls ,
read_freq_ctr_period_estimate ( & tmpctx - > call_rate , MS_TO_TICKS ( 1000 ) ) ) ;
2023-09-27 02:17:17 -04:00
}
scb = strm - > scb ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " %s scb=%p flags=0x%08x ioto=%s state=%s endp=%s,%p,0x%08x sub=%d " , pfx ,
scb , scb - > flags , human_time ( scb - > ioto , TICKS_TO_MS ( 1000 ) ) , sc_state_str ( scb - > state ) ,
2023-09-27 02:17:17 -04:00
( sc_ep_test ( scb , SE_FL_T_MUX ) ? " CONN " : ( sc_ep_test ( scb , SE_FL_T_APPLET ) ? " APPCTX " : " NONE " ) ) ,
scb - > sedesc - > se , sc_ep_get ( scb ) , scb - > wait_event . events ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " rex=%s " ,
2023-09-27 02:17:17 -04:00
sc_ep_rcv_ex ( scb ) ? human_time ( TICKS_TO_MS ( sc_ep_rcv_ex ( scb ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " wex=%s " ,
2023-09-27 02:17:17 -04:00
sc_ep_snd_ex ( scb ) ? human_time ( TICKS_TO_MS ( sc_ep_snd_ex ( scb ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-12-01 05:16:53 -05:00
chunk_appendf ( buf , " rto=%s " ,
tick_isset ( scb - > sedesc - > lra ) ? human_time ( TICKS_TO_MS ( tick_add ( scb - > sedesc - > lra , scb - > ioto ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
chunk_appendf ( buf , " wto=%s \n " ,
tick_isset ( scb - > sedesc - > fsb ) ? human_time ( TICKS_TO_MS ( tick_add ( scb - > sedesc - > fsb , scb - > ioto ) - now_ms ) , TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ) ;
2023-09-27 02:17:17 -04:00
2023-08-03 09:30:55 -04:00
chunk_appendf ( & trash , " %s iobuf.flags=0x%08x .pipe=%d .buf=%u@%p+%u/%u \n " , pfx ,
scb - > sedesc - > iobuf . flags ,
scb - > sedesc - > iobuf . pipe ? scb - > sedesc - > iobuf . pipe - > data : 0 ,
scb - > sedesc - > iobuf . buf ? ( unsigned int ) b_data ( scb - > sedesc - > iobuf . buf ) : 0 ,
scb - > sedesc - > iobuf . buf ? b_orig ( scb - > sedesc - > iobuf . buf ) : NULL ,
scb - > sedesc - > iobuf . buf ? ( unsigned int ) b_head_ofs ( scb - > sedesc - > iobuf . buf ) : 0 ,
scb - > sedesc - > iobuf . buf ? ( unsigned int ) b_size ( scb - > sedesc - > iobuf . buf ) : 0 ) ;
2023-09-27 02:17:17 -04:00
if ( ( conn = sc_conn ( scb ) ) ! = NULL ) {
if ( conn - > mux & & conn - > mux - > show_sd ) {
2023-09-28 02:22:51 -04:00
char muxpfx [ 100 ] = " " ;
snprintf ( muxpfx , sizeof ( muxpfx ) , " %s " , pfx ) ;
chunk_appendf ( buf , " %s " , pfx ) ;
conn - > mux - > show_sd ( buf , scb - > sedesc , muxpfx ) ;
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf , " \n " ) ;
2016-11-21 02:51:11 -05:00
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s co1=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p \n " , pfx ,
2023-09-27 02:17:17 -04:00
conn ,
conn_get_ctrl_name ( conn ) ,
conn_get_xprt_name ( conn ) ,
conn_get_mux_name ( conn ) ,
sc_get_data_name ( scb ) ,
obj_type_name ( conn - > target ) ,
obj_base_ptr ( conn - > target ) ) ;
2016-11-21 02:51:11 -05:00
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s flags=0x%08x fd=%d fd.state=%02x updt=%d fd.tmask=0x%lx \n " , pfx ,
2023-09-27 02:17:17 -04:00
conn - > flags ,
conn_fd ( conn ) ,
conn_fd ( conn ) > = 0 ? fdtab [ conn - > handle . fd ] . state : 0 ,
conn_fd ( conn ) > = 0 ? ! ! ( fdtab [ conn - > handle . fd ] . update_mask & ti - > ltid_bit ) : 0 ,
conn_fd ( conn ) > = 0 ? fdtab [ conn - > handle . fd ] . thread_mask : 0 ) ;
}
else if ( ( tmpctx = sc_appctx ( scb ) ) ! = NULL ) {
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s app1=%p st0=%d st1=%d applet=%s tid=%d nice=%d calls=%u rate=%u \n " , pfx ,
2023-09-27 02:17:17 -04:00
tmpctx ,
tmpctx - > st0 ,
tmpctx - > st1 ,
tmpctx - > applet - > name ,
tmpctx - > t - > tid ,
2025-02-21 12:23:23 -05:00
tmpctx - > t - > nice , tmpctx - > t - > calls ,
read_freq_ctr_period_estimate ( & tmpctx - > call_rate , MS_TO_TICKS ( 1000 ) ) ) ;
2023-09-27 02:17:17 -04:00
}
2023-09-28 01:40:35 -04:00
if ( HAS_FILTERS ( strm ) ) {
const struct filter * flt ;
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s filters={ " , pfx ) ;
2023-09-28 01:40:35 -04:00
list_for_each_entry ( flt , & strm - > strm_flt . filters , list ) {
if ( flt - > list . p ! = & strm - > strm_flt . filters )
chunk_appendf ( buf , " , " ) ;
2024-10-22 10:57:03 -04:00
chunk_appendf ( buf , " %p= \" %s \" [%u] " , flt , FLT_ID ( flt ) , flt - > calls ) ;
2023-09-28 01:40:35 -04:00
}
chunk_appendf ( buf , " } \n " ) ;
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-08-03 09:30:55 -04:00
" %s req=%p (f=0x%06x an=0x%x tofwd=%d total=%lld) \n "
2023-09-28 02:22:51 -04:00
" %s an_exp=%s buf=%p data=%p o=%u p=%u i=%u size=%u \n " ,
pfx ,
2023-09-27 02:17:17 -04:00
& strm - > req ,
strm - > req . flags , strm - > req . analysers ,
strm - > req . to_forward , strm - > req . total ,
2023-09-28 02:22:51 -04:00
pfx ,
2023-09-27 02:17:17 -04:00
strm - > req . analyse_exp ?
human_time ( TICKS_TO_MS ( strm - > req . analyse_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ,
& strm - > req . buf ,
2025-02-21 11:18:00 -05:00
b_orig ( & strm - > req . buf ) , ( unsigned int ) strm - > req . output ,
( unsigned int ) b_peek_ofs ( & strm - > req . buf , strm - > req . output ) ,
( unsigned int ) ( c_data ( & strm - > req ) - strm - > req . output ) ,
2023-09-27 02:17:17 -04:00
( unsigned int ) strm - > req . buf . size ) ;
if ( IS_HTX_STRM ( strm ) ) {
struct htx * htx = htxbuf ( & strm - > req . buf ) ;
2022-09-02 09:00:48 -04:00
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu \n " , pfx ,
2023-09-27 02:17:17 -04:00
htx , htx - > flags , htx - > size , htx - > data , htx_nbblks ( htx ) ,
( htx - > tail > = htx - > head ) ? " NO " : " YES " ,
( unsigned long long ) htx - > extra ) ;
}
2023-09-29 02:43:51 -04:00
if ( HAS_FILTERS ( strm ) & & strm - > strm_flt . current [ 0 ] ) {
const struct filter * flt = strm - > strm_flt . current [ 0 ] ;
2023-09-27 02:17:17 -04:00
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s current_filter=%p (id= \" %s \" flags=0x%x pre=0x%x post=0x%x) \n " , pfx ,
2023-09-27 02:17:17 -04:00
flt , flt - > config - > id , flt - > flags , flt - > pre_analyzers , flt - > post_analyzers ) ;
}
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-08-03 09:30:55 -04:00
" %s res=%p (f=0x%06x an=0x%x tofwd=%d total=%lld) \n "
2023-09-28 02:22:51 -04:00
" %s an_exp=%s buf=%p data=%p o=%u p=%u i=%u size=%u \n " ,
pfx ,
2023-09-27 02:17:17 -04:00
& strm - > res ,
strm - > res . flags , strm - > res . analysers ,
strm - > res . to_forward , strm - > res . total ,
2023-09-28 02:22:51 -04:00
pfx ,
2023-09-27 02:17:17 -04:00
strm - > res . analyse_exp ?
human_time ( TICKS_TO_MS ( strm - > res . analyse_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " <NEVER> " ,
& strm - > res . buf ,
2025-02-21 11:18:00 -05:00
b_orig ( & strm - > res . buf ) , ( unsigned int ) strm - > res . output ,
( unsigned int ) b_peek_ofs ( & strm - > res . buf , strm - > res . output ) ,
( unsigned int ) ( c_data ( & strm - > res ) - strm - > res . output ) ,
2023-09-27 02:17:17 -04:00
( unsigned int ) strm - > res . buf . size ) ;
if ( IS_HTX_STRM ( strm ) ) {
struct htx * htx = htxbuf ( & strm - > res . buf ) ;
2016-11-21 02:51:11 -05:00
2023-09-27 02:23:49 -04:00
chunk_appendf ( buf ,
2023-09-28 02:22:51 -04:00
" %s htx=%p flags=0x%x size=%u data=%u used=%u wrap=%s extra=%llu \n " , pfx ,
2023-09-27 02:17:17 -04:00
htx , htx - > flags , htx - > size , htx - > data , htx_nbblks ( htx ) ,
( htx - > tail > = htx - > head ) ? " NO " : " YES " ,
( unsigned long long ) htx - > extra ) ;
}
2023-09-28 02:22:51 -04:00
2023-09-29 02:43:51 -04:00
if ( HAS_FILTERS ( strm ) & & strm - > strm_flt . current [ 1 ] ) {
const struct filter * flt = strm - > strm_flt . current [ 1 ] ;
2022-09-02 09:00:48 -04:00
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s current_filter=%p (id= \" %s \" flags=0x%x pre=0x%x post=0x%x) \n " , pfx ,
2023-09-27 02:17:17 -04:00
flt , flt - > config - > id , flt - > flags , flt - > pre_analyzers , flt - > post_analyzers ) ;
}
2016-11-21 02:51:11 -05:00
2023-09-27 02:17:17 -04:00
if ( strm - > current_rule_list & & strm - > current_rule ) {
const struct act_rule * rule = strm - > current_rule ;
2023-09-28 02:22:51 -04:00
chunk_appendf ( buf , " %s current_rule= \" %s \" [%s:%d] \n " , pfx , rule - > kw - > kw , rule - > conf . file , rule - > conf . line ) ;
2023-09-27 02:17:17 -04:00
}
}
2021-10-12 05:02:48 -04:00
2025-04-02 14:44:22 -04:00
/* Context-less function to append a complete dump of a stream state onto the
2024-11-18 09:17:13 -05:00
* buffer . It relies on __strm_dump_to_buffer .
*/
void strm_dump_to_buffer ( struct buffer * buf , const struct stream * strm , const char * pfx , uint32_t anon_key )
{
__strm_dump_to_buffer ( buf , NULL , strm , pfx , anon_key ) ;
}
2023-09-27 02:17:17 -04:00
/* This function dumps a complete stream state onto the stream connector's
* read buffer . The stream has to be set in strm . It returns 0 if the output
* buffer is full and it needs to be called again , otherwise non - zero . It is
* designed to be called from stats_dump_strm_to_buffer ( ) below .
*/
2024-02-06 08:54:54 -05:00
static int stats_dump_full_strm_to_buffer ( struct appctx * appctx , struct stream * strm )
2023-09-27 02:17:17 -04:00
{
struct show_sess_ctx * ctx = appctx - > svcctx ;
2019-01-04 08:30:44 -05:00
2023-09-27 02:17:17 -04:00
chunk_reset ( & trash ) ;
2021-10-12 05:02:48 -04:00
2023-09-27 02:17:17 -04:00
if ( ctx - > section > 0 & & ctx - > uid ! = strm - > uniq_id ) {
/* stream changed, no need to go any further */
chunk_appendf ( & trash , " *** session terminated while we were watching it *** \n " ) ;
if ( applet_putchk ( appctx , & trash ) = = - 1 )
goto full ;
goto done ;
}
2019-01-04 08:30:44 -05:00
2023-09-27 02:17:17 -04:00
switch ( ctx - > section ) {
case 0 : /* main status of the stream */
ctx - > uid = strm - > uniq_id ;
ctx - > section = 1 ;
__fallthrough ;
2021-10-11 03:49:03 -04:00
2023-09-27 02:17:17 -04:00
case 1 :
2025-04-24 05:17:07 -04:00
__strm_dump_to_buffer ( & trash , ctx , strm , " " , appctx - > cli_ctx . anon_key ) ;
2022-05-18 09:07:19 -04:00
if ( applet_putchk ( appctx , & trash ) = = - 1 )
2019-01-04 11:42:57 -05:00
goto full ;
2016-11-21 02:51:11 -05:00
/* use other states to dump the contents */
}
/* end of dump */
2019-01-04 11:42:57 -05:00
done :
2022-05-03 04:49:00 -04:00
ctx - > uid = 0 ;
ctx - > section = 0 ;
2016-11-21 02:51:11 -05:00
return 1 ;
2019-01-04 11:42:57 -05:00
full :
return 0 ;
2016-11-21 02:51:11 -05:00
}
2018-04-18 07:26:46 -04:00
static int cli_parse_show_sess ( char * * args , char * payload , struct appctx * appctx , void * private )
2016-11-21 02:51:11 -05:00
{
2022-05-03 04:49:00 -04:00
struct show_sess_ctx * ctx = applet_reserve_svcctx ( appctx , sizeof ( * ctx ) ) ;
2024-11-18 09:17:13 -05:00
int cur_arg = 2 ;
2022-05-03 04:49:00 -04:00
2016-11-21 02:51:11 -05:00
if ( ! cli_has_level ( appctx , ACCESS_LVL_OPER ) )
return 1 ;
2023-11-17 12:32:59 -05:00
/* now all sessions by default */
ctx - > target = NULL ;
ctx - > min_age = 0 ;
2022-05-03 04:49:00 -04:00
ctx - > section = 0 ; /* start with stream status */
ctx - > pos = 0 ;
ctx - > thr = 0 ;
2016-11-21 02:51:11 -05:00
2024-11-18 09:17:13 -05:00
if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " older " ) = = 0 ) {
2023-11-17 12:32:59 -05:00
unsigned timeout ;
const char * res ;
2024-11-18 09:17:13 -05:00
if ( ! * args [ cur_arg + 1 ] )
2023-11-17 12:32:59 -05:00
return cli_err ( appctx , " Expects a minimum age (in seconds by default) . \ n " ) ;
2024-11-18 09:17:13 -05:00
res = parse_time_err ( args [ cur_arg + 1 ] , & timeout , TIME_UNIT_S ) ;
2023-11-17 12:32:59 -05:00
if ( res ! = 0 )
return cli_err ( appctx , " Invalid age. \n " ) ;
ctx - > min_age = timeout ;
ctx - > target = ( void * ) - 1 ; /* show all matching entries */
2024-11-18 09:17:13 -05:00
cur_arg + = 2 ;
2023-11-17 12:32:59 -05:00
}
2024-11-18 09:17:13 -05:00
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " all " ) = = 0 ) {
2023-11-17 12:32:59 -05:00
ctx - > target = ( void * ) - 1 ;
2024-11-18 09:17:13 -05:00
cur_arg + + ;
}
2024-11-25 12:34:01 -05:00
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " help " ) = = 0 ) {
chunk_printf ( & trash ,
2025-03-07 04:21:21 -05:00
" Usage: show sess [<id> | all | help] [<options>*] \n "
2024-11-25 12:34:01 -05:00
" Dumps active streams (formerly called 'sessions'). Available selectors: \n "
" <id> dump only this stream identifier (0x...) \n "
2025-03-07 04:21:21 -05:00
" all dump all matching streams in large format \n "
" help show this message \n "
2024-11-25 12:34:01 -05:00
" susp report streams considered suspicious \n "
" Available options: \n "
" show-uri also display the transaction URI, if available \n "
2025-03-07 04:21:21 -05:00
" older <age> only display streams older than <age> seconds \n "
2025-03-07 04:34:19 -05:00
" server <b/s> only show streams attached to this backend+server \n "
" backend <b> only show streams attached to this backend \n "
" frontend <f> only show streams attached to this frontend \n "
2024-11-25 12:34:01 -05:00
" Without any argument, all streams are dumped in a shorter format. " ) ;
return cli_err ( appctx , trash . area ) ;
}
2024-11-18 09:17:13 -05:00
else if ( * args [ cur_arg ] ) {
ctx - > target = ( void * ) strtoul ( args [ cur_arg ] , NULL , 0 ) ;
if ( ctx - > target )
cur_arg + + ;
}
/* show-sess options parsing */
while ( * args [ cur_arg ] ) {
if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " show-uri " ) = = 0 ) {
ctx - > flags | = CLI_SHOWSESS_F_DUMP_URI ;
}
2025-03-07 04:21:21 -05:00
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " susp " ) = = 0 ) {
ctx - > flags | = CLI_SHOWSESS_F_SUSP ;
}
2025-03-07 04:34:19 -05:00
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " server " ) = = 0 ) {
struct ist be_name , sv_name ;
struct proxy * be ;
struct server * sv ;
if ( ctx - > flags & ( CLI_SHOWSESS_F_FRONTEND | CLI_SHOWSESS_F_BACKEND | CLI_SHOWSESS_F_SERVER ) )
return cli_err ( appctx , " Only one of backend, frontend or server may be set. \n " ) ;
if ( ! * args [ cur_arg + 1 ] )
return cli_err ( appctx , " Missing server name (<backend>/<server>) . \ n " ) ;
sv_name = ist ( args [ cur_arg + 1 ] ) ;
be_name = istsplit ( & sv_name , ' / ' ) ;
if ( ! istlen ( sv_name ) )
return cli_err ( appctx , " Require 'backend/server'. \n " ) ;
if ( ! ( be = proxy_be_by_name ( ist0 ( be_name ) ) ) )
return cli_err ( appctx , " No such backend. \n " ) ;
if ( ! ( sv = server_find_by_name ( be , ist0 ( sv_name ) ) ) )
return cli_err ( appctx , " No such server. \n " ) ;
ctx - > flags | = CLI_SHOWSESS_F_SERVER ;
ctx - > filter = sv ;
cur_arg + + ;
}
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " backend " ) = = 0 ) {
struct proxy * be ;
if ( ctx - > flags & ( CLI_SHOWSESS_F_FRONTEND | CLI_SHOWSESS_F_BACKEND | CLI_SHOWSESS_F_SERVER ) )
return cli_err ( appctx , " Only one of backend, frontend or server may be set. \n " ) ;
if ( ! * args [ cur_arg + 1 ] )
return cli_err ( appctx , " Missing backend name. \n " ) ;
if ( ! ( be = proxy_be_by_name ( args [ cur_arg + 1 ] ) ) )
return cli_err ( appctx , " No such backend. \n " ) ;
ctx - > flags | = CLI_SHOWSESS_F_BACKEND ;
ctx - > filter = be ;
cur_arg + + ;
}
else if ( * args [ cur_arg ] & & strcmp ( args [ cur_arg ] , " frontend " ) = = 0 ) {
struct proxy * fe ;
if ( ctx - > flags & ( CLI_SHOWSESS_F_FRONTEND | CLI_SHOWSESS_F_BACKEND | CLI_SHOWSESS_F_SERVER ) )
return cli_err ( appctx , " Only one of backend, frontend or server may be set. \n " ) ;
if ( ! * args [ cur_arg + 1 ] )
return cli_err ( appctx , " Missing frontend name. \n " ) ;
if ( ! ( fe = proxy_fe_by_name ( args [ cur_arg + 1 ] ) ) )
return cli_err ( appctx , " No such frontend. \n " ) ;
ctx - > flags | = CLI_SHOWSESS_F_FRONTEND ;
ctx - > filter = fe ;
cur_arg + + ;
}
2024-11-18 09:17:13 -05:00
else {
2024-11-25 12:34:01 -05:00
chunk_printf ( & trash , " Unsupported option '%s', try 'help' for more info. \n " , args [ cur_arg ] ) ;
2024-11-18 09:17:13 -05:00
return cli_err ( appctx , trash . area ) ;
}
cur_arg + + ;
}
2023-11-17 12:32:59 -05:00
2022-05-03 05:05:39 -04:00
/* The back-ref must be reset, it will be detected and set by
* the dump code upon first invocation .
*/
LIST_INIT ( & ctx - > bref . users ) ;
2021-02-24 05:29:51 -05:00
/* let's set our own stream's epoch to the current one and increment
* it so that we know which streams were already there before us .
*/
2022-05-11 08:09:57 -04:00
appctx_strm ( appctx ) - > stream_epoch = _HA_ATOMIC_FETCH_ADD ( & stream_epoch , 1 ) ;
2016-11-21 02:51:11 -05:00
return 0 ;
}
2022-05-17 13:07:51 -04:00
/* This function dumps all streams' states onto the stream connector's
2016-11-21 02:51:11 -05:00
* read buffer . It returns 0 if the output buffer is full and it needs
2019-01-04 11:42:57 -05:00
* to be called again , otherwise non - zero . It proceeds in an isolated
* thread so there is no thread safety issue here .
2016-11-21 02:51:11 -05:00
*/
static int cli_io_handler_dump_sess ( struct appctx * appctx )
{
2022-05-03 04:49:00 -04:00
struct show_sess_ctx * ctx = appctx - > svcctx ;
2016-11-21 02:51:11 -05:00
struct connection * conn ;
2019-01-04 11:42:57 -05:00
thread_isolate ( ) ;
2022-05-03 05:10:19 -04:00
if ( ctx - > thr > = global . nbthread ) {
/* already terminated */
goto done ;
}
2016-11-21 02:51:11 -05:00
chunk_reset ( & trash ) ;
2022-05-03 05:17:35 -04:00
/* first, let's detach the back-ref from a possible previous stream */
if ( ! LIST_ISEMPTY ( & ctx - > bref . users ) ) {
LIST_DELETE ( & ctx - > bref . users ) ;
LIST_INIT ( & ctx - > bref . users ) ;
} else if ( ! ctx - > bref . ref ) {
/* first call, start with first stream */
ctx - > bref . ref = ha_thread_ctx [ ctx - > thr ] . streams . n ;
}
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
/* and start from where we stopped */
while ( 1 ) {
char pn [ INET6_ADDRSTRLEN ] ;
struct stream * curr_strm ;
2025-03-06 12:56:13 -05:00
uint64_t request_ts ;
2022-05-03 05:17:35 -04:00
int done = 0 ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
if ( ctx - > bref . ref = = & ha_thread_ctx [ ctx - > thr ] . streams )
done = 1 ;
else {
/* check if we've found a stream created after issuing the "show sess" */
curr_strm = LIST_ELEM ( ctx - > bref . ref , struct stream * , list ) ;
2022-05-11 08:09:57 -04:00
if ( ( int ) ( curr_strm - > stream_epoch - appctx_strm ( appctx ) - > stream_epoch ) > 0 )
2021-02-24 04:37:01 -05:00
done = 1 ;
2022-05-03 05:17:35 -04:00
}
2021-02-24 05:53:17 -05:00
2022-05-03 05:17:35 -04:00
if ( done ) {
ctx - > thr + + ;
if ( ctx - > thr > = global . nbthread )
break ;
ctx - > bref . ref = ha_thread_ctx [ ctx - > thr ] . streams . n ;
continue ;
}
2016-11-21 02:51:11 -05:00
2023-11-17 12:32:59 -05:00
if ( ctx - > min_age ) {
uint age = ns_to_sec ( now_ns ) - ns_to_sec ( curr_strm - > logs . request_ts ) ;
if ( age < ctx - > min_age )
goto next_sess ;
}
2025-03-07 04:34:19 -05:00
if ( ( ctx - > flags & CLI_SHOWSESS_F_SERVER ) & &
( ! ( curr_strm - > be - > cap & PR_CAP_BE ) | | curr_strm - > target ! = ctx - > filter ) )
goto next_sess ;
if ( ( ctx - > flags & CLI_SHOWSESS_F_BACKEND ) & & ( curr_strm - > be ! = ctx - > filter ) )
goto next_sess ;
if ( ( ctx - > flags & CLI_SHOWSESS_F_FRONTEND ) & & ( curr_strm - > sess - > fe ! = ctx - > filter ) )
goto next_sess ;
2023-11-17 13:29:02 -05:00
if ( ctx - > flags & CLI_SHOWSESS_F_SUSP ) {
/* only show suspicious streams. Non-suspicious ones have a valid
* expiration date in the future and a valid front endpoint .
*/
if ( curr_strm - > task - > expire & &
! tick_is_expired ( curr_strm - > task - > expire , now_ms ) & &
curr_strm - > scf & & curr_strm - > scf - > sedesc & & curr_strm - > scf - > sedesc - > se )
goto next_sess ;
}
2022-05-03 05:17:35 -04:00
if ( ctx - > target ) {
if ( ctx - > target ! = ( void * ) - 1 & & ctx - > target ! = curr_strm )
goto next_sess ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
LIST_APPEND ( & curr_strm - > back_refs , & ctx - > bref . users ) ;
/* call the proper dump() function and return if we're missing space */
2024-02-06 08:54:54 -05:00
if ( ! stats_dump_full_strm_to_buffer ( appctx , curr_strm ) )
2022-05-03 05:17:35 -04:00
goto full ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
/* stream dump complete */
LIST_DELETE ( & ctx - > bref . users ) ;
LIST_INIT ( & ctx - > bref . users ) ;
if ( ctx - > target ! = ( void * ) - 1 ) {
ctx - > target = NULL ;
2016-11-21 02:51:11 -05:00
break ;
}
2022-05-03 05:17:35 -04:00
else
goto next_sess ;
}
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
" %p: proto=%s " ,
curr_strm ,
strm_li ( curr_strm ) ? strm_li ( curr_strm ) - > rx . proto - > name : " ? " ) ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
conn = objt_conn ( strm_orig ( curr_strm ) ) ;
switch ( conn & & conn_get_src ( conn ) ? addr_to_str ( conn - > src , pn , sizeof ( pn ) ) : AF_UNSPEC ) {
case AF_INET :
case AF_INET6 :
2016-11-21 02:51:11 -05:00
chunk_appendf ( & trash ,
2022-05-03 05:17:35 -04:00
" src=%s:%d fe=%s be=%s srv=%s " ,
2022-09-14 11:45:41 -04:00
HA_ANON_CLI ( pn ) ,
2022-05-03 05:17:35 -04:00
get_host_port ( conn - > src ) ,
2022-09-14 11:45:41 -04:00
HA_ANON_CLI ( strm_fe ( curr_strm ) - > id ) ,
( curr_strm - > be - > cap & PR_CAP_BE ) ? HA_ANON_CLI ( curr_strm - > be - > id ) : " <NONE> " ,
objt_server ( curr_strm - > target ) ? HA_ANON_CLI ( __objt_server ( curr_strm - > target ) - > id ) : " <none> "
2022-05-03 05:17:35 -04:00
) ;
break ;
case AF_UNIX :
MEDIUM: protocol: make abns a custom unix socket address family
This is a pre-requisite to adding the abnsz socket address family:
in this patch we make use of protocol API rework started by 732913f
("MINOR: protocol: properly assign the sock_domain and sock_family") in
order to implement a dedicated address family for ABNS sockets (based on
UNIX parent family).
Thanks to this, it will become trivial to implement a new ABNSZ (for abns
zero) family which is essentially the same as ABNS but with a slight
difference when it comes to path handling (ABNS uses the whole sun_path
length, while ABNSZ's path is zero terminated and evaluation stops at 0)
It was verified that this patch doesn't break reg-tests and behaves
properly (tests performed on the CLI with show sess and show fd).
Anywhere relevant, AF_CUST_ABNS is handled alongside AF_UNIX. If no
distinction needs to be made, real_family() is used to fetch the proper
real family type to handle it properly.
Both stream and dgram were converted, so no functional change should be
expected for this "internal" rework, except that proto will be displayed
as "abns_{stream,dgram}" instead of "unix_{stream,dgram}".
Before ("show sess" output):
0x64c35528aab0: proto=unix_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=21,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
After:
0x619da7ad74c0: proto=abns_stream src=unix:1 fe=GLOBAL be=<NONE> srv=<none> ts=00 epoch=0 age=0s calls=1 rate=0 cpu=0 lat=0 rq[f=848000h,i=0,an=00h,ax=] rp[f=80008000h,i=0,an=00h,ax=] scf=[8,0h,fd=22,rex=10s,wex=] scb=[8,1h,fd=-1,rex=,wex=] exp=10s rc=0 c_exp=
Co-authored-by: Aurelien DARRAGON <adarragon@haproxy.com>
2024-08-09 12:48:14 -04:00
case AF_CUST_ABNS :
2024-08-09 15:12:23 -04:00
case AF_CUST_ABNSZ :
2016-11-21 02:51:11 -05:00
chunk_appendf ( & trash ,
2022-05-03 05:17:35 -04:00
" src=unix:%d fe=%s be=%s srv=%s " ,
strm_li ( curr_strm ) - > luid ,
2022-09-14 11:45:41 -04:00
HA_ANON_CLI ( strm_fe ( curr_strm ) - > id ) ,
( curr_strm - > be - > cap & PR_CAP_BE ) ? HA_ANON_CLI ( curr_strm - > be - > id ) : " <NONE> " ,
objt_server ( curr_strm - > target ) ? HA_ANON_CLI ( __objt_server ( curr_strm - > target ) - > id ) : " <none> "
2022-05-03 05:17:35 -04:00
) ;
break ;
}
2016-11-21 02:51:11 -05:00
2025-03-06 12:56:13 -05:00
request_ts = curr_strm - > logs . accept_ts ;
request_ts + = ms_to_ns ( curr_strm - > logs . t_idle > = 0 ? curr_strm - > logs . t_idle + curr_strm - > logs . t_handshake : 0 ) ;
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
" ts=%02x epoch=%#x age=%s calls=%u rate=%u cpu=%llu lat=%llu " ,
curr_strm - > task - > state , curr_strm - > stream_epoch ,
2025-03-06 12:56:13 -05:00
human_time ( ns_to_sec ( now_ns ) - ns_to_sec ( request_ts ) , 1 ) ,
2022-05-03 05:17:35 -04:00
curr_strm - > task - > calls , read_freq_ctr ( & curr_strm - > call_rate ) ,
2022-09-07 03:17:45 -04:00
( unsigned long long ) curr_strm - > cpu_time , ( unsigned long long ) curr_strm - > lat_time ) ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
2023-02-07 10:06:14 -05:00
" rq[f=%06xh,i=%u,an=%02xh " ,
2022-05-03 05:17:35 -04:00
curr_strm - > req . flags ,
( unsigned int ) ci_data ( & curr_strm - > req ) ,
2023-02-07 10:06:14 -05:00
curr_strm - > req . analysers ) ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
" ,ax=%s] " ,
curr_strm - > req . analyse_exp ?
human_time ( TICKS_TO_MS ( curr_strm - > req . analyse_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " " ) ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
2023-02-07 10:06:14 -05:00
" rp[f=%06xh,i=%u,an=%02xh " ,
2022-05-03 05:17:35 -04:00
curr_strm - > res . flags ,
( unsigned int ) ci_data ( & curr_strm - > res ) ,
2023-02-07 10:06:14 -05:00
curr_strm - > res . analysers ) ;
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
" ,ax=%s] " ,
curr_strm - > res . analyse_exp ?
human_time ( TICKS_TO_MS ( curr_strm - > res . analyse_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " " ) ;
2016-11-21 02:51:11 -05:00
2022-05-18 10:23:22 -04:00
conn = sc_conn ( curr_strm - > scf ) ;
2023-02-07 10:06:14 -05:00
chunk_appendf ( & trash , " scf=[%d,%1xh,fd=%d " ,
curr_strm - > scf - > state , curr_strm - > scf - > flags , conn_fd ( conn ) ) ;
chunk_appendf ( & trash , " ,rex=%s " ,
2023-02-22 08:43:22 -05:00
sc_ep_rcv_ex ( curr_strm - > scf ) ?
human_time ( TICKS_TO_MS ( sc_ep_rcv_ex ( curr_strm - > scf ) - now_ms ) ,
2023-02-07 10:06:14 -05:00
TICKS_TO_MS ( 1000 ) ) : " " ) ;
chunk_appendf ( & trash , " ,wex=%s] " ,
2023-02-22 08:43:22 -05:00
sc_ep_snd_ex ( curr_strm - > scf ) ?
human_time ( TICKS_TO_MS ( sc_ep_snd_ex ( curr_strm - > scf ) - now_ms ) ,
2023-02-07 10:06:14 -05:00
TICKS_TO_MS ( 1000 ) ) : " " ) ;
2016-11-21 02:51:11 -05:00
2022-05-18 10:23:22 -04:00
conn = sc_conn ( curr_strm - > scb ) ;
2023-02-07 10:06:14 -05:00
chunk_appendf ( & trash , " scb=[%d,%1xh,fd=%d " ,
curr_strm - > scb - > state , curr_strm - > scb - > flags , conn_fd ( conn ) ) ;
chunk_appendf ( & trash , " ,rex=%s " ,
2023-02-22 08:43:22 -05:00
sc_ep_rcv_ex ( curr_strm - > scb ) ?
human_time ( TICKS_TO_MS ( sc_ep_rcv_ex ( curr_strm - > scb ) - now_ms ) ,
2023-02-07 10:06:14 -05:00
TICKS_TO_MS ( 1000 ) ) : " " ) ;
chunk_appendf ( & trash , " ,wex=%s] " ,
2023-02-22 08:43:22 -05:00
sc_ep_snd_ex ( curr_strm - > scb ) ?
human_time ( TICKS_TO_MS ( sc_ep_snd_ex ( curr_strm - > scb ) - now_ms ) ,
2023-02-07 10:06:14 -05:00
TICKS_TO_MS ( 1000 ) ) : " " ) ;
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash ,
" exp=%s rc=%d c_exp=%s " ,
curr_strm - > task - > expire ?
human_time ( TICKS_TO_MS ( curr_strm - > task - > expire - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " " ,
curr_strm - > conn_retries ,
curr_strm - > conn_exp ?
human_time ( TICKS_TO_MS ( curr_strm - > conn_exp - now_ms ) ,
TICKS_TO_MS ( 1000 ) ) : " " ) ;
if ( task_in_rq ( curr_strm - > task ) )
chunk_appendf ( & trash , " run(nice=%d) " , curr_strm - > task - > nice ) ;
2024-11-18 09:17:13 -05:00
if ( ( ctx - > flags & CLI_SHOWSESS_F_DUMP_URI ) & & curr_strm - > txn & & curr_strm - > txn - > uri )
chunk_appendf ( & trash , " uri= \" %s \" " , HA_ANON_CLI ( curr_strm - > txn - > uri ) ) ;
2022-05-03 05:17:35 -04:00
chunk_appendf ( & trash , " \n " ) ;
2022-05-18 09:07:19 -04:00
if ( applet_putchk ( appctx , & trash ) = = - 1 ) {
2022-05-03 05:17:35 -04:00
/* let's try again later from this stream. We add ourselves into
* this stream ' s users so that it can remove us upon termination .
*/
LIST_APPEND ( & curr_strm - > back_refs , & ctx - > bref . users ) ;
goto full ;
2016-11-21 02:51:11 -05:00
}
2022-05-03 05:17:35 -04:00
next_sess :
ctx - > bref . ref = curr_strm - > list . n ;
}
2016-11-21 02:51:11 -05:00
2022-05-03 05:17:35 -04:00
if ( ctx - > target & & ctx - > target ! = ( void * ) - 1 ) {
/* specified stream not found */
if ( ctx - > section > 0 )
chunk_appendf ( & trash , " *** session terminated while we were watching it *** \n " ) ;
else
chunk_appendf ( & trash , " Session not found. \n " ) ;
2016-11-21 02:51:11 -05:00
2022-05-18 09:07:19 -04:00
if ( applet_putchk ( appctx , & trash ) = = - 1 )
2022-05-03 05:17:35 -04:00
goto full ;
ctx - > target = NULL ;
ctx - > uid = 0 ;
goto done ;
2016-11-21 02:51:11 -05:00
}
2022-05-03 05:17:35 -04:00
2019-01-04 11:42:57 -05:00
done :
thread_release ( ) ;
return 1 ;
full :
thread_release ( ) ;
return 0 ;
2016-11-21 02:51:11 -05:00
}
static void cli_release_show_sess ( struct appctx * appctx )
{
2022-05-03 04:49:00 -04:00
struct show_sess_ctx * ctx = appctx - > svcctx ;
2022-05-03 05:10:19 -04:00
if ( ctx - > thr < global . nbthread ) {
2021-02-24 07:46:12 -05:00
/* a dump was aborted, either in error or timeout. We need to
* safely detach from the target stream ' s list . It ' s mandatory
* to lock because a stream on the target thread could be moving
* our node .
*/
thread_isolate ( ) ;
2022-05-03 04:49:00 -04:00
if ( ! LIST_ISEMPTY ( & ctx - > bref . users ) )
LIST_DELETE ( & ctx - > bref . users ) ;
2021-02-24 07:46:12 -05:00
thread_release ( ) ;
2016-11-21 02:51:11 -05:00
}
}
2016-11-24 05:09:25 -05:00
/* Parses the "shutdown session" directive, it always returns 1 */
2018-04-18 07:26:46 -04:00
static int cli_parse_shutdown_session ( char * * args , char * payload , struct appctx * appctx , void * private )
2016-11-24 05:09:25 -05:00
{
struct stream * strm , * ptr ;
2021-02-24 04:37:01 -05:00
int thr ;
2016-11-24 05:09:25 -05:00
if ( ! cli_has_level ( appctx , ACCESS_LVL_ADMIN ) )
return 1 ;
2022-03-31 08:49:45 -04:00
ptr = ( void * ) strtoul ( args [ 2 ] , NULL , 0 ) ;
if ( ! ptr )
2019-08-09 05:21:01 -04:00
return cli_err ( appctx , " Session pointer expected (use 'show sess') . \ n " ) ;
2016-11-24 05:09:25 -05:00
2021-02-24 04:37:01 -05:00
strm = NULL ;
2016-11-24 05:09:25 -05:00
2021-02-24 05:11:06 -05:00
thread_isolate ( ) ;
2016-11-24 05:09:25 -05:00
/* first, look for the requested stream in the stream table */
2022-03-31 08:49:45 -04:00
for ( thr = 0 ; strm ! = ptr & & thr < global . nbthread ; thr + + ) {
2021-09-30 13:02:18 -04:00
list_for_each_entry ( strm , & ha_thread_ctx [ thr ] . streams , list ) {
2021-02-24 04:37:01 -05:00
if ( strm = = ptr ) {
stream_shutdown ( strm , SF_ERR_KILLED ) ;
break ;
}
2021-02-24 05:11:06 -05:00
}
2016-11-24 05:09:25 -05:00
}
2021-02-24 05:11:06 -05:00
thread_release ( ) ;
2016-11-24 05:09:25 -05:00
/* do we have the stream ? */
2022-03-31 08:49:45 -04:00
if ( strm ! = ptr )
2019-08-09 05:21:01 -04:00
return cli_err ( appctx , " No such session (use 'show sess') . \ n " ) ;
2016-11-24 05:09:25 -05:00
return 1 ;
}
2016-11-23 10:50:48 -05:00
/* Parses the "shutdown session server" directive, it always returns 1 */
2018-04-18 07:26:46 -04:00
static int cli_parse_shutdown_sessions_server ( char * * args , char * payload , struct appctx * appctx , void * private )
2016-11-23 10:50:48 -05:00
{
struct server * sv ;
if ( ! cli_has_level ( appctx , ACCESS_LVL_ADMIN ) )
return 1 ;
sv = cli_find_server ( appctx , args [ 3 ] ) ;
if ( ! sv )
return 1 ;
/* kill all the stream that are on this server */
2017-11-07 04:42:54 -05:00
HA_SPIN_LOCK ( SERVER_LOCK , & sv - > lock ) ;
2019-11-14 10:37:16 -05:00
srv_shutdown_streams ( sv , SF_ERR_KILLED ) ;
2017-11-07 04:42:54 -05:00
HA_SPIN_UNLOCK ( SERVER_LOCK , & sv - > lock ) ;
2016-11-23 10:50:48 -05:00
return 1 ;
}
2016-11-21 02:51:11 -05:00
/* register cli keywords */
static struct cli_kw_list cli_kws = { { } , {
2025-03-07 04:21:21 -05:00
{ { " show " , " sess " , NULL } , " show sess [help|<id>|all] [opts...] : report the list of current streams or dump this exact stream " , cli_parse_show_sess , cli_io_handler_dump_sess , cli_release_show_sess } ,
2021-05-07 05:38:37 -04:00
{ { " shutdown " , " session " , NULL } , " shutdown session [id] : kill a specific session " , cli_parse_shutdown_session , NULL , NULL } ,
{ { " shutdown " , " sessions " , " server " } , " shutdown sessions server <bk>/<srv> : kill sessions on a server " , cli_parse_shutdown_sessions_server , NULL , NULL } ,
2016-11-21 02:51:11 -05:00
{ { } , }
} } ;
2018-11-25 13:14:37 -05:00
INITCALL1 ( STG_REGISTER , cli_register_kw , & cli_kws ) ;
2015-09-27 13:29:33 -04:00
/* main configuration keyword registration. */
2021-06-25 08:35:29 -04:00
static struct action_kw_list stream_tcp_req_keywords = { ILH , {
2024-09-25 09:10:08 -04:00
{ " set-retries " , stream_parse_set_retries } ,
2021-06-25 08:35:29 -04:00
{ " set-log-level " , stream_parse_set_log_level } ,
2021-06-25 08:46:02 -04:00
{ " set-nice " , stream_parse_set_nice } ,
2021-06-25 08:35:29 -04:00
{ " switch-mode " , stream_parse_switch_mode } ,
{ " use-service " , stream_parse_use_service } ,
{ /* END */ }
} } ;
INITCALL1 ( STG_REGISTER , tcp_req_cont_keywords_register , & stream_tcp_req_keywords ) ;
/* main configuration keyword registration. */
static struct action_kw_list stream_tcp_res_keywords = { ILH , {
{ " set-log-level " , stream_parse_set_log_level } ,
2021-06-25 08:46:02 -04:00
{ " set-nice " , stream_parse_set_nice } ,
2021-06-25 08:35:29 -04:00
{ /* END */ }
} } ;
INITCALL1 ( STG_REGISTER , tcp_res_cont_keywords_register , & stream_tcp_res_keywords ) ;
static struct action_kw_list stream_http_req_keywords = { ILH , {
2024-09-25 09:10:08 -04:00
{ " set-retries " , stream_parse_set_retries } ,
2021-06-25 08:35:29 -04:00
{ " set-log-level " , stream_parse_set_log_level } ,
2021-06-25 08:46:02 -04:00
{ " set-nice " , stream_parse_set_nice } ,
2021-06-25 08:35:29 -04:00
{ " use-service " , stream_parse_use_service } ,
2015-09-27 13:29:33 -04:00
{ /* END */ }
} } ;
2021-06-25 08:35:29 -04:00
INITCALL1 ( STG_REGISTER , http_req_keywords_register , & stream_http_req_keywords ) ;
2018-11-25 13:14:37 -05:00
2021-06-25 08:35:29 -04:00
static struct action_kw_list stream_http_res_keywords = { ILH , {
{ " set-log-level " , stream_parse_set_log_level } ,
2021-06-25 08:46:02 -04:00
{ " set-nice " , stream_parse_set_nice } ,
2015-09-27 13:29:33 -04:00
{ /* END */ }
} } ;
2021-06-25 08:35:29 -04:00
INITCALL1 ( STG_REGISTER , http_res_keywords_register , & stream_http_res_keywords ) ;
2010-06-18 11:46:06 -04:00
2023-01-05 05:17:38 -05:00
static struct action_kw_list stream_http_after_res_actions = { ILH , {
{ " set-log-level " , stream_parse_set_log_level } ,
{ /* END */ }
} } ;
INITCALL1 ( STG_REGISTER , http_after_res_keywords_register , & stream_http_after_res_actions ) ;
2023-09-27 10:43:07 -04:00
static int smp_fetch_cur_client_timeout ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
if ( ! smp - > strm )
return 0 ;
smp - > data . u . sint = TICKS_TO_MS ( smp - > strm - > scf - > ioto ) ;
return 1 ;
}
2020-12-10 07:43:58 -05:00
static int smp_fetch_cur_server_timeout ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
if ( ! smp - > strm )
return 0 ;
2023-02-15 02:13:33 -05:00
smp - > data . u . sint = TICKS_TO_MS ( smp - > strm - > scb - > ioto ) ;
2020-12-10 07:43:58 -05:00
return 1 ;
}
static int smp_fetch_cur_tunnel_timeout ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
if ( ! smp - > strm )
return 0 ;
smp - > data . u . sint = TICKS_TO_MS ( smp - > strm - > tunnel_timeout ) ;
return 1 ;
}
2022-03-09 11:33:05 -05:00
static int smp_fetch_last_rule_file ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
2024-10-31 06:23:12 -04:00
struct act_rule * rule ;
2022-03-09 11:33:05 -05:00
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_STR ;
2024-10-31 08:53:56 -04:00
if ( ! smp - > strm | | smp - > strm - > last_entity . type ! = STRM_ENTITY_RULE )
2022-03-09 11:33:05 -05:00
return 0 ;
2024-10-31 06:23:12 -04:00
rule = smp - > strm - > last_entity . ptr ;
2022-03-09 11:33:05 -05:00
smp - > flags | = SMP_F_CONST ;
2024-10-31 06:23:12 -04:00
smp - > data . u . str . area = ( char * ) rule - > conf . file ;
smp - > data . u . str . data = strlen ( rule - > conf . file ) ;
2022-03-09 11:33:05 -05:00
return 1 ;
}
static int smp_fetch_last_rule_line ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
2024-10-31 06:23:12 -04:00
struct act_rule * rule ;
2022-03-09 11:33:05 -05:00
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
2024-10-31 08:53:56 -04:00
if ( ! smp - > strm | | smp - > strm - > last_entity . type ! = STRM_ENTITY_RULE )
2024-10-31 06:23:12 -04:00
return 0 ;
rule = smp - > strm - > last_entity . ptr ;
smp - > data . u . sint = rule - > conf . line ;
return 1 ;
}
static int smp_fetch_last_entity ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_STR ;
if ( ! smp - > strm )
return 0 ;
2024-10-31 08:53:56 -04:00
if ( smp - > strm - > last_entity . type = = STRM_ENTITY_RULE ) {
2024-10-31 06:23:12 -04:00
struct act_rule * rule = smp - > strm - > last_entity . ptr ;
struct buffer * trash = get_trash_chunk ( ) ;
trash - > data = snprintf ( trash - > area , trash - > size , " %s:%d " , rule - > conf . file , rule - > conf . line ) ;
smp - > data . u . str = * trash ;
}
2024-10-31 08:53:56 -04:00
else if ( smp - > strm - > last_entity . type = = STRM_ENTITY_FILTER ) {
2024-10-31 06:30:46 -04:00
struct filter * filter = smp - > strm - > last_entity . ptr ;
if ( FLT_ID ( filter ) ) {
smp - > flags | = SMP_F_CONST ;
smp - > data . u . str . area = ( char * ) FLT_ID ( filter ) ;
smp - > data . u . str . data = strlen ( FLT_ID ( filter ) ) ;
}
else {
struct buffer * trash = get_trash_chunk ( ) ;
trash - > data = snprintf ( trash - > area , trash - > size , " %p " , filter - > config ) ;
smp - > data . u . str = * trash ;
}
}
2024-10-31 06:23:12 -04:00
else
2022-03-09 11:33:05 -05:00
return 0 ;
return 1 ;
}
2024-10-31 06:34:54 -04:00
static int smp_fetch_waiting_entity ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_STR ;
if ( ! smp - > strm )
return 0 ;
2024-10-31 08:53:56 -04:00
if ( smp - > strm - > waiting_entity . type = = STRM_ENTITY_RULE ) {
2024-10-31 06:34:54 -04:00
struct act_rule * rule = smp - > strm - > waiting_entity . ptr ;
struct buffer * trash = get_trash_chunk ( ) ;
trash - > data = snprintf ( trash - > area , trash - > size , " %s:%d " , rule - > conf . file , rule - > conf . line ) ;
smp - > data . u . str = * trash ;
}
2024-10-31 08:53:56 -04:00
else if ( smp - > strm - > waiting_entity . type = = STRM_ENTITY_FILTER ) {
2024-10-31 06:34:54 -04:00
struct filter * filter = smp - > strm - > waiting_entity . ptr ;
if ( FLT_ID ( filter ) ) {
smp - > flags | = SMP_F_CONST ;
smp - > data . u . str . area = ( char * ) FLT_ID ( filter ) ;
smp - > data . u . str . data = strlen ( FLT_ID ( filter ) ) ;
}
else {
struct buffer * trash = get_trash_chunk ( ) ;
trash - > data = snprintf ( trash - > area , trash - > size , " %p " , filter - > config ) ;
smp - > data . u . str = * trash ;
}
}
2024-10-31 09:16:01 -04:00
else if ( smp - > strm - > waiting_entity . type = = STRM_ENTITY_WREQ_BODY ) {
struct buffer * trash = get_trash_chunk ( ) ;
chunk_memcat ( trash , " http-buffer-request " , 19 ) ;
smp - > data . u . str = * trash ;
}
2024-10-31 06:34:54 -04:00
else
return 0 ;
return 1 ;
}
2023-11-28 02:45:33 -05:00
static int smp_fetch_sess_term_state ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
struct buffer * trash = get_trash_chunk ( ) ;
smp - > flags = SMP_F_VOLATILE ;
smp - > data . type = SMP_T_STR ;
if ( ! smp - > strm )
return 0 ;
trash - > area [ trash - > data + + ] = sess_term_cond [ ( smp - > strm - > flags & SF_ERR_MASK ) > > SF_ERR_SHIFT ] ;
trash - > area [ trash - > data + + ] = sess_fin_state [ ( smp - > strm - > flags & SF_FINST_MASK ) > > SF_FINST_SHIFT ] ;
smp - > data . u . str = * trash ;
smp - > data . type = SMP_T_STR ;
smp - > flags & = ~ SMP_F_CONST ;
return 1 ;
}
2023-11-28 07:59:59 -05:00
static int smp_fetch_conn_retries ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
if ( ! smp - > strm )
return 0 ;
if ( ! sc_state_in ( smp - > strm - > scb - > state , SC_SB_DIS | SC_SB_CLO ) )
smp - > flags | = SMP_F_VOL_TEST ;
smp - > data . u . sint = smp - > strm - > conn_retries ;
return 1 ;
}
2025-01-21 01:46:17 -05:00
static int smp_fetch_tevts ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
struct buffer * trash = get_trash_chunk ( ) ;
struct connection * fconn , * bconn ;
int fc_mux_ret , bc_mux_ret ;
fconn = smp - > sess ? objt_conn ( smp - > sess - > origin ) : NULL ;
bconn = smp - > strm ? sc_conn ( smp - > strm - > scb ) : NULL ;
fc_mux_ret = bc_mux_ret = - 1 ;
if ( fconn & & fconn - > mux & & fconn - > mux - > ctl )
fc_mux_ret = fconn - > mux - > ctl ( fconn , MUX_CTL_TEVTS , NULL ) ;
if ( bconn & & bconn - > mux & & bconn - > mux - > ctl )
bc_mux_ret = bconn - > mux - > ctl ( bconn , MUX_CTL_TEVTS , NULL ) ;
2025-01-21 12:16:27 -05:00
chunk_printf ( trash , " {%s, " , tevt_evts2str ( fconn ? fconn - > term_evts_log : - 1 ) ) ;
chunk_appendf ( trash , " %s, " , tevt_evts2str ( fc_mux_ret ) ) ;
chunk_appendf ( trash , " %s, " , tevt_evts2str ( smp - > strm ? smp - > strm - > scf - > sedesc - > term_evts_log : - 1 ) ) ;
chunk_appendf ( trash , " %s, " , tevt_evts2str ( smp - > strm ? smp - > strm - > term_evts_log : - 1 ) ) ;
chunk_appendf ( trash , " %s, " , tevt_evts2str ( smp - > strm ? smp - > strm - > scb - > sedesc - > term_evts_log : - 1 ) ) ;
chunk_appendf ( trash , " %s, " , tevt_evts2str ( bc_mux_ret ) ) ;
chunk_appendf ( trash , " %s} " , tevt_evts2str ( bconn ? bconn - > term_evts_log : - 1 ) ) ;
2025-01-21 01:46:17 -05:00
smp - > data . u . str = * trash ;
smp - > data . type = SMP_T_STR ;
smp - > flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE ;
return 1 ;
}
2023-11-28 15:01:18 -05:00
static int smp_fetch_id32 ( const struct arg * args , struct sample * smp , const char * km , void * private )
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_SINT ;
if ( ! smp - > strm )
return 0 ;
smp - > data . u . sint = smp - > strm - > uniq_id ;
return 1 ;
}
2024-02-06 11:42:21 -05:00
static int smp_fetch_redispatched ( const struct arg * args , struct sample * smp , const char * km , void * private )
2024-01-30 06:03:28 -05:00
{
smp - > flags = SMP_F_VOL_TXN ;
smp - > data . type = SMP_T_BOOL ;
if ( ! smp - > strm )
return 0 ;
if ( ! sc_state_in ( smp - > strm - > scb - > state , SC_SB_DIS | SC_SB_CLO ) )
smp - > flags | = SMP_F_VOL_TEST ;
smp - > data . u . sint = ! ! ( smp - > strm - > flags & SF_REDISP ) ;
return 1 ;
}
2020-12-10 07:43:57 -05:00
/* Note: must not be declared <const> as its list will be overwritten.
* Please take care of keeping this list alphabetically sorted .
*/
static struct sample_fetch_kw_list smp_kws = { ILH , {
2023-09-27 10:43:07 -04:00
{ " cur_client_timeout " , smp_fetch_cur_client_timeout , 0 , NULL , SMP_T_SINT , SMP_USE_FTEND , } ,
2020-12-10 07:43:58 -05:00
{ " cur_server_timeout " , smp_fetch_cur_server_timeout , 0 , NULL , SMP_T_SINT , SMP_USE_BKEND , } ,
{ " cur_tunnel_timeout " , smp_fetch_cur_tunnel_timeout , 0 , NULL , SMP_T_SINT , SMP_USE_BKEND , } ,
2024-10-31 06:23:12 -04:00
{ " last_entity " , smp_fetch_last_entity , 0 , NULL , SMP_T_STR , SMP_USE_INTRN , } ,
2022-03-09 11:33:05 -05:00
{ " last_rule_file " , smp_fetch_last_rule_file , 0 , NULL , SMP_T_STR , SMP_USE_INTRN , } ,
{ " last_rule_line " , smp_fetch_last_rule_line , 0 , NULL , SMP_T_SINT , SMP_USE_INTRN , } ,
2025-01-21 01:46:17 -05:00
{ " term_events " , smp_fetch_tevts , 0 , NULL , SMP_T_STR , SMP_USE_INTRN , } ,
2023-11-28 07:59:59 -05:00
{ " txn.conn_retries " , smp_fetch_conn_retries , 0 , NULL , SMP_T_SINT , SMP_USE_L4SRV , } ,
2023-11-28 15:01:18 -05:00
{ " txn.id32 " , smp_fetch_id32 , 0 , NULL , SMP_T_SINT , SMP_USE_INTRN , } ,
2024-02-06 11:42:21 -05:00
{ " txn.redispatched " , smp_fetch_redispatched , 0 , NULL , SMP_T_BOOL , SMP_USE_L4SRV , } ,
2023-11-28 02:45:33 -05:00
{ " txn.sess_term_state " , smp_fetch_sess_term_state , 0 , NULL , SMP_T_STR , SMP_USE_INTRN , } ,
2024-10-31 06:34:54 -04:00
{ " waiting_entity " , smp_fetch_waiting_entity , 0 , NULL , SMP_T_STR , SMP_USE_INTRN , } ,
2020-12-10 07:43:57 -05:00
{ NULL , NULL , 0 , 0 , 0 } ,
} } ;
INITCALL1 ( STG_REGISTER , sample_register_fetches , & smp_kws ) ;
2006-06-25 20:48:02 -04:00
/*
* Local variables :
* c - indent - level : 8
* c - basic - offset : 8
* End :
*/