]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: Add bc_rtt and bc_rttvar
authorAleksandar Lazic <al-haproxy@none.at>
Fri, 28 Apr 2023 09:39:12 +0000 (11:39 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 28 Apr 2023 14:31:08 +0000 (16:31 +0200)
This Patch adds fetch samples for backends round trip time.

doc/configuration.txt
reg-tests/sample_fetches/tcpinfo_rtt.vtc [new file with mode: 0644]
src/tcp_sample.c

index 32d2fec17a5daa53094ae6027f34a3f677d050fe..28f308f9d5e8293243b8cc2cc9bb3083f50dab58 100644 (file)
@@ -19642,6 +19642,22 @@ be_name : string
   frontends with responses to check which backend processed the request. It can
   also be used in a tcp-check or an http-check ruleset.
 
+bc_rtt(<unit>) : integer
+  Returns the Round Trip Time (RTT) measured by the kernel for the backend
+  connection. <unit> is facultative, by default the unit is milliseconds. <unit>
+  can be set to "ms" for milliseconds or "us" for microseconds. If the server
+  connection is not established, if the connection is not TCP or if the
+  operating system does not support TCP_INFO, for example Linux kernels before
+  2.4, the sample fetch fails.
+
+bc_rttvar(<unit>) : integer
+  Returns the Round Trip Time (RTT) variance measured by the kernel for the
+  backend connection. <unit> is facultative, by default the unit is milliseconds.
+  <unit> can be set to "ms" for milliseconds or "us" for microseconds. If the
+  server connection is not established, if the connection is not TCP or if the
+  operating system does not support TCP_INFO, for example Linux kernels before
+  2.4, the sample fetch fails.
+
 be_server_timeout : integer
   Returns the configuration value in millisecond for the server timeout of the
   current backend. This timeout can be overwritten by a "set-timeout" rule. See
diff --git a/reg-tests/sample_fetches/tcpinfo_rtt.vtc b/reg-tests/sample_fetches/tcpinfo_rtt.vtc
new file mode 100644 (file)
index 0000000..e21c542
--- /dev/null
@@ -0,0 +1,39 @@
+varnishtest "Test declaration of TCP rtt fetches"
+
+# feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(v2.8-dev8)'"
+feature ignore_unknown_macro
+
+server s1 {
+        rxreq
+        txresp
+}  -start
+
+haproxy h1 -conf {
+  defaults common
+      mode http
+      timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+      timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+      timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+  frontend fe from common
+      bind "fd@${feh1}"
+
+      default_backend be
+
+  backend be from common
+
+      http-response set-header x-test1 "%[fc_rtt]"
+      http-response set-header x-test2 "%[bc_rtt(us)]"
+      http-response set-header x-test3 "%[fc_rttvar]"
+      http-response set-header x-test4 "%[bc_rttvar]"
+
+      server s1 ${s1_addr}:${s1_port}
+
+} -start
+
+client c1 -connect ${h1_feh1_sock} {
+        txreq -req GET -url /
+        rxresp
+        expect resp.status == 200
+        expect resp.http.x-test2 ~ "[0-9]+"
+} -run
\ No newline at end of file
index 12eb25c4eb1f3fa24fa2bfc6fb57f296cbbcf6c0..393e39e93d540b41164ea4fa2eb2893b2e1a389a 100644 (file)
@@ -401,6 +401,35 @@ smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw,
        return 1;
 }
 
+/* get the mean rtt of a backend connection */
+static int
+smp_fetch_bc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       if (!get_tcp_info(args, smp, 1, 0))
+               return 0;
+
+       /* By default or if explicitly specified, convert rtt to ms */
+       if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
+               smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
+
+       return 1;
+}
+
+/* get the variance of the mean rtt of a backend connection */
+static int
+smp_fetch_bc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       if (!get_tcp_info(args, smp, 1, 1))
+               return 0;
+
+       /* By default or if explicitly specified, convert rttvar to ms */
+       if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
+               smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
+
+       return 1;
+}
+
+
 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
 /* get the unacked counter on a client connection */
 static int
@@ -497,6 +526,9 @@ static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
 #ifdef TCP_INFO
        { "fc_rtt",           smp_fetch_fc_rtt,           ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
        { "fc_rttvar",        smp_fetch_fc_rttvar,        ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
+       { "bc_rtt",           smp_fetch_bc_rtt,           ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
+       { "bc_rttvar",        smp_fetch_bc_rttvar,        ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
+
 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
        { "fc_unacked",       smp_fetch_fc_unacked,       ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
 #endif