blob: d95c77f745da469e81f669a3c038f76b1da9ce52 [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>
18#include <common/memory.h>
19#include <common/mini-clist.h>
20#include <common/standard.h>
21#include <common/time.h>
22
23#include <ebmbtree.h>
24#include <ebsttree.h>
25
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010026#include <types/cli.h>
Willy Tarreau39713102016-11-25 15:49:32 +010027#include <types/global.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010028#include <types/stats.h>
29
Willy Tarreaud9f316a2014-07-10 14:03:38 +020030#include <proto/arg.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010031#include <proto/cli.h>
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020032#include <proto/log.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020033#include <proto/proto_http.h>
Willy Tarreau7d562212016-11-25 16:10:05 +010034#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010035#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020036#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020037#include <proto/stream.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010038#include <proto/stream_interface.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020039#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010040#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020041#include <proto/peers.h>
Willy Tarreau39713102016-11-25 15:49:32 +010042#include <proto/tcp_rules.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010043
Willy Tarreau12785782012-04-27 21:37:17 +020044/* structure used to return a table key built from a sample */
Christopher Fauletca20d022017-08-29 15:30:31 +020045static struct stktable_key static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020046
Emeric Brun3bd697e2010-01-04 15:23:48 +010047/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020048 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
49 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010050 */
51void stksess_free(struct stktable *t, struct stksess *ts)
52{
53 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020054 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010055}
56
57/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020058 * Kill an stksess (only if its ref_cnt is zero).
59 */
60void stksess_kill(struct stktable *t, struct stksess *ts)
61{
62 if (ts->ref_cnt)
63 return;
64
65 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020066 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020067 ebmb_delete(&ts->key);
68 stksess_free(t, ts);
69}
70
71/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020072 * Initialize or update the key in the sticky session <ts> present in table <t>
73 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010074 */
Willy Tarreau393379c2010-06-06 12:11:37 +020075void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010076{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020077 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020078 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010079 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020080 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
81 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010082 }
83}
84
85
86/*
Willy Tarreau393379c2010-06-06 12:11:37 +020087 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
88 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 */
Willy Tarreau393379c2010-06-06 12:11:37 +020090static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010091{
Willy Tarreau393379c2010-06-06 12:11:37 +020092 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020093 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020094 ts->key.node.leaf_p = NULL;
95 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020096 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010097 return ts;
98}
99
100/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200101 * Trash oldest <to_batch> sticky sessions from table <t>
102 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100103 */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200104int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100105{
106 struct stksess *ts;
107 struct eb32_node *eb;
108 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200109 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100110
111 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
112
113 while (batched < to_batch) {
114
115 if (unlikely(!eb)) {
116 /* we might have reached the end of the tree, typically because
117 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200118 * half. Let's loop back to the beginning of the tree now if we
119 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100120 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200121 if (looped)
122 break;
123 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100124 eb = eb32_first(&t->exps);
125 if (likely(!eb))
126 break;
127 }
128
129 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200130 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100131 eb = eb32_next(eb);
132
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200133 /* don't delete an entry which is currently referenced */
134 if (ts->ref_cnt)
135 continue;
136
Willy Tarreau86257dc2010-06-06 12:57:10 +0200137 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100138
Willy Tarreau86257dc2010-06-06 12:57:10 +0200139 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140 if (!tick_isset(ts->expire))
141 continue;
142
Willy Tarreau86257dc2010-06-06 12:57:10 +0200143 ts->exp.key = ts->expire;
144 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100145
Willy Tarreau86257dc2010-06-06 12:57:10 +0200146 if (!eb || eb->key > ts->exp.key)
147 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100148
149 continue;
150 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100151
Willy Tarreauaea940e2010-06-06 11:56:36 +0200152 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200153 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200154 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100155 stksess_free(t, ts);
156 batched++;
157 }
158
159 return batched;
160}
161
162/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200163 * Allocate and initialise a new sticky session.
164 * The new sticky session is returned or NULL in case of lack of memory.
165 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200166 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
167 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100168 */
169struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
170{
171 struct stksess *ts;
172
173 if (unlikely(t->current == t->size)) {
174 if ( t->nopurge )
175 return NULL;
176
Emeric Brunfbce6d02010-09-23 18:10:00 +0200177 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100178 return NULL;
179 }
180
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100181 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100182 if (ts) {
183 t->current++;
Willy Tarreau51791462016-11-18 18:21:39 +0100184 ts = (void *)ts + t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200185 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200186 if (key)
187 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100188 }
189
190 return ts;
191}
192
193/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200194 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200195 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200197struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100198{
199 struct ebmb_node *eb;
200
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200201 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200202 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 +0100203 else
204 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
205
206 if (unlikely(!eb)) {
207 /* no session found */
208 return NULL;
209 }
210
Willy Tarreau86257dc2010-06-06 12:57:10 +0200211 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100212}
213
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200214/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
215 * This is mainly used for situations where we want to refresh a key's usage so
216 * that it does not expire, and we want to have it created if it was not there.
217 * The stksess is returned, or NULL if it could not be created.
218 */
219struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
220{
221 struct stksess *ts;
222
223 ts = stktable_lookup_key(table, key);
224 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200225 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200226
227 /* entry does not exist, initialize a new one */
228 ts = stksess_new(table, key);
229 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200230 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200231 return ts;
232}
233
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200234/*
235 * Looks in table <t> for a sticky session with same key as <ts>.
236 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200238struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100239{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100240 struct ebmb_node *eb;
241
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200242 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200243 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100244 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200245 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100246
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200247 if (unlikely(!eb))
248 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100249
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200250 return ebmb_entry(eb, struct stksess, key);
251}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100252
Willy Tarreaucb183642010-06-06 17:58:34 +0200253/* Update the expiration timer for <ts> but do not touch its expiration node.
254 * The table's expiration timer is updated if set.
255 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200256struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
257 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200258{
Emeric Brun85e77c72010-09-23 18:16:52 +0200259 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200260 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200261 if (t->expire) {
262 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
263 task_queue(t->exp_task);
264 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200265
Emeric Brunaaf58602015-06-15 17:23:30 +0200266 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200267 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200268 /* If this entry is not in the tree
269 or not scheduled for at least one peer */
270 if (!ts->upd.node.leaf_p
271 || (int)(t->commitupdate - ts->upd.key) >= 0
272 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200273 ts->upd.key = ++t->update;
274 t->localupdate = t->update;
275 eb32_delete(&ts->upd);
276 eb = eb32_insert(&t->updates, &ts->upd);
277 if (eb != &ts->upd) {
278 eb32_delete(eb);
279 eb32_insert(&t->updates, &ts->upd);
280 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200281 }
282 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
283 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200284 return ts;
285}
286
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200287/* Update the expiration timer for <ts> but do not touch its expiration node.
288 * The table's expiration timer is updated if set. The date of expiration coming from
289 * <t> stick-table configuration.
290 */
291struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
292{
293 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
294
295 return stktable_touch_with_exp(t, ts, local, expire);
296}
297
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200298/* Insert new sticky session <ts> in the table. It is assumed that it does not
299 * yet exist (the caller must check this). The table's timeout is updated if it
300 * is set. <ts> is returned.
301 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200302struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200303{
304 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200305 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200306 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200307 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200308 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100309}
310
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200311/* Same function as stktable_store(), but with <expire> as supplementary argument
312 * to set the date of expiration of <ts> new sticky session thanks to
313 * stktable_touch_with_exp().
314 */
315struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts,
316 int local, int expire)
317{
318 ebmb_insert(&t->keys, &ts->key, t->key_size);
319 stktable_touch_with_exp(t, ts, local, expire);
320 ts->exp.key = ts->expire;
321 eb32_insert(&t->exps, &ts->exp);
322 return ts;
323}
324
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200325/* Returns a valid or initialized stksess for the specified stktable_key in the
326 * specified table, or NULL if the key was NULL, or if no entry was found nor
327 * could be created. The entry's expiration is updated.
328 */
329struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
330{
331 struct stksess *ts;
332
333 if (!key)
334 return NULL;
335
336 ts = stktable_lookup_key(table, key);
337 if (ts == NULL) {
338 /* entry does not exist, initialize a new one */
339 ts = stksess_new(table, key);
340 if (!ts)
341 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200342 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200343 }
344 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200345 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200346 return ts;
347}
348
Emeric Brun3bd697e2010-01-04 15:23:48 +0100349/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200350 * Trash expired sticky sessions from table <t>. The next expiration date is
351 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100352 */
353static int stktable_trash_expired(struct stktable *t)
354{
355 struct stksess *ts;
356 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200357 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100358
359 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
360
361 while (1) {
362 if (unlikely(!eb)) {
363 /* we might have reached the end of the tree, typically because
364 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200365 * half. Let's loop back to the beginning of the tree now if we
366 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100367 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200368 if (looped)
369 break;
370 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100371 eb = eb32_first(&t->exps);
372 if (likely(!eb))
373 break;
374 }
375
376 if (likely(tick_is_lt(now_ms, eb->key))) {
377 /* timer not expired yet, revisit it later */
378 t->exp_next = eb->key;
379 return t->exp_next;
380 }
381
382 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200383 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100384 eb = eb32_next(eb);
385
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200386 /* don't delete an entry which is currently referenced */
387 if (ts->ref_cnt)
388 continue;
389
Willy Tarreau86257dc2010-06-06 12:57:10 +0200390 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100391
392 if (!tick_is_expired(ts->expire, now_ms)) {
393 if (!tick_isset(ts->expire))
394 continue;
395
Willy Tarreau86257dc2010-06-06 12:57:10 +0200396 ts->exp.key = ts->expire;
397 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100398
Willy Tarreau86257dc2010-06-06 12:57:10 +0200399 if (!eb || eb->key > ts->exp.key)
400 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100401 continue;
402 }
403
404 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200405 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200406 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100407 stksess_free(t, ts);
408 }
409
410 /* We have found no task to expire in any tree */
411 t->exp_next = TICK_ETERNITY;
412 return t->exp_next;
413}
414
415/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200416 * Task processing function to trash expired sticky sessions. A pointer to the
417 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100418 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200419static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100420{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200421 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100422
423 task->expire = stktable_trash_expired(t);
424 return task;
425}
426
Willy Tarreauaea940e2010-06-06 11:56:36 +0200427/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100428int stktable_init(struct stktable *t)
429{
430 if (t->size) {
431 memset(&t->keys, 0, sizeof(t->keys));
432 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100433 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100434
Willy Tarreau393379c2010-06-06 12:11:37 +0200435 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100436
437 t->exp_next = TICK_ETERNITY;
438 if ( t->expire ) {
439 t->exp_task = task_new();
440 t->exp_task->process = process_table_expire;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100441 t->exp_task->context = (void *)t;
442 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200443 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200444 peers_register_table(t->peers.p, t);
445 }
446
Emeric Brun3bd697e2010-01-04 15:23:48 +0100447 return t->pool != NULL;
448 }
449 return 1;
450}
451
452/*
453 * Configuration keywords of known table types
454 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200455struct stktable_type stktable_types[SMP_TYPES] = {
456 [SMP_T_SINT] = { "integer", 0, 4 },
457 [SMP_T_IPV4] = { "ip", 0, 4 },
458 [SMP_T_IPV6] = { "ipv6", 0, 16 },
459 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
460 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
461};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100462
463/*
464 * Parse table type configuration.
465 * Returns 0 on successful parsing, else 1.
466 * <myidx> is set at next configuration <args> index.
467 */
468int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
469{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200470 for (*type = 0; *type < SMP_TYPES; (*type)++) {
471 if (!stktable_types[*type].kw)
472 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100473 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
474 continue;
475
476 *key_size = stktable_types[*type].default_size;
477 (*myidx)++;
478
Willy Tarreauaea940e2010-06-06 11:56:36 +0200479 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100480 if (strcmp("len", args[*myidx]) == 0) {
481 (*myidx)++;
482 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200483 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100484 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200485 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200486 /* null terminated string needs +1 for '\0'. */
487 (*key_size)++;
488 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100489 (*myidx)++;
490 }
491 }
492 return 0;
493 }
494 return 1;
495}
496
Willy Tarreau8fed9032014-07-03 17:02:46 +0200497/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200498 * Note that the sample *is* modified and that the returned key may point
499 * to it, so the sample must not be modified afterwards before the lookup.
Willy Tarreau8fed9032014-07-03 17:02:46 +0200500 * Returns NULL if the sample could not be converted (eg: no matching type),
501 * otherwise a pointer to the static stktable_key filled with what is needed
502 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200503 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200504struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200505{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200507 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200508 return NULL;
509
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200510 /* Fill static_table_key. */
511 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200512
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200513 case SMP_T_IPV4:
Christopher Fauletca20d022017-08-29 15:30:31 +0200514 static_table_key.key = &smp->data.u.ipv4;
515 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200516 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200517
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200518 case SMP_T_IPV6:
Christopher Fauletca20d022017-08-29 15:30:31 +0200519 static_table_key.key = &smp->data.u.ipv6;
520 static_table_key.key_len = 16;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200521 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200522
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200523 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200524 /* The stick table require a 32bit unsigned int, "sint" is a
525 * signed 64 it, so we can convert it inplace.
526 */
527 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
Christopher Fauletca20d022017-08-29 15:30:31 +0200528 static_table_key.key = &smp->data.u.sint;
529 static_table_key.key_len = 4;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200530 break;
531
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200532 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200533 if (!smp_make_safe(smp))
534 return NULL;
Christopher Fauletca20d022017-08-29 15:30:31 +0200535 static_table_key.key = smp->data.u.str.str;
536 static_table_key.key_len = smp->data.u.str.len;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200537 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200538
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200539 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200540 if (smp->data.u.str.len < t->key_size) {
541 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200542 if (!smp_make_rw(smp))
543 return NULL;
544
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200545 if (smp->data.u.str.size < t->key_size)
546 if (!smp_dup(smp))
547 return NULL;
548 if (smp->data.u.str.size < t->key_size)
549 return NULL;
550 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
551 t->key_size - smp->data.u.str.len);
552 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200553 }
Christopher Fauletca20d022017-08-29 15:30:31 +0200554 static_table_key.key = smp->data.u.str.str;
555 static_table_key.key_len = smp->data.u.str.len;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200556 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200557
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200558 default: /* impossible case. */
559 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200560 }
561
Christopher Fauletca20d022017-08-29 15:30:31 +0200562 return &static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200563}
564
565/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200566 * Process a fetch + format conversion as defined by the sample expression <expr>
567 * on request or response considering the <opt> parameter. Returns either NULL if
568 * no key could be extracted, or a pointer to the converted result stored in
569 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
570 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200571 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
572 * without SMP_OPT_FINAL). The output will be usable like this :
573 *
574 * return MAY_CHANGE FINAL Meaning for the sample
575 * NULL 0 * Not present and will never be (eg: header)
576 * NULL 1 0 Not present or unstable, could change (eg: req_len)
577 * NULL 1 1 Not present, will not change anymore
578 * smp 0 * Present and will not change (eg: header)
579 * smp 1 0 not possible
580 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200581 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200582struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200583 unsigned int opt, struct sample_expr *expr, struct sample *smp)
584{
585 if (smp)
586 memset(smp, 0, sizeof(*smp));
587
Willy Tarreau192252e2015-04-04 01:47:55 +0200588 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200589 if (!smp)
590 return NULL;
591
592 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
593 return NULL; /* we can only use stable samples */
594
595 return smp_to_stkey(smp, t);
596}
597
598/*
Willy Tarreau12785782012-04-27 21:37:17 +0200599 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200600 * type <table_type>, otherwise zero. Used in configuration check.
601 */
Willy Tarreau12785782012-04-27 21:37:17 +0200602int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200603{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100604 int out_type;
605
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200606 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200607 return 0;
608
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100609 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200610
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200611 /* Convert sample. */
612 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100613 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200614
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200615 return 1;
616}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100617
Willy Tarreauedee1d62014-07-15 16:44:27 +0200618/* Extra data types processing : after the last one, some room may remain
619 * before STKTABLE_DATA_TYPES that may be used to register extra data types
620 * at run time.
621 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200622struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200623 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200624 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200625 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200626 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200627 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
628 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
629 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
630 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
631 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
632 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
633 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
634 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
635 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
636 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
637 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
638 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
639 [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau08d5f982010-06-06 13:34:54 +0200640};
641
Willy Tarreauedee1d62014-07-15 16:44:27 +0200642/* Registers stick-table extra data type with index <idx>, name <name>, type
643 * <std_type> and arg type <arg_type>. If the index is negative, the next free
644 * index is automatically allocated. The allocated index is returned, or -1 if
645 * no free index was found or <name> was already registered. The <name> is used
646 * directly as a pointer, so if it's not stable, the caller must allocate it.
647 */
648int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
649{
650 if (idx < 0) {
651 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
652 if (!stktable_data_types[idx].name)
653 break;
654
655 if (strcmp(stktable_data_types[idx].name, name) == 0)
656 return -1;
657 }
658 }
659
660 if (idx >= STKTABLE_DATA_TYPES)
661 return -1;
662
663 if (stktable_data_types[idx].name != NULL)
664 return -1;
665
666 stktable_data_types[idx].name = name;
667 stktable_data_types[idx].std_type = std_type;
668 stktable_data_types[idx].arg_type = arg_type;
669 return idx;
670}
671
Willy Tarreau08d5f982010-06-06 13:34:54 +0200672/*
673 * Returns the data type number for the stktable_data_type whose name is <name>,
674 * or <0 if not found.
675 */
676int stktable_get_data_type(char *name)
677{
678 int type;
679
680 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200681 if (!stktable_data_types[type].name)
682 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200683 if (strcmp(name, stktable_data_types[type].name) == 0)
684 return type;
685 }
686 return -1;
687}
688
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200689/* Casts sample <smp> to the type of the table specified in arg(0), and looks
690 * it up into this table. Returns true if found, false otherwise. The input
691 * type is STR so that input samples are converted to string (since all types
692 * can be converted to strings), then the function casts the string again into
693 * the table's type. This is a double conversion, but in the future we might
694 * support automatic input types to perform the cast on the fly.
695 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200696static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200697{
698 struct stktable *t;
699 struct stktable_key *key;
700 struct stksess *ts;
701
702 t = &arg_p[0].data.prx->table;
703
704 key = smp_to_stkey(smp, t);
705 if (!key)
706 return 0;
707
708 ts = stktable_lookup_key(t, key);
709
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200710 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200711 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200712 smp->flags = SMP_F_VOL_TEST;
713 return 1;
714}
715
716/* Casts sample <smp> to the type of the table specified in arg(0), and looks
717 * it up into this table. Returns the data rate received from clients in bytes/s
718 * if the key is present in the table, otherwise zero, so that comparisons can
719 * be easily performed. If the inspected parameter is not stored in the table,
720 * <not found> is returned.
721 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200722static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200723{
724 struct stktable *t;
725 struct stktable_key *key;
726 struct stksess *ts;
727 void *ptr;
728
729 t = &arg_p[0].data.prx->table;
730
731 key = smp_to_stkey(smp, t);
732 if (!key)
733 return 0;
734
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200735 ts = stktable_lookup_key(t, key);
736
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200737 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200739 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200740
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200741 if (!ts) /* key not present */
742 return 1;
743
744 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
745 if (!ptr)
746 return 0; /* parameter not stored */
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200749 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
750 return 1;
751}
752
753/* Casts sample <smp> to the type of the table specified in arg(0), and looks
754 * it up into this table. Returns the cumulated number of connections for the key
755 * if the key is present in the table, otherwise zero, so that comparisons can
756 * be easily performed. If the inspected parameter is not stored in the table,
757 * <not found> is returned.
758 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200759static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200760{
761 struct stktable *t;
762 struct stktable_key *key;
763 struct stksess *ts;
764 void *ptr;
765
766 t = &arg_p[0].data.prx->table;
767
768 key = smp_to_stkey(smp, t);
769 if (!key)
770 return 0;
771
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200772 ts = stktable_lookup_key(t, key);
773
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200774 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200775 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200776 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200777
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200778 if (!ts) /* key not present */
779 return 1;
780
781 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
782 if (!ptr)
783 return 0; /* parameter not stored */
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200786 return 1;
787}
788
789/* Casts sample <smp> to the type of the table specified in arg(0), and looks
790 * it up into this table. Returns the number of concurrent connections for the
791 * key if the key is present in the table, otherwise zero, so that comparisons
792 * can be easily performed. If the inspected parameter is not stored in the
793 * table, <not found> is returned.
794 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200795static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200796{
797 struct stktable *t;
798 struct stktable_key *key;
799 struct stksess *ts;
800 void *ptr;
801
802 t = &arg_p[0].data.prx->table;
803
804 key = smp_to_stkey(smp, t);
805 if (!key)
806 return 0;
807
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200808 ts = stktable_lookup_key(t, key);
809
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200810 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200812 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200813
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200814 if (!ts) /* key not present */
815 return 1;
816
817 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
818 if (!ptr)
819 return 0; /* parameter not stored */
820
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200821 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200822 return 1;
823}
824
825/* Casts sample <smp> to the type of the table specified in arg(0), and looks
826 * it up into this table. Returns the rate of incoming connections from the key
827 * if the key is present in the table, otherwise zero, so that comparisons can
828 * be easily performed. If the inspected parameter is not stored in the table,
829 * <not found> is returned.
830 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200831static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200832{
833 struct stktable *t;
834 struct stktable_key *key;
835 struct stksess *ts;
836 void *ptr;
837
838 t = &arg_p[0].data.prx->table;
839
840 key = smp_to_stkey(smp, t);
841 if (!key)
842 return 0;
843
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200844 ts = stktable_lookup_key(t, key);
845
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200846 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200847 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200848 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200849
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200850 if (!ts) /* key not present */
851 return 1;
852
853 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
854 if (!ptr)
855 return 0; /* parameter not stored */
856
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200857 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200858 t->data_arg[STKTABLE_DT_CONN_RATE].u);
859 return 1;
860}
861
862/* Casts sample <smp> to the type of the table specified in arg(0), and looks
863 * it up into this table. Returns the data rate sent to clients in bytes/s
864 * if the key is present in the table, otherwise zero, so that comparisons can
865 * be easily performed. If the inspected parameter is not stored in the table,
866 * <not found> is returned.
867 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200868static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200869{
870 struct stktable *t;
871 struct stktable_key *key;
872 struct stksess *ts;
873 void *ptr;
874
875 t = &arg_p[0].data.prx->table;
876
877 key = smp_to_stkey(smp, t);
878 if (!key)
879 return 0;
880
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200881 ts = stktable_lookup_key(t, key);
882
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200883 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200884 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200885 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200886
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200887 if (!ts) /* key not present */
888 return 1;
889
890 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE);
891 if (!ptr)
892 return 0; /* parameter not stored */
893
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200894 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200895 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
896 return 1;
897}
898
899/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200900 * it up into this table. Returns the value of the GPT0 tag for the key
901 * if the key is present in the table, otherwise false, so that comparisons can
902 * be easily performed. If the inspected parameter is not stored in the table,
903 * <not found> is returned.
904 */
905static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
906{
907 struct stktable *t;
908 struct stktable_key *key;
909 struct stksess *ts;
910 void *ptr;
911
912 t = &arg_p[0].data.prx->table;
913
914 key = smp_to_stkey(smp, t);
915 if (!key)
916 return 0;
917
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200918 ts = stktable_lookup_key(t, key);
919
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200920 smp->flags = SMP_F_VOL_TEST;
921 smp->data.type = SMP_T_SINT;
922 smp->data.u.sint = 0;
923
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200924 if (!ts) /* key not present */
925 return 1;
926
927 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
928 if (!ptr)
929 return 0; /* parameter not stored */
930
931 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
932 return 1;
933}
934
935/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200936 * it up into this table. Returns the value of the GPC0 counter for the key
937 * if the key is present in the table, otherwise zero, so that comparisons can
938 * be easily performed. If the inspected parameter is not stored in the table,
939 * <not found> is returned.
940 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200941static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200942{
943 struct stktable *t;
944 struct stktable_key *key;
945 struct stksess *ts;
946 void *ptr;
947
948 t = &arg_p[0].data.prx->table;
949
950 key = smp_to_stkey(smp, t);
951 if (!key)
952 return 0;
953
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200954 ts = stktable_lookup_key(t, key);
955
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200956 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200957 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200958 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200959
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200960 if (!ts) /* key not present */
961 return 1;
962
963 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
964 if (!ptr)
965 return 0; /* parameter not stored */
966
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200967 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200968 return 1;
969}
970
971/* Casts sample <smp> to the type of the table specified in arg(0), and looks
972 * it up into this table. Returns the event rate of the GPC0 counter for the key
973 * if the key is present in the table, otherwise zero, so that comparisons can
974 * be easily performed. If the inspected parameter is not stored in the table,
975 * <not found> is returned.
976 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200977static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200978{
979 struct stktable *t;
980 struct stktable_key *key;
981 struct stksess *ts;
982 void *ptr;
983
984 t = &arg_p[0].data.prx->table;
985
986 key = smp_to_stkey(smp, t);
987 if (!key)
988 return 0;
989
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200990 ts = stktable_lookup_key(t, key);
991
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200992 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200993 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200994 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200995
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200996 if (!ts) /* key not present */
997 return 1;
998
999 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
1000 if (!ptr)
1001 return 0; /* parameter not stored */
1002
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001004 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
1005 return 1;
1006}
1007
1008/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1009 * it up into this table. Returns the cumulated number of HTTP request errors
1010 * for the key if the key is present in the table, otherwise zero, so that
1011 * comparisons can be easily performed. If the inspected parameter is not stored
1012 * in the table, <not found> is returned.
1013 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001014static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001015{
1016 struct stktable *t;
1017 struct stktable_key *key;
1018 struct stksess *ts;
1019 void *ptr;
1020
1021 t = &arg_p[0].data.prx->table;
1022
1023 key = smp_to_stkey(smp, t);
1024 if (!key)
1025 return 0;
1026
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001027 ts = stktable_lookup_key(t, key);
1028
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001029 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001030 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001031 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001032
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001033 if (!ts) /* key not present */
1034 return 1;
1035
1036 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
1037 if (!ptr)
1038 return 0; /* parameter not stored */
1039
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001040 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001041 return 1;
1042}
1043
1044/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1045 * it up into this table. Returns the HTTP request error rate the key
1046 * if the key is present in the table, otherwise zero, so that comparisons can
1047 * be easily performed. If the inspected parameter is not stored in the table,
1048 * <not found> is returned.
1049 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001050static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001051{
1052 struct stktable *t;
1053 struct stktable_key *key;
1054 struct stksess *ts;
1055 void *ptr;
1056
1057 t = &arg_p[0].data.prx->table;
1058
1059 key = smp_to_stkey(smp, t);
1060 if (!key)
1061 return 0;
1062
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001063 ts = stktable_lookup_key(t, key);
1064
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001065 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001066 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001067 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001068
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001069 if (!ts) /* key not present */
1070 return 1;
1071
1072 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1073 if (!ptr)
1074 return 0; /* parameter not stored */
1075
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001076 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001077 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1078 return 1;
1079}
1080
1081/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1082 * it up into this table. Returns the cumulated number of HTTP request for the
1083 * key if the key is present in the table, otherwise zero, so that comparisons
1084 * can be easily performed. If the inspected parameter is not stored in the
1085 * table, <not found> is returned.
1086 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001087static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001088{
1089 struct stktable *t;
1090 struct stktable_key *key;
1091 struct stksess *ts;
1092 void *ptr;
1093
1094 t = &arg_p[0].data.prx->table;
1095
1096 key = smp_to_stkey(smp, t);
1097 if (!key)
1098 return 0;
1099
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001100 ts = stktable_lookup_key(t, key);
1101
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001102 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001103 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001104 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001105
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001106 if (!ts) /* key not present */
1107 return 1;
1108
1109 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1110 if (!ptr)
1111 return 0; /* parameter not stored */
1112
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001113 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001114 return 1;
1115}
1116
1117/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1118 * it up into this table. Returns the HTTP request rate the key if the key is
1119 * present in the table, otherwise zero, so that comparisons can be easily
1120 * performed. If the inspected parameter is not stored in the table, <not found>
1121 * is returned.
1122 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001123static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001124{
1125 struct stktable *t;
1126 struct stktable_key *key;
1127 struct stksess *ts;
1128 void *ptr;
1129
1130 t = &arg_p[0].data.prx->table;
1131
1132 key = smp_to_stkey(smp, t);
1133 if (!key)
1134 return 0;
1135
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001136 ts = stktable_lookup_key(t, key);
1137
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001138 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001139 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001140 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001141
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001142 if (!ts) /* key not present */
1143 return 1;
1144
1145 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1146 if (!ptr)
1147 return 0; /* parameter not stored */
1148
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001149 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001150 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1151 return 1;
1152}
1153
1154/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1155 * it up into this table. Returns the volume of datareceived from clients in kbytes
1156 * if the key is present in the table, otherwise zero, so that comparisons can
1157 * be easily performed. If the inspected parameter is not stored in the table,
1158 * <not found> is returned.
1159 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001160static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001161{
1162 struct stktable *t;
1163 struct stktable_key *key;
1164 struct stksess *ts;
1165 void *ptr;
1166
1167 t = &arg_p[0].data.prx->table;
1168
1169 key = smp_to_stkey(smp, t);
1170 if (!key)
1171 return 0;
1172
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001173 ts = stktable_lookup_key(t, key);
1174
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001175 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001176 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001177 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001178
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001179 if (!ts) /* key not present */
1180 return 1;
1181
1182 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1183 if (!ptr)
1184 return 0; /* parameter not stored */
1185
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001186 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001187 return 1;
1188}
1189
1190/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1191 * it up into this table. Returns the volume of data sent to clients in kbytes
1192 * if the key is present in the table, otherwise zero, so that comparisons can
1193 * be easily performed. If the inspected parameter is not stored in the table,
1194 * <not found> is returned.
1195 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001196static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001197{
1198 struct stktable *t;
1199 struct stktable_key *key;
1200 struct stksess *ts;
1201 void *ptr;
1202
1203 t = &arg_p[0].data.prx->table;
1204
1205 key = smp_to_stkey(smp, t);
1206 if (!key)
1207 return 0;
1208
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001209 ts = stktable_lookup_key(t, key);
1210
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001211 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001212 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001213 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001214
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001215 if (!ts) /* key not present */
1216 return 1;
1217
1218 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1219 if (!ptr)
1220 return 0; /* parameter not stored */
1221
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001222 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001223 return 1;
1224}
1225
1226/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1227 * it up into this table. Returns the server ID associated with the key if the
1228 * key is present in the table, otherwise zero, so that comparisons can be
1229 * easily performed. If the inspected parameter is not stored in the table,
1230 * <not found> is returned.
1231 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001232static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001233{
1234 struct stktable *t;
1235 struct stktable_key *key;
1236 struct stksess *ts;
1237 void *ptr;
1238
1239 t = &arg_p[0].data.prx->table;
1240
1241 key = smp_to_stkey(smp, t);
1242 if (!key)
1243 return 0;
1244
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001245 ts = stktable_lookup_key(t, key);
1246
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001247 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001248 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001249 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001250
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001251 if (!ts) /* key not present */
1252 return 1;
1253
1254 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1255 if (!ptr)
1256 return 0; /* parameter not stored */
1257
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001258 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001259 return 1;
1260}
1261
1262/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1263 * it up into this table. Returns the cumulated number of sessions for the
1264 * key if the key is present in the table, otherwise zero, so that comparisons
1265 * can be easily performed. If the inspected parameter is not stored in the
1266 * table, <not found> is returned.
1267 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001268static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001269{
1270 struct stktable *t;
1271 struct stktable_key *key;
1272 struct stksess *ts;
1273 void *ptr;
1274
1275 t = &arg_p[0].data.prx->table;
1276
1277 key = smp_to_stkey(smp, t);
1278 if (!key)
1279 return 0;
1280
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001281 ts = stktable_lookup_key(t, key);
1282
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001283 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001284 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001285 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001286
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001287 if (!ts) /* key not present */
1288 return 1;
1289
1290 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1291 if (!ptr)
1292 return 0; /* parameter not stored */
1293
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001294 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001295 return 1;
1296}
1297
1298/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1299 * it up into this table. Returns the session rate the key if the key is
1300 * present in the table, otherwise zero, so that comparisons can be easily
1301 * performed. If the inspected parameter is not stored in the table, <not found>
1302 * is returned.
1303 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001304static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001305{
1306 struct stktable *t;
1307 struct stktable_key *key;
1308 struct stksess *ts;
1309 void *ptr;
1310
1311 t = &arg_p[0].data.prx->table;
1312
1313 key = smp_to_stkey(smp, t);
1314 if (!key)
1315 return 0;
1316
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001317 ts = stktable_lookup_key(t, key);
1318
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001319 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001320 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001321 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001322
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001323 if (!ts) /* key not present */
1324 return 1;
1325
1326 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1327 if (!ptr)
1328 return 0; /* parameter not stored */
1329
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001330 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001331 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1332 return 1;
1333}
1334
1335/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1336 * it up into this table. Returns the amount of concurrent connections tracking
1337 * the same key if the key is present in the table, otherwise zero, so that
1338 * comparisons can be easily performed. If the inspected parameter is not
1339 * stored in the table, <not found> is returned.
1340 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001341static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001342{
1343 struct stktable *t;
1344 struct stktable_key *key;
1345 struct stksess *ts;
1346
1347 t = &arg_p[0].data.prx->table;
1348
1349 key = smp_to_stkey(smp, t);
1350 if (!key)
1351 return 0;
1352
Willy Tarreauf0c730a2016-05-25 17:07:56 +02001353 ts = stktable_lookup_key(t, key);
1354
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001356 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001357 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001358
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001359 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001360 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001361
1362 return 1;
1363}
1364
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001365/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001366static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001367 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001368{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001369 struct stksess *ts;
1370 struct stkctr *stkctr;
1371
1372 /* Extract the stksess, return OK if no stksess available. */
1373 if (s)
1374 stkctr = &s->stkctr[rule->arg.gpc.sc];
1375 else
1376 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001377
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001378 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001379 if (ts) {
1380 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001381
Willy Tarreau79c1e912016-01-25 14:54:45 +01001382 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1383 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
1384 if (ptr1)
1385 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1386 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001387
Willy Tarreau79c1e912016-01-25 14:54:45 +01001388 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1389 if (ptr2)
1390 stktable_data_cast(ptr2, gpc0)++;
1391
1392 /* If data was modified, we need to touch to re-schedule sync */
1393 if (ptr1 || ptr2)
1394 stktable_touch(stkctr->table, ts, 1);
1395 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001396 return ACT_RET_CONT;
1397}
1398
1399/* This function is a common parser for using variables. It understands
1400 * the formats:
1401 *
1402 * sc-inc-gpc0(<stick-table ID>)
1403 *
1404 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1405 * it returns 1 and the variable <expr> is filled with the pointer to the
1406 * expression to execute.
1407 */
1408static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1409 struct act_rule *rule, char **err)
1410{
1411 const char *cmd_name = args[*arg-1];
1412 char *error;
1413
1414 cmd_name += strlen("sc-inc-gpc0");
1415 if (*cmd_name == '\0') {
1416 /* default stick table id. */
1417 rule->arg.gpc.sc = 0;
1418 } else {
1419 /* parse the stick table id. */
1420 if (*cmd_name != '(') {
1421 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1422 return ACT_RET_PRS_ERR;
1423 }
1424 cmd_name++; /* jump the '(' */
1425 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1426 if (*error != ')') {
1427 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1428 return ACT_RET_PRS_ERR;
1429 }
1430
1431 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1432 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1433 ACT_ACTION_TRK_SCMAX-1);
1434 return ACT_RET_PRS_ERR;
1435 }
1436 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001437 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001438 rule->action_ptr = action_inc_gpc0;
1439 return ACT_RET_PRS_OK;
1440}
1441
1442/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001443static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001444 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001445{
1446 void *ptr;
1447 struct stksess *ts;
1448 struct stkctr *stkctr;
1449
1450 /* Extract the stksess, return OK if no stksess available. */
1451 if (s)
1452 stkctr = &s->stkctr[rule->arg.gpt.sc];
1453 else
1454 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001455
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001456 ts = stkctr_entry(stkctr);
1457 if (!ts)
1458 return ACT_RET_CONT;
1459
1460 /* Store the sample in the required sc, and ignore errors. */
1461 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001462 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001463 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001464 stktable_touch(stkctr->table, ts, 1);
1465 }
1466
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001467 return ACT_RET_CONT;
1468}
1469
1470/* This function is a common parser for using variables. It understands
1471 * the format:
1472 *
1473 * set-gpt0(<stick-table ID>) <expression>
1474 *
1475 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1476 * it returns 1 and the variable <expr> is filled with the pointer to the
1477 * expression to execute.
1478 */
1479static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1480 struct act_rule *rule, char **err)
1481
1482
1483{
1484 const char *cmd_name = args[*arg-1];
1485 char *error;
1486
1487 cmd_name += strlen("sc-set-gpt0");
1488 if (*cmd_name == '\0') {
1489 /* default stick table id. */
1490 rule->arg.gpt.sc = 0;
1491 } else {
1492 /* parse the stick table id. */
1493 if (*cmd_name != '(') {
1494 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1495 return ACT_RET_PRS_ERR;
1496 }
1497 cmd_name++; /* jump the '(' */
1498 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1499 if (*error != ')') {
1500 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1501 return ACT_RET_PRS_ERR;
1502 }
1503
1504 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1505 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1506 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1507 return ACT_RET_PRS_ERR;
1508 }
1509 }
1510
1511 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1512 if (*error != '\0') {
1513 memprintf(err, "invalid integer value '%s'", args[*arg]);
1514 return ACT_RET_PRS_ERR;
1515 }
1516 (*arg)++;
1517
Thierry FOURNIER42148732015-09-02 17:17:33 +02001518 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001519 rule->action_ptr = action_set_gpt0;
1520
1521 return ACT_RET_PRS_OK;
1522}
1523
Willy Tarreau7d562212016-11-25 16:10:05 +01001524/* set temp integer to the number of used entries in the table pointed to by expr.
1525 * Accepts exactly 1 argument of type table.
1526 */
1527static int
1528smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1529{
1530 smp->flags = SMP_F_VOL_TEST;
1531 smp->data.type = SMP_T_SINT;
1532 smp->data.u.sint = args->data.prx->table.current;
1533 return 1;
1534}
1535
1536/* set temp integer to the number of free entries in the table pointed to by expr.
1537 * Accepts exactly 1 argument of type table.
1538 */
1539static int
1540smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
1541{
1542 struct proxy *px;
1543
1544 px = args->data.prx;
1545 smp->flags = SMP_F_VOL_TEST;
1546 smp->data.type = SMP_T_SINT;
1547 smp->data.u.sint = px->table.size - px->table.current;
1548 return 1;
1549}
1550
1551/* Returns a pointer to a stkctr depending on the fetch keyword name.
1552 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
1553 * sc[0-9]_* will return a pointer to the respective field in the
1554 * stream <l4>. sc_* requires an UINT argument specifying the stick
1555 * counter number. src_* will fill a locally allocated structure with
1556 * the table and entry corresponding to what is specified with src_*.
1557 * NULL may be returned if the designated stkctr is not tracked. For
1558 * the sc_* and sc[0-9]_* forms, an optional table argument may be
1559 * passed. When present, the currently tracked key is then looked up
1560 * in the specified table instead of the current table. The purpose is
1561 * to be able to convery multiple values per key (eg: have gpc0 from
1562 * multiple tables). <strm> is allowed to be NULL, in which case only
1563 * the session will be consulted.
1564 */
1565struct stkctr *
1566smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
1567{
1568 static struct stkctr stkctr;
1569 struct stkctr *stkptr;
1570 struct stksess *stksess;
1571 unsigned int num = kw[2] - '0';
1572 int arg = 0;
1573
1574 if (num == '_' - '0') {
1575 /* sc_* variant, args[0] = ctr# (mandatory) */
1576 num = args[arg++].data.sint;
1577 if (num >= MAX_SESS_STKCTR)
1578 return NULL;
1579 }
1580 else if (num > 9) { /* src_* variant, args[0] = table */
1581 struct stktable_key *key;
1582 struct connection *conn = objt_conn(sess->origin);
1583 struct sample smp;
1584
1585 if (!conn)
1586 return NULL;
1587
1588 /* Fetch source adress in a sample. */
1589 smp.px = NULL;
1590 smp.sess = sess;
1591 smp.strm = strm;
1592 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
1593 return NULL;
1594
1595 /* Converts into key. */
1596 key = smp_to_stkey(&smp, &args->data.prx->table);
1597 if (!key)
1598 return NULL;
1599
1600 stkctr.table = &args->data.prx->table;
1601 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
1602 return &stkctr;
1603 }
1604
1605 /* Here, <num> contains the counter number from 0 to 9 for
1606 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
1607 * args[arg] is the first optional argument. We first lookup the
1608 * ctr form the stream, then from the session if it was not there.
1609 */
1610
1611 if (strm)
1612 stkptr = &strm->stkctr[num];
1613 if (!strm || !stkctr_entry(stkptr)) {
1614 stkptr = &sess->stkctr[num];
1615 if (!stkctr_entry(stkptr))
1616 return NULL;
1617 }
1618
1619 stksess = stkctr_entry(stkptr);
1620 if (!stksess)
1621 return NULL;
1622
1623 if (unlikely(args[arg].type == ARGT_TAB)) {
1624 /* an alternate table was specified, let's look up the same key there */
1625 stkctr.table = &args[arg].data.prx->table;
1626 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
1627 return &stkctr;
1628 }
1629 return stkptr;
1630}
1631
1632/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
1633 * the entry if it doesn't exist yet. This is needed for a few fetch
1634 * functions which need to create an entry, such as src_inc_gpc* and
1635 * src_clr_gpc*.
1636 */
1637struct stkctr *
1638smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
1639{
1640 static struct stkctr stkctr;
1641 struct stktable_key *key;
1642 struct connection *conn = objt_conn(sess->origin);
1643 struct sample smp;
1644
1645 if (strncmp(kw, "src_", 4) != 0)
1646 return NULL;
1647
1648 if (!conn)
1649 return NULL;
1650
1651 /* Fetch source adress in a sample. */
1652 smp.px = NULL;
1653 smp.sess = sess;
1654 smp.strm = strm;
1655 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
1656 return NULL;
1657
1658 /* Converts into key. */
1659 key = smp_to_stkey(&smp, &args->data.prx->table);
1660 if (!key)
1661 return NULL;
1662
1663 stkctr.table = &args->data.prx->table;
1664 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
1665 return &stkctr;
1666}
1667
1668/* set return a boolean indicating if the requested stream counter is
1669 * currently being tracked or not.
1670 * Supports being called as "sc[0-9]_tracked" only.
1671 */
1672static int
1673smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
1674{
1675 smp->flags = SMP_F_VOL_TEST;
1676 smp->data.type = SMP_T_BOOL;
1677 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1678 return 1;
1679}
1680
1681/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
1682 * frontend counters or from the src.
1683 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
1684 * zero is returned if the key is new.
1685 */
1686static int
1687smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1688{
1689 struct stkctr *stkctr;
1690
1691 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1692 if (!stkctr)
1693 return 0;
1694
1695 smp->flags = SMP_F_VOL_TEST;
1696 smp->data.type = SMP_T_SINT;
1697 smp->data.u.sint = 0;
1698
1699 if (stkctr_entry(stkctr) != NULL) {
1700 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
1701 if (!ptr)
1702 return 0; /* parameter not stored */
1703 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
1704 }
1705 return 1;
1706}
1707
1708/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
1709 * frontend counters or from the src.
1710 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
1711 * zero is returned if the key is new.
1712 */
1713static int
1714smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1715{
1716 struct stkctr *stkctr;
1717
1718 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1719 if (!stkctr)
1720 return 0;
1721
1722 smp->flags = SMP_F_VOL_TEST;
1723 smp->data.type = SMP_T_SINT;
1724 smp->data.u.sint = 0;
1725
1726 if (stkctr_entry(stkctr) != NULL) {
1727 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1728 if (!ptr)
1729 return 0; /* parameter not stored */
1730 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
1731 }
1732 return 1;
1733}
1734
1735/* set <smp> to the General Purpose Counter 0's event rate from the stream's
1736 * tracked frontend counters or from the src.
1737 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
1738 * Value zero is returned if the key is new.
1739 */
1740static int
1741smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1742{
1743 struct stkctr *stkctr;
1744
1745 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1746 if (!stkctr)
1747 return 0;
1748
1749 smp->flags = SMP_F_VOL_TEST;
1750 smp->data.type = SMP_T_SINT;
1751 smp->data.u.sint = 0;
1752 if (stkctr_entry(stkctr) != NULL) {
1753 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
1754 if (!ptr)
1755 return 0; /* parameter not stored */
1756 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
1757 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
1758 }
1759 return 1;
1760}
1761
1762/* Increment the General Purpose Counter 0 value from the stream's tracked
1763 * frontend counters and return it into temp integer.
1764 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
1765 */
1766static int
1767smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1768{
1769 struct stkctr *stkctr;
1770
1771 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1772 if (!stkctr)
1773 return 0;
1774
1775 smp->flags = SMP_F_VOL_TEST;
1776 smp->data.type = SMP_T_SINT;
1777 smp->data.u.sint = 0;
1778
1779 if (stkctr_entry(stkctr) == NULL)
1780 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
1781
1782 if (stkctr && stkctr_entry(stkctr)) {
1783 void *ptr1,*ptr2;
1784
1785 /* First, update gpc0_rate if it's tracked. Second, update its
1786 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
1787 */
1788 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
1789 if (ptr1) {
1790 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1791 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
1792 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
1793 }
1794
1795 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1796 if (ptr2)
1797 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
1798
1799 /* If data was modified, we need to touch to re-schedule sync */
1800 if (ptr1 || ptr2)
1801 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
1802 }
1803 return 1;
1804}
1805
1806/* Clear the General Purpose Counter 0 value from the stream's tracked
1807 * frontend counters and return its previous value into temp integer.
1808 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
1809 */
1810static int
1811smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
1812{
1813 struct stkctr *stkctr;
1814
1815 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1816 if (!stkctr)
1817 return 0;
1818
1819 smp->flags = SMP_F_VOL_TEST;
1820 smp->data.type = SMP_T_SINT;
1821 smp->data.u.sint = 0;
1822
1823 if (stkctr_entry(stkctr) == NULL)
1824 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
1825
1826 if (stkctr_entry(stkctr) != NULL) {
1827 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
1828 if (!ptr)
1829 return 0; /* parameter not stored */
1830 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
1831 stktable_data_cast(ptr, gpc0) = 0;
1832 /* If data was modified, we need to touch to re-schedule sync */
1833 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
1834 }
1835 return 1;
1836}
1837
1838/* set <smp> to the cumulated number of connections from the stream's tracked
1839 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
1840 * "src_conn_cnt" only.
1841 */
1842static int
1843smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1844{
1845 struct stkctr *stkctr;
1846
1847 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1848 if (!stkctr)
1849 return 0;
1850
1851 smp->flags = SMP_F_VOL_TEST;
1852 smp->data.type = SMP_T_SINT;
1853 smp->data.u.sint = 0;
1854 if (stkctr_entry(stkctr) != NULL) {
1855 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
1856 if (!ptr)
1857 return 0; /* parameter not stored */
1858 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
1859 }
1860 return 1;
1861}
1862
1863/* set <smp> to the connection rate from the stream's tracked frontend
1864 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
1865 * only.
1866 */
1867static int
1868smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1869{
1870 struct stkctr *stkctr;
1871
1872 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1873 if (!stkctr)
1874 return 0;
1875
1876 smp->flags = SMP_F_VOL_TEST;
1877 smp->data.type = SMP_T_SINT;
1878 smp->data.u.sint = 0;
1879 if (stkctr_entry(stkctr) != NULL) {
1880 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
1881 if (!ptr)
1882 return 0; /* parameter not stored */
1883 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
1884 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
1885 }
1886 return 1;
1887}
1888
1889/* set temp integer to the number of connections from the stream's source address
1890 * in the table pointed to by expr, after updating it.
1891 * Accepts exactly 1 argument of type table.
1892 */
1893static int
1894smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1895{
1896 struct connection *conn = objt_conn(smp->sess->origin);
1897 struct stksess *ts;
1898 struct stktable_key *key;
1899 void *ptr;
1900 struct proxy *px;
1901
1902 if (!conn)
1903 return 0;
1904
1905 /* Fetch source adress in a sample. */
1906 if (!smp_fetch_src(NULL, smp, NULL, NULL))
1907 return 0;
1908
1909 /* Converts into key. */
1910 key = smp_to_stkey(smp, &args->data.prx->table);
1911 if (!key)
1912 return 0;
1913
1914 px = args->data.prx;
1915
1916 if ((ts = stktable_update_key(&px->table, key)) == NULL)
1917 /* entry does not exist and could not be created */
1918 return 0;
1919
1920 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
1921 if (!ptr)
1922 return 0; /* parameter not stored in this table */
1923
1924 smp->data.type = SMP_T_SINT;
1925 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
1926 /* Touch was previously performed by stktable_update_key */
1927 smp->flags = SMP_F_VOL_TEST;
1928 return 1;
1929}
1930
1931/* set <smp> to the number of concurrent connections from the stream's tracked
1932 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
1933 * "src_conn_cur" only.
1934 */
1935static int
1936smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
1937{
1938 struct stkctr *stkctr;
1939
1940 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1941 if (!stkctr)
1942 return 0;
1943
1944 smp->flags = SMP_F_VOL_TEST;
1945 smp->data.type = SMP_T_SINT;
1946 smp->data.u.sint = 0;
1947 if (stkctr_entry(stkctr) != NULL) {
1948 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
1949 if (!ptr)
1950 return 0; /* parameter not stored */
1951 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
1952 }
1953 return 1;
1954}
1955
1956/* set <smp> to the cumulated number of streams from the stream's tracked
1957 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
1958 * "src_sess_cnt" only.
1959 */
1960static int
1961smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1962{
1963 struct stkctr *stkctr;
1964
1965 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1966 if (!stkctr)
1967 return 0;
1968
1969 smp->flags = SMP_F_VOL_TEST;
1970 smp->data.type = SMP_T_SINT;
1971 smp->data.u.sint = 0;
1972 if (stkctr_entry(stkctr) != NULL) {
1973 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
1974 if (!ptr)
1975 return 0; /* parameter not stored */
1976 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
1977 }
1978 return 1;
1979}
1980
1981/* set <smp> to the stream rate from the stream's tracked frontend counters.
1982 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
1983 */
1984static int
1985smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
1986{
1987 struct stkctr *stkctr;
1988
1989 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
1990 if (!stkctr)
1991 return 0;
1992
1993 smp->flags = SMP_F_VOL_TEST;
1994 smp->data.type = SMP_T_SINT;
1995 smp->data.u.sint = 0;
1996 if (stkctr_entry(stkctr) != NULL) {
1997 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
1998 if (!ptr)
1999 return 0; /* parameter not stored */
2000 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
2001 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
2002 }
2003 return 1;
2004}
2005
2006/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
2007 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2008 * "src_http_req_cnt" only.
2009 */
2010static int
2011smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2012{
2013 struct stkctr *stkctr;
2014
2015 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2016 if (!stkctr)
2017 return 0;
2018
2019 smp->flags = SMP_F_VOL_TEST;
2020 smp->data.type = SMP_T_SINT;
2021 smp->data.u.sint = 0;
2022 if (stkctr_entry(stkctr) != NULL) {
2023 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
2024 if (!ptr)
2025 return 0; /* parameter not stored */
2026 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
2027 }
2028 return 1;
2029}
2030
2031/* set <smp> to the HTTP request rate from the stream's tracked frontend
2032 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2033 * "src_http_req_rate" only.
2034 */
2035static int
2036smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2037{
2038 struct stkctr *stkctr;
2039
2040 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2041 if (!stkctr)
2042 return 0;
2043
2044 smp->flags = SMP_F_VOL_TEST;
2045 smp->data.type = SMP_T_SINT;
2046 smp->data.u.sint = 0;
2047 if (stkctr_entry(stkctr) != NULL) {
2048 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
2049 if (!ptr)
2050 return 0; /* parameter not stored */
2051 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2052 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
2053 }
2054 return 1;
2055}
2056
2057/* set <smp> to the cumulated number of HTTP requests errors from the stream's
2058 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2059 * "src_http_err_cnt" only.
2060 */
2061static int
2062smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2063{
2064 struct stkctr *stkctr;
2065
2066 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2067 if (!stkctr)
2068 return 0;
2069
2070 smp->flags = SMP_F_VOL_TEST;
2071 smp->data.type = SMP_T_SINT;
2072 smp->data.u.sint = 0;
2073 if (stkctr_entry(stkctr) != NULL) {
2074 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
2075 if (!ptr)
2076 return 0; /* parameter not stored */
2077 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
2078 }
2079 return 1;
2080}
2081
2082/* set <smp> to the HTTP request error rate from the stream's tracked frontend
2083 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2084 * "src_http_err_rate" only.
2085 */
2086static int
2087smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2088{
2089 struct stkctr *stkctr;
2090
2091 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2092 if (!stkctr)
2093 return 0;
2094
2095 smp->flags = SMP_F_VOL_TEST;
2096 smp->data.type = SMP_T_SINT;
2097 smp->data.u.sint = 0;
2098 if (stkctr_entry(stkctr) != NULL) {
2099 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
2100 if (!ptr)
2101 return 0; /* parameter not stored */
2102 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2103 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
2104 }
2105 return 1;
2106}
2107
2108/* set <smp> to the number of kbytes received from clients, as found in the
2109 * stream's tracked frontend counters. Supports being called as
2110 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
2111 */
2112static int
2113smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
2114{
2115 struct stkctr *stkctr;
2116
2117 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2118 if (!stkctr)
2119 return 0;
2120
2121 smp->flags = SMP_F_VOL_TEST;
2122 smp->data.type = SMP_T_SINT;
2123 smp->data.u.sint = 0;
2124 if (stkctr_entry(stkctr) != NULL) {
2125 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
2126 if (!ptr)
2127 return 0; /* parameter not stored */
2128 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
2129 }
2130 return 1;
2131}
2132
2133/* set <smp> to the data rate received from clients in bytes/s, as found
2134 * in the stream's tracked frontend counters. Supports being called as
2135 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
2136 */
2137static int
2138smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2139{
2140 struct stkctr *stkctr;
2141
2142 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2143 if (!stkctr)
2144 return 0;
2145
2146 smp->flags = SMP_F_VOL_TEST;
2147 smp->data.type = SMP_T_SINT;
2148 smp->data.u.sint = 0;
2149 if (stkctr_entry(stkctr) != NULL) {
2150 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
2151 if (!ptr)
2152 return 0; /* parameter not stored */
2153 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
2154 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
2155 }
2156 return 1;
2157}
2158
2159/* set <smp> to the number of kbytes sent to clients, as found in the
2160 * stream's tracked frontend counters. Supports being called as
2161 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
2162 */
2163static int
2164smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
2165{
2166 struct stkctr *stkctr;
2167
2168 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2169 if (!stkctr)
2170 return 0;
2171
2172 smp->flags = SMP_F_VOL_TEST;
2173 smp->data.type = SMP_T_SINT;
2174 smp->data.u.sint = 0;
2175 if (stkctr_entry(stkctr) != NULL) {
2176 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
2177 if (!ptr)
2178 return 0; /* parameter not stored */
2179 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
2180 }
2181 return 1;
2182}
2183
2184/* set <smp> to the data rate sent to clients in bytes/s, as found in the
2185 * stream's tracked frontend counters. Supports being called as
2186 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
2187 */
2188static int
2189smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
2190{
2191 struct stkctr *stkctr;
2192
2193 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2194 if (!stkctr)
2195 return 0;
2196
2197 smp->flags = SMP_F_VOL_TEST;
2198 smp->data.type = SMP_T_SINT;
2199 smp->data.u.sint = 0;
2200 if (stkctr_entry(stkctr) != NULL) {
2201 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
2202 if (!ptr)
2203 return 0; /* parameter not stored */
2204 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
2205 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
2206 }
2207 return 1;
2208}
2209
2210/* set <smp> to the number of active trackers on the SC entry in the stream's
2211 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
2212 */
2213static int
2214smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
2215{
2216 struct stkctr *stkctr;
2217
2218 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2219 if (!stkctr)
2220 return 0;
2221
2222 smp->flags = SMP_F_VOL_TEST;
2223 smp->data.type = SMP_T_SINT;
2224 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
2225 return 1;
2226}
2227
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002228
2229/* The functions below are used to manipulate table contents from the CLI.
2230 * There are 3 main actions, "clear", "set" and "show". The code is shared
2231 * between all actions, and the action is encoded in the void *private in
2232 * the appctx as well as in the keyword registration, among one of the
2233 * following values.
2234 */
2235
2236enum {
2237 STK_CLI_ACT_CLR,
2238 STK_CLI_ACT_SET,
2239 STK_CLI_ACT_SHOW,
2240};
2241
2242/* Dump the status of a table to a stream interface's
2243 * read buffer. It returns 0 if the output buffer is full
2244 * and needs to be called again, otherwise non-zero.
2245 */
2246static int table_dump_head_to_buffer(struct chunk *msg, struct stream_interface *si,
2247 struct proxy *proxy, struct proxy *target)
2248{
2249 struct stream *s = si_strm(si);
2250
2251 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
2252 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
2253
2254 /* any other information should be dumped here */
2255
William Lallemand07a62f72017-05-24 00:57:40 +02002256 if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002257 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
2258
Willy Tarreau06d80a92017-10-19 14:32:15 +02002259 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002260 si_applet_cant_put(si);
2261 return 0;
2262 }
2263
2264 return 1;
2265}
2266
2267/* Dump a table entry to a stream interface's
2268 * read buffer. It returns 0 if the output buffer is full
2269 * and needs to be called again, otherwise non-zero.
2270 */
2271static int table_dump_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
2272 struct proxy *proxy, struct stksess *entry)
2273{
2274 int dt;
2275
2276 chunk_appendf(msg, "%p:", entry);
2277
2278 if (proxy->table.type == SMP_T_IPV4) {
2279 char addr[INET_ADDRSTRLEN];
2280 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
2281 chunk_appendf(msg, " key=%s", addr);
2282 }
2283 else if (proxy->table.type == SMP_T_IPV6) {
2284 char addr[INET6_ADDRSTRLEN];
2285 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
2286 chunk_appendf(msg, " key=%s", addr);
2287 }
2288 else if (proxy->table.type == SMP_T_SINT) {
2289 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
2290 }
2291 else if (proxy->table.type == SMP_T_STR) {
2292 chunk_appendf(msg, " key=");
2293 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
2294 }
2295 else {
2296 chunk_appendf(msg, " key=");
2297 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
2298 }
2299
2300 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
2301
2302 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
2303 void *ptr;
2304
2305 if (proxy->table.data_ofs[dt] == 0)
2306 continue;
2307 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
2308 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
2309 else
2310 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
2311
2312 ptr = stktable_data_ptr(&proxy->table, entry, dt);
2313 switch (stktable_data_types[dt].std_type) {
2314 case STD_T_SINT:
2315 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
2316 break;
2317 case STD_T_UINT:
2318 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
2319 break;
2320 case STD_T_ULL:
2321 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
2322 break;
2323 case STD_T_FRQP:
2324 chunk_appendf(msg, "%d",
2325 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2326 proxy->table.data_arg[dt].u));
2327 break;
2328 }
2329 }
2330 chunk_appendf(msg, "\n");
2331
Willy Tarreau06d80a92017-10-19 14:32:15 +02002332 if (ci_putchk(si_ic(si), msg) == -1) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002333 si_applet_cant_put(si);
2334 return 0;
2335 }
2336
2337 return 1;
2338}
2339
2340
2341/* Processes a single table entry matching a specific key passed in argument.
2342 * returns 0 if wants to be called again, 1 if has ended processing.
2343 */
2344static int table_process_entry_per_key(struct appctx *appctx, char **args)
2345{
2346 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002347 struct proxy *px = appctx->ctx.table.target;
2348 struct stksess *ts;
2349 uint32_t uint32_key;
2350 unsigned char ip6_key[sizeof(struct in6_addr)];
2351 long long value;
2352 int data_type;
2353 int cur_arg;
2354 void *ptr;
2355 struct freq_ctr_period *frqp;
2356
2357 if (!*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002358 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002359 appctx->ctx.cli.msg = "Key value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002360 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002361 return 1;
2362 }
2363
2364 switch (px->table.type) {
2365 case SMP_T_IPV4:
2366 uint32_key = htonl(inetaddr_host(args[4]));
Christopher Fauletca20d022017-08-29 15:30:31 +02002367 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002368 break;
2369 case SMP_T_IPV6:
2370 inet_pton(AF_INET6, args[4], ip6_key);
Christopher Fauletca20d022017-08-29 15:30:31 +02002371 static_table_key.key = &ip6_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002372 break;
2373 case SMP_T_SINT:
2374 {
2375 char *endptr;
2376 unsigned long val;
2377 errno = 0;
2378 val = strtoul(args[4], &endptr, 10);
2379 if ((errno == ERANGE && val == ULONG_MAX) ||
2380 (errno != 0 && val == 0) || endptr == args[4] ||
2381 val > 0xffffffff) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002382 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002383 appctx->ctx.cli.msg = "Invalid key\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002384 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002385 return 1;
2386 }
2387 uint32_key = (uint32_t) val;
Christopher Fauletca20d022017-08-29 15:30:31 +02002388 static_table_key.key = &uint32_key;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002389 break;
2390 }
2391 break;
2392 case SMP_T_STR:
Christopher Fauletca20d022017-08-29 15:30:31 +02002393 static_table_key.key = args[4];
2394 static_table_key.key_len = strlen(args[4]);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002395 break;
2396 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002397 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002398 case STK_CLI_ACT_SHOW:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002399 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002400 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2401 break;
2402 case STK_CLI_ACT_CLR:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002403 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002404 appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2405 break;
2406 case STK_CLI_ACT_SET:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002407 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002408 appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n";
2409 break;
2410 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002411 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002412 appctx->ctx.cli.msg = "Unknown action\n";
2413 break;
2414 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002415 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002416 return 1;
2417 }
2418
2419 /* check permissions */
2420 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2421 return 1;
2422
Christopher Fauletca20d022017-08-29 15:30:31 +02002423 ts = stktable_lookup_key(&px->table, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002424
Willy Tarreaua24bc782016-12-14 15:50:35 +01002425 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002426 case STK_CLI_ACT_SHOW:
2427 if (!ts)
2428 return 1;
2429 chunk_reset(&trash);
2430 if (!table_dump_head_to_buffer(&trash, si, px, px))
2431 return 0;
2432 if (!table_dump_entry_to_buffer(&trash, si, px, ts))
2433 return 0;
2434 break;
2435
2436 case STK_CLI_ACT_CLR:
2437 if (!ts)
2438 return 1;
2439 if (ts->ref_cnt) {
2440 /* don't delete an entry which is currently referenced */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002441 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002442 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002443 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002444 return 1;
2445 }
2446 stksess_kill(&px->table, ts);
2447 break;
2448
2449 case STK_CLI_ACT_SET:
2450 if (ts)
2451 stktable_touch(&px->table, ts, 1);
2452 else {
Christopher Fauletca20d022017-08-29 15:30:31 +02002453 ts = stksess_new(&px->table, &static_table_key);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002454 if (!ts) {
2455 /* don't delete an entry which is currently referenced */
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002456 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002457 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002458 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002459 return 1;
2460 }
2461 stktable_store(&px->table, ts, 1);
2462 }
2463
2464 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
2465 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002466 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002467 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002468 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002469 return 1;
2470 }
2471
2472 data_type = stktable_get_data_type(args[cur_arg] + 5);
2473 if (data_type < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002474 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002475 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002476 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002477 return 1;
2478 }
2479
2480 if (!px->table.data_ofs[data_type]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002481 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002482 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002483 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002484 return 1;
2485 }
2486
2487 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002488 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002489 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002490 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002491 return 1;
2492 }
2493
2494 ptr = stktable_data_ptr(&px->table, ts, data_type);
2495
2496 switch (stktable_data_types[data_type].std_type) {
2497 case STD_T_SINT:
2498 stktable_data_cast(ptr, std_t_sint) = value;
2499 break;
2500 case STD_T_UINT:
2501 stktable_data_cast(ptr, std_t_uint) = value;
2502 break;
2503 case STD_T_ULL:
2504 stktable_data_cast(ptr, std_t_ull) = value;
2505 break;
2506 case STD_T_FRQP:
2507 /* We set both the current and previous values. That way
2508 * the reported frequency is stable during all the period
2509 * then slowly fades out. This allows external tools to
2510 * push measures without having to update them too often.
2511 */
2512 frqp = &stktable_data_cast(ptr, std_t_frqp);
2513 frqp->curr_tick = now_ms;
2514 frqp->prev_ctr = 0;
2515 frqp->curr_ctr = value;
2516 break;
2517 }
2518 }
2519 break;
2520
2521 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002522 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002523 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002524 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002525 break;
2526 }
2527 return 1;
2528}
2529
2530/* Prepares the appctx fields with the data-based filters from the command line.
2531 * Returns 0 if the dump can proceed, 1 if has ended processing.
2532 */
2533static int table_prepare_data_request(struct appctx *appctx, char **args)
2534{
Willy Tarreaua24bc782016-12-14 15:50:35 +01002535 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002536 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002537 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002538 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002539 return 1;
2540 }
2541
2542 /* condition on stored data value */
2543 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
2544 if (appctx->ctx.table.data_type < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002545 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002546 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002547 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002548 return 1;
2549 }
2550
2551 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002552 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002553 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002554 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002555 return 1;
2556 }
2557
2558 appctx->ctx.table.data_op = get_std_op(args[4]);
2559 if (appctx->ctx.table.data_op < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002560 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002561 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002562 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002563 return 1;
2564 }
2565
2566 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002567 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002568 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002569 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002570 return 1;
2571 }
2572
2573 /* OK we're done, all the fields are set */
2574 return 0;
2575}
2576
2577/* returns 0 if wants to be called, 1 if has ended processing */
2578static int cli_parse_table_req(char **args, struct appctx *appctx, void *private)
2579{
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002580 appctx->ctx.table.data_type = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002581 appctx->ctx.table.target = NULL;
2582 appctx->ctx.table.proxy = NULL;
2583 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002584 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002585
2586 if (*args[2]) {
2587 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
2588 if (!appctx->ctx.table.target) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002589 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002590 appctx->ctx.cli.msg = "No such table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002591 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002592 return 1;
2593 }
2594 }
2595 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01002596 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002597 goto err_args;
2598 return 0;
2599 }
2600
2601 if (strcmp(args[3], "key") == 0)
2602 return table_process_entry_per_key(appctx, args);
2603 else if (strncmp(args[3], "data.", 5) == 0)
2604 return table_prepare_data_request(appctx, args);
2605 else if (*args[3])
2606 goto err_args;
2607
2608 return 0;
2609
2610err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002611 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002612 case STK_CLI_ACT_SHOW:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002613 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002614 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
2615 break;
2616 case STK_CLI_ACT_CLR:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002617 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002618 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
2619 break;
2620 case STK_CLI_ACT_SET:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002621 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002622 appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n";
2623 break;
2624 default:
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02002625 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002626 appctx->ctx.cli.msg = "Unknown action\n";
2627 break;
2628 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002629 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002630 return 1;
2631}
2632
2633/* This function is used to deal with table operations (dump or clear depending
2634 * on the action stored in appctx->private). It returns 0 if the output buffer is
2635 * full and it needs to be called again, otherwise non-zero.
2636 */
2637static int cli_io_handler_table(struct appctx *appctx)
2638{
2639 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002640 struct stream *s = si_strm(si);
2641 struct ebmb_node *eb;
2642 int dt;
2643 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002644 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002645
2646 /*
2647 * We have 3 possible states in appctx->st2 :
2648 * - STAT_ST_INIT : the first call
2649 * - STAT_ST_INFO : the proxy pointer points to the next table to
2650 * dump, the entry pointer is NULL ;
2651 * - STAT_ST_LIST : the proxy pointer points to the current table
2652 * and the entry pointer points to the next entry to be dumped,
2653 * and the refcount on the next entry is held ;
2654 * - STAT_ST_END : nothing left to dump, the buffer may contain some
2655 * data though.
2656 */
2657
2658 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2659 /* in case of abort, remove any refcount we might have set on an entry */
2660 if (appctx->st2 == STAT_ST_LIST) {
2661 appctx->ctx.table.entry->ref_cnt--;
2662 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2663 }
2664 return 1;
2665 }
2666
2667 chunk_reset(&trash);
2668
2669 while (appctx->st2 != STAT_ST_FIN) {
2670 switch (appctx->st2) {
2671 case STAT_ST_INIT:
2672 appctx->ctx.table.proxy = appctx->ctx.table.target;
2673 if (!appctx->ctx.table.proxy)
2674 appctx->ctx.table.proxy = proxy;
2675
2676 appctx->ctx.table.entry = NULL;
2677 appctx->st2 = STAT_ST_INFO;
2678 break;
2679
2680 case STAT_ST_INFO:
2681 if (!appctx->ctx.table.proxy ||
2682 (appctx->ctx.table.target &&
2683 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
2684 appctx->st2 = STAT_ST_END;
2685 break;
2686 }
2687
2688 if (appctx->ctx.table.proxy->table.size) {
2689 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.target))
2690 return 0;
2691
2692 if (appctx->ctx.table.target &&
William Lallemand07a62f72017-05-24 00:57:40 +02002693 (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002694 /* dump entries only if table explicitly requested */
2695 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
2696 if (eb) {
2697 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2698 appctx->ctx.table.entry->ref_cnt++;
2699 appctx->st2 = STAT_ST_LIST;
2700 break;
2701 }
2702 }
2703 }
2704 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2705 break;
2706
2707 case STAT_ST_LIST:
2708 skip_entry = 0;
2709
2710 if (appctx->ctx.table.data_type >= 0) {
2711 /* we're filtering on some data contents */
2712 void *ptr;
2713 long long data;
2714
2715 dt = appctx->ctx.table.data_type;
2716 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
2717 appctx->ctx.table.entry,
2718 dt);
2719
2720 data = 0;
2721 switch (stktable_data_types[dt].std_type) {
2722 case STD_T_SINT:
2723 data = stktable_data_cast(ptr, std_t_sint);
2724 break;
2725 case STD_T_UINT:
2726 data = stktable_data_cast(ptr, std_t_uint);
2727 break;
2728 case STD_T_ULL:
2729 data = stktable_data_cast(ptr, std_t_ull);
2730 break;
2731 case STD_T_FRQP:
2732 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2733 appctx->ctx.table.proxy->table.data_arg[dt].u);
2734 break;
2735 }
2736
2737 /* skip the entry if the data does not match the test and the value */
2738 if ((data < appctx->ctx.table.value &&
2739 (appctx->ctx.table.data_op == STD_OP_EQ ||
2740 appctx->ctx.table.data_op == STD_OP_GT ||
2741 appctx->ctx.table.data_op == STD_OP_GE)) ||
2742 (data == appctx->ctx.table.value &&
2743 (appctx->ctx.table.data_op == STD_OP_NE ||
2744 appctx->ctx.table.data_op == STD_OP_GT ||
2745 appctx->ctx.table.data_op == STD_OP_LT)) ||
2746 (data > appctx->ctx.table.value &&
2747 (appctx->ctx.table.data_op == STD_OP_EQ ||
2748 appctx->ctx.table.data_op == STD_OP_LT ||
2749 appctx->ctx.table.data_op == STD_OP_LE)))
2750 skip_entry = 1;
2751 }
2752
2753 if (show && !skip_entry &&
2754 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.entry))
2755 return 0;
2756
2757 appctx->ctx.table.entry->ref_cnt--;
2758
2759 eb = ebmb_next(&appctx->ctx.table.entry->key);
2760 if (eb) {
2761 struct stksess *old = appctx->ctx.table.entry;
2762 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2763 if (show)
2764 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
2765 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2766 stksess_kill(&appctx->ctx.table.proxy->table, old);
2767 appctx->ctx.table.entry->ref_cnt++;
2768 break;
2769 }
2770
2771
2772 if (show)
2773 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2774 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2775 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2776
2777 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2778 appctx->st2 = STAT_ST_INFO;
2779 break;
2780
2781 case STAT_ST_END:
2782 appctx->st2 = STAT_ST_FIN;
2783 break;
2784 }
2785 }
2786 return 1;
2787}
2788
2789static void cli_release_show_table(struct appctx *appctx)
2790{
2791 if (appctx->st2 == STAT_ST_LIST) {
2792 appctx->ctx.table.entry->ref_cnt--;
2793 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2794 }
2795}
2796
2797/* register cli keywords */
2798static struct cli_kw_list cli_kws = {{ },{
2799 { { "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 },
2800 { { "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 },
2801 { { "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 },
2802 {{},}
2803}};
2804
2805
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002806static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002807 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002808 { "sc-set-gpt0", parse_set_gpt0, 1 },
2809 { /* END */ }
2810}};
2811
Willy Tarreau620408f2016-10-21 16:37:51 +02002812static struct action_kw_list tcp_sess_kws = { { }, {
2813 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
2814 { "sc-set-gpt0", parse_set_gpt0, 1 },
2815 { /* END */ }
2816}};
2817
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002818static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002819 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002820 { "sc-set-gpt0", parse_set_gpt0, 1 },
2821 { /* END */ }
2822}};
2823
2824static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002825 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002826 { "sc-set-gpt0", parse_set_gpt0, 1 },
2827 { /* END */ }
2828}};
2829
2830static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002831 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002832 { "sc-set-gpt0", parse_set_gpt0, 1 },
2833 { /* END */ }
2834}};
2835
2836static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002837 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002838 { "sc-set-gpt0", parse_set_gpt0, 1 },
2839 { /* END */ }
2840}};
2841
Willy Tarreau7d562212016-11-25 16:10:05 +01002842///* Note: must not be declared <const> as its list will be overwritten.
2843// * Please take care of keeping this list alphabetically sorted.
2844// */
2845//static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2846// { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2847// { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2848// { /* END */ },
2849//}};
2850/* Note: must not be declared <const> as its list will be overwritten.
2851 * Please take care of keeping this list alphabetically sorted.
2852 */
2853static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2854 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2855 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2856 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2857 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2858 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2859 { "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 +01002860 { "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 +01002861 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2862 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2863 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2864 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2865 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2866 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2867 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2868 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2869 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2870 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2871 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2872 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2873 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2874 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2875 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2876 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2877 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2878 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2879 { "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 +01002880 { "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 +01002881 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2882 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2883 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2884 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2885 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2886 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2887 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2888 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2889 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2890 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2891 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2892 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2893 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2894 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2895 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2896 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2897 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2898 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2899 { "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 +01002900 { "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 +01002901 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2902 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2903 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2904 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2905 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2906 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2907 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2908 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2909 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2910 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2911 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2912 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2913 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2914 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2915 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2916 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2917 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2918 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2919 { "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 +01002920 { "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 +01002921 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2922 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2923 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2924 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2925 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2926 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2927 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2928 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2929 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2930 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2931 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2932 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2933 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2934 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2935 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2936 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2937 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2938 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2939 { "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 +01002940 { "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 +01002941 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2942 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2943 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2944 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2945 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2946 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2947 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2948 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2949 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2950 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2951 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2952 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2953 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2954 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2955 { /* END */ },
2956}};
2957
2958
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002959/* Note: must not be declared <const> as its list will be overwritten */
2960static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02002961 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
2962 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2963 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2964 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2965 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2966 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2967 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2968 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2969 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2970 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2971 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2972 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2973 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2974 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2975 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2976 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2977 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2978 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2979 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002980 { /* END */ },
2981}};
2982
2983__attribute__((constructor))
2984static void __stick_table_init(void)
2985{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002986 /* register som action keywords. */
2987 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02002988 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002989 tcp_req_cont_keywords_register(&tcp_req_kws);
2990 tcp_res_cont_keywords_register(&tcp_res_kws);
2991 http_req_keywords_register(&http_req_kws);
2992 http_res_keywords_register(&http_res_kws);
2993
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002994 /* register sample fetch and format conversion keywords */
Willy Tarreau7d562212016-11-25 16:10:05 +01002995 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002996 sample_register_convs(&sample_conv_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002997 cli_register_kw(&cli_kws);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002998}