blob: a2aa9c4515c31d3769481146225d8230c5ab23f2 [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
775/*
776 * Configuration keywords of known table types
777 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200778struct stktable_type stktable_types[SMP_TYPES] = {
779 [SMP_T_SINT] = { "integer", 0, 4 },
780 [SMP_T_IPV4] = { "ip", 0, 4 },
781 [SMP_T_IPV6] = { "ipv6", 0, 16 },
782 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
783 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
784};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100785
786/*
787 * Parse table type configuration.
788 * Returns 0 on successful parsing, else 1.
789 * <myidx> is set at next configuration <args> index.
790 */
William Lallemand3f210972023-04-13 14:33:52 +0200791int 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 +0100792{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200793 for (*type = 0; *type < SMP_TYPES; (*type)++) {
794 if (!stktable_types[*type].kw)
795 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100796 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
797 continue;
798
799 *key_size = stktable_types[*type].default_size;
800 (*myidx)++;
801
Willy Tarreauaea940e2010-06-06 11:56:36 +0200802 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100803 if (strcmp("len", args[*myidx]) == 0) {
William Lallemand3f210972023-04-13 14:33:52 +0200804 char *stop;
805
Emeric Brun3bd697e2010-01-04 15:23:48 +0100806 (*myidx)++;
William Lallemand3f210972023-04-13 14:33:52 +0200807 *key_size = strtol(args[*myidx], &stop, 10);
808 if (*stop != '\0' || !*key_size) {
809 ha_alert("parsing [%s:%d] : 'len' expects a positive integer argument.\n", file, linenum);
810 return 1;
811 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200812 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200813 /* null terminated string needs +1 for '\0'. */
814 (*key_size)++;
815 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100816 (*myidx)++;
817 }
818 }
819 return 0;
820 }
William Lallemand3f210972023-04-13 14:33:52 +0200821 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", file, linenum, args[0], args[*myidx]);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100822 return 1;
823}
824
Emeric Brunc64a2a32021-06-30 18:01:02 +0200825/* reserve some space for data type <type>, there is 2 optionnals
826 * argument at <sa> and <sa2> to configure this data type and
827 * they can be NULL if unused for a given type.
828 * Returns PE_NONE (0) if OK or an error code among :
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200829 * - PE_ENUM_OOR if <type> does not exist
830 * - PE_EXIST if <type> is already registered
Emeric Brunc64a2a32021-06-30 18:01:02 +0200831 * - PE_ARG_NOT_USE if <sa>/<sa2> was provided but not expected
832 * - PE_ARG_MISSING if <sa>/<sa2> was expected but not provided
833 * - PE_ARG_VALUE_OOR if type is an array and <sa> it out of array size range.
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200834 */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200835int stktable_alloc_data_type(struct stktable *t, int type, const char *sa, const char *sa2)
836
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200837{
838 if (type >= STKTABLE_DATA_TYPES)
839 return PE_ENUM_OOR;
840
841 if (t->data_ofs[type])
842 /* already allocated */
843 return PE_EXIST;
844
Emeric Brunc64a2a32021-06-30 18:01:02 +0200845 t->data_nbelem[type] = 1;
846 if (stktable_data_types[type].is_array) {
847 /* arrays take their element count on first argument */
848 if (!sa)
849 return PE_ARG_MISSING;
850 t->data_nbelem[type] = atoi(sa);
851 if (!t->data_nbelem[type] || (t->data_nbelem[type] > STKTABLE_MAX_DT_ARRAY_SIZE))
852 return PE_ARG_VALUE_OOR;
853 sa = sa2;
854 }
855
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200856 switch (stktable_data_types[type].arg_type) {
857 case ARG_T_NONE:
858 if (sa)
859 return PE_ARG_NOT_USED;
860 break;
861 case ARG_T_INT:
862 if (!sa)
863 return PE_ARG_MISSING;
864 t->data_arg[type].i = atoi(sa);
865 break;
866 case ARG_T_DELAY:
867 if (!sa)
868 return PE_ARG_MISSING;
869 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
870 if (sa)
871 return PE_ARG_INVC; /* invalid char */
872 break;
873 }
874
Emeric Brunc64a2a32021-06-30 18:01:02 +0200875 t->data_size += t->data_nbelem[type] * stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200876 t->data_ofs[type] = -t->data_size;
877 return PE_NONE;
878}
879
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100880/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100881 * Parse a line with <linenum> as number in <file> configuration file to configure
882 * the stick-table with <t> as address and <id> as ID.
883 * <peers> provides the "peers" section pointer only if this function is called
884 * from a "peers" section.
885 * <nid> is the stick-table name which is sent over the network. It must be equal
886 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
887 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500888 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100889 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
890 */
891int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100892 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100893{
894 int err_code = 0;
895 int idx = 1;
896 unsigned int val;
897
898 if (!id || !*id) {
899 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
900 err_code |= ERR_ALERT | ERR_ABORT;
901 goto out;
902 }
903
904 /* Store the "peers" section if this function is called from a "peers" section. */
905 if (peers) {
906 t->peers.p = peers;
907 idx++;
908 }
909
910 t->id = id;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200911 t->idlen = strlen(id);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100912 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100913 t->type = (unsigned int)-1;
914 t->conf.file = file;
915 t->conf.line = linenum;
916
917 while (*args[idx]) {
918 const char *err;
919
920 if (strcmp(args[idx], "size") == 0) {
921 idx++;
922 if (!*(args[idx])) {
923 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
924 file, linenum, args[0], args[idx-1]);
925 err_code |= ERR_ALERT | ERR_FATAL;
926 goto out;
927 }
928 if ((err = parse_size_err(args[idx], &t->size))) {
929 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
930 file, linenum, args[0], *err, args[idx-1]);
931 err_code |= ERR_ALERT | ERR_FATAL;
932 goto out;
933 }
934 idx++;
935 }
936 /* This argument does not exit in "peers" section. */
937 else if (!peers && strcmp(args[idx], "peers") == 0) {
938 idx++;
939 if (!*(args[idx])) {
940 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
941 file, linenum, args[0], args[idx-1]);
942 err_code |= ERR_ALERT | ERR_FATAL;
943 goto out;
944 }
945 t->peers.name = strdup(args[idx++]);
946 }
947 else if (strcmp(args[idx], "expire") == 0) {
948 idx++;
949 if (!*(args[idx])) {
950 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
951 file, linenum, args[0], args[idx-1]);
952 err_code |= ERR_ALERT | ERR_FATAL;
953 goto out;
954 }
955 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200956 if (err == PARSE_TIME_OVER) {
957 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
958 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200962 else if (err == PARSE_TIME_UNDER) {
963 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
964 file, linenum, args[0], args[idx], args[idx-1]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto out;
967 }
968 else if (err) {
969 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
970 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100971 err_code |= ERR_ALERT | ERR_FATAL;
972 goto out;
973 }
974 t->expire = val;
975 idx++;
976 }
977 else if (strcmp(args[idx], "nopurge") == 0) {
978 t->nopurge = 1;
979 idx++;
980 }
981 else if (strcmp(args[idx], "type") == 0) {
982 idx++;
William Lallemand3f210972023-04-13 14:33:52 +0200983 if (stktable_parse_type(args, &idx, &t->type, &t->key_size, file, linenum) != 0) {
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100984 err_code |= ERR_ALERT | ERR_FATAL;
985 goto out;
986 }
987 /* idx already points to next arg */
988 }
989 else if (strcmp(args[idx], "store") == 0) {
990 int type, err;
Emeric Brunc64a2a32021-06-30 18:01:02 +0200991 char *cw, *nw, *sa, *sa2;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100992
993 idx++;
994 nw = args[idx];
995 while (*nw) {
996 /* the "store" keyword supports a comma-separated list */
997 cw = nw;
998 sa = NULL; /* store arg */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200999 sa2 = NULL;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001000 while (*nw && *nw != ',') {
1001 if (*nw == '(') {
1002 *nw = 0;
1003 sa = ++nw;
1004 while (*nw != ')') {
1005 if (!*nw) {
1006 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
1007 file, linenum, args[0], cw);
1008 err_code |= ERR_ALERT | ERR_FATAL;
1009 goto out;
1010 }
Emeric Brunc64a2a32021-06-30 18:01:02 +02001011 if (*nw == ',') {
1012 *nw = '\0';
1013 sa2 = nw + 1;
1014 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001015 nw++;
1016 }
1017 *nw = '\0';
1018 }
1019 nw++;
1020 }
1021 if (*nw)
1022 *nw++ = '\0';
1023 type = stktable_get_data_type(cw);
1024 if (type < 0) {
1025 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
1026 file, linenum, args[0], cw);
1027 err_code |= ERR_ALERT | ERR_FATAL;
1028 goto out;
1029 }
1030
Emeric Brunc64a2a32021-06-30 18:01:02 +02001031 err = stktable_alloc_data_type(t, type, sa, sa2);
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001032 switch (err) {
1033 case PE_NONE: break;
1034 case PE_EXIST:
1035 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
1036 file, linenum, args[0], cw);
1037 err_code |= ERR_WARN;
1038 break;
1039
1040 case PE_ARG_MISSING:
1041 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
1042 file, linenum, args[0], cw);
1043 err_code |= ERR_ALERT | ERR_FATAL;
1044 goto out;
1045
1046 case PE_ARG_NOT_USED:
1047 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
1048 file, linenum, args[0], cw);
1049 err_code |= ERR_ALERT | ERR_FATAL;
1050 goto out;
Emeric Brunc64a2a32021-06-30 18:01:02 +02001051 case PE_ARG_VALUE_OOR:
1052 ha_alert("parsing [%s:%d] : %s: array size is out of allowed range (1-%d) for store option '%s'.\n",
1053 file, linenum, args[0], STKTABLE_MAX_DT_ARRAY_SIZE, cw);
1054 err_code |= ERR_ALERT | ERR_FATAL;
1055 goto out;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001056
1057 default:
1058 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
1059 file, linenum, args[0], cw);
1060 err_code |= ERR_ALERT | ERR_FATAL;
1061 goto out;
1062 }
1063 }
1064 idx++;
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001065 if (t->data_ofs[STKTABLE_DT_GPT] && t->data_ofs[STKTABLE_DT_GPT0]) {
1066 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpt' and 'gpt0' in a same table is not permitted as 'gpt' overrides 'gpt0'.\n",
1067 file, linenum, args[0]);
1068 err_code |= ERR_ALERT | ERR_FATAL;
1069 goto out;
1070 }
Emeric Brun726783d2021-06-30 19:06:43 +02001071 else if (t->data_ofs[STKTABLE_DT_GPC] && (t->data_ofs[STKTABLE_DT_GPC0] || t->data_ofs[STKTABLE_DT_GPC1])) {
1072 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",
1073 file, linenum, args[0]);
1074 err_code |= ERR_ALERT | ERR_FATAL;
1075 goto out;
1076 }
1077 else if (t->data_ofs[STKTABLE_DT_GPC_RATE] && (t->data_ofs[STKTABLE_DT_GPC0_RATE] || t->data_ofs[STKTABLE_DT_GPC1_RATE])) {
1078 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",
1079 file, linenum, args[0]);
1080 err_code |= ERR_ALERT | ERR_FATAL;
1081 goto out;
1082 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001083 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001084 else if (strcmp(args[idx], "srvkey") == 0) {
1085 char *keytype;
1086 idx++;
1087 keytype = args[idx];
1088 if (strcmp(keytype, "name") == 0) {
1089 t->server_key_type = STKTABLE_SRV_NAME;
1090 }
1091 else if (strcmp(keytype, "addr") == 0) {
1092 t->server_key_type = STKTABLE_SRV_ADDR;
1093 }
1094 else {
1095 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
1096 file, linenum, args[0], keytype);
1097 err_code |= ERR_ALERT | ERR_FATAL;
1098 goto out;
1099
1100 }
1101 idx++;
1102 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001103 else {
1104 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
1105 file, linenum, args[0], args[idx]);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108 }
1109 }
1110
1111 if (!t->size) {
1112 ha_alert("parsing [%s:%d] : %s: missing size.\n",
1113 file, linenum, args[0]);
1114 err_code |= ERR_ALERT | ERR_FATAL;
1115 goto out;
1116 }
1117
1118 if (t->type == (unsigned int)-1) {
1119 ha_alert("parsing [%s:%d] : %s: missing type.\n",
1120 file, linenum, args[0]);
1121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124
1125 out:
1126 return err_code;
1127}
1128
Willy Tarreau8fed9032014-07-03 17:02:46 +02001129/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001130 * Note that the sample *is* modified and that the returned key may point
1131 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +02001132 * Returns NULL if the sample could not be converted (eg: no matching type),
1133 * otherwise a pointer to the static stktable_key filled with what is needed
1134 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001135 */
Willy Tarreau8fed9032014-07-03 17:02:46 +02001136struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001137{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001138 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001139 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +02001140 return NULL;
1141
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001142 /* Fill static_table_key. */
1143 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +02001144
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001145 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +02001146 static_table_key.key = &smp->data.u.ipv4;
1147 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001148 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001149
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001150 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +02001151 static_table_key.key = &smp->data.u.ipv6;
1152 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001153 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001154
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001155 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001156 /* The stick table require a 32bit unsigned int, "sint" is a
1157 * signed 64 it, so we can convert it inplace.
1158 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001159 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001160 static_table_key.key = &smp->data.u.sint;
1161 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001162 break;
1163
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001164 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001165 if (!smp_make_safe(smp))
1166 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001167 static_table_key.key = smp->data.u.str.area;
1168 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001169 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001170
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001171 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001172 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001173 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001174 if (!smp_make_rw(smp))
1175 return NULL;
1176
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001177 if (smp->data.u.str.size < t->key_size)
1178 if (!smp_dup(smp))
1179 return NULL;
1180 if (smp->data.u.str.size < t->key_size)
1181 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1183 t->key_size - smp->data.u.str.data);
1184 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001185 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001186 static_table_key.key = smp->data.u.str.area;
1187 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001188 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001189
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001190 default: /* impossible case. */
1191 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001192 }
1193
Christopher Fauletca20d022017-08-29 15:30:31 +02001194 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001195}
1196
1197/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001198 * Process a fetch + format conversion as defined by the sample expression <expr>
1199 * on request or response considering the <opt> parameter. Returns either NULL if
1200 * no key could be extracted, or a pointer to the converted result stored in
1201 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1202 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001203 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1204 * without SMP_OPT_FINAL). The output will be usable like this :
1205 *
1206 * return MAY_CHANGE FINAL Meaning for the sample
1207 * NULL 0 * Not present and will never be (eg: header)
1208 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1209 * NULL 1 1 Not present, will not change anymore
1210 * smp 0 * Present and will not change (eg: header)
1211 * smp 1 0 not possible
1212 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001213 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001214struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001215 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1216{
1217 if (smp)
1218 memset(smp, 0, sizeof(*smp));
1219
Willy Tarreau192252e2015-04-04 01:47:55 +02001220 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001221 if (!smp)
1222 return NULL;
1223
1224 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1225 return NULL; /* we can only use stable samples */
1226
1227 return smp_to_stkey(smp, t);
1228}
1229
1230/*
Willy Tarreau12785782012-04-27 21:37:17 +02001231 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001232 * type <table_type>, otherwise zero. Used in configuration check.
1233 */
Willy Tarreau12785782012-04-27 21:37:17 +02001234int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001235{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001236 int out_type;
1237
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001238 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001239 return 0;
1240
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001241 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001242
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001243 /* Convert sample. */
1244 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001245 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001246
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001247 return 1;
1248}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001249
Willy Tarreauedee1d62014-07-15 16:44:27 +02001250/* Extra data types processing : after the last one, some room may remain
1251 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1252 * at run time.
1253 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001254struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001255 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001256 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001257 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001258 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001259 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1260 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001261 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT, .is_local = 1 },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001262 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1263 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1264 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1265 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1266 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1267 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1268 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1269 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1270 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1271 [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 +01001272 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1273 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001274 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001275 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1276 [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 +02001277 [STKTABLE_DT_GPT] = { .name = "gpt", .std_type = STD_T_UINT, .is_array = 1 },
Emeric Brun4d7ada82021-06-30 19:04:16 +02001278 [STKTABLE_DT_GPC] = { .name = "gpc", .std_type = STD_T_UINT, .is_array = 1 },
1279 [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 +02001280};
1281
Willy Tarreauedee1d62014-07-15 16:44:27 +02001282/* Registers stick-table extra data type with index <idx>, name <name>, type
1283 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1284 * index is automatically allocated. The allocated index is returned, or -1 if
1285 * no free index was found or <name> was already registered. The <name> is used
1286 * directly as a pointer, so if it's not stable, the caller must allocate it.
1287 */
1288int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1289{
1290 if (idx < 0) {
1291 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1292 if (!stktable_data_types[idx].name)
1293 break;
1294
1295 if (strcmp(stktable_data_types[idx].name, name) == 0)
1296 return -1;
1297 }
1298 }
1299
1300 if (idx >= STKTABLE_DATA_TYPES)
1301 return -1;
1302
1303 if (stktable_data_types[idx].name != NULL)
1304 return -1;
1305
1306 stktable_data_types[idx].name = name;
1307 stktable_data_types[idx].std_type = std_type;
1308 stktable_data_types[idx].arg_type = arg_type;
1309 return idx;
1310}
1311
Willy Tarreau08d5f982010-06-06 13:34:54 +02001312/*
1313 * Returns the data type number for the stktable_data_type whose name is <name>,
1314 * or <0 if not found.
1315 */
1316int stktable_get_data_type(char *name)
1317{
1318 int type;
1319
1320 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001321 if (!stktable_data_types[type].name)
1322 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001323 if (strcmp(name, stktable_data_types[type].name) == 0)
1324 return type;
1325 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001326 /* For backwards compatibility */
1327 if (strcmp(name, "server_name") == 0)
1328 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001329 return -1;
1330}
1331
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001332/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1333 * it up into this table. Returns true if found, false otherwise. The input
1334 * type is STR so that input samples are converted to string (since all types
1335 * can be converted to strings), then the function casts the string again into
1336 * the table's type. This is a double conversion, but in the future we might
1337 * support automatic input types to perform the cast on the fly.
1338 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001339static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001340{
1341 struct stktable *t;
1342 struct stktable_key *key;
1343 struct stksess *ts;
1344
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001345 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001346
1347 key = smp_to_stkey(smp, t);
1348 if (!key)
1349 return 0;
1350
1351 ts = stktable_lookup_key(t, key);
1352
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001353 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001354 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001356 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001357 return 1;
1358}
1359
1360/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1361 * it up into this table. Returns the data rate received from clients in bytes/s
1362 * if the key is present in the table, otherwise zero, so that comparisons can
1363 * be easily performed. If the inspected parameter is not stored in the table,
1364 * <not found> is returned.
1365 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001366static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001367{
1368 struct stktable *t;
1369 struct stktable_key *key;
1370 struct stksess *ts;
1371 void *ptr;
1372
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001373 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001374
1375 key = smp_to_stkey(smp, t);
1376 if (!key)
1377 return 0;
1378
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001379 ts = stktable_lookup_key(t, key);
1380
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001381 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001383 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001384
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001385 if (!ts) /* key not present */
1386 return 1;
1387
1388 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001389 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001390 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001391 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001392
Daniel Corbett3e60b112018-05-27 09:47:12 -04001393 stktable_release(t, ts);
1394 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001395}
1396
1397/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1398 * it up into this table. Returns the cumulated number of connections for the key
1399 * if the key is present in the table, otherwise zero, so that comparisons can
1400 * be easily performed. If the inspected parameter is not stored in the table,
1401 * <not found> is returned.
1402 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001403static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001404{
1405 struct stktable *t;
1406 struct stktable_key *key;
1407 struct stksess *ts;
1408 void *ptr;
1409
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001410 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001411
1412 key = smp_to_stkey(smp, t);
1413 if (!key)
1414 return 0;
1415
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001416 ts = stktable_lookup_key(t, key);
1417
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001418 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001419 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001420 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001421
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001422 if (!ts) /* key not present */
1423 return 1;
1424
1425 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001426 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001427 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001428
Daniel Corbett3e60b112018-05-27 09:47:12 -04001429 stktable_release(t, ts);
1430 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001431}
1432
1433/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1434 * it up into this table. Returns the number of concurrent connections for the
1435 * key if the key is present in the table, otherwise zero, so that comparisons
1436 * can be easily performed. If the inspected parameter is not stored in the
1437 * table, <not found> is returned.
1438 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001439static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001440{
1441 struct stktable *t;
1442 struct stktable_key *key;
1443 struct stksess *ts;
1444 void *ptr;
1445
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001446 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001447
1448 key = smp_to_stkey(smp, t);
1449 if (!key)
1450 return 0;
1451
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001452 ts = stktable_lookup_key(t, key);
1453
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001454 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001455 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001456 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001457
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001458 if (!ts) /* key not present */
1459 return 1;
1460
1461 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001462 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001463 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001464
Daniel Corbett3e60b112018-05-27 09:47:12 -04001465 stktable_release(t, ts);
1466 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001467}
1468
1469/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1470 * it up into this table. Returns the rate of incoming connections from the key
1471 * if the key is present in the table, otherwise zero, so that comparisons can
1472 * be easily performed. If the inspected parameter is not stored in the table,
1473 * <not found> is returned.
1474 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001475static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001476{
1477 struct stktable *t;
1478 struct stktable_key *key;
1479 struct stksess *ts;
1480 void *ptr;
1481
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001482 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001483
1484 key = smp_to_stkey(smp, t);
1485 if (!key)
1486 return 0;
1487
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001488 ts = stktable_lookup_key(t, key);
1489
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001490 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001491 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001492 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001493
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001494 if (!ts) /* key not present */
1495 return 1;
1496
1497 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001498 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001499 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001500 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001501
Daniel Corbett3e60b112018-05-27 09:47:12 -04001502 stktable_release(t, ts);
1503 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001504}
1505
1506/* 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 +02001507 * it up into this table. Returns the expiration delay for the key if the key is
1508 * present in the table, otherwise the default value provided as second argument
1509 * if any, if not (no default value), <not found> is returned.
1510 */
1511static int sample_conv_table_expire(const struct arg *arg_p, struct sample *smp, void *private)
1512{
1513 struct stktable *t;
1514 struct stktable_key *key;
1515 struct stksess *ts;
1516
1517 t = arg_p[0].data.t;
1518
1519 key = smp_to_stkey(smp, t);
1520 if (!key)
1521 return 0;
1522
1523 ts = stktable_lookup_key(t, key);
1524
1525 smp->flags = SMP_F_VOL_TEST;
1526 smp->data.type = SMP_T_SINT;
1527 smp->data.u.sint = 0;
1528
1529 if (!ts) { /* key not present */
1530 if (arg_p[1].type == ARGT_STOP)
1531 return 0;
1532
1533 /* default value */
1534 smp->data.u.sint = arg_p[1].data.sint;
1535 return 1;
1536 }
1537
1538 smp->data.u.sint = tick_remain(now_ms, ts->expire);
1539
1540 stktable_release(t, ts);
1541 return 1;
1542}
1543
1544/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1545 * it up into this table. Returns the time the key remains unused if the key is
1546 * present in the table, otherwise the default value provided as second argument
1547 * if any, if not (no default value), <not found> is returned.
1548 */
1549static int sample_conv_table_idle(const struct arg *arg_p, struct sample *smp, void *private)
1550{
1551 struct stktable *t;
1552 struct stktable_key *key;
1553 struct stksess *ts;
1554
1555 t = arg_p[0].data.t;
1556
1557 key = smp_to_stkey(smp, t);
1558 if (!key)
1559 return 0;
1560
1561 ts = stktable_lookup_key(t, key);
1562
1563 smp->flags = SMP_F_VOL_TEST;
1564 smp->data.type = SMP_T_SINT;
1565 smp->data.u.sint = 0;
1566
1567 if (!ts) { /* key not present */
1568 if (arg_p[1].type == ARGT_STOP)
1569 return 0;
1570
1571 /* default value */
1572 smp->data.u.sint = arg_p[1].data.sint;
1573 return 1;
1574 }
1575
1576 smp->data.u.sint = tick_remain(tick_remain(now_ms, ts->expire), t->expire);
1577
1578 stktable_release(t, ts);
1579 return 1;
1580}
1581
1582/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001583 * it up into this table. Returns the data rate sent to clients in bytes/s
1584 * if the key is present in the table, otherwise zero, so that comparisons can
1585 * be easily performed. If the inspected parameter is not stored in the table,
1586 * <not found> is returned.
1587 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001588static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001589{
1590 struct stktable *t;
1591 struct stktable_key *key;
1592 struct stksess *ts;
1593 void *ptr;
1594
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001595 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001596
1597 key = smp_to_stkey(smp, t);
1598 if (!key)
1599 return 0;
1600
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001601 ts = stktable_lookup_key(t, key);
1602
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001603 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001604 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001605 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001606
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001607 if (!ts) /* key not present */
1608 return 1;
1609
1610 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001611 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001612 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001613 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001614
Daniel Corbett3e60b112018-05-27 09:47:12 -04001615 stktable_release(t, ts);
1616 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001617}
1618
Emeric Brun877b0b52021-06-30 18:57:49 +02001619/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1620 * it up into this table. Returns the value of the GPT[arg_p(0)] tag for the key
1621 * if the key is present in the table, otherwise false, so that comparisons can
1622 * be easily performed. If the inspected parameter is not stored in the table,
1623 * <not found> is returned.
1624 */
1625static int sample_conv_table_gpt(const struct arg *arg_p, struct sample *smp, void *private)
1626{
1627 struct stktable *t;
1628 struct stktable_key *key;
1629 struct stksess *ts;
1630 void *ptr;
1631 unsigned int idx;
1632
1633 idx = arg_p[0].data.sint;
1634
1635 t = arg_p[1].data.t;
1636
1637 key = smp_to_stkey(smp, t);
1638 if (!key)
1639 return 0;
1640
1641 ts = stktable_lookup_key(t, key);
1642
1643 smp->flags = SMP_F_VOL_TEST;
1644 smp->data.type = SMP_T_SINT;
1645 smp->data.u.sint = 0;
1646
1647 if (!ts) /* key not present */
1648 return 1;
1649
1650 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, idx);
1651 if (ptr)
1652 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1653
1654 stktable_release(t, ts);
1655 return !!ptr;
1656}
1657
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001658/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001659 * it up into this table. Returns the value of the GPT0 tag for the key
1660 * if the key is present in the table, otherwise false, so that comparisons can
1661 * be easily performed. If the inspected parameter is not stored in the table,
1662 * <not found> is returned.
1663 */
1664static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1665{
1666 struct stktable *t;
1667 struct stktable_key *key;
1668 struct stksess *ts;
1669 void *ptr;
1670
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001671 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001672
1673 key = smp_to_stkey(smp, t);
1674 if (!key)
1675 return 0;
1676
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001677 ts = stktable_lookup_key(t, key);
1678
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001679 smp->flags = SMP_F_VOL_TEST;
1680 smp->data.type = SMP_T_SINT;
1681 smp->data.u.sint = 0;
1682
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001683 if (!ts) /* key not present */
1684 return 1;
1685
1686 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001687 if (!ptr)
1688 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, 0);
1689
Daniel Corbett3e60b112018-05-27 09:47:12 -04001690 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001691 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001692
Daniel Corbett3e60b112018-05-27 09:47:12 -04001693 stktable_release(t, ts);
1694 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001695}
1696
Emeric Brun4d7ada82021-06-30 19:04:16 +02001697/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1698 * it up into this table. Returns the value of the GPC[arg_p(0)] counter for the key
1699 * if the key is present in the table, otherwise zero, so that comparisons can
1700 * be easily performed. If the inspected parameter is not stored in the table,
1701 * <not found> is returned.
1702 */
1703static int sample_conv_table_gpc(const struct arg *arg_p, struct sample *smp, void *private)
1704{
1705 struct stktable *t;
1706 struct stktable_key *key;
1707 struct stksess *ts;
1708 void *ptr;
1709 unsigned int idx;
1710
1711 idx = arg_p[0].data.sint;
1712
1713 t = arg_p[1].data.t;
1714
1715 key = smp_to_stkey(smp, t);
1716 if (!key)
1717 return 0;
1718
1719 ts = stktable_lookup_key(t, key);
1720
1721 smp->flags = SMP_F_VOL_TEST;
1722 smp->data.type = SMP_T_SINT;
1723 smp->data.u.sint = 0;
1724
1725 if (!ts) /* key not present */
1726 return 1;
1727
1728 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, idx);
1729 if (ptr)
1730 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1731
1732 stktable_release(t, ts);
1733 return !!ptr;
1734}
1735
1736/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1737 * it up into this table. Returns the event rate of the GPC[arg_p(0)] counter
1738 * for the key if the key is present in the table, otherwise zero, so that
1739 * comparisons can be easily performed. If the inspected parameter is not
1740 * stored in the table, <not found> is returned.
1741 */
1742static int sample_conv_table_gpc_rate(const struct arg *arg_p, struct sample *smp, void *private)
1743{
1744 struct stktable *t;
1745 struct stktable_key *key;
1746 struct stksess *ts;
1747 void *ptr;
1748 unsigned int idx;
1749
1750 idx = arg_p[0].data.sint;
1751
1752 t = arg_p[1].data.t;
1753
1754 key = smp_to_stkey(smp, t);
1755 if (!key)
1756 return 0;
1757
1758 ts = stktable_lookup_key(t, key);
1759
1760 smp->flags = SMP_F_VOL_TEST;
1761 smp->data.type = SMP_T_SINT;
1762 smp->data.u.sint = 0;
1763
1764 if (!ts) /* key not present */
1765 return 1;
1766
1767 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, idx);
1768 if (ptr)
1769 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1770 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1771
1772 stktable_release(t, ts);
1773 return !!ptr;
1774}
1775
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001776/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001777 * it up into this table. Returns the value of the GPC0 counter for the key
1778 * if the key is present in the table, otherwise zero, so that comparisons can
1779 * be easily performed. If the inspected parameter is not stored in the table,
1780 * <not found> is returned.
1781 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001782static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001783{
1784 struct stktable *t;
1785 struct stktable_key *key;
1786 struct stksess *ts;
1787 void *ptr;
1788
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001789 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001790
1791 key = smp_to_stkey(smp, t);
1792 if (!key)
1793 return 0;
1794
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001795 ts = stktable_lookup_key(t, key);
1796
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001797 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001798 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001799 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001800
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001801 if (!ts) /* key not present */
1802 return 1;
1803
1804 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02001805 if (!ptr) {
1806 /* fallback on the gpc array */
1807 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 0);
1808 }
1809
Daniel Corbett3e60b112018-05-27 09:47:12 -04001810 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001811 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001812
Daniel Corbett3e60b112018-05-27 09:47:12 -04001813 stktable_release(t, ts);
1814 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001815}
1816
1817/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1818 * it up into this table. Returns the event rate of the GPC0 counter for the key
1819 * if the key is present in the table, otherwise zero, so that comparisons can
1820 * be easily performed. If the inspected parameter is not stored in the table,
1821 * <not found> is returned.
1822 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001823static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001824{
1825 struct stktable *t;
1826 struct stktable_key *key;
1827 struct stksess *ts;
1828 void *ptr;
1829
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001830 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001831
1832 key = smp_to_stkey(smp, t);
1833 if (!key)
1834 return 0;
1835
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001836 ts = stktable_lookup_key(t, key);
1837
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001838 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001839 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001840 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001841
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001842 if (!ts) /* key not present */
1843 return 1;
1844
1845 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001846 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001847 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001848 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001849 else {
1850 /* fallback on the gpc array */
1851 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 0);
1852 if (ptr)
1853 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1854 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1855 }
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001856
Daniel Corbett3e60b112018-05-27 09:47:12 -04001857 stktable_release(t, ts);
1858 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001859}
1860
1861/* 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 +01001862 * it up into this table. Returns the value of the GPC1 counter for the key
1863 * if the key is present in the table, otherwise zero, so that comparisons can
1864 * be easily performed. If the inspected parameter is not stored in the table,
1865 * <not found> is returned.
1866 */
1867static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1868{
1869 struct stktable *t;
1870 struct stktable_key *key;
1871 struct stksess *ts;
1872 void *ptr;
1873
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001874 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001875
1876 key = smp_to_stkey(smp, t);
1877 if (!key)
1878 return 0;
1879
1880 ts = stktable_lookup_key(t, key);
1881
1882 smp->flags = SMP_F_VOL_TEST;
1883 smp->data.type = SMP_T_SINT;
1884 smp->data.u.sint = 0;
1885
1886 if (!ts) /* key not present */
1887 return 1;
1888
1889 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02001890 if (!ptr) {
1891 /* fallback on the gpc array */
1892 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 1);
1893 }
1894
Daniel Corbett3e60b112018-05-27 09:47:12 -04001895 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001896 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001897
Daniel Corbett3e60b112018-05-27 09:47:12 -04001898 stktable_release(t, ts);
1899 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001900}
1901
1902/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1903 * it up into this table. Returns the event rate of the GPC1 counter for the key
1904 * if the key is present in the table, otherwise zero, so that comparisons can
1905 * be easily performed. If the inspected parameter is not stored in the table,
1906 * <not found> is returned.
1907 */
1908static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1909{
1910 struct stktable *t;
1911 struct stktable_key *key;
1912 struct stksess *ts;
1913 void *ptr;
1914
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001915 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001916
1917 key = smp_to_stkey(smp, t);
1918 if (!key)
1919 return 0;
1920
1921 ts = stktable_lookup_key(t, key);
1922
1923 smp->flags = SMP_F_VOL_TEST;
1924 smp->data.type = SMP_T_SINT;
1925 smp->data.u.sint = 0;
1926
1927 if (!ts) /* key not present */
1928 return 1;
1929
1930 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001931 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001932 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001933 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001934 else {
1935 /* fallback on the gpc array */
1936 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 1);
1937 if (ptr)
1938 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1939 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1940 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001941
Daniel Corbett3e60b112018-05-27 09:47:12 -04001942 stktable_release(t, ts);
1943 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001944}
1945
1946/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001947 * it up into this table. Returns the cumulated number of HTTP request errors
1948 * for the key if the key is present in the table, otherwise zero, so that
1949 * comparisons can be easily performed. If the inspected parameter is not stored
1950 * in the table, <not found> is returned.
1951 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001952static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001953{
1954 struct stktable *t;
1955 struct stktable_key *key;
1956 struct stksess *ts;
1957 void *ptr;
1958
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001959 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001960
1961 key = smp_to_stkey(smp, t);
1962 if (!key)
1963 return 0;
1964
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001965 ts = stktable_lookup_key(t, key);
1966
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001967 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001968 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001969 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001970
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001971 if (!ts) /* key not present */
1972 return 1;
1973
1974 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001975 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001976 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001977
Daniel Corbett3e60b112018-05-27 09:47:12 -04001978 stktable_release(t, ts);
1979 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001980}
1981
1982/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1983 * it up into this table. Returns the HTTP request error rate the key
1984 * if the key is present in the table, otherwise zero, so that comparisons can
1985 * be easily performed. If the inspected parameter is not stored in the table,
1986 * <not found> is returned.
1987 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001988static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001989{
1990 struct stktable *t;
1991 struct stktable_key *key;
1992 struct stksess *ts;
1993 void *ptr;
1994
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001995 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001996
1997 key = smp_to_stkey(smp, t);
1998 if (!key)
1999 return 0;
2000
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002001 ts = stktable_lookup_key(t, key);
2002
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002003 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002004 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002005 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002006
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002007 if (!ts) /* key not present */
2008 return 1;
2009
2010 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002011 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002012 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002013 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002014
Daniel Corbett3e60b112018-05-27 09:47:12 -04002015 stktable_release(t, ts);
2016 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002017}
2018
2019/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002020 * it up into this table. Returns the cumulated number of HTTP response failures
2021 * for the key if the key is present in the table, otherwise zero, so that
2022 * comparisons can be easily performed. If the inspected parameter is not stored
2023 * in the table, <not found> is returned.
2024 */
2025static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
2026{
2027 struct stktable *t;
2028 struct stktable_key *key;
2029 struct stksess *ts;
2030 void *ptr;
2031
2032 t = arg_p[0].data.t;
2033
2034 key = smp_to_stkey(smp, t);
2035 if (!key)
2036 return 0;
2037
2038 ts = stktable_lookup_key(t, key);
2039
2040 smp->flags = SMP_F_VOL_TEST;
2041 smp->data.type = SMP_T_SINT;
2042 smp->data.u.sint = 0;
2043
2044 if (!ts) /* key not present */
2045 return 1;
2046
2047 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2048 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002049 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002050
2051 stktable_release(t, ts);
2052 return !!ptr;
2053}
2054
2055/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2056 * it up into this table. Returns the HTTP response failure rate for the key
2057 * if the key is present in the table, otherwise zero, so that comparisons can
2058 * be easily performed. If the inspected parameter is not stored in the table,
2059 * <not found> is returned.
2060 */
2061static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
2062{
2063 struct stktable *t;
2064 struct stktable_key *key;
2065 struct stksess *ts;
2066 void *ptr;
2067
2068 t = arg_p[0].data.t;
2069
2070 key = smp_to_stkey(smp, t);
2071 if (!key)
2072 return 0;
2073
2074 ts = stktable_lookup_key(t, key);
2075
2076 smp->flags = SMP_F_VOL_TEST;
2077 smp->data.type = SMP_T_SINT;
2078 smp->data.u.sint = 0;
2079
2080 if (!ts) /* key not present */
2081 return 1;
2082
2083 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2084 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002085 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002086 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
2087
2088 stktable_release(t, ts);
2089 return !!ptr;
2090}
2091
2092/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002093 * it up into this table. Returns the cumulated number of HTTP request for the
2094 * key if the key is present in the table, otherwise zero, so that comparisons
2095 * can be easily performed. If the inspected parameter is not stored in the
2096 * table, <not found> is returned.
2097 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002098static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002099{
2100 struct stktable *t;
2101 struct stktable_key *key;
2102 struct stksess *ts;
2103 void *ptr;
2104
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002105 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002106
2107 key = smp_to_stkey(smp, t);
2108 if (!key)
2109 return 0;
2110
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002111 ts = stktable_lookup_key(t, key);
2112
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002113 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002114 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002115 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002116
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002117 if (!ts) /* key not present */
2118 return 1;
2119
2120 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002121 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002122 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002123
Daniel Corbett3e60b112018-05-27 09:47:12 -04002124 stktable_release(t, ts);
2125 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002126}
2127
2128/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2129 * it up into this table. Returns the HTTP request rate the key if the key is
2130 * present in the table, otherwise zero, so that comparisons can be easily
2131 * performed. If the inspected parameter is not stored in the table, <not found>
2132 * is returned.
2133 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002134static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002135{
2136 struct stktable *t;
2137 struct stktable_key *key;
2138 struct stksess *ts;
2139 void *ptr;
2140
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002141 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002142
2143 key = smp_to_stkey(smp, t);
2144 if (!key)
2145 return 0;
2146
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002147 ts = stktable_lookup_key(t, key);
2148
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002149 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002150 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002151 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002152
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002153 if (!ts) /* key not present */
2154 return 1;
2155
2156 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002157 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002158 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002159 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002160
Daniel Corbett3e60b112018-05-27 09:47:12 -04002161 stktable_release(t, ts);
2162 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002163}
2164
2165/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2166 * it up into this table. Returns the volume of datareceived from clients in kbytes
2167 * if the key is present in the table, otherwise zero, so that comparisons can
2168 * be easily performed. If the inspected parameter is not stored in the table,
2169 * <not found> is returned.
2170 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002171static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002172{
2173 struct stktable *t;
2174 struct stktable_key *key;
2175 struct stksess *ts;
2176 void *ptr;
2177
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002178 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002179
2180 key = smp_to_stkey(smp, t);
2181 if (!key)
2182 return 0;
2183
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002184 ts = stktable_lookup_key(t, key);
2185
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002186 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002187 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002188 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002189
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002190 if (!ts) /* key not present */
2191 return 1;
2192
2193 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002194 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002195 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002196
Daniel Corbett3e60b112018-05-27 09:47:12 -04002197 stktable_release(t, ts);
2198 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002199}
2200
2201/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2202 * it up into this table. Returns the volume of data sent to clients in kbytes
2203 * if the key is present in the table, otherwise zero, so that comparisons can
2204 * be easily performed. If the inspected parameter is not stored in the table,
2205 * <not found> is returned.
2206 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002207static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002208{
2209 struct stktable *t;
2210 struct stktable_key *key;
2211 struct stksess *ts;
2212 void *ptr;
2213
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002214 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002215
2216 key = smp_to_stkey(smp, t);
2217 if (!key)
2218 return 0;
2219
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002220 ts = stktable_lookup_key(t, key);
2221
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002222 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002224 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002225
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002226 if (!ts) /* key not present */
2227 return 1;
2228
2229 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002230 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002231 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002232
Daniel Corbett3e60b112018-05-27 09:47:12 -04002233 stktable_release(t, ts);
2234 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002235}
2236
2237/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2238 * it up into this table. Returns the server ID associated with the key if the
2239 * key is present in the table, otherwise zero, so that comparisons can be
2240 * easily performed. If the inspected parameter is not stored in the table,
2241 * <not found> is returned.
2242 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002243static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002244{
2245 struct stktable *t;
2246 struct stktable_key *key;
2247 struct stksess *ts;
2248 void *ptr;
2249
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002250 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002251
2252 key = smp_to_stkey(smp, t);
2253 if (!key)
2254 return 0;
2255
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002256 ts = stktable_lookup_key(t, key);
2257
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002258 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002259 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002260 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002261
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002262 if (!ts) /* key not present */
2263 return 1;
2264
2265 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002266 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002267 smp->data.u.sint = stktable_data_cast(ptr, std_t_sint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002268
Daniel Corbett3e60b112018-05-27 09:47:12 -04002269 stktable_release(t, ts);
2270 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002271}
2272
2273/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2274 * it up into this table. Returns the cumulated number of sessions for the
2275 * key if the key is present in the table, otherwise zero, so that comparisons
2276 * can be easily performed. If the inspected parameter is not stored in the
2277 * table, <not found> is returned.
2278 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002279static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002280{
2281 struct stktable *t;
2282 struct stktable_key *key;
2283 struct stksess *ts;
2284 void *ptr;
2285
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002286 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002287
2288 key = smp_to_stkey(smp, t);
2289 if (!key)
2290 return 0;
2291
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002292 ts = stktable_lookup_key(t, key);
2293
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002294 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002295 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002296 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002297
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002298 if (!ts) /* key not present */
2299 return 1;
2300
2301 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002302 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002303 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002304
Daniel Corbett3e60b112018-05-27 09:47:12 -04002305 stktable_release(t, ts);
2306 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002307}
2308
2309/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2310 * it up into this table. Returns the session rate the key if the key is
2311 * present in the table, otherwise zero, so that comparisons can be easily
2312 * performed. If the inspected parameter is not stored in the table, <not found>
2313 * is returned.
2314 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002315static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002316{
2317 struct stktable *t;
2318 struct stktable_key *key;
2319 struct stksess *ts;
2320 void *ptr;
2321
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002322 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002323
2324 key = smp_to_stkey(smp, t);
2325 if (!key)
2326 return 0;
2327
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002328 ts = stktable_lookup_key(t, key);
2329
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002330 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002331 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002332 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002333
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002334 if (!ts) /* key not present */
2335 return 1;
2336
2337 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002338 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002339 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002340 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002341
Daniel Corbett3e60b112018-05-27 09:47:12 -04002342 stktable_release(t, ts);
2343 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002344}
2345
2346/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2347 * it up into this table. Returns the amount of concurrent connections tracking
2348 * the same key if the key is present in the table, otherwise zero, so that
2349 * comparisons can be easily performed. If the inspected parameter is not
2350 * stored in the table, <not found> is returned.
2351 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002352static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002353{
2354 struct stktable *t;
2355 struct stktable_key *key;
2356 struct stksess *ts;
2357
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002358 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002359
2360 key = smp_to_stkey(smp, t);
2361 if (!key)
2362 return 0;
2363
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002364 ts = stktable_lookup_key(t, key);
2365
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002366 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002367 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002368 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002369
Tim Duesterhus65189c12018-06-26 15:57:29 +02002370 if (!ts)
2371 return 1;
2372
2373 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002374
Daniel Corbett3e60b112018-05-27 09:47:12 -04002375 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002376 return 1;
2377}
2378
Emeric Brun4d7ada82021-06-30 19:04:16 +02002379/* This function increments the gpc counter at index 'rule->arg.gpc.idx' of the
2380 * array on the tracksc counter of index 'rule->arg.gpc.sc' stored into the
2381 * <stream> or directly in the session <sess> if <stream> is set to NULL
2382 *
2383 * This function always returns ACT_RET_CONT and parameter flags is unused.
2384 */
2385static enum act_return action_inc_gpc(struct act_rule *rule, struct proxy *px,
2386 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002387{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002388 struct stksess *ts;
2389 struct stkctr *stkctr;
2390
2391 /* Extract the stksess, return OK if no stksess available. */
2392 if (s)
2393 stkctr = &s->stkctr[rule->arg.gpc.sc];
2394 else
2395 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002396
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002397 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002398 if (ts) {
2399 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002400
Emeric Brun4d7ada82021-06-30 19:04:16 +02002401 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2402 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2403 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2404
Emeric Brun819fc6f2017-06-13 19:37:32 +02002405 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002406 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002407
2408 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002409 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun4d7ada82021-06-30 19:04:16 +02002410 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002411
Emeric Brun819fc6f2017-06-13 19:37:32 +02002412 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002413 stktable_data_cast(ptr2, std_t_uint)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002414
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002415 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002416
2417 /* If data was modified, we need to touch to re-schedule sync */
2418 stktable_touch_local(stkctr->table, ts, 0);
2419 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002420 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002421 return ACT_RET_CONT;
2422}
2423
Emeric Brun4d7ada82021-06-30 19:04:16 +02002424/* Same as action_inc_gpc() but for gpc0 only */
2425static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
2426 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002427{
Emeric Brun4d7ada82021-06-30 19:04:16 +02002428 struct stksess *ts;
2429 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002430 unsigned int period = 0;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002431
Emeric Brun4d7ada82021-06-30 19:04:16 +02002432 /* Extract the stksess, return OK if no stksess available. */
2433 if (s)
2434 stkctr = &s->stkctr[rule->arg.gpc.sc];
2435 else
2436 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002437
Emeric Brun4d7ada82021-06-30 19:04:16 +02002438 ts = stkctr_entry(stkctr);
2439 if (ts) {
2440 void *ptr1, *ptr2;
2441
2442 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2443 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002444 if (ptr1) {
2445 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
2446 }
2447 else {
2448 /* fallback on the gpc array */
2449 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 0);
2450 if (ptr1)
2451 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2452 }
2453
Emeric Brun4d7ada82021-06-30 19:04:16 +02002454 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02002455 if (!ptr2) {
2456 /* fallback on the gpc array */
2457 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 0);
2458 }
2459
Emeric Brun4d7ada82021-06-30 19:04:16 +02002460 if (ptr1 || ptr2) {
2461 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2462
2463 if (ptr1)
2464 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002465 period, 1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002466
2467 if (ptr2)
2468 stktable_data_cast(ptr2, std_t_uint)++;
2469
2470 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2471
2472 /* If data was modified, we need to touch to re-schedule sync */
2473 stktable_touch_local(stkctr->table, ts, 0);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002474 }
2475 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002476 return ACT_RET_CONT;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002477}
2478
Emeric Brun4d7ada82021-06-30 19:04:16 +02002479/* Same as action_inc_gpc() but for gpc1 only */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002480static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2481 struct session *sess, struct stream *s, int flags)
2482{
2483 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002484 struct stkctr *stkctr = NULL;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002485 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002486
2487 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002488 if (s && s->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002489 stkctr = &s->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002490 else if (sess->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002491 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002492 else
2493 return ACT_RET_CONT;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002494
2495 ts = stkctr_entry(stkctr);
2496 if (ts) {
2497 void *ptr1, *ptr2;
2498
2499 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2500 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002501 if (ptr1) {
2502 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
2503 }
2504 else {
2505 /* fallback on the gpc array */
2506 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 1);
2507 if (ptr1)
2508 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2509 }
2510
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002511 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02002512 if (!ptr2) {
2513 /* fallback on the gpc array */
2514 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 1);
2515 }
2516
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002517 if (ptr1 || ptr2) {
2518 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2519
2520 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002521 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002522 period, 1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002523
2524 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002525 stktable_data_cast(ptr2, std_t_uint)++;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002526
2527 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2528
2529 /* If data was modified, we need to touch to re-schedule sync */
2530 stktable_touch_local(stkctr->table, ts, 0);
2531 }
2532 }
2533 return ACT_RET_CONT;
2534}
2535
Emeric Brun4d7ada82021-06-30 19:04:16 +02002536/* This function is a common parser for actions incrementing the GPC
2537 * (General Purpose Counters). It understands the formats:
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002538 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002539 * sc-inc-gpc(<gpc IDX>,<track ID>)
2540 * sc-inc-gpc0([<track ID>])
2541 * sc-inc-gpc1([<track ID>])
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002542 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002543 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
2544 * message. Otherwise it returns ACT_RET_PRS_OK.
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002545 */
Emeric Brun4d7ada82021-06-30 19:04:16 +02002546static enum act_parse_ret parse_inc_gpc(const char **args, int *arg, struct proxy *px,
2547 struct act_rule *rule, char **err)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002548{
2549 const char *cmd_name = args[*arg-1];
2550 char *error;
2551
Willy Tarreau6c011712023-01-06 16:09:58 +01002552 if (!global.tune.nb_stk_ctr) {
2553 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2554 return ACT_RET_PRS_ERR;
2555 }
2556
Emeric Brun4d7ada82021-06-30 19:04:16 +02002557 cmd_name += strlen("sc-inc-gpc");
2558 if (*cmd_name == '(') {
2559 cmd_name++; /* skip the '(' */
2560 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2561 if (*error != ',') {
2562 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 +01002563 return ACT_RET_PRS_ERR;
2564 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002565 else {
2566 cmd_name = error + 1; /* skip the ',' */
2567 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2568 if (*error != ')') {
2569 memprintf(err, "invalid stick table track ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2570 return ACT_RET_PRS_ERR;
2571 }
2572
Willy Tarreau6c011712023-01-06 16:09:58 +01002573 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2574 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d (tune.stick-counters)",
2575 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002576 return ACT_RET_PRS_ERR;
2577 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002578 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002579 rule->action_ptr = action_inc_gpc;
2580 }
2581 else if (*cmd_name == '0' ||*cmd_name == '1') {
2582 char c = *cmd_name;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002583
Emeric Brun4d7ada82021-06-30 19:04:16 +02002584 cmd_name++;
2585 if (*cmd_name == '\0') {
2586 /* default stick table id. */
2587 rule->arg.gpc.sc = 0;
2588 } else {
2589 /* parse the stick table id. */
2590 if (*cmd_name != '(') {
2591 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2592 return ACT_RET_PRS_ERR;
2593 }
2594 cmd_name++; /* jump the '(' */
2595 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2596 if (*error != ')') {
2597 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2598 return ACT_RET_PRS_ERR;
2599 }
2600
Willy Tarreau6c011712023-01-06 16:09:58 +01002601 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2602 memprintf(err, "invalid stick table track ID. The max allowed ID is %d (tune.stick-counters)",
2603 global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002604 return ACT_RET_PRS_ERR;
2605 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002606 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002607 if (c == '1')
2608 rule->action_ptr = action_inc_gpc1;
2609 else
2610 rule->action_ptr = action_inc_gpc0;
2611 }
2612 else {
2613 /* default stick table id. */
Willy Tarreau20391512023-01-02 17:35:50 +01002614 memprintf(err, "invalid gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002615 return ACT_RET_PRS_ERR;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002616 }
2617 rule->action = ACT_CUSTOM;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002618 return ACT_RET_PRS_OK;
2619}
2620
Emeric Brun877b0b52021-06-30 18:57:49 +02002621/* This function sets the gpt at index 'rule->arg.gpt.idx' of the array on the
2622 * tracksc counter of index 'rule->arg.gpt.sc' stored into the <stream> or
2623 * directly in the session <sess> if <stream> is set to NULL. This gpt is
2624 * set to the value computed by the expression 'rule->arg.gpt.expr' or if
2625 * 'rule->arg.gpt.expr' is null directly to the value of 'rule->arg.gpt.value'.
2626 *
2627 * This function always returns ACT_RET_CONT and parameter flags is unused.
2628 */
2629static enum act_return action_set_gpt(struct act_rule *rule, struct proxy *px,
2630 struct session *sess, struct stream *s, int flags)
2631{
2632 void *ptr;
2633 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002634 struct stkctr *stkctr = NULL;
Emeric Brun877b0b52021-06-30 18:57:49 +02002635 unsigned int value = 0;
2636 struct sample *smp;
2637 int smp_opt_dir;
2638
2639 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002640 if (s && s->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002641 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002642 else if (sess->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002643 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002644 else
2645 return ACT_RET_CONT;
Emeric Brun877b0b52021-06-30 18:57:49 +02002646
2647 ts = stkctr_entry(stkctr);
2648 if (!ts)
2649 return ACT_RET_CONT;
2650
2651 /* Store the sample in the required sc, and ignore errors. */
2652 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, rule->arg.gpt.idx);
2653 if (ptr) {
2654
2655 if (!rule->arg.gpt.expr)
2656 value = (unsigned int)(rule->arg.gpt.value);
2657 else {
2658 switch (rule->from) {
2659 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2660 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2661 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2662 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2663 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2664 default:
2665 send_log(px, LOG_ERR, "stick table: internal error while setting gpt%u.", rule->arg.gpt.idx);
2666 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2667 ha_alert("stick table: internal error while executing setting gpt%u.\n", rule->arg.gpt.idx);
2668 return ACT_RET_CONT;
2669 }
2670
2671 /* Fetch and cast the expression. */
2672 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2673 if (!smp) {
2674 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt%u.", rule->arg.gpt.idx);
2675 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2676 ha_alert("stick table: invalid expression or data type while setting gpt%u.\n", rule->arg.gpt.idx);
2677 return ACT_RET_CONT;
2678 }
2679 value = (unsigned int)(smp->data.u.sint);
2680 }
2681
2682 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2683
2684 stktable_data_cast(ptr, std_t_uint) = value;
2685
2686 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2687
2688 stktable_touch_local(stkctr->table, ts, 0);
2689 }
2690
2691 return ACT_RET_CONT;
2692}
2693
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002694/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002695static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002696 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002697{
2698 void *ptr;
2699 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002700 struct stkctr *stkctr = NULL;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002701 unsigned int value = 0;
2702 struct sample *smp;
2703 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002704
2705 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002706 if (s && s->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002707 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002708 else if (sess->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002709 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002710 else
2711 return ACT_RET_CONT;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002712
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002713 ts = stkctr_entry(stkctr);
2714 if (!ts)
2715 return ACT_RET_CONT;
2716
2717 /* Store the sample in the required sc, and ignore errors. */
2718 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02002719 if (!ptr)
2720 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, 0);
2721
Willy Tarreau79c1e912016-01-25 14:54:45 +01002722 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002723 if (!rule->arg.gpt.expr)
2724 value = (unsigned int)(rule->arg.gpt.value);
2725 else {
2726 switch (rule->from) {
2727 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2728 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2729 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2730 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2731 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2732 default:
2733 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2734 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2735 ha_alert("stick table: internal error while executing setting gpt0.\n");
2736 return ACT_RET_CONT;
2737 }
2738
2739 /* Fetch and cast the expression. */
2740 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2741 if (!smp) {
2742 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2743 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2744 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2745 return ACT_RET_CONT;
2746 }
2747 value = (unsigned int)(smp->data.u.sint);
2748 }
2749
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002750 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002751
Emeric Brun0e3457b2021-06-30 17:18:28 +02002752 stktable_data_cast(ptr, std_t_uint) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002753
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002754 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002755
2756 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002757 }
2758
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002759 return ACT_RET_CONT;
2760}
2761
Emeric Brun877b0b52021-06-30 18:57:49 +02002762/* This function is a parser for the "sc-set-gpt" and "sc-set-gpt0" actions.
2763 * It understands the formats:
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002764 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002765 * sc-set-gpt(<gpt IDX>,<track ID>) <expression>
2766 * sc-set-gpt0(<track ID>) <expression>
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002767 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002768 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2769 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpt.expr'
2770 * is filled with the pointer to the expression to execute or NULL if the arg
2771 * is directly an integer stored into 'rule->arg.gpt.value'.
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002772 */
Emeric Brun877b0b52021-06-30 18:57:49 +02002773static enum act_parse_ret parse_set_gpt(const char **args, int *arg, struct proxy *px,
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002774 struct act_rule *rule, char **err)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002775{
2776 const char *cmd_name = args[*arg-1];
2777 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002778 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002779
Willy Tarreau6c011712023-01-06 16:09:58 +01002780 if (!global.tune.nb_stk_ctr) {
2781 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2782 return ACT_RET_PRS_ERR;
2783 }
2784
Emeric Brun877b0b52021-06-30 18:57:49 +02002785 cmd_name += strlen("sc-set-gpt");
2786 if (*cmd_name == '(') {
2787 cmd_name++; /* skip the '(' */
2788 rule->arg.gpt.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2789 if (*error != ',') {
2790 memprintf(err, "Missing gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002791 return ACT_RET_PRS_ERR;
2792 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002793 else {
2794 cmd_name = error + 1; /* skip the ',' */
2795 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2796 if (*error != ')') {
2797 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2798 return ACT_RET_PRS_ERR;
2799 }
2800
Willy Tarreau6c011712023-01-06 16:09:58 +01002801 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002802 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002803 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002804 return ACT_RET_PRS_ERR;
2805 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002806 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002807 rule->action_ptr = action_set_gpt;
2808 }
2809 else if (*cmd_name == '0') {
2810 cmd_name++;
2811 if (*cmd_name == '\0') {
2812 /* default stick table id. */
2813 rule->arg.gpt.sc = 0;
2814 } else {
2815 /* parse the stick table id. */
2816 if (*cmd_name != '(') {
2817 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2818 return ACT_RET_PRS_ERR;
2819 }
2820 cmd_name++; /* jump the '(' */
2821 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2822 if (*error != ')') {
2823 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2824 return ACT_RET_PRS_ERR;
2825 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002826
Willy Tarreau6c011712023-01-06 16:09:58 +01002827 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002828 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002829 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002830 return ACT_RET_PRS_ERR;
2831 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002832 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002833 rule->action_ptr = action_set_gpt0;
2834 }
2835 else {
2836 /* default stick table id. */
2837 memprintf(err, "invalid gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2838 return ACT_RET_PRS_ERR;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002839 }
2840
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002841 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002842 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002843 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002844 if (*error == '\0') {
2845 /* valid integer, skip it */
2846 (*arg)++;
2847 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002848 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002849 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002850 if (!rule->arg.gpt.expr)
2851 return ACT_RET_PRS_ERR;
2852
2853 switch (rule->from) {
2854 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2855 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2856 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2857 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2858 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2859 default:
2860 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2861 return ACT_RET_PRS_ERR;
2862 }
2863 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2864 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2865 sample_src_names(rule->arg.gpt.expr->fetch->use));
2866 free(rule->arg.gpt.expr);
2867 return ACT_RET_PRS_ERR;
2868 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002869 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002870
Thierry FOURNIER42148732015-09-02 17:17:33 +02002871 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002872
2873 return ACT_RET_PRS_OK;
2874}
2875
Willy Tarreau5a72d032023-01-02 18:15:20 +01002876/* This function updates the gpc at index 'rule->arg.gpc.idx' of the array on
2877 * the tracksc counter of index 'rule->arg.gpc.sc' stored into the <stream> or
2878 * directly in the session <sess> if <stream> is set to NULL. This gpc is
2879 * set to the value computed by the expression 'rule->arg.gpc.expr' or if
2880 * 'rule->arg.gpc.expr' is null directly to the value of 'rule->arg.gpc.value'.
2881 *
2882 * This function always returns ACT_RET_CONT and parameter flags is unused.
2883 */
2884static enum act_return action_add_gpc(struct act_rule *rule, struct proxy *px,
2885 struct session *sess, struct stream *s, int flags)
2886{
2887 void *ptr1, *ptr2;
2888 struct stksess *ts;
2889 struct stkctr *stkctr;
2890 unsigned int value = 0;
2891 struct sample *smp;
2892 int smp_opt_dir;
2893
2894 /* Extract the stksess, return OK if no stksess available. */
2895 if (s)
2896 stkctr = &s->stkctr[rule->arg.gpc.sc];
2897 else
2898 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2899
2900 ts = stkctr_entry(stkctr);
2901 if (!ts)
2902 return ACT_RET_CONT;
2903
2904 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2905 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2906 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2907
2908 if (ptr1 || ptr2) {
2909 if (!rule->arg.gpc.expr)
2910 value = (unsigned int)(rule->arg.gpc.value);
2911 else {
2912 switch (rule->from) {
2913 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2914 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2915 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2916 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2917 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2918 default:
2919 send_log(px, LOG_ERR, "stick table: internal error while setting gpc%u.", rule->arg.gpc.idx);
2920 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2921 ha_alert("stick table: internal error while executing setting gpc%u.\n", rule->arg.gpc.idx);
2922 return ACT_RET_CONT;
2923 }
2924
2925 /* Fetch and cast the expression. */
2926 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpc.expr, SMP_T_SINT);
2927 if (!smp) {
2928 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpc%u.", rule->arg.gpc.idx);
2929 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2930 ha_alert("stick table: invalid expression or data type while setting gpc%u.\n", rule->arg.gpc.idx);
2931 return ACT_RET_CONT;
2932 }
2933 value = (unsigned int)(smp->data.u.sint);
2934 }
2935
2936 if (value) {
2937 /* only update the value if non-null increment */
2938 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2939
2940 if (ptr1)
2941 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
2942 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, value);
2943
2944 if (ptr2)
2945 stktable_data_cast(ptr2, std_t_uint) += value;
2946
2947 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2948 }
2949 /* always touch the table so that it doesn't expire */
2950 stktable_touch_local(stkctr->table, ts, 0);
2951 }
2952
2953 return ACT_RET_CONT;
2954}
2955
2956/* This function is a parser for the "sc-add-gpc" action. It understands the
2957 * format:
2958 *
2959 * sc-add-gpc(<gpc IDX>,<track ID>) <expression>
2960 *
2961 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2962 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpc.expr'
2963 * is filled with the pointer to the expression to execute or NULL if the arg
2964 * is directly an integer stored into 'rule->arg.gpt.value'.
2965 */
2966static enum act_parse_ret parse_add_gpc(const char **args, int *arg, struct proxy *px,
2967 struct act_rule *rule, char **err)
2968{
2969 const char *cmd_name = args[*arg-1];
2970 char *error;
2971 int smp_val;
2972
2973 cmd_name += strlen("sc-add-gpc");
2974 if (*cmd_name != '(') {
2975 memprintf(err, "Missing or invalid arguments for '%s'. Expects sc-add-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2976 return ACT_RET_PRS_ERR;
2977 }
2978 cmd_name++; /* skip the '(' */
2979 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2980 if (*error != ',') {
2981 memprintf(err, "Missing gpc ID. Expects %s(<GPC ID>,<Track ID>)", args[*arg-1]);
2982 return ACT_RET_PRS_ERR;
2983 }
2984 else {
2985 cmd_name = error + 1; /* skip the ',' */
2986 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2987 if (*error != ')') {
2988 memprintf(err, "invalid stick table track ID '%s'. Expects %s(<GPC ID>,<Track ID>)", cmd_name, args[*arg-1]);
2989 return ACT_RET_PRS_ERR;
2990 }
2991
2992 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
2993 memprintf(err, "invalid stick table track ID '%s' for '%s'. The max allowed ID is %d",
2994 cmd_name, args[*arg-1], MAX_SESS_STKCTR-1);
2995 return ACT_RET_PRS_ERR;
2996 }
2997 }
2998 rule->action_ptr = action_add_gpc;
2999
3000 /* value may be either an integer or an expression */
3001 rule->arg.gpc.expr = NULL;
3002 rule->arg.gpc.value = strtol(args[*arg], &error, 10);
3003 if (*error == '\0') {
3004 /* valid integer, skip it */
3005 (*arg)++;
3006 } else {
3007 rule->arg.gpc.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
3008 px->conf.args.line, err, &px->conf.args, NULL);
3009 if (!rule->arg.gpc.expr)
3010 return ACT_RET_PRS_ERR;
3011
3012 switch (rule->from) {
3013 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
3014 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
3015 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
3016 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
3017 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
3018 default:
3019 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
3020 return ACT_RET_PRS_ERR;
3021 }
3022
3023 if (!(rule->arg.gpc.expr->fetch->val & smp_val)) {
3024 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
3025 sample_src_names(rule->arg.gpc.expr->fetch->use));
3026 free(rule->arg.gpc.expr);
3027 return ACT_RET_PRS_ERR;
3028 }
3029 }
3030
3031 rule->action = ACT_CUSTOM;
3032
3033 return ACT_RET_PRS_OK;
3034}
3035
Willy Tarreau7d562212016-11-25 16:10:05 +01003036/* set temp integer to the number of used entries in the table pointed to by expr.
3037 * Accepts exactly 1 argument of type table.
3038 */
3039static int
3040smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3041{
3042 smp->flags = SMP_F_VOL_TEST;
3043 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003044 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003045 return 1;
3046}
3047
3048/* set temp integer to the number of free entries in the table pointed to by expr.
3049 * Accepts exactly 1 argument of type table.
3050 */
3051static int
3052smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
3053{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003054 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003055
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003056 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003057 smp->flags = SMP_F_VOL_TEST;
3058 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003059 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003060 return 1;
3061}
3062
3063/* Returns a pointer to a stkctr depending on the fetch keyword name.
3064 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
3065 * sc[0-9]_* will return a pointer to the respective field in the
3066 * stream <l4>. sc_* requires an UINT argument specifying the stick
3067 * counter number. src_* will fill a locally allocated structure with
3068 * the table and entry corresponding to what is specified with src_*.
3069 * NULL may be returned if the designated stkctr is not tracked. For
3070 * the sc_* and sc[0-9]_* forms, an optional table argument may be
3071 * passed. When present, the currently tracked key is then looked up
3072 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003073 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01003074 * multiple tables). <strm> is allowed to be NULL, in which case only
3075 * the session will be consulted.
3076 */
3077struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003078smp_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 +01003079{
Willy Tarreau7d562212016-11-25 16:10:05 +01003080 struct stkctr *stkptr;
3081 struct stksess *stksess;
3082 unsigned int num = kw[2] - '0';
3083 int arg = 0;
3084
3085 if (num == '_' - '0') {
3086 /* sc_* variant, args[0] = ctr# (mandatory) */
3087 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01003088 }
3089 else if (num > 9) { /* src_* variant, args[0] = table */
3090 struct stktable_key *key;
3091 struct connection *conn = objt_conn(sess->origin);
3092 struct sample smp;
3093
3094 if (!conn)
3095 return NULL;
3096
Joseph Herlant5662fa42018-11-15 13:43:28 -08003097 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003098 smp.px = NULL;
3099 smp.sess = sess;
3100 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003101 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003102 return NULL;
3103
3104 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003105 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003106 if (!key)
3107 return NULL;
3108
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003109 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003110 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
3111 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003112 }
3113
3114 /* Here, <num> contains the counter number from 0 to 9 for
3115 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
3116 * args[arg] is the first optional argument. We first lookup the
3117 * ctr form the stream, then from the session if it was not there.
Willy Tarreau6c011712023-01-06 16:09:58 +01003118 * But we must be sure the counter does not exceed global.tune.nb_stk_ctr.
Willy Tarreau7d562212016-11-25 16:10:05 +01003119 */
Willy Tarreau6c011712023-01-06 16:09:58 +01003120 if (num >= global.tune.nb_stk_ctr)
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02003121 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003122
Willy Tarreau6c011712023-01-06 16:09:58 +01003123 stkptr = NULL;
3124 if (strm && strm->stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003125 stkptr = &strm->stkctr[num];
Willy Tarreau6c011712023-01-06 16:09:58 +01003126 if (!strm || !stkptr || !stkctr_entry(stkptr)) {
3127 if (sess->stkctr)
3128 stkptr = &sess->stkctr[num];
3129 else
3130 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003131 if (!stkctr_entry(stkptr))
3132 return NULL;
3133 }
3134
3135 stksess = stkctr_entry(stkptr);
3136 if (!stksess)
3137 return NULL;
3138
3139 if (unlikely(args[arg].type == ARGT_TAB)) {
3140 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003141 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003142 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
3143 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003144 }
3145 return stkptr;
3146}
3147
3148/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
3149 * the entry if it doesn't exist yet. This is needed for a few fetch
3150 * functions which need to create an entry, such as src_inc_gpc* and
3151 * src_clr_gpc*.
3152 */
3153struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003154smp_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 +01003155{
Willy Tarreau7d562212016-11-25 16:10:05 +01003156 struct stktable_key *key;
3157 struct connection *conn = objt_conn(sess->origin);
3158 struct sample smp;
3159
3160 if (strncmp(kw, "src_", 4) != 0)
3161 return NULL;
3162
3163 if (!conn)
3164 return NULL;
3165
Joseph Herlant5662fa42018-11-15 13:43:28 -08003166 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003167 smp.px = NULL;
3168 smp.sess = sess;
3169 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003170 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003171 return NULL;
3172
3173 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003174 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003175 if (!key)
3176 return NULL;
3177
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003178 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003179 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
3180 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003181}
3182
3183/* set return a boolean indicating if the requested stream counter is
3184 * currently being tracked or not.
3185 * Supports being called as "sc[0-9]_tracked" only.
3186 */
3187static int
3188smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
3189{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003190 struct stkctr tmpstkctr;
3191 struct stkctr *stkctr;
3192
Willy Tarreau7d562212016-11-25 16:10:05 +01003193 smp->flags = SMP_F_VOL_TEST;
3194 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003195 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3196 smp->data.u.sint = !!stkctr;
3197
3198 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02003199 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02003200 stktable_release(stkctr->table, stkctr_entry(stkctr));
3201
Emeric Brun877b0b52021-06-30 18:57:49 +02003202 return 1;
3203}
3204
3205/* set <smp> to the General Purpose Tag of index set as first arg
3206 * to value from the stream's tracked frontend counters or from the src.
3207 * Supports being called as "sc_get_gpt(<gpt-idx>,<sc-idx>[,<table>])" or
3208 * "src_get_gpt(<gpt-idx>[,<table>])" only. Value zero is returned if
3209 * the key is new or gpt is not stored.
3210 */
3211static int
3212smp_fetch_sc_get_gpt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3213{
3214 struct stkctr tmpstkctr;
3215 struct stkctr *stkctr;
3216 unsigned int idx;
3217
3218 idx = args[0].data.sint;
3219
3220 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3221 if (!stkctr)
3222 return 0;
3223
3224 smp->flags = SMP_F_VOL_TEST;
3225 smp->data.type = SMP_T_SINT;
3226 smp->data.u.sint = 0;
3227
3228 if (stkctr_entry(stkctr)) {
3229 void *ptr;
3230
3231 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, idx);
3232 if (!ptr) {
3233 if (stkctr == &tmpstkctr)
3234 stktable_release(stkctr->table, stkctr_entry(stkctr));
3235 return 0; /* parameter not stored */
3236 }
3237
3238 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3239
3240 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3241
3242 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3243
3244 if (stkctr == &tmpstkctr)
3245 stktable_release(stkctr->table, stkctr_entry(stkctr));
3246 }
Willy Tarreau7d562212016-11-25 16:10:05 +01003247 return 1;
3248}
3249
3250/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
3251 * frontend counters or from the src.
3252 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
3253 * zero is returned if the key is new.
3254 */
3255static int
3256smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3257{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003258 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003259 struct stkctr *stkctr;
3260
Emeric Brun819fc6f2017-06-13 19:37:32 +02003261 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003262 if (!stkctr)
3263 return 0;
3264
3265 smp->flags = SMP_F_VOL_TEST;
3266 smp->data.type = SMP_T_SINT;
3267 smp->data.u.sint = 0;
3268
Emeric Brun819fc6f2017-06-13 19:37:32 +02003269 if (stkctr_entry(stkctr)) {
3270 void *ptr;
3271
3272 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02003273 if (!ptr)
3274 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, 0);
3275
Emeric Brun4d7ada82021-06-30 19:04:16 +02003276 if (!ptr) {
3277 if (stkctr == &tmpstkctr)
3278 stktable_release(stkctr->table, stkctr_entry(stkctr));
3279 return 0; /* parameter not stored */
3280 }
3281
3282 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3283
3284 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3285
3286 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3287
3288 if (stkctr == &tmpstkctr)
3289 stktable_release(stkctr->table, stkctr_entry(stkctr));
3290 }
3291 return 1;
3292}
3293
3294/* set <smp> to the GPC[args(0)]'s value from the stream's tracked
3295 * frontend counters or from the src.
3296 * Supports being called as "sc_get_gpc(<gpc-idx>,<sc-idx>[,<table>])" or
3297 * "src_get_gpc(<gpc-idx>[,<table>])" only. Value
3298 * Value zero is returned if the key is new or gpc is not stored.
3299 */
3300static int
3301smp_fetch_sc_get_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3302{
3303 struct stkctr tmpstkctr;
3304 struct stkctr *stkctr;
3305 unsigned int idx;
3306
3307 idx = args[0].data.sint;
3308
3309 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3310 if (!stkctr)
3311 return 0;
3312
3313 smp->flags = SMP_F_VOL_TEST;
3314 smp->data.type = SMP_T_SINT;
3315 smp->data.u.sint = 0;
3316
3317 if (stkctr_entry(stkctr) != NULL) {
3318 void *ptr;
3319
3320 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003321 if (!ptr) {
3322 if (stkctr == &tmpstkctr)
3323 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003324 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003325 }
3326
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003327 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003328
Emeric Brun0e3457b2021-06-30 17:18:28 +02003329 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003330
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003331 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003332
3333 if (stkctr == &tmpstkctr)
3334 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003335 }
3336 return 1;
3337}
3338
3339/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
3340 * frontend counters or from the src.
3341 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
3342 * zero is returned if the key is new.
3343 */
3344static int
3345smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3346{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003347 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003348 struct stkctr *stkctr;
3349
Emeric Brun819fc6f2017-06-13 19:37:32 +02003350 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003351 if (!stkctr)
3352 return 0;
3353
3354 smp->flags = SMP_F_VOL_TEST;
3355 smp->data.type = SMP_T_SINT;
3356 smp->data.u.sint = 0;
3357
3358 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003359 void *ptr;
3360
3361 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3362 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003363 /* fallback on the gpc array */
3364 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3365 }
3366
3367 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003368 if (stkctr == &tmpstkctr)
3369 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003370 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003371 }
3372
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003373 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003374
Emeric Brun0e3457b2021-06-30 17:18:28 +02003375 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003376
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003377 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003378
3379 if (stkctr == &tmpstkctr)
3380 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003381 }
3382 return 1;
3383}
3384
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003385/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
3386 * frontend counters or from the src.
3387 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
3388 * zero is returned if the key is new.
3389 */
3390static int
3391smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3392{
3393 struct stkctr tmpstkctr;
3394 struct stkctr *stkctr;
3395
3396 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3397 if (!stkctr)
3398 return 0;
3399
3400 smp->flags = SMP_F_VOL_TEST;
3401 smp->data.type = SMP_T_SINT;
3402 smp->data.u.sint = 0;
3403
3404 if (stkctr_entry(stkctr) != NULL) {
3405 void *ptr;
3406
3407 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3408 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003409 /* fallback on the gpc array */
3410 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3411 }
3412
3413 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003414 if (stkctr == &tmpstkctr)
3415 stktable_release(stkctr->table, stkctr_entry(stkctr));
3416 return 0; /* parameter not stored */
3417 }
3418
3419 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3420
Emeric Brun0e3457b2021-06-30 17:18:28 +02003421 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003422
3423 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3424
3425 if (stkctr == &tmpstkctr)
3426 stktable_release(stkctr->table, stkctr_entry(stkctr));
3427 }
3428 return 1;
3429}
3430
Emeric Brun4d7ada82021-06-30 19:04:16 +02003431/* set <smp> to the GPC[args(0)]'s event rate from the stream's
3432 * tracked frontend counters or from the src.
3433 * Supports being called as "sc_gpc_rate(<gpc-idx>,<sc-idx>[,<table])"
3434 * or "src_gpc_rate(<gpc-idx>[,<table>])" only.
3435 * Value zero is returned if the key is new or gpc_rate is not stored.
3436 */
3437static int
3438smp_fetch_sc_gpc_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3439{
3440 struct stkctr tmpstkctr;
3441 struct stkctr *stkctr;
3442 unsigned int idx;
3443
3444 idx = args[0].data.sint;
3445
3446 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3447 if (!stkctr)
3448 return 0;
3449
3450 smp->flags = SMP_F_VOL_TEST;
3451 smp->data.type = SMP_T_SINT;
3452 smp->data.u.sint = 0;
3453 if (stkctr_entry(stkctr) != NULL) {
3454 void *ptr;
3455
3456 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3457 if (!ptr) {
3458 if (stkctr == &tmpstkctr)
3459 stktable_release(stkctr->table, stkctr_entry(stkctr));
3460 return 0; /* parameter not stored */
3461 }
3462
3463 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3464
3465 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3466 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u);
3467
3468 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3469
3470 if (stkctr == &tmpstkctr)
3471 stktable_release(stkctr->table, stkctr_entry(stkctr));
3472 }
3473 return 1;
3474}
3475
Willy Tarreau7d562212016-11-25 16:10:05 +01003476/* set <smp> to the General Purpose Counter 0's event rate from the stream's
3477 * tracked frontend counters or from the src.
3478 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
3479 * Value zero is returned if the key is new.
3480 */
3481static int
3482smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3483{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003484 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003485 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003486 unsigned int period;
Willy Tarreau7d562212016-11-25 16:10:05 +01003487
Emeric Brun819fc6f2017-06-13 19:37:32 +02003488 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003489 if (!stkctr)
3490 return 0;
3491
3492 smp->flags = SMP_F_VOL_TEST;
3493 smp->data.type = SMP_T_SINT;
3494 smp->data.u.sint = 0;
3495 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003496 void *ptr;
3497
3498 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003499 if (ptr) {
3500 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3501 }
3502 else {
3503 /* fallback on the gpc array */
3504 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3505 if (ptr)
3506 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3507 }
3508
Emeric Brun819fc6f2017-06-13 19:37:32 +02003509 if (!ptr) {
3510 if (stkctr == &tmpstkctr)
3511 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003512 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003513 }
3514
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003515 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003516
Emeric Brun726783d2021-06-30 19:06:43 +02003517 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003518
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003519 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003520
3521 if (stkctr == &tmpstkctr)
3522 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003523 }
3524 return 1;
3525}
3526
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003527/* set <smp> to the General Purpose Counter 1's event rate from the stream's
3528 * tracked frontend counters or from the src.
3529 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
3530 * Value zero is returned if the key is new.
3531 */
3532static int
3533smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3534{
3535 struct stkctr tmpstkctr;
3536 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003537 unsigned int period;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003538
3539 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3540 if (!stkctr)
3541 return 0;
3542
3543 smp->flags = SMP_F_VOL_TEST;
3544 smp->data.type = SMP_T_SINT;
3545 smp->data.u.sint = 0;
3546 if (stkctr_entry(stkctr) != NULL) {
3547 void *ptr;
3548
3549 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003550 if (ptr) {
3551 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3552 }
3553 else {
3554 /* fallback on the gpc array */
3555 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3556 if (ptr)
3557 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3558 }
3559
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003560 if (!ptr) {
3561 if (stkctr == &tmpstkctr)
3562 stktable_release(stkctr->table, stkctr_entry(stkctr));
3563 return 0; /* parameter not stored */
3564 }
3565
3566 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3567
Emeric Brun726783d2021-06-30 19:06:43 +02003568 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 +01003569
3570 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3571
3572 if (stkctr == &tmpstkctr)
3573 stktable_release(stkctr->table, stkctr_entry(stkctr));
3574 }
3575 return 1;
3576}
3577
Emeric Brun4d7ada82021-06-30 19:04:16 +02003578/* Increment the GPC[args(0)] value from the stream's tracked
3579 * frontend counters and return it into temp integer.
3580 * Supports being called as "sc_inc_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3581 * or "src_inc_gpc(<gpc-idx>[,<table>])" only.
3582 */
3583static int
3584smp_fetch_sc_inc_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3585{
3586 struct stkctr tmpstkctr;
3587 struct stkctr *stkctr;
3588 unsigned int idx;
3589
3590 idx = args[0].data.sint;
3591
3592 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3593 if (!stkctr)
3594 return 0;
3595
3596 smp->flags = SMP_F_VOL_TEST;
3597 smp->data.type = SMP_T_SINT;
3598 smp->data.u.sint = 0;
3599
3600 if (!stkctr_entry(stkctr))
3601 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3602
3603 if (stkctr && stkctr_entry(stkctr)) {
3604 void *ptr1,*ptr2;
3605
3606
3607 /* First, update gpc0_rate if it's tracked. Second, update its
3608 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3609 */
3610 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3611 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3612 if (ptr1 || ptr2) {
3613 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3614
3615 if (ptr1) {
3616 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
3617 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
3618 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
3619 }
3620
3621 if (ptr2)
3622 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
3623
3624 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3625
3626 /* If data was modified, we need to touch to re-schedule sync */
3627 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3628 }
3629 else if (stkctr == &tmpstkctr)
3630 stktable_release(stkctr->table, stkctr_entry(stkctr));
3631 }
3632 return 1;
3633}
3634
Willy Tarreau7d562212016-11-25 16:10:05 +01003635/* Increment the General Purpose Counter 0 value from the stream's tracked
3636 * frontend counters and return it into temp integer.
3637 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
3638 */
3639static int
3640smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3641{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003642 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003643 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003644 unsigned int period = 0;
Willy Tarreau7d562212016-11-25 16:10:05 +01003645
Emeric Brun819fc6f2017-06-13 19:37:32 +02003646 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003647 if (!stkctr)
3648 return 0;
3649
3650 smp->flags = SMP_F_VOL_TEST;
3651 smp->data.type = SMP_T_SINT;
3652 smp->data.u.sint = 0;
3653
Emeric Brun819fc6f2017-06-13 19:37:32 +02003654 if (!stkctr_entry(stkctr))
3655 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003656
3657 if (stkctr && stkctr_entry(stkctr)) {
3658 void *ptr1,*ptr2;
3659
Emeric Brun819fc6f2017-06-13 19:37:32 +02003660
Willy Tarreau7d562212016-11-25 16:10:05 +01003661 /* First, update gpc0_rate if it's tracked. Second, update its
3662 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3663 */
3664 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003665 if (ptr1) {
3666 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3667 }
3668 else {
3669 /* fallback on the gpc array */
3670 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3671 if (ptr1)
3672 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3673 }
3674
Willy Tarreau7d562212016-11-25 16:10:05 +01003675 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02003676 if (!ptr2) {
3677 /* fallback on the gpc array */
3678 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3679 }
3680
Emeric Brun819fc6f2017-06-13 19:37:32 +02003681 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003682 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01003683
Emeric Brun819fc6f2017-06-13 19:37:32 +02003684 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003685 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003686 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003687 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003688 }
3689
3690 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003691 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003692
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003693 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003694
3695 /* If data was modified, we need to touch to re-schedule sync */
3696 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3697 }
3698 else if (stkctr == &tmpstkctr)
3699 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003700 }
3701 return 1;
3702}
3703
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003704/* Increment the General Purpose Counter 1 value from the stream's tracked
3705 * frontend counters and return it into temp integer.
3706 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
3707 */
3708static int
3709smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3710{
3711 struct stkctr tmpstkctr;
3712 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003713 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003714
3715 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3716 if (!stkctr)
3717 return 0;
3718
3719 smp->flags = SMP_F_VOL_TEST;
3720 smp->data.type = SMP_T_SINT;
3721 smp->data.u.sint = 0;
3722
3723 if (!stkctr_entry(stkctr))
3724 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3725
3726 if (stkctr && stkctr_entry(stkctr)) {
3727 void *ptr1,*ptr2;
3728
3729
3730 /* First, update gpc1_rate if it's tracked. Second, update its
3731 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
3732 */
3733 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003734 if (ptr1) {
3735 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3736 }
3737 else {
3738 /* fallback on the gpc array */
3739 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3740 if (ptr1)
3741 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3742 }
3743
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003744 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02003745 if (!ptr2) {
3746 /* fallback on the gpc array */
3747 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3748 }
3749
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003750 if (ptr1 || ptr2) {
3751 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3752
3753 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003754 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003755 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003756 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003757 }
3758
3759 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003760 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003761
3762 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3763
3764 /* If data was modified, we need to touch to re-schedule sync */
3765 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3766 }
3767 else if (stkctr == &tmpstkctr)
3768 stktable_release(stkctr->table, stkctr_entry(stkctr));
3769 }
3770 return 1;
3771}
3772
Emeric Brun4d7ada82021-06-30 19:04:16 +02003773/* Clear the GPC[args(0)] value from the stream's tracked
3774 * frontend counters and return its previous value into temp integer.
3775 * Supports being called as "sc_clr_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3776 * or "src_clr_gpc(<gpc-idx>[,<table>])" only.
3777 */
3778static int
3779smp_fetch_sc_clr_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3780{
3781 struct stkctr tmpstkctr;
3782 struct stkctr *stkctr;
3783 unsigned int idx;
3784
3785 idx = args[0].data.sint;
3786
3787 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3788 if (!stkctr)
3789 return 0;
3790
3791 smp->flags = SMP_F_VOL_TEST;
3792 smp->data.type = SMP_T_SINT;
3793 smp->data.u.sint = 0;
3794
3795 if (!stkctr_entry(stkctr))
3796 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3797
3798 if (stkctr && stkctr_entry(stkctr)) {
3799 void *ptr;
3800
3801 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3802 if (!ptr) {
3803 if (stkctr == &tmpstkctr)
3804 stktable_release(stkctr->table, stkctr_entry(stkctr));
3805 return 0; /* parameter not stored */
3806 }
3807
3808 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3809
3810 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3811 stktable_data_cast(ptr, std_t_uint) = 0;
3812
3813 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3814
3815 /* If data was modified, we need to touch to re-schedule sync */
3816 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3817 }
3818 return 1;
3819}
3820
Willy Tarreau7d562212016-11-25 16:10:05 +01003821/* Clear the General Purpose Counter 0 value from the stream's tracked
3822 * frontend counters and return its previous value into temp integer.
3823 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
3824 */
3825static int
3826smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3827{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003828 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003829 struct stkctr *stkctr;
3830
Emeric Brun819fc6f2017-06-13 19:37:32 +02003831 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003832 if (!stkctr)
3833 return 0;
3834
3835 smp->flags = SMP_F_VOL_TEST;
3836 smp->data.type = SMP_T_SINT;
3837 smp->data.u.sint = 0;
3838
Emeric Brun819fc6f2017-06-13 19:37:32 +02003839 if (!stkctr_entry(stkctr))
3840 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003841
Emeric Brun819fc6f2017-06-13 19:37:32 +02003842 if (stkctr && stkctr_entry(stkctr)) {
3843 void *ptr;
3844
3845 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3846 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003847 /* fallback on the gpc array */
3848 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3849 }
3850
3851 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003852 if (stkctr == &tmpstkctr)
3853 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003854 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003855 }
3856
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003857 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003858
Emeric Brun0e3457b2021-06-30 17:18:28 +02003859 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3860 stktable_data_cast(ptr, std_t_uint) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003861
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003862 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003863
Willy Tarreau7d562212016-11-25 16:10:05 +01003864 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003865 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01003866 }
3867 return 1;
3868}
3869
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003870/* Clear the General Purpose Counter 1 value from the stream's tracked
3871 * frontend counters and return its previous value into temp integer.
3872 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
3873 */
3874static int
3875smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3876{
3877 struct stkctr tmpstkctr;
3878 struct stkctr *stkctr;
3879
3880 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3881 if (!stkctr)
3882 return 0;
3883
3884 smp->flags = SMP_F_VOL_TEST;
3885 smp->data.type = SMP_T_SINT;
3886 smp->data.u.sint = 0;
3887
3888 if (!stkctr_entry(stkctr))
3889 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3890
3891 if (stkctr && stkctr_entry(stkctr)) {
3892 void *ptr;
3893
3894 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3895 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003896 /* fallback on the gpc array */
3897 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3898 }
3899
3900 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003901 if (stkctr == &tmpstkctr)
3902 stktable_release(stkctr->table, stkctr_entry(stkctr));
3903 return 0; /* parameter not stored */
3904 }
3905
3906 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3907
Emeric Brun0e3457b2021-06-30 17:18:28 +02003908 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3909 stktable_data_cast(ptr, std_t_uint) = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003910
3911 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3912
3913 /* If data was modified, we need to touch to re-schedule sync */
3914 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3915 }
3916 return 1;
3917}
3918
Willy Tarreau7d562212016-11-25 16:10:05 +01003919/* set <smp> to the cumulated number of connections from the stream's tracked
3920 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3921 * "src_conn_cnt" only.
3922 */
3923static int
3924smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3925{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003926 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003927 struct stkctr *stkctr;
3928
Emeric Brun819fc6f2017-06-13 19:37:32 +02003929 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003930 if (!stkctr)
3931 return 0;
3932
3933 smp->flags = SMP_F_VOL_TEST;
3934 smp->data.type = SMP_T_SINT;
3935 smp->data.u.sint = 0;
3936 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003937 void *ptr;
3938
3939 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
3940 if (!ptr) {
3941 if (stkctr == &tmpstkctr)
3942 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003943 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003944 }
3945
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003946 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003947
Emeric Brun0e3457b2021-06-30 17:18:28 +02003948 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003949
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003951
3952 if (stkctr == &tmpstkctr)
3953 stktable_release(stkctr->table, stkctr_entry(stkctr));
3954
3955
Willy Tarreau7d562212016-11-25 16:10:05 +01003956 }
3957 return 1;
3958}
3959
3960/* set <smp> to the connection rate from the stream's tracked frontend
3961 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3962 * only.
3963 */
3964static int
3965smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3966{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003967 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003968 struct stkctr *stkctr;
3969
Emeric Brun819fc6f2017-06-13 19:37:32 +02003970 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003971 if (!stkctr)
3972 return 0;
3973
3974 smp->flags = SMP_F_VOL_TEST;
3975 smp->data.type = SMP_T_SINT;
3976 smp->data.u.sint = 0;
3977 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003978 void *ptr;
3979
3980 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
3981 if (!ptr) {
3982 if (stkctr == &tmpstkctr)
3983 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003984 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003985 }
3986
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003987 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003988
Emeric Brun0e3457b2021-06-30 17:18:28 +02003989 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01003990 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003991
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003992 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003993
3994 if (stkctr == &tmpstkctr)
3995 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003996 }
3997 return 1;
3998}
3999
4000/* set temp integer to the number of connections from the stream's source address
4001 * in the table pointed to by expr, after updating it.
4002 * Accepts exactly 1 argument of type table.
4003 */
4004static int
4005smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4006{
4007 struct connection *conn = objt_conn(smp->sess->origin);
4008 struct stksess *ts;
4009 struct stktable_key *key;
4010 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004011 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004012
4013 if (!conn)
4014 return 0;
4015
Joseph Herlant5662fa42018-11-15 13:43:28 -08004016 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02004017 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01004018 return 0;
4019
4020 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004021 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01004022 if (!key)
4023 return 0;
4024
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004025 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004026
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004027 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01004028 /* entry does not exist and could not be created */
4029 return 0;
4030
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004031 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004032 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01004033 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004034 }
Willy Tarreau7d562212016-11-25 16:10:05 +01004035
4036 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004037
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004038 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004039
Emeric Brun0e3457b2021-06-30 17:18:28 +02004040 smp->data.u.sint = ++stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004041
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004042 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004043
Willy Tarreau7d562212016-11-25 16:10:05 +01004044 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004045
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004046 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004047
4048 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01004049 return 1;
4050}
4051
4052/* set <smp> to the number of concurrent connections from the stream's tracked
4053 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
4054 * "src_conn_cur" only.
4055 */
4056static int
4057smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
4058{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004059 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004060 struct stkctr *stkctr;
4061
Emeric Brun819fc6f2017-06-13 19:37:32 +02004062 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004063 if (!stkctr)
4064 return 0;
4065
4066 smp->flags = SMP_F_VOL_TEST;
4067 smp->data.type = SMP_T_SINT;
4068 smp->data.u.sint = 0;
4069 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004070 void *ptr;
4071
4072 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
4073 if (!ptr) {
4074 if (stkctr == &tmpstkctr)
4075 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004076 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004077 }
4078
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004079 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004080
Emeric Brun0e3457b2021-06-30 17:18:28 +02004081 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004082
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004083 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004084
4085 if (stkctr == &tmpstkctr)
4086 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004087 }
4088 return 1;
4089}
4090
4091/* set <smp> to the cumulated number of streams from the stream's tracked
4092 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
4093 * "src_sess_cnt" only.
4094 */
4095static int
4096smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4097{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004098 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004099 struct stkctr *stkctr;
4100
Emeric Brun819fc6f2017-06-13 19:37:32 +02004101 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004102 if (!stkctr)
4103 return 0;
4104
4105 smp->flags = SMP_F_VOL_TEST;
4106 smp->data.type = SMP_T_SINT;
4107 smp->data.u.sint = 0;
4108 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004109 void *ptr;
4110
4111 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
4112 if (!ptr) {
4113 if (stkctr == &tmpstkctr)
4114 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004115 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004116 }
4117
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004118 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004119
Emeric Brun0e3457b2021-06-30 17:18:28 +02004120 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004121
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004122 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004123
4124 if (stkctr == &tmpstkctr)
4125 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004126 }
4127 return 1;
4128}
4129
4130/* set <smp> to the stream rate from the stream's tracked frontend counters.
4131 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
4132 */
4133static int
4134smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4135{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004136 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004137 struct stkctr *stkctr;
4138
Emeric Brun819fc6f2017-06-13 19:37:32 +02004139 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004140 if (!stkctr)
4141 return 0;
4142
4143 smp->flags = SMP_F_VOL_TEST;
4144 smp->data.type = SMP_T_SINT;
4145 smp->data.u.sint = 0;
4146 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004147 void *ptr;
4148
4149 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
4150 if (!ptr) {
4151 if (stkctr == &tmpstkctr)
4152 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004153 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004154 }
4155
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004156 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004157
Emeric Brun0e3457b2021-06-30 17:18:28 +02004158 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004159 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004160
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004161 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004162
4163 if (stkctr == &tmpstkctr)
4164 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004165 }
4166 return 1;
4167}
4168
4169/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
4170 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
4171 * "src_http_req_cnt" only.
4172 */
4173static int
4174smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4175{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004176 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004177 struct stkctr *stkctr;
4178
Emeric Brun819fc6f2017-06-13 19:37:32 +02004179 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004180 if (!stkctr)
4181 return 0;
4182
4183 smp->flags = SMP_F_VOL_TEST;
4184 smp->data.type = SMP_T_SINT;
4185 smp->data.u.sint = 0;
4186 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004187 void *ptr;
4188
4189 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
4190 if (!ptr) {
4191 if (stkctr == &tmpstkctr)
4192 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004193 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004194 }
4195
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004196 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004197
Emeric Brun0e3457b2021-06-30 17:18:28 +02004198 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004199
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004200 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004201
4202 if (stkctr == &tmpstkctr)
4203 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004204 }
4205 return 1;
4206}
4207
4208/* set <smp> to the HTTP request rate from the stream's tracked frontend
4209 * counters. Supports being called as "sc[0-9]_http_req_rate" or
4210 * "src_http_req_rate" only.
4211 */
4212static int
4213smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4214{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004215 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004216 struct stkctr *stkctr;
4217
Emeric Brun819fc6f2017-06-13 19:37:32 +02004218 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004219 if (!stkctr)
4220 return 0;
4221
4222 smp->flags = SMP_F_VOL_TEST;
4223 smp->data.type = SMP_T_SINT;
4224 smp->data.u.sint = 0;
4225 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004226 void *ptr;
4227
4228 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
4229 if (!ptr) {
4230 if (stkctr == &tmpstkctr)
4231 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004232 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004233 }
4234
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004235 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004236
Emeric Brun0e3457b2021-06-30 17:18:28 +02004237 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004238 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004239
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004240 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004241
4242 if (stkctr == &tmpstkctr)
4243 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004244 }
4245 return 1;
4246}
4247
4248/* set <smp> to the cumulated number of HTTP requests errors from the stream's
4249 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
4250 * "src_http_err_cnt" only.
4251 */
4252static int
4253smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4254{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004255 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004256 struct stkctr *stkctr;
4257
Emeric Brun819fc6f2017-06-13 19:37:32 +02004258 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004259 if (!stkctr)
4260 return 0;
4261
4262 smp->flags = SMP_F_VOL_TEST;
4263 smp->data.type = SMP_T_SINT;
4264 smp->data.u.sint = 0;
4265 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004266 void *ptr;
4267
4268 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
4269 if (!ptr) {
4270 if (stkctr == &tmpstkctr)
4271 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004272 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004273 }
4274
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004275 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004276
Emeric Brun0e3457b2021-06-30 17:18:28 +02004277 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004278
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004279 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004280
4281 if (stkctr == &tmpstkctr)
4282 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004283 }
4284 return 1;
4285}
4286
4287/* set <smp> to the HTTP request error rate from the stream's tracked frontend
4288 * counters. Supports being called as "sc[0-9]_http_err_rate" or
4289 * "src_http_err_rate" only.
4290 */
4291static int
4292smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4293{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004294 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004295 struct stkctr *stkctr;
4296
Emeric Brun819fc6f2017-06-13 19:37:32 +02004297 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004298 if (!stkctr)
4299 return 0;
4300
4301 smp->flags = SMP_F_VOL_TEST;
4302 smp->data.type = SMP_T_SINT;
4303 smp->data.u.sint = 0;
4304 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004305 void *ptr;
4306
4307 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
4308 if (!ptr) {
4309 if (stkctr == &tmpstkctr)
4310 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004311 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004312 }
4313
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004314 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004315
Emeric Brun0e3457b2021-06-30 17:18:28 +02004316 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004317 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004318
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004319 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004320
4321 if (stkctr == &tmpstkctr)
4322 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004323 }
4324 return 1;
4325}
4326
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004327/* set <smp> to the cumulated number of HTTP response failures from the stream's
4328 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
4329 * "src_http_fail_cnt" only.
4330 */
4331static int
4332smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4333{
4334 struct stkctr tmpstkctr;
4335 struct stkctr *stkctr;
4336
4337 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4338 if (!stkctr)
4339 return 0;
4340
4341 smp->flags = SMP_F_VOL_TEST;
4342 smp->data.type = SMP_T_SINT;
4343 smp->data.u.sint = 0;
4344 if (stkctr_entry(stkctr) != NULL) {
4345 void *ptr;
4346
4347 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
4348 if (!ptr) {
4349 if (stkctr == &tmpstkctr)
4350 stktable_release(stkctr->table, stkctr_entry(stkctr));
4351 return 0; /* parameter not stored */
4352 }
4353
4354 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4355
Emeric Brun0e3457b2021-06-30 17:18:28 +02004356 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004357
4358 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4359
4360 if (stkctr == &tmpstkctr)
4361 stktable_release(stkctr->table, stkctr_entry(stkctr));
4362 }
4363 return 1;
4364}
4365
4366/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
4367 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
4368 * "src_http_fail_rate" only.
4369 */
4370static int
4371smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4372{
4373 struct stkctr tmpstkctr;
4374 struct stkctr *stkctr;
4375
4376 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4377 if (!stkctr)
4378 return 0;
4379
4380 smp->flags = SMP_F_VOL_TEST;
4381 smp->data.type = SMP_T_SINT;
4382 smp->data.u.sint = 0;
4383 if (stkctr_entry(stkctr) != NULL) {
4384 void *ptr;
4385
4386 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
4387 if (!ptr) {
4388 if (stkctr == &tmpstkctr)
4389 stktable_release(stkctr->table, stkctr_entry(stkctr));
4390 return 0; /* parameter not stored */
4391 }
4392
4393 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4394
Emeric Brun0e3457b2021-06-30 17:18:28 +02004395 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004396 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
4397
4398 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4399
4400 if (stkctr == &tmpstkctr)
4401 stktable_release(stkctr->table, stkctr_entry(stkctr));
4402 }
4403 return 1;
4404}
4405
Willy Tarreau7d562212016-11-25 16:10:05 +01004406/* set <smp> to the number of kbytes received from clients, as found in the
4407 * stream's tracked frontend counters. Supports being called as
4408 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
4409 */
4410static int
4411smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
4412{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004413 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004414 struct stkctr *stkctr;
4415
Emeric Brun819fc6f2017-06-13 19:37:32 +02004416 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004417 if (!stkctr)
4418 return 0;
4419
4420 smp->flags = SMP_F_VOL_TEST;
4421 smp->data.type = SMP_T_SINT;
4422 smp->data.u.sint = 0;
4423 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004424 void *ptr;
4425
4426 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
4427 if (!ptr) {
4428 if (stkctr == &tmpstkctr)
4429 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004430 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004431 }
4432
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004433 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004434
Emeric Brun0e3457b2021-06-30 17:18:28 +02004435 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004436
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004437 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004438
4439 if (stkctr == &tmpstkctr)
4440 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004441 }
4442 return 1;
4443}
4444
4445/* set <smp> to the data rate received from clients in bytes/s, as found
4446 * in the stream's tracked frontend counters. Supports being called as
4447 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
4448 */
4449static int
4450smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4451{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004452 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004453 struct stkctr *stkctr;
4454
Emeric Brun819fc6f2017-06-13 19:37:32 +02004455 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004456 if (!stkctr)
4457 return 0;
4458
4459 smp->flags = SMP_F_VOL_TEST;
4460 smp->data.type = SMP_T_SINT;
4461 smp->data.u.sint = 0;
4462 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004463 void *ptr;
4464
4465 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
4466 if (!ptr) {
4467 if (stkctr == &tmpstkctr)
4468 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004469 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004470 }
4471
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004472 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004473
Emeric Brun0e3457b2021-06-30 17:18:28 +02004474 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004475 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004476
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004477 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004478
4479 if (stkctr == &tmpstkctr)
4480 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004481 }
4482 return 1;
4483}
4484
4485/* set <smp> to the number of kbytes sent to clients, as found in the
4486 * stream's tracked frontend counters. Supports being called as
4487 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
4488 */
4489static int
4490smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
4491{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004492 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004493 struct stkctr *stkctr;
4494
Emeric Brun819fc6f2017-06-13 19:37:32 +02004495 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004496 if (!stkctr)
4497 return 0;
4498
4499 smp->flags = SMP_F_VOL_TEST;
4500 smp->data.type = SMP_T_SINT;
4501 smp->data.u.sint = 0;
4502 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004503 void *ptr;
4504
4505 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
4506 if (!ptr) {
4507 if (stkctr == &tmpstkctr)
4508 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004509 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004510 }
4511
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004512 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004513
Emeric Brun0e3457b2021-06-30 17:18:28 +02004514 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004515
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004516 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004517
4518 if (stkctr == &tmpstkctr)
4519 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004520 }
4521 return 1;
4522}
4523
4524/* set <smp> to the data rate sent to clients in bytes/s, as found in the
4525 * stream's tracked frontend counters. Supports being called as
4526 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
4527 */
4528static int
4529smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4530{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004531 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004532 struct stkctr *stkctr;
4533
Emeric Brun819fc6f2017-06-13 19:37:32 +02004534 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004535 if (!stkctr)
4536 return 0;
4537
4538 smp->flags = SMP_F_VOL_TEST;
4539 smp->data.type = SMP_T_SINT;
4540 smp->data.u.sint = 0;
4541 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004542 void *ptr;
4543
4544 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
4545 if (!ptr) {
4546 if (stkctr == &tmpstkctr)
4547 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004548 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004549 }
4550
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004551 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004552
Emeric Brun0e3457b2021-06-30 17:18:28 +02004553 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004554 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004555
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004556 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004557
4558 if (stkctr == &tmpstkctr)
4559 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004560 }
4561 return 1;
4562}
4563
4564/* set <smp> to the number of active trackers on the SC entry in the stream's
4565 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
4566 */
4567static int
4568smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
4569{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004570 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004571 struct stkctr *stkctr;
4572
Emeric Brun819fc6f2017-06-13 19:37:32 +02004573 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004574 if (!stkctr)
4575 return 0;
4576
4577 smp->flags = SMP_F_VOL_TEST;
4578 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004579 if (stkctr == &tmpstkctr) {
4580 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
4581 stktable_release(stkctr->table, stkctr_entry(stkctr));
4582 }
4583 else {
4584 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
4585 }
4586
Willy Tarreau7d562212016-11-25 16:10:05 +01004587 return 1;
4588}
4589
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004590
4591/* The functions below are used to manipulate table contents from the CLI.
4592 * There are 3 main actions, "clear", "set" and "show". The code is shared
4593 * between all actions, and the action is encoded in the void *private in
4594 * the appctx as well as in the keyword registration, among one of the
4595 * following values.
4596 */
4597
4598enum {
4599 STK_CLI_ACT_CLR,
4600 STK_CLI_ACT_SET,
4601 STK_CLI_ACT_SHOW,
4602};
4603
Willy Tarreau4596fe22022-05-17 19:07:51 +02004604/* Dump the status of a table to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004605 * read buffer. It returns 0 if the output buffer is full
4606 * and needs to be called again, otherwise non-zero.
4607 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004608static int table_dump_head_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004609 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004610 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004611{
Willy Tarreauc12b3212022-05-27 11:08:15 +02004612 struct stream *s = __sc_strm(appctx_sc(appctx));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004613
4614 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004615 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004616
4617 /* any other information should be dumped here */
4618
William Lallemand07a62f72017-05-24 00:57:40 +02004619 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004620 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
4621
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004622 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004623 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004624
4625 return 1;
4626}
4627
Willy Tarreau4596fe22022-05-17 19:07:51 +02004628/* Dump a table entry to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004629 * read buffer. It returns 0 if the output buffer is full
4630 * and needs to be called again, otherwise non-zero.
4631 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004632static int table_dump_entry_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004633 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004634 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004635{
4636 int dt;
4637
4638 chunk_appendf(msg, "%p:", entry);
4639
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004640 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004641 char addr[INET_ADDRSTRLEN];
4642 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
4643 chunk_appendf(msg, " key=%s", addr);
4644 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004645 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004646 char addr[INET6_ADDRSTRLEN];
4647 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
4648 chunk_appendf(msg, " key=%s", addr);
4649 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004650 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01004651 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004652 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004653 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004654 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004655 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004656 }
4657 else {
4658 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004659 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004660 }
4661
Willy Tarreau16b282f2022-11-29 11:55:18 +01004662 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 +01004663
4664 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
4665 void *ptr;
4666
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004667 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004668 continue;
Emeric Brunc64a2a32021-06-30 18:01:02 +02004669 if (stktable_data_types[dt].is_array) {
4670 char tmp[16] = {};
4671 const char *name_pfx = stktable_data_types[dt].name;
4672 const char *name_sfx = NULL;
4673 unsigned int idx = 0;
4674 int i = 0;
4675
4676 /* split name to show index before first _ of the name
4677 * for example: 'gpc3_rate' if array name is 'gpc_rate'.
4678 */
4679 for (i = 0 ; i < (sizeof(tmp) - 1); i++) {
4680 if (!name_pfx[i])
4681 break;
4682 if (name_pfx[i] == '_') {
4683 name_pfx = &tmp[0];
4684 name_sfx = &stktable_data_types[dt].name[i];
4685 break;
4686 }
4687 tmp[i] = name_pfx[i];
4688 }
4689
4690 ptr = stktable_data_ptr_idx(t, entry, dt, idx);
4691 while (ptr) {
4692 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
4693 chunk_appendf(msg, " %s%u%s(%u)=", name_pfx, idx, name_sfx ? name_sfx : "", t->data_arg[dt].u);
4694 else
4695 chunk_appendf(msg, " %s%u%s=", name_pfx, idx, name_sfx ? name_sfx : "");
4696 switch (stktable_data_types[dt].std_type) {
4697 case STD_T_SINT:
4698 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4699 break;
4700 case STD_T_UINT:
4701 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4702 break;
4703 case STD_T_ULL:
4704 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
4705 break;
4706 case STD_T_FRQP:
4707 chunk_appendf(msg, "%u",
4708 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4709 t->data_arg[dt].u));
4710 break;
4711 }
4712 ptr = stktable_data_ptr_idx(t, entry, dt, ++idx);
4713 }
4714 continue;
4715 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004716 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brun01928ae2021-06-30 16:24:04 +02004717 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004718 else
4719 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
4720
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004721 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004722 switch (stktable_data_types[dt].std_type) {
4723 case STD_T_SINT:
4724 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4725 break;
4726 case STD_T_UINT:
4727 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4728 break;
4729 case STD_T_ULL:
Emeric Brun01928ae2021-06-30 16:24:04 +02004730 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004731 break;
4732 case STD_T_FRQP:
Emeric Brun01928ae2021-06-30 16:24:04 +02004733 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004734 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004735 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004736 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02004737 case STD_T_DICT: {
4738 struct dict_entry *de;
4739 de = stktable_data_cast(ptr, std_t_dict);
4740 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
4741 break;
4742 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004743 }
4744 }
4745 chunk_appendf(msg, "\n");
4746
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004747 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004748 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004749
4750 return 1;
4751}
4752
Willy Tarreau3c69e082022-05-03 11:35:07 +02004753/* appctx context used by the "show table" command */
4754struct show_table_ctx {
4755 void *target; /* table we want to dump, or NULL for all */
4756 struct stktable *t; /* table being currently dumped (first if NULL) */
4757 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
4758 long long value[STKTABLE_FILTER_LEN]; /* value to compare against */
4759 signed char data_type[STKTABLE_FILTER_LEN]; /* type of data to compare, or -1 if none */
4760 signed char data_op[STKTABLE_FILTER_LEN]; /* operator (STD_OP_*) when data_type set */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004761 enum {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004762 STATE_NEXT = 0, /* px points to next table, entry=NULL */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004763 STATE_DUMP, /* px points to curr table, entry is valid, refcount held */
4764 STATE_DONE, /* done dumping */
4765 } state;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004766 char action; /* action on the table : one of STK_CLI_ACT_* */
4767};
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004768
4769/* Processes a single table entry matching a specific key passed in argument.
4770 * returns 0 if wants to be called again, 1 if has ended processing.
4771 */
4772static int table_process_entry_per_key(struct appctx *appctx, char **args)
4773{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004774 struct show_table_ctx *ctx = appctx->svcctx;
4775 struct stktable *t = ctx->target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004776 struct stksess *ts;
4777 uint32_t uint32_key;
4778 unsigned char ip6_key[sizeof(struct in6_addr)];
4779 long long value;
4780 int data_type;
4781 int cur_arg;
4782 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004783 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004784
Willy Tarreau9d008692019-08-09 11:21:01 +02004785 if (!*args[4])
4786 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004787
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004788 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004789 case SMP_T_IPV4:
4790 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02004791 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004792 break;
4793 case SMP_T_IPV6:
Christopher Fauletb7c962b2021-11-15 09:17:25 +01004794 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
4795 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004796 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004797 break;
4798 case SMP_T_SINT:
4799 {
4800 char *endptr;
4801 unsigned long val;
4802 errno = 0;
4803 val = strtoul(args[4], &endptr, 10);
4804 if ((errno == ERANGE && val == ULONG_MAX) ||
4805 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02004806 val > 0xffffffff)
4807 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004808 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02004809 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004810 break;
4811 }
4812 break;
4813 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02004814 static_table_key.key = args[4];
4815 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004816 break;
4817 default:
Willy Tarreau3c69e082022-05-03 11:35:07 +02004818 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004819 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02004820 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 +01004821 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02004822 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 +01004823 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02004824 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 +01004825 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004826 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004827 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004828 }
4829
4830 /* check permissions */
4831 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4832 return 1;
4833
Willy Tarreau3c69e082022-05-03 11:35:07 +02004834 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004835 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004836 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004837 if (!ts)
4838 return 1;
4839 chunk_reset(&trash);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004840 if (!table_dump_head_to_buffer(&trash, appctx, t, t)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004841 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004842 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004843 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004844 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004845 if (!table_dump_entry_to_buffer(&trash, appctx, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004846 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004847 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004848 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004849 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004850 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004851 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004852 break;
4853
4854 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004855 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004856 if (!ts)
4857 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004858
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004859 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004860 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004861 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004862 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004863 break;
4864
4865 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004866 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004867 if (!ts) {
4868 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004869 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004870 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004871 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004872 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
4873 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004874 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004875 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004876 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004877 return 1;
4878 }
4879
4880 data_type = stktable_get_data_type(args[cur_arg] + 5);
4881 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004882 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004883 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004884 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004885 return 1;
4886 }
4887
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004888 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004889 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004890 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004891 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004892 return 1;
4893 }
4894
4895 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004896 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004897 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004898 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004899 return 1;
4900 }
4901
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004902 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004903
4904 switch (stktable_data_types[data_type].std_type) {
4905 case STD_T_SINT:
4906 stktable_data_cast(ptr, std_t_sint) = value;
4907 break;
4908 case STD_T_UINT:
4909 stktable_data_cast(ptr, std_t_uint) = value;
4910 break;
4911 case STD_T_ULL:
4912 stktable_data_cast(ptr, std_t_ull) = value;
4913 break;
4914 case STD_T_FRQP:
4915 /* We set both the current and previous values. That way
4916 * the reported frequency is stable during all the period
4917 * then slowly fades out. This allows external tools to
4918 * push measures without having to update them too often.
4919 */
4920 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004921 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004922 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004923 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004924 using its internal lock */
4925 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004926 frqp->prev_ctr = 0;
4927 frqp->curr_ctr = value;
4928 break;
4929 }
4930 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004931 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004932 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004933 break;
4934
4935 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004936 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004937 }
4938 return 1;
4939}
4940
4941/* Prepares the appctx fields with the data-based filters from the command line.
4942 * Returns 0 if the dump can proceed, 1 if has ended processing.
4943 */
4944static int table_prepare_data_request(struct appctx *appctx, char **args)
4945{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004946 struct show_table_ctx *ctx = appctx->svcctx;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004947 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004948 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004949
Willy Tarreau3c69e082022-05-03 11:35:07 +02004950 if (ctx->action != STK_CLI_ACT_SHOW && ctx->action != STK_CLI_ACT_CLR)
Willy Tarreau9d008692019-08-09 11:21:01 +02004951 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004952
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004953 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
4954 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
4955 break;
4956 /* condition on stored data value */
Willy Tarreau3c69e082022-05-03 11:35:07 +02004957 ctx->data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
4958 if (ctx->data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004959 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004960
Willy Tarreau3c69e082022-05-03 11:35:07 +02004961 if (!((struct stktable *)ctx->target)->data_ofs[ctx->data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004962 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 +01004963
Willy Tarreau3c69e082022-05-03 11:35:07 +02004964 ctx->data_op[i] = get_std_op(args[4+3*i]);
4965 if (ctx->data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004966 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 +01004967
Willy Tarreau3c69e082022-05-03 11:35:07 +02004968 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 +01004969 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
4970 }
4971
4972 if (*args[3+3*i]) {
4973 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 +01004974 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004975
4976 /* OK we're done, all the fields are set */
4977 return 0;
4978}
4979
4980/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004981static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004982{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004983 struct show_table_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004984 int i;
4985
4986 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
Willy Tarreau3c69e082022-05-03 11:35:07 +02004987 ctx->data_type[i] = -1;
4988 ctx->target = NULL;
4989 ctx->entry = NULL;
4990 ctx->action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004991
4992 if (*args[2]) {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004993 ctx->t = ctx->target = stktable_find_by_name(args[2]);
Willy Tarreau3c69e082022-05-03 11:35:07 +02004994 if (!ctx->target)
Willy Tarreau9d008692019-08-09 11:21:01 +02004995 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004996 }
4997 else {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004998 ctx->t = stktables_list;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004999 if (ctx->action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005000 goto err_args;
5001 return 0;
5002 }
5003
5004 if (strcmp(args[3], "key") == 0)
5005 return table_process_entry_per_key(appctx, args);
5006 else if (strncmp(args[3], "data.", 5) == 0)
5007 return table_prepare_data_request(appctx, args);
5008 else if (*args[3])
5009 goto err_args;
5010
5011 return 0;
5012
5013err_args:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005014 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005015 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02005016 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 +01005017 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02005018 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 +01005019 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02005020 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005021 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02005022 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005023 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005024}
5025
5026/* This function is used to deal with table operations (dump or clear depending
5027 * on the action stored in appctx->private). It returns 0 if the output buffer is
5028 * full and it needs to be called again, otherwise non-zero.
5029 */
5030static int cli_io_handler_table(struct appctx *appctx)
5031{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005032 struct show_table_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02005033 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02005034 struct stream *s = __sc_strm(sc);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005035 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005036 int skip_entry;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005037 int show = ctx->action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005038
5039 /*
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005040 * We have 3 possible states in ctx->state :
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005041 * - STATE_NEXT : the proxy pointer points to the next table to
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005042 * dump, the entry pointer is NULL ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005043 * - STATE_DUMP : the proxy pointer points to the current table
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005044 * and the entry pointer points to the next entry to be dumped,
5045 * and the refcount on the next entry is held ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005046 * - STATE_DONE : nothing left to dump, the buffer may contain some
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005047 * data though.
5048 */
Christopher Faulet87633c32023-04-03 18:32:50 +02005049 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02005050 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005051 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005052 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005053 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005054 }
5055 return 1;
5056 }
5057
5058 chunk_reset(&trash);
5059
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005060 while (ctx->state != STATE_DONE) {
5061 switch (ctx->state) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005062 case STATE_NEXT:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005063 if (!ctx->t ||
5064 (ctx->target &&
5065 ctx->t != ctx->target)) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005066 ctx->state = STATE_DONE;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005067 break;
5068 }
5069
Willy Tarreau3c69e082022-05-03 11:35:07 +02005070 if (ctx->t->size) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005071 if (show && !table_dump_head_to_buffer(&trash, appctx, ctx->t, ctx->target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005072 return 0;
5073
Willy Tarreau3c69e082022-05-03 11:35:07 +02005074 if (ctx->target &&
William Lallemand07a62f72017-05-24 00:57:40 +02005075 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005076 /* dump entries only if table explicitly requested */
Willy Tarreau76642222022-10-11 12:02:50 +02005077 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005078 eb = ebmb_first(&ctx->t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005079 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005080 ctx->entry = ebmb_entry(eb, struct stksess, key);
5081 ctx->entry->ref_cnt++;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005082 ctx->state = STATE_DUMP;
Willy Tarreau76642222022-10-11 12:02:50 +02005083 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005084 break;
5085 }
Willy Tarreau76642222022-10-11 12:02:50 +02005086 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005087 }
5088 }
Willy Tarreau3c69e082022-05-03 11:35:07 +02005089 ctx->t = ctx->t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005090 break;
5091
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005092 case STATE_DUMP:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005093 skip_entry = 0;
5094
Willy Tarreau3c69e082022-05-03 11:35:07 +02005095 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005096
Willy Tarreau3c69e082022-05-03 11:35:07 +02005097 if (ctx->data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005098 /* we're filtering on some data contents */
5099 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01005100 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005101 signed char op;
5102 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005103
Emeric Brun819fc6f2017-06-13 19:37:32 +02005104
Willy Tarreau2b64a352020-01-22 17:09:47 +01005105 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005106 if (ctx->data_type[i] == -1)
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005107 break;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005108 dt = ctx->data_type[i];
5109 ptr = stktable_data_ptr(ctx->t,
5110 ctx->entry,
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005111 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005112
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005113 data = 0;
5114 switch (stktable_data_types[dt].std_type) {
5115 case STD_T_SINT:
5116 data = stktable_data_cast(ptr, std_t_sint);
5117 break;
5118 case STD_T_UINT:
5119 data = stktable_data_cast(ptr, std_t_uint);
5120 break;
5121 case STD_T_ULL:
5122 data = stktable_data_cast(ptr, std_t_ull);
5123 break;
5124 case STD_T_FRQP:
5125 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau3c69e082022-05-03 11:35:07 +02005126 ctx->t->data_arg[dt].u);
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005127 break;
5128 }
5129
Willy Tarreau3c69e082022-05-03 11:35:07 +02005130 op = ctx->data_op[i];
5131 value = ctx->value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005132
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005133 /* skip the entry if the data does not match the test and the value */
5134 if ((data < value &&
5135 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
5136 (data == value &&
5137 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
5138 (data > value &&
5139 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
5140 skip_entry = 1;
5141 break;
5142 }
5143 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005144 }
5145
5146 if (show && !skip_entry &&
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005147 !table_dump_entry_to_buffer(&trash, appctx, ctx->t, ctx->entry)) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005148 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005149 return 0;
5150 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005151
Willy Tarreau3c69e082022-05-03 11:35:07 +02005152 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005153
Willy Tarreau76642222022-10-11 12:02:50 +02005154 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005155 ctx->entry->ref_cnt--;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005156
Willy Tarreau3c69e082022-05-03 11:35:07 +02005157 eb = ebmb_next(&ctx->entry->key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005158 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005159 struct stksess *old = ctx->entry;
5160 ctx->entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005161 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005162 __stksess_kill_if_expired(ctx->t, old);
5163 else if (!skip_entry && !ctx->entry->ref_cnt)
5164 __stksess_kill(ctx->t, old);
5165 ctx->entry->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02005166 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005167 break;
5168 }
5169
5170
5171 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005172 __stksess_kill_if_expired(ctx->t, ctx->entry);
5173 else if (!skip_entry && !ctx->entry->ref_cnt)
5174 __stksess_kill(ctx->t, ctx->entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005175
Willy Tarreau76642222022-10-11 12:02:50 +02005176 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005177
Willy Tarreau3c69e082022-05-03 11:35:07 +02005178 ctx->t = ctx->t->next;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005179 ctx->state = STATE_NEXT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005180 break;
5181
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005182 default:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005183 break;
5184 }
5185 }
5186 return 1;
5187}
5188
5189static void cli_release_show_table(struct appctx *appctx)
5190{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005191 struct show_table_ctx *ctx = appctx->svcctx;
5192
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005193 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005194 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005195 }
5196}
5197
Willy Tarreau6c011712023-01-06 16:09:58 +01005198static int stk_parse_stick_counters(char **args, int section_type, struct proxy *curpx,
5199 const struct proxy *defpx, const char *file, int line,
5200 char **err)
5201{
5202 char *error;
5203 int counters;
5204
5205 counters = strtol(args[1], &error, 10);
5206 if (*error != 0) {
5207 memprintf(err, "%s: '%s' is an invalid number", args[0], args[1]);
5208 return -1;
5209 }
5210
5211 if (counters < 0) {
5212 memprintf(err, "%s: the number of stick-counters may not be negative (was %d)", args[0], counters);
5213 return -1;
5214 }
5215
5216 global.tune.nb_stk_ctr = counters;
5217 return 0;
5218}
5219
5220/* This function creates the stk_ctr pools after the configuration parsing. It
5221 * returns 0 on success otherwise ERR_*. If nb_stk_ctr is 0, the pool remains
5222 * NULL.
5223 */
5224static int stkt_create_stk_ctr_pool(void)
5225{
5226 if (!global.tune.nb_stk_ctr)
5227 return 0;
5228
5229 pool_head_stk_ctr = create_pool("stk_ctr", sizeof(*((struct session*)0)->stkctr) * global.tune.nb_stk_ctr, MEM_F_SHARED);
5230 if (!pool_head_stk_ctr) {
5231 ha_alert("out of memory while creating the stick-counters pool.\n");
5232 return ERR_ABORT;
5233 }
5234 return 0;
5235}
5236
Willy Tarreau478331d2020-08-28 11:31:31 +02005237static void stkt_late_init(void)
5238{
5239 struct sample_fetch *f;
5240
5241 f = find_sample_fetch("src", strlen("src"));
5242 if (f)
5243 smp_fetch_src = f->process;
Willy Tarreau6c011712023-01-06 16:09:58 +01005244 hap_register_post_check(stkt_create_stk_ctr_pool);
Willy Tarreau478331d2020-08-28 11:31:31 +02005245}
5246
5247INITCALL0(STG_INIT, stkt_late_init);
5248
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005249/* register cli keywords */
5250static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005251 { { "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 },
5252 { { "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 },
5253 { { "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 +01005254 {{},}
5255}};
5256
Willy Tarreau0108d902018-11-25 19:14:37 +01005257INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005258
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005259static struct action_kw_list tcp_conn_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005260 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005261 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5262 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5263 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005264 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5265 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005266 { /* END */ }
5267}};
5268
Willy Tarreau0108d902018-11-25 19:14:37 +01005269INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
5270
Willy Tarreau620408f2016-10-21 16:37:51 +02005271static struct action_kw_list tcp_sess_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005272 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005273 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5274 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5275 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005276 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5277 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02005278 { /* END */ }
5279}};
5280
Willy Tarreau0108d902018-11-25 19:14:37 +01005281INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
5282
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005283static struct action_kw_list tcp_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005284 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005285 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5286 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5287 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005288 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5289 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005290 { /* END */ }
5291}};
5292
Willy Tarreau0108d902018-11-25 19:14:37 +01005293INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
5294
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005295static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005296 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005297 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5298 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5299 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005300 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5301 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005302 { /* END */ }
5303}};
5304
Willy Tarreau0108d902018-11-25 19:14:37 +01005305INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
5306
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005307static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005308 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005309 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5310 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5311 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005312 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5313 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005314 { /* END */ }
5315}};
5316
Willy Tarreau0108d902018-11-25 19:14:37 +01005317INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
5318
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005319static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005320 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005321 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5322 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5323 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005324 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5325 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005326 { /* END */ }
5327}};
5328
Willy Tarreau0108d902018-11-25 19:14:37 +01005329INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
5330
Christopher Fauleta9248042023-01-05 11:17:38 +01005331static struct action_kw_list http_after_res_kws = { { }, {
Aurelien DARRAGONe2907c72023-03-17 11:28:58 +01005332 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Christopher Fauleta9248042023-01-05 11:17:38 +01005333 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5334 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5335 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
5336 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5337 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
5338 { /* END */ }
5339}};
5340
5341INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
5342
Willy Tarreau7d562212016-11-25 16:10:05 +01005343/* Note: must not be declared <const> as its list will be overwritten.
5344 * Please take care of keeping this list alphabetically sorted.
5345 */
5346static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
5347 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5348 { "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 +02005349 { "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 +01005350 { "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 +01005351 { "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 +01005352 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5353 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5354 { "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 +02005355 { "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 +01005356 { "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 +02005357 { "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 +01005358 { "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 +01005359 { "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 +02005360 { "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 +01005361 { "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 +01005362 { "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 +01005363 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5364 { "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 +01005365 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5366 { "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 +01005367 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5368 { "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 +02005369 { "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 +01005370 { "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 +01005371 { "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 +01005372 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5373 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5374 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5375 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5376 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5377 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5378 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5379 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5380 { "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 +01005381 { "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 +01005382 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5383 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5384 { "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 +01005385 { "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 +01005386 { "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 +01005387 { "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 +01005388 { "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 +01005389 { "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 +01005390 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5391 { "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 +01005392 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5393 { "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 +01005394 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5395 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5396 { "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 +01005397 { "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 +01005398 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5399 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5400 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5401 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5402 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5403 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5404 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5405 { "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 +02005406 { "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 +01005407 { "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 +01005408 { "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 +01005409 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5410 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5411 { "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 +01005412 { "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 +01005413 { "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 +01005414 { "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 +01005415 { "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 +01005416 { "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 +01005417 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5418 { "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 +01005419 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5420 { "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 +01005421 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5422 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5423 { "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 +01005424 { "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 +01005425 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5426 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5427 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5428 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5429 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5430 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5431 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5432 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5433 { "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 +01005434 { "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 +01005435 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5436 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5437 { "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 +01005438 { "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 +01005439 { "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 +01005440 { "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 +01005441 { "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 +01005442 { "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 +01005443 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5444 { "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 +01005445 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5446 { "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 +01005447 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5448 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5449 { "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 +01005450 { "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 +01005451 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5452 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5453 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5454 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5455 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5456 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5457 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5458 { "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 +02005459 { "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 +01005460 { "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 +01005461 { "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 +01005462 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5463 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5464 { "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 +02005465 { "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 +01005466 { "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 +02005467 { "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 +01005468 { "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 +01005469 { "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 +02005470 { "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 +01005471 { "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 +01005472 { "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 +01005473 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5474 { "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 +01005475 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5476 { "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 +01005477 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5478 { "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 +02005479 { "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 +01005480 { "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 +01005481 { "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 +01005482 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5483 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5484 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5485 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5486 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5487 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5488 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5489 { /* END */ },
5490}};
5491
Willy Tarreau0108d902018-11-25 19:14:37 +01005492INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01005493
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005494/* Note: must not be declared <const> as its list will be overwritten */
5495static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02005496 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
5497 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5498 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5499 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5500 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5501 { "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 +02005502 { "table_expire", sample_conv_table_expire, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun877b0b52021-06-30 18:57:49 +02005503 { "table_gpt", sample_conv_table_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005504 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005505 { "table_gpc", sample_conv_table_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005506 { "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 +01005507 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005508 { "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 +02005509 { "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 +01005510 { "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 +02005511 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5512 { "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 +01005513 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5514 { "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 +02005515 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5516 { "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 +02005517 { "table_idle", sample_conv_table_idle, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005518 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5519 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5520 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5521 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5522 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5523 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005524 { /* END */ },
5525}};
5526
Willy Tarreau0108d902018-11-25 19:14:37 +01005527INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau6c011712023-01-06 16:09:58 +01005528
5529static struct cfg_kw_list cfg_kws = {{ },{
5530 { CFG_GLOBAL, "tune.stick-counters", stk_parse_stick_counters },
5531 { /* END */ }
5532}};
5533
5534INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);