blob: de40f413b1f21675561a4a153da0457359a419cb [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 */
791int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
792{
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) {
804 (*myidx)++;
805 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200806 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100807 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200808 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200809 /* null terminated string needs +1 for '\0'. */
810 (*key_size)++;
811 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100812 (*myidx)++;
813 }
814 }
815 return 0;
816 }
817 return 1;
818}
819
Emeric Brunc64a2a32021-06-30 18:01:02 +0200820/* reserve some space for data type <type>, there is 2 optionnals
821 * argument at <sa> and <sa2> to configure this data type and
822 * they can be NULL if unused for a given type.
823 * Returns PE_NONE (0) if OK or an error code among :
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200824 * - PE_ENUM_OOR if <type> does not exist
825 * - PE_EXIST if <type> is already registered
Emeric Brunc64a2a32021-06-30 18:01:02 +0200826 * - PE_ARG_NOT_USE if <sa>/<sa2> was provided but not expected
827 * - PE_ARG_MISSING if <sa>/<sa2> was expected but not provided
828 * - PE_ARG_VALUE_OOR if type is an array and <sa> it out of array size range.
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200829 */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200830int stktable_alloc_data_type(struct stktable *t, int type, const char *sa, const char *sa2)
831
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200832{
833 if (type >= STKTABLE_DATA_TYPES)
834 return PE_ENUM_OOR;
835
836 if (t->data_ofs[type])
837 /* already allocated */
838 return PE_EXIST;
839
Emeric Brunc64a2a32021-06-30 18:01:02 +0200840 t->data_nbelem[type] = 1;
841 if (stktable_data_types[type].is_array) {
842 /* arrays take their element count on first argument */
843 if (!sa)
844 return PE_ARG_MISSING;
845 t->data_nbelem[type] = atoi(sa);
846 if (!t->data_nbelem[type] || (t->data_nbelem[type] > STKTABLE_MAX_DT_ARRAY_SIZE))
847 return PE_ARG_VALUE_OOR;
848 sa = sa2;
849 }
850
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200851 switch (stktable_data_types[type].arg_type) {
852 case ARG_T_NONE:
853 if (sa)
854 return PE_ARG_NOT_USED;
855 break;
856 case ARG_T_INT:
857 if (!sa)
858 return PE_ARG_MISSING;
859 t->data_arg[type].i = atoi(sa);
860 break;
861 case ARG_T_DELAY:
862 if (!sa)
863 return PE_ARG_MISSING;
864 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
865 if (sa)
866 return PE_ARG_INVC; /* invalid char */
867 break;
868 }
869
Emeric Brunc64a2a32021-06-30 18:01:02 +0200870 t->data_size += t->data_nbelem[type] * stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200871 t->data_ofs[type] = -t->data_size;
872 return PE_NONE;
873}
874
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100875/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100876 * Parse a line with <linenum> as number in <file> configuration file to configure
877 * the stick-table with <t> as address and <id> as ID.
878 * <peers> provides the "peers" section pointer only if this function is called
879 * from a "peers" section.
880 * <nid> is the stick-table name which is sent over the network. It must be equal
881 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
882 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500883 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100884 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
885 */
886int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100887 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100888{
889 int err_code = 0;
890 int idx = 1;
891 unsigned int val;
892
893 if (!id || !*id) {
894 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
895 err_code |= ERR_ALERT | ERR_ABORT;
896 goto out;
897 }
898
899 /* Store the "peers" section if this function is called from a "peers" section. */
900 if (peers) {
901 t->peers.p = peers;
902 idx++;
903 }
904
905 t->id = id;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200906 t->idlen = strlen(id);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100907 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100908 t->type = (unsigned int)-1;
909 t->conf.file = file;
910 t->conf.line = linenum;
911
912 while (*args[idx]) {
913 const char *err;
914
915 if (strcmp(args[idx], "size") == 0) {
916 idx++;
917 if (!*(args[idx])) {
918 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
919 file, linenum, args[0], args[idx-1]);
920 err_code |= ERR_ALERT | ERR_FATAL;
921 goto out;
922 }
923 if ((err = parse_size_err(args[idx], &t->size))) {
924 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
925 file, linenum, args[0], *err, args[idx-1]);
926 err_code |= ERR_ALERT | ERR_FATAL;
927 goto out;
928 }
929 idx++;
930 }
931 /* This argument does not exit in "peers" section. */
932 else if (!peers && strcmp(args[idx], "peers") == 0) {
933 idx++;
934 if (!*(args[idx])) {
935 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
936 file, linenum, args[0], args[idx-1]);
937 err_code |= ERR_ALERT | ERR_FATAL;
938 goto out;
939 }
940 t->peers.name = strdup(args[idx++]);
941 }
942 else if (strcmp(args[idx], "expire") == 0) {
943 idx++;
944 if (!*(args[idx])) {
945 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
946 file, linenum, args[0], args[idx-1]);
947 err_code |= ERR_ALERT | ERR_FATAL;
948 goto out;
949 }
950 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200951 if (err == PARSE_TIME_OVER) {
952 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
953 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100954 err_code |= ERR_ALERT | ERR_FATAL;
955 goto out;
956 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200957 else if (err == PARSE_TIME_UNDER) {
958 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
959 file, linenum, args[0], args[idx], args[idx-1]);
960 err_code |= ERR_ALERT | ERR_FATAL;
961 goto out;
962 }
963 else if (err) {
964 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
965 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100966 err_code |= ERR_ALERT | ERR_FATAL;
967 goto out;
968 }
969 t->expire = val;
970 idx++;
971 }
972 else if (strcmp(args[idx], "nopurge") == 0) {
973 t->nopurge = 1;
974 idx++;
975 }
976 else if (strcmp(args[idx], "type") == 0) {
977 idx++;
978 if (stktable_parse_type(args, &idx, &t->type, &t->key_size) != 0) {
979 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n",
980 file, linenum, args[0], args[idx]);
981 err_code |= ERR_ALERT | ERR_FATAL;
982 goto out;
983 }
984 /* idx already points to next arg */
985 }
986 else if (strcmp(args[idx], "store") == 0) {
987 int type, err;
Emeric Brunc64a2a32021-06-30 18:01:02 +0200988 char *cw, *nw, *sa, *sa2;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100989
990 idx++;
991 nw = args[idx];
992 while (*nw) {
993 /* the "store" keyword supports a comma-separated list */
994 cw = nw;
995 sa = NULL; /* store arg */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200996 sa2 = NULL;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100997 while (*nw && *nw != ',') {
998 if (*nw == '(') {
999 *nw = 0;
1000 sa = ++nw;
1001 while (*nw != ')') {
1002 if (!*nw) {
1003 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
1004 file, linenum, args[0], cw);
1005 err_code |= ERR_ALERT | ERR_FATAL;
1006 goto out;
1007 }
Emeric Brunc64a2a32021-06-30 18:01:02 +02001008 if (*nw == ',') {
1009 *nw = '\0';
1010 sa2 = nw + 1;
1011 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001012 nw++;
1013 }
1014 *nw = '\0';
1015 }
1016 nw++;
1017 }
1018 if (*nw)
1019 *nw++ = '\0';
1020 type = stktable_get_data_type(cw);
1021 if (type < 0) {
1022 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
1023 file, linenum, args[0], cw);
1024 err_code |= ERR_ALERT | ERR_FATAL;
1025 goto out;
1026 }
1027
Emeric Brunc64a2a32021-06-30 18:01:02 +02001028 err = stktable_alloc_data_type(t, type, sa, sa2);
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001029 switch (err) {
1030 case PE_NONE: break;
1031 case PE_EXIST:
1032 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
1033 file, linenum, args[0], cw);
1034 err_code |= ERR_WARN;
1035 break;
1036
1037 case PE_ARG_MISSING:
1038 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
1039 file, linenum, args[0], cw);
1040 err_code |= ERR_ALERT | ERR_FATAL;
1041 goto out;
1042
1043 case PE_ARG_NOT_USED:
1044 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
1045 file, linenum, args[0], cw);
1046 err_code |= ERR_ALERT | ERR_FATAL;
1047 goto out;
Emeric Brunc64a2a32021-06-30 18:01:02 +02001048 case PE_ARG_VALUE_OOR:
1049 ha_alert("parsing [%s:%d] : %s: array size is out of allowed range (1-%d) for store option '%s'.\n",
1050 file, linenum, args[0], STKTABLE_MAX_DT_ARRAY_SIZE, cw);
1051 err_code |= ERR_ALERT | ERR_FATAL;
1052 goto out;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001053
1054 default:
1055 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
1056 file, linenum, args[0], cw);
1057 err_code |= ERR_ALERT | ERR_FATAL;
1058 goto out;
1059 }
1060 }
1061 idx++;
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001062 if (t->data_ofs[STKTABLE_DT_GPT] && t->data_ofs[STKTABLE_DT_GPT0]) {
1063 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpt' and 'gpt0' in a same table is not permitted as 'gpt' overrides 'gpt0'.\n",
1064 file, linenum, args[0]);
1065 err_code |= ERR_ALERT | ERR_FATAL;
1066 goto out;
1067 }
Emeric Brun726783d2021-06-30 19:06:43 +02001068 else if (t->data_ofs[STKTABLE_DT_GPC] && (t->data_ofs[STKTABLE_DT_GPC0] || t->data_ofs[STKTABLE_DT_GPC1])) {
1069 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",
1070 file, linenum, args[0]);
1071 err_code |= ERR_ALERT | ERR_FATAL;
1072 goto out;
1073 }
1074 else if (t->data_ofs[STKTABLE_DT_GPC_RATE] && (t->data_ofs[STKTABLE_DT_GPC0_RATE] || t->data_ofs[STKTABLE_DT_GPC1_RATE])) {
1075 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",
1076 file, linenum, args[0]);
1077 err_code |= ERR_ALERT | ERR_FATAL;
1078 goto out;
1079 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001080 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001081 else if (strcmp(args[idx], "srvkey") == 0) {
1082 char *keytype;
1083 idx++;
1084 keytype = args[idx];
1085 if (strcmp(keytype, "name") == 0) {
1086 t->server_key_type = STKTABLE_SRV_NAME;
1087 }
1088 else if (strcmp(keytype, "addr") == 0) {
1089 t->server_key_type = STKTABLE_SRV_ADDR;
1090 }
1091 else {
1092 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
1093 file, linenum, args[0], keytype);
1094 err_code |= ERR_ALERT | ERR_FATAL;
1095 goto out;
1096
1097 }
1098 idx++;
1099 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001100 else {
1101 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
1102 file, linenum, args[0], args[idx]);
1103 err_code |= ERR_ALERT | ERR_FATAL;
1104 goto out;
1105 }
1106 }
1107
1108 if (!t->size) {
1109 ha_alert("parsing [%s:%d] : %s: missing size.\n",
1110 file, linenum, args[0]);
1111 err_code |= ERR_ALERT | ERR_FATAL;
1112 goto out;
1113 }
1114
1115 if (t->type == (unsigned int)-1) {
1116 ha_alert("parsing [%s:%d] : %s: missing type.\n",
1117 file, linenum, args[0]);
1118 err_code |= ERR_ALERT | ERR_FATAL;
1119 goto out;
1120 }
1121
1122 out:
1123 return err_code;
1124}
1125
Willy Tarreau8fed9032014-07-03 17:02:46 +02001126/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001127 * Note that the sample *is* modified and that the returned key may point
1128 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +02001129 * Returns NULL if the sample could not be converted (eg: no matching type),
1130 * otherwise a pointer to the static stktable_key filled with what is needed
1131 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001132 */
Willy Tarreau8fed9032014-07-03 17:02:46 +02001133struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001134{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001135 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001136 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +02001137 return NULL;
1138
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001139 /* Fill static_table_key. */
1140 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +02001141
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001142 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +02001143 static_table_key.key = &smp->data.u.ipv4;
1144 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001145 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001146
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001147 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +02001148 static_table_key.key = &smp->data.u.ipv6;
1149 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001150 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001151
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001152 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001153 /* The stick table require a 32bit unsigned int, "sint" is a
1154 * signed 64 it, so we can convert it inplace.
1155 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001156 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001157 static_table_key.key = &smp->data.u.sint;
1158 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001159 break;
1160
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001161 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001162 if (!smp_make_safe(smp))
1163 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001164 static_table_key.key = smp->data.u.str.area;
1165 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001166 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001167
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001168 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001169 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001170 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001171 if (!smp_make_rw(smp))
1172 return NULL;
1173
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001174 if (smp->data.u.str.size < t->key_size)
1175 if (!smp_dup(smp))
1176 return NULL;
1177 if (smp->data.u.str.size < t->key_size)
1178 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001179 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1180 t->key_size - smp->data.u.str.data);
1181 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001182 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001183 static_table_key.key = smp->data.u.str.area;
1184 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001185 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001186
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001187 default: /* impossible case. */
1188 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001189 }
1190
Christopher Fauletca20d022017-08-29 15:30:31 +02001191 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001192}
1193
1194/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001195 * Process a fetch + format conversion as defined by the sample expression <expr>
1196 * on request or response considering the <opt> parameter. Returns either NULL if
1197 * no key could be extracted, or a pointer to the converted result stored in
1198 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1199 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001200 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1201 * without SMP_OPT_FINAL). The output will be usable like this :
1202 *
1203 * return MAY_CHANGE FINAL Meaning for the sample
1204 * NULL 0 * Not present and will never be (eg: header)
1205 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1206 * NULL 1 1 Not present, will not change anymore
1207 * smp 0 * Present and will not change (eg: header)
1208 * smp 1 0 not possible
1209 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001210 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001211struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001212 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1213{
1214 if (smp)
1215 memset(smp, 0, sizeof(*smp));
1216
Willy Tarreau192252e2015-04-04 01:47:55 +02001217 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001218 if (!smp)
1219 return NULL;
1220
1221 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1222 return NULL; /* we can only use stable samples */
1223
1224 return smp_to_stkey(smp, t);
1225}
1226
1227/*
Willy Tarreau12785782012-04-27 21:37:17 +02001228 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001229 * type <table_type>, otherwise zero. Used in configuration check.
1230 */
Willy Tarreau12785782012-04-27 21:37:17 +02001231int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001232{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001233 int out_type;
1234
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001235 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001236 return 0;
1237
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001238 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001239
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001240 /* Convert sample. */
1241 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001242 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001243
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001244 return 1;
1245}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001246
Willy Tarreauedee1d62014-07-15 16:44:27 +02001247/* Extra data types processing : after the last one, some room may remain
1248 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1249 * at run time.
1250 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001251struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001252 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001253 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001254 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001255 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001256 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1257 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001258 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT, .is_local = 1 },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001259 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1260 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1261 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1262 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1263 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1264 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1265 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1266 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1267 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1268 [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 +01001269 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1270 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001271 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001272 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1273 [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 +02001274 [STKTABLE_DT_GPT] = { .name = "gpt", .std_type = STD_T_UINT, .is_array = 1 },
Emeric Brun4d7ada82021-06-30 19:04:16 +02001275 [STKTABLE_DT_GPC] = { .name = "gpc", .std_type = STD_T_UINT, .is_array = 1 },
1276 [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 +02001277};
1278
Willy Tarreauedee1d62014-07-15 16:44:27 +02001279/* Registers stick-table extra data type with index <idx>, name <name>, type
1280 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1281 * index is automatically allocated. The allocated index is returned, or -1 if
1282 * no free index was found or <name> was already registered. The <name> is used
1283 * directly as a pointer, so if it's not stable, the caller must allocate it.
1284 */
1285int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1286{
1287 if (idx < 0) {
1288 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1289 if (!stktable_data_types[idx].name)
1290 break;
1291
1292 if (strcmp(stktable_data_types[idx].name, name) == 0)
1293 return -1;
1294 }
1295 }
1296
1297 if (idx >= STKTABLE_DATA_TYPES)
1298 return -1;
1299
1300 if (stktable_data_types[idx].name != NULL)
1301 return -1;
1302
1303 stktable_data_types[idx].name = name;
1304 stktable_data_types[idx].std_type = std_type;
1305 stktable_data_types[idx].arg_type = arg_type;
1306 return idx;
1307}
1308
Willy Tarreau08d5f982010-06-06 13:34:54 +02001309/*
1310 * Returns the data type number for the stktable_data_type whose name is <name>,
1311 * or <0 if not found.
1312 */
1313int stktable_get_data_type(char *name)
1314{
1315 int type;
1316
1317 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001318 if (!stktable_data_types[type].name)
1319 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001320 if (strcmp(name, stktable_data_types[type].name) == 0)
1321 return type;
1322 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001323 /* For backwards compatibility */
1324 if (strcmp(name, "server_name") == 0)
1325 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001326 return -1;
1327}
1328
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001329/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1330 * it up into this table. Returns true if found, false otherwise. The input
1331 * type is STR so that input samples are converted to string (since all types
1332 * can be converted to strings), then the function casts the string again into
1333 * the table's type. This is a double conversion, but in the future we might
1334 * support automatic input types to perform the cast on the fly.
1335 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001336static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001337{
1338 struct stktable *t;
1339 struct stktable_key *key;
1340 struct stksess *ts;
1341
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001342 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001343
1344 key = smp_to_stkey(smp, t);
1345 if (!key)
1346 return 0;
1347
1348 ts = stktable_lookup_key(t, key);
1349
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001350 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001351 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001352 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001353 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001354 return 1;
1355}
1356
1357/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1358 * it up into this table. Returns the data rate received from clients in bytes/s
1359 * if the key is present in the table, otherwise zero, so that comparisons can
1360 * be easily performed. If the inspected parameter is not stored in the table,
1361 * <not found> is returned.
1362 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001363static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001364{
1365 struct stktable *t;
1366 struct stktable_key *key;
1367 struct stksess *ts;
1368 void *ptr;
1369
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001370 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001371
1372 key = smp_to_stkey(smp, t);
1373 if (!key)
1374 return 0;
1375
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001376 ts = stktable_lookup_key(t, key);
1377
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001378 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001379 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001380 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001381
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001382 if (!ts) /* key not present */
1383 return 1;
1384
1385 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001386 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001387 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001388 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001389
Daniel Corbett3e60b112018-05-27 09:47:12 -04001390 stktable_release(t, ts);
1391 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001392}
1393
1394/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1395 * it up into this table. Returns the cumulated number of connections for the key
1396 * if the key is present in the table, otherwise zero, so that comparisons can
1397 * be easily performed. If the inspected parameter is not stored in the table,
1398 * <not found> is returned.
1399 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001400static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001401{
1402 struct stktable *t;
1403 struct stktable_key *key;
1404 struct stksess *ts;
1405 void *ptr;
1406
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001407 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001408
1409 key = smp_to_stkey(smp, t);
1410 if (!key)
1411 return 0;
1412
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001413 ts = stktable_lookup_key(t, key);
1414
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001415 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001416 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001417 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001418
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001419 if (!ts) /* key not present */
1420 return 1;
1421
1422 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001423 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001424 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001425
Daniel Corbett3e60b112018-05-27 09:47:12 -04001426 stktable_release(t, ts);
1427 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001428}
1429
1430/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1431 * it up into this table. Returns the number of concurrent connections for the
1432 * key if the key is present in the table, otherwise zero, so that comparisons
1433 * can be easily performed. If the inspected parameter is not stored in the
1434 * table, <not found> is returned.
1435 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001436static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001437{
1438 struct stktable *t;
1439 struct stktable_key *key;
1440 struct stksess *ts;
1441 void *ptr;
1442
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001443 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001444
1445 key = smp_to_stkey(smp, t);
1446 if (!key)
1447 return 0;
1448
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001449 ts = stktable_lookup_key(t, key);
1450
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001451 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001452 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001453 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001454
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001455 if (!ts) /* key not present */
1456 return 1;
1457
1458 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001459 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001460 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001461
Daniel Corbett3e60b112018-05-27 09:47:12 -04001462 stktable_release(t, ts);
1463 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001464}
1465
1466/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1467 * it up into this table. Returns the rate of incoming connections from the key
1468 * if the key is present in the table, otherwise zero, so that comparisons can
1469 * be easily performed. If the inspected parameter is not stored in the table,
1470 * <not found> is returned.
1471 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001472static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001473{
1474 struct stktable *t;
1475 struct stktable_key *key;
1476 struct stksess *ts;
1477 void *ptr;
1478
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001479 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001480
1481 key = smp_to_stkey(smp, t);
1482 if (!key)
1483 return 0;
1484
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001485 ts = stktable_lookup_key(t, key);
1486
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001487 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001488 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001489 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001490
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001491 if (!ts) /* key not present */
1492 return 1;
1493
1494 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001495 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001496 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001497 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001498
Daniel Corbett3e60b112018-05-27 09:47:12 -04001499 stktable_release(t, ts);
1500 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001501}
1502
1503/* 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 +02001504 * it up into this table. Returns the expiration delay for the key if the key is
1505 * present in the table, otherwise the default value provided as second argument
1506 * if any, if not (no default value), <not found> is returned.
1507 */
1508static int sample_conv_table_expire(const struct arg *arg_p, struct sample *smp, void *private)
1509{
1510 struct stktable *t;
1511 struct stktable_key *key;
1512 struct stksess *ts;
1513
1514 t = arg_p[0].data.t;
1515
1516 key = smp_to_stkey(smp, t);
1517 if (!key)
1518 return 0;
1519
1520 ts = stktable_lookup_key(t, key);
1521
1522 smp->flags = SMP_F_VOL_TEST;
1523 smp->data.type = SMP_T_SINT;
1524 smp->data.u.sint = 0;
1525
1526 if (!ts) { /* key not present */
1527 if (arg_p[1].type == ARGT_STOP)
1528 return 0;
1529
1530 /* default value */
1531 smp->data.u.sint = arg_p[1].data.sint;
1532 return 1;
1533 }
1534
1535 smp->data.u.sint = tick_remain(now_ms, ts->expire);
1536
1537 stktable_release(t, ts);
1538 return 1;
1539}
1540
1541/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1542 * it up into this table. Returns the time the key remains unused if the key is
1543 * present in the table, otherwise the default value provided as second argument
1544 * if any, if not (no default value), <not found> is returned.
1545 */
1546static int sample_conv_table_idle(const struct arg *arg_p, struct sample *smp, void *private)
1547{
1548 struct stktable *t;
1549 struct stktable_key *key;
1550 struct stksess *ts;
1551
1552 t = arg_p[0].data.t;
1553
1554 key = smp_to_stkey(smp, t);
1555 if (!key)
1556 return 0;
1557
1558 ts = stktable_lookup_key(t, key);
1559
1560 smp->flags = SMP_F_VOL_TEST;
1561 smp->data.type = SMP_T_SINT;
1562 smp->data.u.sint = 0;
1563
1564 if (!ts) { /* key not present */
1565 if (arg_p[1].type == ARGT_STOP)
1566 return 0;
1567
1568 /* default value */
1569 smp->data.u.sint = arg_p[1].data.sint;
1570 return 1;
1571 }
1572
1573 smp->data.u.sint = tick_remain(tick_remain(now_ms, ts->expire), t->expire);
1574
1575 stktable_release(t, ts);
1576 return 1;
1577}
1578
1579/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001580 * it up into this table. Returns the data rate sent to clients in bytes/s
1581 * if the key is present in the table, otherwise zero, so that comparisons can
1582 * be easily performed. If the inspected parameter is not stored in the table,
1583 * <not found> is returned.
1584 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001585static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001586{
1587 struct stktable *t;
1588 struct stktable_key *key;
1589 struct stksess *ts;
1590 void *ptr;
1591
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001592 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001593
1594 key = smp_to_stkey(smp, t);
1595 if (!key)
1596 return 0;
1597
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001598 ts = stktable_lookup_key(t, key);
1599
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001600 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001601 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001602 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001603
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001604 if (!ts) /* key not present */
1605 return 1;
1606
1607 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001608 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001609 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001610 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001611
Daniel Corbett3e60b112018-05-27 09:47:12 -04001612 stktable_release(t, ts);
1613 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001614}
1615
Emeric Brun877b0b52021-06-30 18:57:49 +02001616/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1617 * it up into this table. Returns the value of the GPT[arg_p(0)] tag for the key
1618 * if the key is present in the table, otherwise false, so that comparisons can
1619 * be easily performed. If the inspected parameter is not stored in the table,
1620 * <not found> is returned.
1621 */
1622static int sample_conv_table_gpt(const struct arg *arg_p, struct sample *smp, void *private)
1623{
1624 struct stktable *t;
1625 struct stktable_key *key;
1626 struct stksess *ts;
1627 void *ptr;
1628 unsigned int idx;
1629
1630 idx = arg_p[0].data.sint;
1631
1632 t = arg_p[1].data.t;
1633
1634 key = smp_to_stkey(smp, t);
1635 if (!key)
1636 return 0;
1637
1638 ts = stktable_lookup_key(t, key);
1639
1640 smp->flags = SMP_F_VOL_TEST;
1641 smp->data.type = SMP_T_SINT;
1642 smp->data.u.sint = 0;
1643
1644 if (!ts) /* key not present */
1645 return 1;
1646
1647 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, idx);
1648 if (ptr)
1649 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1650
1651 stktable_release(t, ts);
1652 return !!ptr;
1653}
1654
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001655/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001656 * it up into this table. Returns the value of the GPT0 tag for the key
1657 * if the key is present in the table, otherwise false, so that comparisons can
1658 * be easily performed. If the inspected parameter is not stored in the table,
1659 * <not found> is returned.
1660 */
1661static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1662{
1663 struct stktable *t;
1664 struct stktable_key *key;
1665 struct stksess *ts;
1666 void *ptr;
1667
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001668 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001669
1670 key = smp_to_stkey(smp, t);
1671 if (!key)
1672 return 0;
1673
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001674 ts = stktable_lookup_key(t, key);
1675
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001676 smp->flags = SMP_F_VOL_TEST;
1677 smp->data.type = SMP_T_SINT;
1678 smp->data.u.sint = 0;
1679
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001680 if (!ts) /* key not present */
1681 return 1;
1682
1683 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001684 if (!ptr)
1685 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, 0);
1686
Daniel Corbett3e60b112018-05-27 09:47:12 -04001687 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001688 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001689
Daniel Corbett3e60b112018-05-27 09:47:12 -04001690 stktable_release(t, ts);
1691 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001692}
1693
Emeric Brun4d7ada82021-06-30 19:04:16 +02001694/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1695 * it up into this table. Returns the value of the GPC[arg_p(0)] counter for the key
1696 * if the key is present in the table, otherwise zero, so that comparisons can
1697 * be easily performed. If the inspected parameter is not stored in the table,
1698 * <not found> is returned.
1699 */
1700static int sample_conv_table_gpc(const struct arg *arg_p, struct sample *smp, void *private)
1701{
1702 struct stktable *t;
1703 struct stktable_key *key;
1704 struct stksess *ts;
1705 void *ptr;
1706 unsigned int idx;
1707
1708 idx = arg_p[0].data.sint;
1709
1710 t = arg_p[1].data.t;
1711
1712 key = smp_to_stkey(smp, t);
1713 if (!key)
1714 return 0;
1715
1716 ts = stktable_lookup_key(t, key);
1717
1718 smp->flags = SMP_F_VOL_TEST;
1719 smp->data.type = SMP_T_SINT;
1720 smp->data.u.sint = 0;
1721
1722 if (!ts) /* key not present */
1723 return 1;
1724
1725 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, idx);
1726 if (ptr)
1727 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1728
1729 stktable_release(t, ts);
1730 return !!ptr;
1731}
1732
1733/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1734 * it up into this table. Returns the event rate of the GPC[arg_p(0)] counter
1735 * for the key if the key is present in the table, otherwise zero, so that
1736 * comparisons can be easily performed. If the inspected parameter is not
1737 * stored in the table, <not found> is returned.
1738 */
1739static int sample_conv_table_gpc_rate(const struct arg *arg_p, struct sample *smp, void *private)
1740{
1741 struct stktable *t;
1742 struct stktable_key *key;
1743 struct stksess *ts;
1744 void *ptr;
1745 unsigned int idx;
1746
1747 idx = arg_p[0].data.sint;
1748
1749 t = arg_p[1].data.t;
1750
1751 key = smp_to_stkey(smp, t);
1752 if (!key)
1753 return 0;
1754
1755 ts = stktable_lookup_key(t, key);
1756
1757 smp->flags = SMP_F_VOL_TEST;
1758 smp->data.type = SMP_T_SINT;
1759 smp->data.u.sint = 0;
1760
1761 if (!ts) /* key not present */
1762 return 1;
1763
1764 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, idx);
1765 if (ptr)
1766 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1767 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1768
1769 stktable_release(t, ts);
1770 return !!ptr;
1771}
1772
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001773/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001774 * it up into this table. Returns the value of the GPC0 counter for the key
1775 * if the key is present in the table, otherwise zero, so that comparisons can
1776 * be easily performed. If the inspected parameter is not stored in the table,
1777 * <not found> is returned.
1778 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001779static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001780{
1781 struct stktable *t;
1782 struct stktable_key *key;
1783 struct stksess *ts;
1784 void *ptr;
1785
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001786 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001787
1788 key = smp_to_stkey(smp, t);
1789 if (!key)
1790 return 0;
1791
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001792 ts = stktable_lookup_key(t, key);
1793
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001794 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001795 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001796 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001797
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001798 if (!ts) /* key not present */
1799 return 1;
1800
1801 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02001802 if (!ptr) {
1803 /* fallback on the gpc array */
1804 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 0);
1805 }
1806
Daniel Corbett3e60b112018-05-27 09:47:12 -04001807 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001808 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001809
Daniel Corbett3e60b112018-05-27 09:47:12 -04001810 stktable_release(t, ts);
1811 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001812}
1813
1814/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1815 * it up into this table. Returns the event rate of the GPC0 counter for the key
1816 * if the key is present in the table, otherwise zero, so that comparisons can
1817 * be easily performed. If the inspected parameter is not stored in the table,
1818 * <not found> is returned.
1819 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001820static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001821{
1822 struct stktable *t;
1823 struct stktable_key *key;
1824 struct stksess *ts;
1825 void *ptr;
1826
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001827 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001828
1829 key = smp_to_stkey(smp, t);
1830 if (!key)
1831 return 0;
1832
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001833 ts = stktable_lookup_key(t, key);
1834
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001835 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001836 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001837 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001838
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001839 if (!ts) /* key not present */
1840 return 1;
1841
1842 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001843 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001844 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001845 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001846 else {
1847 /* fallback on the gpc array */
1848 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 0);
1849 if (ptr)
1850 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1851 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1852 }
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001853
Daniel Corbett3e60b112018-05-27 09:47:12 -04001854 stktable_release(t, ts);
1855 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001856}
1857
1858/* 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 +01001859 * it up into this table. Returns the value of the GPC1 counter for the key
1860 * if the key is present in the table, otherwise zero, so that comparisons can
1861 * be easily performed. If the inspected parameter is not stored in the table,
1862 * <not found> is returned.
1863 */
1864static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1865{
1866 struct stktable *t;
1867 struct stktable_key *key;
1868 struct stksess *ts;
1869 void *ptr;
1870
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001871 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001872
1873 key = smp_to_stkey(smp, t);
1874 if (!key)
1875 return 0;
1876
1877 ts = stktable_lookup_key(t, key);
1878
1879 smp->flags = SMP_F_VOL_TEST;
1880 smp->data.type = SMP_T_SINT;
1881 smp->data.u.sint = 0;
1882
1883 if (!ts) /* key not present */
1884 return 1;
1885
1886 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02001887 if (!ptr) {
1888 /* fallback on the gpc array */
1889 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 1);
1890 }
1891
Daniel Corbett3e60b112018-05-27 09:47:12 -04001892 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001893 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001894
Daniel Corbett3e60b112018-05-27 09:47:12 -04001895 stktable_release(t, ts);
1896 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001897}
1898
1899/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1900 * it up into this table. Returns the event rate of the GPC1 counter for the key
1901 * if the key is present in the table, otherwise zero, so that comparisons can
1902 * be easily performed. If the inspected parameter is not stored in the table,
1903 * <not found> is returned.
1904 */
1905static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1906{
1907 struct stktable *t;
1908 struct stktable_key *key;
1909 struct stksess *ts;
1910 void *ptr;
1911
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001912 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001913
1914 key = smp_to_stkey(smp, t);
1915 if (!key)
1916 return 0;
1917
1918 ts = stktable_lookup_key(t, key);
1919
1920 smp->flags = SMP_F_VOL_TEST;
1921 smp->data.type = SMP_T_SINT;
1922 smp->data.u.sint = 0;
1923
1924 if (!ts) /* key not present */
1925 return 1;
1926
1927 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001928 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001929 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001930 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001931 else {
1932 /* fallback on the gpc array */
1933 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 1);
1934 if (ptr)
1935 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1936 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1937 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001938
Daniel Corbett3e60b112018-05-27 09:47:12 -04001939 stktable_release(t, ts);
1940 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001941}
1942
1943/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001944 * it up into this table. Returns the cumulated number of HTTP request errors
1945 * for the key if the key is present in the table, otherwise zero, so that
1946 * comparisons can be easily performed. If the inspected parameter is not stored
1947 * in the table, <not found> is returned.
1948 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001949static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001950{
1951 struct stktable *t;
1952 struct stktable_key *key;
1953 struct stksess *ts;
1954 void *ptr;
1955
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001956 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001957
1958 key = smp_to_stkey(smp, t);
1959 if (!key)
1960 return 0;
1961
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001962 ts = stktable_lookup_key(t, key);
1963
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001964 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001965 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001966 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001967
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001968 if (!ts) /* key not present */
1969 return 1;
1970
1971 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001972 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001973 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001974
Daniel Corbett3e60b112018-05-27 09:47:12 -04001975 stktable_release(t, ts);
1976 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001977}
1978
1979/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1980 * it up into this table. Returns the HTTP request error rate the key
1981 * if the key is present in the table, otherwise zero, so that comparisons can
1982 * be easily performed. If the inspected parameter is not stored in the table,
1983 * <not found> is returned.
1984 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001985static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001986{
1987 struct stktable *t;
1988 struct stktable_key *key;
1989 struct stksess *ts;
1990 void *ptr;
1991
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001992 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001993
1994 key = smp_to_stkey(smp, t);
1995 if (!key)
1996 return 0;
1997
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001998 ts = stktable_lookup_key(t, key);
1999
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002000 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002001 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002002 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002003
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002004 if (!ts) /* key not present */
2005 return 1;
2006
2007 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002008 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002009 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002010 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002011
Daniel Corbett3e60b112018-05-27 09:47:12 -04002012 stktable_release(t, ts);
2013 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002014}
2015
2016/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002017 * it up into this table. Returns the cumulated number of HTTP response failures
2018 * for the key if the key is present in the table, otherwise zero, so that
2019 * comparisons can be easily performed. If the inspected parameter is not stored
2020 * in the table, <not found> is returned.
2021 */
2022static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
2023{
2024 struct stktable *t;
2025 struct stktable_key *key;
2026 struct stksess *ts;
2027 void *ptr;
2028
2029 t = arg_p[0].data.t;
2030
2031 key = smp_to_stkey(smp, t);
2032 if (!key)
2033 return 0;
2034
2035 ts = stktable_lookup_key(t, key);
2036
2037 smp->flags = SMP_F_VOL_TEST;
2038 smp->data.type = SMP_T_SINT;
2039 smp->data.u.sint = 0;
2040
2041 if (!ts) /* key not present */
2042 return 1;
2043
2044 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2045 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002046 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002047
2048 stktable_release(t, ts);
2049 return !!ptr;
2050}
2051
2052/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2053 * it up into this table. Returns the HTTP response failure rate for the key
2054 * if the key is present in the table, otherwise zero, so that comparisons can
2055 * be easily performed. If the inspected parameter is not stored in the table,
2056 * <not found> is returned.
2057 */
2058static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
2059{
2060 struct stktable *t;
2061 struct stktable_key *key;
2062 struct stksess *ts;
2063 void *ptr;
2064
2065 t = arg_p[0].data.t;
2066
2067 key = smp_to_stkey(smp, t);
2068 if (!key)
2069 return 0;
2070
2071 ts = stktable_lookup_key(t, key);
2072
2073 smp->flags = SMP_F_VOL_TEST;
2074 smp->data.type = SMP_T_SINT;
2075 smp->data.u.sint = 0;
2076
2077 if (!ts) /* key not present */
2078 return 1;
2079
2080 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2081 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002082 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002083 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
2084
2085 stktable_release(t, ts);
2086 return !!ptr;
2087}
2088
2089/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002090 * it up into this table. Returns the cumulated number of HTTP request for the
2091 * key if the key is present in the table, otherwise zero, so that comparisons
2092 * can be easily performed. If the inspected parameter is not stored in the
2093 * table, <not found> is returned.
2094 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002095static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002096{
2097 struct stktable *t;
2098 struct stktable_key *key;
2099 struct stksess *ts;
2100 void *ptr;
2101
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002102 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002103
2104 key = smp_to_stkey(smp, t);
2105 if (!key)
2106 return 0;
2107
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002108 ts = stktable_lookup_key(t, key);
2109
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002110 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002111 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002112 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002113
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002114 if (!ts) /* key not present */
2115 return 1;
2116
2117 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002118 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002119 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002120
Daniel Corbett3e60b112018-05-27 09:47:12 -04002121 stktable_release(t, ts);
2122 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002123}
2124
2125/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2126 * it up into this table. Returns the HTTP request rate the key if the key is
2127 * present in the table, otherwise zero, so that comparisons can be easily
2128 * performed. If the inspected parameter is not stored in the table, <not found>
2129 * is returned.
2130 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002131static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002132{
2133 struct stktable *t;
2134 struct stktable_key *key;
2135 struct stksess *ts;
2136 void *ptr;
2137
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002138 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002139
2140 key = smp_to_stkey(smp, t);
2141 if (!key)
2142 return 0;
2143
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002144 ts = stktable_lookup_key(t, key);
2145
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002146 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002147 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002148 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002149
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002150 if (!ts) /* key not present */
2151 return 1;
2152
2153 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002154 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002155 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002156 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002157
Daniel Corbett3e60b112018-05-27 09:47:12 -04002158 stktable_release(t, ts);
2159 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002160}
2161
2162/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2163 * it up into this table. Returns the volume of datareceived from clients in kbytes
2164 * if the key is present in the table, otherwise zero, so that comparisons can
2165 * be easily performed. If the inspected parameter is not stored in the table,
2166 * <not found> is returned.
2167 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002168static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002169{
2170 struct stktable *t;
2171 struct stktable_key *key;
2172 struct stksess *ts;
2173 void *ptr;
2174
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002175 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002176
2177 key = smp_to_stkey(smp, t);
2178 if (!key)
2179 return 0;
2180
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002181 ts = stktable_lookup_key(t, key);
2182
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002183 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002184 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002185 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002186
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002187 if (!ts) /* key not present */
2188 return 1;
2189
2190 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002191 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002192 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002193
Daniel Corbett3e60b112018-05-27 09:47:12 -04002194 stktable_release(t, ts);
2195 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002196}
2197
2198/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2199 * it up into this table. Returns the volume of data sent to clients in kbytes
2200 * if the key is present in the table, otherwise zero, so that comparisons can
2201 * be easily performed. If the inspected parameter is not stored in the table,
2202 * <not found> is returned.
2203 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002204static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002205{
2206 struct stktable *t;
2207 struct stktable_key *key;
2208 struct stksess *ts;
2209 void *ptr;
2210
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002211 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002212
2213 key = smp_to_stkey(smp, t);
2214 if (!key)
2215 return 0;
2216
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002217 ts = stktable_lookup_key(t, key);
2218
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002219 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002220 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002221 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002222
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002223 if (!ts) /* key not present */
2224 return 1;
2225
2226 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002227 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002228 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002229
Daniel Corbett3e60b112018-05-27 09:47:12 -04002230 stktable_release(t, ts);
2231 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002232}
2233
2234/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2235 * it up into this table. Returns the server ID associated with the key if the
2236 * key is present in the table, otherwise zero, so that comparisons can be
2237 * easily performed. If the inspected parameter is not stored in the table,
2238 * <not found> is returned.
2239 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002240static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002241{
2242 struct stktable *t;
2243 struct stktable_key *key;
2244 struct stksess *ts;
2245 void *ptr;
2246
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002247 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002248
2249 key = smp_to_stkey(smp, t);
2250 if (!key)
2251 return 0;
2252
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002253 ts = stktable_lookup_key(t, key);
2254
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002255 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002256 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002257 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002258
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002259 if (!ts) /* key not present */
2260 return 1;
2261
2262 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002263 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002264 smp->data.u.sint = stktable_data_cast(ptr, std_t_sint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002265
Daniel Corbett3e60b112018-05-27 09:47:12 -04002266 stktable_release(t, ts);
2267 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002268}
2269
2270/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2271 * it up into this table. Returns the cumulated number of sessions for the
2272 * key if the key is present in the table, otherwise zero, so that comparisons
2273 * can be easily performed. If the inspected parameter is not stored in the
2274 * table, <not found> is returned.
2275 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002276static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002277{
2278 struct stktable *t;
2279 struct stktable_key *key;
2280 struct stksess *ts;
2281 void *ptr;
2282
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002283 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002284
2285 key = smp_to_stkey(smp, t);
2286 if (!key)
2287 return 0;
2288
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002289 ts = stktable_lookup_key(t, key);
2290
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002291 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002292 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002293 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002294
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002295 if (!ts) /* key not present */
2296 return 1;
2297
2298 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002299 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002300 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002301
Daniel Corbett3e60b112018-05-27 09:47:12 -04002302 stktable_release(t, ts);
2303 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002304}
2305
2306/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2307 * it up into this table. Returns the session rate the key if the key is
2308 * present in the table, otherwise zero, so that comparisons can be easily
2309 * performed. If the inspected parameter is not stored in the table, <not found>
2310 * is returned.
2311 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002312static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002313{
2314 struct stktable *t;
2315 struct stktable_key *key;
2316 struct stksess *ts;
2317 void *ptr;
2318
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002319 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002320
2321 key = smp_to_stkey(smp, t);
2322 if (!key)
2323 return 0;
2324
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002325 ts = stktable_lookup_key(t, key);
2326
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002327 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002328 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002329 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002330
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002331 if (!ts) /* key not present */
2332 return 1;
2333
2334 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002335 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002336 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002337 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002338
Daniel Corbett3e60b112018-05-27 09:47:12 -04002339 stktable_release(t, ts);
2340 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002341}
2342
2343/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2344 * it up into this table. Returns the amount of concurrent connections tracking
2345 * the same key if the key is present in the table, otherwise zero, so that
2346 * comparisons can be easily performed. If the inspected parameter is not
2347 * stored in the table, <not found> is returned.
2348 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002349static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002350{
2351 struct stktable *t;
2352 struct stktable_key *key;
2353 struct stksess *ts;
2354
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002355 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002356
2357 key = smp_to_stkey(smp, t);
2358 if (!key)
2359 return 0;
2360
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002361 ts = stktable_lookup_key(t, key);
2362
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002363 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002364 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002365 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002366
Tim Duesterhus65189c12018-06-26 15:57:29 +02002367 if (!ts)
2368 return 1;
2369
2370 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002371
Daniel Corbett3e60b112018-05-27 09:47:12 -04002372 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002373 return 1;
2374}
2375
Emeric Brun4d7ada82021-06-30 19:04:16 +02002376/* This function increments the gpc counter at index 'rule->arg.gpc.idx' of the
2377 * array on the tracksc counter of index 'rule->arg.gpc.sc' stored into the
2378 * <stream> or directly in the session <sess> if <stream> is set to NULL
2379 *
2380 * This function always returns ACT_RET_CONT and parameter flags is unused.
2381 */
2382static enum act_return action_inc_gpc(struct act_rule *rule, struct proxy *px,
2383 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002384{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002385 struct stksess *ts;
2386 struct stkctr *stkctr;
2387
2388 /* Extract the stksess, return OK if no stksess available. */
2389 if (s)
2390 stkctr = &s->stkctr[rule->arg.gpc.sc];
2391 else
2392 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002393
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002394 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002395 if (ts) {
2396 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002397
Emeric Brun4d7ada82021-06-30 19:04:16 +02002398 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2399 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2400 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2401
Emeric Brun819fc6f2017-06-13 19:37:32 +02002402 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002403 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002404
2405 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002406 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun4d7ada82021-06-30 19:04:16 +02002407 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002408
Emeric Brun819fc6f2017-06-13 19:37:32 +02002409 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002410 stktable_data_cast(ptr2, std_t_uint)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002411
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002412 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002413
2414 /* If data was modified, we need to touch to re-schedule sync */
2415 stktable_touch_local(stkctr->table, ts, 0);
2416 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002417 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002418 return ACT_RET_CONT;
2419}
2420
Emeric Brun4d7ada82021-06-30 19:04:16 +02002421/* Same as action_inc_gpc() but for gpc0 only */
2422static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
2423 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002424{
Emeric Brun4d7ada82021-06-30 19:04:16 +02002425 struct stksess *ts;
2426 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002427 unsigned int period = 0;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002428
Emeric Brun4d7ada82021-06-30 19:04:16 +02002429 /* Extract the stksess, return OK if no stksess available. */
2430 if (s)
2431 stkctr = &s->stkctr[rule->arg.gpc.sc];
2432 else
2433 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002434
Emeric Brun4d7ada82021-06-30 19:04:16 +02002435 ts = stkctr_entry(stkctr);
2436 if (ts) {
2437 void *ptr1, *ptr2;
2438
2439 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2440 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002441 if (ptr1) {
2442 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
2443 }
2444 else {
2445 /* fallback on the gpc array */
2446 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 0);
2447 if (ptr1)
2448 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2449 }
2450
Emeric Brun4d7ada82021-06-30 19:04:16 +02002451 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02002452 if (!ptr2) {
2453 /* fallback on the gpc array */
2454 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 0);
2455 }
2456
Emeric Brun4d7ada82021-06-30 19:04:16 +02002457 if (ptr1 || ptr2) {
2458 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2459
2460 if (ptr1)
2461 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002462 period, 1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002463
2464 if (ptr2)
2465 stktable_data_cast(ptr2, std_t_uint)++;
2466
2467 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2468
2469 /* If data was modified, we need to touch to re-schedule sync */
2470 stktable_touch_local(stkctr->table, ts, 0);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002471 }
2472 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002473 return ACT_RET_CONT;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002474}
2475
Emeric Brun4d7ada82021-06-30 19:04:16 +02002476/* Same as action_inc_gpc() but for gpc1 only */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002477static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2478 struct session *sess, struct stream *s, int flags)
2479{
2480 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002481 struct stkctr *stkctr = NULL;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002482 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002483
2484 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002485 if (s && s->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002486 stkctr = &s->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002487 else if (sess->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002488 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002489 else
2490 return ACT_RET_CONT;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002491
2492 ts = stkctr_entry(stkctr);
2493 if (ts) {
2494 void *ptr1, *ptr2;
2495
2496 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2497 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002498 if (ptr1) {
2499 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
2500 }
2501 else {
2502 /* fallback on the gpc array */
2503 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 1);
2504 if (ptr1)
2505 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2506 }
2507
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002508 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02002509 if (!ptr2) {
2510 /* fallback on the gpc array */
2511 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 1);
2512 }
2513
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002514 if (ptr1 || ptr2) {
2515 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2516
2517 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002518 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002519 period, 1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002520
2521 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002522 stktable_data_cast(ptr2, std_t_uint)++;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002523
2524 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2525
2526 /* If data was modified, we need to touch to re-schedule sync */
2527 stktable_touch_local(stkctr->table, ts, 0);
2528 }
2529 }
2530 return ACT_RET_CONT;
2531}
2532
Emeric Brun4d7ada82021-06-30 19:04:16 +02002533/* This function is a common parser for actions incrementing the GPC
2534 * (General Purpose Counters). It understands the formats:
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002535 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002536 * sc-inc-gpc(<gpc IDX>,<track ID>)
2537 * sc-inc-gpc0([<track ID>])
2538 * sc-inc-gpc1([<track ID>])
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002539 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002540 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
2541 * message. Otherwise it returns ACT_RET_PRS_OK.
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002542 */
Emeric Brun4d7ada82021-06-30 19:04:16 +02002543static enum act_parse_ret parse_inc_gpc(const char **args, int *arg, struct proxy *px,
2544 struct act_rule *rule, char **err)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002545{
2546 const char *cmd_name = args[*arg-1];
2547 char *error;
2548
Willy Tarreau6c011712023-01-06 16:09:58 +01002549 if (!global.tune.nb_stk_ctr) {
2550 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2551 return ACT_RET_PRS_ERR;
2552 }
2553
Emeric Brun4d7ada82021-06-30 19:04:16 +02002554 cmd_name += strlen("sc-inc-gpc");
2555 if (*cmd_name == '(') {
2556 cmd_name++; /* skip the '(' */
2557 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2558 if (*error != ',') {
2559 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 +01002560 return ACT_RET_PRS_ERR;
2561 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002562 else {
2563 cmd_name = error + 1; /* skip the ',' */
2564 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2565 if (*error != ')') {
2566 memprintf(err, "invalid stick table track ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2567 return ACT_RET_PRS_ERR;
2568 }
2569
Willy Tarreau6c011712023-01-06 16:09:58 +01002570 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2571 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d (tune.stick-counters)",
2572 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002573 return ACT_RET_PRS_ERR;
2574 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002575 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002576 rule->action_ptr = action_inc_gpc;
2577 }
2578 else if (*cmd_name == '0' ||*cmd_name == '1') {
2579 char c = *cmd_name;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002580
Emeric Brun4d7ada82021-06-30 19:04:16 +02002581 cmd_name++;
2582 if (*cmd_name == '\0') {
2583 /* default stick table id. */
2584 rule->arg.gpc.sc = 0;
2585 } else {
2586 /* parse the stick table id. */
2587 if (*cmd_name != '(') {
2588 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2589 return ACT_RET_PRS_ERR;
2590 }
2591 cmd_name++; /* jump the '(' */
2592 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2593 if (*error != ')') {
2594 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2595 return ACT_RET_PRS_ERR;
2596 }
2597
Willy Tarreau6c011712023-01-06 16:09:58 +01002598 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2599 memprintf(err, "invalid stick table track ID. The max allowed ID is %d (tune.stick-counters)",
2600 global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002601 return ACT_RET_PRS_ERR;
2602 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002603 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002604 if (c == '1')
2605 rule->action_ptr = action_inc_gpc1;
2606 else
2607 rule->action_ptr = action_inc_gpc0;
2608 }
2609 else {
2610 /* default stick table id. */
Willy Tarreau20391512023-01-02 17:35:50 +01002611 memprintf(err, "invalid gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002612 return ACT_RET_PRS_ERR;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002613 }
2614 rule->action = ACT_CUSTOM;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002615 return ACT_RET_PRS_OK;
2616}
2617
Emeric Brun877b0b52021-06-30 18:57:49 +02002618/* This function sets the gpt at index 'rule->arg.gpt.idx' of the array on the
2619 * tracksc counter of index 'rule->arg.gpt.sc' stored into the <stream> or
2620 * directly in the session <sess> if <stream> is set to NULL. This gpt is
2621 * set to the value computed by the expression 'rule->arg.gpt.expr' or if
2622 * 'rule->arg.gpt.expr' is null directly to the value of 'rule->arg.gpt.value'.
2623 *
2624 * This function always returns ACT_RET_CONT and parameter flags is unused.
2625 */
2626static enum act_return action_set_gpt(struct act_rule *rule, struct proxy *px,
2627 struct session *sess, struct stream *s, int flags)
2628{
2629 void *ptr;
2630 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002631 struct stkctr *stkctr = NULL;
Emeric Brun877b0b52021-06-30 18:57:49 +02002632 unsigned int value = 0;
2633 struct sample *smp;
2634 int smp_opt_dir;
2635
2636 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002637 if (s && s->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002638 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002639 else if (sess->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002640 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002641 else
2642 return ACT_RET_CONT;
Emeric Brun877b0b52021-06-30 18:57:49 +02002643
2644 ts = stkctr_entry(stkctr);
2645 if (!ts)
2646 return ACT_RET_CONT;
2647
2648 /* Store the sample in the required sc, and ignore errors. */
2649 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, rule->arg.gpt.idx);
2650 if (ptr) {
2651
2652 if (!rule->arg.gpt.expr)
2653 value = (unsigned int)(rule->arg.gpt.value);
2654 else {
2655 switch (rule->from) {
2656 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2657 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2658 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2659 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2660 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2661 default:
2662 send_log(px, LOG_ERR, "stick table: internal error while setting gpt%u.", rule->arg.gpt.idx);
2663 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2664 ha_alert("stick table: internal error while executing setting gpt%u.\n", rule->arg.gpt.idx);
2665 return ACT_RET_CONT;
2666 }
2667
2668 /* Fetch and cast the expression. */
2669 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2670 if (!smp) {
2671 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt%u.", rule->arg.gpt.idx);
2672 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2673 ha_alert("stick table: invalid expression or data type while setting gpt%u.\n", rule->arg.gpt.idx);
2674 return ACT_RET_CONT;
2675 }
2676 value = (unsigned int)(smp->data.u.sint);
2677 }
2678
2679 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2680
2681 stktable_data_cast(ptr, std_t_uint) = value;
2682
2683 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2684
2685 stktable_touch_local(stkctr->table, ts, 0);
2686 }
2687
2688 return ACT_RET_CONT;
2689}
2690
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002691/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002692static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002693 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002694{
2695 void *ptr;
2696 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002697 struct stkctr *stkctr = NULL;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002698 unsigned int value = 0;
2699 struct sample *smp;
2700 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002701
2702 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002703 if (s && s->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002704 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002705 else if (sess->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002706 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002707 else
2708 return ACT_RET_CONT;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002709
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002710 ts = stkctr_entry(stkctr);
2711 if (!ts)
2712 return ACT_RET_CONT;
2713
2714 /* Store the sample in the required sc, and ignore errors. */
2715 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02002716 if (!ptr)
2717 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, 0);
2718
Willy Tarreau79c1e912016-01-25 14:54:45 +01002719 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002720 if (!rule->arg.gpt.expr)
2721 value = (unsigned int)(rule->arg.gpt.value);
2722 else {
2723 switch (rule->from) {
2724 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2725 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2726 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2727 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2728 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2729 default:
2730 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2731 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2732 ha_alert("stick table: internal error while executing setting gpt0.\n");
2733 return ACT_RET_CONT;
2734 }
2735
2736 /* Fetch and cast the expression. */
2737 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2738 if (!smp) {
2739 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2740 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2741 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2742 return ACT_RET_CONT;
2743 }
2744 value = (unsigned int)(smp->data.u.sint);
2745 }
2746
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002747 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002748
Emeric Brun0e3457b2021-06-30 17:18:28 +02002749 stktable_data_cast(ptr, std_t_uint) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002750
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002751 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002752
2753 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002754 }
2755
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002756 return ACT_RET_CONT;
2757}
2758
Emeric Brun877b0b52021-06-30 18:57:49 +02002759/* This function is a parser for the "sc-set-gpt" and "sc-set-gpt0" actions.
2760 * It understands the formats:
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002761 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002762 * sc-set-gpt(<gpt IDX>,<track ID>) <expression>
2763 * sc-set-gpt0(<track ID>) <expression>
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002764 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002765 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2766 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpt.expr'
2767 * is filled with the pointer to the expression to execute or NULL if the arg
2768 * is directly an integer stored into 'rule->arg.gpt.value'.
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002769 */
Emeric Brun877b0b52021-06-30 18:57:49 +02002770static enum act_parse_ret parse_set_gpt(const char **args, int *arg, struct proxy *px,
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002771 struct act_rule *rule, char **err)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002772{
2773 const char *cmd_name = args[*arg-1];
2774 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002775 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002776
Willy Tarreau6c011712023-01-06 16:09:58 +01002777 if (!global.tune.nb_stk_ctr) {
2778 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2779 return ACT_RET_PRS_ERR;
2780 }
2781
Emeric Brun877b0b52021-06-30 18:57:49 +02002782 cmd_name += strlen("sc-set-gpt");
2783 if (*cmd_name == '(') {
2784 cmd_name++; /* skip the '(' */
2785 rule->arg.gpt.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2786 if (*error != ',') {
2787 memprintf(err, "Missing gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002788 return ACT_RET_PRS_ERR;
2789 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002790 else {
2791 cmd_name = error + 1; /* skip the ',' */
2792 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2793 if (*error != ')') {
2794 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2795 return ACT_RET_PRS_ERR;
2796 }
2797
Willy Tarreau6c011712023-01-06 16:09:58 +01002798 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002799 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002800 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002801 return ACT_RET_PRS_ERR;
2802 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002803 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002804 rule->action_ptr = action_set_gpt;
2805 }
2806 else if (*cmd_name == '0') {
2807 cmd_name++;
2808 if (*cmd_name == '\0') {
2809 /* default stick table id. */
2810 rule->arg.gpt.sc = 0;
2811 } else {
2812 /* parse the stick table id. */
2813 if (*cmd_name != '(') {
2814 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2815 return ACT_RET_PRS_ERR;
2816 }
2817 cmd_name++; /* jump the '(' */
2818 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2819 if (*error != ')') {
2820 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2821 return ACT_RET_PRS_ERR;
2822 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002823
Willy Tarreau6c011712023-01-06 16:09:58 +01002824 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002825 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002826 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002827 return ACT_RET_PRS_ERR;
2828 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002829 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002830 rule->action_ptr = action_set_gpt0;
2831 }
2832 else {
2833 /* default stick table id. */
2834 memprintf(err, "invalid gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2835 return ACT_RET_PRS_ERR;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002836 }
2837
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002838 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002839 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002840 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002841 if (*error == '\0') {
2842 /* valid integer, skip it */
2843 (*arg)++;
2844 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002845 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002846 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002847 if (!rule->arg.gpt.expr)
2848 return ACT_RET_PRS_ERR;
2849
2850 switch (rule->from) {
2851 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2852 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2853 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2854 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2855 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2856 default:
2857 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2858 return ACT_RET_PRS_ERR;
2859 }
2860 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2861 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2862 sample_src_names(rule->arg.gpt.expr->fetch->use));
2863 free(rule->arg.gpt.expr);
2864 return ACT_RET_PRS_ERR;
2865 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002866 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002867
Thierry FOURNIER42148732015-09-02 17:17:33 +02002868 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002869
2870 return ACT_RET_PRS_OK;
2871}
2872
Willy Tarreau5a72d032023-01-02 18:15:20 +01002873/* This function updates the gpc at index 'rule->arg.gpc.idx' of the array on
2874 * the tracksc counter of index 'rule->arg.gpc.sc' stored into the <stream> or
2875 * directly in the session <sess> if <stream> is set to NULL. This gpc is
2876 * set to the value computed by the expression 'rule->arg.gpc.expr' or if
2877 * 'rule->arg.gpc.expr' is null directly to the value of 'rule->arg.gpc.value'.
2878 *
2879 * This function always returns ACT_RET_CONT and parameter flags is unused.
2880 */
2881static enum act_return action_add_gpc(struct act_rule *rule, struct proxy *px,
2882 struct session *sess, struct stream *s, int flags)
2883{
2884 void *ptr1, *ptr2;
2885 struct stksess *ts;
2886 struct stkctr *stkctr;
2887 unsigned int value = 0;
2888 struct sample *smp;
2889 int smp_opt_dir;
2890
2891 /* Extract the stksess, return OK if no stksess available. */
2892 if (s)
2893 stkctr = &s->stkctr[rule->arg.gpc.sc];
2894 else
2895 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2896
2897 ts = stkctr_entry(stkctr);
2898 if (!ts)
2899 return ACT_RET_CONT;
2900
2901 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2902 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2903 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2904
2905 if (ptr1 || ptr2) {
2906 if (!rule->arg.gpc.expr)
2907 value = (unsigned int)(rule->arg.gpc.value);
2908 else {
2909 switch (rule->from) {
2910 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2911 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2912 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2913 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2914 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2915 default:
2916 send_log(px, LOG_ERR, "stick table: internal error while setting gpc%u.", rule->arg.gpc.idx);
2917 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2918 ha_alert("stick table: internal error while executing setting gpc%u.\n", rule->arg.gpc.idx);
2919 return ACT_RET_CONT;
2920 }
2921
2922 /* Fetch and cast the expression. */
2923 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpc.expr, SMP_T_SINT);
2924 if (!smp) {
2925 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpc%u.", rule->arg.gpc.idx);
2926 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2927 ha_alert("stick table: invalid expression or data type while setting gpc%u.\n", rule->arg.gpc.idx);
2928 return ACT_RET_CONT;
2929 }
2930 value = (unsigned int)(smp->data.u.sint);
2931 }
2932
2933 if (value) {
2934 /* only update the value if non-null increment */
2935 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2936
2937 if (ptr1)
2938 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
2939 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, value);
2940
2941 if (ptr2)
2942 stktable_data_cast(ptr2, std_t_uint) += value;
2943
2944 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2945 }
2946 /* always touch the table so that it doesn't expire */
2947 stktable_touch_local(stkctr->table, ts, 0);
2948 }
2949
2950 return ACT_RET_CONT;
2951}
2952
2953/* This function is a parser for the "sc-add-gpc" action. It understands the
2954 * format:
2955 *
2956 * sc-add-gpc(<gpc IDX>,<track ID>) <expression>
2957 *
2958 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2959 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpc.expr'
2960 * is filled with the pointer to the expression to execute or NULL if the arg
2961 * is directly an integer stored into 'rule->arg.gpt.value'.
2962 */
2963static enum act_parse_ret parse_add_gpc(const char **args, int *arg, struct proxy *px,
2964 struct act_rule *rule, char **err)
2965{
2966 const char *cmd_name = args[*arg-1];
2967 char *error;
2968 int smp_val;
2969
2970 cmd_name += strlen("sc-add-gpc");
2971 if (*cmd_name != '(') {
2972 memprintf(err, "Missing or invalid arguments for '%s'. Expects sc-add-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2973 return ACT_RET_PRS_ERR;
2974 }
2975 cmd_name++; /* skip the '(' */
2976 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2977 if (*error != ',') {
2978 memprintf(err, "Missing gpc ID. Expects %s(<GPC ID>,<Track ID>)", args[*arg-1]);
2979 return ACT_RET_PRS_ERR;
2980 }
2981 else {
2982 cmd_name = error + 1; /* skip the ',' */
2983 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2984 if (*error != ')') {
2985 memprintf(err, "invalid stick table track ID '%s'. Expects %s(<GPC ID>,<Track ID>)", cmd_name, args[*arg-1]);
2986 return ACT_RET_PRS_ERR;
2987 }
2988
2989 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
2990 memprintf(err, "invalid stick table track ID '%s' for '%s'. The max allowed ID is %d",
2991 cmd_name, args[*arg-1], MAX_SESS_STKCTR-1);
2992 return ACT_RET_PRS_ERR;
2993 }
2994 }
2995 rule->action_ptr = action_add_gpc;
2996
2997 /* value may be either an integer or an expression */
2998 rule->arg.gpc.expr = NULL;
2999 rule->arg.gpc.value = strtol(args[*arg], &error, 10);
3000 if (*error == '\0') {
3001 /* valid integer, skip it */
3002 (*arg)++;
3003 } else {
3004 rule->arg.gpc.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
3005 px->conf.args.line, err, &px->conf.args, NULL);
3006 if (!rule->arg.gpc.expr)
3007 return ACT_RET_PRS_ERR;
3008
3009 switch (rule->from) {
3010 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
3011 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
3012 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
3013 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
3014 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
3015 default:
3016 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
3017 return ACT_RET_PRS_ERR;
3018 }
3019
3020 if (!(rule->arg.gpc.expr->fetch->val & smp_val)) {
3021 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
3022 sample_src_names(rule->arg.gpc.expr->fetch->use));
3023 free(rule->arg.gpc.expr);
3024 return ACT_RET_PRS_ERR;
3025 }
3026 }
3027
3028 rule->action = ACT_CUSTOM;
3029
3030 return ACT_RET_PRS_OK;
3031}
3032
Willy Tarreau7d562212016-11-25 16:10:05 +01003033/* set temp integer to the number of used entries in the table pointed to by expr.
3034 * Accepts exactly 1 argument of type table.
3035 */
3036static int
3037smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3038{
3039 smp->flags = SMP_F_VOL_TEST;
3040 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003041 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003042 return 1;
3043}
3044
3045/* set temp integer to the number of free entries in the table pointed to by expr.
3046 * Accepts exactly 1 argument of type table.
3047 */
3048static int
3049smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
3050{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003051 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003052
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003053 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003054 smp->flags = SMP_F_VOL_TEST;
3055 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003056 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003057 return 1;
3058}
3059
3060/* Returns a pointer to a stkctr depending on the fetch keyword name.
3061 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
3062 * sc[0-9]_* will return a pointer to the respective field in the
3063 * stream <l4>. sc_* requires an UINT argument specifying the stick
3064 * counter number. src_* will fill a locally allocated structure with
3065 * the table and entry corresponding to what is specified with src_*.
3066 * NULL may be returned if the designated stkctr is not tracked. For
3067 * the sc_* and sc[0-9]_* forms, an optional table argument may be
3068 * passed. When present, the currently tracked key is then looked up
3069 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003070 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01003071 * multiple tables). <strm> is allowed to be NULL, in which case only
3072 * the session will be consulted.
3073 */
3074struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003075smp_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 +01003076{
Willy Tarreau7d562212016-11-25 16:10:05 +01003077 struct stkctr *stkptr;
3078 struct stksess *stksess;
3079 unsigned int num = kw[2] - '0';
3080 int arg = 0;
3081
3082 if (num == '_' - '0') {
3083 /* sc_* variant, args[0] = ctr# (mandatory) */
3084 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01003085 }
3086 else if (num > 9) { /* src_* variant, args[0] = table */
3087 struct stktable_key *key;
3088 struct connection *conn = objt_conn(sess->origin);
3089 struct sample smp;
3090
3091 if (!conn)
3092 return NULL;
3093
Joseph Herlant5662fa42018-11-15 13:43:28 -08003094 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003095 smp.px = NULL;
3096 smp.sess = sess;
3097 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003098 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003099 return NULL;
3100
3101 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003102 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003103 if (!key)
3104 return NULL;
3105
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003106 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003107 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
3108 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003109 }
3110
3111 /* Here, <num> contains the counter number from 0 to 9 for
3112 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
3113 * args[arg] is the first optional argument. We first lookup the
3114 * ctr form the stream, then from the session if it was not there.
Willy Tarreau6c011712023-01-06 16:09:58 +01003115 * But we must be sure the counter does not exceed global.tune.nb_stk_ctr.
Willy Tarreau7d562212016-11-25 16:10:05 +01003116 */
Willy Tarreau6c011712023-01-06 16:09:58 +01003117 if (num >= global.tune.nb_stk_ctr)
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02003118 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003119
Willy Tarreau6c011712023-01-06 16:09:58 +01003120 stkptr = NULL;
3121 if (strm && strm->stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003122 stkptr = &strm->stkctr[num];
Willy Tarreau6c011712023-01-06 16:09:58 +01003123 if (!strm || !stkptr || !stkctr_entry(stkptr)) {
3124 if (sess->stkctr)
3125 stkptr = &sess->stkctr[num];
3126 else
3127 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003128 if (!stkctr_entry(stkptr))
3129 return NULL;
3130 }
3131
3132 stksess = stkctr_entry(stkptr);
3133 if (!stksess)
3134 return NULL;
3135
3136 if (unlikely(args[arg].type == ARGT_TAB)) {
3137 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003138 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003139 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
3140 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003141 }
3142 return stkptr;
3143}
3144
3145/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
3146 * the entry if it doesn't exist yet. This is needed for a few fetch
3147 * functions which need to create an entry, such as src_inc_gpc* and
3148 * src_clr_gpc*.
3149 */
3150struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003151smp_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 +01003152{
Willy Tarreau7d562212016-11-25 16:10:05 +01003153 struct stktable_key *key;
3154 struct connection *conn = objt_conn(sess->origin);
3155 struct sample smp;
3156
3157 if (strncmp(kw, "src_", 4) != 0)
3158 return NULL;
3159
3160 if (!conn)
3161 return NULL;
3162
Joseph Herlant5662fa42018-11-15 13:43:28 -08003163 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003164 smp.px = NULL;
3165 smp.sess = sess;
3166 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003167 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003168 return NULL;
3169
3170 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003171 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003172 if (!key)
3173 return NULL;
3174
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003175 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003176 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
3177 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003178}
3179
3180/* set return a boolean indicating if the requested stream counter is
3181 * currently being tracked or not.
3182 * Supports being called as "sc[0-9]_tracked" only.
3183 */
3184static int
3185smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
3186{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003187 struct stkctr tmpstkctr;
3188 struct stkctr *stkctr;
3189
Willy Tarreau7d562212016-11-25 16:10:05 +01003190 smp->flags = SMP_F_VOL_TEST;
3191 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003192 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3193 smp->data.u.sint = !!stkctr;
3194
3195 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02003196 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02003197 stktable_release(stkctr->table, stkctr_entry(stkctr));
3198
Emeric Brun877b0b52021-06-30 18:57:49 +02003199 return 1;
3200}
3201
3202/* set <smp> to the General Purpose Tag of index set as first arg
3203 * to value from the stream's tracked frontend counters or from the src.
3204 * Supports being called as "sc_get_gpt(<gpt-idx>,<sc-idx>[,<table>])" or
3205 * "src_get_gpt(<gpt-idx>[,<table>])" only. Value zero is returned if
3206 * the key is new or gpt is not stored.
3207 */
3208static int
3209smp_fetch_sc_get_gpt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3210{
3211 struct stkctr tmpstkctr;
3212 struct stkctr *stkctr;
3213 unsigned int idx;
3214
3215 idx = args[0].data.sint;
3216
3217 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3218 if (!stkctr)
3219 return 0;
3220
3221 smp->flags = SMP_F_VOL_TEST;
3222 smp->data.type = SMP_T_SINT;
3223 smp->data.u.sint = 0;
3224
3225 if (stkctr_entry(stkctr)) {
3226 void *ptr;
3227
3228 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, idx);
3229 if (!ptr) {
3230 if (stkctr == &tmpstkctr)
3231 stktable_release(stkctr->table, stkctr_entry(stkctr));
3232 return 0; /* parameter not stored */
3233 }
3234
3235 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3236
3237 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3238
3239 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3240
3241 if (stkctr == &tmpstkctr)
3242 stktable_release(stkctr->table, stkctr_entry(stkctr));
3243 }
Willy Tarreau7d562212016-11-25 16:10:05 +01003244 return 1;
3245}
3246
3247/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
3248 * frontend counters or from the src.
3249 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
3250 * zero is returned if the key is new.
3251 */
3252static int
3253smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3254{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003255 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003256 struct stkctr *stkctr;
3257
Emeric Brun819fc6f2017-06-13 19:37:32 +02003258 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003259 if (!stkctr)
3260 return 0;
3261
3262 smp->flags = SMP_F_VOL_TEST;
3263 smp->data.type = SMP_T_SINT;
3264 smp->data.u.sint = 0;
3265
Emeric Brun819fc6f2017-06-13 19:37:32 +02003266 if (stkctr_entry(stkctr)) {
3267 void *ptr;
3268
3269 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02003270 if (!ptr)
3271 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, 0);
3272
Emeric Brun4d7ada82021-06-30 19:04:16 +02003273 if (!ptr) {
3274 if (stkctr == &tmpstkctr)
3275 stktable_release(stkctr->table, stkctr_entry(stkctr));
3276 return 0; /* parameter not stored */
3277 }
3278
3279 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3280
3281 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3282
3283 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3284
3285 if (stkctr == &tmpstkctr)
3286 stktable_release(stkctr->table, stkctr_entry(stkctr));
3287 }
3288 return 1;
3289}
3290
3291/* set <smp> to the GPC[args(0)]'s value from the stream's tracked
3292 * frontend counters or from the src.
3293 * Supports being called as "sc_get_gpc(<gpc-idx>,<sc-idx>[,<table>])" or
3294 * "src_get_gpc(<gpc-idx>[,<table>])" only. Value
3295 * Value zero is returned if the key is new or gpc is not stored.
3296 */
3297static int
3298smp_fetch_sc_get_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3299{
3300 struct stkctr tmpstkctr;
3301 struct stkctr *stkctr;
3302 unsigned int idx;
3303
3304 idx = args[0].data.sint;
3305
3306 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3307 if (!stkctr)
3308 return 0;
3309
3310 smp->flags = SMP_F_VOL_TEST;
3311 smp->data.type = SMP_T_SINT;
3312 smp->data.u.sint = 0;
3313
3314 if (stkctr_entry(stkctr) != NULL) {
3315 void *ptr;
3316
3317 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003318 if (!ptr) {
3319 if (stkctr == &tmpstkctr)
3320 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003321 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003322 }
3323
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003324 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003325
Emeric Brun0e3457b2021-06-30 17:18:28 +02003326 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003327
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003328 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003329
3330 if (stkctr == &tmpstkctr)
3331 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003332 }
3333 return 1;
3334}
3335
3336/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
3337 * frontend counters or from the src.
3338 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
3339 * zero is returned if the key is new.
3340 */
3341static int
3342smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3343{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003344 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003345 struct stkctr *stkctr;
3346
Emeric Brun819fc6f2017-06-13 19:37:32 +02003347 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003348 if (!stkctr)
3349 return 0;
3350
3351 smp->flags = SMP_F_VOL_TEST;
3352 smp->data.type = SMP_T_SINT;
3353 smp->data.u.sint = 0;
3354
3355 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003356 void *ptr;
3357
3358 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3359 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003360 /* fallback on the gpc array */
3361 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3362 }
3363
3364 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003365 if (stkctr == &tmpstkctr)
3366 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003367 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003368 }
3369
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003370 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003371
Emeric Brun0e3457b2021-06-30 17:18:28 +02003372 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003373
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003374 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003375
3376 if (stkctr == &tmpstkctr)
3377 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003378 }
3379 return 1;
3380}
3381
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003382/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
3383 * frontend counters or from the src.
3384 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
3385 * zero is returned if the key is new.
3386 */
3387static int
3388smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3389{
3390 struct stkctr tmpstkctr;
3391 struct stkctr *stkctr;
3392
3393 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3394 if (!stkctr)
3395 return 0;
3396
3397 smp->flags = SMP_F_VOL_TEST;
3398 smp->data.type = SMP_T_SINT;
3399 smp->data.u.sint = 0;
3400
3401 if (stkctr_entry(stkctr) != NULL) {
3402 void *ptr;
3403
3404 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3405 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003406 /* fallback on the gpc array */
3407 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3408 }
3409
3410 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003411 if (stkctr == &tmpstkctr)
3412 stktable_release(stkctr->table, stkctr_entry(stkctr));
3413 return 0; /* parameter not stored */
3414 }
3415
3416 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3417
Emeric Brun0e3457b2021-06-30 17:18:28 +02003418 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003419
3420 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3421
3422 if (stkctr == &tmpstkctr)
3423 stktable_release(stkctr->table, stkctr_entry(stkctr));
3424 }
3425 return 1;
3426}
3427
Emeric Brun4d7ada82021-06-30 19:04:16 +02003428/* set <smp> to the GPC[args(0)]'s event rate from the stream's
3429 * tracked frontend counters or from the src.
3430 * Supports being called as "sc_gpc_rate(<gpc-idx>,<sc-idx>[,<table])"
3431 * or "src_gpc_rate(<gpc-idx>[,<table>])" only.
3432 * Value zero is returned if the key is new or gpc_rate is not stored.
3433 */
3434static int
3435smp_fetch_sc_gpc_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3436{
3437 struct stkctr tmpstkctr;
3438 struct stkctr *stkctr;
3439 unsigned int idx;
3440
3441 idx = args[0].data.sint;
3442
3443 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3444 if (!stkctr)
3445 return 0;
3446
3447 smp->flags = SMP_F_VOL_TEST;
3448 smp->data.type = SMP_T_SINT;
3449 smp->data.u.sint = 0;
3450 if (stkctr_entry(stkctr) != NULL) {
3451 void *ptr;
3452
3453 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3454 if (!ptr) {
3455 if (stkctr == &tmpstkctr)
3456 stktable_release(stkctr->table, stkctr_entry(stkctr));
3457 return 0; /* parameter not stored */
3458 }
3459
3460 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3461
3462 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3463 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u);
3464
3465 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3466
3467 if (stkctr == &tmpstkctr)
3468 stktable_release(stkctr->table, stkctr_entry(stkctr));
3469 }
3470 return 1;
3471}
3472
Willy Tarreau7d562212016-11-25 16:10:05 +01003473/* set <smp> to the General Purpose Counter 0's event rate from the stream's
3474 * tracked frontend counters or from the src.
3475 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
3476 * Value zero is returned if the key is new.
3477 */
3478static int
3479smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3480{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003481 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003482 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003483 unsigned int period;
Willy Tarreau7d562212016-11-25 16:10:05 +01003484
Emeric Brun819fc6f2017-06-13 19:37:32 +02003485 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003486 if (!stkctr)
3487 return 0;
3488
3489 smp->flags = SMP_F_VOL_TEST;
3490 smp->data.type = SMP_T_SINT;
3491 smp->data.u.sint = 0;
3492 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003493 void *ptr;
3494
3495 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003496 if (ptr) {
3497 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3498 }
3499 else {
3500 /* fallback on the gpc array */
3501 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3502 if (ptr)
3503 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3504 }
3505
Emeric Brun819fc6f2017-06-13 19:37:32 +02003506 if (!ptr) {
3507 if (stkctr == &tmpstkctr)
3508 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003509 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003510 }
3511
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003512 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003513
Emeric Brun726783d2021-06-30 19:06:43 +02003514 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003515
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003516 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003517
3518 if (stkctr == &tmpstkctr)
3519 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003520 }
3521 return 1;
3522}
3523
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003524/* set <smp> to the General Purpose Counter 1's event rate from the stream's
3525 * tracked frontend counters or from the src.
3526 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
3527 * Value zero is returned if the key is new.
3528 */
3529static int
3530smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3531{
3532 struct stkctr tmpstkctr;
3533 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003534 unsigned int period;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003535
3536 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3537 if (!stkctr)
3538 return 0;
3539
3540 smp->flags = SMP_F_VOL_TEST;
3541 smp->data.type = SMP_T_SINT;
3542 smp->data.u.sint = 0;
3543 if (stkctr_entry(stkctr) != NULL) {
3544 void *ptr;
3545
3546 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003547 if (ptr) {
3548 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3549 }
3550 else {
3551 /* fallback on the gpc array */
3552 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3553 if (ptr)
3554 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3555 }
3556
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003557 if (!ptr) {
3558 if (stkctr == &tmpstkctr)
3559 stktable_release(stkctr->table, stkctr_entry(stkctr));
3560 return 0; /* parameter not stored */
3561 }
3562
3563 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3564
Emeric Brun726783d2021-06-30 19:06:43 +02003565 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 +01003566
3567 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3568
3569 if (stkctr == &tmpstkctr)
3570 stktable_release(stkctr->table, stkctr_entry(stkctr));
3571 }
3572 return 1;
3573}
3574
Emeric Brun4d7ada82021-06-30 19:04:16 +02003575/* Increment the GPC[args(0)] value from the stream's tracked
3576 * frontend counters and return it into temp integer.
3577 * Supports being called as "sc_inc_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3578 * or "src_inc_gpc(<gpc-idx>[,<table>])" only.
3579 */
3580static int
3581smp_fetch_sc_inc_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3582{
3583 struct stkctr tmpstkctr;
3584 struct stkctr *stkctr;
3585 unsigned int idx;
3586
3587 idx = args[0].data.sint;
3588
3589 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3590 if (!stkctr)
3591 return 0;
3592
3593 smp->flags = SMP_F_VOL_TEST;
3594 smp->data.type = SMP_T_SINT;
3595 smp->data.u.sint = 0;
3596
3597 if (!stkctr_entry(stkctr))
3598 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3599
3600 if (stkctr && stkctr_entry(stkctr)) {
3601 void *ptr1,*ptr2;
3602
3603
3604 /* First, update gpc0_rate if it's tracked. Second, update its
3605 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3606 */
3607 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3608 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3609 if (ptr1 || ptr2) {
3610 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3611
3612 if (ptr1) {
3613 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
3614 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
3615 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
3616 }
3617
3618 if (ptr2)
3619 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
3620
3621 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3622
3623 /* If data was modified, we need to touch to re-schedule sync */
3624 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3625 }
3626 else if (stkctr == &tmpstkctr)
3627 stktable_release(stkctr->table, stkctr_entry(stkctr));
3628 }
3629 return 1;
3630}
3631
Willy Tarreau7d562212016-11-25 16:10:05 +01003632/* Increment the General Purpose Counter 0 value from the stream's tracked
3633 * frontend counters and return it into temp integer.
3634 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
3635 */
3636static int
3637smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3638{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003639 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003640 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003641 unsigned int period = 0;
Willy Tarreau7d562212016-11-25 16:10:05 +01003642
Emeric Brun819fc6f2017-06-13 19:37:32 +02003643 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003644 if (!stkctr)
3645 return 0;
3646
3647 smp->flags = SMP_F_VOL_TEST;
3648 smp->data.type = SMP_T_SINT;
3649 smp->data.u.sint = 0;
3650
Emeric Brun819fc6f2017-06-13 19:37:32 +02003651 if (!stkctr_entry(stkctr))
3652 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003653
3654 if (stkctr && stkctr_entry(stkctr)) {
3655 void *ptr1,*ptr2;
3656
Emeric Brun819fc6f2017-06-13 19:37:32 +02003657
Willy Tarreau7d562212016-11-25 16:10:05 +01003658 /* First, update gpc0_rate if it's tracked. Second, update its
3659 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3660 */
3661 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003662 if (ptr1) {
3663 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3664 }
3665 else {
3666 /* fallback on the gpc array */
3667 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3668 if (ptr1)
3669 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3670 }
3671
Willy Tarreau7d562212016-11-25 16:10:05 +01003672 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02003673 if (!ptr2) {
3674 /* fallback on the gpc array */
3675 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3676 }
3677
Emeric Brun819fc6f2017-06-13 19:37:32 +02003678 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003679 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01003680
Emeric Brun819fc6f2017-06-13 19:37:32 +02003681 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003682 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003683 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003684 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003685 }
3686
3687 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003688 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003689
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003690 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003691
3692 /* If data was modified, we need to touch to re-schedule sync */
3693 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3694 }
3695 else if (stkctr == &tmpstkctr)
3696 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003697 }
3698 return 1;
3699}
3700
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003701/* Increment the General Purpose Counter 1 value from the stream's tracked
3702 * frontend counters and return it into temp integer.
3703 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
3704 */
3705static int
3706smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3707{
3708 struct stkctr tmpstkctr;
3709 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003710 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003711
3712 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3713 if (!stkctr)
3714 return 0;
3715
3716 smp->flags = SMP_F_VOL_TEST;
3717 smp->data.type = SMP_T_SINT;
3718 smp->data.u.sint = 0;
3719
3720 if (!stkctr_entry(stkctr))
3721 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3722
3723 if (stkctr && stkctr_entry(stkctr)) {
3724 void *ptr1,*ptr2;
3725
3726
3727 /* First, update gpc1_rate if it's tracked. Second, update its
3728 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
3729 */
3730 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003731 if (ptr1) {
3732 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3733 }
3734 else {
3735 /* fallback on the gpc array */
3736 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3737 if (ptr1)
3738 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3739 }
3740
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003741 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02003742 if (!ptr2) {
3743 /* fallback on the gpc array */
3744 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3745 }
3746
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003747 if (ptr1 || ptr2) {
3748 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3749
3750 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003751 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003752 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003753 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003754 }
3755
3756 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003757 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003758
3759 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3760
3761 /* If data was modified, we need to touch to re-schedule sync */
3762 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3763 }
3764 else if (stkctr == &tmpstkctr)
3765 stktable_release(stkctr->table, stkctr_entry(stkctr));
3766 }
3767 return 1;
3768}
3769
Emeric Brun4d7ada82021-06-30 19:04:16 +02003770/* Clear the GPC[args(0)] value from the stream's tracked
3771 * frontend counters and return its previous value into temp integer.
3772 * Supports being called as "sc_clr_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3773 * or "src_clr_gpc(<gpc-idx>[,<table>])" only.
3774 */
3775static int
3776smp_fetch_sc_clr_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3777{
3778 struct stkctr tmpstkctr;
3779 struct stkctr *stkctr;
3780 unsigned int idx;
3781
3782 idx = args[0].data.sint;
3783
3784 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3785 if (!stkctr)
3786 return 0;
3787
3788 smp->flags = SMP_F_VOL_TEST;
3789 smp->data.type = SMP_T_SINT;
3790 smp->data.u.sint = 0;
3791
3792 if (!stkctr_entry(stkctr))
3793 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3794
3795 if (stkctr && stkctr_entry(stkctr)) {
3796 void *ptr;
3797
3798 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3799 if (!ptr) {
3800 if (stkctr == &tmpstkctr)
3801 stktable_release(stkctr->table, stkctr_entry(stkctr));
3802 return 0; /* parameter not stored */
3803 }
3804
3805 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3806
3807 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3808 stktable_data_cast(ptr, std_t_uint) = 0;
3809
3810 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3811
3812 /* If data was modified, we need to touch to re-schedule sync */
3813 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3814 }
3815 return 1;
3816}
3817
Willy Tarreau7d562212016-11-25 16:10:05 +01003818/* Clear the General Purpose Counter 0 value from the stream's tracked
3819 * frontend counters and return its previous value into temp integer.
3820 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
3821 */
3822static int
3823smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3824{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003825 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003826 struct stkctr *stkctr;
3827
Emeric Brun819fc6f2017-06-13 19:37:32 +02003828 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003829 if (!stkctr)
3830 return 0;
3831
3832 smp->flags = SMP_F_VOL_TEST;
3833 smp->data.type = SMP_T_SINT;
3834 smp->data.u.sint = 0;
3835
Emeric Brun819fc6f2017-06-13 19:37:32 +02003836 if (!stkctr_entry(stkctr))
3837 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003838
Emeric Brun819fc6f2017-06-13 19:37:32 +02003839 if (stkctr && stkctr_entry(stkctr)) {
3840 void *ptr;
3841
3842 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3843 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003844 /* fallback on the gpc array */
3845 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3846 }
3847
3848 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003849 if (stkctr == &tmpstkctr)
3850 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003851 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003852 }
3853
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003854 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003855
Emeric Brun0e3457b2021-06-30 17:18:28 +02003856 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3857 stktable_data_cast(ptr, std_t_uint) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003858
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003859 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003860
Willy Tarreau7d562212016-11-25 16:10:05 +01003861 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003862 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01003863 }
3864 return 1;
3865}
3866
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003867/* Clear the General Purpose Counter 1 value from the stream's tracked
3868 * frontend counters and return its previous value into temp integer.
3869 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
3870 */
3871static int
3872smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3873{
3874 struct stkctr tmpstkctr;
3875 struct stkctr *stkctr;
3876
3877 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3878 if (!stkctr)
3879 return 0;
3880
3881 smp->flags = SMP_F_VOL_TEST;
3882 smp->data.type = SMP_T_SINT;
3883 smp->data.u.sint = 0;
3884
3885 if (!stkctr_entry(stkctr))
3886 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3887
3888 if (stkctr && stkctr_entry(stkctr)) {
3889 void *ptr;
3890
3891 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3892 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003893 /* fallback on the gpc array */
3894 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3895 }
3896
3897 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003898 if (stkctr == &tmpstkctr)
3899 stktable_release(stkctr->table, stkctr_entry(stkctr));
3900 return 0; /* parameter not stored */
3901 }
3902
3903 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3904
Emeric Brun0e3457b2021-06-30 17:18:28 +02003905 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3906 stktable_data_cast(ptr, std_t_uint) = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003907
3908 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3909
3910 /* If data was modified, we need to touch to re-schedule sync */
3911 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3912 }
3913 return 1;
3914}
3915
Willy Tarreau7d562212016-11-25 16:10:05 +01003916/* set <smp> to the cumulated number of connections from the stream's tracked
3917 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3918 * "src_conn_cnt" only.
3919 */
3920static int
3921smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3922{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003923 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003924 struct stkctr *stkctr;
3925
Emeric Brun819fc6f2017-06-13 19:37:32 +02003926 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003927 if (!stkctr)
3928 return 0;
3929
3930 smp->flags = SMP_F_VOL_TEST;
3931 smp->data.type = SMP_T_SINT;
3932 smp->data.u.sint = 0;
3933 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003934 void *ptr;
3935
3936 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
3937 if (!ptr) {
3938 if (stkctr == &tmpstkctr)
3939 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003940 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003941 }
3942
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003943 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003944
Emeric Brun0e3457b2021-06-30 17:18:28 +02003945 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003946
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003947 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003948
3949 if (stkctr == &tmpstkctr)
3950 stktable_release(stkctr->table, stkctr_entry(stkctr));
3951
3952
Willy Tarreau7d562212016-11-25 16:10:05 +01003953 }
3954 return 1;
3955}
3956
3957/* set <smp> to the connection rate from the stream's tracked frontend
3958 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3959 * only.
3960 */
3961static int
3962smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3963{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003964 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003965 struct stkctr *stkctr;
3966
Emeric Brun819fc6f2017-06-13 19:37:32 +02003967 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003968 if (!stkctr)
3969 return 0;
3970
3971 smp->flags = SMP_F_VOL_TEST;
3972 smp->data.type = SMP_T_SINT;
3973 smp->data.u.sint = 0;
3974 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003975 void *ptr;
3976
3977 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
3978 if (!ptr) {
3979 if (stkctr == &tmpstkctr)
3980 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003981 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003982 }
3983
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003984 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003985
Emeric Brun0e3457b2021-06-30 17:18:28 +02003986 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01003987 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003988
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003989 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003990
3991 if (stkctr == &tmpstkctr)
3992 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003993 }
3994 return 1;
3995}
3996
3997/* set temp integer to the number of connections from the stream's source address
3998 * in the table pointed to by expr, after updating it.
3999 * Accepts exactly 1 argument of type table.
4000 */
4001static int
4002smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4003{
4004 struct connection *conn = objt_conn(smp->sess->origin);
4005 struct stksess *ts;
4006 struct stktable_key *key;
4007 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004008 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004009
4010 if (!conn)
4011 return 0;
4012
Joseph Herlant5662fa42018-11-15 13:43:28 -08004013 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02004014 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01004015 return 0;
4016
4017 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004018 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01004019 if (!key)
4020 return 0;
4021
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004022 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004023
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004024 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01004025 /* entry does not exist and could not be created */
4026 return 0;
4027
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004028 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004029 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01004030 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004031 }
Willy Tarreau7d562212016-11-25 16:10:05 +01004032
4033 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004034
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004035 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004036
Emeric Brun0e3457b2021-06-30 17:18:28 +02004037 smp->data.u.sint = ++stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004038
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004039 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004040
Willy Tarreau7d562212016-11-25 16:10:05 +01004041 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004042
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004043 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004044
4045 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01004046 return 1;
4047}
4048
4049/* set <smp> to the number of concurrent connections from the stream's tracked
4050 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
4051 * "src_conn_cur" only.
4052 */
4053static int
4054smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
4055{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004056 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004057 struct stkctr *stkctr;
4058
Emeric Brun819fc6f2017-06-13 19:37:32 +02004059 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004060 if (!stkctr)
4061 return 0;
4062
4063 smp->flags = SMP_F_VOL_TEST;
4064 smp->data.type = SMP_T_SINT;
4065 smp->data.u.sint = 0;
4066 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004067 void *ptr;
4068
4069 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
4070 if (!ptr) {
4071 if (stkctr == &tmpstkctr)
4072 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004073 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004074 }
4075
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004076 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004077
Emeric Brun0e3457b2021-06-30 17:18:28 +02004078 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004079
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004080 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004081
4082 if (stkctr == &tmpstkctr)
4083 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004084 }
4085 return 1;
4086}
4087
4088/* set <smp> to the cumulated number of streams from the stream's tracked
4089 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
4090 * "src_sess_cnt" only.
4091 */
4092static int
4093smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4094{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004095 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004096 struct stkctr *stkctr;
4097
Emeric Brun819fc6f2017-06-13 19:37:32 +02004098 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004099 if (!stkctr)
4100 return 0;
4101
4102 smp->flags = SMP_F_VOL_TEST;
4103 smp->data.type = SMP_T_SINT;
4104 smp->data.u.sint = 0;
4105 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004106 void *ptr;
4107
4108 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
4109 if (!ptr) {
4110 if (stkctr == &tmpstkctr)
4111 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004112 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004113 }
4114
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004115 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004116
Emeric Brun0e3457b2021-06-30 17:18:28 +02004117 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004118
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004119 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004120
4121 if (stkctr == &tmpstkctr)
4122 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004123 }
4124 return 1;
4125}
4126
4127/* set <smp> to the stream rate from the stream's tracked frontend counters.
4128 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
4129 */
4130static int
4131smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4132{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004133 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004134 struct stkctr *stkctr;
4135
Emeric Brun819fc6f2017-06-13 19:37:32 +02004136 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004137 if (!stkctr)
4138 return 0;
4139
4140 smp->flags = SMP_F_VOL_TEST;
4141 smp->data.type = SMP_T_SINT;
4142 smp->data.u.sint = 0;
4143 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004144 void *ptr;
4145
4146 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
4147 if (!ptr) {
4148 if (stkctr == &tmpstkctr)
4149 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004150 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004151 }
4152
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004153 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004154
Emeric Brun0e3457b2021-06-30 17:18:28 +02004155 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004156 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004157
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004158 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004159
4160 if (stkctr == &tmpstkctr)
4161 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004162 }
4163 return 1;
4164}
4165
4166/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
4167 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
4168 * "src_http_req_cnt" only.
4169 */
4170static int
4171smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4172{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004173 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004174 struct stkctr *stkctr;
4175
Emeric Brun819fc6f2017-06-13 19:37:32 +02004176 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004177 if (!stkctr)
4178 return 0;
4179
4180 smp->flags = SMP_F_VOL_TEST;
4181 smp->data.type = SMP_T_SINT;
4182 smp->data.u.sint = 0;
4183 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004184 void *ptr;
4185
4186 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
4187 if (!ptr) {
4188 if (stkctr == &tmpstkctr)
4189 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004190 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004191 }
4192
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004193 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004194
Emeric Brun0e3457b2021-06-30 17:18:28 +02004195 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004196
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004197 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004198
4199 if (stkctr == &tmpstkctr)
4200 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004201 }
4202 return 1;
4203}
4204
4205/* set <smp> to the HTTP request rate from the stream's tracked frontend
4206 * counters. Supports being called as "sc[0-9]_http_req_rate" or
4207 * "src_http_req_rate" only.
4208 */
4209static int
4210smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4211{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004212 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004213 struct stkctr *stkctr;
4214
Emeric Brun819fc6f2017-06-13 19:37:32 +02004215 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004216 if (!stkctr)
4217 return 0;
4218
4219 smp->flags = SMP_F_VOL_TEST;
4220 smp->data.type = SMP_T_SINT;
4221 smp->data.u.sint = 0;
4222 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004223 void *ptr;
4224
4225 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
4226 if (!ptr) {
4227 if (stkctr == &tmpstkctr)
4228 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004229 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004230 }
4231
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004232 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004233
Emeric Brun0e3457b2021-06-30 17:18:28 +02004234 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004235 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004236
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004237 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004238
4239 if (stkctr == &tmpstkctr)
4240 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004241 }
4242 return 1;
4243}
4244
4245/* set <smp> to the cumulated number of HTTP requests errors from the stream's
4246 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
4247 * "src_http_err_cnt" only.
4248 */
4249static int
4250smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4251{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004252 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004253 struct stkctr *stkctr;
4254
Emeric Brun819fc6f2017-06-13 19:37:32 +02004255 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004256 if (!stkctr)
4257 return 0;
4258
4259 smp->flags = SMP_F_VOL_TEST;
4260 smp->data.type = SMP_T_SINT;
4261 smp->data.u.sint = 0;
4262 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004263 void *ptr;
4264
4265 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
4266 if (!ptr) {
4267 if (stkctr == &tmpstkctr)
4268 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004269 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004270 }
4271
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004272 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004273
Emeric Brun0e3457b2021-06-30 17:18:28 +02004274 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004275
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004276 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004277
4278 if (stkctr == &tmpstkctr)
4279 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004280 }
4281 return 1;
4282}
4283
4284/* set <smp> to the HTTP request error rate from the stream's tracked frontend
4285 * counters. Supports being called as "sc[0-9]_http_err_rate" or
4286 * "src_http_err_rate" only.
4287 */
4288static int
4289smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4290{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004291 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004292 struct stkctr *stkctr;
4293
Emeric Brun819fc6f2017-06-13 19:37:32 +02004294 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004295 if (!stkctr)
4296 return 0;
4297
4298 smp->flags = SMP_F_VOL_TEST;
4299 smp->data.type = SMP_T_SINT;
4300 smp->data.u.sint = 0;
4301 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004302 void *ptr;
4303
4304 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
4305 if (!ptr) {
4306 if (stkctr == &tmpstkctr)
4307 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004308 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004309 }
4310
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004311 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004312
Emeric Brun0e3457b2021-06-30 17:18:28 +02004313 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004314 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004315
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004316 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004317
4318 if (stkctr == &tmpstkctr)
4319 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004320 }
4321 return 1;
4322}
4323
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004324/* set <smp> to the cumulated number of HTTP response failures from the stream's
4325 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
4326 * "src_http_fail_cnt" only.
4327 */
4328static int
4329smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4330{
4331 struct stkctr tmpstkctr;
4332 struct stkctr *stkctr;
4333
4334 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4335 if (!stkctr)
4336 return 0;
4337
4338 smp->flags = SMP_F_VOL_TEST;
4339 smp->data.type = SMP_T_SINT;
4340 smp->data.u.sint = 0;
4341 if (stkctr_entry(stkctr) != NULL) {
4342 void *ptr;
4343
4344 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
4345 if (!ptr) {
4346 if (stkctr == &tmpstkctr)
4347 stktable_release(stkctr->table, stkctr_entry(stkctr));
4348 return 0; /* parameter not stored */
4349 }
4350
4351 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4352
Emeric Brun0e3457b2021-06-30 17:18:28 +02004353 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004354
4355 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4356
4357 if (stkctr == &tmpstkctr)
4358 stktable_release(stkctr->table, stkctr_entry(stkctr));
4359 }
4360 return 1;
4361}
4362
4363/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
4364 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
4365 * "src_http_fail_rate" only.
4366 */
4367static int
4368smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4369{
4370 struct stkctr tmpstkctr;
4371 struct stkctr *stkctr;
4372
4373 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4374 if (!stkctr)
4375 return 0;
4376
4377 smp->flags = SMP_F_VOL_TEST;
4378 smp->data.type = SMP_T_SINT;
4379 smp->data.u.sint = 0;
4380 if (stkctr_entry(stkctr) != NULL) {
4381 void *ptr;
4382
4383 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
4384 if (!ptr) {
4385 if (stkctr == &tmpstkctr)
4386 stktable_release(stkctr->table, stkctr_entry(stkctr));
4387 return 0; /* parameter not stored */
4388 }
4389
4390 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4391
Emeric Brun0e3457b2021-06-30 17:18:28 +02004392 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004393 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
4394
4395 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4396
4397 if (stkctr == &tmpstkctr)
4398 stktable_release(stkctr->table, stkctr_entry(stkctr));
4399 }
4400 return 1;
4401}
4402
Willy Tarreau7d562212016-11-25 16:10:05 +01004403/* set <smp> to the number of kbytes received from clients, as found in the
4404 * stream's tracked frontend counters. Supports being called as
4405 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
4406 */
4407static int
4408smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
4409{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004410 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004411 struct stkctr *stkctr;
4412
Emeric Brun819fc6f2017-06-13 19:37:32 +02004413 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004414 if (!stkctr)
4415 return 0;
4416
4417 smp->flags = SMP_F_VOL_TEST;
4418 smp->data.type = SMP_T_SINT;
4419 smp->data.u.sint = 0;
4420 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004421 void *ptr;
4422
4423 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
4424 if (!ptr) {
4425 if (stkctr == &tmpstkctr)
4426 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004427 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004428 }
4429
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004430 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004431
Emeric Brun0e3457b2021-06-30 17:18:28 +02004432 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004433
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004434 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004435
4436 if (stkctr == &tmpstkctr)
4437 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004438 }
4439 return 1;
4440}
4441
4442/* set <smp> to the data rate received from clients in bytes/s, as found
4443 * in the stream's tracked frontend counters. Supports being called as
4444 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
4445 */
4446static int
4447smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4448{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004449 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004450 struct stkctr *stkctr;
4451
Emeric Brun819fc6f2017-06-13 19:37:32 +02004452 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004453 if (!stkctr)
4454 return 0;
4455
4456 smp->flags = SMP_F_VOL_TEST;
4457 smp->data.type = SMP_T_SINT;
4458 smp->data.u.sint = 0;
4459 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004460 void *ptr;
4461
4462 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
4463 if (!ptr) {
4464 if (stkctr == &tmpstkctr)
4465 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004466 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004467 }
4468
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004469 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004470
Emeric Brun0e3457b2021-06-30 17:18:28 +02004471 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004472 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004473
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004474 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004475
4476 if (stkctr == &tmpstkctr)
4477 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004478 }
4479 return 1;
4480}
4481
4482/* set <smp> to the number of kbytes sent to clients, as found in the
4483 * stream's tracked frontend counters. Supports being called as
4484 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
4485 */
4486static int
4487smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
4488{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004489 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004490 struct stkctr *stkctr;
4491
Emeric Brun819fc6f2017-06-13 19:37:32 +02004492 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004493 if (!stkctr)
4494 return 0;
4495
4496 smp->flags = SMP_F_VOL_TEST;
4497 smp->data.type = SMP_T_SINT;
4498 smp->data.u.sint = 0;
4499 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004500 void *ptr;
4501
4502 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
4503 if (!ptr) {
4504 if (stkctr == &tmpstkctr)
4505 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004506 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004507 }
4508
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004509 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004510
Emeric Brun0e3457b2021-06-30 17:18:28 +02004511 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004512
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004513 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004514
4515 if (stkctr == &tmpstkctr)
4516 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004517 }
4518 return 1;
4519}
4520
4521/* set <smp> to the data rate sent to clients in bytes/s, as found in the
4522 * stream's tracked frontend counters. Supports being called as
4523 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
4524 */
4525static int
4526smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4527{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004528 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004529 struct stkctr *stkctr;
4530
Emeric Brun819fc6f2017-06-13 19:37:32 +02004531 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004532 if (!stkctr)
4533 return 0;
4534
4535 smp->flags = SMP_F_VOL_TEST;
4536 smp->data.type = SMP_T_SINT;
4537 smp->data.u.sint = 0;
4538 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004539 void *ptr;
4540
4541 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
4542 if (!ptr) {
4543 if (stkctr == &tmpstkctr)
4544 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004545 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004546 }
4547
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004548 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004549
Emeric Brun0e3457b2021-06-30 17:18:28 +02004550 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004551 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004552
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004553 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004554
4555 if (stkctr == &tmpstkctr)
4556 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004557 }
4558 return 1;
4559}
4560
4561/* set <smp> to the number of active trackers on the SC entry in the stream's
4562 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
4563 */
4564static int
4565smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
4566{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004567 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004568 struct stkctr *stkctr;
4569
Emeric Brun819fc6f2017-06-13 19:37:32 +02004570 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004571 if (!stkctr)
4572 return 0;
4573
4574 smp->flags = SMP_F_VOL_TEST;
4575 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004576 if (stkctr == &tmpstkctr) {
4577 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
4578 stktable_release(stkctr->table, stkctr_entry(stkctr));
4579 }
4580 else {
4581 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
4582 }
4583
Willy Tarreau7d562212016-11-25 16:10:05 +01004584 return 1;
4585}
4586
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004587
4588/* The functions below are used to manipulate table contents from the CLI.
4589 * There are 3 main actions, "clear", "set" and "show". The code is shared
4590 * between all actions, and the action is encoded in the void *private in
4591 * the appctx as well as in the keyword registration, among one of the
4592 * following values.
4593 */
4594
4595enum {
4596 STK_CLI_ACT_CLR,
4597 STK_CLI_ACT_SET,
4598 STK_CLI_ACT_SHOW,
4599};
4600
Willy Tarreau4596fe22022-05-17 19:07:51 +02004601/* Dump the status of a table to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004602 * read buffer. It returns 0 if the output buffer is full
4603 * and needs to be called again, otherwise non-zero.
4604 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004605static int table_dump_head_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004606 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004607 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004608{
Willy Tarreauc12b3212022-05-27 11:08:15 +02004609 struct stream *s = __sc_strm(appctx_sc(appctx));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004610
4611 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004612 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004613
4614 /* any other information should be dumped here */
4615
William Lallemand07a62f72017-05-24 00:57:40 +02004616 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004617 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
4618
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004619 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004620 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004621
4622 return 1;
4623}
4624
Willy Tarreau4596fe22022-05-17 19:07:51 +02004625/* Dump a table entry to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004626 * read buffer. It returns 0 if the output buffer is full
4627 * and needs to be called again, otherwise non-zero.
4628 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004629static int table_dump_entry_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004630 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004631 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004632{
4633 int dt;
4634
4635 chunk_appendf(msg, "%p:", entry);
4636
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004637 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004638 char addr[INET_ADDRSTRLEN];
4639 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
4640 chunk_appendf(msg, " key=%s", addr);
4641 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004642 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004643 char addr[INET6_ADDRSTRLEN];
4644 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
4645 chunk_appendf(msg, " key=%s", addr);
4646 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004647 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01004648 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004649 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004650 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004651 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004652 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004653 }
4654 else {
4655 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004656 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004657 }
4658
Willy Tarreau16b282f2022-11-29 11:55:18 +01004659 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 +01004660
4661 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
4662 void *ptr;
4663
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004664 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004665 continue;
Emeric Brunc64a2a32021-06-30 18:01:02 +02004666 if (stktable_data_types[dt].is_array) {
4667 char tmp[16] = {};
4668 const char *name_pfx = stktable_data_types[dt].name;
4669 const char *name_sfx = NULL;
4670 unsigned int idx = 0;
4671 int i = 0;
4672
4673 /* split name to show index before first _ of the name
4674 * for example: 'gpc3_rate' if array name is 'gpc_rate'.
4675 */
4676 for (i = 0 ; i < (sizeof(tmp) - 1); i++) {
4677 if (!name_pfx[i])
4678 break;
4679 if (name_pfx[i] == '_') {
4680 name_pfx = &tmp[0];
4681 name_sfx = &stktable_data_types[dt].name[i];
4682 break;
4683 }
4684 tmp[i] = name_pfx[i];
4685 }
4686
4687 ptr = stktable_data_ptr_idx(t, entry, dt, idx);
4688 while (ptr) {
4689 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
4690 chunk_appendf(msg, " %s%u%s(%u)=", name_pfx, idx, name_sfx ? name_sfx : "", t->data_arg[dt].u);
4691 else
4692 chunk_appendf(msg, " %s%u%s=", name_pfx, idx, name_sfx ? name_sfx : "");
4693 switch (stktable_data_types[dt].std_type) {
4694 case STD_T_SINT:
4695 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4696 break;
4697 case STD_T_UINT:
4698 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4699 break;
4700 case STD_T_ULL:
4701 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
4702 break;
4703 case STD_T_FRQP:
4704 chunk_appendf(msg, "%u",
4705 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4706 t->data_arg[dt].u));
4707 break;
4708 }
4709 ptr = stktable_data_ptr_idx(t, entry, dt, ++idx);
4710 }
4711 continue;
4712 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004713 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brun01928ae2021-06-30 16:24:04 +02004714 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004715 else
4716 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
4717
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004718 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004719 switch (stktable_data_types[dt].std_type) {
4720 case STD_T_SINT:
4721 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4722 break;
4723 case STD_T_UINT:
4724 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4725 break;
4726 case STD_T_ULL:
Emeric Brun01928ae2021-06-30 16:24:04 +02004727 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004728 break;
4729 case STD_T_FRQP:
Emeric Brun01928ae2021-06-30 16:24:04 +02004730 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004731 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004732 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004733 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02004734 case STD_T_DICT: {
4735 struct dict_entry *de;
4736 de = stktable_data_cast(ptr, std_t_dict);
4737 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
4738 break;
4739 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004740 }
4741 }
4742 chunk_appendf(msg, "\n");
4743
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004744 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004745 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004746
4747 return 1;
4748}
4749
Willy Tarreau3c69e082022-05-03 11:35:07 +02004750/* appctx context used by the "show table" command */
4751struct show_table_ctx {
4752 void *target; /* table we want to dump, or NULL for all */
4753 struct stktable *t; /* table being currently dumped (first if NULL) */
4754 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
4755 long long value[STKTABLE_FILTER_LEN]; /* value to compare against */
4756 signed char data_type[STKTABLE_FILTER_LEN]; /* type of data to compare, or -1 if none */
4757 signed char data_op[STKTABLE_FILTER_LEN]; /* operator (STD_OP_*) when data_type set */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004758 enum {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004759 STATE_NEXT = 0, /* px points to next table, entry=NULL */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004760 STATE_DUMP, /* px points to curr table, entry is valid, refcount held */
4761 STATE_DONE, /* done dumping */
4762 } state;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004763 char action; /* action on the table : one of STK_CLI_ACT_* */
4764};
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004765
4766/* Processes a single table entry matching a specific key passed in argument.
4767 * returns 0 if wants to be called again, 1 if has ended processing.
4768 */
4769static int table_process_entry_per_key(struct appctx *appctx, char **args)
4770{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004771 struct show_table_ctx *ctx = appctx->svcctx;
4772 struct stktable *t = ctx->target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004773 struct stksess *ts;
4774 uint32_t uint32_key;
4775 unsigned char ip6_key[sizeof(struct in6_addr)];
4776 long long value;
4777 int data_type;
4778 int cur_arg;
4779 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004780 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004781
Willy Tarreau9d008692019-08-09 11:21:01 +02004782 if (!*args[4])
4783 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004784
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004785 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004786 case SMP_T_IPV4:
4787 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02004788 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004789 break;
4790 case SMP_T_IPV6:
Christopher Fauletb7c962b2021-11-15 09:17:25 +01004791 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
4792 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004793 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004794 break;
4795 case SMP_T_SINT:
4796 {
4797 char *endptr;
4798 unsigned long val;
4799 errno = 0;
4800 val = strtoul(args[4], &endptr, 10);
4801 if ((errno == ERANGE && val == ULONG_MAX) ||
4802 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02004803 val > 0xffffffff)
4804 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004805 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02004806 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004807 break;
4808 }
4809 break;
4810 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02004811 static_table_key.key = args[4];
4812 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004813 break;
4814 default:
Willy Tarreau3c69e082022-05-03 11:35:07 +02004815 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004816 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02004817 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 +01004818 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02004819 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 +01004820 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02004821 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 +01004822 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004823 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004824 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004825 }
4826
4827 /* check permissions */
4828 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4829 return 1;
4830
Willy Tarreau3c69e082022-05-03 11:35:07 +02004831 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004832 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004833 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004834 if (!ts)
4835 return 1;
4836 chunk_reset(&trash);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004837 if (!table_dump_head_to_buffer(&trash, appctx, t, t)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004838 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004839 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004840 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004841 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004842 if (!table_dump_entry_to_buffer(&trash, appctx, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004843 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
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_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004848 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004849 break;
4850
4851 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004852 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004853 if (!ts)
4854 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004855
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004856 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004857 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004858 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004859 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004860 break;
4861
4862 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004863 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004864 if (!ts) {
4865 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004866 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004867 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004868 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004869 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
4870 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004871 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004872 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004873 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004874 return 1;
4875 }
4876
4877 data_type = stktable_get_data_type(args[cur_arg] + 5);
4878 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004879 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004880 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004881 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004882 return 1;
4883 }
4884
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004885 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004886 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004887 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004888 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004889 return 1;
4890 }
4891
4892 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004893 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004894 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004895 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004896 return 1;
4897 }
4898
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004899 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004900
4901 switch (stktable_data_types[data_type].std_type) {
4902 case STD_T_SINT:
4903 stktable_data_cast(ptr, std_t_sint) = value;
4904 break;
4905 case STD_T_UINT:
4906 stktable_data_cast(ptr, std_t_uint) = value;
4907 break;
4908 case STD_T_ULL:
4909 stktable_data_cast(ptr, std_t_ull) = value;
4910 break;
4911 case STD_T_FRQP:
4912 /* We set both the current and previous values. That way
4913 * the reported frequency is stable during all the period
4914 * then slowly fades out. This allows external tools to
4915 * push measures without having to update them too often.
4916 */
4917 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004918 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004919 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004920 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004921 using its internal lock */
4922 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004923 frqp->prev_ctr = 0;
4924 frqp->curr_ctr = value;
4925 break;
4926 }
4927 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004928 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004929 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004930 break;
4931
4932 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004933 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004934 }
4935 return 1;
4936}
4937
4938/* Prepares the appctx fields with the data-based filters from the command line.
4939 * Returns 0 if the dump can proceed, 1 if has ended processing.
4940 */
4941static int table_prepare_data_request(struct appctx *appctx, char **args)
4942{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004943 struct show_table_ctx *ctx = appctx->svcctx;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004944 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004945 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004946
Willy Tarreau3c69e082022-05-03 11:35:07 +02004947 if (ctx->action != STK_CLI_ACT_SHOW && ctx->action != STK_CLI_ACT_CLR)
Willy Tarreau9d008692019-08-09 11:21:01 +02004948 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004949
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004950 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
4951 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
4952 break;
4953 /* condition on stored data value */
Willy Tarreau3c69e082022-05-03 11:35:07 +02004954 ctx->data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
4955 if (ctx->data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004956 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004957
Willy Tarreau3c69e082022-05-03 11:35:07 +02004958 if (!((struct stktable *)ctx->target)->data_ofs[ctx->data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004959 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 +01004960
Willy Tarreau3c69e082022-05-03 11:35:07 +02004961 ctx->data_op[i] = get_std_op(args[4+3*i]);
4962 if (ctx->data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004963 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 +01004964
Willy Tarreau3c69e082022-05-03 11:35:07 +02004965 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 +01004966 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
4967 }
4968
4969 if (*args[3+3*i]) {
4970 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 +01004971 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004972
4973 /* OK we're done, all the fields are set */
4974 return 0;
4975}
4976
4977/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004978static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004979{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004980 struct show_table_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004981 int i;
4982
4983 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
Willy Tarreau3c69e082022-05-03 11:35:07 +02004984 ctx->data_type[i] = -1;
4985 ctx->target = NULL;
4986 ctx->entry = NULL;
4987 ctx->action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004988
4989 if (*args[2]) {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004990 ctx->t = ctx->target = stktable_find_by_name(args[2]);
Willy Tarreau3c69e082022-05-03 11:35:07 +02004991 if (!ctx->target)
Willy Tarreau9d008692019-08-09 11:21:01 +02004992 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004993 }
4994 else {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004995 ctx->t = stktables_list;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004996 if (ctx->action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004997 goto err_args;
4998 return 0;
4999 }
5000
5001 if (strcmp(args[3], "key") == 0)
5002 return table_process_entry_per_key(appctx, args);
5003 else if (strncmp(args[3], "data.", 5) == 0)
5004 return table_prepare_data_request(appctx, args);
5005 else if (*args[3])
5006 goto err_args;
5007
5008 return 0;
5009
5010err_args:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005011 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005012 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02005013 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 +01005014 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02005015 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 +01005016 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02005017 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005018 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02005019 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005020 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005021}
5022
5023/* This function is used to deal with table operations (dump or clear depending
5024 * on the action stored in appctx->private). It returns 0 if the output buffer is
5025 * full and it needs to be called again, otherwise non-zero.
5026 */
5027static int cli_io_handler_table(struct appctx *appctx)
5028{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005029 struct show_table_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02005030 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02005031 struct stream *s = __sc_strm(sc);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005032 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005033 int skip_entry;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005034 int show = ctx->action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005035
5036 /*
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005037 * We have 3 possible states in ctx->state :
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005038 * - STATE_NEXT : the proxy pointer points to the next table to
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005039 * dump, the entry pointer is NULL ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005040 * - STATE_DUMP : the proxy pointer points to the current table
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005041 * and the entry pointer points to the next entry to be dumped,
5042 * and the refcount on the next entry is held ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005043 * - STATE_DONE : nothing left to dump, the buffer may contain some
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005044 * data though.
5045 */
5046
Christopher Fauletda89e9b2023-01-04 14:11:10 +01005047 if (unlikely(sc_ic(sc)->flags & CF_SHUTW)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005048 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005049 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005050 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005051 }
5052 return 1;
5053 }
5054
5055 chunk_reset(&trash);
5056
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005057 while (ctx->state != STATE_DONE) {
5058 switch (ctx->state) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005059 case STATE_NEXT:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005060 if (!ctx->t ||
5061 (ctx->target &&
5062 ctx->t != ctx->target)) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005063 ctx->state = STATE_DONE;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005064 break;
5065 }
5066
Willy Tarreau3c69e082022-05-03 11:35:07 +02005067 if (ctx->t->size) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005068 if (show && !table_dump_head_to_buffer(&trash, appctx, ctx->t, ctx->target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005069 return 0;
5070
Willy Tarreau3c69e082022-05-03 11:35:07 +02005071 if (ctx->target &&
William Lallemand07a62f72017-05-24 00:57:40 +02005072 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005073 /* dump entries only if table explicitly requested */
Willy Tarreau76642222022-10-11 12:02:50 +02005074 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005075 eb = ebmb_first(&ctx->t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005076 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005077 ctx->entry = ebmb_entry(eb, struct stksess, key);
5078 ctx->entry->ref_cnt++;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005079 ctx->state = STATE_DUMP;
Willy Tarreau76642222022-10-11 12:02:50 +02005080 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005081 break;
5082 }
Willy Tarreau76642222022-10-11 12:02:50 +02005083 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005084 }
5085 }
Willy Tarreau3c69e082022-05-03 11:35:07 +02005086 ctx->t = ctx->t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005087 break;
5088
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005089 case STATE_DUMP:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005090 skip_entry = 0;
5091
Willy Tarreau3c69e082022-05-03 11:35:07 +02005092 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005093
Willy Tarreau3c69e082022-05-03 11:35:07 +02005094 if (ctx->data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005095 /* we're filtering on some data contents */
5096 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01005097 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005098 signed char op;
5099 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005100
Emeric Brun819fc6f2017-06-13 19:37:32 +02005101
Willy Tarreau2b64a352020-01-22 17:09:47 +01005102 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005103 if (ctx->data_type[i] == -1)
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005104 break;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005105 dt = ctx->data_type[i];
5106 ptr = stktable_data_ptr(ctx->t,
5107 ctx->entry,
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005108 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005109
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005110 data = 0;
5111 switch (stktable_data_types[dt].std_type) {
5112 case STD_T_SINT:
5113 data = stktable_data_cast(ptr, std_t_sint);
5114 break;
5115 case STD_T_UINT:
5116 data = stktable_data_cast(ptr, std_t_uint);
5117 break;
5118 case STD_T_ULL:
5119 data = stktable_data_cast(ptr, std_t_ull);
5120 break;
5121 case STD_T_FRQP:
5122 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau3c69e082022-05-03 11:35:07 +02005123 ctx->t->data_arg[dt].u);
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005124 break;
5125 }
5126
Willy Tarreau3c69e082022-05-03 11:35:07 +02005127 op = ctx->data_op[i];
5128 value = ctx->value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005129
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005130 /* skip the entry if the data does not match the test and the value */
5131 if ((data < value &&
5132 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
5133 (data == value &&
5134 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
5135 (data > value &&
5136 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
5137 skip_entry = 1;
5138 break;
5139 }
5140 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005141 }
5142
5143 if (show && !skip_entry &&
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005144 !table_dump_entry_to_buffer(&trash, appctx, ctx->t, ctx->entry)) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005145 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005146 return 0;
5147 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005148
Willy Tarreau3c69e082022-05-03 11:35:07 +02005149 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005150
Willy Tarreau76642222022-10-11 12:02:50 +02005151 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005152 ctx->entry->ref_cnt--;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005153
Willy Tarreau3c69e082022-05-03 11:35:07 +02005154 eb = ebmb_next(&ctx->entry->key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005155 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005156 struct stksess *old = ctx->entry;
5157 ctx->entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005158 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005159 __stksess_kill_if_expired(ctx->t, old);
5160 else if (!skip_entry && !ctx->entry->ref_cnt)
5161 __stksess_kill(ctx->t, old);
5162 ctx->entry->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02005163 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005164 break;
5165 }
5166
5167
5168 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005169 __stksess_kill_if_expired(ctx->t, ctx->entry);
5170 else if (!skip_entry && !ctx->entry->ref_cnt)
5171 __stksess_kill(ctx->t, ctx->entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005172
Willy Tarreau76642222022-10-11 12:02:50 +02005173 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005174
Willy Tarreau3c69e082022-05-03 11:35:07 +02005175 ctx->t = ctx->t->next;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005176 ctx->state = STATE_NEXT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005177 break;
5178
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005179 default:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005180 break;
5181 }
5182 }
5183 return 1;
5184}
5185
5186static void cli_release_show_table(struct appctx *appctx)
5187{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005188 struct show_table_ctx *ctx = appctx->svcctx;
5189
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005190 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005191 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005192 }
5193}
5194
Willy Tarreau6c011712023-01-06 16:09:58 +01005195static int stk_parse_stick_counters(char **args, int section_type, struct proxy *curpx,
5196 const struct proxy *defpx, const char *file, int line,
5197 char **err)
5198{
5199 char *error;
5200 int counters;
5201
5202 counters = strtol(args[1], &error, 10);
5203 if (*error != 0) {
5204 memprintf(err, "%s: '%s' is an invalid number", args[0], args[1]);
5205 return -1;
5206 }
5207
5208 if (counters < 0) {
5209 memprintf(err, "%s: the number of stick-counters may not be negative (was %d)", args[0], counters);
5210 return -1;
5211 }
5212
5213 global.tune.nb_stk_ctr = counters;
5214 return 0;
5215}
5216
5217/* This function creates the stk_ctr pools after the configuration parsing. It
5218 * returns 0 on success otherwise ERR_*. If nb_stk_ctr is 0, the pool remains
5219 * NULL.
5220 */
5221static int stkt_create_stk_ctr_pool(void)
5222{
5223 if (!global.tune.nb_stk_ctr)
5224 return 0;
5225
5226 pool_head_stk_ctr = create_pool("stk_ctr", sizeof(*((struct session*)0)->stkctr) * global.tune.nb_stk_ctr, MEM_F_SHARED);
5227 if (!pool_head_stk_ctr) {
5228 ha_alert("out of memory while creating the stick-counters pool.\n");
5229 return ERR_ABORT;
5230 }
5231 return 0;
5232}
5233
Willy Tarreau478331d2020-08-28 11:31:31 +02005234static void stkt_late_init(void)
5235{
5236 struct sample_fetch *f;
5237
5238 f = find_sample_fetch("src", strlen("src"));
5239 if (f)
5240 smp_fetch_src = f->process;
Willy Tarreau6c011712023-01-06 16:09:58 +01005241 hap_register_post_check(stkt_create_stk_ctr_pool);
Willy Tarreau478331d2020-08-28 11:31:31 +02005242}
5243
5244INITCALL0(STG_INIT, stkt_late_init);
5245
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005246/* register cli keywords */
5247static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005248 { { "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 },
5249 { { "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 },
5250 { { "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 +01005251 {{},}
5252}};
5253
Willy Tarreau0108d902018-11-25 19:14:37 +01005254INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005255
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005256static struct action_kw_list tcp_conn_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005257 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005258 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5259 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5260 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005261 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5262 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005263 { /* END */ }
5264}};
5265
Willy Tarreau0108d902018-11-25 19:14:37 +01005266INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
5267
Willy Tarreau620408f2016-10-21 16:37:51 +02005268static struct action_kw_list tcp_sess_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005269 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005270 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5271 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5272 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005273 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5274 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02005275 { /* END */ }
5276}};
5277
Willy Tarreau0108d902018-11-25 19:14:37 +01005278INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
5279
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005280static struct action_kw_list tcp_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005281 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005282 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5283 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5284 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005285 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5286 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005287 { /* END */ }
5288}};
5289
Willy Tarreau0108d902018-11-25 19:14:37 +01005290INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
5291
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005292static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005293 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005294 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5295 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5296 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005297 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5298 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005299 { /* END */ }
5300}};
5301
Willy Tarreau0108d902018-11-25 19:14:37 +01005302INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
5303
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005304static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005305 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005306 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5307 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5308 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005309 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5310 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005311 { /* END */ }
5312}};
5313
Willy Tarreau0108d902018-11-25 19:14:37 +01005314INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
5315
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005316static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005317 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005318 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5319 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5320 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005321 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5322 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005323 { /* END */ }
5324}};
5325
Willy Tarreau0108d902018-11-25 19:14:37 +01005326INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
5327
Christopher Fauleta9248042023-01-05 11:17:38 +01005328static struct action_kw_list http_after_res_kws = { { }, {
5329 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5330 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5331 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
5332 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5333 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
5334 { /* END */ }
5335}};
5336
5337INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
5338
Willy Tarreau7d562212016-11-25 16:10:05 +01005339/* Note: must not be declared <const> as its list will be overwritten.
5340 * Please take care of keeping this list alphabetically sorted.
5341 */
5342static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
5343 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5344 { "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 +02005345 { "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 +01005346 { "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 +01005347 { "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 +01005348 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5349 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5350 { "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 +02005351 { "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 +01005352 { "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 +02005353 { "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 +01005354 { "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 +01005355 { "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 +02005356 { "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 +01005357 { "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 +01005358 { "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 +01005359 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5360 { "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 +01005361 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5362 { "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 +01005363 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5364 { "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 +02005365 { "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 +01005366 { "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 +01005367 { "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 +01005368 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5369 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5370 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5371 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5372 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5373 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5374 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5375 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5376 { "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 +01005377 { "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 +01005378 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5379 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5380 { "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 +01005381 { "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 +01005382 { "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 +01005383 { "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 +01005384 { "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 +01005385 { "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 +01005386 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5387 { "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 +01005388 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5389 { "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 +01005390 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5391 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5392 { "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 +01005393 { "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 +01005394 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5395 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5396 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5397 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5398 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5399 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5400 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5401 { "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 +02005402 { "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 +01005403 { "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 +01005404 { "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 +01005405 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5406 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5407 { "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 +01005408 { "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 +01005409 { "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 +01005410 { "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 +01005411 { "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 +01005412 { "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 +01005413 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5414 { "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 +01005415 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5416 { "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 +01005417 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5418 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5419 { "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 +01005420 { "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 +01005421 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5422 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5423 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5424 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5425 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5426 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5427 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5428 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5429 { "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 +01005430 { "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 +01005431 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5432 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5433 { "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 +01005434 { "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 +01005435 { "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 +01005436 { "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 +01005437 { "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 +01005438 { "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 +01005439 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5440 { "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 +01005441 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5442 { "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 +01005443 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5444 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5445 { "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 +01005446 { "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 +01005447 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5448 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5449 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5450 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5451 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5452 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5453 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5454 { "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 +02005455 { "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 +01005456 { "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 +01005457 { "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 +01005458 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5459 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5460 { "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 +02005461 { "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 +01005462 { "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 +02005463 { "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 +01005464 { "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 +01005465 { "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 +02005466 { "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 +01005467 { "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 +01005468 { "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 +01005469 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5470 { "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 +01005471 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5472 { "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 +01005473 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5474 { "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 +02005475 { "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 +01005476 { "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 +01005477 { "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 +01005478 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5479 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5480 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5481 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5482 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5483 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5484 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5485 { /* END */ },
5486}};
5487
Willy Tarreau0108d902018-11-25 19:14:37 +01005488INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01005489
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005490/* Note: must not be declared <const> as its list will be overwritten */
5491static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02005492 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
5493 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5494 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5495 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5496 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5497 { "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 +02005498 { "table_expire", sample_conv_table_expire, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun877b0b52021-06-30 18:57:49 +02005499 { "table_gpt", sample_conv_table_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005500 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005501 { "table_gpc", sample_conv_table_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005502 { "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 +01005503 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005504 { "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 +02005505 { "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 +01005506 { "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 +02005507 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5508 { "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 +01005509 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5510 { "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 +02005511 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5512 { "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 +02005513 { "table_idle", sample_conv_table_idle, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005514 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5515 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5516 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5517 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5518 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5519 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005520 { /* END */ },
5521}};
5522
Willy Tarreau0108d902018-11-25 19:14:37 +01005523INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau6c011712023-01-06 16:09:58 +01005524
5525static struct cfg_kw_list cfg_kws = {{ },{
5526 { CFG_GLOBAL, "tune.stick-counters", stk_parse_stick_counters },
5527 { /* END */ }
5528}};
5529
5530INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);