blob: 86aaedfc1d3a94a6710f8d9c4c13490f86570006 [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
17#include <common/config.h>
Frédéric Lécailled456aa42019-03-08 14:47:00 +010018#include <common/cfgparse.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010020#include <common/memory.h>
21#include <common/mini-clist.h>
22#include <common/standard.h>
23#include <common/time.h>
24
25#include <ebmbtree.h>
26#include <ebsttree.h>
27
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010028#include <types/cli.h>
Willy Tarreau39713102016-11-25 15:49:32 +010029#include <types/global.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010030#include <types/stats.h>
31
Willy Tarreaud9f316a2014-07-10 14:03:38 +020032#include <proto/arg.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010033#include <proto/cli.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020034#include <proto/http_rules.h>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020035#include <proto/log.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020036#include <proto/proto_http.h>
Willy Tarreau7d562212016-11-25 16:10:05 +010037#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010038#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020039#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010041#include <proto/stream_interface.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020042#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010043#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020044#include <proto/peers.h>
Willy Tarreau39713102016-11-25 15:49:32 +010045#include <proto/tcp_rules.h>
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 Tarreauf0b38bf2010-06-06 13:22:23 +020049
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010050struct stktable *stktables_list;
51struct eb_root stktable_by_name = EB_ROOT;
52
Olivier Houchard52dabbc2018-11-14 17:54:36 +010053#define round_ptr_size(i) (((i) + (sizeof(void *) - 1)) &~ (sizeof(void *) - 1))
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010054
55/* This function inserts stktable <t> into the tree of known stick-table.
56 * The stick-table ID is used as the storing key so it must already have
57 * been initialized.
58 */
59void stktable_store_name(struct stktable *t)
60{
61 t->name.key = t->id;
62 ebis_insert(&stktable_by_name, &t->name);
63}
64
65struct stktable *stktable_find_by_name(const char *name)
66{
67 struct ebpt_node *node;
68 struct stktable *t;
69
70 node = ebis_lookup(&stktable_by_name, name);
71 if (node) {
72 t = container_of(node, struct stktable, name);
73 if (!strcmp(t->id, name))
74 return t;
75 }
76
77 return NULL;
78}
79
Emeric Brun3bd697e2010-01-04 15:23:48 +010080/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020081 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
82 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010083 */
Emeric Brun819fc6f2017-06-13 19:37:32 +020084void __stksess_free(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010085{
86 t->current--;
Olivier Houchard52dabbc2018-11-14 17:54:36 +010087 pool_free(t->pool, (void *)ts - round_ptr_size(t->data_size));
Emeric Brun3bd697e2010-01-04 15:23:48 +010088}
89
90/*
Emeric Brun819fc6f2017-06-13 19:37:32 +020091 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
92 * in table <t>.
93 * This function locks the table
94 */
95void stksess_free(struct stktable *t, struct stksess *ts)
96{
Christopher Faulet2a944ee2017-11-07 10:42:54 +010097 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +020098 __stksess_free(t, ts);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010099 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200100}
101
102/*
Willy Tarreauf6efda12010-08-03 20:34:06 +0200103 * Kill an stksess (only if its ref_cnt is zero).
104 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200105int __stksess_kill(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200106{
107 if (ts->ref_cnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200108 return 0;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200109
110 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +0200111 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200112 ebmb_delete(&ts->key);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200113 __stksess_free(t, ts);
114 return 1;
Willy Tarreauf6efda12010-08-03 20:34:06 +0200115}
116
117/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200118 * Decrease the refcount if decrefcnt is not 0.
119 * and try to kill the stksess
120 * This function locks the table
121 */
122int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcnt)
123{
124 int ret;
125
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200127 if (decrefcnt)
128 ts->ref_cnt--;
129 ret = __stksess_kill(t, ts);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200131
132 return ret;
133}
134
135/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200136 * Initialize or update the key in the sticky session <ts> present in table <t>
137 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100138 */
Willy Tarreau393379c2010-06-06 12:11:37 +0200139void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200141 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200142 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +0200144 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
145 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100146 }
147}
148
149
150/*
Willy Tarreau393379c2010-06-06 12:11:37 +0200151 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
152 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200154static struct stksess *__stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100155{
Willy Tarreau393379c2010-06-06 12:11:37 +0200156 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200157 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +0200158 ts->key.node.leaf_p = NULL;
159 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200160 ts->upd.node.leaf_p = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200161 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100162 HA_RWLOCK_INIT(&ts->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100163 return ts;
164}
165
166/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200167 * Trash oldest <to_batch> sticky sessions from table <t>
Willy Tarreaud0526c62020-11-03 17:47:41 +0100168 * Returns number of trashed sticky sessions. It may actually trash less
169 * than expected if finding these requires too long a search time (e.g.
170 * most of them have ts->ref_cnt>0).
Emeric Brun3bd697e2010-01-04 15:23:48 +0100171 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200172int __stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100173{
174 struct stksess *ts;
175 struct eb32_node *eb;
Willy Tarreaud0526c62020-11-03 17:47:41 +0100176 int max_search = to_batch * 2; // no more than 50% misses
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200178 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179
180 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
181
182 while (batched < to_batch) {
183
184 if (unlikely(!eb)) {
185 /* we might have reached the end of the tree, typically because
186 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200187 * half. Let's loop back to the beginning of the tree now if we
188 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200190 if (looped)
191 break;
192 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100193 eb = eb32_first(&t->exps);
194 if (likely(!eb))
195 break;
196 }
197
Willy Tarreaud0526c62020-11-03 17:47:41 +0100198 if (--max_search < 0)
199 break;
200
Emeric Brun3bd697e2010-01-04 15:23:48 +0100201 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200202 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203 eb = eb32_next(eb);
204
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200205 /* don't delete an entry which is currently referenced */
206 if (ts->ref_cnt)
207 continue;
208
Willy Tarreau86257dc2010-06-06 12:57:10 +0200209 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100210
Willy Tarreau86257dc2010-06-06 12:57:10 +0200211 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100212 if (!tick_isset(ts->expire))
213 continue;
214
Willy Tarreau86257dc2010-06-06 12:57:10 +0200215 ts->exp.key = ts->expire;
216 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100217
Willy Tarreau86257dc2010-06-06 12:57:10 +0200218 if (!eb || eb->key > ts->exp.key)
219 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100220
221 continue;
222 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100223
Willy Tarreauaea940e2010-06-06 11:56:36 +0200224 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200225 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200226 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200227 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228 batched++;
229 }
230
231 return batched;
232}
233
234/*
Emeric Brun819fc6f2017-06-13 19:37:32 +0200235 * Trash oldest <to_batch> sticky sessions from table <t>
236 * Returns number of trashed sticky sessions.
237 * This function locks the table
238 */
239int stktable_trash_oldest(struct stktable *t, int to_batch)
240{
241 int ret;
242
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200244 ret = __stktable_trash_oldest(t, to_batch);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200246
247 return ret;
248}
249/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200250 * Allocate and initialise a new sticky session.
251 * The new sticky session is returned or NULL in case of lack of memory.
252 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200253 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
254 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100255 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200256struct stksess *__stksess_new(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100257{
258 struct stksess *ts;
259
260 if (unlikely(t->current == t->size)) {
261 if ( t->nopurge )
262 return NULL;
263
Emeric Brun819fc6f2017-06-13 19:37:32 +0200264 if (!__stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100265 return NULL;
266 }
267
Willy Tarreaubafbe012017-11-24 17:34:44 +0100268 ts = pool_alloc(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100269 if (ts) {
270 t->current++;
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100271 ts = (void *)ts + round_ptr_size(t->data_size);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200272 __stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200273 if (key)
274 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100275 }
276
277 return ts;
278}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200279/*
280 * Allocate and initialise a new sticky session.
281 * The new sticky session is returned or NULL in case of lack of memory.
282 * Sticky sessions should only be allocated this way, and must be freed using
283 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
284 * is not NULL, it is assigned to the new session.
285 * This function locks the table
286 */
287struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
288{
289 struct stksess *ts;
290
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100291 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200292 ts = __stksess_new(t, key);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100293 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200294
295 return ts;
296}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100297
298/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200299 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200300 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100301 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200302struct stksess *__stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100303{
304 struct ebmb_node *eb;
305
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200306 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200307 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 +0100308 else
309 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
310
311 if (unlikely(!eb)) {
312 /* no session found */
313 return NULL;
314 }
315
Willy Tarreau86257dc2010-06-06 12:57:10 +0200316 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100317}
318
Emeric Brun819fc6f2017-06-13 19:37:32 +0200319/*
320 * Looks in table <t> for a sticky session matching key <key>.
321 * Returns pointer on requested sticky session or NULL if none was found.
322 * The refcount of the found entry is increased and this function
323 * is protected using the table lock
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200324 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200325struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200326{
327 struct stksess *ts;
328
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200330 ts = __stktable_lookup_key(t, key);
331 if (ts)
332 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200334
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200335 return ts;
336}
337
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200338/*
339 * Looks in table <t> for a sticky session with same key as <ts>.
340 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100341 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200342struct stksess *__stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100343{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100344 struct ebmb_node *eb;
345
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200346 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200347 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200349 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100350
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200351 if (unlikely(!eb))
352 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100353
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200354 return ebmb_entry(eb, struct stksess, key);
355}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100356
Emeric Brun819fc6f2017-06-13 19:37:32 +0200357/*
358 * Looks in table <t> for a sticky session with same key as <ts>.
359 * Returns pointer on requested sticky session or NULL if none was found.
360 * The refcount of the found entry is increased and this function
361 * is protected using the table lock
362 */
363struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
364{
365 struct stksess *lts;
366
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100367 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200368 lts = __stktable_lookup(t, ts);
369 if (lts)
370 lts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200372
373 return lts;
374}
375
Willy Tarreaucb183642010-06-06 17:58:34 +0200376/* Update the expiration timer for <ts> but do not touch its expiration node.
377 * The table's expiration timer is updated if set.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200378 * The node will be also inserted into the update tree if needed, at a position
379 * depending if the update is a local or coming from a remote node
Willy Tarreaucb183642010-06-06 17:58:34 +0200380 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200381void __stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200382{
Emeric Brun85e77c72010-09-23 18:16:52 +0200383 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200384 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200385 if (t->expire) {
386 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
387 task_queue(t->exp_task);
388 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200389
Emeric Brun819fc6f2017-06-13 19:37:32 +0200390 /* If sync is enabled */
391 if (t->sync_task) {
392 if (local) {
393 /* If this entry is not in the tree
394 or not scheduled for at least one peer */
395 if (!ts->upd.node.leaf_p
396 || (int)(t->commitupdate - ts->upd.key) >= 0
397 || (int)(ts->upd.key - t->localupdate) >= 0) {
398 ts->upd.key = ++t->update;
399 t->localupdate = t->update;
400 eb32_delete(&ts->upd);
401 eb = eb32_insert(&t->updates, &ts->upd);
402 if (eb != &ts->upd) {
403 eb32_delete(eb);
404 eb32_insert(&t->updates, &ts->upd);
405 }
Emeric Brunaaf58602015-06-15 17:23:30 +0200406 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200407 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
Emeric Brun85e77c72010-09-23 18:16:52 +0200408 }
Emeric Brun819fc6f2017-06-13 19:37:32 +0200409 else {
410 /* If this entry is not in the tree */
411 if (!ts->upd.node.leaf_p) {
412 ts->upd.key= (++t->update)+(2147483648U);
413 eb = eb32_insert(&t->updates, &ts->upd);
414 if (eb != &ts->upd) {
415 eb32_delete(eb);
416 eb32_insert(&t->updates, &ts->upd);
417 }
418 }
419 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200420 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200421}
422
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200423/* Update the expiration timer for <ts> but do not touch its expiration node.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200424 * The table's expiration timer is updated using the date of expiration coming from
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200425 * <t> stick-table configuration.
Emeric Brun819fc6f2017-06-13 19:37:32 +0200426 * The node will be also inserted into the update tree if needed, at a position
427 * considering the update is coming from a remote node
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200428 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200429void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt)
430{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100431 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200432 __stktable_touch_with_exp(t, ts, 0, ts->expire);
433 if (decrefcnt)
434 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100435 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200436}
437
438/* Update the expiration timer for <ts> but do not touch its expiration node.
439 * The table's expiration timer is updated using the date of expiration coming from
440 * <t> stick-table configuration.
441 * The node will be also inserted into the update tree if needed, at a position
442 * considering the update was made locally
443 */
444void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefcnt)
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200445{
446 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
447
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100448 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200449 __stktable_touch_with_exp(t, ts, 1, expire);
450 if (decrefcnt)
451 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100452 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200453}
Willy Tarreau43e90352018-06-27 06:25:57 +0200454/* Just decrease the ref_cnt of the current session. Does nothing if <ts> is NULL */
455static void stktable_release(struct stktable *t, struct stksess *ts)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200456{
Willy Tarreau43e90352018-06-27 06:25:57 +0200457 if (!ts)
458 return;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100459 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200460 ts->ref_cnt--;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100461 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200462}
463
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200464/* Insert new sticky session <ts> in the table. It is assumed that it does not
465 * yet exist (the caller must check this). The table's timeout is updated if it
466 * is set. <ts> is returned.
467 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200468void __stktable_store(struct stktable *t, struct stksess *ts)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200469{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100470
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200471 ebmb_insert(&t->keys, &ts->key, t->key_size);
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200472 ts->exp.key = ts->expire;
473 eb32_insert(&t->exps, &ts->exp);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200474 if (t->expire) {
475 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
476 task_queue(t->exp_task);
477 }
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200478}
479
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200480/* Returns a valid or initialized stksess for the specified stktable_key in the
481 * specified table, or NULL if the key was NULL, or if no entry was found nor
482 * could be created. The entry's expiration is updated.
483 */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200484struct stksess *__stktable_get_entry(struct stktable *table, struct stktable_key *key)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200485{
486 struct stksess *ts;
487
488 if (!key)
489 return NULL;
490
Emeric Brun819fc6f2017-06-13 19:37:32 +0200491 ts = __stktable_lookup_key(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200492 if (ts == NULL) {
493 /* entry does not exist, initialize a new one */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200494 ts = __stksess_new(table, key);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200495 if (!ts)
496 return NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200497 __stktable_store(table, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200498 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200499 return ts;
500}
Emeric Brun819fc6f2017-06-13 19:37:32 +0200501/* Returns a valid or initialized stksess for the specified stktable_key in the
502 * specified table, or NULL if the key was NULL, or if no entry was found nor
503 * could be created. The entry's expiration is updated.
504 * This function locks the table, and the refcount of the entry is increased.
505 */
506struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
507{
508 struct stksess *ts;
509
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100510 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200511 ts = __stktable_get_entry(table, key);
512 if (ts)
513 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100514 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200515
516 return ts;
517}
518
519/* Lookup for an entry with the same key and store the submitted
520 * stksess if not found.
521 */
522struct stksess *__stktable_set_entry(struct stktable *table, struct stksess *nts)
523{
524 struct stksess *ts;
525
526 ts = __stktable_lookup(table, nts);
527 if (ts == NULL) {
528 ts = nts;
529 __stktable_store(table, ts);
530 }
531 return ts;
532}
533
534/* Lookup for an entry with the same key and store the submitted
535 * stksess if not found.
536 * This function locks the table, and the refcount of the entry is increased.
537 */
538struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts)
539{
540 struct stksess *ts;
541
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100542 HA_SPIN_LOCK(STK_TABLE_LOCK, &table->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200543 ts = __stktable_set_entry(table, nts);
544 ts->ref_cnt++;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &table->lock);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200546
Emeric Brun819fc6f2017-06-13 19:37:32 +0200547 return ts;
548}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100549/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200550 * Trash expired sticky sessions from table <t>. The next expiration date is
551 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100552 */
553static int stktable_trash_expired(struct stktable *t)
554{
555 struct stksess *ts;
556 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200557 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100558
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100559 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100560 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
561
562 while (1) {
563 if (unlikely(!eb)) {
564 /* we might have reached the end of the tree, typically because
565 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200566 * half. Let's loop back to the beginning of the tree now if we
567 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100568 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200569 if (looped)
570 break;
571 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100572 eb = eb32_first(&t->exps);
573 if (likely(!eb))
574 break;
575 }
576
577 if (likely(tick_is_lt(now_ms, eb->key))) {
578 /* timer not expired yet, revisit it later */
579 t->exp_next = eb->key;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100580 goto out_unlock;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100581 }
582
583 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200584 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100585 eb = eb32_next(eb);
586
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200587 /* don't delete an entry which is currently referenced */
588 if (ts->ref_cnt)
589 continue;
590
Willy Tarreau86257dc2010-06-06 12:57:10 +0200591 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100592
593 if (!tick_is_expired(ts->expire, now_ms)) {
594 if (!tick_isset(ts->expire))
595 continue;
596
Willy Tarreau86257dc2010-06-06 12:57:10 +0200597 ts->exp.key = ts->expire;
598 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100599
Willy Tarreau86257dc2010-06-06 12:57:10 +0200600 if (!eb || eb->key > ts->exp.key)
601 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100602 continue;
603 }
604
605 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200606 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200607 eb32_delete(&ts->upd);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200608 __stksess_free(t, ts);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100609 }
610
611 /* We have found no task to expire in any tree */
612 t->exp_next = TICK_ETERNITY;
Willy Tarreau4d5f13c2017-11-05 11:04:47 +0100613out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100614 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100615 return t->exp_next;
616}
617
618/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200619 * Task processing function to trash expired sticky sessions. A pointer to the
620 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100621 */
Olivier Houchard9f6af332018-05-25 14:04:04 +0200622static struct task *process_table_expire(struct task *task, void *context, unsigned short state)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100623{
Olivier Houchard9f6af332018-05-25 14:04:04 +0200624 struct stktable *t = context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100625
626 task->expire = stktable_trash_expired(t);
627 return task;
628}
629
Willy Tarreauaea940e2010-06-06 11:56:36 +0200630/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100631int stktable_init(struct stktable *t)
632{
633 if (t->size) {
Emeric Brun819fc6f2017-06-13 19:37:32 +0200634 t->keys = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100635 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100636 t->updates = EB_ROOT_UNIQUE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100637 HA_SPIN_INIT(&t->lock);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100638
Olivier Houchard52dabbc2018-11-14 17:54:36 +0100639 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 +0100640
641 t->exp_next = TICK_ETERNITY;
642 if ( t->expire ) {
Emeric Brunc60def82017-09-27 14:59:38 +0200643 t->exp_task = task_new(MAX_THREADS_MASK);
Willy Tarreau848522f2018-10-15 11:12:15 +0200644 if (!t->exp_task)
645 return 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100646 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100647 t->exp_task->context = (void *)t;
648 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200649 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200650 peers_register_table(t->peers.p, t);
651 }
652
Emeric Brun3bd697e2010-01-04 15:23:48 +0100653 return t->pool != NULL;
654 }
655 return 1;
656}
657
658/*
659 * Configuration keywords of known table types
660 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200661struct stktable_type stktable_types[SMP_TYPES] = {
662 [SMP_T_SINT] = { "integer", 0, 4 },
663 [SMP_T_IPV4] = { "ip", 0, 4 },
664 [SMP_T_IPV6] = { "ipv6", 0, 16 },
665 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
666 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
667};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100668
669/*
670 * Parse table type configuration.
671 * Returns 0 on successful parsing, else 1.
672 * <myidx> is set at next configuration <args> index.
673 */
674int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
675{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200676 for (*type = 0; *type < SMP_TYPES; (*type)++) {
677 if (!stktable_types[*type].kw)
678 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100679 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
680 continue;
681
682 *key_size = stktable_types[*type].default_size;
683 (*myidx)++;
684
Willy Tarreauaea940e2010-06-06 11:56:36 +0200685 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100686 if (strcmp("len", args[*myidx]) == 0) {
687 (*myidx)++;
688 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200689 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100690 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200691 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200692 /* null terminated string needs +1 for '\0'. */
693 (*key_size)++;
694 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100695 (*myidx)++;
696 }
697 }
698 return 0;
699 }
700 return 1;
701}
702
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100703/*
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100704 * Parse a line with <linenum> as number in <file> configuration file to configure
705 * the stick-table with <t> as address and <id> as ID.
706 * <peers> provides the "peers" section pointer only if this function is called
707 * from a "peers" section.
708 * <nid> is the stick-table name which is sent over the network. It must be equal
709 * to <id> if this stick-table is parsed from a proxy section, and prefixed by <peers>
710 * "peers" section name followed by a '/' character if parsed from a "peers" section.
711 * This is the responsability of the caller to check this.
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100712 * Return an error status with ERR_* flags set if required, 0 if no error was encountered.
713 */
714int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100715 struct stktable *t, char *id, char *nid, struct peers *peers)
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100716{
717 int err_code = 0;
718 int idx = 1;
719 unsigned int val;
720
721 if (!id || !*id) {
722 ha_alert("parsing [%s:%d] : %s: ID not provided.\n", file, linenum, args[0]);
723 err_code |= ERR_ALERT | ERR_ABORT;
724 goto out;
725 }
726
727 /* Store the "peers" section if this function is called from a "peers" section. */
728 if (peers) {
729 t->peers.p = peers;
730 idx++;
731 }
732
733 t->id = id;
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100734 t->nid = nid;
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100735 t->type = (unsigned int)-1;
736 t->conf.file = file;
737 t->conf.line = linenum;
738
739 while (*args[idx]) {
740 const char *err;
741
742 if (strcmp(args[idx], "size") == 0) {
743 idx++;
744 if (!*(args[idx])) {
745 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
746 file, linenum, args[0], args[idx-1]);
747 err_code |= ERR_ALERT | ERR_FATAL;
748 goto out;
749 }
750 if ((err = parse_size_err(args[idx], &t->size))) {
751 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
752 file, linenum, args[0], *err, args[idx-1]);
753 err_code |= ERR_ALERT | ERR_FATAL;
754 goto out;
755 }
756 idx++;
757 }
758 /* This argument does not exit in "peers" section. */
759 else if (!peers && strcmp(args[idx], "peers") == 0) {
760 idx++;
761 if (!*(args[idx])) {
762 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
763 file, linenum, args[0], args[idx-1]);
764 err_code |= ERR_ALERT | ERR_FATAL;
765 goto out;
766 }
767 t->peers.name = strdup(args[idx++]);
768 }
769 else if (strcmp(args[idx], "expire") == 0) {
770 idx++;
771 if (!*(args[idx])) {
772 ha_alert("parsing [%s:%d] : %s: missing argument after '%s'.\n",
773 file, linenum, args[0], args[idx-1]);
774 err_code |= ERR_ALERT | ERR_FATAL;
775 goto out;
776 }
777 err = parse_time_err(args[idx], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200778 if (err == PARSE_TIME_OVER) {
779 ha_alert("parsing [%s:%d]: %s: timer overflow in argument <%s> to <%s>, maximum value is 2147483647 ms (~24.8 days).\n",
780 file, linenum, args[0], args[idx], args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100781 err_code |= ERR_ALERT | ERR_FATAL;
782 goto out;
783 }
Willy Tarreau9faebe32019-06-07 19:00:37 +0200784 else if (err == PARSE_TIME_UNDER) {
785 ha_alert("parsing [%s:%d]: %s: timer underflow in argument <%s> to <%s>, minimum non-null value is 1 ms.\n",
786 file, linenum, args[0], args[idx], args[idx-1]);
787 err_code |= ERR_ALERT | ERR_FATAL;
788 goto out;
789 }
790 else if (err) {
791 ha_alert("parsing [%s:%d] : %s: unexpected character '%c' in argument of '%s'.\n",
792 file, linenum, args[0], *err, args[idx-1]);
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100793 err_code |= ERR_ALERT | ERR_FATAL;
794 goto out;
795 }
796 t->expire = val;
797 idx++;
798 }
799 else if (strcmp(args[idx], "nopurge") == 0) {
800 t->nopurge = 1;
801 idx++;
802 }
803 else if (strcmp(args[idx], "type") == 0) {
804 idx++;
805 if (stktable_parse_type(args, &idx, &t->type, &t->key_size) != 0) {
806 ha_alert("parsing [%s:%d] : %s: unknown type '%s'.\n",
807 file, linenum, args[0], args[idx]);
808 err_code |= ERR_ALERT | ERR_FATAL;
809 goto out;
810 }
811 /* idx already points to next arg */
812 }
813 else if (strcmp(args[idx], "store") == 0) {
814 int type, err;
815 char *cw, *nw, *sa;
816
817 idx++;
818 nw = args[idx];
819 while (*nw) {
820 /* the "store" keyword supports a comma-separated list */
821 cw = nw;
822 sa = NULL; /* store arg */
823 while (*nw && *nw != ',') {
824 if (*nw == '(') {
825 *nw = 0;
826 sa = ++nw;
827 while (*nw != ')') {
828 if (!*nw) {
829 ha_alert("parsing [%s:%d] : %s: missing closing parenthesis after store option '%s'.\n",
830 file, linenum, args[0], cw);
831 err_code |= ERR_ALERT | ERR_FATAL;
832 goto out;
833 }
834 nw++;
835 }
836 *nw = '\0';
837 }
838 nw++;
839 }
840 if (*nw)
841 *nw++ = '\0';
842 type = stktable_get_data_type(cw);
843 if (type < 0) {
844 ha_alert("parsing [%s:%d] : %s: unknown store option '%s'.\n",
845 file, linenum, args[0], cw);
846 err_code |= ERR_ALERT | ERR_FATAL;
847 goto out;
848 }
849
850 err = stktable_alloc_data_type(t, type, sa);
851 switch (err) {
852 case PE_NONE: break;
853 case PE_EXIST:
854 ha_warning("parsing [%s:%d]: %s: store option '%s' already enabled, ignored.\n",
855 file, linenum, args[0], cw);
856 err_code |= ERR_WARN;
857 break;
858
859 case PE_ARG_MISSING:
860 ha_alert("parsing [%s:%d] : %s: missing argument to store option '%s'.\n",
861 file, linenum, args[0], cw);
862 err_code |= ERR_ALERT | ERR_FATAL;
863 goto out;
864
865 case PE_ARG_NOT_USED:
866 ha_alert("parsing [%s:%d] : %s: unexpected argument to store option '%s'.\n",
867 file, linenum, args[0], cw);
868 err_code |= ERR_ALERT | ERR_FATAL;
869 goto out;
870
871 default:
872 ha_alert("parsing [%s:%d] : %s: error when processing store option '%s'.\n",
873 file, linenum, args[0], cw);
874 err_code |= ERR_ALERT | ERR_FATAL;
875 goto out;
876 }
877 }
878 idx++;
879 }
880 else {
881 ha_alert("parsing [%s:%d] : %s: unknown argument '%s'.\n",
882 file, linenum, args[0], args[idx]);
883 err_code |= ERR_ALERT | ERR_FATAL;
884 goto out;
885 }
886 }
887
888 if (!t->size) {
889 ha_alert("parsing [%s:%d] : %s: missing size.\n",
890 file, linenum, args[0]);
891 err_code |= ERR_ALERT | ERR_FATAL;
892 goto out;
893 }
894
895 if (t->type == (unsigned int)-1) {
896 ha_alert("parsing [%s:%d] : %s: missing type.\n",
897 file, linenum, args[0]);
898 err_code |= ERR_ALERT | ERR_FATAL;
899 goto out;
900 }
901
902 out:
903 return err_code;
904}
905
Willy Tarreau8fed9032014-07-03 17:02:46 +0200906/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200907 * Note that the sample *is* modified and that the returned key may point
908 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200909 * Returns NULL if the sample could not be converted (eg: no matching type),
910 * otherwise a pointer to the static stktable_key filled with what is needed
911 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200912 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200913struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200914{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200915 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200916 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200917 return NULL;
918
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200919 /* Fill static_table_key. */
920 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200921
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200922 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +0200923 static_table_key.key = &smp->data.u.ipv4;
924 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200925 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200926
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200927 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +0200928 static_table_key.key = &smp->data.u.ipv6;
929 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200930 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200931
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200932 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200933 /* The stick table require a 32bit unsigned int, "sint" is a
934 * signed 64 it, so we can convert it inplace.
935 */
Willy Tarreau6fe22ed2019-10-23 06:21:05 +0200936 smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +0200937 static_table_key.key = &smp->data.u.sint;
938 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200939 break;
940
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200941 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200942 if (!smp_make_safe(smp))
943 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200944 static_table_key.key = smp->data.u.str.area;
945 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200946 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200947
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200948 case SMP_T_BIN:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200949 if (smp->data.u.str.data < t->key_size) {
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200950 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200951 if (!smp_make_rw(smp))
952 return NULL;
953
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200954 if (smp->data.u.str.size < t->key_size)
955 if (!smp_dup(smp))
956 return NULL;
957 if (smp->data.u.str.size < t->key_size)
958 return NULL;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200959 memset(smp->data.u.str.area + smp->data.u.str.data, 0,
960 t->key_size - smp->data.u.str.data);
961 smp->data.u.str.data = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200962 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200963 static_table_key.key = smp->data.u.str.area;
964 static_table_key.key_len = smp->data.u.str.data;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200965 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200966
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200967 default: /* impossible case. */
968 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200969 }
970
Christopher Fauletca20d022017-08-29 15:30:31 +0200971 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200972}
973
974/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200975 * Process a fetch + format conversion as defined by the sample expression <expr>
976 * on request or response considering the <opt> parameter. Returns either NULL if
977 * no key could be extracted, or a pointer to the converted result stored in
978 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
979 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200980 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
981 * without SMP_OPT_FINAL). The output will be usable like this :
982 *
983 * return MAY_CHANGE FINAL Meaning for the sample
984 * NULL 0 * Not present and will never be (eg: header)
985 * NULL 1 0 Not present or unstable, could change (eg: req_len)
986 * NULL 1 1 Not present, will not change anymore
987 * smp 0 * Present and will not change (eg: header)
988 * smp 1 0 not possible
989 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200990 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200991struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200992 unsigned int opt, struct sample_expr *expr, struct sample *smp)
993{
994 if (smp)
995 memset(smp, 0, sizeof(*smp));
996
Willy Tarreau192252e2015-04-04 01:47:55 +0200997 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200998 if (!smp)
999 return NULL;
1000
1001 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1002 return NULL; /* we can only use stable samples */
1003
1004 return smp_to_stkey(smp, t);
1005}
1006
1007/*
Willy Tarreau12785782012-04-27 21:37:17 +02001008 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001009 * type <table_type>, otherwise zero. Used in configuration check.
1010 */
Willy Tarreau12785782012-04-27 21:37:17 +02001011int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001012{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001013 int out_type;
1014
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001015 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001016 return 0;
1017
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001018 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001019
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +02001020 /* Convert sample. */
1021 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +01001022 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001023
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001024 return 1;
1025}
Emeric Brun3bd697e2010-01-04 15:23:48 +01001026
Willy Tarreauedee1d62014-07-15 16:44:27 +02001027/* Extra data types processing : after the last one, some room may remain
1028 * before STKTABLE_DATA_TYPES that may be used to register extra data types
1029 * at run time.
1030 */
Willy Tarreau08d5f982010-06-06 13:34:54 +02001031struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001032 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +02001033 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001034 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +02001035 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +02001036 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
1037 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1038 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
1039 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
1040 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1041 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
1042 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1043 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
1044 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1045 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
1046 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
1047 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
1048 [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 +01001049 [STKTABLE_DT_GPC1] = { .name = "gpc1", .std_type = STD_T_UINT },
1050 [STKTABLE_DT_GPC1_RATE] = { .name = "gpc1_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Frédéric Lécaille5ad57ea2019-05-17 10:08:29 +02001051 [STKTABLE_DT_SERVER_NAME] = { .name = "server_name", .std_type = STD_T_DICT },
Willy Tarreau08d5f982010-06-06 13:34:54 +02001052};
1053
Willy Tarreauedee1d62014-07-15 16:44:27 +02001054/* Registers stick-table extra data type with index <idx>, name <name>, type
1055 * <std_type> and arg type <arg_type>. If the index is negative, the next free
1056 * index is automatically allocated. The allocated index is returned, or -1 if
1057 * no free index was found or <name> was already registered. The <name> is used
1058 * directly as a pointer, so if it's not stable, the caller must allocate it.
1059 */
1060int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
1061{
1062 if (idx < 0) {
1063 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
1064 if (!stktable_data_types[idx].name)
1065 break;
1066
1067 if (strcmp(stktable_data_types[idx].name, name) == 0)
1068 return -1;
1069 }
1070 }
1071
1072 if (idx >= STKTABLE_DATA_TYPES)
1073 return -1;
1074
1075 if (stktable_data_types[idx].name != NULL)
1076 return -1;
1077
1078 stktable_data_types[idx].name = name;
1079 stktable_data_types[idx].std_type = std_type;
1080 stktable_data_types[idx].arg_type = arg_type;
1081 return idx;
1082}
1083
Willy Tarreau08d5f982010-06-06 13:34:54 +02001084/*
1085 * Returns the data type number for the stktable_data_type whose name is <name>,
1086 * or <0 if not found.
1087 */
1088int stktable_get_data_type(char *name)
1089{
1090 int type;
1091
1092 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +02001093 if (!stktable_data_types[type].name)
1094 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +02001095 if (strcmp(name, stktable_data_types[type].name) == 0)
1096 return type;
1097 }
1098 return -1;
1099}
1100
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001101/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1102 * it up into this table. Returns true if found, false otherwise. The input
1103 * type is STR so that input samples are converted to string (since all types
1104 * can be converted to strings), then the function casts the string again into
1105 * the table's type. This is a double conversion, but in the future we might
1106 * support automatic input types to perform the cast on the fly.
1107 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001108static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001109{
1110 struct stktable *t;
1111 struct stktable_key *key;
1112 struct stksess *ts;
1113
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001114 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001115
1116 key = smp_to_stkey(smp, t);
1117 if (!key)
1118 return 0;
1119
1120 ts = stktable_lookup_key(t, key);
1121
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001122 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001123 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001124 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau43e90352018-06-27 06:25:57 +02001125 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001126 return 1;
1127}
1128
1129/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1130 * it up into this table. Returns the data rate received from clients in bytes/s
1131 * if the key is present in the table, otherwise zero, so that comparisons can
1132 * be easily performed. If the inspected parameter is not stored in the table,
1133 * <not found> is returned.
1134 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001135static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001136{
1137 struct stktable *t;
1138 struct stktable_key *key;
1139 struct stksess *ts;
1140 void *ptr;
1141
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001142 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001143
1144 key = smp_to_stkey(smp, t);
1145 if (!key)
1146 return 0;
1147
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001148 ts = stktable_lookup_key(t, key);
1149
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001150 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001151 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001152 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001153
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001154 if (!ts) /* key not present */
1155 return 1;
1156
1157 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001158 if (ptr)
1159 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
1160 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001161
Daniel Corbett3e60b112018-05-27 09:47:12 -04001162 stktable_release(t, ts);
1163 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001164}
1165
1166/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1167 * it up into this table. Returns the cumulated number of connections for the key
1168 * if the key is present in the table, otherwise zero, so that comparisons can
1169 * be easily performed. If the inspected parameter is not stored in the table,
1170 * <not found> is returned.
1171 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001172static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001173{
1174 struct stktable *t;
1175 struct stktable_key *key;
1176 struct stksess *ts;
1177 void *ptr;
1178
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001179 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001180
1181 key = smp_to_stkey(smp, t);
1182 if (!key)
1183 return 0;
1184
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001185 ts = stktable_lookup_key(t, key);
1186
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001187 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001188 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001189 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001190
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001191 if (!ts) /* key not present */
1192 return 1;
1193
1194 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001195 if (ptr)
1196 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001197
Daniel Corbett3e60b112018-05-27 09:47:12 -04001198 stktable_release(t, ts);
1199 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001200}
1201
1202/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1203 * it up into this table. Returns the number of concurrent connections for the
1204 * key if the key is present in the table, otherwise zero, so that comparisons
1205 * can be easily performed. If the inspected parameter is not stored in the
1206 * table, <not found> is returned.
1207 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001208static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001209{
1210 struct stktable *t;
1211 struct stktable_key *key;
1212 struct stksess *ts;
1213 void *ptr;
1214
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001215 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001216
1217 key = smp_to_stkey(smp, t);
1218 if (!key)
1219 return 0;
1220
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001221 ts = stktable_lookup_key(t, key);
1222
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001223 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001224 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001225 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001226
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001227 if (!ts) /* key not present */
1228 return 1;
1229
1230 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001231 if (ptr)
1232 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
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 rate of incoming connections from 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_rate(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_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001267 if (ptr)
1268 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
1269 t->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001270
Daniel Corbett3e60b112018-05-27 09:47:12 -04001271 stktable_release(t, ts);
1272 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001273}
1274
1275/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1276 * it up into this table. Returns the data rate sent to clients in bytes/s
1277 * if the key is present in the table, otherwise zero, so that comparisons can
1278 * be easily performed. If the inspected parameter is not stored in the table,
1279 * <not found> is returned.
1280 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001281static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001282{
1283 struct stktable *t;
1284 struct stktable_key *key;
1285 struct stksess *ts;
1286 void *ptr;
1287
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001288 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001289
1290 key = smp_to_stkey(smp, t);
1291 if (!key)
1292 return 0;
1293
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001294 ts = stktable_lookup_key(t, key);
1295
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001296 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001297 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001298 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001299
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001300 if (!ts) /* key not present */
1301 return 1;
1302
1303 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001304 if (ptr)
1305 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
1306 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001307
Daniel Corbett3e60b112018-05-27 09:47:12 -04001308 stktable_release(t, ts);
1309 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001310}
1311
1312/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001313 * it up into this table. Returns the value of the GPT0 tag for the key
1314 * if the key is present in the table, otherwise false, so that comparisons can
1315 * be easily performed. If the inspected parameter is not stored in the table,
1316 * <not found> is returned.
1317 */
1318static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
1319{
1320 struct stktable *t;
1321 struct stktable_key *key;
1322 struct stksess *ts;
1323 void *ptr;
1324
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001325 t = arg_p[0].data.t;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001326
1327 key = smp_to_stkey(smp, t);
1328 if (!key)
1329 return 0;
1330
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001331 ts = stktable_lookup_key(t, key);
1332
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001333 smp->flags = SMP_F_VOL_TEST;
1334 smp->data.type = SMP_T_SINT;
1335 smp->data.u.sint = 0;
1336
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001337 if (!ts) /* key not present */
1338 return 1;
1339
1340 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001341 if (ptr)
1342 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001343
Daniel Corbett3e60b112018-05-27 09:47:12 -04001344 stktable_release(t, ts);
1345 return !!ptr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001346}
1347
1348/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001349 * it up into this table. Returns the value of the GPC0 counter for the key
1350 * if the key is present in the table, otherwise zero, so that comparisons can
1351 * be easily performed. If the inspected parameter is not stored in the table,
1352 * <not found> is returned.
1353 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001354static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355{
1356 struct stktable *t;
1357 struct stktable_key *key;
1358 struct stksess *ts;
1359 void *ptr;
1360
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001361 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001362
1363 key = smp_to_stkey(smp, t);
1364 if (!key)
1365 return 0;
1366
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001367 ts = stktable_lookup_key(t, key);
1368
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001369 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001370 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001371 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001372
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001373 if (!ts) /* key not present */
1374 return 1;
1375
1376 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001377 if (ptr)
1378 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
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
1385 * it up into this table. Returns the event rate of the GPC0 counter for the key
1386 * if the key is present in the table, otherwise zero, so that comparisons can
1387 * be easily performed. If the inspected parameter is not stored in the table,
1388 * <not found> is returned.
1389 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001390static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001391{
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;
Willy Tarreaud9f316a2014-07-10 14:03:38 +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
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001405 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001406 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001407 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001408
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001409 if (!ts) /* key not present */
1410 return 1;
1411
1412 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001413 if (ptr)
1414 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1415 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001416
Daniel Corbett3e60b112018-05-27 09:47:12 -04001417 stktable_release(t, ts);
1418 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001419}
1420
1421/* 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 +01001422 * it up into this table. Returns the value of the GPC1 counter for the key
1423 * if the key is present in the table, otherwise zero, so that comparisons can
1424 * be easily performed. If the inspected parameter is not stored in the table,
1425 * <not found> is returned.
1426 */
1427static int sample_conv_table_gpc1(const struct arg *arg_p, struct sample *smp, void *private)
1428{
1429 struct stktable *t;
1430 struct stktable_key *key;
1431 struct stksess *ts;
1432 void *ptr;
1433
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001434 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001435
1436 key = smp_to_stkey(smp, t);
1437 if (!key)
1438 return 0;
1439
1440 ts = stktable_lookup_key(t, key);
1441
1442 smp->flags = SMP_F_VOL_TEST;
1443 smp->data.type = SMP_T_SINT;
1444 smp->data.u.sint = 0;
1445
1446 if (!ts) /* key not present */
1447 return 1;
1448
1449 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001450 if (ptr)
1451 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001452
Daniel Corbett3e60b112018-05-27 09:47:12 -04001453 stktable_release(t, ts);
1454 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001455}
1456
1457/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1458 * it up into this table. Returns the event rate of the GPC1 counter for the key
1459 * if the key is present in the table, otherwise zero, so that comparisons can
1460 * be easily performed. If the inspected parameter is not stored in the table,
1461 * <not found> is returned.
1462 */
1463static int sample_conv_table_gpc1_rate(const struct arg *arg_p, struct sample *smp, void *private)
1464{
1465 struct stktable *t;
1466 struct stktable_key *key;
1467 struct stksess *ts;
1468 void *ptr;
1469
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001470 t = arg_p[0].data.t;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001471
1472 key = smp_to_stkey(smp, t);
1473 if (!key)
1474 return 0;
1475
1476 ts = stktable_lookup_key(t, key);
1477
1478 smp->flags = SMP_F_VOL_TEST;
1479 smp->data.type = SMP_T_SINT;
1480 smp->data.u.sint = 0;
1481
1482 if (!ts) /* key not present */
1483 return 1;
1484
1485 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC1_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001486 if (ptr)
1487 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
1488 t->data_arg[STKTABLE_DT_GPC1_RATE].u);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001489
Daniel Corbett3e60b112018-05-27 09:47:12 -04001490 stktable_release(t, ts);
1491 return !!ptr;
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001492}
1493
1494/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001495 * it up into this table. Returns the cumulated number of HTTP request errors
1496 * for the key if the key is present in the table, otherwise zero, so that
1497 * comparisons can be easily performed. If the inspected parameter is not stored
1498 * in the table, <not found> is returned.
1499 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001500static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001501{
1502 struct stktable *t;
1503 struct stktable_key *key;
1504 struct stksess *ts;
1505 void *ptr;
1506
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001507 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001508
1509 key = smp_to_stkey(smp, t);
1510 if (!key)
1511 return 0;
1512
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001513 ts = stktable_lookup_key(t, key);
1514
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001515 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001516 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001517 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001518
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001519 if (!ts) /* key not present */
1520 return 1;
1521
1522 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001523 if (ptr)
1524 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001525
Daniel Corbett3e60b112018-05-27 09:47:12 -04001526 stktable_release(t, ts);
1527 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001528}
1529
1530/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1531 * it up into this table. Returns the HTTP request error rate the key
1532 * if the key is present in the table, otherwise zero, so that comparisons can
1533 * be easily performed. If the inspected parameter is not stored in the table,
1534 * <not found> is returned.
1535 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001536static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001537{
1538 struct stktable *t;
1539 struct stktable_key *key;
1540 struct stksess *ts;
1541 void *ptr;
1542
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001543 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001544
1545 key = smp_to_stkey(smp, t);
1546 if (!key)
1547 return 0;
1548
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001549 ts = stktable_lookup_key(t, key);
1550
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001551 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001552 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001553 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001554
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001555 if (!ts) /* key not present */
1556 return 1;
1557
1558 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001559 if (ptr)
1560 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
1561 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001562
Daniel Corbett3e60b112018-05-27 09:47:12 -04001563 stktable_release(t, ts);
1564 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001565}
1566
1567/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1568 * it up into this table. Returns the cumulated number of HTTP request for the
1569 * key if the key is present in the table, otherwise zero, so that comparisons
1570 * can be easily performed. If the inspected parameter is not stored in the
1571 * table, <not found> is returned.
1572 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001573static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001574{
1575 struct stktable *t;
1576 struct stktable_key *key;
1577 struct stksess *ts;
1578 void *ptr;
1579
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001580 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001581
1582 key = smp_to_stkey(smp, t);
1583 if (!key)
1584 return 0;
1585
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001586 ts = stktable_lookup_key(t, key);
1587
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001588 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001589 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001590 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001591
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001592 if (!ts) /* key not present */
1593 return 1;
1594
1595 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001596 if (ptr)
1597 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001598
Daniel Corbett3e60b112018-05-27 09:47:12 -04001599 stktable_release(t, ts);
1600 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001601}
1602
1603/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1604 * it up into this table. Returns the HTTP request rate the key if the key is
1605 * present in the table, otherwise zero, so that comparisons can be easily
1606 * performed. If the inspected parameter is not stored in the table, <not found>
1607 * is returned.
1608 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001609static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001610{
1611 struct stktable *t;
1612 struct stktable_key *key;
1613 struct stksess *ts;
1614 void *ptr;
1615
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001616 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001617
1618 key = smp_to_stkey(smp, t);
1619 if (!key)
1620 return 0;
1621
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001622 ts = stktable_lookup_key(t, key);
1623
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001624 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001625 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001626 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001627
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001628 if (!ts) /* key not present */
1629 return 1;
1630
1631 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001632 if (ptr)
1633 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
1634 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001635
Daniel Corbett3e60b112018-05-27 09:47:12 -04001636 stktable_release(t, ts);
1637 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001638}
1639
1640/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1641 * it up into this table. Returns the volume of datareceived from clients in kbytes
1642 * if the key is present in the table, otherwise zero, so that comparisons can
1643 * be easily performed. If the inspected parameter is not stored in the table,
1644 * <not found> is returned.
1645 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001646static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001647{
1648 struct stktable *t;
1649 struct stktable_key *key;
1650 struct stksess *ts;
1651 void *ptr;
1652
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001653 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001654
1655 key = smp_to_stkey(smp, t);
1656 if (!key)
1657 return 0;
1658
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001659 ts = stktable_lookup_key(t, key);
1660
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001661 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001662 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001663 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001664
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001665 if (!ts) /* key not present */
1666 return 1;
1667
1668 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001669 if (ptr)
1670 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001671
Daniel Corbett3e60b112018-05-27 09:47:12 -04001672 stktable_release(t, ts);
1673 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001674}
1675
1676/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1677 * it up into this table. Returns the volume of data sent to clients in kbytes
1678 * if the key is present in the table, otherwise zero, so that comparisons can
1679 * be easily performed. If the inspected parameter is not stored in the table,
1680 * <not found> is returned.
1681 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001682static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001683{
1684 struct stktable *t;
1685 struct stktable_key *key;
1686 struct stksess *ts;
1687 void *ptr;
1688
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001689 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001690
1691 key = smp_to_stkey(smp, t);
1692 if (!key)
1693 return 0;
1694
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001695 ts = stktable_lookup_key(t, key);
1696
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001697 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001698 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001699 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001700
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001701 if (!ts) /* key not present */
1702 return 1;
1703
1704 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001705 if (ptr)
1706 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001707
Daniel Corbett3e60b112018-05-27 09:47:12 -04001708 stktable_release(t, ts);
1709 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001710}
1711
1712/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1713 * it up into this table. Returns the server ID associated with the key if the
1714 * key is present in the table, otherwise zero, so that comparisons can be
1715 * easily performed. If the inspected parameter is not stored in the table,
1716 * <not found> is returned.
1717 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001718static int sample_conv_table_server_id(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_SERVER_ID);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001741 if (ptr)
1742 smp->data.u.sint = stktable_data_cast(ptr, server_id);
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 cumulated number of sessions for the
1750 * key if the key is present in the table, otherwise zero, so that comparisons
1751 * can be easily performed. If the inspected parameter is not stored in the
1752 * table, <not found> is returned.
1753 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001754static int sample_conv_table_sess_cnt(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_SESS_CNT);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001777 if (ptr)
1778 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001779
Daniel Corbett3e60b112018-05-27 09:47:12 -04001780 stktable_release(t, ts);
1781 return !!ptr;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001782}
1783
1784/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1785 * it up into this table. Returns the session rate the key if the key is
1786 * present in the table, otherwise zero, so that comparisons can be easily
1787 * performed. If the inspected parameter is not stored in the table, <not found>
1788 * is returned.
1789 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001790static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001791{
1792 struct stktable *t;
1793 struct stktable_key *key;
1794 struct stksess *ts;
1795 void *ptr;
1796
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001797 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001798
1799 key = smp_to_stkey(smp, t);
1800 if (!key)
1801 return 0;
1802
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001803 ts = stktable_lookup_key(t, key);
1804
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001805 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001806 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001807 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001808
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001809 if (!ts) /* key not present */
1810 return 1;
1811
1812 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
Daniel Corbett3e60b112018-05-27 09:47:12 -04001813 if (ptr)
1814 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
1815 t->data_arg[STKTABLE_DT_SESS_RATE].u);
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 amount of concurrent connections tracking
1823 * the same key if the key is present in the table, otherwise zero, so that
1824 * comparisons can be easily performed. If the inspected parameter is not
1825 * stored in the table, <not found> is returned.
1826 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001827static int sample_conv_table_trackers(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
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001833 t = arg_p[0].data.t;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001834
1835 key = smp_to_stkey(smp, t);
1836 if (!key)
1837 return 0;
1838
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001839 ts = stktable_lookup_key(t, key);
1840
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001841 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001842 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001843 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001844
Tim Duesterhus65189c12018-06-26 15:57:29 +02001845 if (!ts)
1846 return 1;
1847
1848 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001849
Daniel Corbett3e60b112018-05-27 09:47:12 -04001850 stktable_release(t, ts);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001851 return 1;
1852}
1853
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001854/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001855static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001856 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001857{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001858 struct stksess *ts;
1859 struct stkctr *stkctr;
1860
1861 /* Extract the stksess, return OK if no stksess available. */
1862 if (s)
1863 stkctr = &s->stkctr[rule->arg.gpc.sc];
1864 else
1865 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001866
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001867 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001868 if (ts) {
1869 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001870
Willy Tarreau79c1e912016-01-25 14:54:45 +01001871 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1872 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001873 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1874 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001875 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001876
1877 if (ptr1)
1878 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau79c1e912016-01-25 14:54:45 +01001879 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001880
Emeric Brun819fc6f2017-06-13 19:37:32 +02001881 if (ptr2)
1882 stktable_data_cast(ptr2, gpc0)++;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001883
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001884 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001885
1886 /* If data was modified, we need to touch to re-schedule sync */
1887 stktable_touch_local(stkctr->table, ts, 0);
1888 }
Willy Tarreau79c1e912016-01-25 14:54:45 +01001889 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001890 return ACT_RET_CONT;
1891}
1892
1893/* This function is a common parser for using variables. It understands
1894 * the formats:
1895 *
1896 * sc-inc-gpc0(<stick-table ID>)
1897 *
1898 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1899 * it returns 1 and the variable <expr> is filled with the pointer to the
1900 * expression to execute.
1901 */
1902static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1903 struct act_rule *rule, char **err)
1904{
1905 const char *cmd_name = args[*arg-1];
1906 char *error;
1907
1908 cmd_name += strlen("sc-inc-gpc0");
1909 if (*cmd_name == '\0') {
1910 /* default stick table id. */
1911 rule->arg.gpc.sc = 0;
1912 } else {
1913 /* parse the stick table id. */
1914 if (*cmd_name != '(') {
1915 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1916 return ACT_RET_PRS_ERR;
1917 }
1918 cmd_name++; /* jump the '(' */
1919 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1920 if (*error != ')') {
1921 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1922 return ACT_RET_PRS_ERR;
1923 }
1924
Christopher Faulete9c8b502019-12-18 10:25:46 +01001925 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001926 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulete9c8b502019-12-18 10:25:46 +01001927 MAX_SESS_STKCTR-1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001928 return ACT_RET_PRS_ERR;
1929 }
1930 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001931 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001932 rule->action_ptr = action_inc_gpc0;
1933 return ACT_RET_PRS_OK;
1934}
1935
1936/* Always returns 1. */
Frédéric Lécaille6778b272018-01-29 15:22:53 +01001937static enum act_return action_inc_gpc1(struct act_rule *rule, struct proxy *px,
1938 struct session *sess, struct stream *s, int flags)
1939{
1940 struct stksess *ts;
1941 struct stkctr *stkctr;
1942
1943 /* Extract the stksess, return OK if no stksess available. */
1944 if (s)
1945 stkctr = &s->stkctr[rule->arg.gpc.sc];
1946 else
1947 stkctr = &sess->stkctr[rule->arg.gpc.sc];
1948
1949 ts = stkctr_entry(stkctr);
1950 if (ts) {
1951 void *ptr1, *ptr2;
1952
1953 /* First, update gpc1_rate if it's tracked. Second, update its gpc1 if tracked. */
1954 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1_RATE);
1955 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC1);
1956 if (ptr1 || ptr2) {
1957 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
1958
1959 if (ptr1)
1960 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
1961 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
1962
1963 if (ptr2)
1964 stktable_data_cast(ptr2, gpc1)++;
1965
1966 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
1967
1968 /* If data was modified, we need to touch to re-schedule sync */
1969 stktable_touch_local(stkctr->table, ts, 0);
1970 }
1971 }
1972 return ACT_RET_CONT;
1973}
1974
1975/* This function is a common parser for using variables. It understands
1976 * the formats:
1977 *
1978 * sc-inc-gpc1(<stick-table ID>)
1979 *
1980 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1981 * it returns 1 and the variable <expr> is filled with the pointer to the
1982 * expression to execute.
1983 */
1984static enum act_parse_ret parse_inc_gpc1(const char **args, int *arg, struct proxy *px,
1985 struct act_rule *rule, char **err)
1986{
1987 const char *cmd_name = args[*arg-1];
1988 char *error;
1989
1990 cmd_name += strlen("sc-inc-gpc1");
1991 if (*cmd_name == '\0') {
1992 /* default stick table id. */
1993 rule->arg.gpc.sc = 0;
1994 } else {
1995 /* parse the stick table id. */
1996 if (*cmd_name != '(') {
1997 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1998 return ACT_RET_PRS_ERR;
1999 }
2000 cmd_name++; /* jump the '(' */
2001 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2002 if (*error != ')') {
2003 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
2004 return ACT_RET_PRS_ERR;
2005 }
2006
Christopher Faulete9c8b502019-12-18 10:25:46 +01002007 if (rule->arg.gpc.sc >= MAX_SESS_STKCTR) {
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002008 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
Christopher Faulete9c8b502019-12-18 10:25:46 +01002009 MAX_SESS_STKCTR-1);
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002010 return ACT_RET_PRS_ERR;
2011 }
2012 }
2013 rule->action = ACT_CUSTOM;
2014 rule->action_ptr = action_inc_gpc1;
2015 return ACT_RET_PRS_OK;
2016}
2017
2018/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002019static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02002020 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002021{
2022 void *ptr;
2023 struct stksess *ts;
2024 struct stkctr *stkctr;
2025
2026 /* Extract the stksess, return OK if no stksess available. */
2027 if (s)
2028 stkctr = &s->stkctr[rule->arg.gpt.sc];
2029 else
2030 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01002031
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002032 ts = stkctr_entry(stkctr);
2033 if (!ts)
2034 return ACT_RET_CONT;
2035
2036 /* Store the sample in the required sc, and ignore errors. */
2037 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002038 if (ptr) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002039 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002040
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002041 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002044
2045 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01002046 }
2047
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002048 return ACT_RET_CONT;
2049}
2050
2051/* This function is a common parser for using variables. It understands
2052 * the format:
2053 *
2054 * set-gpt0(<stick-table ID>) <expression>
2055 *
2056 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
2057 * it returns 1 and the variable <expr> is filled with the pointer to the
2058 * expression to execute.
2059 */
2060static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
2061 struct act_rule *rule, char **err)
2062
2063
2064{
2065 const char *cmd_name = args[*arg-1];
2066 char *error;
2067
2068 cmd_name += strlen("sc-set-gpt0");
2069 if (*cmd_name == '\0') {
2070 /* default stick table id. */
2071 rule->arg.gpt.sc = 0;
2072 } else {
2073 /* parse the stick table id. */
2074 if (*cmd_name != '(') {
2075 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2076 return ACT_RET_PRS_ERR;
2077 }
2078 cmd_name++; /* jump the '(' */
2079 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
2080 if (*error != ')') {
2081 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
2082 return ACT_RET_PRS_ERR;
2083 }
2084
Christopher Faulete9c8b502019-12-18 10:25:46 +01002085 if (rule->arg.gpt.sc >= MAX_SESS_STKCTR) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002086 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
Christopher Faulete9c8b502019-12-18 10:25:46 +01002087 args[*arg-1], MAX_SESS_STKCTR-1);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002088 return ACT_RET_PRS_ERR;
2089 }
2090 }
2091
2092 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
2093 if (*error != '\0') {
2094 memprintf(err, "invalid integer value '%s'", args[*arg]);
2095 return ACT_RET_PRS_ERR;
2096 }
2097 (*arg)++;
2098
Thierry FOURNIER42148732015-09-02 17:17:33 +02002099 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002100 rule->action_ptr = action_set_gpt0;
2101
2102 return ACT_RET_PRS_OK;
2103}
2104
Willy Tarreau7d562212016-11-25 16:10:05 +01002105/* set temp integer to the number of used entries in the table pointed to by expr.
2106 * Accepts exactly 1 argument of type table.
2107 */
2108static int
2109smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2110{
2111 smp->flags = SMP_F_VOL_TEST;
2112 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002113 smp->data.u.sint = args->data.t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002114 return 1;
2115}
2116
2117/* set temp integer to the number of free entries in the table pointed to by expr.
2118 * Accepts exactly 1 argument of type table.
2119 */
2120static int
2121smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
2122{
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002123 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002124
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002125 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002126 smp->flags = SMP_F_VOL_TEST;
2127 smp->data.type = SMP_T_SINT;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002128 smp->data.u.sint = t->size - t->current;
Willy Tarreau7d562212016-11-25 16:10:05 +01002129 return 1;
2130}
2131
2132/* Returns a pointer to a stkctr depending on the fetch keyword name.
2133 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
2134 * sc[0-9]_* will return a pointer to the respective field in the
2135 * stream <l4>. sc_* requires an UINT argument specifying the stick
2136 * counter number. src_* will fill a locally allocated structure with
2137 * the table and entry corresponding to what is specified with src_*.
2138 * NULL may be returned if the designated stkctr is not tracked. For
2139 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2140 * passed. When present, the currently tracked key is then looked up
2141 * in the specified table instead of the current table. The purpose is
2142 * to be able to convery multiple values per key (eg: have gpc0 from
2143 * multiple tables). <strm> is allowed to be NULL, in which case only
2144 * the session will be consulted.
2145 */
2146struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002147smp_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 +01002148{
Willy Tarreau7d562212016-11-25 16:10:05 +01002149 struct stkctr *stkptr;
2150 struct stksess *stksess;
2151 unsigned int num = kw[2] - '0';
2152 int arg = 0;
2153
2154 if (num == '_' - '0') {
2155 /* sc_* variant, args[0] = ctr# (mandatory) */
2156 num = args[arg++].data.sint;
Willy Tarreau7d562212016-11-25 16:10:05 +01002157 }
2158 else if (num > 9) { /* src_* variant, args[0] = table */
2159 struct stktable_key *key;
2160 struct connection *conn = objt_conn(sess->origin);
2161 struct sample smp;
2162
2163 if (!conn)
2164 return NULL;
2165
Joseph Herlant5662fa42018-11-15 13:43:28 -08002166 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002167 smp.px = NULL;
2168 smp.sess = sess;
2169 smp.strm = strm;
2170 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2171 return NULL;
2172
2173 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002174 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002175 if (!key)
2176 return NULL;
2177
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002178 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002179 stkctr_set_entry(stkctr, stktable_lookup_key(stkctr->table, key));
2180 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002181 }
2182
2183 /* Here, <num> contains the counter number from 0 to 9 for
2184 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
2185 * args[arg] is the first optional argument. We first lookup the
2186 * ctr form the stream, then from the session if it was not there.
Christopher Faulet33c7e122019-10-21 10:53:34 +02002187 * But we must be sure the counter does not exceed MAX_SESS_STKCTR.
Willy Tarreau7d562212016-11-25 16:10:05 +01002188 */
Christopher Faulet33c7e122019-10-21 10:53:34 +02002189 if (num >= MAX_SESS_STKCTR)
2190 return NULL;
Willy Tarreau7d562212016-11-25 16:10:05 +01002191
2192 if (strm)
2193 stkptr = &strm->stkctr[num];
2194 if (!strm || !stkctr_entry(stkptr)) {
2195 stkptr = &sess->stkctr[num];
2196 if (!stkctr_entry(stkptr))
2197 return NULL;
2198 }
2199
2200 stksess = stkctr_entry(stkptr);
2201 if (!stksess)
2202 return NULL;
2203
2204 if (unlikely(args[arg].type == ARGT_TAB)) {
2205 /* an alternate table was specified, let's look up the same key there */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002206 stkctr->table = args[arg].data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002207 stkctr_set_entry(stkctr, stktable_lookup(stkctr->table, stksess));
2208 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002209 }
2210 return stkptr;
2211}
2212
2213/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2214 * the entry if it doesn't exist yet. This is needed for a few fetch
2215 * functions which need to create an entry, such as src_inc_gpc* and
2216 * src_clr_gpc*.
2217 */
2218struct stkctr *
Emeric Brun819fc6f2017-06-13 19:37:32 +02002219smp_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 +01002220{
Willy Tarreau7d562212016-11-25 16:10:05 +01002221 struct stktable_key *key;
2222 struct connection *conn = objt_conn(sess->origin);
2223 struct sample smp;
2224
2225 if (strncmp(kw, "src_", 4) != 0)
2226 return NULL;
2227
2228 if (!conn)
2229 return NULL;
2230
Joseph Herlant5662fa42018-11-15 13:43:28 -08002231 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002232 smp.px = NULL;
2233 smp.sess = sess;
2234 smp.strm = strm;
2235 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2236 return NULL;
2237
2238 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002239 key = smp_to_stkey(&smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002240 if (!key)
2241 return NULL;
2242
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002243 stkctr->table = args->data.t;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002244 stkctr_set_entry(stkctr, stktable_get_entry(stkctr->table, key));
2245 return stkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002246}
2247
2248/* set return a boolean indicating if the requested stream counter is
2249 * currently being tracked or not.
2250 * Supports being called as "sc[0-9]_tracked" only.
2251 */
2252static int
2253smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
2254{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002255 struct stkctr tmpstkctr;
2256 struct stkctr *stkctr;
2257
Willy Tarreau7d562212016-11-25 16:10:05 +01002258 smp->flags = SMP_F_VOL_TEST;
2259 smp->data.type = SMP_T_BOOL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002260 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2261 smp->data.u.sint = !!stkctr;
2262
2263 /* release the ref count */
Dirkjan Bussinkff57f1b2018-09-14 14:31:22 +02002264 if (stkctr == &tmpstkctr)
Emeric Brun819fc6f2017-06-13 19:37:32 +02002265 stktable_release(stkctr->table, stkctr_entry(stkctr));
2266
Willy Tarreau7d562212016-11-25 16:10:05 +01002267 return 1;
2268}
2269
2270/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2271 * frontend counters or from the src.
2272 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2273 * zero is returned if the key is new.
2274 */
2275static int
2276smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2277{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002278 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002279 struct stkctr *stkctr;
2280
Emeric Brun819fc6f2017-06-13 19:37:32 +02002281 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002282 if (!stkctr)
2283 return 0;
2284
2285 smp->flags = SMP_F_VOL_TEST;
2286 smp->data.type = SMP_T_SINT;
2287 smp->data.u.sint = 0;
2288
Emeric Brun819fc6f2017-06-13 19:37:32 +02002289 if (stkctr_entry(stkctr)) {
2290 void *ptr;
2291
2292 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2293 if (!ptr) {
2294 if (stkctr == &tmpstkctr)
2295 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002296 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002297 }
2298
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002299 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002300
Willy Tarreau7d562212016-11-25 16:10:05 +01002301 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002302
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002303 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002304
2305 if (stkctr == &tmpstkctr)
2306 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002307 }
2308 return 1;
2309}
2310
2311/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
2312 * frontend counters or from the src.
2313 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2314 * zero is returned if the key is new.
2315 */
2316static int
2317smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2318{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002319 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002320 struct stkctr *stkctr;
2321
Emeric Brun819fc6f2017-06-13 19:37:32 +02002322 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002323 if (!stkctr)
2324 return 0;
2325
2326 smp->flags = SMP_F_VOL_TEST;
2327 smp->data.type = SMP_T_SINT;
2328 smp->data.u.sint = 0;
2329
2330 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002331 void *ptr;
2332
2333 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2334 if (!ptr) {
2335 if (stkctr == &tmpstkctr)
2336 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002337 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002338 }
2339
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002340 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002341
Willy Tarreau7d562212016-11-25 16:10:05 +01002342 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002343
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002344 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002345
2346 if (stkctr == &tmpstkctr)
2347 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002348 }
2349 return 1;
2350}
2351
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002352/* set <smp> to the General Purpose Counter 1 value from the stream's tracked
2353 * frontend counters or from the src.
2354 * Supports being called as "sc[0-9]_get_gpc1" or "src_get_gpc1" only. Value
2355 * zero is returned if the key is new.
2356 */
2357static int
2358smp_fetch_sc_get_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2359{
2360 struct stkctr tmpstkctr;
2361 struct stkctr *stkctr;
2362
2363 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2364 if (!stkctr)
2365 return 0;
2366
2367 smp->flags = SMP_F_VOL_TEST;
2368 smp->data.type = SMP_T_SINT;
2369 smp->data.u.sint = 0;
2370
2371 if (stkctr_entry(stkctr) != NULL) {
2372 void *ptr;
2373
2374 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2375 if (!ptr) {
2376 if (stkctr == &tmpstkctr)
2377 stktable_release(stkctr->table, stkctr_entry(stkctr));
2378 return 0; /* parameter not stored */
2379 }
2380
2381 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2382
2383 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2384
2385 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2386
2387 if (stkctr == &tmpstkctr)
2388 stktable_release(stkctr->table, stkctr_entry(stkctr));
2389 }
2390 return 1;
2391}
2392
Willy Tarreau7d562212016-11-25 16:10:05 +01002393/* set <smp> to the General Purpose Counter 0's event rate from the stream's
2394 * tracked frontend counters or from the src.
2395 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2396 * Value zero is returned if the key is new.
2397 */
2398static int
2399smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2400{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002401 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002402 struct stkctr *stkctr;
2403
Emeric Brun819fc6f2017-06-13 19:37:32 +02002404 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002405 if (!stkctr)
2406 return 0;
2407
2408 smp->flags = SMP_F_VOL_TEST;
2409 smp->data.type = SMP_T_SINT;
2410 smp->data.u.sint = 0;
2411 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002412 void *ptr;
2413
2414 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2415 if (!ptr) {
2416 if (stkctr == &tmpstkctr)
2417 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002418 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002419 }
2420
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002421 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002422
Willy Tarreau7d562212016-11-25 16:10:05 +01002423 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
2424 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002425
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002426 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002427
2428 if (stkctr == &tmpstkctr)
2429 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002430 }
2431 return 1;
2432}
2433
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002434/* set <smp> to the General Purpose Counter 1's event rate from the stream's
2435 * tracked frontend counters or from the src.
2436 * Supports being called as "sc[0-9]_gpc1_rate" or "src_gpc1_rate" only.
2437 * Value zero is returned if the key is new.
2438 */
2439static int
2440smp_fetch_sc_gpc1_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2441{
2442 struct stkctr tmpstkctr;
2443 struct stkctr *stkctr;
2444
2445 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2446 if (!stkctr)
2447 return 0;
2448
2449 smp->flags = SMP_F_VOL_TEST;
2450 smp->data.type = SMP_T_SINT;
2451 smp->data.u.sint = 0;
2452 if (stkctr_entry(stkctr) != NULL) {
2453 void *ptr;
2454
2455 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2456 if (!ptr) {
2457 if (stkctr == &tmpstkctr)
2458 stktable_release(stkctr->table, stkctr_entry(stkctr));
2459 return 0; /* parameter not stored */
2460 }
2461
2462 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2463
2464 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc1_rate),
2465 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u);
2466
2467 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2468
2469 if (stkctr == &tmpstkctr)
2470 stktable_release(stkctr->table, stkctr_entry(stkctr));
2471 }
2472 return 1;
2473}
2474
Willy Tarreau7d562212016-11-25 16:10:05 +01002475/* Increment the General Purpose Counter 0 value from the stream's tracked
2476 * frontend counters and return it into temp integer.
2477 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
2478 */
2479static int
2480smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2481{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002482 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002483 struct stkctr *stkctr;
2484
Emeric Brun819fc6f2017-06-13 19:37:32 +02002485 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002486 if (!stkctr)
2487 return 0;
2488
2489 smp->flags = SMP_F_VOL_TEST;
2490 smp->data.type = SMP_T_SINT;
2491 smp->data.u.sint = 0;
2492
Emeric Brun819fc6f2017-06-13 19:37:32 +02002493 if (!stkctr_entry(stkctr))
2494 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002495
2496 if (stkctr && stkctr_entry(stkctr)) {
2497 void *ptr1,*ptr2;
2498
Emeric Brun819fc6f2017-06-13 19:37:32 +02002499
Willy Tarreau7d562212016-11-25 16:10:05 +01002500 /* First, update gpc0_rate if it's tracked. Second, update its
2501 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2502 */
2503 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreau7d562212016-11-25 16:10:05 +01002504 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002505 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002506 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Willy Tarreau7d562212016-11-25 16:10:05 +01002507
Emeric Brun819fc6f2017-06-13 19:37:32 +02002508 if (ptr1) {
2509 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
2510 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
2511 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
2512 }
2513
2514 if (ptr2)
2515 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
2516
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002517 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002518
2519 /* If data was modified, we need to touch to re-schedule sync */
2520 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2521 }
2522 else if (stkctr == &tmpstkctr)
2523 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002524 }
2525 return 1;
2526}
2527
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002528/* Increment the General Purpose Counter 1 value from the stream's tracked
2529 * frontend counters and return it into temp integer.
2530 * Supports being called as "sc[0-9]_inc_gpc1" or "src_inc_gpc1" only.
2531 */
2532static int
2533smp_fetch_sc_inc_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2534{
2535 struct stkctr tmpstkctr;
2536 struct stkctr *stkctr;
2537
2538 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2539 if (!stkctr)
2540 return 0;
2541
2542 smp->flags = SMP_F_VOL_TEST;
2543 smp->data.type = SMP_T_SINT;
2544 smp->data.u.sint = 0;
2545
2546 if (!stkctr_entry(stkctr))
2547 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2548
2549 if (stkctr && stkctr_entry(stkctr)) {
2550 void *ptr1,*ptr2;
2551
2552
2553 /* First, update gpc1_rate if it's tracked. Second, update its
2554 * gpc1 if tracked. Returns gpc1's value otherwise the curr_ctr.
2555 */
2556 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1_RATE);
2557 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2558 if (ptr1 || ptr2) {
2559 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2560
2561 if (ptr1) {
2562 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc1_rate),
2563 stkctr->table->data_arg[STKTABLE_DT_GPC1_RATE].u, 1);
2564 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc1_rate))->curr_ctr;
2565 }
2566
2567 if (ptr2)
2568 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc1);
2569
2570 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2571
2572 /* If data was modified, we need to touch to re-schedule sync */
2573 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2574 }
2575 else if (stkctr == &tmpstkctr)
2576 stktable_release(stkctr->table, stkctr_entry(stkctr));
2577 }
2578 return 1;
2579}
2580
Willy Tarreau7d562212016-11-25 16:10:05 +01002581/* Clear the General Purpose Counter 0 value from the stream's tracked
2582 * frontend counters and return its previous value into temp integer.
2583 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
2584 */
2585static int
2586smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2587{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002588 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002589 struct stkctr *stkctr;
2590
Emeric Brun819fc6f2017-06-13 19:37:32 +02002591 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002592 if (!stkctr)
2593 return 0;
2594
2595 smp->flags = SMP_F_VOL_TEST;
2596 smp->data.type = SMP_T_SINT;
2597 smp->data.u.sint = 0;
2598
Emeric Brun819fc6f2017-06-13 19:37:32 +02002599 if (!stkctr_entry(stkctr))
2600 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002601
Emeric Brun819fc6f2017-06-13 19:37:32 +02002602 if (stkctr && stkctr_entry(stkctr)) {
2603 void *ptr;
2604
2605 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2606 if (!ptr) {
2607 if (stkctr == &tmpstkctr)
2608 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002609 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002610 }
2611
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002612 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002613
Willy Tarreau7d562212016-11-25 16:10:05 +01002614 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
2615 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002616
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002617 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002618
Willy Tarreau7d562212016-11-25 16:10:05 +01002619 /* If data was modified, we need to touch to re-schedule sync */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002620 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
Willy Tarreau7d562212016-11-25 16:10:05 +01002621 }
2622 return 1;
2623}
2624
Frédéric Lécaille6778b272018-01-29 15:22:53 +01002625/* Clear the General Purpose Counter 1 value from the stream's tracked
2626 * frontend counters and return its previous value into temp integer.
2627 * Supports being called as "sc[0-9]_clr_gpc1" or "src_clr_gpc1" only.
2628 */
2629static int
2630smp_fetch_sc_clr_gpc1(const struct arg *args, struct sample *smp, const char *kw, void *private)
2631{
2632 struct stkctr tmpstkctr;
2633 struct stkctr *stkctr;
2634
2635 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2636 if (!stkctr)
2637 return 0;
2638
2639 smp->flags = SMP_F_VOL_TEST;
2640 smp->data.type = SMP_T_SINT;
2641 smp->data.u.sint = 0;
2642
2643 if (!stkctr_entry(stkctr))
2644 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
2645
2646 if (stkctr && stkctr_entry(stkctr)) {
2647 void *ptr;
2648
2649 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC1);
2650 if (!ptr) {
2651 if (stkctr == &tmpstkctr)
2652 stktable_release(stkctr->table, stkctr_entry(stkctr));
2653 return 0; /* parameter not stored */
2654 }
2655
2656 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2657
2658 smp->data.u.sint = stktable_data_cast(ptr, gpc1);
2659 stktable_data_cast(ptr, gpc1) = 0;
2660
2661 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
2662
2663 /* If data was modified, we need to touch to re-schedule sync */
2664 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), (stkctr == &tmpstkctr) ? 1 : 0);
2665 }
2666 return 1;
2667}
2668
Willy Tarreau7d562212016-11-25 16:10:05 +01002669/* set <smp> to the cumulated number of connections from the stream's tracked
2670 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2671 * "src_conn_cnt" only.
2672 */
2673static int
2674smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2675{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002676 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002677 struct stkctr *stkctr;
2678
Emeric Brun819fc6f2017-06-13 19:37:32 +02002679 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002680 if (!stkctr)
2681 return 0;
2682
2683 smp->flags = SMP_F_VOL_TEST;
2684 smp->data.type = SMP_T_SINT;
2685 smp->data.u.sint = 0;
2686 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002687 void *ptr;
2688
2689 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
2690 if (!ptr) {
2691 if (stkctr == &tmpstkctr)
2692 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002693 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002694 }
2695
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002696 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002697
Willy Tarreau7d562212016-11-25 16:10:05 +01002698 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002699
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002700 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002701
2702 if (stkctr == &tmpstkctr)
2703 stktable_release(stkctr->table, stkctr_entry(stkctr));
2704
2705
Willy Tarreau7d562212016-11-25 16:10:05 +01002706 }
2707 return 1;
2708}
2709
2710/* set <smp> to the connection rate from the stream's tracked frontend
2711 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2712 * only.
2713 */
2714static int
2715smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2716{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002717 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002718 struct stkctr *stkctr;
2719
Emeric Brun819fc6f2017-06-13 19:37:32 +02002720 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002721 if (!stkctr)
2722 return 0;
2723
2724 smp->flags = SMP_F_VOL_TEST;
2725 smp->data.type = SMP_T_SINT;
2726 smp->data.u.sint = 0;
2727 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002728 void *ptr;
2729
2730 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
2731 if (!ptr) {
2732 if (stkctr == &tmpstkctr)
2733 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002734 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002735 }
2736
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002737 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002738
Willy Tarreau7d562212016-11-25 16:10:05 +01002739 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
2740 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002741
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002742 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002743
2744 if (stkctr == &tmpstkctr)
2745 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002746 }
2747 return 1;
2748}
2749
2750/* set temp integer to the number of connections from the stream's source address
2751 * in the table pointed to by expr, after updating it.
2752 * Accepts exactly 1 argument of type table.
2753 */
2754static int
2755smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2756{
2757 struct connection *conn = objt_conn(smp->sess->origin);
2758 struct stksess *ts;
2759 struct stktable_key *key;
2760 void *ptr;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002761 struct stktable *t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002762
2763 if (!conn)
2764 return 0;
2765
Joseph Herlant5662fa42018-11-15 13:43:28 -08002766 /* Fetch source address in a sample. */
Willy Tarreau7d562212016-11-25 16:10:05 +01002767 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2768 return 0;
2769
2770 /* Converts into key. */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002771 key = smp_to_stkey(smp, args->data.t);
Willy Tarreau7d562212016-11-25 16:10:05 +01002772 if (!key)
2773 return 0;
2774
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002775 t = args->data.t;
Willy Tarreau7d562212016-11-25 16:10:05 +01002776
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002777 if ((ts = stktable_get_entry(t, key)) == NULL)
Willy Tarreau7d562212016-11-25 16:10:05 +01002778 /* entry does not exist and could not be created */
2779 return 0;
2780
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002781 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002782 if (!ptr) {
Willy Tarreau7d562212016-11-25 16:10:05 +01002783 return 0; /* parameter not stored in this table */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002784 }
Willy Tarreau7d562212016-11-25 16:10:05 +01002785
2786 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002787
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002788 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002789
Willy Tarreau7d562212016-11-25 16:10:05 +01002790 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002791
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002792 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002793
Willy Tarreau7d562212016-11-25 16:10:05 +01002794 smp->flags = SMP_F_VOL_TEST;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002795
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002796 stktable_touch_local(t, ts, 1);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002797
2798 /* Touch was previously performed by stktable_update_key */
Willy Tarreau7d562212016-11-25 16:10:05 +01002799 return 1;
2800}
2801
2802/* set <smp> to the number of concurrent connections from the stream's tracked
2803 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2804 * "src_conn_cur" only.
2805 */
2806static int
2807smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
2808{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002809 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002810 struct stkctr *stkctr;
2811
Emeric Brun819fc6f2017-06-13 19:37:32 +02002812 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002813 if (!stkctr)
2814 return 0;
2815
2816 smp->flags = SMP_F_VOL_TEST;
2817 smp->data.type = SMP_T_SINT;
2818 smp->data.u.sint = 0;
2819 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002820 void *ptr;
2821
2822 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
2823 if (!ptr) {
2824 if (stkctr == &tmpstkctr)
2825 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002826 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002827 }
2828
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002829 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002830
Willy Tarreau7d562212016-11-25 16:10:05 +01002831 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002832
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002833 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002834
2835 if (stkctr == &tmpstkctr)
2836 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002837 }
2838 return 1;
2839}
2840
2841/* set <smp> to the cumulated number of streams from the stream's tracked
2842 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2843 * "src_sess_cnt" only.
2844 */
2845static int
2846smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2847{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002848 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002849 struct stkctr *stkctr;
2850
Emeric Brun819fc6f2017-06-13 19:37:32 +02002851 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002852 if (!stkctr)
2853 return 0;
2854
2855 smp->flags = SMP_F_VOL_TEST;
2856 smp->data.type = SMP_T_SINT;
2857 smp->data.u.sint = 0;
2858 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002859 void *ptr;
2860
2861 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
2862 if (!ptr) {
2863 if (stkctr == &tmpstkctr)
2864 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002865 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002866 }
2867
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002868 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002869
Willy Tarreau7d562212016-11-25 16:10:05 +01002870 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002871
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002872 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002873
2874 if (stkctr == &tmpstkctr)
2875 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002876 }
2877 return 1;
2878}
2879
2880/* set <smp> to the stream rate from the stream's tracked frontend counters.
2881 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2882 */
2883static int
2884smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2885{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002886 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002887 struct stkctr *stkctr;
2888
Emeric Brun819fc6f2017-06-13 19:37:32 +02002889 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002890 if (!stkctr)
2891 return 0;
2892
2893 smp->flags = SMP_F_VOL_TEST;
2894 smp->data.type = SMP_T_SINT;
2895 smp->data.u.sint = 0;
2896 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002897 void *ptr;
2898
2899 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
2900 if (!ptr) {
2901 if (stkctr == &tmpstkctr)
2902 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002903 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002904 }
2905
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002906 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002907
Willy Tarreau7d562212016-11-25 16:10:05 +01002908 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2909 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002910
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002911 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002912
2913 if (stkctr == &tmpstkctr)
2914 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002915 }
2916 return 1;
2917}
2918
2919/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
2920 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2921 * "src_http_req_cnt" only.
2922 */
2923static int
2924smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2925{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002926 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002927 struct stkctr *stkctr;
2928
Emeric Brun819fc6f2017-06-13 19:37:32 +02002929 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002930 if (!stkctr)
2931 return 0;
2932
2933 smp->flags = SMP_F_VOL_TEST;
2934 smp->data.type = SMP_T_SINT;
2935 smp->data.u.sint = 0;
2936 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002937 void *ptr;
2938
2939 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
2940 if (!ptr) {
2941 if (stkctr == &tmpstkctr)
2942 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002943 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002944 }
2945
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002946 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002947
Willy Tarreau7d562212016-11-25 16:10:05 +01002948 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002949
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002950 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002951
2952 if (stkctr == &tmpstkctr)
2953 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002954 }
2955 return 1;
2956}
2957
2958/* set <smp> to the HTTP request rate from the stream's tracked frontend
2959 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2960 * "src_http_req_rate" only.
2961 */
2962static int
2963smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2964{
Emeric Brun819fc6f2017-06-13 19:37:32 +02002965 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01002966 struct stkctr *stkctr;
2967
Emeric Brun819fc6f2017-06-13 19:37:32 +02002968 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01002969 if (!stkctr)
2970 return 0;
2971
2972 smp->flags = SMP_F_VOL_TEST;
2973 smp->data.type = SMP_T_SINT;
2974 smp->data.u.sint = 0;
2975 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02002976 void *ptr;
2977
2978 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
2979 if (!ptr) {
2980 if (stkctr == &tmpstkctr)
2981 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002982 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002983 }
2984
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002985 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002986
Willy Tarreau7d562212016-11-25 16:10:05 +01002987 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2988 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002989
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002990 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002991
2992 if (stkctr == &tmpstkctr)
2993 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01002994 }
2995 return 1;
2996}
2997
2998/* set <smp> to the cumulated number of HTTP requests errors from the stream's
2999 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3000 * "src_http_err_cnt" only.
3001 */
3002static int
3003smp_fetch_sc_http_err_cnt(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_HTTP_ERR_CNT);
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, http_err_cnt);
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 HTTP request error rate from the stream's tracked frontend
3038 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3039 * "src_http_err_rate" only.
3040 */
3041static int
3042smp_fetch_sc_http_err_rate(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_HTTP_ERR_RATE);
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 = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3067 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003068
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003069 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003070
3071 if (stkctr == &tmpstkctr)
3072 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003073 }
3074 return 1;
3075}
3076
3077/* set <smp> to the number of kbytes received from clients, as found in the
3078 * stream's tracked frontend counters. Supports being called as
3079 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3080 */
3081static int
3082smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
3083{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003084 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003085 struct stkctr *stkctr;
3086
Emeric Brun819fc6f2017-06-13 19:37:32 +02003087 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003088 if (!stkctr)
3089 return 0;
3090
3091 smp->flags = SMP_F_VOL_TEST;
3092 smp->data.type = SMP_T_SINT;
3093 smp->data.u.sint = 0;
3094 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003095 void *ptr;
3096
3097 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
3098 if (!ptr) {
3099 if (stkctr == &tmpstkctr)
3100 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003101 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003102 }
3103
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003104 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003105
Willy Tarreau7d562212016-11-25 16:10:05 +01003106 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003107
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003108 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003109
3110 if (stkctr == &tmpstkctr)
3111 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003112 }
3113 return 1;
3114}
3115
3116/* set <smp> to the data rate received from clients in bytes/s, as found
3117 * in the stream's tracked frontend counters. Supports being called as
3118 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
3119 */
3120static int
3121smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3122{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003123 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003124 struct stkctr *stkctr;
3125
Emeric Brun819fc6f2017-06-13 19:37:32 +02003126 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003127 if (!stkctr)
3128 return 0;
3129
3130 smp->flags = SMP_F_VOL_TEST;
3131 smp->data.type = SMP_T_SINT;
3132 smp->data.u.sint = 0;
3133 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003134 void *ptr;
3135
3136 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
3137 if (!ptr) {
3138 if (stkctr == &tmpstkctr)
3139 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003140 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003141 }
3142
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003143 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003144
Willy Tarreau7d562212016-11-25 16:10:05 +01003145 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
3146 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003147
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003148 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003149
3150 if (stkctr == &tmpstkctr)
3151 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003152 }
3153 return 1;
3154}
3155
3156/* set <smp> to the number of kbytes sent to clients, as found in the
3157 * stream's tracked frontend counters. Supports being called as
3158 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3159 */
3160static int
3161smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
3162{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003163 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003164 struct stkctr *stkctr;
3165
Emeric Brun819fc6f2017-06-13 19:37:32 +02003166 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003167 if (!stkctr)
3168 return 0;
3169
3170 smp->flags = SMP_F_VOL_TEST;
3171 smp->data.type = SMP_T_SINT;
3172 smp->data.u.sint = 0;
3173 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003174 void *ptr;
3175
3176 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
3177 if (!ptr) {
3178 if (stkctr == &tmpstkctr)
3179 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003180 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003181 }
3182
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003183 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003184
Willy Tarreau7d562212016-11-25 16:10:05 +01003185 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003186
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003187 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003188
3189 if (stkctr == &tmpstkctr)
3190 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003191 }
3192 return 1;
3193}
3194
3195/* set <smp> to the data rate sent to clients in bytes/s, as found in the
3196 * stream's tracked frontend counters. Supports being called as
3197 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
3198 */
3199static int
3200smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
3201{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003202 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003203 struct stkctr *stkctr;
3204
Emeric Brun819fc6f2017-06-13 19:37:32 +02003205 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003206 if (!stkctr)
3207 return 0;
3208
3209 smp->flags = SMP_F_VOL_TEST;
3210 smp->data.type = SMP_T_SINT;
3211 smp->data.u.sint = 0;
3212 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun819fc6f2017-06-13 19:37:32 +02003213 void *ptr;
3214
3215 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
3216 if (!ptr) {
3217 if (stkctr == &tmpstkctr)
3218 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003219 return 0; /* parameter not stored */
Emeric Brun819fc6f2017-06-13 19:37:32 +02003220 }
3221
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003222 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003223
Willy Tarreau7d562212016-11-25 16:10:05 +01003224 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
3225 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003226
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003227 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &stkctr_entry(stkctr)->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003228
3229 if (stkctr == &tmpstkctr)
3230 stktable_release(stkctr->table, stkctr_entry(stkctr));
Willy Tarreau7d562212016-11-25 16:10:05 +01003231 }
3232 return 1;
3233}
3234
3235/* set <smp> to the number of active trackers on the SC entry in the stream's
3236 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3237 */
3238static int
3239smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
3240{
Emeric Brun819fc6f2017-06-13 19:37:32 +02003241 struct stkctr tmpstkctr;
Willy Tarreau7d562212016-11-25 16:10:05 +01003242 struct stkctr *stkctr;
3243
Emeric Brun819fc6f2017-06-13 19:37:32 +02003244 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw, &tmpstkctr);
Willy Tarreau7d562212016-11-25 16:10:05 +01003245 if (!stkctr)
3246 return 0;
3247
3248 smp->flags = SMP_F_VOL_TEST;
3249 smp->data.type = SMP_T_SINT;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003250 if (stkctr == &tmpstkctr) {
3251 smp->data.u.sint = stkctr_entry(stkctr) ? (stkctr_entry(stkctr)->ref_cnt-1) : 0;
3252 stktable_release(stkctr->table, stkctr_entry(stkctr));
3253 }
3254 else {
3255 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
3256 }
3257
Willy Tarreau7d562212016-11-25 16:10:05 +01003258 return 1;
3259}
3260
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003261
3262/* The functions below are used to manipulate table contents from the CLI.
3263 * There are 3 main actions, "clear", "set" and "show". The code is shared
3264 * between all actions, and the action is encoded in the void *private in
3265 * the appctx as well as in the keyword registration, among one of the
3266 * following values.
3267 */
3268
3269enum {
3270 STK_CLI_ACT_CLR,
3271 STK_CLI_ACT_SET,
3272 STK_CLI_ACT_SHOW,
3273};
3274
3275/* Dump the status of a table to a stream interface's
3276 * read buffer. It returns 0 if the output buffer is full
3277 * and needs to be called again, otherwise non-zero.
3278 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003279static int table_dump_head_to_buffer(struct buffer *msg,
3280 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003281 struct stktable *t, struct stktable *target)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003282{
3283 struct stream *s = si_strm(si);
3284
3285 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003286 t->id, stktable_types[t->type].kw, t->size, t->current);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003287
3288 /* any other information should be dumped here */
3289
William Lallemand07a62f72017-05-24 00:57:40 +02003290 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003291 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
3292
Willy Tarreau06d80a92017-10-19 14:32:15 +02003293 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003294 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003295 return 0;
3296 }
3297
3298 return 1;
3299}
3300
3301/* Dump a table entry to a stream interface's
3302 * read buffer. It returns 0 if the output buffer is full
3303 * and needs to be called again, otherwise non-zero.
3304 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003305static int table_dump_entry_to_buffer(struct buffer *msg,
3306 struct stream_interface *si,
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003307 struct stktable *t, struct stksess *entry)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003308{
3309 int dt;
3310
3311 chunk_appendf(msg, "%p:", entry);
3312
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003313 if (t->type == SMP_T_IPV4) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003314 char addr[INET_ADDRSTRLEN];
3315 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
3316 chunk_appendf(msg, " key=%s", addr);
3317 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003318 else if (t->type == SMP_T_IPV6) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003319 char addr[INET6_ADDRSTRLEN];
3320 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
3321 chunk_appendf(msg, " key=%s", addr);
3322 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003323 else if (t->type == SMP_T_SINT) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003324 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
3325 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003326 else if (t->type == SMP_T_STR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003327 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003328 dump_text(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003329 }
3330 else {
3331 chunk_appendf(msg, " key=");
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003332 dump_binary(msg, (const char *)entry->key.key, t->key_size);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003333 }
3334
3335 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
3336
3337 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
3338 void *ptr;
3339
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003340 if (t->data_ofs[dt] == 0)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003341 continue;
3342 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003343 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003344 else
3345 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
3346
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003347 ptr = stktable_data_ptr(t, entry, dt);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003348 switch (stktable_data_types[dt].std_type) {
3349 case STD_T_SINT:
3350 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
3351 break;
3352 case STD_T_UINT:
3353 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
3354 break;
3355 case STD_T_ULL:
3356 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
3357 break;
3358 case STD_T_FRQP:
3359 chunk_appendf(msg, "%d",
3360 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003361 t->data_arg[dt].u));
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003362 break;
Frédéric Lécaille16b4f542019-05-23 12:15:04 +02003363 case STD_T_DICT: {
3364 struct dict_entry *de;
3365 de = stktable_data_cast(ptr, std_t_dict);
3366 chunk_appendf(msg, "%s", de ? (char *)de->value.key : "-");
3367 break;
3368 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003369 }
3370 }
3371 chunk_appendf(msg, "\n");
3372
Willy Tarreau06d80a92017-10-19 14:32:15 +02003373 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003374 si_rx_room_blk(si);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003375 return 0;
3376 }
3377
3378 return 1;
3379}
3380
3381
3382/* Processes a single table entry matching a specific key passed in argument.
3383 * returns 0 if wants to be called again, 1 if has ended processing.
3384 */
3385static int table_process_entry_per_key(struct appctx *appctx, char **args)
3386{
3387 struct stream_interface *si = appctx->owner;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003388 struct stktable *t = appctx->ctx.table.target;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003389 struct stksess *ts;
3390 uint32_t uint32_key;
3391 unsigned char ip6_key[sizeof(struct in6_addr)];
3392 long long value;
3393 int data_type;
3394 int cur_arg;
3395 void *ptr;
3396 struct freq_ctr_period *frqp;
3397
3398 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003399 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003400 appctx->ctx.cli.msg = "Key value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003401 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003402 return 1;
3403 }
3404
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003405 switch (t->type) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003406 case SMP_T_IPV4:
3407 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02003408 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003409 break;
3410 case SMP_T_IPV6:
3411 inet_pton(AF_INET6, args[4], ip6_key);
Christopher Fauletca20d022017-08-29 15:30:31 +02003412 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003413 break;
3414 case SMP_T_SINT:
3415 {
3416 char *endptr;
3417 unsigned long val;
3418 errno = 0;
3419 val = strtoul(args[4], &endptr, 10);
3420 if ((errno == ERANGE && val == ULONG_MAX) ||
3421 (errno != 0 && val == 0) || endptr == args[4] ||
3422 val > 0xffffffff) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003423 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003424 appctx->ctx.cli.msg = "Invalid key\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003425 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003426 return 1;
3427 }
3428 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02003429 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003430 break;
3431 }
3432 break;
3433 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02003434 static_table_key.key = args[4];
3435 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003436 break;
3437 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003438 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003439 case STK_CLI_ACT_SHOW:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003440 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003441 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
3442 break;
3443 case STK_CLI_ACT_CLR:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003444 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003445 appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
3446 break;
3447 case STK_CLI_ACT_SET:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003448 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003449 appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n";
3450 break;
3451 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003452 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003453 appctx->ctx.cli.msg = "Unknown action\n";
3454 break;
3455 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003456 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003457 return 1;
3458 }
3459
3460 /* check permissions */
3461 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3462 return 1;
3463
Willy Tarreaua24bc782016-12-14 15:50:35 +01003464 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003465 case STK_CLI_ACT_SHOW:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003466 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003467 if (!ts)
3468 return 1;
3469 chunk_reset(&trash);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003470 if (!table_dump_head_to_buffer(&trash, si, t, t)) {
3471 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003472 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003473 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003474 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003475 if (!table_dump_entry_to_buffer(&trash, si, t, ts)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003476 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003477 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003478 return 0;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003479 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003480 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003481 stktable_release(t, ts);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003482 break;
3483
3484 case STK_CLI_ACT_CLR:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003485 ts = stktable_lookup_key(t, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003486 if (!ts)
3487 return 1;
Emeric Brun819fc6f2017-06-13 19:37:32 +02003488
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003489 if (!stksess_kill(t, ts, 1)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003490 /* don't delete an entry which is currently referenced */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003491 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003492 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003493 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003494 return 1;
3495 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003496
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003497 break;
3498
3499 case STK_CLI_ACT_SET:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003500 ts = stktable_get_entry(t, &static_table_key);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003501 if (!ts) {
3502 /* don't delete an entry which is currently referenced */
3503 appctx->ctx.cli.severity = LOG_ERR;
3504 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
3505 appctx->st0 = CLI_ST_PRINT;
3506 return 1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003507 }
3508
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003509 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003510 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
3511 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003512 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003513 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003514 appctx->st0 = CLI_ST_PRINT;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003515 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003516 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003517 return 1;
3518 }
3519
3520 data_type = stktable_get_data_type(args[cur_arg] + 5);
3521 if (data_type < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003522 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003523 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003524 appctx->st0 = CLI_ST_PRINT;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003525 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003526 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003527 return 1;
3528 }
3529
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003530 if (!t->data_ofs[data_type]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003531 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003532 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003533 appctx->st0 = CLI_ST_PRINT;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003534 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003535 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003536 return 1;
3537 }
3538
3539 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003540 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003541 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003542 appctx->st0 = CLI_ST_PRINT;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003543 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003544 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003545 return 1;
3546 }
3547
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003548 ptr = stktable_data_ptr(t, ts, data_type);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003549
3550 switch (stktable_data_types[data_type].std_type) {
3551 case STD_T_SINT:
3552 stktable_data_cast(ptr, std_t_sint) = value;
3553 break;
3554 case STD_T_UINT:
3555 stktable_data_cast(ptr, std_t_uint) = value;
3556 break;
3557 case STD_T_ULL:
3558 stktable_data_cast(ptr, std_t_ull) = value;
3559 break;
3560 case STD_T_FRQP:
3561 /* We set both the current and previous values. That way
3562 * the reported frequency is stable during all the period
3563 * then slowly fades out. This allows external tools to
3564 * push measures without having to update them too often.
3565 */
3566 frqp = &stktable_data_cast(ptr, std_t_frqp);
Emeric Brunf2fc1fd2017-11-02 17:32:43 +01003567 /* First bit is reserved for the freq_ctr_period lock
3568 Note: here we're still protected by the stksess lock
3569 so we don't need to update the update the freq_ctr_period
3570 using its internal lock */
3571 frqp->curr_tick = now_ms & ~0x1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003572 frqp->prev_ctr = 0;
3573 frqp->curr_ctr = value;
3574 break;
3575 }
3576 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003577 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003578 stktable_touch_local(t, ts, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003579 break;
3580
3581 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003582 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003583 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003584 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003585 break;
3586 }
3587 return 1;
3588}
3589
3590/* Prepares the appctx fields with the data-based filters from the command line.
3591 * Returns 0 if the dump can proceed, 1 if has ended processing.
3592 */
3593static int table_prepare_data_request(struct appctx *appctx, char **args)
3594{
Willy Tarreaua24bc782016-12-14 15:50:35 +01003595 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003596 appctx->ctx.cli.severity = LOG_ERR;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01003597 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003598 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003599 return 1;
3600 }
3601
3602 /* condition on stored data value */
3603 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
3604 if (appctx->ctx.table.data_type < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003605 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003606 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003607 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003608 return 1;
3609 }
3610
Christopher Fauletbe17bb82019-09-13 15:15:56 +02003611 if (!((struct stktable *)appctx->ctx.table.target)->data_ofs[appctx->ctx.table.data_type]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003612 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003613 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003614 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003615 return 1;
3616 }
3617
3618 appctx->ctx.table.data_op = get_std_op(args[4]);
3619 if (appctx->ctx.table.data_op < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003620 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003621 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003622 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003623 return 1;
3624 }
3625
3626 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003627 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003628 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003629 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003630 return 1;
3631 }
3632
3633 /* OK we're done, all the fields are set */
3634 return 0;
3635}
3636
3637/* returns 0 if wants to be called, 1 if has ended processing */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003638static int cli_parse_table_req(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003639{
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003640 appctx->ctx.table.data_type = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003641 appctx->ctx.table.target = NULL;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003642 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003643 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003644
3645 if (*args[2]) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003646 appctx->ctx.table.target = stktable_find_by_name(args[2]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003647 if (!appctx->ctx.table.target) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003648 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003649 appctx->ctx.cli.msg = "No such table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003650 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003651 return 1;
3652 }
3653 }
3654 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01003655 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003656 goto err_args;
3657 return 0;
3658 }
3659
3660 if (strcmp(args[3], "key") == 0)
3661 return table_process_entry_per_key(appctx, args);
3662 else if (strncmp(args[3], "data.", 5) == 0)
3663 return table_prepare_data_request(appctx, args);
3664 else if (*args[3])
3665 goto err_args;
3666
3667 return 0;
3668
3669err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01003670 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003671 case STK_CLI_ACT_SHOW:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003672 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003673 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
3674 break;
3675 case STK_CLI_ACT_CLR:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003676 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003677 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
3678 break;
3679 case STK_CLI_ACT_SET:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003680 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003681 appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n";
3682 break;
3683 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003684 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003685 appctx->ctx.cli.msg = "Unknown action\n";
3686 break;
3687 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003688 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003689 return 1;
3690}
3691
3692/* This function is used to deal with table operations (dump or clear depending
3693 * on the action stored in appctx->private). It returns 0 if the output buffer is
3694 * full and it needs to be called again, otherwise non-zero.
3695 */
3696static int cli_io_handler_table(struct appctx *appctx)
3697{
3698 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003699 struct stream *s = si_strm(si);
3700 struct ebmb_node *eb;
3701 int dt;
3702 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01003703 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003704
3705 /*
3706 * We have 3 possible states in appctx->st2 :
3707 * - STAT_ST_INIT : the first call
3708 * - STAT_ST_INFO : the proxy pointer points to the next table to
3709 * dump, the entry pointer is NULL ;
3710 * - STAT_ST_LIST : the proxy pointer points to the current table
3711 * and the entry pointer points to the next entry to be dumped,
3712 * and the refcount on the next entry is held ;
3713 * - STAT_ST_END : nothing left to dump, the buffer may contain some
3714 * data though.
3715 */
3716
3717 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3718 /* in case of abort, remove any refcount we might have set on an entry */
3719 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003720 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003721 }
3722 return 1;
3723 }
3724
3725 chunk_reset(&trash);
3726
3727 while (appctx->st2 != STAT_ST_FIN) {
3728 switch (appctx->st2) {
3729 case STAT_ST_INIT:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003730 appctx->ctx.table.t = appctx->ctx.table.target;
3731 if (!appctx->ctx.table.t)
3732 appctx->ctx.table.t = stktables_list;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003733
3734 appctx->ctx.table.entry = NULL;
3735 appctx->st2 = STAT_ST_INFO;
3736 break;
3737
3738 case STAT_ST_INFO:
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003739 if (!appctx->ctx.table.t ||
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003740 (appctx->ctx.table.target &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003741 appctx->ctx.table.t != appctx->ctx.table.target)) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003742 appctx->st2 = STAT_ST_END;
3743 break;
3744 }
3745
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003746 if (appctx->ctx.table.t->size) {
3747 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.target))
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003748 return 0;
3749
3750 if (appctx->ctx.table.target &&
William Lallemand07a62f72017-05-24 00:57:40 +02003751 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003752 /* dump entries only if table explicitly requested */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003753 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
3754 eb = ebmb_first(&appctx->ctx.table.t->keys);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003755 if (eb) {
3756 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3757 appctx->ctx.table.entry->ref_cnt++;
3758 appctx->st2 = STAT_ST_LIST;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003759 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003760 break;
3761 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003762 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003763 }
3764 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003765 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003766 break;
3767
3768 case STAT_ST_LIST:
3769 skip_entry = 0;
3770
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003771 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003772
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003773 if (appctx->ctx.table.data_type >= 0) {
3774 /* we're filtering on some data contents */
3775 void *ptr;
3776 long long data;
3777
Emeric Brun819fc6f2017-06-13 19:37:32 +02003778
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003779 dt = appctx->ctx.table.data_type;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003780 ptr = stktable_data_ptr(appctx->ctx.table.t,
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003781 appctx->ctx.table.entry,
3782 dt);
3783
3784 data = 0;
3785 switch (stktable_data_types[dt].std_type) {
3786 case STD_T_SINT:
3787 data = stktable_data_cast(ptr, std_t_sint);
3788 break;
3789 case STD_T_UINT:
3790 data = stktable_data_cast(ptr, std_t_uint);
3791 break;
3792 case STD_T_ULL:
3793 data = stktable_data_cast(ptr, std_t_ull);
3794 break;
3795 case STD_T_FRQP:
3796 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003797 appctx->ctx.table.t->data_arg[dt].u);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003798 break;
3799 }
3800
3801 /* skip the entry if the data does not match the test and the value */
3802 if ((data < appctx->ctx.table.value &&
3803 (appctx->ctx.table.data_op == STD_OP_EQ ||
3804 appctx->ctx.table.data_op == STD_OP_GT ||
3805 appctx->ctx.table.data_op == STD_OP_GE)) ||
3806 (data == appctx->ctx.table.value &&
3807 (appctx->ctx.table.data_op == STD_OP_NE ||
3808 appctx->ctx.table.data_op == STD_OP_GT ||
3809 appctx->ctx.table.data_op == STD_OP_LT)) ||
3810 (data > appctx->ctx.table.value &&
3811 (appctx->ctx.table.data_op == STD_OP_EQ ||
3812 appctx->ctx.table.data_op == STD_OP_LT ||
3813 appctx->ctx.table.data_op == STD_OP_LE)))
3814 skip_entry = 1;
3815 }
3816
3817 if (show && !skip_entry &&
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003818 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.t, appctx->ctx.table.entry)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003819 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003820 return 0;
3821 }
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003822
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003823 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &appctx->ctx.table.entry->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003824
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003825 HA_SPIN_LOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003826 appctx->ctx.table.entry->ref_cnt--;
3827
3828 eb = ebmb_next(&appctx->ctx.table.entry->key);
3829 if (eb) {
3830 struct stksess *old = appctx->ctx.table.entry;
3831 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3832 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003833 __stksess_kill_if_expired(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003834 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003835 __stksess_kill(appctx->ctx.table.t, old);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003836 appctx->ctx.table.entry->ref_cnt++;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003837 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003838 break;
3839 }
3840
3841
3842 if (show)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003843 __stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003844 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003845 __stksess_kill(appctx->ctx.table.t, appctx->ctx.table.entry);
Emeric Brun819fc6f2017-06-13 19:37:32 +02003846
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003847 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &appctx->ctx.table.t->lock);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003848
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003849 appctx->ctx.table.t = appctx->ctx.table.t->next;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003850 appctx->st2 = STAT_ST_INFO;
3851 break;
3852
3853 case STAT_ST_END:
3854 appctx->st2 = STAT_ST_FIN;
3855 break;
3856 }
3857 }
3858 return 1;
3859}
3860
3861static void cli_release_show_table(struct appctx *appctx)
3862{
3863 if (appctx->st2 == STAT_ST_LIST) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01003864 stksess_kill_if_expired(appctx->ctx.table.t, appctx->ctx.table.entry, 1);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003865 }
3866}
3867
3868/* register cli keywords */
3869static struct cli_kw_list cli_kws = {{ },{
3870 { { "clear", "table", NULL }, "clear table : remove an entry from a table", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_CLR },
3871 { { "set", "table", NULL }, "set table [id] : update or create a table entry's data", cli_parse_table_req, cli_io_handler_table, NULL, (void *)STK_CLI_ACT_SET },
3872 { { "show", "table", NULL }, "show table [id]: report table usage stats or dump this table's contents", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_SHOW },
3873 {{},}
3874}};
3875
Willy Tarreau0108d902018-11-25 19:14:37 +01003876INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01003877
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003878static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02003879 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003880 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003881 { "sc-set-gpt0", parse_set_gpt0, 1 },
3882 { /* END */ }
3883}};
3884
Willy Tarreau0108d902018-11-25 19:14:37 +01003885INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_conn_kws);
3886
Willy Tarreau620408f2016-10-21 16:37:51 +02003887static struct action_kw_list tcp_sess_kws = { { }, {
3888 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003889 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Willy Tarreau620408f2016-10-21 16:37:51 +02003890 { "sc-set-gpt0", parse_set_gpt0, 1 },
3891 { /* END */ }
3892}};
3893
Willy Tarreau0108d902018-11-25 19:14:37 +01003894INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_sess_kws);
3895
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003896static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02003897 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003898 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003899 { "sc-set-gpt0", parse_set_gpt0, 1 },
3900 { /* END */ }
3901}};
3902
Willy Tarreau0108d902018-11-25 19:14:37 +01003903INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_kws);
3904
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003905static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02003906 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003907 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003908 { "sc-set-gpt0", parse_set_gpt0, 1 },
3909 { /* END */ }
3910}};
3911
Willy Tarreau0108d902018-11-25 19:14:37 +01003912INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_kws);
3913
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003914static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02003915 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003916 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003917 { "sc-set-gpt0", parse_set_gpt0, 1 },
3918 { /* END */ }
3919}};
3920
Willy Tarreau0108d902018-11-25 19:14:37 +01003921INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_kws);
3922
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003923static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02003924 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Frédéric Lécaille6778b272018-01-29 15:22:53 +01003925 { "sc-inc-gpc1", parse_inc_gpc1, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003926 { "sc-set-gpt0", parse_set_gpt0, 1 },
3927 { /* END */ }
3928}};
3929
Willy Tarreau0108d902018-11-25 19:14:37 +01003930INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_kws);
3931
Willy Tarreau7d562212016-11-25 16:10:05 +01003932///* Note: must not be declared <const> as its list will be overwritten.
3933// * Please take care of keeping this list alphabetically sorted.
3934// */
3935//static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
3936// { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3937// { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3938// { /* END */ },
3939//}};
3940/* Note: must not be declared <const> as its list will be overwritten.
3941 * Please take care of keeping this list alphabetically sorted.
3942 */
3943static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
3944 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3945 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3946 { "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 +01003947 { "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 +01003948 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3949 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3950 { "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 +01003951 { "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 +01003952 { "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 +01003953 { "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 +01003954 { "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 +01003955 { "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 +01003956 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3957 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3958 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3959 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3960 { "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 +01003961 { "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 +01003962 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3963 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3964 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3965 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3966 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3967 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3968 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3969 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3970 { "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 +01003971 { "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 +01003972 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3973 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3974 { "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 +01003975 { "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 +01003976 { "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 +01003977 { "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 +01003978 { "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 +01003979 { "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 +01003980 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3981 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3982 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3983 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3984 { "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 +01003985 { "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 +01003986 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3987 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3988 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3989 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3990 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3991 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3992 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3993 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3994 { "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 +01003995 { "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 +01003996 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3997 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3998 { "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 +01003999 { "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 +01004000 { "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 +01004001 { "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 +01004002 { "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 +01004003 { "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 +01004004 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4005 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4006 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4007 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4008 { "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 +01004009 { "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 +01004010 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4011 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4012 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4013 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4014 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4015 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4016 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4017 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4018 { "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 +01004019 { "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 +01004020 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4021 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4022 { "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 +01004023 { "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 +01004024 { "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 +01004025 { "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 +01004026 { "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 +01004027 { "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 +01004028 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4029 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4030 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4031 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4032 { "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 +01004033 { "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 +01004034 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4035 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4036 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4037 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4038 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
4039 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4040 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4041 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4042 { "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 +01004043 { "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 +01004044 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4045 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4046 { "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 +01004047 { "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 +01004048 { "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 +01004049 { "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 +01004050 { "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 +01004051 { "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 +01004052 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4053 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4054 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4055 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4056 { "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 +01004057 { "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 +01004058 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4059 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4060 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4061 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4062 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
4063 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4064 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
4065 { /* END */ },
4066}};
4067
Willy Tarreau0108d902018-11-25 19:14:37 +01004068INITCALL1(STG_REGISTER, sample_register_fetches, &smp_fetch_keywords);
Willy Tarreau7d562212016-11-25 16:10:05 +01004069
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004070/* Note: must not be declared <const> as its list will be overwritten */
4071static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02004072 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
4073 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4074 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4075 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4076 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4077 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4078 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4079 { "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 +01004080 { "table_gpc1", sample_conv_table_gpc1, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreau2d17db52016-05-25 17:16:38 +02004081 { "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 +01004082 { "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 +02004083 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4084 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4085 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4086 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4087 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4088 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4089 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4090 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4091 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
4092 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02004093 { /* END */ },
4094}};
4095
Willy Tarreau0108d902018-11-25 19:14:37 +01004096INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);