blob: a4863ae1944d10389dcb5fef79141890ee6e0af1 [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * Stick tables management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02005 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01006 *
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 <string.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010015#include <errno.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010016
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <import/ebmbtree.h>
18#include <import/ebsttree.h>
19#include <import/ebistree.h>
20
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreau3c69e082022-05-03 11:35:07 +020022#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Thayne McCombs92149f92020-11-20 01:28:26 -070026#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020029#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020030#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/pool.h>
35#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020036#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020038#include <haproxy/sc_strm.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020039#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020040#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020044#include <haproxy/tcp_rules.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020045#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/tools.h>
Willy Tarreaua4728582022-11-24 07:35:17 +010047#include <haproxy/xxhash.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010048
Emeric Brun3bd697e2010-01-04 15:23:48 +010049
Willy Tarreau12785782012-04-27 21:37:17 +020050/* structure used to return a table key built from a sample */
Emeric Brun819fc6f2017-06-13 19:37:32 +020051static THREAD_LOCAL struct stktable_key static_table_key;
Willy Tarreau478331d2020-08-28 11:31:31 +020052static int (*smp_fetch_src)(const struct arg *, struct sample *, const char *, void *);
Willy Tarreau6c011712023-01-06 16:09:58 +010053struct pool_head *pool_head_stk_ctr __read_mostly = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010054struct stktable *stktables_list;
55struct eb_root stktable_by_name = EB_ROOT;
56
Olivier Houchard52dabbc2018-11-14 17:54:36 +010057#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010058
59/* This function inserts stktable <t> into the tree of known stick-table.
60 * The stick-table ID is used as the storing key so it must already have
61 * been initialized.
62 */
63void stktable_store_name(struct stktable *t)
64{
65 t->name.key = t->id;
66 ebis_insert(&stktable_by_name, &t->name);
67}
68
69struct stktable *stktable_find_by_name(const char *name)
70{
71 struct ebpt_node *node;
72 struct stktable *t;
73
74 node = ebis_lookup(&stktable_by_name, name);
75 if (node) {
76 t = container_of(node, struct stktable, name);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +010077 if (strcmp(t->id, name) == 0)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010078 return t;
79 }
80
81 return NULL;
82}
83
Emeric Brun3bd697e2010-01-04 15:23:48 +010084/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020085 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
Willy Tarreau996f1a52022-10-11 16:19:35 +020086 * in table <t>. It's safe to call it under or out of a lock.
Emeric Brun3bd697e2010-01-04 15:23:48 +010087 */
Emeric Brun819fc6f2017-06-13 19:37:32 +020088void __stksess_free(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010089{
Willy Tarreau996f1a52022-10-11 16:19:35 +020090 HA_ATOMIC_DEC(&t->current);
Olivier Houchard52dabbc2018-11-14 17:54:36 +010091 pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
Emeric Brun3bd697e2010-01-04 15:23:48 +010092}
93
94/*
Emeric Brun819fc6f2017-06-13 19:37:32 +020095 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
96 * in table <t>.
97 * This function locks the table
98 */
99void stksess_free(struct stktable *t, struct stksess *ts)
100{
Thayne McCombs92149f92020-11-20 01:28:26 -0700101 void *data;
102 data = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_KEY);
103 if (data) {
Emeric Brun0e3457b2021-06-30 17:18:28 +0200104 dict_entry_unref(&server_key_dict, stktable_data_cast(data, std_t_dict));
105 stktable_data_cast(data, std_t_dict) = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -0700106 }
Willy Tarreau8d3c3332022-10-11 18:50:22 +0000107 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200108 __stksess_free(t, ts);
Willy Tarreau8d3c3332022-10-11 18:50:22 +0000109 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200110}
111
112/*
Willy Tarreauf6efda12010-08-03 20:34:06 +0200113 * Kill an stksess (only if its ref_cnt is zero).
114 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200115int __stksess_kill(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200116{
117 if (ts->ref_cnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200118 return 0;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200119
120 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +0200121 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200122 ebmb_delete(&ts->key);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200123 __stksess_free(t, ts);
124 return 1;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200125}
126
127/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200128 * Decrease the refcount if decrefcnt is not 0.
129 * and try to kill the stksess
130 * This function locks the table
131 */
132int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcnt)
133{
134 int ret;
135
Willy Tarreau76642222022-10-11 12:02:50 +0200136 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200137 if (decrefcnt)
138 ts->ref_cnt--;
139 ret = __stksess_kill(t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200140 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200141
142 return ret;
143}
144
145/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200146 * Initialize or update the key in the sticky session <ts> present in table <t>
147 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100148 */
Willy Tarreau393379c2010-06-06 12:11:37 +0200149void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100150{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200151 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200152 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +0200154 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
155 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100156 }
157}
158
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100159/* return a shard number for key <key> of len <len> present in table <t>. This
160 * takes into account the presence or absence of a peers section with shards
161 * and the number of shards, the table's hash_seed, and of course the key. The
162 * caller must pass a valid <key> and <len>. The shard number to be used by the
163 * entry is returned (from 1 to nb_shards, otherwise 0 for none).
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200164 */
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100165int stktable_get_key_shard(struct stktable *t, const void *key, size_t len)
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200166{
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100167 /* no peers section or no shards in the peers section */
168 if (!t->peers.p || !t->peers.p->nb_shards)
169 return 0;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200170
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100171 return XXH64(key, len, t->hash_seed) % t->peers.p->nb_shards + 1;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200172}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100173
174/*
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200175 * Set the shard for <key> key of <ts> sticky session attached to <t> stick table.
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100176 * Use zero for stick-table without peers synchronisation.
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200177 */
178static void stksess_setkey_shard(struct stktable *t, struct stksess *ts,
179 struct stktable_key *key)
180{
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100181 size_t keylen;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200182
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100183 if (t->type == SMP_T_STR)
184 keylen = key->key_len;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200185 else
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100186 keylen = t->key_size;
187
188 ts->shard = stktable_get_key_shard(t, key->key, keylen);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200189}
190
191/*
Willy Tarreau393379c2010-06-06 12:11:37 +0200192 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
193 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200195static struct stksess *__stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196{
Willy Tarreau393379c2010-06-06 12:11:37 +0200197 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200198 ts->ref_cnt = 0;
Willy Tarreaue548a7a2022-11-29 16:08:35 +0100199 ts->shard = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200200 ts->key.node.leaf_p = NULL;
201 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200202 ts->upd.node.leaf_p = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200203 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100204 HA_RWLOCK_INIT(&ts->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100205 return ts;
206}
207
208/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200209 * Trash oldest <to_batch> sticky sessions from table <t>
Willy Tarreaudfe79252020-11-03 17:47:41 +0100210 * Returns number of trashed sticky sessions. It may actually trash less
211 * than expected if finding these requires too long a search time (e.g.
212 * most of them have ts->ref_cnt>0).
Emeric Brun3bd697e2010-01-04 15:23:48 +0100213 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200214int __stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100215{
216 struct stksess *ts;
217 struct eb32_node *eb;
Willy Tarreaudfe79252020-11-03 17:47:41 +0100218 int max_search = to_batch * 2; // no more than 50% misses
Emeric Brun3bd697e2010-01-04 15:23:48 +0100219 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200220 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221
222 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
223
224 while (batched < to_batch) {
225
226 if (unlikely(!eb)) {
227 /* we might have reached the end of the tree, typically because
228 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200229 * half. Let's loop back to the beginning of the tree now if we
230 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200232 if (looped)
233 break;
234 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 eb = eb32_first(&t->exps);
236 if (likely(!eb))
237 break;
238 }
239
Willy Tarreaudfe79252020-11-03 17:47:41 +0100240 if (--max_search < 0)
241 break;
242
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200244 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100245 eb = eb32_next(eb);
246
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200247 /* don't delete an entry which is currently referenced */
248 if (ts->ref_cnt)
249 continue;
250
Willy Tarreau86257dc2010-06-06 12:57:10 +0200251 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100252
Willy Tarreau86257dc2010-06-06 12:57:10 +0200253 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100254 if (!tick_isset(ts->expire))
255 continue;
256
Willy Tarreau86257dc2010-06-06 12:57:10 +0200257 ts->exp.key = ts->expire;
258 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100259
Aleksey Ponomaryov59380212023-02-07 19:27:06 +0100260 /* the update might have jumped beyond the next element,
261 * possibly causing a wrapping. We need to check whether
262 * the next element should be used instead. If the next
263 * element doesn't exist it means we're on the right
264 * side and have to check the first one then. If it
265 * exists and is closer, we must use it, otherwise we
266 * use the current one.
267 */
268 if (!eb)
269 eb = eb32_first(&t->exps);
270
271 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200272 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100273
274 continue;
275 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100276
Willy Tarreauaea940e2010-06-06 11:56:36 +0200277 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200278 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200279 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200280 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100281 batched++;
282 }
283
284 return batched;
285}
286
287/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200288 * Trash oldest <to_batch> sticky sessions from table <t>
289 * Returns number of trashed sticky sessions.
290 * This function locks the table
291 */
292int stktable_trash_oldest(struct stktable *t, int to_batch)
293{
294 int ret;
295
Willy Tarreau76642222022-10-11 12:02:50 +0200296 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200297 ret = __stktable_trash_oldest(t, to_batch);
Willy Tarreau76642222022-10-11 12:02:50 +0200298 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200299
300 return ret;
301}
302/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200303 * Allocate and initialise a new sticky session.
304 * The new sticky session is returned or NULL in case of lack of memory.
305 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200306 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
Willy Tarreau996f1a52022-10-11 16:19:35 +0200307 * is not NULL, it is assigned to the new session. It must be called unlocked
308 * as it may rely on a lock to trash older entries.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100309 */
Willy Tarreau996f1a52022-10-11 16:19:35 +0200310struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100311{
312 struct stksess *ts;
Willy Tarreau996f1a52022-10-11 16:19:35 +0200313 unsigned int current;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314
Willy Tarreau996f1a52022-10-11 16:19:35 +0200315 current = HA_ATOMIC_FETCH_ADD(&t->current, 1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100316
Willy Tarreau996f1a52022-10-11 16:19:35 +0200317 if (unlikely(current >= t->size)) {
318 /* the table was already full, we may have to purge entries */
319 if (t->nopurge || !stktable_trash_oldest(t, (t->size >> 8) + 1)) {
320 HA_ATOMIC_DEC(&t->current);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100321 return NULL;
Willy Tarreau996f1a52022-10-11 16:19:35 +0200322 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 }
324
Willy Tarreaubafbe012017-11-24 17:34:44 +0100325 ts = pool_alloc(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100326 if (ts) {
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100327 ts = (void *)ts + round_ptr_size(t->data_size);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200328 __stksess_init(t, ts);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200329 if (key) {
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200330 stksess_setkey(t, ts, key);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200331 stksess_setkey_shard(t, ts, key);
332 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100333 }
334
335 return ts;
336}
337
338/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200339 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200340 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100341 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200342struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100343{
344 struct ebmb_node *eb;
345
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200346 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200347 eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348 else
349 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
350
351 if (unlikely(!eb)) {
352 /* no session found */
353 return NULL;
354 }
355
Willy Tarreau86257dc2010-06-06 12:57:10 +0200356 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357}
358
Emeric Brun819fc6f2017-06-13 19:37:32 +0200359/*
360 * Looks in table <t> for a sticky session matching key <key>.
361 * Returns pointer on requested sticky session or NULL if none was found.
362 * The refcount of the found entry is increased and this function
363 * is protected using the table lock
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200364 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200365struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200366{
367 struct stksess *ts;
368
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200369 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200370 ts = __stktable_lookup_key(t, key);
371 if (ts)
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200372 HA_ATOMIC_INC(&ts->ref_cnt);
373 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200374
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200375 return ts;
376}
377
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200378/*
379 * Looks in table <t> for a sticky session with same key as <ts>.
380 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100381 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200382struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100384 struct ebmb_node *eb;
385
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200386 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200387 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100388 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200389 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200391 if (unlikely(!eb))
392 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200394 return ebmb_entry(eb, struct stksess, key);
395}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100396
Emeric Brun819fc6f2017-06-13 19:37:32 +0200397/*
398 * Looks in table <t> for a sticky session with same key as <ts>.
399 * Returns pointer on requested sticky session or NULL if none was found.
400 * The refcount of the found entry is increased and this function
401 * is protected using the table lock
402 */
403struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
404{
405 struct stksess *lts;
406
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200407 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200408 lts = __stktable_lookup(t, ts);
409 if (lts)
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200410 HA_ATOMIC_INC(&lts->ref_cnt);
411 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200412
413 return lts;
414}
415
Willy Tarreaucb183642010-06-06 17:58:34 +0200416/* Update the expiration timer for <ts> but do not touch its expiration node.
417 * The table's expiration timer is updated if set.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200418 * The node will be also inserted into the update tree if needed, at a position
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000419 * depending if the update is a local or coming from a remote node.
420 * If <decrefcnt> is set, the ts entry's ref_cnt will be decremented. The table's
421 * write lock may be taken.
Willy Tarreaucb183642010-06-06 17:58:34 +0200422 */
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000423void stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire, int decrefcnt)
Willy Tarreaucb183642010-06-06 17:58:34 +0200424{
Emeric Brun85e77c72010-09-23 18:16:52 +0200425 struct eb32_node * eb;
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000426 int locked = 0;
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000427
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000428 if (expire != HA_ATOMIC_LOAD(&ts->expire)) {
429 /* we'll need to set the expiration and to wake up the expiration timer .*/
430 HA_ATOMIC_STORE(&ts->expire, expire);
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000431 stktable_requeue_exp(t, ts);
Willy Tarreaucb183642010-06-06 17:58:34 +0200432 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200433
Emeric Brun819fc6f2017-06-13 19:37:32 +0200434 /* If sync is enabled */
435 if (t->sync_task) {
436 if (local) {
437 /* If this entry is not in the tree
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000438 * or not scheduled for at least one peer.
439 */
440 if (!locked++)
441 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
442
Emeric Brun819fc6f2017-06-13 19:37:32 +0200443 if (!ts->upd.node.leaf_p
444 || (int)(t->commitupdate - ts->upd.key) >= 0
445 || (int)(ts->upd.key - t->localupdate) >= 0) {
446 ts->upd.key = ++t->update;
447 t->localupdate = t->update;
448 eb32_delete(&ts->upd);
449 eb = eb32_insert(&t->updates, &ts->upd);
450 if (eb != &ts->upd) {
451 eb32_delete(eb);
452 eb32_insert(&t->updates, &ts->upd);
453 }
Emeric Brunaaf58602015-06-15 17:23:30 +0200454 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200455 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
Emeric Brun85e77c72010-09-23 18:16:52 +0200456 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200457 else {
458 /* If this entry is not in the tree */
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000459 if (!locked++)
460 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
461
Emeric Brun819fc6f2017-06-13 19:37:32 +0200462 if (!ts->upd.node.leaf_p) {
463 ts->upd.key= (++t->update)+(2147483648U);
464 eb = eb32_insert(&t->updates, &ts->upd);
465 if (eb != &ts->upd) {
466 eb32_delete(eb);
467 eb32_insert(&t->updates, &ts->upd);
468 }
469 }
470 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200471 }
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000472
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000473 if (decrefcnt) {
474 if (locked)
475 ts->ref_cnt--;
476 else {
477 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
478 HA_ATOMIC_DEC(&ts->ref_cnt);
479 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
480 }
481 }
482
483 if (locked)
484 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreaucb183642010-06-06 17:58:34 +0200485}
486
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200487/* Update the expiration timer for <ts> but do not touch its expiration node.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200488 * The table's expiration timer is updated using the date of expiration coming from
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200489 * <t> stick-table configuration.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200490 * The node will be also inserted into the update tree if needed, at a position
491 * considering the update is coming from a remote node
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200492 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200493void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
494{
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000495 stktable_touch_with_exp(t, ts, 0, ts->expire, decrefcnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200496}
497
498/* Update the expiration timer for <ts> but do not touch its expiration node.
499 * The table's expiration timer is updated using the date of expiration coming from
500 * <t> stick-table configuration.
501 * The node will be also inserted into the update tree if needed, at a position
502 * considering the update was made locally
503 */
504void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200505{
506 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
507
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000508 stktable_touch_with_exp(t, ts, 1, expire, decrefcnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200509}
Willy Tarreau4be073b2022-10-11 18:10:27 +0000510/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL.
511 * Note that we still need to take the read lock because a number of other places
512 * (including in Lua and peers) update the ref_cnt non-atomically under the write
513 * lock.
514 */
Willy Tarreau43e90352018-06-27 06:25:57 +0200515static void stktable_release(struct stktable *t, struct stksess *ts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200516{
Willy Tarreau43e90352018-06-27 06:25:57 +0200517 if (!ts)
518 return;
Willy Tarreau4be073b2022-10-11 18:10:27 +0000519 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
520 HA_ATOMIC_DEC(&ts->ref_cnt);
521 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200522}
523
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200524/* Insert new sticky session <ts> in the table. It is assumed that it does not
525 * yet exist (the caller must check this). The table's timeout is updated if it
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200526 * is set. <ts> is returned if properly inserted, otherwise the one already
527 * present if any.
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200528 */
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200529struct stksess *__stktable_store(struct stktable *t, struct stksess *ts)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200530{
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200531 struct ebmb_node *eb;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100532
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200533 eb = ebmb_insert(&t->keys, &ts->key, t->key_size);
534 if (likely(eb == &ts->key)) {
535 ts->exp.key = ts->expire;
536 eb32_insert(&t->exps, &ts->exp);
537 }
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200538 return ebmb_entry(eb, struct stksess, key); // most commonly this is <ts>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200539}
540
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000541/* requeues the table's expiration task to take the recently added <ts> into
542 * account. This is performed atomically and doesn't require any lock.
543 */
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000544void stktable_requeue_exp(struct stktable *t, const struct stksess *ts)
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000545{
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000546 int old_exp, new_exp;
547 int expire = ts->expire;
548
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000549 if (!t->expire)
550 return;
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000551
Willy Tarreau63427142022-11-14 17:33:02 +0100552 /* set the task's expire to the newest expiration date. */
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000553 old_exp = HA_ATOMIC_LOAD(&t->exp_task->expire);
Willy Tarreau3238f792022-11-14 17:54:07 +0100554 new_exp = tick_first(expire, old_exp);
555
556 /* let's not go further if we're already up to date */
557 if (new_exp == old_exp)
558 return;
559
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100560 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
561
Willy Tarreau3238f792022-11-14 17:54:07 +0100562 while (new_exp != old_exp &&
563 !HA_ATOMIC_CAS(&t->exp_task->expire, &old_exp, new_exp)) {
564 __ha_cpu_relax();
Willy Tarreau63427142022-11-14 17:33:02 +0100565 new_exp = tick_first(expire, old_exp);
Willy Tarreau3238f792022-11-14 17:54:07 +0100566 }
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000567
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000568 task_queue(t->exp_task);
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100569
570 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000571}
572
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200573/* Returns a valid or initialized stksess for the specified stktable_key in the
574 * specified table, or NULL if the key was NULL, or if no entry was found nor
Willy Tarreau47f22972022-10-11 15:22:42 +0200575 * could be created. The entry's expiration is updated. This function locks the
576 * table, and the refcount of the entry is increased.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200577 */
Willy Tarreau47f22972022-10-11 15:22:42 +0200578struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200579{
Willy Tarreau175aa062022-10-11 15:13:46 +0200580 struct stksess *ts, *ts2;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200581
582 if (!key)
583 return NULL;
584
Willy Tarreau47f22972022-10-11 15:22:42 +0200585 ts = stktable_lookup_key(table, key);
586 if (ts)
587 return ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200588
Willy Tarreau996f1a52022-10-11 16:19:35 +0200589 /* No such entry exists, let's try to create a new one. this doesn't
590 * require locking yet.
591 */
592
593 ts = stksess_new(table, key);
594 if (!ts)
595 return NULL;
596
597 /* Now we're certain to have a ts. We need to store it. For this we'll
Willy Tarreau47f22972022-10-11 15:22:42 +0200598 * need an exclusive access. We don't need an atomic upgrade, this is
599 * rare and an unlock+lock sequence will do the job fine. Given that
600 * this will not be atomic, the missing entry might appear in the mean
601 * tome so we have to be careful that the one we try to insert is the
602 * one we find.
603 */
Willy Tarreau47f22972022-10-11 15:22:42 +0200604
Willy Tarreau996f1a52022-10-11 16:19:35 +0200605 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreau47f22972022-10-11 15:22:42 +0200606
607 ts2 = __stktable_store(table, ts);
608 if (unlikely(ts2 != ts)) {
609 /* another entry was added in the mean time, let's
610 * switch to it.
611 */
612 __stksess_free(table, ts);
613 ts = ts2;
614 }
615
616 HA_ATOMIC_INC(&ts->ref_cnt);
Willy Tarreau76642222022-10-11 12:02:50 +0200617 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200618
Willy Tarreaucbdb5282022-10-12 10:04:01 +0000619 stktable_requeue_exp(table, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200620 return ts;
621}
622
623/* Lookup for an entry with the same key and store the submitted
Willy Tarreaue6288522022-10-12 09:13:14 +0000624 * stksess if not found. This function locks the table either shared or
625 * exclusively, and the refcount of the entry is increased.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200626 */
Willy Tarreaue6288522022-10-12 09:13:14 +0000627struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200628{
629 struct stksess *ts;
630
Willy Tarreaue6288522022-10-12 09:13:14 +0000631 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200632 ts = __stktable_lookup(table, nts);
Willy Tarreaue6288522022-10-12 09:13:14 +0000633 if (ts) {
634 HA_ATOMIC_INC(&ts->ref_cnt);
635 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &table->lock);
636 return ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200637 }
Willy Tarreaue6288522022-10-12 09:13:14 +0000638 ts = nts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200639
Willy Tarreaue6288522022-10-12 09:13:14 +0000640 /* let's increment it before switching to exclusive */
641 HA_ATOMIC_INC(&ts->ref_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200642
Willy Tarreaue6288522022-10-12 09:13:14 +0000643 if (HA_RWLOCK_TRYRDTOSK(STK_TABLE_LOCK, &table->lock) != 0) {
644 /* upgrade to seek lock failed, let's drop and take */
645 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &table->lock);
646 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &table->lock);
647 }
648 else
649 HA_RWLOCK_SKTOWR(STK_TABLE_LOCK, &table->lock);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200650
Willy Tarreaue6288522022-10-12 09:13:14 +0000651 /* now we're write-locked */
652
653 __stktable_store(table, ts);
654 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreaucbdb5282022-10-12 10:04:01 +0000655
656 stktable_requeue_exp(table, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200657 return ts;
658}
Willy Tarreaue6288522022-10-12 09:13:14 +0000659
Emeric Brun3bd697e2010-01-04 15:23:48 +0100660/*
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100661 * Task processing function to trash expired sticky sessions. A pointer to the
662 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100663 */
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100664struct task *process_table_expire(struct task *task, void *context, unsigned int state)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100665{
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100666 struct stktable *t = context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100667 struct stksess *ts;
668 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200669 int looped = 0;
Willy Tarreau63427142022-11-14 17:33:02 +0100670 int exp_next;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100671
Willy Tarreau76642222022-10-11 12:02:50 +0200672 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100673 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
674
675 while (1) {
676 if (unlikely(!eb)) {
677 /* we might have reached the end of the tree, typically because
678 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200679 * half. Let's loop back to the beginning of the tree now if we
680 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100681 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200682 if (looped)
683 break;
684 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100685 eb = eb32_first(&t->exps);
686 if (likely(!eb))
687 break;
688 }
689
690 if (likely(tick_is_lt(now_ms, eb->key))) {
691 /* timer not expired yet, revisit it later */
Willy Tarreau63427142022-11-14 17:33:02 +0100692 exp_next = eb->key;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100693 goto out_unlock;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100694 }
695
696 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200697 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100698 eb = eb32_next(eb);
699
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200700 /* don't delete an entry which is currently referenced */
701 if (ts->ref_cnt)
702 continue;
703
Willy Tarreau86257dc2010-06-06 12:57:10 +0200704 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100705
706 if (!tick_is_expired(ts->expire, now_ms)) {
707 if (!tick_isset(ts->expire))
708 continue;
709
Willy Tarreau86257dc2010-06-06 12:57:10 +0200710 ts->exp.key = ts->expire;
711 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100712
Aleksey Ponomaryov59380212023-02-07 19:27:06 +0100713 /* the update might have jumped beyond the next element,
714 * possibly causing a wrapping. We need to check whether
715 * the next element should be used instead. If the next
716 * element doesn't exist it means we're on the right
717 * side and have to check the first one then. If it
718 * exists and is closer, we must use it, otherwise we
719 * use the current one.
720 */
721 if (!eb)
722 eb = eb32_first(&t->exps);
723
724 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200725 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100726 continue;
727 }
728
729 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200730 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200731 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200732 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100733 }
734
735 /* We have found no task to expire in any tree */
Willy Tarreau63427142022-11-14 17:33:02 +0100736 exp_next = TICK_ETERNITY;
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100737
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100738out_unlock:
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100739 task->expire = exp_next;
Willy Tarreau76642222022-10-11 12:02:50 +0200740 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100741 return task;
742}
743
Willy Tarreauaea940e2010-06-06 11:56:36 +0200744/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100745int stktable_init(struct stktable *t)
746{
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200747 int peers_retval = 0;
Willy Tarreau56460ee2022-11-28 18:53:06 +0100748
749 t->hash_seed = XXH64(t->id, t->idlen, 0);
750
Emeric Brun3bd697e2010-01-04 15:23:48 +0100751 if (t->size) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200752 t->keys = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100753 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100754 t->updates = EB_ROOT_UNIQUE;
Amaury Denoyelle3e064882022-10-12 16:47:59 +0200755 HA_RWLOCK_INIT(&t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100756
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100757 t->pool = create_pool("sticktables", sizeof(struct stksess) + round_ptr_size(t->data_size) + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100758
Emeric Brun3bd697e2010-01-04 15:23:48 +0100759 if ( t->expire ) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200760 t->exp_task = task_new_anywhere();
Willy Tarreau848522f2018-10-15 11:12:15 +0200761 if (!t->exp_task)
762 return 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100763 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100764 t->exp_task->context = (void *)t;
765 }
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200766 if (t->peers.p && t->peers.p->peers_fe && !(t->peers.p->peers_fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200767 peers_retval = peers_register_table(t->peers.p, t);
Emeric Brun32da3c42010-09-23 18:39:19 +0200768 }
769
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200770 return (t->pool != NULL) && !peers_retval;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100771 }
772 return 1;
773}
774
Aurelien DARRAGON6ce5cd02023-11-16 16:17:12 +0100775/* Performs stick table cleanup: it's meant to be called after the table
776 * has been initialized ith stktable_init(), else it will lead to undefined
777 * behavior.
778 *
779 * However it does not free the table pointer itself
780 */
781void stktable_deinit(struct stktable *t)
782{
783 if (!t)
784 return;
Aurelien DARRAGONfebaa3e2023-11-16 16:18:14 +0100785 task_destroy(t->exp_task);
Aurelien DARRAGON6ce5cd02023-11-16 16:17:12 +0100786 pool_destroy(t->pool);
787}
788
Emeric Brun3bd697e2010-01-04 15:23:48 +0100789/*
790 * Configuration keywords of known table types
791 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200792struct stktable_type stktable_types[SMP_TYPES] = {
793 [SMP_T_SINT] = { "integer", 0, 4 },
794 [SMP_T_IPV4] = { "ip", 0, 4 },
795 [SMP_T_IPV6] = { "ipv6", 0, 16 },
796 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
797 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
798};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100799
800/*
801 * Parse table type configuration.
802 * Returns 0 on successful parsing, else 1.
803 * <myidx> is set at next configuration <args> index.
804 */
William Lallemand3f210972023-04-13 14:33:52 +0200805int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size, const char *file, int linenum)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100806{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200807 for (*type = 0; *type < SMP_TYPES; (*type)++) {
808 if (!stktable_types[*type].kw)
809 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100810 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
811 continue;
812
813 *key_size = stktable_types[*type].default_size;
814 (*myidx)++;
815
Willy Tarreauaea940e2010-06-06 11:56:36 +0200816 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100817 if (strcmp("len", args[*myidx]) == 0) {
William Lallemand3f210972023-04-13 14:33:52 +0200818 char *stop;
819
Emeric Brun3bd697e2010-01-04 15:23:48 +0100820 (*myidx)++;
William Lallemand3f210972023-04-13 14:33:52 +0200821 *key_size = strtol(args[*myidx], &stop, 10);
822 if (*stop != '\0' || !*key_size) {
823 ha_alert("parsing [%s:%d] : 'len' expects a positive integer argument.\n", file, linenum);
824 return 1;
825 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200826 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200827 /* null terminated string needs +1 for '\0'. */
828 (*key_size)++;
829 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100830 (*myidx)++;
831 }
832 }
833 return 0;
834 }
William Lallemand3f210972023-04-13 14:33:52 +0200835 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", file, linenum, args[0], args[*myidx]);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100836 return 1;
837}
838
Emeric Brunc64a2a32021-06-30 18:01:02 +0200839/* reserve some space for data type <type>, there is 2 optionnals
840 * argument at <sa> and <sa2> to configure this data type and
841 * they can be NULL if unused for a given type.
842 * Returns PE_NONE (0) if OK or an error code among :
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200843 * - PE_ENUM_OOR if <type> does not exist
844 * - PE_EXIST if <type> is already registered
Emeric Brunc64a2a32021-06-30 18:01:02 +0200845 * - PE_ARG_NOT_USE if <sa>/<sa2> was provided but not expected
846 * - PE_ARG_MISSING if <sa>/<sa2> was expected but not provided
847 * - PE_ARG_VALUE_OOR if type is an array and <sa> it out of array size range.
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200848 */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200849int stktable_alloc_data_type(struct stktable *t, int type, const char *sa, const char *sa2)
850
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200851{
852 if (type >= STKTABLE_DATA_TYPES)
853 return PE_ENUM_OOR;
854
855 if (t->data_ofs[type])
856 /* already allocated */
857 return PE_EXIST;
858
Emeric Brunc64a2a32021-06-30 18:01:02 +0200859 t->data_nbelem[type] = 1;
860 if (stktable_data_types[type].is_array) {
861 /* arrays take their element count on first argument */
862 if (!sa)
863 return PE_ARG_MISSING;
864 t->data_nbelem[type] = atoi(sa);
865 if (!t->data_nbelem[type] || (t->data_nbelem[type] > STKTABLE_MAX_DT_ARRAY_SIZE))
866 return PE_ARG_VALUE_OOR;
867 sa = sa2;
868 }
869
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200870 switch (stktable_data_types[type].arg_type) {
871 case ARG_T_NONE:
872 if (sa)
873 return PE_ARG_NOT_USED;
874 break;
875 case ARG_T_INT:
876 if (!sa)
877 return PE_ARG_MISSING;
878 t->data_arg[type].i = atoi(sa);
879 break;
880 case ARG_T_DELAY:
881 if (!sa)
882 return PE_ARG_MISSING;
883 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
884 if (sa)
885 return PE_ARG_INVC; /* invalid char */
886 break;
887 }
888
Emeric Brunc64a2a32021-06-30 18:01:02 +0200889 t->data_size += t->data_nbelem[type] * stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200890 t->data_ofs[type] = -t->data_size;
891 return PE_NONE;
892}
893
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100894/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100895 * Parse a line with <linenum> as number in <file> configuration file to configure
896 * the stick-table with <t> as address and <id> as ID.
897 * <peers> provides the "peers" section pointer only if this function is called
898 * from a "peers" section.
899 * <nid> is the stick-table name which is sent over the network. It must be equal
900 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
901 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500902 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100903 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
904 */
905int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100906 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100907{
908 int err_code = 0;
909 int idx = 1;
910 unsigned int val;
911
912 if (!id || !*id) {
913 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
914 err_code |= ERR_ALERT | ERR_ABORT;
915 goto out;
916 }
917
918 /* Store the "peers" section if this function is called from a "peers" section. */
919 if (peers) {
920 t->peers.p = peers;
921 idx++;
922 }
923
924 t->id = id;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200925 t->idlen = strlen(id);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100926 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100927 t->type = (unsigned int)-1;
928 t->conf.file = file;
929 t->conf.line = linenum;
930
931 while (*args[idx]) {
932 const char *err;
933
934 if (strcmp(args[idx], "size") == 0) {
935 idx++;
936 if (!*(args[idx])) {
937 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
938 file, linenum, args[0], args[idx-1]);
939 err_code |= ERR_ALERT | ERR_FATAL;
940 goto out;
941 }
942 if ((err = parse_size_err(args[idx], &t->size))) {
943 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
944 file, linenum, args[0], *err, args[idx-1]);
945 err_code |= ERR_ALERT | ERR_FATAL;
946 goto out;
947 }
948 idx++;
949 }
950 /* This argument does not exit in "peers" section. */
951 else if (!peers && strcmp(args[idx], "peers") == 0) {
952 idx++;
953 if (!*(args[idx])) {
954 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
955 file, linenum, args[0], args[idx-1]);
956 err_code |= ERR_ALERT | ERR_FATAL;
957 goto out;
958 }
Aurelien DARRAGON0509b1e2023-11-02 09:18:55 +0100959 ha_free(&t->peers.name);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100960 t->peers.name = strdup(args[idx++]);
961 }
962 else if (strcmp(args[idx], "expire") == 0) {
963 idx++;
964 if (!*(args[idx])) {
965 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
966 file, linenum, args[0], args[idx-1]);
967 err_code |= ERR_ALERT | ERR_FATAL;
968 goto out;
969 }
970 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200971 if (err == PARSE_TIME_OVER) {
972 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
973 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100974 err_code |= ERR_ALERT | ERR_FATAL;
975 goto out;
976 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200977 else if (err == PARSE_TIME_UNDER) {
978 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
979 file, linenum, args[0], args[idx], args[idx-1]);
980 err_code |= ERR_ALERT | ERR_FATAL;
981 goto out;
982 }
983 else if (err) {
984 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
985 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100986 err_code |= ERR_ALERT | ERR_FATAL;
987 goto out;
988 }
989 t->expire = val;
990 idx++;
991 }
992 else if (strcmp(args[idx], "nopurge") == 0) {
993 t->nopurge = 1;
994 idx++;
995 }
996 else if (strcmp(args[idx], "type") == 0) {
997 idx++;
William Lallemand3f210972023-04-13 14:33:52 +0200998 if (stktable_parse_type(args, &idx, &t->type, &t->key_size, file, linenum) != 0) {
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100999 err_code |= ERR_ALERT | ERR_FATAL;
1000 goto out;
1001 }
1002 /* idx already points to next arg */
1003 }
1004 else if (strcmp(args[idx], "store") == 0) {
1005 int type, err;
Emeric Brunc64a2a32021-06-30 18:01:02 +02001006 char *cw, *nw, *sa, *sa2;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001007
1008 idx++;
1009 nw = args[idx];
1010 while (*nw) {
1011 /* the "store" keyword supports a comma-separated list */
1012 cw = nw;
1013 sa = NULL; /* store arg */
Emeric Brunc64a2a32021-06-30 18:01:02 +02001014 sa2 = NULL;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001015 while (*nw && *nw != ',') {
1016 if (*nw == '(') {
1017 *nw = 0;
1018 sa = ++nw;
1019 while (*nw != ')') {
1020 if (!*nw) {
1021 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
1022 file, linenum, args[0], cw);
1023 err_code |= ERR_ALERT | ERR_FATAL;
1024 goto out;
1025 }
Emeric Brunc64a2a32021-06-30 18:01:02 +02001026 if (*nw == ',') {
1027 *nw = '\0';
1028 sa2 = nw + 1;
1029 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001030 nw++;
1031 }
1032 *nw = '\0';
1033 }
1034 nw++;
1035 }
1036 if (*nw)
1037 *nw++ = '\0';
1038 type = stktable_get_data_type(cw);
1039 if (type < 0) {
1040 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
1041 file, linenum, args[0], cw);
1042 err_code |= ERR_ALERT | ERR_FATAL;
1043 goto out;
1044 }
1045
Emeric Brunc64a2a32021-06-30 18:01:02 +02001046 err = stktable_alloc_data_type(t, type, sa, sa2);
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001047 switch (err) {
1048 case PE_NONE: break;
1049 case PE_EXIST:
1050 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
1051 file, linenum, args[0], cw);
1052 err_code |= ERR_WARN;
1053 break;
1054
1055 case PE_ARG_MISSING:
1056 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
1057 file, linenum, args[0], cw);
1058 err_code |= ERR_ALERT | ERR_FATAL;
1059 goto out;
1060
1061 case PE_ARG_NOT_USED:
1062 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
1063 file, linenum, args[0], cw);
1064 err_code |= ERR_ALERT | ERR_FATAL;
1065 goto out;
Emeric Brunc64a2a32021-06-30 18:01:02 +02001066 case PE_ARG_VALUE_OOR:
1067 ha_alert("parsing [%s:%d] : %s: array size is out of allowed range (1-%d) for store option '%s'.\n",
1068 file, linenum, args[0], STKTABLE_MAX_DT_ARRAY_SIZE, cw);
1069 err_code |= ERR_ALERT | ERR_FATAL;
1070 goto out;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001071
1072 default:
1073 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
1074 file, linenum, args[0], cw);
1075 err_code |= ERR_ALERT | ERR_FATAL;
1076 goto out;
1077 }
1078 }
1079 idx++;
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001080 if (t->data_ofs[STKTABLE_DT_GPT] && t->data_ofs[STKTABLE_DT_GPT0]) {
1081 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpt' and 'gpt0' in a same table is not permitted as 'gpt' overrides 'gpt0'.\n",
1082 file, linenum, args[0]);
1083 err_code |= ERR_ALERT | ERR_FATAL;
1084 goto out;
1085 }
Emeric Brun726783d2021-06-30 19:06:43 +02001086 else if (t->data_ofs[STKTABLE_DT_GPC] && (t->data_ofs[STKTABLE_DT_GPC0] || t->data_ofs[STKTABLE_DT_GPC1])) {
1087 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc' and 'gpc[0/1]' in a same table is not permitted as 'gpc' overrides 'gpc[0/1]'.\n",
1088 file, linenum, args[0]);
1089 err_code |= ERR_ALERT | ERR_FATAL;
1090 goto out;
1091 }
1092 else if (t->data_ofs[STKTABLE_DT_GPC_RATE] && (t->data_ofs[STKTABLE_DT_GPC0_RATE] || t->data_ofs[STKTABLE_DT_GPC1_RATE])) {
1093 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc_rate' and 'gpc[0/1]_rate' in a same table is not permitted as 'gpc_rate' overrides 'gpc[0/1]_rate'.\n",
1094 file, linenum, args[0]);
1095 err_code |= ERR_ALERT | ERR_FATAL;
1096 goto out;
1097 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001098 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001099 else if (strcmp(args[idx], "srvkey") == 0) {
1100 char *keytype;
1101 idx++;
1102 keytype = args[idx];
1103 if (strcmp(keytype, "name") == 0) {
1104 t->server_key_type = STKTABLE_SRV_NAME;
1105 }
1106 else if (strcmp(keytype, "addr") == 0) {
1107 t->server_key_type = STKTABLE_SRV_ADDR;
1108 }
1109 else {
1110 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
1111 file, linenum, args[0], keytype);
1112 err_code |= ERR_ALERT | ERR_FATAL;
1113 goto out;
1114
1115 }
1116 idx++;
1117 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001118 else {
1119 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
1120 file, linenum, args[0], args[idx]);
1121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124 }
1125
1126 if (!t->size) {
1127 ha_alert("parsing [%s:%d] : %s: missing size.\n",
1128 file, linenum, args[0]);
1129 err_code |= ERR_ALERT | ERR_FATAL;
1130 goto out;
1131 }
1132
1133 if (t->type == (unsigned int)-1) {
1134 ha_alert("parsing [%s:%d] : %s: missing type.\n",
1135 file, linenum, args[0]);
1136 err_code |= ERR_ALERT | ERR_FATAL;
1137 goto out;
1138 }
1139
1140 out:
1141 return err_code;
1142}
1143
Willy Tarreau8fed9032014-07-03 17:02:46 +02001144/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001145 * Note that the sample *is* modified and that the returned key may point
1146 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +02001147 * Returns NULL if the sample could not be converted (eg: no matching type),
1148 * otherwise a pointer to the static stktable_key filled with what is needed
1149 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001150 */
Willy Tarreau8fed9032014-07-03 17:02:46 +02001151struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001152{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001153 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001154 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +02001155 return NULL;
1156
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001157 /* Fill static_table_key. */
1158 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +02001159
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001160 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +02001161 static_table_key.key = &smp->data.u.ipv4;
1162 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001163 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001164
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001165 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +02001166 static_table_key.key = &smp->data.u.ipv6;
1167 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001168 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001169
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001170 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001171 /* The stick table require a 32bit unsigned int, "sint" is a
1172 * signed 64 it, so we can convert it inplace.
1173 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001174 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001175 static_table_key.key = &smp->data.u.sint;
1176 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001177 break;
1178
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001179 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001180 if (!smp_make_safe(smp))
1181 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 static_table_key.key = smp->data.u.str.area;
1183 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001184 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001185
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001186 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001187 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001188 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001189 if (!smp_make_rw(smp))
1190 return NULL;
1191
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001192 if (smp->data.u.str.size < t->key_size)
1193 if (!smp_dup(smp))
1194 return NULL;
1195 if (smp->data.u.str.size < t->key_size)
1196 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001197 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1198 t->key_size - smp->data.u.str.data);
1199 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001200 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001201 static_table_key.key = smp->data.u.str.area;
1202 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001203 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001204
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001205 default: /* impossible case. */
1206 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001207 }
1208
Christopher Fauletca20d022017-08-29 15:30:31 +02001209 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001210}
1211
1212/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001213 * Process a fetch + format conversion as defined by the sample expression <expr>
1214 * on request or response considering the <opt> parameter. Returns either NULL if
1215 * no key could be extracted, or a pointer to the converted result stored in
1216 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1217 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001218 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1219 * without SMP_OPT_FINAL). The output will be usable like this :
1220 *
1221 * return MAY_CHANGE FINAL Meaning for the sample
1222 * NULL 0 * Not present and will never be (eg: header)
1223 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1224 * NULL 1 1 Not present, will not change anymore
1225 * smp 0 * Present and will not change (eg: header)
1226 * smp 1 0 not possible
1227 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001228 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001229struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001230 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1231{
1232 if (smp)
1233 memset(smp, 0, sizeof(*smp));
1234
Willy Tarreau192252e2015-04-04 01:47:55 +02001235 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001236 if (!smp)
1237 return NULL;
1238
1239 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1240 return NULL; /* we can only use stable samples */
1241
1242 return smp_to_stkey(smp, t);
1243}
1244
1245/*
Willy Tarreau12785782012-04-27 21:37:17 +02001246 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001247 * type <table_type>, otherwise zero. Used in configuration check.
1248 */
Willy Tarreau12785782012-04-27 21:37:17 +02001249int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001250{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001251 int out_type;
1252
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001253 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001254 return 0;
1255
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001256 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001257
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001258 /* Convert sample. */
1259 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001260 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001261
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001262 return 1;
1263}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001264
Willy Tarreauedee1d62014-07-15 16:44:27 +02001265/* Extra data types processing : after the last one, some room may remain
1266 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1267 * at run time.
1268 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001269struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001270 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001271 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001272 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001273 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001274 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1275 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001276 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT, .is_local = 1 },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001277 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1278 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1279 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1280 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1281 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1282 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1283 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1284 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1285 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1286 [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001287 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1288 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001289 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001290 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1291 [STKTABLE_DT_HTTP_FAIL_RATE]= { .name = "http_fail_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Emeric Brun877b0b52021-06-30 18:57:49 +02001292 [STKTABLE_DT_GPT] = { .name = "gpt", .std_type = STD_T_UINT, .is_array = 1 },
Emeric Brun4d7ada82021-06-30 19:04:16 +02001293 [STKTABLE_DT_GPC] = { .name = "gpc", .std_type = STD_T_UINT, .is_array = 1 },
1294 [STKTABLE_DT_GPC_RATE] = { .name = "gpc_rate", .std_type = STD_T_FRQP, .is_array = 1, .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +02001295};
1296
Willy Tarreauedee1d62014-07-15 16:44:27 +02001297/* Registers stick-table extra data type with index <idx>, name <name>, type
1298 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1299 * index is automatically allocated. The allocated index is returned, or -1 if
1300 * no free index was found or <name> was already registered. The <name> is used
1301 * directly as a pointer, so if it's not stable, the caller must allocate it.
1302 */
1303int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1304{
1305 if (idx < 0) {
1306 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1307 if (!stktable_data_types[idx].name)
1308 break;
1309
1310 if (strcmp(stktable_data_types[idx].name, name) == 0)
1311 return -1;
1312 }
1313 }
1314
1315 if (idx >= STKTABLE_DATA_TYPES)
1316 return -1;
1317
1318 if (stktable_data_types[idx].name != NULL)
1319 return -1;
1320
1321 stktable_data_types[idx].name = name;
1322 stktable_data_types[idx].std_type = std_type;
1323 stktable_data_types[idx].arg_type = arg_type;
1324 return idx;
1325}
1326
Willy Tarreau08d5f982010-06-06 13:34:54 +02001327/*
1328 * Returns the data type number for the stktable_data_type whose name is <name>,
1329 * or <0 if not found.
1330 */
1331int stktable_get_data_type(char *name)
1332{
1333 int type;
1334
1335 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001336 if (!stktable_data_types[type].name)
1337 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001338 if (strcmp(name, stktable_data_types[type].name) == 0)
1339 return type;
1340 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001341 /* For backwards compatibility */
1342 if (strcmp(name, "server_name") == 0)
1343 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001344 return -1;
1345}
1346
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001347/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1348 * it up into this table. Returns true if found, false otherwise. The input
1349 * type is STR so that input samples are converted to string (since all types
1350 * can be converted to strings), then the function casts the string again into
1351 * the table's type. This is a double conversion, but in the future we might
1352 * support automatic input types to perform the cast on the fly.
1353 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001354static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355{
1356 struct stktable *t;
1357 struct stktable_key *key;
1358 struct stksess *ts;
1359
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001360 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001361
1362 key = smp_to_stkey(smp, t);
1363 if (!key)
1364 return 0;
1365
1366 ts = stktable_lookup_key(t, key);
1367
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001368 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001369 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001370 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001371 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001372 return 1;
1373}
1374
1375/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1376 * it up into this table. Returns the data rate received from clients in bytes/s
1377 * if the key is present in the table, otherwise zero, so that comparisons can
1378 * be easily performed. If the inspected parameter is not stored in the table,
1379 * <not found> is returned.
1380 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001381static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001382{
1383 struct stktable *t;
1384 struct stktable_key *key;
1385 struct stksess *ts;
1386 void *ptr;
1387
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001388 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001389
1390 key = smp_to_stkey(smp, t);
1391 if (!key)
1392 return 0;
1393
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001394 ts = stktable_lookup_key(t, key);
1395
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001396 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001397 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001398 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001399
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001400 if (!ts) /* key not present */
1401 return 1;
1402
1403 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001404 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001405 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001406 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001407
Daniel Corbett3e60b112018-05-27 09:47:12 -04001408 stktable_release(t, ts);
1409 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001410}
1411
1412/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1413 * it up into this table. Returns the cumulated number of connections for the key
1414 * if the key is present in the table, otherwise zero, so that comparisons can
1415 * be easily performed. If the inspected parameter is not stored in the table,
1416 * <not found> is returned.
1417 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001418static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001419{
1420 struct stktable *t;
1421 struct stktable_key *key;
1422 struct stksess *ts;
1423 void *ptr;
1424
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001425 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001426
1427 key = smp_to_stkey(smp, t);
1428 if (!key)
1429 return 0;
1430
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001431 ts = stktable_lookup_key(t, key);
1432
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001433 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001434 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001435 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001436
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001437 if (!ts) /* key not present */
1438 return 1;
1439
1440 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001441 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001442 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001443
Daniel Corbett3e60b112018-05-27 09:47:12 -04001444 stktable_release(t, ts);
1445 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001446}
1447
1448/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1449 * it up into this table. Returns the number of concurrent connections for the
1450 * key if the key is present in the table, otherwise zero, so that comparisons
1451 * can be easily performed. If the inspected parameter is not stored in the
1452 * table, <not found> is returned.
1453 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001454static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001455{
1456 struct stktable *t;
1457 struct stktable_key *key;
1458 struct stksess *ts;
1459 void *ptr;
1460
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001461 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001462
1463 key = smp_to_stkey(smp, t);
1464 if (!key)
1465 return 0;
1466
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001467 ts = stktable_lookup_key(t, key);
1468
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001469 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001470 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001471 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001472
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001473 if (!ts) /* key not present */
1474 return 1;
1475
1476 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001477 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001478 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001479
Daniel Corbett3e60b112018-05-27 09:47:12 -04001480 stktable_release(t, ts);
1481 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001482}
1483
1484/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1485 * it up into this table. Returns the rate of incoming connections from the key
1486 * if the key is present in the table, otherwise zero, so that comparisons can
1487 * be easily performed. If the inspected parameter is not stored in the table,
1488 * <not found> is returned.
1489 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001490static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001491{
1492 struct stktable *t;
1493 struct stktable_key *key;
1494 struct stksess *ts;
1495 void *ptr;
1496
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001497 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001498
1499 key = smp_to_stkey(smp, t);
1500 if (!key)
1501 return 0;
1502
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001503 ts = stktable_lookup_key(t, key);
1504
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001505 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001506 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001507 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001508
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001509 if (!ts) /* key not present */
1510 return 1;
1511
1512 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001513 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001514 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001515 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001516
Daniel Corbett3e60b112018-05-27 09:47:12 -04001517 stktable_release(t, ts);
1518 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001519}
1520
1521/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Frédéric Lécaillebbeec372022-08-16 18:11:25 +02001522 * it up into this table. Returns the expiration delay for the key if the key is
1523 * present in the table, otherwise the default value provided as second argument
1524 * if any, if not (no default value), <not found> is returned.
1525 */
1526static int sample_conv_table_expire(const struct arg *arg_p, struct sample *smp, void *private)
1527{
1528 struct stktable *t;
1529 struct stktable_key *key;
1530 struct stksess *ts;
1531
1532 t = arg_p[0].data.t;
1533
1534 key = smp_to_stkey(smp, t);
1535 if (!key)
1536 return 0;
1537
1538 ts = stktable_lookup_key(t, key);
1539
1540 smp->flags = SMP_F_VOL_TEST;
1541 smp->data.type = SMP_T_SINT;
1542 smp->data.u.sint = 0;
1543
1544 if (!ts) { /* key not present */
1545 if (arg_p[1].type == ARGT_STOP)
1546 return 0;
1547
1548 /* default value */
1549 smp->data.u.sint = arg_p[1].data.sint;
1550 return 1;
1551 }
1552
1553 smp->data.u.sint = tick_remain(now_ms, ts->expire);
1554
1555 stktable_release(t, ts);
1556 return 1;
1557}
1558
1559/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1560 * it up into this table. Returns the time the key remains unused if the key is
1561 * present in the table, otherwise the default value provided as second argument
1562 * if any, if not (no default value), <not found> is returned.
1563 */
1564static int sample_conv_table_idle(const struct arg *arg_p, struct sample *smp, void *private)
1565{
1566 struct stktable *t;
1567 struct stktable_key *key;
1568 struct stksess *ts;
1569
1570 t = arg_p[0].data.t;
1571
1572 key = smp_to_stkey(smp, t);
1573 if (!key)
1574 return 0;
1575
1576 ts = stktable_lookup_key(t, key);
1577
1578 smp->flags = SMP_F_VOL_TEST;
1579 smp->data.type = SMP_T_SINT;
1580 smp->data.u.sint = 0;
1581
1582 if (!ts) { /* key not present */
1583 if (arg_p[1].type == ARGT_STOP)
1584 return 0;
1585
1586 /* default value */
1587 smp->data.u.sint = arg_p[1].data.sint;
1588 return 1;
1589 }
1590
1591 smp->data.u.sint = tick_remain(tick_remain(now_ms, ts->expire), t->expire);
1592
1593 stktable_release(t, ts);
1594 return 1;
1595}
1596
1597/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001598 * it up into this table. Returns the data rate sent to clients in bytes/s
1599 * if the key is present in the table, otherwise zero, so that comparisons can
1600 * be easily performed. If the inspected parameter is not stored in the table,
1601 * <not found> is returned.
1602 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001603static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001604{
1605 struct stktable *t;
1606 struct stktable_key *key;
1607 struct stksess *ts;
1608 void *ptr;
1609
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001610 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001611
1612 key = smp_to_stkey(smp, t);
1613 if (!key)
1614 return 0;
1615
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001616 ts = stktable_lookup_key(t, key);
1617
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001618 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001619 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001620 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001621
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001622 if (!ts) /* key not present */
1623 return 1;
1624
1625 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001626 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001627 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001628 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001629
Daniel Corbett3e60b112018-05-27 09:47:12 -04001630 stktable_release(t, ts);
1631 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001632}
1633
Emeric Brun877b0b52021-06-30 18:57:49 +02001634/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1635 * it up into this table. Returns the value of the GPT[arg_p(0)] tag for the key
1636 * if the key is present in the table, otherwise false, so that comparisons can
1637 * be easily performed. If the inspected parameter is not stored in the table,
1638 * <not found> is returned.
1639 */
1640static int sample_conv_table_gpt(const struct arg *arg_p, struct sample *smp, void *private)
1641{
1642 struct stktable *t;
1643 struct stktable_key *key;
1644 struct stksess *ts;
1645 void *ptr;
1646 unsigned int idx;
1647
1648 idx = arg_p[0].data.sint;
1649
1650 t = arg_p[1].data.t;
1651
1652 key = smp_to_stkey(smp, t);
1653 if (!key)
1654 return 0;
1655
1656 ts = stktable_lookup_key(t, key);
1657
1658 smp->flags = SMP_F_VOL_TEST;
1659 smp->data.type = SMP_T_SINT;
1660 smp->data.u.sint = 0;
1661
1662 if (!ts) /* key not present */
1663 return 1;
1664
1665 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, idx);
1666 if (ptr)
1667 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1668
1669 stktable_release(t, ts);
1670 return !!ptr;
1671}
1672
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001673/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001674 * it up into this table. Returns the value of the GPT0 tag for the key
1675 * if the key is present in the table, otherwise false, so that comparisons can
1676 * be easily performed. If the inspected parameter is not stored in the table,
1677 * <not found> is returned.
1678 */
1679static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1680{
1681 struct stktable *t;
1682 struct stktable_key *key;
1683 struct stksess *ts;
1684 void *ptr;
1685
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001686 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001687
1688 key = smp_to_stkey(smp, t);
1689 if (!key)
1690 return 0;
1691
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001692 ts = stktable_lookup_key(t, key);
1693
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001694 smp->flags = SMP_F_VOL_TEST;
1695 smp->data.type = SMP_T_SINT;
1696 smp->data.u.sint = 0;
1697
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001698 if (!ts) /* key not present */
1699 return 1;
1700
1701 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001702 if (!ptr)
1703 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, 0);
1704
Daniel Corbett3e60b112018-05-27 09:47:12 -04001705 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001706 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001707
Daniel Corbett3e60b112018-05-27 09:47:12 -04001708 stktable_release(t, ts);
1709 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001710}
1711
Emeric Brun4d7ada82021-06-30 19:04:16 +02001712/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1713 * it up into this table. Returns the value of the GPC[arg_p(0)] counter for the key
1714 * if the key is present in the table, otherwise zero, so that comparisons can
1715 * be easily performed. If the inspected parameter is not stored in the table,
1716 * <not found> is returned.
1717 */
1718static int sample_conv_table_gpc(const struct arg *arg_p, struct sample *smp, void *private)
1719{
1720 struct stktable *t;
1721 struct stktable_key *key;
1722 struct stksess *ts;
1723 void *ptr;
1724 unsigned int idx;
1725
1726 idx = arg_p[0].data.sint;
1727
1728 t = arg_p[1].data.t;
1729
1730 key = smp_to_stkey(smp, t);
1731 if (!key)
1732 return 0;
1733
1734 ts = stktable_lookup_key(t, key);
1735
1736 smp->flags = SMP_F_VOL_TEST;
1737 smp->data.type = SMP_T_SINT;
1738 smp->data.u.sint = 0;
1739
1740 if (!ts) /* key not present */
1741 return 1;
1742
1743 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, idx);
1744 if (ptr)
1745 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1746
1747 stktable_release(t, ts);
1748 return !!ptr;
1749}
1750
1751/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1752 * it up into this table. Returns the event rate of the GPC[arg_p(0)] counter
1753 * for the key if the key is present in the table, otherwise zero, so that
1754 * comparisons can be easily performed. If the inspected parameter is not
1755 * stored in the table, <not found> is returned.
1756 */
1757static int sample_conv_table_gpc_rate(const struct arg *arg_p, struct sample *smp, void *private)
1758{
1759 struct stktable *t;
1760 struct stktable_key *key;
1761 struct stksess *ts;
1762 void *ptr;
1763 unsigned int idx;
1764
1765 idx = arg_p[0].data.sint;
1766
1767 t = arg_p[1].data.t;
1768
1769 key = smp_to_stkey(smp, t);
1770 if (!key)
1771 return 0;
1772
1773 ts = stktable_lookup_key(t, key);
1774
1775 smp->flags = SMP_F_VOL_TEST;
1776 smp->data.type = SMP_T_SINT;
1777 smp->data.u.sint = 0;
1778
1779 if (!ts) /* key not present */
1780 return 1;
1781
1782 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, idx);
1783 if (ptr)
1784 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1785 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1786
1787 stktable_release(t, ts);
1788 return !!ptr;
1789}
1790
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001791/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001792 * it up into this table. Returns the value of the GPC0 counter for the key
1793 * if the key is present in the table, otherwise zero, so that comparisons can
1794 * be easily performed. If the inspected parameter is not stored in the table,
1795 * <not found> is returned.
1796 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001797static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001798{
1799 struct stktable *t;
1800 struct stktable_key *key;
1801 struct stksess *ts;
1802 void *ptr;
1803
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001804 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001805
1806 key = smp_to_stkey(smp, t);
1807 if (!key)
1808 return 0;
1809
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001810 ts = stktable_lookup_key(t, key);
1811
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001812 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001813 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001814 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001815
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001816 if (!ts) /* key not present */
1817 return 1;
1818
1819 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02001820 if (!ptr) {
1821 /* fallback on the gpc array */
1822 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 0);
1823 }
1824
Daniel Corbett3e60b112018-05-27 09:47:12 -04001825 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001826 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001827
Daniel Corbett3e60b112018-05-27 09:47:12 -04001828 stktable_release(t, ts);
1829 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001830}
1831
1832/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1833 * it up into this table. Returns the event rate of the GPC0 counter for the key
1834 * if the key is present in the table, otherwise zero, so that comparisons can
1835 * be easily performed. If the inspected parameter is not stored in the table,
1836 * <not found> is returned.
1837 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001838static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001839{
1840 struct stktable *t;
1841 struct stktable_key *key;
1842 struct stksess *ts;
1843 void *ptr;
1844
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001845 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001846
1847 key = smp_to_stkey(smp, t);
1848 if (!key)
1849 return 0;
1850
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001851 ts = stktable_lookup_key(t, key);
1852
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001853 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001854 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001855 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001856
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001857 if (!ts) /* key not present */
1858 return 1;
1859
1860 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001861 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001862 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001863 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001864 else {
1865 /* fallback on the gpc array */
1866 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 0);
1867 if (ptr)
1868 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1869 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1870 }
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001871
Daniel Corbett3e60b112018-05-27 09:47:12 -04001872 stktable_release(t, ts);
1873 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001874}
1875
1876/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001877 * it up into this table. Returns the value of the GPC1 counter for the key
1878 * if the key is present in the table, otherwise zero, so that comparisons can
1879 * be easily performed. If the inspected parameter is not stored in the table,
1880 * <not found> is returned.
1881 */
1882static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1883{
1884 struct stktable *t;
1885 struct stktable_key *key;
1886 struct stksess *ts;
1887 void *ptr;
1888
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001889 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001890
1891 key = smp_to_stkey(smp, t);
1892 if (!key)
1893 return 0;
1894
1895 ts = stktable_lookup_key(t, key);
1896
1897 smp->flags = SMP_F_VOL_TEST;
1898 smp->data.type = SMP_T_SINT;
1899 smp->data.u.sint = 0;
1900
1901 if (!ts) /* key not present */
1902 return 1;
1903
1904 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02001905 if (!ptr) {
1906 /* fallback on the gpc array */
1907 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 1);
1908 }
1909
Daniel Corbett3e60b112018-05-27 09:47:12 -04001910 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001911 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001912
Daniel Corbett3e60b112018-05-27 09:47:12 -04001913 stktable_release(t, ts);
1914 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001915}
1916
1917/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1918 * it up into this table. Returns the event rate of the GPC1 counter for the key
1919 * if the key is present in the table, otherwise zero, so that comparisons can
1920 * be easily performed. If the inspected parameter is not stored in the table,
1921 * <not found> is returned.
1922 */
1923static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1924{
1925 struct stktable *t;
1926 struct stktable_key *key;
1927 struct stksess *ts;
1928 void *ptr;
1929
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001930 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001931
1932 key = smp_to_stkey(smp, t);
1933 if (!key)
1934 return 0;
1935
1936 ts = stktable_lookup_key(t, key);
1937
1938 smp->flags = SMP_F_VOL_TEST;
1939 smp->data.type = SMP_T_SINT;
1940 smp->data.u.sint = 0;
1941
1942 if (!ts) /* key not present */
1943 return 1;
1944
1945 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001946 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001947 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001948 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001949 else {
1950 /* fallback on the gpc array */
1951 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 1);
1952 if (ptr)
1953 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1954 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1955 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001956
Daniel Corbett3e60b112018-05-27 09:47:12 -04001957 stktable_release(t, ts);
1958 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001959}
1960
1961/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001962 * it up into this table. Returns the cumulated number of HTTP request errors
1963 * for the key if the key is present in the table, otherwise zero, so that
1964 * comparisons can be easily performed. If the inspected parameter is not stored
1965 * in the table, <not found> is returned.
1966 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001967static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001968{
1969 struct stktable *t;
1970 struct stktable_key *key;
1971 struct stksess *ts;
1972 void *ptr;
1973
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001974 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001975
1976 key = smp_to_stkey(smp, t);
1977 if (!key)
1978 return 0;
1979
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001980 ts = stktable_lookup_key(t, key);
1981
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001982 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001984 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001985
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001986 if (!ts) /* key not present */
1987 return 1;
1988
1989 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001990 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001991 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001992
Daniel Corbett3e60b112018-05-27 09:47:12 -04001993 stktable_release(t, ts);
1994 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001995}
1996
1997/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1998 * it up into this table. Returns the HTTP request error rate the key
1999 * if the key is present in the table, otherwise zero, so that comparisons can
2000 * be easily performed. If the inspected parameter is not stored in the table,
2001 * <not found> is returned.
2002 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002003static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002004{
2005 struct stktable *t;
2006 struct stktable_key *key;
2007 struct stksess *ts;
2008 void *ptr;
2009
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002010 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002011
2012 key = smp_to_stkey(smp, t);
2013 if (!key)
2014 return 0;
2015
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002016 ts = stktable_lookup_key(t, key);
2017
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002018 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002019 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002020 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002021
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002022 if (!ts) /* key not present */
2023 return 1;
2024
2025 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002026 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002027 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002028 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002029
Daniel Corbett3e60b112018-05-27 09:47:12 -04002030 stktable_release(t, ts);
2031 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002032}
2033
2034/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002035 * it up into this table. Returns the cumulated number of HTTP response failures
2036 * for the key if the key is present in the table, otherwise zero, so that
2037 * comparisons can be easily performed. If the inspected parameter is not stored
2038 * in the table, <not found> is returned.
2039 */
2040static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
2041{
2042 struct stktable *t;
2043 struct stktable_key *key;
2044 struct stksess *ts;
2045 void *ptr;
2046
2047 t = arg_p[0].data.t;
2048
2049 key = smp_to_stkey(smp, t);
2050 if (!key)
2051 return 0;
2052
2053 ts = stktable_lookup_key(t, key);
2054
2055 smp->flags = SMP_F_VOL_TEST;
2056 smp->data.type = SMP_T_SINT;
2057 smp->data.u.sint = 0;
2058
2059 if (!ts) /* key not present */
2060 return 1;
2061
2062 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2063 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002064 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002065
2066 stktable_release(t, ts);
2067 return !!ptr;
2068}
2069
2070/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2071 * it up into this table. Returns the HTTP response failure rate for the key
2072 * if the key is present in the table, otherwise zero, so that comparisons can
2073 * be easily performed. If the inspected parameter is not stored in the table,
2074 * <not found> is returned.
2075 */
2076static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
2077{
2078 struct stktable *t;
2079 struct stktable_key *key;
2080 struct stksess *ts;
2081 void *ptr;
2082
2083 t = arg_p[0].data.t;
2084
2085 key = smp_to_stkey(smp, t);
2086 if (!key)
2087 return 0;
2088
2089 ts = stktable_lookup_key(t, key);
2090
2091 smp->flags = SMP_F_VOL_TEST;
2092 smp->data.type = SMP_T_SINT;
2093 smp->data.u.sint = 0;
2094
2095 if (!ts) /* key not present */
2096 return 1;
2097
2098 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2099 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002100 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002101 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
2102
2103 stktable_release(t, ts);
2104 return !!ptr;
2105}
2106
2107/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002108 * it up into this table. Returns the cumulated number of HTTP request for the
2109 * key if the key is present in the table, otherwise zero, so that comparisons
2110 * can be easily performed. If the inspected parameter is not stored in the
2111 * table, <not found> is returned.
2112 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002113static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002114{
2115 struct stktable *t;
2116 struct stktable_key *key;
2117 struct stksess *ts;
2118 void *ptr;
2119
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002120 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002121
2122 key = smp_to_stkey(smp, t);
2123 if (!key)
2124 return 0;
2125
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002126 ts = stktable_lookup_key(t, key);
2127
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002128 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002129 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002130 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002131
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002132 if (!ts) /* key not present */
2133 return 1;
2134
2135 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002136 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002137 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002138
Daniel Corbett3e60b112018-05-27 09:47:12 -04002139 stktable_release(t, ts);
2140 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002141}
2142
2143/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2144 * it up into this table. Returns the HTTP request rate the key if the key is
2145 * present in the table, otherwise zero, so that comparisons can be easily
2146 * performed. If the inspected parameter is not stored in the table, <not found>
2147 * is returned.
2148 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002149static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002150{
2151 struct stktable *t;
2152 struct stktable_key *key;
2153 struct stksess *ts;
2154 void *ptr;
2155
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002156 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002157
2158 key = smp_to_stkey(smp, t);
2159 if (!key)
2160 return 0;
2161
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002162 ts = stktable_lookup_key(t, key);
2163
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002164 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002165 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002166 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002167
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002168 if (!ts) /* key not present */
2169 return 1;
2170
2171 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002172 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002173 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002174 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002175
Daniel Corbett3e60b112018-05-27 09:47:12 -04002176 stktable_release(t, ts);
2177 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002178}
2179
2180/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2181 * it up into this table. Returns the volume of datareceived from clients in kbytes
2182 * if the key is present in the table, otherwise zero, so that comparisons can
2183 * be easily performed. If the inspected parameter is not stored in the table,
2184 * <not found> is returned.
2185 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002186static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002187{
2188 struct stktable *t;
2189 struct stktable_key *key;
2190 struct stksess *ts;
2191 void *ptr;
2192
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002193 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002194
2195 key = smp_to_stkey(smp, t);
2196 if (!key)
2197 return 0;
2198
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002199 ts = stktable_lookup_key(t, key);
2200
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002201 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002202 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002203 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002204
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002205 if (!ts) /* key not present */
2206 return 1;
2207
2208 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002209 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002210 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002211
Daniel Corbett3e60b112018-05-27 09:47:12 -04002212 stktable_release(t, ts);
2213 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002214}
2215
2216/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2217 * it up into this table. Returns the volume of data sent to clients in kbytes
2218 * if the key is present in the table, otherwise zero, so that comparisons can
2219 * be easily performed. If the inspected parameter is not stored in the table,
2220 * <not found> is returned.
2221 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002222static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002223{
2224 struct stktable *t;
2225 struct stktable_key *key;
2226 struct stksess *ts;
2227 void *ptr;
2228
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002229 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002230
2231 key = smp_to_stkey(smp, t);
2232 if (!key)
2233 return 0;
2234
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002235 ts = stktable_lookup_key(t, key);
2236
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002237 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002238 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002239 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002240
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002241 if (!ts) /* key not present */
2242 return 1;
2243
2244 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002245 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002246 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002247
Daniel Corbett3e60b112018-05-27 09:47:12 -04002248 stktable_release(t, ts);
2249 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002250}
2251
2252/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2253 * it up into this table. Returns the server ID associated with the key if the
2254 * key is present in the table, otherwise zero, so that comparisons can be
2255 * easily performed. If the inspected parameter is not stored in the table,
2256 * <not found> is returned.
2257 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002258static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002259{
2260 struct stktable *t;
2261 struct stktable_key *key;
2262 struct stksess *ts;
2263 void *ptr;
2264
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002265 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002266
2267 key = smp_to_stkey(smp, t);
2268 if (!key)
2269 return 0;
2270
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002271 ts = stktable_lookup_key(t, key);
2272
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002273 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002274 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002275 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002276
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002277 if (!ts) /* key not present */
2278 return 1;
2279
2280 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002281 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002282 smp->data.u.sint = stktable_data_cast(ptr, std_t_sint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002283
Daniel Corbett3e60b112018-05-27 09:47:12 -04002284 stktable_release(t, ts);
2285 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002286}
2287
2288/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2289 * it up into this table. Returns the cumulated number of sessions for the
2290 * key if the key is present in the table, otherwise zero, so that comparisons
2291 * can be easily performed. If the inspected parameter is not stored in the
2292 * table, <not found> is returned.
2293 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002294static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002295{
2296 struct stktable *t;
2297 struct stktable_key *key;
2298 struct stksess *ts;
2299 void *ptr;
2300
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002301 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002302
2303 key = smp_to_stkey(smp, t);
2304 if (!key)
2305 return 0;
2306
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002307 ts = stktable_lookup_key(t, key);
2308
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002309 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002310 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002311 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002312
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002313 if (!ts) /* key not present */
2314 return 1;
2315
2316 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002317 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002318 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002319
Daniel Corbett3e60b112018-05-27 09:47:12 -04002320 stktable_release(t, ts);
2321 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002322}
2323
2324/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2325 * it up into this table. Returns the session rate the key if the key is
2326 * present in the table, otherwise zero, so that comparisons can be easily
2327 * performed. If the inspected parameter is not stored in the table, <not found>
2328 * is returned.
2329 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002330static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002331{
2332 struct stktable *t;
2333 struct stktable_key *key;
2334 struct stksess *ts;
2335 void *ptr;
2336
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002337 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002338
2339 key = smp_to_stkey(smp, t);
2340 if (!key)
2341 return 0;
2342
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002343 ts = stktable_lookup_key(t, key);
2344
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002345 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002346 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002347 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002348
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002349 if (!ts) /* key not present */
2350 return 1;
2351
2352 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002353 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002354 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002355 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002356
Daniel Corbett3e60b112018-05-27 09:47:12 -04002357 stktable_release(t, ts);
2358 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002359}
2360
2361/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2362 * it up into this table. Returns the amount of concurrent connections tracking
2363 * the same key if the key is present in the table, otherwise zero, so that
2364 * comparisons can be easily performed. If the inspected parameter is not
2365 * stored in the table, <not found> is returned.
2366 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002367static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002368{
2369 struct stktable *t;
2370 struct stktable_key *key;
2371 struct stksess *ts;
2372
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002373 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002374
2375 key = smp_to_stkey(smp, t);
2376 if (!key)
2377 return 0;
2378
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002379 ts = stktable_lookup_key(t, key);
2380
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002381 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002382 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002383 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002384
Tim Duesterhus65189c12018-06-26 15:57:29 +02002385 if (!ts)
2386 return 1;
2387
2388 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002389
Daniel Corbett3e60b112018-05-27 09:47:12 -04002390 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002391 return 1;
2392}
2393
Emeric Brun4d7ada82021-06-30 19:04:16 +02002394/* This function increments the gpc counter at index 'rule->arg.gpc.idx' of the
2395 * array on the tracksc counter of index 'rule->arg.gpc.sc' stored into the
2396 * <stream> or directly in the session <sess> if <stream> is set to NULL
2397 *
2398 * This function always returns ACT_RET_CONT and parameter flags is unused.
2399 */
2400static enum act_return action_inc_gpc(struct act_rule *rule, struct proxy *px,
2401 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002402{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002403 struct stksess *ts;
2404 struct stkctr *stkctr;
2405
2406 /* Extract the stksess, return OK if no stksess available. */
2407 if (s)
2408 stkctr = &s->stkctr[rule->arg.gpc.sc];
2409 else
2410 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002411
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002412 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002413 if (ts) {
2414 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002415
Emeric Brun4d7ada82021-06-30 19:04:16 +02002416 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2417 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2418 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2419
Emeric Brun819fc6f2017-06-13 19:37:32 +02002420 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002421 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002422
2423 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002424 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun4d7ada82021-06-30 19:04:16 +02002425 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002426
Emeric Brun819fc6f2017-06-13 19:37:32 +02002427 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002428 stktable_data_cast(ptr2, std_t_uint)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002429
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002430 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002431
2432 /* If data was modified, we need to touch to re-schedule sync */
2433 stktable_touch_local(stkctr->table, ts, 0);
2434 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002435 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002436 return ACT_RET_CONT;
2437}
2438
Emeric Brun4d7ada82021-06-30 19:04:16 +02002439/* Same as action_inc_gpc() but for gpc0 only */
2440static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
2441 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002442{
Emeric Brun4d7ada82021-06-30 19:04:16 +02002443 struct stksess *ts;
2444 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002445 unsigned int period = 0;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002446
Emeric Brun4d7ada82021-06-30 19:04:16 +02002447 /* Extract the stksess, return OK if no stksess available. */
2448 if (s)
2449 stkctr = &s->stkctr[rule->arg.gpc.sc];
2450 else
2451 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002452
Emeric Brun4d7ada82021-06-30 19:04:16 +02002453 ts = stkctr_entry(stkctr);
2454 if (ts) {
2455 void *ptr1, *ptr2;
2456
2457 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2458 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002459 if (ptr1) {
2460 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
2461 }
2462 else {
2463 /* fallback on the gpc array */
2464 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 0);
2465 if (ptr1)
2466 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2467 }
2468
Emeric Brun4d7ada82021-06-30 19:04:16 +02002469 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02002470 if (!ptr2) {
2471 /* fallback on the gpc array */
2472 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 0);
2473 }
2474
Emeric Brun4d7ada82021-06-30 19:04:16 +02002475 if (ptr1 || ptr2) {
2476 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2477
2478 if (ptr1)
2479 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002480 period, 1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002481
2482 if (ptr2)
2483 stktable_data_cast(ptr2, std_t_uint)++;
2484
2485 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2486
2487 /* If data was modified, we need to touch to re-schedule sync */
2488 stktable_touch_local(stkctr->table, ts, 0);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002489 }
2490 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002491 return ACT_RET_CONT;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002492}
2493
Emeric Brun4d7ada82021-06-30 19:04:16 +02002494/* Same as action_inc_gpc() but for gpc1 only */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002495static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2496 struct session *sess, struct stream *s, int flags)
2497{
2498 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002499 struct stkctr *stkctr = NULL;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002500 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002501
2502 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002503 if (s && s->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002504 stkctr = &s->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002505 else if (sess->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002506 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002507 else
2508 return ACT_RET_CONT;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002509
2510 ts = stkctr_entry(stkctr);
2511 if (ts) {
2512 void *ptr1, *ptr2;
2513
2514 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2515 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002516 if (ptr1) {
2517 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
2518 }
2519 else {
2520 /* fallback on the gpc array */
2521 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 1);
2522 if (ptr1)
2523 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2524 }
2525
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002526 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02002527 if (!ptr2) {
2528 /* fallback on the gpc array */
2529 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 1);
2530 }
2531
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002532 if (ptr1 || ptr2) {
2533 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2534
2535 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002536 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002537 period, 1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002538
2539 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002540 stktable_data_cast(ptr2, std_t_uint)++;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002541
2542 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2543
2544 /* If data was modified, we need to touch to re-schedule sync */
2545 stktable_touch_local(stkctr->table, ts, 0);
2546 }
2547 }
2548 return ACT_RET_CONT;
2549}
2550
Emeric Brun4d7ada82021-06-30 19:04:16 +02002551/* This function is a common parser for actions incrementing the GPC
2552 * (General Purpose Counters). It understands the formats:
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002553 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002554 * sc-inc-gpc(<gpc IDX>,<track ID>)
2555 * sc-inc-gpc0([<track ID>])
2556 * sc-inc-gpc1([<track ID>])
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002557 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002558 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
2559 * message. Otherwise it returns ACT_RET_PRS_OK.
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002560 */
Emeric Brun4d7ada82021-06-30 19:04:16 +02002561static enum act_parse_ret parse_inc_gpc(const char **args, int *arg, struct proxy *px,
2562 struct act_rule *rule, char **err)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002563{
2564 const char *cmd_name = args[*arg-1];
2565 char *error;
2566
Willy Tarreau6c011712023-01-06 16:09:58 +01002567 if (!global.tune.nb_stk_ctr) {
2568 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2569 return ACT_RET_PRS_ERR;
2570 }
2571
Emeric Brun4d7ada82021-06-30 19:04:16 +02002572 cmd_name += strlen("sc-inc-gpc");
2573 if (*cmd_name == '(') {
2574 cmd_name++; /* skip the '(' */
2575 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2576 if (*error != ',') {
2577 memprintf(err, "Missing gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002578 return ACT_RET_PRS_ERR;
2579 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002580 else {
2581 cmd_name = error + 1; /* skip the ',' */
2582 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2583 if (*error != ')') {
2584 memprintf(err, "invalid stick table track ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2585 return ACT_RET_PRS_ERR;
2586 }
2587
Willy Tarreau6c011712023-01-06 16:09:58 +01002588 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2589 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d (tune.stick-counters)",
2590 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002591 return ACT_RET_PRS_ERR;
2592 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002593 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002594 rule->action_ptr = action_inc_gpc;
2595 }
2596 else if (*cmd_name == '0' ||*cmd_name == '1') {
2597 char c = *cmd_name;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002598
Emeric Brun4d7ada82021-06-30 19:04:16 +02002599 cmd_name++;
2600 if (*cmd_name == '\0') {
2601 /* default stick table id. */
2602 rule->arg.gpc.sc = 0;
2603 } else {
2604 /* parse the stick table id. */
2605 if (*cmd_name != '(') {
2606 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2607 return ACT_RET_PRS_ERR;
2608 }
2609 cmd_name++; /* jump the '(' */
2610 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2611 if (*error != ')') {
2612 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2613 return ACT_RET_PRS_ERR;
2614 }
2615
Willy Tarreau6c011712023-01-06 16:09:58 +01002616 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2617 memprintf(err, "invalid stick table track ID. The max allowed ID is %d (tune.stick-counters)",
2618 global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002619 return ACT_RET_PRS_ERR;
2620 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002621 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002622 if (c == '1')
2623 rule->action_ptr = action_inc_gpc1;
2624 else
2625 rule->action_ptr = action_inc_gpc0;
2626 }
2627 else {
2628 /* default stick table id. */
Willy Tarreau20391512023-01-02 17:35:50 +01002629 memprintf(err, "invalid gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002630 return ACT_RET_PRS_ERR;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002631 }
2632 rule->action = ACT_CUSTOM;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002633 return ACT_RET_PRS_OK;
2634}
2635
Emeric Brun877b0b52021-06-30 18:57:49 +02002636/* This function sets the gpt at index 'rule->arg.gpt.idx' of the array on the
2637 * tracksc counter of index 'rule->arg.gpt.sc' stored into the <stream> or
2638 * directly in the session <sess> if <stream> is set to NULL. This gpt is
2639 * set to the value computed by the expression 'rule->arg.gpt.expr' or if
2640 * 'rule->arg.gpt.expr' is null directly to the value of 'rule->arg.gpt.value'.
2641 *
2642 * This function always returns ACT_RET_CONT and parameter flags is unused.
2643 */
2644static enum act_return action_set_gpt(struct act_rule *rule, struct proxy *px,
2645 struct session *sess, struct stream *s, int flags)
2646{
2647 void *ptr;
2648 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002649 struct stkctr *stkctr = NULL;
Emeric Brun877b0b52021-06-30 18:57:49 +02002650 unsigned int value = 0;
2651 struct sample *smp;
2652 int smp_opt_dir;
2653
2654 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002655 if (s && s->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002656 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002657 else if (sess->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002658 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002659 else
2660 return ACT_RET_CONT;
Emeric Brun877b0b52021-06-30 18:57:49 +02002661
2662 ts = stkctr_entry(stkctr);
2663 if (!ts)
2664 return ACT_RET_CONT;
2665
2666 /* Store the sample in the required sc, and ignore errors. */
2667 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, rule->arg.gpt.idx);
2668 if (ptr) {
2669
2670 if (!rule->arg.gpt.expr)
2671 value = (unsigned int)(rule->arg.gpt.value);
2672 else {
2673 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002674 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Emeric Brun877b0b52021-06-30 18:57:49 +02002675 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2676 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2677 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2678 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2679 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2680 default:
2681 send_log(px, LOG_ERR, "stick table: internal error while setting gpt%u.", rule->arg.gpt.idx);
2682 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2683 ha_alert("stick table: internal error while executing setting gpt%u.\n", rule->arg.gpt.idx);
2684 return ACT_RET_CONT;
2685 }
2686
2687 /* Fetch and cast the expression. */
2688 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2689 if (!smp) {
2690 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt%u.", rule->arg.gpt.idx);
2691 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2692 ha_alert("stick table: invalid expression or data type while setting gpt%u.\n", rule->arg.gpt.idx);
2693 return ACT_RET_CONT;
2694 }
2695 value = (unsigned int)(smp->data.u.sint);
2696 }
2697
2698 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2699
2700 stktable_data_cast(ptr, std_t_uint) = value;
2701
2702 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2703
2704 stktable_touch_local(stkctr->table, ts, 0);
2705 }
2706
2707 return ACT_RET_CONT;
2708}
2709
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002710/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002711static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002712 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002713{
2714 void *ptr;
2715 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002716 struct stkctr *stkctr = NULL;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002717 unsigned int value = 0;
2718 struct sample *smp;
2719 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002720
2721 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002722 if (s && s->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002723 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002724 else if (sess->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002725 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002726 else
2727 return ACT_RET_CONT;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002728
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002729 ts = stkctr_entry(stkctr);
2730 if (!ts)
2731 return ACT_RET_CONT;
2732
2733 /* Store the sample in the required sc, and ignore errors. */
2734 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02002735 if (!ptr)
2736 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, 0);
2737
Willy Tarreau79c1e912016-01-25 14:54:45 +01002738 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002739 if (!rule->arg.gpt.expr)
2740 value = (unsigned int)(rule->arg.gpt.value);
2741 else {
2742 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002743 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002744 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2745 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2746 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2747 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2748 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2749 default:
2750 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2751 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2752 ha_alert("stick table: internal error while executing setting gpt0.\n");
2753 return ACT_RET_CONT;
2754 }
2755
2756 /* Fetch and cast the expression. */
2757 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2758 if (!smp) {
2759 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2760 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2761 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2762 return ACT_RET_CONT;
2763 }
2764 value = (unsigned int)(smp->data.u.sint);
2765 }
2766
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002767 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002768
Emeric Brun0e3457b2021-06-30 17:18:28 +02002769 stktable_data_cast(ptr, std_t_uint) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002770
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002771 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002772
2773 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002774 }
2775
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002776 return ACT_RET_CONT;
2777}
2778
Emeric Brun877b0b52021-06-30 18:57:49 +02002779/* This function is a parser for the "sc-set-gpt" and "sc-set-gpt0" actions.
2780 * It understands the formats:
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002781 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002782 * sc-set-gpt(<gpt IDX>,<track ID>) <expression>
2783 * sc-set-gpt0(<track ID>) <expression>
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002784 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002785 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2786 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpt.expr'
2787 * is filled with the pointer to the expression to execute or NULL if the arg
2788 * is directly an integer stored into 'rule->arg.gpt.value'.
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002789 */
Emeric Brun877b0b52021-06-30 18:57:49 +02002790static enum act_parse_ret parse_set_gpt(const char **args, int *arg, struct proxy *px,
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002791 struct act_rule *rule, char **err)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002792{
2793 const char *cmd_name = args[*arg-1];
2794 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002795 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002796
Willy Tarreau6c011712023-01-06 16:09:58 +01002797 if (!global.tune.nb_stk_ctr) {
2798 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2799 return ACT_RET_PRS_ERR;
2800 }
2801
Emeric Brun877b0b52021-06-30 18:57:49 +02002802 cmd_name += strlen("sc-set-gpt");
2803 if (*cmd_name == '(') {
2804 cmd_name++; /* skip the '(' */
2805 rule->arg.gpt.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2806 if (*error != ',') {
2807 memprintf(err, "Missing gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002808 return ACT_RET_PRS_ERR;
2809 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002810 else {
2811 cmd_name = error + 1; /* skip the ',' */
2812 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2813 if (*error != ')') {
2814 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2815 return ACT_RET_PRS_ERR;
2816 }
2817
Willy Tarreau6c011712023-01-06 16:09:58 +01002818 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002819 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002820 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002821 return ACT_RET_PRS_ERR;
2822 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002823 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002824 rule->action_ptr = action_set_gpt;
2825 }
2826 else if (*cmd_name == '0') {
2827 cmd_name++;
2828 if (*cmd_name == '\0') {
2829 /* default stick table id. */
2830 rule->arg.gpt.sc = 0;
2831 } else {
2832 /* parse the stick table id. */
2833 if (*cmd_name != '(') {
2834 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2835 return ACT_RET_PRS_ERR;
2836 }
2837 cmd_name++; /* jump the '(' */
2838 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2839 if (*error != ')') {
2840 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2841 return ACT_RET_PRS_ERR;
2842 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002843
Willy Tarreau6c011712023-01-06 16:09:58 +01002844 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002845 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002846 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002847 return ACT_RET_PRS_ERR;
2848 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002849 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002850 rule->action_ptr = action_set_gpt0;
2851 }
2852 else {
2853 /* default stick table id. */
2854 memprintf(err, "invalid gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2855 return ACT_RET_PRS_ERR;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002856 }
2857
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002858 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002859 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002860 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002861 if (*error == '\0') {
2862 /* valid integer, skip it */
2863 (*arg)++;
2864 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002865 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002866 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002867 if (!rule->arg.gpt.expr)
2868 return ACT_RET_PRS_ERR;
2869
2870 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002871 case ACT_F_TCP_REQ_CON: smp_val = SMP_VAL_FE_CON_ACC; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002872 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2873 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2874 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2875 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2876 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2877 default:
2878 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2879 return ACT_RET_PRS_ERR;
2880 }
2881 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2882 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2883 sample_src_names(rule->arg.gpt.expr->fetch->use));
2884 free(rule->arg.gpt.expr);
2885 return ACT_RET_PRS_ERR;
2886 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002887 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002888
Thierry FOURNIER42148732015-09-02 17:17:33 +02002889 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002890
2891 return ACT_RET_PRS_OK;
2892}
2893
Willy Tarreau5a72d032023-01-02 18:15:20 +01002894/* This function updates the gpc at index 'rule->arg.gpc.idx' of the array on
2895 * the tracksc counter of index 'rule->arg.gpc.sc' stored into the <stream> or
2896 * directly in the session <sess> if <stream> is set to NULL. This gpc is
2897 * set to the value computed by the expression 'rule->arg.gpc.expr' or if
2898 * 'rule->arg.gpc.expr' is null directly to the value of 'rule->arg.gpc.value'.
2899 *
2900 * This function always returns ACT_RET_CONT and parameter flags is unused.
2901 */
2902static enum act_return action_add_gpc(struct act_rule *rule, struct proxy *px,
2903 struct session *sess, struct stream *s, int flags)
2904{
2905 void *ptr1, *ptr2;
2906 struct stksess *ts;
2907 struct stkctr *stkctr;
2908 unsigned int value = 0;
2909 struct sample *smp;
2910 int smp_opt_dir;
2911
2912 /* Extract the stksess, return OK if no stksess available. */
2913 if (s)
2914 stkctr = &s->stkctr[rule->arg.gpc.sc];
2915 else
2916 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2917
2918 ts = stkctr_entry(stkctr);
2919 if (!ts)
2920 return ACT_RET_CONT;
2921
2922 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2923 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2924 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2925
2926 if (ptr1 || ptr2) {
2927 if (!rule->arg.gpc.expr)
2928 value = (unsigned int)(rule->arg.gpc.value);
2929 else {
2930 switch (rule->from) {
Aurelien DARRAGONb425f752023-08-09 17:39:29 +02002931 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau5a72d032023-01-02 18:15:20 +01002932 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2933 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2934 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2935 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2936 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2937 default:
2938 send_log(px, LOG_ERR, "stick table: internal error while setting gpc%u.", rule->arg.gpc.idx);
2939 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2940 ha_alert("stick table: internal error while executing setting gpc%u.\n", rule->arg.gpc.idx);
2941 return ACT_RET_CONT;
2942 }
2943
2944 /* Fetch and cast the expression. */
2945 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpc.expr, SMP_T_SINT);
2946 if (!smp) {
2947 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpc%u.", rule->arg.gpc.idx);
2948 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2949 ha_alert("stick table: invalid expression or data type while setting gpc%u.\n", rule->arg.gpc.idx);
2950 return ACT_RET_CONT;
2951 }
2952 value = (unsigned int)(smp->data.u.sint);
2953 }
2954
2955 if (value) {
2956 /* only update the value if non-null increment */
2957 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2958
2959 if (ptr1)
2960 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
2961 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, value);
2962
2963 if (ptr2)
2964 stktable_data_cast(ptr2, std_t_uint) += value;
2965
2966 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2967 }
2968 /* always touch the table so that it doesn't expire */
2969 stktable_touch_local(stkctr->table, ts, 0);
2970 }
2971
2972 return ACT_RET_CONT;
2973}
2974
2975/* This function is a parser for the "sc-add-gpc" action. It understands the
2976 * format:
2977 *
2978 * sc-add-gpc(<gpc IDX>,<track ID>) <expression>
2979 *
2980 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2981 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpc.expr'
2982 * is filled with the pointer to the expression to execute or NULL if the arg
2983 * is directly an integer stored into 'rule->arg.gpt.value'.
2984 */
2985static enum act_parse_ret parse_add_gpc(const char **args, int *arg, struct proxy *px,
2986 struct act_rule *rule, char **err)
2987{
2988 const char *cmd_name = args[*arg-1];
2989 char *error;
2990 int smp_val;
2991
2992 cmd_name += strlen("sc-add-gpc");
2993 if (*cmd_name != '(') {
2994 memprintf(err, "Missing or invalid arguments for '%s'. Expects sc-add-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2995 return ACT_RET_PRS_ERR;
2996 }
2997 cmd_name++; /* skip the '(' */
2998 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2999 if (*error != ',') {
3000 memprintf(err, "Missing gpc ID. Expects %s(<GPC ID>,<Track ID>)", args[*arg-1]);
3001 return ACT_RET_PRS_ERR;
3002 }
3003 else {
3004 cmd_name = error + 1; /* skip the ',' */
3005 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
3006 if (*error != ')') {
3007 memprintf(err, "invalid stick table track ID '%s'. Expects %s(<GPC ID>,<Track ID>)", cmd_name, args[*arg-1]);
3008 return ACT_RET_PRS_ERR;
3009 }
3010
3011 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
3012 memprintf(err, "invalid stick table track ID '%s' for '%s'. The max allowed ID is %d",
3013 cmd_name, args[*arg-1], MAX_SESS_STKCTR-1);
3014 return ACT_RET_PRS_ERR;
3015 }
3016 }
3017 rule->action_ptr = action_add_gpc;
3018
3019 /* value may be either an integer or an expression */
3020 rule->arg.gpc.expr = NULL;
3021 rule->arg.gpc.value = strtol(args[*arg], &error, 10);
3022 if (*error == '\0') {
3023 /* valid integer, skip it */
3024 (*arg)++;
3025 } else {
3026 rule->arg.gpc.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
3027 px->conf.args.line, err, &px->conf.args, NULL);
3028 if (!rule->arg.gpc.expr)
3029 return ACT_RET_PRS_ERR;
3030
3031 switch (rule->from) {
Aurelien DARRAGONb425f752023-08-09 17:39:29 +02003032 case ACT_F_TCP_REQ_CON: smp_val = SMP_VAL_FE_CON_ACC; break;
Willy Tarreau5a72d032023-01-02 18:15:20 +01003033 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
3034 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
3035 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
3036 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
3037 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
3038 default:
3039 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
3040 return ACT_RET_PRS_ERR;
3041 }
3042
3043 if (!(rule->arg.gpc.expr->fetch->val & smp_val)) {
3044 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
3045 sample_src_names(rule->arg.gpc.expr->fetch->use));
3046 free(rule->arg.gpc.expr);
3047 return ACT_RET_PRS_ERR;
3048 }
3049 }
3050
3051 rule->action = ACT_CUSTOM;
3052
3053 return ACT_RET_PRS_OK;
3054}
3055
Willy Tarreau7d562212016-11-25 16:10:05 +01003056/* set temp integer to the number of used entries in the table pointed to by expr.
3057 * Accepts exactly 1 argument of type table.
3058 */
3059static int
3060smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3061{
3062 smp->flags = SMP_F_VOL_TEST;
3063 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003064 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003065 return 1;
3066}
3067
3068/* set temp integer to the number of free entries in the table pointed to by expr.
3069 * Accepts exactly 1 argument of type table.
3070 */
3071static int
3072smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
3073{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003074 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003075
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003076 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003077 smp->flags = SMP_F_VOL_TEST;
3078 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003079 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003080 return 1;
3081}
3082
3083/* Returns a pointer to a stkctr depending on the fetch keyword name.
3084 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
3085 * sc[0-9]_* will return a pointer to the respective field in the
3086 * stream <l4>. sc_* requires an UINT argument specifying the stick
3087 * counter number. src_* will fill a locally allocated structure with
3088 * the table and entry corresponding to what is specified with src_*.
3089 * NULL may be returned if the designated stkctr is not tracked. For
3090 * the sc_* and sc[0-9]_* forms, an optional table argument may be
3091 * passed. When present, the currently tracked key is then looked up
3092 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003093 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01003094 * multiple tables). <strm> is allowed to be NULL, in which case only
3095 * the session will be consulted.
3096 */
3097struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003098smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003099{
Willy Tarreau7d562212016-11-25 16:10:05 +01003100 struct stkctr *stkptr;
3101 struct stksess *stksess;
3102 unsigned int num = kw[2] - '0';
3103 int arg = 0;
3104
3105 if (num == '_' - '0') {
3106 /* sc_* variant, args[0] = ctr# (mandatory) */
3107 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01003108 }
3109 else if (num > 9) { /* src_* variant, args[0] = table */
3110 struct stktable_key *key;
3111 struct connection *conn = objt_conn(sess->origin);
3112 struct sample smp;
3113
3114 if (!conn)
3115 return NULL;
3116
Joseph Herlant5662fa42018-11-15 13:43:28 -08003117 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003118 smp.px = NULL;
3119 smp.sess = sess;
3120 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003121 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003122 return NULL;
3123
3124 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003125 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003126 if (!key)
3127 return NULL;
3128
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003129 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003130 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
3131 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003132 }
3133
3134 /* Here, <num> contains the counter number from 0 to 9 for
3135 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
3136 * args[arg] is the first optional argument. We first lookup the
3137 * ctr form the stream, then from the session if it was not there.
Willy Tarreau6c011712023-01-06 16:09:58 +01003138 * But we must be sure the counter does not exceed global.tune.nb_stk_ctr.
Willy Tarreau7d562212016-11-25 16:10:05 +01003139 */
Willy Tarreau6c011712023-01-06 16:09:58 +01003140 if (num >= global.tune.nb_stk_ctr)
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02003141 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003142
Willy Tarreau6c011712023-01-06 16:09:58 +01003143 stkptr = NULL;
3144 if (strm && strm->stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003145 stkptr = &strm->stkctr[num];
Willy Tarreau6c011712023-01-06 16:09:58 +01003146 if (!strm || !stkptr || !stkctr_entry(stkptr)) {
3147 if (sess->stkctr)
3148 stkptr = &sess->stkctr[num];
3149 else
3150 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003151 if (!stkctr_entry(stkptr))
3152 return NULL;
3153 }
3154
3155 stksess = stkctr_entry(stkptr);
3156 if (!stksess)
3157 return NULL;
3158
3159 if (unlikely(args[arg].type == ARGT_TAB)) {
3160 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003161 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003162 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
3163 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003164 }
3165 return stkptr;
3166}
3167
3168/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
3169 * the entry if it doesn't exist yet. This is needed for a few fetch
3170 * functions which need to create an entry, such as src_inc_gpc* and
3171 * src_clr_gpc*.
3172 */
3173struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003174smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003175{
Willy Tarreau7d562212016-11-25 16:10:05 +01003176 struct stktable_key *key;
3177 struct connection *conn = objt_conn(sess->origin);
3178 struct sample smp;
3179
3180 if (strncmp(kw, "src_", 4) != 0)
3181 return NULL;
3182
3183 if (!conn)
3184 return NULL;
3185
Joseph Herlant5662fa42018-11-15 13:43:28 -08003186 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003187 smp.px = NULL;
3188 smp.sess = sess;
3189 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003190 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003191 return NULL;
3192
3193 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003194 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003195 if (!key)
3196 return NULL;
3197
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003198 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003199 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
3200 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003201}
3202
3203/* set return a boolean indicating if the requested stream counter is
3204 * currently being tracked or not.
3205 * Supports being called as "sc[0-9]_tracked" only.
3206 */
3207static int
3208smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
3209{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003210 struct stkctr tmpstkctr;
3211 struct stkctr *stkctr;
3212
Willy Tarreau7d562212016-11-25 16:10:05 +01003213 smp->flags = SMP_F_VOL_TEST;
3214 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003215 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3216 smp->data.u.sint = !!stkctr;
3217
3218 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02003219 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02003220 stktable_release(stkctr->table, stkctr_entry(stkctr));
3221
Emeric Brun877b0b52021-06-30 18:57:49 +02003222 return 1;
3223}
3224
3225/* set <smp> to the General Purpose Tag of index set as first arg
3226 * to value from the stream's tracked frontend counters or from the src.
3227 * Supports being called as "sc_get_gpt(<gpt-idx>,<sc-idx>[,<table>])" or
3228 * "src_get_gpt(<gpt-idx>[,<table>])" only. Value zero is returned if
3229 * the key is new or gpt is not stored.
3230 */
3231static int
3232smp_fetch_sc_get_gpt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3233{
3234 struct stkctr tmpstkctr;
3235 struct stkctr *stkctr;
3236 unsigned int idx;
3237
3238 idx = args[0].data.sint;
3239
3240 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3241 if (!stkctr)
3242 return 0;
3243
3244 smp->flags = SMP_F_VOL_TEST;
3245 smp->data.type = SMP_T_SINT;
3246 smp->data.u.sint = 0;
3247
3248 if (stkctr_entry(stkctr)) {
3249 void *ptr;
3250
3251 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, idx);
3252 if (!ptr) {
3253 if (stkctr == &tmpstkctr)
3254 stktable_release(stkctr->table, stkctr_entry(stkctr));
3255 return 0; /* parameter not stored */
3256 }
3257
3258 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3259
3260 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3261
3262 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3263
3264 if (stkctr == &tmpstkctr)
3265 stktable_release(stkctr->table, stkctr_entry(stkctr));
3266 }
Willy Tarreau7d562212016-11-25 16:10:05 +01003267 return 1;
3268}
3269
3270/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
3271 * frontend counters or from the src.
3272 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
3273 * zero is returned if the key is new.
3274 */
3275static int
3276smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3277{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003278 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003279 struct stkctr *stkctr;
3280
Emeric Brun819fc6f2017-06-13 19:37:32 +02003281 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003282 if (!stkctr)
3283 return 0;
3284
3285 smp->flags = SMP_F_VOL_TEST;
3286 smp->data.type = SMP_T_SINT;
3287 smp->data.u.sint = 0;
3288
Emeric Brun819fc6f2017-06-13 19:37:32 +02003289 if (stkctr_entry(stkctr)) {
3290 void *ptr;
3291
3292 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02003293 if (!ptr)
3294 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, 0);
3295
Emeric Brun4d7ada82021-06-30 19:04:16 +02003296 if (!ptr) {
3297 if (stkctr == &tmpstkctr)
3298 stktable_release(stkctr->table, stkctr_entry(stkctr));
3299 return 0; /* parameter not stored */
3300 }
3301
3302 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3303
3304 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3305
3306 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3307
3308 if (stkctr == &tmpstkctr)
3309 stktable_release(stkctr->table, stkctr_entry(stkctr));
3310 }
3311 return 1;
3312}
3313
3314/* set <smp> to the GPC[args(0)]'s value from the stream's tracked
3315 * frontend counters or from the src.
3316 * Supports being called as "sc_get_gpc(<gpc-idx>,<sc-idx>[,<table>])" or
3317 * "src_get_gpc(<gpc-idx>[,<table>])" only. Value
3318 * Value zero is returned if the key is new or gpc is not stored.
3319 */
3320static int
3321smp_fetch_sc_get_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3322{
3323 struct stkctr tmpstkctr;
3324 struct stkctr *stkctr;
3325 unsigned int idx;
3326
3327 idx = args[0].data.sint;
3328
3329 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3330 if (!stkctr)
3331 return 0;
3332
3333 smp->flags = SMP_F_VOL_TEST;
3334 smp->data.type = SMP_T_SINT;
3335 smp->data.u.sint = 0;
3336
3337 if (stkctr_entry(stkctr) != NULL) {
3338 void *ptr;
3339
3340 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003341 if (!ptr) {
3342 if (stkctr == &tmpstkctr)
3343 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003344 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003345 }
3346
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003347 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003348
Emeric Brun0e3457b2021-06-30 17:18:28 +02003349 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003350
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003351 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003352
3353 if (stkctr == &tmpstkctr)
3354 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003355 }
3356 return 1;
3357}
3358
3359/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
3360 * frontend counters or from the src.
3361 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
3362 * zero is returned if the key is new.
3363 */
3364static int
3365smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3366{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003367 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003368 struct stkctr *stkctr;
3369
Emeric Brun819fc6f2017-06-13 19:37:32 +02003370 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003371 if (!stkctr)
3372 return 0;
3373
3374 smp->flags = SMP_F_VOL_TEST;
3375 smp->data.type = SMP_T_SINT;
3376 smp->data.u.sint = 0;
3377
3378 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003379 void *ptr;
3380
3381 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3382 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003383 /* fallback on the gpc array */
3384 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3385 }
3386
3387 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003388 if (stkctr == &tmpstkctr)
3389 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003390 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003391 }
3392
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003393 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003394
Emeric Brun0e3457b2021-06-30 17:18:28 +02003395 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003396
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003397 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003398
3399 if (stkctr == &tmpstkctr)
3400 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003401 }
3402 return 1;
3403}
3404
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003405/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
3406 * frontend counters or from the src.
3407 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
3408 * zero is returned if the key is new.
3409 */
3410static int
3411smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3412{
3413 struct stkctr tmpstkctr;
3414 struct stkctr *stkctr;
3415
3416 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3417 if (!stkctr)
3418 return 0;
3419
3420 smp->flags = SMP_F_VOL_TEST;
3421 smp->data.type = SMP_T_SINT;
3422 smp->data.u.sint = 0;
3423
3424 if (stkctr_entry(stkctr) != NULL) {
3425 void *ptr;
3426
3427 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3428 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003429 /* fallback on the gpc array */
3430 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3431 }
3432
3433 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003434 if (stkctr == &tmpstkctr)
3435 stktable_release(stkctr->table, stkctr_entry(stkctr));
3436 return 0; /* parameter not stored */
3437 }
3438
3439 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3440
Emeric Brun0e3457b2021-06-30 17:18:28 +02003441 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003442
3443 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3444
3445 if (stkctr == &tmpstkctr)
3446 stktable_release(stkctr->table, stkctr_entry(stkctr));
3447 }
3448 return 1;
3449}
3450
Emeric Brun4d7ada82021-06-30 19:04:16 +02003451/* set <smp> to the GPC[args(0)]'s event rate from the stream's
3452 * tracked frontend counters or from the src.
3453 * Supports being called as "sc_gpc_rate(<gpc-idx>,<sc-idx>[,<table])"
3454 * or "src_gpc_rate(<gpc-idx>[,<table>])" only.
3455 * Value zero is returned if the key is new or gpc_rate is not stored.
3456 */
3457static int
3458smp_fetch_sc_gpc_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3459{
3460 struct stkctr tmpstkctr;
3461 struct stkctr *stkctr;
3462 unsigned int idx;
3463
3464 idx = args[0].data.sint;
3465
3466 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3467 if (!stkctr)
3468 return 0;
3469
3470 smp->flags = SMP_F_VOL_TEST;
3471 smp->data.type = SMP_T_SINT;
3472 smp->data.u.sint = 0;
3473 if (stkctr_entry(stkctr) != NULL) {
3474 void *ptr;
3475
3476 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3477 if (!ptr) {
3478 if (stkctr == &tmpstkctr)
3479 stktable_release(stkctr->table, stkctr_entry(stkctr));
3480 return 0; /* parameter not stored */
3481 }
3482
3483 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3484
3485 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3486 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u);
3487
3488 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3489
3490 if (stkctr == &tmpstkctr)
3491 stktable_release(stkctr->table, stkctr_entry(stkctr));
3492 }
3493 return 1;
3494}
3495
Willy Tarreau7d562212016-11-25 16:10:05 +01003496/* set <smp> to the General Purpose Counter 0's event rate from the stream's
3497 * tracked frontend counters or from the src.
3498 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
3499 * Value zero is returned if the key is new.
3500 */
3501static int
3502smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3503{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003504 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003505 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003506 unsigned int period;
Willy Tarreau7d562212016-11-25 16:10:05 +01003507
Emeric Brun819fc6f2017-06-13 19:37:32 +02003508 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003509 if (!stkctr)
3510 return 0;
3511
3512 smp->flags = SMP_F_VOL_TEST;
3513 smp->data.type = SMP_T_SINT;
3514 smp->data.u.sint = 0;
3515 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003516 void *ptr;
3517
3518 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003519 if (ptr) {
3520 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3521 }
3522 else {
3523 /* fallback on the gpc array */
3524 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3525 if (ptr)
3526 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3527 }
3528
Emeric Brun819fc6f2017-06-13 19:37:32 +02003529 if (!ptr) {
3530 if (stkctr == &tmpstkctr)
3531 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003532 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003533 }
3534
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003535 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003536
Emeric Brun726783d2021-06-30 19:06:43 +02003537 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003538
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003539 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003540
3541 if (stkctr == &tmpstkctr)
3542 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003543 }
3544 return 1;
3545}
3546
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003547/* set <smp> to the General Purpose Counter 1's event rate from the stream's
3548 * tracked frontend counters or from the src.
3549 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
3550 * Value zero is returned if the key is new.
3551 */
3552static int
3553smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3554{
3555 struct stkctr tmpstkctr;
3556 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003557 unsigned int period;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003558
3559 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3560 if (!stkctr)
3561 return 0;
3562
3563 smp->flags = SMP_F_VOL_TEST;
3564 smp->data.type = SMP_T_SINT;
3565 smp->data.u.sint = 0;
3566 if (stkctr_entry(stkctr) != NULL) {
3567 void *ptr;
3568
3569 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003570 if (ptr) {
3571 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3572 }
3573 else {
3574 /* fallback on the gpc array */
3575 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3576 if (ptr)
3577 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3578 }
3579
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003580 if (!ptr) {
3581 if (stkctr == &tmpstkctr)
3582 stktable_release(stkctr->table, stkctr_entry(stkctr));
3583 return 0; /* parameter not stored */
3584 }
3585
3586 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3587
Emeric Brun726783d2021-06-30 19:06:43 +02003588 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003589
3590 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3591
3592 if (stkctr == &tmpstkctr)
3593 stktable_release(stkctr->table, stkctr_entry(stkctr));
3594 }
3595 return 1;
3596}
3597
Emeric Brun4d7ada82021-06-30 19:04:16 +02003598/* Increment the GPC[args(0)] value from the stream's tracked
3599 * frontend counters and return it into temp integer.
3600 * Supports being called as "sc_inc_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3601 * or "src_inc_gpc(<gpc-idx>[,<table>])" only.
3602 */
3603static int
3604smp_fetch_sc_inc_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3605{
3606 struct stkctr tmpstkctr;
3607 struct stkctr *stkctr;
3608 unsigned int idx;
3609
3610 idx = args[0].data.sint;
3611
3612 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3613 if (!stkctr)
3614 return 0;
3615
3616 smp->flags = SMP_F_VOL_TEST;
3617 smp->data.type = SMP_T_SINT;
3618 smp->data.u.sint = 0;
3619
3620 if (!stkctr_entry(stkctr))
Amaury Denoyelle0a43b992024-07-18 14:48:55 +02003621 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
Emeric Brun4d7ada82021-06-30 19:04:16 +02003622
3623 if (stkctr && stkctr_entry(stkctr)) {
3624 void *ptr1,*ptr2;
3625
3626
3627 /* First, update gpc0_rate if it's tracked. Second, update its
3628 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3629 */
3630 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3631 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3632 if (ptr1 || ptr2) {
3633 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3634
3635 if (ptr1) {
3636 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
3637 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
3638 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
3639 }
3640
3641 if (ptr2)
3642 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
3643
3644 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3645
3646 /* If data was modified, we need to touch to re-schedule sync */
3647 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3648 }
3649 else if (stkctr == &tmpstkctr)
3650 stktable_release(stkctr->table, stkctr_entry(stkctr));
3651 }
3652 return 1;
3653}
3654
Willy Tarreau7d562212016-11-25 16:10:05 +01003655/* Increment the General Purpose Counter 0 value from the stream's tracked
3656 * frontend counters and return it into temp integer.
3657 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
3658 */
3659static int
3660smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3661{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003662 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003663 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003664 unsigned int period = 0;
Willy Tarreau7d562212016-11-25 16:10:05 +01003665
Emeric Brun819fc6f2017-06-13 19:37:32 +02003666 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003667 if (!stkctr)
3668 return 0;
3669
3670 smp->flags = SMP_F_VOL_TEST;
3671 smp->data.type = SMP_T_SINT;
3672 smp->data.u.sint = 0;
3673
Emeric Brun819fc6f2017-06-13 19:37:32 +02003674 if (!stkctr_entry(stkctr))
3675 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003676
3677 if (stkctr && stkctr_entry(stkctr)) {
3678 void *ptr1,*ptr2;
3679
Emeric Brun819fc6f2017-06-13 19:37:32 +02003680
Willy Tarreau7d562212016-11-25 16:10:05 +01003681 /* First, update gpc0_rate if it's tracked. Second, update its
3682 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3683 */
3684 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003685 if (ptr1) {
3686 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3687 }
3688 else {
3689 /* fallback on the gpc array */
3690 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3691 if (ptr1)
3692 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3693 }
3694
Willy Tarreau7d562212016-11-25 16:10:05 +01003695 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02003696 if (!ptr2) {
3697 /* fallback on the gpc array */
3698 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3699 }
3700
Emeric Brun819fc6f2017-06-13 19:37:32 +02003701 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003702 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01003703
Emeric Brun819fc6f2017-06-13 19:37:32 +02003704 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003705 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003706 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003707 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003708 }
3709
3710 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003711 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003712
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003713 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003714
3715 /* If data was modified, we need to touch to re-schedule sync */
3716 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3717 }
3718 else if (stkctr == &tmpstkctr)
3719 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003720 }
3721 return 1;
3722}
3723
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003724/* Increment the General Purpose Counter 1 value from the stream's tracked
3725 * frontend counters and return it into temp integer.
3726 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
3727 */
3728static int
3729smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3730{
3731 struct stkctr tmpstkctr;
3732 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003733 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003734
3735 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3736 if (!stkctr)
3737 return 0;
3738
3739 smp->flags = SMP_F_VOL_TEST;
3740 smp->data.type = SMP_T_SINT;
3741 smp->data.u.sint = 0;
3742
3743 if (!stkctr_entry(stkctr))
3744 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3745
3746 if (stkctr && stkctr_entry(stkctr)) {
3747 void *ptr1,*ptr2;
3748
3749
3750 /* First, update gpc1_rate if it's tracked. Second, update its
3751 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
3752 */
3753 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003754 if (ptr1) {
3755 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3756 }
3757 else {
3758 /* fallback on the gpc array */
3759 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3760 if (ptr1)
3761 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3762 }
3763
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003764 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02003765 if (!ptr2) {
3766 /* fallback on the gpc array */
3767 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3768 }
3769
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003770 if (ptr1 || ptr2) {
3771 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3772
3773 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003774 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003775 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003776 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003777 }
3778
3779 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003780 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003781
3782 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3783
3784 /* If data was modified, we need to touch to re-schedule sync */
3785 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3786 }
3787 else if (stkctr == &tmpstkctr)
3788 stktable_release(stkctr->table, stkctr_entry(stkctr));
3789 }
3790 return 1;
3791}
3792
Emeric Brun4d7ada82021-06-30 19:04:16 +02003793/* Clear the GPC[args(0)] value from the stream's tracked
3794 * frontend counters and return its previous value into temp integer.
3795 * Supports being called as "sc_clr_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3796 * or "src_clr_gpc(<gpc-idx>[,<table>])" only.
3797 */
3798static int
3799smp_fetch_sc_clr_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3800{
3801 struct stkctr tmpstkctr;
3802 struct stkctr *stkctr;
3803 unsigned int idx;
3804
3805 idx = args[0].data.sint;
3806
3807 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3808 if (!stkctr)
3809 return 0;
3810
3811 smp->flags = SMP_F_VOL_TEST;
3812 smp->data.type = SMP_T_SINT;
3813 smp->data.u.sint = 0;
3814
3815 if (!stkctr_entry(stkctr))
3816 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3817
3818 if (stkctr && stkctr_entry(stkctr)) {
3819 void *ptr;
3820
3821 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3822 if (!ptr) {
3823 if (stkctr == &tmpstkctr)
3824 stktable_release(stkctr->table, stkctr_entry(stkctr));
3825 return 0; /* parameter not stored */
3826 }
3827
3828 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3829
3830 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3831 stktable_data_cast(ptr, std_t_uint) = 0;
3832
3833 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3834
3835 /* If data was modified, we need to touch to re-schedule sync */
3836 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3837 }
3838 return 1;
3839}
3840
Willy Tarreau7d562212016-11-25 16:10:05 +01003841/* Clear the General Purpose Counter 0 value from the stream's tracked
3842 * frontend counters and return its previous value into temp integer.
3843 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
3844 */
3845static int
3846smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3847{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003848 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003849 struct stkctr *stkctr;
3850
Emeric Brun819fc6f2017-06-13 19:37:32 +02003851 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003852 if (!stkctr)
3853 return 0;
3854
3855 smp->flags = SMP_F_VOL_TEST;
3856 smp->data.type = SMP_T_SINT;
3857 smp->data.u.sint = 0;
3858
Emeric Brun819fc6f2017-06-13 19:37:32 +02003859 if (!stkctr_entry(stkctr))
3860 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003861
Emeric Brun819fc6f2017-06-13 19:37:32 +02003862 if (stkctr && stkctr_entry(stkctr)) {
3863 void *ptr;
3864
3865 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3866 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003867 /* fallback on the gpc array */
3868 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3869 }
3870
3871 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003872 if (stkctr == &tmpstkctr)
3873 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003874 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003875 }
3876
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003877 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003878
Emeric Brun0e3457b2021-06-30 17:18:28 +02003879 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3880 stktable_data_cast(ptr, std_t_uint) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003881
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003882 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003883
Willy Tarreau7d562212016-11-25 16:10:05 +01003884 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003885 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01003886 }
3887 return 1;
3888}
3889
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003890/* Clear the General Purpose Counter 1 value from the stream's tracked
3891 * frontend counters and return its previous value into temp integer.
3892 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
3893 */
3894static int
3895smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3896{
3897 struct stkctr tmpstkctr;
3898 struct stkctr *stkctr;
3899
3900 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3901 if (!stkctr)
3902 return 0;
3903
3904 smp->flags = SMP_F_VOL_TEST;
3905 smp->data.type = SMP_T_SINT;
3906 smp->data.u.sint = 0;
3907
3908 if (!stkctr_entry(stkctr))
3909 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3910
3911 if (stkctr && stkctr_entry(stkctr)) {
3912 void *ptr;
3913
3914 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3915 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003916 /* fallback on the gpc array */
3917 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3918 }
3919
3920 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003921 if (stkctr == &tmpstkctr)
3922 stktable_release(stkctr->table, stkctr_entry(stkctr));
3923 return 0; /* parameter not stored */
3924 }
3925
3926 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3927
Emeric Brun0e3457b2021-06-30 17:18:28 +02003928 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3929 stktable_data_cast(ptr, std_t_uint) = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003930
3931 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3932
3933 /* If data was modified, we need to touch to re-schedule sync */
3934 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3935 }
3936 return 1;
3937}
3938
Willy Tarreau7d562212016-11-25 16:10:05 +01003939/* set <smp> to the cumulated number of connections from the stream's tracked
3940 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3941 * "src_conn_cnt" only.
3942 */
3943static int
3944smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3945{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003946 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003947 struct stkctr *stkctr;
3948
Emeric Brun819fc6f2017-06-13 19:37:32 +02003949 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003950 if (!stkctr)
3951 return 0;
3952
3953 smp->flags = SMP_F_VOL_TEST;
3954 smp->data.type = SMP_T_SINT;
3955 smp->data.u.sint = 0;
3956 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003957 void *ptr;
3958
3959 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
3960 if (!ptr) {
3961 if (stkctr == &tmpstkctr)
3962 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003963 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003964 }
3965
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003966 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003967
Emeric Brun0e3457b2021-06-30 17:18:28 +02003968 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003969
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003970 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003971
3972 if (stkctr == &tmpstkctr)
3973 stktable_release(stkctr->table, stkctr_entry(stkctr));
3974
3975
Willy Tarreau7d562212016-11-25 16:10:05 +01003976 }
3977 return 1;
3978}
3979
3980/* set <smp> to the connection rate from the stream's tracked frontend
3981 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3982 * only.
3983 */
3984static int
3985smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3986{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003987 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003988 struct stkctr *stkctr;
3989
Emeric Brun819fc6f2017-06-13 19:37:32 +02003990 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003991 if (!stkctr)
3992 return 0;
3993
3994 smp->flags = SMP_F_VOL_TEST;
3995 smp->data.type = SMP_T_SINT;
3996 smp->data.u.sint = 0;
3997 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003998 void *ptr;
3999
4000 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
4001 if (!ptr) {
4002 if (stkctr == &tmpstkctr)
4003 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004004 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004005 }
4006
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004007 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004008
Emeric Brun0e3457b2021-06-30 17:18:28 +02004009 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004010 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004011
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004012 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004013
4014 if (stkctr == &tmpstkctr)
4015 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004016 }
4017 return 1;
4018}
4019
4020/* set temp integer to the number of connections from the stream's source address
4021 * in the table pointed to by expr, after updating it.
4022 * Accepts exactly 1 argument of type table.
4023 */
4024static int
4025smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4026{
4027 struct connection *conn = objt_conn(smp->sess->origin);
4028 struct stksess *ts;
4029 struct stktable_key *key;
4030 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004031 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004032
4033 if (!conn)
4034 return 0;
4035
Joseph Herlant5662fa42018-11-15 13:43:28 -08004036 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02004037 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01004038 return 0;
4039
4040 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004041 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01004042 if (!key)
4043 return 0;
4044
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004045 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004046
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004047 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01004048 /* entry does not exist and could not be created */
4049 return 0;
4050
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004051 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004052 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01004053 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004054 }
Willy Tarreau7d562212016-11-25 16:10:05 +01004055
4056 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004057
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004058 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004059
Emeric Brun0e3457b2021-06-30 17:18:28 +02004060 smp->data.u.sint = ++stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004061
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004062 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004063
Willy Tarreau7d562212016-11-25 16:10:05 +01004064 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004065
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004066 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004067
4068 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01004069 return 1;
4070}
4071
4072/* set <smp> to the number of concurrent connections from the stream's tracked
4073 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
4074 * "src_conn_cur" only.
4075 */
4076static int
4077smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
4078{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004079 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004080 struct stkctr *stkctr;
4081
Emeric Brun819fc6f2017-06-13 19:37:32 +02004082 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004083 if (!stkctr)
4084 return 0;
4085
4086 smp->flags = SMP_F_VOL_TEST;
4087 smp->data.type = SMP_T_SINT;
4088 smp->data.u.sint = 0;
4089 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004090 void *ptr;
4091
4092 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
4093 if (!ptr) {
4094 if (stkctr == &tmpstkctr)
4095 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004096 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004097 }
4098
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004099 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004100
Emeric Brun0e3457b2021-06-30 17:18:28 +02004101 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004102
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004103 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004104
4105 if (stkctr == &tmpstkctr)
4106 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004107 }
4108 return 1;
4109}
4110
4111/* set <smp> to the cumulated number of streams from the stream's tracked
4112 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
4113 * "src_sess_cnt" only.
4114 */
4115static int
4116smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4117{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004118 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004119 struct stkctr *stkctr;
4120
Emeric Brun819fc6f2017-06-13 19:37:32 +02004121 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004122 if (!stkctr)
4123 return 0;
4124
4125 smp->flags = SMP_F_VOL_TEST;
4126 smp->data.type = SMP_T_SINT;
4127 smp->data.u.sint = 0;
4128 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004129 void *ptr;
4130
4131 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
4132 if (!ptr) {
4133 if (stkctr == &tmpstkctr)
4134 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004135 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004136 }
4137
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004138 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004139
Emeric Brun0e3457b2021-06-30 17:18:28 +02004140 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004141
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004142 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004143
4144 if (stkctr == &tmpstkctr)
4145 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004146 }
4147 return 1;
4148}
4149
4150/* set <smp> to the stream rate from the stream's tracked frontend counters.
4151 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
4152 */
4153static int
4154smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4155{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004156 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004157 struct stkctr *stkctr;
4158
Emeric Brun819fc6f2017-06-13 19:37:32 +02004159 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004160 if (!stkctr)
4161 return 0;
4162
4163 smp->flags = SMP_F_VOL_TEST;
4164 smp->data.type = SMP_T_SINT;
4165 smp->data.u.sint = 0;
4166 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004167 void *ptr;
4168
4169 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
4170 if (!ptr) {
4171 if (stkctr == &tmpstkctr)
4172 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004173 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004174 }
4175
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004176 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004177
Emeric Brun0e3457b2021-06-30 17:18:28 +02004178 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004179 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004180
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004181 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004182
4183 if (stkctr == &tmpstkctr)
4184 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004185 }
4186 return 1;
4187}
4188
4189/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
4190 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
4191 * "src_http_req_cnt" only.
4192 */
4193static int
4194smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4195{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004196 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004197 struct stkctr *stkctr;
4198
Emeric Brun819fc6f2017-06-13 19:37:32 +02004199 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004200 if (!stkctr)
4201 return 0;
4202
4203 smp->flags = SMP_F_VOL_TEST;
4204 smp->data.type = SMP_T_SINT;
4205 smp->data.u.sint = 0;
4206 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004207 void *ptr;
4208
4209 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
4210 if (!ptr) {
4211 if (stkctr == &tmpstkctr)
4212 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004213 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004214 }
4215
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004216 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004217
Emeric Brun0e3457b2021-06-30 17:18:28 +02004218 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004219
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004220 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004221
4222 if (stkctr == &tmpstkctr)
4223 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004224 }
4225 return 1;
4226}
4227
4228/* set <smp> to the HTTP request rate from the stream's tracked frontend
4229 * counters. Supports being called as "sc[0-9]_http_req_rate" or
4230 * "src_http_req_rate" only.
4231 */
4232static int
4233smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4234{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004235 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004236 struct stkctr *stkctr;
4237
Emeric Brun819fc6f2017-06-13 19:37:32 +02004238 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004239 if (!stkctr)
4240 return 0;
4241
4242 smp->flags = SMP_F_VOL_TEST;
4243 smp->data.type = SMP_T_SINT;
4244 smp->data.u.sint = 0;
4245 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004246 void *ptr;
4247
4248 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
4249 if (!ptr) {
4250 if (stkctr == &tmpstkctr)
4251 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004252 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004253 }
4254
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004255 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004256
Emeric Brun0e3457b2021-06-30 17:18:28 +02004257 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004258 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004259
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004260 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004261
4262 if (stkctr == &tmpstkctr)
4263 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004264 }
4265 return 1;
4266}
4267
4268/* set <smp> to the cumulated number of HTTP requests errors from the stream's
4269 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
4270 * "src_http_err_cnt" only.
4271 */
4272static int
4273smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4274{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004275 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004276 struct stkctr *stkctr;
4277
Emeric Brun819fc6f2017-06-13 19:37:32 +02004278 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004279 if (!stkctr)
4280 return 0;
4281
4282 smp->flags = SMP_F_VOL_TEST;
4283 smp->data.type = SMP_T_SINT;
4284 smp->data.u.sint = 0;
4285 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004286 void *ptr;
4287
4288 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
4289 if (!ptr) {
4290 if (stkctr == &tmpstkctr)
4291 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004292 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004293 }
4294
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004295 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004296
Emeric Brun0e3457b2021-06-30 17:18:28 +02004297 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004298
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004299 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004300
4301 if (stkctr == &tmpstkctr)
4302 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004303 }
4304 return 1;
4305}
4306
4307/* set <smp> to the HTTP request error rate from the stream's tracked frontend
4308 * counters. Supports being called as "sc[0-9]_http_err_rate" or
4309 * "src_http_err_rate" only.
4310 */
4311static int
4312smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4313{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004314 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004315 struct stkctr *stkctr;
4316
Emeric Brun819fc6f2017-06-13 19:37:32 +02004317 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004318 if (!stkctr)
4319 return 0;
4320
4321 smp->flags = SMP_F_VOL_TEST;
4322 smp->data.type = SMP_T_SINT;
4323 smp->data.u.sint = 0;
4324 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004325 void *ptr;
4326
4327 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
4328 if (!ptr) {
4329 if (stkctr == &tmpstkctr)
4330 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004331 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004332 }
4333
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004334 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004335
Emeric Brun0e3457b2021-06-30 17:18:28 +02004336 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004337 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004338
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004339 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004340
4341 if (stkctr == &tmpstkctr)
4342 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004343 }
4344 return 1;
4345}
4346
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004347/* set <smp> to the cumulated number of HTTP response failures from the stream's
4348 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
4349 * "src_http_fail_cnt" only.
4350 */
4351static int
4352smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4353{
4354 struct stkctr tmpstkctr;
4355 struct stkctr *stkctr;
4356
4357 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4358 if (!stkctr)
4359 return 0;
4360
4361 smp->flags = SMP_F_VOL_TEST;
4362 smp->data.type = SMP_T_SINT;
4363 smp->data.u.sint = 0;
4364 if (stkctr_entry(stkctr) != NULL) {
4365 void *ptr;
4366
4367 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
4368 if (!ptr) {
4369 if (stkctr == &tmpstkctr)
4370 stktable_release(stkctr->table, stkctr_entry(stkctr));
4371 return 0; /* parameter not stored */
4372 }
4373
4374 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4375
Emeric Brun0e3457b2021-06-30 17:18:28 +02004376 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004377
4378 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4379
4380 if (stkctr == &tmpstkctr)
4381 stktable_release(stkctr->table, stkctr_entry(stkctr));
4382 }
4383 return 1;
4384}
4385
4386/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
4387 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
4388 * "src_http_fail_rate" only.
4389 */
4390static int
4391smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4392{
4393 struct stkctr tmpstkctr;
4394 struct stkctr *stkctr;
4395
4396 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4397 if (!stkctr)
4398 return 0;
4399
4400 smp->flags = SMP_F_VOL_TEST;
4401 smp->data.type = SMP_T_SINT;
4402 smp->data.u.sint = 0;
4403 if (stkctr_entry(stkctr) != NULL) {
4404 void *ptr;
4405
4406 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
4407 if (!ptr) {
4408 if (stkctr == &tmpstkctr)
4409 stktable_release(stkctr->table, stkctr_entry(stkctr));
4410 return 0; /* parameter not stored */
4411 }
4412
4413 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4414
Emeric Brun0e3457b2021-06-30 17:18:28 +02004415 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004416 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
4417
4418 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4419
4420 if (stkctr == &tmpstkctr)
4421 stktable_release(stkctr->table, stkctr_entry(stkctr));
4422 }
4423 return 1;
4424}
4425
Willy Tarreau7d562212016-11-25 16:10:05 +01004426/* set <smp> to the number of kbytes received from clients, as found in the
4427 * stream's tracked frontend counters. Supports being called as
4428 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
4429 */
4430static int
4431smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
4432{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004433 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004434 struct stkctr *stkctr;
4435
Emeric Brun819fc6f2017-06-13 19:37:32 +02004436 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004437 if (!stkctr)
4438 return 0;
4439
4440 smp->flags = SMP_F_VOL_TEST;
4441 smp->data.type = SMP_T_SINT;
4442 smp->data.u.sint = 0;
4443 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004444 void *ptr;
4445
4446 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
4447 if (!ptr) {
4448 if (stkctr == &tmpstkctr)
4449 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004450 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004451 }
4452
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004453 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004454
Emeric Brun0e3457b2021-06-30 17:18:28 +02004455 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004456
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004457 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004458
4459 if (stkctr == &tmpstkctr)
4460 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004461 }
4462 return 1;
4463}
4464
4465/* set <smp> to the data rate received from clients in bytes/s, as found
4466 * in the stream's tracked frontend counters. Supports being called as
4467 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
4468 */
4469static int
4470smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4471{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004472 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004473 struct stkctr *stkctr;
4474
Emeric Brun819fc6f2017-06-13 19:37:32 +02004475 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004476 if (!stkctr)
4477 return 0;
4478
4479 smp->flags = SMP_F_VOL_TEST;
4480 smp->data.type = SMP_T_SINT;
4481 smp->data.u.sint = 0;
4482 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004483 void *ptr;
4484
4485 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
4486 if (!ptr) {
4487 if (stkctr == &tmpstkctr)
4488 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004489 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004490 }
4491
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004492 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004493
Emeric Brun0e3457b2021-06-30 17:18:28 +02004494 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004495 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004496
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004497 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004498
4499 if (stkctr == &tmpstkctr)
4500 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004501 }
4502 return 1;
4503}
4504
4505/* set <smp> to the number of kbytes sent to clients, as found in the
4506 * stream's tracked frontend counters. Supports being called as
4507 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
4508 */
4509static int
4510smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
4511{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004512 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004513 struct stkctr *stkctr;
4514
Emeric Brun819fc6f2017-06-13 19:37:32 +02004515 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004516 if (!stkctr)
4517 return 0;
4518
4519 smp->flags = SMP_F_VOL_TEST;
4520 smp->data.type = SMP_T_SINT;
4521 smp->data.u.sint = 0;
4522 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004523 void *ptr;
4524
4525 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
4526 if (!ptr) {
4527 if (stkctr == &tmpstkctr)
4528 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004529 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004530 }
4531
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004532 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004533
Emeric Brun0e3457b2021-06-30 17:18:28 +02004534 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004535
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004536 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004537
4538 if (stkctr == &tmpstkctr)
4539 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004540 }
4541 return 1;
4542}
4543
4544/* set <smp> to the data rate sent to clients in bytes/s, as found in the
4545 * stream's tracked frontend counters. Supports being called as
4546 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
4547 */
4548static int
4549smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4550{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004551 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004552 struct stkctr *stkctr;
4553
Emeric Brun819fc6f2017-06-13 19:37:32 +02004554 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004555 if (!stkctr)
4556 return 0;
4557
4558 smp->flags = SMP_F_VOL_TEST;
4559 smp->data.type = SMP_T_SINT;
4560 smp->data.u.sint = 0;
4561 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004562 void *ptr;
4563
4564 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
4565 if (!ptr) {
4566 if (stkctr == &tmpstkctr)
4567 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004568 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004569 }
4570
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004571 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004572
Emeric Brun0e3457b2021-06-30 17:18:28 +02004573 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004574 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004575
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004576 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004577
4578 if (stkctr == &tmpstkctr)
4579 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004580 }
4581 return 1;
4582}
4583
4584/* set <smp> to the number of active trackers on the SC entry in the stream's
4585 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
4586 */
4587static int
4588smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
4589{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004590 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004591 struct stkctr *stkctr;
4592
Emeric Brun819fc6f2017-06-13 19:37:32 +02004593 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004594 if (!stkctr)
4595 return 0;
4596
4597 smp->flags = SMP_F_VOL_TEST;
4598 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004599 if (stkctr == &tmpstkctr) {
4600 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
4601 stktable_release(stkctr->table, stkctr_entry(stkctr));
4602 }
4603 else {
4604 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
4605 }
4606
Willy Tarreau7d562212016-11-25 16:10:05 +01004607 return 1;
4608}
4609
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004610
4611/* The functions below are used to manipulate table contents from the CLI.
4612 * There are 3 main actions, "clear", "set" and "show". The code is shared
4613 * between all actions, and the action is encoded in the void *private in
4614 * the appctx as well as in the keyword registration, among one of the
4615 * following values.
4616 */
4617
4618enum {
4619 STK_CLI_ACT_CLR,
4620 STK_CLI_ACT_SET,
4621 STK_CLI_ACT_SHOW,
4622};
4623
Willy Tarreau4596fe22022-05-17 19:07:51 +02004624/* Dump the status of a table to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004625 * read buffer. It returns 0 if the output buffer is full
4626 * and needs to be called again, otherwise non-zero.
4627 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004628static int table_dump_head_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004629 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004630 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004631{
Willy Tarreauc12b3212022-05-27 11:08:15 +02004632 struct stream *s = __sc_strm(appctx_sc(appctx));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004633
4634 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004635 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004636
4637 /* any other information should be dumped here */
4638
William Lallemand07a62f72017-05-24 00:57:40 +02004639 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004640 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
4641
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004642 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004643 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004644
4645 return 1;
4646}
4647
Willy Tarreau4596fe22022-05-17 19:07:51 +02004648/* Dump a table entry to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004649 * read buffer. It returns 0 if the output buffer is full
4650 * and needs to be called again, otherwise non-zero.
4651 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004652static int table_dump_entry_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004653 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004654 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004655{
4656 int dt;
4657
4658 chunk_appendf(msg, "%p:", entry);
4659
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004660 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004661 char addr[INET_ADDRSTRLEN];
4662 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
4663 chunk_appendf(msg, " key=%s", addr);
4664 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004665 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004666 char addr[INET6_ADDRSTRLEN];
4667 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
4668 chunk_appendf(msg, " key=%s", addr);
4669 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004670 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01004671 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004672 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004673 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004674 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004675 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004676 }
4677 else {
4678 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004679 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004680 }
4681
Willy Tarreau16b282f2022-11-29 11:55:18 +01004682 chunk_appendf(msg, " use=%d exp=%d shard=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire), entry->shard);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004683
4684 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
4685 void *ptr;
4686
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004687 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004688 continue;
Emeric Brunc64a2a32021-06-30 18:01:02 +02004689 if (stktable_data_types[dt].is_array) {
4690 char tmp[16] = {};
4691 const char *name_pfx = stktable_data_types[dt].name;
4692 const char *name_sfx = NULL;
4693 unsigned int idx = 0;
4694 int i = 0;
4695
4696 /* split name to show index before first _ of the name
4697 * for example: 'gpc3_rate' if array name is 'gpc_rate'.
4698 */
4699 for (i = 0 ; i < (sizeof(tmp) - 1); i++) {
4700 if (!name_pfx[i])
4701 break;
4702 if (name_pfx[i] == '_') {
4703 name_pfx = &tmp[0];
4704 name_sfx = &stktable_data_types[dt].name[i];
4705 break;
4706 }
4707 tmp[i] = name_pfx[i];
4708 }
4709
4710 ptr = stktable_data_ptr_idx(t, entry, dt, idx);
4711 while (ptr) {
4712 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
4713 chunk_appendf(msg, " %s%u%s(%u)=", name_pfx, idx, name_sfx ? name_sfx : "", t->data_arg[dt].u);
4714 else
4715 chunk_appendf(msg, " %s%u%s=", name_pfx, idx, name_sfx ? name_sfx : "");
4716 switch (stktable_data_types[dt].std_type) {
4717 case STD_T_SINT:
4718 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4719 break;
4720 case STD_T_UINT:
4721 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4722 break;
4723 case STD_T_ULL:
4724 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
4725 break;
4726 case STD_T_FRQP:
4727 chunk_appendf(msg, "%u",
4728 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4729 t->data_arg[dt].u));
4730 break;
4731 }
4732 ptr = stktable_data_ptr_idx(t, entry, dt, ++idx);
4733 }
4734 continue;
4735 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004736 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brun01928ae2021-06-30 16:24:04 +02004737 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004738 else
4739 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
4740
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004741 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004742 switch (stktable_data_types[dt].std_type) {
4743 case STD_T_SINT:
4744 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4745 break;
4746 case STD_T_UINT:
4747 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4748 break;
4749 case STD_T_ULL:
Emeric Brun01928ae2021-06-30 16:24:04 +02004750 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004751 break;
4752 case STD_T_FRQP:
Emeric Brun01928ae2021-06-30 16:24:04 +02004753 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004754 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004755 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004756 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02004757 case STD_T_DICT: {
4758 struct dict_entry *de;
4759 de = stktable_data_cast(ptr, std_t_dict);
4760 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
4761 break;
4762 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004763 }
4764 }
4765 chunk_appendf(msg, "\n");
4766
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004767 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004768 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004769
4770 return 1;
4771}
4772
Willy Tarreau3c69e082022-05-03 11:35:07 +02004773/* appctx context used by the "show table" command */
4774struct show_table_ctx {
4775 void *target; /* table we want to dump, or NULL for all */
4776 struct stktable *t; /* table being currently dumped (first if NULL) */
4777 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
4778 long long value[STKTABLE_FILTER_LEN]; /* value to compare against */
4779 signed char data_type[STKTABLE_FILTER_LEN]; /* type of data to compare, or -1 if none */
4780 signed char data_op[STKTABLE_FILTER_LEN]; /* operator (STD_OP_*) when data_type set */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004781 enum {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004782 STATE_NEXT = 0, /* px points to next table, entry=NULL */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004783 STATE_DUMP, /* px points to curr table, entry is valid, refcount held */
4784 STATE_DONE, /* done dumping */
4785 } state;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004786 char action; /* action on the table : one of STK_CLI_ACT_* */
4787};
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004788
4789/* Processes a single table entry matching a specific key passed in argument.
4790 * returns 0 if wants to be called again, 1 if has ended processing.
4791 */
4792static int table_process_entry_per_key(struct appctx *appctx, char **args)
4793{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004794 struct show_table_ctx *ctx = appctx->svcctx;
4795 struct stktable *t = ctx->target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004796 struct stksess *ts;
4797 uint32_t uint32_key;
4798 unsigned char ip6_key[sizeof(struct in6_addr)];
4799 long long value;
4800 int data_type;
4801 int cur_arg;
4802 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004803 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004804
Willy Tarreau9d008692019-08-09 11:21:01 +02004805 if (!*args[4])
4806 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004807
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004808 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004809 case SMP_T_IPV4:
Aurelien DARRAGONdeb4e5e2023-08-28 13:57:19 +02004810 if (inet_pton(AF_INET, args[4], &uint32_key) <= 0)
4811 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004812 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004813 break;
4814 case SMP_T_IPV6:
Christopher Fauletb7c962b2021-11-15 09:17:25 +01004815 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
4816 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004817 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004818 break;
4819 case SMP_T_SINT:
4820 {
4821 char *endptr;
4822 unsigned long val;
4823 errno = 0;
4824 val = strtoul(args[4], &endptr, 10);
4825 if ((errno == ERANGE && val == ULONG_MAX) ||
4826 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02004827 val > 0xffffffff)
4828 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004829 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02004830 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004831 break;
4832 }
4833 break;
4834 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02004835 static_table_key.key = args[4];
4836 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004837 break;
4838 default:
Willy Tarreau3c69e082022-05-03 11:35:07 +02004839 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004840 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02004841 return cli_err(appctx, "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004842 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02004843 return cli_err(appctx, "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004844 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02004845 return cli_err(appctx, "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004846 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004847 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004848 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004849 }
4850
4851 /* check permissions */
4852 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4853 return 1;
4854
Willy Tarreau3c69e082022-05-03 11:35:07 +02004855 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004856 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004857 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004858 if (!ts)
4859 return 1;
4860 chunk_reset(&trash);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004861 if (!table_dump_head_to_buffer(&trash, appctx, t, t)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004862 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004863 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004864 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004865 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004866 if (!table_dump_entry_to_buffer(&trash, appctx, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004867 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004868 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004869 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004870 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004871 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004872 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004873 break;
4874
4875 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004876 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004877 if (!ts)
4878 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004879
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004880 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004881 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004882 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004883 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004884 break;
4885
4886 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004887 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004888 if (!ts) {
4889 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004890 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004891 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004892 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004893 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
4894 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004895 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004896 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004897 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004898 return 1;
4899 }
4900
4901 data_type = stktable_get_data_type(args[cur_arg] + 5);
4902 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004903 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004904 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004905 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004906 return 1;
4907 }
4908
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004909 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004910 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004911 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004912 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004913 return 1;
4914 }
4915
4916 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004917 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004918 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004919 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004920 return 1;
4921 }
4922
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004923 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004924
4925 switch (stktable_data_types[data_type].std_type) {
4926 case STD_T_SINT:
4927 stktable_data_cast(ptr, std_t_sint) = value;
4928 break;
4929 case STD_T_UINT:
4930 stktable_data_cast(ptr, std_t_uint) = value;
4931 break;
4932 case STD_T_ULL:
4933 stktable_data_cast(ptr, std_t_ull) = value;
4934 break;
4935 case STD_T_FRQP:
4936 /* We set both the current and previous values. That way
4937 * the reported frequency is stable during all the period
4938 * then slowly fades out. This allows external tools to
4939 * push measures without having to update them too often.
4940 */
4941 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004942 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004943 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004944 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004945 using its internal lock */
4946 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004947 frqp->prev_ctr = 0;
4948 frqp->curr_ctr = value;
4949 break;
4950 }
4951 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004952 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004953 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004954 break;
4955
4956 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004957 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004958 }
4959 return 1;
4960}
4961
4962/* Prepares the appctx fields with the data-based filters from the command line.
4963 * Returns 0 if the dump can proceed, 1 if has ended processing.
4964 */
4965static int table_prepare_data_request(struct appctx *appctx, char **args)
4966{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004967 struct show_table_ctx *ctx = appctx->svcctx;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004968 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004969 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004970
Willy Tarreau3c69e082022-05-03 11:35:07 +02004971 if (ctx->action != STK_CLI_ACT_SHOW && ctx->action != STK_CLI_ACT_CLR)
Willy Tarreau9d008692019-08-09 11:21:01 +02004972 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004973
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004974 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
4975 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
4976 break;
4977 /* condition on stored data value */
Willy Tarreau3c69e082022-05-03 11:35:07 +02004978 ctx->data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
4979 if (ctx->data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004980 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004981
Willy Tarreau3c69e082022-05-03 11:35:07 +02004982 if (!((struct stktable *)ctx->target)->data_ofs[ctx->data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004983 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Data type not stored in this table\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004984
Willy Tarreau3c69e082022-05-03 11:35:07 +02004985 ctx->data_op[i] = get_std_op(args[4+3*i]);
4986 if (ctx->data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004987 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004988
Willy Tarreau3c69e082022-05-03 11:35:07 +02004989 if (!*args[5+3*i] || strl2llrc(args[5+3*i], strlen(args[5+3*i]), &ctx->value[i]) != 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004990 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
4991 }
4992
4993 if (*args[3+3*i]) {
4994 return cli_dynerr(appctx, memprintf(&err, "Detected extra data in filter, %ith word of input, after '%s'\n", 3+3*i + 1, args[2+3*i]));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004995 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004996
4997 /* OK we're done, all the fields are set */
4998 return 0;
4999}
5000
5001/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02005002static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005003{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005004 struct show_table_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005005 int i;
5006
5007 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005008 ctx->data_type[i] = -1;
5009 ctx->target = NULL;
5010 ctx->entry = NULL;
5011 ctx->action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005012
5013 if (*args[2]) {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005014 ctx->t = ctx->target = stktable_find_by_name(args[2]);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005015 if (!ctx->target)
Willy Tarreau9d008692019-08-09 11:21:01 +02005016 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005017 }
5018 else {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005019 ctx->t = stktables_list;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005020 if (ctx->action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005021 goto err_args;
5022 return 0;
5023 }
5024
5025 if (strcmp(args[3], "key") == 0)
5026 return table_process_entry_per_key(appctx, args);
5027 else if (strncmp(args[3], "data.", 5) == 0)
5028 return table_prepare_data_request(appctx, args);
5029 else if (*args[3])
5030 goto err_args;
5031
5032 return 0;
5033
5034err_args:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005035 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005036 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02005037 return cli_err(appctx, "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005038 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02005039 return cli_err(appctx, "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005040 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02005041 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005042 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02005043 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005044 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005045}
5046
5047/* This function is used to deal with table operations (dump or clear depending
5048 * on the action stored in appctx->private). It returns 0 if the output buffer is
5049 * full and it needs to be called again, otherwise non-zero.
5050 */
5051static int cli_io_handler_table(struct appctx *appctx)
5052{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005053 struct show_table_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02005054 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02005055 struct stream *s = __sc_strm(sc);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005056 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005057 int skip_entry;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005058 int show = ctx->action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005059
5060 /*
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005061 * We have 3 possible states in ctx->state :
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005062 * - STATE_NEXT : the proxy pointer points to the next table to
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005063 * dump, the entry pointer is NULL ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005064 * - STATE_DUMP : the proxy pointer points to the current table
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005065 * and the entry pointer points to the next entry to be dumped,
5066 * and the refcount on the next entry is held ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005067 * - STATE_DONE : nothing left to dump, the buffer may contain some
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005068 * data though.
5069 */
Christopher Faulet87633c32023-04-03 18:32:50 +02005070 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02005071 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005072 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005073 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005074 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005075 }
5076 return 1;
5077 }
5078
5079 chunk_reset(&trash);
5080
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005081 while (ctx->state != STATE_DONE) {
5082 switch (ctx->state) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005083 case STATE_NEXT:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005084 if (!ctx->t ||
5085 (ctx->target &&
5086 ctx->t != ctx->target)) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005087 ctx->state = STATE_DONE;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005088 break;
5089 }
5090
Willy Tarreau3c69e082022-05-03 11:35:07 +02005091 if (ctx->t->size) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005092 if (show && !table_dump_head_to_buffer(&trash, appctx, ctx->t, ctx->target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005093 return 0;
5094
Willy Tarreau3c69e082022-05-03 11:35:07 +02005095 if (ctx->target &&
William Lallemand07a62f72017-05-24 00:57:40 +02005096 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005097 /* dump entries only if table explicitly requested */
Willy Tarreau76642222022-10-11 12:02:50 +02005098 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005099 eb = ebmb_first(&ctx->t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005100 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005101 ctx->entry = ebmb_entry(eb, struct stksess, key);
5102 ctx->entry->ref_cnt++;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005103 ctx->state = STATE_DUMP;
Willy Tarreau76642222022-10-11 12:02:50 +02005104 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005105 break;
5106 }
Willy Tarreau76642222022-10-11 12:02:50 +02005107 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005108 }
5109 }
Willy Tarreau3c69e082022-05-03 11:35:07 +02005110 ctx->t = ctx->t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005111 break;
5112
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005113 case STATE_DUMP:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005114 skip_entry = 0;
5115
Willy Tarreau3c69e082022-05-03 11:35:07 +02005116 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005117
Willy Tarreau3c69e082022-05-03 11:35:07 +02005118 if (ctx->data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005119 /* we're filtering on some data contents */
5120 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01005121 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005122 signed char op;
5123 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005124
Emeric Brun819fc6f2017-06-13 19:37:32 +02005125
Willy Tarreau2b64a352020-01-22 17:09:47 +01005126 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005127 if (ctx->data_type[i] == -1)
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005128 break;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005129 dt = ctx->data_type[i];
5130 ptr = stktable_data_ptr(ctx->t,
5131 ctx->entry,
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005132 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005133
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005134 data = 0;
5135 switch (stktable_data_types[dt].std_type) {
5136 case STD_T_SINT:
5137 data = stktable_data_cast(ptr, std_t_sint);
5138 break;
5139 case STD_T_UINT:
5140 data = stktable_data_cast(ptr, std_t_uint);
5141 break;
5142 case STD_T_ULL:
5143 data = stktable_data_cast(ptr, std_t_ull);
5144 break;
5145 case STD_T_FRQP:
5146 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau3c69e082022-05-03 11:35:07 +02005147 ctx->t->data_arg[dt].u);
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005148 break;
5149 }
5150
Willy Tarreau3c69e082022-05-03 11:35:07 +02005151 op = ctx->data_op[i];
5152 value = ctx->value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005153
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005154 /* skip the entry if the data does not match the test and the value */
5155 if ((data < value &&
5156 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
5157 (data == value &&
5158 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
5159 (data > value &&
5160 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
5161 skip_entry = 1;
5162 break;
5163 }
5164 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005165 }
5166
5167 if (show && !skip_entry &&
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005168 !table_dump_entry_to_buffer(&trash, appctx, ctx->t, ctx->entry)) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005169 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005170 return 0;
5171 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005172
Willy Tarreau3c69e082022-05-03 11:35:07 +02005173 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005174
Willy Tarreau76642222022-10-11 12:02:50 +02005175 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005176 ctx->entry->ref_cnt--;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005177
Willy Tarreau3c69e082022-05-03 11:35:07 +02005178 eb = ebmb_next(&ctx->entry->key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005179 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005180 struct stksess *old = ctx->entry;
5181 ctx->entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005182 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005183 __stksess_kill_if_expired(ctx->t, old);
5184 else if (!skip_entry && !ctx->entry->ref_cnt)
5185 __stksess_kill(ctx->t, old);
5186 ctx->entry->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02005187 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005188 break;
5189 }
5190
5191
5192 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005193 __stksess_kill_if_expired(ctx->t, ctx->entry);
5194 else if (!skip_entry && !ctx->entry->ref_cnt)
5195 __stksess_kill(ctx->t, ctx->entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005196
Willy Tarreau76642222022-10-11 12:02:50 +02005197 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005198
Willy Tarreau3c69e082022-05-03 11:35:07 +02005199 ctx->t = ctx->t->next;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005200 ctx->state = STATE_NEXT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005201 break;
5202
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005203 default:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005204 break;
5205 }
5206 }
5207 return 1;
5208}
5209
5210static void cli_release_show_table(struct appctx *appctx)
5211{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005212 struct show_table_ctx *ctx = appctx->svcctx;
5213
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005214 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005215 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005216 }
5217}
5218
Willy Tarreau6c011712023-01-06 16:09:58 +01005219static int stk_parse_stick_counters(char **args, int section_type, struct proxy *curpx,
5220 const struct proxy *defpx, const char *file, int line,
5221 char **err)
5222{
5223 char *error;
5224 int counters;
5225
5226 counters = strtol(args[1], &error, 10);
5227 if (*error != 0) {
5228 memprintf(err, "%s: '%s' is an invalid number", args[0], args[1]);
5229 return -1;
5230 }
5231
5232 if (counters < 0) {
5233 memprintf(err, "%s: the number of stick-counters may not be negative (was %d)", args[0], counters);
5234 return -1;
5235 }
5236
5237 global.tune.nb_stk_ctr = counters;
5238 return 0;
5239}
5240
5241/* This function creates the stk_ctr pools after the configuration parsing. It
5242 * returns 0 on success otherwise ERR_*. If nb_stk_ctr is 0, the pool remains
5243 * NULL.
5244 */
5245static int stkt_create_stk_ctr_pool(void)
5246{
5247 if (!global.tune.nb_stk_ctr)
5248 return 0;
5249
5250 pool_head_stk_ctr = create_pool("stk_ctr", sizeof(*((struct session*)0)->stkctr) * global.tune.nb_stk_ctr, MEM_F_SHARED);
5251 if (!pool_head_stk_ctr) {
5252 ha_alert("out of memory while creating the stick-counters pool.\n");
5253 return ERR_ABORT;
5254 }
5255 return 0;
5256}
5257
Willy Tarreau478331d2020-08-28 11:31:31 +02005258static void stkt_late_init(void)
5259{
5260 struct sample_fetch *f;
5261
5262 f = find_sample_fetch("src", strlen("src"));
5263 if (f)
5264 smp_fetch_src = f->process;
Willy Tarreau6c011712023-01-06 16:09:58 +01005265 hap_register_post_check(stkt_create_stk_ctr_pool);
Willy Tarreau478331d2020-08-28 11:31:31 +02005266}
5267
5268INITCALL0(STG_INIT, stkt_late_init);
5269
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005270/* register cli keywords */
5271static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005272 { { "clear", "table", NULL }, "clear table <table> [<filter>]* : remove an entry from a table (filter: data/key)", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_CLR },
5273 { { "set", "table", NULL }, "set table <table> key <k> [data.* <v>]* : update or create a table entry's data", cli_parse_table_req, cli_io_handler_table, NULL, (void *)STK_CLI_ACT_SET },
5274 { { "show", "table", NULL }, "show table <table> [<filter>]* : report table usage stats or dump this table's contents (filter: data/key)", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_SHOW },
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005275 {{},}
5276}};
5277
Willy Tarreau0108d902018-11-25 19:14:37 +01005278INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005279
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005280static struct action_kw_list tcp_conn_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005281 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005282 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5283 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5284 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005285 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5286 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005287 { /* END */ }
5288}};
5289
Willy Tarreau0108d902018-11-25 19:14:37 +01005290INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
5291
Willy Tarreau620408f2016-10-21 16:37:51 +02005292static struct action_kw_list tcp_sess_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005293 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005294 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5295 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5296 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005297 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5298 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02005299 { /* END */ }
5300}};
5301
Willy Tarreau0108d902018-11-25 19:14:37 +01005302INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
5303
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005304static struct action_kw_list tcp_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005305 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005306 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5307 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5308 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005309 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5310 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005311 { /* END */ }
5312}};
5313
Willy Tarreau0108d902018-11-25 19:14:37 +01005314INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
5315
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005316static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005317 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005318 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5319 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5320 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005321 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5322 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005323 { /* END */ }
5324}};
5325
Willy Tarreau0108d902018-11-25 19:14:37 +01005326INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
5327
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005328static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005329 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005330 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5331 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5332 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005333 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5334 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005335 { /* END */ }
5336}};
5337
Willy Tarreau0108d902018-11-25 19:14:37 +01005338INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
5339
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005340static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005341 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005342 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5343 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5344 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005345 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5346 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005347 { /* END */ }
5348}};
5349
Willy Tarreau0108d902018-11-25 19:14:37 +01005350INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
5351
Christopher Fauleta9248042023-01-05 11:17:38 +01005352static struct action_kw_list http_after_res_kws = { { }, {
Aurelien DARRAGONe2907c72023-03-17 11:28:58 +01005353 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Christopher Fauleta9248042023-01-05 11:17:38 +01005354 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5355 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5356 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
5357 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5358 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
5359 { /* END */ }
5360}};
5361
5362INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
5363
Willy Tarreau7d562212016-11-25 16:10:05 +01005364/* Note: must not be declared <const> as its list will be overwritten.
5365 * Please take care of keeping this list alphabetically sorted.
5366 */
5367static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
5368 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5369 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005370 { "sc_clr_gpc", smp_fetch_sc_clr_gpc, ARG3(2,SINT,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005371 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005372 { "sc_clr_gpc1", smp_fetch_sc_clr_gpc1, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau7d562212016-11-25 16:10:05 +01005373 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5374 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5375 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Emeric Brun877b0b52021-06-30 18:57:49 +02005376 { "sc_get_gpt", smp_fetch_sc_get_gpt, ARG3(2,SINT,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01005377 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005378 { "sc_get_gpc", smp_fetch_sc_get_gpc, ARG3(2,SINT,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005379 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005380 { "sc_get_gpc1", smp_fetch_sc_get_gpc1, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005381 { "sc_gpc_rate", smp_fetch_sc_gpc_rate, ARG3(2,SINT,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005382 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005383 { "sc_gpc1_rate", smp_fetch_sc_gpc1_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005384 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5385 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005386 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5387 { "sc_http_fail_rate", smp_fetch_sc_http_fail_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005388 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5389 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005390 { "sc_inc_gpc", smp_fetch_sc_inc_gpc, ARG3(2,SINT,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005391 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005392 { "sc_inc_gpc1", smp_fetch_sc_inc_gpc1, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005393 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5394 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5395 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5396 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5397 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5398 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5399 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5400 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5401 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005402 { "sc0_clr_gpc1", smp_fetch_sc_clr_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005403 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5404 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5405 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01005406 { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005407 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005408 { "sc0_get_gpc1", smp_fetch_sc_get_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005409 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005410 { "sc0_gpc1_rate", smp_fetch_sc_gpc1_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005411 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5412 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005413 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5414 { "sc0_http_fail_rate", smp_fetch_sc_http_fail_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005415 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5416 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5417 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005418 { "sc0_inc_gpc1", smp_fetch_sc_inc_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005419 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5420 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5421 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5422 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5423 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5424 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5425 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5426 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005427 { "sc1_clr_gpc", smp_fetch_sc_clr_gpc, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005428 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005429 { "sc1_clr_gpc1", smp_fetch_sc_clr_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005430 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5431 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5432 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01005433 { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005434 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005435 { "sc1_get_gpc1", smp_fetch_sc_get_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005436 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005437 { "sc1_gpc1_rate", smp_fetch_sc_gpc1_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005438 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5439 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005440 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5441 { "sc1_http_fail_rate", smp_fetch_sc_http_fail_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005442 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5443 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5444 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005445 { "sc1_inc_gpc1", smp_fetch_sc_inc_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005446 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5447 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5448 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5449 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5450 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5451 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5452 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5453 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5454 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005455 { "sc2_clr_gpc1", smp_fetch_sc_clr_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005456 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5457 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5458 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01005459 { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005460 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005461 { "sc2_get_gpc1", smp_fetch_sc_get_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005462 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005463 { "sc2_gpc1_rate", smp_fetch_sc_gpc1_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005464 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5465 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005466 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5467 { "sc2_http_fail_rate", smp_fetch_sc_http_fail_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005468 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5469 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5470 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005471 { "sc2_inc_gpc1", smp_fetch_sc_inc_gpc1, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005472 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5473 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5474 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5475 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5476 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5477 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5478 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5479 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005480 { "src_clr_gpc", smp_fetch_sc_clr_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005481 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005482 { "src_clr_gpc1", smp_fetch_sc_clr_gpc1, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005483 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5484 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5485 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Emeric Brun877b0b52021-06-30 18:57:49 +02005486 { "src_get_gpt" , smp_fetch_sc_get_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01005487 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005488 { "src_get_gpc", smp_fetch_sc_get_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005489 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005490 { "src_get_gpc1", smp_fetch_sc_get_gpc1, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005491 { "src_gpc_rate", smp_fetch_sc_gpc_rate, ARG2(2,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005492 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005493 { "src_gpc1_rate", smp_fetch_sc_gpc1_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005494 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5495 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005496 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5497 { "src_http_fail_rate", smp_fetch_sc_http_fail_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005498 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5499 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005500 { "src_inc_gpc", smp_fetch_sc_inc_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005501 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005502 { "src_inc_gpc1", smp_fetch_sc_inc_gpc1, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01005503 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5504 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5505 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5506 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5507 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5508 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5509 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5510 { /* END */ },
5511}};
5512
Willy Tarreau0108d902018-11-25 19:14:37 +01005513INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01005514
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005515/* Note: must not be declared <const> as its list will be overwritten */
5516static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02005517 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
5518 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5519 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5520 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5521 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5522 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Frédéric Lécaillebbeec372022-08-16 18:11:25 +02005523 { "table_expire", sample_conv_table_expire, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun877b0b52021-06-30 18:57:49 +02005524 { "table_gpt", sample_conv_table_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005525 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005526 { "table_gpc", sample_conv_table_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005527 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005528 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005529 { "table_gpc_rate", sample_conv_table_gpc_rate, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005530 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01005531 { "table_gpc1_rate", sample_conv_table_gpc1_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005532 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5533 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01005534 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5535 { "table_http_fail_rate", sample_conv_table_http_fail_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005536 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5537 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Frédéric Lécaillebbeec372022-08-16 18:11:25 +02005538 { "table_idle", sample_conv_table_idle, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005539 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5540 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5541 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5542 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5543 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5544 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005545 { /* END */ },
5546}};
5547
Willy Tarreau0108d902018-11-25 19:14:37 +01005548INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau6c011712023-01-06 16:09:58 +01005549
5550static struct cfg_kw_list cfg_kws = {{ },{
5551 { CFG_GLOBAL, "tune.stick-counters", stk_parse_stick_counters },
5552 { /* END */ }
5553}};
5554
5555INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);