]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: Add sample fetches to get the frontend and backend stream ID
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 28 Nov 2023 15:34:23 +0000 (16:34 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 29 Nov 2023 10:11:12 +0000 (11:11 +0100)
"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.

doc/configuration.txt
src/stconn.c

index 1f47d1ebc19151734e5ec2e69b2b15ea45a9cbf0..2f611d24482d659431010aee2c372b3f10bb7979 100644 (file)
@@ -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
@@ -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
@@ -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
index b2342c98c9536da2effca77f770e2761f474fed5..2afe8d152203bd437e77791ba41d52dea9d3c5dc 100644 (file)
@@ -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>
@@ -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);