blob: 363269f01a3f84264f54a754eefce6c153758bcc [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) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002659 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Emeric Brun877b0b52021-06-30 18:57:49 +02002660 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2661 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2662 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2663 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2664 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2665 default:
2666 send_log(px, LOG_ERR, "stick table: internal error while setting gpt%u.", rule->arg.gpt.idx);
2667 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2668 ha_alert("stick table: internal error while executing setting gpt%u.\n", rule->arg.gpt.idx);
2669 return ACT_RET_CONT;
2670 }
2671
2672 /* Fetch and cast the expression. */
2673 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2674 if (!smp) {
2675 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt%u.", rule->arg.gpt.idx);
2676 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2677 ha_alert("stick table: invalid expression or data type while setting gpt%u.\n", rule->arg.gpt.idx);
2678 return ACT_RET_CONT;
2679 }
2680 value = (unsigned int)(smp->data.u.sint);
2681 }
2682
2683 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2684
2685 stktable_data_cast(ptr, std_t_uint) = value;
2686
2687 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2688
2689 stktable_touch_local(stkctr->table, ts, 0);
2690 }
2691
2692 return ACT_RET_CONT;
2693}
2694
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002695/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002696static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002697 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002698{
2699 void *ptr;
2700 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002701 struct stkctr *stkctr = NULL;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002702 unsigned int value = 0;
2703 struct sample *smp;
2704 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002705
2706 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002707 if (s && s->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002708 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002709 else if (sess->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002710 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002711 else
2712 return ACT_RET_CONT;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002713
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002714 ts = stkctr_entry(stkctr);
2715 if (!ts)
2716 return ACT_RET_CONT;
2717
2718 /* Store the sample in the required sc, and ignore errors. */
2719 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02002720 if (!ptr)
2721 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, 0);
2722
Willy Tarreau79c1e912016-01-25 14:54:45 +01002723 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002724 if (!rule->arg.gpt.expr)
2725 value = (unsigned int)(rule->arg.gpt.value);
2726 else {
2727 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002728 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002729 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2730 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2731 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2732 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2733 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2734 default:
2735 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2736 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2737 ha_alert("stick table: internal error while executing setting gpt0.\n");
2738 return ACT_RET_CONT;
2739 }
2740
2741 /* Fetch and cast the expression. */
2742 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2743 if (!smp) {
2744 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2745 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2746 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2747 return ACT_RET_CONT;
2748 }
2749 value = (unsigned int)(smp->data.u.sint);
2750 }
2751
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002752 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002753
Emeric Brun0e3457b2021-06-30 17:18:28 +02002754 stktable_data_cast(ptr, std_t_uint) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002755
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002756 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002757
2758 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002759 }
2760
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002761 return ACT_RET_CONT;
2762}
2763
Emeric Brun877b0b52021-06-30 18:57:49 +02002764/* This function is a parser for the "sc-set-gpt" and "sc-set-gpt0" actions.
2765 * It understands the formats:
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002766 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002767 * sc-set-gpt(<gpt IDX>,<track ID>) <expression>
2768 * sc-set-gpt0(<track ID>) <expression>
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002769 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002770 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2771 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpt.expr'
2772 * is filled with the pointer to the expression to execute or NULL if the arg
2773 * is directly an integer stored into 'rule->arg.gpt.value'.
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002774 */
Emeric Brun877b0b52021-06-30 18:57:49 +02002775static enum act_parse_ret parse_set_gpt(const char **args, int *arg, struct proxy *px,
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002776 struct act_rule *rule, char **err)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002777{
2778 const char *cmd_name = args[*arg-1];
2779 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002780 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002781
Willy Tarreau6c011712023-01-06 16:09:58 +01002782 if (!global.tune.nb_stk_ctr) {
2783 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2784 return ACT_RET_PRS_ERR;
2785 }
2786
Emeric Brun877b0b52021-06-30 18:57:49 +02002787 cmd_name += strlen("sc-set-gpt");
2788 if (*cmd_name == '(') {
2789 cmd_name++; /* skip the '(' */
2790 rule->arg.gpt.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2791 if (*error != ',') {
2792 memprintf(err, "Missing gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002793 return ACT_RET_PRS_ERR;
2794 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002795 else {
2796 cmd_name = error + 1; /* skip the ',' */
2797 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2798 if (*error != ')') {
2799 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2800 return ACT_RET_PRS_ERR;
2801 }
2802
Willy Tarreau6c011712023-01-06 16:09:58 +01002803 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002804 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002805 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002806 return ACT_RET_PRS_ERR;
2807 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002808 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002809 rule->action_ptr = action_set_gpt;
2810 }
2811 else if (*cmd_name == '0') {
2812 cmd_name++;
2813 if (*cmd_name == '\0') {
2814 /* default stick table id. */
2815 rule->arg.gpt.sc = 0;
2816 } else {
2817 /* parse the stick table id. */
2818 if (*cmd_name != '(') {
2819 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2820 return ACT_RET_PRS_ERR;
2821 }
2822 cmd_name++; /* jump the '(' */
2823 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2824 if (*error != ')') {
2825 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2826 return ACT_RET_PRS_ERR;
2827 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002828
Willy Tarreau6c011712023-01-06 16:09:58 +01002829 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002830 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002831 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002832 return ACT_RET_PRS_ERR;
2833 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002834 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002835 rule->action_ptr = action_set_gpt0;
2836 }
2837 else {
2838 /* default stick table id. */
2839 memprintf(err, "invalid gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2840 return ACT_RET_PRS_ERR;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002841 }
2842
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002843 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002844 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002845 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002846 if (*error == '\0') {
2847 /* valid integer, skip it */
2848 (*arg)++;
2849 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002850 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002851 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002852 if (!rule->arg.gpt.expr)
2853 return ACT_RET_PRS_ERR;
2854
2855 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002856 case ACT_F_TCP_REQ_CON: smp_val = SMP_VAL_FE_CON_ACC; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002857 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2858 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2859 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2860 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2861 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2862 default:
2863 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2864 return ACT_RET_PRS_ERR;
2865 }
2866 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2867 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2868 sample_src_names(rule->arg.gpt.expr->fetch->use));
2869 free(rule->arg.gpt.expr);
2870 return ACT_RET_PRS_ERR;
2871 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002872 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002873
Thierry FOURNIER42148732015-09-02 17:17:33 +02002874 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002875
2876 return ACT_RET_PRS_OK;
2877}
2878
Willy Tarreau5a72d032023-01-02 18:15:20 +01002879/* This function updates the gpc at index 'rule->arg.gpc.idx' of the array on
2880 * the tracksc counter of index 'rule->arg.gpc.sc' stored into the <stream> or
2881 * directly in the session <sess> if <stream> is set to NULL. This gpc is
2882 * set to the value computed by the expression 'rule->arg.gpc.expr' or if
2883 * 'rule->arg.gpc.expr' is null directly to the value of 'rule->arg.gpc.value'.
2884 *
2885 * This function always returns ACT_RET_CONT and parameter flags is unused.
2886 */
2887static enum act_return action_add_gpc(struct act_rule *rule, struct proxy *px,
2888 struct session *sess, struct stream *s, int flags)
2889{
2890 void *ptr1, *ptr2;
2891 struct stksess *ts;
2892 struct stkctr *stkctr;
2893 unsigned int value = 0;
2894 struct sample *smp;
2895 int smp_opt_dir;
2896
2897 /* Extract the stksess, return OK if no stksess available. */
2898 if (s)
2899 stkctr = &s->stkctr[rule->arg.gpc.sc];
2900 else
2901 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2902
2903 ts = stkctr_entry(stkctr);
2904 if (!ts)
2905 return ACT_RET_CONT;
2906
2907 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2908 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2909 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2910
2911 if (ptr1 || ptr2) {
2912 if (!rule->arg.gpc.expr)
2913 value = (unsigned int)(rule->arg.gpc.value);
2914 else {
2915 switch (rule->from) {
2916 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2917 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2918 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2919 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2920 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2921 default:
2922 send_log(px, LOG_ERR, "stick table: internal error while setting gpc%u.", rule->arg.gpc.idx);
2923 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2924 ha_alert("stick table: internal error while executing setting gpc%u.\n", rule->arg.gpc.idx);
2925 return ACT_RET_CONT;
2926 }
2927
2928 /* Fetch and cast the expression. */
2929 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpc.expr, SMP_T_SINT);
2930 if (!smp) {
2931 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpc%u.", rule->arg.gpc.idx);
2932 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2933 ha_alert("stick table: invalid expression or data type while setting gpc%u.\n", rule->arg.gpc.idx);
2934 return ACT_RET_CONT;
2935 }
2936 value = (unsigned int)(smp->data.u.sint);
2937 }
2938
2939 if (value) {
2940 /* only update the value if non-null increment */
2941 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2942
2943 if (ptr1)
2944 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
2945 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, value);
2946
2947 if (ptr2)
2948 stktable_data_cast(ptr2, std_t_uint) += value;
2949
2950 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2951 }
2952 /* always touch the table so that it doesn't expire */
2953 stktable_touch_local(stkctr->table, ts, 0);
2954 }
2955
2956 return ACT_RET_CONT;
2957}
2958
2959/* This function is a parser for the "sc-add-gpc" action. It understands the
2960 * format:
2961 *
2962 * sc-add-gpc(<gpc IDX>,<track ID>) <expression>
2963 *
2964 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2965 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpc.expr'
2966 * is filled with the pointer to the expression to execute or NULL if the arg
2967 * is directly an integer stored into 'rule->arg.gpt.value'.
2968 */
2969static enum act_parse_ret parse_add_gpc(const char **args, int *arg, struct proxy *px,
2970 struct act_rule *rule, char **err)
2971{
2972 const char *cmd_name = args[*arg-1];
2973 char *error;
2974 int smp_val;
2975
2976 cmd_name += strlen("sc-add-gpc");
2977 if (*cmd_name != '(') {
2978 memprintf(err, "Missing or invalid arguments for '%s'. Expects sc-add-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2979 return ACT_RET_PRS_ERR;
2980 }
2981 cmd_name++; /* skip the '(' */
2982 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2983 if (*error != ',') {
2984 memprintf(err, "Missing gpc ID. Expects %s(<GPC ID>,<Track ID>)", args[*arg-1]);
2985 return ACT_RET_PRS_ERR;
2986 }
2987 else {
2988 cmd_name = error + 1; /* skip the ',' */
2989 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2990 if (*error != ')') {
2991 memprintf(err, "invalid stick table track ID '%s'. Expects %s(<GPC ID>,<Track ID>)", cmd_name, args[*arg-1]);
2992 return ACT_RET_PRS_ERR;
2993 }
2994
2995 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
2996 memprintf(err, "invalid stick table track ID '%s' for '%s'. The max allowed ID is %d",
2997 cmd_name, args[*arg-1], MAX_SESS_STKCTR-1);
2998 return ACT_RET_PRS_ERR;
2999 }
3000 }
3001 rule->action_ptr = action_add_gpc;
3002
3003 /* value may be either an integer or an expression */
3004 rule->arg.gpc.expr = NULL;
3005 rule->arg.gpc.value = strtol(args[*arg], &error, 10);
3006 if (*error == '\0') {
3007 /* valid integer, skip it */
3008 (*arg)++;
3009 } else {
3010 rule->arg.gpc.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
3011 px->conf.args.line, err, &px->conf.args, NULL);
3012 if (!rule->arg.gpc.expr)
3013 return ACT_RET_PRS_ERR;
3014
3015 switch (rule->from) {
3016 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
3017 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
3018 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
3019 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
3020 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
3021 default:
3022 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
3023 return ACT_RET_PRS_ERR;
3024 }
3025
3026 if (!(rule->arg.gpc.expr->fetch->val & smp_val)) {
3027 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
3028 sample_src_names(rule->arg.gpc.expr->fetch->use));
3029 free(rule->arg.gpc.expr);
3030 return ACT_RET_PRS_ERR;
3031 }
3032 }
3033
3034 rule->action = ACT_CUSTOM;
3035
3036 return ACT_RET_PRS_OK;
3037}
3038
Willy Tarreau7d562212016-11-25 16:10:05 +01003039/* set temp integer to the number of used entries in the table pointed to by expr.
3040 * Accepts exactly 1 argument of type table.
3041 */
3042static int
3043smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3044{
3045 smp->flags = SMP_F_VOL_TEST;
3046 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003047 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003048 return 1;
3049}
3050
3051/* set temp integer to the number of free entries in the table pointed to by expr.
3052 * Accepts exactly 1 argument of type table.
3053 */
3054static int
3055smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
3056{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003057 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003058
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003059 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003060 smp->flags = SMP_F_VOL_TEST;
3061 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003062 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003063 return 1;
3064}
3065
3066/* Returns a pointer to a stkctr depending on the fetch keyword name.
3067 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
3068 * sc[0-9]_* will return a pointer to the respective field in the
3069 * stream <l4>. sc_* requires an UINT argument specifying the stick
3070 * counter number. src_* will fill a locally allocated structure with
3071 * the table and entry corresponding to what is specified with src_*.
3072 * NULL may be returned if the designated stkctr is not tracked. For
3073 * the sc_* and sc[0-9]_* forms, an optional table argument may be
3074 * passed. When present, the currently tracked key is then looked up
3075 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003076 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01003077 * multiple tables). <strm> is allowed to be NULL, in which case only
3078 * the session will be consulted.
3079 */
3080struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003081smp_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 +01003082{
Willy Tarreau7d562212016-11-25 16:10:05 +01003083 struct stkctr *stkptr;
3084 struct stksess *stksess;
3085 unsigned int num = kw[2] - '0';
3086 int arg = 0;
3087
3088 if (num == '_' - '0') {
3089 /* sc_* variant, args[0] = ctr# (mandatory) */
3090 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01003091 }
3092 else if (num > 9) { /* src_* variant, args[0] = table */
3093 struct stktable_key *key;
3094 struct connection *conn = objt_conn(sess->origin);
3095 struct sample smp;
3096
3097 if (!conn)
3098 return NULL;
3099
Joseph Herlant5662fa42018-11-15 13:43:28 -08003100 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003101 smp.px = NULL;
3102 smp.sess = sess;
3103 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003104 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003105 return NULL;
3106
3107 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003108 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003109 if (!key)
3110 return NULL;
3111
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003112 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003113 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
3114 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003115 }
3116
3117 /* Here, <num> contains the counter number from 0 to 9 for
3118 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
3119 * args[arg] is the first optional argument. We first lookup the
3120 * ctr form the stream, then from the session if it was not there.
Willy Tarreau6c011712023-01-06 16:09:58 +01003121 * But we must be sure the counter does not exceed global.tune.nb_stk_ctr.
Willy Tarreau7d562212016-11-25 16:10:05 +01003122 */
Willy Tarreau6c011712023-01-06 16:09:58 +01003123 if (num >= global.tune.nb_stk_ctr)
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02003124 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003125
Willy Tarreau6c011712023-01-06 16:09:58 +01003126 stkptr = NULL;
3127 if (strm && strm->stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003128 stkptr = &strm->stkctr[num];
Willy Tarreau6c011712023-01-06 16:09:58 +01003129 if (!strm || !stkptr || !stkctr_entry(stkptr)) {
3130 if (sess->stkctr)
3131 stkptr = &sess->stkctr[num];
3132 else
3133 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003134 if (!stkctr_entry(stkptr))
3135 return NULL;
3136 }
3137
3138 stksess = stkctr_entry(stkptr);
3139 if (!stksess)
3140 return NULL;
3141
3142 if (unlikely(args[arg].type == ARGT_TAB)) {
3143 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003144 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003145 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
3146 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003147 }
3148 return stkptr;
3149}
3150
3151/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
3152 * the entry if it doesn't exist yet. This is needed for a few fetch
3153 * functions which need to create an entry, such as src_inc_gpc* and
3154 * src_clr_gpc*.
3155 */
3156struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003157smp_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 +01003158{
Willy Tarreau7d562212016-11-25 16:10:05 +01003159 struct stktable_key *key;
3160 struct connection *conn = objt_conn(sess->origin);
3161 struct sample smp;
3162
3163 if (strncmp(kw, "src_", 4) != 0)
3164 return NULL;
3165
3166 if (!conn)
3167 return NULL;
3168
Joseph Herlant5662fa42018-11-15 13:43:28 -08003169 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003170 smp.px = NULL;
3171 smp.sess = sess;
3172 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003173 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003174 return NULL;
3175
3176 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003177 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003178 if (!key)
3179 return NULL;
3180
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003181 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003182 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
3183 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003184}
3185
3186/* set return a boolean indicating if the requested stream counter is
3187 * currently being tracked or not.
3188 * Supports being called as "sc[0-9]_tracked" only.
3189 */
3190static int
3191smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
3192{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003193 struct stkctr tmpstkctr;
3194 struct stkctr *stkctr;
3195
Willy Tarreau7d562212016-11-25 16:10:05 +01003196 smp->flags = SMP_F_VOL_TEST;
3197 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003198 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3199 smp->data.u.sint = !!stkctr;
3200
3201 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02003202 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02003203 stktable_release(stkctr->table, stkctr_entry(stkctr));
3204
Emeric Brun877b0b52021-06-30 18:57:49 +02003205 return 1;
3206}
3207
3208/* set <smp> to the General Purpose Tag of index set as first arg
3209 * to value from the stream's tracked frontend counters or from the src.
3210 * Supports being called as "sc_get_gpt(<gpt-idx>,<sc-idx>[,<table>])" or
3211 * "src_get_gpt(<gpt-idx>[,<table>])" only. Value zero is returned if
3212 * the key is new or gpt is not stored.
3213 */
3214static int
3215smp_fetch_sc_get_gpt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3216{
3217 struct stkctr tmpstkctr;
3218 struct stkctr *stkctr;
3219 unsigned int idx;
3220
3221 idx = args[0].data.sint;
3222
3223 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3224 if (!stkctr)
3225 return 0;
3226
3227 smp->flags = SMP_F_VOL_TEST;
3228 smp->data.type = SMP_T_SINT;
3229 smp->data.u.sint = 0;
3230
3231 if (stkctr_entry(stkctr)) {
3232 void *ptr;
3233
3234 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, idx);
3235 if (!ptr) {
3236 if (stkctr == &tmpstkctr)
3237 stktable_release(stkctr->table, stkctr_entry(stkctr));
3238 return 0; /* parameter not stored */
3239 }
3240
3241 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3242
3243 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3244
3245 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3246
3247 if (stkctr == &tmpstkctr)
3248 stktable_release(stkctr->table, stkctr_entry(stkctr));
3249 }
Willy Tarreau7d562212016-11-25 16:10:05 +01003250 return 1;
3251}
3252
3253/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
3254 * frontend counters or from the src.
3255 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
3256 * zero is returned if the key is new.
3257 */
3258static int
3259smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3260{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003261 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003262 struct stkctr *stkctr;
3263
Emeric Brun819fc6f2017-06-13 19:37:32 +02003264 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003265 if (!stkctr)
3266 return 0;
3267
3268 smp->flags = SMP_F_VOL_TEST;
3269 smp->data.type = SMP_T_SINT;
3270 smp->data.u.sint = 0;
3271
Emeric Brun819fc6f2017-06-13 19:37:32 +02003272 if (stkctr_entry(stkctr)) {
3273 void *ptr;
3274
3275 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02003276 if (!ptr)
3277 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, 0);
3278
Emeric Brun4d7ada82021-06-30 19:04:16 +02003279 if (!ptr) {
3280 if (stkctr == &tmpstkctr)
3281 stktable_release(stkctr->table, stkctr_entry(stkctr));
3282 return 0; /* parameter not stored */
3283 }
3284
3285 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3286
3287 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3288
3289 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3290
3291 if (stkctr == &tmpstkctr)
3292 stktable_release(stkctr->table, stkctr_entry(stkctr));
3293 }
3294 return 1;
3295}
3296
3297/* set <smp> to the GPC[args(0)]'s value from the stream's tracked
3298 * frontend counters or from the src.
3299 * Supports being called as "sc_get_gpc(<gpc-idx>,<sc-idx>[,<table>])" or
3300 * "src_get_gpc(<gpc-idx>[,<table>])" only. Value
3301 * Value zero is returned if the key is new or gpc is not stored.
3302 */
3303static int
3304smp_fetch_sc_get_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3305{
3306 struct stkctr tmpstkctr;
3307 struct stkctr *stkctr;
3308 unsigned int idx;
3309
3310 idx = args[0].data.sint;
3311
3312 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3313 if (!stkctr)
3314 return 0;
3315
3316 smp->flags = SMP_F_VOL_TEST;
3317 smp->data.type = SMP_T_SINT;
3318 smp->data.u.sint = 0;
3319
3320 if (stkctr_entry(stkctr) != NULL) {
3321 void *ptr;
3322
3323 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003324 if (!ptr) {
3325 if (stkctr == &tmpstkctr)
3326 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003327 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003328 }
3329
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003330 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003331
Emeric Brun0e3457b2021-06-30 17:18:28 +02003332 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003333
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003334 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003335
3336 if (stkctr == &tmpstkctr)
3337 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003338 }
3339 return 1;
3340}
3341
3342/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
3343 * frontend counters or from the src.
3344 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
3345 * zero is returned if the key is new.
3346 */
3347static int
3348smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3349{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003350 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003351 struct stkctr *stkctr;
3352
Emeric Brun819fc6f2017-06-13 19:37:32 +02003353 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003354 if (!stkctr)
3355 return 0;
3356
3357 smp->flags = SMP_F_VOL_TEST;
3358 smp->data.type = SMP_T_SINT;
3359 smp->data.u.sint = 0;
3360
3361 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003362 void *ptr;
3363
3364 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3365 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003366 /* fallback on the gpc array */
3367 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3368 }
3369
3370 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003371 if (stkctr == &tmpstkctr)
3372 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003373 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003374 }
3375
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003376 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003377
Emeric Brun0e3457b2021-06-30 17:18:28 +02003378 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003379
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003380 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003381
3382 if (stkctr == &tmpstkctr)
3383 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003384 }
3385 return 1;
3386}
3387
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003388/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
3389 * frontend counters or from the src.
3390 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
3391 * zero is returned if the key is new.
3392 */
3393static int
3394smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3395{
3396 struct stkctr tmpstkctr;
3397 struct stkctr *stkctr;
3398
3399 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3400 if (!stkctr)
3401 return 0;
3402
3403 smp->flags = SMP_F_VOL_TEST;
3404 smp->data.type = SMP_T_SINT;
3405 smp->data.u.sint = 0;
3406
3407 if (stkctr_entry(stkctr) != NULL) {
3408 void *ptr;
3409
3410 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3411 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003412 /* fallback on the gpc array */
3413 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3414 }
3415
3416 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003417 if (stkctr == &tmpstkctr)
3418 stktable_release(stkctr->table, stkctr_entry(stkctr));
3419 return 0; /* parameter not stored */
3420 }
3421
3422 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3423
Emeric Brun0e3457b2021-06-30 17:18:28 +02003424 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003425
3426 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3427
3428 if (stkctr == &tmpstkctr)
3429 stktable_release(stkctr->table, stkctr_entry(stkctr));
3430 }
3431 return 1;
3432}
3433
Emeric Brun4d7ada82021-06-30 19:04:16 +02003434/* set <smp> to the GPC[args(0)]'s event rate from the stream's
3435 * tracked frontend counters or from the src.
3436 * Supports being called as "sc_gpc_rate(<gpc-idx>,<sc-idx>[,<table])"
3437 * or "src_gpc_rate(<gpc-idx>[,<table>])" only.
3438 * Value zero is returned if the key is new or gpc_rate is not stored.
3439 */
3440static int
3441smp_fetch_sc_gpc_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3442{
3443 struct stkctr tmpstkctr;
3444 struct stkctr *stkctr;
3445 unsigned int idx;
3446
3447 idx = args[0].data.sint;
3448
3449 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3450 if (!stkctr)
3451 return 0;
3452
3453 smp->flags = SMP_F_VOL_TEST;
3454 smp->data.type = SMP_T_SINT;
3455 smp->data.u.sint = 0;
3456 if (stkctr_entry(stkctr) != NULL) {
3457 void *ptr;
3458
3459 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3460 if (!ptr) {
3461 if (stkctr == &tmpstkctr)
3462 stktable_release(stkctr->table, stkctr_entry(stkctr));
3463 return 0; /* parameter not stored */
3464 }
3465
3466 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3467
3468 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3469 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u);
3470
3471 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3472
3473 if (stkctr == &tmpstkctr)
3474 stktable_release(stkctr->table, stkctr_entry(stkctr));
3475 }
3476 return 1;
3477}
3478
Willy Tarreau7d562212016-11-25 16:10:05 +01003479/* set <smp> to the General Purpose Counter 0's event rate from the stream's
3480 * tracked frontend counters or from the src.
3481 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
3482 * Value zero is returned if the key is new.
3483 */
3484static int
3485smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3486{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003487 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003488 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003489 unsigned int period;
Willy Tarreau7d562212016-11-25 16:10:05 +01003490
Emeric Brun819fc6f2017-06-13 19:37:32 +02003491 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003492 if (!stkctr)
3493 return 0;
3494
3495 smp->flags = SMP_F_VOL_TEST;
3496 smp->data.type = SMP_T_SINT;
3497 smp->data.u.sint = 0;
3498 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003499 void *ptr;
3500
3501 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003502 if (ptr) {
3503 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3504 }
3505 else {
3506 /* fallback on the gpc array */
3507 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3508 if (ptr)
3509 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3510 }
3511
Emeric Brun819fc6f2017-06-13 19:37:32 +02003512 if (!ptr) {
3513 if (stkctr == &tmpstkctr)
3514 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003515 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003516 }
3517
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003518 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003519
Emeric Brun726783d2021-06-30 19:06:43 +02003520 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003521
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003522 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003523
3524 if (stkctr == &tmpstkctr)
3525 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003526 }
3527 return 1;
3528}
3529
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003530/* set <smp> to the General Purpose Counter 1's event rate from the stream's
3531 * tracked frontend counters or from the src.
3532 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
3533 * Value zero is returned if the key is new.
3534 */
3535static int
3536smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3537{
3538 struct stkctr tmpstkctr;
3539 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003540 unsigned int period;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003541
3542 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3543 if (!stkctr)
3544 return 0;
3545
3546 smp->flags = SMP_F_VOL_TEST;
3547 smp->data.type = SMP_T_SINT;
3548 smp->data.u.sint = 0;
3549 if (stkctr_entry(stkctr) != NULL) {
3550 void *ptr;
3551
3552 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003553 if (ptr) {
3554 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3555 }
3556 else {
3557 /* fallback on the gpc array */
3558 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3559 if (ptr)
3560 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3561 }
3562
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003563 if (!ptr) {
3564 if (stkctr == &tmpstkctr)
3565 stktable_release(stkctr->table, stkctr_entry(stkctr));
3566 return 0; /* parameter not stored */
3567 }
3568
3569 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3570
Emeric Brun726783d2021-06-30 19:06:43 +02003571 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 +01003572
3573 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3574
3575 if (stkctr == &tmpstkctr)
3576 stktable_release(stkctr->table, stkctr_entry(stkctr));
3577 }
3578 return 1;
3579}
3580
Emeric Brun4d7ada82021-06-30 19:04:16 +02003581/* Increment the GPC[args(0)] value from the stream's tracked
3582 * frontend counters and return it into temp integer.
3583 * Supports being called as "sc_inc_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3584 * or "src_inc_gpc(<gpc-idx>[,<table>])" only.
3585 */
3586static int
3587smp_fetch_sc_inc_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3588{
3589 struct stkctr tmpstkctr;
3590 struct stkctr *stkctr;
3591 unsigned int idx;
3592
3593 idx = args[0].data.sint;
3594
3595 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3596 if (!stkctr)
3597 return 0;
3598
3599 smp->flags = SMP_F_VOL_TEST;
3600 smp->data.type = SMP_T_SINT;
3601 smp->data.u.sint = 0;
3602
3603 if (!stkctr_entry(stkctr))
3604 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3605
3606 if (stkctr && stkctr_entry(stkctr)) {
3607 void *ptr1,*ptr2;
3608
3609
3610 /* First, update gpc0_rate if it's tracked. Second, update its
3611 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3612 */
3613 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3614 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3615 if (ptr1 || ptr2) {
3616 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3617
3618 if (ptr1) {
3619 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
3620 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
3621 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
3622 }
3623
3624 if (ptr2)
3625 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
3626
3627 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3628
3629 /* If data was modified, we need to touch to re-schedule sync */
3630 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3631 }
3632 else if (stkctr == &tmpstkctr)
3633 stktable_release(stkctr->table, stkctr_entry(stkctr));
3634 }
3635 return 1;
3636}
3637
Willy Tarreau7d562212016-11-25 16:10:05 +01003638/* Increment the General Purpose Counter 0 value from the stream's tracked
3639 * frontend counters and return it into temp integer.
3640 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
3641 */
3642static int
3643smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3644{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003645 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003646 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003647 unsigned int period = 0;
Willy Tarreau7d562212016-11-25 16:10:05 +01003648
Emeric Brun819fc6f2017-06-13 19:37:32 +02003649 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003650 if (!stkctr)
3651 return 0;
3652
3653 smp->flags = SMP_F_VOL_TEST;
3654 smp->data.type = SMP_T_SINT;
3655 smp->data.u.sint = 0;
3656
Emeric Brun819fc6f2017-06-13 19:37:32 +02003657 if (!stkctr_entry(stkctr))
3658 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003659
3660 if (stkctr && stkctr_entry(stkctr)) {
3661 void *ptr1,*ptr2;
3662
Emeric Brun819fc6f2017-06-13 19:37:32 +02003663
Willy Tarreau7d562212016-11-25 16:10:05 +01003664 /* First, update gpc0_rate if it's tracked. Second, update its
3665 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3666 */
3667 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003668 if (ptr1) {
3669 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3670 }
3671 else {
3672 /* fallback on the gpc array */
3673 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3674 if (ptr1)
3675 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3676 }
3677
Willy Tarreau7d562212016-11-25 16:10:05 +01003678 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02003679 if (!ptr2) {
3680 /* fallback on the gpc array */
3681 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3682 }
3683
Emeric Brun819fc6f2017-06-13 19:37:32 +02003684 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003685 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01003686
Emeric Brun819fc6f2017-06-13 19:37:32 +02003687 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003688 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003689 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003690 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003691 }
3692
3693 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003694 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003695
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003696 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003697
3698 /* If data was modified, we need to touch to re-schedule sync */
3699 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3700 }
3701 else if (stkctr == &tmpstkctr)
3702 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003703 }
3704 return 1;
3705}
3706
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003707/* Increment the General Purpose Counter 1 value from the stream's tracked
3708 * frontend counters and return it into temp integer.
3709 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
3710 */
3711static int
3712smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3713{
3714 struct stkctr tmpstkctr;
3715 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003716 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003717
3718 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3719 if (!stkctr)
3720 return 0;
3721
3722 smp->flags = SMP_F_VOL_TEST;
3723 smp->data.type = SMP_T_SINT;
3724 smp->data.u.sint = 0;
3725
3726 if (!stkctr_entry(stkctr))
3727 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3728
3729 if (stkctr && stkctr_entry(stkctr)) {
3730 void *ptr1,*ptr2;
3731
3732
3733 /* First, update gpc1_rate if it's tracked. Second, update its
3734 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
3735 */
3736 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003737 if (ptr1) {
3738 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3739 }
3740 else {
3741 /* fallback on the gpc array */
3742 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3743 if (ptr1)
3744 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3745 }
3746
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003747 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02003748 if (!ptr2) {
3749 /* fallback on the gpc array */
3750 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3751 }
3752
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003753 if (ptr1 || ptr2) {
3754 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3755
3756 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003757 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003758 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003759 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003760 }
3761
3762 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003763 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003764
3765 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3766
3767 /* If data was modified, we need to touch to re-schedule sync */
3768 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3769 }
3770 else if (stkctr == &tmpstkctr)
3771 stktable_release(stkctr->table, stkctr_entry(stkctr));
3772 }
3773 return 1;
3774}
3775
Emeric Brun4d7ada82021-06-30 19:04:16 +02003776/* Clear the GPC[args(0)] value from the stream's tracked
3777 * frontend counters and return its previous value into temp integer.
3778 * Supports being called as "sc_clr_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3779 * or "src_clr_gpc(<gpc-idx>[,<table>])" only.
3780 */
3781static int
3782smp_fetch_sc_clr_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3783{
3784 struct stkctr tmpstkctr;
3785 struct stkctr *stkctr;
3786 unsigned int idx;
3787
3788 idx = args[0].data.sint;
3789
3790 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3791 if (!stkctr)
3792 return 0;
3793
3794 smp->flags = SMP_F_VOL_TEST;
3795 smp->data.type = SMP_T_SINT;
3796 smp->data.u.sint = 0;
3797
3798 if (!stkctr_entry(stkctr))
3799 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3800
3801 if (stkctr && stkctr_entry(stkctr)) {
3802 void *ptr;
3803
3804 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3805 if (!ptr) {
3806 if (stkctr == &tmpstkctr)
3807 stktable_release(stkctr->table, stkctr_entry(stkctr));
3808 return 0; /* parameter not stored */
3809 }
3810
3811 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3812
3813 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3814 stktable_data_cast(ptr, std_t_uint) = 0;
3815
3816 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3817
3818 /* If data was modified, we need to touch to re-schedule sync */
3819 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3820 }
3821 return 1;
3822}
3823
Willy Tarreau7d562212016-11-25 16:10:05 +01003824/* Clear the General Purpose Counter 0 value from the stream's tracked
3825 * frontend counters and return its previous value into temp integer.
3826 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
3827 */
3828static int
3829smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3830{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003831 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003832 struct stkctr *stkctr;
3833
Emeric Brun819fc6f2017-06-13 19:37:32 +02003834 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003835 if (!stkctr)
3836 return 0;
3837
3838 smp->flags = SMP_F_VOL_TEST;
3839 smp->data.type = SMP_T_SINT;
3840 smp->data.u.sint = 0;
3841
Emeric Brun819fc6f2017-06-13 19:37:32 +02003842 if (!stkctr_entry(stkctr))
3843 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003844
Emeric Brun819fc6f2017-06-13 19:37:32 +02003845 if (stkctr && stkctr_entry(stkctr)) {
3846 void *ptr;
3847
3848 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3849 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003850 /* fallback on the gpc array */
3851 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3852 }
3853
3854 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003855 if (stkctr == &tmpstkctr)
3856 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003857 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003858 }
3859
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003860 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003861
Emeric Brun0e3457b2021-06-30 17:18:28 +02003862 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3863 stktable_data_cast(ptr, std_t_uint) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003864
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003865 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003866
Willy Tarreau7d562212016-11-25 16:10:05 +01003867 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003868 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01003869 }
3870 return 1;
3871}
3872
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003873/* Clear the General Purpose Counter 1 value from the stream's tracked
3874 * frontend counters and return its previous value into temp integer.
3875 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
3876 */
3877static int
3878smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3879{
3880 struct stkctr tmpstkctr;
3881 struct stkctr *stkctr;
3882
3883 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3884 if (!stkctr)
3885 return 0;
3886
3887 smp->flags = SMP_F_VOL_TEST;
3888 smp->data.type = SMP_T_SINT;
3889 smp->data.u.sint = 0;
3890
3891 if (!stkctr_entry(stkctr))
3892 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3893
3894 if (stkctr && stkctr_entry(stkctr)) {
3895 void *ptr;
3896
3897 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3898 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003899 /* fallback on the gpc array */
3900 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3901 }
3902
3903 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003904 if (stkctr == &tmpstkctr)
3905 stktable_release(stkctr->table, stkctr_entry(stkctr));
3906 return 0; /* parameter not stored */
3907 }
3908
3909 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3910
Emeric Brun0e3457b2021-06-30 17:18:28 +02003911 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3912 stktable_data_cast(ptr, std_t_uint) = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003913
3914 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3915
3916 /* If data was modified, we need to touch to re-schedule sync */
3917 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3918 }
3919 return 1;
3920}
3921
Willy Tarreau7d562212016-11-25 16:10:05 +01003922/* set <smp> to the cumulated number of connections from the stream's tracked
3923 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3924 * "src_conn_cnt" only.
3925 */
3926static int
3927smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3928{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003929 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003930 struct stkctr *stkctr;
3931
Emeric Brun819fc6f2017-06-13 19:37:32 +02003932 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003933 if (!stkctr)
3934 return 0;
3935
3936 smp->flags = SMP_F_VOL_TEST;
3937 smp->data.type = SMP_T_SINT;
3938 smp->data.u.sint = 0;
3939 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003940 void *ptr;
3941
3942 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
3943 if (!ptr) {
3944 if (stkctr == &tmpstkctr)
3945 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003946 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003947 }
3948
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003949 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003950
Emeric Brun0e3457b2021-06-30 17:18:28 +02003951 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003952
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003953 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003954
3955 if (stkctr == &tmpstkctr)
3956 stktable_release(stkctr->table, stkctr_entry(stkctr));
3957
3958
Willy Tarreau7d562212016-11-25 16:10:05 +01003959 }
3960 return 1;
3961}
3962
3963/* set <smp> to the connection rate from the stream's tracked frontend
3964 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3965 * only.
3966 */
3967static int
3968smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3969{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003970 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003971 struct stkctr *stkctr;
3972
Emeric Brun819fc6f2017-06-13 19:37:32 +02003973 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003974 if (!stkctr)
3975 return 0;
3976
3977 smp->flags = SMP_F_VOL_TEST;
3978 smp->data.type = SMP_T_SINT;
3979 smp->data.u.sint = 0;
3980 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003981 void *ptr;
3982
3983 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
3984 if (!ptr) {
3985 if (stkctr == &tmpstkctr)
3986 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003987 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003988 }
3989
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003990 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003991
Emeric Brun0e3457b2021-06-30 17:18:28 +02003992 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01003993 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003994
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003995 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003996
3997 if (stkctr == &tmpstkctr)
3998 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003999 }
4000 return 1;
4001}
4002
4003/* set temp integer to the number of connections from the stream's source address
4004 * in the table pointed to by expr, after updating it.
4005 * Accepts exactly 1 argument of type table.
4006 */
4007static int
4008smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4009{
4010 struct connection *conn = objt_conn(smp->sess->origin);
4011 struct stksess *ts;
4012 struct stktable_key *key;
4013 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004014 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004015
4016 if (!conn)
4017 return 0;
4018
Joseph Herlant5662fa42018-11-15 13:43:28 -08004019 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02004020 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01004021 return 0;
4022
4023 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004024 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01004025 if (!key)
4026 return 0;
4027
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004028 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004029
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004030 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01004031 /* entry does not exist and could not be created */
4032 return 0;
4033
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004034 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004035 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01004036 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004037 }
Willy Tarreau7d562212016-11-25 16:10:05 +01004038
4039 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004040
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004041 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004042
Emeric Brun0e3457b2021-06-30 17:18:28 +02004043 smp->data.u.sint = ++stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004044
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004045 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004046
Willy Tarreau7d562212016-11-25 16:10:05 +01004047 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004048
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004049 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004050
4051 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01004052 return 1;
4053}
4054
4055/* set <smp> to the number of concurrent connections from the stream's tracked
4056 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
4057 * "src_conn_cur" only.
4058 */
4059static int
4060smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
4061{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004062 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004063 struct stkctr *stkctr;
4064
Emeric Brun819fc6f2017-06-13 19:37:32 +02004065 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004066 if (!stkctr)
4067 return 0;
4068
4069 smp->flags = SMP_F_VOL_TEST;
4070 smp->data.type = SMP_T_SINT;
4071 smp->data.u.sint = 0;
4072 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004073 void *ptr;
4074
4075 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
4076 if (!ptr) {
4077 if (stkctr == &tmpstkctr)
4078 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004079 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004080 }
4081
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004082 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004083
Emeric Brun0e3457b2021-06-30 17:18:28 +02004084 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004085
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004086 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004087
4088 if (stkctr == &tmpstkctr)
4089 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004090 }
4091 return 1;
4092}
4093
4094/* set <smp> to the cumulated number of streams from the stream's tracked
4095 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
4096 * "src_sess_cnt" only.
4097 */
4098static int
4099smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4100{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004101 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004102 struct stkctr *stkctr;
4103
Emeric Brun819fc6f2017-06-13 19:37:32 +02004104 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004105 if (!stkctr)
4106 return 0;
4107
4108 smp->flags = SMP_F_VOL_TEST;
4109 smp->data.type = SMP_T_SINT;
4110 smp->data.u.sint = 0;
4111 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004112 void *ptr;
4113
4114 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
4115 if (!ptr) {
4116 if (stkctr == &tmpstkctr)
4117 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004118 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004119 }
4120
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004121 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004122
Emeric Brun0e3457b2021-06-30 17:18:28 +02004123 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004124
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004125 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004126
4127 if (stkctr == &tmpstkctr)
4128 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004129 }
4130 return 1;
4131}
4132
4133/* set <smp> to the stream rate from the stream's tracked frontend counters.
4134 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
4135 */
4136static int
4137smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4138{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004139 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004140 struct stkctr *stkctr;
4141
Emeric Brun819fc6f2017-06-13 19:37:32 +02004142 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004143 if (!stkctr)
4144 return 0;
4145
4146 smp->flags = SMP_F_VOL_TEST;
4147 smp->data.type = SMP_T_SINT;
4148 smp->data.u.sint = 0;
4149 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004150 void *ptr;
4151
4152 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
4153 if (!ptr) {
4154 if (stkctr == &tmpstkctr)
4155 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004156 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004157 }
4158
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004159 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004160
Emeric Brun0e3457b2021-06-30 17:18:28 +02004161 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004162 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004163
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004164 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004165
4166 if (stkctr == &tmpstkctr)
4167 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004168 }
4169 return 1;
4170}
4171
4172/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
4173 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
4174 * "src_http_req_cnt" only.
4175 */
4176static int
4177smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4178{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004179 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004180 struct stkctr *stkctr;
4181
Emeric Brun819fc6f2017-06-13 19:37:32 +02004182 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004183 if (!stkctr)
4184 return 0;
4185
4186 smp->flags = SMP_F_VOL_TEST;
4187 smp->data.type = SMP_T_SINT;
4188 smp->data.u.sint = 0;
4189 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004190 void *ptr;
4191
4192 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
4193 if (!ptr) {
4194 if (stkctr == &tmpstkctr)
4195 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004196 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004197 }
4198
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004199 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004200
Emeric Brun0e3457b2021-06-30 17:18:28 +02004201 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004202
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004203 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004204
4205 if (stkctr == &tmpstkctr)
4206 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004207 }
4208 return 1;
4209}
4210
4211/* set <smp> to the HTTP request rate from the stream's tracked frontend
4212 * counters. Supports being called as "sc[0-9]_http_req_rate" or
4213 * "src_http_req_rate" only.
4214 */
4215static int
4216smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4217{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004218 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004219 struct stkctr *stkctr;
4220
Emeric Brun819fc6f2017-06-13 19:37:32 +02004221 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004222 if (!stkctr)
4223 return 0;
4224
4225 smp->flags = SMP_F_VOL_TEST;
4226 smp->data.type = SMP_T_SINT;
4227 smp->data.u.sint = 0;
4228 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004229 void *ptr;
4230
4231 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
4232 if (!ptr) {
4233 if (stkctr == &tmpstkctr)
4234 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004235 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004236 }
4237
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004238 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004239
Emeric Brun0e3457b2021-06-30 17:18:28 +02004240 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004241 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004242
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004243 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004244
4245 if (stkctr == &tmpstkctr)
4246 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004247 }
4248 return 1;
4249}
4250
4251/* set <smp> to the cumulated number of HTTP requests errors from the stream's
4252 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
4253 * "src_http_err_cnt" only.
4254 */
4255static int
4256smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4257{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004258 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004259 struct stkctr *stkctr;
4260
Emeric Brun819fc6f2017-06-13 19:37:32 +02004261 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004262 if (!stkctr)
4263 return 0;
4264
4265 smp->flags = SMP_F_VOL_TEST;
4266 smp->data.type = SMP_T_SINT;
4267 smp->data.u.sint = 0;
4268 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004269 void *ptr;
4270
4271 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
4272 if (!ptr) {
4273 if (stkctr == &tmpstkctr)
4274 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004275 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004276 }
4277
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004278 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004279
Emeric Brun0e3457b2021-06-30 17:18:28 +02004280 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004281
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004282 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004283
4284 if (stkctr == &tmpstkctr)
4285 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004286 }
4287 return 1;
4288}
4289
4290/* set <smp> to the HTTP request error rate from the stream's tracked frontend
4291 * counters. Supports being called as "sc[0-9]_http_err_rate" or
4292 * "src_http_err_rate" only.
4293 */
4294static int
4295smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4296{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004297 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004298 struct stkctr *stkctr;
4299
Emeric Brun819fc6f2017-06-13 19:37:32 +02004300 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004301 if (!stkctr)
4302 return 0;
4303
4304 smp->flags = SMP_F_VOL_TEST;
4305 smp->data.type = SMP_T_SINT;
4306 smp->data.u.sint = 0;
4307 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004308 void *ptr;
4309
4310 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
4311 if (!ptr) {
4312 if (stkctr == &tmpstkctr)
4313 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004314 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004315 }
4316
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004317 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004318
Emeric Brun0e3457b2021-06-30 17:18:28 +02004319 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004320 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004321
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004322 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004323
4324 if (stkctr == &tmpstkctr)
4325 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004326 }
4327 return 1;
4328}
4329
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004330/* set <smp> to the cumulated number of HTTP response failures from the stream's
4331 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
4332 * "src_http_fail_cnt" only.
4333 */
4334static int
4335smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4336{
4337 struct stkctr tmpstkctr;
4338 struct stkctr *stkctr;
4339
4340 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4341 if (!stkctr)
4342 return 0;
4343
4344 smp->flags = SMP_F_VOL_TEST;
4345 smp->data.type = SMP_T_SINT;
4346 smp->data.u.sint = 0;
4347 if (stkctr_entry(stkctr) != NULL) {
4348 void *ptr;
4349
4350 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
4351 if (!ptr) {
4352 if (stkctr == &tmpstkctr)
4353 stktable_release(stkctr->table, stkctr_entry(stkctr));
4354 return 0; /* parameter not stored */
4355 }
4356
4357 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4358
Emeric Brun0e3457b2021-06-30 17:18:28 +02004359 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004360
4361 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4362
4363 if (stkctr == &tmpstkctr)
4364 stktable_release(stkctr->table, stkctr_entry(stkctr));
4365 }
4366 return 1;
4367}
4368
4369/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
4370 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
4371 * "src_http_fail_rate" only.
4372 */
4373static int
4374smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4375{
4376 struct stkctr tmpstkctr;
4377 struct stkctr *stkctr;
4378
4379 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4380 if (!stkctr)
4381 return 0;
4382
4383 smp->flags = SMP_F_VOL_TEST;
4384 smp->data.type = SMP_T_SINT;
4385 smp->data.u.sint = 0;
4386 if (stkctr_entry(stkctr) != NULL) {
4387 void *ptr;
4388
4389 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
4390 if (!ptr) {
4391 if (stkctr == &tmpstkctr)
4392 stktable_release(stkctr->table, stkctr_entry(stkctr));
4393 return 0; /* parameter not stored */
4394 }
4395
4396 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4397
Emeric Brun0e3457b2021-06-30 17:18:28 +02004398 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004399 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
4400
4401 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4402
4403 if (stkctr == &tmpstkctr)
4404 stktable_release(stkctr->table, stkctr_entry(stkctr));
4405 }
4406 return 1;
4407}
4408
Willy Tarreau7d562212016-11-25 16:10:05 +01004409/* set <smp> to the number of kbytes received from clients, as found in the
4410 * stream's tracked frontend counters. Supports being called as
4411 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
4412 */
4413static int
4414smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
4415{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004416 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004417 struct stkctr *stkctr;
4418
Emeric Brun819fc6f2017-06-13 19:37:32 +02004419 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004420 if (!stkctr)
4421 return 0;
4422
4423 smp->flags = SMP_F_VOL_TEST;
4424 smp->data.type = SMP_T_SINT;
4425 smp->data.u.sint = 0;
4426 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004427 void *ptr;
4428
4429 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
4430 if (!ptr) {
4431 if (stkctr == &tmpstkctr)
4432 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004433 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004434 }
4435
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004436 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004437
Emeric Brun0e3457b2021-06-30 17:18:28 +02004438 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004439
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004440 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004441
4442 if (stkctr == &tmpstkctr)
4443 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004444 }
4445 return 1;
4446}
4447
4448/* set <smp> to the data rate received from clients in bytes/s, as found
4449 * in the stream's tracked frontend counters. Supports being called as
4450 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
4451 */
4452static int
4453smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4454{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004455 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004456 struct stkctr *stkctr;
4457
Emeric Brun819fc6f2017-06-13 19:37:32 +02004458 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004459 if (!stkctr)
4460 return 0;
4461
4462 smp->flags = SMP_F_VOL_TEST;
4463 smp->data.type = SMP_T_SINT;
4464 smp->data.u.sint = 0;
4465 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004466 void *ptr;
4467
4468 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
4469 if (!ptr) {
4470 if (stkctr == &tmpstkctr)
4471 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004472 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004473 }
4474
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004475 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004476
Emeric Brun0e3457b2021-06-30 17:18:28 +02004477 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004478 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004479
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004480 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004481
4482 if (stkctr == &tmpstkctr)
4483 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004484 }
4485 return 1;
4486}
4487
4488/* set <smp> to the number of kbytes sent to clients, as found in the
4489 * stream's tracked frontend counters. Supports being called as
4490 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
4491 */
4492static int
4493smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
4494{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004495 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004496 struct stkctr *stkctr;
4497
Emeric Brun819fc6f2017-06-13 19:37:32 +02004498 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004499 if (!stkctr)
4500 return 0;
4501
4502 smp->flags = SMP_F_VOL_TEST;
4503 smp->data.type = SMP_T_SINT;
4504 smp->data.u.sint = 0;
4505 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004506 void *ptr;
4507
4508 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
4509 if (!ptr) {
4510 if (stkctr == &tmpstkctr)
4511 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004512 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004513 }
4514
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004515 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004516
Emeric Brun0e3457b2021-06-30 17:18:28 +02004517 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004518
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004519 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004520
4521 if (stkctr == &tmpstkctr)
4522 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004523 }
4524 return 1;
4525}
4526
4527/* set <smp> to the data rate sent to clients in bytes/s, as found in the
4528 * stream's tracked frontend counters. Supports being called as
4529 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
4530 */
4531static int
4532smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4533{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004534 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004535 struct stkctr *stkctr;
4536
Emeric Brun819fc6f2017-06-13 19:37:32 +02004537 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004538 if (!stkctr)
4539 return 0;
4540
4541 smp->flags = SMP_F_VOL_TEST;
4542 smp->data.type = SMP_T_SINT;
4543 smp->data.u.sint = 0;
4544 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004545 void *ptr;
4546
4547 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
4548 if (!ptr) {
4549 if (stkctr == &tmpstkctr)
4550 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004551 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004552 }
4553
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004554 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004555
Emeric Brun0e3457b2021-06-30 17:18:28 +02004556 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004557 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004558
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004559 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004560
4561 if (stkctr == &tmpstkctr)
4562 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004563 }
4564 return 1;
4565}
4566
4567/* set <smp> to the number of active trackers on the SC entry in the stream's
4568 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
4569 */
4570static int
4571smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
4572{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004573 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004574 struct stkctr *stkctr;
4575
Emeric Brun819fc6f2017-06-13 19:37:32 +02004576 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004577 if (!stkctr)
4578 return 0;
4579
4580 smp->flags = SMP_F_VOL_TEST;
4581 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004582 if (stkctr == &tmpstkctr) {
4583 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
4584 stktable_release(stkctr->table, stkctr_entry(stkctr));
4585 }
4586 else {
4587 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
4588 }
4589
Willy Tarreau7d562212016-11-25 16:10:05 +01004590 return 1;
4591}
4592
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004593
4594/* The functions below are used to manipulate table contents from the CLI.
4595 * There are 3 main actions, "clear", "set" and "show". The code is shared
4596 * between all actions, and the action is encoded in the void *private in
4597 * the appctx as well as in the keyword registration, among one of the
4598 * following values.
4599 */
4600
4601enum {
4602 STK_CLI_ACT_CLR,
4603 STK_CLI_ACT_SET,
4604 STK_CLI_ACT_SHOW,
4605};
4606
Willy Tarreau4596fe22022-05-17 19:07:51 +02004607/* Dump the status of a table to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004608 * read buffer. It returns 0 if the output buffer is full
4609 * and needs to be called again, otherwise non-zero.
4610 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004611static int table_dump_head_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004612 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004613 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004614{
Willy Tarreauc12b3212022-05-27 11:08:15 +02004615 struct stream *s = __sc_strm(appctx_sc(appctx));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004616
4617 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004618 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004619
4620 /* any other information should be dumped here */
4621
William Lallemand07a62f72017-05-24 00:57:40 +02004622 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004623 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
4624
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004625 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004626 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004627
4628 return 1;
4629}
4630
Willy Tarreau4596fe22022-05-17 19:07:51 +02004631/* Dump a table entry to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004632 * read buffer. It returns 0 if the output buffer is full
4633 * and needs to be called again, otherwise non-zero.
4634 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004635static int table_dump_entry_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004636 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004637 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004638{
4639 int dt;
4640
4641 chunk_appendf(msg, "%p:", entry);
4642
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004643 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004644 char addr[INET_ADDRSTRLEN];
4645 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
4646 chunk_appendf(msg, " key=%s", addr);
4647 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004648 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004649 char addr[INET6_ADDRSTRLEN];
4650 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
4651 chunk_appendf(msg, " key=%s", addr);
4652 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004653 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01004654 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004655 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004656 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004657 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004658 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004659 }
4660 else {
4661 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004662 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004663 }
4664
Willy Tarreau16b282f2022-11-29 11:55:18 +01004665 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 +01004666
4667 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
4668 void *ptr;
4669
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004670 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004671 continue;
Emeric Brunc64a2a32021-06-30 18:01:02 +02004672 if (stktable_data_types[dt].is_array) {
4673 char tmp[16] = {};
4674 const char *name_pfx = stktable_data_types[dt].name;
4675 const char *name_sfx = NULL;
4676 unsigned int idx = 0;
4677 int i = 0;
4678
4679 /* split name to show index before first _ of the name
4680 * for example: 'gpc3_rate' if array name is 'gpc_rate'.
4681 */
4682 for (i = 0 ; i < (sizeof(tmp) - 1); i++) {
4683 if (!name_pfx[i])
4684 break;
4685 if (name_pfx[i] == '_') {
4686 name_pfx = &tmp[0];
4687 name_sfx = &stktable_data_types[dt].name[i];
4688 break;
4689 }
4690 tmp[i] = name_pfx[i];
4691 }
4692
4693 ptr = stktable_data_ptr_idx(t, entry, dt, idx);
4694 while (ptr) {
4695 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
4696 chunk_appendf(msg, " %s%u%s(%u)=", name_pfx, idx, name_sfx ? name_sfx : "", t->data_arg[dt].u);
4697 else
4698 chunk_appendf(msg, " %s%u%s=", name_pfx, idx, name_sfx ? name_sfx : "");
4699 switch (stktable_data_types[dt].std_type) {
4700 case STD_T_SINT:
4701 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4702 break;
4703 case STD_T_UINT:
4704 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4705 break;
4706 case STD_T_ULL:
4707 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
4708 break;
4709 case STD_T_FRQP:
4710 chunk_appendf(msg, "%u",
4711 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4712 t->data_arg[dt].u));
4713 break;
4714 }
4715 ptr = stktable_data_ptr_idx(t, entry, dt, ++idx);
4716 }
4717 continue;
4718 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004719 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brun01928ae2021-06-30 16:24:04 +02004720 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004721 else
4722 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
4723
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004724 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004725 switch (stktable_data_types[dt].std_type) {
4726 case STD_T_SINT:
4727 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4728 break;
4729 case STD_T_UINT:
4730 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4731 break;
4732 case STD_T_ULL:
Emeric Brun01928ae2021-06-30 16:24:04 +02004733 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004734 break;
4735 case STD_T_FRQP:
Emeric Brun01928ae2021-06-30 16:24:04 +02004736 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004737 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004738 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004739 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02004740 case STD_T_DICT: {
4741 struct dict_entry *de;
4742 de = stktable_data_cast(ptr, std_t_dict);
4743 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
4744 break;
4745 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004746 }
4747 }
4748 chunk_appendf(msg, "\n");
4749
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004750 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004751 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004752
4753 return 1;
4754}
4755
Willy Tarreau3c69e082022-05-03 11:35:07 +02004756/* appctx context used by the "show table" command */
4757struct show_table_ctx {
4758 void *target; /* table we want to dump, or NULL for all */
4759 struct stktable *t; /* table being currently dumped (first if NULL) */
4760 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
4761 long long value[STKTABLE_FILTER_LEN]; /* value to compare against */
4762 signed char data_type[STKTABLE_FILTER_LEN]; /* type of data to compare, or -1 if none */
4763 signed char data_op[STKTABLE_FILTER_LEN]; /* operator (STD_OP_*) when data_type set */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004764 enum {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004765 STATE_NEXT = 0, /* px points to next table, entry=NULL */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004766 STATE_DUMP, /* px points to curr table, entry is valid, refcount held */
4767 STATE_DONE, /* done dumping */
4768 } state;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004769 char action; /* action on the table : one of STK_CLI_ACT_* */
4770};
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004771
4772/* Processes a single table entry matching a specific key passed in argument.
4773 * returns 0 if wants to be called again, 1 if has ended processing.
4774 */
4775static int table_process_entry_per_key(struct appctx *appctx, char **args)
4776{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004777 struct show_table_ctx *ctx = appctx->svcctx;
4778 struct stktable *t = ctx->target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004779 struct stksess *ts;
4780 uint32_t uint32_key;
4781 unsigned char ip6_key[sizeof(struct in6_addr)];
4782 long long value;
4783 int data_type;
4784 int cur_arg;
4785 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004786 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004787
Willy Tarreau9d008692019-08-09 11:21:01 +02004788 if (!*args[4])
4789 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004790
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004791 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004792 case SMP_T_IPV4:
4793 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02004794 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004795 break;
4796 case SMP_T_IPV6:
Christopher Fauletb7c962b2021-11-15 09:17:25 +01004797 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
4798 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004799 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004800 break;
4801 case SMP_T_SINT:
4802 {
4803 char *endptr;
4804 unsigned long val;
4805 errno = 0;
4806 val = strtoul(args[4], &endptr, 10);
4807 if ((errno == ERANGE && val == ULONG_MAX) ||
4808 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02004809 val > 0xffffffff)
4810 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004811 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02004812 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004813 break;
4814 }
4815 break;
4816 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02004817 static_table_key.key = args[4];
4818 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004819 break;
4820 default:
Willy Tarreau3c69e082022-05-03 11:35:07 +02004821 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004822 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02004823 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 +01004824 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02004825 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 +01004826 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02004827 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 +01004828 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004829 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004830 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004831 }
4832
4833 /* check permissions */
4834 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4835 return 1;
4836
Willy Tarreau3c69e082022-05-03 11:35:07 +02004837 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004838 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004839 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004840 if (!ts)
4841 return 1;
4842 chunk_reset(&trash);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004843 if (!table_dump_head_to_buffer(&trash, appctx, t, t)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004844 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004845 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004846 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004847 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004848 if (!table_dump_entry_to_buffer(&trash, appctx, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004849 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004850 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004851 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004852 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004853 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004854 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004855 break;
4856
4857 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004858 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004859 if (!ts)
4860 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004861
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004862 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004863 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004864 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004865 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004866 break;
4867
4868 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004869 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004870 if (!ts) {
4871 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004872 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004873 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004874 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004875 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
4876 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004877 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004878 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004879 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004880 return 1;
4881 }
4882
4883 data_type = stktable_get_data_type(args[cur_arg] + 5);
4884 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004885 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004886 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004887 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004888 return 1;
4889 }
4890
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004891 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004892 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004893 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004894 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004895 return 1;
4896 }
4897
4898 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004899 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004900 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004901 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004902 return 1;
4903 }
4904
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004905 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004906
4907 switch (stktable_data_types[data_type].std_type) {
4908 case STD_T_SINT:
4909 stktable_data_cast(ptr, std_t_sint) = value;
4910 break;
4911 case STD_T_UINT:
4912 stktable_data_cast(ptr, std_t_uint) = value;
4913 break;
4914 case STD_T_ULL:
4915 stktable_data_cast(ptr, std_t_ull) = value;
4916 break;
4917 case STD_T_FRQP:
4918 /* We set both the current and previous values. That way
4919 * the reported frequency is stable during all the period
4920 * then slowly fades out. This allows external tools to
4921 * push measures without having to update them too often.
4922 */
4923 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004924 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004925 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004926 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004927 using its internal lock */
4928 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004929 frqp->prev_ctr = 0;
4930 frqp->curr_ctr = value;
4931 break;
4932 }
4933 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004934 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004935 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004936 break;
4937
4938 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004939 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004940 }
4941 return 1;
4942}
4943
4944/* Prepares the appctx fields with the data-based filters from the command line.
4945 * Returns 0 if the dump can proceed, 1 if has ended processing.
4946 */
4947static int table_prepare_data_request(struct appctx *appctx, char **args)
4948{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004949 struct show_table_ctx *ctx = appctx->svcctx;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004950 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004951 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004952
Willy Tarreau3c69e082022-05-03 11:35:07 +02004953 if (ctx->action != STK_CLI_ACT_SHOW && ctx->action != STK_CLI_ACT_CLR)
Willy Tarreau9d008692019-08-09 11:21:01 +02004954 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004955
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004956 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
4957 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
4958 break;
4959 /* condition on stored data value */
Willy Tarreau3c69e082022-05-03 11:35:07 +02004960 ctx->data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
4961 if (ctx->data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004962 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004963
Willy Tarreau3c69e082022-05-03 11:35:07 +02004964 if (!((struct stktable *)ctx->target)->data_ofs[ctx->data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004965 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 +01004966
Willy Tarreau3c69e082022-05-03 11:35:07 +02004967 ctx->data_op[i] = get_std_op(args[4+3*i]);
4968 if (ctx->data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004969 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 +01004970
Willy Tarreau3c69e082022-05-03 11:35:07 +02004971 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 +01004972 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
4973 }
4974
4975 if (*args[3+3*i]) {
4976 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 +01004977 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004978
4979 /* OK we're done, all the fields are set */
4980 return 0;
4981}
4982
4983/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004984static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004985{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004986 struct show_table_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004987 int i;
4988
4989 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
Willy Tarreau3c69e082022-05-03 11:35:07 +02004990 ctx->data_type[i] = -1;
4991 ctx->target = NULL;
4992 ctx->entry = NULL;
4993 ctx->action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004994
4995 if (*args[2]) {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004996 ctx->t = ctx->target = stktable_find_by_name(args[2]);
Willy Tarreau3c69e082022-05-03 11:35:07 +02004997 if (!ctx->target)
Willy Tarreau9d008692019-08-09 11:21:01 +02004998 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004999 }
5000 else {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005001 ctx->t = stktables_list;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005002 if (ctx->action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005003 goto err_args;
5004 return 0;
5005 }
5006
5007 if (strcmp(args[3], "key") == 0)
5008 return table_process_entry_per_key(appctx, args);
5009 else if (strncmp(args[3], "data.", 5) == 0)
5010 return table_prepare_data_request(appctx, args);
5011 else if (*args[3])
5012 goto err_args;
5013
5014 return 0;
5015
5016err_args:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005017 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005018 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02005019 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 +01005020 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02005021 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 +01005022 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02005023 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005024 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02005025 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005026 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005027}
5028
5029/* This function is used to deal with table operations (dump or clear depending
5030 * on the action stored in appctx->private). It returns 0 if the output buffer is
5031 * full and it needs to be called again, otherwise non-zero.
5032 */
5033static int cli_io_handler_table(struct appctx *appctx)
5034{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005035 struct show_table_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02005036 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02005037 struct stream *s = __sc_strm(sc);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005038 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005039 int skip_entry;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005040 int show = ctx->action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005041
5042 /*
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005043 * We have 3 possible states in ctx->state :
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005044 * - STATE_NEXT : the proxy pointer points to the next table to
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005045 * dump, the entry pointer is NULL ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005046 * - STATE_DUMP : the proxy pointer points to the current table
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005047 * and the entry pointer points to the next entry to be dumped,
5048 * and the refcount on the next entry is held ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005049 * - STATE_DONE : nothing left to dump, the buffer may contain some
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005050 * data though.
5051 */
Christopher Faulet87633c32023-04-03 18:32:50 +02005052 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02005053 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005054 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005055 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005056 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005057 }
5058 return 1;
5059 }
5060
5061 chunk_reset(&trash);
5062
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005063 while (ctx->state != STATE_DONE) {
5064 switch (ctx->state) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005065 case STATE_NEXT:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005066 if (!ctx->t ||
5067 (ctx->target &&
5068 ctx->t != ctx->target)) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005069 ctx->state = STATE_DONE;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005070 break;
5071 }
5072
Willy Tarreau3c69e082022-05-03 11:35:07 +02005073 if (ctx->t->size) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005074 if (show && !table_dump_head_to_buffer(&trash, appctx, ctx->t, ctx->target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005075 return 0;
5076
Willy Tarreau3c69e082022-05-03 11:35:07 +02005077 if (ctx->target &&
William Lallemand07a62f72017-05-24 00:57:40 +02005078 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005079 /* dump entries only if table explicitly requested */
Willy Tarreau76642222022-10-11 12:02:50 +02005080 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005081 eb = ebmb_first(&ctx->t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005082 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005083 ctx->entry = ebmb_entry(eb, struct stksess, key);
5084 ctx->entry->ref_cnt++;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005085 ctx->state = STATE_DUMP;
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 break;
5088 }
Willy Tarreau76642222022-10-11 12:02:50 +02005089 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005090 }
5091 }
Willy Tarreau3c69e082022-05-03 11:35:07 +02005092 ctx->t = ctx->t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005093 break;
5094
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005095 case STATE_DUMP:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005096 skip_entry = 0;
5097
Willy Tarreau3c69e082022-05-03 11:35:07 +02005098 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005099
Willy Tarreau3c69e082022-05-03 11:35:07 +02005100 if (ctx->data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005101 /* we're filtering on some data contents */
5102 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01005103 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005104 signed char op;
5105 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005106
Emeric Brun819fc6f2017-06-13 19:37:32 +02005107
Willy Tarreau2b64a352020-01-22 17:09:47 +01005108 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005109 if (ctx->data_type[i] == -1)
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005110 break;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005111 dt = ctx->data_type[i];
5112 ptr = stktable_data_ptr(ctx->t,
5113 ctx->entry,
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005114 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005115
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005116 data = 0;
5117 switch (stktable_data_types[dt].std_type) {
5118 case STD_T_SINT:
5119 data = stktable_data_cast(ptr, std_t_sint);
5120 break;
5121 case STD_T_UINT:
5122 data = stktable_data_cast(ptr, std_t_uint);
5123 break;
5124 case STD_T_ULL:
5125 data = stktable_data_cast(ptr, std_t_ull);
5126 break;
5127 case STD_T_FRQP:
5128 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau3c69e082022-05-03 11:35:07 +02005129 ctx->t->data_arg[dt].u);
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005130 break;
5131 }
5132
Willy Tarreau3c69e082022-05-03 11:35:07 +02005133 op = ctx->data_op[i];
5134 value = ctx->value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005135
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005136 /* skip the entry if the data does not match the test and the value */
5137 if ((data < value &&
5138 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
5139 (data == value &&
5140 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
5141 (data > value &&
5142 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
5143 skip_entry = 1;
5144 break;
5145 }
5146 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005147 }
5148
5149 if (show && !skip_entry &&
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005150 !table_dump_entry_to_buffer(&trash, appctx, ctx->t, ctx->entry)) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005151 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005152 return 0;
5153 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005154
Willy Tarreau3c69e082022-05-03 11:35:07 +02005155 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005156
Willy Tarreau76642222022-10-11 12:02:50 +02005157 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005158 ctx->entry->ref_cnt--;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005159
Willy Tarreau3c69e082022-05-03 11:35:07 +02005160 eb = ebmb_next(&ctx->entry->key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005161 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005162 struct stksess *old = ctx->entry;
5163 ctx->entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005164 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005165 __stksess_kill_if_expired(ctx->t, old);
5166 else if (!skip_entry && !ctx->entry->ref_cnt)
5167 __stksess_kill(ctx->t, old);
5168 ctx->entry->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02005169 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005170 break;
5171 }
5172
5173
5174 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005175 __stksess_kill_if_expired(ctx->t, ctx->entry);
5176 else if (!skip_entry && !ctx->entry->ref_cnt)
5177 __stksess_kill(ctx->t, ctx->entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005178
Willy Tarreau76642222022-10-11 12:02:50 +02005179 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005180
Willy Tarreau3c69e082022-05-03 11:35:07 +02005181 ctx->t = ctx->t->next;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005182 ctx->state = STATE_NEXT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005183 break;
5184
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005185 default:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005186 break;
5187 }
5188 }
5189 return 1;
5190}
5191
5192static void cli_release_show_table(struct appctx *appctx)
5193{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005194 struct show_table_ctx *ctx = appctx->svcctx;
5195
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005196 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005197 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005198 }
5199}
5200
Willy Tarreau6c011712023-01-06 16:09:58 +01005201static int stk_parse_stick_counters(char **args, int section_type, struct proxy *curpx,
5202 const struct proxy *defpx, const char *file, int line,
5203 char **err)
5204{
5205 char *error;
5206 int counters;
5207
5208 counters = strtol(args[1], &error, 10);
5209 if (*error != 0) {
5210 memprintf(err, "%s: '%s' is an invalid number", args[0], args[1]);
5211 return -1;
5212 }
5213
5214 if (counters < 0) {
5215 memprintf(err, "%s: the number of stick-counters may not be negative (was %d)", args[0], counters);
5216 return -1;
5217 }
5218
5219 global.tune.nb_stk_ctr = counters;
5220 return 0;
5221}
5222
5223/* This function creates the stk_ctr pools after the configuration parsing. It
5224 * returns 0 on success otherwise ERR_*. If nb_stk_ctr is 0, the pool remains
5225 * NULL.
5226 */
5227static int stkt_create_stk_ctr_pool(void)
5228{
5229 if (!global.tune.nb_stk_ctr)
5230 return 0;
5231
5232 pool_head_stk_ctr = create_pool("stk_ctr", sizeof(*((struct session*)0)->stkctr) * global.tune.nb_stk_ctr, MEM_F_SHARED);
5233 if (!pool_head_stk_ctr) {
5234 ha_alert("out of memory while creating the stick-counters pool.\n");
5235 return ERR_ABORT;
5236 }
5237 return 0;
5238}
5239
Willy Tarreau478331d2020-08-28 11:31:31 +02005240static void stkt_late_init(void)
5241{
5242 struct sample_fetch *f;
5243
5244 f = find_sample_fetch("src", strlen("src"));
5245 if (f)
5246 smp_fetch_src = f->process;
Willy Tarreau6c011712023-01-06 16:09:58 +01005247 hap_register_post_check(stkt_create_stk_ctr_pool);
Willy Tarreau478331d2020-08-28 11:31:31 +02005248}
5249
5250INITCALL0(STG_INIT, stkt_late_init);
5251
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005252/* register cli keywords */
5253static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005254 { { "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 },
5255 { { "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 },
5256 { { "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 +01005257 {{},}
5258}};
5259
Willy Tarreau0108d902018-11-25 19:14:37 +01005260INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005261
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005262static struct action_kw_list tcp_conn_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005263 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005264 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5265 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5266 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005267 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5268 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005269 { /* END */ }
5270}};
5271
Willy Tarreau0108d902018-11-25 19:14:37 +01005272INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
5273
Willy Tarreau620408f2016-10-21 16:37:51 +02005274static struct action_kw_list tcp_sess_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005275 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005276 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5277 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5278 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005279 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5280 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02005281 { /* END */ }
5282}};
5283
Willy Tarreau0108d902018-11-25 19:14:37 +01005284INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
5285
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005286static struct action_kw_list tcp_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005287 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005288 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5289 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5290 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005291 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5292 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005293 { /* END */ }
5294}};
5295
Willy Tarreau0108d902018-11-25 19:14:37 +01005296INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
5297
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005298static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005299 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005300 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5301 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5302 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005303 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5304 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005305 { /* END */ }
5306}};
5307
Willy Tarreau0108d902018-11-25 19:14:37 +01005308INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
5309
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005310static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005311 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005312 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5313 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5314 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005315 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5316 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005317 { /* END */ }
5318}};
5319
Willy Tarreau0108d902018-11-25 19:14:37 +01005320INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
5321
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005322static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005323 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005324 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5325 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5326 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005327 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5328 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005329 { /* END */ }
5330}};
5331
Willy Tarreau0108d902018-11-25 19:14:37 +01005332INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
5333
Christopher Fauleta9248042023-01-05 11:17:38 +01005334static struct action_kw_list http_after_res_kws = { { }, {
Aurelien DARRAGONe2907c72023-03-17 11:28:58 +01005335 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Christopher Fauleta9248042023-01-05 11:17:38 +01005336 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5337 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5338 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
5339 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5340 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
5341 { /* END */ }
5342}};
5343
5344INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
5345
Willy Tarreau7d562212016-11-25 16:10:05 +01005346/* Note: must not be declared <const> as its list will be overwritten.
5347 * Please take care of keeping this list alphabetically sorted.
5348 */
5349static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
5350 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5351 { "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 +02005352 { "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 +01005353 { "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 +01005354 { "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 +01005355 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5356 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5357 { "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 +02005358 { "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 +01005359 { "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 +02005360 { "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 +01005361 { "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 +01005362 { "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 +02005363 { "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 +01005364 { "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 +01005365 { "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 +01005366 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5367 { "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 +01005368 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5369 { "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 +01005370 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5371 { "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 +02005372 { "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 +01005373 { "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 +01005374 { "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 +01005375 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5376 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5377 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5378 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5379 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5380 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5381 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5382 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5383 { "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 +01005384 { "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 +01005385 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5386 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5387 { "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 +01005388 { "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 +01005389 { "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 +01005390 { "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 +01005391 { "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 +01005392 { "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 +01005393 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5394 { "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 +01005395 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5396 { "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 +01005397 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5398 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5399 { "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 +01005400 { "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 +01005401 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5402 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5403 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5404 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5405 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5406 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5407 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5408 { "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 +02005409 { "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 +01005410 { "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 +01005411 { "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 +01005412 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5413 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5414 { "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 +01005415 { "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 +01005416 { "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 +01005417 { "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 +01005418 { "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 +01005419 { "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 +01005420 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5421 { "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 +01005422 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5423 { "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 +01005424 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5425 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5426 { "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 +01005427 { "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 +01005428 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5429 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5430 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5431 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5432 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5433 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5434 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5435 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5436 { "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 +01005437 { "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 +01005438 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5439 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5440 { "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 +01005441 { "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 +01005442 { "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 +01005443 { "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 +01005444 { "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 +01005445 { "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 +01005446 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5447 { "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 +01005448 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5449 { "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 +01005450 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5451 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5452 { "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 +01005453 { "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 +01005454 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5455 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5456 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5457 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5458 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5459 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5460 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5461 { "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 +02005462 { "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 +01005463 { "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 +01005464 { "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 +01005465 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5466 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5467 { "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 +02005468 { "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 +01005469 { "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 +02005470 { "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 +01005471 { "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 +01005472 { "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 +02005473 { "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 +01005474 { "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 +01005475 { "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 +01005476 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5477 { "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 +01005478 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5479 { "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 +01005480 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5481 { "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 +02005482 { "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 +01005483 { "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 +01005484 { "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 +01005485 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5486 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5487 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5488 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5489 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5490 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5491 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5492 { /* END */ },
5493}};
5494
Willy Tarreau0108d902018-11-25 19:14:37 +01005495INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01005496
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005497/* Note: must not be declared <const> as its list will be overwritten */
5498static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02005499 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
5500 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5501 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5502 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5503 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5504 { "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 +02005505 { "table_expire", sample_conv_table_expire, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun877b0b52021-06-30 18:57:49 +02005506 { "table_gpt", sample_conv_table_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005507 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005508 { "table_gpc", sample_conv_table_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005509 { "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 +01005510 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005511 { "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 +02005512 { "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 +01005513 { "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 +02005514 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5515 { "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 +01005516 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5517 { "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 +02005518 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5519 { "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 +02005520 { "table_idle", sample_conv_table_idle, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005521 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5522 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5523 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5524 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5525 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5526 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005527 { /* END */ },
5528}};
5529
Willy Tarreau0108d902018-11-25 19:14:37 +01005530INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau6c011712023-01-06 16:09:58 +01005531
5532static struct cfg_kw_list cfg_kws = {{ },{
5533 { CFG_GLOBAL, "tune.stick-counters", stk_parse_stick_counters },
5534 { /* END */ }
5535}};
5536
5537INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);