blob: e3d3c0dc09ce433a5956a66657a564623fb20055 [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
38struct bwlim_config {
39 struct proxy *proxy;
40 char *name;
41 unsigned int flags;
42 struct sample_expr *expr;
43 union {
44 char *n;
45 struct stktable *t;
46 } table;
47 unsigned int period;
48 unsigned int limit;
49 unsigned int min_size;
50};
51
52struct bwlim_state {
53 struct freq_ctr bytes_rate;
54 struct stksess *ts;
55 struct act_rule *rule;
56 unsigned int limit;
57 unsigned int period;
58 unsigned int exp;
59};
60
61
62/* Pools used to allocate comp_state structs */
63DECLARE_STATIC_POOL(pool_head_bwlim_state, "bwlim_state", sizeof(struct bwlim_state));
64
65
66/* Apply the bandwidth limitation of the filter <filter>. <len> is the maximum
67 * amount of data that the filter can forward. This function applies the
68 * limitation and returns what the stream is authorized to forward. Several
69 * limitation can be stacked.
70 */
71static int bwlim_apply_limit(struct filter *filter, struct channel *chn, unsigned int len)
72{
73 struct bwlim_config *conf = FLT_CONF(filter);
74 struct bwlim_state *st = filter->ctx;
75 struct freq_ctr *bytes_rate;
76 unsigned int period, limit, remain, tokens, users;
77 unsigned int wait = 0;
78 int overshoot, ret = 0;
79
80 /* Don't forward anything if there is nothing to forward or the waiting
81 * time is not expired
82 */
83 if (!len || (tick_isset(st->exp) && !tick_is_expired(st->exp, now_ms)))
84 goto end;
85
86 st->exp = TICK_ETERNITY;
87 ret = len;
88 if (conf->flags & BWLIM_FL_SHARED) {
89 void *ptr;
90 unsigned int type = ((conf->flags & BWLIM_FL_IN) ? STKTABLE_DT_BYTES_IN_RATE : STKTABLE_DT_BYTES_OUT_RATE);
91
92 /* In shared mode, get a pointer on the stick table entry. it
93 * will be used to get the freq-counter. It is also used to get
94 * The number of users.
95 */
96 ptr = stktable_data_ptr(conf->table.t, st->ts, type);
97 if (!ptr)
98 goto end;
99
100 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &st->ts->lock);
101 bytes_rate = &stktable_data_cast(ptr, std_t_frqp);
102 period = conf->table.t->data_arg[type].u;
103 limit = conf->limit;
104 users = st->ts->ref_cnt;
105 }
106 else {
107 /* On per-stream mode, the freq-counter is private to the
108 * stream. Get it from the filter state. Rely on the custom
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500109 * limit/period if defined or use the default ones. In this mode,
Christopher Faulet2b677702022-06-22 16:55:04 +0200110 * there is only one user.
111 */
112 bytes_rate = &st->bytes_rate;
113 period = (st->period ? st->period : conf->period);
114 limit = (st->limit ? st->limit : conf->limit);
115 users = 1;
116 }
117
118 /* Be sure the current rate does not exceed the limit over the current
119 * period. In this case, nothing is forwarded and the waiting time is
120 * computed to be sure to not retry too early.
121 *
122 * The test is used to avoid the initial burst. Otherwise, streams will
123 * consume the limit as fast as possible and will then be paused for
124 * long time.
125 */
126 overshoot = freq_ctr_overshoot_period(bytes_rate, period, limit);
127 if (overshoot > 0) {
128 if (conf->flags & BWLIM_FL_SHARED)
129 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &st->ts->lock);
130 wait = div64_32((uint64_t)(conf->min_size + overshoot) * period * users,
131 limit);
132 st->exp = tick_add(now_ms, (wait ? wait : 1));
133 ret = 0;
134 goto end;
135 }
136
137 /* Get the allowed quota per user. */
138 remain = freq_ctr_remain_period(bytes_rate, period, limit, 0);
139 tokens = div64_32((uint64_t)(remain + users - 1), users);
140
141 if (tokens < len) {
142 /* The stream cannot forward all its data. But we will check if
143 * it can perform a small burst if the global quota is large
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500144 * enough. But, in this case, its waiting time will be
Christopher Faulet2b677702022-06-22 16:55:04 +0200145 * increased accordingly.
146 */
147 ret = tokens;
148 if (tokens < conf->min_size) {
149 ret = (chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR))
150 ? MIN(len, conf->min_size)
151 : conf->min_size;
152
153 if (ret <= remain)
154 wait = div64_32((uint64_t)(ret - tokens) * period * users + limit - 1, limit);
155 else
156 ret = (limit < ret) ? remain : 0;
157 }
158 }
159
160 /* At the end, update the freq-counter and compute the waiting time if
161 * the stream is limited
162 */
163 update_freq_ctr_period(bytes_rate, period, ret);
164 if (ret < len) {
165 wait += next_event_delay_period(bytes_rate, period, limit, MIN(len - ret, conf->min_size * users));
166 st->exp = tick_add(now_ms, (wait ? wait : 1));
167 }
168
169 if (conf->flags & BWLIM_FL_SHARED)
170 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &st->ts->lock);
171
172 end:
173 chn->analyse_exp = tick_first((tick_is_expired(chn->analyse_exp, now_ms) ? TICK_ETERNITY : chn->analyse_exp),
174 st->exp);
175 return ret;
176}
177
178/***************************************************************************
179 * Hooks that manage the filter lifecycle (init/check/deinit)
180 **************************************************************************/
181/* Initialize the filter. Returns -1 on error, else 0. */
182static int bwlim_init(struct proxy *px, struct flt_conf *fconf)
183{
184 fconf->flags |= FLT_CFG_FL_HTX;
185 return 0;
186}
187
188/* Free resources allocated by the bwlim filter. */
189static void bwlim_deinit(struct proxy *px, struct flt_conf *fconf)
190{
191 struct bwlim_config *conf = fconf->conf;
192
193 if (conf) {
Christopher Fauletf0196f42022-06-24 14:52:18 +0200194 ha_free(&conf->name);
Christopher Faulet2b677702022-06-22 16:55:04 +0200195 release_sample_expr(conf->expr);
Christopher Fauletf0196f42022-06-24 14:52:18 +0200196 conf->expr = NULL;
197 ha_free(&fconf->conf);
Christopher Faulet2b677702022-06-22 16:55:04 +0200198 }
199}
200
201/* Check configuration of a bwlim filter for a specified proxy.
202 * Return 1 on error, else 0. */
203static int bwlim_check(struct proxy *px, struct flt_conf *fconf)
204{
205 struct bwlim_config *conf = fconf->conf;
206 struct stktable *target;
207
208 if (!(conf->flags & BWLIM_FL_SHARED))
209 return 0;
210
211 if (conf->table.n)
212 target = stktable_find_by_name(conf->table.n);
213 else
214 target = px->table;
215
216 if (!target) {
217 ha_alert("Proxy %s : unable to find table '%s' referenced by bwlim filter '%s'",
218 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
219 return 1;
220 }
221
222 if ((conf->flags & BWLIM_FL_IN) && !target->data_ofs[STKTABLE_DT_BYTES_IN_RATE]) {
223 ha_alert("Proxy %s : stick-table '%s' uses a data type incompatible with bwlim filter '%s'."
224 " It must be 'bytes_in_rate'",
225 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
226 return 1;
227 }
228 else if ((conf->flags & BWLIM_FL_OUT) && !target->data_ofs[STKTABLE_DT_BYTES_OUT_RATE]) {
229 ha_alert("Proxy %s : stick-table '%s' uses a data type incompatible with bwlim filter '%s'."
230 " It must be 'bytes_out_rate'",
231 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
232 return 1;
233 }
234
235 if (!stktable_compatible_sample(conf->expr, target->type)) {
236 ha_alert("Proxy %s : stick-table '%s' uses a key type incompatible with bwlim filter '%s'",
237 px->id, conf->table.n ? conf->table.n : px->id, conf->name);
238 return 1;
239 }
240 else {
241 if (!in_proxies_list(target->proxies_list, px)) {
242 px->next_stkt_ref = target->proxies_list;
243 target->proxies_list = px;
244 }
Christopher Fauletf0196f42022-06-24 14:52:18 +0200245 ha_free(&conf->table.n);
Christopher Faulet2b677702022-06-22 16:55:04 +0200246 conf->table.t = target;
247 }
248
249 return 0;
250}
251
252/**************************************************************************
253 * Hooks to handle start/stop of streams
254 *************************************************************************/
255/* Called when a filter instance is created and attach to a stream */
256static int bwlim_attach(struct stream *s, struct filter *filter)
257{
258 struct bwlim_state *st;
259
260 st = pool_zalloc(pool_head_bwlim_state);
261 if (!st)
262 return -1;
263 filter->ctx = st;
264 return 1;
265}
266
267/* Called when a filter instance is detach from a stream, just before its
268 * destruction */
269static void bwlim_detach(struct stream *s, struct filter *filter)
270{
271 struct bwlim_config *conf = FLT_CONF(filter);
272 struct bwlim_state *st = filter->ctx;
273 struct stktable *t = conf->table.t;
274
275 if (!st)
276 return;
277
278 if (st->ts)
279 stktable_touch_local(t, st->ts, 1);
280
281 /* release any possible compression context */
282 pool_free(pool_head_bwlim_state, st);
283 filter->ctx = NULL;
284}
285
286/**************************************************************************
287 * Hooks to filter HTTP messages
288 *************************************************************************/
289static int bwlim_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg)
290{
291 msg->chn->analyse_exp = TICK_ETERNITY;
292 return 1;
293}
294
295static int bwlim_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
296 unsigned int offset, unsigned int len)
297{
298 return bwlim_apply_limit(filter, msg->chn, len);
299}
300
301/**************************************************************************
302 * Hooks to filter TCP data
303 *************************************************************************/
304static int bwlim_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
305 unsigned int offset, unsigned int len)
306{
307 return bwlim_apply_limit(filter, chn, len);
308}
309
310/********************************************************************
311 * Functions that manage the filter initialization
312 ********************************************************************/
313struct flt_ops bwlim_ops = {
314 /* Manage bwlim filter, called for each filter declaration */
315 .init = bwlim_init,
316 .deinit = bwlim_deinit,
317 .check = bwlim_check,
318
319 /* Handle start/stop of streams */
320 .attach = bwlim_attach,
321 .detach = bwlim_detach,
322
323
324 /* Filter HTTP requests and responses */
325 .http_headers = bwlim_http_headers,
326 .http_payload = bwlim_http_payload,
327
328 /* Filter TCP data */
329 .tcp_payload = bwlim_tcp_payload,
330};
331
332/* Set a bandwidth limitation. It always return ACT_RET_CONT. On error, the rule
333 * is ignored. First of all, it looks for the corresponding filter. Then, for a
334 * shared limitation, the stick-table entry is retrieved. For a per-stream
335 * limitation, the custom limit and period are computed, if necessary. At the
336 * end, the filter is registered on the data filtering for the right channel
337 * (bwlim-in = request, bwlim-out = response).
338 */
339static enum act_return bwlim_set_limit(struct act_rule *rule, struct proxy *px,
340 struct session *sess, struct stream *s, int flags)
341{
342 struct bwlim_config *conf = rule->arg.act.p[3];
343 struct filter *filter;
344 struct bwlim_state *st = NULL;
345 struct stktable *t;
346 struct stktable_key *key;
347 struct stksess *ts;
348 int opt;
349
350 list_for_each_entry(filter, &s->strm_flt.filters, list) {
351 if (FLT_ID(filter) == bwlim_flt_id && FLT_CONF(filter) == conf) {
352 st = filter->ctx;
353 break;
354 }
355 }
356
357 if (!st)
358 goto end;
359
360 switch (rule->from) {
361 case ACT_F_TCP_REQ_CNT: opt = SMP_OPT_DIR_REQ | SMP_OPT_FINAL; break;
362 case ACT_F_TCP_RES_CNT: opt = SMP_OPT_DIR_RES | SMP_OPT_FINAL; break;
363 case ACT_F_HTTP_REQ: opt = SMP_OPT_DIR_REQ | SMP_OPT_FINAL; break;
364 case ACT_F_HTTP_RES: opt = SMP_OPT_DIR_RES | SMP_OPT_FINAL; break;
365 default:
366 goto end;
367 }
368
369 if (conf->flags & BWLIM_FL_SHARED) {
370 t = conf->table.t;
371 key = stktable_fetch_key(t, px, sess, s, opt, conf->expr, NULL);
372 if (!key)
373 goto end;
374
375 ts = stktable_get_entry(t, key);
376 if (!ts)
377 goto end;
378
379 st->ts = ts;
380 st->rule = rule;
381 }
382 else {
383 struct sample *smp;
384
385 st->limit = 0;
386 st->period = 0;
387 if (rule->arg.act.p[1]) {
388 smp = sample_fetch_as_type(px, sess, s, opt, rule->arg.act.p[1], SMP_T_SINT);
389 if (smp && smp->data.u.sint > 0)
390 st->limit = smp->data.u.sint;
391 }
392 if (rule->arg.act.p[2]) {
393 smp = sample_fetch_as_type(px, sess, s, opt, rule->arg.act.p[2], SMP_T_SINT);
394 if (smp && smp->data.u.sint > 0)
395 st->period = smp->data.u.sint;
396 }
397 }
398
399 st->exp = TICK_ETERNITY;
400 if (conf->flags & BWLIM_FL_IN)
401 register_data_filter(s, &s->req, filter);
402 else
403 register_data_filter(s, &s->res, filter);
404
405 end:
406 return ACT_RET_CONT;
407}
408
409/* Check function for "set-bandwidth-limit" aciton. It returns 1 on
410 * success. Otherwise, it returns 0 and <err> is filled.
411 */
412int check_bwlim_action(struct act_rule *rule, struct proxy *px, char **err)
413{
414 struct flt_conf *fconf;
415 struct bwlim_config *conf = NULL;
416 unsigned int where;
417
418 list_for_each_entry(fconf, &px->filter_configs, list) {
419 conf = NULL;
420 if (fconf->id == bwlim_flt_id) {
421 conf = fconf->conf;
422 if (!strcmp(rule->arg.act.p[0], conf->name))
423 break;
424 }
425 }
426 if (!conf) {
427 memprintf(err, "unable to find bwlim filter '%s' referenced by set-bandwidth-limit rule",
428 (char *)rule->arg.act.p[0]);
429 return 0;
430 }
431
432 if ((conf->flags & BWLIM_FL_SHARED) && rule->arg.act.p[1]) {
433 memprintf(err, "set-bandwidth-limit rule cannot define a limit for a shared bwlim filter");
434 return 0;
435 }
436
437 where = 0;
438 if (px->cap & PR_CAP_FE)
439 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_FE_HRS_HDR);
440 if (px->cap & PR_CAP_BE)
441 where |= (rule->from == ACT_F_HTTP_REQ ? SMP_VAL_BE_HRQ_HDR : SMP_VAL_BE_HRS_HDR);
442
443 if (rule->arg.act.p[1]) {
444 struct sample_expr *expr = rule->arg.act.p[1];
445
446 if (!(expr->fetch->val & where)) {
447 memprintf(err, "set-bandwidth-limit rule uses a limit extracting information from '%s', none of which is available here",
448 sample_src_names(expr->fetch->use));
449 return 0;
450 }
451
452 if (rule->from == ACT_F_TCP_REQ_CNT && (px->cap & PR_CAP_FE)) {
453 if (!px->tcp_req.inspect_delay && !(expr->fetch->val & SMP_VAL_FE_SES_ACC)) {
454 ha_warning("%s '%s' : a 'tcp-request content set-bandwidth-limit*' rule explicitly depending on request"
455 " contents without any 'tcp-request inspect-delay' setting."
456 " This means that this rule will randomly find its contents. This can be fixed by"
457 " setting the tcp-request inspect-delay.\n",
458 proxy_type_str(px), px->id);
459 }
460 }
461 }
462
463 if (conf->expr) {
464 if (!(conf->expr->fetch->val & where)) {
465 memprintf(err, "bwlim filter '%s uses a key extracting information from '%s', none of which is available here",
466 conf->name, sample_src_names(conf->expr->fetch->use));
467 return 0;
468 }
469
470 if (rule->from == ACT_F_TCP_REQ_CNT && (px->cap & PR_CAP_FE)) {
471 if (!px->tcp_req.inspect_delay && !(conf->expr->fetch->val & SMP_VAL_FE_SES_ACC)) {
472 ha_warning("%s '%s' : a 'tcp-request content set-bandwidth-limit*' rule explicitly depending on request"
473 " contents without any 'tcp-request inspect-delay' setting."
474 " This means that this rule will randomly find its contents. This can be fixed by"
475 " setting the tcp-request inspect-delay.\n",
476 proxy_type_str(px), px->id);
477 }
478 }
479 }
480
481 end:
482 rule->arg.act.p[3] = conf;
483 return 1;
484}
485
486/* Release memory allocated by "set-bandwidth-limit" action. */
487static void release_bwlim_action(struct act_rule *rule)
488{
Christopher Fauletf0196f42022-06-24 14:52:18 +0200489 ha_free(&rule->arg.act.p[0]);
490 if (rule->arg.act.p[1]) {
491 release_sample_expr(rule->arg.act.p[1]);
492 rule->arg.act.p[1] = NULL;
493 }
494 if (rule->arg.act.p[2]) {
495 release_sample_expr(rule->arg.act.p[2]);
496 rule->arg.act.p[2] = NULL;
497 }
498 rule->arg.act.p[3] = NULL; /* points on the filter's config */
Christopher Faulet2b677702022-06-22 16:55:04 +0200499}
500
501/* Parse "set-bandwidth-limit" action. The filter name must be specified. For
502 * shared limitations, there is no other supported parameter. For per-stream
503 * limitations, a custom limit and period may be specified. In both case, it
504 * must be an expression. On success:
505 *
506 * arg.act.p[0] will be the filter name (mandatory)
507 * arg.act.p[1] will be an expression for the custom limit (optional, may be NULL)
Ilya Shipitsin3b64a282022-07-29 22:26:53 +0500508 * arg.act.p[2] will be an expression for the custom period (optional, may be NULL)
Christopher Faulet2b677702022-06-22 16:55:04 +0200509 *
510 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
511 */
512static enum act_parse_ret parse_bandwidth_limit(const char **args, int *orig_arg, struct proxy *px,
513 struct act_rule *rule, char **err)
514{
515 struct sample_expr *expr;
516 int cur_arg;
517
518 cur_arg = *orig_arg;
519
520 if (!*args[cur_arg]) {
521 memprintf(err, "missing bwlim filter name");
522 return ACT_RET_PRS_ERR;
523 }
524
525 rule->arg.act.p[0] = strdup(args[cur_arg]);
526 if (!rule->arg.act.p[0]) {
527 memprintf(err, "out of memory");
528 return ACT_RET_PRS_ERR;
529 }
530 cur_arg++;
531
532 while (1) {
533 if (strcmp(args[cur_arg], "limit") == 0) {
534 cur_arg++;
535 if (!args[cur_arg]) {
536 memprintf(err, "missing limit expression");
537 goto error;
538 }
539
540 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
541 if (!expr)
542 goto error;
543 rule->arg.act.p[1] = expr;
544 }
545 else if (strcmp(args[cur_arg], "period") == 0) {
546 cur_arg++;
547 if (!args[cur_arg]) {
548 memprintf(err, "missing period expression");
549 goto error;
550 }
551
552 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL);
553 if (!expr)
554 goto error;
555 rule->arg.act.p[2] = expr;
556 }
557 else
558 break;
559 }
560
561 rule->action_ptr = bwlim_set_limit;
562 rule->check_ptr = check_bwlim_action;
563 rule->release_ptr = release_bwlim_action;
564
565 *orig_arg = cur_arg;
566 return ACT_RET_PRS_OK;
567
568error:
569 release_bwlim_action(rule);
570 return ACT_RET_PRS_ERR;
571}
572
573
574static struct action_kw_list tcp_req_cont_actions = {
575 .kw = {
576 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
577 { NULL, NULL }
578 }
579};
580
581static struct action_kw_list tcp_res_cont_actions = {
582 .kw = {
583 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
584 { NULL, NULL }
585 }
586};
587
588static struct action_kw_list http_req_actions = {
589 .kw = {
590 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
591 { NULL, NULL }
592 }
593};
594
595static struct action_kw_list http_res_actions = {
596 .kw = {
597 { "set-bandwidth-limit", parse_bandwidth_limit, 0 },
598 { NULL, NULL }
599 }
600};
601
602INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions);
603INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions);
604INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
605INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions);
606
607
608/* Generic function to parse bandwidth limitation filter configurartion. It
609 * Returns -1 on error and 0 on success. It handles configuration for per-stream
610 * and shared limitations.
611 */
612static int parse_bwlim_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
613 char **err, void *private)
614{
615 struct flt_conf *fc;
616 struct bwlim_config *conf;
617 int shared, per_stream;
618 int pos = *cur_arg + 1;
619
620 conf = calloc(1, sizeof(*conf));
621 if (!conf) {
622 memprintf(err, "%s: out of memory", args[*cur_arg]);
623 return -1;
624 }
625 conf->proxy = px;
626
627 if (!*args[pos]) {
628 memprintf(err, "'%s' : a name is expected as first argument ", args[*cur_arg]);
629 goto error;
630 }
631 conf->flags = BWLIM_FL_NONE;
632 conf->name = strdup(args[pos]);
633 if (!conf->name) {
634 memprintf(err, "%s: out of memory", args[*cur_arg]);
635 goto error;
636 }
637
638 list_for_each_entry(fc, &px->filter_configs, list) {
639 if (fc->id == bwlim_flt_id) {
640 struct bwlim_config *c = fc->conf;
641
642 if (!strcmp(conf->name, c->name)) {
643 memprintf(err, "bwlim filter '%s' already declared for proxy '%s'\n",
644 conf->name, px->id);
645 goto error;
646 }
647 }
648 }
649 shared = per_stream = 0;
650 pos++;
651 while (*args[pos]) {
652 if (strcmp(args[pos], "key") == 0) {
653 if (per_stream) {
654 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
655 args[*cur_arg]);
656 goto error;
657 }
658 if (!*args[pos + 1]) {
659 memprintf(err, "'%s' : the sample expression is missing for '%s' option",
660 args[*cur_arg], args[pos]);
661 goto error;
662 }
663 shared = 1;
664 pos++;
665 conf->expr = sample_parse_expr((char **)args, &pos, px->conf.args.file, px->conf.args.line,
666 err, &px->conf.args, NULL);
667 if (!conf->expr)
668 goto error;
669 }
670 else if (strcmp(args[pos], "table") == 0) {
671 if (per_stream) {
672 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
673 args[*cur_arg]);
674 goto error;
675 }
676 if (!*args[pos + 1]) {
677 memprintf(err, "'%s' : the table name is missing for '%s' option",
678 args[*cur_arg], args[pos]);
679 goto error;
680 }
681 shared = 1;
682 conf->table.n = strdup(args[pos + 1]);
683 if (!conf->table.n) {
684 memprintf(err, "%s: out of memory", args[*cur_arg]);
685 goto error;
686 }
687 pos += 2;
688 }
689 else if (strcmp(args[pos], "default-period") == 0) {
690 const char *res;
691
692 if (shared) {
693 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
694 args[*cur_arg]);
695 goto error;
696 }
697 if (!*args[pos + 1]) {
698 memprintf(err, "'%s' : the value is missing for '%s' option",
699 args[*cur_arg], args[pos]);
700 goto error;
701 }
702 per_stream = 1;
703 res = parse_time_err(args[pos + 1], &conf->period, TIME_UNIT_MS);
704 if (res) {
705 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
706 args[*cur_arg], args[pos], *res);
707 goto error;
708 }
709 pos += 2;
710 }
711 else if (strcmp(args[pos], "limit") == 0) {
712 const char *res;
713
714 if (per_stream) {
715 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
716 args[*cur_arg]);
717 goto error;
718 }
719 if (!*args[pos + 1]) {
720 memprintf(err, "'%s' : the value is missing for '%s' option",
721 args[*cur_arg], args[pos]);
722 goto error;
723 }
724 shared = 1;
725 res = parse_size_err(args[pos + 1], &conf->limit);
726 if (res) {
727 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
728 args[*cur_arg], args[pos], *res);
729 goto error;
730 }
731 pos += 2;
732 }
733 else if (strcmp(args[pos], "default-limit") == 0) {
734 const char *res;
735
736 if (shared) {
737 memprintf(err, "'%s' : cannot mix per-stream and shared parameter",
738 args[*cur_arg]);
739 goto error;
740 }
741 if (!*args[pos + 1]) {
742 memprintf(err, "'%s' : the value is missing for '%s' option",
743 args[*cur_arg], args[pos]);
744 goto error;
745 }
746 per_stream = 1;
747 res = parse_size_err(args[pos + 1], &conf->limit);
748 if (res) {
749 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
750 args[*cur_arg], args[pos], *res);
751 goto error;
752 }
753 pos += 2;
754 }
755 else if (strcmp(args[pos], "min-size") == 0) {
756 const char *res;
757
758 if (!*args[pos + 1]) {
759 memprintf(err, "'%s' : the value is missing for '%s' option",
760 args[*cur_arg], args[pos]);
761 goto error;
762 }
763 res = parse_size_err(args[pos + 1], &conf->min_size);
764 if (res) {
765 memprintf(err, "'%s' : invalid value for option '%s' (unexpected character '%c')",
766 args[*cur_arg], args[pos], *res);
767 goto error;
768 }
769 pos += 2;
770 }
771 else
772 break;
773 }
774
775 if (shared) {
776 conf->flags |= BWLIM_FL_SHARED;
777 if (!conf->expr) {
778 memprintf(err, "'%s' : <key> option is missing", args[*cur_arg]);
779 goto error;
780 }
781 if (!conf->limit) {
782 memprintf(err, "'%s' : <limit> option is missing", args[*cur_arg]);
783 goto error;
784 }
785 }
786 else {
787 /* Per-stream: limit downloads only for now */
788 conf->flags |= BWLIM_FL_OUT;
789 if (!conf->period) {
790 memprintf(err, "'%s' : <default-period> option is missing", args[*cur_arg]);
791 goto error;
792 }
793 if (!conf->limit) {
794 memprintf(err, "'%s' : <default-limit> option is missing", args[*cur_arg]);
795 goto error;
796 }
797 }
798
799 *cur_arg = pos;
800 fconf->id = bwlim_flt_id;
801 fconf->ops = &bwlim_ops;
802 fconf->conf = conf;
803 return 0;
804
805 error:
806 if (conf->name)
Christopher Fauletf0196f42022-06-24 14:52:18 +0200807 ha_free(&conf->name);
808 if (conf->expr) {
Christopher Faulet2b677702022-06-22 16:55:04 +0200809 release_sample_expr(conf->expr);
Christopher Fauletf0196f42022-06-24 14:52:18 +0200810 conf->expr = NULL;
811 }
Christopher Faulet2b677702022-06-22 16:55:04 +0200812 if (conf->table.n)
Christopher Fauletf0196f42022-06-24 14:52:18 +0200813 ha_free(&conf->table.n);
Christopher Faulet2b677702022-06-22 16:55:04 +0200814 free(conf);
815 return -1;
816}
817
818
819static int parse_bwlim_in_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
820 char **err, void *private)
821{
822 int ret;
823
824 ret = parse_bwlim_flt(args, cur_arg, px, fconf, err, private);
825 if (!ret) {
826 struct bwlim_config *conf = fconf->conf;
827
828 conf->flags |= BWLIM_FL_IN;
829 }
830
831 return ret;
832}
833
834static int parse_bwlim_out_flt(char **args, int *cur_arg, struct proxy *px, struct flt_conf *fconf,
835 char **err, void *private)
836{
837 int ret;
838
839 ret = parse_bwlim_flt(args, cur_arg, px, fconf, err, private);
840 if (!ret) {
841 struct bwlim_config *conf = fconf->conf;
842
843 conf->flags |= BWLIM_FL_OUT;
844 }
845 return ret;
846}
847
848/* Declare the filter parser for "trace" keyword */
849static struct flt_kw_list flt_kws = { "BWLIM", { }, {
850 { "bwlim-in", parse_bwlim_in_flt, NULL },
851 { "bwlim-out", parse_bwlim_out_flt, NULL },
852 { NULL, NULL, NULL },
853 }
854};
855
856INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);