]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: flt-trace: Add an option to limit the amount of data forwarded
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 16 Feb 2026 10:42:36 +0000 (11:42 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 18 Feb 2026 08:44:15 +0000 (09:44 +0100)
"max-fwd <max>" option can now be used to limit the maximum amount of data
forwarded at a time by the filter. It could be handy to make tests.

doc/configuration.txt
src/flt_trace.c

index caa1810ccf4f51a18b7c540e48a3e9945a06b588..037f701e82864c94bc8e5b9d58c5c7869aa2a40b 100644 (file)
@@ -29778,7 +29778,7 @@ See also : "filter"
 9.1. Trace
 ----------
 
-filter trace [name <name>] [random-forwarding] [hexdump]
+filter trace [name <name>] [random-forwarding] [max-fwd <max>] [hexdump]
 
   Arguments:
     <name>               is an arbitrary name that will be reported in
@@ -29791,6 +29791,11 @@ filter trace [name <name>] [random-forwarding] [hexdump]
                          data. With this parameter, it only forwards a random
                          amount of the parsed data.
 
+    <max>                is the maximum amount of data that can be forwarded at
+                         a time. "max-fwd" option can be combined with the
+                         random forwarding. <max> must be an positive integer.
+                         0 means there is no limit.
+
     <hexdump>             dumps all forwarded data to the server and the client.
 
 This filter can be used as a base to develop new filters. It defines all
index 4605013bbd5900ab22658791df047be40ee9e761..ba50d6d4eb2be2065a25aa917a76b1d2d6b25bd7 100644 (file)
@@ -37,6 +37,7 @@ struct trace_config {
        struct proxy *proxy;
        char         *name;
        unsigned int  flags;
+       unsigned int  max_fwd;
 };
 
 #define FLT_TRACE(conf, fmt, ...)                                              \
@@ -194,10 +195,11 @@ trace_init(struct proxy *px, struct flt_conf *fconf)
        fconf->flags |= FLT_CFG_FL_HTX;
        fconf->conf = conf;
 
-       FLT_TRACE(conf, "filter initialized [quiet=%s - fwd random=%s - hexdump=%s]",
+       FLT_TRACE(conf, "filter initialized [quiet=%s - fwd random=%s - hexdump=%s - max fwd=%u]",
                  ((conf->flags & TRACE_F_QUIET) ? "true" : "false"),
                  ((conf->flags & TRACE_F_RAND_FWD) ? "true" : "false"),
-                 ((conf->flags & TRACE_F_HEXDUMP) ? "true" : "false"));
+                 ((conf->flags & TRACE_F_HEXDUMP) ? "true" : "false"),
+                 conf->max_fwd);
        return 0;
 }
 
@@ -473,6 +475,8 @@ trace_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg
                                ret = len;
                }
        }
+       if (conf->max_fwd && ret > conf->max_fwd)
+               ret = conf->max_fwd;
 
        FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
                   "offset=%u - len=%u - forward=%d",
@@ -541,6 +545,8 @@ trace_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
                                        ret = len;
                        }
                }
+               if (conf->max_fwd && ret > conf->max_fwd)
+                       ret = conf->max_fwd;
 
                FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
                               "offset=%u - len=%u - forward=%d",
@@ -555,6 +561,8 @@ trace_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
 
                if (ret && (conf->flags & TRACE_F_RAND_FWD))
                        ret = ha_random() % (ret+1);
+               if (conf->max_fwd && ret > conf->max_fwd)
+                       ret = conf->max_fwd;
 
                FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
                               "offset=%u - len=%u - forward=%d",
@@ -651,6 +659,22 @@ parse_trace_flt(char **args, int *cur_arg, struct proxy *px,
                                conf->flags |= TRACE_F_RAND_FWD;
                        else if (strcmp(args[pos], "hexdump") == 0)
                                conf->flags |= TRACE_F_HEXDUMP;
+                       else if (strcmp(args[pos], "max-fwd") == 0) {
+                               long long max;
+                               if (!*args[pos + 1]) {
+                                       memprintf(err, "'%s' : '%s' option without value",
+                                                 args[*cur_arg], args[pos]);
+                                       goto error;
+                               }
+                               max = atoll(args[pos + 1]);
+                               if (max < 0 || max > UINT_MAX) {
+                                       memprintf(err, "'%s' : '%s' expect unsigned integer (0 to 4294967295 expected)",
+                                                 args[*cur_arg], args[pos]);
+                                       goto error;
+                               }
+                               conf->max_fwd = max;
+                               pos++;
+                       }
                        else
                                break;
                        pos++;