blob: b11e94961464499cffe1821494504377fd88100c [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * Stick tables management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02005 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <string.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010015#include <errno.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010016
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <import/ebmbtree.h>
18#include <import/ebsttree.h>
19#include <import/ebistree.h>
20
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020021#include <haproxy/api.h>
Willy Tarreau3c69e082022-05-03 11:35:07 +020022#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020023#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Thayne McCombs92149f92020-11-20 01:28:26 -070026#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020029#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020030#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020033#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/pool.h>
35#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020036#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020038#include <haproxy/sc_strm.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020039#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020040#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020044#include <haproxy/tcp_rules.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020045#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/tools.h>
Willy Tarreaua4728582022-11-24 07:35:17 +010047#include <haproxy/xxhash.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010048
Emeric Brun3bd697e2010-01-04 15:23:48 +010049
Willy Tarreau12785782012-04-27 21:37:17 +020050/* structure used to return a table key built from a sample */
Emeric Brun819fc6f2017-06-13 19:37:32 +020051static THREAD_LOCAL struct stktable_key static_table_key;
Willy Tarreau478331d2020-08-28 11:31:31 +020052static int (*smp_fetch_src)(const struct arg *, struct sample *, const char *, void *);
Willy Tarreau6c011712023-01-06 16:09:58 +010053struct pool_head *pool_head_stk_ctr __read_mostly = NULL;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010054struct stktable *stktables_list;
55struct eb_root stktable_by_name = EB_ROOT;
56
Olivier Houchard52dabbc2018-11-14 17:54:36 +010057#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010058
59/* This function inserts stktable <t> into the tree of known stick-table.
60 * The stick-table ID is used as the storing key so it must already have
61 * been initialized.
62 */
63void stktable_store_name(struct stktable *t)
64{
65 t->name.key = t->id;
66 ebis_insert(&stktable_by_name, &t->name);
67}
68
69struct stktable *stktable_find_by_name(const char *name)
70{
71 struct ebpt_node *node;
72 struct stktable *t;
73
74 node = ebis_lookup(&stktable_by_name, name);
75 if (node) {
76 t = container_of(node, struct stktable, name);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +010077 if (strcmp(t->id, name) == 0)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010078 return t;
79 }
80
81 return NULL;
82}
83
Emeric Brun3bd697e2010-01-04 15:23:48 +010084/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020085 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
Willy Tarreau996f1a52022-10-11 16:19:35 +020086 * in table <t>. It's safe to call it under or out of a lock.
Emeric Brun3bd697e2010-01-04 15:23:48 +010087 */
Emeric Brun819fc6f2017-06-13 19:37:32 +020088void __stksess_free(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010089{
Willy Tarreau996f1a52022-10-11 16:19:35 +020090 HA_ATOMIC_DEC(&t->current);
Olivier Houchard52dabbc2018-11-14 17:54:36 +010091 pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
Emeric Brun3bd697e2010-01-04 15:23:48 +010092}
93
94/*
Emeric Brun819fc6f2017-06-13 19:37:32 +020095 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
96 * in table <t>.
97 * This function locks the table
98 */
99void stksess_free(struct stktable *t, struct stksess *ts)
100{
Thayne McCombs92149f92020-11-20 01:28:26 -0700101 void *data;
102 data = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_KEY);
103 if (data) {
Emeric Brun0e3457b2021-06-30 17:18:28 +0200104 dict_entry_unref(&server_key_dict, stktable_data_cast(data, std_t_dict));
105 stktable_data_cast(data, std_t_dict) = NULL;
Thayne McCombs92149f92020-11-20 01:28:26 -0700106 }
Willy Tarreau8d3c3332022-10-11 18:50:22 +0000107 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200108 __stksess_free(t, ts);
Willy Tarreau8d3c3332022-10-11 18:50:22 +0000109 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200110}
111
112/*
Willy Tarreauf6efda12010-08-03 20:34:06 +0200113 * Kill an stksess (only if its ref_cnt is zero).
114 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200115int __stksess_kill(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200116{
117 if (ts->ref_cnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200118 return 0;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200119
120 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +0200121 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200122 ebmb_delete(&ts->key);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200123 __stksess_free(t, ts);
124 return 1;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200125}
126
127/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200128 * Decrease the refcount if decrefcnt is not 0.
129 * and try to kill the stksess
130 * This function locks the table
131 */
132int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcnt)
133{
134 int ret;
135
Willy Tarreau76642222022-10-11 12:02:50 +0200136 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200137 if (decrefcnt)
138 ts->ref_cnt--;
139 ret = __stksess_kill(t, ts);
Willy Tarreau76642222022-10-11 12:02:50 +0200140 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200141
142 return ret;
143}
144
145/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200146 * Initialize or update the key in the sticky session <ts> present in table <t>
147 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100148 */
Willy Tarreau393379c2010-06-06 12:11:37 +0200149void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100150{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200151 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200152 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +0200154 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
155 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100156 }
157}
158
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100159/* return a shard number for key <key> of len <len> present in table <t>. This
160 * takes into account the presence or absence of a peers section with shards
161 * and the number of shards, the table's hash_seed, and of course the key. The
162 * caller must pass a valid <key> and <len>. The shard number to be used by the
163 * entry is returned (from 1 to nb_shards, otherwise 0 for none).
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200164 */
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100165int stktable_get_key_shard(struct stktable *t, const void *key, size_t len)
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200166{
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100167 /* no peers section or no shards in the peers section */
168 if (!t->peers.p || !t->peers.p->nb_shards)
169 return 0;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200170
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100171 return XXH64(key, len, t->hash_seed) % t->peers.p->nb_shards + 1;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200172}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100173
174/*
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200175 * Set the shard for <key> key of <ts> sticky session attached to <t> stick table.
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100176 * Use zero for stick-table without peers synchronisation.
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200177 */
178static void stksess_setkey_shard(struct stktable *t, struct stksess *ts,
179 struct stktable_key *key)
180{
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100181 size_t keylen;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200182
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100183 if (t->type == SMP_T_STR)
184 keylen = key->key_len;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200185 else
Willy Tarreaud5cae6a2022-11-29 17:36:44 +0100186 keylen = t->key_size;
187
188 ts->shard = stktable_get_key_shard(t, key->key, keylen);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200189}
190
191/*
Willy Tarreau393379c2010-06-06 12:11:37 +0200192 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
193 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200195static struct stksess *__stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196{
Willy Tarreau393379c2010-06-06 12:11:37 +0200197 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200198 ts->ref_cnt = 0;
Willy Tarreaue548a7a2022-11-29 16:08:35 +0100199 ts->shard = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200200 ts->key.node.leaf_p = NULL;
201 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200202 ts->upd.node.leaf_p = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200203 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100204 HA_RWLOCK_INIT(&ts->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100205 return ts;
206}
207
208/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200209 * Trash oldest <to_batch> sticky sessions from table <t>
Willy Tarreaudfe79252020-11-03 17:47:41 +0100210 * Returns number of trashed sticky sessions. It may actually trash less
211 * than expected if finding these requires too long a search time (e.g.
212 * most of them have ts->ref_cnt>0).
Emeric Brun3bd697e2010-01-04 15:23:48 +0100213 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200214int __stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100215{
216 struct stksess *ts;
217 struct eb32_node *eb;
Willy Tarreaudfe79252020-11-03 17:47:41 +0100218 int max_search = to_batch * 2; // no more than 50% misses
Emeric Brun3bd697e2010-01-04 15:23:48 +0100219 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200220 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100221
222 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
223
224 while (batched < to_batch) {
225
226 if (unlikely(!eb)) {
227 /* we might have reached the end of the tree, typically because
228 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200229 * half. Let's loop back to the beginning of the tree now if we
230 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200232 if (looped)
233 break;
234 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 eb = eb32_first(&t->exps);
236 if (likely(!eb))
237 break;
238 }
239
Willy Tarreaudfe79252020-11-03 17:47:41 +0100240 if (--max_search < 0)
241 break;
242
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200244 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100245 eb = eb32_next(eb);
246
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200247 /* don't delete an entry which is currently referenced */
248 if (ts->ref_cnt)
249 continue;
250
Willy Tarreau86257dc2010-06-06 12:57:10 +0200251 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100252
Willy Tarreau86257dc2010-06-06 12:57:10 +0200253 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100254 if (!tick_isset(ts->expire))
255 continue;
256
Willy Tarreau86257dc2010-06-06 12:57:10 +0200257 ts->exp.key = ts->expire;
258 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100259
Aleksey Ponomaryov59380212023-02-07 19:27:06 +0100260 /* the update might have jumped beyond the next element,
261 * possibly causing a wrapping. We need to check whether
262 * the next element should be used instead. If the next
263 * element doesn't exist it means we're on the right
264 * side and have to check the first one then. If it
265 * exists and is closer, we must use it, otherwise we
266 * use the current one.
267 */
268 if (!eb)
269 eb = eb32_first(&t->exps);
270
271 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200272 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100273
274 continue;
275 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100276
Willy Tarreauaea940e2010-06-06 11:56:36 +0200277 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200278 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200279 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200280 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100281 batched++;
282 }
283
284 return batched;
285}
286
287/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200288 * Trash oldest <to_batch> sticky sessions from table <t>
289 * Returns number of trashed sticky sessions.
290 * This function locks the table
291 */
292int stktable_trash_oldest(struct stktable *t, int to_batch)
293{
294 int ret;
295
Willy Tarreau76642222022-10-11 12:02:50 +0200296 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200297 ret = __stktable_trash_oldest(t, to_batch);
Willy Tarreau76642222022-10-11 12:02:50 +0200298 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200299
300 return ret;
301}
302/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200303 * Allocate and initialise a new sticky session.
304 * The new sticky session is returned or NULL in case of lack of memory.
305 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200306 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
Willy Tarreau996f1a52022-10-11 16:19:35 +0200307 * is not NULL, it is assigned to the new session. It must be called unlocked
308 * as it may rely on a lock to trash older entries.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100309 */
Willy Tarreau996f1a52022-10-11 16:19:35 +0200310struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100311{
312 struct stksess *ts;
Willy Tarreau996f1a52022-10-11 16:19:35 +0200313 unsigned int current;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314
Willy Tarreau996f1a52022-10-11 16:19:35 +0200315 current = HA_ATOMIC_FETCH_ADD(&t->current, 1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100316
Willy Tarreau996f1a52022-10-11 16:19:35 +0200317 if (unlikely(current >= t->size)) {
318 /* the table was already full, we may have to purge entries */
319 if (t->nopurge || !stktable_trash_oldest(t, (t->size >> 8) + 1)) {
320 HA_ATOMIC_DEC(&t->current);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100321 return NULL;
Willy Tarreau996f1a52022-10-11 16:19:35 +0200322 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323 }
324
Willy Tarreaubafbe012017-11-24 17:34:44 +0100325 ts = pool_alloc(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100326 if (ts) {
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100327 ts = (void *)ts + round_ptr_size(t->data_size);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200328 __stksess_init(t, ts);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200329 if (key) {
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200330 stksess_setkey(t, ts, key);
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200331 stksess_setkey_shard(t, ts, key);
332 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100333 }
334
335 return ts;
336}
337
338/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200339 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200340 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100341 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200342struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100343{
344 struct ebmb_node *eb;
345
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200346 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200347 eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348 else
349 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
350
351 if (unlikely(!eb)) {
352 /* no session found */
353 return NULL;
354 }
355
Willy Tarreau86257dc2010-06-06 12:57:10 +0200356 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357}
358
Emeric Brun819fc6f2017-06-13 19:37:32 +0200359/*
360 * Looks in table <t> for a sticky session matching key <key>.
361 * Returns pointer on requested sticky session or NULL if none was found.
362 * The refcount of the found entry is increased and this function
363 * is protected using the table lock
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200364 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200365struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200366{
367 struct stksess *ts;
368
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200369 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200370 ts = __stktable_lookup_key(t, key);
371 if (ts)
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200372 HA_ATOMIC_INC(&ts->ref_cnt);
373 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200374
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200375 return ts;
376}
377
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200378/*
379 * Looks in table <t> for a sticky session with same key as <ts>.
380 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100381 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200382struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100384 struct ebmb_node *eb;
385
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200386 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200387 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100388 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200389 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200391 if (unlikely(!eb))
392 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200394 return ebmb_entry(eb, struct stksess, key);
395}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100396
Emeric Brun819fc6f2017-06-13 19:37:32 +0200397/*
398 * Looks in table <t> for a sticky session with same key as <ts>.
399 * Returns pointer on requested sticky session or NULL if none was found.
400 * The refcount of the found entry is increased and this function
401 * is protected using the table lock
402 */
403struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
404{
405 struct stksess *lts;
406
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200407 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200408 lts = __stktable_lookup(t, ts);
409 if (lts)
Willy Tarreaua7d6a132022-10-11 15:42:54 +0200410 HA_ATOMIC_INC(&lts->ref_cnt);
411 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200412
413 return lts;
414}
415
Willy Tarreaucb183642010-06-06 17:58:34 +0200416/* Update the expiration timer for <ts> but do not touch its expiration node.
417 * The table's expiration timer is updated if set.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200418 * The node will be also inserted into the update tree if needed, at a position
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000419 * depending if the update is a local or coming from a remote node.
420 * If <decrefcnt> is set, the ts entry's ref_cnt will be decremented. The table's
421 * write lock may be taken.
Willy Tarreaucb183642010-06-06 17:58:34 +0200422 */
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000423void stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire, int decrefcnt)
Willy Tarreaucb183642010-06-06 17:58:34 +0200424{
Emeric Brun85e77c72010-09-23 18:16:52 +0200425 struct eb32_node * eb;
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000426 int locked = 0;
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000427
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000428 if (expire != HA_ATOMIC_LOAD(&ts->expire)) {
429 /* we'll need to set the expiration and to wake up the expiration timer .*/
430 HA_ATOMIC_STORE(&ts->expire, expire);
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000431 stktable_requeue_exp(t, ts);
Willy Tarreaucb183642010-06-06 17:58:34 +0200432 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200433
Emeric Brun819fc6f2017-06-13 19:37:32 +0200434 /* If sync is enabled */
435 if (t->sync_task) {
436 if (local) {
437 /* If this entry is not in the tree
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000438 * or not scheduled for at least one peer.
439 */
440 if (!locked++)
441 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
442
Emeric Brun819fc6f2017-06-13 19:37:32 +0200443 if (!ts->upd.node.leaf_p
444 || (int)(t->commitupdate - ts->upd.key) >= 0
445 || (int)(ts->upd.key - t->localupdate) >= 0) {
446 ts->upd.key = ++t->update;
447 t->localupdate = t->update;
448 eb32_delete(&ts->upd);
449 eb = eb32_insert(&t->updates, &ts->upd);
450 if (eb != &ts->upd) {
451 eb32_delete(eb);
452 eb32_insert(&t->updates, &ts->upd);
453 }
Emeric Brunaaf58602015-06-15 17:23:30 +0200454 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200455 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
Emeric Brun85e77c72010-09-23 18:16:52 +0200456 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200457 else {
458 /* If this entry is not in the tree */
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000459 if (!locked++)
460 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
461
Emeric Brun819fc6f2017-06-13 19:37:32 +0200462 if (!ts->upd.node.leaf_p) {
463 ts->upd.key= (++t->update)+(2147483648U);
464 eb = eb32_insert(&t->updates, &ts->upd);
465 if (eb != &ts->upd) {
466 eb32_delete(eb);
467 eb32_insert(&t->updates, &ts->upd);
468 }
469 }
470 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200471 }
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000472
Willy Tarreaua7536ef2022-10-11 18:31:04 +0000473 if (decrefcnt) {
474 if (locked)
475 ts->ref_cnt--;
476 else {
477 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
478 HA_ATOMIC_DEC(&ts->ref_cnt);
479 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
480 }
481 }
482
483 if (locked)
484 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreaucb183642010-06-06 17:58:34 +0200485}
486
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200487/* Update the expiration timer for <ts> but do not touch its expiration node.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200488 * The table's expiration timer is updated using the date of expiration coming from
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200489 * <t> stick-table configuration.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200490 * The node will be also inserted into the update tree if needed, at a position
491 * considering the update is coming from a remote node
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200492 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200493void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
494{
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000495 stktable_touch_with_exp(t, ts, 0, ts->expire, decrefcnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200496}
497
498/* Update the expiration timer for <ts> but do not touch its expiration node.
499 * The table's expiration timer is updated using the date of expiration coming from
500 * <t> stick-table configuration.
501 * The node will be also inserted into the update tree if needed, at a position
502 * considering the update was made locally
503 */
504void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200505{
506 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
507
Willy Tarreau9f5cb432022-10-11 18:17:58 +0000508 stktable_touch_with_exp(t, ts, 1, expire, decrefcnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200509}
Willy Tarreau4be073b2022-10-11 18:10:27 +0000510/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL.
511 * Note that we still need to take the read lock because a number of other places
512 * (including in Lua and peers) update the ref_cnt non-atomically under the write
513 * lock.
514 */
Willy Tarreau43e90352018-06-27 06:25:57 +0200515static void stktable_release(struct stktable *t, struct stksess *ts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200516{
Willy Tarreau43e90352018-06-27 06:25:57 +0200517 if (!ts)
518 return;
Willy Tarreau4be073b2022-10-11 18:10:27 +0000519 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
520 HA_ATOMIC_DEC(&ts->ref_cnt);
521 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200522}
523
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200524/* Insert new sticky session <ts> in the table. It is assumed that it does not
525 * yet exist (the caller must check this). The table's timeout is updated if it
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200526 * is set. <ts> is returned if properly inserted, otherwise the one already
527 * present if any.
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200528 */
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200529struct stksess *__stktable_store(struct stktable *t, struct stksess *ts)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200530{
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200531 struct ebmb_node *eb;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100532
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200533 eb = ebmb_insert(&t->keys, &ts->key, t->key_size);
534 if (likely(eb == &ts->key)) {
535 ts->exp.key = ts->expire;
536 eb32_insert(&t->exps, &ts->exp);
537 }
Willy Tarreaud2d3fd92022-10-11 15:09:46 +0200538 return ebmb_entry(eb, struct stksess, key); // most commonly this is <ts>
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200539}
540
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000541/* requeues the table's expiration task to take the recently added <ts> into
542 * account. This is performed atomically and doesn't require any lock.
543 */
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000544void stktable_requeue_exp(struct stktable *t, const struct stksess *ts)
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000545{
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000546 int old_exp, new_exp;
547 int expire = ts->expire;
548
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000549 if (!t->expire)
550 return;
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000551
Willy Tarreau63427142022-11-14 17:33:02 +0100552 /* set the task's expire to the newest expiration date. */
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000553 old_exp = HA_ATOMIC_LOAD(&t->exp_task->expire);
Willy Tarreau3238f792022-11-14 17:54:07 +0100554 new_exp = tick_first(expire, old_exp);
555
556 /* let's not go further if we're already up to date */
557 if (new_exp == old_exp)
558 return;
559
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100560 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
561
Willy Tarreau3238f792022-11-14 17:54:07 +0100562 while (new_exp != old_exp &&
563 !HA_ATOMIC_CAS(&t->exp_task->expire, &old_exp, new_exp)) {
564 __ha_cpu_relax();
Willy Tarreau63427142022-11-14 17:33:02 +0100565 new_exp = tick_first(expire, old_exp);
Willy Tarreau3238f792022-11-14 17:54:07 +0100566 }
Willy Tarreaudbae89e2022-10-12 10:00:50 +0000567
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000568 task_queue(t->exp_task);
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100569
570 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreaueb23e3e2022-10-12 09:56:08 +0000571}
572
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200573/* Returns a valid or initialized stksess for the specified stktable_key in the
574 * specified table, or NULL if the key was NULL, or if no entry was found nor
Willy Tarreau47f22972022-10-11 15:22:42 +0200575 * could be created. The entry's expiration is updated. This function locks the
576 * table, and the refcount of the entry is increased.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200577 */
Willy Tarreau47f22972022-10-11 15:22:42 +0200578struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200579{
Willy Tarreau175aa062022-10-11 15:13:46 +0200580 struct stksess *ts, *ts2;
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200581
582 if (!key)
583 return NULL;
584
Willy Tarreau47f22972022-10-11 15:22:42 +0200585 ts = stktable_lookup_key(table, key);
586 if (ts)
587 return ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200588
Willy Tarreau996f1a52022-10-11 16:19:35 +0200589 /* No such entry exists, let's try to create a new one. this doesn't
590 * require locking yet.
591 */
592
593 ts = stksess_new(table, key);
594 if (!ts)
595 return NULL;
596
597 /* Now we're certain to have a ts. We need to store it. For this we'll
Willy Tarreau47f22972022-10-11 15:22:42 +0200598 * need an exclusive access. We don't need an atomic upgrade, this is
599 * rare and an unlock+lock sequence will do the job fine. Given that
600 * this will not be atomic, the missing entry might appear in the mean
601 * tome so we have to be careful that the one we try to insert is the
602 * one we find.
603 */
Willy Tarreau47f22972022-10-11 15:22:42 +0200604
Willy Tarreau996f1a52022-10-11 16:19:35 +0200605 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreau47f22972022-10-11 15:22:42 +0200606
607 ts2 = __stktable_store(table, ts);
608 if (unlikely(ts2 != ts)) {
609 /* another entry was added in the mean time, let's
610 * switch to it.
611 */
612 __stksess_free(table, ts);
613 ts = ts2;
614 }
615
616 HA_ATOMIC_INC(&ts->ref_cnt);
Willy Tarreau76642222022-10-11 12:02:50 +0200617 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200618
Willy Tarreaucbdb5282022-10-12 10:04:01 +0000619 stktable_requeue_exp(table, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200620 return ts;
621}
622
623/* Lookup for an entry with the same key and store the submitted
Willy Tarreaue6288522022-10-12 09:13:14 +0000624 * stksess if not found. This function locks the table either shared or
625 * exclusively, and the refcount of the entry is increased.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200626 */
Willy Tarreaue6288522022-10-12 09:13:14 +0000627struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200628{
629 struct stksess *ts;
630
Willy Tarreaue6288522022-10-12 09:13:14 +0000631 HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200632 ts = __stktable_lookup(table, nts);
Willy Tarreaue6288522022-10-12 09:13:14 +0000633 if (ts) {
634 HA_ATOMIC_INC(&ts->ref_cnt);
635 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &table->lock);
636 return ts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200637 }
Willy Tarreaue6288522022-10-12 09:13:14 +0000638 ts = nts;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200639
Willy Tarreaue6288522022-10-12 09:13:14 +0000640 /* let's increment it before switching to exclusive */
641 HA_ATOMIC_INC(&ts->ref_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200642
Willy Tarreaue6288522022-10-12 09:13:14 +0000643 if (HA_RWLOCK_TRYRDTOSK(STK_TABLE_LOCK, &table->lock) != 0) {
644 /* upgrade to seek lock failed, let's drop and take */
645 HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &table->lock);
646 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &table->lock);
647 }
648 else
649 HA_RWLOCK_SKTOWR(STK_TABLE_LOCK, &table->lock);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200650
Willy Tarreaue6288522022-10-12 09:13:14 +0000651 /* now we're write-locked */
652
653 __stktable_store(table, ts);
654 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreaucbdb5282022-10-12 10:04:01 +0000655
656 stktable_requeue_exp(table, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200657 return ts;
658}
Willy Tarreaue6288522022-10-12 09:13:14 +0000659
Emeric Brun3bd697e2010-01-04 15:23:48 +0100660/*
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100661 * Task processing function to trash expired sticky sessions. A pointer to the
662 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100663 */
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100664struct task *process_table_expire(struct task *task, void *context, unsigned int state)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100665{
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100666 struct stktable *t = context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100667 struct stksess *ts;
668 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200669 int looped = 0;
Willy Tarreau63427142022-11-14 17:33:02 +0100670 int exp_next;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100671
Willy Tarreau76642222022-10-11 12:02:50 +0200672 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100673 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
674
675 while (1) {
676 if (unlikely(!eb)) {
677 /* we might have reached the end of the tree, typically because
678 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200679 * half. Let's loop back to the beginning of the tree now if we
680 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100681 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200682 if (looped)
683 break;
684 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100685 eb = eb32_first(&t->exps);
686 if (likely(!eb))
687 break;
688 }
689
690 if (likely(tick_is_lt(now_ms, eb->key))) {
691 /* timer not expired yet, revisit it later */
Willy Tarreau63427142022-11-14 17:33:02 +0100692 exp_next = eb->key;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100693 goto out_unlock;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100694 }
695
696 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200697 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100698 eb = eb32_next(eb);
699
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200700 /* don't delete an entry which is currently referenced */
701 if (ts->ref_cnt)
702 continue;
703
Willy Tarreau86257dc2010-06-06 12:57:10 +0200704 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100705
706 if (!tick_is_expired(ts->expire, now_ms)) {
707 if (!tick_isset(ts->expire))
708 continue;
709
Willy Tarreau86257dc2010-06-06 12:57:10 +0200710 ts->exp.key = ts->expire;
711 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100712
Aleksey Ponomaryov59380212023-02-07 19:27:06 +0100713 /* the update might have jumped beyond the next element,
714 * possibly causing a wrapping. We need to check whether
715 * the next element should be used instead. If the next
716 * element doesn't exist it means we're on the right
717 * side and have to check the first one then. If it
718 * exists and is closer, we must use it, otherwise we
719 * use the current one.
720 */
721 if (!eb)
722 eb = eb32_first(&t->exps);
723
724 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200725 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100726 continue;
727 }
728
729 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200730 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200731 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200732 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100733 }
734
735 /* We have found no task to expire in any tree */
Willy Tarreau63427142022-11-14 17:33:02 +0100736 exp_next = TICK_ETERNITY;
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100737
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100738out_unlock:
Willy Tarreaufbb934d2022-11-14 18:02:44 +0100739 task->expire = exp_next;
Willy Tarreau76642222022-10-11 12:02:50 +0200740 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100741 return task;
742}
743
Willy Tarreauaea940e2010-06-06 11:56:36 +0200744/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100745int stktable_init(struct stktable *t)
746{
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200747 int peers_retval = 0;
Willy Tarreau56460ee2022-11-28 18:53:06 +0100748
749 t->hash_seed = XXH64(t->id, t->idlen, 0);
750
Emeric Brun3bd697e2010-01-04 15:23:48 +0100751 if (t->size) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200752 t->keys = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100753 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100754 t->updates = EB_ROOT_UNIQUE;
Amaury Denoyelle3e064882022-10-12 16:47:59 +0200755 HA_RWLOCK_INIT(&t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100756
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100757 t->pool = create_pool("sticktables", sizeof(struct stksess) + round_ptr_size(t->data_size) + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100758
Emeric Brun3bd697e2010-01-04 15:23:48 +0100759 if ( t->expire ) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200760 t->exp_task = task_new_anywhere();
Willy Tarreau848522f2018-10-15 11:12:15 +0200761 if (!t->exp_task)
762 return 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100763 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100764 t->exp_task->context = (void *)t;
765 }
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200766 if (t->peers.p && t->peers.p->peers_fe && !(t->peers.p->peers_fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) {
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200767 peers_retval = peers_register_table(t->peers.p, t);
Emeric Brun32da3c42010-09-23 18:39:19 +0200768 }
769
Remi Tricot-Le Breton208ff012021-05-12 17:39:04 +0200770 return (t->pool != NULL) && !peers_retval;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100771 }
772 return 1;
773}
774
775/*
776 * Configuration keywords of known table types
777 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200778struct stktable_type stktable_types[SMP_TYPES] = {
779 [SMP_T_SINT] = { "integer", 0, 4 },
780 [SMP_T_IPV4] = { "ip", 0, 4 },
781 [SMP_T_IPV6] = { "ipv6", 0, 16 },
782 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
783 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
784};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100785
786/*
787 * Parse table type configuration.
788 * Returns 0 on successful parsing, else 1.
789 * <myidx> is set at next configuration <args> index.
790 */
William Lallemand3f210972023-04-13 14:33:52 +0200791int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size, const char *file, int linenum)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100792{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200793 for (*type = 0; *type < SMP_TYPES; (*type)++) {
794 if (!stktable_types[*type].kw)
795 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100796 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
797 continue;
798
799 *key_size = stktable_types[*type].default_size;
800 (*myidx)++;
801
Willy Tarreauaea940e2010-06-06 11:56:36 +0200802 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100803 if (strcmp("len", args[*myidx]) == 0) {
William Lallemand3f210972023-04-13 14:33:52 +0200804 char *stop;
805
Emeric Brun3bd697e2010-01-04 15:23:48 +0100806 (*myidx)++;
William Lallemand3f210972023-04-13 14:33:52 +0200807 *key_size = strtol(args[*myidx], &stop, 10);
808 if (*stop != '\0' || !*key_size) {
809 ha_alert("parsing [%s:%d] : 'len' expects a positive integer argument.\n", file, linenum);
810 return 1;
811 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200812 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200813 /* null terminated string needs +1 for '\0'. */
814 (*key_size)++;
815 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100816 (*myidx)++;
817 }
818 }
819 return 0;
820 }
William Lallemand3f210972023-04-13 14:33:52 +0200821 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", file, linenum, args[0], args[*myidx]);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100822 return 1;
823}
824
Emeric Brunc64a2a32021-06-30 18:01:02 +0200825/* reserve some space for data type <type>, there is 2 optionnals
826 * argument at <sa> and <sa2> to configure this data type and
827 * they can be NULL if unused for a given type.
828 * Returns PE_NONE (0) if OK or an error code among :
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200829 * - PE_ENUM_OOR if <type> does not exist
830 * - PE_EXIST if <type> is already registered
Emeric Brunc64a2a32021-06-30 18:01:02 +0200831 * - PE_ARG_NOT_USE if <sa>/<sa2> was provided but not expected
832 * - PE_ARG_MISSING if <sa>/<sa2> was expected but not provided
833 * - PE_ARG_VALUE_OOR if type is an array and <sa> it out of array size range.
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200834 */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200835int stktable_alloc_data_type(struct stktable *t, int type, const char *sa, const char *sa2)
836
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200837{
838 if (type >= STKTABLE_DATA_TYPES)
839 return PE_ENUM_OOR;
840
841 if (t->data_ofs[type])
842 /* already allocated */
843 return PE_EXIST;
844
Emeric Brunc64a2a32021-06-30 18:01:02 +0200845 t->data_nbelem[type] = 1;
846 if (stktable_data_types[type].is_array) {
847 /* arrays take their element count on first argument */
848 if (!sa)
849 return PE_ARG_MISSING;
850 t->data_nbelem[type] = atoi(sa);
851 if (!t->data_nbelem[type] || (t->data_nbelem[type] > STKTABLE_MAX_DT_ARRAY_SIZE))
852 return PE_ARG_VALUE_OOR;
853 sa = sa2;
854 }
855
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200856 switch (stktable_data_types[type].arg_type) {
857 case ARG_T_NONE:
858 if (sa)
859 return PE_ARG_NOT_USED;
860 break;
861 case ARG_T_INT:
862 if (!sa)
863 return PE_ARG_MISSING;
864 t->data_arg[type].i = atoi(sa);
865 break;
866 case ARG_T_DELAY:
867 if (!sa)
868 return PE_ARG_MISSING;
869 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
870 if (sa)
871 return PE_ARG_INVC; /* invalid char */
872 break;
873 }
874
Emeric Brunc64a2a32021-06-30 18:01:02 +0200875 t->data_size += t->data_nbelem[type] * stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200876 t->data_ofs[type] = -t->data_size;
877 return PE_NONE;
878}
879
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100880/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100881 * Parse a line with <linenum> as number in <file> configuration file to configure
882 * the stick-table with <t> as address and <id> as ID.
883 * <peers> provides the "peers" section pointer only if this function is called
884 * from a "peers" section.
885 * <nid> is the stick-table name which is sent over the network. It must be equal
886 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
887 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500888 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100889 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
890 */
891int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100892 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100893{
894 int err_code = 0;
895 int idx = 1;
896 unsigned int val;
897
898 if (!id || !*id) {
899 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
900 err_code |= ERR_ALERT | ERR_ABORT;
901 goto out;
902 }
903
904 /* Store the "peers" section if this function is called from a "peers" section. */
905 if (peers) {
906 t->peers.p = peers;
907 idx++;
908 }
909
910 t->id = id;
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200911 t->idlen = strlen(id);
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100912 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100913 t->type = (unsigned int)-1;
914 t->conf.file = file;
915 t->conf.line = linenum;
916
917 while (*args[idx]) {
918 const char *err;
919
920 if (strcmp(args[idx], "size") == 0) {
921 idx++;
922 if (!*(args[idx])) {
923 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
924 file, linenum, args[0], args[idx-1]);
925 err_code |= ERR_ALERT | ERR_FATAL;
926 goto out;
927 }
928 if ((err = parse_size_err(args[idx], &t->size))) {
929 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
930 file, linenum, args[0], *err, args[idx-1]);
931 err_code |= ERR_ALERT | ERR_FATAL;
932 goto out;
933 }
934 idx++;
935 }
936 /* This argument does not exit in "peers" section. */
937 else if (!peers && strcmp(args[idx], "peers") == 0) {
938 idx++;
939 if (!*(args[idx])) {
940 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
941 file, linenum, args[0], args[idx-1]);
942 err_code |= ERR_ALERT | ERR_FATAL;
943 goto out;
944 }
945 t->peers.name = strdup(args[idx++]);
946 }
947 else if (strcmp(args[idx], "expire") == 0) {
948 idx++;
949 if (!*(args[idx])) {
950 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
951 file, linenum, args[0], args[idx-1]);
952 err_code |= ERR_ALERT | ERR_FATAL;
953 goto out;
954 }
955 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200956 if (err == PARSE_TIME_OVER) {
957 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
958 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100959 err_code |= ERR_ALERT | ERR_FATAL;
960 goto out;
961 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200962 else if (err == PARSE_TIME_UNDER) {
963 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
964 file, linenum, args[0], args[idx], args[idx-1]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto out;
967 }
968 else if (err) {
969 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
970 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100971 err_code |= ERR_ALERT | ERR_FATAL;
972 goto out;
973 }
974 t->expire = val;
975 idx++;
976 }
977 else if (strcmp(args[idx], "nopurge") == 0) {
978 t->nopurge = 1;
979 idx++;
980 }
981 else if (strcmp(args[idx], "type") == 0) {
982 idx++;
William Lallemand3f210972023-04-13 14:33:52 +0200983 if (stktable_parse_type(args, &idx, &t->type, &t->key_size, file, linenum) != 0) {
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100984 err_code |= ERR_ALERT | ERR_FATAL;
985 goto out;
986 }
987 /* idx already points to next arg */
988 }
989 else if (strcmp(args[idx], "store") == 0) {
990 int type, err;
Emeric Brunc64a2a32021-06-30 18:01:02 +0200991 char *cw, *nw, *sa, *sa2;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100992
993 idx++;
994 nw = args[idx];
995 while (*nw) {
996 /* the "store" keyword supports a comma-separated list */
997 cw = nw;
998 sa = NULL; /* store arg */
Emeric Brunc64a2a32021-06-30 18:01:02 +0200999 sa2 = NULL;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001000 while (*nw && *nw != ',') {
1001 if (*nw == '(') {
1002 *nw = 0;
1003 sa = ++nw;
1004 while (*nw != ')') {
1005 if (!*nw) {
1006 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
1007 file, linenum, args[0], cw);
1008 err_code |= ERR_ALERT | ERR_FATAL;
1009 goto out;
1010 }
Emeric Brunc64a2a32021-06-30 18:01:02 +02001011 if (*nw == ',') {
1012 *nw = '\0';
1013 sa2 = nw + 1;
1014 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001015 nw++;
1016 }
1017 *nw = '\0';
1018 }
1019 nw++;
1020 }
1021 if (*nw)
1022 *nw++ = '\0';
1023 type = stktable_get_data_type(cw);
1024 if (type < 0) {
1025 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
1026 file, linenum, args[0], cw);
1027 err_code |= ERR_ALERT | ERR_FATAL;
1028 goto out;
1029 }
1030
Emeric Brunc64a2a32021-06-30 18:01:02 +02001031 err = stktable_alloc_data_type(t, type, sa, sa2);
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001032 switch (err) {
1033 case PE_NONE: break;
1034 case PE_EXIST:
1035 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
1036 file, linenum, args[0], cw);
1037 err_code |= ERR_WARN;
1038 break;
1039
1040 case PE_ARG_MISSING:
1041 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
1042 file, linenum, args[0], cw);
1043 err_code |= ERR_ALERT | ERR_FATAL;
1044 goto out;
1045
1046 case PE_ARG_NOT_USED:
1047 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
1048 file, linenum, args[0], cw);
1049 err_code |= ERR_ALERT | ERR_FATAL;
1050 goto out;
Emeric Brunc64a2a32021-06-30 18:01:02 +02001051 case PE_ARG_VALUE_OOR:
1052 ha_alert("parsing [%s:%d] : %s: array size is out of allowed range (1-%d) for store option '%s'.\n",
1053 file, linenum, args[0], STKTABLE_MAX_DT_ARRAY_SIZE, cw);
1054 err_code |= ERR_ALERT | ERR_FATAL;
1055 goto out;
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001056
1057 default:
1058 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
1059 file, linenum, args[0], cw);
1060 err_code |= ERR_ALERT | ERR_FATAL;
1061 goto out;
1062 }
1063 }
1064 idx++;
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001065 if (t->data_ofs[STKTABLE_DT_GPT] && t->data_ofs[STKTABLE_DT_GPT0]) {
1066 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpt' and 'gpt0' in a same table is not permitted as 'gpt' overrides 'gpt0'.\n",
1067 file, linenum, args[0]);
1068 err_code |= ERR_ALERT | ERR_FATAL;
1069 goto out;
1070 }
Emeric Brun726783d2021-06-30 19:06:43 +02001071 else if (t->data_ofs[STKTABLE_DT_GPC] && (t->data_ofs[STKTABLE_DT_GPC0] || t->data_ofs[STKTABLE_DT_GPC1])) {
1072 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc' and 'gpc[0/1]' in a same table is not permitted as 'gpc' overrides 'gpc[0/1]'.\n",
1073 file, linenum, args[0]);
1074 err_code |= ERR_ALERT | ERR_FATAL;
1075 goto out;
1076 }
1077 else if (t->data_ofs[STKTABLE_DT_GPC_RATE] && (t->data_ofs[STKTABLE_DT_GPC0_RATE] || t->data_ofs[STKTABLE_DT_GPC1_RATE])) {
1078 ha_alert("parsing [%s:%d] : %s: simultaneous usage of 'gpc_rate' and 'gpc[0/1]_rate' in a same table is not permitted as 'gpc_rate' overrides 'gpc[0/1]_rate'.\n",
1079 file, linenum, args[0]);
1080 err_code |= ERR_ALERT | ERR_FATAL;
1081 goto out;
1082 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001083 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001084 else if (strcmp(args[idx], "srvkey") == 0) {
1085 char *keytype;
1086 idx++;
1087 keytype = args[idx];
1088 if (strcmp(keytype, "name") == 0) {
1089 t->server_key_type = STKTABLE_SRV_NAME;
1090 }
1091 else if (strcmp(keytype, "addr") == 0) {
1092 t->server_key_type = STKTABLE_SRV_ADDR;
1093 }
1094 else {
1095 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
1096 file, linenum, args[0], keytype);
1097 err_code |= ERR_ALERT | ERR_FATAL;
1098 goto out;
1099
1100 }
1101 idx++;
1102 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +01001103 else {
1104 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
1105 file, linenum, args[0], args[idx]);
1106 err_code |= ERR_ALERT | ERR_FATAL;
1107 goto out;
1108 }
1109 }
1110
1111 if (!t->size) {
1112 ha_alert("parsing [%s:%d] : %s: missing size.\n",
1113 file, linenum, args[0]);
1114 err_code |= ERR_ALERT | ERR_FATAL;
1115 goto out;
1116 }
1117
1118 if (t->type == (unsigned int)-1) {
1119 ha_alert("parsing [%s:%d] : %s: missing type.\n",
1120 file, linenum, args[0]);
1121 err_code |= ERR_ALERT | ERR_FATAL;
1122 goto out;
1123 }
1124
1125 out:
1126 return err_code;
1127}
1128
Willy Tarreau8fed9032014-07-03 17:02:46 +02001129/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001130 * Note that the sample *is* modified and that the returned key may point
1131 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +02001132 * Returns NULL if the sample could not be converted (eg: no matching type),
1133 * otherwise a pointer to the static stktable_key filled with what is needed
1134 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001135 */
Willy Tarreau8fed9032014-07-03 17:02:46 +02001136struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001137{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001138 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001139 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +02001140 return NULL;
1141
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001142 /* Fill static_table_key. */
1143 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +02001144
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001145 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +02001146 static_table_key.key = &smp->data.u.ipv4;
1147 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001148 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001149
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001150 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +02001151 static_table_key.key = &smp->data.u.ipv6;
1152 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001153 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001154
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001155 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001156 /* The stick table require a 32bit unsigned int, "sint" is a
1157 * signed 64 it, so we can convert it inplace.
1158 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001159 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001160 static_table_key.key = &smp->data.u.sint;
1161 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001162 break;
1163
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001164 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001165 if (!smp_make_safe(smp))
1166 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001167 static_table_key.key = smp->data.u.str.area;
1168 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001169 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001170
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001171 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001172 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001173 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001174 if (!smp_make_rw(smp))
1175 return NULL;
1176
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001177 if (smp->data.u.str.size < t->key_size)
1178 if (!smp_dup(smp))
1179 return NULL;
1180 if (smp->data.u.str.size < t->key_size)
1181 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1183 t->key_size - smp->data.u.str.data);
1184 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001185 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001186 static_table_key.key = smp->data.u.str.area;
1187 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001188 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001189
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001190 default: /* impossible case. */
1191 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001192 }
1193
Christopher Fauletca20d022017-08-29 15:30:31 +02001194 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001195}
1196
1197/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001198 * Process a fetch + format conversion as defined by the sample expression <expr>
1199 * on request or response considering the <opt> parameter. Returns either NULL if
1200 * no key could be extracted, or a pointer to the converted result stored in
1201 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1202 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001203 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1204 * without SMP_OPT_FINAL). The output will be usable like this :
1205 *
1206 * return MAY_CHANGE FINAL Meaning for the sample
1207 * NULL 0 * Not present and will never be (eg: header)
1208 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1209 * NULL 1 1 Not present, will not change anymore
1210 * smp 0 * Present and will not change (eg: header)
1211 * smp 1 0 not possible
1212 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001213 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001214struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001215 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1216{
1217 if (smp)
1218 memset(smp, 0, sizeof(*smp));
1219
Willy Tarreau192252e2015-04-04 01:47:55 +02001220 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001221 if (!smp)
1222 return NULL;
1223
1224 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1225 return NULL; /* we can only use stable samples */
1226
1227 return smp_to_stkey(smp, t);
1228}
1229
1230/*
Willy Tarreau12785782012-04-27 21:37:17 +02001231 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001232 * type <table_type>, otherwise zero. Used in configuration check.
1233 */
Willy Tarreau12785782012-04-27 21:37:17 +02001234int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001235{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001236 int out_type;
1237
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001238 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001239 return 0;
1240
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001241 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001242
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001243 /* Convert sample. */
1244 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001245 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001246
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001247 return 1;
1248}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001249
Willy Tarreauedee1d62014-07-15 16:44:27 +02001250/* Extra data types processing : after the last one, some room may remain
1251 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1252 * at run time.
1253 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001254struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001255 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001256 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001257 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001258 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001259 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1260 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreaudb2ab822021-10-08 17:53:12 +02001261 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT, .is_local = 1 },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001262 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1263 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1264 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1265 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1266 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1267 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1268 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1269 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1270 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1271 [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001272 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1273 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001274 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001275 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1276 [STKTABLE_DT_HTTP_FAIL_RATE]= { .name = "http_fail_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Emeric Brun877b0b52021-06-30 18:57:49 +02001277 [STKTABLE_DT_GPT] = { .name = "gpt", .std_type = STD_T_UINT, .is_array = 1 },
Emeric Brun4d7ada82021-06-30 19:04:16 +02001278 [STKTABLE_DT_GPC] = { .name = "gpc", .std_type = STD_T_UINT, .is_array = 1 },
1279 [STKTABLE_DT_GPC_RATE] = { .name = "gpc_rate", .std_type = STD_T_FRQP, .is_array = 1, .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +02001280};
1281
Willy Tarreauedee1d62014-07-15 16:44:27 +02001282/* Registers stick-table extra data type with index <idx>, name <name>, type
1283 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1284 * index is automatically allocated. The allocated index is returned, or -1 if
1285 * no free index was found or <name> was already registered. The <name> is used
1286 * directly as a pointer, so if it's not stable, the caller must allocate it.
1287 */
1288int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1289{
1290 if (idx < 0) {
1291 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1292 if (!stktable_data_types[idx].name)
1293 break;
1294
1295 if (strcmp(stktable_data_types[idx].name, name) == 0)
1296 return -1;
1297 }
1298 }
1299
1300 if (idx >= STKTABLE_DATA_TYPES)
1301 return -1;
1302
1303 if (stktable_data_types[idx].name != NULL)
1304 return -1;
1305
1306 stktable_data_types[idx].name = name;
1307 stktable_data_types[idx].std_type = std_type;
1308 stktable_data_types[idx].arg_type = arg_type;
1309 return idx;
1310}
1311
Willy Tarreau08d5f982010-06-06 13:34:54 +02001312/*
1313 * Returns the data type number for the stktable_data_type whose name is <name>,
1314 * or <0 if not found.
1315 */
1316int stktable_get_data_type(char *name)
1317{
1318 int type;
1319
1320 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001321 if (!stktable_data_types[type].name)
1322 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001323 if (strcmp(name, stktable_data_types[type].name) == 0)
1324 return type;
1325 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001326 /* For backwards compatibility */
1327 if (strcmp(name, "server_name") == 0)
1328 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001329 return -1;
1330}
1331
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001332/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1333 * it up into this table. Returns true if found, false otherwise. The input
1334 * type is STR so that input samples are converted to string (since all types
1335 * can be converted to strings), then the function casts the string again into
1336 * the table's type. This is a double conversion, but in the future we might
1337 * support automatic input types to perform the cast on the fly.
1338 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001339static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001340{
1341 struct stktable *t;
1342 struct stktable_key *key;
1343 struct stksess *ts;
1344
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001345 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001346
1347 key = smp_to_stkey(smp, t);
1348 if (!key)
1349 return 0;
1350
1351 ts = stktable_lookup_key(t, key);
1352
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001353 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001354 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001356 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001357 return 1;
1358}
1359
1360/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1361 * it up into this table. Returns the data rate received from clients in bytes/s
1362 * if the key is present in the table, otherwise zero, so that comparisons can
1363 * be easily performed. If the inspected parameter is not stored in the table,
1364 * <not found> is returned.
1365 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001366static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001367{
1368 struct stktable *t;
1369 struct stktable_key *key;
1370 struct stksess *ts;
1371 void *ptr;
1372
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001373 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001374
1375 key = smp_to_stkey(smp, t);
1376 if (!key)
1377 return 0;
1378
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001379 ts = stktable_lookup_key(t, key);
1380
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001381 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001383 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001384
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001385 if (!ts) /* key not present */
1386 return 1;
1387
1388 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001389 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001390 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001391 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001392
Daniel Corbett3e60b112018-05-27 09:47:12 -04001393 stktable_release(t, ts);
1394 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001395}
1396
1397/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1398 * it up into this table. Returns the cumulated number of connections for the key
1399 * if the key is present in the table, otherwise zero, so that comparisons can
1400 * be easily performed. If the inspected parameter is not stored in the table,
1401 * <not found> is returned.
1402 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001403static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001404{
1405 struct stktable *t;
1406 struct stktable_key *key;
1407 struct stksess *ts;
1408 void *ptr;
1409
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001410 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001411
1412 key = smp_to_stkey(smp, t);
1413 if (!key)
1414 return 0;
1415
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001416 ts = stktable_lookup_key(t, key);
1417
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001418 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001419 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001420 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001421
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001422 if (!ts) /* key not present */
1423 return 1;
1424
1425 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001426 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001427 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001428
Daniel Corbett3e60b112018-05-27 09:47:12 -04001429 stktable_release(t, ts);
1430 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001431}
1432
1433/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1434 * it up into this table. Returns the number of concurrent connections for the
1435 * key if the key is present in the table, otherwise zero, so that comparisons
1436 * can be easily performed. If the inspected parameter is not stored in the
1437 * table, <not found> is returned.
1438 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001439static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001440{
1441 struct stktable *t;
1442 struct stktable_key *key;
1443 struct stksess *ts;
1444 void *ptr;
1445
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001446 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001447
1448 key = smp_to_stkey(smp, t);
1449 if (!key)
1450 return 0;
1451
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001452 ts = stktable_lookup_key(t, key);
1453
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001454 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001455 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001456 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001457
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001458 if (!ts) /* key not present */
1459 return 1;
1460
1461 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001462 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001463 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001464
Daniel Corbett3e60b112018-05-27 09:47:12 -04001465 stktable_release(t, ts);
1466 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001467}
1468
1469/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1470 * it up into this table. Returns the rate of incoming connections from the key
1471 * if the key is present in the table, otherwise zero, so that comparisons can
1472 * be easily performed. If the inspected parameter is not stored in the table,
1473 * <not found> is returned.
1474 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001475static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001476{
1477 struct stktable *t;
1478 struct stktable_key *key;
1479 struct stksess *ts;
1480 void *ptr;
1481
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001482 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001483
1484 key = smp_to_stkey(smp, t);
1485 if (!key)
1486 return 0;
1487
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001488 ts = stktable_lookup_key(t, key);
1489
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001490 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001491 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001492 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001493
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001494 if (!ts) /* key not present */
1495 return 1;
1496
1497 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001498 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001499 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001500 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001501
Daniel Corbett3e60b112018-05-27 09:47:12 -04001502 stktable_release(t, ts);
1503 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001504}
1505
1506/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Frédéric Lécaillebbeec372022-08-16 18:11:25 +02001507 * it up into this table. Returns the expiration delay for the key if the key is
1508 * present in the table, otherwise the default value provided as second argument
1509 * if any, if not (no default value), <not found> is returned.
1510 */
1511static int sample_conv_table_expire(const struct arg *arg_p, struct sample *smp, void *private)
1512{
1513 struct stktable *t;
1514 struct stktable_key *key;
1515 struct stksess *ts;
1516
1517 t = arg_p[0].data.t;
1518
1519 key = smp_to_stkey(smp, t);
1520 if (!key)
1521 return 0;
1522
1523 ts = stktable_lookup_key(t, key);
1524
1525 smp->flags = SMP_F_VOL_TEST;
1526 smp->data.type = SMP_T_SINT;
1527 smp->data.u.sint = 0;
1528
1529 if (!ts) { /* key not present */
1530 if (arg_p[1].type == ARGT_STOP)
1531 return 0;
1532
1533 /* default value */
1534 smp->data.u.sint = arg_p[1].data.sint;
1535 return 1;
1536 }
1537
1538 smp->data.u.sint = tick_remain(now_ms, ts->expire);
1539
1540 stktable_release(t, ts);
1541 return 1;
1542}
1543
1544/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1545 * it up into this table. Returns the time the key remains unused if the key is
1546 * present in the table, otherwise the default value provided as second argument
1547 * if any, if not (no default value), <not found> is returned.
1548 */
1549static int sample_conv_table_idle(const struct arg *arg_p, struct sample *smp, void *private)
1550{
1551 struct stktable *t;
1552 struct stktable_key *key;
1553 struct stksess *ts;
1554
1555 t = arg_p[0].data.t;
1556
1557 key = smp_to_stkey(smp, t);
1558 if (!key)
1559 return 0;
1560
1561 ts = stktable_lookup_key(t, key);
1562
1563 smp->flags = SMP_F_VOL_TEST;
1564 smp->data.type = SMP_T_SINT;
1565 smp->data.u.sint = 0;
1566
1567 if (!ts) { /* key not present */
1568 if (arg_p[1].type == ARGT_STOP)
1569 return 0;
1570
1571 /* default value */
1572 smp->data.u.sint = arg_p[1].data.sint;
1573 return 1;
1574 }
1575
1576 smp->data.u.sint = tick_remain(tick_remain(now_ms, ts->expire), t->expire);
1577
1578 stktable_release(t, ts);
1579 return 1;
1580}
1581
1582/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001583 * it up into this table. Returns the data rate sent to clients in bytes/s
1584 * if the key is present in the table, otherwise zero, so that comparisons can
1585 * be easily performed. If the inspected parameter is not stored in the table,
1586 * <not found> is returned.
1587 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001588static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001589{
1590 struct stktable *t;
1591 struct stktable_key *key;
1592 struct stksess *ts;
1593 void *ptr;
1594
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001595 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001596
1597 key = smp_to_stkey(smp, t);
1598 if (!key)
1599 return 0;
1600
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001601 ts = stktable_lookup_key(t, key);
1602
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001603 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001604 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001605 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001606
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001607 if (!ts) /* key not present */
1608 return 1;
1609
1610 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001611 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001612 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001613 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001614
Daniel Corbett3e60b112018-05-27 09:47:12 -04001615 stktable_release(t, ts);
1616 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001617}
1618
Emeric Brun877b0b52021-06-30 18:57:49 +02001619/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1620 * it up into this table. Returns the value of the GPT[arg_p(0)] tag for the key
1621 * if the key is present in the table, otherwise false, so that comparisons can
1622 * be easily performed. If the inspected parameter is not stored in the table,
1623 * <not found> is returned.
1624 */
1625static int sample_conv_table_gpt(const struct arg *arg_p, struct sample *smp, void *private)
1626{
1627 struct stktable *t;
1628 struct stktable_key *key;
1629 struct stksess *ts;
1630 void *ptr;
1631 unsigned int idx;
1632
1633 idx = arg_p[0].data.sint;
1634
1635 t = arg_p[1].data.t;
1636
1637 key = smp_to_stkey(smp, t);
1638 if (!key)
1639 return 0;
1640
1641 ts = stktable_lookup_key(t, key);
1642
1643 smp->flags = SMP_F_VOL_TEST;
1644 smp->data.type = SMP_T_SINT;
1645 smp->data.u.sint = 0;
1646
1647 if (!ts) /* key not present */
1648 return 1;
1649
1650 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, idx);
1651 if (ptr)
1652 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1653
1654 stktable_release(t, ts);
1655 return !!ptr;
1656}
1657
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001658/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001659 * it up into this table. Returns the value of the GPT0 tag for the key
1660 * if the key is present in the table, otherwise false, so that comparisons can
1661 * be easily performed. If the inspected parameter is not stored in the table,
1662 * <not found> is returned.
1663 */
1664static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1665{
1666 struct stktable *t;
1667 struct stktable_key *key;
1668 struct stksess *ts;
1669 void *ptr;
1670
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001671 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001672
1673 key = smp_to_stkey(smp, t);
1674 if (!key)
1675 return 0;
1676
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001677 ts = stktable_lookup_key(t, key);
1678
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001679 smp->flags = SMP_F_VOL_TEST;
1680 smp->data.type = SMP_T_SINT;
1681 smp->data.u.sint = 0;
1682
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001683 if (!ts) /* key not present */
1684 return 1;
1685
1686 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02001687 if (!ptr)
1688 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPT, 0);
1689
Daniel Corbett3e60b112018-05-27 09:47:12 -04001690 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001691 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001692
Daniel Corbett3e60b112018-05-27 09:47:12 -04001693 stktable_release(t, ts);
1694 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001695}
1696
Emeric Brun4d7ada82021-06-30 19:04:16 +02001697/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1698 * it up into this table. Returns the value of the GPC[arg_p(0)] counter for the key
1699 * if the key is present in the table, otherwise zero, so that comparisons can
1700 * be easily performed. If the inspected parameter is not stored in the table,
1701 * <not found> is returned.
1702 */
1703static int sample_conv_table_gpc(const struct arg *arg_p, struct sample *smp, void *private)
1704{
1705 struct stktable *t;
1706 struct stktable_key *key;
1707 struct stksess *ts;
1708 void *ptr;
1709 unsigned int idx;
1710
1711 idx = arg_p[0].data.sint;
1712
1713 t = arg_p[1].data.t;
1714
1715 key = smp_to_stkey(smp, t);
1716 if (!key)
1717 return 0;
1718
1719 ts = stktable_lookup_key(t, key);
1720
1721 smp->flags = SMP_F_VOL_TEST;
1722 smp->data.type = SMP_T_SINT;
1723 smp->data.u.sint = 0;
1724
1725 if (!ts) /* key not present */
1726 return 1;
1727
1728 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, idx);
1729 if (ptr)
1730 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
1731
1732 stktable_release(t, ts);
1733 return !!ptr;
1734}
1735
1736/* Casts sample <smp> to the type of the table specified in arg_p(1), and looks
1737 * it up into this table. Returns the event rate of the GPC[arg_p(0)] counter
1738 * for the key if the key is present in the table, otherwise zero, so that
1739 * comparisons can be easily performed. If the inspected parameter is not
1740 * stored in the table, <not found> is returned.
1741 */
1742static int sample_conv_table_gpc_rate(const struct arg *arg_p, struct sample *smp, void *private)
1743{
1744 struct stktable *t;
1745 struct stktable_key *key;
1746 struct stksess *ts;
1747 void *ptr;
1748 unsigned int idx;
1749
1750 idx = arg_p[0].data.sint;
1751
1752 t = arg_p[1].data.t;
1753
1754 key = smp_to_stkey(smp, t);
1755 if (!key)
1756 return 0;
1757
1758 ts = stktable_lookup_key(t, key);
1759
1760 smp->flags = SMP_F_VOL_TEST;
1761 smp->data.type = SMP_T_SINT;
1762 smp->data.u.sint = 0;
1763
1764 if (!ts) /* key not present */
1765 return 1;
1766
1767 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, idx);
1768 if (ptr)
1769 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1770 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1771
1772 stktable_release(t, ts);
1773 return !!ptr;
1774}
1775
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001776/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001777 * it up into this table. Returns the value of the GPC0 counter for the key
1778 * if the key is present in the table, otherwise zero, so that comparisons can
1779 * be easily performed. If the inspected parameter is not stored in the table,
1780 * <not found> is returned.
1781 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001782static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001783{
1784 struct stktable *t;
1785 struct stktable_key *key;
1786 struct stksess *ts;
1787 void *ptr;
1788
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001789 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001790
1791 key = smp_to_stkey(smp, t);
1792 if (!key)
1793 return 0;
1794
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001795 ts = stktable_lookup_key(t, key);
1796
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001797 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001798 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001799 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001800
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001801 if (!ts) /* key not present */
1802 return 1;
1803
1804 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02001805 if (!ptr) {
1806 /* fallback on the gpc array */
1807 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 0);
1808 }
1809
Daniel Corbett3e60b112018-05-27 09:47:12 -04001810 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001811 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001812
Daniel Corbett3e60b112018-05-27 09:47:12 -04001813 stktable_release(t, ts);
1814 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001815}
1816
1817/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1818 * it up into this table. Returns the event rate of the GPC0 counter for the key
1819 * if the key is present in the table, otherwise zero, so that comparisons can
1820 * be easily performed. If the inspected parameter is not stored in the table,
1821 * <not found> is returned.
1822 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001823static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001824{
1825 struct stktable *t;
1826 struct stktable_key *key;
1827 struct stksess *ts;
1828 void *ptr;
1829
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001830 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001831
1832 key = smp_to_stkey(smp, t);
1833 if (!key)
1834 return 0;
1835
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001836 ts = stktable_lookup_key(t, key);
1837
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001838 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001839 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001840 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001841
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001842 if (!ts) /* key not present */
1843 return 1;
1844
1845 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001846 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001847 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001848 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001849 else {
1850 /* fallback on the gpc array */
1851 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 0);
1852 if (ptr)
1853 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1854 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1855 }
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001856
Daniel Corbett3e60b112018-05-27 09:47:12 -04001857 stktable_release(t, ts);
1858 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001859}
1860
1861/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001862 * it up into this table. Returns the value of the GPC1 counter for the key
1863 * if the key is present in the table, otherwise zero, so that comparisons can
1864 * be easily performed. If the inspected parameter is not stored in the table,
1865 * <not found> is returned.
1866 */
1867static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1868{
1869 struct stktable *t;
1870 struct stktable_key *key;
1871 struct stksess *ts;
1872 void *ptr;
1873
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001874 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001875
1876 key = smp_to_stkey(smp, t);
1877 if (!key)
1878 return 0;
1879
1880 ts = stktable_lookup_key(t, key);
1881
1882 smp->flags = SMP_F_VOL_TEST;
1883 smp->data.type = SMP_T_SINT;
1884 smp->data.u.sint = 0;
1885
1886 if (!ts) /* key not present */
1887 return 1;
1888
1889 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02001890 if (!ptr) {
1891 /* fallback on the gpc array */
1892 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC, 1);
1893 }
1894
Daniel Corbett3e60b112018-05-27 09:47:12 -04001895 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001896 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001897
Daniel Corbett3e60b112018-05-27 09:47:12 -04001898 stktable_release(t, ts);
1899 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001900}
1901
1902/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1903 * it up into this table. Returns the event rate of the GPC1 counter for the key
1904 * if the key is present in the table, otherwise zero, so that comparisons can
1905 * be easily performed. If the inspected parameter is not stored in the table,
1906 * <not found> is returned.
1907 */
1908static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1909{
1910 struct stktable *t;
1911 struct stktable_key *key;
1912 struct stksess *ts;
1913 void *ptr;
1914
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001915 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001916
1917 key = smp_to_stkey(smp, t);
1918 if (!key)
1919 return 0;
1920
1921 ts = stktable_lookup_key(t, key);
1922
1923 smp->flags = SMP_F_VOL_TEST;
1924 smp->data.type = SMP_T_SINT;
1925 smp->data.u.sint = 0;
1926
1927 if (!ts) /* key not present */
1928 return 1;
1929
1930 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001931 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001932 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04001933 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Emeric Brun726783d2021-06-30 19:06:43 +02001934 else {
1935 /* fallback on the gpc array */
1936 ptr = stktable_data_ptr_idx(t, ts, STKTABLE_DT_GPC_RATE, 1);
1937 if (ptr)
1938 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
1939 t->data_arg[STKTABLE_DT_GPC_RATE].u);
1940 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001941
Daniel Corbett3e60b112018-05-27 09:47:12 -04001942 stktable_release(t, ts);
1943 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001944}
1945
1946/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001947 * it up into this table. Returns the cumulated number of HTTP request errors
1948 * for the key if the key is present in the table, otherwise zero, so that
1949 * comparisons can be easily performed. If the inspected parameter is not stored
1950 * in the table, <not found> is returned.
1951 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001952static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001953{
1954 struct stktable *t;
1955 struct stktable_key *key;
1956 struct stksess *ts;
1957 void *ptr;
1958
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001959 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001960
1961 key = smp_to_stkey(smp, t);
1962 if (!key)
1963 return 0;
1964
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001965 ts = stktable_lookup_key(t, key);
1966
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001967 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001968 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001969 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001970
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001971 if (!ts) /* key not present */
1972 return 1;
1973
1974 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001975 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02001976 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001977
Daniel Corbett3e60b112018-05-27 09:47:12 -04001978 stktable_release(t, ts);
1979 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001980}
1981
1982/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1983 * it up into this table. Returns the HTTP request error rate the key
1984 * if the key is present in the table, otherwise zero, so that comparisons can
1985 * be easily performed. If the inspected parameter is not stored in the table,
1986 * <not found> is returned.
1987 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001988static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001989{
1990 struct stktable *t;
1991 struct stktable_key *key;
1992 struct stksess *ts;
1993 void *ptr;
1994
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001995 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001996
1997 key = smp_to_stkey(smp, t);
1998 if (!key)
1999 return 0;
2000
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002001 ts = stktable_lookup_key(t, key);
2002
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002003 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002004 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002005 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002006
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002007 if (!ts) /* key not present */
2008 return 1;
2009
2010 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002011 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002012 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002013 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002014
Daniel Corbett3e60b112018-05-27 09:47:12 -04002015 stktable_release(t, ts);
2016 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002017}
2018
2019/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002020 * it up into this table. Returns the cumulated number of HTTP response failures
2021 * for the key if the key is present in the table, otherwise zero, so that
2022 * comparisons can be easily performed. If the inspected parameter is not stored
2023 * in the table, <not found> is returned.
2024 */
2025static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
2026{
2027 struct stktable *t;
2028 struct stktable_key *key;
2029 struct stksess *ts;
2030 void *ptr;
2031
2032 t = arg_p[0].data.t;
2033
2034 key = smp_to_stkey(smp, t);
2035 if (!key)
2036 return 0;
2037
2038 ts = stktable_lookup_key(t, key);
2039
2040 smp->flags = SMP_F_VOL_TEST;
2041 smp->data.type = SMP_T_SINT;
2042 smp->data.u.sint = 0;
2043
2044 if (!ts) /* key not present */
2045 return 1;
2046
2047 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
2048 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002049 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002050
2051 stktable_release(t, ts);
2052 return !!ptr;
2053}
2054
2055/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2056 * it up into this table. Returns the HTTP response failure rate for the key
2057 * if the key is present in the table, otherwise zero, so that comparisons can
2058 * be easily performed. If the inspected parameter is not stored in the table,
2059 * <not found> is returned.
2060 */
2061static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
2062{
2063 struct stktable *t;
2064 struct stktable_key *key;
2065 struct stksess *ts;
2066 void *ptr;
2067
2068 t = arg_p[0].data.t;
2069
2070 key = smp_to_stkey(smp, t);
2071 if (!key)
2072 return 0;
2073
2074 ts = stktable_lookup_key(t, key);
2075
2076 smp->flags = SMP_F_VOL_TEST;
2077 smp->data.type = SMP_T_SINT;
2078 smp->data.u.sint = 0;
2079
2080 if (!ts) /* key not present */
2081 return 1;
2082
2083 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
2084 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002085 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002086 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
2087
2088 stktable_release(t, ts);
2089 return !!ptr;
2090}
2091
2092/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002093 * it up into this table. Returns the cumulated number of HTTP request for the
2094 * key if the key is present in the table, otherwise zero, so that comparisons
2095 * can be easily performed. If the inspected parameter is not stored in the
2096 * table, <not found> is returned.
2097 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002098static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002099{
2100 struct stktable *t;
2101 struct stktable_key *key;
2102 struct stksess *ts;
2103 void *ptr;
2104
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002105 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002106
2107 key = smp_to_stkey(smp, t);
2108 if (!key)
2109 return 0;
2110
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002111 ts = stktable_lookup_key(t, key);
2112
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002113 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002114 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002115 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002116
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002117 if (!ts) /* key not present */
2118 return 1;
2119
2120 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002121 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002122 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002123
Daniel Corbett3e60b112018-05-27 09:47:12 -04002124 stktable_release(t, ts);
2125 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002126}
2127
2128/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2129 * it up into this table. Returns the HTTP request rate the key if the key is
2130 * present in the table, otherwise zero, so that comparisons can be easily
2131 * performed. If the inspected parameter is not stored in the table, <not found>
2132 * is returned.
2133 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002134static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002135{
2136 struct stktable *t;
2137 struct stktable_key *key;
2138 struct stksess *ts;
2139 void *ptr;
2140
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002141 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002142
2143 key = smp_to_stkey(smp, t);
2144 if (!key)
2145 return 0;
2146
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002147 ts = stktable_lookup_key(t, key);
2148
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002149 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002150 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002151 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002152
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002153 if (!ts) /* key not present */
2154 return 1;
2155
2156 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002157 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002158 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002159 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002160
Daniel Corbett3e60b112018-05-27 09:47:12 -04002161 stktable_release(t, ts);
2162 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002163}
2164
2165/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2166 * it up into this table. Returns the volume of datareceived from clients in kbytes
2167 * if the key is present in the table, otherwise zero, so that comparisons can
2168 * be easily performed. If the inspected parameter is not stored in the table,
2169 * <not found> is returned.
2170 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002171static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002172{
2173 struct stktable *t;
2174 struct stktable_key *key;
2175 struct stksess *ts;
2176 void *ptr;
2177
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002178 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002179
2180 key = smp_to_stkey(smp, t);
2181 if (!key)
2182 return 0;
2183
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002184 ts = stktable_lookup_key(t, key);
2185
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002186 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002187 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002188 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002189
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002190 if (!ts) /* key not present */
2191 return 1;
2192
2193 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002194 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002195 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002196
Daniel Corbett3e60b112018-05-27 09:47:12 -04002197 stktable_release(t, ts);
2198 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002199}
2200
2201/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2202 * it up into this table. Returns the volume of data sent to clients in kbytes
2203 * if the key is present in the table, otherwise zero, so that comparisons can
2204 * be easily performed. If the inspected parameter is not stored in the table,
2205 * <not found> is returned.
2206 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002207static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002208{
2209 struct stktable *t;
2210 struct stktable_key *key;
2211 struct stksess *ts;
2212 void *ptr;
2213
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002214 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002215
2216 key = smp_to_stkey(smp, t);
2217 if (!key)
2218 return 0;
2219
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002220 ts = stktable_lookup_key(t, key);
2221
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002222 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002224 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002225
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002226 if (!ts) /* key not present */
2227 return 1;
2228
2229 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002230 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002231 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002232
Daniel Corbett3e60b112018-05-27 09:47:12 -04002233 stktable_release(t, ts);
2234 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002235}
2236
2237/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2238 * it up into this table. Returns the server ID associated with the key if the
2239 * key is present in the table, otherwise zero, so that comparisons can be
2240 * easily performed. If the inspected parameter is not stored in the table,
2241 * <not found> is returned.
2242 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002243static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002244{
2245 struct stktable *t;
2246 struct stktable_key *key;
2247 struct stksess *ts;
2248 void *ptr;
2249
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002250 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002251
2252 key = smp_to_stkey(smp, t);
2253 if (!key)
2254 return 0;
2255
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002256 ts = stktable_lookup_key(t, key);
2257
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002258 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002259 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002260 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002261
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002262 if (!ts) /* key not present */
2263 return 1;
2264
2265 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002266 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002267 smp->data.u.sint = stktable_data_cast(ptr, std_t_sint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002268
Daniel Corbett3e60b112018-05-27 09:47:12 -04002269 stktable_release(t, ts);
2270 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002271}
2272
2273/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2274 * it up into this table. Returns the cumulated number of sessions for the
2275 * key if the key is present in the table, otherwise zero, so that comparisons
2276 * can be easily performed. If the inspected parameter is not stored in the
2277 * table, <not found> is returned.
2278 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002279static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002280{
2281 struct stktable *t;
2282 struct stktable_key *key;
2283 struct stksess *ts;
2284 void *ptr;
2285
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002286 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002287
2288 key = smp_to_stkey(smp, t);
2289 if (!key)
2290 return 0;
2291
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002292 ts = stktable_lookup_key(t, key);
2293
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002294 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002295 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002296 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002297
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002298 if (!ts) /* key not present */
2299 return 1;
2300
2301 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002302 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002303 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002304
Daniel Corbett3e60b112018-05-27 09:47:12 -04002305 stktable_release(t, ts);
2306 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002307}
2308
2309/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2310 * it up into this table. Returns the session rate the key if the key is
2311 * present in the table, otherwise zero, so that comparisons can be easily
2312 * performed. If the inspected parameter is not stored in the table, <not found>
2313 * is returned.
2314 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002315static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002316{
2317 struct stktable *t;
2318 struct stktable_key *key;
2319 struct stksess *ts;
2320 void *ptr;
2321
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002322 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002323
2324 key = smp_to_stkey(smp, t);
2325 if (!key)
2326 return 0;
2327
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002328 ts = stktable_lookup_key(t, key);
2329
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002330 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002331 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002332 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002333
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002334 if (!ts) /* key not present */
2335 return 1;
2336
2337 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04002338 if (ptr)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002339 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Daniel Corbett3e60b112018-05-27 09:47:12 -04002340 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002341
Daniel Corbett3e60b112018-05-27 09:47:12 -04002342 stktable_release(t, ts);
2343 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002344}
2345
2346/* Casts sample <smp> to the type of the table specified in arg(0), and looks
2347 * it up into this table. Returns the amount of concurrent connections tracking
2348 * the same key if the key is present in the table, otherwise zero, so that
2349 * comparisons can be easily performed. If the inspected parameter is not
2350 * stored in the table, <not found> is returned.
2351 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002352static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002353{
2354 struct stktable *t;
2355 struct stktable_key *key;
2356 struct stksess *ts;
2357
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002358 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002359
2360 key = smp_to_stkey(smp, t);
2361 if (!key)
2362 return 0;
2363
Willy Tarreauf0c730a2016-05-25 17:07:56 +02002364 ts = stktable_lookup_key(t, key);
2365
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002366 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002367 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002368 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002369
Tim Duesterhus65189c12018-06-26 15:57:29 +02002370 if (!ts)
2371 return 1;
2372
2373 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002374
Daniel Corbett3e60b112018-05-27 09:47:12 -04002375 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002376 return 1;
2377}
2378
Emeric Brun4d7ada82021-06-30 19:04:16 +02002379/* This function increments the gpc counter at index 'rule->arg.gpc.idx' of the
2380 * array on the tracksc counter of index 'rule->arg.gpc.sc' stored into the
2381 * <stream> or directly in the session <sess> if <stream> is set to NULL
2382 *
2383 * This function always returns ACT_RET_CONT and parameter flags is unused.
2384 */
2385static enum act_return action_inc_gpc(struct act_rule *rule, struct proxy *px,
2386 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002387{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002388 struct stksess *ts;
2389 struct stkctr *stkctr;
2390
2391 /* Extract the stksess, return OK if no stksess available. */
2392 if (s)
2393 stkctr = &s->stkctr[rule->arg.gpc.sc];
2394 else
2395 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002396
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002397 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002398 if (ts) {
2399 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002400
Emeric Brun4d7ada82021-06-30 19:04:16 +02002401 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2402 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2403 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2404
Emeric Brun819fc6f2017-06-13 19:37:32 +02002405 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002406 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002407
2408 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002409 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun4d7ada82021-06-30 19:04:16 +02002410 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002411
Emeric Brun819fc6f2017-06-13 19:37:32 +02002412 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002413 stktable_data_cast(ptr2, std_t_uint)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002414
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002415 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002416
2417 /* If data was modified, we need to touch to re-schedule sync */
2418 stktable_touch_local(stkctr->table, ts, 0);
2419 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002420 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002421 return ACT_RET_CONT;
2422}
2423
Emeric Brun4d7ada82021-06-30 19:04:16 +02002424/* Same as action_inc_gpc() but for gpc0 only */
2425static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
2426 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002427{
Emeric Brun4d7ada82021-06-30 19:04:16 +02002428 struct stksess *ts;
2429 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002430 unsigned int period = 0;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002431
Emeric Brun4d7ada82021-06-30 19:04:16 +02002432 /* Extract the stksess, return OK if no stksess available. */
2433 if (s)
2434 stkctr = &s->stkctr[rule->arg.gpc.sc];
2435 else
2436 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002437
Emeric Brun4d7ada82021-06-30 19:04:16 +02002438 ts = stkctr_entry(stkctr);
2439 if (ts) {
2440 void *ptr1, *ptr2;
2441
2442 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2443 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002444 if (ptr1) {
2445 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
2446 }
2447 else {
2448 /* fallback on the gpc array */
2449 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 0);
2450 if (ptr1)
2451 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2452 }
2453
Emeric Brun4d7ada82021-06-30 19:04:16 +02002454 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02002455 if (!ptr2) {
2456 /* fallback on the gpc array */
2457 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 0);
2458 }
2459
Emeric Brun4d7ada82021-06-30 19:04:16 +02002460 if (ptr1 || ptr2) {
2461 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2462
2463 if (ptr1)
2464 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002465 period, 1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002466
2467 if (ptr2)
2468 stktable_data_cast(ptr2, std_t_uint)++;
2469
2470 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2471
2472 /* If data was modified, we need to touch to re-schedule sync */
2473 stktable_touch_local(stkctr->table, ts, 0);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002474 }
2475 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002476 return ACT_RET_CONT;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002477}
2478
Emeric Brun4d7ada82021-06-30 19:04:16 +02002479/* Same as action_inc_gpc() but for gpc1 only */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002480static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2481 struct session *sess, struct stream *s, int flags)
2482{
2483 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002484 struct stkctr *stkctr = NULL;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02002485 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002486
2487 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002488 if (s && s->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002489 stkctr = &s->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002490 else if (sess->stkctr)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002491 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002492 else
2493 return ACT_RET_CONT;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002494
2495 ts = stkctr_entry(stkctr);
2496 if (ts) {
2497 void *ptr1, *ptr2;
2498
2499 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2500 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02002501 if (ptr1) {
2502 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
2503 }
2504 else {
2505 /* fallback on the gpc array */
2506 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, 1);
2507 if (ptr1)
2508 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
2509 }
2510
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002511 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02002512 if (!ptr2) {
2513 /* fallback on the gpc array */
2514 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, 1);
2515 }
2516
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002517 if (ptr1 || ptr2) {
2518 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2519
2520 if (ptr1)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002521 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02002522 period, 1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002523
2524 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02002525 stktable_data_cast(ptr2, std_t_uint)++;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002526
2527 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2528
2529 /* If data was modified, we need to touch to re-schedule sync */
2530 stktable_touch_local(stkctr->table, ts, 0);
2531 }
2532 }
2533 return ACT_RET_CONT;
2534}
2535
Emeric Brun4d7ada82021-06-30 19:04:16 +02002536/* This function is a common parser for actions incrementing the GPC
2537 * (General Purpose Counters). It understands the formats:
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002538 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002539 * sc-inc-gpc(<gpc IDX>,<track ID>)
2540 * sc-inc-gpc0([<track ID>])
2541 * sc-inc-gpc1([<track ID>])
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002542 *
Emeric Brun4d7ada82021-06-30 19:04:16 +02002543 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
2544 * message. Otherwise it returns ACT_RET_PRS_OK.
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002545 */
Emeric Brun4d7ada82021-06-30 19:04:16 +02002546static enum act_parse_ret parse_inc_gpc(const char **args, int *arg, struct proxy *px,
2547 struct act_rule *rule, char **err)
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002548{
2549 const char *cmd_name = args[*arg-1];
2550 char *error;
2551
Willy Tarreau6c011712023-01-06 16:09:58 +01002552 if (!global.tune.nb_stk_ctr) {
2553 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2554 return ACT_RET_PRS_ERR;
2555 }
2556
Emeric Brun4d7ada82021-06-30 19:04:16 +02002557 cmd_name += strlen("sc-inc-gpc");
2558 if (*cmd_name == '(') {
2559 cmd_name++; /* skip the '(' */
2560 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2561 if (*error != ',') {
2562 memprintf(err, "Missing gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002563 return ACT_RET_PRS_ERR;
2564 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002565 else {
2566 cmd_name = error + 1; /* skip the ',' */
2567 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2568 if (*error != ')') {
2569 memprintf(err, "invalid stick table track ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2570 return ACT_RET_PRS_ERR;
2571 }
2572
Willy Tarreau6c011712023-01-06 16:09:58 +01002573 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2574 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d (tune.stick-counters)",
2575 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002576 return ACT_RET_PRS_ERR;
2577 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002578 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002579 rule->action_ptr = action_inc_gpc;
2580 }
2581 else if (*cmd_name == '0' ||*cmd_name == '1') {
2582 char c = *cmd_name;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002583
Emeric Brun4d7ada82021-06-30 19:04:16 +02002584 cmd_name++;
2585 if (*cmd_name == '\0') {
2586 /* default stick table id. */
2587 rule->arg.gpc.sc = 0;
2588 } else {
2589 /* parse the stick table id. */
2590 if (*cmd_name != '(') {
2591 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2592 return ACT_RET_PRS_ERR;
2593 }
2594 cmd_name++; /* jump the '(' */
2595 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2596 if (*error != ')') {
2597 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2598 return ACT_RET_PRS_ERR;
2599 }
2600
Willy Tarreau6c011712023-01-06 16:09:58 +01002601 if (rule->arg.gpc.sc >= global.tune.nb_stk_ctr) {
2602 memprintf(err, "invalid stick table track ID. The max allowed ID is %d (tune.stick-counters)",
2603 global.tune.nb_stk_ctr-1);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002604 return ACT_RET_PRS_ERR;
2605 }
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002606 }
Emeric Brun4d7ada82021-06-30 19:04:16 +02002607 if (c == '1')
2608 rule->action_ptr = action_inc_gpc1;
2609 else
2610 rule->action_ptr = action_inc_gpc0;
2611 }
2612 else {
2613 /* default stick table id. */
Willy Tarreau20391512023-01-02 17:35:50 +01002614 memprintf(err, "invalid gpc ID '%s'. Expects sc-inc-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
Emeric Brun4d7ada82021-06-30 19:04:16 +02002615 return ACT_RET_PRS_ERR;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002616 }
2617 rule->action = ACT_CUSTOM;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002618 return ACT_RET_PRS_OK;
2619}
2620
Emeric Brun877b0b52021-06-30 18:57:49 +02002621/* This function sets the gpt at index 'rule->arg.gpt.idx' of the array on the
2622 * tracksc counter of index 'rule->arg.gpt.sc' stored into the <stream> or
2623 * directly in the session <sess> if <stream> is set to NULL. This gpt is
2624 * set to the value computed by the expression 'rule->arg.gpt.expr' or if
2625 * 'rule->arg.gpt.expr' is null directly to the value of 'rule->arg.gpt.value'.
2626 *
2627 * This function always returns ACT_RET_CONT and parameter flags is unused.
2628 */
2629static enum act_return action_set_gpt(struct act_rule *rule, struct proxy *px,
2630 struct session *sess, struct stream *s, int flags)
2631{
2632 void *ptr;
2633 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002634 struct stkctr *stkctr = NULL;
Emeric Brun877b0b52021-06-30 18:57:49 +02002635 unsigned int value = 0;
2636 struct sample *smp;
2637 int smp_opt_dir;
2638
2639 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002640 if (s && s->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002641 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002642 else if (sess->stkctr)
Emeric Brun877b0b52021-06-30 18:57:49 +02002643 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002644 else
2645 return ACT_RET_CONT;
Emeric Brun877b0b52021-06-30 18:57:49 +02002646
2647 ts = stkctr_entry(stkctr);
2648 if (!ts)
2649 return ACT_RET_CONT;
2650
2651 /* Store the sample in the required sc, and ignore errors. */
2652 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, rule->arg.gpt.idx);
2653 if (ptr) {
2654
2655 if (!rule->arg.gpt.expr)
2656 value = (unsigned int)(rule->arg.gpt.value);
2657 else {
2658 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002659 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Emeric Brun877b0b52021-06-30 18:57:49 +02002660 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2661 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2662 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2663 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2664 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2665 default:
2666 send_log(px, LOG_ERR, "stick table: internal error while setting gpt%u.", rule->arg.gpt.idx);
2667 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2668 ha_alert("stick table: internal error while executing setting gpt%u.\n", rule->arg.gpt.idx);
2669 return ACT_RET_CONT;
2670 }
2671
2672 /* Fetch and cast the expression. */
2673 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2674 if (!smp) {
2675 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt%u.", rule->arg.gpt.idx);
2676 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2677 ha_alert("stick table: invalid expression or data type while setting gpt%u.\n", rule->arg.gpt.idx);
2678 return ACT_RET_CONT;
2679 }
2680 value = (unsigned int)(smp->data.u.sint);
2681 }
2682
2683 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2684
2685 stktable_data_cast(ptr, std_t_uint) = value;
2686
2687 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2688
2689 stktable_touch_local(stkctr->table, ts, 0);
2690 }
2691
2692 return ACT_RET_CONT;
2693}
2694
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002695/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002696static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002697 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002698{
2699 void *ptr;
2700 struct stksess *ts;
Willy Tarreau6c011712023-01-06 16:09:58 +01002701 struct stkctr *stkctr = NULL;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002702 unsigned int value = 0;
2703 struct sample *smp;
2704 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002705
2706 /* Extract the stksess, return OK if no stksess available. */
Willy Tarreau6c011712023-01-06 16:09:58 +01002707 if (s && s->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002708 stkctr = &s->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002709 else if (sess->stkctr)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002710 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau6c011712023-01-06 16:09:58 +01002711 else
2712 return ACT_RET_CONT;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002713
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002714 ts = stkctr_entry(stkctr);
2715 if (!ts)
2716 return ACT_RET_CONT;
2717
2718 /* Store the sample in the required sc, and ignore errors. */
2719 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02002720 if (!ptr)
2721 ptr = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPT, 0);
2722
Willy Tarreau79c1e912016-01-25 14:54:45 +01002723 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002724 if (!rule->arg.gpt.expr)
2725 value = (unsigned int)(rule->arg.gpt.value);
2726 else {
2727 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002728 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002729 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2730 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2731 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2732 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2733 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2734 default:
2735 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2736 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2737 ha_alert("stick table: internal error while executing setting gpt0.\n");
2738 return ACT_RET_CONT;
2739 }
2740
2741 /* Fetch and cast the expression. */
2742 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2743 if (!smp) {
2744 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2745 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2746 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2747 return ACT_RET_CONT;
2748 }
2749 value = (unsigned int)(smp->data.u.sint);
2750 }
2751
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002752 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002753
Emeric Brun0e3457b2021-06-30 17:18:28 +02002754 stktable_data_cast(ptr, std_t_uint) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002755
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002756 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002757
2758 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002759 }
2760
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002761 return ACT_RET_CONT;
2762}
2763
Emeric Brun877b0b52021-06-30 18:57:49 +02002764/* This function is a parser for the "sc-set-gpt" and "sc-set-gpt0" actions.
2765 * It understands the formats:
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002766 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002767 * sc-set-gpt(<gpt IDX>,<track ID>) <expression>
2768 * sc-set-gpt0(<track ID>) <expression>
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002769 *
Emeric Brun877b0b52021-06-30 18:57:49 +02002770 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2771 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpt.expr'
2772 * is filled with the pointer to the expression to execute or NULL if the arg
2773 * is directly an integer stored into 'rule->arg.gpt.value'.
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002774 */
Emeric Brun877b0b52021-06-30 18:57:49 +02002775static enum act_parse_ret parse_set_gpt(const char **args, int *arg, struct proxy *px,
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002776 struct act_rule *rule, char **err)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002777{
2778 const char *cmd_name = args[*arg-1];
2779 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002780 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002781
Willy Tarreau6c011712023-01-06 16:09:58 +01002782 if (!global.tune.nb_stk_ctr) {
2783 memprintf(err, "Cannot use '%s', stick-counters are disabled via tune.stick-counters", args[*arg-1]);
2784 return ACT_RET_PRS_ERR;
2785 }
2786
Emeric Brun877b0b52021-06-30 18:57:49 +02002787 cmd_name += strlen("sc-set-gpt");
2788 if (*cmd_name == '(') {
2789 cmd_name++; /* skip the '(' */
2790 rule->arg.gpt.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2791 if (*error != ',') {
2792 memprintf(err, "Missing gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002793 return ACT_RET_PRS_ERR;
2794 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002795 else {
2796 cmd_name = error + 1; /* skip the ',' */
2797 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2798 if (*error != ')') {
2799 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2800 return ACT_RET_PRS_ERR;
2801 }
2802
Willy Tarreau6c011712023-01-06 16:09:58 +01002803 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002804 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002805 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002806 return ACT_RET_PRS_ERR;
2807 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002808 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002809 rule->action_ptr = action_set_gpt;
2810 }
2811 else if (*cmd_name == '0') {
2812 cmd_name++;
2813 if (*cmd_name == '\0') {
2814 /* default stick table id. */
2815 rule->arg.gpt.sc = 0;
2816 } else {
2817 /* parse the stick table id. */
2818 if (*cmd_name != '(') {
2819 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2820 return ACT_RET_PRS_ERR;
2821 }
2822 cmd_name++; /* jump the '(' */
2823 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2824 if (*error != ')') {
2825 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2826 return ACT_RET_PRS_ERR;
2827 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002828
Willy Tarreau6c011712023-01-06 16:09:58 +01002829 if (rule->arg.gpt.sc >= global.tune.nb_stk_ctr) {
Emeric Brun877b0b52021-06-30 18:57:49 +02002830 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Willy Tarreau6c011712023-01-06 16:09:58 +01002831 args[*arg-1], global.tune.nb_stk_ctr-1);
Emeric Brun877b0b52021-06-30 18:57:49 +02002832 return ACT_RET_PRS_ERR;
2833 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002834 }
Emeric Brun877b0b52021-06-30 18:57:49 +02002835 rule->action_ptr = action_set_gpt0;
2836 }
2837 else {
2838 /* default stick table id. */
2839 memprintf(err, "invalid gpt ID '%s'. Expects sc-set-gpt(<GPT ID>,<Track ID>)", args[*arg-1]);
2840 return ACT_RET_PRS_ERR;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002841 }
2842
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002843 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002844 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002845 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauece4c4a2021-08-24 14:57:28 +02002846 if (*error == '\0') {
2847 /* valid integer, skip it */
2848 (*arg)++;
2849 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002850 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002851 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002852 if (!rule->arg.gpt.expr)
2853 return ACT_RET_PRS_ERR;
2854
2855 switch (rule->from) {
Aurelien DARRAGONe18fe562023-08-09 17:23:32 +02002856 case ACT_F_TCP_REQ_CON: smp_val = SMP_VAL_FE_CON_ACC; break;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002857 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2858 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2859 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2860 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2861 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2862 default:
2863 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2864 return ACT_RET_PRS_ERR;
2865 }
2866 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2867 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2868 sample_src_names(rule->arg.gpt.expr->fetch->use));
2869 free(rule->arg.gpt.expr);
2870 return ACT_RET_PRS_ERR;
2871 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002872 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002873
Thierry FOURNIER42148732015-09-02 17:17:33 +02002874 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002875
2876 return ACT_RET_PRS_OK;
2877}
2878
Willy Tarreau5a72d032023-01-02 18:15:20 +01002879/* This function updates the gpc at index 'rule->arg.gpc.idx' of the array on
2880 * the tracksc counter of index 'rule->arg.gpc.sc' stored into the <stream> or
2881 * directly in the session <sess> if <stream> is set to NULL. This gpc is
2882 * set to the value computed by the expression 'rule->arg.gpc.expr' or if
2883 * 'rule->arg.gpc.expr' is null directly to the value of 'rule->arg.gpc.value'.
2884 *
2885 * This function always returns ACT_RET_CONT and parameter flags is unused.
2886 */
2887static enum act_return action_add_gpc(struct act_rule *rule, struct proxy *px,
2888 struct session *sess, struct stream *s, int flags)
2889{
2890 void *ptr1, *ptr2;
2891 struct stksess *ts;
2892 struct stkctr *stkctr;
2893 unsigned int value = 0;
2894 struct sample *smp;
2895 int smp_opt_dir;
2896
2897 /* Extract the stksess, return OK if no stksess available. */
2898 if (s)
2899 stkctr = &s->stkctr[rule->arg.gpc.sc];
2900 else
2901 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2902
2903 ts = stkctr_entry(stkctr);
2904 if (!ts)
2905 return ACT_RET_CONT;
2906
2907 /* First, update gpc_rate if it's tracked. Second, update its gpc if tracked. */
2908 ptr1 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC_RATE, rule->arg.gpc.idx);
2909 ptr2 = stktable_data_ptr_idx(stkctr->table, ts, STKTABLE_DT_GPC, rule->arg.gpc.idx);
2910
2911 if (ptr1 || ptr2) {
2912 if (!rule->arg.gpc.expr)
2913 value = (unsigned int)(rule->arg.gpc.value);
2914 else {
2915 switch (rule->from) {
Aurelien DARRAGONb425f752023-08-09 17:39:29 +02002916 case ACT_F_TCP_REQ_CON: smp_opt_dir = SMP_OPT_DIR_REQ; break;
Willy Tarreau5a72d032023-01-02 18:15:20 +01002917 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2918 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2919 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2920 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2921 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2922 default:
2923 send_log(px, LOG_ERR, "stick table: internal error while setting gpc%u.", rule->arg.gpc.idx);
2924 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2925 ha_alert("stick table: internal error while executing setting gpc%u.\n", rule->arg.gpc.idx);
2926 return ACT_RET_CONT;
2927 }
2928
2929 /* Fetch and cast the expression. */
2930 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpc.expr, SMP_T_SINT);
2931 if (!smp) {
2932 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpc%u.", rule->arg.gpc.idx);
2933 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2934 ha_alert("stick table: invalid expression or data type while setting gpc%u.\n", rule->arg.gpc.idx);
2935 return ACT_RET_CONT;
2936 }
2937 value = (unsigned int)(smp->data.u.sint);
2938 }
2939
2940 if (value) {
2941 /* only update the value if non-null increment */
2942 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2943
2944 if (ptr1)
2945 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
2946 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, value);
2947
2948 if (ptr2)
2949 stktable_data_cast(ptr2, std_t_uint) += value;
2950
2951 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2952 }
2953 /* always touch the table so that it doesn't expire */
2954 stktable_touch_local(stkctr->table, ts, 0);
2955 }
2956
2957 return ACT_RET_CONT;
2958}
2959
2960/* This function is a parser for the "sc-add-gpc" action. It understands the
2961 * format:
2962 *
2963 * sc-add-gpc(<gpc IDX>,<track ID>) <expression>
2964 *
2965 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error message.
2966 * Otherwise, it returns ACT_RET_PRS_OK and the variable 'rule->arg.gpc.expr'
2967 * is filled with the pointer to the expression to execute or NULL if the arg
2968 * is directly an integer stored into 'rule->arg.gpt.value'.
2969 */
2970static enum act_parse_ret parse_add_gpc(const char **args, int *arg, struct proxy *px,
2971 struct act_rule *rule, char **err)
2972{
2973 const char *cmd_name = args[*arg-1];
2974 char *error;
2975 int smp_val;
2976
2977 cmd_name += strlen("sc-add-gpc");
2978 if (*cmd_name != '(') {
2979 memprintf(err, "Missing or invalid arguments for '%s'. Expects sc-add-gpc(<GPC ID>,<Track ID>)", args[*arg-1]);
2980 return ACT_RET_PRS_ERR;
2981 }
2982 cmd_name++; /* skip the '(' */
2983 rule->arg.gpc.idx = strtoul(cmd_name, &error, 10); /* Convert stick table id. */
2984 if (*error != ',') {
2985 memprintf(err, "Missing gpc ID. Expects %s(<GPC ID>,<Track ID>)", args[*arg-1]);
2986 return ACT_RET_PRS_ERR;
2987 }
2988 else {
2989 cmd_name = error + 1; /* skip the ',' */
2990 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2991 if (*error != ')') {
2992 memprintf(err, "invalid stick table track ID '%s'. Expects %s(<GPC ID>,<Track ID>)", cmd_name, args[*arg-1]);
2993 return ACT_RET_PRS_ERR;
2994 }
2995
2996 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
2997 memprintf(err, "invalid stick table track ID '%s' for '%s'. The max allowed ID is %d",
2998 cmd_name, args[*arg-1], MAX_SESS_STKCTR-1);
2999 return ACT_RET_PRS_ERR;
3000 }
3001 }
3002 rule->action_ptr = action_add_gpc;
3003
3004 /* value may be either an integer or an expression */
3005 rule->arg.gpc.expr = NULL;
3006 rule->arg.gpc.value = strtol(args[*arg], &error, 10);
3007 if (*error == '\0') {
3008 /* valid integer, skip it */
3009 (*arg)++;
3010 } else {
3011 rule->arg.gpc.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
3012 px->conf.args.line, err, &px->conf.args, NULL);
3013 if (!rule->arg.gpc.expr)
3014 return ACT_RET_PRS_ERR;
3015
3016 switch (rule->from) {
Aurelien DARRAGONb425f752023-08-09 17:39:29 +02003017 case ACT_F_TCP_REQ_CON: smp_val = SMP_VAL_FE_CON_ACC; break;
Willy Tarreau5a72d032023-01-02 18:15:20 +01003018 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
3019 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
3020 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
3021 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
3022 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
3023 default:
3024 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
3025 return ACT_RET_PRS_ERR;
3026 }
3027
3028 if (!(rule->arg.gpc.expr->fetch->val & smp_val)) {
3029 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
3030 sample_src_names(rule->arg.gpc.expr->fetch->use));
3031 free(rule->arg.gpc.expr);
3032 return ACT_RET_PRS_ERR;
3033 }
3034 }
3035
3036 rule->action = ACT_CUSTOM;
3037
3038 return ACT_RET_PRS_OK;
3039}
3040
Willy Tarreau7d562212016-11-25 16:10:05 +01003041/* set temp integer to the number of used entries in the table pointed to by expr.
3042 * Accepts exactly 1 argument of type table.
3043 */
3044static int
3045smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3046{
3047 smp->flags = SMP_F_VOL_TEST;
3048 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003049 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003050 return 1;
3051}
3052
3053/* set temp integer to the number of free entries in the table pointed to by expr.
3054 * Accepts exactly 1 argument of type table.
3055 */
3056static int
3057smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
3058{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003059 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003060
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003061 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01003062 smp->flags = SMP_F_VOL_TEST;
3063 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003064 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01003065 return 1;
3066}
3067
3068/* Returns a pointer to a stkctr depending on the fetch keyword name.
3069 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
3070 * sc[0-9]_* will return a pointer to the respective field in the
3071 * stream <l4>. sc_* requires an UINT argument specifying the stick
3072 * counter number. src_* will fill a locally allocated structure with
3073 * the table and entry corresponding to what is specified with src_*.
3074 * NULL may be returned if the designated stkctr is not tracked. For
3075 * the sc_* and sc[0-9]_* forms, an optional table argument may be
3076 * passed. When present, the currently tracked key is then looked up
3077 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05003078 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01003079 * multiple tables). <strm> is allowed to be NULL, in which case only
3080 * the session will be consulted.
3081 */
3082struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003083smp_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 +01003084{
Willy Tarreau7d562212016-11-25 16:10:05 +01003085 struct stkctr *stkptr;
3086 struct stksess *stksess;
3087 unsigned int num = kw[2] - '0';
3088 int arg = 0;
3089
3090 if (num == '_' - '0') {
3091 /* sc_* variant, args[0] = ctr# (mandatory) */
3092 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01003093 }
3094 else if (num > 9) { /* src_* variant, args[0] = table */
3095 struct stktable_key *key;
3096 struct connection *conn = objt_conn(sess->origin);
3097 struct sample smp;
3098
3099 if (!conn)
3100 return NULL;
3101
Joseph Herlant5662fa42018-11-15 13:43:28 -08003102 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003103 smp.px = NULL;
3104 smp.sess = sess;
3105 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003106 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003107 return NULL;
3108
3109 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003110 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003111 if (!key)
3112 return NULL;
3113
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003114 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003115 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
3116 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003117 }
3118
3119 /* Here, <num> contains the counter number from 0 to 9 for
3120 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
3121 * args[arg] is the first optional argument. We first lookup the
3122 * ctr form the stream, then from the session if it was not there.
Willy Tarreau6c011712023-01-06 16:09:58 +01003123 * But we must be sure the counter does not exceed global.tune.nb_stk_ctr.
Willy Tarreau7d562212016-11-25 16:10:05 +01003124 */
Willy Tarreau6c011712023-01-06 16:09:58 +01003125 if (num >= global.tune.nb_stk_ctr)
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02003126 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003127
Willy Tarreau6c011712023-01-06 16:09:58 +01003128 stkptr = NULL;
3129 if (strm && strm->stkctr)
Willy Tarreau7d562212016-11-25 16:10:05 +01003130 stkptr = &strm->stkctr[num];
Willy Tarreau6c011712023-01-06 16:09:58 +01003131 if (!strm || !stkptr || !stkctr_entry(stkptr)) {
3132 if (sess->stkctr)
3133 stkptr = &sess->stkctr[num];
3134 else
3135 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01003136 if (!stkctr_entry(stkptr))
3137 return NULL;
3138 }
3139
3140 stksess = stkctr_entry(stkptr);
3141 if (!stksess)
3142 return NULL;
3143
3144 if (unlikely(args[arg].type == ARGT_TAB)) {
3145 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003146 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003147 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
3148 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003149 }
3150 return stkptr;
3151}
3152
3153/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
3154 * the entry if it doesn't exist yet. This is needed for a few fetch
3155 * functions which need to create an entry, such as src_inc_gpc* and
3156 * src_clr_gpc*.
3157 */
3158struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02003159smp_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 +01003160{
Willy Tarreau7d562212016-11-25 16:10:05 +01003161 struct stktable_key *key;
3162 struct connection *conn = objt_conn(sess->origin);
3163 struct sample smp;
3164
3165 if (strncmp(kw, "src_", 4) != 0)
3166 return NULL;
3167
3168 if (!conn)
3169 return NULL;
3170
Joseph Herlant5662fa42018-11-15 13:43:28 -08003171 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01003172 smp.px = NULL;
3173 smp.sess = sess;
3174 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02003175 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01003176 return NULL;
3177
3178 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003179 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01003180 if (!key)
3181 return NULL;
3182
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003183 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003184 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
3185 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003186}
3187
3188/* set return a boolean indicating if the requested stream counter is
3189 * currently being tracked or not.
3190 * Supports being called as "sc[0-9]_tracked" only.
3191 */
3192static int
3193smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
3194{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003195 struct stkctr tmpstkctr;
3196 struct stkctr *stkctr;
3197
Willy Tarreau7d562212016-11-25 16:10:05 +01003198 smp->flags = SMP_F_VOL_TEST;
3199 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003200 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3201 smp->data.u.sint = !!stkctr;
3202
3203 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02003204 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02003205 stktable_release(stkctr->table, stkctr_entry(stkctr));
3206
Emeric Brun877b0b52021-06-30 18:57:49 +02003207 return 1;
3208}
3209
3210/* set <smp> to the General Purpose Tag of index set as first arg
3211 * to value from the stream's tracked frontend counters or from the src.
3212 * Supports being called as "sc_get_gpt(<gpt-idx>,<sc-idx>[,<table>])" or
3213 * "src_get_gpt(<gpt-idx>[,<table>])" only. Value zero is returned if
3214 * the key is new or gpt is not stored.
3215 */
3216static int
3217smp_fetch_sc_get_gpt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3218{
3219 struct stkctr tmpstkctr;
3220 struct stkctr *stkctr;
3221 unsigned int idx;
3222
3223 idx = args[0].data.sint;
3224
3225 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3226 if (!stkctr)
3227 return 0;
3228
3229 smp->flags = SMP_F_VOL_TEST;
3230 smp->data.type = SMP_T_SINT;
3231 smp->data.u.sint = 0;
3232
3233 if (stkctr_entry(stkctr)) {
3234 void *ptr;
3235
3236 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, idx);
3237 if (!ptr) {
3238 if (stkctr == &tmpstkctr)
3239 stktable_release(stkctr->table, stkctr_entry(stkctr));
3240 return 0; /* parameter not stored */
3241 }
3242
3243 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3244
3245 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3246
3247 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3248
3249 if (stkctr == &tmpstkctr)
3250 stktable_release(stkctr->table, stkctr_entry(stkctr));
3251 }
Willy Tarreau7d562212016-11-25 16:10:05 +01003252 return 1;
3253}
3254
3255/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
3256 * frontend counters or from the src.
3257 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
3258 * zero is returned if the key is new.
3259 */
3260static int
3261smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3262{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003263 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003264 struct stkctr *stkctr;
3265
Emeric Brun819fc6f2017-06-13 19:37:32 +02003266 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003267 if (!stkctr)
3268 return 0;
3269
3270 smp->flags = SMP_F_VOL_TEST;
3271 smp->data.type = SMP_T_SINT;
3272 smp->data.u.sint = 0;
3273
Emeric Brun819fc6f2017-06-13 19:37:32 +02003274 if (stkctr_entry(stkctr)) {
3275 void *ptr;
3276
3277 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
Emeric Brunf7ab0bf2021-06-30 18:58:22 +02003278 if (!ptr)
3279 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT, 0);
3280
Emeric Brun4d7ada82021-06-30 19:04:16 +02003281 if (!ptr) {
3282 if (stkctr == &tmpstkctr)
3283 stktable_release(stkctr->table, stkctr_entry(stkctr));
3284 return 0; /* parameter not stored */
3285 }
3286
3287 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3288
3289 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3290
3291 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3292
3293 if (stkctr == &tmpstkctr)
3294 stktable_release(stkctr->table, stkctr_entry(stkctr));
3295 }
3296 return 1;
3297}
3298
3299/* set <smp> to the GPC[args(0)]'s value from the stream's tracked
3300 * frontend counters or from the src.
3301 * Supports being called as "sc_get_gpc(<gpc-idx>,<sc-idx>[,<table>])" or
3302 * "src_get_gpc(<gpc-idx>[,<table>])" only. Value
3303 * Value zero is returned if the key is new or gpc is not stored.
3304 */
3305static int
3306smp_fetch_sc_get_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3307{
3308 struct stkctr tmpstkctr;
3309 struct stkctr *stkctr;
3310 unsigned int idx;
3311
3312 idx = args[0].data.sint;
3313
3314 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3315 if (!stkctr)
3316 return 0;
3317
3318 smp->flags = SMP_F_VOL_TEST;
3319 smp->data.type = SMP_T_SINT;
3320 smp->data.u.sint = 0;
3321
3322 if (stkctr_entry(stkctr) != NULL) {
3323 void *ptr;
3324
3325 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003326 if (!ptr) {
3327 if (stkctr == &tmpstkctr)
3328 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003329 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003330 }
3331
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003332 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003333
Emeric Brun0e3457b2021-06-30 17:18:28 +02003334 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003335
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003336 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003337
3338 if (stkctr == &tmpstkctr)
3339 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003340 }
3341 return 1;
3342}
3343
3344/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
3345 * frontend counters or from the src.
3346 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
3347 * zero is returned if the key is new.
3348 */
3349static int
3350smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3351{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003352 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003353 struct stkctr *stkctr;
3354
Emeric Brun819fc6f2017-06-13 19:37:32 +02003355 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003356 if (!stkctr)
3357 return 0;
3358
3359 smp->flags = SMP_F_VOL_TEST;
3360 smp->data.type = SMP_T_SINT;
3361 smp->data.u.sint = 0;
3362
3363 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003364 void *ptr;
3365
3366 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3367 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003368 /* fallback on the gpc array */
3369 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3370 }
3371
3372 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003373 if (stkctr == &tmpstkctr)
3374 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003375 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003376 }
3377
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003378 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003379
Emeric Brun0e3457b2021-06-30 17:18:28 +02003380 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003381
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003382 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003383
3384 if (stkctr == &tmpstkctr)
3385 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003386 }
3387 return 1;
3388}
3389
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003390/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
3391 * frontend counters or from the src.
3392 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
3393 * zero is returned if the key is new.
3394 */
3395static int
3396smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3397{
3398 struct stkctr tmpstkctr;
3399 struct stkctr *stkctr;
3400
3401 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3402 if (!stkctr)
3403 return 0;
3404
3405 smp->flags = SMP_F_VOL_TEST;
3406 smp->data.type = SMP_T_SINT;
3407 smp->data.u.sint = 0;
3408
3409 if (stkctr_entry(stkctr) != NULL) {
3410 void *ptr;
3411
3412 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3413 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003414 /* fallback on the gpc array */
3415 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3416 }
3417
3418 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003419 if (stkctr == &tmpstkctr)
3420 stktable_release(stkctr->table, stkctr_entry(stkctr));
3421 return 0; /* parameter not stored */
3422 }
3423
3424 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3425
Emeric Brun0e3457b2021-06-30 17:18:28 +02003426 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003427
3428 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3429
3430 if (stkctr == &tmpstkctr)
3431 stktable_release(stkctr->table, stkctr_entry(stkctr));
3432 }
3433 return 1;
3434}
3435
Emeric Brun4d7ada82021-06-30 19:04:16 +02003436/* set <smp> to the GPC[args(0)]'s event rate from the stream's
3437 * tracked frontend counters or from the src.
3438 * Supports being called as "sc_gpc_rate(<gpc-idx>,<sc-idx>[,<table])"
3439 * or "src_gpc_rate(<gpc-idx>[,<table>])" only.
3440 * Value zero is returned if the key is new or gpc_rate is not stored.
3441 */
3442static int
3443smp_fetch_sc_gpc_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3444{
3445 struct stkctr tmpstkctr;
3446 struct stkctr *stkctr;
3447 unsigned int idx;
3448
3449 idx = args[0].data.sint;
3450
3451 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3452 if (!stkctr)
3453 return 0;
3454
3455 smp->flags = SMP_F_VOL_TEST;
3456 smp->data.type = SMP_T_SINT;
3457 smp->data.u.sint = 0;
3458 if (stkctr_entry(stkctr) != NULL) {
3459 void *ptr;
3460
3461 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3462 if (!ptr) {
3463 if (stkctr == &tmpstkctr)
3464 stktable_release(stkctr->table, stkctr_entry(stkctr));
3465 return 0; /* parameter not stored */
3466 }
3467
3468 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3469
3470 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3471 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u);
3472
3473 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3474
3475 if (stkctr == &tmpstkctr)
3476 stktable_release(stkctr->table, stkctr_entry(stkctr));
3477 }
3478 return 1;
3479}
3480
Willy Tarreau7d562212016-11-25 16:10:05 +01003481/* set <smp> to the General Purpose Counter 0's event rate from the stream's
3482 * tracked frontend counters or from the src.
3483 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
3484 * Value zero is returned if the key is new.
3485 */
3486static int
3487smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3488{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003489 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003490 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003491 unsigned int period;
Willy Tarreau7d562212016-11-25 16:10:05 +01003492
Emeric Brun819fc6f2017-06-13 19:37:32 +02003493 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003494 if (!stkctr)
3495 return 0;
3496
3497 smp->flags = SMP_F_VOL_TEST;
3498 smp->data.type = SMP_T_SINT;
3499 smp->data.u.sint = 0;
3500 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003501 void *ptr;
3502
3503 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003504 if (ptr) {
3505 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3506 }
3507 else {
3508 /* fallback on the gpc array */
3509 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3510 if (ptr)
3511 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3512 }
3513
Emeric Brun819fc6f2017-06-13 19:37:32 +02003514 if (!ptr) {
3515 if (stkctr == &tmpstkctr)
3516 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003517 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003518 }
3519
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003520 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003521
Emeric Brun726783d2021-06-30 19:06:43 +02003522 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), period);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003523
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003524 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003525
3526 if (stkctr == &tmpstkctr)
3527 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003528 }
3529 return 1;
3530}
3531
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003532/* set <smp> to the General Purpose Counter 1's event rate from the stream's
3533 * tracked frontend counters or from the src.
3534 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
3535 * Value zero is returned if the key is new.
3536 */
3537static int
3538smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3539{
3540 struct stkctr tmpstkctr;
3541 struct stkctr *stkctr;
Emeric Brun726783d2021-06-30 19:06:43 +02003542 unsigned int period;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003543
3544 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3545 if (!stkctr)
3546 return 0;
3547
3548 smp->flags = SMP_F_VOL_TEST;
3549 smp->data.type = SMP_T_SINT;
3550 smp->data.u.sint = 0;
3551 if (stkctr_entry(stkctr) != NULL) {
3552 void *ptr;
3553
3554 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003555 if (ptr) {
3556 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3557 }
3558 else {
3559 /* fallback on the gpc array */
3560 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3561 if (ptr)
3562 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3563 }
3564
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003565 if (!ptr) {
3566 if (stkctr == &tmpstkctr)
3567 stktable_release(stkctr->table, stkctr_entry(stkctr));
3568 return 0; /* parameter not stored */
3569 }
3570
3571 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3572
Emeric Brun726783d2021-06-30 19:06:43 +02003573 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 +01003574
3575 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3576
3577 if (stkctr == &tmpstkctr)
3578 stktable_release(stkctr->table, stkctr_entry(stkctr));
3579 }
3580 return 1;
3581}
3582
Emeric Brun4d7ada82021-06-30 19:04:16 +02003583/* Increment the GPC[args(0)] value from the stream's tracked
3584 * frontend counters and return it into temp integer.
3585 * Supports being called as "sc_inc_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3586 * or "src_inc_gpc(<gpc-idx>[,<table>])" only.
3587 */
3588static int
3589smp_fetch_sc_inc_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3590{
3591 struct stkctr tmpstkctr;
3592 struct stkctr *stkctr;
3593 unsigned int idx;
3594
3595 idx = args[0].data.sint;
3596
3597 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3598 if (!stkctr)
3599 return 0;
3600
3601 smp->flags = SMP_F_VOL_TEST;
3602 smp->data.type = SMP_T_SINT;
3603 smp->data.u.sint = 0;
3604
3605 if (!stkctr_entry(stkctr))
3606 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3607
3608 if (stkctr && stkctr_entry(stkctr)) {
3609 void *ptr1,*ptr2;
3610
3611
3612 /* First, update gpc0_rate if it's tracked. Second, update its
3613 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3614 */
3615 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, idx);
3616 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3617 if (ptr1 || ptr2) {
3618 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3619
3620 if (ptr1) {
3621 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
3622 stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u, 1);
3623 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
3624 }
3625
3626 if (ptr2)
3627 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
3628
3629 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3630
3631 /* If data was modified, we need to touch to re-schedule sync */
3632 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3633 }
3634 else if (stkctr == &tmpstkctr)
3635 stktable_release(stkctr->table, stkctr_entry(stkctr));
3636 }
3637 return 1;
3638}
3639
Willy Tarreau7d562212016-11-25 16:10:05 +01003640/* Increment the General Purpose Counter 0 value from the stream's tracked
3641 * frontend counters and return it into temp integer.
3642 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
3643 */
3644static int
3645smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3646{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003647 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003648 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003649 unsigned int period = 0;
Willy Tarreau7d562212016-11-25 16:10:05 +01003650
Emeric Brun819fc6f2017-06-13 19:37:32 +02003651 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003652 if (!stkctr)
3653 return 0;
3654
3655 smp->flags = SMP_F_VOL_TEST;
3656 smp->data.type = SMP_T_SINT;
3657 smp->data.u.sint = 0;
3658
Emeric Brun819fc6f2017-06-13 19:37:32 +02003659 if (!stkctr_entry(stkctr))
3660 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003661
3662 if (stkctr && stkctr_entry(stkctr)) {
3663 void *ptr1,*ptr2;
3664
Emeric Brun819fc6f2017-06-13 19:37:32 +02003665
Willy Tarreau7d562212016-11-25 16:10:05 +01003666 /* First, update gpc0_rate if it's tracked. Second, update its
3667 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
3668 */
3669 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003670 if (ptr1) {
3671 period = stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u;
3672 }
3673 else {
3674 /* fallback on the gpc array */
3675 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 0);
3676 if (ptr1)
3677 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3678 }
3679
Willy Tarreau7d562212016-11-25 16:10:05 +01003680 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun726783d2021-06-30 19:06:43 +02003681 if (!ptr2) {
3682 /* fallback on the gpc array */
3683 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3684 }
3685
Emeric Brun819fc6f2017-06-13 19:37:32 +02003686 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003687 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01003688
Emeric Brun819fc6f2017-06-13 19:37:32 +02003689 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003690 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003691 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003692 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003693 }
3694
3695 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003696 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003697
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003698 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003699
3700 /* If data was modified, we need to touch to re-schedule sync */
3701 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3702 }
3703 else if (stkctr == &tmpstkctr)
3704 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003705 }
3706 return 1;
3707}
3708
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003709/* Increment the General Purpose Counter 1 value from the stream's tracked
3710 * frontend counters and return it into temp integer.
3711 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
3712 */
3713static int
3714smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3715{
3716 struct stkctr tmpstkctr;
3717 struct stkctr *stkctr;
Willy Tarreau5b654ad2021-07-06 18:51:12 +02003718 unsigned int period = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003719
3720 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3721 if (!stkctr)
3722 return 0;
3723
3724 smp->flags = SMP_F_VOL_TEST;
3725 smp->data.type = SMP_T_SINT;
3726 smp->data.u.sint = 0;
3727
3728 if (!stkctr_entry(stkctr))
3729 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3730
3731 if (stkctr && stkctr_entry(stkctr)) {
3732 void *ptr1,*ptr2;
3733
3734
3735 /* First, update gpc1_rate if it's tracked. Second, update its
3736 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
3737 */
3738 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
Emeric Brun726783d2021-06-30 19:06:43 +02003739 if (ptr1) {
3740 period = stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u;
3741 }
3742 else {
3743 /* fallback on the gpc array */
3744 ptr1 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC_RATE, 1);
3745 if (ptr1)
3746 period = stkctr->table->data_arg[STKTABLE_DT_GPC_RATE].u;
3747 }
3748
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003749 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
Emeric Brun726783d2021-06-30 19:06:43 +02003750 if (!ptr2) {
3751 /* fallback on the gpc array */
3752 ptr2 = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3753 }
3754
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003755 if (ptr1 || ptr2) {
3756 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3757
3758 if (ptr1) {
Emeric Brun0e3457b2021-06-30 17:18:28 +02003759 update_freq_ctr_period(&stktable_data_cast(ptr1, std_t_frqp),
Emeric Brun726783d2021-06-30 19:06:43 +02003760 period, 1);
Emeric Brun0e3457b2021-06-30 17:18:28 +02003761 smp->data.u.sint = (&stktable_data_cast(ptr1, std_t_frqp))->curr_ctr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003762 }
3763
3764 if (ptr2)
Emeric Brun0e3457b2021-06-30 17:18:28 +02003765 smp->data.u.sint = ++stktable_data_cast(ptr2, std_t_uint);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003766
3767 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3768
3769 /* If data was modified, we need to touch to re-schedule sync */
3770 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3771 }
3772 else if (stkctr == &tmpstkctr)
3773 stktable_release(stkctr->table, stkctr_entry(stkctr));
3774 }
3775 return 1;
3776}
3777
Emeric Brun4d7ada82021-06-30 19:04:16 +02003778/* Clear the GPC[args(0)] value from the stream's tracked
3779 * frontend counters and return its previous value into temp integer.
3780 * Supports being called as "sc_clr_gpc(<gpc-idx>,<sc-idx>[,<table>])"
3781 * or "src_clr_gpc(<gpc-idx>[,<table>])" only.
3782 */
3783static int
3784smp_fetch_sc_clr_gpc(const struct arg *args, struct sample *smp, const char *kw, void *private)
3785{
3786 struct stkctr tmpstkctr;
3787 struct stkctr *stkctr;
3788 unsigned int idx;
3789
3790 idx = args[0].data.sint;
3791
3792 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args + 1, kw, &tmpstkctr);
3793 if (!stkctr)
3794 return 0;
3795
3796 smp->flags = SMP_F_VOL_TEST;
3797 smp->data.type = SMP_T_SINT;
3798 smp->data.u.sint = 0;
3799
3800 if (!stkctr_entry(stkctr))
3801 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3802
3803 if (stkctr && stkctr_entry(stkctr)) {
3804 void *ptr;
3805
3806 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, idx);
3807 if (!ptr) {
3808 if (stkctr == &tmpstkctr)
3809 stktable_release(stkctr->table, stkctr_entry(stkctr));
3810 return 0; /* parameter not stored */
3811 }
3812
3813 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3814
3815 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3816 stktable_data_cast(ptr, std_t_uint) = 0;
3817
3818 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3819
3820 /* If data was modified, we need to touch to re-schedule sync */
3821 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3822 }
3823 return 1;
3824}
3825
Willy Tarreau7d562212016-11-25 16:10:05 +01003826/* Clear the General Purpose Counter 0 value from the stream's tracked
3827 * frontend counters and return its previous value into temp integer.
3828 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
3829 */
3830static int
3831smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
3832{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003833 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003834 struct stkctr *stkctr;
3835
Emeric Brun819fc6f2017-06-13 19:37:32 +02003836 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003837 if (!stkctr)
3838 return 0;
3839
3840 smp->flags = SMP_F_VOL_TEST;
3841 smp->data.type = SMP_T_SINT;
3842 smp->data.u.sint = 0;
3843
Emeric Brun819fc6f2017-06-13 19:37:32 +02003844 if (!stkctr_entry(stkctr))
3845 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003846
Emeric Brun819fc6f2017-06-13 19:37:32 +02003847 if (stkctr && stkctr_entry(stkctr)) {
3848 void *ptr;
3849
3850 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
3851 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003852 /* fallback on the gpc array */
3853 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 0);
3854 }
3855
3856 if (!ptr) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003857 if (stkctr == &tmpstkctr)
3858 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003859 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003860 }
3861
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003862 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003863
Emeric Brun0e3457b2021-06-30 17:18:28 +02003864 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3865 stktable_data_cast(ptr, std_t_uint) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003866
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003867 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003868
Willy Tarreau7d562212016-11-25 16:10:05 +01003869 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003870 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01003871 }
3872 return 1;
3873}
3874
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003875/* Clear the General Purpose Counter 1 value from the stream's tracked
3876 * frontend counters and return its previous value into temp integer.
3877 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
3878 */
3879static int
3880smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
3881{
3882 struct stkctr tmpstkctr;
3883 struct stkctr *stkctr;
3884
3885 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3886 if (!stkctr)
3887 return 0;
3888
3889 smp->flags = SMP_F_VOL_TEST;
3890 smp->data.type = SMP_T_SINT;
3891 smp->data.u.sint = 0;
3892
3893 if (!stkctr_entry(stkctr))
3894 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3895
3896 if (stkctr && stkctr_entry(stkctr)) {
3897 void *ptr;
3898
3899 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
3900 if (!ptr) {
Emeric Brun726783d2021-06-30 19:06:43 +02003901 /* fallback on the gpc array */
3902 ptr = stktable_data_ptr_idx(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC, 1);
3903 }
3904
3905 if (!ptr) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003906 if (stkctr == &tmpstkctr)
3907 stktable_release(stkctr->table, stkctr_entry(stkctr));
3908 return 0; /* parameter not stored */
3909 }
3910
3911 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3912
Emeric Brun0e3457b2021-06-30 17:18:28 +02003913 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
3914 stktable_data_cast(ptr, std_t_uint) = 0;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003915
3916 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3917
3918 /* If data was modified, we need to touch to re-schedule sync */
3919 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
3920 }
3921 return 1;
3922}
3923
Willy Tarreau7d562212016-11-25 16:10:05 +01003924/* set <smp> to the cumulated number of connections from the stream's tracked
3925 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3926 * "src_conn_cnt" only.
3927 */
3928static int
3929smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3930{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003931 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003932 struct stkctr *stkctr;
3933
Emeric Brun819fc6f2017-06-13 19:37:32 +02003934 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003935 if (!stkctr)
3936 return 0;
3937
3938 smp->flags = SMP_F_VOL_TEST;
3939 smp->data.type = SMP_T_SINT;
3940 smp->data.u.sint = 0;
3941 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003942 void *ptr;
3943
3944 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
3945 if (!ptr) {
3946 if (stkctr == &tmpstkctr)
3947 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003948 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003949 }
3950
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003951 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003952
Emeric Brun0e3457b2021-06-30 17:18:28 +02003953 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003954
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003955 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003956
3957 if (stkctr == &tmpstkctr)
3958 stktable_release(stkctr->table, stkctr_entry(stkctr));
3959
3960
Willy Tarreau7d562212016-11-25 16:10:05 +01003961 }
3962 return 1;
3963}
3964
3965/* set <smp> to the connection rate from the stream's tracked frontend
3966 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3967 * only.
3968 */
3969static int
3970smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3971{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003972 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003973 struct stkctr *stkctr;
3974
Emeric Brun819fc6f2017-06-13 19:37:32 +02003975 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003976 if (!stkctr)
3977 return 0;
3978
3979 smp->flags = SMP_F_VOL_TEST;
3980 smp->data.type = SMP_T_SINT;
3981 smp->data.u.sint = 0;
3982 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003983 void *ptr;
3984
3985 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
3986 if (!ptr) {
3987 if (stkctr == &tmpstkctr)
3988 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003989 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003990 }
3991
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003992 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003993
Emeric Brun0e3457b2021-06-30 17:18:28 +02003994 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01003995 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003996
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003997 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003998
3999 if (stkctr == &tmpstkctr)
4000 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004001 }
4002 return 1;
4003}
4004
4005/* set temp integer to the number of connections from the stream's source address
4006 * in the table pointed to by expr, after updating it.
4007 * Accepts exactly 1 argument of type table.
4008 */
4009static int
4010smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4011{
4012 struct connection *conn = objt_conn(smp->sess->origin);
4013 struct stksess *ts;
4014 struct stktable_key *key;
4015 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004016 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004017
4018 if (!conn)
4019 return 0;
4020
Joseph Herlant5662fa42018-11-15 13:43:28 -08004021 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02004022 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01004023 return 0;
4024
4025 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004026 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01004027 if (!key)
4028 return 0;
4029
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004030 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01004031
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004032 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01004033 /* entry does not exist and could not be created */
4034 return 0;
4035
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004036 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004037 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01004038 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004039 }
Willy Tarreau7d562212016-11-25 16:10:05 +01004040
4041 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004043 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004044
Emeric Brun0e3457b2021-06-30 17:18:28 +02004045 smp->data.u.sint = ++stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004046
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004047 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004048
Willy Tarreau7d562212016-11-25 16:10:05 +01004049 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004050
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004051 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004052
4053 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01004054 return 1;
4055}
4056
4057/* set <smp> to the number of concurrent connections from the stream's tracked
4058 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
4059 * "src_conn_cur" only.
4060 */
4061static int
4062smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
4063{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004064 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004065 struct stkctr *stkctr;
4066
Emeric Brun819fc6f2017-06-13 19:37:32 +02004067 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004068 if (!stkctr)
4069 return 0;
4070
4071 smp->flags = SMP_F_VOL_TEST;
4072 smp->data.type = SMP_T_SINT;
4073 smp->data.u.sint = 0;
4074 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004075 void *ptr;
4076
4077 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
4078 if (!ptr) {
4079 if (stkctr == &tmpstkctr)
4080 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004081 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004082 }
4083
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004084 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004085
Emeric Brun0e3457b2021-06-30 17:18:28 +02004086 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004087
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004088 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004089
4090 if (stkctr == &tmpstkctr)
4091 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004092 }
4093 return 1;
4094}
4095
4096/* set <smp> to the cumulated number of streams from the stream's tracked
4097 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
4098 * "src_sess_cnt" only.
4099 */
4100static int
4101smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4102{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004103 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004104 struct stkctr *stkctr;
4105
Emeric Brun819fc6f2017-06-13 19:37:32 +02004106 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004107 if (!stkctr)
4108 return 0;
4109
4110 smp->flags = SMP_F_VOL_TEST;
4111 smp->data.type = SMP_T_SINT;
4112 smp->data.u.sint = 0;
4113 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004114 void *ptr;
4115
4116 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
4117 if (!ptr) {
4118 if (stkctr == &tmpstkctr)
4119 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004120 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004121 }
4122
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004123 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004124
Emeric Brun0e3457b2021-06-30 17:18:28 +02004125 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004126
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004127 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004128
4129 if (stkctr == &tmpstkctr)
4130 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004131 }
4132 return 1;
4133}
4134
4135/* set <smp> to the stream rate from the stream's tracked frontend counters.
4136 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
4137 */
4138static int
4139smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4140{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004141 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004142 struct stkctr *stkctr;
4143
Emeric Brun819fc6f2017-06-13 19:37:32 +02004144 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004145 if (!stkctr)
4146 return 0;
4147
4148 smp->flags = SMP_F_VOL_TEST;
4149 smp->data.type = SMP_T_SINT;
4150 smp->data.u.sint = 0;
4151 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004152 void *ptr;
4153
4154 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
4155 if (!ptr) {
4156 if (stkctr == &tmpstkctr)
4157 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004158 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004159 }
4160
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004161 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004162
Emeric Brun0e3457b2021-06-30 17:18:28 +02004163 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004164 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004165
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004166 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004167
4168 if (stkctr == &tmpstkctr)
4169 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004170 }
4171 return 1;
4172}
4173
4174/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
4175 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
4176 * "src_http_req_cnt" only.
4177 */
4178static int
4179smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4180{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004181 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004182 struct stkctr *stkctr;
4183
Emeric Brun819fc6f2017-06-13 19:37:32 +02004184 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004185 if (!stkctr)
4186 return 0;
4187
4188 smp->flags = SMP_F_VOL_TEST;
4189 smp->data.type = SMP_T_SINT;
4190 smp->data.u.sint = 0;
4191 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004192 void *ptr;
4193
4194 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
4195 if (!ptr) {
4196 if (stkctr == &tmpstkctr)
4197 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004198 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004199 }
4200
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004201 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004202
Emeric Brun0e3457b2021-06-30 17:18:28 +02004203 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004204
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004205 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004206
4207 if (stkctr == &tmpstkctr)
4208 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004209 }
4210 return 1;
4211}
4212
4213/* set <smp> to the HTTP request rate from the stream's tracked frontend
4214 * counters. Supports being called as "sc[0-9]_http_req_rate" or
4215 * "src_http_req_rate" only.
4216 */
4217static int
4218smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4219{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004220 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004221 struct stkctr *stkctr;
4222
Emeric Brun819fc6f2017-06-13 19:37:32 +02004223 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004224 if (!stkctr)
4225 return 0;
4226
4227 smp->flags = SMP_F_VOL_TEST;
4228 smp->data.type = SMP_T_SINT;
4229 smp->data.u.sint = 0;
4230 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004231 void *ptr;
4232
4233 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
4234 if (!ptr) {
4235 if (stkctr == &tmpstkctr)
4236 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004237 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004238 }
4239
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004240 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004241
Emeric Brun0e3457b2021-06-30 17:18:28 +02004242 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004243 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004244
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004245 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004246
4247 if (stkctr == &tmpstkctr)
4248 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004249 }
4250 return 1;
4251}
4252
4253/* set <smp> to the cumulated number of HTTP requests errors from the stream's
4254 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
4255 * "src_http_err_cnt" only.
4256 */
4257static int
4258smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4259{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004260 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004261 struct stkctr *stkctr;
4262
Emeric Brun819fc6f2017-06-13 19:37:32 +02004263 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004264 if (!stkctr)
4265 return 0;
4266
4267 smp->flags = SMP_F_VOL_TEST;
4268 smp->data.type = SMP_T_SINT;
4269 smp->data.u.sint = 0;
4270 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004271 void *ptr;
4272
4273 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
4274 if (!ptr) {
4275 if (stkctr == &tmpstkctr)
4276 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004277 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004278 }
4279
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004280 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004281
Emeric Brun0e3457b2021-06-30 17:18:28 +02004282 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004283
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004284 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004285
4286 if (stkctr == &tmpstkctr)
4287 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004288 }
4289 return 1;
4290}
4291
4292/* set <smp> to the HTTP request error rate from the stream's tracked frontend
4293 * counters. Supports being called as "sc[0-9]_http_err_rate" or
4294 * "src_http_err_rate" only.
4295 */
4296static int
4297smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4298{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004299 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004300 struct stkctr *stkctr;
4301
Emeric Brun819fc6f2017-06-13 19:37:32 +02004302 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004303 if (!stkctr)
4304 return 0;
4305
4306 smp->flags = SMP_F_VOL_TEST;
4307 smp->data.type = SMP_T_SINT;
4308 smp->data.u.sint = 0;
4309 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004310 void *ptr;
4311
4312 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
4313 if (!ptr) {
4314 if (stkctr == &tmpstkctr)
4315 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004316 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004317 }
4318
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004319 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004320
Emeric Brun0e3457b2021-06-30 17:18:28 +02004321 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004322 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004323
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004324 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004325
4326 if (stkctr == &tmpstkctr)
4327 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004328 }
4329 return 1;
4330}
4331
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004332/* set <smp> to the cumulated number of HTTP response failures from the stream's
4333 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
4334 * "src_http_fail_cnt" only.
4335 */
4336static int
4337smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
4338{
4339 struct stkctr tmpstkctr;
4340 struct stkctr *stkctr;
4341
4342 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4343 if (!stkctr)
4344 return 0;
4345
4346 smp->flags = SMP_F_VOL_TEST;
4347 smp->data.type = SMP_T_SINT;
4348 smp->data.u.sint = 0;
4349 if (stkctr_entry(stkctr) != NULL) {
4350 void *ptr;
4351
4352 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
4353 if (!ptr) {
4354 if (stkctr == &tmpstkctr)
4355 stktable_release(stkctr->table, stkctr_entry(stkctr));
4356 return 0; /* parameter not stored */
4357 }
4358
4359 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4360
Emeric Brun0e3457b2021-06-30 17:18:28 +02004361 smp->data.u.sint = stktable_data_cast(ptr, std_t_uint);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004362
4363 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4364
4365 if (stkctr == &tmpstkctr)
4366 stktable_release(stkctr->table, stkctr_entry(stkctr));
4367 }
4368 return 1;
4369}
4370
4371/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
4372 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
4373 * "src_http_fail_rate" only.
4374 */
4375static int
4376smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4377{
4378 struct stkctr tmpstkctr;
4379 struct stkctr *stkctr;
4380
4381 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
4382 if (!stkctr)
4383 return 0;
4384
4385 smp->flags = SMP_F_VOL_TEST;
4386 smp->data.type = SMP_T_SINT;
4387 smp->data.u.sint = 0;
4388 if (stkctr_entry(stkctr) != NULL) {
4389 void *ptr;
4390
4391 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
4392 if (!ptr) {
4393 if (stkctr == &tmpstkctr)
4394 stktable_release(stkctr->table, stkctr_entry(stkctr));
4395 return 0; /* parameter not stored */
4396 }
4397
4398 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4399
Emeric Brun0e3457b2021-06-30 17:18:28 +02004400 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau826f3ab2021-02-10 12:07:15 +01004401 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
4402
4403 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
4404
4405 if (stkctr == &tmpstkctr)
4406 stktable_release(stkctr->table, stkctr_entry(stkctr));
4407 }
4408 return 1;
4409}
4410
Willy Tarreau7d562212016-11-25 16:10:05 +01004411/* set <smp> to the number of kbytes received from clients, as found in the
4412 * stream's tracked frontend counters. Supports being called as
4413 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
4414 */
4415static int
4416smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
4417{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004418 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004419 struct stkctr *stkctr;
4420
Emeric Brun819fc6f2017-06-13 19:37:32 +02004421 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004422 if (!stkctr)
4423 return 0;
4424
4425 smp->flags = SMP_F_VOL_TEST;
4426 smp->data.type = SMP_T_SINT;
4427 smp->data.u.sint = 0;
4428 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004429 void *ptr;
4430
4431 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
4432 if (!ptr) {
4433 if (stkctr == &tmpstkctr)
4434 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004435 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004436 }
4437
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004438 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004439
Emeric Brun0e3457b2021-06-30 17:18:28 +02004440 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004441
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004442 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004443
4444 if (stkctr == &tmpstkctr)
4445 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004446 }
4447 return 1;
4448}
4449
4450/* set <smp> to the data rate received from clients in bytes/s, as found
4451 * in the stream's tracked frontend counters. Supports being called as
4452 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
4453 */
4454static int
4455smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4456{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004457 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004458 struct stkctr *stkctr;
4459
Emeric Brun819fc6f2017-06-13 19:37:32 +02004460 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004461 if (!stkctr)
4462 return 0;
4463
4464 smp->flags = SMP_F_VOL_TEST;
4465 smp->data.type = SMP_T_SINT;
4466 smp->data.u.sint = 0;
4467 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004468 void *ptr;
4469
4470 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
4471 if (!ptr) {
4472 if (stkctr == &tmpstkctr)
4473 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004474 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004475 }
4476
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004477 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004478
Emeric Brun0e3457b2021-06-30 17:18:28 +02004479 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004480 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004481
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004482 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004483
4484 if (stkctr == &tmpstkctr)
4485 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004486 }
4487 return 1;
4488}
4489
4490/* set <smp> to the number of kbytes sent to clients, as found in the
4491 * stream's tracked frontend counters. Supports being called as
4492 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
4493 */
4494static int
4495smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
4496{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004497 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004498 struct stkctr *stkctr;
4499
Emeric Brun819fc6f2017-06-13 19:37:32 +02004500 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004501 if (!stkctr)
4502 return 0;
4503
4504 smp->flags = SMP_F_VOL_TEST;
4505 smp->data.type = SMP_T_SINT;
4506 smp->data.u.sint = 0;
4507 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004508 void *ptr;
4509
4510 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
4511 if (!ptr) {
4512 if (stkctr == &tmpstkctr)
4513 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004514 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004515 }
4516
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004517 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004518
Emeric Brun0e3457b2021-06-30 17:18:28 +02004519 smp->data.u.sint = stktable_data_cast(ptr, std_t_ull) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004520
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004521 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004522
4523 if (stkctr == &tmpstkctr)
4524 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004525 }
4526 return 1;
4527}
4528
4529/* set <smp> to the data rate sent to clients in bytes/s, as found in the
4530 * stream's tracked frontend counters. Supports being called as
4531 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
4532 */
4533static int
4534smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
4535{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004536 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004537 struct stkctr *stkctr;
4538
Emeric Brun819fc6f2017-06-13 19:37:32 +02004539 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004540 if (!stkctr)
4541 return 0;
4542
4543 smp->flags = SMP_F_VOL_TEST;
4544 smp->data.type = SMP_T_SINT;
4545 smp->data.u.sint = 0;
4546 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02004547 void *ptr;
4548
4549 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
4550 if (!ptr) {
4551 if (stkctr == &tmpstkctr)
4552 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004553 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02004554 }
4555
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004556 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004557
Emeric Brun0e3457b2021-06-30 17:18:28 +02004558 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7d562212016-11-25 16:10:05 +01004559 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004560
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004561 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004562
4563 if (stkctr == &tmpstkctr)
4564 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01004565 }
4566 return 1;
4567}
4568
4569/* set <smp> to the number of active trackers on the SC entry in the stream's
4570 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
4571 */
4572static int
4573smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
4574{
Emeric Brun819fc6f2017-06-13 19:37:32 +02004575 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01004576 struct stkctr *stkctr;
4577
Emeric Brun819fc6f2017-06-13 19:37:32 +02004578 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01004579 if (!stkctr)
4580 return 0;
4581
4582 smp->flags = SMP_F_VOL_TEST;
4583 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004584 if (stkctr == &tmpstkctr) {
4585 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
4586 stktable_release(stkctr->table, stkctr_entry(stkctr));
4587 }
4588 else {
4589 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
4590 }
4591
Willy Tarreau7d562212016-11-25 16:10:05 +01004592 return 1;
4593}
4594
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004595
4596/* The functions below are used to manipulate table contents from the CLI.
4597 * There are 3 main actions, "clear", "set" and "show". The code is shared
4598 * between all actions, and the action is encoded in the void *private in
4599 * the appctx as well as in the keyword registration, among one of the
4600 * following values.
4601 */
4602
4603enum {
4604 STK_CLI_ACT_CLR,
4605 STK_CLI_ACT_SET,
4606 STK_CLI_ACT_SHOW,
4607};
4608
Willy Tarreau4596fe22022-05-17 19:07:51 +02004609/* Dump the status of a table to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004610 * read buffer. It returns 0 if the output buffer is full
4611 * and needs to be called again, otherwise non-zero.
4612 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004613static int table_dump_head_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004614 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004615 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004616{
Willy Tarreauc12b3212022-05-27 11:08:15 +02004617 struct stream *s = __sc_strm(appctx_sc(appctx));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004618
4619 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004620 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004621
4622 /* any other information should be dumped here */
4623
William Lallemand07a62f72017-05-24 00:57:40 +02004624 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004625 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
4626
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004627 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004628 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004629
4630 return 1;
4631}
4632
Willy Tarreau4596fe22022-05-17 19:07:51 +02004633/* Dump a table entry to a stream connector's
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004634 * read buffer. It returns 0 if the output buffer is full
4635 * and needs to be called again, otherwise non-zero.
4636 */
Willy Tarreau83061a82018-07-13 11:56:34 +02004637static int table_dump_entry_to_buffer(struct buffer *msg,
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004638 struct appctx *appctx,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004639 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004640{
4641 int dt;
4642
4643 chunk_appendf(msg, "%p:", entry);
4644
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004645 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004646 char addr[INET_ADDRSTRLEN];
4647 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
4648 chunk_appendf(msg, " key=%s", addr);
4649 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004650 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004651 char addr[INET6_ADDRSTRLEN];
4652 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
4653 chunk_appendf(msg, " key=%s", addr);
4654 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004655 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01004656 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004657 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004658 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004659 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004660 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004661 }
4662 else {
4663 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004664 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004665 }
4666
Willy Tarreau16b282f2022-11-29 11:55:18 +01004667 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 +01004668
4669 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
4670 void *ptr;
4671
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004672 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004673 continue;
Emeric Brunc64a2a32021-06-30 18:01:02 +02004674 if (stktable_data_types[dt].is_array) {
4675 char tmp[16] = {};
4676 const char *name_pfx = stktable_data_types[dt].name;
4677 const char *name_sfx = NULL;
4678 unsigned int idx = 0;
4679 int i = 0;
4680
4681 /* split name to show index before first _ of the name
4682 * for example: 'gpc3_rate' if array name is 'gpc_rate'.
4683 */
4684 for (i = 0 ; i < (sizeof(tmp) - 1); i++) {
4685 if (!name_pfx[i])
4686 break;
4687 if (name_pfx[i] == '_') {
4688 name_pfx = &tmp[0];
4689 name_sfx = &stktable_data_types[dt].name[i];
4690 break;
4691 }
4692 tmp[i] = name_pfx[i];
4693 }
4694
4695 ptr = stktable_data_ptr_idx(t, entry, dt, idx);
4696 while (ptr) {
4697 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
4698 chunk_appendf(msg, " %s%u%s(%u)=", name_pfx, idx, name_sfx ? name_sfx : "", t->data_arg[dt].u);
4699 else
4700 chunk_appendf(msg, " %s%u%s=", name_pfx, idx, name_sfx ? name_sfx : "");
4701 switch (stktable_data_types[dt].std_type) {
4702 case STD_T_SINT:
4703 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4704 break;
4705 case STD_T_UINT:
4706 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4707 break;
4708 case STD_T_ULL:
4709 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
4710 break;
4711 case STD_T_FRQP:
4712 chunk_appendf(msg, "%u",
4713 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4714 t->data_arg[dt].u));
4715 break;
4716 }
4717 ptr = stktable_data_ptr_idx(t, entry, dt, ++idx);
4718 }
4719 continue;
4720 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004721 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brun01928ae2021-06-30 16:24:04 +02004722 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004723 else
4724 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
4725
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004726 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004727 switch (stktable_data_types[dt].std_type) {
4728 case STD_T_SINT:
4729 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
4730 break;
4731 case STD_T_UINT:
4732 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
4733 break;
4734 case STD_T_ULL:
Emeric Brun01928ae2021-06-30 16:24:04 +02004735 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004736 break;
4737 case STD_T_FRQP:
Emeric Brun01928ae2021-06-30 16:24:04 +02004738 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004739 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004740 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004741 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02004742 case STD_T_DICT: {
4743 struct dict_entry *de;
4744 de = stktable_data_cast(ptr, std_t_dict);
4745 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
4746 break;
4747 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004748 }
4749 }
4750 chunk_appendf(msg, "\n");
4751
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004752 if (applet_putchk(appctx, msg) == -1)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004753 return 0;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004754
4755 return 1;
4756}
4757
Willy Tarreau3c69e082022-05-03 11:35:07 +02004758/* appctx context used by the "show table" command */
4759struct show_table_ctx {
4760 void *target; /* table we want to dump, or NULL for all */
4761 struct stktable *t; /* table being currently dumped (first if NULL) */
4762 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
4763 long long value[STKTABLE_FILTER_LEN]; /* value to compare against */
4764 signed char data_type[STKTABLE_FILTER_LEN]; /* type of data to compare, or -1 if none */
4765 signed char data_op[STKTABLE_FILTER_LEN]; /* operator (STD_OP_*) when data_type set */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004766 enum {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004767 STATE_NEXT = 0, /* px points to next table, entry=NULL */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02004768 STATE_DUMP, /* px points to curr table, entry is valid, refcount held */
4769 STATE_DONE, /* done dumping */
4770 } state;
Willy Tarreau3c69e082022-05-03 11:35:07 +02004771 char action; /* action on the table : one of STK_CLI_ACT_* */
4772};
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004773
4774/* Processes a single table entry matching a specific key passed in argument.
4775 * returns 0 if wants to be called again, 1 if has ended processing.
4776 */
4777static int table_process_entry_per_key(struct appctx *appctx, char **args)
4778{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004779 struct show_table_ctx *ctx = appctx->svcctx;
4780 struct stktable *t = ctx->target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004781 struct stksess *ts;
4782 uint32_t uint32_key;
4783 unsigned char ip6_key[sizeof(struct in6_addr)];
4784 long long value;
4785 int data_type;
4786 int cur_arg;
4787 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004788 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004789
Willy Tarreau9d008692019-08-09 11:21:01 +02004790 if (!*args[4])
4791 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004792
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004793 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004794 case SMP_T_IPV4:
4795 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02004796 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004797 break;
4798 case SMP_T_IPV6:
Christopher Fauletb7c962b2021-11-15 09:17:25 +01004799 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
4800 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02004801 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004802 break;
4803 case SMP_T_SINT:
4804 {
4805 char *endptr;
4806 unsigned long val;
4807 errno = 0;
4808 val = strtoul(args[4], &endptr, 10);
4809 if ((errno == ERANGE && val == ULONG_MAX) ||
4810 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02004811 val > 0xffffffff)
4812 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004813 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02004814 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004815 break;
4816 }
4817 break;
4818 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02004819 static_table_key.key = args[4];
4820 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004821 break;
4822 default:
Willy Tarreau3c69e082022-05-03 11:35:07 +02004823 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004824 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02004825 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 +01004826 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02004827 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 +01004828 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02004829 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 +01004830 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004831 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004832 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004833 }
4834
4835 /* check permissions */
4836 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
4837 return 1;
4838
Willy Tarreau3c69e082022-05-03 11:35:07 +02004839 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004840 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004841 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004842 if (!ts)
4843 return 1;
4844 chunk_reset(&trash);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004845 if (!table_dump_head_to_buffer(&trash, appctx, t, t)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004846 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004847 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004848 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004849 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004850 if (!table_dump_entry_to_buffer(&trash, appctx, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004851 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004852 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004853 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004854 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004855 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004856 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004857 break;
4858
4859 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004860 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004861 if (!ts)
4862 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02004863
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004864 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004865 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004866 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004867 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004868 break;
4869
4870 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004871 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004872 if (!ts) {
4873 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02004874 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004875 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004876 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004877 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
4878 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004879 cli_err(appctx, "\"data.<type>\" followed by a value expected\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
4885 data_type = stktable_get_data_type(args[cur_arg] + 5);
4886 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004887 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004888 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004889 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004890 return 1;
4891 }
4892
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004893 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004894 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004895 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004896 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004897 return 1;
4898 }
4899
4900 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004901 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004902 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004903 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004904 return 1;
4905 }
4906
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004907 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004908
4909 switch (stktable_data_types[data_type].std_type) {
4910 case STD_T_SINT:
4911 stktable_data_cast(ptr, std_t_sint) = value;
4912 break;
4913 case STD_T_UINT:
4914 stktable_data_cast(ptr, std_t_uint) = value;
4915 break;
4916 case STD_T_ULL:
4917 stktable_data_cast(ptr, std_t_ull) = value;
4918 break;
4919 case STD_T_FRQP:
4920 /* We set both the current and previous values. That way
4921 * the reported frequency is stable during all the period
4922 * then slowly fades out. This allows external tools to
4923 * push measures without having to update them too often.
4924 */
4925 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004926 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004927 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02004928 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01004929 using its internal lock */
4930 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004931 frqp->prev_ctr = 0;
4932 frqp->curr_ctr = value;
4933 break;
4934 }
4935 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004936 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004937 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004938 break;
4939
4940 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02004941 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004942 }
4943 return 1;
4944}
4945
4946/* Prepares the appctx fields with the data-based filters from the command line.
4947 * Returns 0 if the dump can proceed, 1 if has ended processing.
4948 */
4949static int table_prepare_data_request(struct appctx *appctx, char **args)
4950{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004951 struct show_table_ctx *ctx = appctx->svcctx;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004952 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004953 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004954
Willy Tarreau3c69e082022-05-03 11:35:07 +02004955 if (ctx->action != STK_CLI_ACT_SHOW && ctx->action != STK_CLI_ACT_CLR)
Willy Tarreau9d008692019-08-09 11:21:01 +02004956 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004957
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004958 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
4959 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
4960 break;
4961 /* condition on stored data value */
Willy Tarreau3c69e082022-05-03 11:35:07 +02004962 ctx->data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
4963 if (ctx->data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004964 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004965
Willy Tarreau3c69e082022-05-03 11:35:07 +02004966 if (!((struct stktable *)ctx->target)->data_ofs[ctx->data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004967 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 +01004968
Willy Tarreau3c69e082022-05-03 11:35:07 +02004969 ctx->data_op[i] = get_std_op(args[4+3*i]);
4970 if (ctx->data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01004971 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 +01004972
Willy Tarreau3c69e082022-05-03 11:35:07 +02004973 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 +01004974 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
4975 }
4976
4977 if (*args[3+3*i]) {
4978 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 +01004979 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004980
4981 /* OK we're done, all the fields are set */
4982 return 0;
4983}
4984
4985/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004986static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004987{
Willy Tarreau3c69e082022-05-03 11:35:07 +02004988 struct show_table_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004989 int i;
4990
4991 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
Willy Tarreau3c69e082022-05-03 11:35:07 +02004992 ctx->data_type[i] = -1;
4993 ctx->target = NULL;
4994 ctx->entry = NULL;
4995 ctx->action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004996
4997 if (*args[2]) {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02004998 ctx->t = ctx->target = stktable_find_by_name(args[2]);
Willy Tarreau3c69e082022-05-03 11:35:07 +02004999 if (!ctx->target)
Willy Tarreau9d008692019-08-09 11:21:01 +02005000 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005001 }
5002 else {
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005003 ctx->t = stktables_list;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005004 if (ctx->action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005005 goto err_args;
5006 return 0;
5007 }
5008
5009 if (strcmp(args[3], "key") == 0)
5010 return table_process_entry_per_key(appctx, args);
5011 else if (strncmp(args[3], "data.", 5) == 0)
5012 return table_prepare_data_request(appctx, args);
5013 else if (*args[3])
5014 goto err_args;
5015
5016 return 0;
5017
5018err_args:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005019 switch (ctx->action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005020 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02005021 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 +01005022 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02005023 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 +01005024 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02005025 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005026 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02005027 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005028 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005029}
5030
5031/* This function is used to deal with table operations (dump or clear depending
5032 * on the action stored in appctx->private). It returns 0 if the output buffer is
5033 * full and it needs to be called again, otherwise non-zero.
5034 */
5035static int cli_io_handler_table(struct appctx *appctx)
5036{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005037 struct show_table_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02005038 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau475e4632022-05-27 10:26:46 +02005039 struct stream *s = __sc_strm(sc);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005040 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005041 int skip_entry;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005042 int show = ctx->action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005043
5044 /*
Willy Tarreau0f154ed2022-05-03 12:02:36 +02005045 * We have 3 possible states in ctx->state :
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005046 * - STATE_NEXT : the proxy pointer points to the next table to
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005047 * dump, the entry pointer is NULL ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005048 * - STATE_DUMP : the proxy pointer points to the current table
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005049 * and the entry pointer points to the next entry to be dumped,
5050 * and the refcount on the next entry is held ;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005051 * - STATE_DONE : nothing left to dump, the buffer may contain some
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005052 * data though.
5053 */
Christopher Faulet87633c32023-04-03 18:32:50 +02005054 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02005055 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005056 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005057 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005058 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005059 }
5060 return 1;
5061 }
5062
5063 chunk_reset(&trash);
5064
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005065 while (ctx->state != STATE_DONE) {
5066 switch (ctx->state) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005067 case STATE_NEXT:
Willy Tarreau3c69e082022-05-03 11:35:07 +02005068 if (!ctx->t ||
5069 (ctx->target &&
5070 ctx->t != ctx->target)) {
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005071 ctx->state = STATE_DONE;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005072 break;
5073 }
5074
Willy Tarreau3c69e082022-05-03 11:35:07 +02005075 if (ctx->t->size) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005076 if (show && !table_dump_head_to_buffer(&trash, appctx, ctx->t, ctx->target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005077 return 0;
5078
Willy Tarreau3c69e082022-05-03 11:35:07 +02005079 if (ctx->target &&
William Lallemand07a62f72017-05-24 00:57:40 +02005080 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005081 /* dump entries only if table explicitly requested */
Willy Tarreau76642222022-10-11 12:02:50 +02005082 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005083 eb = ebmb_first(&ctx->t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005084 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005085 ctx->entry = ebmb_entry(eb, struct stksess, key);
5086 ctx->entry->ref_cnt++;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005087 ctx->state = STATE_DUMP;
Willy Tarreau76642222022-10-11 12:02:50 +02005088 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005089 break;
5090 }
Willy Tarreau76642222022-10-11 12:02:50 +02005091 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005092 }
5093 }
Willy Tarreau3c69e082022-05-03 11:35:07 +02005094 ctx->t = ctx->t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005095 break;
5096
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005097 case STATE_DUMP:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005098 skip_entry = 0;
5099
Willy Tarreau3c69e082022-05-03 11:35:07 +02005100 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005101
Willy Tarreau3c69e082022-05-03 11:35:07 +02005102 if (ctx->data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005103 /* we're filtering on some data contents */
5104 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01005105 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005106 signed char op;
5107 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005108
Emeric Brun819fc6f2017-06-13 19:37:32 +02005109
Willy Tarreau2b64a352020-01-22 17:09:47 +01005110 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005111 if (ctx->data_type[i] == -1)
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005112 break;
Willy Tarreau3c69e082022-05-03 11:35:07 +02005113 dt = ctx->data_type[i];
5114 ptr = stktable_data_ptr(ctx->t,
5115 ctx->entry,
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005116 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005117
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005118 data = 0;
5119 switch (stktable_data_types[dt].std_type) {
5120 case STD_T_SINT:
5121 data = stktable_data_cast(ptr, std_t_sint);
5122 break;
5123 case STD_T_UINT:
5124 data = stktable_data_cast(ptr, std_t_uint);
5125 break;
5126 case STD_T_ULL:
5127 data = stktable_data_cast(ptr, std_t_ull);
5128 break;
5129 case STD_T_FRQP:
5130 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau3c69e082022-05-03 11:35:07 +02005131 ctx->t->data_arg[dt].u);
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005132 break;
5133 }
5134
Willy Tarreau3c69e082022-05-03 11:35:07 +02005135 op = ctx->data_op[i];
5136 value = ctx->value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005137
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01005138 /* skip the entry if the data does not match the test and the value */
5139 if ((data < value &&
5140 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
5141 (data == value &&
5142 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
5143 (data > value &&
5144 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
5145 skip_entry = 1;
5146 break;
5147 }
5148 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005149 }
5150
5151 if (show && !skip_entry &&
Willy Tarreaud0a06d52022-05-18 15:07:19 +02005152 !table_dump_entry_to_buffer(&trash, appctx, ctx->t, ctx->entry)) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005153 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005154 return 0;
5155 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005156
Willy Tarreau3c69e082022-05-03 11:35:07 +02005157 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ctx->entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005158
Willy Tarreau76642222022-10-11 12:02:50 +02005159 HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreau3c69e082022-05-03 11:35:07 +02005160 ctx->entry->ref_cnt--;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005161
Willy Tarreau3c69e082022-05-03 11:35:07 +02005162 eb = ebmb_next(&ctx->entry->key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005163 if (eb) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005164 struct stksess *old = ctx->entry;
5165 ctx->entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005166 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005167 __stksess_kill_if_expired(ctx->t, old);
5168 else if (!skip_entry && !ctx->entry->ref_cnt)
5169 __stksess_kill(ctx->t, old);
5170 ctx->entry->ref_cnt++;
Willy Tarreau76642222022-10-11 12:02:50 +02005171 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005172 break;
5173 }
5174
5175
5176 if (show)
Willy Tarreau3c69e082022-05-03 11:35:07 +02005177 __stksess_kill_if_expired(ctx->t, ctx->entry);
5178 else if (!skip_entry && !ctx->entry->ref_cnt)
5179 __stksess_kill(ctx->t, ctx->entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02005180
Willy Tarreau76642222022-10-11 12:02:50 +02005181 HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &ctx->t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005182
Willy Tarreau3c69e082022-05-03 11:35:07 +02005183 ctx->t = ctx->t->next;
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005184 ctx->state = STATE_NEXT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005185 break;
5186
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005187 default:
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005188 break;
5189 }
5190 }
5191 return 1;
5192}
5193
5194static void cli_release_show_table(struct appctx *appctx)
5195{
Willy Tarreau3c69e082022-05-03 11:35:07 +02005196 struct show_table_ctx *ctx = appctx->svcctx;
5197
Willy Tarreau7849d4c2022-05-03 11:45:02 +02005198 if (ctx->state == STATE_DUMP) {
Willy Tarreau3c69e082022-05-03 11:35:07 +02005199 stksess_kill_if_expired(ctx->t, ctx->entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005200 }
5201}
5202
Willy Tarreau6c011712023-01-06 16:09:58 +01005203static int stk_parse_stick_counters(char **args, int section_type, struct proxy *curpx,
5204 const struct proxy *defpx, const char *file, int line,
5205 char **err)
5206{
5207 char *error;
5208 int counters;
5209
5210 counters = strtol(args[1], &error, 10);
5211 if (*error != 0) {
5212 memprintf(err, "%s: '%s' is an invalid number", args[0], args[1]);
5213 return -1;
5214 }
5215
5216 if (counters < 0) {
5217 memprintf(err, "%s: the number of stick-counters may not be negative (was %d)", args[0], counters);
5218 return -1;
5219 }
5220
5221 global.tune.nb_stk_ctr = counters;
5222 return 0;
5223}
5224
5225/* This function creates the stk_ctr pools after the configuration parsing. It
5226 * returns 0 on success otherwise ERR_*. If nb_stk_ctr is 0, the pool remains
5227 * NULL.
5228 */
5229static int stkt_create_stk_ctr_pool(void)
5230{
5231 if (!global.tune.nb_stk_ctr)
5232 return 0;
5233
5234 pool_head_stk_ctr = create_pool("stk_ctr", sizeof(*((struct session*)0)->stkctr) * global.tune.nb_stk_ctr, MEM_F_SHARED);
5235 if (!pool_head_stk_ctr) {
5236 ha_alert("out of memory while creating the stick-counters pool.\n");
5237 return ERR_ABORT;
5238 }
5239 return 0;
5240}
5241
Willy Tarreau478331d2020-08-28 11:31:31 +02005242static void stkt_late_init(void)
5243{
5244 struct sample_fetch *f;
5245
5246 f = find_sample_fetch("src", strlen("src"));
5247 if (f)
5248 smp_fetch_src = f->process;
Willy Tarreau6c011712023-01-06 16:09:58 +01005249 hap_register_post_check(stkt_create_stk_ctr_pool);
Willy Tarreau478331d2020-08-28 11:31:31 +02005250}
5251
5252INITCALL0(STG_INIT, stkt_late_init);
5253
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005254/* register cli keywords */
5255static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005256 { { "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 },
5257 { { "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 },
5258 { { "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 +01005259 {{},}
5260}};
5261
Willy Tarreau0108d902018-11-25 19:14:37 +01005262INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01005263
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005264static struct action_kw_list tcp_conn_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005265 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005266 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5267 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5268 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005269 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5270 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005271 { /* END */ }
5272}};
5273
Willy Tarreau0108d902018-11-25 19:14:37 +01005274INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
5275
Willy Tarreau620408f2016-10-21 16:37:51 +02005276static struct action_kw_list tcp_sess_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005277 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005278 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5279 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5280 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005281 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5282 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02005283 { /* END */ }
5284}};
5285
Willy Tarreau0108d902018-11-25 19:14:37 +01005286INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
5287
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005288static struct action_kw_list tcp_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005289 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005290 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5291 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5292 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005293 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5294 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005295 { /* END */ }
5296}};
5297
Willy Tarreau0108d902018-11-25 19:14:37 +01005298INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
5299
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005300static struct action_kw_list tcp_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005301 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005302 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5303 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5304 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005305 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5306 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005307 { /* END */ }
5308}};
5309
Willy Tarreau0108d902018-11-25 19:14:37 +01005310INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
5311
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005312static struct action_kw_list http_req_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005313 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005314 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5315 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5316 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005317 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5318 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005319 { /* END */ }
5320}};
5321
Willy Tarreau0108d902018-11-25 19:14:37 +01005322INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
5323
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005324static struct action_kw_list http_res_kws = { { }, {
Willy Tarreau5a72d032023-01-02 18:15:20 +01005325 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005326 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5327 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5328 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
Emeric Brun877b0b52021-06-30 18:57:49 +02005329 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5330 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02005331 { /* END */ }
5332}};
5333
Willy Tarreau0108d902018-11-25 19:14:37 +01005334INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
5335
Christopher Fauleta9248042023-01-05 11:17:38 +01005336static struct action_kw_list http_after_res_kws = { { }, {
Aurelien DARRAGONe2907c72023-03-17 11:28:58 +01005337 { "sc-add-gpc", parse_add_gpc, KWF_MATCH_PREFIX },
Christopher Fauleta9248042023-01-05 11:17:38 +01005338 { "sc-inc-gpc", parse_inc_gpc, KWF_MATCH_PREFIX },
5339 { "sc-inc-gpc0", parse_inc_gpc, KWF_MATCH_PREFIX },
5340 { "sc-inc-gpc1", parse_inc_gpc, KWF_MATCH_PREFIX },
5341 { "sc-set-gpt", parse_set_gpt, KWF_MATCH_PREFIX },
5342 { "sc-set-gpt0", parse_set_gpt, KWF_MATCH_PREFIX },
5343 { /* END */ }
5344}};
5345
5346INITCALL1(STG_REGISTER, http_after_res_keywords_register, &http_after_res_kws);
5347
Willy Tarreau7d562212016-11-25 16:10:05 +01005348/* Note: must not be declared <const> as its list will be overwritten.
5349 * Please take care of keeping this list alphabetically sorted.
5350 */
5351static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
5352 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5353 { "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 +02005354 { "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 +01005355 { "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 +01005356 { "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 +01005357 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5358 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5359 { "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 +02005360 { "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 +01005361 { "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 +02005362 { "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 +01005363 { "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 +01005364 { "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 +02005365 { "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 +01005366 { "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 +01005367 { "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 +01005368 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5369 { "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 +01005370 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5371 { "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 +01005372 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5373 { "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 +02005374 { "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 +01005375 { "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 +01005376 { "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 +01005377 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5378 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5379 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5380 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5381 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5382 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5383 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5384 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5385 { "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 +01005386 { "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 +01005387 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5388 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5389 { "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 +01005390 { "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 +01005391 { "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 +01005392 { "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 +01005393 { "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 +01005394 { "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 +01005395 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5396 { "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 +01005397 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5398 { "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 +01005399 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5400 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5401 { "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 +01005402 { "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 +01005403 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5404 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5405 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5406 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5407 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5408 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5409 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5410 { "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 +02005411 { "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 +01005412 { "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 +01005413 { "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 +01005414 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5415 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5416 { "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 +01005417 { "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 +01005418 { "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 +01005419 { "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 +01005420 { "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 +01005421 { "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 +01005422 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5423 { "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 +01005424 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5425 { "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 +01005426 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5427 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5428 { "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 +01005429 { "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 +01005430 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5431 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5432 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5433 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5434 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5435 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5436 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5437 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5438 { "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 +01005439 { "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 +01005440 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5441 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5442 { "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 +01005443 { "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 +01005444 { "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 +01005445 { "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 +01005446 { "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 +01005447 { "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 +01005448 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5449 { "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 +01005450 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5451 { "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 +01005452 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5453 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5454 { "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 +01005455 { "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 +01005456 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5457 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5458 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5459 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5460 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
5461 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5462 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5463 { "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 +02005464 { "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 +01005465 { "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 +01005466 { "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 +01005467 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5468 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5469 { "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 +02005470 { "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 +01005471 { "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 +02005472 { "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 +01005473 { "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 +01005474 { "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 +02005475 { "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 +01005476 { "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 +01005477 { "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 +01005478 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5479 { "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 +01005480 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5481 { "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 +01005482 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5483 { "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 +02005484 { "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 +01005485 { "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 +01005486 { "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 +01005487 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5488 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5489 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5490 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5491 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
5492 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5493 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
5494 { /* END */ },
5495}};
5496
Willy Tarreau0108d902018-11-25 19:14:37 +01005497INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01005498
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005499/* Note: must not be declared <const> as its list will be overwritten */
5500static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02005501 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
5502 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5503 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5504 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5505 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5506 { "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 +02005507 { "table_expire", sample_conv_table_expire, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun877b0b52021-06-30 18:57:49 +02005508 { "table_gpt", sample_conv_table_gpt, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005509 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005510 { "table_gpc", sample_conv_table_gpc, ARG2(2,SINT,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005511 { "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 +01005512 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Emeric Brun4d7ada82021-06-30 19:04:16 +02005513 { "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 +02005514 { "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 +01005515 { "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 +02005516 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5517 { "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 +01005518 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5519 { "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 +02005520 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5521 { "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 +02005522 { "table_idle", sample_conv_table_idle, ARG2(1,TAB,SINT), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02005523 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5524 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5525 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5526 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5527 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
5528 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02005529 { /* END */ },
5530}};
5531
Willy Tarreau0108d902018-11-25 19:14:37 +01005532INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau6c011712023-01-06 16:09:58 +01005533
5534static struct cfg_kw_list cfg_kws = {{ },{
5535 { CFG_GLOBAL, "tune.stick-counters", stk_parse_stick_counters },
5536 { /* END */ }
5537}};
5538
5539INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);