blob: 27db231fd1c519ad6b987cdaf776da21b57eb941 [file] [log] [blame]
Christopher Faulet2b677702022-06-22 16:55:04 +02001/*
2 * Bandwidth limitation filter.
3 *
4 * Copyright 2022 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15
16#include <haproxy/api.h>
17#include <haproxy/channel-t.h>
18#include <haproxy/filters.h>
19#include <haproxy/global.h>
20#include <haproxy/http_ana-t.h>
21#include <haproxy/http_rules.h>
22#include <haproxy/proxy.h>
23#include <haproxy/sample.h>
24#include <haproxy/stream.h>
25#include <haproxy/tcp_rules.h>
26#include <haproxy/time.h>
27#include <haproxy/tools.h>
28
29const char *bwlim_flt_id = "bandwidth limitation filter";
30
31struct flt_ops bwlim_ops;
32
33#define BWLIM_FL_NONE 0x00000000 /* For init purposr */
34#define BWLIM_FL_IN 0x00000001 /* Limit clients uploads */
35#define BWLIM_FL_OUT 0x00000002 /* Limit clients downloads */
36#define BWLIM_FL_SHARED 0x00000004 /* Limit shared between clients (using stick-tables) */
37
Christopher Fauletda2e1172023-01-13 15:33:32 +010038#define BWLIM_ACT_LIMIT_EXPR 0x00000001
39#define BWLIM_ACT_LIMIT_CONST 0x00000002
40#define BWLIM_ACT_PERIOD_EXPR 0x00000004
41#define BWLIM_ACT_PERIOD_CONST 0x00000008
42
Christopher Faulet2b677702022-06-22 16:55:04 +020043struct bwlim_config {
44 struct proxy *proxy;
45 char *name;
46 unsigned int flags;
47 struct sample_expr *expr;
48 union {
49 char *n;
50 struct stktable *t;
51 } table;
52 unsigned int period;
53 unsigned int limit;
54 unsigned int min_size;
55};
56
57struct bwlim_state {
58 struct freq_ctr bytes_rate;
59 struct stksess *ts;
60 struct act_rule *rule;
61 unsigned int limit;
62 unsigned int period;
63 unsigned int exp;
64};
65
66
67/* Pools used to allocate comp_state structs */
68DECLARE_STATIC_POOL(pool_head_bwlim_state, "bwlim_state", sizeof(struct bwlim_state));
69
70
71/* Apply the bandwidth limitation of the filter <filter>. <len> is the maximum
72 * amount of data that the filter can forward. This function applies the
73 * limitation and returns what the stream is authorized to forward. Several
74 * limitation can be stacked.
75 */
76static int bwlim_apply_limit(struct filter *filter, struct channel *chn, unsigned int len)
77{
78 struct bwlim_config *conf = FLT_CONF(filter);
79 struct bwlim_state *st = filter->ctx;
80 struct freq_ctr *bytes_rate;
81 unsigned int period, limit, remain, tokens, users;
82 unsigned int wait = 0;
83 int overshoot, ret = 0;
84
85 /* Don't forward anything if there is nothing to forward or the waiting
86 * time is not expired
87 */
88 if (!len || (tick_isset(st->exp) && !tick_is_expired(st->exp, now_ms)))
89 goto end;
90
91 st->exp = TICK_ETERNITY;
92 ret = len;
93 if (conf->flags & BWLIM_FL_SHARED) {
94 void *ptr;
95 unsigned int type = ((conf->flags & BWLIM_FL_IN) ? STKTABLE_DT_BYTES_IN_RATE : STKTABLE_DT_BYTES_OUT_RATE);
96
97 /* In shared mode, get a pointer on the stick table entry. it
98 * will be used to get the freq-counter. It is also used to get
99 * The number of users.
100 */
101 ptr = stktable_data_ptr(conf->table.t, st->ts, type);
102 if (!ptr)
103 goto end;
104
105 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &st->ts->lock);
106 bytes_rate = &stktable_data_cast(ptr, std_t_frqp);
107 period = conf->table.t->data_arg[type].u;
108 limit = conf->limit;
109 users = st->ts->ref_cnt;
110 }
111 else {
112 /* On per-stream mode, the freq-counter is private to the
113 * stream. Get it from the filter state. Rely on the custom
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500114 * limit/period if defined or use the default ones. In this mode,
Christopher Faulet2b677702022-06-22 16:55:04 +0200115 * there is only one user.
116 */
117 bytes_rate = &st->bytes_rate;
118 period = (st->period ? st->period : conf->period);
119 limit = (st->limit ? st->limit : conf->limit);
120 users = 1;
121 }
122
123 /* Be sure the current rate does not exceed the limit over the current
124 * period. In this case, nothing is forwarded and the waiting time is
125 * computed to be sure to not retry too early.
126 *
127 * The test is used to avoid the initial burst. Otherwise, streams will
128 * consume the limit as fast as possible and will then be paused for
129 * long time.
130 */
131 overshoot = freq_ctr_overshoot_period(bytes_rate, period, limit);
132 if (overshoot > 0) {
133 if (conf->flags & BWLIM_FL_SHARED)
134 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &st->ts->lock);
135 wait = div64_32((uint64_t)(conf->min_size + overshoot) * period * users,
136 limit);
137 st->exp = tick_add(now_ms, (wait ? wait : 1));
138 ret = 0;
139 goto end;
140 }
141
142 /* Get the allowed quota per user. */
143 remain = freq_ctr_remain_period(bytes_rate, period, limit, 0);
144 tokens = div64_32((uint64_t)(remain + users - 1), users);
145
146 if (tokens < len) {
147 /* The stream cannot forward all its data. But we will check if
148 * it can perform a small burst if the global quota is large
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500149 * enough. But, in this case, its waiting time will be
Christopher Faulet2b677702022-06-22 16:55:04 +0200150 * increased accordingly.
151 */
152 ret = tokens;
153 if (tokens < conf->min_size) {
Christopher Fauletca5309a2023-04-17 16:17:32 +0200154 ret = (chn_prod(chn)->flags & (SC_FL_EOI|SC_FL_EOS|SC_FL_ABRT_DONE))
Christopher Faulet2b677702022-06-22 16:55:04 +0200155 ? MIN(len, conf->min_size)
156 : conf->min_size;
157
158 if (ret <= remain)
159 wait = div64_32((uint64_t)(ret - tokens) * period * users + limit - 1, limit);
160 else
161 ret = (limit < ret) ? remain : 0;
162 }
163 }
164
165 /* At the end, update the freq-counter and compute the waiting time if
166 * the stream is limited
167 */
168 update_freq_ctr_period(bytes_rate, period, ret);
169 if (ret < len) {
170 wait += next_event_delay_period(bytes_rate, period, limit, MIN(len - ret, conf->min_size * users));
171 st->exp = tick_add(now_ms, (wait ? wait : 1));
172 }
173
174 if (conf->flags & BWLIM_FL_SHARED)
175 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &st->ts->lock);
176
177 end:
178 chn->analyse_exp = tick_first((tick_is_expired(chn->analyse_exp, now_ms) ? TICK_ETERNITY : chn->analyse_exp),
179 st->exp);
180 return ret;
181}
182
183/***************************************************************************
184 * Hooks that manage the filter lifecycle (init/check/deinit)
185 **************************************************************************/
186/* Initialize the filter. Returns -1 on error, else 0. */
187static int bwlim_init(struct proxy *px, struct flt_conf *fconf)
188{
189 fconf->flags |= FLT_CFG_FL_HTX;
190 return 0;
191}
192
193/* Free resources allocated by the bwlim filter. */
194static void bwlim_deinit(struct proxy *px, struct flt_conf *fconf)
195{
196 struct bwlim_config *conf = fconf->conf;
197
198 if (conf) {
Christopher Fauletf0196f42022-06-24 14:52:18 +0200199 ha_free(&conf->name);
Christopher Faulet2b677702022-06-22 16:55:04 +0200200 release_sample_expr(conf->expr);
Christopher Fauletf0196f42022-06-24 14:52:18 +0200201 conf->expr = NULL;
202 ha_free(&fconf->conf);
Christopher Faulet2b677702022-06-22 16:55:04 +0200203 }
204}
205
206/* Check configuration of a bwlim filter for a specified proxy.
207 * Return 1 on error, else 0. */
208static int bwlim_check(struct proxy *px, struct flt_conf *fconf)
209{
210 struct bwlim_config *conf = fconf->conf;
211 struct stktable *target;
212
213 if (!(conf->flags & BWLIM_FL_SHARED))
214 return 0;
215
216 if (conf->table.n)
217 target = stktable_find_by_name(conf->table.n);
218 else
219 target = px->table;
220
221 if (!target) {
222 ha_alert("Proxy %s : unable to find table '%s' referenced by bwlim filter '%s'",
223 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
224 return 1;
225 }
226
227 if ((conf->flags & BWLIM_FL_IN) && !target->data_ofs[STKTABLE_DT_BYTES_IN_RATE]) {
228 ha_alert("Proxy %s : stick-table '%s' uses a data type incompatible with bwlim filter '%s'."
229 " It must be 'bytes_in_rate'",
230 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
231 return 1;
232 }
233 else if ((conf->flags & BWLIM_FL_OUT) && !target->data_ofs[STKTABLE_DT_BYTES_OUT_RATE]) {
234 ha_alert("Proxy %s : stick-table '%s' uses a data type incompatible with bwlim filter '%s'."
235 " It must be 'bytes_out_rate'",
236 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
237 return 1;
238 }
239
240 if (!stktable_compatible_sample(conf->expr, target->type)) {
241 ha_alert("Proxy %s : stick-table '%s' uses a key type incompatible with bwlim filter '%s'",
242 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
243 return 1;
244 }
245 else {
246 if (!in_proxies_list(target->proxies_list, px)) {
247 px->next_stkt_ref = target->proxies_list;
248 target->proxies_list = px;
249 }
Christopher Fauletf0196f42022-06-24 14:52:18 +0200250 ha_free(&conf->table.n);
Christopher Faulet2b677702022-06-22 16:55:04 +0200251 conf->table.t = target;
252 }
253
254 return 0;
255}
256
257/**************************************************************************
258 * Hooks to handle start/stop of streams
259 *************************************************************************/
260/* Called when a filter instance is created and attach to a stream */
261static int bwlim_attach(struct stream *s, struct filter *filter)
262{
263 struct bwlim_state *st;
264
265 st = pool_zalloc(pool_head_bwlim_state);
266 if (!st)
267 return -1;
268 filter->ctx = st;
269 return 1;
270}
271
272/* Called when a filter instance is detach from a stream, just before its
273 * destruction */
274static void bwlim_detach(struct stream *s, struct filter *filter)
275{
276 struct bwlim_config *conf = FLT_CONF(filter);
277 struct bwlim_state *st = filter->ctx;
278 struct stktable *t = conf->table.t;
279
280 if (!st)
281 return;
282
283 if (st->ts)
284 stktable_touch_local(t, st->ts, 1);
285
286 /* release any possible compression context */
287 pool_free(pool_head_bwlim_state, st);
288 filter->ctx = NULL;
289}
290
291/**************************************************************************
Christopher Faulet331a3b92023-08-01 08:16:42 +0200292 * Hooks to handle channels activity
293 *************************************************************************/
294
295/* Called when analyze ends for a given channel */
296static int bwlim_chn_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
297{
298 chn->analyse_exp = TICK_ETERNITY;
299 return 1;
300}
301
302
303/**************************************************************************
Christopher Faulet2b677702022-06-22 16:55:04 +0200304 * Hooks to filter HTTP messages
305 *************************************************************************/
306static int bwlim_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
307{
308 msg->chn->analyse_exp = TICK_ETERNITY;
309 return 1;
310}
311
312static int bwlim_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
313 unsigned int offset, unsigned int len)
314{
315 return bwlim_apply_limit(filter, msg->chn, len);
316}
317
318/**************************************************************************
319 * Hooks to filter TCP data
320 *************************************************************************/
321static int bwlim_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
322 unsigned int offset, unsigned int len)
323{
324 return bwlim_apply_limit(filter, chn, len);
325}
326
327/********************************************************************
328 * Functions that manage the filter initialization
329 ********************************************************************/
330struct flt_ops bwlim_ops = {
331 /* Manage bwlim filter, called for each filter declaration */
332 .init = bwlim_init,
333 .deinit = bwlim_deinit,
334 .check = bwlim_check,
335
336 /* Handle start/stop of streams */
337 .attach = bwlim_attach,
338 .detach = bwlim_detach,
339
Christopher Faulet331a3b92023-08-01 08:16:42 +0200340 /* Handle channels activity */
341 .channel_end_analyze = bwlim_chn_end_analyze,
Christopher Faulet2b677702022-06-22 16:55:04 +0200342
343 /* Filter HTTP requests and responses */
344 .http_headers = bwlim_http_headers,
345 .http_payload = bwlim_http_payload,
346
347 /* Filter TCP data */
348 .tcp_payload = bwlim_tcp_payload,
349};
350
351/* Set a bandwidth limitation. It always return ACT_RET_CONT. On error, the rule
352 * is ignored. First of all, it looks for the corresponding filter. Then, for a
353 * shared limitation, the stick-table entry is retrieved. For a per-stream
354 * limitation, the custom limit and period are computed, if necessary. At the
355 * end, the filter is registered on the data filtering for the right channel
356 * (bwlim-in = request, bwlim-out = response).
357 */
358static enum act_return bwlim_set_limit(struct act_rule *rule, struct proxy *px,
359 struct session *sess, struct stream *s, int flags)
360{
361 struct bwlim_config *conf = rule->arg.act.p[3];
362 struct filter *filter;
363 struct bwlim_state *st = NULL;
364 struct stktable *t;
365 struct stktable_key *key;
366 struct stksess *ts;
367 int opt;
368
369 list_for_each_entry(filter, &s->strm_flt.filters, list) {
370 if (FLT_ID(filter) == bwlim_flt_id && FLT_CONF(filter) == conf) {
371 st = filter->ctx;
372 break;
373 }
374 }
375
376 if (!st)
377 goto end;
378
379 switch (rule->from) {
380 case ACT_F_TCP_REQ_CNT: opt = SMP_OPT_DIR_REQ | SMP_OPT_FINAL; break;
381 case ACT_F_TCP_RES_CNT: opt = SMP_OPT_DIR_RES | SMP_OPT_FINAL; break;
382 case ACT_F_HTTP_REQ: opt = SMP_OPT_DIR_REQ | SMP_OPT_FINAL; break;
383 case ACT_F_HTTP_RES: opt = SMP_OPT_DIR_RES | SMP_OPT_FINAL; break;
384 default:
385 goto end;
386 }
387
388 if (conf->flags & BWLIM_FL_SHARED) {
389 t = conf->table.t;
390 key = stktable_fetch_key(t, px, sess, s, opt, conf->expr, NULL);
391 if (!key)
392 goto end;
393
394 ts = stktable_get_entry(t, key);
395 if (!ts)
396 goto end;
397
398 st->ts = ts;
399 st->rule = rule;
400 }
401 else {
402 struct sample *smp;
403
404 st->limit = 0;
405 st->period = 0;
Christopher Fauletda2e1172023-01-13 15:33:32 +0100406 if (rule->action & BWLIM_ACT_LIMIT_EXPR) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200407 smp = sample_fetch_as_type(px, sess, s, opt, rule->arg.act.p[1], SMP_T_SINT);
408 if (smp && smp->data.u.sint > 0)
409 st->limit = smp->data.u.sint;
410 }
Christopher Fauletda2e1172023-01-13 15:33:32 +0100411 else if (rule->action & BWLIM_ACT_LIMIT_CONST)
412 st->limit = (uintptr_t)rule->arg.act.p[1];
413
414 if (rule->action & BWLIM_ACT_PERIOD_EXPR) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200415 smp = sample_fetch_as_type(px, sess, s, opt, rule->arg.act.p[2], SMP_T_SINT);
416 if (smp && smp->data.u.sint > 0)
417 st->period = smp->data.u.sint;
418 }
Christopher Fauletda2e1172023-01-13 15:33:32 +0100419 else if (rule->action & BWLIM_ACT_PERIOD_CONST)
420 st->period = (uintptr_t)rule->arg.act.p[2];
Christopher Faulet2b677702022-06-22 16:55:04 +0200421 }
422
423 st->exp = TICK_ETERNITY;
424 if (conf->flags & BWLIM_FL_IN)
425 register_data_filter(s, &s->req, filter);
426 else
427 register_data_filter(s, &s->res, filter);
428
429 end:
430 return ACT_RET_CONT;
431}
432
433/* Check function for "set-bandwidth-limit" aciton. It returns 1 on
434 * success. Otherwise, it returns 0 and <err> is filled.
435 */
436int check_bwlim_action(struct act_rule *rule, struct proxy *px, char **err)
437{
438 struct flt_conf *fconf;
439 struct bwlim_config *conf = NULL;
440 unsigned int where;
441
442 list_for_each_entry(fconf, &px->filter_configs, list) {
443 conf = NULL;
444 if (fconf->id == bwlim_flt_id) {
445 conf = fconf->conf;
Tim Duesterhusa6fc6162022-10-08 12:33:19 +0200446 if (strcmp(rule->arg.act.p[0], conf->name) == 0)
Christopher Faulet2b677702022-06-22 16:55:04 +0200447 break;
448 }
449 }
450 if (!conf) {
451 memprintf(err, "unable to find bwlim filter '%s' referenced by set-bandwidth-limit rule",
452 (char *)rule->arg.act.p[0]);
453 return 0;
454 }
455
456 if ((conf->flags & BWLIM_FL_SHARED) && rule->arg.act.p[1]) {
457 memprintf(err, "set-bandwidth-limit rule cannot define a limit for a shared bwlim filter");
458 return 0;
459 }
460
Christopher Fauletab34ebe2023-01-13 15:21:53 +0100461 if ((conf->flags & BWLIM_FL_SHARED) && rule->arg.act.p[2]) {
462 memprintf(err, "set-bandwidth-limit rule cannot define a period for a shared bwlim filter");
463 return 0;
464 }
465
Christopher Faulet2b677702022-06-22 16:55:04 +0200466 where = 0;
Christopher Faulet6bf86c72023-01-13 15:39:54 +0100467 if (px->cap & PR_CAP_FE) {
468 if (rule->from == ACT_F_TCP_REQ_CNT)
469 where |= SMP_VAL_FE_REQ_CNT;
470 else if (rule->from == ACT_F_HTTP_REQ)
471 where |= SMP_VAL_FE_HRQ_HDR;
472 else if (rule->from == ACT_F_TCP_RES_CNT)
473 where |= SMP_VAL_FE_RES_CNT;
474 else if (rule->from == ACT_F_HTTP_RES)
475 where |= SMP_VAL_FE_HRS_HDR;
476 }
477 if (px->cap & PR_CAP_BE) {
478 if (rule->from == ACT_F_TCP_REQ_CNT)
479 where |= SMP_VAL_BE_REQ_CNT;
480 else if (rule->from == ACT_F_HTTP_REQ)
481 where |= SMP_VAL_BE_HRQ_HDR;
482 else if (rule->from == ACT_F_TCP_RES_CNT)
483 where |= SMP_VAL_BE_RES_CNT;
484 else if (rule->from == ACT_F_HTTP_RES)
485 where |= SMP_VAL_BE_HRS_HDR;
486 }
Christopher Faulet2b677702022-06-22 16:55:04 +0200487
Christopher Fauletda2e1172023-01-13 15:33:32 +0100488 if ((rule->action & BWLIM_ACT_LIMIT_EXPR) && rule->arg.act.p[1]) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200489 struct sample_expr *expr = rule->arg.act.p[1];
490
491 if (!(expr->fetch->val & where)) {
492 memprintf(err, "set-bandwidth-limit rule uses a limit extracting information from '%s', none of which is available here",
493 sample_src_names(expr->fetch->use));
494 return 0;
495 }
496
497 if (rule->from == ACT_F_TCP_REQ_CNT && (px->cap & PR_CAP_FE)) {
498 if (!px->tcp_req.inspect_delay && !(expr->fetch->val & SMP_VAL_FE_SES_ACC)) {
499 ha_warning("%s '%s' : a 'tcp-request content set-bandwidth-limit*' rule explicitly depending on request"
500 " contents without any 'tcp-request inspect-delay' setting."
501 " This means that this rule will randomly find its contents. This can be fixed by"
502 " setting the tcp-request inspect-delay.\n",
503 proxy_type_str(px), px->id);
504 }
505 }
Christopher Faulet6bf86c72023-01-13 15:39:54 +0100506 if (rule->from == ACT_F_TCP_RES_CNT && (px->cap & PR_CAP_BE)) {
507 if (!px->tcp_rep.inspect_delay && !(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
508 ha_warning("%s '%s' : a 'tcp-response content set-bandwidth-limit*' rule explicitly depending on response"
509 " contents without any 'tcp-response inspect-delay' setting."
510 " This means that this rule will randomly find its contents. This can be fixed by"
511 " setting the tcp-response inspect-delay.\n",
512 proxy_type_str(px), px->id);
513 }
514 }
Christopher Faulet2b677702022-06-22 16:55:04 +0200515 }
516
Christopher Fauletda2e1172023-01-13 15:33:32 +0100517 if ((rule->action & BWLIM_ACT_PERIOD_EXPR) && rule->arg.act.p[2]) {
Christopher Fauletab34ebe2023-01-13 15:21:53 +0100518 struct sample_expr *expr = rule->arg.act.p[2];
519
520 if (!(expr->fetch->val & where)) {
521 memprintf(err, "set-bandwidth-limit rule uses a period extracting information from '%s', none of which is available here",
522 sample_src_names(expr->fetch->use));
523 return 0;
524 }
525
526 if (rule->from == ACT_F_TCP_REQ_CNT && (px->cap & PR_CAP_FE)) {
527 if (!px->tcp_req.inspect_delay && !(expr->fetch->val & SMP_VAL_FE_SES_ACC)) {
528 ha_warning("%s '%s' : a 'tcp-request content set-bandwidth-limit*' rule explicitly depending on request"
529 " contents without any 'tcp-request inspect-delay' setting."
530 " This means that this rule will randomly find its contents. This can be fixed by"
531 " setting the tcp-request inspect-delay.\n",
532 proxy_type_str(px), px->id);
Christopher Faulet6bf86c72023-01-13 15:39:54 +0100533 }
534 }
535 if (rule->from == ACT_F_TCP_RES_CNT && (px->cap & PR_CAP_BE)) {
536 if (!px->tcp_rep.inspect_delay && !(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
537 ha_warning("%s '%s' : a 'tcp-response content set-bandwidth-limit*' rule explicitly depending on response"
538 " contents without any 'tcp-response inspect-delay' setting."
539 " This means that this rule will randomly find its contents. This can be fixed by"
540 " setting the tcp-response inspect-delay.\n",
541 proxy_type_str(px), px->id);
Christopher Fauletab34ebe2023-01-13 15:21:53 +0100542 }
543 }
544 }
545
Christopher Faulet2b677702022-06-22 16:55:04 +0200546 if (conf->expr) {
547 if (!(conf->expr->fetch->val & where)) {
548 memprintf(err, "bwlim filter '%s uses a key extracting information from '%s', none of which is available here",
549 conf->name, sample_src_names(conf->expr->fetch->use));
550 return 0;
551 }
552
553 if (rule->from == ACT_F_TCP_REQ_CNT && (px->cap & PR_CAP_FE)) {
554 if (!px->tcp_req.inspect_delay && !(conf->expr->fetch->val & SMP_VAL_FE_SES_ACC)) {
555 ha_warning("%s '%s' : a 'tcp-request content set-bandwidth-limit*' rule explicitly depending on request"
556 " contents without any 'tcp-request inspect-delay' setting."
557 " This means that this rule will randomly find its contents. This can be fixed by"
558 " setting the tcp-request inspect-delay.\n",
559 proxy_type_str(px), px->id);
560 }
561 }
Christopher Faulet6bf86c72023-01-13 15:39:54 +0100562 if (rule->from == ACT_F_TCP_RES_CNT && (px->cap & PR_CAP_BE)) {
563 if (!px->tcp_rep.inspect_delay && !(conf->expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
564 ha_warning("%s '%s' : a 'tcp-response content set-bandwidth-limit*' rule explicitly depending on response"
565 " contents without any 'tcp-response inspect-delay' setting."
566 " This means that this rule will randomly find its contents. This can be fixed by"
567 " setting the tcp-response inspect-delay.\n",
568 proxy_type_str(px), px->id);
569 }
570 }
Christopher Faulet2b677702022-06-22 16:55:04 +0200571 }
572
573 end:
574 rule->arg.act.p[3] = conf;
575 return 1;
576}
577
578/* Release memory allocated by "set-bandwidth-limit" action. */
579static void release_bwlim_action(struct act_rule *rule)
580{
Christopher Fauletf0196f42022-06-24 14:52:18 +0200581 ha_free(&rule->arg.act.p[0]);
Christopher Fauletda2e1172023-01-13 15:33:32 +0100582 if ((rule->action & BWLIM_ACT_LIMIT_EXPR) && rule->arg.act.p[1]) {
Christopher Fauletf0196f42022-06-24 14:52:18 +0200583 release_sample_expr(rule->arg.act.p[1]);
584 rule->arg.act.p[1] = NULL;
585 }
Christopher Fauletda2e1172023-01-13 15:33:32 +0100586 if ((rule->action & BWLIM_ACT_PERIOD_EXPR) && rule->arg.act.p[2]) {
Christopher Fauletf0196f42022-06-24 14:52:18 +0200587 release_sample_expr(rule->arg.act.p[2]);
588 rule->arg.act.p[2] = NULL;
589 }
590 rule->arg.act.p[3] = NULL; /* points on the filter's config */
Christopher Faulet2b677702022-06-22 16:55:04 +0200591}
592
593/* Parse "set-bandwidth-limit" action. The filter name must be specified. For
594 * shared limitations, there is no other supported parameter. For per-stream
595 * limitations, a custom limit and period may be specified. In both case, it
596 * must be an expression. On success:
597 *
598 * arg.act.p[0] will be the filter name (mandatory)
599 * arg.act.p[1] will be an expression for the custom limit (optional, may be NULL)
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500600 * arg.act.p[2] will be an expression for the custom period (optional, may be NULL)
Christopher Faulet2b677702022-06-22 16:55:04 +0200601 *
602 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
603 */
604static enum act_parse_ret parse_bandwidth_limit(const char **args, int *orig_arg, struct proxy *px,
605 struct act_rule *rule, char **err)
606{
607 struct sample_expr *expr;
608 int cur_arg;
609
610 cur_arg = *orig_arg;
611
612 if (!*args[cur_arg]) {
613 memprintf(err, "missing bwlim filter name");
614 return ACT_RET_PRS_ERR;
615 }
616
617 rule->arg.act.p[0] = strdup(args[cur_arg]);
618 if (!rule->arg.act.p[0]) {
619 memprintf(err, "out of memory");
620 return ACT_RET_PRS_ERR;
621 }
622 cur_arg++;
623
624 while (1) {
625 if (strcmp(args[cur_arg], "limit") == 0) {
Christopher Fauletda2e1172023-01-13 15:33:32 +0100626 const char *res;
627 unsigned int limit;
628
Christopher Faulet2b677702022-06-22 16:55:04 +0200629 cur_arg++;
630 if (!args[cur_arg]) {
Christopher Fauletda2e1172023-01-13 15:33:32 +0100631 memprintf(err, "missing limit value or expression");
Christopher Faulet2b677702022-06-22 16:55:04 +0200632 goto error;
633 }
634
Christopher Fauletda2e1172023-01-13 15:33:32 +0100635 res = parse_size_err(args[cur_arg], &limit);
636 if (!res) {
637 rule->action |= BWLIM_ACT_LIMIT_CONST;
638 rule->arg.act.p[1] = (void *)(uintptr_t)limit;
639 cur_arg++;
640 continue;
641 }
642
643 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, NULL, &px->conf.args, NULL);
644 if (!expr) {
645 memprintf(err, "'%s': invalid size value or unknown fetch method '%s'", args[cur_arg-1], args[cur_arg]);
Christopher Faulet2b677702022-06-22 16:55:04 +0200646 goto error;
Christopher Fauletda2e1172023-01-13 15:33:32 +0100647 }
648 rule->action |= BWLIM_ACT_LIMIT_EXPR;
Christopher Faulet2b677702022-06-22 16:55:04 +0200649 rule->arg.act.p[1] = expr;
650 }
651 else if (strcmp(args[cur_arg], "period") == 0) {
Christopher Fauletda2e1172023-01-13 15:33:32 +0100652 const char *res;
653 unsigned int period;
654
Christopher Faulet2b677702022-06-22 16:55:04 +0200655 cur_arg++;
656 if (!args[cur_arg]) {
Christopher Fauletda2e1172023-01-13 15:33:32 +0100657 memprintf(err, "missing period value or expression");
Christopher Faulet2b677702022-06-22 16:55:04 +0200658 goto error;
659 }
660
Christopher Fauletda2e1172023-01-13 15:33:32 +0100661 res = parse_time_err(args[cur_arg], &period, TIME_UNIT_MS);
662 if (!res) {
663 rule->action |= BWLIM_ACT_PERIOD_CONST;
664 rule->arg.act.p[2] = (void *)(uintptr_t)period;
665 cur_arg++;
666 continue;
667 }
668
669 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, NULL, &px->conf.args, NULL);
670 if (!expr) {
671 memprintf(err, "'%s': invalid time value or unknown fetch method '%s'", args[cur_arg-1], args[cur_arg]);
Christopher Faulet2b677702022-06-22 16:55:04 +0200672 goto error;
Christopher Fauletda2e1172023-01-13 15:33:32 +0100673 }
674 rule->action |= BWLIM_ACT_PERIOD_EXPR;
Christopher Faulet2b677702022-06-22 16:55:04 +0200675 rule->arg.act.p[2] = expr;
676 }
677 else
678 break;
679 }
680
681 rule->action_ptr = bwlim_set_limit;
682 rule->check_ptr = check_bwlim_action;
683 rule->release_ptr = release_bwlim_action;
684
685 *orig_arg = cur_arg;
686 return ACT_RET_PRS_OK;
687
688error:
689 release_bwlim_action(rule);
690 return ACT_RET_PRS_ERR;
691}
692
693
694static struct action_kw_list tcp_req_cont_actions = {
695 .kw = {
696 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
697 { NULL, NULL }
698 }
699};
700
701static struct action_kw_list tcp_res_cont_actions = {
702 .kw = {
703 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
704 { NULL, NULL }
705 }
706};
707
708static struct action_kw_list http_req_actions = {
709 .kw = {
710 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
711 { NULL, NULL }
712 }
713};
714
715static struct action_kw_list http_res_actions = {
716 .kw = {
717 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
718 { NULL, NULL }
719 }
720};
721
722INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
723INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
724INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
725INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
726
727
728/* Generic function to parse bandwidth limitation filter configurartion. It
729 * Returns -1 on error and 0 on success. It handles configuration for per-stream
730 * and shared limitations.
731 */
732static int parse_bwlim_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
733 char **err, void *private)
734{
735 struct flt_conf *fc;
736 struct bwlim_config *conf;
737 int shared, per_stream;
738 int pos = *cur_arg + 1;
739
740 conf = calloc(1, sizeof(*conf));
741 if (!conf) {
742 memprintf(err, "%s: out of memory", args[*cur_arg]);
743 return -1;
744 }
745 conf->proxy = px;
746
747 if (!*args[pos]) {
748 memprintf(err, "'%s' : a name is expected as first argument ", args[*cur_arg]);
749 goto error;
750 }
751 conf->flags = BWLIM_FL_NONE;
752 conf->name = strdup(args[pos]);
753 if (!conf->name) {
754 memprintf(err, "%s: out of memory", args[*cur_arg]);
755 goto error;
756 }
757
758 list_for_each_entry(fc, &px->filter_configs, list) {
759 if (fc->id == bwlim_flt_id) {
760 struct bwlim_config *c = fc->conf;
761
Tim Duesterhusa6fc6162022-10-08 12:33:19 +0200762 if (strcmp(conf->name, c->name) == 0) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200763 memprintf(err, "bwlim filter '%s' already declared for proxy '%s'\n",
764 conf->name, px->id);
765 goto error;
766 }
767 }
768 }
769 shared = per_stream = 0;
770 pos++;
771 while (*args[pos]) {
772 if (strcmp(args[pos], "key") == 0) {
773 if (per_stream) {
774 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
775 args[*cur_arg]);
776 goto error;
777 }
778 if (!*args[pos + 1]) {
779 memprintf(err, "'%s' : the sample expression is missing for '%s' option",
780 args[*cur_arg], args[pos]);
781 goto error;
782 }
783 shared = 1;
784 pos++;
785 conf->expr = sample_parse_expr((char **)args, &pos, px->conf.args.file, px->conf.args.line,
786 err, &px->conf.args, NULL);
787 if (!conf->expr)
788 goto error;
789 }
790 else if (strcmp(args[pos], "table") == 0) {
791 if (per_stream) {
792 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
793 args[*cur_arg]);
794 goto error;
795 }
796 if (!*args[pos + 1]) {
797 memprintf(err, "'%s' : the table name is missing for '%s' option",
798 args[*cur_arg], args[pos]);
799 goto error;
800 }
801 shared = 1;
802 conf->table.n = strdup(args[pos + 1]);
803 if (!conf->table.n) {
804 memprintf(err, "%s: out of memory", args[*cur_arg]);
805 goto error;
806 }
807 pos += 2;
808 }
809 else if (strcmp(args[pos], "default-period") == 0) {
810 const char *res;
811
812 if (shared) {
813 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
814 args[*cur_arg]);
815 goto error;
816 }
817 if (!*args[pos + 1]) {
818 memprintf(err, "'%s' : the value is missing for '%s' option",
819 args[*cur_arg], args[pos]);
820 goto error;
821 }
822 per_stream = 1;
823 res = parse_time_err(args[pos + 1], &conf->period, TIME_UNIT_MS);
824 if (res) {
825 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
826 args[*cur_arg], args[pos], *res);
827 goto error;
828 }
829 pos += 2;
830 }
831 else if (strcmp(args[pos], "limit") == 0) {
832 const char *res;
833
834 if (per_stream) {
835 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
836 args[*cur_arg]);
837 goto error;
838 }
839 if (!*args[pos + 1]) {
840 memprintf(err, "'%s' : the value is missing for '%s' option",
841 args[*cur_arg], args[pos]);
842 goto error;
843 }
844 shared = 1;
845 res = parse_size_err(args[pos + 1], &conf->limit);
846 if (res) {
847 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
848 args[*cur_arg], args[pos], *res);
849 goto error;
850 }
851 pos += 2;
852 }
853 else if (strcmp(args[pos], "default-limit") == 0) {
854 const char *res;
855
856 if (shared) {
857 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
858 args[*cur_arg]);
859 goto error;
860 }
861 if (!*args[pos + 1]) {
862 memprintf(err, "'%s' : the value is missing for '%s' option",
863 args[*cur_arg], args[pos]);
864 goto error;
865 }
866 per_stream = 1;
867 res = parse_size_err(args[pos + 1], &conf->limit);
868 if (res) {
869 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
870 args[*cur_arg], args[pos], *res);
871 goto error;
872 }
873 pos += 2;
874 }
875 else if (strcmp(args[pos], "min-size") == 0) {
876 const char *res;
877
878 if (!*args[pos + 1]) {
879 memprintf(err, "'%s' : the value is missing for '%s' option",
880 args[*cur_arg], args[pos]);
881 goto error;
882 }
883 res = parse_size_err(args[pos + 1], &conf->min_size);
884 if (res) {
885 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
886 args[*cur_arg], args[pos], *res);
887 goto error;
888 }
889 pos += 2;
890 }
891 else
892 break;
893 }
894
895 if (shared) {
896 conf->flags |= BWLIM_FL_SHARED;
897 if (!conf->expr) {
898 memprintf(err, "'%s' : <key> option is missing", args[*cur_arg]);
899 goto error;
900 }
901 if (!conf->limit) {
902 memprintf(err, "'%s' : <limit> option is missing", args[*cur_arg]);
903 goto error;
904 }
905 }
906 else {
907 /* Per-stream: limit downloads only for now */
908 conf->flags |= BWLIM_FL_OUT;
909 if (!conf->period) {
910 memprintf(err, "'%s' : <default-period> option is missing", args[*cur_arg]);
911 goto error;
912 }
913 if (!conf->limit) {
914 memprintf(err, "'%s' : <default-limit> option is missing", args[*cur_arg]);
915 goto error;
916 }
917 }
918
919 *cur_arg = pos;
920 fconf->id = bwlim_flt_id;
921 fconf->ops = &bwlim_ops;
922 fconf->conf = conf;
923 return 0;
924
925 error:
926 if (conf->name)
Christopher Fauletf0196f42022-06-24 14:52:18 +0200927 ha_free(&conf->name);
928 if (conf->expr) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200929 release_sample_expr(conf->expr);
Christopher Fauletf0196f42022-06-24 14:52:18 +0200930 conf->expr = NULL;
931 }
Christopher Faulet2b677702022-06-22 16:55:04 +0200932 if (conf->table.n)
Christopher Fauletf0196f42022-06-24 14:52:18 +0200933 ha_free(&conf->table.n);
Christopher Faulet2b677702022-06-22 16:55:04 +0200934 free(conf);
935 return -1;
936}
937
938
939static int parse_bwlim_in_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
940 char **err, void *private)
941{
942 int ret;
943
944 ret = parse_bwlim_flt(args, cur_arg, px, fconf, err, private);
945 if (!ret) {
946 struct bwlim_config *conf = fconf->conf;
947
948 conf->flags |= BWLIM_FL_IN;
949 }
950
951 return ret;
952}
953
954static int parse_bwlim_out_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
955 char **err, void *private)
956{
957 int ret;
958
959 ret = parse_bwlim_flt(args, cur_arg, px, fconf, err, private);
960 if (!ret) {
961 struct bwlim_config *conf = fconf->conf;
962
963 conf->flags |= BWLIM_FL_OUT;
964 }
965 return ret;
966}
967
968/* Declare the filter parser for "trace" keyword */
969static struct flt_kw_list flt_kws = { "BWLIM", { }, {
970 { "bwlim-in", parse_bwlim_in_flt, NULL },
971 { "bwlim-out", parse_bwlim_out_flt, NULL },
972 { NULL, NULL, NULL },
973 }
974};
975
976INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);