blob: fbfbc4a8c7310a30645c82f123331081f9ca911e [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
Willy Tarreau86257dc2010-06-06 12:57:10 +0200225 if (!eb || eb->key > ts->exp.key)
226 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100227
228 continue;
229 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100230
Willy Tarreauaea940e2010-06-06 11:56:36 +0200231 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200232 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200233 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200234 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 batched++;
236 }
237
238 return batched;
239}
240
241/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200242 * Trash oldest <to_batch> sticky sessions from table <t>
243 * Returns number of trashed sticky sessions.
244 * This function locks the table
245 */
246int stktable_trash_oldest(struct stktable *t, int to_batch)
247{
248 int ret;
249
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100250 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200251 ret = __stktable_trash_oldest(t, to_batch);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100252 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200253
254 return ret;
255}
256/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200257 * Allocate and initialise a new sticky session.
258 * The new sticky session is returned or NULL in case of lack of memory.
259 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200260 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
261 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100262 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200263struct stksess *__stksess_new(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100264{
265 struct stksess *ts;
266
267 if (unlikely(t->current == t->size)) {
268 if ( t->nopurge )
269 return NULL;
270
Emeric Brun819fc6f2017-06-13 19:37:32 +0200271 if (!__stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100272 return NULL;
273 }
274
Willy Tarreaubafbe012017-11-24 17:34:44 +0100275 ts = pool_alloc(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100276 if (ts) {
277 t->current++;
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100278 ts = (void *)ts + round_ptr_size(t->data_size);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200279 __stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200280 if (key)
281 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100282 }
283
284 return ts;
285}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200286/*
287 * Allocate and initialise a new sticky session.
288 * The new sticky session is returned or NULL in case of lack of memory.
289 * Sticky sessions should only be allocated this way, and must be freed using
290 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
291 * is not NULL, it is assigned to the new session.
292 * This function locks the table
293 */
294struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
295{
296 struct stksess *ts;
297
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100298 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200299 ts = __stksess_new(t, key);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100300 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200301
302 return ts;
303}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100304
305/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200306 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200307 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200309struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100310{
311 struct ebmb_node *eb;
312
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200313 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200314 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 +0100315 else
316 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
317
318 if (unlikely(!eb)) {
319 /* no session found */
320 return NULL;
321 }
322
Willy Tarreau86257dc2010-06-06 12:57:10 +0200323 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100324}
325
Emeric Brun819fc6f2017-06-13 19:37:32 +0200326/*
327 * Looks in table <t> for a sticky session matching key <key>.
328 * Returns pointer on requested sticky session or NULL if none was found.
329 * The refcount of the found entry is increased and this function
330 * is protected using the table lock
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200331 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200332struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200333{
334 struct stksess *ts;
335
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100336 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200337 ts = __stktable_lookup_key(t, key);
338 if (ts)
339 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200341
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200342 return ts;
343}
344
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200345/*
346 * Looks in table <t> for a sticky session with same key as <ts>.
347 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200349struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100351 struct ebmb_node *eb;
352
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200353 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200354 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100355 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200356 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200358 if (unlikely(!eb))
359 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100360
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200361 return ebmb_entry(eb, struct stksess, key);
362}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363
Emeric Brun819fc6f2017-06-13 19:37:32 +0200364/*
365 * Looks in table <t> for a sticky session with same key as <ts>.
366 * Returns pointer on requested sticky session or NULL if none was found.
367 * The refcount of the found entry is increased and this function
368 * is protected using the table lock
369 */
370struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
371{
372 struct stksess *lts;
373
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100374 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200375 lts = __stktable_lookup(t, ts);
376 if (lts)
377 lts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100378 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200379
380 return lts;
381}
382
Willy Tarreaucb183642010-06-06 17:58:34 +0200383/* Update the expiration timer for <ts> but do not touch its expiration node.
384 * The table's expiration timer is updated if set.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200385 * The node will be also inserted into the update tree if needed, at a position
386 * depending if the update is a local or coming from a remote node
Willy Tarreaucb183642010-06-06 17:58:34 +0200387 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200388void __stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200389{
Emeric Brun85e77c72010-09-23 18:16:52 +0200390 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200391 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200392 if (t->expire) {
393 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
394 task_queue(t->exp_task);
395 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200396
Emeric Brun819fc6f2017-06-13 19:37:32 +0200397 /* If sync is enabled */
398 if (t->sync_task) {
399 if (local) {
400 /* If this entry is not in the tree
401 or not scheduled for at least one peer */
402 if (!ts->upd.node.leaf_p
403 || (int)(t->commitupdate - ts->upd.key) >= 0
404 || (int)(ts->upd.key - t->localupdate) >= 0) {
405 ts->upd.key = ++t->update;
406 t->localupdate = t->update;
407 eb32_delete(&ts->upd);
408 eb = eb32_insert(&t->updates, &ts->upd);
409 if (eb != &ts->upd) {
410 eb32_delete(eb);
411 eb32_insert(&t->updates, &ts->upd);
412 }
Emeric Brunaaf58602015-06-15 17:23:30 +0200413 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200414 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
Emeric Brun85e77c72010-09-23 18:16:52 +0200415 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200416 else {
417 /* If this entry is not in the tree */
418 if (!ts->upd.node.leaf_p) {
419 ts->upd.key= (++t->update)+(2147483648U);
420 eb = eb32_insert(&t->updates, &ts->upd);
421 if (eb != &ts->upd) {
422 eb32_delete(eb);
423 eb32_insert(&t->updates, &ts->upd);
424 }
425 }
426 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200427 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200428}
429
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200430/* Update the expiration timer for <ts> but do not touch its expiration node.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200431 * The table's expiration timer is updated using the date of expiration coming from
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200432 * <t> stick-table configuration.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200433 * The node will be also inserted into the update tree if needed, at a position
434 * considering the update is coming from a remote node
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200435 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200436void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
437{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100438 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200439 __stktable_touch_with_exp(t, ts, 0, ts->expire);
440 if (decrefcnt)
441 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100442 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200443}
444
445/* Update the expiration timer for <ts> but do not touch its expiration node.
446 * The table's expiration timer is updated using the date of expiration coming from
447 * <t> stick-table configuration.
448 * The node will be also inserted into the update tree if needed, at a position
449 * considering the update was made locally
450 */
451void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200452{
453 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
454
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100455 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200456 __stktable_touch_with_exp(t, ts, 1, expire);
457 if (decrefcnt)
458 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100459 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200460}
Willy Tarreau43e90352018-06-27 06:25:57 +0200461/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL */
462static void stktable_release(struct stktable *t, struct stksess *ts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200463{
Willy Tarreau43e90352018-06-27 06:25:57 +0200464 if (!ts)
465 return;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100466 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200467 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100468 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200469}
470
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200471/* Insert new sticky session <ts> in the table. It is assumed that it does not
472 * yet exist (the caller must check this). The table's timeout is updated if it
473 * is set. <ts> is returned.
474 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200475void __stktable_store(struct stktable *t, struct stksess *ts)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200476{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100477
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200478 ebmb_insert(&t->keys, &ts->key, t->key_size);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200479 ts->exp.key = ts->expire;
480 eb32_insert(&t->exps, &ts->exp);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200481 if (t->expire) {
482 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
483 task_queue(t->exp_task);
484 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200485}
486
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200487/* Returns a valid or initialized stksess for the specified stktable_key in the
488 * specified table, or NULL if the key was NULL, or if no entry was found nor
489 * could be created. The entry's expiration is updated.
490 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200491struct stksess *__stktable_get_entry(struct stktable *table, struct stktable_key *key)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200492{
493 struct stksess *ts;
494
495 if (!key)
496 return NULL;
497
Emeric Brun819fc6f2017-06-13 19:37:32 +0200498 ts = __stktable_lookup_key(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200499 if (ts == NULL) {
500 /* entry does not exist, initialize a new one */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200501 ts = __stksess_new(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200502 if (!ts)
503 return NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200504 __stktable_store(table, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200505 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200506 return ts;
507}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200508/* Returns a valid or initialized stksess for the specified stktable_key in the
509 * specified table, or NULL if the key was NULL, or if no entry was found nor
510 * could be created. The entry's expiration is updated.
511 * This function locks the table, and the refcount of the entry is increased.
512 */
513struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
514{
515 struct stksess *ts;
516
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100517 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200518 ts = __stktable_get_entry(table, key);
519 if (ts)
520 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100521 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200522
523 return ts;
524}
525
526/* Lookup for an entry with the same key and store the submitted
527 * stksess if not found.
528 */
529struct stksess *__stktable_set_entry(struct stktable *table, struct stksess *nts)
530{
531 struct stksess *ts;
532
533 ts = __stktable_lookup(table, nts);
534 if (ts == NULL) {
535 ts = nts;
536 __stktable_store(table, ts);
537 }
538 return ts;
539}
540
541/* Lookup for an entry with the same key and store the submitted
542 * stksess if not found.
543 * This function locks the table, and the refcount of the entry is increased.
544 */
545struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
546{
547 struct stksess *ts;
548
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100549 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200550 ts = __stktable_set_entry(table, nts);
551 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100552 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200553
Emeric Brun819fc6f2017-06-13 19:37:32 +0200554 return ts;
555}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100556/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200557 * Trash expired sticky sessions from table <t>. The next expiration date is
558 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100559 */
560static int stktable_trash_expired(struct stktable *t)
561{
562 struct stksess *ts;
563 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200564 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100565
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100566 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100567 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
568
569 while (1) {
570 if (unlikely(!eb)) {
571 /* we might have reached the end of the tree, typically because
572 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200573 * half. Let's loop back to the beginning of the tree now if we
574 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100575 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200576 if (looped)
577 break;
578 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100579 eb = eb32_first(&t->exps);
580 if (likely(!eb))
581 break;
582 }
583
584 if (likely(tick_is_lt(now_ms, eb->key))) {
585 /* timer not expired yet, revisit it later */
586 t->exp_next = eb->key;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100587 goto out_unlock;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100588 }
589
590 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200591 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100592 eb = eb32_next(eb);
593
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200594 /* don't delete an entry which is currently referenced */
595 if (ts->ref_cnt)
596 continue;
597
Willy Tarreau86257dc2010-06-06 12:57:10 +0200598 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100599
600 if (!tick_is_expired(ts->expire, now_ms)) {
601 if (!tick_isset(ts->expire))
602 continue;
603
Willy Tarreau86257dc2010-06-06 12:57:10 +0200604 ts->exp.key = ts->expire;
605 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100606
Willy Tarreau86257dc2010-06-06 12:57:10 +0200607 if (!eb || eb->key > ts->exp.key)
608 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100609 continue;
610 }
611
612 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200613 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200614 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200615 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100616 }
617
618 /* We have found no task to expire in any tree */
619 t->exp_next = TICK_ETERNITY;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100620out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100621 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100622 return t->exp_next;
623}
624
625/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200626 * Task processing function to trash expired sticky sessions. A pointer to the
627 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100628 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100629struct task *process_table_expire(struct task *task, void *context, unsigned int state)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100630{
Olivier Houchard9f6af332018-05-25 14:04:04 +0200631 struct stktable *t = context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100632
633 task->expire = stktable_trash_expired(t);
634 return task;
635}
636
Willy Tarreauaea940e2010-06-06 11:56:36 +0200637/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100638int stktable_init(struct stktable *t)
639{
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200640 int peers_retval = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100641 if (t->size) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200642 t->keys = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100643 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100644 t->updates = EB_ROOT_UNIQUE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100645 HA_SPIN_INIT(&t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100646
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100647 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 +0100648
649 t->exp_next = TICK_ETERNITY;
650 if ( t->expire ) {
Emeric Brunc60def82017-09-27 14:59:38 +0200651 t->exp_task = task_new(MAX_THREADS_MASK);
Willy Tarreau848522f2018-10-15 11:12:15 +0200652 if (!t->exp_task)
653 return 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100654 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100655 t->exp_task->context = (void *)t;
656 }
Willy Tarreauc3914d42020-09-24 08:39:22 +0200657 if (t->peers.p && t->peers.p->peers_fe && !t->peers.p->peers_fe->disabled) {
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200658 peers_retval = peers_register_table(t->peers.p, t);
Emeric Brun32da3c42010-09-23 18:39:19 +0200659 }
660
Remi Tricot-Le Bretonbe5b1bb2021-05-12 17:39:04 +0200661 return (t->pool != NULL) && !peers_retval;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100662 }
663 return 1;
664}
665
666/*
667 * Configuration keywords of known table types
668 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200669struct stktable_type stktable_types[SMP_TYPES] = {
670 [SMP_T_SINT] = { "integer", 0, 4 },
671 [SMP_T_IPV4] = { "ip", 0, 4 },
672 [SMP_T_IPV6] = { "ipv6", 0, 16 },
673 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
674 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
675};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100676
677/*
678 * Parse table type configuration.
679 * Returns 0 on successful parsing, else 1.
680 * <myidx> is set at next configuration <args> index.
681 */
682int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
683{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200684 for (*type = 0; *type < SMP_TYPES; (*type)++) {
685 if (!stktable_types[*type].kw)
686 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100687 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
688 continue;
689
690 *key_size = stktable_types[*type].default_size;
691 (*myidx)++;
692
Willy Tarreauaea940e2010-06-06 11:56:36 +0200693 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100694 if (strcmp("len", args[*myidx]) == 0) {
695 (*myidx)++;
696 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200697 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100698 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200699 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200700 /* null terminated string needs +1 for '\0'. */
701 (*key_size)++;
702 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100703 (*myidx)++;
704 }
705 }
706 return 0;
707 }
708 return 1;
709}
710
Willy Tarreau3b63ca22021-05-08 14:10:42 +0200711/* reserve some space for data type <type>, and associate argument at <sa> if
712 * not NULL. Returns PE_NONE (0) if OK or an error code among :
713 * - PE_ENUM_OOR if <type> does not exist
714 * - PE_EXIST if <type> is already registered
715 * - PE_ARG_NOT_USE if <sa> was provided but not expected
716 * - PE_ARG_MISSING if <sa> was expected but not provided
717 */
718int stktable_alloc_data_type(struct stktable *t, int type, const char *sa)
719{
720 if (type >= STKTABLE_DATA_TYPES)
721 return PE_ENUM_OOR;
722
723 if (t->data_ofs[type])
724 /* already allocated */
725 return PE_EXIST;
726
727 switch (stktable_data_types[type].arg_type) {
728 case ARG_T_NONE:
729 if (sa)
730 return PE_ARG_NOT_USED;
731 break;
732 case ARG_T_INT:
733 if (!sa)
734 return PE_ARG_MISSING;
735 t->data_arg[type].i = atoi(sa);
736 break;
737 case ARG_T_DELAY:
738 if (!sa)
739 return PE_ARG_MISSING;
740 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
741 if (sa)
742 return PE_ARG_INVC; /* invalid char */
743 break;
744 }
745
746 t->data_size += stktable_type_size(stktable_data_types[type].std_type);
747 t->data_ofs[type] = -t->data_size;
748 return PE_NONE;
749}
750
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100751/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100752 * Parse a line with <linenum> as number in <file> configuration file to configure
753 * the stick-table with <t> as address and <id> as ID.
754 * <peers> provides the "peers" section pointer only if this function is called
755 * from a "peers" section.
756 * <nid> is the stick-table name which is sent over the network. It must be equal
757 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
758 * "peers" section name followed by a '/' character if parsed from a "peers" section.
Ilya Shipitsind4259502020-04-08 01:07:56 +0500759 * This is the responsibility of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100760 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
761 */
762int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100763 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100764{
765 int err_code = 0;
766 int idx = 1;
767 unsigned int val;
768
769 if (!id || !*id) {
770 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
771 err_code |= ERR_ALERT | ERR_ABORT;
772 goto out;
773 }
774
775 /* Store the "peers" section if this function is called from a "peers" section. */
776 if (peers) {
777 t->peers.p = peers;
778 idx++;
779 }
780
781 t->id = id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100782 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100783 t->type = (unsigned int)-1;
784 t->conf.file = file;
785 t->conf.line = linenum;
786
787 while (*args[idx]) {
788 const char *err;
789
790 if (strcmp(args[idx], "size") == 0) {
791 idx++;
792 if (!*(args[idx])) {
793 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
794 file, linenum, args[0], args[idx-1]);
795 err_code |= ERR_ALERT | ERR_FATAL;
796 goto out;
797 }
798 if ((err = parse_size_err(args[idx], &t->size))) {
799 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
800 file, linenum, args[0], *err, args[idx-1]);
801 err_code |= ERR_ALERT | ERR_FATAL;
802 goto out;
803 }
804 idx++;
805 }
806 /* This argument does not exit in "peers" section. */
807 else if (!peers && strcmp(args[idx], "peers") == 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 t->peers.name = strdup(args[idx++]);
816 }
817 else if (strcmp(args[idx], "expire") == 0) {
818 idx++;
819 if (!*(args[idx])) {
820 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
821 file, linenum, args[0], args[idx-1]);
822 err_code |= ERR_ALERT | ERR_FATAL;
823 goto out;
824 }
825 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200826 if (err == PARSE_TIME_OVER) {
827 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
828 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100829 err_code |= ERR_ALERT | ERR_FATAL;
830 goto out;
831 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200832 else if (err == PARSE_TIME_UNDER) {
833 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
834 file, linenum, args[0], args[idx], args[idx-1]);
835 err_code |= ERR_ALERT | ERR_FATAL;
836 goto out;
837 }
838 else if (err) {
839 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
840 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100841 err_code |= ERR_ALERT | ERR_FATAL;
842 goto out;
843 }
844 t->expire = val;
845 idx++;
846 }
847 else if (strcmp(args[idx], "nopurge") == 0) {
848 t->nopurge = 1;
849 idx++;
850 }
851 else if (strcmp(args[idx], "type") == 0) {
852 idx++;
853 if (stktable_parse_type(args, &idx, &t->type, &t->key_size) != 0) {
854 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n",
855 file, linenum, args[0], args[idx]);
856 err_code |= ERR_ALERT | ERR_FATAL;
857 goto out;
858 }
859 /* idx already points to next arg */
860 }
861 else if (strcmp(args[idx], "store") == 0) {
862 int type, err;
863 char *cw, *nw, *sa;
864
865 idx++;
866 nw = args[idx];
867 while (*nw) {
868 /* the "store" keyword supports a comma-separated list */
869 cw = nw;
870 sa = NULL; /* store arg */
871 while (*nw && *nw != ',') {
872 if (*nw == '(') {
873 *nw = 0;
874 sa = ++nw;
875 while (*nw != ')') {
876 if (!*nw) {
877 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
878 file, linenum, args[0], cw);
879 err_code |= ERR_ALERT | ERR_FATAL;
880 goto out;
881 }
882 nw++;
883 }
884 *nw = '\0';
885 }
886 nw++;
887 }
888 if (*nw)
889 *nw++ = '\0';
890 type = stktable_get_data_type(cw);
891 if (type < 0) {
892 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
893 file, linenum, args[0], cw);
894 err_code |= ERR_ALERT | ERR_FATAL;
895 goto out;
896 }
897
898 err = stktable_alloc_data_type(t, type, sa);
899 switch (err) {
900 case PE_NONE: break;
901 case PE_EXIST:
902 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
903 file, linenum, args[0], cw);
904 err_code |= ERR_WARN;
905 break;
906
907 case PE_ARG_MISSING:
908 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
909 file, linenum, args[0], cw);
910 err_code |= ERR_ALERT | ERR_FATAL;
911 goto out;
912
913 case PE_ARG_NOT_USED:
914 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
915 file, linenum, args[0], cw);
916 err_code |= ERR_ALERT | ERR_FATAL;
917 goto out;
918
919 default:
920 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
921 file, linenum, args[0], cw);
922 err_code |= ERR_ALERT | ERR_FATAL;
923 goto out;
924 }
925 }
926 idx++;
927 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700928 else if (strcmp(args[idx], "srvkey") == 0) {
929 char *keytype;
930 idx++;
931 keytype = args[idx];
932 if (strcmp(keytype, "name") == 0) {
933 t->server_key_type = STKTABLE_SRV_NAME;
934 }
935 else if (strcmp(keytype, "addr") == 0) {
936 t->server_key_type = STKTABLE_SRV_ADDR;
937 }
938 else {
939 ha_alert("parsing [%s:%d] : %s : unknown server key type '%s'.\n",
940 file, linenum, args[0], keytype);
941 err_code |= ERR_ALERT | ERR_FATAL;
942 goto out;
943
944 }
945 idx++;
946 }
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100947 else {
948 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
949 file, linenum, args[0], args[idx]);
950 err_code |= ERR_ALERT | ERR_FATAL;
951 goto out;
952 }
953 }
954
955 if (!t->size) {
956 ha_alert("parsing [%s:%d] : %s: missing size.\n",
957 file, linenum, args[0]);
958 err_code |= ERR_ALERT | ERR_FATAL;
959 goto out;
960 }
961
962 if (t->type == (unsigned int)-1) {
963 ha_alert("parsing [%s:%d] : %s: missing type.\n",
964 file, linenum, args[0]);
965 err_code |= ERR_ALERT | ERR_FATAL;
966 goto out;
967 }
968
969 out:
970 return err_code;
971}
972
Willy Tarreau8fed9032014-07-03 17:02:46 +0200973/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200974 * Note that the sample *is* modified and that the returned key may point
975 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200976 * Returns NULL if the sample could not be converted (eg: no matching type),
977 * otherwise a pointer to the static stktable_key filled with what is needed
978 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200979 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200980struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200981{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200982 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200983 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200984 return NULL;
985
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200986 /* Fill static_table_key. */
987 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200988
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200989 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +0200990 static_table_key.key = &smp->data.u.ipv4;
991 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200992 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200993
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200994 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +0200995 static_table_key.key = &smp->data.u.ipv6;
996 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200997 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200998
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200999 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001000 /* The stick table require a 32bit unsigned int, "sint" is a
1001 * signed 64 it, so we can convert it inplace.
1002 */
Willy Tarreau28c63c12019-10-23 06:21:05 +02001003 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +02001004 static_table_key.key = &smp->data.u.sint;
1005 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001006 break;
1007
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001008 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +02001009 if (!smp_make_safe(smp))
1010 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001011 static_table_key.key = smp->data.u.str.area;
1012 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001013 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001014
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001015 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001016 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001017 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +02001018 if (!smp_make_rw(smp))
1019 return NULL;
1020
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001021 if (smp->data.u.str.size < t->key_size)
1022 if (!smp_dup(smp))
1023 return NULL;
1024 if (smp->data.u.str.size < t->key_size)
1025 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
1027 t->key_size - smp->data.u.str.data);
1028 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +02001029 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001030 static_table_key.key = smp->data.u.str.area;
1031 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001032 break;
Emeric Brun485479d2010-09-23 18:02:19 +02001033
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001034 default: /* impossible case. */
1035 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +02001036 }
1037
Christopher Fauletca20d022017-08-29 15:30:31 +02001038 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001039}
1040
1041/*
Willy Tarreau8fed9032014-07-03 17:02:46 +02001042 * Process a fetch + format conversion as defined by the sample expression <expr>
1043 * on request or response considering the <opt> parameter. Returns either NULL if
1044 * no key could be extracted, or a pointer to the converted result stored in
1045 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
1046 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001047 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
1048 * without SMP_OPT_FINAL). The output will be usable like this :
1049 *
1050 * return MAY_CHANGE FINAL Meaning for the sample
1051 * NULL 0 * Not present and will never be (eg: header)
1052 * NULL 1 0 Not present or unstable, could change (eg: req_len)
1053 * NULL 1 1 Not present, will not change anymore
1054 * smp 0 * Present and will not change (eg: header)
1055 * smp 1 0 not possible
1056 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +02001057 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001058struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +02001059 unsigned int opt, struct sample_expr *expr, struct sample *smp)
1060{
1061 if (smp)
1062 memset(smp, 0, sizeof(*smp));
1063
Willy Tarreau192252e2015-04-04 01:47:55 +02001064 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +02001065 if (!smp)
1066 return NULL;
1067
1068 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1069 return NULL; /* we can only use stable samples */
1070
1071 return smp_to_stkey(smp, t);
1072}
1073
1074/*
Willy Tarreau12785782012-04-27 21:37:17 +02001075 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001076 * type <table_type>, otherwise zero. Used in configuration check.
1077 */
Willy Tarreau12785782012-04-27 21:37:17 +02001078int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001079{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001080 int out_type;
1081
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001082 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001083 return 0;
1084
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001085 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001086
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001087 /* Convert sample. */
1088 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001089 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001090
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001091 return 1;
1092}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001093
Willy Tarreauedee1d62014-07-15 16:44:27 +02001094/* Extra data types processing : after the last one, some room may remain
1095 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1096 * at run time.
1097 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001098struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001099 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001100 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001101 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001102 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001103 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1104 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1105 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
1106 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1107 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1108 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1109 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1110 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1111 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1112 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1113 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1114 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1115 [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 +01001116 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1117 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Thayne McCombs92149f92020-11-20 01:28:26 -07001118 [STKTABLE_DT_SERVER_KEY] = { .name = "server_key", .std_type = STD_T_DICT },
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001119 [STKTABLE_DT_HTTP_FAIL_CNT] = { .name = "http_fail_cnt", .std_type = STD_T_UINT },
1120 [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 +02001121};
1122
Willy Tarreauedee1d62014-07-15 16:44:27 +02001123/* Registers stick-table extra data type with index <idx>, name <name>, type
1124 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1125 * index is automatically allocated. The allocated index is returned, or -1 if
1126 * no free index was found or <name> was already registered. The <name> is used
1127 * directly as a pointer, so if it's not stable, the caller must allocate it.
1128 */
1129int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1130{
1131 if (idx < 0) {
1132 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1133 if (!stktable_data_types[idx].name)
1134 break;
1135
1136 if (strcmp(stktable_data_types[idx].name, name) == 0)
1137 return -1;
1138 }
1139 }
1140
1141 if (idx >= STKTABLE_DATA_TYPES)
1142 return -1;
1143
1144 if (stktable_data_types[idx].name != NULL)
1145 return -1;
1146
1147 stktable_data_types[idx].name = name;
1148 stktable_data_types[idx].std_type = std_type;
1149 stktable_data_types[idx].arg_type = arg_type;
1150 return idx;
1151}
1152
Willy Tarreau08d5f982010-06-06 13:34:54 +02001153/*
1154 * Returns the data type number for the stktable_data_type whose name is <name>,
1155 * or <0 if not found.
1156 */
1157int stktable_get_data_type(char *name)
1158{
1159 int type;
1160
1161 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001162 if (!stktable_data_types[type].name)
1163 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001164 if (strcmp(name, stktable_data_types[type].name) == 0)
1165 return type;
1166 }
Thayne McCombs92149f92020-11-20 01:28:26 -07001167 /* For backwards compatibility */
1168 if (strcmp(name, "server_name") == 0)
1169 return STKTABLE_DT_SERVER_KEY;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001170 return -1;
1171}
1172
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001173/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1174 * it up into this table. Returns true if found, false otherwise. The input
1175 * type is STR so that input samples are converted to string (since all types
1176 * can be converted to strings), then the function casts the string again into
1177 * the table's type. This is a double conversion, but in the future we might
1178 * support automatic input types to perform the cast on the fly.
1179 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001180static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001181{
1182 struct stktable *t;
1183 struct stktable_key *key;
1184 struct stksess *ts;
1185
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001186 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001187
1188 key = smp_to_stkey(smp, t);
1189 if (!key)
1190 return 0;
1191
1192 ts = stktable_lookup_key(t, key);
1193
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001194 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001195 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001196 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001197 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001198 return 1;
1199}
1200
1201/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1202 * it up into this table. Returns the data rate received from clients in bytes/s
1203 * if the key is present in the table, otherwise zero, so that comparisons can
1204 * be easily performed. If the inspected parameter is not stored in the table,
1205 * <not found> is returned.
1206 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001207static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001208{
1209 struct stktable *t;
1210 struct stktable_key *key;
1211 struct stksess *ts;
1212 void *ptr;
1213
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001214 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001215
1216 key = smp_to_stkey(smp, t);
1217 if (!key)
1218 return 0;
1219
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001220 ts = stktable_lookup_key(t, key);
1221
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001222 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001224 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001225
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001226 if (!ts) /* key not present */
1227 return 1;
1228
1229 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001230 if (ptr)
1231 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
1232 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001233
Daniel Corbett3e60b112018-05-27 09:47:12 -04001234 stktable_release(t, ts);
1235 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001236}
1237
1238/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1239 * it up into this table. Returns the cumulated number of connections for the key
1240 * if the key is present in the table, otherwise zero, so that comparisons can
1241 * be easily performed. If the inspected parameter is not stored in the table,
1242 * <not found> is returned.
1243 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001244static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001245{
1246 struct stktable *t;
1247 struct stktable_key *key;
1248 struct stksess *ts;
1249 void *ptr;
1250
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001251 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001252
1253 key = smp_to_stkey(smp, t);
1254 if (!key)
1255 return 0;
1256
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001257 ts = stktable_lookup_key(t, key);
1258
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001259 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001260 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001261 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001262
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001263 if (!ts) /* key not present */
1264 return 1;
1265
1266 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001267 if (ptr)
1268 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001269
Daniel Corbett3e60b112018-05-27 09:47:12 -04001270 stktable_release(t, ts);
1271 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001272}
1273
1274/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1275 * it up into this table. Returns the number of concurrent connections for the
1276 * key if the key is present in the table, otherwise zero, so that comparisons
1277 * can be easily performed. If the inspected parameter is not stored in the
1278 * table, <not found> is returned.
1279 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001280static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001281{
1282 struct stktable *t;
1283 struct stktable_key *key;
1284 struct stksess *ts;
1285 void *ptr;
1286
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001287 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001288
1289 key = smp_to_stkey(smp, t);
1290 if (!key)
1291 return 0;
1292
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001293 ts = stktable_lookup_key(t, key);
1294
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001295 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001296 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001297 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001298
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001299 if (!ts) /* key not present */
1300 return 1;
1301
1302 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001303 if (ptr)
1304 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001305
Daniel Corbett3e60b112018-05-27 09:47:12 -04001306 stktable_release(t, ts);
1307 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001308}
1309
1310/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1311 * it up into this table. Returns the rate of incoming connections from the key
1312 * if the key is present in the table, otherwise zero, so that comparisons can
1313 * be easily performed. If the inspected parameter is not stored in the table,
1314 * <not found> is returned.
1315 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001316static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001317{
1318 struct stktable *t;
1319 struct stktable_key *key;
1320 struct stksess *ts;
1321 void *ptr;
1322
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001323 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001324
1325 key = smp_to_stkey(smp, t);
1326 if (!key)
1327 return 0;
1328
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001329 ts = stktable_lookup_key(t, key);
1330
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001331 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001332 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001333 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001334
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001335 if (!ts) /* key not present */
1336 return 1;
1337
1338 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001339 if (ptr)
1340 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
1341 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001342
Daniel Corbett3e60b112018-05-27 09:47:12 -04001343 stktable_release(t, ts);
1344 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001345}
1346
1347/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1348 * it up into this table. Returns the data rate sent to clients in bytes/s
1349 * if the key is present in the table, otherwise zero, so that comparisons can
1350 * be easily performed. If the inspected parameter is not stored in the table,
1351 * <not found> is returned.
1352 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001353static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001354{
1355 struct stktable *t;
1356 struct stktable_key *key;
1357 struct stksess *ts;
1358 void *ptr;
1359
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001360 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001361
1362 key = smp_to_stkey(smp, t);
1363 if (!key)
1364 return 0;
1365
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001366 ts = stktable_lookup_key(t, key);
1367
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001368 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001369 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001370 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001371
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001372 if (!ts) /* key not present */
1373 return 1;
1374
1375 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001376 if (ptr)
1377 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
1378 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001379
Daniel Corbett3e60b112018-05-27 09:47:12 -04001380 stktable_release(t, ts);
1381 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001382}
1383
1384/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001385 * it up into this table. Returns the value of the GPT0 tag for the key
1386 * if the key is present in the table, otherwise false, so that comparisons can
1387 * be easily performed. If the inspected parameter is not stored in the table,
1388 * <not found> is returned.
1389 */
1390static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1391{
1392 struct stktable *t;
1393 struct stktable_key *key;
1394 struct stksess *ts;
1395 void *ptr;
1396
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001397 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001398
1399 key = smp_to_stkey(smp, t);
1400 if (!key)
1401 return 0;
1402
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001403 ts = stktable_lookup_key(t, key);
1404
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001405 smp->flags = SMP_F_VOL_TEST;
1406 smp->data.type = SMP_T_SINT;
1407 smp->data.u.sint = 0;
1408
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001409 if (!ts) /* key not present */
1410 return 1;
1411
1412 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001413 if (ptr)
1414 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001415
Daniel Corbett3e60b112018-05-27 09:47:12 -04001416 stktable_release(t, ts);
1417 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001418}
1419
1420/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001421 * it up into this table. Returns the value of the GPC0 counter for the key
1422 * if the key is present in the table, otherwise zero, so that comparisons can
1423 * be easily performed. If the inspected parameter is not stored in the table,
1424 * <not found> is returned.
1425 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001426static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001427{
1428 struct stktable *t;
1429 struct stktable_key *key;
1430 struct stksess *ts;
1431 void *ptr;
1432
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001433 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001434
1435 key = smp_to_stkey(smp, t);
1436 if (!key)
1437 return 0;
1438
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001439 ts = stktable_lookup_key(t, key);
1440
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001441 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001442 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001443 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001444
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001445 if (!ts) /* key not present */
1446 return 1;
1447
1448 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001449 if (ptr)
1450 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001451
Daniel Corbett3e60b112018-05-27 09:47:12 -04001452 stktable_release(t, ts);
1453 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001454}
1455
1456/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1457 * it up into this table. Returns the event rate of the GPC0 counter for the key
1458 * if the key is present in the table, otherwise zero, so that comparisons can
1459 * be easily performed. If the inspected parameter is not stored in the table,
1460 * <not found> is returned.
1461 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001462static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001463{
1464 struct stktable *t;
1465 struct stktable_key *key;
1466 struct stksess *ts;
1467 void *ptr;
1468
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001469 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001470
1471 key = smp_to_stkey(smp, t);
1472 if (!key)
1473 return 0;
1474
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001475 ts = stktable_lookup_key(t, key);
1476
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001477 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001478 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001479 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001480
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001481 if (!ts) /* key not present */
1482 return 1;
1483
1484 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001485 if (ptr)
1486 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1487 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001488
Daniel Corbett3e60b112018-05-27 09:47:12 -04001489 stktable_release(t, ts);
1490 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001491}
1492
1493/* 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 +01001494 * it up into this table. Returns the value of the GPC1 counter for the key
1495 * if the key is present in the table, otherwise zero, so that comparisons can
1496 * be easily performed. If the inspected parameter is not stored in the table,
1497 * <not found> is returned.
1498 */
1499static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1500{
1501 struct stktable *t;
1502 struct stktable_key *key;
1503 struct stksess *ts;
1504 void *ptr;
1505
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001506 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001507
1508 key = smp_to_stkey(smp, t);
1509 if (!key)
1510 return 0;
1511
1512 ts = stktable_lookup_key(t, key);
1513
1514 smp->flags = SMP_F_VOL_TEST;
1515 smp->data.type = SMP_T_SINT;
1516 smp->data.u.sint = 0;
1517
1518 if (!ts) /* key not present */
1519 return 1;
1520
1521 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001522 if (ptr)
1523 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001524
Daniel Corbett3e60b112018-05-27 09:47:12 -04001525 stktable_release(t, ts);
1526 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001527}
1528
1529/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1530 * it up into this table. Returns the event rate of the GPC1 counter for the key
1531 * if the key is present in the table, otherwise zero, so that comparisons can
1532 * be easily performed. If the inspected parameter is not stored in the table,
1533 * <not found> is returned.
1534 */
1535static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1536{
1537 struct stktable *t;
1538 struct stktable_key *key;
1539 struct stksess *ts;
1540 void *ptr;
1541
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001542 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001543
1544 key = smp_to_stkey(smp, t);
1545 if (!key)
1546 return 0;
1547
1548 ts = stktable_lookup_key(t, key);
1549
1550 smp->flags = SMP_F_VOL_TEST;
1551 smp->data.type = SMP_T_SINT;
1552 smp->data.u.sint = 0;
1553
1554 if (!ts) /* key not present */
1555 return 1;
1556
1557 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001558 if (ptr)
1559 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
1560 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001561
Daniel Corbett3e60b112018-05-27 09:47:12 -04001562 stktable_release(t, ts);
1563 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001564}
1565
1566/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001567 * it up into this table. Returns the cumulated number of HTTP request errors
1568 * for the key if the key is present in the table, otherwise zero, so that
1569 * comparisons can be easily performed. If the inspected parameter is not stored
1570 * in the table, <not found> is returned.
1571 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001572static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001573{
1574 struct stktable *t;
1575 struct stktable_key *key;
1576 struct stksess *ts;
1577 void *ptr;
1578
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001579 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001580
1581 key = smp_to_stkey(smp, t);
1582 if (!key)
1583 return 0;
1584
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001585 ts = stktable_lookup_key(t, key);
1586
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001587 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001588 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001589 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001590
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001591 if (!ts) /* key not present */
1592 return 1;
1593
1594 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001595 if (ptr)
1596 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001597
Daniel Corbett3e60b112018-05-27 09:47:12 -04001598 stktable_release(t, ts);
1599 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001600}
1601
1602/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1603 * it up into this table. Returns the HTTP request error rate the key
1604 * if the key is present in the table, otherwise zero, so that comparisons can
1605 * be easily performed. If the inspected parameter is not stored in the table,
1606 * <not found> is returned.
1607 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001608static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001609{
1610 struct stktable *t;
1611 struct stktable_key *key;
1612 struct stksess *ts;
1613 void *ptr;
1614
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001615 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001616
1617 key = smp_to_stkey(smp, t);
1618 if (!key)
1619 return 0;
1620
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001621 ts = stktable_lookup_key(t, key);
1622
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001623 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001624 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001625 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001626
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001627 if (!ts) /* key not present */
1628 return 1;
1629
1630 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001631 if (ptr)
1632 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
1633 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001634
Daniel Corbett3e60b112018-05-27 09:47:12 -04001635 stktable_release(t, ts);
1636 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001637}
1638
1639/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001640 * it up into this table. Returns the cumulated number of HTTP response failures
1641 * for the key if the key is present in the table, otherwise zero, so that
1642 * comparisons can be easily performed. If the inspected parameter is not stored
1643 * in the table, <not found> is returned.
1644 */
1645static int sample_conv_table_http_fail_cnt(const struct arg *arg_p, struct sample *smp, void *private)
1646{
1647 struct stktable *t;
1648 struct stktable_key *key;
1649 struct stksess *ts;
1650 void *ptr;
1651
1652 t = arg_p[0].data.t;
1653
1654 key = smp_to_stkey(smp, t);
1655 if (!key)
1656 return 0;
1657
1658 ts = stktable_lookup_key(t, key);
1659
1660 smp->flags = SMP_F_VOL_TEST;
1661 smp->data.type = SMP_T_SINT;
1662 smp->data.u.sint = 0;
1663
1664 if (!ts) /* key not present */
1665 return 1;
1666
1667 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_CNT);
1668 if (ptr)
1669 smp->data.u.sint = stktable_data_cast(ptr, http_fail_cnt);
1670
1671 stktable_release(t, ts);
1672 return !!ptr;
1673}
1674
1675/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1676 * it up into this table. Returns the HTTP response failure rate for the key
1677 * if the key is present in the table, otherwise zero, so that comparisons can
1678 * be easily performed. If the inspected parameter is not stored in the table,
1679 * <not found> is returned.
1680 */
1681static int sample_conv_table_http_fail_rate(const struct arg *arg_p, struct sample *smp, void *private)
1682{
1683 struct stktable *t;
1684 struct stktable_key *key;
1685 struct stksess *ts;
1686 void *ptr;
1687
1688 t = arg_p[0].data.t;
1689
1690 key = smp_to_stkey(smp, t);
1691 if (!key)
1692 return 0;
1693
1694 ts = stktable_lookup_key(t, key);
1695
1696 smp->flags = SMP_F_VOL_TEST;
1697 smp->data.type = SMP_T_SINT;
1698 smp->data.u.sint = 0;
1699
1700 if (!ts) /* key not present */
1701 return 1;
1702
1703 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_FAIL_RATE);
1704 if (ptr)
1705 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_fail_rate),
1706 t->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
1707
1708 stktable_release(t, ts);
1709 return !!ptr;
1710}
1711
1712/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001713 * it up into this table. Returns the cumulated number of HTTP request for the
1714 * key if the key is present in the table, otherwise zero, so that comparisons
1715 * can be easily performed. If the inspected parameter is not stored in the
1716 * table, <not found> is returned.
1717 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001718static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001719{
1720 struct stktable *t;
1721 struct stktable_key *key;
1722 struct stksess *ts;
1723 void *ptr;
1724
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001725 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001726
1727 key = smp_to_stkey(smp, t);
1728 if (!key)
1729 return 0;
1730
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001731 ts = stktable_lookup_key(t, key);
1732
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001733 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001734 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001735 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001736
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001737 if (!ts) /* key not present */
1738 return 1;
1739
1740 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001741 if (ptr)
1742 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001743
Daniel Corbett3e60b112018-05-27 09:47:12 -04001744 stktable_release(t, ts);
1745 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001746}
1747
1748/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1749 * it up into this table. Returns the HTTP request rate the key if the key is
1750 * present in the table, otherwise zero, so that comparisons can be easily
1751 * performed. If the inspected parameter is not stored in the table, <not found>
1752 * is returned.
1753 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001754static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001755{
1756 struct stktable *t;
1757 struct stktable_key *key;
1758 struct stksess *ts;
1759 void *ptr;
1760
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001761 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001762
1763 key = smp_to_stkey(smp, t);
1764 if (!key)
1765 return 0;
1766
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001767 ts = stktable_lookup_key(t, key);
1768
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001769 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001770 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001771 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001772
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001773 if (!ts) /* key not present */
1774 return 1;
1775
1776 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001777 if (ptr)
1778 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
1779 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001780
Daniel Corbett3e60b112018-05-27 09:47:12 -04001781 stktable_release(t, ts);
1782 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001783}
1784
1785/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1786 * it up into this table. Returns the volume of datareceived from clients in kbytes
1787 * if the key is present in the table, otherwise zero, so that comparisons can
1788 * be easily performed. If the inspected parameter is not stored in the table,
1789 * <not found> is returned.
1790 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001791static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001792{
1793 struct stktable *t;
1794 struct stktable_key *key;
1795 struct stksess *ts;
1796 void *ptr;
1797
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001798 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001799
1800 key = smp_to_stkey(smp, t);
1801 if (!key)
1802 return 0;
1803
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001804 ts = stktable_lookup_key(t, key);
1805
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001806 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001807 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001808 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001809
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001810 if (!ts) /* key not present */
1811 return 1;
1812
1813 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001814 if (ptr)
1815 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001816
Daniel Corbett3e60b112018-05-27 09:47:12 -04001817 stktable_release(t, ts);
1818 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001819}
1820
1821/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1822 * it up into this table. Returns the volume of data sent to clients in kbytes
1823 * if the key is present in the table, otherwise zero, so that comparisons can
1824 * be easily performed. If the inspected parameter is not stored in the table,
1825 * <not found> is returned.
1826 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001827static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001828{
1829 struct stktable *t;
1830 struct stktable_key *key;
1831 struct stksess *ts;
1832 void *ptr;
1833
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001834 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001835
1836 key = smp_to_stkey(smp, t);
1837 if (!key)
1838 return 0;
1839
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001840 ts = stktable_lookup_key(t, key);
1841
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001842 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001843 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001844 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001845
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001846 if (!ts) /* key not present */
1847 return 1;
1848
1849 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001850 if (ptr)
1851 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001852
Daniel Corbett3e60b112018-05-27 09:47:12 -04001853 stktable_release(t, ts);
1854 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001855}
1856
1857/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1858 * it up into this table. Returns the server ID associated with the key if the
1859 * key is present in the table, otherwise zero, so that comparisons can be
1860 * easily performed. If the inspected parameter is not stored in the table,
1861 * <not found> is returned.
1862 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001863static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001864{
1865 struct stktable *t;
1866 struct stktable_key *key;
1867 struct stksess *ts;
1868 void *ptr;
1869
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001870 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001871
1872 key = smp_to_stkey(smp, t);
1873 if (!key)
1874 return 0;
1875
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001876 ts = stktable_lookup_key(t, key);
1877
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001878 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001879 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001880 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001881
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001882 if (!ts) /* key not present */
1883 return 1;
1884
1885 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001886 if (ptr)
1887 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001888
Daniel Corbett3e60b112018-05-27 09:47:12 -04001889 stktable_release(t, ts);
1890 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001891}
1892
1893/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1894 * it up into this table. Returns the cumulated number of sessions for the
1895 * key if the key is present in the table, otherwise zero, so that comparisons
1896 * can be easily performed. If the inspected parameter is not stored in the
1897 * table, <not found> is returned.
1898 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001899static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001900{
1901 struct stktable *t;
1902 struct stktable_key *key;
1903 struct stksess *ts;
1904 void *ptr;
1905
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001906 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001907
1908 key = smp_to_stkey(smp, t);
1909 if (!key)
1910 return 0;
1911
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001912 ts = stktable_lookup_key(t, key);
1913
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001914 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001915 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001916 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001917
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001918 if (!ts) /* key not present */
1919 return 1;
1920
1921 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001922 if (ptr)
1923 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001924
Daniel Corbett3e60b112018-05-27 09:47:12 -04001925 stktable_release(t, ts);
1926 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001927}
1928
1929/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1930 * it up into this table. Returns the session rate the key if the key is
1931 * present in the table, otherwise zero, so that comparisons can be easily
1932 * performed. If the inspected parameter is not stored in the table, <not found>
1933 * is returned.
1934 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001935static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001936{
1937 struct stktable *t;
1938 struct stktable_key *key;
1939 struct stksess *ts;
1940 void *ptr;
1941
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001942 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001943
1944 key = smp_to_stkey(smp, t);
1945 if (!key)
1946 return 0;
1947
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001948 ts = stktable_lookup_key(t, key);
1949
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001950 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001951 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001952 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001953
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001954 if (!ts) /* key not present */
1955 return 1;
1956
1957 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001958 if (ptr)
1959 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
1960 t->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001961
Daniel Corbett3e60b112018-05-27 09:47:12 -04001962 stktable_release(t, ts);
1963 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001964}
1965
1966/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1967 * it up into this table. Returns the amount of concurrent connections tracking
1968 * the same key if the key is present in the table, otherwise zero, so that
1969 * comparisons can be easily performed. If the inspected parameter is not
1970 * stored in the table, <not found> is returned.
1971 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001972static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001973{
1974 struct stktable *t;
1975 struct stktable_key *key;
1976 struct stksess *ts;
1977
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001978 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001979
1980 key = smp_to_stkey(smp, t);
1981 if (!key)
1982 return 0;
1983
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001984 ts = stktable_lookup_key(t, key);
1985
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001986 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001987 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001988 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001989
Tim Duesterhus65189c12018-06-26 15:57:29 +02001990 if (!ts)
1991 return 1;
1992
1993 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001994
Daniel Corbett3e60b112018-05-27 09:47:12 -04001995 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001996 return 1;
1997}
1998
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001999/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002000static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002001 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002002{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002003 struct stksess *ts;
2004 struct stkctr *stkctr;
2005
2006 /* Extract the stksess, return OK if no stksess available. */
2007 if (s)
2008 stkctr = &s->stkctr[rule->arg.gpc.sc];
2009 else
2010 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002011
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002012 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002013 if (ts) {
2014 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002015
Willy Tarreau79c1e912016-01-25 14:54:45 +01002016 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
2017 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002018 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
2019 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002020 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002021
2022 if (ptr1)
2023 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau79c1e912016-01-25 14:54:45 +01002024 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002025
Emeric Brun819fc6f2017-06-13 19:37:32 +02002026 if (ptr2)
2027 stktable_data_cast(ptr2, gpc0)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01002028
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002029 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002030
2031 /* If data was modified, we need to touch to re-schedule sync */
2032 stktable_touch_local(stkctr->table, ts, 0);
2033 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01002034 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002035 return ACT_RET_CONT;
2036}
2037
2038/* This function is a common parser for using variables. It understands
2039 * the formats:
2040 *
2041 * sc-inc-gpc0(<stick-table ID>)
2042 *
2043 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2044 * it returns 1 and the variable <expr> is filled with the pointer to the
2045 * expression to execute.
2046 */
2047static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
2048 struct act_rule *rule, char **err)
2049{
2050 const char *cmd_name = args[*arg-1];
2051 char *error;
2052
2053 cmd_name += strlen("sc-inc-gpc0");
2054 if (*cmd_name == '\0') {
2055 /* default stick table id. */
2056 rule->arg.gpc.sc = 0;
2057 } else {
2058 /* parse the stick table id. */
2059 if (*cmd_name != '(') {
2060 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2061 return ACT_RET_PRS_ERR;
2062 }
2063 cmd_name++; /* jump the '(' */
2064 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2065 if (*error != ')') {
2066 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2067 return ACT_RET_PRS_ERR;
2068 }
2069
Christopher Faulet28436e22019-12-18 10:25:46 +01002070 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002071 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002072 MAX_SESS_STKCTR-1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002073 return ACT_RET_PRS_ERR;
2074 }
2075 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02002076 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002077 rule->action_ptr = action_inc_gpc0;
2078 return ACT_RET_PRS_OK;
2079}
2080
2081/* Always returns 1. */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002082static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
2083 struct session *sess, struct stream *s, int flags)
2084{
2085 struct stksess *ts;
2086 struct stkctr *stkctr;
2087
2088 /* Extract the stksess, return OK if no stksess available. */
2089 if (s)
2090 stkctr = &s->stkctr[rule->arg.gpc.sc];
2091 else
2092 stkctr = &sess->stkctr[rule->arg.gpc.sc];
2093
2094 ts = stkctr_entry(stkctr);
2095 if (ts) {
2096 void *ptr1, *ptr2;
2097
2098 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
2099 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
2100 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
2101 if (ptr1 || ptr2) {
2102 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
2103
2104 if (ptr1)
2105 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
2106 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
2107
2108 if (ptr2)
2109 stktable_data_cast(ptr2, gpc1)++;
2110
2111 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2112
2113 /* If data was modified, we need to touch to re-schedule sync */
2114 stktable_touch_local(stkctr->table, ts, 0);
2115 }
2116 }
2117 return ACT_RET_CONT;
2118}
2119
2120/* This function is a common parser for using variables. It understands
2121 * the formats:
2122 *
2123 * sc-inc-gpc1(<stick-table ID>)
2124 *
2125 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2126 * it returns 1 and the variable <expr> is filled with the pointer to the
2127 * expression to execute.
2128 */
2129static enum act_parse_ret parse_inc_gpc1(const char **args, int *arg, struct proxy *px,
2130 struct act_rule *rule, char **err)
2131{
2132 const char *cmd_name = args[*arg-1];
2133 char *error;
2134
2135 cmd_name += strlen("sc-inc-gpc1");
2136 if (*cmd_name == '\0') {
2137 /* default stick table id. */
2138 rule->arg.gpc.sc = 0;
2139 } else {
2140 /* parse the stick table id. */
2141 if (*cmd_name != '(') {
2142 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2143 return ACT_RET_PRS_ERR;
2144 }
2145 cmd_name++; /* jump the '(' */
2146 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2147 if (*error != ')') {
2148 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2149 return ACT_RET_PRS_ERR;
2150 }
2151
Christopher Faulet28436e22019-12-18 10:25:46 +01002152 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002153 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002154 MAX_SESS_STKCTR-1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002155 return ACT_RET_PRS_ERR;
2156 }
2157 }
2158 rule->action = ACT_CUSTOM;
2159 rule->action_ptr = action_inc_gpc1;
2160 return ACT_RET_PRS_OK;
2161}
2162
2163/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002164static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002165 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002166{
2167 void *ptr;
2168 struct stksess *ts;
2169 struct stkctr *stkctr;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002170 unsigned int value = 0;
2171 struct sample *smp;
2172 int smp_opt_dir;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002173
2174 /* Extract the stksess, return OK if no stksess available. */
2175 if (s)
2176 stkctr = &s->stkctr[rule->arg.gpt.sc];
2177 else
2178 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002179
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002180 ts = stkctr_entry(stkctr);
2181 if (!ts)
2182 return ACT_RET_CONT;
2183
2184 /* Store the sample in the required sc, and ignore errors. */
2185 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002186 if (ptr) {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002187 if (!rule->arg.gpt.expr)
2188 value = (unsigned int)(rule->arg.gpt.value);
2189 else {
2190 switch (rule->from) {
2191 case ACT_F_TCP_REQ_SES: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2192 case ACT_F_TCP_REQ_CNT: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2193 case ACT_F_TCP_RES_CNT: smp_opt_dir = SMP_OPT_DIR_RES; break;
2194 case ACT_F_HTTP_REQ: smp_opt_dir = SMP_OPT_DIR_REQ; break;
2195 case ACT_F_HTTP_RES: smp_opt_dir = SMP_OPT_DIR_RES; break;
2196 default:
2197 send_log(px, LOG_ERR, "stick table: internal error while setting gpt0.");
2198 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2199 ha_alert("stick table: internal error while executing setting gpt0.\n");
2200 return ACT_RET_CONT;
2201 }
2202
2203 /* Fetch and cast the expression. */
2204 smp = sample_fetch_as_type(px, sess, s, smp_opt_dir|SMP_OPT_FINAL, rule->arg.gpt.expr, SMP_T_SINT);
2205 if (!smp) {
2206 send_log(px, LOG_WARNING, "stick table: invalid expression or data type while setting gpt0.");
2207 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))
2208 ha_alert("stick table: invalid expression or data type while setting gpt0.\n");
2209 return ACT_RET_CONT;
2210 }
2211 value = (unsigned int)(smp->data.u.sint);
2212 }
2213
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002214 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002215
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002216 stktable_data_cast(ptr, gpt0) = value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002217
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002218 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002219
2220 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002221 }
2222
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002223 return ACT_RET_CONT;
2224}
2225
2226/* This function is a common parser for using variables. It understands
2227 * the format:
2228 *
2229 * set-gpt0(<stick-table ID>) <expression>
2230 *
2231 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2232 * it returns 1 and the variable <expr> is filled with the pointer to the
2233 * expression to execute.
2234 */
2235static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
2236 struct act_rule *rule, char **err)
2237
2238
2239{
2240 const char *cmd_name = args[*arg-1];
2241 char *error;
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002242 int smp_val;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002243
2244 cmd_name += strlen("sc-set-gpt0");
2245 if (*cmd_name == '\0') {
2246 /* default stick table id. */
2247 rule->arg.gpt.sc = 0;
2248 } else {
2249 /* parse the stick table id. */
2250 if (*cmd_name != '(') {
2251 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2252 return ACT_RET_PRS_ERR;
2253 }
2254 cmd_name++; /* jump the '(' */
2255 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2256 if (*error != ')') {
2257 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2258 return ACT_RET_PRS_ERR;
2259 }
2260
Christopher Faulet28436e22019-12-18 10:25:46 +01002261 if (rule->arg.gpt.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002262 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Christopher Faulet28436e22019-12-18 10:25:46 +01002263 args[*arg-1], MAX_SESS_STKCTR-1);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002264 return ACT_RET_PRS_ERR;
2265 }
2266 }
2267
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002268 rule->arg.gpt.expr = NULL;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002269 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
2270 if (*error != '\0') {
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002271 rule->arg.gpt.expr = sample_parse_expr((char **)args, arg, px->conf.args.file,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002272 px->conf.args.line, err, &px->conf.args, NULL);
Cédric Dufour0d7712d2019-11-06 18:38:53 +01002273 if (!rule->arg.gpt.expr)
2274 return ACT_RET_PRS_ERR;
2275
2276 switch (rule->from) {
2277 case ACT_F_TCP_REQ_SES: smp_val = SMP_VAL_FE_SES_ACC; break;
2278 case ACT_F_TCP_REQ_CNT: smp_val = SMP_VAL_FE_REQ_CNT; break;
2279 case ACT_F_TCP_RES_CNT: smp_val = SMP_VAL_BE_RES_CNT; break;
2280 case ACT_F_HTTP_REQ: smp_val = SMP_VAL_FE_HRQ_HDR; break;
2281 case ACT_F_HTTP_RES: smp_val = SMP_VAL_BE_HRS_HDR; break;
2282 default:
2283 memprintf(err, "internal error, unexpected rule->from=%d, please report this bug!", rule->from);
2284 return ACT_RET_PRS_ERR;
2285 }
2286 if (!(rule->arg.gpt.expr->fetch->val & smp_val)) {
2287 memprintf(err, "fetch method '%s' extracts information from '%s', none of which is available here", args[*arg-1],
2288 sample_src_names(rule->arg.gpt.expr->fetch->use));
2289 free(rule->arg.gpt.expr);
2290 return ACT_RET_PRS_ERR;
2291 }
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002292 }
2293 (*arg)++;
2294
Thierry FOURNIER42148732015-09-02 17:17:33 +02002295 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002296 rule->action_ptr = action_set_gpt0;
2297
2298 return ACT_RET_PRS_OK;
2299}
2300
Willy Tarreau7d562212016-11-25 16:10:05 +01002301/* set temp integer to the number of used entries in the table pointed to by expr.
2302 * Accepts exactly 1 argument of type table.
2303 */
2304static int
2305smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2306{
2307 smp->flags = SMP_F_VOL_TEST;
2308 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002309 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002310 return 1;
2311}
2312
2313/* set temp integer to the number of free entries in the table pointed to by expr.
2314 * Accepts exactly 1 argument of type table.
2315 */
2316static int
2317smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
2318{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002319 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002320
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002321 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002322 smp->flags = SMP_F_VOL_TEST;
2323 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002324 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002325 return 1;
2326}
2327
2328/* Returns a pointer to a stkctr depending on the fetch keyword name.
2329 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
2330 * sc[0-9]_* will return a pointer to the respective field in the
2331 * stream <l4>. sc_* requires an UINT argument specifying the stick
2332 * counter number. src_* will fill a locally allocated structure with
2333 * the table and entry corresponding to what is specified with src_*.
2334 * NULL may be returned if the designated stkctr is not tracked. For
2335 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2336 * passed. When present, the currently tracked key is then looked up
2337 * in the specified table instead of the current table. The purpose is
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002338 * to be able to convert multiple values per key (eg: have gpc0 from
Willy Tarreau7d562212016-11-25 16:10:05 +01002339 * multiple tables). <strm> is allowed to be NULL, in which case only
2340 * the session will be consulted.
2341 */
2342struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002343smp_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 +01002344{
Willy Tarreau7d562212016-11-25 16:10:05 +01002345 struct stkctr *stkptr;
2346 struct stksess *stksess;
2347 unsigned int num = kw[2] - '0';
2348 int arg = 0;
2349
2350 if (num == '_' - '0') {
2351 /* sc_* variant, args[0] = ctr# (mandatory) */
2352 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01002353 }
2354 else if (num > 9) { /* src_* variant, args[0] = table */
2355 struct stktable_key *key;
2356 struct connection *conn = objt_conn(sess->origin);
2357 struct sample smp;
2358
2359 if (!conn)
2360 return NULL;
2361
Joseph Herlant5662fa42018-11-15 13:43:28 -08002362 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002363 smp.px = NULL;
2364 smp.sess = sess;
2365 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02002366 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002367 return NULL;
2368
2369 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002370 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002371 if (!key)
2372 return NULL;
2373
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002374 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002375 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
2376 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002377 }
2378
2379 /* Here, <num> contains the counter number from 0 to 9 for
2380 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2381 * args[arg] is the first optional argument. We first lookup the
2382 * ctr form the stream, then from the session if it was not there.
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02002383 * But we must be sure the counter does not exceed MAX_SESS_STKCTR.
Willy Tarreau7d562212016-11-25 16:10:05 +01002384 */
Christopher Fauleta9fa88a2019-10-21 10:53:34 +02002385 if (num >= MAX_SESS_STKCTR)
2386 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01002387
2388 if (strm)
2389 stkptr = &strm->stkctr[num];
2390 if (!strm || !stkctr_entry(stkptr)) {
2391 stkptr = &sess->stkctr[num];
2392 if (!stkctr_entry(stkptr))
2393 return NULL;
2394 }
2395
2396 stksess = stkctr_entry(stkptr);
2397 if (!stksess)
2398 return NULL;
2399
2400 if (unlikely(args[arg].type == ARGT_TAB)) {
2401 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002402 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002403 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
2404 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002405 }
2406 return stkptr;
2407}
2408
2409/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2410 * the entry if it doesn't exist yet. This is needed for a few fetch
2411 * functions which need to create an entry, such as src_inc_gpc* and
2412 * src_clr_gpc*.
2413 */
2414struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002415smp_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 +01002416{
Willy Tarreau7d562212016-11-25 16:10:05 +01002417 struct stktable_key *key;
2418 struct connection *conn = objt_conn(sess->origin);
2419 struct sample smp;
2420
2421 if (strncmp(kw, "src_", 4) != 0)
2422 return NULL;
2423
2424 if (!conn)
2425 return NULL;
2426
Joseph Herlant5662fa42018-11-15 13:43:28 -08002427 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002428 smp.px = NULL;
2429 smp.sess = sess;
2430 smp.strm = strm;
Amaury Denoyellec460c702021-05-12 10:17:47 +02002431 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, &smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002432 return NULL;
2433
2434 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002435 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002436 if (!key)
2437 return NULL;
2438
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002439 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002440 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
2441 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002442}
2443
2444/* set return a boolean indicating if the requested stream counter is
2445 * currently being tracked or not.
2446 * Supports being called as "sc[0-9]_tracked" only.
2447 */
2448static int
2449smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2450{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002451 struct stkctr tmpstkctr;
2452 struct stkctr *stkctr;
2453
Willy Tarreau7d562212016-11-25 16:10:05 +01002454 smp->flags = SMP_F_VOL_TEST;
2455 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002456 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2457 smp->data.u.sint = !!stkctr;
2458
2459 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02002460 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02002461 stktable_release(stkctr->table, stkctr_entry(stkctr));
2462
Willy Tarreau7d562212016-11-25 16:10:05 +01002463 return 1;
2464}
2465
2466/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2467 * frontend counters or from the src.
2468 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2469 * zero is returned if the key is new.
2470 */
2471static int
2472smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2473{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002474 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002475 struct stkctr *stkctr;
2476
Emeric Brun819fc6f2017-06-13 19:37:32 +02002477 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002478 if (!stkctr)
2479 return 0;
2480
2481 smp->flags = SMP_F_VOL_TEST;
2482 smp->data.type = SMP_T_SINT;
2483 smp->data.u.sint = 0;
2484
Emeric Brun819fc6f2017-06-13 19:37:32 +02002485 if (stkctr_entry(stkctr)) {
2486 void *ptr;
2487
2488 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2489 if (!ptr) {
2490 if (stkctr == &tmpstkctr)
2491 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002492 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002493 }
2494
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002495 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002496
Willy Tarreau7d562212016-11-25 16:10:05 +01002497 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002498
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002499 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002500
2501 if (stkctr == &tmpstkctr)
2502 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002503 }
2504 return 1;
2505}
2506
2507/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
2508 * frontend counters or from the src.
2509 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2510 * zero is returned if the key is new.
2511 */
2512static int
2513smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2514{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002515 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002516 struct stkctr *stkctr;
2517
Emeric Brun819fc6f2017-06-13 19:37:32 +02002518 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002519 if (!stkctr)
2520 return 0;
2521
2522 smp->flags = SMP_F_VOL_TEST;
2523 smp->data.type = SMP_T_SINT;
2524 smp->data.u.sint = 0;
2525
2526 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002527 void *ptr;
2528
2529 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2530 if (!ptr) {
2531 if (stkctr == &tmpstkctr)
2532 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002533 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002534 }
2535
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002536 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002537
Willy Tarreau7d562212016-11-25 16:10:05 +01002538 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002539
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002540 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002541
2542 if (stkctr == &tmpstkctr)
2543 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002544 }
2545 return 1;
2546}
2547
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002548/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
2549 * frontend counters or from the src.
2550 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
2551 * zero is returned if the key is new.
2552 */
2553static int
2554smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2555{
2556 struct stkctr tmpstkctr;
2557 struct stkctr *stkctr;
2558
2559 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2560 if (!stkctr)
2561 return 0;
2562
2563 smp->flags = SMP_F_VOL_TEST;
2564 smp->data.type = SMP_T_SINT;
2565 smp->data.u.sint = 0;
2566
2567 if (stkctr_entry(stkctr) != NULL) {
2568 void *ptr;
2569
2570 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2571 if (!ptr) {
2572 if (stkctr == &tmpstkctr)
2573 stktable_release(stkctr->table, stkctr_entry(stkctr));
2574 return 0; /* parameter not stored */
2575 }
2576
2577 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2578
2579 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2580
2581 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2582
2583 if (stkctr == &tmpstkctr)
2584 stktable_release(stkctr->table, stkctr_entry(stkctr));
2585 }
2586 return 1;
2587}
2588
Willy Tarreau7d562212016-11-25 16:10:05 +01002589/* set <smp> to the General Purpose Counter 0's event rate from the stream's
2590 * tracked frontend counters or from the src.
2591 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2592 * Value zero is returned if the key is new.
2593 */
2594static int
2595smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2596{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002597 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002598 struct stkctr *stkctr;
2599
Emeric Brun819fc6f2017-06-13 19:37:32 +02002600 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002601 if (!stkctr)
2602 return 0;
2603
2604 smp->flags = SMP_F_VOL_TEST;
2605 smp->data.type = SMP_T_SINT;
2606 smp->data.u.sint = 0;
2607 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002608 void *ptr;
2609
2610 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2611 if (!ptr) {
2612 if (stkctr == &tmpstkctr)
2613 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002614 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002615 }
2616
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002618
Willy Tarreau7d562212016-11-25 16:10:05 +01002619 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
2620 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002621
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002622 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002623
2624 if (stkctr == &tmpstkctr)
2625 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002626 }
2627 return 1;
2628}
2629
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002630/* set <smp> to the General Purpose Counter 1's event rate from the stream's
2631 * tracked frontend counters or from the src.
2632 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
2633 * Value zero is returned if the key is new.
2634 */
2635static int
2636smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2637{
2638 struct stkctr tmpstkctr;
2639 struct stkctr *stkctr;
2640
2641 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2642 if (!stkctr)
2643 return 0;
2644
2645 smp->flags = SMP_F_VOL_TEST;
2646 smp->data.type = SMP_T_SINT;
2647 smp->data.u.sint = 0;
2648 if (stkctr_entry(stkctr) != NULL) {
2649 void *ptr;
2650
2651 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2652 if (!ptr) {
2653 if (stkctr == &tmpstkctr)
2654 stktable_release(stkctr->table, stkctr_entry(stkctr));
2655 return 0; /* parameter not stored */
2656 }
2657
2658 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2659
2660 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
2661 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u);
2662
2663 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2664
2665 if (stkctr == &tmpstkctr)
2666 stktable_release(stkctr->table, stkctr_entry(stkctr));
2667 }
2668 return 1;
2669}
2670
Willy Tarreau7d562212016-11-25 16:10:05 +01002671/* Increment the General Purpose Counter 0 value from the stream's tracked
2672 * frontend counters and return it into temp integer.
2673 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
2674 */
2675static int
2676smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2677{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002678 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002679 struct stkctr *stkctr;
2680
Emeric Brun819fc6f2017-06-13 19:37:32 +02002681 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002682 if (!stkctr)
2683 return 0;
2684
2685 smp->flags = SMP_F_VOL_TEST;
2686 smp->data.type = SMP_T_SINT;
2687 smp->data.u.sint = 0;
2688
Emeric Brun819fc6f2017-06-13 19:37:32 +02002689 if (!stkctr_entry(stkctr))
2690 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002691
2692 if (stkctr && stkctr_entry(stkctr)) {
2693 void *ptr1,*ptr2;
2694
Emeric Brun819fc6f2017-06-13 19:37:32 +02002695
Willy Tarreau7d562212016-11-25 16:10:05 +01002696 /* First, update gpc0_rate if it's tracked. Second, update its
2697 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2698 */
2699 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreau7d562212016-11-25 16:10:05 +01002700 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002701 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002702 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01002703
Emeric Brun819fc6f2017-06-13 19:37:32 +02002704 if (ptr1) {
2705 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
2706 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
2707 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
2708 }
2709
2710 if (ptr2)
2711 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
2712
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002713 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002714
2715 /* If data was modified, we need to touch to re-schedule sync */
2716 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2717 }
2718 else if (stkctr == &tmpstkctr)
2719 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002720 }
2721 return 1;
2722}
2723
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002724/* Increment the General Purpose Counter 1 value from the stream's tracked
2725 * frontend counters and return it into temp integer.
2726 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
2727 */
2728static int
2729smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2730{
2731 struct stkctr tmpstkctr;
2732 struct stkctr *stkctr;
2733
2734 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2735 if (!stkctr)
2736 return 0;
2737
2738 smp->flags = SMP_F_VOL_TEST;
2739 smp->data.type = SMP_T_SINT;
2740 smp->data.u.sint = 0;
2741
2742 if (!stkctr_entry(stkctr))
2743 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2744
2745 if (stkctr && stkctr_entry(stkctr)) {
2746 void *ptr1,*ptr2;
2747
2748
2749 /* First, update gpc1_rate if it's tracked. Second, update its
2750 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
2751 */
2752 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2753 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2754 if (ptr1 || ptr2) {
2755 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2756
2757 if (ptr1) {
2758 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
2759 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
2760 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc1_rate))->curr_ctr;
2761 }
2762
2763 if (ptr2)
2764 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc1);
2765
2766 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2767
2768 /* If data was modified, we need to touch to re-schedule sync */
2769 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2770 }
2771 else if (stkctr == &tmpstkctr)
2772 stktable_release(stkctr->table, stkctr_entry(stkctr));
2773 }
2774 return 1;
2775}
2776
Willy Tarreau7d562212016-11-25 16:10:05 +01002777/* Clear the General Purpose Counter 0 value from the stream's tracked
2778 * frontend counters and return its previous value into temp integer.
2779 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
2780 */
2781static int
2782smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2783{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002784 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002785 struct stkctr *stkctr;
2786
Emeric Brun819fc6f2017-06-13 19:37:32 +02002787 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002788 if (!stkctr)
2789 return 0;
2790
2791 smp->flags = SMP_F_VOL_TEST;
2792 smp->data.type = SMP_T_SINT;
2793 smp->data.u.sint = 0;
2794
Emeric Brun819fc6f2017-06-13 19:37:32 +02002795 if (!stkctr_entry(stkctr))
2796 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002797
Emeric Brun819fc6f2017-06-13 19:37:32 +02002798 if (stkctr && stkctr_entry(stkctr)) {
2799 void *ptr;
2800
2801 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2802 if (!ptr) {
2803 if (stkctr == &tmpstkctr)
2804 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002805 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002806 }
2807
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002808 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002809
Willy Tarreau7d562212016-11-25 16:10:05 +01002810 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
2811 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002812
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002813 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002814
Willy Tarreau7d562212016-11-25 16:10:05 +01002815 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002816 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01002817 }
2818 return 1;
2819}
2820
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002821/* Clear the General Purpose Counter 1 value from the stream's tracked
2822 * frontend counters and return its previous value into temp integer.
2823 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
2824 */
2825static int
2826smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2827{
2828 struct stkctr tmpstkctr;
2829 struct stkctr *stkctr;
2830
2831 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2832 if (!stkctr)
2833 return 0;
2834
2835 smp->flags = SMP_F_VOL_TEST;
2836 smp->data.type = SMP_T_SINT;
2837 smp->data.u.sint = 0;
2838
2839 if (!stkctr_entry(stkctr))
2840 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2841
2842 if (stkctr && stkctr_entry(stkctr)) {
2843 void *ptr;
2844
2845 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2846 if (!ptr) {
2847 if (stkctr == &tmpstkctr)
2848 stktable_release(stkctr->table, stkctr_entry(stkctr));
2849 return 0; /* parameter not stored */
2850 }
2851
2852 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2853
2854 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2855 stktable_data_cast(ptr, gpc1) = 0;
2856
2857 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2858
2859 /* If data was modified, we need to touch to re-schedule sync */
2860 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2861 }
2862 return 1;
2863}
2864
Willy Tarreau7d562212016-11-25 16:10:05 +01002865/* set <smp> to the cumulated number of connections from the stream's tracked
2866 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2867 * "src_conn_cnt" only.
2868 */
2869static int
2870smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2871{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002872 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002873 struct stkctr *stkctr;
2874
Emeric Brun819fc6f2017-06-13 19:37:32 +02002875 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002876 if (!stkctr)
2877 return 0;
2878
2879 smp->flags = SMP_F_VOL_TEST;
2880 smp->data.type = SMP_T_SINT;
2881 smp->data.u.sint = 0;
2882 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002883 void *ptr;
2884
2885 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
2886 if (!ptr) {
2887 if (stkctr == &tmpstkctr)
2888 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002889 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002890 }
2891
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002892 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002893
Willy Tarreau7d562212016-11-25 16:10:05 +01002894 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002895
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002896 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002897
2898 if (stkctr == &tmpstkctr)
2899 stktable_release(stkctr->table, stkctr_entry(stkctr));
2900
2901
Willy Tarreau7d562212016-11-25 16:10:05 +01002902 }
2903 return 1;
2904}
2905
2906/* set <smp> to the connection rate from the stream's tracked frontend
2907 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2908 * only.
2909 */
2910static int
2911smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2912{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002913 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002914 struct stkctr *stkctr;
2915
Emeric Brun819fc6f2017-06-13 19:37:32 +02002916 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002917 if (!stkctr)
2918 return 0;
2919
2920 smp->flags = SMP_F_VOL_TEST;
2921 smp->data.type = SMP_T_SINT;
2922 smp->data.u.sint = 0;
2923 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002924 void *ptr;
2925
2926 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
2927 if (!ptr) {
2928 if (stkctr == &tmpstkctr)
2929 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002930 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002931 }
2932
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002933 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002934
Willy Tarreau7d562212016-11-25 16:10:05 +01002935 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2936 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002937
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002938 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002939
2940 if (stkctr == &tmpstkctr)
2941 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002942 }
2943 return 1;
2944}
2945
2946/* set temp integer to the number of connections from the stream's source address
2947 * in the table pointed to by expr, after updating it.
2948 * Accepts exactly 1 argument of type table.
2949 */
2950static int
2951smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2952{
2953 struct connection *conn = objt_conn(smp->sess->origin);
2954 struct stksess *ts;
2955 struct stktable_key *key;
2956 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002957 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002958
2959 if (!conn)
2960 return 0;
2961
Joseph Herlant5662fa42018-11-15 13:43:28 -08002962 /* Fetch source address in a sample. */
Amaury Denoyellec460c702021-05-12 10:17:47 +02002963 if (!smp_fetch_src || !smp_fetch_src(empty_arg_list, smp, "src", NULL))
Willy Tarreau7d562212016-11-25 16:10:05 +01002964 return 0;
2965
2966 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002967 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002968 if (!key)
2969 return 0;
2970
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002971 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002972
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002973 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01002974 /* entry does not exist and could not be created */
2975 return 0;
2976
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002977 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002978 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01002979 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002980 }
Willy Tarreau7d562212016-11-25 16:10:05 +01002981
2982 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002983
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002984 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002985
Willy Tarreau7d562212016-11-25 16:10:05 +01002986 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002987
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002988 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002989
Willy Tarreau7d562212016-11-25 16:10:05 +01002990 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002991
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002992 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002993
2994 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01002995 return 1;
2996}
2997
2998/* set <smp> to the number of concurrent connections from the stream's tracked
2999 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3000 * "src_conn_cur" only.
3001 */
3002static int
3003smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
3004{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003005 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003006 struct stkctr *stkctr;
3007
Emeric Brun819fc6f2017-06-13 19:37:32 +02003008 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003009 if (!stkctr)
3010 return 0;
3011
3012 smp->flags = SMP_F_VOL_TEST;
3013 smp->data.type = SMP_T_SINT;
3014 smp->data.u.sint = 0;
3015 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003016 void *ptr;
3017
3018 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
3019 if (!ptr) {
3020 if (stkctr == &tmpstkctr)
3021 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003022 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003023 }
3024
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003025 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003026
Willy Tarreau7d562212016-11-25 16:10:05 +01003027 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003028
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003029 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003030
3031 if (stkctr == &tmpstkctr)
3032 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003033 }
3034 return 1;
3035}
3036
3037/* set <smp> to the cumulated number of streams from the stream's tracked
3038 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3039 * "src_sess_cnt" only.
3040 */
3041static int
3042smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3043{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003044 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003045 struct stkctr *stkctr;
3046
Emeric Brun819fc6f2017-06-13 19:37:32 +02003047 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003048 if (!stkctr)
3049 return 0;
3050
3051 smp->flags = SMP_F_VOL_TEST;
3052 smp->data.type = SMP_T_SINT;
3053 smp->data.u.sint = 0;
3054 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003055 void *ptr;
3056
3057 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
3058 if (!ptr) {
3059 if (stkctr == &tmpstkctr)
3060 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003061 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003062 }
3063
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003064 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003065
Willy Tarreau7d562212016-11-25 16:10:05 +01003066 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003067
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003068 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003069
3070 if (stkctr == &tmpstkctr)
3071 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003072 }
3073 return 1;
3074}
3075
3076/* set <smp> to the stream rate from the stream's tracked frontend counters.
3077 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3078 */
3079static int
3080smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3081{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003082 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003083 struct stkctr *stkctr;
3084
Emeric Brun819fc6f2017-06-13 19:37:32 +02003085 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003086 if (!stkctr)
3087 return 0;
3088
3089 smp->flags = SMP_F_VOL_TEST;
3090 smp->data.type = SMP_T_SINT;
3091 smp->data.u.sint = 0;
3092 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003093 void *ptr;
3094
3095 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
3096 if (!ptr) {
3097 if (stkctr == &tmpstkctr)
3098 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003099 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003100 }
3101
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003102 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003103
Willy Tarreau7d562212016-11-25 16:10:05 +01003104 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
3105 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003106
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003107 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003108
3109 if (stkctr == &tmpstkctr)
3110 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003111 }
3112 return 1;
3113}
3114
3115/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
3116 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3117 * "src_http_req_cnt" only.
3118 */
3119static int
3120smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3121{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003122 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003123 struct stkctr *stkctr;
3124
Emeric Brun819fc6f2017-06-13 19:37:32 +02003125 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003126 if (!stkctr)
3127 return 0;
3128
3129 smp->flags = SMP_F_VOL_TEST;
3130 smp->data.type = SMP_T_SINT;
3131 smp->data.u.sint = 0;
3132 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003133 void *ptr;
3134
3135 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
3136 if (!ptr) {
3137 if (stkctr == &tmpstkctr)
3138 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003139 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003140 }
3141
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003142 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003143
Willy Tarreau7d562212016-11-25 16:10:05 +01003144 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003145
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003146 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003147
3148 if (stkctr == &tmpstkctr)
3149 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003150 }
3151 return 1;
3152}
3153
3154/* set <smp> to the HTTP request rate from the stream's tracked frontend
3155 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3156 * "src_http_req_rate" only.
3157 */
3158static int
3159smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3160{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003161 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003162 struct stkctr *stkctr;
3163
Emeric Brun819fc6f2017-06-13 19:37:32 +02003164 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003165 if (!stkctr)
3166 return 0;
3167
3168 smp->flags = SMP_F_VOL_TEST;
3169 smp->data.type = SMP_T_SINT;
3170 smp->data.u.sint = 0;
3171 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003172 void *ptr;
3173
3174 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
3175 if (!ptr) {
3176 if (stkctr == &tmpstkctr)
3177 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003178 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003179 }
3180
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003181 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003182
Willy Tarreau7d562212016-11-25 16:10:05 +01003183 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3184 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003185
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003186 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003187
3188 if (stkctr == &tmpstkctr)
3189 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003190 }
3191 return 1;
3192}
3193
3194/* set <smp> to the cumulated number of HTTP requests errors from the stream's
3195 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3196 * "src_http_err_cnt" only.
3197 */
3198static int
3199smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3200{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003201 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003202 struct stkctr *stkctr;
3203
Emeric Brun819fc6f2017-06-13 19:37:32 +02003204 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003205 if (!stkctr)
3206 return 0;
3207
3208 smp->flags = SMP_F_VOL_TEST;
3209 smp->data.type = SMP_T_SINT;
3210 smp->data.u.sint = 0;
3211 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003212 void *ptr;
3213
3214 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
3215 if (!ptr) {
3216 if (stkctr == &tmpstkctr)
3217 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003218 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003219 }
3220
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003221 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003222
Willy Tarreau7d562212016-11-25 16:10:05 +01003223 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003224
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003225 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003226
3227 if (stkctr == &tmpstkctr)
3228 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003229 }
3230 return 1;
3231}
3232
3233/* set <smp> to the HTTP request error rate from the stream's tracked frontend
3234 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3235 * "src_http_err_rate" only.
3236 */
3237static int
3238smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3239{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003240 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003241 struct stkctr *stkctr;
3242
Emeric Brun819fc6f2017-06-13 19:37:32 +02003243 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003244 if (!stkctr)
3245 return 0;
3246
3247 smp->flags = SMP_F_VOL_TEST;
3248 smp->data.type = SMP_T_SINT;
3249 smp->data.u.sint = 0;
3250 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003251 void *ptr;
3252
3253 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
3254 if (!ptr) {
3255 if (stkctr == &tmpstkctr)
3256 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003257 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003258 }
3259
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003260 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003261
Willy Tarreau7d562212016-11-25 16:10:05 +01003262 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3263 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003264
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003265 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003266
3267 if (stkctr == &tmpstkctr)
3268 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003269 }
3270 return 1;
3271}
3272
Willy Tarreau826f3ab2021-02-10 12:07:15 +01003273/* set <smp> to the cumulated number of HTTP response failures from the stream's
3274 * tracked frontend counters. Supports being called as "sc[0-9]_http_fail_cnt" or
3275 * "src_http_fail_cnt" only.
3276 */
3277static int
3278smp_fetch_sc_http_fail_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
3279{
3280 struct stkctr tmpstkctr;
3281 struct stkctr *stkctr;
3282
3283 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3284 if (!stkctr)
3285 return 0;
3286
3287 smp->flags = SMP_F_VOL_TEST;
3288 smp->data.type = SMP_T_SINT;
3289 smp->data.u.sint = 0;
3290 if (stkctr_entry(stkctr) != NULL) {
3291 void *ptr;
3292
3293 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_CNT);
3294 if (!ptr) {
3295 if (stkctr == &tmpstkctr)
3296 stktable_release(stkctr->table, stkctr_entry(stkctr));
3297 return 0; /* parameter not stored */
3298 }
3299
3300 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3301
3302 smp->data.u.sint = stktable_data_cast(ptr, http_fail_cnt);
3303
3304 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3305
3306 if (stkctr == &tmpstkctr)
3307 stktable_release(stkctr->table, stkctr_entry(stkctr));
3308 }
3309 return 1;
3310}
3311
3312/* set <smp> to the HTTP response failure rate from the stream's tracked frontend
3313 * counters. Supports being called as "sc[0-9]_http_fail_rate" or
3314 * "src_http_fail_rate" only.
3315 */
3316static int
3317smp_fetch_sc_http_fail_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3318{
3319 struct stkctr tmpstkctr;
3320 struct stkctr *stkctr;
3321
3322 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
3323 if (!stkctr)
3324 return 0;
3325
3326 smp->flags = SMP_F_VOL_TEST;
3327 smp->data.type = SMP_T_SINT;
3328 smp->data.u.sint = 0;
3329 if (stkctr_entry(stkctr) != NULL) {
3330 void *ptr;
3331
3332 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_FAIL_RATE);
3333 if (!ptr) {
3334 if (stkctr == &tmpstkctr)
3335 stktable_release(stkctr->table, stkctr_entry(stkctr));
3336 return 0; /* parameter not stored */
3337 }
3338
3339 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3340
3341 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_fail_rate),
3342 stkctr->table->data_arg[STKTABLE_DT_HTTP_FAIL_RATE].u);
3343
3344 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
3345
3346 if (stkctr == &tmpstkctr)
3347 stktable_release(stkctr->table, stkctr_entry(stkctr));
3348 }
3349 return 1;
3350}
3351
Willy Tarreau7d562212016-11-25 16:10:05 +01003352/* set <smp> to the number of kbytes received from clients, as found in the
3353 * stream's tracked frontend counters. Supports being called as
3354 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3355 */
3356static int
3357smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
3358{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003359 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003360 struct stkctr *stkctr;
3361
Emeric Brun819fc6f2017-06-13 19:37:32 +02003362 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003363 if (!stkctr)
3364 return 0;
3365
3366 smp->flags = SMP_F_VOL_TEST;
3367 smp->data.type = SMP_T_SINT;
3368 smp->data.u.sint = 0;
3369 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003370 void *ptr;
3371
3372 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
3373 if (!ptr) {
3374 if (stkctr == &tmpstkctr)
3375 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003376 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003377 }
3378
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003379 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003380
Willy Tarreau7d562212016-11-25 16:10:05 +01003381 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003382
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003383 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003384
3385 if (stkctr == &tmpstkctr)
3386 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003387 }
3388 return 1;
3389}
3390
3391/* set <smp> to the data rate received from clients in bytes/s, as found
3392 * in the stream's tracked frontend counters. Supports being called as
3393 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
3394 */
3395static int
3396smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3397{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003398 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003399 struct stkctr *stkctr;
3400
Emeric Brun819fc6f2017-06-13 19:37:32 +02003401 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003402 if (!stkctr)
3403 return 0;
3404
3405 smp->flags = SMP_F_VOL_TEST;
3406 smp->data.type = SMP_T_SINT;
3407 smp->data.u.sint = 0;
3408 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003409 void *ptr;
3410
3411 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
3412 if (!ptr) {
3413 if (stkctr == &tmpstkctr)
3414 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003415 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003416 }
3417
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003418 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003419
Willy Tarreau7d562212016-11-25 16:10:05 +01003420 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
3421 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003422
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003423 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003424
3425 if (stkctr == &tmpstkctr)
3426 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003427 }
3428 return 1;
3429}
3430
3431/* set <smp> to the number of kbytes sent to clients, as found in the
3432 * stream's tracked frontend counters. Supports being called as
3433 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3434 */
3435static int
3436smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
3437{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003438 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003439 struct stkctr *stkctr;
3440
Emeric Brun819fc6f2017-06-13 19:37:32 +02003441 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003442 if (!stkctr)
3443 return 0;
3444
3445 smp->flags = SMP_F_VOL_TEST;
3446 smp->data.type = SMP_T_SINT;
3447 smp->data.u.sint = 0;
3448 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003449 void *ptr;
3450
3451 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
3452 if (!ptr) {
3453 if (stkctr == &tmpstkctr)
3454 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003455 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003456 }
3457
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003458 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003459
Willy Tarreau7d562212016-11-25 16:10:05 +01003460 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003461
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003462 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003463
3464 if (stkctr == &tmpstkctr)
3465 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003466 }
3467 return 1;
3468}
3469
3470/* set <smp> to the data rate sent to clients in bytes/s, as found in the
3471 * stream's tracked frontend counters. Supports being called as
3472 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
3473 */
3474static int
3475smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3476{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003477 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003478 struct stkctr *stkctr;
3479
Emeric Brun819fc6f2017-06-13 19:37:32 +02003480 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003481 if (!stkctr)
3482 return 0;
3483
3484 smp->flags = SMP_F_VOL_TEST;
3485 smp->data.type = SMP_T_SINT;
3486 smp->data.u.sint = 0;
3487 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003488 void *ptr;
3489
3490 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
3491 if (!ptr) {
3492 if (stkctr == &tmpstkctr)
3493 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003494 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003495 }
3496
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003497 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003498
Willy Tarreau7d562212016-11-25 16:10:05 +01003499 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3500 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003501
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003502 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003503
3504 if (stkctr == &tmpstkctr)
3505 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003506 }
3507 return 1;
3508}
3509
3510/* set <smp> to the number of active trackers on the SC entry in the stream's
3511 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3512 */
3513static int
3514smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
3515{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003516 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003517 struct stkctr *stkctr;
3518
Emeric Brun819fc6f2017-06-13 19:37:32 +02003519 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003520 if (!stkctr)
3521 return 0;
3522
3523 smp->flags = SMP_F_VOL_TEST;
3524 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003525 if (stkctr == &tmpstkctr) {
3526 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
3527 stktable_release(stkctr->table, stkctr_entry(stkctr));
3528 }
3529 else {
3530 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
3531 }
3532
Willy Tarreau7d562212016-11-25 16:10:05 +01003533 return 1;
3534}
3535
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003536
3537/* The functions below are used to manipulate table contents from the CLI.
3538 * There are 3 main actions, "clear", "set" and "show". The code is shared
3539 * between all actions, and the action is encoded in the void *private in
3540 * the appctx as well as in the keyword registration, among one of the
3541 * following values.
3542 */
3543
3544enum {
3545 STK_CLI_ACT_CLR,
3546 STK_CLI_ACT_SET,
3547 STK_CLI_ACT_SHOW,
3548};
3549
3550/* Dump the status of a table to a stream interface's
3551 * read buffer. It returns 0 if the output buffer is full
3552 * and needs to be called again, otherwise non-zero.
3553 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003554static int table_dump_head_to_buffer(struct buffer *msg,
3555 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003556 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003557{
3558 struct stream *s = si_strm(si);
3559
3560 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003561 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003562
3563 /* any other information should be dumped here */
3564
William Lallemand07a62f72017-05-24 00:57:40 +02003565 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003566 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
3567
Willy Tarreau06d80a92017-10-19 14:32:15 +02003568 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003569 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003570 return 0;
3571 }
3572
3573 return 1;
3574}
3575
3576/* Dump a table entry to a stream interface's
3577 * read buffer. It returns 0 if the output buffer is full
3578 * and needs to be called again, otherwise non-zero.
3579 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003580static int table_dump_entry_to_buffer(struct buffer *msg,
3581 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003582 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003583{
3584 int dt;
3585
3586 chunk_appendf(msg, "%p:", entry);
3587
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003588 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003589 char addr[INET_ADDRSTRLEN];
3590 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
3591 chunk_appendf(msg, " key=%s", addr);
3592 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003593 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003594 char addr[INET6_ADDRSTRLEN];
3595 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
3596 chunk_appendf(msg, " key=%s", addr);
3597 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003598 else if (t->type == SMP_T_SINT) {
Willy Tarreau6cde5d82020-02-25 09:41:22 +01003599 chunk_appendf(msg, " key=%u", read_u32(entry->key.key));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003600 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003601 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003602 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003603 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003604 }
3605 else {
3606 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003607 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003608 }
3609
3610 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
3611
3612 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3613 void *ptr;
3614
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003615 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003616 continue;
3617 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003618 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003619 else
3620 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
3621
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003622 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003623 switch (stktable_data_types[dt].std_type) {
3624 case STD_T_SINT:
3625 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
3626 break;
3627 case STD_T_UINT:
3628 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
3629 break;
3630 case STD_T_ULL:
3631 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3632 break;
3633 case STD_T_FRQP:
3634 chunk_appendf(msg, "%d",
3635 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003636 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003637 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02003638 case STD_T_DICT: {
3639 struct dict_entry *de;
3640 de = stktable_data_cast(ptr, std_t_dict);
3641 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
3642 break;
3643 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003644 }
3645 }
3646 chunk_appendf(msg, "\n");
3647
Willy Tarreau06d80a92017-10-19 14:32:15 +02003648 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003649 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003650 return 0;
3651 }
3652
3653 return 1;
3654}
3655
3656
3657/* Processes a single table entry matching a specific key passed in argument.
3658 * returns 0 if wants to be called again, 1 if has ended processing.
3659 */
3660static int table_process_entry_per_key(struct appctx *appctx, char **args)
3661{
3662 struct stream_interface *si = appctx->owner;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003663 struct stktable *t = appctx->ctx.table.target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003664 struct stksess *ts;
3665 uint32_t uint32_key;
3666 unsigned char ip6_key[sizeof(struct in6_addr)];
3667 long long value;
3668 int data_type;
3669 int cur_arg;
3670 void *ptr;
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003671 struct freq_ctr *frqp;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003672
Willy Tarreau9d008692019-08-09 11:21:01 +02003673 if (!*args[4])
3674 return cli_err(appctx, "Key value expected\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003675
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003676 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003677 case SMP_T_IPV4:
3678 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02003679 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003680 break;
3681 case SMP_T_IPV6:
3682 inet_pton(AF_INET6, args[4], ip6_key);
Christopher Fauletca20d022017-08-29 15:30:31 +02003683 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003684 break;
3685 case SMP_T_SINT:
3686 {
3687 char *endptr;
3688 unsigned long val;
3689 errno = 0;
3690 val = strtoul(args[4], &endptr, 10);
3691 if ((errno == ERANGE && val == ULONG_MAX) ||
3692 (errno != 0 && val == 0) || endptr == args[4] ||
Willy Tarreau9d008692019-08-09 11:21:01 +02003693 val > 0xffffffff)
3694 return cli_err(appctx, "Invalid key\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003695 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02003696 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003697 break;
3698 }
3699 break;
3700 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02003701 static_table_key.key = args[4];
3702 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003703 break;
3704 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003705 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003706 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02003707 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 +01003708 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02003709 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 +01003710 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02003711 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 +01003712 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003713 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003714 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003715 }
3716
3717 /* check permissions */
3718 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3719 return 1;
3720
Willy Tarreaua24bc782016-12-14 15:50:35 +01003721 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003722 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003723 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003724 if (!ts)
3725 return 1;
3726 chunk_reset(&trash);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003727 if (!table_dump_head_to_buffer(&trash, si, t, t)) {
3728 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003729 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003730 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003731 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003732 if (!table_dump_entry_to_buffer(&trash, si, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003733 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003734 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003735 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003736 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003737 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003738 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003739 break;
3740
3741 case STK_CLI_ACT_CLR:
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;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003745
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003746 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003747 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02003748 return cli_err(appctx, "Entry currently in use, cannot remove\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003749 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003750 break;
3751
3752 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003753 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003754 if (!ts) {
3755 /* don't delete an entry which is currently referenced */
Willy Tarreau9d008692019-08-09 11:21:01 +02003756 return cli_err(appctx, "Unable to allocate a new entry\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003757 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003758 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003759 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
3760 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003761 cli_err(appctx, "\"data.<type>\" followed by a value expected\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003762 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003763 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003764 return 1;
3765 }
3766
3767 data_type = stktable_get_data_type(args[cur_arg] + 5);
3768 if (data_type < 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003769 cli_err(appctx, "Unknown data type\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003770 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003771 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003772 return 1;
3773 }
3774
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003775 if (!t->data_ofs[data_type]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003776 cli_err(appctx, "Data type not stored in this table\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003777 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003778 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003779 return 1;
3780 }
3781
3782 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003783 cli_err(appctx, "Require a valid integer value to store\n");
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003784 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003785 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003786 return 1;
3787 }
3788
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003789 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003790
3791 switch (stktable_data_types[data_type].std_type) {
3792 case STD_T_SINT:
3793 stktable_data_cast(ptr, std_t_sint) = value;
3794 break;
3795 case STD_T_UINT:
3796 stktable_data_cast(ptr, std_t_uint) = value;
3797 break;
3798 case STD_T_ULL:
3799 stktable_data_cast(ptr, std_t_ull) = value;
3800 break;
3801 case STD_T_FRQP:
3802 /* We set both the current and previous values. That way
3803 * the reported frequency is stable during all the period
3804 * then slowly fades out. This allows external tools to
3805 * push measures without having to update them too often.
3806 */
3807 frqp = &stktable_data_cast(ptr, std_t_frqp);
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003808 /* First bit is reserved for the freq_ctr lock
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01003809 Note: here we're still protected by the stksess lock
Willy Tarreaufa1258f2021-04-10 23:00:53 +02003810 so we don't need to update the update the freq_ctr
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01003811 using its internal lock */
3812 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003813 frqp->prev_ctr = 0;
3814 frqp->curr_ctr = value;
3815 break;
3816 }
3817 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003818 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003819 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003820 break;
3821
3822 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003823 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003824 }
3825 return 1;
3826}
3827
3828/* Prepares the appctx fields with the data-based filters from the command line.
3829 * Returns 0 if the dump can proceed, 1 if has ended processing.
3830 */
3831static int table_prepare_data_request(struct appctx *appctx, char **args)
3832{
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003833 int i;
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003834 char *err = NULL;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003835
Willy Tarreau9d008692019-08-09 11:21:01 +02003836 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR)
3837 return cli_err(appctx, "content-based lookup is only supported with the \"show\" and \"clear\" actions\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003838
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003839 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
3840 if (i > 0 && !*args[3+3*i]) // number of filter entries can be less than STKTABLE_FILTER_LEN
3841 break;
3842 /* condition on stored data value */
3843 appctx->ctx.table.data_type[i] = stktable_get_data_type(args[3+3*i] + 5);
3844 if (appctx->ctx.table.data_type[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003845 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Unknown data type\n", i + 1));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003846
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003847 if (!((struct stktable *)appctx->ctx.table.target)->data_ofs[appctx->ctx.table.data_type[i]])
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003848 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 +01003849
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003850 appctx->ctx.table.data_op[i] = get_std_op(args[4+3*i]);
Willy Tarreau2b64a352020-01-22 17:09:47 +01003851 if (appctx->ctx.table.data_op[i] < 0)
Adis Nezirovicd0142e72020-01-22 16:50:27 +01003852 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 +01003853
Adis Nezirovic56dd3542020-01-22 16:16:48 +01003854 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 +01003855 return cli_dynerr(appctx, memprintf(&err, "Filter entry #%i: Require a valid integer value to compare against\n", i + 1));
3856 }
3857
3858 if (*args[3+3*i]) {
3859 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 +01003860 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003861
3862 /* OK we're done, all the fields are set */
3863 return 0;
3864}
3865
3866/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003867static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003868{
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003869 int i;
3870
3871 for (i = 0; i < STKTABLE_FILTER_LEN; i++)
3872 appctx->ctx.table.data_type[i] = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003873 appctx->ctx.table.target = NULL;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003874 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003875 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003876
3877 if (*args[2]) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003878 appctx->ctx.table.target = stktable_find_by_name(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003879 if (!appctx->ctx.table.target)
3880 return cli_err(appctx, "No such table\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003881 }
3882 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01003883 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003884 goto err_args;
3885 return 0;
3886 }
3887
3888 if (strcmp(args[3], "key") == 0)
3889 return table_process_entry_per_key(appctx, args);
3890 else if (strncmp(args[3], "data.", 5) == 0)
3891 return table_prepare_data_request(appctx, args);
3892 else if (*args[3])
3893 goto err_args;
3894
3895 return 0;
3896
3897err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003898 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003899 case STK_CLI_ACT_SHOW:
Willy Tarreau9d008692019-08-09 11:21:01 +02003900 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 +01003901 case STK_CLI_ACT_CLR:
Willy Tarreau9d008692019-08-09 11:21:01 +02003902 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 +01003903 case STK_CLI_ACT_SET:
Willy Tarreau9d008692019-08-09 11:21:01 +02003904 return cli_err(appctx, "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003905 default:
Willy Tarreau9d008692019-08-09 11:21:01 +02003906 return cli_err(appctx, "Unknown action\n");
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003907 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003908}
3909
3910/* This function is used to deal with table operations (dump or clear depending
3911 * on the action stored in appctx->private). It returns 0 if the output buffer is
3912 * full and it needs to be called again, otherwise non-zero.
3913 */
3914static int cli_io_handler_table(struct appctx *appctx)
3915{
3916 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003917 struct stream *s = si_strm(si);
3918 struct ebmb_node *eb;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003919 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003920 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003921
3922 /*
3923 * We have 3 possible states in appctx->st2 :
3924 * - STAT_ST_INIT : the first call
3925 * - STAT_ST_INFO : the proxy pointer points to the next table to
3926 * dump, the entry pointer is NULL ;
3927 * - STAT_ST_LIST : the proxy pointer points to the current table
3928 * and the entry pointer points to the next entry to be dumped,
3929 * and the refcount on the next entry is held ;
3930 * - STAT_ST_END : nothing left to dump, the buffer may contain some
3931 * data though.
3932 */
3933
3934 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3935 /* in case of abort, remove any refcount we might have set on an entry */
3936 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003937 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003938 }
3939 return 1;
3940 }
3941
3942 chunk_reset(&trash);
3943
3944 while (appctx->st2 != STAT_ST_FIN) {
3945 switch (appctx->st2) {
3946 case STAT_ST_INIT:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003947 appctx->ctx.table.t = appctx->ctx.table.target;
3948 if (!appctx->ctx.table.t)
3949 appctx->ctx.table.t = stktables_list;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003950
3951 appctx->ctx.table.entry = NULL;
3952 appctx->st2 = STAT_ST_INFO;
3953 break;
3954
3955 case STAT_ST_INFO:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003956 if (!appctx->ctx.table.t ||
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003957 (appctx->ctx.table.target &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003958 appctx->ctx.table.t != appctx->ctx.table.target)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003959 appctx->st2 = STAT_ST_END;
3960 break;
3961 }
3962
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003963 if (appctx->ctx.table.t->size) {
3964 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003965 return 0;
3966
3967 if (appctx->ctx.table.target &&
William Lallemand07a62f72017-05-24 00:57:40 +02003968 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003969 /* dump entries only if table explicitly requested */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003970 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
3971 eb = ebmb_first(&appctx->ctx.table.t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003972 if (eb) {
3973 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3974 appctx->ctx.table.entry->ref_cnt++;
3975 appctx->st2 = STAT_ST_LIST;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003976 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003977 break;
3978 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003979 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003980 }
3981 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003982 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003983 break;
3984
3985 case STAT_ST_LIST:
3986 skip_entry = 0;
3987
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003989
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003990 if (appctx->ctx.table.data_type[0] >= 0) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003991 /* we're filtering on some data contents */
3992 void *ptr;
Willy Tarreau2b64a352020-01-22 17:09:47 +01003993 int dt, i;
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003994 signed char op;
3995 long long data, value;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003996
Emeric Brun819fc6f2017-06-13 19:37:32 +02003997
Willy Tarreau2b64a352020-01-22 17:09:47 +01003998 for (i = 0; i < STKTABLE_FILTER_LEN; i++) {
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01003999 if (appctx->ctx.table.data_type[i] == -1)
4000 break;
4001 dt = appctx->ctx.table.data_type[i];
4002 ptr = stktable_data_ptr(appctx->ctx.table.t,
4003 appctx->ctx.table.entry,
4004 dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004005
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004006 data = 0;
4007 switch (stktable_data_types[dt].std_type) {
4008 case STD_T_SINT:
4009 data = stktable_data_cast(ptr, std_t_sint);
4010 break;
4011 case STD_T_UINT:
4012 data = stktable_data_cast(ptr, std_t_uint);
4013 break;
4014 case STD_T_ULL:
4015 data = stktable_data_cast(ptr, std_t_ull);
4016 break;
4017 case STD_T_FRQP:
4018 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
4019 appctx->ctx.table.t->data_arg[dt].u);
4020 break;
4021 }
4022
4023 op = appctx->ctx.table.data_op[i];
4024 value = appctx->ctx.table.value[i];
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004025
Adis Nezirovic1a693fc2020-01-16 15:19:29 +01004026 /* skip the entry if the data does not match the test and the value */
4027 if ((data < value &&
4028 (op == STD_OP_EQ || op == STD_OP_GT || op == STD_OP_GE)) ||
4029 (data == value &&
4030 (op == STD_OP_NE || op == STD_OP_GT || op == STD_OP_LT)) ||
4031 (data > value &&
4032 (op == STD_OP_EQ || op == STD_OP_LT || op == STD_OP_LE))) {
4033 skip_entry = 1;
4034 break;
4035 }
4036 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004037 }
4038
4039 if (show && !skip_entry &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004040 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.entry)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004041 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004042 return 0;
4043 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004044
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004045 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004046
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004047 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004048 appctx->ctx.table.entry->ref_cnt--;
4049
4050 eb = ebmb_next(&appctx->ctx.table.entry->key);
4051 if (eb) {
4052 struct stksess *old = appctx->ctx.table.entry;
4053 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4054 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004055 __stksess_kill_if_expired(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004056 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004057 __stksess_kill(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004058 appctx->ctx.table.entry->ref_cnt++;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004059 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004060 break;
4061 }
4062
4063
4064 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004065 __stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004066 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004067 __stksess_kill(appctx->ctx.table.t, appctx->ctx.table.entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02004068
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004069 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004070
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004071 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004072 appctx->st2 = STAT_ST_INFO;
4073 break;
4074
4075 case STAT_ST_END:
4076 appctx->st2 = STAT_ST_FIN;
4077 break;
4078 }
4079 }
4080 return 1;
4081}
4082
4083static void cli_release_show_table(struct appctx *appctx)
4084{
4085 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01004086 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004087 }
4088}
4089
Willy Tarreau478331d2020-08-28 11:31:31 +02004090static void stkt_late_init(void)
4091{
4092 struct sample_fetch *f;
4093
4094 f = find_sample_fetch("src", strlen("src"));
4095 if (f)
4096 smp_fetch_src = f->process;
4097}
4098
4099INITCALL0(STG_INIT, stkt_late_init);
4100
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004101/* register cli keywords */
4102static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004103 { { "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 },
4104 { { "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 },
4105 { { "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 +01004106 {{},}
4107}};
4108
Willy Tarreau0108d902018-11-25 19:14:37 +01004109INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01004110
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004111static struct action_kw_list tcp_conn_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004112 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4113 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4114 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004115 { /* END */ }
4116}};
4117
Willy Tarreau0108d902018-11-25 19:14:37 +01004118INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
4119
Willy Tarreau620408f2016-10-21 16:37:51 +02004120static struct action_kw_list tcp_sess_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004121 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4122 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4123 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Willy Tarreau620408f2016-10-21 16:37:51 +02004124 { /* END */ }
4125}};
4126
Willy Tarreau0108d902018-11-25 19:14:37 +01004127INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
4128
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004129static struct action_kw_list tcp_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004130 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4131 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4132 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004133 { /* END */ }
4134}};
4135
Willy Tarreau0108d902018-11-25 19:14:37 +01004136INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
4137
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004138static struct action_kw_list tcp_res_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004139 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4140 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4141 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004142 { /* END */ }
4143}};
4144
Willy Tarreau0108d902018-11-25 19:14:37 +01004145INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
4146
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004147static struct action_kw_list http_req_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004148 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4149 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4150 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004151 { /* END */ }
4152}};
4153
Willy Tarreau0108d902018-11-25 19:14:37 +01004154INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
4155
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004156static struct action_kw_list http_res_kws = { { }, {
Amaury Denoyellee4a617c2021-05-06 15:33:09 +02004157 { "sc-inc-gpc0", parse_inc_gpc0, KWF_MATCH_PREFIX },
4158 { "sc-inc-gpc1", parse_inc_gpc1, KWF_MATCH_PREFIX },
4159 { "sc-set-gpt0", parse_set_gpt0, KWF_MATCH_PREFIX },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02004160 { /* END */ }
4161}};
4162
Willy Tarreau0108d902018-11-25 19:14:37 +01004163INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
4164
Willy Tarreau7d562212016-11-25 16:10:05 +01004165/* Note: must not be declared <const> as its list will be overwritten.
4166 * Please take care of keeping this list alphabetically sorted.
4167 */
4168static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
4169 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4170 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4171 { "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 +01004172 { "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 +01004173 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4174 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4175 { "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 +01004176 { "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 +01004177 { "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 +01004178 { "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 +01004179 { "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 +01004180 { "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 +01004181 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4182 { "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 +01004183 { "sc_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4184 { "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 +01004185 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4186 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4187 { "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 +01004188 { "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 +01004189 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4190 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4191 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4192 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4193 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4194 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4195 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4196 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4197 { "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 +01004198 { "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 +01004199 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4200 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4201 { "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 +01004202 { "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 +01004203 { "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 +01004204 { "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 +01004205 { "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 +01004206 { "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 +01004207 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4208 { "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 +01004209 { "sc0_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4210 { "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 +01004211 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4212 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4213 { "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 +01004214 { "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 +01004215 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4216 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4217 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4218 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4219 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4220 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4221 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4222 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4223 { "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 +01004224 { "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 +01004225 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4226 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4227 { "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 +01004228 { "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 +01004229 { "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 +01004230 { "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 +01004231 { "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 +01004232 { "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 +01004233 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4234 { "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 +01004235 { "sc1_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4236 { "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 +01004237 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4238 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4239 { "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 +01004240 { "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 +01004241 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4242 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4243 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4244 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4245 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4246 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4247 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4248 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4249 { "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 +01004250 { "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 +01004251 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4252 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4253 { "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 +01004254 { "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 +01004255 { "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 +01004256 { "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 +01004257 { "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 +01004258 { "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 +01004259 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4260 { "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 +01004261 { "sc2_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4262 { "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 +01004263 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4264 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4265 { "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 +01004266 { "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 +01004267 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4268 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4269 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4270 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4271 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4272 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4273 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4274 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4275 { "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 +01004276 { "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 +01004277 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4278 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4279 { "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 +01004280 { "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 +01004281 { "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 +01004282 { "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 +01004283 { "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 +01004284 { "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 +01004285 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4286 { "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 +01004287 { "src_http_fail_cnt", smp_fetch_sc_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4288 { "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 +01004289 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4290 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4291 { "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 +01004292 { "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 +01004293 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4294 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4295 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4296 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4297 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4298 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4299 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4300 { /* END */ },
4301}};
4302
Willy Tarreau0108d902018-11-25 19:14:37 +01004303INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01004304
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004305/* Note: must not be declared <const> as its list will be overwritten */
4306static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02004307 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
4308 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4309 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4310 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4311 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4312 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4313 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4314 { "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 +01004315 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02004316 { "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 +01004317 { "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 +02004318 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4319 { "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 +01004320 { "table_http_fail_cnt", sample_conv_table_http_fail_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4321 { "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 +02004322 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4323 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4324 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4325 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4326 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4327 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4328 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4329 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004330 { /* END */ },
4331}};
4332
Willy Tarreau0108d902018-11-25 19:14:37 +01004333INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);