blob: 851ae10c899359ef4f2ee7c3d268fd880afd7719 [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 Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020023#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli.h>
Thayne McCombs92149f92020-11-20 01:28:26 -070025#include <haproxy/dict.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020026#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020027#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020029#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020031#include <haproxy/net_helper.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020032#include <haproxy/peers.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/pool.h>
34#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020035#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/sample.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/stick_table.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020041#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020042#include <haproxy/tcp_rules.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020043#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020044#include <haproxy/tools.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010045
Emeric Brun3bd697e2010-01-04 15:23:48 +010046
Willy Tarreau12785782012-04-27 21:37:17 +020047/* structure used to return a table key built from a sample */
Emeric Brun819fc6f2017-06-13 19:37:32 +020048static THREAD_LOCAL struct stktable_key static_table_key;
Willy Tarreau478331d2020-08-28 11:31:31 +020049static int (*smp_fetch_src)(const struct arg *, struct sample *, const char *, void *);
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020050
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010051struct stktable *stktables_list;
52struct eb_root stktable_by_name = EB_ROOT;
53
Olivier Houchard52dabbc2018-11-14 17:54:36 +010054#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010055
56/* This function inserts stktable <t> into the tree of known stick-table.
57 * The stick-table ID is used as the storing key so it must already have
58 * been initialized.
59 */
60void stktable_store_name(struct stktable *t)
61{
62 t->name.key = t->id;
63 ebis_insert(&stktable_by_name, &t->name);
64}
65
66struct stktable *stktable_find_by_name(const char *name)
67{
68 struct ebpt_node *node;
69 struct stktable *t;
70
71 node = ebis_lookup(&stktable_by_name, name);
72 if (node) {
73 t = container_of(node, struct stktable, name);
Tim Duesterhuse5ff1412021-01-02 22:31:53 +010074 if (strcmp(t->id, name) == 0)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010075 return t;
76 }
77
78 return NULL;
79}
80
Emeric Brun3bd697e2010-01-04 15:23:48 +010081/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020082 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
83 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010084 */
Emeric Brun819fc6f2017-06-13 19:37:32 +020085void __stksess_free(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010086{
87 t->current--;
Olivier Houchard52dabbc2018-11-14 17:54:36 +010088 pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
Emeric Brun3bd697e2010-01-04 15:23:48 +010089}
90
91/*
Emeric Brun819fc6f2017-06-13 19:37:32 +020092 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
93 * in table <t>.
94 * This function locks the table
95 */
96void stksess_free(struct stktable *t, struct stksess *ts)
97{
Thayne McCombs92149f92020-11-20 01:28:26 -070098 void *data;
99 data = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_KEY);
100 if (data) {
101 dict_entry_unref(&server_key_dict, stktable_data_cast(data, server_key));
102 stktable_data_cast(data, server_key) = NULL;
103 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100104 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200105 __stksess_free(t, ts);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100106 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200107}
108
109/*
Willy Tarreauf6efda12010-08-03 20:34:06 +0200110 * Kill an stksess (only if its ref_cnt is zero).
111 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200112int __stksess_kill(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200113{
114 if (ts->ref_cnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200115 return 0;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200116
117 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +0200118 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200119 ebmb_delete(&ts->key);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200120 __stksess_free(t, ts);
121 return 1;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200122}
123
124/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200125 * Decrease the refcount if decrefcnt is not 0.
126 * and try to kill the stksess
127 * This function locks the table
128 */
129int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcnt)
130{
131 int ret;
132
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100133 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200134 if (decrefcnt)
135 ts->ref_cnt--;
136 ret = __stksess_kill(t, ts);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100137 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200138
139 return ret;
140}
141
142/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200143 * Initialize or update the key in the sticky session <ts> present in table <t>
144 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100145 */
Willy Tarreau393379c2010-06-06 12:11:37 +0200146void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200148 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200149 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100150 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +0200151 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
152 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 }
154}
155
156
157/*
Willy Tarreau393379c2010-06-06 12:11:37 +0200158 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
159 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100160 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200161static struct stksess *__stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100162{
Willy Tarreau393379c2010-06-06 12:11:37 +0200163 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200164 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200165 ts->key.node.leaf_p = NULL;
166 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200167 ts->upd.node.leaf_p = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200168 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100169 HA_RWLOCK_INIT(&ts->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 return ts;
171}
172
173/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200174 * Trash oldest <to_batch> sticky sessions from table <t>
Willy Tarreaudfe79252020-11-03 17:47:41 +0100175 * Returns number of trashed sticky sessions. It may actually trash less
176 * than expected if finding these requires too long a search time (e.g.
177 * most of them have ts->ref_cnt>0).
Emeric Brun3bd697e2010-01-04 15:23:48 +0100178 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200179int __stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100180{
181 struct stksess *ts;
182 struct eb32_node *eb;
Willy Tarreaudfe79252020-11-03 17:47:41 +0100183 int max_search = to_batch * 2; // no more than 50% misses
Emeric Brun3bd697e2010-01-04 15:23:48 +0100184 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200185 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100186
187 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
188
189 while (batched < to_batch) {
190
191 if (unlikely(!eb)) {
192 /* we might have reached the end of the tree, typically because
193 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200194 * half. Let's loop back to the beginning of the tree now if we
195 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200197 if (looped)
198 break;
199 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100200 eb = eb32_first(&t->exps);
201 if (likely(!eb))
202 break;
203 }
204
Willy Tarreaudfe79252020-11-03 17:47:41 +0100205 if (--max_search < 0)
206 break;
207
Emeric Brun3bd697e2010-01-04 15:23:48 +0100208 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200209 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100210 eb = eb32_next(eb);
211
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200212 /* don't delete an entry which is currently referenced */
213 if (ts->ref_cnt)
214 continue;
215
Willy Tarreau86257dc2010-06-06 12:57:10 +0200216 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100217
Willy Tarreau86257dc2010-06-06 12:57:10 +0200218 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100219 if (!tick_isset(ts->expire))
220 continue;
221
Willy Tarreau86257dc2010-06-06 12:57:10 +0200222 ts->exp.key = ts->expire;
223 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100224
Aleksey Ponomaryovb38ad122023-02-07 19:27:06 +0100225 /* the update might have jumped beyond the next element,
226 * possibly causing a wrapping. We need to check whether
227 * the next element should be used instead. If the next
228 * element doesn't exist it means we're on the right
229 * side and have to check the first one then. If it
230 * exists and is closer, we must use it, otherwise we
231 * use the current one.
232 */
233 if (!eb)
234 eb = eb32_first(&t->exps);
235
236 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200237 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238
239 continue;
240 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100241
Willy Tarreauaea940e2010-06-06 11:56:36 +0200242 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200243 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200244 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200245 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100246 batched++;
247 }
248
249 return batched;
250}
251
252/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200253 * Trash oldest <to_batch> sticky sessions from table <t>
254 * Returns number of trashed sticky sessions.
255 * This function locks the table
256 */
257int stktable_trash_oldest(struct stktable *t, int to_batch)
258{
259 int ret;
260
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100261 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200262 ret = __stktable_trash_oldest(t, to_batch);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100263 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200264
265 return ret;
266}
267/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200268 * Allocate and initialise a new sticky session.
269 * The new sticky session is returned or NULL in case of lack of memory.
270 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200271 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
272 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100273 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200274struct stksess *__stksess_new(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100275{
276 struct stksess *ts;
277
278 if (unlikely(t->current == t->size)) {
279 if ( t->nopurge )
280 return NULL;
281
Emeric Brun819fc6f2017-06-13 19:37:32 +0200282 if (!__stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100283 return NULL;
284 }
285
Willy Tarreaubafbe012017-11-24 17:34:44 +0100286 ts = pool_alloc(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100287 if (ts) {
288 t->current++;
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100289 ts = (void *)ts + round_ptr_size(t->data_size);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 __stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200291 if (key)
292 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100293 }
294
295 return ts;
296}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200297/*
298 * Allocate and initialise a new sticky session.
299 * The new sticky session is returned or NULL in case of lack of memory.
300 * Sticky sessions should only be allocated this way, and must be freed using
301 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
302 * is not NULL, it is assigned to the new session.
303 * This function locks the table
304 */
305struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
306{
307 struct stksess *ts;
308
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100309 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200310 ts = __stksess_new(t, key);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100311 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200312
313 return ts;
314}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100315
316/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200317 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200318 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100319 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200320struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100321{
322 struct ebmb_node *eb;
323
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200324 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200325 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 +0100326 else
327 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
328
329 if (unlikely(!eb)) {
330 /* no session found */
331 return NULL;
332 }
333
Willy Tarreau86257dc2010-06-06 12:57:10 +0200334 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100335}
336
Emeric Brun819fc6f2017-06-13 19:37:32 +0200337/*
338 * Looks in table <t> for a sticky session matching key <key>.
339 * Returns pointer on requested sticky session or NULL if none was found.
340 * The refcount of the found entry is increased and this function
341 * is protected using the table lock
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200342 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200343struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200344{
345 struct stksess *ts;
346
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200348 ts = __stktable_lookup_key(t, key);
349 if (ts)
350 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200352
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200353 return ts;
354}
355
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200356/*
357 * Looks in table <t> for a sticky session with same key as <ts>.
358 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100359 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200360struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100361{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100362 struct ebmb_node *eb;
363
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200364 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200365 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100366 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200367 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100368
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200369 if (unlikely(!eb))
370 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100371
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200372 return ebmb_entry(eb, struct stksess, key);
373}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100374
Emeric Brun819fc6f2017-06-13 19:37:32 +0200375/*
376 * Looks in table <t> for a sticky session with same key as <ts>.
377 * Returns pointer on requested sticky session or NULL if none was found.
378 * The refcount of the found entry is increased and this function
379 * is protected using the table lock
380 */
381struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
382{
383 struct stksess *lts;
384
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100385 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200386 lts = __stktable_lookup(t, ts);
387 if (lts)
388 lts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100389 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200390
391 return lts;
392}
393
Willy Tarreaucb183642010-06-06 17:58:34 +0200394/* Update the expiration timer for <ts> but do not touch its expiration node.
395 * The table's expiration timer is updated if set.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200396 * The node will be also inserted into the update tree if needed, at a position
397 * depending if the update is a local or coming from a remote node
Willy Tarreaucb183642010-06-06 17:58:34 +0200398 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200399void __stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200400{
Emeric Brun85e77c72010-09-23 18:16:52 +0200401 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200402 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200403 if (t->expire) {
404 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
405 task_queue(t->exp_task);
406 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200407
Emeric Brun819fc6f2017-06-13 19:37:32 +0200408 /* If sync is enabled */
409 if (t->sync_task) {
410 if (local) {
411 /* If this entry is not in the tree
412 or not scheduled for at least one peer */
413 if (!ts->upd.node.leaf_p
414 || (int)(t->commitupdate - ts->upd.key) >= 0
415 || (int)(ts->upd.key - t->localupdate) >= 0) {
416 ts->upd.key = ++t->update;
417 t->localupdate = t->update;
418 eb32_delete(&ts->upd);
419 eb = eb32_insert(&t->updates, &ts->upd);
420 if (eb != &ts->upd) {
421 eb32_delete(eb);
422 eb32_insert(&t->updates, &ts->upd);
423 }
Emeric Brunaaf58602015-06-15 17:23:30 +0200424 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200425 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
Emeric Brun85e77c72010-09-23 18:16:52 +0200426 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200427 else {
428 /* If this entry is not in the tree */
429 if (!ts->upd.node.leaf_p) {
430 ts->upd.key= (++t->update)+(2147483648U);
431 eb = eb32_insert(&t->updates, &ts->upd);
432 if (eb != &ts->upd) {
433 eb32_delete(eb);
434 eb32_insert(&t->updates, &ts->upd);
435 }
436 }
437 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200438 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200439}
440
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200441/* Update the expiration timer for <ts> but do not touch its expiration node.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200442 * The table's expiration timer is updated using the date of expiration coming from
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200443 * <t> stick-table configuration.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200444 * The node will be also inserted into the update tree if needed, at a position
445 * considering the update is coming from a remote node
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200446 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200447void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
448{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200450 __stktable_touch_with_exp(t, ts, 0, ts->expire);
451 if (decrefcnt)
452 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100453 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200454}
455
456/* Update the expiration timer for <ts> but do not touch its expiration node.
457 * The table's expiration timer is updated using the date of expiration coming from
458 * <t> stick-table configuration.
459 * The node will be also inserted into the update tree if needed, at a position
460 * considering the update was made locally
461 */
462void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200463{
464 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
465
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100466 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200467 __stktable_touch_with_exp(t, ts, 1, expire);
468 if (decrefcnt)
469 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100470 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200471}
Willy Tarreau43e90352018-06-27 06:25:57 +0200472/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL */
473static void stktable_release(struct stktable *t, struct stksess *ts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200474{
Willy Tarreau43e90352018-06-27 06:25:57 +0200475 if (!ts)
476 return;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200478 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200480}
481
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200482/* Insert new sticky session <ts> in the table. It is assumed that it does not
483 * yet exist (the caller must check this). The table's timeout is updated if it
484 * is set. <ts> is returned.
485 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200486void __stktable_store(struct stktable *t, struct stksess *ts)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200487{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100488
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200489 ebmb_insert(&t->keys, &ts->key, t->key_size);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200490 ts->exp.key = ts->expire;
491 eb32_insert(&t->exps, &ts->exp);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200492 if (t->expire) {
493 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
494 task_queue(t->exp_task);
495 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200496}
497
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200498/* Returns a valid or initialized stksess for the specified stktable_key in the
499 * specified table, or NULL if the key was NULL, or if no entry was found nor
500 * could be created. The entry's expiration is updated.
501 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200502struct stksess *__stktable_get_entry(struct stktable *table, struct stktable_key *key)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200503{
504 struct stksess *ts;
505
506 if (!key)
507 return NULL;
508
Emeric Brun819fc6f2017-06-13 19:37:32 +0200509 ts = __stktable_lookup_key(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200510 if (ts == NULL) {
511 /* entry does not exist, initialize a new one */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200512 ts = __stksess_new(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200513 if (!ts)
514 return NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200515 __stktable_store(table, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200516 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200517 return ts;
518}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200519/* Returns a valid or initialized stksess for the specified stktable_key in the
520 * specified table, or NULL if the key was NULL, or if no entry was found nor
521 * could be created. The entry's expiration is updated.
522 * This function locks the table, and the refcount of the entry is increased.
523 */
524struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
525{
526 struct stksess *ts;
527
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100528 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200529 ts = __stktable_get_entry(table, key);
530 if (ts)
531 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100532 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200533
534 return ts;
535}
536
537/* Lookup for an entry with the same key and store the submitted
538 * stksess if not found.
539 */
540struct stksess *__stktable_set_entry(struct stktable *table, struct stksess *nts)
541{
542 struct stksess *ts;
543
544 ts = __stktable_lookup(table, nts);
545 if (ts == NULL) {
546 ts = nts;
547 __stktable_store(table, ts);
548 }
549 return ts;
550}
551
552/* Lookup for an entry with the same key and store the submitted
553 * stksess if not found.
554 * This function locks the table, and the refcount of the entry is increased.
555 */
556struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
557{
558 struct stksess *ts;
559
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100560 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200561 ts = __stktable_set_entry(table, nts);
562 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100563 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200564
Emeric Brun819fc6f2017-06-13 19:37:32 +0200565 return ts;
566}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100567/*
Willy Tarreaud2636522022-11-14 18:02:44 +0100568 * Task processing function to trash expired sticky sessions. A pointer to the
569 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100570 */
Willy Tarreaud2636522022-11-14 18:02:44 +0100571struct task *process_table_expire(struct task *task, void *context, unsigned int state)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100572{
Willy Tarreaud2636522022-11-14 18:02:44 +0100573 struct stktable *t = context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100574 struct stksess *ts;
575 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200576 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100577
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100578 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100579 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
580
581 while (1) {
582 if (unlikely(!eb)) {
583 /* we might have reached the end of the tree, typically because
584 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200585 * half. Let's loop back to the beginning of the tree now if we
586 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100587 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200588 if (looped)
589 break;
590 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100591 eb = eb32_first(&t->exps);
592 if (likely(!eb))
593 break;
594 }
595
596 if (likely(tick_is_lt(now_ms, eb->key))) {
597 /* timer not expired yet, revisit it later */
598 t->exp_next = eb->key;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100599 goto out_unlock;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100600 }
601
602 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200603 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100604 eb = eb32_next(eb);
605
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200606 /* don't delete an entry which is currently referenced */
607 if (ts->ref_cnt)
608 continue;
609
Willy Tarreau86257dc2010-06-06 12:57:10 +0200610 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100611
612 if (!tick_is_expired(ts->expire, now_ms)) {
613 if (!tick_isset(ts->expire))
614 continue;
615
Willy Tarreau86257dc2010-06-06 12:57:10 +0200616 ts->exp.key = ts->expire;
617 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100618
Aleksey Ponomaryovb38ad122023-02-07 19:27:06 +0100619 /* the update might have jumped beyond the next element,
620 * possibly causing a wrapping. We need to check whether
621 * the next element should be used instead. If the next
622 * element doesn't exist it means we're on the right
623 * side and have to check the first one then. If it
624 * exists and is closer, we must use it, otherwise we
625 * use the current one.
626 */
627 if (!eb)
628 eb = eb32_first(&t->exps);
629
630 if (!eb || tick_is_lt(ts->exp.key, eb->key))
Willy Tarreau86257dc2010-06-06 12:57:10 +0200631 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100632 continue;
633 }
634
635 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200636 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200637 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200638 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100639 }
640
641 /* We have found no task to expire in any tree */
642 t->exp_next = TICK_ETERNITY;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100643out_unlock:
Willy Tarreaud2636522022-11-14 18:02:44 +0100644 task->expire = t->exp_next;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100645 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100646 return task;
647}
648
Willy Tarreauaea940e2010-06-06 11:56:36 +0200649/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100650int stktable_init(struct stktable *t)
651{
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200652 int peers_retval = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100653 if (t->size) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200654 t->keys = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100655 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100656 t->updates = EB_ROOT_UNIQUE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100657 HA_SPIN_INIT(&t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100658
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100659 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 +0100660
661 t->exp_next = TICK_ETERNITY;
662 if ( t->expire ) {
Emeric Brunc60def82017-09-27 14:59:38 +0200663 t->exp_task = task_new(MAX_THREADS_MASK);
Willy Tarreau848522f2018-10-15 11:12:15 +0200664 if (!t->exp_task)
665 return 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100666 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100667 t->exp_task->context = (void *)t;
668 }
Willy Tarreauc3914d42020-09-24 08:39:22 +0200669 if (t->peers.p && t->peers.p->peers_fe && !t->peers.p->peers_fe->disabled) {
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200670 peers_retval = peers_register_table(t->peers.p, t);
Emeric Brun32da3c42010-09-23 18:39:19 +0200671 }
672
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200673 return (t->pool != NULL) && !peers_retval;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100674 }
675 return 1;
676}
677
678/*
679 * Configuration keywords of known table types
680 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200681struct stktable_type stktable_types[SMP_TYPES] = {
682 [SMP_T_SINT] = { "integer", 0, 4 },
683 [SMP_T_IPV4] = { "ip", 0, 4 },
684 [SMP_T_IPV6] = { "ipv6", 0, 16 },
685 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
686 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
687};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100688
689/*
690 * Parse table type configuration.
691 * Returns 0 on successful parsing, else 1.
692 * <myidx> is set at next configuration <args> index.
693 */
William Lallemand42b46252023-04-13 14:33:52 +0200694int 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 +0100695{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200696 for (*type = 0; *type < SMP_TYPES; (*type)++) {
697 if (!stktable_types[*type].kw)
698 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100699 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
700 continue;
701
702 *key_size = stktable_types[*type].default_size;
703 (*myidx)++;
704
Willy Tarreauaea940e2010-06-06 11:56:36 +0200705 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100706 if (strcmp("len", args[*myidx]) == 0) {
William Lallemand42b46252023-04-13 14:33:52 +0200707 char *stop;
708
Emeric Brun3bd697e2010-01-04 15:23:48 +0100709 (*myidx)++;
William Lallemand42b46252023-04-13 14:33:52 +0200710 *key_size = strtol(args[*myidx], &stop, 10);
711 if (*stop != '\0' || !*key_size) {
712 ha_alert("parsing [%s:%d] : 'len' expects a positive integer argument.\n", file, linenum);
713 return 1;
714 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200715 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200716 /* null terminated string needs +1 for '\0'. */
717 (*key_size)++;
718 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100719 (*myidx)++;
720 }
721 }
722 return 0;
723 }
William Lallemand42b46252023-04-13 14:33:52 +0200724 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n", file, linenum, args[0], args[*myidx]);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100725 return 1;
726}
727
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200728/* reserve some space for data type <type>, and associate argument at <sa> if
729 * not NULL. Returns PE_NONE (0) if OK or an error code among :
730 * - PE_ENUM_OOR if <type> does not exist
731 * - PE_EXIST if <type> is already registered
732 * - PE_ARG_NOT_USE if <sa> was provided but not expected
733 * - PE_ARG_MISSING if <sa> was expected but not provided
734 */
735int stktable_alloc_data_type(struct stktable *t, int type, const char *sa)
736{
737 if (type >= STKTABLE_DATA_TYPES)
738 return PE_ENUM_OOR;
739
740 if (t->data_ofs[type])
741 /* already allocated */
742 return PE_EXIST;
743
744 switch (stktable_data_types[type].arg_type) {
745 case ARG_T_NONE:
746 if (sa)
747 return PE_ARG_NOT_USED;
748 break;
749 case ARG_T_INT:
750 if (!sa)
751 return PE_ARG_MISSING;
752 t->data_arg[type].i = atoi(sa);
753 break;
754 case ARG_T_DELAY:
755 if (!sa)
756 return PE_ARG_MISSING;
757 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
758 if (sa)
759 return PE_ARG_INVC; /* invalid char */
760 break;
761 }
762
763 t->data_size += stktable_type_size(stktable_data_types[type].std_type);
764 t->data_ofs[type] = -t->data_size;
765 return PE_NONE;
766}
767
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100768/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100769 * Parse a line with <linenum> as number in <file> configuration file to configure
770 * the stick-table with <t> as address and <id> as ID.
771 * <peers> provides the "peers" section pointer only if this function is called
772 * from a "peers" section.
773 * <nid> is the stick-table name which is sent over the network. It must be equal
774 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
775 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500776 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100777 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
778 */
779int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100780 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100781{
782 int err_code = 0;
783 int idx = 1;
784 unsigned int val;
785
786 if (!id || !*id) {
787 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
788 err_code |= ERR_ALERT | ERR_ABORT;
789 goto out;
790 }
791
792 /* Store the "peers" section if this function is called from a "peers" section. */
793 if (peers) {
794 t->peers.p = peers;
795 idx++;
796 }
797
798 t->id = id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100799 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100800 t->type = (unsigned int)-1;
801 t->conf.file = file;
802 t->conf.line = linenum;
803
804 while (*args[idx]) {
805 const char *err;
806
807 if (strcmp(args[idx], "size") == 0) {
808 idx++;
809 if (!*(args[idx])) {
810 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
811 file, linenum, args[0], args[idx-1]);
812 err_code |= ERR_ALERT | ERR_FATAL;
813 goto out;
814 }
815 if ((err = parse_size_err(args[idx], &t->size))) {
816 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
817 file, linenum, args[0], *err, args[idx-1]);
818 err_code |= ERR_ALERT | ERR_FATAL;
819 goto out;
820 }
821 idx++;
822 }
823 /* This argument does not exit in "peers" section. */
824 else if (!peers && strcmp(args[idx], "peers") == 0) {
825 idx++;
826 if (!*(args[idx])) {
827 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
828 file, linenum, args[0], args[idx-1]);
829 err_code |= ERR_ALERT | ERR_FATAL;
830 goto out;
831 }
832 t->peers.name = strdup(args[idx++]);
833 }
834 else if (strcmp(args[idx], "expire") == 0) {
835 idx++;
836 if (!*(args[idx])) {
837 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
838 file, linenum, args[0], args[idx-1]);
839 err_code |= ERR_ALERT | ERR_FATAL;
840 goto out;
841 }
842 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200843 if (err == PARSE_TIME_OVER) {
844 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
845 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100846 err_code |= ERR_ALERT | ERR_FATAL;
847 goto out;
848 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200849 else if (err == PARSE_TIME_UNDER) {
850 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
851 file, linenum, args[0], args[idx], args[idx-1]);
852 err_code |= ERR_ALERT | ERR_FATAL;
853 goto out;
854 }
855 else if (err) {
856 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
857 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100858 err_code |= ERR_ALERT | ERR_FATAL;
859 goto out;
860 }
861 t->expire = val;
862 idx++;
863 }
864 else if (strcmp(args[idx], "nopurge") == 0) {
865 t->nopurge = 1;
866 idx++;
867 }
868 else if (strcmp(args[idx], "type") == 0) {
869 idx++;
William Lallemand42b46252023-04-13 14:33:52 +0200870 if (stktable_parse_type(args, &idx, &t->type, &t->key_size, file, linenum) != 0) {
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100871 err_code |= ERR_ALERT | ERR_FATAL;
872 goto out;
873 }
874 /* idx already points to next arg */
875 }
876 else if (strcmp(args[idx], "store") == 0) {
877 int type, err;
878 char *cw, *nw, *sa;
879
880 idx++;
881 nw = args[idx];
882 while (*nw) {
883 /* the "store" keyword supports a comma-separated list */
884 cw = nw;
885 sa = NULL; /* store arg */
886 while (*nw && *nw != ',') {
887 if (*nw == '(') {
888 *nw = 0;
889 sa = ++nw;
890 while (*nw != ')') {
891 if (!*nw) {
892 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
893 file, linenum, args[0], cw);
894 err_code |= ERR_ALERT | ERR_FATAL;
895 goto out;
896 }
897 nw++;
898 }
899 *nw = '\0';
900 }
901 nw++;
902 }
903 if (*nw)
904 *nw++ = '\0';
905 type = stktable_get_data_type(cw);
906 if (type < 0) {
907 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
908 file, linenum, args[0], cw);
909 err_code |= ERR_ALERT | ERR_FATAL;
910 goto out;
911 }
912
913 err = stktable_alloc_data_type(t, type, sa);
914 switch (err) {
915 case PE_NONE: break;
916 case PE_EXIST:
917 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
918 file, linenum, args[0], cw);
919 err_code |= ERR_WARN;
920 break;
921
922 case PE_ARG_MISSING:
923 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
924 file, linenum, args[0], cw);
925 err_code |= ERR_ALERT | ERR_FATAL;
926 goto out;
927
928 case PE_ARG_NOT_USED:
929 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
930 file, linenum, args[0], cw);
931 err_code |= ERR_ALERT | ERR_FATAL;
932 goto out;
933
934 default:
935 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
936 file, linenum, args[0], cw);
937 err_code |= ERR_ALERT | ERR_FATAL;
938 goto out;
939 }
940 }
941 idx++;
942 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700943 else if (strcmp(args[idx], "srvkey") == 0) {
944 char *keytype;
945 idx++;
946 keytype = args[idx];
947 if (strcmp(keytype, "name") == 0) {
948 t->server_key_type = STKTABLE_SRV_NAME;
949 }
950 else if (strcmp(keytype, "addr") == 0) {
951 t->server_key_type = STKTABLE_SRV_ADDR;
952 }
953 else {
954 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
955 file, linenum, args[0], keytype);
956 err_code |= ERR_ALERT | ERR_FATAL;
957 goto out;
958
959 }
960 idx++;
961 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100962 else {
963 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
964 file, linenum, args[0], args[idx]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto out;
967 }
968 }
969
970 if (!t->size) {
971 ha_alert("parsing [%s:%d] : %s: missing size.\n",
972 file, linenum, args[0]);
973 err_code |= ERR_ALERT | ERR_FATAL;
974 goto out;
975 }
976
977 if (t->type == (unsigned int)-1) {
978 ha_alert("parsing [%s:%d] : %s: missing type.\n",
979 file, linenum, args[0]);
980 err_code |= ERR_ALERT | ERR_FATAL;
981 goto out;
982 }
983
984 out:
985 return err_code;
986}
987
Willy Tarreau8fed9032014-07-03 17:02:46 +0200988/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200989 * Note that the sample *is* modified and that the returned key may point
990 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200991 * Returns NULL if the sample could not be converted (eg: no matching type),
992 * otherwise a pointer to the static stktable_key filled with what is needed
993 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200994 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200995struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200996{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200997 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200998 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200999 return NULL;
1000
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001001 /* Fill static_table_key. */
1002 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +02001003
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001004 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +02001005 static_table_key.key = &smp->data.u.ipv4;
1006 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001007 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001008
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001009 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +02001010 static_table_key.key = &smp->data.u.ipv6;
1011 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001012 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001013
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001014 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001015 /* The stick table require a 32bit unsigned int, "sint" is a
1016 * signed 64 it, so we can convert it inplace.
1017 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001018 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001019 static_table_key.key = &smp->data.u.sint;
1020 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001021 break;
1022
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001023 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001024 if (!smp_make_safe(smp))
1025 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 static_table_key.key = smp->data.u.str.area;
1027 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001028 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001029
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001030 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001031 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001032 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001033 if (!smp_make_rw(smp))
1034 return NULL;
1035
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001036 if (smp->data.u.str.size < t->key_size)
1037 if (!smp_dup(smp))
1038 return NULL;
1039 if (smp->data.u.str.size < t->key_size)
1040 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001041 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1042 t->key_size - smp->data.u.str.data);
1043 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001044 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001045 static_table_key.key = smp->data.u.str.area;
1046 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001047 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001048
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001049 default: /* impossible case. */
1050 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001051 }
1052
Christopher Fauletca20d022017-08-29 15:30:31 +02001053 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001054}
1055
1056/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001057 * Process a fetch + format conversion as defined by the sample expression <expr>
1058 * on request or response considering the <opt> parameter. Returns either NULL if
1059 * no key could be extracted, or a pointer to the converted result stored in
1060 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1061 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001062 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1063 * without SMP_OPT_FINAL). The output will be usable like this :
1064 *
1065 * return MAY_CHANGE FINAL Meaning for the sample
1066 * NULL 0 * Not present and will never be (eg: header)
1067 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1068 * NULL 1 1 Not present, will not change anymore
1069 * smp 0 * Present and will not change (eg: header)
1070 * smp 1 0 not possible
1071 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001072 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001073struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001074 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1075{
1076 if (smp)
1077 memset(smp, 0, sizeof(*smp));
1078
Willy Tarreau192252e2015-04-04 01:47:55 +02001079 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001080 if (!smp)
1081 return NULL;
1082
1083 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1084 return NULL; /* we can only use stable samples */
1085
1086 return smp_to_stkey(smp, t);
1087}
1088
1089/*
Willy Tarreau12785782012-04-27 21:37:17 +02001090 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001091 * type <table_type>, otherwise zero. Used in configuration check.
1092 */
Willy Tarreau12785782012-04-27 21:37:17 +02001093int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001094{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001095 int out_type;
1096
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001097 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001098 return 0;
1099
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001100 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001101
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001102 /* Convert sample. */
1103 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001104 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001105
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001106 return 1;
1107}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001108
Willy Tarreauedee1d62014-07-15 16:44:27 +02001109/* Extra data types processing : after the last one, some room may remain
1110 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1111 * at run time.
1112 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001113struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001114 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001115 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001116 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001117 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001118 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1119 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1120 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
1121 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1122 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1123 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1124 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1125 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1126 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1127 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1128 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1129 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1130 [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 +01001131 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1132 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001133 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001134 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1135 [STKTABLE_DT_HTTP_FAIL_RATE]= { .name = "http_fail_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +02001136};
1137
Willy Tarreauedee1d62014-07-15 16:44:27 +02001138/* Registers stick-table extra data type with index <idx>, name <name>, type
1139 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1140 * index is automatically allocated. The allocated index is returned, or -1 if
1141 * no free index was found or <name> was already registered. The <name> is used
1142 * directly as a pointer, so if it's not stable, the caller must allocate it.
1143 */
1144int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1145{
1146 if (idx < 0) {
1147 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1148 if (!stktable_data_types[idx].name)
1149 break;
1150
1151 if (strcmp(stktable_data_types[idx].name, name) == 0)
1152 return -1;
1153 }
1154 }
1155
1156 if (idx >= STKTABLE_DATA_TYPES)
1157 return -1;
1158
1159 if (stktable_data_types[idx].name != NULL)
1160 return -1;
1161
1162 stktable_data_types[idx].name = name;
1163 stktable_data_types[idx].std_type = std_type;
1164 stktable_data_types[idx].arg_type = arg_type;
1165 return idx;
1166}
1167
Willy Tarreau08d5f982010-06-06 13:34:54 +02001168/*
1169 * Returns the data type number for the stktable_data_type whose name is <name>,
1170 * or <0 if not found.
1171 */
1172int stktable_get_data_type(char *name)
1173{
1174 int type;
1175
1176 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001177 if (!stktable_data_types[type].name)
1178 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001179 if (strcmp(name, stktable_data_types[type].name) == 0)
1180 return type;
1181 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001182 /* For backwards compatibility */
1183 if (strcmp(name, "server_name") == 0)
1184 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001185 return -1;
1186}
1187
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001188/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1189 * it up into this table. Returns true if found, false otherwise. The input
1190 * type is STR so that input samples are converted to string (since all types
1191 * can be converted to strings), then the function casts the string again into
1192 * the table's type. This is a double conversion, but in the future we might
1193 * support automatic input types to perform the cast on the fly.
1194 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001195static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001196{
1197 struct stktable *t;
1198 struct stktable_key *key;
1199 struct stksess *ts;
1200
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001201 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001202
1203 key = smp_to_stkey(smp, t);
1204 if (!key)
1205 return 0;
1206
1207 ts = stktable_lookup_key(t, key);
1208
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001209 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001210 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001211 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001212 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001213 return 1;
1214}
1215
1216/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1217 * it up into this table. Returns the data rate received from clients in bytes/s
1218 * if the key is present in the table, otherwise zero, so that comparisons can
1219 * be easily performed. If the inspected parameter is not stored in the table,
1220 * <not found> is returned.
1221 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001222static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001223{
1224 struct stktable *t;
1225 struct stktable_key *key;
1226 struct stksess *ts;
1227 void *ptr;
1228
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001229 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001230
1231 key = smp_to_stkey(smp, t);
1232 if (!key)
1233 return 0;
1234
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001235 ts = stktable_lookup_key(t, key);
1236
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001237 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001238 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001239 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001240
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001241 if (!ts) /* key not present */
1242 return 1;
1243
1244 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001245 if (ptr)
1246 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
1247 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001248
Daniel Corbett3e60b112018-05-27 09:47:12 -04001249 stktable_release(t, ts);
1250 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001251}
1252
1253/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1254 * it up into this table. Returns the cumulated number of connections for the key
1255 * if the key is present in the table, otherwise zero, so that comparisons can
1256 * be easily performed. If the inspected parameter is not stored in the table,
1257 * <not found> is returned.
1258 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001259static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001260{
1261 struct stktable *t;
1262 struct stktable_key *key;
1263 struct stksess *ts;
1264 void *ptr;
1265
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001266 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001267
1268 key = smp_to_stkey(smp, t);
1269 if (!key)
1270 return 0;
1271
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001272 ts = stktable_lookup_key(t, key);
1273
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001274 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001275 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001276 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001277
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001278 if (!ts) /* key not present */
1279 return 1;
1280
1281 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001282 if (ptr)
1283 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001284
Daniel Corbett3e60b112018-05-27 09:47:12 -04001285 stktable_release(t, ts);
1286 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001287}
1288
1289/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1290 * it up into this table. Returns the number of concurrent connections for the
1291 * key if the key is present in the table, otherwise zero, so that comparisons
1292 * can be easily performed. If the inspected parameter is not stored in the
1293 * table, <not found> is returned.
1294 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001295static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001296{
1297 struct stktable *t;
1298 struct stktable_key *key;
1299 struct stksess *ts;
1300 void *ptr;
1301
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001302 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001303
1304 key = smp_to_stkey(smp, t);
1305 if (!key)
1306 return 0;
1307
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001308 ts = stktable_lookup_key(t, key);
1309
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001310 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001311 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001312 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001313
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001314 if (!ts) /* key not present */
1315 return 1;
1316
1317 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001318 if (ptr)
1319 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001320
Daniel Corbett3e60b112018-05-27 09:47:12 -04001321 stktable_release(t, ts);
1322 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001323}
1324
1325/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1326 * it up into this table. Returns the rate of incoming connections from the key
1327 * if the key is present in the table, otherwise zero, so that comparisons can
1328 * be easily performed. If the inspected parameter is not stored in the table,
1329 * <not found> is returned.
1330 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001331static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001332{
1333 struct stktable *t;
1334 struct stktable_key *key;
1335 struct stksess *ts;
1336 void *ptr;
1337
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001338 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001339
1340 key = smp_to_stkey(smp, t);
1341 if (!key)
1342 return 0;
1343
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001344 ts = stktable_lookup_key(t, key);
1345
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001346 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001347 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001348 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001349
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001350 if (!ts) /* key not present */
1351 return 1;
1352
1353 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001354 if (ptr)
1355 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
1356 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001357
Daniel Corbett3e60b112018-05-27 09:47:12 -04001358 stktable_release(t, ts);
1359 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001360}
1361
1362/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1363 * it up into this table. Returns the data rate sent to clients in bytes/s
1364 * if the key is present in the table, otherwise zero, so that comparisons can
1365 * be easily performed. If the inspected parameter is not stored in the table,
1366 * <not found> is returned.
1367 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001368static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001369{
1370 struct stktable *t;
1371 struct stktable_key *key;
1372 struct stksess *ts;
1373 void *ptr;
1374
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001375 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001376
1377 key = smp_to_stkey(smp, t);
1378 if (!key)
1379 return 0;
1380
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001381 ts = stktable_lookup_key(t, key);
1382
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001383 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001385 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001386
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001387 if (!ts) /* key not present */
1388 return 1;
1389
1390 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001391 if (ptr)
1392 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
1393 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001394
Daniel Corbett3e60b112018-05-27 09:47:12 -04001395 stktable_release(t, ts);
1396 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001397}
1398
1399/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001400 * it up into this table. Returns the value of the GPT0 tag for the key
1401 * if the key is present in the table, otherwise false, so that comparisons can
1402 * be easily performed. If the inspected parameter is not stored in the table,
1403 * <not found> is returned.
1404 */
1405static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1406{
1407 struct stktable *t;
1408 struct stktable_key *key;
1409 struct stksess *ts;
1410 void *ptr;
1411
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001412 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001413
1414 key = smp_to_stkey(smp, t);
1415 if (!key)
1416 return 0;
1417
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001418 ts = stktable_lookup_key(t, key);
1419
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001420 smp->flags = SMP_F_VOL_TEST;
1421 smp->data.type = SMP_T_SINT;
1422 smp->data.u.sint = 0;
1423
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001424 if (!ts) /* key not present */
1425 return 1;
1426
1427 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001428 if (ptr)
1429 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001430
Daniel Corbett3e60b112018-05-27 09:47:12 -04001431 stktable_release(t, ts);
1432 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001433}
1434
1435/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001436 * it up into this table. Returns the value of the GPC0 counter for the key
1437 * if the key is present in the table, otherwise zero, so that comparisons can
1438 * be easily performed. If the inspected parameter is not stored in the table,
1439 * <not found> is returned.
1440 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001441static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001442{
1443 struct stktable *t;
1444 struct stktable_key *key;
1445 struct stksess *ts;
1446 void *ptr;
1447
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001448 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001449
1450 key = smp_to_stkey(smp, t);
1451 if (!key)
1452 return 0;
1453
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001454 ts = stktable_lookup_key(t, key);
1455
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001456 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001457 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001458 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001459
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001460 if (!ts) /* key not present */
1461 return 1;
1462
1463 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001464 if (ptr)
1465 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001466
Daniel Corbett3e60b112018-05-27 09:47:12 -04001467 stktable_release(t, ts);
1468 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001469}
1470
1471/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1472 * it up into this table. Returns the event rate of the GPC0 counter for the key
1473 * if the key is present in the table, otherwise zero, so that comparisons can
1474 * be easily performed. If the inspected parameter is not stored in the table,
1475 * <not found> is returned.
1476 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001477static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001478{
1479 struct stktable *t;
1480 struct stktable_key *key;
1481 struct stksess *ts;
1482 void *ptr;
1483
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001484 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001485
1486 key = smp_to_stkey(smp, t);
1487 if (!key)
1488 return 0;
1489
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001490 ts = stktable_lookup_key(t, key);
1491
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001492 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001493 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001494 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001495
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001496 if (!ts) /* key not present */
1497 return 1;
1498
1499 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001500 if (ptr)
1501 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1502 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001503
Daniel Corbett3e60b112018-05-27 09:47:12 -04001504 stktable_release(t, ts);
1505 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001506}
1507
1508/* 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 +01001509 * it up into this table. Returns the value of the GPC1 counter for the key
1510 * if the key is present in the table, otherwise zero, so that comparisons can
1511 * be easily performed. If the inspected parameter is not stored in the table,
1512 * <not found> is returned.
1513 */
1514static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1515{
1516 struct stktable *t;
1517 struct stktable_key *key;
1518 struct stksess *ts;
1519 void *ptr;
1520
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001521 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001522
1523 key = smp_to_stkey(smp, t);
1524 if (!key)
1525 return 0;
1526
1527 ts = stktable_lookup_key(t, key);
1528
1529 smp->flags = SMP_F_VOL_TEST;
1530 smp->data.type = SMP_T_SINT;
1531 smp->data.u.sint = 0;
1532
1533 if (!ts) /* key not present */
1534 return 1;
1535
1536 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001537 if (ptr)
1538 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001539
Daniel Corbett3e60b112018-05-27 09:47:12 -04001540 stktable_release(t, ts);
1541 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001542}
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 event rate of the GPC1 counter for the key
1546 * if the key is present in the table, otherwise zero, so that comparisons can
1547 * be easily performed. If the inspected parameter is not stored in the table,
1548 * <not found> is returned.
1549 */
1550static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1551{
1552 struct stktable *t;
1553 struct stktable_key *key;
1554 struct stksess *ts;
1555 void *ptr;
1556
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001557 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001558
1559 key = smp_to_stkey(smp, t);
1560 if (!key)
1561 return 0;
1562
1563 ts = stktable_lookup_key(t, key);
1564
1565 smp->flags = SMP_F_VOL_TEST;
1566 smp->data.type = SMP_T_SINT;
1567 smp->data.u.sint = 0;
1568
1569 if (!ts) /* key not present */
1570 return 1;
1571
1572 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001573 if (ptr)
1574 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
1575 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001576
Daniel Corbett3e60b112018-05-27 09:47:12 -04001577 stktable_release(t, ts);
1578 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001579}
1580
1581/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001582 * it up into this table. Returns the cumulated number of HTTP request errors
1583 * for the key if the key is present in the table, otherwise zero, so that
1584 * comparisons can be easily performed. If the inspected parameter is not stored
1585 * in the table, <not found> is returned.
1586 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001587static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001588{
1589 struct stktable *t;
1590 struct stktable_key *key;
1591 struct stksess *ts;
1592 void *ptr;
1593
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001594 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001595
1596 key = smp_to_stkey(smp, t);
1597 if (!key)
1598 return 0;
1599
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001600 ts = stktable_lookup_key(t, key);
1601
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001602 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001603 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001604 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001605
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001606 if (!ts) /* key not present */
1607 return 1;
1608
1609 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001610 if (ptr)
1611 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001612
Daniel Corbett3e60b112018-05-27 09:47:12 -04001613 stktable_release(t, ts);
1614 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001615}
1616
1617/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1618 * it up into this table. Returns the HTTP request error rate the key
1619 * if the key is present in the table, otherwise zero, so that comparisons can
1620 * be easily performed. If the inspected parameter is not stored in the table,
1621 * <not found> is returned.
1622 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001623static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001624{
1625 struct stktable *t;
1626 struct stktable_key *key;
1627 struct stksess *ts;
1628 void *ptr;
1629
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001630 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001631
1632 key = smp_to_stkey(smp, t);
1633 if (!key)
1634 return 0;
1635
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001636 ts = stktable_lookup_key(t, key);
1637
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001638 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001639 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001640 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001641
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001642 if (!ts) /* key not present */
1643 return 1;
1644
1645 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001646 if (ptr)
1647 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
1648 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001649
Daniel Corbett3e60b112018-05-27 09:47:12 -04001650 stktable_release(t, ts);
1651 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001652}
1653
1654/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001655 * it up into this table. Returns the cumulated number of HTTP response failures
1656 * for the key if the key is present in the table, otherwise zero, so that
1657 * comparisons can be easily performed. If the inspected parameter is not stored
1658 * in the table, <not found> is returned.
1659 */
1660static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
1661{
1662 struct stktable *t;
1663 struct stktable_key *key;
1664 struct stksess *ts;
1665 void *ptr;
1666
1667 t = arg_p[0].data.t;
1668
1669 key = smp_to_stkey(smp, t);
1670 if (!key)
1671 return 0;
1672
1673 ts = stktable_lookup_key(t, key);
1674
1675 smp->flags = SMP_F_VOL_TEST;
1676 smp->data.type = SMP_T_SINT;
1677 smp->data.u.sint = 0;
1678
1679 if (!ts) /* key not present */
1680 return 1;
1681
1682 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
1683 if (ptr)
1684 smp->data.u.sint = stktable_data_cast(ptr, http_fail_cnt);
1685
1686 stktable_release(t, ts);
1687 return !!ptr;
1688}
1689
1690/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1691 * it up into this table. Returns the HTTP response failure rate for the key
1692 * if the key is present in the table, otherwise zero, so that comparisons can
1693 * be easily performed. If the inspected parameter is not stored in the table,
1694 * <not found> is returned.
1695 */
1696static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
1697{
1698 struct stktable *t;
1699 struct stktable_key *key;
1700 struct stksess *ts;
1701 void *ptr;
1702
1703 t = arg_p[0].data.t;
1704
1705 key = smp_to_stkey(smp, t);
1706 if (!key)
1707 return 0;
1708
1709 ts = stktable_lookup_key(t, key);
1710
1711 smp->flags = SMP_F_VOL_TEST;
1712 smp->data.type = SMP_T_SINT;
1713 smp->data.u.sint = 0;
1714
1715 if (!ts) /* key not present */
1716 return 1;
1717
1718 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
1719 if (ptr)
1720 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_fail_rate),
1721 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
1722
1723 stktable_release(t, ts);
1724 return !!ptr;
1725}
1726
1727/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001728 * it up into this table. Returns the cumulated number of HTTP request for the
1729 * key if the key is present in the table, otherwise zero, so that comparisons
1730 * can be easily performed. If the inspected parameter is not stored in the
1731 * table, <not found> is returned.
1732 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001733static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001734{
1735 struct stktable *t;
1736 struct stktable_key *key;
1737 struct stksess *ts;
1738 void *ptr;
1739
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001740 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001741
1742 key = smp_to_stkey(smp, t);
1743 if (!key)
1744 return 0;
1745
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001746 ts = stktable_lookup_key(t, key);
1747
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001748 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001749 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001750 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001751
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001752 if (!ts) /* key not present */
1753 return 1;
1754
1755 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001756 if (ptr)
1757 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001758
Daniel Corbett3e60b112018-05-27 09:47:12 -04001759 stktable_release(t, ts);
1760 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001761}
1762
1763/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1764 * it up into this table. Returns the HTTP request rate the key if the key is
1765 * present in the table, otherwise zero, so that comparisons can be easily
1766 * performed. If the inspected parameter is not stored in the table, <not found>
1767 * is returned.
1768 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001769static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001770{
1771 struct stktable *t;
1772 struct stktable_key *key;
1773 struct stksess *ts;
1774 void *ptr;
1775
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001776 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001777
1778 key = smp_to_stkey(smp, t);
1779 if (!key)
1780 return 0;
1781
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001782 ts = stktable_lookup_key(t, key);
1783
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001784 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001785 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001786 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001787
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001788 if (!ts) /* key not present */
1789 return 1;
1790
1791 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001792 if (ptr)
1793 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
1794 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001795
Daniel Corbett3e60b112018-05-27 09:47:12 -04001796 stktable_release(t, ts);
1797 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001798}
1799
1800/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1801 * it up into this table. Returns the volume of datareceived from clients in kbytes
1802 * if the key is present in the table, otherwise zero, so that comparisons can
1803 * be easily performed. If the inspected parameter is not stored in the table,
1804 * <not found> is returned.
1805 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001806static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001807{
1808 struct stktable *t;
1809 struct stktable_key *key;
1810 struct stksess *ts;
1811 void *ptr;
1812
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001813 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001814
1815 key = smp_to_stkey(smp, t);
1816 if (!key)
1817 return 0;
1818
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001819 ts = stktable_lookup_key(t, key);
1820
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001821 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001822 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001823 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001824
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001825 if (!ts) /* key not present */
1826 return 1;
1827
1828 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001829 if (ptr)
1830 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001831
Daniel Corbett3e60b112018-05-27 09:47:12 -04001832 stktable_release(t, ts);
1833 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001834}
1835
1836/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1837 * it up into this table. Returns the volume of data sent to clients in kbytes
1838 * if the key is present in the table, otherwise zero, so that comparisons can
1839 * be easily performed. If the inspected parameter is not stored in the table,
1840 * <not found> is returned.
1841 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001842static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001843{
1844 struct stktable *t;
1845 struct stktable_key *key;
1846 struct stksess *ts;
1847 void *ptr;
1848
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001849 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001850
1851 key = smp_to_stkey(smp, t);
1852 if (!key)
1853 return 0;
1854
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001855 ts = stktable_lookup_key(t, key);
1856
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001857 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001858 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001859 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001860
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001861 if (!ts) /* key not present */
1862 return 1;
1863
1864 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001865 if (ptr)
1866 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001867
Daniel Corbett3e60b112018-05-27 09:47:12 -04001868 stktable_release(t, ts);
1869 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001870}
1871
1872/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1873 * it up into this table. Returns the server ID associated with the key if the
1874 * key is present in the table, otherwise zero, so that comparisons can be
1875 * easily performed. If the inspected parameter is not stored in the table,
1876 * <not found> is returned.
1877 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001878static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001879{
1880 struct stktable *t;
1881 struct stktable_key *key;
1882 struct stksess *ts;
1883 void *ptr;
1884
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001885 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001886
1887 key = smp_to_stkey(smp, t);
1888 if (!key)
1889 return 0;
1890
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001891 ts = stktable_lookup_key(t, key);
1892
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001893 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001894 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001895 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001896
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001897 if (!ts) /* key not present */
1898 return 1;
1899
1900 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001901 if (ptr)
1902 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001903
Daniel Corbett3e60b112018-05-27 09:47:12 -04001904 stktable_release(t, ts);
1905 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001906}
1907
1908/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1909 * it up into this table. Returns the cumulated number of sessions for the
1910 * key if the key is present in the table, otherwise zero, so that comparisons
1911 * can be easily performed. If the inspected parameter is not stored in the
1912 * table, <not found> is returned.
1913 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001914static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001915{
1916 struct stktable *t;
1917 struct stktable_key *key;
1918 struct stksess *ts;
1919 void *ptr;
1920
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001921 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001922
1923 key = smp_to_stkey(smp, t);
1924 if (!key)
1925 return 0;
1926
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001927 ts = stktable_lookup_key(t, key);
1928
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001929 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001930 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001931 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001932
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001933 if (!ts) /* key not present */
1934 return 1;
1935
1936 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001937 if (ptr)
1938 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001939
Daniel Corbett3e60b112018-05-27 09:47:12 -04001940 stktable_release(t, ts);
1941 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001942}
1943
1944/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1945 * it up into this table. Returns the session rate the key if the key is
1946 * present in the table, otherwise zero, so that comparisons can be easily
1947 * performed. If the inspected parameter is not stored in the table, <not found>
1948 * is returned.
1949 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001950static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001951{
1952 struct stktable *t;
1953 struct stktable_key *key;
1954 struct stksess *ts;
1955 void *ptr;
1956
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001957 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001958
1959 key = smp_to_stkey(smp, t);
1960 if (!key)
1961 return 0;
1962
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001963 ts = stktable_lookup_key(t, key);
1964
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001965 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001966 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001967 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001968
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001969 if (!ts) /* key not present */
1970 return 1;
1971
1972 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001973 if (ptr)
1974 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
1975 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001976
Daniel Corbett3e60b112018-05-27 09:47:12 -04001977 stktable_release(t, ts);
1978 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001979}
1980
1981/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1982 * it up into this table. Returns the amount of concurrent connections tracking
1983 * the same key if the key is present in the table, otherwise zero, so that
1984 * comparisons can be easily performed. If the inspected parameter is not
1985 * stored in the table, <not found> is returned.
1986 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001987static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001988{
1989 struct stktable *t;
1990 struct stktable_key *key;
1991 struct stksess *ts;
1992
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001993 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001994
1995 key = smp_to_stkey(smp, t);
1996 if (!key)
1997 return 0;
1998
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001999 ts = stktable_lookup_key(t, key);
2000
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002001 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002002 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002003 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002004
Tim Duesterhus65189c12018-06-26 15:57:29 +02002005 if (!ts)
2006 return 1;
2007
2008 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002009
Daniel Corbett3e60b112018-05-27 09:47:12 -04002010 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002011 return 1;
2012}
2013
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002014/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002015static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002016 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002017{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002018 struct stksess *ts;
2019 struct stkctr *stkctr;
2020
2021 /* Extract the stksess, return OK if no stksess available. */
2022 if (s)
2023 stkctr = &s->stkctr[rule->arg.gpc.sc];
2024 else
2025 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002026
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002027 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002028 if (ts) {
2029 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002030
Willy Tarreau79c1e912016-01-25 14:54:45 +01002031 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2032 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002033 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
2034 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002035 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002036
2037 if (ptr1)
2038 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau79c1e912016-01-25 14:54:45 +01002039 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002040
Emeric Brun819fc6f2017-06-13 19:37:32 +02002041 if (ptr2)
2042 stktable_data_cast(ptr2, gpc0)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002043
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002044 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002045
2046 /* If data was modified, we need to touch to re-schedule sync */
2047 stktable_touch_local(stkctr->table, ts, 0);
2048 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002049 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002050 return ACT_RET_CONT;
2051}
2052
2053/* This function is a common parser for using variables. It understands
2054 * the formats:
2055 *
2056 * sc-inc-gpc0(<stick-table ID>)
2057 *
2058 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2059 * it returns 1 and the variable <expr> is filled with the pointer to the
2060 * expression to execute.
2061 */
2062static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
2063 struct act_rule *rule, char **err)
2064{
2065 const char *cmd_name = args[*arg-1];
2066 char *error;
2067
2068 cmd_name += strlen("sc-inc-gpc0");
2069 if (*cmd_name == '\0') {
2070 /* default stick table id. */
2071 rule->arg.gpc.sc = 0;
2072 } else {
2073 /* parse the stick table id. */
2074 if (*cmd_name != '(') {
2075 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2076 return ACT_RET_PRS_ERR;
2077 }
2078 cmd_name++; /* jump the '(' */
2079 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2080 if (*error != ')') {
2081 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2082 return ACT_RET_PRS_ERR;
2083 }
2084
Christopher Faulet28436e22019-12-18 10:25:46 +01002085 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002086 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002087 MAX_SESS_STKCTR-1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002088 return ACT_RET_PRS_ERR;
2089 }
2090 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02002091 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002092 rule->action_ptr = action_inc_gpc0;
2093 return ACT_RET_PRS_OK;
2094}
2095
2096/* Always returns 1. */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002097static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2098 struct session *sess, struct stream *s, int flags)
2099{
2100 struct stksess *ts;
2101 struct stkctr *stkctr;
2102
2103 /* Extract the stksess, return OK if no stksess available. */
2104 if (s)
2105 stkctr = &s->stkctr[rule->arg.gpc.sc];
2106 else
2107 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2108
2109 ts = stkctr_entry(stkctr);
2110 if (ts) {
2111 void *ptr1, *ptr2;
2112
2113 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2114 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
2115 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
2116 if (ptr1 || ptr2) {
2117 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2118
2119 if (ptr1)
2120 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
2121 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
2122
2123 if (ptr2)
2124 stktable_data_cast(ptr2, gpc1)++;
2125
2126 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2127
2128 /* If data was modified, we need to touch to re-schedule sync */
2129 stktable_touch_local(stkctr->table, ts, 0);
2130 }
2131 }
2132 return ACT_RET_CONT;
2133}
2134
2135/* This function is a common parser for using variables. It understands
2136 * the formats:
2137 *
2138 * sc-inc-gpc1(<stick-table ID>)
2139 *
2140 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2141 * it returns 1 and the variable <expr> is filled with the pointer to the
2142 * expression to execute.
2143 */
2144static enum act_parse_ret parse_inc_gpc1(const char **args, int *arg, struct proxy *px,
2145 struct act_rule *rule, char **err)
2146{
2147 const char *cmd_name = args[*arg-1];
2148 char *error;
2149
2150 cmd_name += strlen("sc-inc-gpc1");
2151 if (*cmd_name == '\0') {
2152 /* default stick table id. */
2153 rule->arg.gpc.sc = 0;
2154 } else {
2155 /* parse the stick table id. */
2156 if (*cmd_name != '(') {
2157 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2158 return ACT_RET_PRS_ERR;
2159 }
2160 cmd_name++; /* jump the '(' */
2161 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2162 if (*error != ')') {
2163 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2164 return ACT_RET_PRS_ERR;
2165 }
2166
Christopher Faulet28436e22019-12-18 10:25:46 +01002167 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002168 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002169 MAX_SESS_STKCTR-1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002170 return ACT_RET_PRS_ERR;
2171 }
2172 }
2173 rule->action = ACT_CUSTOM;
2174 rule->action_ptr = action_inc_gpc1;
2175 return ACT_RET_PRS_OK;
2176}
2177
2178/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002179static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002180 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002181{
2182 void *ptr;
2183 struct stksess *ts;
2184 struct stkctr *stkctr;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002185 unsigned int value = 0;
2186 struct sample *smp;
2187 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002188
2189 /* Extract the stksess, return OK if no stksess available. */
2190 if (s)
2191 stkctr = &s->stkctr[rule->arg.gpt.sc];
2192 else
2193 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002194
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002195 ts = stkctr_entry(stkctr);
2196 if (!ts)
2197 return ACT_RET_CONT;
2198
2199 /* Store the sample in the required sc, and ignore errors. */
2200 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002201 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002202 if (!rule->arg.gpt.expr)
2203 value = (unsigned int)(rule->arg.gpt.value);
2204 else {
2205 switch (rule->from) {
2206 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2207 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2208 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2209 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2210 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2211 default:
2212 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2213 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2214 ha_alert("stick table: internal error while executing setting gpt0.\n");
2215 return ACT_RET_CONT;
2216 }
2217
2218 /* Fetch and cast the expression. */
2219 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2220 if (!smp) {
2221 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2222 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2223 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2224 return ACT_RET_CONT;
2225 }
2226 value = (unsigned int)(smp->data.u.sint);
2227 }
2228
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002229 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002230
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002231 stktable_data_cast(ptr, gpt0) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002232
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002233 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002234
2235 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002236 }
2237
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002238 return ACT_RET_CONT;
2239}
2240
2241/* This function is a common parser for using variables. It understands
2242 * the format:
2243 *
2244 * set-gpt0(<stick-table ID>) <expression>
2245 *
2246 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2247 * it returns 1 and the variable <expr> is filled with the pointer to the
2248 * expression to execute.
2249 */
2250static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
2251 struct act_rule *rule, char **err)
2252
2253
2254{
2255 const char *cmd_name = args[*arg-1];
2256 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002257 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002258
2259 cmd_name += strlen("sc-set-gpt0");
2260 if (*cmd_name == '\0') {
2261 /* default stick table id. */
2262 rule->arg.gpt.sc = 0;
2263 } else {
2264 /* parse the stick table id. */
2265 if (*cmd_name != '(') {
2266 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2267 return ACT_RET_PRS_ERR;
2268 }
2269 cmd_name++; /* jump the '(' */
2270 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2271 if (*error != ')') {
2272 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2273 return ACT_RET_PRS_ERR;
2274 }
2275
Christopher Faulet28436e22019-12-18 10:25:46 +01002276 if (rule->arg.gpt.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002277 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002278 args[*arg-1], MAX_SESS_STKCTR-1);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002279 return ACT_RET_PRS_ERR;
2280 }
2281 }
2282
Willy Tarreauf1e1c912021-08-24 14:57:28 +02002283 /* value may be either an integer or an expression */
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002284 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002285 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
Willy Tarreauf1e1c912021-08-24 14:57:28 +02002286 if (*error == '\0') {
2287 /* valid integer, skip it */
2288 (*arg)++;
2289 } else {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002290 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002291 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002292 if (!rule->arg.gpt.expr)
2293 return ACT_RET_PRS_ERR;
2294
2295 switch (rule->from) {
2296 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2297 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2298 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2299 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2300 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2301 default:
2302 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2303 return ACT_RET_PRS_ERR;
2304 }
2305 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2306 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2307 sample_src_names(rule->arg.gpt.expr->fetch->use));
2308 free(rule->arg.gpt.expr);
2309 return ACT_RET_PRS_ERR;
2310 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002311 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002312
Thierry FOURNIER42148732015-09-02 17:17:33 +02002313 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002314 rule->action_ptr = action_set_gpt0;
2315
2316 return ACT_RET_PRS_OK;
2317}
2318
Willy Tarreau7d562212016-11-25 16:10:05 +01002319/* set temp integer to the number of used entries in the table pointed to by expr.
2320 * Accepts exactly 1 argument of type table.
2321 */
2322static int
2323smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2324{
2325 smp->flags = SMP_F_VOL_TEST;
2326 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002327 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002328 return 1;
2329}
2330
2331/* set temp integer to the number of free entries in the table pointed to by expr.
2332 * Accepts exactly 1 argument of type table.
2333 */
2334static int
2335smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
2336{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002337 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002338
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002339 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002340 smp->flags = SMP_F_VOL_TEST;
2341 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002342 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002343 return 1;
2344}
2345
2346/* Returns a pointer to a stkctr depending on the fetch keyword name.
2347 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
2348 * sc[0-9]_* will return a pointer to the respective field in the
2349 * stream <l4>. sc_* requires an UINT argument specifying the stick
2350 * counter number. src_* will fill a locally allocated structure with
2351 * the table and entry corresponding to what is specified with src_*.
2352 * NULL may be returned if the designated stkctr is not tracked. For
2353 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2354 * passed. When present, the currently tracked key is then looked up
2355 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002356 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01002357 * multiple tables). <strm> is allowed to be NULL, in which case only
2358 * the session will be consulted.
2359 */
2360struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002361smp_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 +01002362{
Willy Tarreau7d562212016-11-25 16:10:05 +01002363 struct stkctr *stkptr;
2364 struct stksess *stksess;
2365 unsigned int num = kw[2] - '0';
2366 int arg = 0;
2367
2368 if (num == '_' - '0') {
2369 /* sc_* variant, args[0] = ctr# (mandatory) */
2370 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01002371 }
2372 else if (num > 9) { /* src_* variant, args[0] = table */
2373 struct stktable_key *key;
2374 struct connection *conn = objt_conn(sess->origin);
2375 struct sample smp;
2376
2377 if (!conn)
2378 return NULL;
2379
Joseph Herlant5662fa42018-11-15 13:43:28 -08002380 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002381 smp.px = NULL;
2382 smp.sess = sess;
2383 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02002384 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002385 return NULL;
2386
2387 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002388 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002389 if (!key)
2390 return NULL;
2391
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002392 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002393 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
2394 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002395 }
2396
2397 /* Here, <num> contains the counter number from 0 to 9 for
2398 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2399 * args[arg] is the first optional argument. We first lookup the
2400 * ctr form the stream, then from the session if it was not there.
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02002401 * But we must be sure the counter does not exceed MAX_SESS_STKCTR.
Willy Tarreau7d562212016-11-25 16:10:05 +01002402 */
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02002403 if (num >= MAX_SESS_STKCTR)
2404 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01002405
2406 if (strm)
2407 stkptr = &strm->stkctr[num];
2408 if (!strm || !stkctr_entry(stkptr)) {
2409 stkptr = &sess->stkctr[num];
2410 if (!stkctr_entry(stkptr))
2411 return NULL;
2412 }
2413
2414 stksess = stkctr_entry(stkptr);
2415 if (!stksess)
2416 return NULL;
2417
2418 if (unlikely(args[arg].type == ARGT_TAB)) {
2419 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002420 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002421 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
2422 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002423 }
2424 return stkptr;
2425}
2426
2427/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2428 * the entry if it doesn't exist yet. This is needed for a few fetch
2429 * functions which need to create an entry, such as src_inc_gpc* and
2430 * src_clr_gpc*.
2431 */
2432struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002433smp_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 +01002434{
Willy Tarreau7d562212016-11-25 16:10:05 +01002435 struct stktable_key *key;
2436 struct connection *conn = objt_conn(sess->origin);
2437 struct sample smp;
2438
2439 if (strncmp(kw, "src_", 4) != 0)
2440 return NULL;
2441
2442 if (!conn)
2443 return NULL;
2444
Joseph Herlant5662fa42018-11-15 13:43:28 -08002445 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002446 smp.px = NULL;
2447 smp.sess = sess;
2448 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02002449 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002450 return NULL;
2451
2452 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002453 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002454 if (!key)
2455 return NULL;
2456
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002457 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002458 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
2459 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002460}
2461
2462/* set return a boolean indicating if the requested stream counter is
2463 * currently being tracked or not.
2464 * Supports being called as "sc[0-9]_tracked" only.
2465 */
2466static int
2467smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2468{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002469 struct stkctr tmpstkctr;
2470 struct stkctr *stkctr;
2471
Willy Tarreau7d562212016-11-25 16:10:05 +01002472 smp->flags = SMP_F_VOL_TEST;
2473 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002474 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2475 smp->data.u.sint = !!stkctr;
2476
2477 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02002478 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02002479 stktable_release(stkctr->table, stkctr_entry(stkctr));
2480
Willy Tarreau7d562212016-11-25 16:10:05 +01002481 return 1;
2482}
2483
2484/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2485 * frontend counters or from the src.
2486 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2487 * zero is returned if the key is new.
2488 */
2489static int
2490smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2491{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002492 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002493 struct stkctr *stkctr;
2494
Emeric Brun819fc6f2017-06-13 19:37:32 +02002495 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002496 if (!stkctr)
2497 return 0;
2498
2499 smp->flags = SMP_F_VOL_TEST;
2500 smp->data.type = SMP_T_SINT;
2501 smp->data.u.sint = 0;
2502
Emeric Brun819fc6f2017-06-13 19:37:32 +02002503 if (stkctr_entry(stkctr)) {
2504 void *ptr;
2505
2506 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2507 if (!ptr) {
2508 if (stkctr == &tmpstkctr)
2509 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002510 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002511 }
2512
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002513 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002514
Willy Tarreau7d562212016-11-25 16:10:05 +01002515 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002516
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002517 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002518
2519 if (stkctr == &tmpstkctr)
2520 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002521 }
2522 return 1;
2523}
2524
2525/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
2526 * frontend counters or from the src.
2527 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2528 * zero is returned if the key is new.
2529 */
2530static int
2531smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2532{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002533 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002534 struct stkctr *stkctr;
2535
Emeric Brun819fc6f2017-06-13 19:37:32 +02002536 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002537 if (!stkctr)
2538 return 0;
2539
2540 smp->flags = SMP_F_VOL_TEST;
2541 smp->data.type = SMP_T_SINT;
2542 smp->data.u.sint = 0;
2543
2544 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002545 void *ptr;
2546
2547 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2548 if (!ptr) {
2549 if (stkctr == &tmpstkctr)
2550 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002551 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002552 }
2553
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002554 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002555
Willy Tarreau7d562212016-11-25 16:10:05 +01002556 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002557
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002558 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002559
2560 if (stkctr == &tmpstkctr)
2561 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002562 }
2563 return 1;
2564}
2565
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002566/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
2567 * frontend counters or from the src.
2568 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
2569 * zero is returned if the key is new.
2570 */
2571static int
2572smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2573{
2574 struct stkctr tmpstkctr;
2575 struct stkctr *stkctr;
2576
2577 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2578 if (!stkctr)
2579 return 0;
2580
2581 smp->flags = SMP_F_VOL_TEST;
2582 smp->data.type = SMP_T_SINT;
2583 smp->data.u.sint = 0;
2584
2585 if (stkctr_entry(stkctr) != NULL) {
2586 void *ptr;
2587
2588 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2589 if (!ptr) {
2590 if (stkctr == &tmpstkctr)
2591 stktable_release(stkctr->table, stkctr_entry(stkctr));
2592 return 0; /* parameter not stored */
2593 }
2594
2595 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2596
2597 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2598
2599 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2600
2601 if (stkctr == &tmpstkctr)
2602 stktable_release(stkctr->table, stkctr_entry(stkctr));
2603 }
2604 return 1;
2605}
2606
Willy Tarreau7d562212016-11-25 16:10:05 +01002607/* set <smp> to the General Purpose Counter 0's event rate from the stream's
2608 * tracked frontend counters or from the src.
2609 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2610 * Value zero is returned if the key is new.
2611 */
2612static int
2613smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2614{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002615 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002616 struct stkctr *stkctr;
2617
Emeric Brun819fc6f2017-06-13 19:37:32 +02002618 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002619 if (!stkctr)
2620 return 0;
2621
2622 smp->flags = SMP_F_VOL_TEST;
2623 smp->data.type = SMP_T_SINT;
2624 smp->data.u.sint = 0;
2625 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002626 void *ptr;
2627
2628 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2629 if (!ptr) {
2630 if (stkctr == &tmpstkctr)
2631 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002632 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002633 }
2634
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002635 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002636
Willy Tarreau7d562212016-11-25 16:10:05 +01002637 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
2638 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002639
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002640 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002641
2642 if (stkctr == &tmpstkctr)
2643 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002644 }
2645 return 1;
2646}
2647
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002648/* set <smp> to the General Purpose Counter 1's event rate from the stream's
2649 * tracked frontend counters or from the src.
2650 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
2651 * Value zero is returned if the key is new.
2652 */
2653static int
2654smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2655{
2656 struct stkctr tmpstkctr;
2657 struct stkctr *stkctr;
2658
2659 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2660 if (!stkctr)
2661 return 0;
2662
2663 smp->flags = SMP_F_VOL_TEST;
2664 smp->data.type = SMP_T_SINT;
2665 smp->data.u.sint = 0;
2666 if (stkctr_entry(stkctr) != NULL) {
2667 void *ptr;
2668
2669 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2670 if (!ptr) {
2671 if (stkctr == &tmpstkctr)
2672 stktable_release(stkctr->table, stkctr_entry(stkctr));
2673 return 0; /* parameter not stored */
2674 }
2675
2676 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2677
2678 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
2679 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u);
2680
2681 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2682
2683 if (stkctr == &tmpstkctr)
2684 stktable_release(stkctr->table, stkctr_entry(stkctr));
2685 }
2686 return 1;
2687}
2688
Willy Tarreau7d562212016-11-25 16:10:05 +01002689/* Increment the General Purpose Counter 0 value from the stream's tracked
2690 * frontend counters and return it into temp integer.
2691 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
2692 */
2693static int
2694smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2695{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002696 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002697 struct stkctr *stkctr;
2698
Emeric Brun819fc6f2017-06-13 19:37:32 +02002699 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002700 if (!stkctr)
2701 return 0;
2702
2703 smp->flags = SMP_F_VOL_TEST;
2704 smp->data.type = SMP_T_SINT;
2705 smp->data.u.sint = 0;
2706
Emeric Brun819fc6f2017-06-13 19:37:32 +02002707 if (!stkctr_entry(stkctr))
2708 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002709
2710 if (stkctr && stkctr_entry(stkctr)) {
2711 void *ptr1,*ptr2;
2712
Emeric Brun819fc6f2017-06-13 19:37:32 +02002713
Willy Tarreau7d562212016-11-25 16:10:05 +01002714 /* First, update gpc0_rate if it's tracked. Second, update its
2715 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2716 */
2717 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreau7d562212016-11-25 16:10:05 +01002718 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002719 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002720 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01002721
Emeric Brun819fc6f2017-06-13 19:37:32 +02002722 if (ptr1) {
2723 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
2724 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
2725 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
2726 }
2727
2728 if (ptr2)
2729 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
2730
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002731 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002732
2733 /* If data was modified, we need to touch to re-schedule sync */
2734 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2735 }
2736 else if (stkctr == &tmpstkctr)
2737 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002738 }
2739 return 1;
2740}
2741
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002742/* Increment the General Purpose Counter 1 value from the stream's tracked
2743 * frontend counters and return it into temp integer.
2744 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
2745 */
2746static int
2747smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2748{
2749 struct stkctr tmpstkctr;
2750 struct stkctr *stkctr;
2751
2752 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2753 if (!stkctr)
2754 return 0;
2755
2756 smp->flags = SMP_F_VOL_TEST;
2757 smp->data.type = SMP_T_SINT;
2758 smp->data.u.sint = 0;
2759
2760 if (!stkctr_entry(stkctr))
2761 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2762
2763 if (stkctr && stkctr_entry(stkctr)) {
2764 void *ptr1,*ptr2;
2765
2766
2767 /* First, update gpc1_rate if it's tracked. Second, update its
2768 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
2769 */
2770 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2771 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2772 if (ptr1 || ptr2) {
2773 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2774
2775 if (ptr1) {
2776 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
2777 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
2778 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc1_rate))->curr_ctr;
2779 }
2780
2781 if (ptr2)
2782 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc1);
2783
2784 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2785
2786 /* If data was modified, we need to touch to re-schedule sync */
2787 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2788 }
2789 else if (stkctr == &tmpstkctr)
2790 stktable_release(stkctr->table, stkctr_entry(stkctr));
2791 }
2792 return 1;
2793}
2794
Willy Tarreau7d562212016-11-25 16:10:05 +01002795/* Clear the General Purpose Counter 0 value from the stream's tracked
2796 * frontend counters and return its previous value into temp integer.
2797 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
2798 */
2799static int
2800smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2801{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002802 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002803 struct stkctr *stkctr;
2804
Emeric Brun819fc6f2017-06-13 19:37:32 +02002805 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002806 if (!stkctr)
2807 return 0;
2808
2809 smp->flags = SMP_F_VOL_TEST;
2810 smp->data.type = SMP_T_SINT;
2811 smp->data.u.sint = 0;
2812
Emeric Brun819fc6f2017-06-13 19:37:32 +02002813 if (!stkctr_entry(stkctr))
2814 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002815
Emeric Brun819fc6f2017-06-13 19:37:32 +02002816 if (stkctr && stkctr_entry(stkctr)) {
2817 void *ptr;
2818
2819 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2820 if (!ptr) {
2821 if (stkctr == &tmpstkctr)
2822 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002823 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002824 }
2825
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002826 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002827
Willy Tarreau7d562212016-11-25 16:10:05 +01002828 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
2829 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002830
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002831 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002832
Willy Tarreau7d562212016-11-25 16:10:05 +01002833 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002834 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01002835 }
2836 return 1;
2837}
2838
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002839/* Clear the General Purpose Counter 1 value from the stream's tracked
2840 * frontend counters and return its previous value into temp integer.
2841 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
2842 */
2843static int
2844smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2845{
2846 struct stkctr tmpstkctr;
2847 struct stkctr *stkctr;
2848
2849 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2850 if (!stkctr)
2851 return 0;
2852
2853 smp->flags = SMP_F_VOL_TEST;
2854 smp->data.type = SMP_T_SINT;
2855 smp->data.u.sint = 0;
2856
2857 if (!stkctr_entry(stkctr))
2858 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2859
2860 if (stkctr && stkctr_entry(stkctr)) {
2861 void *ptr;
2862
2863 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2864 if (!ptr) {
2865 if (stkctr == &tmpstkctr)
2866 stktable_release(stkctr->table, stkctr_entry(stkctr));
2867 return 0; /* parameter not stored */
2868 }
2869
2870 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2871
2872 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2873 stktable_data_cast(ptr, gpc1) = 0;
2874
2875 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2876
2877 /* If data was modified, we need to touch to re-schedule sync */
2878 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2879 }
2880 return 1;
2881}
2882
Willy Tarreau7d562212016-11-25 16:10:05 +01002883/* set <smp> to the cumulated number of connections from the stream's tracked
2884 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2885 * "src_conn_cnt" only.
2886 */
2887static int
2888smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2889{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002890 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002891 struct stkctr *stkctr;
2892
Emeric Brun819fc6f2017-06-13 19:37:32 +02002893 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002894 if (!stkctr)
2895 return 0;
2896
2897 smp->flags = SMP_F_VOL_TEST;
2898 smp->data.type = SMP_T_SINT;
2899 smp->data.u.sint = 0;
2900 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002901 void *ptr;
2902
2903 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
2904 if (!ptr) {
2905 if (stkctr == &tmpstkctr)
2906 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002907 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002908 }
2909
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002910 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002911
Willy Tarreau7d562212016-11-25 16:10:05 +01002912 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002913
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002914 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002915
2916 if (stkctr == &tmpstkctr)
2917 stktable_release(stkctr->table, stkctr_entry(stkctr));
2918
2919
Willy Tarreau7d562212016-11-25 16:10:05 +01002920 }
2921 return 1;
2922}
2923
2924/* set <smp> to the connection rate from the stream's tracked frontend
2925 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2926 * only.
2927 */
2928static int
2929smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2930{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002931 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002932 struct stkctr *stkctr;
2933
Emeric Brun819fc6f2017-06-13 19:37:32 +02002934 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002935 if (!stkctr)
2936 return 0;
2937
2938 smp->flags = SMP_F_VOL_TEST;
2939 smp->data.type = SMP_T_SINT;
2940 smp->data.u.sint = 0;
2941 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002942 void *ptr;
2943
2944 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
2945 if (!ptr) {
2946 if (stkctr == &tmpstkctr)
2947 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002948 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002949 }
2950
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002951 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002952
Willy Tarreau7d562212016-11-25 16:10:05 +01002953 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2954 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002955
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002956 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002957
2958 if (stkctr == &tmpstkctr)
2959 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002960 }
2961 return 1;
2962}
2963
2964/* set temp integer to the number of connections from the stream's source address
2965 * in the table pointed to by expr, after updating it.
2966 * Accepts exactly 1 argument of type table.
2967 */
2968static int
2969smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2970{
2971 struct connection *conn = objt_conn(smp->sess->origin);
2972 struct stksess *ts;
2973 struct stktable_key *key;
2974 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002975 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002976
2977 if (!conn)
2978 return 0;
2979
Joseph Herlant5662fa42018-11-15 13:43:28 -08002980 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02002981 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002982 return 0;
2983
2984 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002985 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002986 if (!key)
2987 return 0;
2988
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002989 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002990
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002991 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01002992 /* entry does not exist and could not be created */
2993 return 0;
2994
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002995 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002996 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01002997 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002998 }
Willy Tarreau7d562212016-11-25 16:10:05 +01002999
3000 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003001
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003002 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003003
Willy Tarreau7d562212016-11-25 16:10:05 +01003004 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003005
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003006 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003007
Willy Tarreau7d562212016-11-25 16:10:05 +01003008 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003009
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003010 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003011
3012 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01003013 return 1;
3014}
3015
3016/* set <smp> to the number of concurrent connections from the stream's tracked
3017 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3018 * "src_conn_cur" only.
3019 */
3020static int
3021smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
3022{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003023 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003024 struct stkctr *stkctr;
3025
Emeric Brun819fc6f2017-06-13 19:37:32 +02003026 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003027 if (!stkctr)
3028 return 0;
3029
3030 smp->flags = SMP_F_VOL_TEST;
3031 smp->data.type = SMP_T_SINT;
3032 smp->data.u.sint = 0;
3033 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003034 void *ptr;
3035
3036 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
3037 if (!ptr) {
3038 if (stkctr == &tmpstkctr)
3039 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003040 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003041 }
3042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003043 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003044
Willy Tarreau7d562212016-11-25 16:10:05 +01003045 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003046
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003047 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003048
3049 if (stkctr == &tmpstkctr)
3050 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003051 }
3052 return 1;
3053}
3054
3055/* set <smp> to the cumulated number of streams from the stream's tracked
3056 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3057 * "src_sess_cnt" only.
3058 */
3059static int
3060smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3061{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003062 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003063 struct stkctr *stkctr;
3064
Emeric Brun819fc6f2017-06-13 19:37:32 +02003065 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003066 if (!stkctr)
3067 return 0;
3068
3069 smp->flags = SMP_F_VOL_TEST;
3070 smp->data.type = SMP_T_SINT;
3071 smp->data.u.sint = 0;
3072 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003073 void *ptr;
3074
3075 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
3076 if (!ptr) {
3077 if (stkctr == &tmpstkctr)
3078 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003079 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003080 }
3081
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003082 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003083
Willy Tarreau7d562212016-11-25 16:10:05 +01003084 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003085
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003086 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003087
3088 if (stkctr == &tmpstkctr)
3089 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003090 }
3091 return 1;
3092}
3093
3094/* set <smp> to the stream rate from the stream's tracked frontend counters.
3095 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3096 */
3097static int
3098smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3099{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003100 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003101 struct stkctr *stkctr;
3102
Emeric Brun819fc6f2017-06-13 19:37:32 +02003103 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003104 if (!stkctr)
3105 return 0;
3106
3107 smp->flags = SMP_F_VOL_TEST;
3108 smp->data.type = SMP_T_SINT;
3109 smp->data.u.sint = 0;
3110 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003111 void *ptr;
3112
3113 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
3114 if (!ptr) {
3115 if (stkctr == &tmpstkctr)
3116 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003117 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003118 }
3119
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003120 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003121
Willy Tarreau7d562212016-11-25 16:10:05 +01003122 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
3123 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003124
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003125 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003126
3127 if (stkctr == &tmpstkctr)
3128 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003129 }
3130 return 1;
3131}
3132
3133/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
3134 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3135 * "src_http_req_cnt" only.
3136 */
3137static int
3138smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3139{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003140 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003141 struct stkctr *stkctr;
3142
Emeric Brun819fc6f2017-06-13 19:37:32 +02003143 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003144 if (!stkctr)
3145 return 0;
3146
3147 smp->flags = SMP_F_VOL_TEST;
3148 smp->data.type = SMP_T_SINT;
3149 smp->data.u.sint = 0;
3150 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003151 void *ptr;
3152
3153 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
3154 if (!ptr) {
3155 if (stkctr == &tmpstkctr)
3156 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003157 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003158 }
3159
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003160 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003161
Willy Tarreau7d562212016-11-25 16:10:05 +01003162 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003163
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003164 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003165
3166 if (stkctr == &tmpstkctr)
3167 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003168 }
3169 return 1;
3170}
3171
3172/* set <smp> to the HTTP request rate from the stream's tracked frontend
3173 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3174 * "src_http_req_rate" only.
3175 */
3176static int
3177smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3178{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003179 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003180 struct stkctr *stkctr;
3181
Emeric Brun819fc6f2017-06-13 19:37:32 +02003182 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003183 if (!stkctr)
3184 return 0;
3185
3186 smp->flags = SMP_F_VOL_TEST;
3187 smp->data.type = SMP_T_SINT;
3188 smp->data.u.sint = 0;
3189 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003190 void *ptr;
3191
3192 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
3193 if (!ptr) {
3194 if (stkctr == &tmpstkctr)
3195 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003196 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003197 }
3198
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003199 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003200
Willy Tarreau7d562212016-11-25 16:10:05 +01003201 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3202 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003203
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003204 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003205
3206 if (stkctr == &tmpstkctr)
3207 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003208 }
3209 return 1;
3210}
3211
3212/* set <smp> to the cumulated number of HTTP requests errors from the stream's
3213 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3214 * "src_http_err_cnt" only.
3215 */
3216static int
3217smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3218{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003219 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003220 struct stkctr *stkctr;
3221
Emeric Brun819fc6f2017-06-13 19:37:32 +02003222 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003223 if (!stkctr)
3224 return 0;
3225
3226 smp->flags = SMP_F_VOL_TEST;
3227 smp->data.type = SMP_T_SINT;
3228 smp->data.u.sint = 0;
3229 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003230 void *ptr;
3231
3232 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
3233 if (!ptr) {
3234 if (stkctr == &tmpstkctr)
3235 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003236 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003237 }
3238
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003239 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003240
Willy Tarreau7d562212016-11-25 16:10:05 +01003241 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003242
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003243 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003244
3245 if (stkctr == &tmpstkctr)
3246 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003247 }
3248 return 1;
3249}
3250
3251/* set <smp> to the HTTP request error rate from the stream's tracked frontend
3252 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3253 * "src_http_err_rate" only.
3254 */
3255static int
3256smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3257{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003258 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003259 struct stkctr *stkctr;
3260
Emeric Brun819fc6f2017-06-13 19:37:32 +02003261 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003262 if (!stkctr)
3263 return 0;
3264
3265 smp->flags = SMP_F_VOL_TEST;
3266 smp->data.type = SMP_T_SINT;
3267 smp->data.u.sint = 0;
3268 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003269 void *ptr;
3270
3271 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
3272 if (!ptr) {
3273 if (stkctr == &tmpstkctr)
3274 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003275 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003276 }
3277
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003278 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003279
Willy Tarreau7d562212016-11-25 16:10:05 +01003280 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3281 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003282
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003283 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003284
3285 if (stkctr == &tmpstkctr)
3286 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003287 }
3288 return 1;
3289}
3290
Willy Tarreau826f3ab2021-02-10 12:07:15 +01003291/* set <smp> to the cumulated number of HTTP response failures from the stream's
3292 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
3293 * "src_http_fail_cnt" only.
3294 */
3295static int
3296smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3297{
3298 struct stkctr tmpstkctr;
3299 struct stkctr *stkctr;
3300
3301 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3302 if (!stkctr)
3303 return 0;
3304
3305 smp->flags = SMP_F_VOL_TEST;
3306 smp->data.type = SMP_T_SINT;
3307 smp->data.u.sint = 0;
3308 if (stkctr_entry(stkctr) != NULL) {
3309 void *ptr;
3310
3311 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
3312 if (!ptr) {
3313 if (stkctr == &tmpstkctr)
3314 stktable_release(stkctr->table, stkctr_entry(stkctr));
3315 return 0; /* parameter not stored */
3316 }
3317
3318 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3319
3320 smp->data.u.sint = stktable_data_cast(ptr, http_fail_cnt);
3321
3322 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3323
3324 if (stkctr == &tmpstkctr)
3325 stktable_release(stkctr->table, stkctr_entry(stkctr));
3326 }
3327 return 1;
3328}
3329
3330/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
3331 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
3332 * "src_http_fail_rate" only.
3333 */
3334static int
3335smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3336{
3337 struct stkctr tmpstkctr;
3338 struct stkctr *stkctr;
3339
3340 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3341 if (!stkctr)
3342 return 0;
3343
3344 smp->flags = SMP_F_VOL_TEST;
3345 smp->data.type = SMP_T_SINT;
3346 smp->data.u.sint = 0;
3347 if (stkctr_entry(stkctr) != NULL) {
3348 void *ptr;
3349
3350 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
3351 if (!ptr) {
3352 if (stkctr == &tmpstkctr)
3353 stktable_release(stkctr->table, stkctr_entry(stkctr));
3354 return 0; /* parameter not stored */
3355 }
3356
3357 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3358
3359 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_fail_rate),
3360 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
3361
3362 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3363
3364 if (stkctr == &tmpstkctr)
3365 stktable_release(stkctr->table, stkctr_entry(stkctr));
3366 }
3367 return 1;
3368}
3369
Willy Tarreau7d562212016-11-25 16:10:05 +01003370/* set <smp> to the number of kbytes received from clients, as found in the
3371 * stream's tracked frontend counters. Supports being called as
3372 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3373 */
3374static int
3375smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
3376{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003377 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003378 struct stkctr *stkctr;
3379
Emeric Brun819fc6f2017-06-13 19:37:32 +02003380 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003381 if (!stkctr)
3382 return 0;
3383
3384 smp->flags = SMP_F_VOL_TEST;
3385 smp->data.type = SMP_T_SINT;
3386 smp->data.u.sint = 0;
3387 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003388 void *ptr;
3389
3390 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
3391 if (!ptr) {
3392 if (stkctr == &tmpstkctr)
3393 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003394 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003395 }
3396
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003397 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003398
Willy Tarreau7d562212016-11-25 16:10:05 +01003399 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003400
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003401 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003402
3403 if (stkctr == &tmpstkctr)
3404 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003405 }
3406 return 1;
3407}
3408
3409/* set <smp> to the data rate received from clients in bytes/s, as found
3410 * in the stream's tracked frontend counters. Supports being called as
3411 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
3412 */
3413static int
3414smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3415{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003416 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003417 struct stkctr *stkctr;
3418
Emeric Brun819fc6f2017-06-13 19:37:32 +02003419 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003420 if (!stkctr)
3421 return 0;
3422
3423 smp->flags = SMP_F_VOL_TEST;
3424 smp->data.type = SMP_T_SINT;
3425 smp->data.u.sint = 0;
3426 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003427 void *ptr;
3428
3429 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
3430 if (!ptr) {
3431 if (stkctr == &tmpstkctr)
3432 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003433 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003434 }
3435
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003436 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003437
Willy Tarreau7d562212016-11-25 16:10:05 +01003438 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
3439 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003440
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003441 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003442
3443 if (stkctr == &tmpstkctr)
3444 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003445 }
3446 return 1;
3447}
3448
3449/* set <smp> to the number of kbytes sent to clients, as found in the
3450 * stream's tracked frontend counters. Supports being called as
3451 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3452 */
3453static int
3454smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
3455{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003456 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003457 struct stkctr *stkctr;
3458
Emeric Brun819fc6f2017-06-13 19:37:32 +02003459 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003460 if (!stkctr)
3461 return 0;
3462
3463 smp->flags = SMP_F_VOL_TEST;
3464 smp->data.type = SMP_T_SINT;
3465 smp->data.u.sint = 0;
3466 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003467 void *ptr;
3468
3469 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
3470 if (!ptr) {
3471 if (stkctr == &tmpstkctr)
3472 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003473 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003474 }
3475
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003476 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003477
Willy Tarreau7d562212016-11-25 16:10:05 +01003478 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003479
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003480 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003481
3482 if (stkctr == &tmpstkctr)
3483 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003484 }
3485 return 1;
3486}
3487
3488/* set <smp> to the data rate sent to clients in bytes/s, as found in the
3489 * stream's tracked frontend counters. Supports being called as
3490 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
3491 */
3492static int
3493smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3494{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003495 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003496 struct stkctr *stkctr;
3497
Emeric Brun819fc6f2017-06-13 19:37:32 +02003498 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003499 if (!stkctr)
3500 return 0;
3501
3502 smp->flags = SMP_F_VOL_TEST;
3503 smp->data.type = SMP_T_SINT;
3504 smp->data.u.sint = 0;
3505 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003506 void *ptr;
3507
3508 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
3509 if (!ptr) {
3510 if (stkctr == &tmpstkctr)
3511 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003512 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003513 }
3514
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003515 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003516
Willy Tarreau7d562212016-11-25 16:10:05 +01003517 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3518 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003519
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003520 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003521
3522 if (stkctr == &tmpstkctr)
3523 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003524 }
3525 return 1;
3526}
3527
3528/* set <smp> to the number of active trackers on the SC entry in the stream's
3529 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3530 */
3531static int
3532smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
3533{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003534 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003535 struct stkctr *stkctr;
3536
Emeric Brun819fc6f2017-06-13 19:37:32 +02003537 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003538 if (!stkctr)
3539 return 0;
3540
3541 smp->flags = SMP_F_VOL_TEST;
3542 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003543 if (stkctr == &tmpstkctr) {
3544 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
3545 stktable_release(stkctr->table, stkctr_entry(stkctr));
3546 }
3547 else {
3548 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
3549 }
3550
Willy Tarreau7d562212016-11-25 16:10:05 +01003551 return 1;
3552}
3553
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003554
3555/* The functions below are used to manipulate table contents from the CLI.
3556 * There are 3 main actions, "clear", "set" and "show". The code is shared
3557 * between all actions, and the action is encoded in the void *private in
3558 * the appctx as well as in the keyword registration, among one of the
3559 * following values.
3560 */
3561
3562enum {
3563 STK_CLI_ACT_CLR,
3564 STK_CLI_ACT_SET,
3565 STK_CLI_ACT_SHOW,
3566};
3567
3568/* Dump the status of a table to a stream interface's
3569 * read buffer. It returns 0 if the output buffer is full
3570 * and needs to be called again, otherwise non-zero.
3571 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003572static int table_dump_head_to_buffer(struct buffer *msg,
3573 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003574 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003575{
3576 struct stream *s = si_strm(si);
3577
3578 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003579 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003580
3581 /* any other information should be dumped here */
3582
William Lallemand07a62f72017-05-24 00:57:40 +02003583 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003584 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
3585
Willy Tarreau06d80a92017-10-19 14:32:15 +02003586 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003587 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003588 return 0;
3589 }
3590
3591 return 1;
3592}
3593
3594/* Dump a table entry to a stream interface's
3595 * read buffer. It returns 0 if the output buffer is full
3596 * and needs to be called again, otherwise non-zero.
3597 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003598static int table_dump_entry_to_buffer(struct buffer *msg,
3599 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003600 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003601{
3602 int dt;
3603
3604 chunk_appendf(msg, "%p:", entry);
3605
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003606 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003607 char addr[INET_ADDRSTRLEN];
3608 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
3609 chunk_appendf(msg, " key=%s", addr);
3610 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003611 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003612 char addr[INET6_ADDRSTRLEN];
3613 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
3614 chunk_appendf(msg, " key=%s", addr);
3615 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003616 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01003617 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003618 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003619 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003620 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003621 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003622 }
3623 else {
3624 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003625 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003626 }
3627
3628 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
3629
3630 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3631 void *ptr;
3632
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003633 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003634 continue;
3635 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Emeric Brunc24b4142021-06-30 16:24:04 +02003636 chunk_appendf(msg, " %s(%u)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003637 else
3638 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
3639
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003640 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003641 switch (stktable_data_types[dt].std_type) {
3642 case STD_T_SINT:
3643 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
3644 break;
3645 case STD_T_UINT:
3646 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
3647 break;
3648 case STD_T_ULL:
Emeric Brunc24b4142021-06-30 16:24:04 +02003649 chunk_appendf(msg, "%llu", stktable_data_cast(ptr, std_t_ull));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003650 break;
3651 case STD_T_FRQP:
Emeric Brunc24b4142021-06-30 16:24:04 +02003652 chunk_appendf(msg, "%u",
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003653 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003654 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003655 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02003656 case STD_T_DICT: {
3657 struct dict_entry *de;
3658 de = stktable_data_cast(ptr, std_t_dict);
3659 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
3660 break;
3661 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003662 }
3663 }
3664 chunk_appendf(msg, "\n");
3665
Willy Tarreau06d80a92017-10-19 14:32:15 +02003666 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003667 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003668 return 0;
3669 }
3670
3671 return 1;
3672}
3673
3674
3675/* Processes a single table entry matching a specific key passed in argument.
3676 * returns 0 if wants to be called again, 1 if has ended processing.
3677 */
3678static int table_process_entry_per_key(struct appctx *appctx, char **args)
3679{
3680 struct stream_interface *si = appctx->owner;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003681 struct stktable *t = appctx->ctx.table.target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003682 struct stksess *ts;
3683 uint32_t uint32_key;
3684 unsigned char ip6_key[sizeof(struct in6_addr)];
3685 long long value;
3686 int data_type;
3687 int cur_arg;
3688 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003689 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003690
Willy Tarreau9d008692019-08-09 11:21:01 +02003691 if (!*args[4])
3692 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003693
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003694 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003695 case SMP_T_IPV4:
3696 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02003697 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003698 break;
3699 case SMP_T_IPV6:
Christopher Faulet1e850b52021-11-15 09:17:25 +01003700 if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
3701 return cli_err(appctx, "Invalid key\n");
Christopher Fauletca20d022017-08-29 15:30:31 +02003702 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003703 break;
3704 case SMP_T_SINT:
3705 {
3706 char *endptr;
3707 unsigned long val;
3708 errno = 0;
3709 val = strtoul(args[4], &endptr, 10);
3710 if ((errno == ERANGE && val == ULONG_MAX) ||
3711 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02003712 val > 0xffffffff)
3713 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003714 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02003715 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003716 break;
3717 }
3718 break;
3719 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02003720 static_table_key.key = args[4];
3721 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003722 break;
3723 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003724 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003725 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02003726 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 +01003727 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02003728 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 +01003729 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02003730 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 +01003731 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003732 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003733 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003734 }
3735
3736 /* check permissions */
3737 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3738 return 1;
3739
Willy Tarreaua24bc782016-12-14 15:50:35 +01003740 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003741 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003742 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003743 if (!ts)
3744 return 1;
3745 chunk_reset(&trash);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003746 if (!table_dump_head_to_buffer(&trash, si, t, t)) {
3747 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003748 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003749 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003750 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003751 if (!table_dump_entry_to_buffer(&trash, si, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003752 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003753 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003754 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003755 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003756 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003757 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003758 break;
3759
3760 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003761 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003762 if (!ts)
3763 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003764
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003765 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003766 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02003767 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003768 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003769 break;
3770
3771 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003772 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003773 if (!ts) {
3774 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02003775 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003776 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003777 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003778 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
3779 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003780 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003781 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003782 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003783 return 1;
3784 }
3785
3786 data_type = stktable_get_data_type(args[cur_arg] + 5);
3787 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003788 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003789 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003790 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003791 return 1;
3792 }
3793
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003794 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003795 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003796 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003797 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003798 return 1;
3799 }
3800
3801 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003802 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003803 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003804 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003805 return 1;
3806 }
3807
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003808 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003809
3810 switch (stktable_data_types[data_type].std_type) {
3811 case STD_T_SINT:
3812 stktable_data_cast(ptr, std_t_sint) = value;
3813 break;
3814 case STD_T_UINT:
3815 stktable_data_cast(ptr, std_t_uint) = value;
3816 break;
3817 case STD_T_ULL:
3818 stktable_data_cast(ptr, std_t_ull) = value;
3819 break;
3820 case STD_T_FRQP:
3821 /* We set both the current and previous values. That way
3822 * the reported frequency is stable during all the period
3823 * then slowly fades out. This allows external tools to
3824 * push measures without having to update them too often.
3825 */
3826 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003827 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01003828 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003829 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01003830 using its internal lock */
3831 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003832 frqp->prev_ctr = 0;
3833 frqp->curr_ctr = value;
3834 break;
3835 }
3836 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003837 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003838 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003839 break;
3840
3841 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003842 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003843 }
3844 return 1;
3845}
3846
3847/* Prepares the appctx fields with the data-based filters from the command line.
3848 * Returns 0 if the dump can proceed, 1 if has ended processing.
3849 */
3850static int table_prepare_data_request(struct appctx *appctx, char **args)
3851{
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003852 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003853 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003854
Willy Tarreau9d008692019-08-09 11:21:01 +02003855 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR)
3856 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003857
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003858 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
3859 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
3860 break;
3861 /* condition on stored data value */
3862 appctx->ctx.table.data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
3863 if (appctx->ctx.table.data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003864 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003865
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003866 if (!((struct stktable *)appctx->ctx.table.target)->data_ofs[appctx->ctx.table.data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003867 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 +01003868
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003869 appctx->ctx.table.data_op[i] = get_std_op(args[4+3*i]);
Willy Tarreau2b64a352020-01-22 17:09:47 +01003870 if (appctx->ctx.table.data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003871 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 +01003872
Adis Nezirovic56dd3542020-01-22 16:16:48 +01003873 if (!*args[5+3*i] || strl2llrc(args[5+3*i], strlen(args[5+3*i]), &appctx->ctx.table.value[i]) != 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003874 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
3875 }
3876
3877 if (*args[3+3*i]) {
3878 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 +01003879 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003880
3881 /* OK we're done, all the fields are set */
3882 return 0;
3883}
3884
3885/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003886static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003887{
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003888 int i;
3889
3890 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
3891 appctx->ctx.table.data_type[i] = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003892 appctx->ctx.table.target = NULL;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003893 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003894 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003895
3896 if (*args[2]) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003897 appctx->ctx.table.target = stktable_find_by_name(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003898 if (!appctx->ctx.table.target)
3899 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003900 }
3901 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01003902 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003903 goto err_args;
3904 return 0;
3905 }
3906
3907 if (strcmp(args[3], "key") == 0)
3908 return table_process_entry_per_key(appctx, args);
3909 else if (strncmp(args[3], "data.", 5) == 0)
3910 return table_prepare_data_request(appctx, args);
3911 else if (*args[3])
3912 goto err_args;
3913
3914 return 0;
3915
3916err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003917 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003918 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02003919 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 +01003920 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02003921 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 +01003922 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02003923 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003924 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003925 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003926 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003927}
3928
3929/* This function is used to deal with table operations (dump or clear depending
3930 * on the action stored in appctx->private). It returns 0 if the output buffer is
3931 * full and it needs to be called again, otherwise non-zero.
3932 */
3933static int cli_io_handler_table(struct appctx *appctx)
3934{
3935 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003936 struct stream *s = si_strm(si);
3937 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003938 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003939 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003940
3941 /*
3942 * We have 3 possible states in appctx->st2 :
3943 * - STAT_ST_INIT : the first call
3944 * - STAT_ST_INFO : the proxy pointer points to the next table to
3945 * dump, the entry pointer is NULL ;
3946 * - STAT_ST_LIST : the proxy pointer points to the current table
3947 * and the entry pointer points to the next entry to be dumped,
3948 * and the refcount on the next entry is held ;
3949 * - STAT_ST_END : nothing left to dump, the buffer may contain some
3950 * data though.
3951 */
3952
3953 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3954 /* in case of abort, remove any refcount we might have set on an entry */
3955 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003956 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003957 }
3958 return 1;
3959 }
3960
3961 chunk_reset(&trash);
3962
3963 while (appctx->st2 != STAT_ST_FIN) {
3964 switch (appctx->st2) {
3965 case STAT_ST_INIT:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003966 appctx->ctx.table.t = appctx->ctx.table.target;
3967 if (!appctx->ctx.table.t)
3968 appctx->ctx.table.t = stktables_list;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003969
3970 appctx->ctx.table.entry = NULL;
3971 appctx->st2 = STAT_ST_INFO;
3972 break;
3973
3974 case STAT_ST_INFO:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003975 if (!appctx->ctx.table.t ||
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003976 (appctx->ctx.table.target &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003977 appctx->ctx.table.t != appctx->ctx.table.target)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003978 appctx->st2 = STAT_ST_END;
3979 break;
3980 }
3981
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003982 if (appctx->ctx.table.t->size) {
3983 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003984 return 0;
3985
3986 if (appctx->ctx.table.target &&
William Lallemand07a62f72017-05-24 00:57:40 +02003987 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003988 /* dump entries only if table explicitly requested */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003989 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
3990 eb = ebmb_first(&appctx->ctx.table.t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003991 if (eb) {
3992 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3993 appctx->ctx.table.entry->ref_cnt++;
3994 appctx->st2 = STAT_ST_LIST;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003995 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003996 break;
3997 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003998 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003999 }
4000 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004001 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004002 break;
4003
4004 case STAT_ST_LIST:
4005 skip_entry = 0;
4006
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004007 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004008
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004009 if (appctx->ctx.table.data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004010 /* we're filtering on some data contents */
4011 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01004012 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004013 signed char op;
4014 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004015
Emeric Brun819fc6f2017-06-13 19:37:32 +02004016
Willy Tarreau2b64a352020-01-22 17:09:47 +01004017 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004018 if (appctx->ctx.table.data_type[i] == -1)
4019 break;
4020 dt = appctx->ctx.table.data_type[i];
4021 ptr = stktable_data_ptr(appctx->ctx.table.t,
4022 appctx->ctx.table.entry,
4023 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004024
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004025 data = 0;
4026 switch (stktable_data_types[dt].std_type) {
4027 case STD_T_SINT:
4028 data = stktable_data_cast(ptr, std_t_sint);
4029 break;
4030 case STD_T_UINT:
4031 data = stktable_data_cast(ptr, std_t_uint);
4032 break;
4033 case STD_T_ULL:
4034 data = stktable_data_cast(ptr, std_t_ull);
4035 break;
4036 case STD_T_FRQP:
4037 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4038 appctx->ctx.table.t->data_arg[dt].u);
4039 break;
4040 }
4041
4042 op = appctx->ctx.table.data_op[i];
4043 value = appctx->ctx.table.value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004044
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004045 /* skip the entry if the data does not match the test and the value */
4046 if ((data < value &&
4047 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
4048 (data == value &&
4049 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
4050 (data > value &&
4051 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
4052 skip_entry = 1;
4053 break;
4054 }
4055 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004056 }
4057
4058 if (show && !skip_entry &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004059 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.entry)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004060 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004061 return 0;
4062 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004063
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004064 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004065
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004066 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004067 appctx->ctx.table.entry->ref_cnt--;
4068
4069 eb = ebmb_next(&appctx->ctx.table.entry->key);
4070 if (eb) {
4071 struct stksess *old = appctx->ctx.table.entry;
4072 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4073 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004074 __stksess_kill_if_expired(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004075 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004076 __stksess_kill(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004077 appctx->ctx.table.entry->ref_cnt++;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004078 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004079 break;
4080 }
4081
4082
4083 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004084 __stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004085 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004086 __stksess_kill(appctx->ctx.table.t, appctx->ctx.table.entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004087
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004088 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004089
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004090 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004091 appctx->st2 = STAT_ST_INFO;
4092 break;
4093
4094 case STAT_ST_END:
4095 appctx->st2 = STAT_ST_FIN;
4096 break;
4097 }
4098 }
4099 return 1;
4100}
4101
4102static void cli_release_show_table(struct appctx *appctx)
4103{
4104 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004105 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004106 }
4107}
4108
Willy Tarreau478331d2020-08-28 11:31:31 +02004109static void stkt_late_init(void)
4110{
4111 struct sample_fetch *f;
4112
4113 f = find_sample_fetch("src", strlen("src"));
4114 if (f)
4115 smp_fetch_src = f->process;
4116}
4117
4118INITCALL0(STG_INIT, stkt_late_init);
4119
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004120/* register cli keywords */
4121static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004122 { { "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 },
4123 { { "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 },
4124 { { "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 +01004125 {{},}
4126}};
4127
Willy Tarreau0108d902018-11-25 19:14:37 +01004128INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004129
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004130static struct action_kw_list tcp_conn_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004131 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4132 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4133 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004134 { /* END */ }
4135}};
4136
Willy Tarreau0108d902018-11-25 19:14:37 +01004137INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
4138
Willy Tarreau620408f2016-10-21 16:37:51 +02004139static struct action_kw_list tcp_sess_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004140 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4141 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4142 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02004143 { /* END */ }
4144}};
4145
Willy Tarreau0108d902018-11-25 19:14:37 +01004146INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
4147
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004148static struct action_kw_list tcp_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004149 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4150 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4151 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004152 { /* END */ }
4153}};
4154
Willy Tarreau0108d902018-11-25 19:14:37 +01004155INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
4156
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004157static struct action_kw_list tcp_res_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004158 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4159 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4160 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004161 { /* END */ }
4162}};
4163
Willy Tarreau0108d902018-11-25 19:14:37 +01004164INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
4165
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004166static struct action_kw_list http_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004167 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4168 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4169 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004170 { /* END */ }
4171}};
4172
Willy Tarreau0108d902018-11-25 19:14:37 +01004173INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
4174
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004175static struct action_kw_list http_res_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004176 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4177 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4178 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004179 { /* END */ }
4180}};
4181
Willy Tarreau0108d902018-11-25 19:14:37 +01004182INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
4183
Willy Tarreau7d562212016-11-25 16:10:05 +01004184/* Note: must not be declared <const> as its list will be overwritten.
4185 * Please take care of keeping this list alphabetically sorted.
4186 */
4187static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
4188 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4189 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4190 { "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 +01004191 { "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 +01004192 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4193 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4194 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01004195 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau7d562212016-11-25 16:10:05 +01004196 { "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 +01004197 { "sc_get_gpc1", smp_fetch_sc_get_gpc1, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau7d562212016-11-25 16:10:05 +01004198 { "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 +01004199 { "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 +01004200 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4201 { "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 +01004202 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4203 { "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 +01004204 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4205 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4206 { "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 +01004207 { "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 +01004208 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4209 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4210 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4211 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4212 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4213 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4214 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4215 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4216 { "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 +01004217 { "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 +01004218 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4219 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4220 { "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 +01004221 { "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 +01004222 { "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 +01004223 { "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 +01004224 { "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 +01004225 { "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 +01004226 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4227 { "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 +01004228 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4229 { "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 +01004230 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4231 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4232 { "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 +01004233 { "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 +01004234 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4235 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4236 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4237 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4238 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4239 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4240 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4241 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4242 { "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 +01004243 { "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 +01004244 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4245 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4246 { "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 +01004247 { "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 +01004248 { "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 +01004249 { "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 +01004250 { "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 +01004251 { "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 +01004252 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4253 { "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 +01004254 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4255 { "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 +01004256 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4257 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4258 { "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 +01004259 { "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 +01004260 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4261 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4262 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4263 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4264 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4265 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4266 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4267 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4268 { "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 +01004269 { "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 +01004270 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4271 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4272 { "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 +01004273 { "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 +01004274 { "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 +01004275 { "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 +01004276 { "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 +01004277 { "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 +01004278 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4279 { "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 +01004280 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4281 { "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 +01004282 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4283 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4284 { "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 +01004285 { "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 +01004286 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4287 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4288 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4289 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4290 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4291 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4292 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4293 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4294 { "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 +01004295 { "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 +01004296 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4297 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4298 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Thierry FOURNIER401c64b2017-01-05 11:44:09 +01004299 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01004300 { "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 +01004301 { "src_get_gpc1", smp_fetch_sc_get_gpc1, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Willy Tarreau7d562212016-11-25 16:10:05 +01004302 { "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 +01004303 { "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 +01004304 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4305 { "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 +01004306 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4307 { "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 +01004308 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4309 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4310 { "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 +01004311 { "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 +01004312 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4313 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4314 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4315 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4316 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4317 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4318 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4319 { /* END */ },
4320}};
4321
Willy Tarreau0108d902018-11-25 19:14:37 +01004322INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01004323
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004324/* Note: must not be declared <const> as its list will be overwritten */
4325static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02004326 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
4327 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4328 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4329 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4330 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4331 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4332 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4333 { "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 +01004334 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02004335 { "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 +01004336 { "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 +02004337 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4338 { "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 +01004339 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4340 { "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 +02004341 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4342 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4343 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4344 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4345 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4346 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4347 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4348 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004349 { /* END */ },
4350}};
4351
Willy Tarreau0108d902018-11-25 19:14:37 +01004352INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);