Skip to content

Commit

Permalink
MINOR: Add sample fetches to get the frontend and backend stream ID
Browse files Browse the repository at this point in the history
"fc.id" and "bc.id" sample fetches can now be used to get, respectively, the
frontend or the backend stream ID. They rely on ->sctl() callback function
on the mux attached to the corresponding SC.

It means these sample fetches work only for connection, not applets, and
from the time a multiplexer is installed.
  • Loading branch information
capflam committed Nov 29, 2023
1 parent fd8ce78 commit 2405961
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
11 changes: 10 additions & 1 deletion doc/configuration.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21213,7 +21213,7 @@ ssl_bc_client_random : binary

ssl_bc_curve : string
Returns the name of the curve used in the key agreement when the outgoing
connection was made over an SSL/TLS transport layer. This requires
connection was made over an SSL/TLS transport layer. This requires
OpenSSL >= 3.0.0.

ssl_bc_err : integer
Expand Down Expand Up @@ -21913,6 +21913,10 @@ Warning : Following sample fetches are ignored if used from HTTP proxies. They
all invalid usage (for instance inside a log-format string or a
sample expression). So be careful.

bs.id : integer
Returns the multiplexer's stream ID on the server side. It is the
multiplexer's responsibility to return the appropriate information.

distcc_body(<token>[,<occ>]) : binary
Parses a distcc message and returns the body associated to occurrence #<occ>
of the token <token>. Occurrences start at 1, and when unspecified, any may
Expand All @@ -21939,6 +21943,11 @@ distcc_param(<token>[,<occ>]) : integer
# send large files to the big farm
use_backend big_farm if { distcc_param(DOTI) gt 1000000 }

fs.id : integer
Returns the multiplexer's stream ID on the client side. It is the
multiplexer's responsibility to return the appropriate information. For
instance, on a raw TCP, 0 is always returned because there is no stream.

payload(<offset>,<length>) : binary (deprecated)
This is an alias for "req.payload" when used in the context of a request (e.g.
"stick on", "stick match"), and for "res.payload" when used in the context of
Expand Down
52 changes: 52 additions & 0 deletions src/stconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <haproxy/http_ana.h>
#include <haproxy/pipe.h>
#include <haproxy/pool.h>
#include <haproxy/sample.h>
#include <haproxy/sc_strm.h>
#include <haproxy/stconn.h>
#include <haproxy/xref.h>
Expand Down Expand Up @@ -1990,3 +1991,54 @@ void sc_conn_commit_endp_upgrade(struct stconn *sc)
BUG_ON(!sc);
BUG_ON(!sc->sedesc);
}

/* return the frontend or backend mux stream ID.
*/
static int
smp_fetch_sid(const struct arg *args, struct sample *smp, const char *kw, void *private)
{
struct connection *conn;
struct stconn *sc;
int64_t sid = 0;

if (!smp->strm)
return 0;

sc = (kw[0] == 'f' ? smp->strm->scf : smp->strm->scb);
conn = sc_conn(sc);

/* No connection */
if (!conn)
return 0;

/* No mux install, this may change */
if (!conn->mux) {
smp->flags |= SMP_F_MAY_CHANGE;
return 0;
}

/* No sctl, report sid=0 in this case */
if (conn->mux->sctl) {
if (conn->mux->sctl(sc, MUX_SCTL_SID, &sid) == -1)
return 0;
}

smp->flags = SMP_F_VOL_TXN;
smp->data.type = SMP_T_SINT;
smp->data.u.sint = sid;

return 1;
}

/* Note: must not be declared <const> as its list will be overwritten.
* Note: fetches that may return multiple types should be declared using the
* appropriate pseudo-type. If not available it must be declared as the lowest
* common denominator, the type that can be casted into all other ones.
*/
static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
{ "bs.id", smp_fetch_sid, 0, NULL, SMP_T_SINT, SMP_USE_L6REQ },
{ "fs.id", smp_fetch_sid, 0, NULL, SMP_T_STR, SMP_USE_L6RES },
{ /* END */ },
}};

INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);

0 comments on commit 2405961

Please sign in to comment.